实现facebook风格的消息传递系统,其中用户可以位于另一个模型的两个不同列中

kin*_*eat 6 ruby-on-rails ruby-on-rails-3.1

我正在尝试建立一个类似于facebook的消息系统,你有一个按两个用户之间的对话排序的消息列表(这对于目前的多个收件人来说并不重要,但也许如果我使用更智能的设计那么它可以将来很容易实现.我认为添加到我现在的东西并不容易.)我有一些工作,但我无法实现一些功能.我对rails/web编程很新,因此非常感谢任何和所有提示/提示/解决方案.

所以我有三个相关的模型:User,Conversation和Messages.用户有很多会话,Conversation有很多消息,属于Users,Message属于Conversation.好的,这就是模型的样子:

用户:具有相关字段ID:int,username:string

class User < ActiveRecord::Base

  has_many :conversations, :class_name => "Conversation", :finder_sql =>
            proc { 
            "SELECT * FROM conversations " +
            "WHERE conversations.user1_id = #{id} OR conversations.user2_id = #{id} " +
            "ORDER BY conversations.updated_at DESC" }
Run Code Online (Sandbox Code Playgroud)

会话:有相关字段:ID:int,user1_id:int,user2_id:int,user1_deleted:boolean,user2_deleted:boolean,created_at:datetime,updated_at:datetime

class Conversation < ActiveRecord::Base
  has_many :messages  

  belongs_to :participant_one, :class_name => "User", :foreign_key => :user1_id
  belongs_to :participant_two, :class_name => "User", :foreign_key => :user2_id

  private

    def self.between(user1, user2)
      c = Conversation.arel_table
      Conversation.where(c[:user1_id].eq(user1).and(c[:user2_id].eq(user2)).or(c[:user1_id].eq(user2).and(c[:user2_id].eq(user1))))
Run Code Online (Sandbox Code Playgroud)

消息:具有相关字段:id:int,conversation_id:int,author_id:int,content:text,created_at:datetime,updated_at:datetime

class Message < ActiveRecord::Base
  belongs_to :conversation, :touch => true  
Run Code Online (Sandbox Code Playgroud)

我不确定我是否需要participant_one和participant_two,但我使用了

  def conversation_partner(conversation)
    conversation.participant_one == current_user ? conversation.participant_two : conversation.participant_one
  end
Run Code Online (Sandbox Code Playgroud)

在ConversationHelper中,以便在视图中,我可以显示其他参与者.

所以这基本上有效.但我遇到的一个复杂问题是我在Conversation中并没有真正区分用户,用户可以在user1字段或user2字段中.所以我需要不断地在一个或另一个字段中查找用户,例如在User has_many声明的finder_sql中.此外,当我创建新消息时,我首先搜索是否存在Conversation参数,或者如果没有,则查看两个用户之间是否有对话,如果没有,则创建新对话.(您可以从会话索引发送消息(如回复),或者current_user可以查看另一个用户并单击"向该用户发送消息"链接.messagecontroller看起来像这样,并使用该自我.在Conversation模型中的方法之间:

class MessagesController < ApplicationController
  before_filter :get_user
  before_filter :find_or_create_conversation, :only => [:new, :create]

  def new
     @message = Message.new
  end

  def create
    @message = @conversation.messages.build(params[:message])
    @message.author_id = current_user.id
    if @message.save
      redirect_to user_conversation_path(current_user, @conversation), :notice => "Message sent!"
    else
      redirect_to @conversation
    end
  end

  private
     def get_user
      @user = User.find(params[:user_id])
    end

    def find_or_create_conversation
      if params[:conversation_id]
        @conversation = Conversation.find(params[:conversation_id]) 
      else
        @conversation = Conversation.between(@user.id, current_user.id).first or @conversation = Conversation.create!(:user1_id => current_user.id, :user2_id => @user.id)
      end
    end
Run Code Online (Sandbox Code Playgroud)

(我的路线看起来像这样:)

  resources :users do
    resources :conversations, :only => [:index, :create, :show, :destroy] do
      resources :messages, :only => [:new, :create]
    end
    resources :messages, :only => [:new]
  end
Run Code Online (Sandbox Code Playgroud)

所以现在,我在尝试设置user1_deleted或user2_deleted标志时遇到问题.(同样,如果/当我实现读取/最新标志时).问题是因为同一个用户可以有很多对话,但他可以是user1或user2,很难找到他.我以为我可以在Conversation模型中做这样的事情:

def self.active(user)
  Conversation.where(which_user?(user) + "_deleted = ?", false)
end

def self.which_user?(user)
  :user1_id == user ? 'user1' : 'user2'
end
Run Code Online (Sandbox Code Playgroud)

但是,除非你逐个遍历用户的每个对话,否则你无法运行整个对话,因为有时他是user1,有时候他是user2.我应该抛弃这整个方法并尝试新的设计吗?如果是这样,有没有人可能更优雅/更好/实际工作并仍然满足相同的需求?

这是一个很长的问题,所以我很欣赏任何愿意和我一起讨论这一切的人.谢谢.

Mic*_*ley 13

kindofgreat,

这个问题引起了我的兴趣,所以我花了几个小时进行实验,这是我的发现.结果是一个应用程序,任何数量的用户都可以参与对话.

我选择了一个数据模型,它具有一个介于UserConversation调用之间的中间模型UserConveration; 它是一个连接模型,一起保存有关用户状态和对话的数据(即,是否读取,删除对话等)

实现在GitHub上,您可以在https://github.com/BinaryMuse/so_association_expirement/compare/53f2263上看到我编写的代码的差异(相对于自动生成的代码,以保留所有内容). ..master.

这是我的模型,仅限于协会:

class User < ActiveRecord::Base
  has_many :user_conversations
  has_many :conversations, :through => :user_conversations
  has_many :messages, :through => :conversations
end

class UserConversation < ActiveRecord::Base
  belongs_to :user
  belongs_to :conversation
  has_many :messages, :through => :conversation

  delegate :subject, :to => :conversation
  delegate :users, :to => :conversation
end

class Conversation < ActiveRecord::Base
  has_many :user_conversations
  has_many :users, :through => :user_conversations
  has_many :messages
end

class Message < ActiveRecord::Base
  belongs_to :user
  belongs_to :conversation
end
Run Code Online (Sandbox Code Playgroud)

这是数据库的样子:

create_table "conversations", :force => true do |t|
  t.string   "subject"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "messages", :force => true do |t|
  t.integer  "user_id"
  t.integer  "conversation_id"
  t.text     "body"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "user_conversations", :force => true do |t|
  t.integer  "user_id"
  t.integer  "conversation_id"
  t.boolean  "deleted"
  t.boolean  "read"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "users", :force => true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end
Run Code Online (Sandbox Code Playgroud)

基本思想是向用户呈现"对话",而实际是幕后我们正在UserConversation为参与对话的所有用户管理.特别参见模型create_user_conversations上的方法UserConversation,该方法负责为与会话关联的每个用户在连接表中创建条目.

模型中还有许多has_many :throughdelegates调用,以尽可能轻松地获取我们想要的数据...例如,@user_conversation.conversation.subject您可以使用@user_conversation.subject; 在同样适用于messages属性.

我意识到这是相当多的代码,所以我鼓励你获取源代码并使用它.这一切都有效,除了"删除"对话(我没有打扰这个,但将消息标记为已读/未读确实有效).请注意,您必须以用户身份"登录"以执行某些操作,例如创建新对话等.您可以通过从主页面单击用户并选择"以此用户身份登录"来登录.

要记住的另一件事是,即使正在使用的控制器是"UserConversations"控制器,URL也会说"对话"以保证用户的好处 - 检查路由文件.

如果您有任何更深入/更深入的问题,请随时通过GitHub或我的StackOverflow配置文件上的联系方式与我联系.