防止可操作的订户将消息提交到聊天室,使其不属于Rails

问题描述 投票:0回答:1

[如果我的应用使用AJAX将用户的message提交到不需要控制器的服务器端speak方法(因此也没有空间写入before_filter),那么我将如何实现限制[ users谁不属于通过浏览器控制台提交消息的那个conversation

如果我打开我的应用程序并键入App.messages.speak("hi", #guess random number#),则无论消息是否属于该消息,都会将该消息提交到对话中。>>

messages_channel.rb

class MessagesChannel < ApplicationCable::Channel
 def speak(data)
  Message.create! body: data['body'], conversation_id: data['conversation_id'], user_id: current_user.id
 end
end

messages.coffee

App.messages = App.cable.subscriptions.create "MessagesChannel", 
 speak: (body, conversation_id) ->
  @perform 'speak', body: body, conversation_id: conversation_id

 submit_message = () ->
 $('#response').on 'keydown', (event) ->
  if event.keyCode is 13
    conversation_id = $("[data-conversation-id]").data("conversation-id")
    App.messages.speak(event.target.value, conversation_id)
    event.target.value = ""
    event.preventDefault()

我以为我可以从conversation_id方法中删除speak参数,并将其设置在不依赖用户输入的其他位置?目前,我已将其设置为数据属性,这意味着人们仍然可以使用它。

如果我的应用程序使用AJAX将用户的消息提交到不需要控制器的服务器端语音方法(因此也没有空间来编写before_filter,那么我将如何实现......>

ruby-on-rails actioncable
1个回答
0
投票
通过回调解决。

class Message < ApplicationRecord before_create :exclude_non_participants def exclude_non_participants if conversation.participates?(user) true else throw :abort end end end

© www.soinside.com 2019 - 2024. All rights reserved.