Search code examples
django-rest-frameworkgetstream-io

How to get only Tweet (only posts) from the timeline?


Whenever tweet is created, it's activity is added to getStream production app.

class Tweet(models.Model, Activity):
    user = models.ForeignKey()
    text = models.CharField()

class Follow(models.Model, Activity): <- This is adding new activity to the timeline

def follow_feed_lisnner(~)
signal.post_save.connect(~)

class Like(models.Model, Activity): <- Like is adding to activity so timeline automatically shows who liked this post, 

My Expectation:

Feed: only shows Tweet on timeline (I don't want to see who started to follow me, or liked any post) - Just Like Instagram!

Notification: Who started to follow me, Who liked my post, Who commented on my post.

views.py

feeds = feed_manager.get_news_feeds(request.user.id)
# get the newsfeed for user. 
activities = feeds.get('timeline').get()['results']
activities = enricher.enrich_activities(activities)

Possible Solutions

  • Use python-stream (more low level) to deal with this problem. (I don't know if it helps)
  • Maybe I'm missing a cool feature of stream-django

How can we get only Tweet (Not Like, Follow or other activities which should be in notification) on the timeline?

Thank you

UPDATE

If I understood correctly, this should work. Is this valid?

class Follow(models.Model, Activity):
    follower = 
    following

    @property
    def activity_author_feed(self):
        return 'notification' 

Activity 1: user A follows user B. 
Activity 1 goes to 'user' feed + 'notification' feed (not timeline feed) 
//notification feed name already exists so I don't need to create follow feed group

Activity 2: user B creates Post
Activity 2 goes to 'user' feed + 'timeline' feed

Solution

  • Note: I'm assuming your Follow and Like models have a "user" field. If not, best update the question with the full Model classes and also confirm if you're setting up any other following relationships.

    The stream-django integration provides an 'Activity' model Mixin and the FeedManager model Manager. They work together to add activities to a Feed Group and Feed whose unique "feed id" is derived from the Model instance.

    By default, the feed id is determined by the application wide settings.USER_FEED setting. That should work well for your Tweet model but is probably not what you want for the Follow and Like models. The activities associated with those models ideally belong in separate feeds. This can be setup by overriding the Activity.activity_author_feed property function.

    class Follow(models.Model, Activity):
        # snipping fields
    
        @property
        def activity_author_feed(self):
            return 'Follow' # Must match a Feed Group defined in the Stream dashboard
    
        @property
        def activity_actor_attr(self):
            return self.author
    

    To have to those activities copied into the notification feed, implement the Activity.activity_notify() function to return a list of target feeds.

        @property
        def activity_notify(self):
            return [feed_manager.get_notification_feed(self.user.id)]