我有一个自定义的收件箱邮件系统,我想修复邮件的删除功能。当用户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" %>
答案 0 :(得分:1)
我在你的模型中看到了这个方法,但我没有看到你在任何地方调用它:
def self.not_recipient_deleted
where("recipient_deleted = ?", false)
end
我相信如果您在收件箱视图中更改此for循环
<% for message in @messages %>
到
<% for message in @messages.not_recipient_deleted %>
它只会返回收件人尚未删除的邮件。这应该可以防止已删除的邮件显示在收件箱中。
答案 1 :(得分:0)
您可以在控制器中使用它来进行索引操作 @messages = @ user.received_messages.where(recipient_deleted:false)
因此它只会加载未被收件人删除的邮件