about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/activitypub/activity/add.rb22
-rw-r--r--app/lib/activitypub/activity/remove.rb25
-rw-r--r--app/lib/feed_manager.rb6
-rw-r--r--app/lib/permalink_redirector.rb36
-rw-r--r--app/lib/translation_service.rb4
-rw-r--r--app/lib/translation_service/deepl.rb2
-rw-r--r--app/lib/translation_service/libre_translate.rb2
-rw-r--r--app/lib/translation_service/translation.rb2
-rw-r--r--app/lib/vacuum/media_attachments_vacuum.rb2
-rw-r--r--app/lib/vacuum/preview_cards_vacuum.rb9
10 files changed, 67 insertions, 43 deletions
diff --git a/app/lib/activitypub/activity/add.rb b/app/lib/activitypub/activity/add.rb
index 845eeaef7..9e2483983 100644
--- a/app/lib/activitypub/activity/add.rb
+++ b/app/lib/activitypub/activity/add.rb
@@ -2,12 +2,32 @@
 
 class ActivityPub::Activity::Add < ActivityPub::Activity
   def perform
-    return unless @json['target'].present? && value_or_id(@json['target']) == @account.featured_collection_url
+    return if @json['target'].blank?
 
+    case value_or_id(@json['target'])
+    when @account.featured_collection_url
+      case @object['type']
+      when 'Hashtag'
+        add_featured_tags
+      else
+        add_featured
+      end
+    end
+  end
+
+  private
+
+  def add_featured
     status = status_from_object
 
     return unless !status.nil? && status.account_id == @account.id && !@account.pinned?(status)
 
     StatusPin.create!(account: @account, status: status)
   end
+
+  def add_featured_tags
+    name = @object['name']&.delete_prefix('#')
+
+    FeaturedTag.create!(account: @account, name: name) if name.present?
+  end
 end
diff --git a/app/lib/activitypub/activity/remove.rb b/app/lib/activitypub/activity/remove.rb
index f523ead9f..f5cbef675 100644
--- a/app/lib/activitypub/activity/remove.rb
+++ b/app/lib/activitypub/activity/remove.rb
@@ -2,8 +2,22 @@
 
 class ActivityPub::Activity::Remove < ActivityPub::Activity
   def perform
-    return unless @json['target'].present? && value_or_id(@json['target']) == @account.featured_collection_url
+    return if @json['target'].blank?
 
+    case value_or_id(@json['target'])
+    when @account.featured_collection_url
+      case @object['type']
+      when 'Hashtag'
+        remove_featured_tags
+      else
+        remove_featured
+      end
+    end
+  end
+
+  private
+
+  def remove_featured
     status = status_from_uri(object_uri)
 
     return unless !status.nil? && status.account_id == @account.id
@@ -11,4 +25,13 @@ class ActivityPub::Activity::Remove < ActivityPub::Activity
     pin = StatusPin.find_by(account: @account, status: status)
     pin&.destroy!
   end
+
+  def remove_featured_tags
+    name = @object['name']&.delete_prefix('#')
+
+    return if name.blank?
+
+    featured_tag = FeaturedTag.by_name(name).find_by(account: @account)
+    featured_tag&.destroy!
+  end
 end
diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb
index 0bc7e254e..9fe9ec346 100644
--- a/app/lib/feed_manager.rb
+++ b/app/lib/feed_manager.rb
@@ -278,7 +278,7 @@ class FeedManager
         next if last_status_score < oldest_home_score
       end
 
-      statuses = target_account.statuses.where(visibility: [:public, :unlisted, :private]).includes(:preloadable_poll, :media_attachments, reblog: :account).limit(limit)
+      statuses = target_account.statuses.where(visibility: [:public, :unlisted, :private]).includes(:preloadable_poll, :media_attachments, :account, reblog: :account).limit(limit)
       crutches = build_crutches(account.id, statuses)
 
       statuses.each do |status|
@@ -484,7 +484,7 @@ class FeedManager
   # @param [Hash] crutches
   # @return [Boolean]
   def filter_from_tags?(status, receiver_id, crutches)
-    receiver_id != status.account_id && (((crutches[:active_mentions][status.id] || []) + [status.account_id]).any? { |target_account_id| crutches[:blocking][target_account_id] || crutches[:muting][target_account_id] } || crutches[:blocked_by][status.account_id] || crutches[:domain_blocking][status.account.domain])
+    receiver_id == status.account_id || ((crutches[:active_mentions][status.id] || []) + [status.account_id]).any? { |target_account_id| crutches[:blocking][target_account_id] || crutches[:muting][target_account_id] } || crutches[:blocked_by][status.account_id] || crutches[:domain_blocking][status.account.domain]
   end
 
   # Adds a status to an account's feed, returning true if a status was
@@ -605,7 +605,7 @@ class FeedManager
     crutches[:hiding_reblogs]  = Follow.where(account_id: receiver_id, target_account_id: statuses.map { |s| s.account_id if s.reblog? }.compact, show_reblogs: false).pluck(:target_account_id).index_with(true)
     crutches[:blocking]        = Block.where(account_id: receiver_id, target_account_id: check_for_blocks).pluck(:target_account_id).index_with(true)
     crutches[:muting]          = Mute.where(account_id: receiver_id, target_account_id: check_for_blocks).pluck(:target_account_id).index_with(true)
-    crutches[:domain_blocking] = AccountDomainBlock.where(account_id: receiver_id, domain: statuses.map { |s| s.reblog&.account&.domain }.compact).pluck(:domain).index_with(true)
+    crutches[:domain_blocking] = AccountDomainBlock.where(account_id: receiver_id, domain: statuses.flat_map { |s| [s.account.domain, s.reblog&.account&.domain] }.compact).pluck(:domain).index_with(true)
     crutches[:blocked_by]      = Block.where(target_account_id: receiver_id, account_id: statuses.map { |s| s.reblog&.account_id }.compact).pluck(:account_id).index_with(true)
 
     crutches
diff --git a/app/lib/permalink_redirector.rb b/app/lib/permalink_redirector.rb
index 6d15f3963..cf1a37625 100644
--- a/app/lib/permalink_redirector.rb
+++ b/app/lib/permalink_redirector.rb
@@ -8,16 +8,14 @@ class PermalinkRedirector
   end
 
   def redirect_path
-    if path_segments[0] == 'web'
-      if path_segments[1].present? && path_segments[1].start_with?('@') && path_segments[2] =~ /\d/
-        find_status_url_by_id(path_segments[2])
-      elsif path_segments[1].present? && path_segments[1].start_with?('@')
-        find_account_url_by_name(path_segments[1])
-      elsif path_segments[1] == 'statuses' && path_segments[2] =~ /\d/
-        find_status_url_by_id(path_segments[2])
-      elsif path_segments[1] == 'accounts' && path_segments[2] =~ /\d/
-        find_account_url_by_id(path_segments[2])
-      end
+    if path_segments[0].present? && path_segments[0].start_with?('@') && path_segments[1] =~ /\d/
+      find_status_url_by_id(path_segments[1])
+    elsif path_segments[0].present? && path_segments[0].start_with?('@')
+      find_account_url_by_name(path_segments[0])
+    elsif path_segments[0] == 'statuses' && path_segments[1] =~ /\d/
+      find_status_url_by_id(path_segments[1])
+    elsif path_segments[0] == 'accounts' && path_segments[1] =~ /\d/
+      find_account_url_by_id(path_segments[1])
     end
   end
 
@@ -29,18 +27,12 @@ class PermalinkRedirector
 
   def find_status_url_by_id(id)
     status = Status.find_by(id: id)
-
-    return unless status&.distributable?
-
-    ActivityPub::TagManager.instance.url_for(status)
+    ActivityPub::TagManager.instance.url_for(status) if status&.distributable? && !status.account.local?
   end
 
   def find_account_url_by_id(id)
     account = Account.find_by(id: id)
-
-    return unless account
-
-    ActivityPub::TagManager.instance.url_for(account)
+    ActivityPub::TagManager.instance.url_for(account) if account.present? && !account.local?
   end
 
   def find_account_url_by_name(name)
@@ -48,12 +40,6 @@ class PermalinkRedirector
     domain           = nil if TagManager.instance.local_domain?(domain)
     account          = Account.find_remote(username, domain)
 
-    return unless account
-
-    ActivityPub::TagManager.instance.url_for(account)
-  end
-
-  def find_tag_url_by_name(name)
-    tag_path(CGI.unescape(name))
+    ActivityPub::TagManager.instance.url_for(account) if account.present? && !account.local?
   end
 end
diff --git a/app/lib/translation_service.rb b/app/lib/translation_service.rb
index 526e26ae5..285f30939 100644
--- a/app/lib/translation_service.rb
+++ b/app/lib/translation_service.rb
@@ -17,6 +17,10 @@ class TranslationService
     end
   end
 
+  def self.configured?
+    ENV['DEEPL_API_KEY'].present? || ENV['LIBRE_TRANSLATE_ENDPOINT'].present?
+  end
+
   def translate(_text, _source_language, _target_language)
     raise NotImplementedError
   end
diff --git a/app/lib/translation_service/deepl.rb b/app/lib/translation_service/deepl.rb
index b75b604a8..537fd24c0 100644
--- a/app/lib/translation_service/deepl.rb
+++ b/app/lib/translation_service/deepl.rb
@@ -46,7 +46,7 @@ class TranslationService::DeepL < TranslationService
 
     raise UnexpectedResponseError unless json.is_a?(Hash)
 
-    Translation.new(text: json.dig('translations', 0, 'text'), detected_source_language: json.dig('translations', 0, 'detected_source_language')&.downcase)
+    Translation.new(text: json.dig('translations', 0, 'text'), detected_source_language: json.dig('translations', 0, 'detected_source_language')&.downcase, provider: 'DeepL.com')
   rescue Oj::ParseError
     raise UnexpectedResponseError
   end
diff --git a/app/lib/translation_service/libre_translate.rb b/app/lib/translation_service/libre_translate.rb
index 8cf26f868..43576e306 100644
--- a/app/lib/translation_service/libre_translate.rb
+++ b/app/lib/translation_service/libre_translate.rb
@@ -37,7 +37,7 @@ class TranslationService::LibreTranslate < TranslationService
 
     raise UnexpectedResponseError unless json.is_a?(Hash)
 
-    Translation.new(text: json['translatedText'], detected_source_language: source_language)
+    Translation.new(text: json['translatedText'], detected_source_language: source_language, provider: 'LibreTranslate')
   rescue Oj::ParseError
     raise UnexpectedResponseError
   end
diff --git a/app/lib/translation_service/translation.rb b/app/lib/translation_service/translation.rb
index a55b82574..19318c7e9 100644
--- a/app/lib/translation_service/translation.rb
+++ b/app/lib/translation_service/translation.rb
@@ -1,5 +1,5 @@
 # frozen_string_literal: true
 
 class TranslationService::Translation < ActiveModelSerializers::Model
-  attributes :text, :detected_source_language
+  attributes :text, :detected_source_language, :provider
 end
diff --git a/app/lib/vacuum/media_attachments_vacuum.rb b/app/lib/vacuum/media_attachments_vacuum.rb
index 7fb347ce4..7c0a85a9d 100644
--- a/app/lib/vacuum/media_attachments_vacuum.rb
+++ b/app/lib/vacuum/media_attachments_vacuum.rb
@@ -8,8 +8,8 @@ class Vacuum::MediaAttachmentsVacuum
   end
 
   def perform
-    vacuum_cached_files! if retention_period?
     vacuum_orphaned_records!
+    vacuum_cached_files! if retention_period?
   end
 
   private
diff --git a/app/lib/vacuum/preview_cards_vacuum.rb b/app/lib/vacuum/preview_cards_vacuum.rb
index 84ef100ed..14fdeda1c 100644
--- a/app/lib/vacuum/preview_cards_vacuum.rb
+++ b/app/lib/vacuum/preview_cards_vacuum.rb
@@ -9,7 +9,6 @@ class Vacuum::PreviewCardsVacuum
 
   def perform
     vacuum_cached_images! if retention_period?
-    vacuum_orphaned_records!
   end
 
   private
@@ -21,18 +20,10 @@ class Vacuum::PreviewCardsVacuum
     end
   end
 
-  def vacuum_orphaned_records!
-    orphaned_preview_cards.in_batches.destroy_all
-  end
-
   def preview_cards_past_retention_period
     PreviewCard.cached.where(PreviewCard.arel_table[:updated_at].lt(@retention_period.ago))
   end
 
-  def orphaned_preview_cards
-    PreviewCard.where('NOT EXISTS (SELECT 1 FROM preview_cards_statuses WHERE preview_cards_statuses.preview_card_id = preview_cards.id)').where(PreviewCard.arel_table[:created_at].lt(TTL.ago))
-  end
-
   def retention_period?
     @retention_period.present?
   end