代码之家  ›  专栏  ›  技术社区  ›  mbajur

发送操作电缆消息时的当前用户上下文

  •  1
  • mbajur  · 技术社区  · 7 年前

    我有一个rails应用程序,它使用活动模型序列化器构建REST api。

    Post 返回的序列化程序 title 职位和 is_voted 具有 true false 值,具体取决于当前登录的用户是否对此报道进行了投票。

      attribute :is_voted do
        voteable_ids = current_user.account.votes.where(voteable_type: 'Post').map(&:voteable_id)
    
        voteable_ids.include? object.id
      end
    

    到目前为止一切都很好,但是当我想通知javascript客户机 岗位

    假设标题已更改,我想发送一个关于此的actioncable事件:

    ActionCable.server.broadcast 'updates_channel', {
      event: 'posts.updated',
      data: ActiveModelSerializers::SerializableResource.new(post).to_json
    }
    

    current_user 在这一点上,它是由后端触发的。或者基本上可以由其他用户触发。但如果我做得对,actioncable端点知道谁是 当前用户

    我在想-有没有可能在某种actioncable中间件中将当前的用户注入到序列化程序中?我可以使用 identified_by :current_user 但我不知道如何在这种情况下使用它。

    0 回复  |  直到 7 年前