allow messages to come from system instead of a user

This commit is contained in:
joshua stein 2015-10-15 10:01:42 -05:00
parent 7974ab4090
commit 2ca2c94ef6
5 changed files with 64 additions and 37 deletions

View file

@ -51,20 +51,22 @@ class MessagesController < ApplicationController
@cur_url = "/messages"
@title = @message.subject
@new_message = Message.new
@new_message.recipient_username = (@message.author_user_id == @user.id ?
@message.recipient.username : @message.author.username)
if @message.author
@new_message = Message.new
@new_message.recipient_username = (@message.author_user_id == @user.id ?
@message.recipient.username : @message.author.username)
if @message.subject.match(/^re:/i)
@new_message.subject = @message.subject
else
@new_message.subject = "Re: #{@message.subject}"
end
end
if @message.recipient_user_id == @user.id
@message.has_been_read = true
@message.save
end
if @message.subject.match(/^re:/i)
@new_message.subject = @message.subject
else
@new_message.subject = "Re: #{@message.subject}"
end
end
def destroy

View file

@ -9,7 +9,7 @@ class EmailMessage < ActionMailer::Base
mail(
:to => user.email,
:subject => "[#{Rails.application.name}] Private Message from " <<
"#{message.author.username}: #{message.subject}"
"#{message.author_username}: #{message.subject}"
)
end
end

View file

@ -7,7 +7,6 @@ class Message < ActiveRecord::Base
:foreign_key => "author_user_id"
validates_presence_of :recipient
validates_presence_of :author
attr_accessor :recipient_username
@ -27,6 +26,14 @@ class Message < ActiveRecord::Base
self.short_id = ShortId.new(self.class).generate
end
def author_username
if self.author
self.author.username
else
"System"
end
end
def check_for_both_deleted
if self.deleted_by_author? && self.deleted_by_recipient?
self.destroy
@ -51,10 +58,11 @@ class Message < ActiveRecord::Base
if self.recipient.pushover_messages?
self.recipient.pushover!({
:title => "#{Rails.application.name} message from " <<
"#{self.author.username}: #{self.subject}",
"#{self.author_username}: #{self.subject}",
:message => self.plaintext_body,
:url => self.url,
:url_title => "Reply to #{self.author.username}",
:url_title => (self.author ? "Reply to #{self.author_username}" :
"View message"),
})
end
end

View file

@ -36,8 +36,12 @@
<tr class="<%= message.has_been_read? ? "" : "bold" %>">
<td><%= check_box_tag "delete_#{message.short_id}" %></td>
<td><% if @direction == :in %>
<a href="/u/<%= message.author.username %>"><%=
message.author.username %></a>
<% if message.author %>
<a href="/u/<%= message.author.username %>"><%=
message.author.username %></a>
<% else %>
<%= message.author_username %>
<% end %>
<% else %>
<a href="/u/<%= message.recipient.username %>"><%=
message.recipient.username %></a>

View file

@ -11,12 +11,17 @@
<%= @message.subject %>
<div class="sublegend">
Sent from <a href="/u/<%= @message.author.username %>"><%=
@message.author.username %></a>
<% if @message.author.is_admin? %>
(administrator)
<% elsif @message.author.is_moderator? %>
(moderator)
Sent from
<% if @message.author %>
<a href="/u/<%= @message.author.username %>"><%=
@message.author.username %></a>
<% if @message.author.is_admin? %>
(administrator)
<% elsif @message.author.is_moderator? %>
(moderator)
<% end %>
<% else %>
<%= @message.author_username %>
<% end %>
to
<a href="/u/<%= @message.recipient.username %>"><%=
@ -51,27 +56,35 @@
<br>
<div class="legend">
Compose Reply To <%= @new_message.recipient_username %>
Compose Reply
<% if @new_message %>
To <%= @new_message.recipient_username %>
<% end %>
</div>
<%= form_for @new_message, :method => :post do |f| %>
<%= f.hidden_field :recipient_username %>
<% if @new_message %>
<%= form_for @new_message, :method => :post do |f| %>
<%= f.hidden_field :recipient_username %>
<%= error_messages_for @new_message %>
<%= error_messages_for @new_message %>
<div class="boxline">
<%= f.text_field :subject, :style => "width: 500px;",
:autocomplete => "off" %>
</div>
<div class="boxline">
<%= f.text_field :subject, :style => "width: 500px;",
:autocomplete => "off" %>
</div>
<div class="boxline">
<%= f.text_area :body, :style => "width: 500px;", :rows => 5,
:autocomplete => "off" %>
</div>
<div class="boxline">
<%= f.text_area :body, :style => "width: 500px;", :rows => 5,
:autocomplete => "off" %>
</div>
<div class="boxline">
<p></p>
<%= submit_tag "Send Message" %>
</div>
<div class="boxline">
<p></p>
<%= submit_tag "Send Message" %>
</div>
<% end %>
<% else %>
This message cannot be replied to. Please contact a moderator for
assistance.
<% end %>
</div>