logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe
commit: 2b116131d78460e5cbb8eacc30aaaf04106ea0fa
parent: e2b846f630cae00096a42dffeff759d0ef8e1c3e
Author: Eugen Rochko <eugen@zeonfederated.com>
Date:   Sat, 19 Mar 2016 19:20:07 +0100

Adding e-mail notifications about mentions, follows, favourites and reblogs. Fixing another mention recording bug

Diffstat:

Aapp/mailers/application_mailer.rb4++++
Aapp/mailers/notification_mailer.rb34++++++++++++++++++++++++++++++++++
Mapp/services/fan_out_on_write_service.rb3++-
Mapp/services/favourite_service.rb9+++++++--
Mapp/services/process_feed_service.rb7+++++--
Mapp/services/process_interaction_service.rb5++++-
Mapp/services/process_mentions_service.rb14++++++++++----
Mapp/services/reblog_service.rb9+++++++--
Aapp/views/layouts/mailer.text.erb5+++++
Aapp/views/notification_mailer/favourite.text.erb5+++++
Aapp/views/notification_mailer/follow.text.erb5+++++
Aapp/views/notification_mailer/mention.text.erb7+++++++
Aapp/views/notification_mailer/reblog.text.erb5+++++
Aspec/mailers/notification_mailer_spec.rb61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aspec/mailers/previews/notification_mailer_preview.rb24++++++++++++++++++++++++
15 files changed, 185 insertions(+), 12 deletions(-)

diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb @@ -0,0 +1,4 @@ +class ApplicationMailer < ActionMailer::Base + default from: (ENV['SMTP_FROM_ADDRESS'] || 'notifications@localhost') + layout 'mailer' +end diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb @@ -0,0 +1,34 @@ +class NotificationMailer < ApplicationMailer + helper StreamEntriesHelper + helper AtomBuilderHelper + + def mention(mentioned_account, status) + @me = mentioned_account + @status = status + + mail to: @me.user.email, subject: "You were mentioned by #{@status.account.acct}" + end + + def follow(followed_account, follower) + @me = followed_account + @account = follower + + mail to: @me.user.email, subject: "#{@account.acct} is now following you" + end + + def favourite(target_status, from_account) + @me = target_status.account + @account = from_account + @status = target_status + + mail to: @me.user.email, subject: "#{@account.acct} favourited your status" + end + + def reblog(target_status, from_account) + @me = target_status.account + @account = from_account + @status = target_status + + mail to: @me.user.email, subject: "#{@account.acct} reblogged your status" + end +end diff --git a/app/services/fan_out_on_write_service.rb b/app/services/fan_out_on_write_service.rb @@ -16,7 +16,8 @@ class FanOutOnWriteService < BaseService end # Deliver to local mentioned - status.mentions.each do |mentioned_account| + status.mentioned_accounts.each do |mention| + mentioned_account = mention.account next unless mentioned_account.local? push(:mentions, mentioned_account.id, status) end diff --git a/app/services/favourite_service.rb b/app/services/favourite_service.rb @@ -6,8 +6,13 @@ class FavouriteService < BaseService def call(account, status) favourite = Favourite.create!(account: account, status: status) account.ping!(account_url(account, format: 'atom'), [Rails.configuration.x.hub_url]) - return favourite if status.local? - send_interaction_service.(favourite.stream_entry, status.account) + + if status.local? + NotificationMailer.favourite(status, account).deliver_later + else + send_interaction_service.(favourite.stream_entry, status.account) + end + favourite end diff --git a/app/services/process_feed_service.rb b/app/services/process_feed_service.rb @@ -51,11 +51,11 @@ class ProcessFeedService < BaseService unless mentioned_account.nil? mentioned_account.mentions.where(status: status).first_or_create(status: status) + NotificationMailer.mention(mentioned_account, status).deliver_later end end end - fan_out_on_write_service.(status) end end @@ -74,7 +74,10 @@ class ProcessFeedService < BaseService status.reblog = fetch_remote_status(entry) end - status.save! unless status.reblog.nil? + if !status.reblog.nil? + status.save! + NotificationMailer.reblog(status.reblog, status.account).deliver_later + end end def add_reply!(entry, status) diff --git a/app/services/process_interaction_service.rb b/app/services/process_interaction_service.rb @@ -55,6 +55,7 @@ class ProcessInteractionService < BaseService def follow!(account, target_account) account.follow!(target_account) + NotificationMailer.follow(target_account, account).deliver_later end def unfollow!(account, target_account) @@ -62,7 +63,9 @@ class ProcessInteractionService < BaseService end def favourite!(xml, from_account) - status(xml).favourites.where(account: from_account).first_or_create!(account: from_account) + current_status = status(xml) + current_status.favourites.where(account: from_account).first_or_create!(account: from_account) + NotificationMailer.favourite(current_status, from_account).deliver_later end def add_post!(body, account) diff --git a/app/services/process_mentions_service.rb b/app/services/process_mentions_service.rb @@ -10,16 +10,22 @@ class ProcessMentionsService < BaseService username, domain = match.first.split('@') mentioned_account = Account.find_by(username: username, domain: domain) - if mentioned_account.nil? + if mentioned_account.nil? && !domain.nil? mentioned_account = follow_remote_account_service.("#{match.first}") + next if mentioned_account.nil? end mentioned_account.mentions.where(status: status).first_or_create(status: status) end - status.mentions.each do |mentioned_account| - next if mentioned_account.local? - send_interaction_service.(status.stream_entry, mentioned_account) + status.mentioned_accounts.each do |mention| + mentioned_account = mention.account + + if mentioned_account.local? + NotificationMailer.mention(mentioned_account, status).deliver_later + else + send_interaction_service.(status.stream_entry, mentioned_account) + end end end diff --git a/app/services/reblog_service.rb b/app/services/reblog_service.rb @@ -7,8 +7,13 @@ class ReblogService < BaseService reblog = account.statuses.create!(reblog: reblogged_status, text: '') fan_out_on_write_service.(reblog) account.ping!(account_url(account, format: 'atom'), [Rails.configuration.x.hub_url]) - return reblog if reblogged_status.local? - send_interaction_service.(reblog.stream_entry, reblogged_status.account) + + if reblogged_status.local? + NotificationMailer.reblog(reblogged_status, account).deliver_later + else + send_interaction_service.(reblog.stream_entry, reblogged_status.account) + end + reblog end diff --git a/app/views/layouts/mailer.text.erb b/app/views/layouts/mailer.text.erb @@ -0,0 +1,5 @@ +<%= yield %> + +--- + +Mastodon notifications from <%= Rails.configuration.x.local_domain %> diff --git a/app/views/notification_mailer/favourite.text.erb b/app/views/notification_mailer/favourite.text.erb @@ -0,0 +1,5 @@ +<%= display_name(@me) %>, + +Your status was favourited by <%= @account.acct %>: + +<%= account_stream_entry_url(@me, @status.stream_entry) %> diff --git a/app/views/notification_mailer/follow.text.erb b/app/views/notification_mailer/follow.text.erb @@ -0,0 +1,5 @@ +<%= display_name(@me) %>, + +<%= @account.acct %> is now following you! + +<%= url_for_target(@account) %> diff --git a/app/views/notification_mailer/mention.text.erb b/app/views/notification_mailer/mention.text.erb @@ -0,0 +1,7 @@ +<%= display_name(@me) %>, + +You were mentioned by <%= @status.account.acct %> in: + +<%= @status.content %> + +<%= url_for_target(@status) %> diff --git a/app/views/notification_mailer/reblog.text.erb b/app/views/notification_mailer/reblog.text.erb @@ -0,0 +1,5 @@ +<%= display_name(@me) %>, + +Your status was reblogged by <%= @account.acct %>: + +<%= account_stream_entry_url(@me, @status.stream_entry) %> diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb @@ -0,0 +1,61 @@ +require "rails_helper" + +RSpec.describe NotificationMailer, type: :mailer do + let(:receiver) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) } + let(:sender) { Fabricate(:account, username: 'bob') } + let(:foreign_status) { Fabricate(:status, account: sender) } + let(:own_status) { Fabricate(:status, account: receiver.account) } + + describe "mention" do + let(:mail) { NotificationMailer.mention(receiver.account, foreign_status) } + + it "renders the headers" do + expect(mail.subject).to eq("You were mentioned by bob") + expect(mail.to).to eq([receiver.email]) + end + + it "renders the body" do + expect(mail.body.encoded).to match("You were mentioned by bob") + end + end + + describe "follow" do + let(:mail) { NotificationMailer.follow(receiver.account, sender) } + + it "renders the headers" do + expect(mail.subject).to eq("bob is now following you") + expect(mail.to).to eq([receiver.email]) + end + + it "renders the body" do + expect(mail.body.encoded).to match("bob is now following you") + end + end + + describe "favourite" do + let(:mail) { NotificationMailer.favourite(own_status, sender) } + + it "renders the headers" do + expect(mail.subject).to eq("bob favourited your status") + expect(mail.to).to eq([receiver.email]) + end + + it "renders the body" do + expect(mail.body.encoded).to match("Your status was favourited by bob") + end + end + + describe "reblog" do + let(:mail) { NotificationMailer.reblog(own_status, sender) } + + it "renders the headers" do + expect(mail.subject).to eq("bob reblogged your status") + expect(mail.to).to eq([receiver.email]) + end + + it "renders the body" do + expect(mail.body.encoded).to match("Your status was reblogged by bob") + end + end + +end diff --git a/spec/mailers/previews/notification_mailer_preview.rb b/spec/mailers/previews/notification_mailer_preview.rb @@ -0,0 +1,24 @@ +# Preview all emails at http://localhost:3000/rails/mailers/notification_mailer +class NotificationMailerPreview < ActionMailer::Preview + + # Preview this email at http://localhost:3000/rails/mailers/notification_mailer/mention + def mention + # NotificationMailer.mention + end + + # Preview this email at http://localhost:3000/rails/mailers/notification_mailer/follow + def follow + # NotificationMailer.follow + end + + # Preview this email at http://localhost:3000/rails/mailers/notification_mailer/favourite + def favourite + # NotificationMailer.favourite + end + + # Preview this email at http://localhost:3000/rails/mailers/notification_mailer/reblog + def reblog + # NotificationMailer.reblog + end + +end