about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
Diffstat (limited to 'app/services')
-rw-r--r--app/services/authorize_follow_service.rb36
-rw-r--r--app/services/block_service.rb24
-rw-r--r--app/services/favourite_service.rb29
-rw-r--r--app/services/fetch_atom_service.rb2
-rw-r--r--app/services/follow_service.rb49
-rw-r--r--app/services/process_interaction_service.rb8
-rw-r--r--app/services/reject_follow_service.rb36
-rw-r--r--app/services/remove_status_service.rb4
-rw-r--r--app/services/unblock_service.rb26
-rw-r--r--app/services/unfavourite_service.rb29
-rw-r--r--app/services/unfollow_service.rb26
11 files changed, 235 insertions, 34 deletions
diff --git a/app/services/authorize_follow_service.rb b/app/services/authorize_follow_service.rb
index 5370b4b61..ac465bdb2 100644
--- a/app/services/authorize_follow_service.rb
+++ b/app/services/authorize_follow_service.rb
@@ -1,12 +1,40 @@
 # frozen_string_literal: true
 
 class AuthorizeFollowService < BaseService
-  include StreamEntryRenderer
-
   def call(source_account, target_account)
     follow_request = FollowRequest.find_by!(account: source_account, target_account: target_account)
     follow_request.authorize!
-    NotificationWorker.perform_async(stream_entry_to_xml(follow_request.stream_entry), target_account.id, source_account.id) unless source_account.local?
-    follow_request.stream_entry.destroy
+    NotificationWorker.perform_async(build_xml(follow_request), target_account.id, source_account.id) unless source_account.local?
+  end
+
+  private
+
+  def build_xml(follow_request)
+    Nokogiri::XML::Builder.new do |xml|
+      entry(xml, true) do
+        unique_id xml, Time.now.utc, follow_request.id, 'FollowRequest'
+        title xml, "#{follow_request.target_account.acct} authorizes follow request by #{follow_request.account.acct}"
+
+        author(xml) do
+          include_author xml, follow_request.target_account
+        end
+
+        object_type xml, :activity
+        verb xml, :authorize
+
+        target(xml) do
+          author(xml) do
+            include_author xml, follow_request.account
+          end
+
+          object_type xml, :activity
+          verb xml, :request_friend
+
+          target(xml) do
+            include_author xml, follow_request.target_account
+          end
+        end
+      end
+    end.to_xml
   end
 end
diff --git a/app/services/block_service.rb b/app/services/block_service.rb
index 095d2a8eb..bd914d8be 100644
--- a/app/services/block_service.rb
+++ b/app/services/block_service.rb
@@ -12,6 +12,28 @@ class BlockService < BaseService
     block = account.block!(target_account)
 
     BlockWorker.perform_async(account.id, target_account.id)
-    NotificationWorker.perform_async(stream_entry_to_xml(block.stream_entry), account.id, target_account.id) unless target_account.local?
+    NotificationWorker.perform_async(build_xml(block), account.id, target_account.id) unless target_account.local?
+  end
+
+  private
+
+  def build_xml(block)
+    Nokogiri::XML::Builder.new do |xml|
+      entry(xml, true) do
+        unique_id xml, block.created_at, block.id, 'Block'
+        title xml, "#{block.account.acct} no longer wishes to interact with #{block.target_account.acct}"
+
+        author(xml) do
+          include_author xml, block.account
+        end
+
+        object_type xml, :activity
+        verb xml, :block
+
+        target(xml) do
+          include_author xml, block.target_account
+        end
+      end
+    end.to_xml
   end
 end
diff --git a/app/services/favourite_service.rb b/app/services/favourite_service.rb
index ce1722b77..824729ed6 100644
--- a/app/services/favourite_service.rb
+++ b/app/services/favourite_service.rb
@@ -1,8 +1,6 @@
 # frozen_string_literal: true
 
 class FavouriteService < BaseService
-  include StreamEntryRenderer
-
   # Favourite a status and notify remote user
   # @param [Account] account
   # @param [Status] status
@@ -12,14 +10,35 @@ class FavouriteService < BaseService
 
     favourite = Favourite.create!(account: account, status: status)
 
-    Pubsubhubbub::DistributionWorker.perform_async(favourite.stream_entry.id)
-
     if status.local?
       NotifyService.new.call(favourite.status.account, favourite)
     else
-      NotificationWorker.perform_async(stream_entry_to_xml(favourite.stream_entry), account.id, status.account_id)
+      NotificationWorker.perform_async(build_xml(favourite), account.id, status.account_id)
     end
 
     favourite
   end
+
+  private
+
+  def build_xml(favourite)
+    Nokogiri::XML::Builder.new do |xml|
+      entry(xml, true) do
+        unique_id xml, favourite.created_at, favourite.id, 'Favourite'
+        title xml, "#{favourite.account.acct} favourited a status by #{favourite.status.account.acct}"
+
+        author(xml) do
+          include_author xml, favourite.account
+        end
+
+        object_type xml, :activity
+        verb xml, :favorite
+        in_reply_to xml, TagManager.instance.uri_for(favourite.status), TagManager.instance.url_for(favourite.status)
+
+        target(xml) do
+          include_target xml, favourite.status
+        end
+      end
+    end.to_xml
+  end
 end
diff --git a/app/services/fetch_atom_service.rb b/app/services/fetch_atom_service.rb
index 98ee1db84..f7e9c150a 100644
--- a/app/services/fetch_atom_service.rb
+++ b/app/services/fetch_atom_service.rb
@@ -2,6 +2,8 @@
 
 class FetchAtomService < BaseService
   def call(url)
+    return if url.blank?
+
     response = http_client.head(url)
 
     Rails.logger.debug "Remote status HEAD request returned code #{response.code}"
diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb
index ac0392d16..d67b1bf2d 100644
--- a/app/services/follow_service.rb
+++ b/app/services/follow_service.rb
@@ -7,7 +7,7 @@ class FollowService < BaseService
   # @param [Account] source_account From which to follow
   # @param [String] uri User URI to follow in the form of username@domain
   def call(source_account, uri)
-    target_account = follow_remote_account_service.call(uri)
+    target_account = FollowRemoteAccountService.new.call(uri)
 
     raise ActiveRecord::RecordNotFound if target_account.nil? || target_account.id == source_account.id || target_account.suspended?
     raise Mastodon::NotPermitted       if target_account.blocking?(source_account) || source_account.blocking?(target_account)
@@ -27,7 +27,7 @@ class FollowService < BaseService
     if target_account.local?
       NotifyService.new.call(target_account, follow_request)
     else
-      NotificationWorker.perform_async(stream_entry_to_xml(follow_request.stream_entry), source_account.id, target_account.id)
+      NotificationWorker.perform_async(build_follow_request_xml(follow_request), source_account.id, target_account.id)
       AfterRemoteFollowRequestWorker.perform_async(follow_request.id)
     end
 
@@ -40,13 +40,12 @@ class FollowService < BaseService
     if target_account.local?
       NotifyService.new.call(target_account, follow)
     else
-      subscribe_service.call(target_account) unless target_account.subscribed?
-      NotificationWorker.perform_async(stream_entry_to_xml(follow.stream_entry), source_account.id, target_account.id)
+      SubscribeService.new.call(target_account) unless target_account.subscribed?
+      NotificationWorker.perform_async(build_follow_xml(follow), source_account.id, target_account.id)
       AfterRemoteFollowWorker.perform_async(follow.id)
     end
 
     MergeWorker.perform_async(target_account.id, source_account.id)
-    Pubsubhubbub::DistributionWorker.perform_async(follow.stream_entry.id)
 
     follow
   end
@@ -55,11 +54,43 @@ class FollowService < BaseService
     Redis.current
   end
 
-  def follow_remote_account_service
-    @follow_remote_account_service ||= FollowRemoteAccountService.new
+  def build_follow_request_xml(follow_request)
+    Nokogiri::XML::Builder.new do |xml|
+      entry(xml, true) do
+        unique_id xml, follow_request.created_at, follow_request.id, 'FollowRequest'
+        title xml, "#{follow_request.account.acct} requested to follow #{follow_request.target_account.acct}"
+
+        author(xml) do
+          include_author xml, follow_request.account
+        end
+
+        object_type xml, :activity
+        verb xml, :request_friend
+
+        target(xml) do
+          include_author xml, follow_request.target_account
+        end
+      end
+    end.to_xml
   end
 
-  def subscribe_service
-    @subscribe_service ||= SubscribeService.new
+  def build_follow_xml(follow)
+    Nokogiri::XML::Builder.new do |xml|
+      entry(xml, true) do
+        unique_id xml, follow.created_at, follow.id, 'Follow'
+        title xml, "#{follow.account.acct} started following #{follow.target_account.acct}"
+
+        author(xml) do
+          include_author xml, follow.account
+        end
+
+        object_type xml, :activity
+        verb xml, :follow
+
+        target(xml) do
+          include_author xml, follow.target_account
+        end
+      end
+    end.to_xml
   end
 end
diff --git a/app/services/process_interaction_service.rb b/app/services/process_interaction_service.rb
index 8420ca351..c74ff9e22 100644
--- a/app/services/process_interaction_service.rb
+++ b/app/services/process_interaction_service.rb
@@ -39,6 +39,8 @@ class ProcessInteractionService < BaseService
         unfollow!(account, target_account)
       when :favorite
         favourite!(xml, account)
+      when :unfavorite
+        unfavourite!(xml, account)
       when :post
         add_post!(body, account) if mentions_account?(xml, target_account)
       when :share
@@ -121,6 +123,12 @@ class ProcessInteractionService < BaseService
     NotifyService.new.call(current_status.account, favourite)
   end
 
+  def unfavourite!(xml, from_account)
+    current_status = status(xml)
+    favourite = current_status.favourites.where(account: from_account).first
+    favourite&.destroy
+  end
+
   def add_post!(body, account)
     process_feed_service.call(body, account)
   end
diff --git a/app/services/reject_follow_service.rb b/app/services/reject_follow_service.rb
index a17d6a7be..1b03d62e6 100644
--- a/app/services/reject_follow_service.rb
+++ b/app/services/reject_follow_service.rb
@@ -1,12 +1,40 @@
 # frozen_string_literal: true
 
 class RejectFollowService < BaseService
-  include StreamEntryRenderer
-
   def call(source_account, target_account)
     follow_request = FollowRequest.find_by!(account: source_account, target_account: target_account)
     follow_request.reject!
-    NotificationWorker.perform_async(stream_entry_to_xml(follow_request.stream_entry), target_account.id, source_account.id) unless source_account.local?
-    follow_request.stream_entry.destroy
+    NotificationWorker.perform_async(build_xml(follow_request), target_account.id, source_account.id) unless source_account.local?
+  end
+
+  private
+
+  def build_xml(follow_request)
+    Nokogiri::XML::Builder.new do |xml|
+      entry(xml, true) do
+        unique_id xml, Time.now.utc, follow_request.id, 'FollowRequest'
+        title xml, "#{follow_request.target_account.acct} rejects follow request by #{follow_request.account.acct}"
+
+        author(xml) do
+          include_author xml, follow_request.target_account
+        end
+
+        object_type xml, :activity
+        verb xml, :reject
+
+        target(xml) do
+          author(xml) do
+            include_author xml, follow_request.account
+          end
+
+          object_type xml, :activity
+          verb xml, :request_friend
+
+          target(xml) do
+            include_author xml, follow_request.target_account
+          end
+        end
+      end
+    end.to_xml
   end
 end
diff --git a/app/services/remove_status_service.rb b/app/services/remove_status_service.rb
index b1a646b14..73b545f17 100644
--- a/app/services/remove_status_service.rb
+++ b/app/services/remove_status_service.rb
@@ -32,12 +32,16 @@ class RemoveStatusService < BaseService
   end
 
   def remove_from_mentioned(status)
+    notified_domains = []
+
     status.mentions.each do |mention|
       mentioned_account = mention.account
 
       if mentioned_account.local?
         unpush(:mentions, mentioned_account, status)
       else
+        next if notified_domains.include?(mentioned_account.domain)
+        notified_domains << mentioned_account.domain
         send_delete_salmon(mentioned_account, status)
       end
     end
diff --git a/app/services/unblock_service.rb b/app/services/unblock_service.rb
index 84b1050c1..c4f789f74 100644
--- a/app/services/unblock_service.rb
+++ b/app/services/unblock_service.rb
@@ -1,12 +1,32 @@
 # frozen_string_literal: true
 
 class UnblockService < BaseService
-  include StreamEntryRenderer
-
   def call(account, target_account)
     return unless account.blocking?(target_account)
 
     unblock = account.unblock!(target_account)
-    NotificationWorker.perform_async(stream_entry_to_xml(unblock.stream_entry), account.id, target_account.id) unless target_account.local?
+    NotificationWorker.perform_async(build_xml(unblock), account.id, target_account.id) unless target_account.local?
+  end
+
+  private
+
+  def build_xml(block)
+    Nokogiri::XML::Builder.new do |xml|
+      entry(xml, true) do
+        unique_id xml, Time.now.utc, block.id, 'Block'
+        title xml, "#{block.account.acct} no longer blocks #{block.target_account.acct}"
+
+        author(xml) do
+          include_author xml, block.account
+        end
+
+        object_type xml, :activity
+        verb xml, :unblock
+
+        target(xml) do
+          include_author xml, block.target_account
+        end
+      end
+    end.to_xml
   end
 end
diff --git a/app/services/unfavourite_service.rb b/app/services/unfavourite_service.rb
index 04293ee08..1d3e6f06d 100644
--- a/app/services/unfavourite_service.rb
+++ b/app/services/unfavourite_service.rb
@@ -1,16 +1,35 @@
 # frozen_string_literal: true
 
 class UnfavouriteService < BaseService
-  include StreamEntryRenderer
-
   def call(account, status)
     favourite = Favourite.find_by!(account: account, status: status)
     favourite.destroy!
 
-    unless status.local?
-      NotificationWorker.perform_async(stream_entry_to_xml(favourite.stream_entry), account.id, status.account_id)
-    end
+    NotificationWorker.perform_async(build_xml(favourite), account.id, status.account_id) unless status.local?
 
     favourite
   end
+
+  private
+
+  def build_xml(favourite)
+    Nokogiri::XML::Builder.new do |xml|
+      entry(xml, true) do
+        unique_id xml, Time.now.utc, favourite.id, 'Favourite'
+        title xml, "#{favourite.account.acct} no longer favourites a status by #{favourite.status.account.acct}"
+
+        author(xml) do
+          include_author xml, favourite.account
+        end
+
+        object_type xml, :activity
+        verb xml, :unfavorite
+        in_reply_to xml, TagManager.instance.uri_for(favourite.status), TagManager.instance.url_for(favourite.status)
+
+        target(xml) do
+          include_target xml, favourite.status
+        end
+      end
+    end.to_xml
+  end
 end
diff --git a/app/services/unfollow_service.rb b/app/services/unfollow_service.rb
index 178da4da3..07f9b93dd 100644
--- a/app/services/unfollow_service.rb
+++ b/app/services/unfollow_service.rb
@@ -1,14 +1,34 @@
 # frozen_string_literal: true
 
 class UnfollowService < BaseService
-  include StreamEntryRenderer
-
   # Unfollow and notify the remote user
   # @param [Account] source_account Where to unfollow from
   # @param [Account] target_account Which to unfollow
   def call(source_account, target_account)
     follow = source_account.unfollow!(target_account)
-    NotificationWorker.perform_async(stream_entry_to_xml(follow.stream_entry), source_account.id, target_account.id) unless target_account.local?
+    NotificationWorker.perform_async(build_xml(follow), source_account.id, target_account.id) unless target_account.local?
     UnmergeWorker.perform_async(target_account.id, source_account.id)
   end
+
+  private
+
+  def build_xml(follow)
+    Nokogiri::XML::Builder.new do |xml|
+      entry(xml, true) do
+        unique_id xml, Time.now.utc, follow.id, 'Follow'
+        title xml, "#{follow.account.acct} is no longer following #{follow.target_account.acct}"
+
+        author(xml) do
+          include_author xml, follow.account
+        end
+
+        object_type xml, :activity
+        verb xml, :unfollow
+
+        target(xml) do
+          include_author xml, follow.target_account
+        end
+      end
+    end.to_xml
+  end
 end