问题描述
我正在使用Ruby on Rails和Mongoid
我需要MessagesController中的消息仅由发送消息和向其发送消息的用户读取,也就是说,实际上是两个用户之间的私人消息
我认为我需要通过scope或before_action进行操作,也许为此,我需要添加一个附加字段来存储两个用户的ID?
如果您能帮助我,我会很高兴
message.rb
class Message
include Mongoid::Document
include Mongoid::Timestamps::Created
field :body,type: String
field :read,type: Mongoid::Boolean,default: false
field :is_deleted,default: false
field :conversation_id,type: BSON::ObjectId
field :sender_id,type: BSON::ObjectId
field :receiver_id,type: BSON::ObjectId
belongs_to :conversation
belongs_to :sender,class_name: 'User',foreign_key: 'sender_id'
belongs_to :receiver,foreign_key: 'receiver_id'
validates_presence_of :body,:conversation_id,:sender_id,:receiver_id
index({ conversation_id: 1 },{ name: 'index_messages_on_conversation_id',background: true })
index({ user_id: 1 },{ name: 'index_messages_on_user_id',background: true })
def message_time
created_at.strftime("%d/%m/%y at %l:%M %p")
end
end
messages_controller.rb
class MessagesController < ApplicationController
before_action :authenticate_user!
before_action :set_conversation,only: [:index,:create]
before_action :correct_user,only: [:index]
def index
@conversation.messages.where(receiver_id: current_user.id,read: false).update_all(read: true)
@message = @conversation.messages.new
end
def create
@message = @conversation.messages.new(message_params.except(:recipient_ids))
@message.sender_id = current_user.id
@message.receiver_id = @conversation.recipient(current_user)
@message.save!
end
private
def message_params
params.require(:message).permit(:body)
end
def set_conversation
@conversation = Conversation.find(params[:conversation_id])
end
def correct_user
redirect_to root_path if @conversation.messages.any_of({sender_id: current_user.id},{receiver_id: current_user.id}) == current_user._id
end
end
conversation.rb
class Conversation
include Mongoid::Document
field :is_deleted,default: false
field :sender_id,type: Integer
field :receiver_id,type: Integer
belongs_to :sender,foreign_key: 'receiver_id'
has_many :messages,dependent: :destroy
validates_uniqueness_of :sender_id,scope: :receiver_id
scope :between,-> (sender_id,receiver_id) do
any_of({sender_id: sender_id,receiver_id: receiver_id},{sender_id: receiver_id,receiver_id: sender_id})
end
def recipient(current_user)
self.sender_id == current_user.id ? self.receiver : self.sender
end
end
解决方法
我通过简单的检查来确定当前用户是否不是此@conversation
的发送者和接收者,然后将用户重定向到根页面
class MessagesController < ApplicationController
before_action :correct_user,only: [:index]
private
def correct_user
redirect_to root_path unless @conversation.sender_id == current_user.id || @conversation.receiver_id == current_user.id
end
end