PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/app/mailers/user_mailer.rb

https://github.com/velocitygrass/otwarchive
Ruby | 374 lines | 311 code | 36 blank | 27 comment | 25 complexity | 39a83eb27f448468e82afe66a752606e MD5 | raw file
  1. class UserMailer < ActionMailer::Base
  2. include Resque::Mailer # see README in this directory
  3. layout 'mailer'
  4. include AuthlogicHelpersForMailers # otherwise any logged_in? checks in views will choke and die! :)
  5. helper_method :current_user
  6. helper_method :current_admin
  7. helper_method :logged_in?
  8. helper_method :logged_in_as_admin?
  9. helper :application
  10. helper :tags
  11. helper :works
  12. helper :users
  13. helper :date
  14. include HtmlCleaner
  15. default :from => ArchiveConfig.RETURN_ADDRESS
  16. # Sends an invitation to join the archive
  17. # Must be sent synchronously as it is rescued
  18. # TODO refactor to make it asynchronous
  19. def invitation(invitation)
  20. @invitation = invitation
  21. @user_name = (@invitation.creator.is_a?(User) ? @invitation.creator.login : '')
  22. mail(
  23. :to => @invitation.invitee_email,
  24. :subject => "[#{ArchiveConfig.APP_NAME}] Invitation"
  25. )
  26. end
  27. # Sends an invitation to join the archive and claim stories that have been imported as part of a bulk import
  28. # Must be sent synchronously as it is rescued
  29. # TODO refactor to make it asynchronous
  30. def invitation_to_claim(invitation, archivist_login)
  31. @external_author = invitation.external_author
  32. @archivist = archivist_login || "An archivist"
  33. @token = invitation.token
  34. mail(
  35. :to => invitation.invitee_email,
  36. :subject => "[#{ArchiveConfig.APP_NAME}] Invitation To Claim Stories"
  37. )
  38. end
  39. # Notifies a writer that their imported works have been claimed
  40. def claim_notification(external_author_id, claimed_work_ids)
  41. external_author = ExternalAuthor.find(external_author_id)
  42. @external_email = external_author.email
  43. @claimed_works = Work.find(claimed_work_ids)
  44. mail(
  45. :to => external_author.user.email,
  46. :subject => "[#{ArchiveConfig.APP_NAME}] Stories Uploaded"
  47. )
  48. end
  49. def subscription_notification(user_id, subscription_id, creation_id, creation_class_name)
  50. user = User.find(user_id)
  51. @subscription = Subscription.find(subscription_id)
  52. @creation = creation_class_name.constantize.find(creation_id)
  53. if @subscription.subscribable_type == 'User'
  54. subject_text = "#{@subscription.name} has posted "
  55. if creation_class_name == 'Chapter'
  56. subject_text << "Chapter #{@creation.position} of \"#{@creation.work.title}\""
  57. elsif creation_class_name == 'Work'
  58. subject_text << "\"#{@creation.title}\""
  59. end
  60. else
  61. subject_text = "Subscription Notice for #{@subscription.name}"
  62. end
  63. mail(
  64. :to => user.email,
  65. :subject => "[#{ArchiveConfig.APP_NAME}] #{subject_text}"
  66. )
  67. end
  68. # Emails a user to say they have been given more invitations for their friends
  69. def invite_increase_notification(user_id, total)
  70. @user = User.find(user_id)
  71. @total = total
  72. mail(
  73. :to => @user.email,
  74. :subject => "[#{ArchiveConfig.APP_NAME}] New Invitations"
  75. )
  76. end
  77. # Sends an admin message to a user
  78. def archive_notification(admin_login, user_id, subject, message)
  79. @user = User.find(user_id)
  80. @message = message
  81. @admin_login = admin_login
  82. mail(
  83. :to => @user.email,
  84. :subject => "[#{ArchiveConfig.APP_NAME}] Admin Message #{subject}"
  85. )
  86. end
  87. # Sends an admin message to an array of users
  88. def mass_archive_notification(admin, users, subject, message)
  89. users.each do |user|
  90. archive_notification(admin, user, subject, message)
  91. end
  92. end
  93. def collection_notification(collection_id, subject, message)
  94. @message = message
  95. @collection = Collection.find(collection_id)
  96. mail(
  97. :to => @collection.get_maintainers_email,
  98. :subject => "[#{ArchiveConfig.APP_NAME}][#{@collection.title}] #{subject}"
  99. )
  100. end
  101. def potential_match_generation_notification(collection_id)
  102. @collection = Collection.find(collection_id)
  103. mail(
  104. :to => @collection.get_maintainers_email,
  105. :subject => "[#{ArchiveConfig.APP_NAME}][#{@collection.title}] Potential Assignment Generation Complete"
  106. )
  107. end
  108. def challenge_assignment_notification(collection_id, assigned_user_id, assignment_id)
  109. @collection = Collection.find(collection_id)
  110. @assigned_user = User.find(assigned_user_id)
  111. assignment = ChallengeAssignment.find(assignment_id)
  112. @request = (assignment.request_signup || assignment.pinch_request_signup)
  113. mail(
  114. :to => @assigned_user.email,
  115. :subject => "[#{ArchiveConfig.APP_NAME}][#{@collection.title}] Your Assignment!"
  116. )
  117. end
  118. # Asks a user to validate and activate their new account
  119. def signup_notification(user_id)
  120. @user = User.find(user_id)
  121. mail(
  122. :to => @user.email,
  123. :subject => "[#{ArchiveConfig.APP_NAME}] Please activate your new account"
  124. )
  125. end
  126. # Emails a user to confirm that their account is validated and activated
  127. def activation(user_id)
  128. @user = User.find(user_id)
  129. mail(
  130. :to => @user.email,
  131. :subject => "[#{ArchiveConfig.APP_NAME}] Your account has been activated."
  132. )
  133. end
  134. # Sends a temporary password to the user
  135. def reset_password(user_id, activation_code)
  136. @user = User.find(user_id)
  137. @password = activation_code
  138. mail(
  139. :to => @user.email,
  140. :subject => "[#{ArchiveConfig.APP_NAME}] Generated password"
  141. )
  142. end
  143. # Confirms to a user that their email was changed
  144. def change_email(user_id, old_email, new_email)
  145. @user = User.find(user_id)
  146. @old_email= old_email
  147. @new_email= new_email
  148. mail(
  149. :to => @old_email,
  150. :subject => "[#{ArchiveConfig.APP_NAME}] Email changed"
  151. )
  152. end
  153. ### WORKS NOTIFICATIONS ###
  154. # Sends email when a user is added as a co-author
  155. def coauthor_notification(user_id, creation_id, creation_class_name)
  156. @user = User.find(user_id)
  157. @creation = creation_class_name.constantize.find(creation_id)
  158. mail(
  159. :to => @user.email,
  160. :subject => "[#{ArchiveConfig.APP_NAME}] Co-Author Notification"
  161. )
  162. end
  163. # Sends emails to authors whose stories were listed as the inspiration of another work
  164. def related_work_notification(user_id, related_work_id)
  165. @user = User.find(user_id)
  166. @related_work = RelatedWork.find(related_work_id)
  167. @related_parent_link = url_for(:controller => :works, :action => :show, :id => @related_work.parent)
  168. @related_child_link = url_for(:controller => :works, :action => :show, :id => @related_work.work)
  169. mail(
  170. :to => @user.email,
  171. :subject => "[#{ArchiveConfig.APP_NAME}] Related work notification"
  172. )
  173. end
  174. # Emails a recipient to say that a gift has been posted for them
  175. def recipient_notification(user_id, work_id, collection_id=nil)
  176. @user = User.find(user_id)
  177. @work = Work.find(work_id)
  178. @collection = Collection.find(collection_id) if collection_id
  179. mail(
  180. :to => @user.email,
  181. :subject => "[#{ArchiveConfig.APP_NAME}]#{@collection ? '[' + @collection.title + ']' : ''} A Gift Story For You #{@collection ? 'From ' + @collection.title : ''}"
  182. )
  183. end
  184. # Emails a prompter to say that a response has been posted to their prompt
  185. def prompter_notification(work_id, collection_id=nil)
  186. @work = Work.find(work_id)
  187. @collection = Collection.find(collection_id) if collection_id
  188. @work.challenge_claims.each do |claim|
  189. user = User.find(claim.request_signup.pseud.user.id)
  190. mail(
  191. :to => user.email,
  192. :subject => "[#{ArchiveConfig.APP_NAME}] A Response to your Prompt"
  193. )
  194. end
  195. end
  196. # Sends email to coauthors when a work is edited
  197. # NOTE: this must be sent synchronously! otherwise the new version will be sent.
  198. # TODO refactor to make it asynchronous by passing the content in the method
  199. def edit_work_notification(user, work)
  200. @user = user
  201. @work = work
  202. mail(
  203. :to => user.email,
  204. :subject => "[#{ArchiveConfig.APP_NAME}] Your story has been updated"
  205. )
  206. end
  207. # Sends email to authors when a creation is deleted
  208. # NOTE: this must be sent synchronously! otherwise the work will no longer be there to send
  209. # TODO refactor to make it asynchronous by passing the content in the method
  210. def delete_work_notification(user, work)
  211. @user = user
  212. @work = work
  213. work_copy = generate_attachment_content_from_work(work)
  214. filename = work.title.gsub(/[*:?<>|\/\\\"]/,'')
  215. attachments["#{filename}.txt"] = {:content => work_copy}
  216. attachments["#{filename}.html"] = {:content => work_copy}
  217. mail(
  218. :to => user.email,
  219. :subject => "[#{ArchiveConfig.APP_NAME}] Your story has been deleted"
  220. )
  221. end
  222. def delete_signup_notification(user, challenge_signup)
  223. @user = user
  224. @signup = challenge_signup
  225. signup_copy = generate_attachment_content_from_signup(@signup)
  226. filename = @signup.collection.title.gsub(/[*:?<>|\/\\\"]/,'')
  227. attachments["#{filename}.txt"] = {:content => signup_copy}
  228. attachments["#{filename}.html"] = {:content => signup_copy}
  229. mail(
  230. :to => user.email,
  231. :subject => "[#{ArchiveConfig.APP_NAME}] Your signup for #{@signup.collection.title} has been deleted"
  232. )
  233. end
  234. ### OTHER NOTIFICATIONS ###
  235. # archive feedback
  236. def feedback(feedback_id)
  237. feedback = Feedback.find(feedback_id)
  238. return unless feedback.email
  239. @summary = feedback.summary
  240. @comment = feedback.comment
  241. mail(
  242. :to => feedback.email,
  243. :subject => "#{ArchiveConfig.APP_NAME}: Support - #{strip_html_breaks_simple(feedback.summary)}"
  244. )
  245. end
  246. def abuse_report(report_id)
  247. report = AbuseReport.find(report_id)
  248. setup_email_without_name(report.email)
  249. @url = report.url
  250. @comment = report.comment
  251. mail(
  252. :to => report.email,
  253. :subject => "[#{ArchiveConfig.APP_NAME}] Your abuse report"
  254. )
  255. end
  256. def generate_attachment_content_from_work(work)
  257. attachment_string = "Title: " + work.title + "<br />" + "by " + work.pseuds.collect(&:name).join(", ") + "<br />\n"
  258. attachment_string += "<br/>Tags: " + work.tags.collect(&:name).join(", ") + "<br/>\n" unless work.tags.blank?
  259. attachment_string += "<br/>Summary: " + work.summary + "<br/>\n" unless work.summary.blank?
  260. attachment_string += "<br/>Notes: " + work.notes + "<br/>\n" unless work.notes.blank?
  261. attachment_string += "<br/>End Notes: " + work.endnotes + "<br/>\n" unless work.endnotes.blank?
  262. attachment_string += "<br/>Published at: " + work.first_chapter.published_at.to_s + "<br/>\n" unless work.first_chapter.published_at.blank?
  263. attachment_string += "Revised at: " + work.revised_at.to_s + "<br/>\n" unless work.revised_at.blank?
  264. work.chapters.each do |chapter|
  265. attachment_string += "<br/>Chapter " + chapter.position.to_s unless !work.chaptered?
  266. attachment_string += ": " + chapter.title unless chapter.title.blank?
  267. attachment_string += "\n<br/>by: " + chapter.pseuds.collect(&:name).join(", ") + "<br />\n" unless chapter.pseuds.sort == work.pseuds.sort
  268. attachment_string += "<br/>Summary: " + chapter.summary + "<br/>\n" unless chapter.summary.blank?
  269. attachment_string += "<br/>Notes: " + chapter.notes + "<br/>\n" unless chapter.notes.blank?
  270. attachment_string += "<br/>End Notes: " + chapter.endnotes + "<br/>\n" unless chapter.endnotes.blank?
  271. attachment_string += "<br/>" + chapter.content + "<br />\n"
  272. end
  273. return attachment_string
  274. end
  275. def generate_attachment_content_from_signup(signup)
  276. attachment_string = "Collection: " + signup.collection + "<br />\n"
  277. signup.requests.each_with_index do |prompt, index|
  278. attachment_string += "Request " + index+1 + ":<br />\n"
  279. any_types = TagSet::TAG_TYPES.select {|type| prompt.send("any_#{type}")}
  280. if any_types || (prompt.tag_set && !prompt.tag_set.tags.empty?)
  281. attachment_string += "Tags: "
  282. attachment_string += prompt.tag_set && !prompt.tag_set.tags.empty? ? tag_link_list(prompt.tag_set.tags, link_to_works=true) + (any_types.empty? ? "" : ", ") : ""
  283. unless any_types.empty?
  284. attachment_string += any_types.map {|type| content_tag(:li, ts("Any %{type}", :type => type.capitalize)) }.join(", ").html_safe
  285. end
  286. if prompt.optional_tag_set && !prompt.optional_tag_set.tags.empty?
  287. attachment_string += "<br />\nOptional: "
  288. attachment_string += tag_link_list(prompt.optional_tag_set.tags, link_to_works=true)
  289. end
  290. attachment_string += "<br />\n"
  291. end
  292. unless prompt.url.blank?
  293. url_label = prompt.collection.challenge.send("request_url_label")
  294. attachment_string += url_label.blank? ? "URL" : url_label
  295. attachment_string += ": " + link_to(prompt.url, prompt.url) + "<br />\n"
  296. end
  297. unless prompt.description.blank?
  298. desc_label = prompt.collection.challenge.send("request_description_label")
  299. attachment_string += desc_label.blank? ? ts("Details") : desc_label
  300. attachment_string += ": " + prompt.description + "<br />\n"
  301. end
  302. if prompt.anonymous?
  303. attachment_string += "Anonymous request" + "<br />\n"
  304. end
  305. end
  306. signup.offers.each_with_index do |offer, index|
  307. attachment_string += "Offer " + index+1 + ":<br />\n"
  308. any_types = TagSet::TAG_TYPES.select {|type| prompt.send("any_#{type}")}
  309. if any_types || (prompt.tag_set && !prompt.tag_set.tags.empty?)
  310. attachment_string += "Tags: "
  311. attachment_string += prompt.tag_set && !prompt.tag_set.tags.empty? ? tag_link_list(prompt.tag_set.tags, link_to_works=true) + (any_types.empty? ? "" : ", ") : ""
  312. unless any_types.empty?
  313. attachment_string += any_types.map {|type| content_tag(:li, ts("Any %{type}", :type => type.capitalize)) }.join(", ").html_safe
  314. end
  315. if prompt.optional_tag_set && !prompt.optional_tag_set.tags.empty?
  316. attachment_string += "<br />\nOptional: "
  317. attachment_string += tag_link_list(prompt.optional_tag_set.tags, link_to_works=true)
  318. end
  319. attachment_string += "<br />\n"
  320. end
  321. unless prompt.url.blank?
  322. url_label = prompt.collection.challenge.send("request_url_label")
  323. attachment_string += url_label.blank? ? "URL" : url_label
  324. attachment_string += ": " + link_to(prompt.url, prompt.url) + "<br />\n"
  325. end
  326. unless prompt.description.blank?
  327. desc_label = prompt.collection.challenge.send("request_description_label")
  328. attachment_string += desc_label.blank? ? ts("Details") : desc_label
  329. attachment_string += ": " + prompt.description + "<br />\n"
  330. end
  331. if prompt.anonymous?
  332. attachment_string += "Anonymous request" + "<br />\n"
  333. end
  334. end
  335. return attachment_string
  336. end
  337. protected
  338. end