Skip to content

Commit

Permalink
Wiki email notification WIP:
Browse files Browse the repository at this point in the history
done notification on attachments addition;
added notification on attachments removal for all containers (WikiPage, Project/Version, Document)
  • Loading branch information
Artem Vasiliev committed Jan 20, 2009
1 parent 10baff4 commit d86251e
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 972 deletions.
2 changes: 1 addition & 1 deletion README.rdoc
Expand Up @@ -25,6 +25,6 @@ and remove the number it had in patch (to give a way for migration with the same

delete from schema_migrations where version in ('995')

This version is based on r1783 of http://redmine.rubyforge.org/svn/trunk.
This version is based on r2282 of http://redmine.rubyforge.org/svn/trunk (so it's Redmine 0.8.1+).

Contributed by Texuna Technologies.
12 changes: 11 additions & 1 deletion app/controllers/attachments_controller.rb
Expand Up @@ -47,10 +47,20 @@ def download
options.merge!(:type => @attachment.content_type) if @attachment.content_type
send_file @attachment.diskfile, options
end


def to_notify?
notification_key = @attachment.container.notification_key if @attachment.container.respond_to?(:notification_key)
notification_key ||= NotificationKeys::FILE_ADDED
Setting.notified_events.include?(notification_key)
end

def destroy
# Make sure association callbacks are called
@attachment.container.attachments.delete(@attachment)

if to_notify?
Mailer.deliver_attachments_removed([@attachment])
end
redirect_to :back
rescue ::ActionController::RedirectBackError
redirect_to :controller => 'projects', :action => 'show', :id => @project
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/projects_controller.rb
Expand Up @@ -193,7 +193,7 @@ def add_file
if request.post?
container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id]))
attachments = attach_files(container, params[:attachments])
if !attachments.empty? && Setting.notified_events.include?('file_added')
if !attachments.empty? && Setting.notified_events.include?(NotificationKeys::FILE_ADDED)
Mailer.deliver_attachments_added(attachments)
end
redirect_to :controller => 'projects', :action => 'list_files', :id => @project
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/settings_controller.rb
Expand Up @@ -24,7 +24,7 @@ def index
end

def edit
@notifiables = %w(issue_added issue_updated news_added document_added file_added message_posted)
@notifiables = NotificationKeys::all
if request.post? && params[:settings] && params[:settings].is_a?(Hash)
settings = (params[:settings] || {}).dup.symbolize_keys
settings.each do |name, value|
Expand Down
26 changes: 20 additions & 6 deletions app/controllers/wiki_controller.rb
Expand Up @@ -73,7 +73,7 @@ def edit
else
if !@page.new_record? && @content.text == params[:content][:text]
# don't save if text wasn't changed
redirect_to :action => 'index', :id => @project, :page => @page.title
redirect_to page_link
return
end
#@content.text = params[:content][:text]
Expand All @@ -82,7 +82,7 @@ def edit
@content.author = User.current
# if page is new @page.save will also save content, but not if page isn't a new record
if (@page.new_record? ? @page.save : @content.save)
redirect_to :action => 'index', :id => @project, :page => @page.title
redirect_to page_link
end
end
rescue ActiveRecord::StaleObjectError
Expand All @@ -98,13 +98,13 @@ def rename
@original_title = @page.pretty_title
if request.post? && @page.update_attributes(params[:wiki_page])
flash[:notice] = l(:notice_successful_update)
redirect_to :action => 'index', :id => @project, :page => @page.title
redirect_to page_link
end
end

def protect
@page.update_attribute :protected, params[:protected]
redirect_to :action => 'index', :id => @project, :page => @page.title
redirect_to page_link
end

# show page history
Expand Down Expand Up @@ -177,12 +177,26 @@ def preview

def add_attachment
return render_403 unless editable?
attach_files(@page, params[:attachments])
attachments = attach_files(@page, params[:attachments])
if !attachments.empty? &&
Setting.notified_events.include?(NotificationKeys::WIKI_EDIT)

Mailer.deliver_attachments_added(attachments)
end
redirect_to :action => 'index', :page => @page.title
end


def self.page_link(page)
{:action => 'index', :id => page.project, :page => page.title}
end

private


def page_link()
self.class.page_link(@page)
end

def find_wiki
@project = Project.find(params[:id])
@wiki = @project.wiki
Expand Down
4 changes: 4 additions & 0 deletions app/models/document.rb
Expand Up @@ -34,4 +34,8 @@ def after_initialize
self.category ||= Enumeration.default('DCAT')
end
end

def notification_key
NotificationKeys::DOCUMENT_ADDED
end
end
56 changes: 37 additions & 19 deletions app/models/mailer.rb
Expand Up @@ -75,26 +75,16 @@ def document_added(document)
end

def attachments_added(attachments)
container = attachments.first.container
added_to = ''
added_to_url = ''
case container.class.name
when 'Project'
added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container)
added_to = "#{l(:label_project)}: #{container}"
when 'Version'
added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id)
added_to = "#{l(:label_version)}: #{container.name}"
when 'Document'
added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
added_to = "#{l(:label_document)}: #{container.title}"
attachments_action(attachments) do |container|
subject "[#{container.project.name}] #{l(:label_attachment_new)}"
end
end

def attachments_removed(attachments)
attachments_action(attachments) do |container|
subject "[%s] %s" % [container.project.name, l(:label_attachment_deleted)]
self.template = :attachments_added
end
redmine_headers 'Project' => container.project.identifier
recipients container.project.recipients
subject "[#{container.project.name}] #{l(:label_attachment_new)}"
body :attachments => attachments,
:added_to => added_to,
:added_to_url => added_to_url
end

def news_added(news)
Expand Down Expand Up @@ -286,6 +276,34 @@ def references(object)
@references_objects ||= []
@references_objects << object
end

def attachments_action(attachments)
container = attachments.first.container
added_to = ''
added_to_url = ''
case container.class.name
when 'Project'
added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container)
added_to = "#{l(:label_project)}: #{container}"
when 'Version'
added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id)
added_to = "#{l(:label_version)}: #{container.name}"
when 'Document'
added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
added_to = "#{l(:label_document)}: #{container.title}"
when 'WikiPage'
added_to = "#{l(:label_wiki)}: #{container.title}"
added_to_url = url_for({:controller => 'wiki'}.
merge(WikiController.page_link(container)))
end
redmine_headers 'Project' => container.project.identifier
recipients container.project.recipients
yield(container)
body :attachments => attachments,
:added_to => added_to,
:added_to_url => added_to_url
end

end

# Patch TMail so that message_id is not overwritten
Expand Down
14 changes: 14 additions & 0 deletions app/models/notification_keys.rb
@@ -0,0 +1,14 @@
module NotificationKeys
ISSUE_ADDED = 'issue_added'
ISSUE_UPDATED = 'issue_updated'
NEWS_ADDED = 'news_added'
DOCUMENT_ADDED = 'document_added'
FILE_ADDED = 'file_added'
WIKI_EDIT = 'wiki_edit'
MESSAGE_POSTED = 'message_posted'

def self.all
[ISSUE_ADDED, ISSUE_UPDATED, NEWS_ADDED, DOCUMENT_ADDED, FILE_ADDED,
WIKI_EDIT, MESSAGE_POSTED]
end
end
4 changes: 4 additions & 0 deletions app/models/wiki_page.rb
Expand Up @@ -126,6 +126,10 @@ def parent_title=(t)
self.parent = parent_page
end

def notification_key
NotificationKeys::WIKI_EDIT
end

protected

def validate
Expand Down
1 change: 1 addition & 0 deletions lang/en.yml
Expand Up @@ -382,6 +382,7 @@ label_history: History
label_attachment: File
label_attachment_new: New file
label_attachment_delete: Delete file
label_attachment_deleted: File was deleted
label_attachment_plural: Files
label_file_added: File added
label_report: Report
Expand Down
1 change: 1 addition & 0 deletions lang/ru.yml
Expand Up @@ -258,6 +258,7 @@ label_associated_revisions: Связанные редакции
label_attachment: Файл
label_attachment_delete: Удалить файл
label_attachment_new: Новый файл
label_attachment_deleted: Файл удалён
label_attachment_plural: Файлы
label_attribute: Атрибут
label_attribute_plural: атрибуты
Expand Down

0 comments on commit d86251e

Please sign in to comment.