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/activitypub/process_account_service.rb2
-rw-r--r--app/services/batched_remove_status_service.rb5
-rw-r--r--app/services/follow_service.rb6
-rw-r--r--app/services/import_service.rb90
-rw-r--r--app/services/post_status_service.rb6
-rw-r--r--app/services/process_hashtags_service.rb12
-rw-r--r--app/services/remove_status_service.rb23
7 files changed, 119 insertions, 25 deletions
diff --git a/app/services/activitypub/process_account_service.rb b/app/services/activitypub/process_account_service.rb
index 487456f3a..5e3308428 100644
--- a/app/services/activitypub/process_account_service.rb
+++ b/app/services/activitypub/process_account_service.rb
@@ -212,7 +212,7 @@ class ActivityPub::ProcessAccountService < BaseService
   end
 
   def clear_tombstones!
-    Tombstone.delete_all(account_id: @account.id)
+    Tombstone.where(account_id: @account.id).delete_all
   end
 
   def protocol_changed?
diff --git a/app/services/batched_remove_status_service.rb b/app/services/batched_remove_status_service.rb
index 61c408926..d78f506c6 100644
--- a/app/services/batched_remove_status_service.rb
+++ b/app/services/batched_remove_status_service.rb
@@ -2,6 +2,7 @@
 
 class BatchedRemoveStatusService < BaseService
   include StreamEntryRenderer
+  include Redisable
 
   # Delete given statuses and reblogs of them
   # Dispatch PuSH updates of the deleted statuses, but only local ones
@@ -120,10 +121,6 @@ class BatchedRemoveStatusService < BaseService
     end
   end
 
-  def redis
-    Redis.current
-  end
-
   def build_xml(stream_entry)
     return @activity_xml[stream_entry.id] if @activity_xml.key?(stream_entry.id)
 
diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb
index 9d36a1449..92d8c864a 100644
--- a/app/services/follow_service.rb
+++ b/app/services/follow_service.rb
@@ -1,6 +1,8 @@
 # frozen_string_literal: true
 
 class FollowService < BaseService
+  include Redisable
+
   # Follow a remote user, notify remote user about the follow
   # @param [Account] source_account From which to follow
   # @param [String, Account] uri User URI to follow in the form of username@domain (or account record)
@@ -67,10 +69,6 @@ class FollowService < BaseService
     follow
   end
 
-  def redis
-    Redis.current
-  end
-
   def build_follow_request_xml(follow_request)
     OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.follow_request_salmon(follow_request))
   end
diff --git a/app/services/import_service.rb b/app/services/import_service.rb
new file mode 100644
index 000000000..3f558626e
--- /dev/null
+++ b/app/services/import_service.rb
@@ -0,0 +1,90 @@
+# frozen_string_literal: true
+
+require 'csv'
+
+class ImportService < BaseService
+  ROWS_PROCESSING_LIMIT = 20_000
+
+  def call(import)
+    @import  = import
+    @account = @import.account
+    @data    = CSV.new(import_data).reject(&:blank?)
+
+    case @import.type
+    when 'following'
+      import_follows!
+    when 'blocking'
+      import_blocks!
+    when 'muting'
+      import_mutes!
+    when 'domain_blocking'
+      import_domain_blocks!
+    end
+  end
+
+  private
+
+  def import_follows!
+    import_relationships!('follow', 'unfollow', @account.following, follow_limit)
+  end
+
+  def import_blocks!
+    import_relationships!('block', 'unblock', @account.blocking, ROWS_PROCESSING_LIMIT)
+  end
+
+  def import_mutes!
+    import_relationships!('mute', 'unmute', @account.muting, ROWS_PROCESSING_LIMIT)
+  end
+
+  def import_domain_blocks!
+    items = @data.take(ROWS_PROCESSING_LIMIT).map { |row| row.first.strip }
+
+    if @import.overwrite?
+      presence_hash = items.each_with_object({}) { |id, mapping| mapping[id] = true }
+
+      @account.domain_blocks.find_each do |domain_block|
+        if presence_hash[domain_block.domain]
+          items.delete(domain_block.domain)
+        else
+          @account.unblock_domain!(domain_block.domain)
+        end
+      end
+    end
+
+    items.each do |domain|
+      @account.block_domain!(domain)
+    end
+
+    AfterAccountDomainBlockWorker.push_bulk(items) do |domain|
+      [@account.id, domain]
+    end
+  end
+
+  def import_relationships!(action, undo_action, overwrite_scope, limit)
+    items = @data.take(limit).map { |row| row.first.strip }
+
+    if @import.overwrite?
+      presence_hash = items.each_with_object({}) { |id, mapping| mapping[id] = true }
+
+      overwrite_scope.find_each do |target_account|
+        if presence_hash[target_account.acct]
+          items.delete(target_account.acct)
+        else
+          Import::RelationshipWorker.perform_async(@account.id, target_account.acct, undo_action)
+        end
+      end
+    end
+
+    Import::RelationshipWorker.push_bulk(items) do |acct|
+      [@account.id, acct, action]
+    end
+  end
+
+  def import_data
+    Paperclip.io_adapters.for(@import.data).read
+  end
+
+  def follow_limit
+    FollowLimitValidator.limit_for_account(@account)
+  end
+end
diff --git a/app/services/post_status_service.rb b/app/services/post_status_service.rb
index 5d431c42a..cfb266fbb 100644
--- a/app/services/post_status_service.rb
+++ b/app/services/post_status_service.rb
@@ -1,6 +1,8 @@
 # frozen_string_literal: true
 
 class PostStatusService < BaseService
+  include Redisable
+
   MIN_SCHEDULE_OFFSET = 5.minutes.freeze
 
   # Post a text status update, fetch and notify remote users mentioned
@@ -115,10 +117,6 @@ class PostStatusService < BaseService
     ProcessHashtagsService.new
   end
 
-  def redis
-    Redis.current
-  end
-
   def scheduled?
     @scheduled_at.present?
   end
diff --git a/app/services/process_hashtags_service.rb b/app/services/process_hashtags_service.rb
index cf7471c98..d5ec076a8 100644
--- a/app/services/process_hashtags_service.rb
+++ b/app/services/process_hashtags_service.rb
@@ -2,12 +2,22 @@
 
 class ProcessHashtagsService < BaseService
   def call(status, tags = [])
-    tags = Extractor.extract_hashtags(status.text) if status.local?
+    tags    = Extractor.extract_hashtags(status.text) if status.local?
+    records = []
 
     tags.map { |str| str.mb_chars.downcase }.uniq(&:to_s).each do |name|
       tag = Tag.where(name: name).first_or_create(name: name)
+
       status.tags << tag
+      records << tag
+
       TrendingTags.record_use!(tag, status.account, status.created_at) if status.public_visibility?
     end
+
+    return unless status.public_visibility? || status.unlisted_visibility?
+
+    status.account.featured_tags.where(tag_id: records.map(&:id)).each do |featured_tag|
+      featured_tag.increment(status.created_at)
+    end
   end
 end
diff --git a/app/services/remove_status_service.rb b/app/services/remove_status_service.rb
index 4bee86c8a..99c8e6cbb 100644
--- a/app/services/remove_status_service.rb
+++ b/app/services/remove_status_service.rb
@@ -2,6 +2,7 @@
 
 class RemoveStatusService < BaseService
   include StreamEntryRenderer
+  include Redisable
 
   def call(status, **options)
     @payload      = Oj.dump(event: :delete, payload: status.id.to_s)
@@ -56,7 +57,7 @@ class RemoveStatusService < BaseService
 
   def remove_from_affected
     @mentions.map(&:account).select(&:local?).each do |account|
-      Redis.current.publish("timeline:#{account.id}", @payload)
+      redis.publish("timeline:#{account.id}", @payload)
     end
   end
 
@@ -131,26 +132,30 @@ class RemoveStatusService < BaseService
   end
 
   def remove_from_hashtags
+    @account.featured_tags.where(tag_id: @status.tags.pluck(:id)).each do |featured_tag|
+      featured_tag.decrement(@status.id)
+    end
+
     return unless @status.public_visibility?
 
     @tags.each do |hashtag|
-      Redis.current.publish("timeline:hashtag:#{hashtag}", @payload)
-      Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if @status.local?
+      redis.publish("timeline:hashtag:#{hashtag}", @payload)
+      redis.publish("timeline:hashtag:#{hashtag}:local", @payload) if @status.local?
     end
   end
 
   def remove_from_public
     return unless @status.public_visibility?
 
-    Redis.current.publish('timeline:public', @payload)
-    Redis.current.publish('timeline:public:local', @payload) if @status.local?
+    redis.publish('timeline:public', @payload)
+    redis.publish('timeline:public:local', @payload) if @status.local?
   end
 
   def remove_from_media
     return unless @status.public_visibility?
 
-    Redis.current.publish('timeline:public:media', @payload)
-    Redis.current.publish('timeline:public:local:media', @payload) if @status.local?
+    redis.publish('timeline:public:media', @payload)
+    redis.publish('timeline:public:local:media', @payload) if @status.local?
   end
 
   def remove_from_direct
@@ -159,8 +164,4 @@ class RemoveStatusService < BaseService
     end
     Redis.current.publish("timeline:direct:#{@account.id}", @payload) if @account.local?
   end
-
-  def redis
-    Redis.current
-  end
 end