Repository: OkunaOrg/okuna-api

Issue:
This post is private error ocassionally for couple people
Multiple reports of people seeing a this post is private error across the network.

I believe when we updated the timeline posts query, we should have updated it on the `can_see_post_with_id` too.


Discussion:

Resolving patch:
diff --git a/openbook_auth/models.py b/openbook_auth/models.py
--- a/openbook_auth/models.py
+++ b/openbook_auth/models.py
@@ -1366,7 +1366,8 @@ def _get_timeline_posts_with_filters(self, max_id=None, circles_ids=None, lists_
 
             followed_user_circles_query = Q(circles__id=world_circle_id)
 
-            followed_user_circles_query.add(Q(circles__connections__target_user_id=self.pk, ), Q.OR)
+            followed_user_circles_query.add(Q(circles__connections__target_user_id=self.pk,
+                                              circles__connections__target_connection__circles__isnull=False), Q.OR)
 
             followed_user_query.add(followed_user_circles_query, Q.AND)
 
@@ -1402,7 +1403,8 @@ def _get_timeline_posts_with_no_filters(self, max_id=None):
 
             followed_user_circles_query = Q(circles__id=world_circle_id)
 
-            followed_user_circles_query.add(Q(circles__connections__target_user_id=self.pk, ), Q.OR)
+            followed_user_circles_query.add(Q(circles__connections__target_user_id=self.pk,
+                                              circles__connections__target_connection__circles__isnull=False), Q.OR)
 
             followed_user_query.add(followed_user_circles_query, Q.AND)
 


Test patch:
diff --git a/openbook_posts/tests/views/test_posts.py b/openbook_posts/tests/views/test_posts.py
--- a/openbook_posts/tests/views/test_posts.py
+++ b/openbook_posts/tests/views/test_posts.py
@@ -886,5 +886,115 @@ def test_retrieves_own_posts_of_own_filtered_circle(self):
         for post_id in posts_ids:
             self.assertIn(post_id, response_posts_ids)
 
+    def test_can_retrieve_encircled_posts_of_confirmed_connection(self):
+        """
+        should be able to retrieve the encircled posts of a confirmed connection
+        """
+        user = make_user()
+        user_to_connect_to = make_user()
+
+        user.connect_with_user_with_id(user_id=user_to_connect_to.pk)
+
+        user_to_connect_to_circle = make_circle(creator=user_to_connect_to)
+        user_to_connect_to.confirm_connection_with_user_with_id(user_id=user.pk,
+                                                                circles_ids=[user_to_connect_to_circle.pk])
+
+        post = user_to_connect_to.create_encircled_post(text=make_fake_post_text(),
+                                                        circles_ids=[user_to_connect_to_circle.pk])
+
+        url = self._get_url()
+        headers = make_authentication_headers_for_user(user)
+
+        response = self.client.get(url, **headers)
+
+        self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+        response_posts = json.loads(response.content)
+
+        self.assertEqual(1, len(response_posts))
+
+        response_post = response_posts[0]
+
+        self.assertEqual(response_post['id'], post.pk)
+
+    def test_cant_retrieve_encircled_posts_of_unconfirmed_connection(self):
+        """
+        should not be able to retrieve encircled posts of unconfirmed co
+        """
+        user = make_user()
+        user_to_connect_to = make_user()
+
+        user.connect_with_user_with_id(user_id=user_to_connect_to.pk)
+
+        user_to_connect_to_circle = make_circle(creator=user_to_connect_to)
+
+        user_to_connect_to.create_encircled_post(text=make_fake_post_text(),
+                                                 circles_ids=[user_to_connect_to_circle.pk])
+
+        url = self._get_url()
+        headers = make_authentication_headers_for_user(user)
+
+        response = self.client.get(url, **headers)
+
+        self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+        response_posts = json.loads(response.content)
+
+        self.assertEqual(0, len(response_posts))
+
+    def test_can_retrieve_posts_with_recent_unconfirmed_connection_encircled_post(self):
+        """
+        should be able to retrieve the timeline posts with an unconfirmed connection recent posts
+        https://github.com/OpenbookOrg/openbook-api/issues/301
+        """
+        user = make_user()
+
+        user_timeline_posts_amount = 5
+
+        posts_ids = []
+
+        for i in range(0, user_timeline_posts_amount):
+            foreign_user = make_user()
+            foreign_post = foreign_user.create_encircled_post(text=make_fake_post_text(),
+                                                              circles_ids=[foreign_user.connections_circle_id])
+            posts_ids.append(foreign_post.pk)
+            user.connect_with_user_with_id(user_id=foreign_user.pk)
+            foreign_user.confirm_connection_with_user_with_id(user_id=user.pk)
+
+        connection_requester_user = make_user()
+
+        connection_requester_user.connect_with_user_with_id(user_id=user.pk, circles_ids=[
+            connection_requester_user.connections_circle.pk])
+
+        user.confirm_connection_with_user_with_id(user_id=connection_requester_user.pk)
+
+        connection_requester_user.disconnect_from_user_with_id(user_id=user.pk)
+
+        connection_requester_user.connect_with_user_with_id(user_id=user.pk, circles_ids=[
+            connection_requester_user.connections_circle.pk])
+
+        connection_requester_user_circle = make_circle(creator=connection_requester_user)
+        connection_requester_user.update_connection_with_user_with_id(user_id=user.pk,
+                                                                      circles_ids=[connection_requester_user_circle.pk])
+
+        connection_requester_user.create_encircled_post(text=make_fake_post_text(),
+                                                        circles_ids=[connection_requester_user_circle.pk])
+
+        url = self._get_url()
+        headers = make_authentication_headers_for_user(user)
+
+        response = self.client.get(url, **headers)
+
+        self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+        response_posts = json.loads(response.content)
+
+        self.assertEqual(len(posts_ids), len(response_posts))
+
+        response_posts_ids = [post['id'] for post in response_posts]
+
+        for post_id in posts_ids:
+            self.assertIn(post_id, response_posts_ids)
+
     def _get_url(self):
         return reverse('posts')