]> cat aescling's git repositories - mastodon.git/commitdiff
Adding e-mail notifications about mentions, follows, favourites and reblogs. Fixing...
authorEugen Rochko <eugen@zeonfederated.com>
Sat, 19 Mar 2016 18:20:07 +0000 (19:20 +0100)
committerEugen Rochko <eugen@zeonfederated.com>
Sat, 19 Mar 2016 18:20:07 +0000 (19:20 +0100)
15 files changed:
app/mailers/application_mailer.rb [new file with mode: 0644]
app/mailers/notification_mailer.rb [new file with mode: 0644]
app/services/fan_out_on_write_service.rb
app/services/favourite_service.rb
app/services/process_feed_service.rb
app/services/process_interaction_service.rb
app/services/process_mentions_service.rb
app/services/reblog_service.rb
app/views/layouts/mailer.text.erb [new file with mode: 0644]
app/views/notification_mailer/favourite.text.erb [new file with mode: 0644]
app/views/notification_mailer/follow.text.erb [new file with mode: 0644]
app/views/notification_mailer/mention.text.erb [new file with mode: 0644]
app/views/notification_mailer/reblog.text.erb [new file with mode: 0644]
spec/mailers/notification_mailer_spec.rb [new file with mode: 0644]
spec/mailers/previews/notification_mailer_preview.rb [new file with mode: 0644]

diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb
new file mode 100644 (file)
index 0000000..1a3196a
--- /dev/null
@@ -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
new file mode 100644 (file)
index 0000000..1414c22
--- /dev/null
@@ -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
index 87a7c55ac805eed0a33b22a1dd8f261c0e776a96..87a83e892bf5c50a5df1263f54811a9e268227db 100644 (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
index 547811e73a4eebd48ef0c6bbdaf0bcfef8324148..4a8ecfacbf6e460b8b740bb0dd5b7cc65d81647a 100644 (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
 
index ccdf07cc112679c0e9712b3e945a1ad11d39704d..504d58aed91bb0ea08bc4cad33dbb2f1909e82b7 100644 (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)
index cdc72008390dcb08423e083c4d0086cdea593498..c00f8c66adb1377101c2205d52b3e72096724988 100644 (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)
index 6e92d16b45bd717a274ab54faa4231d8c954cd2f..d566b65c7a6af723b50cc210af9648f538c00dd5 100644 (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
 
index 7078e3aff25a40414e3faad3722e7f510b170395..606970e2343fca015ad391c98f07d69b0e41b616 100644 (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
 
diff --git a/app/views/layouts/mailer.text.erb b/app/views/layouts/mailer.text.erb
new file mode 100644 (file)
index 0000000..6327024
--- /dev/null
@@ -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
new file mode 100644 (file)
index 0000000..e8f7c11
--- /dev/null
@@ -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
new file mode 100644 (file)
index 0000000..6f70c0d
--- /dev/null
@@ -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
new file mode 100644 (file)
index 0000000..c12841e
--- /dev/null
@@ -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
new file mode 100644 (file)
index 0000000..253b22c
--- /dev/null
@@ -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
new file mode 100644 (file)
index 0000000..d7a956b
--- /dev/null
@@ -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
new file mode 100644 (file)
index 0000000..8fc8d0d
--- /dev/null
@@ -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