2021-03-23 01:39:16 +00:00
|
|
|
""" access the activity streams stored in redis """
|
|
|
|
from django.dispatch import receiver
|
2021-03-23 04:11:23 +00:00
|
|
|
from django.db.models import signals, Q
|
2021-03-23 01:39:16 +00:00
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
from bookwyrm import models
|
|
|
|
from bookwyrm.redis_store import RedisStore, r
|
2021-03-23 01:39:16 +00:00
|
|
|
from bookwyrm.views.helpers import privacy_filter
|
|
|
|
|
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
class ActivityStream(RedisStore):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""a category of activity stream (like home, local, federated)"""
|
2021-03-23 01:39:16 +00:00
|
|
|
|
|
|
|
def stream_id(self, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""the redis key for this user's instance of this stream"""
|
2021-03-23 01:54:17 +00:00
|
|
|
return "{}-{}".format(user.id, self.key)
|
2021-03-23 01:39:16 +00:00
|
|
|
|
|
|
|
def unread_id(self, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""the redis key for this user's unread count for this stream"""
|
2021-03-23 01:54:17 +00:00
|
|
|
return "{}-unread".format(self.stream_id(user))
|
2021-03-23 01:39:16 +00:00
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
def get_rank(self, obj): # pylint: disable=no-self-use
|
2021-04-26 16:15:42 +00:00
|
|
|
"""statuses are sorted by date published"""
|
2021-04-05 18:05:37 +00:00
|
|
|
return obj.published_date.timestamp()
|
2021-04-02 17:44:30 +00:00
|
|
|
|
2021-03-23 01:39:16 +00:00
|
|
|
def add_status(self, status):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""add a status to users' feeds"""
|
2021-04-06 14:53:34 +00:00
|
|
|
# the pipeline contains all the add-to-stream activities
|
2021-04-05 18:05:37 +00:00
|
|
|
pipeline = self.add_object_to_related_stores(status, execute=False)
|
|
|
|
|
|
|
|
for user in self.get_audience(status):
|
2021-03-23 01:39:16 +00:00
|
|
|
# add to the unread status count
|
|
|
|
pipeline.incr(self.unread_id(user))
|
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
# and go!
|
2021-03-23 03:32:59 +00:00
|
|
|
pipeline.execute()
|
|
|
|
|
2021-03-23 04:11:23 +00:00
|
|
|
def add_user_statuses(self, viewer, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""add a user's statuses to another user's feed"""
|
2021-04-06 14:53:34 +00:00
|
|
|
# only add the statuses that the viewer should be able to see (ie, not dms)
|
2021-04-05 18:05:37 +00:00
|
|
|
statuses = privacy_filter(viewer, user.status_set.all())
|
|
|
|
self.bulk_add_objects_to_store(statuses, self.stream_id(viewer))
|
2021-03-23 03:32:59 +00:00
|
|
|
|
2021-03-23 04:11:23 +00:00
|
|
|
def remove_user_statuses(self, viewer, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""remove a user's status from another user's feed"""
|
2021-04-06 14:53:34 +00:00
|
|
|
# remove all so that followers only statuses are removed
|
2021-04-05 18:05:37 +00:00
|
|
|
statuses = user.status_set.all()
|
|
|
|
self.bulk_remove_objects_from_store(statuses, self.stream_id(viewer))
|
2021-03-23 03:32:59 +00:00
|
|
|
|
2021-03-23 01:39:16 +00:00
|
|
|
def get_activity_stream(self, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""load the statuses to be displayed"""
|
2021-03-23 01:39:16 +00:00
|
|
|
# clear unreads for this feed
|
|
|
|
r.set(self.unread_id(user), 0)
|
|
|
|
|
2021-04-06 14:53:34 +00:00
|
|
|
statuses = self.get_store(self.stream_id(user))
|
2021-03-23 01:54:17 +00:00
|
|
|
return (
|
|
|
|
models.Status.objects.select_subclasses()
|
|
|
|
.filter(id__in=statuses)
|
2021-05-23 01:34:34 +00:00
|
|
|
.select_related("user", "reply_parent")
|
|
|
|
.prefetch_related("mention_books", "mention_users")
|
2021-03-23 01:54:17 +00:00
|
|
|
.order_by("-published_date")
|
|
|
|
)
|
2021-03-23 01:39:16 +00:00
|
|
|
|
2021-03-23 19:52:38 +00:00
|
|
|
def get_unread_count(self, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get the unread status count for this user's feed"""
|
2021-04-03 18:39:29 +00:00
|
|
|
return int(r.get(self.unread_id(user)) or 0)
|
2021-03-23 19:52:38 +00:00
|
|
|
|
2021-04-05 20:13:56 +00:00
|
|
|
def populate_streams(self, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""go from zero to a timeline"""
|
2021-04-06 14:53:34 +00:00
|
|
|
self.populate_store(self.stream_id(user))
|
2021-04-02 17:44:30 +00:00
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
def get_audience(self, status): # pylint: disable=no-self-use
|
2021-04-26 16:15:42 +00:00
|
|
|
"""given a status, what users should see it"""
|
2021-03-23 02:17:46 +00:00
|
|
|
# direct messages don't appeard in feeds, direct comments/reviews/etc do
|
2021-03-23 02:19:21 +00:00
|
|
|
if status.privacy == "direct" and status.status_type == "Note":
|
2021-03-26 19:09:37 +00:00
|
|
|
return []
|
2021-03-23 01:39:16 +00:00
|
|
|
|
|
|
|
# everybody who could plausibly see this status
|
|
|
|
audience = models.User.objects.filter(
|
|
|
|
is_active=True,
|
2021-03-23 01:54:17 +00:00
|
|
|
local=True, # we only create feeds for users of this instance
|
2021-03-23 01:39:16 +00:00
|
|
|
).exclude(
|
|
|
|
Q(id__in=status.user.blocks.all()) | Q(blocks=status.user) # not blocked
|
|
|
|
)
|
|
|
|
|
2021-03-23 21:59:51 +00:00
|
|
|
# only visible to the poster and mentioned users
|
|
|
|
if status.privacy == "direct":
|
|
|
|
audience = audience.filter(
|
|
|
|
Q(id=status.user.id) # if the user is the post's author
|
|
|
|
| Q(id__in=status.mention_users.all()) # if the user is mentioned
|
|
|
|
)
|
2021-03-23 01:39:16 +00:00
|
|
|
# only visible to the poster's followers and tagged users
|
2021-03-23 21:59:51 +00:00
|
|
|
elif status.privacy == "followers":
|
2021-03-23 01:39:16 +00:00
|
|
|
audience = audience.filter(
|
|
|
|
Q(id=status.user.id) # if the user is the post's author
|
2021-03-23 01:54:17 +00:00
|
|
|
| Q(following=status.user) # if the user is following the author
|
2021-03-23 01:39:16 +00:00
|
|
|
)
|
2021-03-29 00:53:52 +00:00
|
|
|
return audience.distinct()
|
2021-03-23 01:39:16 +00:00
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
def get_stores_for_object(self, obj):
|
|
|
|
return [self.stream_id(u) for u in self.get_audience(obj)]
|
|
|
|
|
|
|
|
def get_statuses_for_user(self, user): # pylint: disable=no-self-use
|
2021-04-26 16:15:42 +00:00
|
|
|
"""given a user, what statuses should they see on this stream"""
|
2021-03-23 01:39:16 +00:00
|
|
|
return privacy_filter(
|
|
|
|
user,
|
|
|
|
models.Status.objects.select_subclasses(),
|
2021-03-23 01:54:17 +00:00
|
|
|
privacy_levels=["public", "unlisted", "followers"],
|
2021-03-23 01:39:16 +00:00
|
|
|
)
|
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
def get_objects_for_store(self, store):
|
2021-04-05 18:10:26 +00:00
|
|
|
user = models.User.objects.get(id=store.split("-")[0])
|
2021-04-05 18:05:37 +00:00
|
|
|
return self.get_statuses_for_user(user)
|
|
|
|
|
2021-03-23 01:39:16 +00:00
|
|
|
|
|
|
|
class HomeStream(ActivityStream):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""users you follow"""
|
2021-03-23 01:54:17 +00:00
|
|
|
|
|
|
|
key = "home"
|
2021-03-23 01:39:16 +00:00
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
def get_audience(self, status):
|
|
|
|
audience = super().get_audience(status)
|
2021-03-29 20:07:22 +00:00
|
|
|
if not audience:
|
|
|
|
return []
|
2021-03-23 01:39:16 +00:00
|
|
|
return audience.filter(
|
|
|
|
Q(id=status.user.id) # if the user is the post's author
|
2021-03-23 01:54:17 +00:00
|
|
|
| Q(following=status.user) # if the user is following the author
|
2021-03-29 00:53:52 +00:00
|
|
|
).distinct()
|
2021-03-23 01:39:16 +00:00
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
def get_statuses_for_user(self, user):
|
2021-03-23 01:39:16 +00:00
|
|
|
return privacy_filter(
|
|
|
|
user,
|
|
|
|
models.Status.objects.select_subclasses(),
|
2021-03-23 01:54:17 +00:00
|
|
|
privacy_levels=["public", "unlisted", "followers"],
|
|
|
|
following_only=True,
|
2021-03-23 01:39:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class LocalStream(ActivityStream):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""users you follow"""
|
2021-03-23 01:54:17 +00:00
|
|
|
|
|
|
|
key = "local"
|
2021-03-23 01:39:16 +00:00
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
def get_audience(self, status):
|
2021-03-23 01:39:16 +00:00
|
|
|
# this stream wants no part in non-public statuses
|
2021-03-23 21:59:51 +00:00
|
|
|
if status.privacy != "public" or not status.user.local:
|
2021-03-26 19:09:37 +00:00
|
|
|
return []
|
2021-04-05 18:05:37 +00:00
|
|
|
return super().get_audience(status)
|
2021-03-23 01:39:16 +00:00
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
def get_statuses_for_user(self, user):
|
2021-03-23 01:39:16 +00:00
|
|
|
# all public statuses by a local user
|
|
|
|
return privacy_filter(
|
|
|
|
user,
|
|
|
|
models.Status.objects.select_subclasses().filter(user__local=True),
|
|
|
|
privacy_levels=["public"],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class FederatedStream(ActivityStream):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""users you follow"""
|
2021-03-23 01:54:17 +00:00
|
|
|
|
|
|
|
key = "federated"
|
2021-03-23 01:39:16 +00:00
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
def get_audience(self, status):
|
2021-03-23 01:39:16 +00:00
|
|
|
# this stream wants no part in non-public statuses
|
2021-03-23 01:54:17 +00:00
|
|
|
if status.privacy != "public":
|
2021-03-26 19:09:37 +00:00
|
|
|
return []
|
2021-04-05 18:05:37 +00:00
|
|
|
return super().get_audience(status)
|
2021-03-23 01:39:16 +00:00
|
|
|
|
2021-04-05 18:05:37 +00:00
|
|
|
def get_statuses_for_user(self, user):
|
2021-03-23 01:39:16 +00:00
|
|
|
return privacy_filter(
|
|
|
|
user,
|
|
|
|
models.Status.objects.select_subclasses(),
|
|
|
|
privacy_levels=["public"],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
streams = {
|
2021-03-23 01:54:17 +00:00
|
|
|
"home": HomeStream(),
|
|
|
|
"local": LocalStream(),
|
|
|
|
"federated": FederatedStream(),
|
2021-03-23 01:39:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 01:54:17 +00:00
|
|
|
|
2021-03-23 01:39:16 +00:00
|
|
|
@receiver(signals.post_save)
|
|
|
|
# pylint: disable=unused-argument
|
2021-03-23 04:11:23 +00:00
|
|
|
def add_status_on_create(sender, instance, created, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""add newly created statuses to activity feeds"""
|
2021-03-23 04:11:23 +00:00
|
|
|
# we're only interested in new statuses
|
2021-03-23 15:13:57 +00:00
|
|
|
if not issubclass(sender, models.Status):
|
|
|
|
return
|
|
|
|
|
2021-03-23 15:53:28 +00:00
|
|
|
if instance.deleted:
|
2021-03-23 15:13:57 +00:00
|
|
|
for stream in streams.values():
|
2021-04-05 18:05:37 +00:00
|
|
|
stream.remove_object_from_related_stores(instance)
|
2021-03-23 01:39:16 +00:00
|
|
|
return
|
|
|
|
|
2021-03-23 15:53:28 +00:00
|
|
|
if not created:
|
|
|
|
return
|
|
|
|
|
2021-03-23 04:11:23 +00:00
|
|
|
# iterates through Home, Local, Federated
|
2021-03-23 01:39:16 +00:00
|
|
|
for stream in streams.values():
|
|
|
|
stream.add_status(instance)
|
2021-03-23 04:11:23 +00:00
|
|
|
|
|
|
|
|
2021-03-23 15:53:28 +00:00
|
|
|
@receiver(signals.post_delete, sender=models.Boost)
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def remove_boost_on_delete(sender, instance, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""boosts are deleted"""
|
2021-03-23 15:53:28 +00:00
|
|
|
# we're only interested in new statuses
|
|
|
|
for stream in streams.values():
|
2021-04-05 18:05:37 +00:00
|
|
|
stream.remove_object_from_related_stores(instance)
|
2021-03-23 15:53:28 +00:00
|
|
|
|
|
|
|
|
2021-03-23 04:11:23 +00:00
|
|
|
@receiver(signals.post_save, sender=models.UserFollows)
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def add_statuses_on_follow(sender, instance, created, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""add a newly followed user's statuses to feeds"""
|
2021-03-23 15:53:28 +00:00
|
|
|
if not created or not instance.user_subject.local:
|
|
|
|
return
|
2021-03-23 04:11:23 +00:00
|
|
|
HomeStream().add_user_statuses(instance.user_subject, instance.user_object)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(signals.post_delete, sender=models.UserFollows)
|
|
|
|
# pylint: disable=unused-argument
|
2021-04-05 19:11:49 +00:00
|
|
|
def remove_statuses_on_unfollow(sender, instance, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""remove statuses from a feed on unfollow"""
|
2021-03-23 15:53:28 +00:00
|
|
|
if not instance.user_subject.local:
|
|
|
|
return
|
2021-03-23 04:11:23 +00:00
|
|
|
HomeStream().remove_user_statuses(instance.user_subject, instance.user_object)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(signals.post_save, sender=models.UserBlocks)
|
|
|
|
# pylint: disable=unused-argument
|
2021-04-05 19:11:49 +00:00
|
|
|
def remove_statuses_on_block(sender, instance, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""remove statuses from all feeds on block"""
|
2021-03-23 04:11:23 +00:00
|
|
|
# blocks apply ot all feeds
|
2021-03-23 15:53:28 +00:00
|
|
|
if instance.user_subject.local:
|
|
|
|
for stream in streams.values():
|
|
|
|
stream.remove_user_statuses(instance.user_subject, instance.user_object)
|
|
|
|
|
|
|
|
# and in both directions
|
|
|
|
if instance.user_object.local:
|
|
|
|
for stream in streams.values():
|
|
|
|
stream.remove_user_statuses(instance.user_object, instance.user_subject)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(signals.post_delete, sender=models.UserBlocks)
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def add_statuses_on_unblock(sender, instance, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""remove statuses from all feeds on block"""
|
2021-03-23 15:53:28 +00:00
|
|
|
public_streams = [LocalStream(), FederatedStream()]
|
|
|
|
# add statuses back to streams with statuses from anyone
|
|
|
|
if instance.user_subject.local:
|
|
|
|
for stream in public_streams:
|
|
|
|
stream.add_user_statuses(instance.user_subject, instance.user_object)
|
|
|
|
|
|
|
|
# add statuses back to streams with statuses from anyone
|
|
|
|
if instance.user_object.local:
|
|
|
|
for stream in public_streams:
|
|
|
|
stream.add_user_statuses(instance.user_object, instance.user_subject)
|
2021-03-23 14:01:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(signals.post_save, sender=models.User)
|
|
|
|
# pylint: disable=unused-argument
|
2021-04-03 17:56:53 +00:00
|
|
|
def populate_streams_on_account_create(sender, instance, created, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""build a user's feeds when they join"""
|
2021-03-23 14:01:49 +00:00
|
|
|
if not created or not instance.local:
|
|
|
|
return
|
|
|
|
|
|
|
|
for stream in streams.values():
|
2021-04-05 18:05:37 +00:00
|
|
|
stream.populate_streams(instance)
|