Browse Source

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

master
Eugen Rochko 8 years ago
parent
commit
2b116131d7
15 changed files with 185 additions and 12 deletions
  1. +4
    -0
      app/mailers/application_mailer.rb
  2. +34
    -0
      app/mailers/notification_mailer.rb
  3. +2
    -1
      app/services/fan_out_on_write_service.rb
  4. +7
    -2
      app/services/favourite_service.rb
  5. +5
    -2
      app/services/process_feed_service.rb
  6. +4
    -1
      app/services/process_interaction_service.rb
  7. +10
    -4
      app/services/process_mentions_service.rb
  8. +7
    -2
      app/services/reblog_service.rb
  9. +5
    -0
      app/views/layouts/mailer.text.erb
  10. +5
    -0
      app/views/notification_mailer/favourite.text.erb
  11. +5
    -0
      app/views/notification_mailer/follow.text.erb
  12. +7
    -0
      app/views/notification_mailer/mention.text.erb
  13. +5
    -0
      app/views/notification_mailer/reblog.text.erb
  14. +61
    -0
      spec/mailers/notification_mailer_spec.rb
  15. +24
    -0
      spec/mailers/previews/notification_mailer_preview.rb

+ 4
- 0
app/mailers/application_mailer.rb View File

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

+ 34
- 0
app/mailers/notification_mailer.rb 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

+ 2
- 1
app/services/fan_out_on_write_service.rb 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


+ 7
- 2
app/services/favourite_service.rb 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?
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



+ 5
- 2
app/services/process_feed_service.rb 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)


+ 4
- 1
app/services/process_interaction_service.rb 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)


+ 10
- 4
app/services/process_mentions_service.rb View File

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



+ 7
- 2
app/services/reblog_service.rb 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?
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



+ 5
- 0
app/views/layouts/mailer.text.erb View File

@@ -0,0 +1,5 @@
<%= yield %>

---

Mastodon notifications from <%= Rails.configuration.x.local_domain %>

+ 5
- 0
app/views/notification_mailer/favourite.text.erb View File

@@ -0,0 +1,5 @@
<%= display_name(@me) %>,

Your status was favourited by <%= @account.acct %>:

<%= account_stream_entry_url(@me, @status.stream_entry) %>

+ 5
- 0
app/views/notification_mailer/follow.text.erb View File

@@ -0,0 +1,5 @@
<%= display_name(@me) %>,

<%= @account.acct %> is now following you!

<%= url_for_target(@account) %>

+ 7
- 0
app/views/notification_mailer/mention.text.erb View File

@@ -0,0 +1,7 @@
<%= display_name(@me) %>,

You were mentioned by <%= @status.account.acct %> in:

<%= @status.content %>

<%= url_for_target(@status) %>

+ 5
- 0
app/views/notification_mailer/reblog.text.erb View File

@@ -0,0 +1,5 @@
<%= display_name(@me) %>,

Your status was reblogged by <%= @account.acct %>:

<%= account_stream_entry_url(@me, @status.stream_entry) %>

+ 61
- 0
spec/mailers/notification_mailer_spec.rb 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

+ 24
- 0
spec/mailers/previews/notification_mailer_preview.rb 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

Loading…
Cancel
Save