修改issues邮件通知查询语句

This commit is contained in:
huang 2015-06-03 14:25:22 +08:00
parent 64e56eca8e
commit 0354e65497
1 changed files with 68 additions and 70 deletions

View File

@ -45,6 +45,7 @@ class Mailer < ActionMailer::Base
MailerProxy.new(self) MailerProxy.new(self)
end end
# author: alan
# 发送邀请未注册用户加入项目邮件 # 发送邀请未注册用户加入项目邮件
# 功能: 在加入项目的同时自动注册用户 # 功能: 在加入项目的同时自动注册用户
def send_invite_in_project(email, project, invitor) def send_invite_in_project(email, project, invitor)
@ -88,7 +89,7 @@ class Mailer < ActionMailer::Base
date_from = "#{date_from} 17:59:59" date_from = "#{date_from} 17:59:59"
date_to = "#{date_to} 17:59:59" date_to = "#{date_to} 17:59:59"
# 生成token用于直接点击登录 # 生成token用于直接点击登录
@user = user @user = user
@token = Token.get_token_from_user(user, 'autologin') @token = Token.get_token_from_user(user, 'autologin')
@ -100,16 +101,14 @@ class Mailer < ActionMailer::Base
course_ids = courses.map {|course| course.id}.join(",") course_ids = courses.map {|course| course.id}.join(",")
# 查询user的缺陷包括发布的跟踪的以及被指派的缺陷 # 查询user的缺陷包括发布的跟踪的以及被指派的缺陷
# sql = "select DISTINCT i.* from issues i, watchers w sql = "select * from members m, issues i where i.project_id = m.project_id and m.user_id='#{user.id}'
# where (i.assigned_to_id = #{user.id} or i.author_id = #{user.id} and (i.created_on between '#{date_from}' and '#{date_to}') order by i.created_on desc"
# or (w.watchable_type = 'Issue' and w.watchable_id = i.id and w.user_id = #{user.id}))
# and (i.created_on between '#{date_from}' and '#{date_to}') order by i.created_on desc"
sql = "select DISTINCT i.* from issues i where (i.created_on between '#{date_from}' and '#{date_to}') order by i.created_on desc"
@issues = Issue.find_by_sql(sql) @issues = Issue.find_by_sql(sql)
# @bids 查询课程作业包括老师发布的作业以及user提交作业 # @bids 查询课程作业包括老师发布的作业以及user提交作业
# @attachments查询课程课件更新 # @attachments查询课程课件更新
@attachments ||= [] @attachments ||= []
@bids ||= [] # 老师发布的作业 @bids ||= [] # 老师发布的作业
unless courses.first.nil? unless courses.first.nil?
@ -118,15 +117,15 @@ class Mailer < ActionMailer::Base
for i in 0..count do for i in 0..count do
bids = courses[i].homeworks.where("bids.created_on between '#{date_from}' and '#{date_to}'").order("bids.created_on desc") bids = courses[i].homeworks.where("bids.created_on between '#{date_from}' and '#{date_to}'").order("bids.created_on desc")
attachments = courses[i].attachments.where("attachments.created_on between '#{date_from}' and '#{date_to}'").order('attachments.created_on DESC') attachments = courses[i].attachments.where("attachments.created_on between '#{date_from}' and '#{date_to}'").order('attachments.created_on DESC')
@bids += bids if bids.count > 0 @bids += bids if bids.count > 0
@attachments += attachments if attachments.count > 0 @attachments += attachments if attachments.count > 0
end end
end end
# user 提交的作业 # user 提交的作业
@homeworks = HomeworkAttach.where("user_id=#{user.id} and (created_at between '#{date_from}' and '#{date_to}')").order("created_at desc") @homeworks = HomeworkAttach.where("user_id=#{user.id} and (created_at between '#{date_from}' and '#{date_to}')").order("created_at desc")
# 查询user在课程。项目中发布的讨论帖子 # 查询user在课程。项目中发布的讨论帖子
messages = Message.find_by_sql("select DISTINCT * from messages where (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") messages = Message.find_by_sql("select DISTINCT * from messages where author_id = #{user.id} and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc")
@course_messages ||= [] @course_messages ||= []
@project_messages ||= [] @project_messages ||= []
unless messages.first.nil? unless messages.first.nil?
@ -141,9 +140,9 @@ class Mailer < ActionMailer::Base
# 查询user在课程中发布的通知项目中发的新闻 # 查询user在课程中发布的通知项目中发的新闻
@course_news = (course_ids && !course_ids.empty?) ? News.find_by_sql("select DISTINCT n.* from news n @course_news = (course_ids && !course_ids.empty?) ? News.find_by_sql("select DISTINCT n.* from news n
where n.course_id in (#{course_ids}) where n.course_id in (#{course_ids})
and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") : [] and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") : []
@project_news = (project_ids && !project_ids.empty?) ? News.find_by_sql("select DISTINCT n.* from news n where n.project_id in (#{project_ids}) @project_news = (project_ids && !project_ids.empty?) ? News.find_by_sql("select DISTINCT n.* from news n where n.project_id in (#{project_ids})
and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") : [] and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") : []
# 查询user在课程及个人中留言 # 查询user在课程及个人中留言
@course_journal_messages = JournalsForMessage.find_by_sql("select DISTINCT * from journals_for_messages where @course_journal_messages = JournalsForMessage.find_by_sql("select DISTINCT * from journals_for_messages where
@ -160,11 +159,11 @@ class Mailer < ActionMailer::Base
has_content = [@issues,@homeworks,@course_messages,@project_messages,@course_news,@project_news, has_content = [@issues,@homeworks,@course_messages,@project_messages,@course_news,@project_news,
@course_journal_messages,@user_journal_messages,@forums,@memos,@attachments,@bids].any? {|o| @course_journal_messages,@user_journal_messages,@forums,@memos,@attachments,@bids].any? {|o|
!o.empty? !o.empty?
} }
mylogger.debug "Sent activity mail : #{user.mail} - #{has_content}" mylogger.debug "Sent activity mail : #{user.mail} - #{has_content}"
#有内容才发,没有不发 #有内容才发,没有不发
mail :to => user.mail, :subject => subject if has_content mail :to => user.mail,:subject => subject if has_content
end end
# 公共讨论区发帖、回帖添加邮件发送信息 # 公共讨论区发帖、回帖添加邮件发送信息
@ -187,8 +186,8 @@ class Mailer < ActionMailer::Base
:subject => "[ #{l(:label_message_plural)} : #{memo.subject} #{l(:label_memo_create_succ)}]", :subject => "[ #{l(:label_message_plural)} : #{memo.subject} #{l(:label_memo_create_succ)}]",
:filter => true :filter => true
end end
# Builds a Mail::Message object used to email recipients of the added journals for message. # Builds a Mail::Message object used to email recipients of the added journals for message.
# 留言分为直接留言,和对留言人留言的回复 # 留言分为直接留言,和对留言人留言的回复
# 字段说明在JournalsForMessage.rb # 字段说明在JournalsForMessage.rb
# 直接留言后 reply_id,m_parent_id 为空相对应的at_user取值为nil # 直接留言后 reply_id,m_parent_id 为空相对应的at_user取值为nil
@ -276,11 +275,11 @@ class Mailer < ActionMailer::Base
@token = Token.get_token_from_user(user, 'autologin') @token = Token.get_token_from_user(user, 'autologin')
@issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :token => @token.value) @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :token => @token.value)
# edit # edit
@issue_author_url = url_for(user_activities_url(@author,:token => @token.value)) @issue_author_url = url_for(user_activities_url(@author,:token => @token.value))
@project_url = url_for(:controller => 'projects', :action => 'show', :id => issue.project_id, :token => @token.value) @project_url = url_for(:controller => 'projects', :action => 'show', :id => issue.project_id, :token => @token.value)
@user_url = url_for(my_account_url(user,:token => @token.value)) @user_url = url_for(my_account_url(user,:token => @token.value))
subject = "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] (#{issue.status.name}) #{issue.subject}" subject = "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] (#{issue.status.name}) #{issue.subject}"
@ -288,13 +287,13 @@ class Mailer < ActionMailer::Base
:subject => subject, :subject => subject,
:filter => true :filter => true
end end
# issue.attachments.each do |attach| # issue.attachments.each do |attach|
# attachments["#{attach.filename}"] = File.read("#{attach.disk_filename}") # attachments["#{attach.filename}"] = File.read("#{attach.disk_filename}")
# end # end
# cc = issue.watcher_recipients - recipients # cc = issue.watcher_recipients - recipients
#mail.attachments['test'] = File.read("#{RAILS.root}/files/2015/01/150114094010_libegl.dll") #mail.attachments['test'] = File.read("#{RAILS.root}/files/2015/01/150114094010_libegl.dll")
# Builds a Mail::Message object used to email recipients of the edited issue. # Builds a Mail::Message object used to email recipients of the edited issue.
@ -323,13 +322,13 @@ class Mailer < ActionMailer::Base
@project_url = url_for(:controller => 'projects', :action => 'show', :id => issue.project_id, :token => @token.value) @project_url = url_for(:controller => 'projects', :action => 'show', :id => issue.project_id, :token => @token.value)
@user_url = url_for(my_account_url(user,:token => @token.value)) @user_url = url_for(my_account_url(user,:token => @token.value))
@issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :anchor => "change-#{journal.id}", :token => @token.value) @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :anchor => "change-#{journal.id}", :token => @token.value)
s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] " s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] "
s << "(#{issue.status.name}) " if journal.new_value_for('status_id') s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
s << issue.subject s << issue.subject
@issue = issue @issue = issue
@journal = journal @journal = journal
# @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}") # @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
mail :to => recipients, mail :to => recipients,
:subject => s, :subject => s,
:filter => true :filter => true
@ -359,10 +358,10 @@ class Mailer < ActionMailer::Base
@issues = issues @issues = issues
@days = days @days = days
@issues_url = url_for(:controller => 'issues', :action => 'index', @issues_url = url_for(:controller => 'issues', :action => 'index',
:set_filter => 1, :assigned_to_id => user.id, :set_filter => 1, :assigned_to_id => user.id,
:sort => 'due_date:asc') :sort => 'due_date:asc')
mail :to => user.mail, mail :to => user.mail,
:subject => l(:mail_subject_reminder, :count => issues.size, :days => days) :subject => l(:mail_subject_reminder, :count => issues.size, :days => days)
end end
#缺陷到期邮件通知 #缺陷到期邮件通知
@ -493,8 +492,8 @@ class Mailer < ActionMailer::Base
@news = news @news = news
@news_url = url_for(:controller => 'news', :action => 'show', :id => news) @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
mail :to => news.recipients, mail :to => news.recipients,
:subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}", :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}",
:filter => true :filter => true
elsif news.course elsif news.course
redmine_headers 'Course' => news.course.id redmine_headers 'Course' => news.course.id
@author = news.author @author = news.author
@ -510,7 +509,7 @@ class Mailer < ActionMailer::Base
end end
# Builds a Mail::Message object used to email recipients of a news' project when a news comment is added. # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added.
# 新增新闻评论时邮件通知 #
# Example: # Example:
# news_comment_added(comment) => Mail::Message object # news_comment_added(comment) => Mail::Message object
# Mailer.news_comment_added(comment) => sends an email to the news' project recipients # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
@ -597,12 +596,12 @@ class Mailer < ActionMailer::Base
cc = wiki_content.page.wiki.watcher_recipients - recipients cc = wiki_content.page.wiki.watcher_recipients - recipients
@wiki_content = wiki_content @wiki_content = wiki_content
@wiki_content_url = url_for(:controller => 'wiki', :action => 'show', @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
:project_id => wiki_content.project, :project_id => wiki_content.project,
:id => wiki_content.page.title) :id => wiki_content.page.title)
mail :to => recipients, mail :to => recipients,
:cc => cc, :cc => cc,
:subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}", :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}",
:filter => true :filter => true
end end
# Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated. # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
@ -619,15 +618,15 @@ class Mailer < ActionMailer::Base
cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients
@wiki_content = wiki_content @wiki_content = wiki_content
@wiki_content_url = url_for(:controller => 'wiki', :action => 'show', @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
:project_id => wiki_content.project, :project_id => wiki_content.project,
:id => wiki_content.page.title) :id => wiki_content.page.title)
@wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff', @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff',
:project_id => wiki_content.project, :id => wiki_content.page.title, :project_id => wiki_content.project, :id => wiki_content.page.title,
:version => wiki_content.version) :version => wiki_content.version)
mail :to => recipients, mail :to => recipients,
:cc => cc, :cc => cc,
:subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}", :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}",
:filter => true :filter => true
end end
# Builds a Mail::Message object used to email the specified user their account information. # Builds a Mail::Message object used to email the specified user their account information.
@ -641,7 +640,7 @@ class Mailer < ActionMailer::Base
@password = password @password = password
@login_url = url_for(:controller => 'account', :action => 'login') @login_url = url_for(:controller => 'account', :action => 'login')
mail :to => user.mail, mail :to => user.mail,
:subject => l(:mail_subject_register, Setting.app_title) :subject => l(:mail_subject_register, Setting.app_title)
end end
# Builds a Mail::Message object used to email all active administrators of an account activation request. # Builds a Mail::Message object used to email all active administrators of an account activation request.
@ -654,10 +653,10 @@ class Mailer < ActionMailer::Base
recipients = User.active.where(:admin => true).all.collect { |u| u.mail }.compact recipients = User.active.where(:admin => true).all.collect { |u| u.mail }.compact
@user = user @user = user
@url = url_for(:controller => 'users', :action => 'index', @url = url_for(:controller => 'users', :action => 'index',
:status => User::STATUS_REGISTERED, :status => User::STATUS_REGISTERED,
:sort_key => 'created_on', :sort_order => 'desc') :sort_key => 'created_on', :sort_order => 'desc')
mail :to => recipients, mail :to => recipients,
:subject => l(:mail_subject_account_activation_request, Setting.app_title) :subject => l(:mail_subject_account_activation_request, Setting.app_title)
end end
# Builds a Mail::Message object used to email the specified user that their account was activated by an administrator. # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator.
@ -670,7 +669,7 @@ class Mailer < ActionMailer::Base
@user = user @user = user
@login_url = url_for(:controller => 'account', :action => 'login') @login_url = url_for(:controller => 'account', :action => 'login')
mail :to => user.mail, mail :to => user.mail,
:subject => l(:mail_subject_register, Setting.app_title) :subject => l(:mail_subject_register, Setting.app_title)
end end
def lost_password(token) def lost_password(token)
@ -678,7 +677,7 @@ class Mailer < ActionMailer::Base
@token = token @token = token
@url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value) @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
mail :to => token.user.mail, mail :to => token.user.mail,
:subject => l(:mail_subject_lost_password, Setting.app_title) :subject => l(:mail_subject_lost_password, Setting.app_title)
end end
def register(token) def register(token)
@ -686,14 +685,14 @@ class Mailer < ActionMailer::Base
@token = token @token = token
@url = url_for(:controller => 'account', :action => 'activate', :token => token.value) @url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
mail :to => token.user.mail, mail :to => token.user.mail,
:subject => l(:mail_subject_register, Setting.app_title) :subject => l(:mail_subject_register, Setting.app_title)
end end
def test_email(user) def test_email(user)
set_language_if_valid(user.language) set_language_if_valid(user.language)
@url = url_for(:controller => 'welcome') @url = url_for(:controller => 'welcome')
mail :to => user.mail, mail :to => user.mail,
:subject => 'forge test' :subject => 'forge test'
end end
# Overrides default deliver! method to prevent from sending an email # Overrides default deliver! method to prevent from sending an email
@ -701,8 +700,8 @@ class Mailer < ActionMailer::Base
def deliver!(mail = @mail) def deliver!(mail = @mail)
set_language_if_valid @initial_language set_language_if_valid @initial_language
return false if (recipients.nil? || recipients.empty?) && return false if (recipients.nil? || recipients.empty?) &&
(cc.nil? || cc.empty?) && (cc.nil? || cc.empty?) &&
(bcc.nil? || bcc.empty?) (bcc.nil? || bcc.empty?)
# Log errors when raise_delivery_errors is set to false, Rails does not # Log errors when raise_delivery_errors is set to false, Rails does not
@ -734,8 +733,8 @@ class Mailer < ActionMailer::Base
user_ids = options[:users] user_ids = options[:users]
scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" + scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" +
" AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" + " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
" AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
) )
scope = scope.where(:assigned_to_id => user_ids) if user_ids.present? scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
scope = scope.where(:project_id => project.id) if project scope = scope.where(:project_id => project.id) if project
@ -799,12 +798,12 @@ class Mailer < ActionMailer::Base
def mail(headers={}) def mail(headers={})
headers.merge! 'X-Mailer' => 'Redmine', headers.merge! 'X-Mailer' => 'Redmine',
'X-Redmine-Host' => Setting.host_name, 'X-Redmine-Host' => Setting.host_name,
'X-Redmine-Site' => Setting.app_title, 'X-Redmine-Site' => Setting.app_title,
'X-Auto-Response-Suppress' => 'OOF', 'X-Auto-Response-Suppress' => 'OOF',
'Auto-Submitted' => 'auto-generated', 'Auto-Submitted' => 'auto-generated',
'From' => Setting.mail_from, 'From' => Setting.mail_from,
'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>" 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
# Removes the author from the recipients and cc # Removes the author from the recipients and cc
# if he doesn't want to receive notifications about what he does # if he doesn't want to receive notifications about what he does
@ -850,7 +849,7 @@ class Mailer < ActionMailer::Base
set_language_if_valid Setting.default_language set_language_if_valid Setting.default_language
super super
end end
def self.deliver_mail(mail) def self.deliver_mail(mail)
return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank? return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
Thread.new do Thread.new do
@ -897,7 +896,7 @@ class Mailer < ActionMailer::Base
end end
def mylogger def mylogger
if Setting.delayjob_enabled? if Setting.delayjob_enabled?
Delayed::Worker.logger Delayed::Worker.logger
else else
Rails.logger Rails.logger
@ -908,11 +907,10 @@ class Mailer < ActionMailer::Base
if email.attachments && email.attachments.any? if email.attachments && email.attachments.any?
email.attachments.each do |attachment| email.attachments.each do |attachment|
obj.attachments << Attachment.create(:container => obj, obj.attachments << Attachment.create(:container => obj,
:file => attachment.decoded, :file => attachment.decoded,
:filename => attachment.filename, :filename => attachment.filename,
:author => user, :author => user,
:content_type => attachment.mime_type)
:content_type => attachment.mime_type)
end end
end end
end end