gpt4 book ai didi

ruby-on-rails - 使用 Mongoid 的 MongoDB 对话/私有(private)消息模式

转载 作者:IT老高 更新时间:2023-10-28 13:29:46 28 4
gpt4 key购买 nike

我正在 Rails 中构建一个论坛系统,以便更加熟悉 Rails 和 Mongoid。我想添加的一个功能是一个私有(private)消息系统论坛用户可以用来互相发送消息。在架构设计方面,我可以想到两种解决方案:

解决方案 1

用户和消息是使用“has_many”和“belongs_to”相互链接的单独文档。

User document

has_many :messages_sent, :class_name => 'Message', :inverse_of => :message_sender

has_many :messages_received, :class_name => 'Message', :inverse_of => :message_recipient

Message Document

field :created, type: DateTime, default: -> { Time.now }

field :content, type: String

belongs_to :message_sender, :class_name => 'User', :inverse_of => :messages_sent

belongs_to :message_recipient, :class_name => 'User', :inverse_of => :messages_received

为了向用户显示他的收件箱,我会查看由 :created 排序的 some_user.messages_received 并过滤,因此我有一个唯一发件人 ID 的列表,由他们最后一条消息发送到 some_user 的时间。

然后为了显示一个特定的对话,我只需获取两个参与者之间的所有消息并根据时间戳将它们交错:

messages_in = some_user.messages_received.where(:message_sender => selected_correspondent)

messages_out = some_user.messages_sent.where(:message_recipient => selected_correspondent).

我不喜欢这种解决方案,因为它涉及多次使用“where”查询来访问 Messages 集合,并且需要对发送和接收的消息进行大量手动过滤和交错处理。努力。

解决方案 2(我现在正在使用)

在对话文档中嵌入消息。我将在下面提供用户、消息和对话的代码。一个对话通过 has_and_belongs_to_many 链接到两个或多个用户(n-n,因为一个用户也可能有许多对话)。这也可能允许多用户对话。

我喜欢这个解决方案,因为为了向用户显示他的收件箱,我可以使用由 :last_message_received 排序的 some_user.conversations 在对话文档中存储和更新,不需要过滤。为了显示特定的对话,我不需要交错发送和接收的消息,因为消息已经以正确的顺序嵌入到对话文档中。

此解决方案的唯一问题是在您要添加消息时找到由两个(或更多)用户共享的正确对话文档。这里建议一种解决方案:mongodb conversation system ,但我不喜欢它,因为查询似乎相对昂贵,并且多用户对话的扩展看起来会变得棘手。相反,我在对话文档中有一个名为 :lookup_hash 的字段,它是根据参与对话的每个用户的对象 ID 计算的 SHA1 哈希。这样,给定两个或更多用户,找到他们对应的对话文档(或者如果它还不存在就创建它)是很容易的。

要将消息添加到对话中,我只需使用 Conversation.add_message(类方法,而不是实例方法,因为对话可能还不存在)给它一个发送者、接收者和新的消息对象。

问题

我的问题是:考虑到 Mongoid(或者一般来说只是 NoSQL)架构设计最佳实践,我是否做任何明显错误的事情?我能做些什么来改进我的解决方案吗?我使用散列查找对话的想法是个坏主意吗?

代码

user.rb

class User
include Mongoid::Document

field :username, type: String
field :joined, type: DateTime, default: ->{ Time.now }
field :last_activity, type: DateTime, default: -> { Time.now }

has_and_belongs_to_many :conversations
end

conversation.rb

require 'digest/sha1'

class Conversation
include Mongoid::Document

field :lookup_hash, type: String
field :created, type: DateTime, default: -> { Time.now }
field :last_message_time, type: DateTime, default: -> { Time.now }
# Array of user ids of users that have read all messages in this conversation
field :last_message_seen_by, type: Array, default: []

embeds_many :messages
has_and_belongs_to_many :participants, :class_name => 'User'

validates_presence_of :lookup_hash

index({ lookup_hash: 1 }, { unique: true, name: "lookup_hash_index" })
# Used to show a user a list of conversations ordered by last_message_time
index({ _id: 1, last_message_time: -1 }, { unique: true, name: "id_last_message_time_index" })

def self.add_message(recipient, sender, message)
# Find or create a conversation:
conversation = Conversation.find_or_create_by(
:lookup_hash => get_lookup_hash([recipient.id, sender.id])) do |c|
c.participants.concat [recipient, sender]
end
conversation.messages << message
conversation.last_message_time = Time.now
conversation.last_message_seen_by.delete(recipient)
conversation.save
end

private
def self.get_lookup_hash(participant_ids)
lookup_key = participant_ids.sort.join(':')
Digest::SHA1.hexdigest lookup_key
end
end

message.rb

class Message
include Mongoid::Document

field :created, type: DateTime, default: -> { Time.now }
field :text, type: String

embedded_in :conversation
belongs_to :author, :class_name => 'User'

validates_length_of :text, minimum: 2, maximum: 256
validates_presence_of :author
end

最佳答案

我了解到您使用的是 MongoId 3.0。我在您的第一个解决方案中没有发现任何问题:

messages_in = some_user.messages_received.where(:message_sender => current_user)

messages_out = some_user.messages_sent.where(:message_recipient => current_user).

您可以找到各种示例:

Preferred way to private messages modeling in Rails 3

http://pastebin.com/fKavivbp

https://groups.google.com/forum/?fromgroups=#!topic/mongoid/BOBqhYLb7O0

我在几个项目中使用 MongoId 有一个内部消息传递系统,并使用第一个解决方案。

如果你添加了其他类"Conversation"你不应该嵌入消息,因为一个 session 可以有无限数量的消息。你应该使用 has_may messagesbelongs_to conversation

我认为这两种解决方案都很好,因此您可以根据项目逻辑选择您的需求。如果您的逻辑更简单,您可以选择第一个解决方案。否则,如果您的逻辑更复杂,请选择后一种解决方案。

问候!

关于ruby-on-rails - 使用 Mongoid 的 MongoDB 对话/私有(private)消息模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14499180/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com