Python formatting

This commit is contained in:
Mouse Reeve 2021-03-22 18:54:17 -07:00
parent 2fe9d1044a
commit 0caea7e9ff
2 changed files with 36 additions and 33 deletions

View file

@ -18,11 +18,11 @@ class ActivityStream(ABC):
def stream_id(self, user): def stream_id(self, user):
""" the redis key for this user's instance of this stream """ """ the redis key for this user's instance of this stream """
return '{}-{}'.format(user.id, self.key) return "{}-{}".format(user.id, self.key)
def unread_id(self, user): def unread_id(self, user):
""" the redis key for this user's unread count for this stream """ """ the redis key for this user's unread count for this stream """
return '{}-unread'.format(self.stream_id(user)) return "{}-unread".format(self.stream_id(user))
def add_status(self, status): def add_status(self, status):
""" add a status to users' feeds """ """ add a status to users' feeds """
@ -38,94 +38,93 @@ class ActivityStream(ABC):
# and go! # and go!
pipeline.execute() pipeline.execute()
def get_value(self, status): # pylint: disable=no-self-use def get_value(self, status): # pylint: disable=no-self-use
""" the status id and the rank (ie, published date) """ """ the status id and the rank (ie, published date) """
return {status.id: status.published_date.timestamp()} return {status.id: status.published_date.timestamp()}
def get_activity_stream(self, user): def get_activity_stream(self, user):
""" load the ids for statuses to be displayed """ """ load the ids for statuses to be displayed """
# clear unreads for this feed # clear unreads for this feed
r.set(self.unread_id(user), 0) r.set(self.unread_id(user), 0)
statuses = r.zrevrange(self.stream_id(user), 0, -1) statuses = r.zrevrange(self.stream_id(user), 0, -1)
return models.Status.objects.select_subclasses().filter( return (
id__in=statuses models.Status.objects.select_subclasses()
).order_by('-published_date') .filter(id__in=statuses)
.order_by("-published_date")
)
def populate_stream(self, user): def populate_stream(self, user):
''' go from zero to a timeline ''' """ go from zero to a timeline """
pipeline = r.pipeline() pipeline = r.pipeline()
statuses = self.stream_statuses(user) statuses = self.stream_statuses(user)
stream_id = self.stream_id(user) stream_id = self.stream_id(user)
for status in statuses.all()[:settings.MAX_STREAM_LENGTH]: for status in statuses.all()[: settings.MAX_STREAM_LENGTH]:
pipeline.zadd(stream_id, self.get_value(status)) pipeline.zadd(stream_id, self.get_value(status))
pipeline.execute() pipeline.execute()
def stream_users(self, status): # pylint: disable=no-self-use def stream_users(self, status): # pylint: disable=no-self-use
""" given a status, what users should see it """ """ given a status, what users should see it """
# direct messages don't appeard in feeds. # direct messages don't appeard in feeds.
if status.privacy == 'direct': if status.privacy == "direct":
return None return None
# everybody who could plausibly see this status # everybody who could plausibly see this status
audience = models.User.objects.filter( audience = models.User.objects.filter(
is_active=True, is_active=True,
local=True # we only create feeds for users of this instance local=True, # we only create feeds for users of this instance
).exclude( ).exclude(
Q(id__in=status.user.blocks.all()) | Q(blocks=status.user) # not blocked Q(id__in=status.user.blocks.all()) | Q(blocks=status.user) # not blocked
) )
# only visible to the poster's followers and tagged users # only visible to the poster's followers and tagged users
if status.privacy == 'followers': if status.privacy == "followers":
audience = audience.filter( audience = audience.filter(
Q(id=status.user.id) # if the user is the post's author Q(id=status.user.id) # if the user is the post's author
| Q(following=status.user) # if the user is following the author | Q(following=status.user) # if the user is following the author
) )
return audience return audience
def stream_statuses(self, user): # pylint: disable=no-self-use def stream_statuses(self, user): # pylint: disable=no-self-use
""" given a user, what statuses should they see on this stream """ """ given a user, what statuses should they see on this stream """
return privacy_filter( return privacy_filter(
user, user,
models.Status.objects.select_subclasses(), models.Status.objects.select_subclasses(),
privacy_levels=["public", 'unlisted', 'followers'], privacy_levels=["public", "unlisted", "followers"],
) )
class HomeStream(ActivityStream): class HomeStream(ActivityStream):
""" users you follow """ """ users you follow """
key = 'home'
key = "home"
def stream_users(self, status): def stream_users(self, status):
audience = super().stream_users(status) audience = super().stream_users(status)
return audience.filter( return audience.filter(
Q(id=status.user.id) # if the user is the post's author Q(id=status.user.id) # if the user is the post's author
| Q(following=status.user) # if the user is following the author | Q(following=status.user) # if the user is following the author
| Q(id__in=status.mention_users.all()) # or the user is mentioned | Q(id__in=status.mention_users.all()) # or the user is mentioned
) )
def stream_statuses(self, user): def stream_statuses(self, user):
return privacy_filter( return privacy_filter(
user, user,
models.Status.objects.select_subclasses(), models.Status.objects.select_subclasses(),
privacy_levels=["public", 'unlisted', 'followers'], privacy_levels=["public", "unlisted", "followers"],
following_only=True following_only=True,
) )
class LocalStream(ActivityStream): class LocalStream(ActivityStream):
""" users you follow """ """ users you follow """
key = 'local'
key = "local"
def stream_users(self, status): def stream_users(self, status):
# this stream wants no part in non-public statuses # this stream wants no part in non-public statuses
if status.privacy != 'public': if status.privacy != "public":
return None return None
return super().stream_users(status) return super().stream_users(status)
@ -140,11 +139,12 @@ class LocalStream(ActivityStream):
class FederatedStream(ActivityStream): class FederatedStream(ActivityStream):
""" users you follow """ """ users you follow """
key = 'federated'
key = "federated"
def stream_users(self, status): def stream_users(self, status):
# this stream wants no part in non-public statuses # this stream wants no part in non-public statuses
if status.privacy != 'public': if status.privacy != "public":
return None return None
return super().stream_users(status) return super().stream_users(status)
@ -156,20 +156,23 @@ class FederatedStream(ActivityStream):
) )
streams = { streams = {
'home': HomeStream(), "home": HomeStream(),
'local': LocalStream(), "local": LocalStream(),
'federated': FederatedStream(), "federated": FederatedStream(),
} }
@receiver(signals.post_save) @receiver(signals.post_save)
# pylint: disable=unused-argument # pylint: disable=unused-argument
def update_feeds(sender, instance, created, *args, **kwargs): def update_feeds(sender, instance, created, *args, **kwargs):
""" add statuses to activity feeds """ """ add statuses to activity feeds """
# we're only interested in new statuses that aren't dms # we're only interested in new statuses that aren't dms
if not created or not issubclass(sender, models.Status) or \ if (
instance.privacy == 'direct': not created
or not issubclass(sender, models.Status)
or instance.privacy == "direct"
):
return return
for stream in streams.values(): for stream in streams.values():

View file

@ -28,7 +28,7 @@ class Feed(View):
page = 1 page = 1
if not tab in STREAMS: if not tab in STREAMS:
tab = 'home' tab = "home"
activities = activitystreams.streams[tab].get_activity_stream(request.user) activities = activitystreams.streams[tab].get_activity_stream(request.user)