add support for viewing sent private messages

This commit is contained in:
joshua stein 2013-01-24 14:06:10 -06:00
parent 08a8165fa9
commit 464b3c3f80
8 changed files with 72 additions and 23 deletions

View file

@ -8,11 +8,26 @@ class MessagesController < ApplicationController
@new_message = Message.new
@direction = :in
@messages = @user.undeleted_received_messages
if params[:to]
@new_message.recipient_username = params[:to]
end
end
def sent
@cur_url = "/messages"
@title = "Messages Sent"
@direction = :out
@messages = @user.undeleted_sent_messages
@new_message = Message.new
render :action => "index"
end
def create
@cur_url = "/messages"
@title = "Messages"
@ -59,10 +74,15 @@ class MessagesController < ApplicationController
@message.deleted_by_recipient = true
end
@message.save
@message.save!
flash[:success] = "Deleted message."
return redirect_to "/messages"
if @message.author_user_id == @user.id
return redirect_to "/messages/sent"
else
return redirect_to "/messages"
end
end
def keep_as_new

View file

@ -18,15 +18,15 @@ class Message < ActiveRecord::Base
before_create :assign_short_id
after_create :deliver_reply_notifications
after_save :check_for_both_deleted
after_save :update_unread_counts
after_save :check_for_both_deleted
def assign_short_id
self.short_id = ShortId.new(self.class).generate
end
def check_for_both_deleted
if self.deleted_by_author && self.deleted_by_recipient
if self.deleted_by_author? && self.deleted_by_recipient?
self.destroy
end
end

View file

@ -14,6 +14,9 @@ class Moderation < ActiveRecord::Base
m = Message.new
m.author_user_id = self.moderator_user_id
# mark as deleted by author so they don't fill up moderator message boxes
m.deleted_by_author = true
if self.story
m.recipient_user_id = self.story.user_id
m.subject = "Your story has been edited by a moderator"
@ -50,8 +53,6 @@ class Moderation < ActiveRecord::Base
return
end
# so this will be deleted when the recipient deletes it
m.deleted_by_author = true
m.save
end
end

View file

@ -2,7 +2,7 @@ class User < ActiveRecord::Base
has_many :stories,
:include => :user
has_many :comments
has_many :authored_messages,
has_many :sent_messages,
:class_name => "Message",
:foreign_key => "author_user_id"
has_many :received_messages,
@ -101,9 +101,11 @@ class User < ActiveRecord::Base
end
def undeleted_received_messages
received_messages.where([ "((recipient_user_id = ? AND " <<
"deleted_by_recipient = 0) OR (author_user_id = ? AND " <<
"deleted_by_author = 0))", self.id, self.id ])
received_messages.where(:deleted_by_recipient => false)
end
def undeleted_sent_messages
sent_messages.where(:deleted_by_author => 0)
end
def initiate_password_reset_for_ip(ip)

View file

@ -66,16 +66,21 @@
<div id="headerright" class="<%= @user ? "loggedin" : "" %>">
<span class="headerlinks">
<% if @user %>
<a href="/filters">Filters</a>
<a href="/filters" <%= @cur_url == "/filters" ?
raw("class=\"cur_url\"") : "" %>>Filters</a>
<% if (count = @user.unread_message_count) > 0 %>
<a href="/messages" class="new_messages"><%= count %> New
Message<%= count == 1 ? "" : "s" %></a>
<a href="/messages" class="new_messages <%= @cur_url == "/messages" ?
"cur_url" : "" %>"><%= count %> New Message<%= count == 1 ? "" :
"s" %></a>
<% else %>
<a href="/messages">Messages</a>
<a href="/messages" <%= @cur_url == "/messages" ?
raw("class=\"cur_url\"") : "" %>>Messages</a>
<% end %>
<a href="/settings"><%= @user.username %> (<%= @user.karma %>)</a>
<a href="/settings" <%= @cur_url == "/settings" ?
raw("class=\"cur_url\"") : "" %>><%= @user.username %>
(<%= @user.karma %>)</a>
<%= link_to "Logout", { :controller => "login", :action => "logout" },
:data => { :confirm => "Are you sure you want to logout?" },

View file

@ -1,19 +1,34 @@
<div class="box wide">
<div class="legend" style="float: right;">
<% if @direction == :out %>
<a href="/messages">View Received</a>
<% else %>
<a href="/messages/sent">View Sent</a>
<% end %>
</div>
<div class="legend">
Private Messages
<% if @direction == :out %>
Sent
<% end %>
</div>
<% if @user.undeleted_received_messages.any? %>
<% if @messages.any? %>
<table class="data zebra" width="100%" cellspacing=0>
<tr>
<th width="15%">From</th>
<th width="20%">Sent</th>
<th width="15%"><%= @direction == :in ? "From" : "To" %></th>
<th width="20%"><%= @direction == :in ? "Received" : "Sent" %></th>
<th width="60%">Subject</th>
</tr>
<% @user.undeleted_received_messages.each do |message| %>
<% @messages.includes(:author, :recipient).each do |message| %>
<tr class="<%= message.has_been_read? ? "" : "bold" %>">
<td><a href="/u/<%= message.author.username %>"><%=
message.author.username %></a></td>
<td><% if @direction == :in %>
<a href="/u/<%= message.author.username %>"><%=
message.author.username %></a>
<% else %>
<a href="/u/<%= message.recipient.username %>"><%=
message.recipient.username %></a>
<% end %></td>
<td><%= time_ago_in_words(message.created_at) %> ago</td>
<td><a href="/messages/<%= message.short_id %>"><%= message.subject
%></a></td>
@ -22,7 +37,8 @@
</table>
<% else %>
<p>
You do not have any private messages.
You do not have any <%= @direction == :in ? "" : "sent" %> private
messages.
</p>
<% end %>

View file

@ -1,6 +1,10 @@
<div class="box wide">
<div class="legend" style="float: right;">
<a href="/messages">Back to Messages</a>
<% if @message.author_user_id == @user.id %>
<a href="/messages/sent">Back to Sent Messages</a>
<% else %>
<a href="/messages">Back to Messages</a>
<% end %>
</div>
<div class="legend">

View file

@ -58,6 +58,7 @@ Lobsters::Application.routes.draw do
post "/comments/post_to/:story_id" => "comments#create"
post "/comments/preview_to/:story_id" => "comments#preview_new"
get "/messages/sent" => "messages#sent"
resources :messages do
post "keep_as_new"
end