Skip to content

Commit

Permalink
Refactor: #160 アカウントのother_settings関連部分を別ファイルに切り出し (#176)
Browse files Browse the repository at this point in the history
* Bump version to 8.0

* Add: 他のサーバーに公開する情報に、制限設定などを追加

* Fix: `quote_of_id`のインデックス

* Fix: #172 他のサーバーからの相乗り絵文字削除が反映されない

* Test: #166 リモートから自分の絵文字を受け取った時、ライセンスが上書きされないことを確認するテスト

* Refactor: #160 アカウントの`other_settings`関連部分を別ファイルに切り出し
  • Loading branch information
kmycode authored Oct 26, 2023
1 parent 952ea84 commit 3caa413
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 115 deletions.
116 changes: 1 addition & 115 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Account < ApplicationRecord
include AccountMerging
include AccountSearch
include AccountStatusesSearch
include AccountOtherSettings

enum protocol: { ostatus: 0, activitypub: 1 }
enum suspension_origin: { local: 0, remote: 1 }, _prefix: true
Expand Down Expand Up @@ -311,32 +312,6 @@ def sign?
true
end

def noindex?
user_prefers_noindex? || (settings.present? && settings['noindex']) || false
end

def noai?
user&.setting_noai || (settings.present? && settings['noai']) || false
end

def translatable_private?
user&.setting_translatable_private || (settings.present? && settings['translatable_private']) || false
end

def link_preview?
return user.setting_link_preview if local? && user.present?
return settings['link_preview'] if settings.present? && settings.key?('link_preview')

true
end

def allow_quote?
return user.setting_allow_quote if local? && user.present?
return settings['allow_quote'] if settings.present? && settings.key?('allow_quote')

true
end

def public_statuses_count
hide_statuses_count? ? 0 : statuses_count
end
Expand All @@ -349,95 +324,6 @@ def public_followers_count
hide_followers_count? ? 0 : followers_count
end

def hide_statuses_count?
return user&.setting_hide_statuses_count unless user&.setting_hide_statuses_count.nil?
return settings['hide_statuses_count'] if settings.present?

false
end

def hide_following_count?
return user&.setting_hide_following_count unless user&.setting_hide_following_count.nil?
return settings['hide_following_count'] if settings.present?

false
end

def hide_followers_count?
return user&.setting_hide_followers_count unless user&.setting_hide_followers_count.nil?
return settings['hide_followers_count'] if settings.present?

false
end

def emoji_reaction_policy
return settings['emoji_reaction_policy']&.to_sym || :allow if settings.present? && user.nil?
return :allow if user.nil?
return :block if local? && !Setting.enable_emoji_reaction

user.setting_emoji_reaction_policy&.to_sym
end

def show_emoji_reaction?(account)
return false unless Setting.enable_emoji_reaction

case emoji_reaction_policy
when :block
false
when :following_only
account.present? && (id == account.id || following?(account))
when :followers_only
account.present? && (id == account.id || followed_by?(account))
when :mutuals_only
account.present? && (id == account.id || mutual?(account))
when :outside_only
account.present? && (id == account.id || following?(account) || followed_by?(account))
else
true
end
end

def allow_emoji_reaction?(account)
return false if account.nil?
return true unless local? || account.local?

show_emoji_reaction?(account)
end

def public_settings
# Please update `app/javascript/mastodon/api_types/accounts.ts` when making changes to the attributes
config = {
'noindex' => noindex?,
'noai' => noai?,
'hide_network' => hide_collections,
'hide_statuses_count' => hide_statuses_count?,
'hide_following_count' => hide_following_count?,
'hide_followers_count' => hide_followers_count?,
'translatable_private' => translatable_private?,
'link_preview' => link_preview?,
'allow_quote' => allow_quote?,
}
if Setting.enable_emoji_reaction
config = config.merge({
'emoji_reaction_policy' => emoji_reaction_policy,
})
end
config = config.merge(settings) if settings.present?
config
end

def public_settings_for_local
config = public_settings

unless Setting.enable_emoji_reaction
config = config.merge({
'emoji_reaction_policy' => :block,
})
end

config
end

def previous_strikes_count
strikes.where(overruled_at: nil).count
end
Expand Down
122 changes: 122 additions & 0 deletions app/models/concerns/account_other_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# frozen_string_literal: true

module AccountOtherSettings
extend ActiveSupport::Concern

included do
def noindex?
user_prefers_noindex? || (settings.present? && settings['noindex']) || false
end

def noai?
user&.setting_noai || (settings.present? && settings['noai']) || false
end

def translatable_private?
user&.setting_translatable_private || (settings.present? && settings['translatable_private']) || false
end

def link_preview?
return user.setting_link_preview if local? && user.present?
return settings['link_preview'] if settings.present? && settings.key?('link_preview')

true
end

def allow_quote?
return user.setting_allow_quote if local? && user.present?
return settings['allow_quote'] if settings.present? && settings.key?('allow_quote')

true
end

def hide_statuses_count?
return user&.setting_hide_statuses_count unless user&.setting_hide_statuses_count.nil?
return settings['hide_statuses_count'] if settings.present?

false
end

def hide_following_count?
return user&.setting_hide_following_count unless user&.setting_hide_following_count.nil?
return settings['hide_following_count'] if settings.present?

false
end

def hide_followers_count?
return user&.setting_hide_followers_count unless user&.setting_hide_followers_count.nil?
return settings['hide_followers_count'] if settings.present?

false
end

def emoji_reaction_policy
return settings['emoji_reaction_policy']&.to_sym || :allow if settings.present? && user.nil?
return :allow if user.nil?
return :block if local? && !Setting.enable_emoji_reaction

user.setting_emoji_reaction_policy&.to_sym
end

def show_emoji_reaction?(account)
return false unless Setting.enable_emoji_reaction

case emoji_reaction_policy
when :block
false
when :following_only
account.present? && (id == account.id || following?(account))
when :followers_only
account.present? && (id == account.id || followed_by?(account))
when :mutuals_only
account.present? && (id == account.id || mutual?(account))
when :outside_only
account.present? && (id == account.id || following?(account) || followed_by?(account))
else
true
end
end

def allow_emoji_reaction?(account)
return false if account.nil?
return true unless local? || account.local?

show_emoji_reaction?(account)
end

def public_settings
# Please update `app/javascript/mastodon/api_types/accounts.ts` when making changes to the attributes
config = {
'noindex' => noindex?,
'noai' => noai?,
'hide_network' => hide_collections,
'hide_statuses_count' => hide_statuses_count?,
'hide_following_count' => hide_following_count?,
'hide_followers_count' => hide_followers_count?,
'translatable_private' => translatable_private?,
'link_preview' => link_preview?,
'allow_quote' => allow_quote?,
}
if Setting.enable_emoji_reaction
config = config.merge({
'emoji_reaction_policy' => emoji_reaction_policy,
})
end
config = config.merge(settings) if settings.present?
config
end

def public_settings_for_local
config = public_settings

unless Setting.enable_emoji_reaction
config = config.merge({
'emoji_reaction_policy' => :block,
})
end

config
end
end
end

0 comments on commit 3caa413

Please sign in to comment.