Peter Boomsma
Peter Boomsma

Reputation: 9844

How to set up friends as recipients in public activity?

I have set up a friendship model for my app,

create_table "friendships", force: :cascade do |t|
  t.integer  "user_id"
  t.integer  "friend_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

And the model looks like this,

belongs_to :user
belongs_to :friend, :class_name => "User"

And this is my user model,

has_many :friendships
has_many :friends, :through => :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user

And I have tracked my movie model with the Public Activity gem,

include PublicActivity::Model
tracked owner: -> (controller, model) { controller && controller.current_user }
tracked :params => {
  :title => :get_title,
  :poster => :get_poster
}

has_and_belongs_to_many :users

The current situation is that when a user adds a activity to the app, every other user sees that notificiation. What I would like is to only send the activity to the friends of the user.

I think all the necessary data and links are preset, but I'm unsure on how to hook it all up.

Upvotes: 0

Views: 170

Answers (1)

qubit
qubit

Reputation: 769

I think you would have to do this through custom activity. For example,

When a new movie is created , you can do the create activity a callback method,

after_create :create_activity

def create_activity
   user.friends.each do |friend|
      self.create_activity action: :new , parameters: {title:title,poster:poster}, recipient: friend, owner: current_user
  end
end

however this solution is very inefficient. I am not sure if there is a method provided for the gem that you can create multiple activities all together, like ActiveRecord, Moview.create([{title:1},{title:2}]). Otherwise you many want to move this into a background job, something like sidekiq .

Upvotes: 1

Related Questions