Search code examples
ruby-on-railsgetstream-io

Adding an activity adds a user object as a string instead of the actual object


I'm currently integrating a stream_rails notification feed into an existing app. I'm trying to add an activity when a user follows another user. Pretty straightforward, but for some reason when I call add_activity on the followed_user's feed, it adds the information as a string.

Here's my activity_data:

  activity_data = {
    actor: current_user,
    verb: 'Follow',
    object: followable,
    to: ["notifications:#{followable.id}"]
  }

Here's the activity_response, shortened for brevity:

activity_response = {"actor"=>
  "{u'twitter_followers_count': 0, u'github_created_at': blahblahblah}", # other attributes removed for brevity
 "duration"=>"41ms",
 "foreign_id"=>nil,
 "id"=>"538bc690-51df-11e7-8080-8001521ad36e",
 "object"=>
  "{u'twitter_followers_count': None, u'github_created_at': blahblahblah}", # other attributes removed for brevity
 "origin"=>nil,
 "target"=>nil,
 "time"=>"2017-06-15T15:28:47.931560",
 "to"=>[["notifications:301", "DVcbej_Id4IdQWHOYlA1n7RB4ps"]],
 "verb"=>"Follow"}

When it should be more like:

activity_response = {"actor"=>
      #<User:0x007fa8fae1a488>,
     "duration"=>"41ms",
    # etc etc
    }

Not too sure why actor points to a string of the actual ActiveRecord object's return value, and the same with object. There's also a bunch of added 'u''s before each attribute.

Also, I avoided using StreamRails.feed_manager.follow_user because I only want to send a notification, not create a feed for the follower to see. Appreciate any help. Thanks!


Solution

  • Never mind, figured out one solution; although I do have more questions...

    Anyway, back to the answer. I suppose the stream-ruby gem's add_activity gem string-interpolates data's actor and object's values.

    Using stream-rails to send a notification by adding the activity_notify and activity_object methods in my Follow model allowed it to properly send a notification to the person who was followed.

    Example:

    1. Andy wants to follow John.
    2. Andy clicks the Follow button.
    3. Follow model (which is a join table) creates a new relationship between Andy and John.
    4. Follow model, which has stream-rails methods of activity_notify and activity_object sends a notification to John's notification feed.
    5. John receives a notification, saying Andy has followed John.

    Follow model:

      include StreamRails::Activity
      as_activity
    
      def activity_notify
        [StreamRails.feed_manager.get_notification_feed(self.followable.id)]
      end
    
      def activity_object
        self.followable
      end