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

This commit is contained in:
Eugen Rochko 2016-03-19 19:20:07 +01:00
parent e2b846f630
commit 2b116131d7
15 changed files with 185 additions and 12 deletions

View file

@ -0,0 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: (ENV['SMTP_FROM_ADDRESS'] || 'notifications@localhost')
layout 'mailer'
end

View file

@ -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

View file

@ -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

View file

@ -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?
if status.local?
NotificationMailer.favourite(status, account).deliver_later
else
send_interaction_service.(favourite.stream_entry, status.account)
end
favourite
end

View file

@ -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)

View file

@ -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)

View file

@ -10,18 +10,24 @@ 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?
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
private

View file

@ -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?
if reblogged_status.local?
NotificationMailer.reblog(reblogged_status, account).deliver_later
else
send_interaction_service.(reblog.stream_entry, reblogged_status.account)
end
reblog
end

View file

@ -0,0 +1,5 @@
<%= yield %>
---
Mastodon notifications from <%= Rails.configuration.x.local_domain %>

View file

@ -0,0 +1,5 @@
<%= display_name(@me) %>,
Your status was favourited by <%= @account.acct %>:
<%= account_stream_entry_url(@me, @status.stream_entry) %>

View file

@ -0,0 +1,5 @@
<%= display_name(@me) %>,
<%= @account.acct %> is now following you!
<%= url_for_target(@account) %>

View file

@ -0,0 +1,7 @@
<%= display_name(@me) %>,
You were mentioned by <%= @status.account.acct %> in:
<%= @status.content %>
<%= url_for_target(@status) %>

View file

@ -0,0 +1,5 @@
<%= display_name(@me) %>,
Your status was reblogged by <%= @account.acct %>:
<%= account_stream_entry_url(@me, @status.stream_entry) %>

View file

@ -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

View file

@ -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