Complete refactoring of messaging module. From now messages are saved only once and send afterwards via a the 'send_emails'-rake-task.
This commit is contained in:
parent
6ce6c2c75a
commit
e8d55e50c0
30 changed files with 220 additions and 349 deletions
|
|
@ -12,6 +12,15 @@ class ApplicationController < ActionController::Base
|
|||
def self.current
|
||||
Thread.current[:application_controller]
|
||||
end
|
||||
|
||||
# Use this method to call a rake task,,
|
||||
# e.g. to deliver mails after there are created.
|
||||
def call_rake(task, options = {})
|
||||
options[:rails_env] ||= Rails.env
|
||||
args = options.map { |n, v| "#{n.to_s.upcase}='#{v}'" }
|
||||
system "/usr/bin/rake #{task} #{args.join(' ')} --trace 2>&1 >> #{Rails.root}/log/rake.log &"
|
||||
end
|
||||
|
||||
|
||||
protected
|
||||
|
||||
|
|
@ -139,7 +148,7 @@ class ApplicationController < ActionController::Base
|
|||
|
||||
# Sends any pending emails that were created during this request.
|
||||
def send_email_messages
|
||||
Message.send_emails if Message.pending?
|
||||
call_rake :send_emails unless Message.pending.empty?
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ class HomeController < ApplicationController
|
|||
if @orderGroup
|
||||
@financial_transactions = @orderGroup.financial_transactions.find(:all, :order => 'created_on desc', :limit => 3)
|
||||
end
|
||||
# unread messages
|
||||
@messages = Message.find_all_by_recipient_id_and_read(@current_user.id, false, :order => 'messages.created_on desc', :include => :sender)
|
||||
# unaccepted tasks
|
||||
@unaccepted_tasks = @current_user.unaccepted_tasks
|
||||
# task in next week
|
||||
|
|
|
|||
|
|
@ -1,27 +1,10 @@
|
|||
class MessagesController < ApplicationController
|
||||
verify :method => :post, :only => [:create, :destroy], :redirect_to => { :action => :index }
|
||||
|
||||
MESSAGE_SEND_SUCCESS = 'Nachricht erfolgreich abgeschickt.'
|
||||
MESSAGE_DELETE_SUCCESS = 'Nachricht gelöscht.'
|
||||
MESSAGES_DELETE_SUCCESS = 'Nachrichten gelöscht.'
|
||||
ERROR_SEND_FAILED = 'Nachricht konnte nicht verschickt werden.'
|
||||
ERROR_CANNOT_DELETE = 'Nachricht kann nicht gelöscht werden.'
|
||||
ERROR_CANNOT_REPLY = 'Auf diese Nachricht kann nicht geantwortet werden.'
|
||||
ERROR_UNKNOWN_USER = 'Unbekannte_r Empfänger_in.'
|
||||
ERROR_INVALID_GROUP = 'Empfängergruppe ist unbekannt oder hat keine Mitglieder.'
|
||||
ERROR_NO_RECIPIENTS = 'Es sind keine Empfänger_innen ausgewählt.'
|
||||
|
||||
|
||||
# Renders the "inbox" action.
|
||||
def index
|
||||
inbox
|
||||
render :action => 'inbox'
|
||||
@messages = Message.user
|
||||
end
|
||||
|
||||
# Shows the user's message inbox.
|
||||
def inbox
|
||||
@messages = Message.find_all_by_recipient_id(@current_user.id, :order => 'messages.created_on desc', :include => :sender)
|
||||
end
|
||||
|
||||
|
||||
# Creates a new message object.
|
||||
def new
|
||||
@message = Message.new
|
||||
|
|
@ -29,136 +12,58 @@ class MessagesController < ApplicationController
|
|||
|
||||
# Creates a new message.
|
||||
def create
|
||||
# Determine recipient(s)...
|
||||
@recipient_nicks = ''
|
||||
if (params[:everyone] == 'yes')
|
||||
@everyone = true
|
||||
recipients = User.find(:all)
|
||||
@message = @current_user.send_messages.new(params[:message])
|
||||
if @message.save
|
||||
flash[:notice] = "Nachricht ist gespeichert und wird versendet."
|
||||
redirect_to messages_path
|
||||
else
|
||||
recipients = Array.new
|
||||
# users
|
||||
for nick in params[:recipient][:nicks].split(%r{,\s*})
|
||||
if (user = User.find_by_nick(nick))
|
||||
recipients << user
|
||||
@recipient_nicks += "#{nick}, "
|
||||
end
|
||||
end
|
||||
@recipient_nicks = @recipient_nicks[0..-3] unless @recipient_nicks.empty?
|
||||
# group
|
||||
group = Group.find_by_id(params[:recipient][:group_id]) if params[:recipient][:group_id]
|
||||
recipients = recipients | group.users if group
|
||||
end
|
||||
|
||||
# Construct message(s) and save them...
|
||||
if recipients.empty?
|
||||
@message = Message.new(params[:message])
|
||||
@message.sender = @current_user
|
||||
@group = group
|
||||
flash[:error] = ERROR_NO_RECIPIENTS
|
||||
render :action => 'new'
|
||||
else
|
||||
begin
|
||||
if (@everyone)
|
||||
recipients_text = 'alle'
|
||||
else
|
||||
recipients_text = @recipient_nicks
|
||||
recipients_text += (group.nil? ? '' : (recipients_text.empty? ? group.name : ", #{group.name}"))
|
||||
end
|
||||
Message.transaction do
|
||||
for recipient in recipients
|
||||
@message = Message.new(
|
||||
:subject => params[:message][:subject],
|
||||
:body => params[:message][:body],
|
||||
:recipient => recipient,
|
||||
:recipients => recipients_text
|
||||
)
|
||||
@message.sender = @current_user
|
||||
@message.save!
|
||||
end
|
||||
end
|
||||
flash[:notice] = MESSAGE_SEND_SUCCESS
|
||||
redirect_to :action=> 'index'
|
||||
rescue
|
||||
@group = group
|
||||
flash[:error] = ERROR_SEND_FAILED
|
||||
render :action => 'new'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Deletes the message(s) specified by the id/ids param if the current user is the recipient.
|
||||
def destroy
|
||||
ids = Array.new
|
||||
ids << params[:id] if params[:id]
|
||||
ids = ids + params[:ids] if (params[:ids] && params[:ids].is_a?(Array))
|
||||
for id in ids
|
||||
message = Message.find(id)
|
||||
if (message && message.recipient && message.recipient == @current_user)
|
||||
message.destroy
|
||||
else
|
||||
flash[:error] = ERROR_CANNOT_DELETE
|
||||
break
|
||||
end
|
||||
end
|
||||
flash[:notice] = MESSAGE_DELETE_SUCCESS if (flash[:error].blank? && ids.size == 1)
|
||||
flash[:notice] = "#{ids.size} #{MESSAGES_DELETE_SUCCESS}" if (flash[:error].blank? && ids.size > 1)
|
||||
redirect_to :action=> 'index'
|
||||
end
|
||||
|
||||
# Shows a single message.
|
||||
def show
|
||||
@message = Message.find_by_id_and_recipient_id(params[:id], @current_user.id)
|
||||
@message.update_attribute('read', true) if (@message && !@message.read?)
|
||||
@message = Message.find(params[:id])
|
||||
end
|
||||
|
||||
# Replys to the message specified through :id.
|
||||
def reply
|
||||
message = Message.find(params[:id])
|
||||
if (message && message.recipient && message.recipient == @current_user && message.sender && message.sender.nick)
|
||||
@message = Message.new(
|
||||
:recipient => message.sender,
|
||||
:subject => "Re: #{message.subject}",
|
||||
:body => "#{message.sender.nick} schrieb am #{I18n.l(message.created_on.to_date)} um #{I18n.l(message.created_on, :format => :time)}:\n"
|
||||
)
|
||||
if (message.body)
|
||||
message.body.each_line{|l| @message.body += "> #{l}"}
|
||||
end
|
||||
@recipient_nicks = message.sender.nick
|
||||
render :action => 'new'
|
||||
else
|
||||
flash[:error] = ERROR_CANNOT_REPLY
|
||||
redirect_to :action=> 'index'
|
||||
end
|
||||
@message = Message.new(:recipient => message.sender, :subject => "Re: #{message.subject}")
|
||||
@message.body = "#{message.sender.nick} schrieb am #{I18n.l(message.created_at.to_date)} um #{I18n.l(message.created_at, :format => :time)}:\n"
|
||||
message.body.each_line{|l| @message.body += "> #{l}"}
|
||||
render :action => 'new'
|
||||
end
|
||||
|
||||
# Shows new-message form with the recipient user specified through :id.
|
||||
def user
|
||||
if (recipient = User.find(params[:id]))
|
||||
@recipient_nicks = recipient.nick
|
||||
@message = Message.new
|
||||
@message = Message.new(:recipient => recipient)
|
||||
render :action => 'new'
|
||||
else
|
||||
flash[:error] = ERROR_UNKNOWN_USER
|
||||
flash[:error] = 'Unbekannte_r EmpfängerIn.'
|
||||
redirect_to :action=> 'index'
|
||||
end
|
||||
end
|
||||
|
||||
# Shows new-message form with the recipient user specified through :id.
|
||||
def group
|
||||
recipient = Group.find(params[:id], :include => :memberships)
|
||||
if (recipient && !recipient.memberships.empty?)
|
||||
@message = Message.new
|
||||
@group = recipient
|
||||
group = Group.find(params[:id], :include => :memberships)
|
||||
if (group && !group.memberships.empty?)
|
||||
@message = Message.new(:group_id => group.id)
|
||||
render :action => 'new'
|
||||
else
|
||||
flash[:error] = ERROR_INVALID_GROUP
|
||||
flash[:error] = 'Empfängergruppe ist unbekannt oder hat keine Mitglieder.'
|
||||
redirect_to :action=> 'index'
|
||||
end
|
||||
end
|
||||
|
||||
# Auto-complete for recipient user list.
|
||||
def auto_complete_for_recipient_nicks
|
||||
@users = User.find(:all, :conditions => ['LOWER(nick) LIKE ?', '%' + params[:recipient][:nicks].downcase + '%'], :order => :nick, :limit => 8)
|
||||
def auto_complete_for_message_recipients_nicks
|
||||
@users = User.find(:all,
|
||||
:conditions => ['LOWER(nick) LIKE ?', '%' + params[:message][:recipients_nicks].downcase + '%'],
|
||||
:order => :nick, :limit => 8)
|
||||
render :partial => '/shared/auto_complete_users'
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue