Как использовать ActionCable broadcast_to с указанием c группы пользователей? - PullRequest
1 голос
/ 01 мая 2020

У меня есть базовая c настройка с rails API + devise_token_auth + actionable и angular9 app для Front.

Я в основном хочу отправлять уведомления определенной c группе пользователей, а не всем пользователям.

Мой код выглядит следующим образом:

connections.rb

module ApplicationCable
 class Connection < ActionCable::Connection::Base
  identified_by :current_user

def connect
  self.current_user = find_verified_user
end

private
def find_verified_user
  uid = request.query_parameters[:uid]
  token = request.query_parameters[:token]
  client_id = request.query_parameters[:client]

  user = User.find_by_uid(uid)

  if user && user.valid_token?(token, client_id)
    user
  else
    reject_unauthorized_connection
  end
end
end
end

назначения. рб

class Appointment < ApplicationRecord
validates_uniqueness_of :start_time
belongs_to :slug
belongs_to :user
belongs_to :property
belongs_to :contact


after_create do
  ChangeAppointmentsJob.perform_later self
end

after_update do
  ChangeAppointmentsJob.perform_later self
end
end

Изменение назначений Работа

class ChangeAppointmentsJob < ApplicationJob
  queue_as :default

  def perform(appointment)

    ActionCable.server.broadcast 'appointment_channel', data: appointment
  end

end

Назначение канала

class AppointmentChannel < ApplicationCable::Channel
  def subscribed
    stream_from 'appointment_channel'
  end

  def unsubscribed
    stop_all_streams
  end
end

Я хочу сделать что-то подобное

ActionCable.server.broadcast_to( User.where(slug_id: current_user.slug_id) ) 

, чтобы отправить уведомление в мое приложение Front, но не всем пользователям Только пользователи, у которых slug_id , как и пользователь, создавший или обновивший встречу

любые идеи, пожалуйста!

1 Ответ

0 голосов
/ 03 мая 2020

Я нашел решение

Я отправляю slug_id с uid, access_token и клиентом для подписки на канал и использую его в connection.rb вот так

module ApplicationCable
  class Connection < ActionCable::Connection::Base
  identified_by :current_user

def connect
  self.current_user = find_verified_user
end

private
def find_verified_user
  uid = request.query_parameters[:uid]
  token = request.query_parameters[:token]
  client_id = request.query_parameters[:client]
  slug_id = request.query_parameters[:slug_id]
  user = User.where(slug_id: slug_id).find_by_uid(uid)  <-------- get all users with the same slug_id :)

  if user && user.valid_token?(token, client_id)
    user
  else
    reject_unauthorized_connection
  end
 end
end
end
...