1

我有一个自定义收件箱消息系统,我想修复消息的删除功能。当用户 A 删除一条消息时,它不会从数据库中永久删除,直到用户 B 也删除该消息。因此,邮件将保留在用户收件箱/删除文件夹中,直到发件人和收件人都删除了邮件。

我希望它的功能是,如果一个用户从收件箱中删除邮件或再次单击删除按钮以删除其删除文件夹中的邮件...该邮件应保持隐藏状态,就好像它已被删除一样。从而使选择删除它的用户无法看到该消息。当然,如果我已经设置了它,那么如果两个用户都选择删除它,则该消息将从数据库中永久删除。

当消息被软删除时(对一个用户隐藏,直到它从数据库中永久删除),该用户不应该看到它。如果他们正在浏览他们的消息并使用下一个/上一个链接...他们已删除的消息在单击下一个/上一个时不应出现。

任何帮助,将不胜感激!

消息控制器:

  def index
    if params[:mailbox] == "sent"
      @messages = @user.sent_messages
    elsif params[:mailbox] == "inbox"
      @messages = @user.received_messages
    #elsif params[:mailbox] == "archieved"
     # @messages = @user.archived_messages
    end
    if params[:mailbox] == "unread"
    @messages = @user.unread_messages
  end
  end

  def new
    @message = Message.new
    if params[:reply_to]
      @reply_to = User.find_by_user_id(params[:reply_to])
      unless @reply_to.nil?
        @message.recipient_id = @reply_to.user_id
      end
    end
  end

  def create
    @message = Message.new(params[:message])
    @message.sender_id = @user.id
    if @message.save
      flash[:notice] = "Message has been sent"
      redirect_to user_messages_path(current_user, :mailbox=>:inbox)
    else
      render :action => :new
    end
  end

  def show
     @message = Message.find(params[:id])
     @message.readingmessage if @message.recipient == current_user
   end

   def destroy
     @message = Message.find(params[:id])
     @message.destroy
     flash[:notice] = "Successfully deleted message."
     redirect_to user_messages_path(@user, @messages)
   end

  def delete_multiple
      if params[:delete]
        params[:delete].each { |id|
          @message = Message.find(id)
          @message.mark_message_deleted(@message.id,@user.id) unless @message.nil?
        }
        flash[:notice] = "Messages deleted"
      end
      redirect_to user_messages_path(@user, @messages)
  end

  private
    def set_user
      @user = current_user
    end
end

消息模型:

belongs_to :sender,
:class_name => 'User',
:foreign_key => 'sender_id'
belongs_to :recipient,
:class_name => 'User',
:foreign_key => 'recipient_id'

# marks a message as deleted by either the sender or the recipient, which ever the user that was passed is.
# When both sender and recipient marks it deleted, it is destroyed.
def mark_message_deleted(id,user_id)
     self.sender_deleted = true if self.sender_id == user_id
     self.recipient_deleted = true if self.recipient_id == user_id
     (self.sender_deleted && self.recipient_deleted) ? self.destroy : self.save!
 end
# Read message and if it is read by recipient then mark it is read
def readingmessage
  self.read_at ||= Time.now
  save
end

# Based on if a message has been read by it's recipient returns true or false.
def read?
    self.read_at.nil? ? false : true
end

def self.received_by(user)
   where(:recipient_id => user.id)
 end

 def self.not_recipient_deleted
   where("recipient_deleted = ?", false)
 end

 def self.sent_by(user)
    Message.where(:sender_id => user.id)
  end

  def previous(same_recipient = true)
    collection = Message.where('id <> ? AND created_at > ?', self.id, self.created_at).order('created_at ASC')
    collection.where(recipient_id: self.recipient_id) if same_recipient
    collection.first
  end

  def next(same_recipient = true)
    collection = Message.where('id <> ? AND created_at < ?', self.id, self.created_at).order('created_at DESC')
    collection.where(recipient_id: self.recipient_id) if same_recipient
    collection.first
  end
end

收件箱视图:

<%= form_tag delete_multiple_user_messages_path, :method=>:post do %>
    <table  class="table table-bordered">
            <tr>
            <th>Delete</th>
            <th>Subject</th>
            <th>Date</th>
            <th>From</th>
        </tr>
            <% for message in @messages %>
                <tr>
                    <td><%= check_box_tag "delete[]", message.id %></td><p>

                    <td>
                        <% if message.read? %>
                          <%= link_to h(message.subject), user_message_path(@user, message) %>
                        <% else %>
                          <%= link_to "#{h(message.subject)} (unread)", user_message_path(@user, message) %>
                        <% end %>
                    </td>

                    <td><%=h message.created_at.to_s(:long) %></td>
            <td><%= "#{message.sender}" %></td>
                </tr>
    <% end %>
    </table>
<%= submit_tag "Delete selected" %>
4

2 回答 2

1

我在你的模型中看到了这个方法,但我没有看到你在任何地方调用它:

def self.not_recipient_deleted
  where("recipient_deleted = ?", false)
end

我相信如果您在收件箱视图中更改此 for 循环

<% for message in @messages %>

<% for message in @messages.not_recipient_deleted %>

它只会返回收件人尚未删除的邮件。这应该可以防止已删除的邮件显示在收件箱中。

于 2013-10-06T04:42:44.120 回答
0

您可以在控制器中将其用于索引操作 @messages = @user.received_messages.where(recipient_deleted: false)

因此它将仅加载收件人未删除的消息

于 2013-10-07T11:33:07.890 回答