def test_adds_with_local_user(self, mock_add, mock_enqueue): update_streams_with_content(self.remote_content) self.assertFalse(mock_add.called) update_streams_with_content(self.content) mock_add.assert_called_once_with( self.content, self.content, ["sh:streams:public:%s" % self.user.id])
def test_content_save_calls_streamconsumer_group_send__user_not_recently_active( self, mock_async): mock_send = Mock() mock_async.return_value = mock_send content = ContentFactory() update_streams_with_content(content) mock_send.assert_not_called()
def test_content_save_calls_streamconsumer_group_send__public_share_with_followers( self, mock_async): mock_send = Mock() mock_async.return_value = mock_send self.profile.following.add(self.remote_profile) other_user = PublicUserFactory() other_profile = ProfileFactory() content = ContentFactory(author=self.remote_profile) share = ContentFactory(content_type=ContentType.SHARE, share_of=content, author=other_profile) with patch("socialhome.users.models.User.recently_active", new_callable=mock.PropertyMock, return_value=True): update_streams_with_content(share) data = { "type": "notification", "payload": { "event": "new", "id": content.id } } calls = [ call(f"streams_profile_all__{share.author.id}__{self.user.id}", data), call(f"streams_profile_all__{share.author.id}__{other_user.id}", data), call(f"streams_followed__{self.user.id}", data), ] mock_send.assert_has_calls(calls, any_order=True) self.assertEqual(mock_send.call_count, 3)
def test_content_save_calls_streamconsumer_group_send__limited_tags_and_followers( self, mock_async): mock_send = Mock() mock_async.return_value = mock_send PublicUserFactory() self.profile.following.add(self.remote_profile) content = ContentFactory(author=self.remote_profile, visibility=Visibility.LIMITED, text="#foobar #barfoo") content.limited_visibilities.add(self.profile) with patch("socialhome.users.models.User.recently_active", new_callable=mock.PropertyMock, return_value=True): update_streams_with_content(content) data = { "type": "notification", "payload": { "event": "new", "id": content.id } } foobar_id = Tag.objects.get(name="foobar").id barfoo_id = Tag.objects.get(name="barfoo").id calls = [ call(f"streams_tag__{foobar_id}__{self.user.id}", data), call(f"streams_tag__{barfoo_id}__{self.user.id}", data), call(f"streams_profile_all__{content.author.id}__{self.user.id}", data), call(f"streams_followed__{self.user.id}", data), call(f"streams_limited__{self.user.id}", data), ] mock_send.assert_has_calls(calls, any_order=True) self.assertEqual(mock_send.call_count, 5)
def test_enqueues_each_stream_to_rq__share(self, mock_enqueue): update_streams_with_content(self.share) calls = [ call(add_to_stream_for_users, self.content.id, self.share.id, "FollowedStream", self.share.author.id), call(add_to_stream_for_users, self.content.id, self.share.id, "ProfileAllStream", self.share.author.id), call(add_to_stream_for_users, self.content.id, self.share.id, "TagsStream", self.share.author.id), ] self.assertEqual(mock_enqueue.call_args_list, calls)
def test_content_save_calls_streamconsumer_group_send__limited_no_followers( self, mock_async): mock_send = Mock() mock_async.return_value = mock_send content = ContentFactory(visibility=Visibility.LIMITED, text="#foobar #barfoo") update_streams_with_content(content) mock_send.assert_not_called()
def test_content_save_calls_streamconsumer_group_send__replies( self, mock_async): mock_send = Mock() mock_async.return_value = mock_send content = ContentFactory() reply = ContentFactory(parent=content) update_streams_with_content(reply) data = { "type": "notification", "payload": { "event": "new", "id": reply.id } } mock_send.assert_called_once_with( "streams_content__%s" % content.channel_group_name, data)
def content_post_save(instance, **kwargs): fetch_preview(instance) render_content(instance) if kwargs.get("created"): notify_listeners(instance) if instance.content_type == ContentType.REPLY: transaction.on_commit(lambda: django_rq.enqueue( send_reply_notifications, instance.id)) elif instance.content_type == ContentType.SHARE and instance.share_of.local: transaction.on_commit(lambda: django_rq.enqueue( send_share_notification, instance.id)) transaction.on_commit(lambda: update_streams_with_content(instance)) if instance.federate and instance.local: transaction.on_commit(lambda: federate_content(instance))
def test_content_save_calls_streamconsumer_group_send__public_no_tags_no_followers( self, mock_async): mock_send = Mock() mock_async.return_value = mock_send content = ContentFactory() with patch("socialhome.users.models.User.recently_active", new_callable=mock.PropertyMock, return_value=True): update_streams_with_content(content) data = { "type": "notification", "payload": { "event": "new", "id": content.id } } calls = [ call(f"streams_profile_all__{content.author.id}__{self.user.id}", data), call(f"streams_public__{self.user.id}", data), ] mock_send.assert_has_calls(calls, any_order=True) self.assertEqual(mock_send.call_count, 2)
def content_post_save(instance, **kwargs): # TODO remove extract mentions from here when we have UI for creating mentions if instance.local: instance.extract_mentions() fetch_preview(instance) render_content(instance) if kwargs.get("created"): notify_listeners(instance) if instance.content_type == ContentType.REPLY: transaction.on_commit(lambda: django_rq.enqueue(send_reply_notifications, instance.id)) elif instance.content_type == ContentType.SHARE and instance.share_of.local: transaction.on_commit(lambda: django_rq.enqueue(send_share_notification, instance.id)) transaction.on_commit(lambda: update_streams_with_content(instance)) if instance.federate and instance.local: transaction.on_commit(lambda: federate_content(instance))
def content_post_save(instance, **kwargs): # TODO remove extract mentions from here when we have UI for creating mentions if instance.local: instance.extract_mentions() fetch_preview(instance) render_content(instance) created = kwargs.get("created") if created: if instance.content_type == ContentType.REPLY: transaction.on_commit(lambda: django_rq.enqueue( send_reply_notifications, instance.id)) elif instance.content_type == ContentType.SHARE and instance.share_of.local: transaction.on_commit(lambda: django_rq.enqueue( send_share_notification, instance.id)) transaction.on_commit(lambda: update_streams_with_content(instance)) if instance.federate and instance.local: # Get an activity to be used when federating activity_type = ActivityType.CREATE if created else ActivityType.UPDATE activity = instance.create_activity(activity_type) transaction.on_commit( lambda: federate_content(instance, activity=activity))
def test_returns_if_reply(self): self.assertIsNone( update_streams_with_content(Mock(content_type=ContentType.REPLY)))
def test_enqueues_each_stream_to_rq(self, mock_enqueue): update_streams_with_content(self.content) mock_enqueue.assert_called_once_with(add_to_stream_for_users, self.content.id, "FollowedStream")
def test_returns_if_reply(self): self.assertIsNone(update_streams_with_content(Mock(content_type=ContentType.REPLY)))
def test_adds_with_local_user(self, mock_add, mock_enqueue): update_streams_with_content(self.remote_content) self.assertFalse(mock_add.called) update_streams_with_content(self.content) mock_add.assert_called_once_with(self.content, self.content, ["sh:streams:public:%s" % self.user.id])