0

I'm trying to send a notification via a different action other than create. My Article model has a published boolean attribute/column, and currently I have it so it only sends a notification when an article is created AND published is true.

Here's my article.rb:

  def activity_actor
    user
  end

  def activity_object
    self
  end

  def activity_notify
    return if user.followers.empty?
    user.followers.map(&:id).map do |user_id|
      StreamRails.feed_manager.get_notification_feed(user_id)
    end
  end

  def activity_should_sync?
    published
  end

  def activity_target
    return "article_#{Time.now}"
  end

I would still like the original functionality (article is created and published is true), but I would also like a notification sent when an article is already created and published is updated to true. Therefore, I'd need to send a notification via the update action.

There doesn't seem to be an obvious way to manually send notifications or to control the trigger that sends notifications. Any help would be appreciated!

PandyZhao
  • 137
  • 1
  • 11

1 Answers1

0

You'll need to manage this manually within your update action, I think.

feed = StreamRails.feed_manager.get_user_feed will return an instance of Stream::Feed which you can then call feed.add_activity(activity_data). If you send the same foreign_id and time as the original activity, we'll perform an upsert and update the original activity. This does not trigger a new notification if you're updating a previous activity though, so you'd need to manually send that activity to those other notification fields as well.

iandouglas
  • 4,146
  • 2
  • 33
  • 33
  • Hey Ian, thanks for the answer. I don't think I need to update an original activity, so that's good news. I'm simply creating a new activity whenever `published` is set to true. From what I'm understanding it would make sense to add an activity to a user's feed via `get_user_feed`, but I actually don't have any users following other users' feeds. Therefore no one gets notifications from another user's feed. I would need to send a notification only to the followers via the notification feed and not via following a user's feed. – PandyZhao Jun 27 '17 at 18:54
  • I looked at the gem's library and I'm thinking the solution for this type of use-case would be in `sync_policies.rb`. If there was a third module called `SyncUpdate` where in the `self.included` method, it would have ` base.after_commit :add_to_feed, on: :update` instead of `on: :create`. Of course it would also have the `add_to_feed` method like the `SyncCreate` module. – PandyZhao Jun 27 '17 at 18:58
  • The issue with `on: :update` though would be that it would run every time something was updated. Not sure how to get around checking for a specific boolean attribute. Maybe `on: :update_attribute` would work? You would then have to either pass in the attribute that you're updating as a check. That seems like too much of a hack though. – PandyZhao Jun 27 '17 at 19:06
  • So if I understand your use case, you want to add activities to a feed which may or may not have this `published` flag, but activities which do have this `published` flag set you want to send another activity to a different feed? If so I think you still want to look at the `feed_manager` to get a reference to that other feed and add an activity as I've described. We offer no SDK/API control for that sort of check. – iandouglas Jun 27 '17 at 19:37
  • Ah I see, thanks anyway. I'll figure out a workaround. Basically, I want to add activities to specifically a notification feed when an article switches from `published == false` to `== true`. Or in more plain terms, when a user publishes an article, send notifications to all their followers saying they published an article. – PandyZhao Jun 27 '17 at 20:26