about summary refs log tree commit diff
path: root/app/services/process_mentions_service.rb
diff options
context:
space:
mode:
authorDavid Yip <yipdw@member.fsf.org>2018-05-03 17:23:44 -0500
committerDavid Yip <yipdw@member.fsf.org>2018-05-03 17:23:44 -0500
commitc816701550d7cdb593371dc47d0b9430c78308b0 (patch)
treecc4417d14de20e69fd5f9a58d66f84af4a623329 /app/services/process_mentions_service.rb
parent3a47842223ff93d8c057f804809f1b111dfd6f76 (diff)
parenta7e71bbd08e089938fbf20ddef5768c2f3ee0702 (diff)
Merge remote-tracking branch 'origin/master' into gs-master
  Conflicts:
 	.travis.yml
 	Gemfile.lock
 	README.md
 	app/controllers/settings/follower_domains_controller.rb
 	app/controllers/statuses_controller.rb
 	app/javascript/mastodon/locales/ja.json
 	app/lib/feed_manager.rb
 	app/models/media_attachment.rb
 	app/models/mute.rb
 	app/models/status.rb
 	app/services/mute_service.rb
 	app/views/home/index.html.haml
 	app/views/stream_entries/_simple_status.html.haml
 	config/locales/ca.yml
 	config/locales/en.yml
 	config/locales/es.yml
 	config/locales/fr.yml
 	config/locales/nl.yml
 	config/locales/pl.yml
 	config/locales/pt-BR.yml
 	config/themes.yml
Diffstat (limited to 'app/services/process_mentions_service.rb')
-rw-r--r--app/services/process_mentions_service.rb44
1 files changed, 25 insertions, 19 deletions
diff --git a/app/services/process_mentions_service.rb b/app/services/process_mentions_service.rb
index dc8df4a9a..2ed6698cf 100644
--- a/app/services/process_mentions_service.rb
+++ b/app/services/process_mentions_service.rb
@@ -10,55 +10,61 @@ class ProcessMentionsService < BaseService
   def call(status)
     return unless status.local?
 
+    @status  = status
+    mentions = []
+
     status.text = status.text.gsub(Account::MENTION_RE) do |match|
-      username, domain  = $1.split('@')
+      username, domain  = Regexp.last_match(1).split('@')
       mentioned_account = Account.find_remote(username, domain)
 
-      if mention_undeliverable?(status, mentioned_account)
+      if mention_undeliverable?(mentioned_account)
         begin
-          mentioned_account = resolve_account_service.call($1)
+          mentioned_account = resolve_account_service.call(Regexp.last_match(1))
         rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::UnexpectedResponseError
           mentioned_account = nil
         end
       end
 
-      next match if mention_undeliverable?(status, mentioned_account)
+      next match if mention_undeliverable?(mentioned_account)
+
+      mentions << mentioned_account.mentions.where(status: status).first_or_create(status: status)
 
-      mentioned_account.mentions.where(status: status).first_or_create(status: status)
       "@#{mentioned_account.acct}"
     end
 
     status.save!
 
-    status.mentions.includes(:account).each do |mention|
-      create_notification(status, mention)
-    end
+    mentions.each { |mention| create_notification(mention) }
   end
 
   private
 
-  def mention_undeliverable?(status, mentioned_account)
-    mentioned_account.nil? || (!mentioned_account.local? && mentioned_account.ostatus? && status.stream_entry.hidden?)
+  def mention_undeliverable?(mentioned_account)
+    mentioned_account.nil? || (!mentioned_account.local? && mentioned_account.ostatus? && @status.stream_entry.hidden?)
   end
 
-  def create_notification(status, mention)
+  def create_notification(mention)
     mentioned_account = mention.account
 
     if mentioned_account.local?
-      NotifyService.new.call(mentioned_account, mention)
-    elsif mentioned_account.ostatus? && !status.stream_entry.hidden?
-      NotificationWorker.perform_async(stream_entry_to_xml(status.stream_entry), status.account_id, mentioned_account.id)
+      LocalNotificationWorker.perform_async(mention.id)
+    elsif mentioned_account.ostatus? && !@status.stream_entry.hidden?
+      NotificationWorker.perform_async(ostatus_xml, @status.account_id, mentioned_account.id)
     elsif mentioned_account.activitypub?
-      ActivityPub::DeliveryWorker.perform_async(build_json(mention.status), mention.status.account_id, mentioned_account.inbox_url)
+      ActivityPub::DeliveryWorker.perform_async(activitypub_json, mention.status.account_id, mentioned_account.inbox_url)
     end
   end
 
-  def build_json(status)
-    Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
-      status,
+  def ostatus_xml
+    @ostatus_xml ||= stream_entry_to_xml(@status.stream_entry)
+  end
+
+  def activitypub_json
+    @activitypub_json ||= Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
+      @status,
       serializer: ActivityPub::ActivitySerializer,
       adapter: ActivityPub::Adapter
-    ).as_json).sign!(status.account))
+    ).as_json).sign!(@status.account))
   end
 
   def resolve_account_service