diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 1b5393b..16bc136 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -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 diff --git a/app/models/message.rb b/app/models/message.rb index 27d26e8..87bdd63 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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 diff --git a/app/models/moderation.rb b/app/models/moderation.rb index f261da7..2c5b26e 100644 --- a/app/models/moderation.rb +++ b/app/models/moderation.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 7d4bbea..a2bea13 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 644f4aa..f8c6b87 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -66,16 +66,21 @@
"> <% if @user %> - Filters + >Filters <% if (count = @user.unread_message_count) > 0 %> - <%= count %> New - Message<%= count == 1 ? "" : "s" %> + "><%= count %> New Message<%= count == 1 ? "" : + "s" %> <% else %> - Messages + >Messages <% end %> - <%= @user.username %> (<%= @user.karma %>) + ><%= @user.username %> + (<%= @user.karma %>) <%= link_to "Logout", { :controller => "login", :action => "logout" }, :data => { :confirm => "Are you sure you want to logout?" }, diff --git a/app/views/messages/index.html.erb b/app/views/messages/index.html.erb index 8fee39e..3ed2a8b 100644 --- a/app/views/messages/index.html.erb +++ b/app/views/messages/index.html.erb @@ -1,19 +1,34 @@
+
+ <% if @direction == :out %> + View Received + <% else %> + View Sent + <% end %> +
Private Messages + <% if @direction == :out %> + Sent + <% end %>
- <% if @user.undeleted_received_messages.any? %> + <% if @messages.any? %> - - + + - <% @user.undeleted_received_messages.each do |message| %> + <% @messages.includes(:author, :recipient).each do |message| %> "> - + @@ -22,7 +37,8 @@
FromSent<%= @direction == :in ? "From" : "To" %><%= @direction == :in ? "Received" : "Sent" %> Subject
<%= - message.author.username %><% if @direction == :in %> + <%= + message.author.username %> + <% else %> + <%= + message.recipient.username %> + <% end %> <%= time_ago_in_words(message.created_at) %> ago <%= message.subject %>
<% else %>

- You do not have any private messages. + You do not have any <%= @direction == :in ? "" : "sent" %> private + messages.

<% end %> diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index 2cc0961..32da710 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -1,6 +1,10 @@
- Back to Messages + <% if @message.author_user_id == @user.id %> + Back to Sent Messages + <% else %> + Back to Messages + <% end %>
diff --git a/config/routes.rb b/config/routes.rb index ad8fed5..6d91a61 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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