向RoR消息收件箱系统添加通知

时间:2016-11-04 21:07:30

标签: ruby-on-rails ruby notifications messaging inbox

我有一个带有实习消息系统的rails应用程序。用户可以向其他用户发送消息。示例:用户1发送消息用户2可以回答反之亦然。

一切都很完美。但我想用notfications functionnality升级这个系统。我想输入通知:

1)在导航栏上 2)通过邮件

问题:我不知道如何做到这一点。

你能帮助我吗?

对话表

class CreateConversations < ActiveRecord::Migration
 def change
  create_table :conversations do |t|
   t.integer :sender_id
   t.integer :recipient_id
   t.timestamps
  end
 end
end

消息表。在这段代码中我有一个布尔值:read。我认为解决方案可以在这里。你怎么看待这个?

class CreateMessages < ActiveRecord::Migration
 def change
  create_table :messages do |t|
   t.text :body
   t.references :conversation, index: true
   t.references :user, index: true
   t.boolean :read, :default => false
   t.timestamps
  end
 end
end

conversation.rb

class Conversation < ActiveRecord::Base
 belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'
 belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'User'
has_many :messages, dependent: :destroy
validates_uniqueness_of :sender_id, :scope => :recipient_id
scope :between, -> (sender_id,recipient_id) do
 where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
 end
end

Message.rb

class Message < ActiveRecord::Base
 belongs_to :conversation
 belongs_to :user
 validates_presence_of :body, :conversation_id, :user_id
 def message_time
  created_at.strftime("%m/%d/%y at %l:%M %p")
 end
end

conversations_controller.rb

class ConversationsController < ApplicationController
  before_action :authenticate_user!

  # GET /conversations
  # GET /conversations.json
  def index
    @users = User.all

    # Restrict to conversations with at least one message and sort by last updated
    @conversations = Conversation.joins(:messages).uniq.order('updated_at DESC')
  end

  # POST /conversations
  # POST /conversations.json
  def create
  if Conversation.between(params[:conversation][:sender_id], params[:conversation][:recipient_id]).present?
    @conversation = Conversation.between(params[:conversation][:sender_id], params[:conversation][:recipient_id]).first
  else
    @conversation = Conversation.create!(conversation_params)
  end

  redirect_to conversation_messages_path(@conversation)
end

  private
    # Use callbacks to share common setup or constraints between actions.
    def conversation_params
      params.require(:conversation).permit(:sender_id, :recipient_id)
    end
end

messages_controller.rb

class MessagesController < ApplicationController
  before_action do
   @conversation = Conversation.find(params[:conversation_id])
  end
def index
 @messages = @conversation.messages
  if @messages.length > 10
   @over_ten = true
   @messages = @messages[-10..-1]
  end
  if params[:m]
   @over_ten = false
   @messages = @conversation.messages
  end
 if @messages.last
  if @messages.last.user_id != current_user.id
   @messages.last.read = true;
  end
 end
@message = @conversation.messages.new
 end
def new
 @message = @conversation.messages.new
end
def create
 @message = @conversation.messages.new(message_params)
 if @message.save
  redirect_to conversation_messages_path(@conversation)
 end
end
private
 def message_params
  params.require(:message).permit(:body, :user_id)
 end
end

/conversations/index.html.erb

<div class="ui segment">
 <h3>Mailbox</h3>
 <div class="ui list">
  <div class="item">
   <% @conversations.each do |conversation| %>
   <% if conversation.sender_id == current_user.id || conversation.recipient_id == current_user.id %>
    <% if conversation.sender_id == current_user.id %>
      <% recipient = User.find(conversation.recipient_id) %>
    <% else %>
      <% recipient = User.find(conversation.sender_id) %>
    <% end %>
    Conversation avec <%= link_to recipient.prenom,   conversation_messages_path(conversation)%>
   <% end %>
  <% end %>
  </div>
 </div>
</div>
<div class="ui segment">
 <h3>All Users</h3>
  <div class="ui list">
   <% @users.each do |user| %>
    <% if user.id != current_user.id %>
     <div class="item">
   <%= user.prenom %> <%= button_to 'Message me', conversations_path(conversation: { sender_id: current_user.id, recipient_id: user.id }), class: 'btn btn-primary m-t' %>
 </div>
  <% end %>
 <% end %>
 </div>
</div>

消息/ index.html.erb

<% if @over_ten %>
 <%= link_to 'Show Previous', "?m=all" %>
<% end %>
<div class="ui segment">
 <% @messages.each do |message| %>
  <% if message.body %>
   <% user = User.find(message.user_id) %>
   <div class="item">
    <div class="content">
     <div class="header"><strong><div class="imageavatarmessage"><%= image_tag user.avatar(:thumb), class:"imageavatarmessage" %></div><%= user.prenom %></strong>     <%= message.message_time %></div>
      <div class="list">
       <div class="item">
        <i class="right triangle icon"></i>
         <%= message.body %>
      </div>
    </div>
   </div>
 </div>
 <% end %>
<% end %>
</div>
<%= form_for [@conversation, @message], html: {class: "ui reply form"} do |f| %>
 <div class=”field”>
   <%= f.text_area :body, class: "form-control" %>
 </div>
 <%= f.text_field :user_id, value: current_user.id, type: "hidden" %>
 <div>
   <%= f.submit "Add Reply", class: "ui blue labeled submit icon button" %>
 </div>
<% end %>

1 个答案:

答案 0 :(得分:1)

我建议你通过本教程来构建一个不错的通知系统 https://www.devwalks.com/lets-build-instagram-part-6-notifications/

基本上,您必须创建新模型,使用Message模型设置依赖关系并集成到控制器

对于电子邮件通知,它甚至更简单。 只需创建新的邮件程序,然后在messages_controller

中创建操作即可启动它
def create
 @message = @conversation.messages.new(message_params)
 if @message.save
  SendMessageMailer.new_message(@message).deliver_later
  redirect_to conversation_messages_path(@conversation)
 end
end

修改 要创建邮件程序,您应该执行以下操作:

rails g mailer SendMessage

转到/app/mailers/send_message_mailer.rb并添加操作,与控制器相同类型的建筑物

def new_message(message)
    @message = message
    mail(to: @message.user.email, subject: 'Hey! Here is what you missed')
end

另外,创建一个视图(电子邮件模板)和带有erb代码的代码

app/views/send_message_mailer/new_message.html.erb

我不会深入研究这个问题,我想你可以弄明白如何通过间隔(如果用户在线或已经阅读过该消息,请说不要发送)并区分接收者/发送者用户

再一次,梅勒就是同一类型的控制器。您可以根据需要传递尽可能多的参数,并在邮件程序控制器和视图中使用模型嵌套

邮件收发器

def new_message(message, sender, receiver)
end

控制器

SendMessageMailer.new_message(@message, @message.user, params[:receiver]).deliver_later