justquick / django-activity-stream

Generate generic activity streams from the actions on your site. Users can follow any actors' activities for personalized streams.
http://django-activity-stream.rtfd.io/en/latest/
BSD 3-Clause "New" or "Revised" License
2.38k stars 482 forks source link

Is there a way to filter actions after user has started following specific user/object? (i.e. ignore events before "follow" for user_stream) #481

Closed rollue closed 3 years ago

rollue commented 3 years ago

Currently user_stream seems to return all actions related to specific user/object. What I need is to filter out to get only actions that happened after someone has started following the user/object. For example, if a user started following a post to get notifications on new comments, I don't need to notify the user for actions related to already existing comments. This can probably be done with individual model streams, but is there a way to implement this with user_stream?

Thanks.

rollue commented 3 years ago

this is how I did it - by using Subquery

from django.db.models import OuterRef, Subquery
...

def get(self, request, *args, **kwargs):
    qs = ActionProxy.objects.user(request.user, with_user_activity=False)
    qs = qs.exclude(verb="started following")
    qs = qs.exclude(actor_object_id=request.user.id)
    follow = Follow.objects.filter(
        content_type_id=OuterRef("target_content_type_id"),
        object_id=OuterRef("target_object_id"),
        user=request.user,
    )
    qs = qs.annotate(followed_at=Subquery(follow.values("started")[:1])).filter(
        timestamp__gte=F("followed_at")
    )
    ser = FeedSerializer(qs, many=True, context={"request": request})
    return response.Response(ser.data)
auvipy commented 3 years ago

is it needed in the package?

rollue commented 3 years ago

I resorted to django-notifications instead of django-activity-stream.

I was implementing a notifications API for my app. The problem with django-activity-stream was that once a user has unfollowed an object, all notifications regarding that objects disappeared from the API, regardless of WHEN the user unfollowed that object.

I needed to make sure some of the notifications(or actions) - which were created between the timeframe when the user started following and decided to unfollow - were still visible to the user. This was very hard to implement with django-activity-stream.