From 13a2abacc8f943e2b1d2da63551443887e1065b0 Mon Sep 17 00:00:00 2001 From: Claire Date: Wed, 25 Jan 2023 19:55:40 +0100 Subject: Add `roles` attribute to Account entities in REST API (#23255) --- app/models/status.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/models/status.rb') diff --git a/app/models/status.rb b/app/models/status.rb index fa9fb9fad..b1c49e99a 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -117,7 +117,7 @@ class Status < ApplicationRecord :tags, :preview_cards, :preloadable_poll, - account: [:account_stat, :user], + account: [:account_stat, user: :role], active_mentions: { account: :account_stat }, reblog: [ :application, @@ -127,7 +127,7 @@ class Status < ApplicationRecord :conversation, :status_stat, :preloadable_poll, - account: [:account_stat, :user], + account: [:account_stat, user: :role], active_mentions: { account: :account_stat }, ], thread: { account: :account_stat } -- cgit From ab7816a4141e88cf7e05ba49638ee95fcc6f71ff Mon Sep 17 00:00:00 2001 From: Nick Schonning Date: Sat, 18 Feb 2023 06:39:00 -0500 Subject: Autofix Rubocop Style/Lambda (#23696) --- .rubocop_todo.yml | 12 ------------ app/models/concerns/paginable.rb | 4 ++-- app/models/status.rb | 4 ++-- app/models/tag.rb | 2 +- lib/cli.rb | 2 +- lib/mastodon/domains_cli.rb | 2 +- 6 files changed, 7 insertions(+), 19 deletions(-) (limited to 'app/models/status.rb') diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0e94741ae..2ec33fd30 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -2930,18 +2930,6 @@ Style/InverseMethods: - 'app/services/update_account_service.rb' - 'spec/controllers/activitypub/replies_controller_spec.rb' -# Offense count: 7 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: line_count_dependent, lambda, literal -Style/Lambda: - Exclude: - - 'app/models/concerns/paginable.rb' - - 'app/models/status.rb' - - 'app/models/tag.rb' - - 'lib/cli.rb' - - 'lib/mastodon/domains_cli.rb' - # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). Style/MapToHash: diff --git a/app/models/concerns/paginable.rb b/app/models/concerns/paginable.rb index 62e39f671..b76e78c1e 100644 --- a/app/models/concerns/paginable.rb +++ b/app/models/concerns/paginable.rb @@ -4,7 +4,7 @@ module Paginable extend ActiveSupport::Concern included do - scope :paginate_by_max_id, ->(limit, max_id = nil, since_id = nil) { + scope :paginate_by_max_id, lambda { |limit, max_id = nil, since_id = nil| query = order(arel_table[:id].desc).limit(limit) query = query.where(arel_table[:id].lt(max_id)) if max_id.present? query = query.where(arel_table[:id].gt(since_id)) if since_id.present? @@ -14,7 +14,7 @@ module Paginable # Differs from :paginate_by_max_id in that it gives the results immediately following min_id, # whereas since_id gives the items with largest id, but with since_id as a cutoff. # Results will be in ascending order by id. - scope :paginate_by_min_id, ->(limit, min_id = nil, max_id = nil) { + scope :paginate_by_min_id, lambda { |limit, min_id = nil, max_id = nil| query = reorder(arel_table[:id]).limit(limit) query = query.where(arel_table[:id].gt(min_id)) if min_id.present? query = query.where(arel_table[:id].lt(max_id)) if max_id.present? diff --git a/app/models/status.rb b/app/models/status.rb index b1c49e99a..a924a985f 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -101,12 +101,12 @@ class Status < ApplicationRecord scope :including_silenced_accounts, -> { left_outer_joins(:account).where.not(accounts: { silenced_at: nil }) } scope :not_excluded_by_account, ->(account) { where.not(account_id: account.excluded_from_timeline_account_ids) } scope :not_domain_blocked_by_account, ->(account) { account.excluded_from_timeline_domains.blank? ? left_outer_joins(:account) : left_outer_joins(:account).where('accounts.domain IS NULL OR accounts.domain NOT IN (?)', account.excluded_from_timeline_domains) } - scope :tagged_with_all, ->(tag_ids) { + scope :tagged_with_all, lambda { |tag_ids| Array(tag_ids).map(&:to_i).reduce(self) do |result, id| result.joins("INNER JOIN statuses_tags t#{id} ON t#{id}.status_id = statuses.id AND t#{id}.tag_id = #{id}") end } - scope :tagged_with_none, ->(tag_ids) { + scope :tagged_with_none, lambda { |tag_ids| where('NOT EXISTS (SELECT * FROM statuses_tags forbidden WHERE forbidden.status_id = statuses.id AND forbidden.tag_id IN (?))', tag_ids) } diff --git a/app/models/tag.rb b/app/models/tag.rb index 47a05d00a..98001d60a 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -49,7 +49,7 @@ class Tag < ApplicationRecord scope :listable, -> { where(listable: [true, nil]) } scope :trendable, -> { Setting.trendable_by_default ? where(trendable: [true, nil]) : where(trendable: true) } scope :not_trendable, -> { where(trendable: false) } - scope :recently_used, ->(account) { + scope :recently_used, lambda { |account| joins(:statuses) .where(statuses: { id: account.statuses.select(:id).limit(1000) }) .group(:id).order(Arel.sql('count(*) desc')) diff --git a/lib/cli.rb b/lib/cli.rb index 35c00e736..157465c4b 100644 --- a/lib/cli.rb +++ b/lib/cli.rb @@ -121,7 +121,7 @@ module Mastodon prompt.warn('Do NOT interrupt this process...') - delete_account = ->(account) do + delete_account = lambda do |account| payload = ActiveModelSerializers::SerializableResource.new( account, serializer: ActivityPub::DeleteActorSerializer, diff --git a/lib/mastodon/domains_cli.rb b/lib/mastodon/domains_cli.rb index 81ee53c18..f24a54e7e 100644 --- a/lib/mastodon/domains_cli.rb +++ b/lib/mastodon/domains_cli.rb @@ -139,7 +139,7 @@ module Mastodon pool = Concurrent::ThreadPoolExecutor.new(min_threads: 0, max_threads: options[:concurrency], idletime: 10, auto_terminate: true, max_queue: 0) - work_unit = ->(domain) do + work_unit = lambda do |domain| next if stats.key?(domain) next if options[:exclude_suspended] && domain.match?(blocked_domains) -- cgit From 2177daeae92b77be6797ba8f2ab6ebe1e641e078 Mon Sep 17 00:00:00 2001 From: Nick Schonning Date: Sat, 18 Feb 2023 17:09:40 -0500 Subject: Autofix Rubocop Style/RedundantBegin (#23703) --- .rubocop_todo.yml | 73 ---------------------- app/controllers/admin/dashboard_controller.rb | 12 ++-- app/controllers/api/v1/announcements_controller.rb | 4 +- app/controllers/api/v1/trends/links_controller.rb | 12 ++-- .../api/v1/trends/statuses_controller.rb | 12 ++-- app/controllers/api/v1/trends/tags_controller.rb | 12 ++-- app/controllers/concerns/rate_limit_headers.rb | 12 ++-- .../concerns/two_factor_authentication_concern.rb | 12 ++-- app/helpers/admin/dashboard_helper.rb | 24 ++++--- app/helpers/admin/trends/statuses_helper.rb | 12 ++-- app/helpers/branding_helper.rb | 14 ++--- app/helpers/domain_control_helper.rb | 12 ++-- app/helpers/formatting_helper.rb | 44 ++++++------- app/helpers/instance_helper.rb | 12 ++-- app/helpers/jsonld_helper.rb | 16 +++-- app/lib/activity_tracker.rb | 14 ++--- app/lib/activitypub/activity/create.rb | 38 ++++++----- app/lib/activitypub/forwarder.rb | 12 ++-- .../dimension/software_versions_dimension.rb | 12 ++-- .../metrics/dimension/space_usage_dimension.rb | 12 ++-- app/lib/extractor.rb | 10 ++- app/lib/importer/statuses_index_importer.rb | 12 ++-- app/lib/link_details_extractor.rb | 30 +++++---- app/lib/request.rb | 34 +++++----- app/models/account.rb | 13 ++-- app/models/account/field.rb | 12 ++-- app/models/admin/account_action.rb | 12 ++-- app/models/announcement.rb | 12 ++-- app/models/concerns/account_merging.rb | 16 ++--- app/models/concerns/pam_authenticable.rb | 12 ++-- app/models/email_domain_block.rb | 12 ++-- app/models/form/admin_settings.rb | 12 ++-- app/models/form/custom_emoji_batch.rb | 12 ++-- app/models/notification.rb | 12 ++-- app/models/remote_follow.rb | 12 ++-- app/models/status.rb | 13 ++-- app/models/status_edit.rb | 14 ++--- app/models/trends/links.rb | 24 +++---- app/models/trends/statuses.rb | 26 ++++---- app/models/trends/tag_filter.rb | 12 ++-- app/models/trends/tags.rb | 12 ++-- app/models/web/push_subscription.rb | 24 +++---- app/presenters/tag_relationships_presenter.rb | 12 ++-- app/services/account_search_service.rb | 16 +++-- .../fetch_featured_tags_collection_service.rb | 14 ++--- .../activitypub/fetch_remote_status_service.rb | 12 ++-- app/services/fetch_link_card_service.rb | 18 +++--- app/services/process_mentions_service.rb | 12 ++-- app/services/reblog_service.rb | 12 ++-- app/services/resolve_account_service.rb | 12 ++-- app/validators/domain_validator.rb | 12 ++-- app/validators/existing_username_validator.rb | 14 ++--- app/validators/import_validator.rb | 12 ++-- app/workers/backup_worker.rb | 10 ++- app/workers/post_process_media_worker.rb | 12 ++-- .../scheduler/follow_recommendations_scheduler.rb | 12 ++-- .../20180528141303_fix_accounts_unique_index.rb | 16 ++--- db/migrate/20180812173710_copy_status_stats.rb | 10 ++- db/migrate/20181116173541_copy_account_stats.rb | 10 ++- lib/mastodon/accounts_cli.rb | 28 ++++----- lib/mastodon/cli_helper.rb | 42 ++++++------- lib/mastodon/ip_blocks_cli.rb | 12 ++-- lib/mastodon/maintenance_cli.rb | 64 +++++++------------ lib/mastodon/media_cli.rb | 12 ++-- lib/mastodon/search_cli.rb | 12 ++-- lib/mastodon/upgrade_cli.rb | 18 +++--- lib/paperclip/color_extractor.rb | 12 ++-- lib/sanitize_ext/sanitize_config.rb | 12 ++-- lib/tasks/db.rake | 14 ++--- 69 files changed, 462 insertions(+), 699 deletions(-) (limited to 'app/models/status.rb') diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 73dae59c5..7620025cf 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -2958,79 +2958,6 @@ Style/RedundantArgument: - 'app/helpers/application_helper.rb' - 'lib/tasks/emojis.rake' -# Offense count: 83 -# This cop supports safe autocorrection (--autocorrect). -Style/RedundantBegin: - Exclude: - - 'app/controllers/admin/dashboard_controller.rb' - - 'app/controllers/api/v1/announcements_controller.rb' - - 'app/controllers/api/v1/trends/links_controller.rb' - - 'app/controllers/api/v1/trends/statuses_controller.rb' - - 'app/controllers/api/v1/trends/tags_controller.rb' - - 'app/controllers/concerns/rate_limit_headers.rb' - - 'app/controllers/concerns/two_factor_authentication_concern.rb' - - 'app/helpers/admin/dashboard_helper.rb' - - 'app/helpers/admin/trends/statuses_helper.rb' - - 'app/helpers/branding_helper.rb' - - 'app/helpers/domain_control_helper.rb' - - 'app/helpers/formatting_helper.rb' - - 'app/helpers/instance_helper.rb' - - 'app/helpers/jsonld_helper.rb' - - 'app/lib/activity_tracker.rb' - - 'app/lib/activitypub/activity/create.rb' - - 'app/lib/activitypub/forwarder.rb' - - 'app/lib/admin/metrics/dimension/software_versions_dimension.rb' - - 'app/lib/admin/metrics/dimension/space_usage_dimension.rb' - - 'app/lib/extractor.rb' - - 'app/lib/importer/statuses_index_importer.rb' - - 'app/lib/link_details_extractor.rb' - - 'app/lib/request.rb' - - 'app/models/account.rb' - - 'app/models/account/field.rb' - - 'app/models/admin/account_action.rb' - - 'app/models/announcement.rb' - - 'app/models/concerns/account_merging.rb' - - 'app/models/concerns/pam_authenticable.rb' - - 'app/models/email_domain_block.rb' - - 'app/models/form/admin_settings.rb' - - 'app/models/form/custom_emoji_batch.rb' - - 'app/models/notification.rb' - - 'app/models/remote_follow.rb' - - 'app/models/status.rb' - - 'app/models/status_edit.rb' - - 'app/models/trends/links.rb' - - 'app/models/trends/statuses.rb' - - 'app/models/trends/tag_filter.rb' - - 'app/models/trends/tags.rb' - - 'app/models/web/push_subscription.rb' - - 'app/presenters/tag_relationships_presenter.rb' - - 'app/services/account_search_service.rb' - - 'app/services/activitypub/fetch_featured_tags_collection_service.rb' - - 'app/services/activitypub/fetch_remote_status_service.rb' - - 'app/services/fetch_link_card_service.rb' - - 'app/services/process_mentions_service.rb' - - 'app/services/reblog_service.rb' - - 'app/services/resolve_account_service.rb' - - 'app/validators/domain_validator.rb' - - 'app/validators/existing_username_validator.rb' - - 'app/validators/import_validator.rb' - - 'app/workers/backup_worker.rb' - - 'app/workers/post_process_media_worker.rb' - - 'app/workers/scheduler/follow_recommendations_scheduler.rb' - - 'db/migrate/20180528141303_fix_accounts_unique_index.rb' - - 'db/migrate/20180812173710_copy_status_stats.rb' - - 'db/migrate/20181116173541_copy_account_stats.rb' - - 'lib/mastodon/accounts_cli.rb' - - 'lib/mastodon/cli_helper.rb' - - 'lib/mastodon/ip_blocks_cli.rb' - - 'lib/mastodon/maintenance_cli.rb' - - 'lib/mastodon/media_cli.rb' - - 'lib/mastodon/search_cli.rb' - - 'lib/mastodon/upgrade_cli.rb' - - 'lib/paperclip/color_extractor.rb' - - 'lib/sanitize_ext/sanitize_config.rb' - - 'lib/tasks/db.rake' - # Offense count: 16 # This cop supports safe autocorrection (--autocorrect). Style/RedundantRegexpCharacterClass: diff --git a/app/controllers/admin/dashboard_controller.rb b/app/controllers/admin/dashboard_controller.rb index 924b623ad..099512248 100644 --- a/app/controllers/admin/dashboard_controller.rb +++ b/app/controllers/admin/dashboard_controller.rb @@ -18,13 +18,11 @@ module Admin private def redis_info - @redis_info ||= begin - if redis.is_a?(Redis::Namespace) - redis.redis.info - else - redis.info - end - end + @redis_info ||= if redis.is_a?(Redis::Namespace) + redis.redis.info + else + redis.info + end end end end diff --git a/app/controllers/api/v1/announcements_controller.rb b/app/controllers/api/v1/announcements_controller.rb index ee79fc19f..82e9cf7de 100644 --- a/app/controllers/api/v1/announcements_controller.rb +++ b/app/controllers/api/v1/announcements_controller.rb @@ -18,9 +18,7 @@ class Api::V1::AnnouncementsController < Api::BaseController private def set_announcements - @announcements = begin - Announcement.published.chronological - end + @announcements = Announcement.published.chronological end def set_announcement diff --git a/app/controllers/api/v1/trends/links_controller.rb b/app/controllers/api/v1/trends/links_controller.rb index 8ff3b364e..3ce20fb78 100644 --- a/app/controllers/api/v1/trends/links_controller.rb +++ b/app/controllers/api/v1/trends/links_controller.rb @@ -18,13 +18,11 @@ class Api::V1::Trends::LinksController < Api::BaseController end def set_links - @links = begin - if enabled? - links_from_trends.offset(offset_param).limit(limit_param(DEFAULT_LINKS_LIMIT)) - else - [] - end - end + @links = if enabled? + links_from_trends.offset(offset_param).limit(limit_param(DEFAULT_LINKS_LIMIT)) + else + [] + end end def links_from_trends diff --git a/app/controllers/api/v1/trends/statuses_controller.rb b/app/controllers/api/v1/trends/statuses_controller.rb index c275d5fc8..3aab92477 100644 --- a/app/controllers/api/v1/trends/statuses_controller.rb +++ b/app/controllers/api/v1/trends/statuses_controller.rb @@ -16,13 +16,11 @@ class Api::V1::Trends::StatusesController < Api::BaseController end def set_statuses - @statuses = begin - if enabled? - cache_collection(statuses_from_trends.offset(offset_param).limit(limit_param(DEFAULT_STATUSES_LIMIT)), Status) - else - [] - end - end + @statuses = if enabled? + cache_collection(statuses_from_trends.offset(offset_param).limit(limit_param(DEFAULT_STATUSES_LIMIT)), Status) + else + [] + end end def statuses_from_trends diff --git a/app/controllers/api/v1/trends/tags_controller.rb b/app/controllers/api/v1/trends/tags_controller.rb index 21adfa2a1..75c3ed218 100644 --- a/app/controllers/api/v1/trends/tags_controller.rb +++ b/app/controllers/api/v1/trends/tags_controller.rb @@ -18,13 +18,11 @@ class Api::V1::Trends::TagsController < Api::BaseController end def set_tags - @tags = begin - if enabled? - tags_from_trends.offset(offset_param).limit(limit_param(DEFAULT_TAGS_LIMIT)) - else - [] - end - end + @tags = if enabled? + tags_from_trends.offset(offset_param).limit(limit_param(DEFAULT_TAGS_LIMIT)) + else + [] + end end def tags_from_trends diff --git a/app/controllers/concerns/rate_limit_headers.rb b/app/controllers/concerns/rate_limit_headers.rb index b21abfb03..30702f00e 100644 --- a/app/controllers/concerns/rate_limit_headers.rb +++ b/app/controllers/concerns/rate_limit_headers.rb @@ -6,13 +6,11 @@ module RateLimitHeaders class_methods do def override_rate_limit_headers(method_name, options = {}) around_action(only: method_name, if: :current_account) do |_controller, block| - begin - block.call - ensure - rate_limiter = RateLimiter.new(current_account, options) - rate_limit_headers = rate_limiter.to_headers - response.headers.merge!(rate_limit_headers) unless response.headers['X-RateLimit-Remaining'].present? && rate_limit_headers['X-RateLimit-Remaining'].to_i > response.headers['X-RateLimit-Remaining'].to_i - end + block.call + ensure + rate_limiter = RateLimiter.new(current_account, options) + rate_limit_headers = rate_limiter.to_headers + response.headers.merge!(rate_limit_headers) unless response.headers['X-RateLimit-Remaining'].present? && rate_limit_headers['X-RateLimit-Remaining'].to_i > response.headers['X-RateLimit-Remaining'].to_i end end end diff --git a/app/controllers/concerns/two_factor_authentication_concern.rb b/app/controllers/concerns/two_factor_authentication_concern.rb index 27f2367a8..e69b67a79 100644 --- a/app/controllers/concerns/two_factor_authentication_concern.rb +++ b/app/controllers/concerns/two_factor_authentication_concern.rb @@ -79,13 +79,11 @@ module TwoFactorAuthenticationConcern @body_classes = 'lighter' @webauthn_enabled = user.webauthn_enabled? - @scheme_type = begin - if user.webauthn_enabled? && user_params[:otp_attempt].blank? - 'webauthn' - else - 'totp' - end - end + @scheme_type = if user.webauthn_enabled? && user_params[:otp_attempt].blank? + 'webauthn' + else + 'totp' + end set_locale { render :two_factor } end diff --git a/app/helpers/admin/dashboard_helper.rb b/app/helpers/admin/dashboard_helper.rb index c21d41341..6096ff138 100644 --- a/app/helpers/admin/dashboard_helper.rb +++ b/app/helpers/admin/dashboard_helper.rb @@ -19,19 +19,17 @@ module Admin::DashboardHelper end def relevant_account_timestamp(account) - timestamp, exact = begin - if account.user_current_sign_in_at && account.user_current_sign_in_at < 24.hours.ago - [account.user_current_sign_in_at, true] - elsif account.user_current_sign_in_at - [account.user_current_sign_in_at, false] - elsif account.user_pending? - [account.user_created_at, true] - elsif account.last_status_at.present? - [account.last_status_at, true] - else - [nil, false] - end - end + timestamp, exact = if account.user_current_sign_in_at && account.user_current_sign_in_at < 24.hours.ago + [account.user_current_sign_in_at, true] + elsif account.user_current_sign_in_at + [account.user_current_sign_in_at, false] + elsif account.user_pending? + [account.user_created_at, true] + elsif account.last_status_at.present? + [account.last_status_at, true] + else + [nil, false] + end return '-' if timestamp.nil? return t('generic.today') unless exact diff --git a/app/helpers/admin/trends/statuses_helper.rb b/app/helpers/admin/trends/statuses_helper.rb index 214c1e2a6..79fee44dc 100644 --- a/app/helpers/admin/trends/statuses_helper.rb +++ b/app/helpers/admin/trends/statuses_helper.rb @@ -2,13 +2,11 @@ module Admin::Trends::StatusesHelper def one_line_preview(status) - text = begin - if status.local? - status.text.split("\n").first - else - Nokogiri::HTML(status.text).css('html > body > *').first&.text - end - end + text = if status.local? + status.text.split("\n").first + else + Nokogiri::HTML(status.text).css('html > body > *').first&.text + end return '' if text.blank? diff --git a/app/helpers/branding_helper.rb b/app/helpers/branding_helper.rb index ad7702aea..548c95411 100644 --- a/app/helpers/branding_helper.rb +++ b/app/helpers/branding_helper.rb @@ -23,14 +23,12 @@ module BrandingHelper end def render_symbol(version = :icon) - path = begin - case version - when :icon - 'logo-symbol-icon.svg' - when :wordmark - 'logo-symbol-wordmark.svg' - end - end + path = case version + when :icon + 'logo-symbol-icon.svg' + when :wordmark + 'logo-symbol-wordmark.svg' + end render(file: Rails.root.join('app', 'javascript', 'images', path)).html_safe # rubocop:disable Rails/OutputSafety end diff --git a/app/helpers/domain_control_helper.rb b/app/helpers/domain_control_helper.rb index ac60cad29..ffcf375ea 100644 --- a/app/helpers/domain_control_helper.rb +++ b/app/helpers/domain_control_helper.rb @@ -4,13 +4,11 @@ module DomainControlHelper def domain_not_allowed?(uri_or_domain) return if uri_or_domain.blank? - domain = begin - if uri_or_domain.include?('://') - Addressable::URI.parse(uri_or_domain).host - else - uri_or_domain - end - end + domain = if uri_or_domain.include?('://') + Addressable::URI.parse(uri_or_domain).host + else + uri_or_domain + end if whitelist_mode? !DomainAllow.allowed?(domain) diff --git a/app/helpers/formatting_helper.rb b/app/helpers/formatting_helper.rb index c70931489..d390b9bc9 100644 --- a/app/helpers/formatting_helper.rb +++ b/app/helpers/formatting_helper.rb @@ -21,30 +21,26 @@ module FormattingHelper def rss_status_content_format(status) html = status_content_format(status) - before_html = begin - if status.spoiler_text? - tag.p do - tag.strong do - I18n.t('rss.content_warning', locale: available_locale_or_nil(status.language) || I18n.default_locale) - end - - status.spoiler_text - end + tag.hr - end - end - - after_html = begin - if status.preloadable_poll - tag.p do - safe_join( - status.preloadable_poll.options.map do |o| - tag.send(status.preloadable_poll.multiple? ? 'checkbox' : 'radio', o, disabled: true) - end, - tag.br - ) - end - end - end + before_html = if status.spoiler_text? + tag.p do + tag.strong do + I18n.t('rss.content_warning', locale: available_locale_or_nil(status.language) || I18n.default_locale) + end + + status.spoiler_text + end + tag.hr + end + + after_html = if status.preloadable_poll + tag.p do + safe_join( + status.preloadable_poll.options.map do |o| + tag.send(status.preloadable_poll.multiple? ? 'checkbox' : 'radio', o, disabled: true) + end, + tag.br + ) + end + end prerender_custom_emojis( safe_join([before_html, html, after_html]), diff --git a/app/helpers/instance_helper.rb b/app/helpers/instance_helper.rb index daacb535b..bedfe6f30 100644 --- a/app/helpers/instance_helper.rb +++ b/app/helpers/instance_helper.rb @@ -10,13 +10,11 @@ module InstanceHelper end def description_for_sign_up - prefix = begin - if @invite.present? - I18n.t('auth.description.prefix_invited_by_user', name: @invite.user.account.username) - else - I18n.t('auth.description.prefix_sign_up') - end - end + prefix = if @invite.present? + I18n.t('auth.description.prefix_invited_by_user', name: @invite.user.account.username) + else + I18n.t('auth.description.prefix_sign_up') + end safe_join([prefix, I18n.t('auth.description.suffix')], ' ') end diff --git a/app/helpers/jsonld_helper.rb b/app/helpers/jsonld_helper.rb index e5787fd47..24362b61e 100644 --- a/app/helpers/jsonld_helper.rb +++ b/app/helpers/jsonld_helper.rb @@ -26,15 +26,13 @@ module JsonLdHelper # The url attribute can be a string, an array of strings, or an array of objects. # The objects could include a mimeType. Not-included mimeType means it's text/html. def url_to_href(value, preferred_type = nil) - single_value = begin - if value.is_a?(Array) && !value.first.is_a?(String) - value.find { |link| preferred_type.nil? || ((link['mimeType'].presence || 'text/html') == preferred_type) } - elsif value.is_a?(Array) - value.first - else - value - end - end + single_value = if value.is_a?(Array) && !value.first.is_a?(String) + value.find { |link| preferred_type.nil? || ((link['mimeType'].presence || 'text/html') == preferred_type) } + elsif value.is_a?(Array) + value.first + else + value + end if single_value.nil? || single_value.is_a?(String) single_value diff --git a/app/lib/activity_tracker.rb b/app/lib/activity_tracker.rb index 6d3401b37..8829d8605 100644 --- a/app/lib/activity_tracker.rb +++ b/app/lib/activity_tracker.rb @@ -27,14 +27,12 @@ class ActivityTracker (start_at.to_date...end_at.to_date).map do |date| key = key_at(date.to_time(:utc)) - value = begin - case @type - when :basic - redis.get(key).to_i - when :unique - redis.pfcount(key) - end - end + value = case @type + when :basic + redis.get(key).to_i + when :unique + redis.pfcount(key) + end [date, value] end diff --git a/app/lib/activitypub/activity/create.rb b/app/lib/activitypub/activity/create.rb index f82112c02..e2355bfbc 100644 --- a/app/lib/activitypub/activity/create.rb +++ b/app/lib/activitypub/activity/create.rb @@ -108,26 +108,24 @@ class ActivityPub::Activity::Create < ActivityPub::Activity def process_status_params @status_parser = ActivityPub::Parser::StatusParser.new(@json, followers_collection: @account.followers_url) - @params = begin - { - uri: @status_parser.uri, - url: @status_parser.url || @status_parser.uri, - account: @account, - text: converted_object_type? ? converted_text : (@status_parser.text || ''), - language: @status_parser.language, - spoiler_text: converted_object_type? ? '' : (@status_parser.spoiler_text || ''), - created_at: @status_parser.created_at, - edited_at: @status_parser.edited_at && @status_parser.edited_at != @status_parser.created_at ? @status_parser.edited_at : nil, - override_timestamps: @options[:override_timestamps], - reply: @status_parser.reply, - sensitive: @account.sensitized? || @status_parser.sensitive || false, - visibility: @status_parser.visibility, - thread: replied_to_status, - conversation: conversation_from_uri(@object['conversation']), - media_attachment_ids: process_attachments.take(4).map(&:id), - poll: process_poll, - } - end + @params = { + uri: @status_parser.uri, + url: @status_parser.url || @status_parser.uri, + account: @account, + text: converted_object_type? ? converted_text : (@status_parser.text || ''), + language: @status_parser.language, + spoiler_text: converted_object_type? ? '' : (@status_parser.spoiler_text || ''), + created_at: @status_parser.created_at, + edited_at: @status_parser.edited_at && @status_parser.edited_at != @status_parser.created_at ? @status_parser.edited_at : nil, + override_timestamps: @options[:override_timestamps], + reply: @status_parser.reply, + sensitive: @account.sensitized? || @status_parser.sensitive || false, + visibility: @status_parser.visibility, + thread: replied_to_status, + conversation: conversation_from_uri(@object['conversation']), + media_attachment_ids: process_attachments.take(4).map(&:id), + poll: process_poll, + } end def process_audience diff --git a/app/lib/activitypub/forwarder.rb b/app/lib/activitypub/forwarder.rb index 4206b9d82..b01d63e58 100644 --- a/app/lib/activitypub/forwarder.rb +++ b/app/lib/activitypub/forwarder.rb @@ -28,13 +28,11 @@ class ActivityPub::Forwarder end def signature_account_id - @signature_account_id ||= begin - if in_reply_to_local? - in_reply_to.account_id - else - reblogged_by_account_ids.first - end - end + @signature_account_id ||= if in_reply_to_local? + in_reply_to.account_id + else + reblogged_by_account_ids.first + end end def inboxes diff --git a/app/lib/admin/metrics/dimension/software_versions_dimension.rb b/app/lib/admin/metrics/dimension/software_versions_dimension.rb index 816615f99..9ab3776c9 100644 --- a/app/lib/admin/metrics/dimension/software_versions_dimension.rb +++ b/app/lib/admin/metrics/dimension/software_versions_dimension.rb @@ -58,12 +58,10 @@ class Admin::Metrics::Dimension::SoftwareVersionsDimension < Admin::Metrics::Dim end def redis_info - @redis_info ||= begin - if redis.is_a?(Redis::Namespace) - redis.redis.info - else - redis.info - end - end + @redis_info ||= if redis.is_a?(Redis::Namespace) + redis.redis.info + else + redis.info + end end end diff --git a/app/lib/admin/metrics/dimension/space_usage_dimension.rb b/app/lib/admin/metrics/dimension/space_usage_dimension.rb index 5867c5bab..cc8560890 100644 --- a/app/lib/admin/metrics/dimension/space_usage_dimension.rb +++ b/app/lib/admin/metrics/dimension/space_usage_dimension.rb @@ -59,12 +59,10 @@ class Admin::Metrics::Dimension::SpaceUsageDimension < Admin::Metrics::Dimension end def redis_info - @redis_info ||= begin - if redis.is_a?(Redis::Namespace) - redis.redis.info - else - redis.info - end - end + @redis_info ||= if redis.is_a?(Redis::Namespace) + redis.redis.info + else + redis.info + end end end diff --git a/app/lib/extractor.rb b/app/lib/extractor.rb index 1eba689ef..540bbe1a9 100644 --- a/app/lib/extractor.rb +++ b/app/lib/extractor.rb @@ -8,12 +8,10 @@ module Extractor module_function def extract_entities_with_indices(text, options = {}, &block) - entities = begin - extract_urls_with_indices(text, options) + - extract_hashtags_with_indices(text, check_url_overlap: false) + - extract_mentions_or_lists_with_indices(text) + - extract_extra_uris_with_indices(text) - end + entities = extract_urls_with_indices(text, options) + + extract_hashtags_with_indices(text, check_url_overlap: false) + + extract_mentions_or_lists_with_indices(text) + + extract_extra_uris_with_indices(text) return [] if entities.empty? diff --git a/app/lib/importer/statuses_index_importer.rb b/app/lib/importer/statuses_index_importer.rb index 5b5153d5c..b0721c2e0 100644 --- a/app/lib/importer/statuses_index_importer.rb +++ b/app/lib/importer/statuses_index_importer.rb @@ -24,13 +24,11 @@ class Importer::StatusesIndexImporter < Importer::BaseImporter # is called before rendering the data and we need to filter based # on the results of the filter, so this filtering happens here instead bulk.map! do |entry| - new_entry = begin - if entry[:index] && entry.dig(:index, :data, 'searchable_by').blank? - { delete: entry[:index].except(:data) } - else - entry - end - end + new_entry = if entry[:index] && entry.dig(:index, :data, 'searchable_by').blank? + { delete: entry[:index].except(:data) } + else + entry + end if new_entry[:index] indexed += 1 diff --git a/app/lib/link_details_extractor.rb b/app/lib/link_details_extractor.rb index 2e0672abe..74a7d0f3b 100644 --- a/app/lib/link_details_extractor.rb +++ b/app/lib/link_details_extractor.rb @@ -232,26 +232,24 @@ class LinkDetailsExtractor end def structured_data - @structured_data ||= begin - # Some publications have more than one JSON-LD definition on the page, - # and some of those definitions aren't valid JSON either, so we have - # to loop through here until we find something that is the right type - # and doesn't break - document.xpath('//script[@type="application/ld+json"]').filter_map do |element| - json_ld = element.content&.gsub(CDATA_JUNK_PATTERN, '') + # Some publications have more than one JSON-LD definition on the page, + # and some of those definitions aren't valid JSON either, so we have + # to loop through here until we find something that is the right type + # and doesn't break + @structured_data ||= document.xpath('//script[@type="application/ld+json"]').filter_map do |element| + json_ld = element.content&.gsub(CDATA_JUNK_PATTERN, '') - next if json_ld.blank? + next if json_ld.blank? - structured_data = StructuredData.new(html_entities.decode(json_ld)) + structured_data = StructuredData.new(html_entities.decode(json_ld)) - next unless structured_data.valid? + next unless structured_data.valid? - structured_data - rescue Oj::ParseError, EncodingError - Rails.logger.debug { "Invalid JSON-LD in #{@original_url}" } - next - end.first - end + structured_data + rescue Oj::ParseError, EncodingError + Rails.logger.debug { "Invalid JSON-LD in #{@original_url}" } + next + end.first end def document diff --git a/app/lib/request.rb b/app/lib/request.rb index 0508169dc..be6a69b3f 100644 --- a/app/lib/request.rb +++ b/app/lib/request.rb @@ -215,26 +215,24 @@ class Request addr_by_socket = {} addresses.each do |address| - begin - check_private_address(address, host) + check_private_address(address, host) - sock = ::Socket.new(address.is_a?(Resolv::IPv6) ? ::Socket::AF_INET6 : ::Socket::AF_INET, ::Socket::SOCK_STREAM, 0) - sockaddr = ::Socket.pack_sockaddr_in(port, address.to_s) + sock = ::Socket.new(address.is_a?(Resolv::IPv6) ? ::Socket::AF_INET6 : ::Socket::AF_INET, ::Socket::SOCK_STREAM, 0) + sockaddr = ::Socket.pack_sockaddr_in(port, address.to_s) - sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1) + sock.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1) - sock.connect_nonblock(sockaddr) + sock.connect_nonblock(sockaddr) - # If that hasn't raised an exception, we somehow managed to connect - # immediately, close pending sockets and return immediately - socks.each(&:close) - return sock - rescue IO::WaitWritable - socks << sock - addr_by_socket[sock] = sockaddr - rescue => e - outer_e = e - end + # If that hasn't raised an exception, we somehow managed to connect + # immediately, close pending sockets and return immediately + socks.each(&:close) + return sock + rescue IO::WaitWritable + socks << sock + addr_by_socket[sock] = sockaddr + rescue => e + outer_e = e end until socks.empty? @@ -279,9 +277,7 @@ class Request end def private_address_exceptions - @private_address_exceptions = begin - (ENV['ALLOWED_PRIVATE_ADDRESSES'] || '').split(',').map { |addr| IPAddr.new(addr) } - end + @private_address_exceptions = (ENV['ALLOWED_PRIVATE_ADDRESSES'] || '').split(',').map { |addr| IPAddr.new(addr) } end end end diff --git a/app/models/account.rb b/app/models/account.rb index a96e204fa..2c0cd577e 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -459,13 +459,12 @@ class Account < ApplicationRecord return [] if text.blank? text.scan(MENTION_RE).map { |match| match.first.split('@', 2) }.uniq.filter_map do |(username, domain)| - domain = begin - if TagManager.instance.local_domain?(domain) - nil - else - TagManager.instance.normalize_domain(domain) - end - end + domain = if TagManager.instance.local_domain?(domain) + nil + else + TagManager.instance.normalize_domain(domain) + end + EntityCache.instance.mention(username, domain) end end diff --git a/app/models/account/field.rb b/app/models/account/field.rb index 4db4cac30..98c29726d 100644 --- a/app/models/account/field.rb +++ b/app/models/account/field.rb @@ -25,13 +25,11 @@ class Account::Field < ActiveModelSerializers::Model end def value_for_verification - @value_for_verification ||= begin - if account.local? - value - else - extract_url_from_html - end - end + @value_for_verification ||= if account.local? + value + else + extract_url_from_html + end end def verifiable? diff --git a/app/models/admin/account_action.rb b/app/models/admin/account_action.rb index bce0d6e17..1ce28f5c8 100644 --- a/app/models/admin/account_action.rb +++ b/app/models/admin/account_action.rb @@ -166,13 +166,11 @@ class Admin::AccountAction end def reports - @reports ||= begin - if type == 'none' - with_report? ? [report] : [] - else - Report.where(target_account: target_account).unresolved - end - end + @reports ||= if type == 'none' + with_report? ? [report] : [] + else + Report.where(target_account: target_account).unresolved + end end def warning_preset diff --git a/app/models/announcement.rb b/app/models/announcement.rb index 4b2cb4c6d..898bf3efa 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -54,13 +54,11 @@ class Announcement < ApplicationRecord end def statuses - @statuses ||= begin - if status_ids.nil? - [] - else - Status.where(id: status_ids, visibility: [:public, :unlisted]) - end - end + @statuses ||= if status_ids.nil? + [] + else + Status.where(id: status_ids, visibility: [:public, :unlisted]) + end end def tags diff --git a/app/models/concerns/account_merging.rb b/app/models/concerns/account_merging.rb index 8161761fb..41071633d 100644 --- a/app/models/concerns/account_merging.rb +++ b/app/models/concerns/account_merging.rb @@ -21,11 +21,9 @@ module AccountMerging owned_classes.each do |klass| klass.where(account_id: other_account.id).find_each do |record| - begin - record.update_attribute(:account_id, id) - rescue ActiveRecord::RecordNotUnique - next - end + record.update_attribute(:account_id, id) + rescue ActiveRecord::RecordNotUnique + next end end @@ -36,11 +34,9 @@ module AccountMerging target_classes.each do |klass| klass.where(target_account_id: other_account.id).find_each do |record| - begin - record.update_attribute(:target_account_id, id) - rescue ActiveRecord::RecordNotUnique - next - end + record.update_attribute(:target_account_id, id) + rescue ActiveRecord::RecordNotUnique + next end end diff --git a/app/models/concerns/pam_authenticable.rb b/app/models/concerns/pam_authenticable.rb index 6169d4dfa..f97f986a4 100644 --- a/app/models/concerns/pam_authenticable.rb +++ b/app/models/concerns/pam_authenticable.rb @@ -42,13 +42,11 @@ module PamAuthenticable def self.pam_get_user(attributes = {}) return nil unless attributes[:email] - resource = begin - if Devise.check_at_sign && !attributes[:email].index('@') - joins(:account).find_by(accounts: { username: attributes[:email] }) - else - find_by(email: attributes[:email]) - end - end + resource = if Devise.check_at_sign && !attributes[:email].index('@') + joins(:account).find_by(accounts: { username: attributes[:email] }) + else + find_by(email: attributes[:email]) + end if resource.nil? resource = new(email: attributes[:email], agreement: true) diff --git a/app/models/email_domain_block.rb b/app/models/email_domain_block.rb index 10a0e5102..3a56e4f2a 100644 --- a/app/models/email_domain_block.rb +++ b/app/models/email_domain_block.rb @@ -69,13 +69,11 @@ class EmailDomainBlock < ApplicationRecord def extract_uris(domain_or_domains) Array(domain_or_domains).map do |str| - domain = begin - if str.include?('@') - str.split('@', 2).last - else - str - end - end + domain = if str.include?('@') + str.split('@', 2).last + else + str + end Addressable::URI.new.tap { |u| u.host = domain.strip } if domain.present? rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError diff --git a/app/models/form/admin_settings.rb b/app/models/form/admin_settings.rb index 070478e8e..95c53084a 100644 --- a/app/models/form/admin_settings.rb +++ b/app/models/form/admin_settings.rb @@ -76,13 +76,11 @@ class Form::AdminSettings define_method(key) do return instance_variable_get("@#{key}") if instance_variable_defined?("@#{key}") - stored_value = begin - if UPLOAD_KEYS.include?(key) - SiteUpload.where(var: key).first_or_initialize(var: key) - else - Setting.public_send(key) - end - end + stored_value = if UPLOAD_KEYS.include?(key) + SiteUpload.where(var: key).first_or_initialize(var: key) + else + Setting.public_send(key) + end instance_variable_set("@#{key}", stored_value) end diff --git a/app/models/form/custom_emoji_batch.rb b/app/models/form/custom_emoji_batch.rb index f4fa84c10..484415f90 100644 --- a/app/models/form/custom_emoji_batch.rb +++ b/app/models/form/custom_emoji_batch.rb @@ -36,13 +36,11 @@ class Form::CustomEmojiBatch def update! custom_emojis.each { |custom_emoji| authorize(custom_emoji, :update?) } - category = begin - if category_id.present? - CustomEmojiCategory.find(category_id) - elsif category_name.present? - CustomEmojiCategory.find_or_create_by!(name: category_name) - end - end + category = if category_id.present? + CustomEmojiCategory.find(category_id) + elsif category_name.present? + CustomEmojiCategory.find_or_create_by!(name: category_name) + end custom_emojis.each do |custom_emoji| custom_emoji.update(category_id: category&.id) diff --git a/app/models/notification.rb b/app/models/notification.rb index bbc63c1c0..01155c363 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -87,13 +87,11 @@ class Notification < ApplicationRecord class << self def browserable(types: [], exclude_types: [], from_account_id: nil) - requested_types = begin - if types.empty? - TYPES - else - types.map(&:to_sym) & TYPES - end - end + requested_types = if types.empty? + TYPES + else + types.map(&:to_sym) & TYPES + end requested_types -= exclude_types.map(&:to_sym) diff --git a/app/models/remote_follow.rb b/app/models/remote_follow.rb index 911c06713..10715ac97 100644 --- a/app/models/remote_follow.rb +++ b/app/models/remote_follow.rb @@ -36,13 +36,11 @@ class RemoteFollow username, domain = value.strip.gsub(/\A@/, '').split('@') - domain = begin - if TagManager.instance.local_domain?(domain) - nil - else - TagManager.instance.normalize_domain(domain) - end - end + domain = if TagManager.instance.local_domain?(domain) + nil + else + TagManager.instance.normalize_domain(domain) + end [username, domain].compact.join('@') rescue Addressable::URI::InvalidURIError diff --git a/app/models/status.rb b/app/models/status.rb index a924a985f..102dfa994 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -368,13 +368,12 @@ class Status < ApplicationRecord return [] if text.blank? text.scan(FetchLinkCardService::URL_PATTERN).map(&:second).uniq.filter_map do |url| - status = begin - if TagManager.instance.local_url?(url) - ActivityPub::TagManager.instance.uri_to_resource(url, Status) - else - EntityCache.instance.status(url) - end - end + status = if TagManager.instance.local_url?(url) + ActivityPub::TagManager.instance.uri_to_resource(url, Status) + else + EntityCache.instance.status(url) + end + status&.distributable? ? status : nil end end diff --git a/app/models/status_edit.rb b/app/models/status_edit.rb index e33470226..dd2d5fc1e 100644 --- a/app/models/status_edit.rb +++ b/app/models/status_edit.rb @@ -51,14 +51,12 @@ class StatusEdit < ApplicationRecord def ordered_media_attachments return @ordered_media_attachments if defined?(@ordered_media_attachments) - @ordered_media_attachments = begin - if ordered_media_attachment_ids.nil? - [] - else - map = status.media_attachments.index_by(&:id) - ordered_media_attachment_ids.map.with_index { |media_attachment_id, index| PreservedMediaAttachment.new(media_attachment: map[media_attachment_id], description: media_descriptions[index]) } - end - end + @ordered_media_attachments = if ordered_media_attachment_ids.nil? + [] + else + map = status.media_attachments.index_by(&:id) + ordered_media_attachment_ids.map.with_index { |media_attachment_id, index| PreservedMediaAttachment.new(media_attachment: map[media_attachment_id], description: media_descriptions[index]) } + end end def proper diff --git a/app/models/trends/links.rb b/app/models/trends/links.rb index 8808b3ab6..c94f7c023 100644 --- a/app/models/trends/links.rb +++ b/app/models/trends/links.rb @@ -113,13 +113,11 @@ class Trends::Links < Trends::Base max_score = preview_card.max_score max_score = 0 if max_time.nil? || max_time < (at_time - options[:max_score_cooldown]) - score = begin - if expected > observed || observed < options[:threshold] - 0 - else - ((observed - expected)**2) / expected - end - end + score = if expected > observed || observed < options[:threshold] + 0 + else + ((observed - expected)**2) / expected + end if score > max_score max_score = score @@ -129,13 +127,11 @@ class Trends::Links < Trends::Base preview_card.update_columns(max_score: max_score, max_score_at: max_time) end - decaying_score = begin - if max_score.zero? || !valid_locale?(preview_card.language) - 0 - else - max_score * (0.5**((at_time.to_f - max_time.to_f) / options[:max_score_halflife].to_f)) - end - end + decaying_score = if max_score.zero? || !valid_locale?(preview_card.language) + 0 + else + max_score * (0.5**((at_time.to_f - max_time.to_f) / options[:max_score_halflife].to_f)) + end [decaying_score, preview_card] end diff --git a/app/models/trends/statuses.rb b/app/models/trends/statuses.rb index c9ef7c8f2..84bff9c02 100644 --- a/app/models/trends/statuses.rb +++ b/app/models/trends/statuses.rb @@ -99,21 +99,17 @@ class Trends::Statuses < Trends::Base expected = 1.0 observed = (status.reblogs_count + status.favourites_count).to_f - score = begin - if expected > observed || observed < options[:threshold] - 0 - else - ((observed - expected)**2) / expected - end - end - - decaying_score = begin - if score.zero? || !eligible?(status) - 0 - else - score * (0.5**((at_time.to_f - status.created_at.to_f) / options[:score_halflife].to_f)) - end - end + score = if expected > observed || observed < options[:threshold] + 0 + else + ((observed - expected)**2) / expected + end + + decaying_score = if score.zero? || !eligible?(status) + 0 + else + score * (0.5**((at_time.to_f - status.created_at.to_f) / options[:score_halflife].to_f)) + end [decaying_score, status] end diff --git a/app/models/trends/tag_filter.rb b/app/models/trends/tag_filter.rb index 3b142efc4..46b747819 100644 --- a/app/models/trends/tag_filter.rb +++ b/app/models/trends/tag_filter.rb @@ -13,13 +13,11 @@ class Trends::TagFilter end def results - scope = begin - if params[:status] == 'pending_review' - Tag.unscoped - else - trending_scope - end - end + scope = if params[:status] == 'pending_review' + Tag.unscoped + else + trending_scope + end params.each do |key, value| next if key.to_s == 'page' diff --git a/app/models/trends/tags.rb b/app/models/trends/tags.rb index 19ade52ba..931532990 100644 --- a/app/models/trends/tags.rb +++ b/app/models/trends/tags.rb @@ -63,13 +63,11 @@ class Trends::Tags < Trends::Base max_score = tag.max_score max_score = 0 if max_time.nil? || max_time < (at_time - options[:max_score_cooldown]) - score = begin - if expected > observed || observed < options[:threshold] - 0 - else - ((observed - expected)**2) / expected - end - end + score = if expected > observed || observed < options[:threshold] + 0 + else + ((observed - expected)**2) / expected + end if score > max_score max_score = score diff --git a/app/models/web/push_subscription.rb b/app/models/web/push_subscription.rb index 6e46573ae..dfaadf5cc 100644 --- a/app/models/web/push_subscription.rb +++ b/app/models/web/push_subscription.rb @@ -53,25 +53,21 @@ class Web::PushSubscription < ApplicationRecord def associated_user return @associated_user if defined?(@associated_user) - @associated_user = begin - if user_id.nil? - session_activation.user - else - user - end - end + @associated_user = if user_id.nil? + session_activation.user + else + user + end end def associated_access_token return @associated_access_token if defined?(@associated_access_token) - @associated_access_token = begin - if access_token_id.nil? - find_or_create_access_token.token - else - access_token.token - end - end + @associated_access_token = if access_token_id.nil? + find_or_create_access_token.token + else + access_token.token + end end class << self diff --git a/app/presenters/tag_relationships_presenter.rb b/app/presenters/tag_relationships_presenter.rb index c3bdbaf07..52e24314b 100644 --- a/app/presenters/tag_relationships_presenter.rb +++ b/app/presenters/tag_relationships_presenter.rb @@ -4,12 +4,10 @@ class TagRelationshipsPresenter attr_reader :following_map def initialize(tags, current_account_id = nil, **options) - @following_map = begin - if current_account_id.nil? - {} - else - TagFollow.select(:tag_id).where(tag_id: tags.map(&:id), account_id: current_account_id).each_with_object({}) { |f, h| h[f.tag_id] = true }.merge(options[:following_map] || {}) - end - end + @following_map = if current_account_id.nil? + {} + else + TagFollow.select(:tag_id).where(tag_id: tags.map(&:id), account_id: current_account_id).each_with_object({}) { |f, h| h[f.tag_id] = true }.merge(options[:following_map] || {}) + end end end diff --git a/app/services/account_search_service.rb b/app/services/account_search_service.rb index 85538870b..dfc3a45f8 100644 --- a/app/services/account_search_service.rb +++ b/app/services/account_search_service.rb @@ -32,15 +32,13 @@ class AccountSearchService < BaseService return @exact_match if defined?(@exact_match) - match = begin - if options[:resolve] - ResolveAccountService.new.call(query) - elsif domain_is_local? - Account.find_local(query_username) - else - Account.find_remote(query_username, query_domain) - end - end + match = if options[:resolve] + ResolveAccountService.new.call(query) + elsif domain_is_local? + Account.find_local(query_username) + else + Account.find_remote(query_username, query_domain) + end match = nil if !match.nil? && !account.nil? && options[:following] && !account.following?(match) diff --git a/app/services/activitypub/fetch_featured_tags_collection_service.rb b/app/services/activitypub/fetch_featured_tags_collection_service.rb index ab047a0f8..ff1a88aa1 100644 --- a/app/services/activitypub/fetch_featured_tags_collection_service.rb +++ b/app/services/activitypub/fetch_featured_tags_collection_service.rb @@ -22,14 +22,12 @@ class ActivityPub::FetchFeaturedTagsCollectionService < BaseService collection = fetch_collection(collection['first']) if collection['first'].present? while collection.is_a?(Hash) - items = begin - case collection['type'] - when 'Collection', 'CollectionPage' - collection['items'] - when 'OrderedCollection', 'OrderedCollectionPage' - collection['orderedItems'] - end - end + items = case collection['type'] + when 'Collection', 'CollectionPage' + collection['items'] + when 'OrderedCollection', 'OrderedCollectionPage' + collection['orderedItems'] + end break if items.blank? diff --git a/app/services/activitypub/fetch_remote_status_service.rb b/app/services/activitypub/fetch_remote_status_service.rb index 936737bf6..aea80f078 100644 --- a/app/services/activitypub/fetch_remote_status_service.rb +++ b/app/services/activitypub/fetch_remote_status_service.rb @@ -9,13 +9,11 @@ class ActivityPub::FetchRemoteStatusService < BaseService # Should be called when uri has already been checked for locality def call(uri, id: true, prefetched_body: nil, on_behalf_of: nil, expected_actor_uri: nil, request_id: nil) @request_id = request_id || "#{Time.now.utc.to_i}-status-#{uri}" - @json = begin - if prefetched_body.nil? - fetch_resource(uri, id, on_behalf_of) - else - body_to_json(prefetched_body, compare_id: id ? uri : nil) - end - end + @json = if prefetched_body.nil? + fetch_resource(uri, id, on_behalf_of) + else + body_to_json(prefetched_body, compare_id: id ? uri : nil) + end return unless supported_context? diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index 4d55aa5e2..d5fa9af54 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -69,16 +69,14 @@ class FetchLinkCardService < BaseService end def parse_urls - urls = begin - if @status.local? - @status.text.scan(URL_PATTERN).map { |array| Addressable::URI.parse(array[1]).normalize } - else - document = Nokogiri::HTML(@status.text) - links = document.css('a') - - links.filter_map { |a| Addressable::URI.parse(a['href']) unless skip_link?(a) }.filter_map(&:normalize) - end - end + urls = if @status.local? + @status.text.scan(URL_PATTERN).map { |array| Addressable::URI.parse(array[1]).normalize } + else + document = Nokogiri::HTML(@status.text) + links = document.css('a') + + links.filter_map { |a| Addressable::URI.parse(a['href']) unless skip_link?(a) }.filter_map(&:normalize) + end urls.reject { |uri| bad_url?(uri) }.first end diff --git a/app/services/process_mentions_service.rb b/app/services/process_mentions_service.rb index 93a96667e..b3b279147 100644 --- a/app/services/process_mentions_service.rb +++ b/app/services/process_mentions_service.rb @@ -28,13 +28,11 @@ class ProcessMentionsService < BaseService @status.text = @status.text.gsub(Account::MENTION_RE) do |match| username, domain = Regexp.last_match(1).split('@') - domain = begin - if TagManager.instance.local_domain?(domain) - nil - else - TagManager.instance.normalize_domain(domain) - end - end + domain = if TagManager.instance.local_domain?(domain) + nil + else + TagManager.instance.normalize_domain(domain) + end mentioned_account = Account.find_remote(username, domain) diff --git a/app/services/reblog_service.rb b/app/services/reblog_service.rb index 7d2981709..6ec094474 100644 --- a/app/services/reblog_service.rb +++ b/app/services/reblog_service.rb @@ -20,13 +20,11 @@ class ReblogService < BaseService return reblog unless reblog.nil? - visibility = begin - if reblogged_status.hidden? - reblogged_status.visibility - else - options[:visibility] || account.user&.setting_default_privacy - end - end + visibility = if reblogged_status.hidden? + reblogged_status.visibility + else + options[:visibility] || account.user&.setting_default_privacy + end reblog = account.statuses.create!(reblog: reblogged_status, text: '', visibility: visibility, rate_limit: options[:with_rate_limit]) diff --git a/app/services/resolve_account_service.rb b/app/services/resolve_account_service.rb index c76df5a0e..abe1534a5 100644 --- a/app/services/resolve_account_service.rb +++ b/app/services/resolve_account_service.rb @@ -71,13 +71,11 @@ class ResolveAccountService < BaseService @username, @domain = uri.strip.gsub(/\A@/, '').split('@') end - @domain = begin - if TagManager.instance.local_domain?(@domain) - nil - else - TagManager.instance.normalize_domain(@domain) - end - end + @domain = if TagManager.instance.local_domain?(@domain) + nil + else + TagManager.instance.normalize_domain(@domain) + end @uri = [@username, @domain].compact.join('@') end diff --git a/app/validators/domain_validator.rb b/app/validators/domain_validator.rb index 6e4a854ff..3a951f9a7 100644 --- a/app/validators/domain_validator.rb +++ b/app/validators/domain_validator.rb @@ -4,13 +4,11 @@ class DomainValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) return if value.blank? - domain = begin - if options[:acct] - value.split('@').last - else - value - end - end + domain = if options[:acct] + value.split('@').last + else + value + end record.errors.add(attribute, I18n.t('domain_validator.invalid_domain')) unless compliant?(domain) end diff --git a/app/validators/existing_username_validator.rb b/app/validators/existing_username_validator.rb index 1c5596821..45de4f4a4 100644 --- a/app/validators/existing_username_validator.rb +++ b/app/validators/existing_username_validator.rb @@ -4,16 +4,14 @@ class ExistingUsernameValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) return if value.blank? - usernames_and_domains = begin - value.split(',').map do |str| - username, domain = str.strip.gsub(/\A@/, '').split('@', 2) - domain = nil if TagManager.instance.local_domain?(domain) + usernames_and_domains = value.split(',').map do |str| + username, domain = str.strip.gsub(/\A@/, '').split('@', 2) + domain = nil if TagManager.instance.local_domain?(domain) - next if username.blank? + next if username.blank? - [str, username, domain] - end.compact - end + [str, username, domain] + end.compact usernames_with_no_accounts = usernames_and_domains.filter_map do |(str, username, domain)| str unless Account.find_remote(username, domain) diff --git a/app/validators/import_validator.rb b/app/validators/import_validator.rb index cbad56df6..782baf5d6 100644 --- a/app/validators/import_validator.rb +++ b/app/validators/import_validator.rb @@ -35,13 +35,11 @@ class ImportValidator < ActiveModel::Validator def validate_following_import(import, row_count) base_limit = FollowLimitValidator.limit_for_account(import.account) - limit = begin - if import.overwrite? - base_limit - else - base_limit - import.account.following_count - end - end + limit = if import.overwrite? + base_limit + else + base_limit - import.account.following_count + end import.errors.add(:data, I18n.t('users.follow_limit_reached', limit: base_limit)) if row_count > limit end diff --git a/app/workers/backup_worker.rb b/app/workers/backup_worker.rb index 7b0b52844..df933142a 100644 --- a/app/workers/backup_worker.rb +++ b/app/workers/backup_worker.rb @@ -9,12 +9,10 @@ class BackupWorker backup_id = msg['args'].first ActiveRecord::Base.connection_pool.with_connection do - begin - backup = Backup.find(backup_id) - backup.destroy - rescue ActiveRecord::RecordNotFound - true - end + backup = Backup.find(backup_id) + backup.destroy + rescue ActiveRecord::RecordNotFound + true end end diff --git a/app/workers/post_process_media_worker.rb b/app/workers/post_process_media_worker.rb index 24201101c..996d5def9 100644 --- a/app/workers/post_process_media_worker.rb +++ b/app/workers/post_process_media_worker.rb @@ -9,13 +9,11 @@ class PostProcessMediaWorker media_attachment_id = msg['args'].first ActiveRecord::Base.connection_pool.with_connection do - begin - media_attachment = MediaAttachment.find(media_attachment_id) - media_attachment.processing = :failed - media_attachment.save - rescue ActiveRecord::RecordNotFound - true - end + media_attachment = MediaAttachment.find(media_attachment_id) + media_attachment.processing = :failed + media_attachment.save + rescue ActiveRecord::RecordNotFound + true end Sidekiq.logger.error("Processing media attachment #{media_attachment_id} failed with #{msg['error_message']}") diff --git a/app/workers/scheduler/follow_recommendations_scheduler.rb b/app/workers/scheduler/follow_recommendations_scheduler.rb index 57f78170e..04008a9d9 100644 --- a/app/workers/scheduler/follow_recommendations_scheduler.rb +++ b/app/workers/scheduler/follow_recommendations_scheduler.rb @@ -19,13 +19,11 @@ class Scheduler::FollowRecommendationsScheduler fallback_recommendations = FollowRecommendation.order(rank: :desc).limit(SET_SIZE) Trends.available_locales.each do |locale| - recommendations = begin - if AccountSummary.safe.filtered.localized(locale).exists? # We can skip the work if no accounts with that language exist - FollowRecommendation.localized(locale).order(rank: :desc).limit(SET_SIZE).map { |recommendation| [recommendation.account_id, recommendation.rank] } - else - [] - end - end + recommendations = if AccountSummary.safe.filtered.localized(locale).exists? # We can skip the work if no accounts with that language exist + FollowRecommendation.localized(locale).order(rank: :desc).limit(SET_SIZE).map { |recommendation| [recommendation.account_id, recommendation.rank] } + else + [] + end # Use language-agnostic results if there are not enough language-specific ones missing = SET_SIZE - recommendations.size diff --git a/db/migrate/20180528141303_fix_accounts_unique_index.rb b/db/migrate/20180528141303_fix_accounts_unique_index.rb index 3e33e2cac..0b39f7107 100644 --- a/db/migrate/20180528141303_fix_accounts_unique_index.rb +++ b/db/migrate/20180528141303_fix_accounts_unique_index.rb @@ -106,21 +106,17 @@ class FixAccountsUniqueIndex < ActiveRecord::Migration[5.2] # to check for (and skip past) uniqueness errors [Favourite, Follow, FollowRequest, Block, Mute].each do |klass| klass.where(account_id: duplicate_account.id).find_each do |record| - begin - record.update_attribute(:account_id, main_account.id) - rescue ActiveRecord::RecordNotUnique - next - end + record.update_attribute(:account_id, main_account.id) + rescue ActiveRecord::RecordNotUnique + next end end [Follow, FollowRequest, Block, Mute].each do |klass| klass.where(target_account_id: duplicate_account.id).find_each do |record| - begin - record.update_attribute(:target_account_id, main_account.id) - rescue ActiveRecord::RecordNotUnique - next - end + record.update_attribute(:target_account_id, main_account.id) + rescue ActiveRecord::RecordNotUnique + next end end end diff --git a/db/migrate/20180812173710_copy_status_stats.rb b/db/migrate/20180812173710_copy_status_stats.rb index 9b2971beb..45eb9501c 100644 --- a/db/migrate/20180812173710_copy_status_stats.rb +++ b/db/migrate/20180812173710_copy_status_stats.rb @@ -43,12 +43,10 @@ class CopyStatusStats < ActiveRecord::Migration[5.2] # We cannot use bulk INSERT or overarching transactions here because of possible # uniqueness violations that we need to skip over Status.unscoped.select('id, reblogs_count, favourites_count, created_at, updated_at').find_each do |status| - begin - params = [[nil, status.id], [nil, status.reblogs_count], [nil, status.favourites_count], [nil, status.created_at], [nil, status.updated_at]] - exec_insert('INSERT INTO status_stats (status_id, reblogs_count, favourites_count, created_at, updated_at) VALUES ($1, $2, $3, $4, $5)', nil, params) - rescue ActiveRecord::RecordNotUnique - next - end + params = [[nil, status.id], [nil, status.reblogs_count], [nil, status.favourites_count], [nil, status.created_at], [nil, status.updated_at]] + exec_insert('INSERT INTO status_stats (status_id, reblogs_count, favourites_count, created_at, updated_at) VALUES ($1, $2, $3, $4, $5)', nil, params) + rescue ActiveRecord::RecordNotUnique + next end end end diff --git a/db/migrate/20181116173541_copy_account_stats.rb b/db/migrate/20181116173541_copy_account_stats.rb index 20dc85195..f908575cb 100644 --- a/db/migrate/20181116173541_copy_account_stats.rb +++ b/db/migrate/20181116173541_copy_account_stats.rb @@ -43,12 +43,10 @@ class CopyAccountStats < ActiveRecord::Migration[5.2] # We cannot use bulk INSERT or overarching transactions here because of possible # uniqueness violations that we need to skip over Account.unscoped.select('id, statuses_count, following_count, followers_count, created_at, updated_at').find_each do |account| - begin - params = [[nil, account.id], [nil, account[:statuses_count]], [nil, account[:following_count]], [nil, account[:followers_count]], [nil, account.created_at], [nil, account.updated_at]] - exec_insert('INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6)', nil, params) - rescue ActiveRecord::RecordNotUnique - next - end + params = [[nil, account.id], [nil, account[:statuses_count]], [nil, account[:following_count]], [nil, account[:followers_count]], [nil, account.created_at], [nil, account.updated_at]] + exec_insert('INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6)', nil, params) + rescue ActiveRecord::RecordNotUnique + next end end end diff --git a/lib/mastodon/accounts_cli.rb b/lib/mastodon/accounts_cli.rb index 34afbc699..db379eb85 100644 --- a/lib/mastodon/accounts_cli.rb +++ b/lib/mastodon/accounts_cli.rb @@ -490,14 +490,12 @@ module Mastodon scope = Account.where(id: ::Follow.where(account: account).select(:target_account_id)) scope.find_each do |target_account| - begin - UnfollowService.new.call(account, target_account) - rescue => e - progress.log pastel.red("Error processing #{target_account.id}: #{e}") - ensure - progress.increment - processed += 1 - end + UnfollowService.new.call(account, target_account) + rescue => e + progress.log pastel.red("Error processing #{target_account.id}: #{e}") + ensure + progress.increment + processed += 1 end BootstrapTimelineWorker.perform_async(account.id) @@ -507,14 +505,12 @@ module Mastodon scope = Account.where(id: ::Follow.where(target_account: account).select(:account_id)) scope.find_each do |target_account| - begin - UnfollowService.new.call(target_account, account) - rescue => e - progress.log pastel.red("Error processing #{target_account.id}: #{e}") - ensure - progress.increment - processed += 1 - end + UnfollowService.new.call(target_account, account) + rescue => e + progress.log pastel.red("Error processing #{target_account.id}: #{e}") + ensure + progress.increment + processed += 1 end end diff --git a/lib/mastodon/cli_helper.rb b/lib/mastodon/cli_helper.rb index a78a28e27..8704edd75 100644 --- a/lib/mastodon/cli_helper.rb +++ b/lib/mastodon/cli_helper.rb @@ -42,30 +42,28 @@ module Mastodon items.each do |item| futures << Concurrent::Future.execute(executor: pool) do - begin - if !progress.total.nil? && progress.progress + 1 > progress.total - # The number of items has changed between start and now, - # since there is no good way to predict the final count from - # here, just change the progress bar to an indeterminate one - - progress.total = nil - end - - progress.log("Processing #{item.id}") if options[:verbose] - - result = ActiveRecord::Base.connection_pool.with_connection do - yield(item) - ensure - RedisConfiguration.pool.checkin if Thread.current[:redis] - Thread.current[:redis] = nil - end - - aggregate.increment(result) if result.is_a?(Integer) - rescue => e - progress.log pastel.red("Error processing #{item.id}: #{e}") + if !progress.total.nil? && progress.progress + 1 > progress.total + # The number of items has changed between start and now, + # since there is no good way to predict the final count from + # here, just change the progress bar to an indeterminate one + + progress.total = nil + end + + progress.log("Processing #{item.id}") if options[:verbose] + + result = ActiveRecord::Base.connection_pool.with_connection do + yield(item) ensure - progress.increment + RedisConfiguration.pool.checkin if Thread.current[:redis] + Thread.current[:redis] = nil end + + aggregate.increment(result) if result.is_a?(Integer) + rescue => e + progress.log pastel.red("Error processing #{item.id}: #{e}") + ensure + progress.increment end end diff --git a/lib/mastodon/ip_blocks_cli.rb b/lib/mastodon/ip_blocks_cli.rb index 5c38c1aca..08939c092 100644 --- a/lib/mastodon/ip_blocks_cli.rb +++ b/lib/mastodon/ip_blocks_cli.rb @@ -79,13 +79,11 @@ module Mastodon skipped = 0 addresses.each do |address| - ip_blocks = begin - if options[:force] - IpBlock.where('ip >>= ?', address) - else - IpBlock.where('ip <<= ?', address) - end - end + ip_blocks = if options[:force] + IpBlock.where('ip >>= ?', address) + else + IpBlock.where('ip <<= ?', address) + end if ip_blocks.empty? say("#{address} is not yet blocked", :yellow) diff --git a/lib/mastodon/maintenance_cli.rb b/lib/mastodon/maintenance_cli.rb index a86a4f2f6..bb3802f56 100644 --- a/lib/mastodon/maintenance_cli.rb +++ b/lib/mastodon/maintenance_cli.rb @@ -98,11 +98,9 @@ module Mastodon owned_classes.each do |klass| klass.where(account_id: other_account.id).find_each do |record| - begin - record.update_attribute(:account_id, id) - rescue ActiveRecord::RecordNotUnique - next - end + record.update_attribute(:account_id, id) + rescue ActiveRecord::RecordNotUnique + next end end @@ -111,11 +109,9 @@ module Mastodon target_classes.each do |klass| klass.where(target_account_id: other_account.id).find_each do |record| - begin - record.update_attribute(:target_account_id, id) - rescue ActiveRecord::RecordNotUnique - next - end + record.update_attribute(:target_account_id, id) + rescue ActiveRecord::RecordNotUnique + next end end @@ -601,11 +597,9 @@ module Mastodon owned_classes = [ConversationMute, AccountConversation] owned_classes.each do |klass| klass.where(conversation_id: duplicate_conv.id).find_each do |record| - begin - record.update_attribute(:account_id, main_conv.id) - rescue ActiveRecord::RecordNotUnique - next - end + record.update_attribute(:account_id, main_conv.id) + rescue ActiveRecord::RecordNotUnique + next end end end @@ -629,47 +623,37 @@ module Mastodon owned_classes << Bookmark if ActiveRecord::Base.connection.table_exists?(:bookmarks) owned_classes.each do |klass| klass.where(status_id: duplicate_status.id).find_each do |record| - begin - record.update_attribute(:status_id, main_status.id) - rescue ActiveRecord::RecordNotUnique - next - end - end - end - - StatusPin.where(account_id: main_status.account_id, status_id: duplicate_status.id).find_each do |record| - begin record.update_attribute(:status_id, main_status.id) rescue ActiveRecord::RecordNotUnique next end end + StatusPin.where(account_id: main_status.account_id, status_id: duplicate_status.id).find_each do |record| + record.update_attribute(:status_id, main_status.id) + rescue ActiveRecord::RecordNotUnique + next + end + Status.where(in_reply_to_id: duplicate_status.id).find_each do |record| - begin - record.update_attribute(:in_reply_to_id, main_status.id) - rescue ActiveRecord::RecordNotUnique - next - end + record.update_attribute(:in_reply_to_id, main_status.id) + rescue ActiveRecord::RecordNotUnique + next end Status.where(reblog_of_id: duplicate_status.id).find_each do |record| - begin - record.update_attribute(:reblog_of_id, main_status.id) - rescue ActiveRecord::RecordNotUnique - next - end + record.update_attribute(:reblog_of_id, main_status.id) + rescue ActiveRecord::RecordNotUnique + next end end def merge_tags!(main_tag, duplicate_tag) [FeaturedTag].each do |klass| klass.where(tag_id: duplicate_tag.id).find_each do |record| - begin - record.update_attribute(:tag_id, main_tag.id) - rescue ActiveRecord::RecordNotUnique - next - end + record.update_attribute(:tag_id, main_tag.id) + rescue ActiveRecord::RecordNotUnique + next end end end diff --git a/lib/mastodon/media_cli.rb b/lib/mastodon/media_cli.rb index a901a6ab9..fc70c8785 100644 --- a/lib/mastodon/media_cli.rb +++ b/lib/mastodon/media_cli.rb @@ -116,13 +116,11 @@ module Mastodon loop do objects = begin - begin - bucket.objects(start_after: last_key, prefix: prefix).limit(1000).map { |x| x } - rescue => e - progress.log(pastel.red("Error fetching list of files: #{e}")) - progress.log("If you want to continue from this point, add --start-after=#{last_key} to your command") if last_key - break - end + bucket.objects(start_after: last_key, prefix: prefix).limit(1000).map { |x| x } + rescue => e + progress.log(pastel.red("Error fetching list of files: #{e}")) + progress.log("If you want to continue from this point, add --start-after=#{last_key} to your command") if last_key + break end break if objects.empty? diff --git a/lib/mastodon/search_cli.rb b/lib/mastodon/search_cli.rb index b206854ab..31e9a3d5a 100644 --- a/lib/mastodon/search_cli.rb +++ b/lib/mastodon/search_cli.rb @@ -43,13 +43,11 @@ module Mastodon exit(1) end - indices = begin - if options[:only] - options[:only].map { |str| "#{str.camelize}Index".constantize } - else - INDICES - end - end + indices = if options[:only] + options[:only].map { |str| "#{str.camelize}Index".constantize } + else + INDICES + end pool = Concurrent::FixedThreadPool.new(options[:concurrency], max_queue: options[:concurrency] * 10) importers = indices.index_with { |index| "Importer::#{index.name}Importer".constantize.new(batch_size: options[:batch_size], executor: pool) } diff --git a/lib/mastodon/upgrade_cli.rb b/lib/mastodon/upgrade_cli.rb index 570b7e6fa..2b60f9eee 100644 --- a/lib/mastodon/upgrade_cli.rb +++ b/lib/mastodon/upgrade_cli.rb @@ -50,16 +50,14 @@ module Mastodon styles << :original unless styles.include?(:original) styles.each do |style| - success = begin - case Paperclip::Attachment.default_options[:storage] - when :s3 - upgrade_storage_s3(progress, attachment, style) - when :fog - upgrade_storage_fog(progress, attachment, style) - when :filesystem - upgrade_storage_filesystem(progress, attachment, style) - end - end + success = case Paperclip::Attachment.default_options[:storage] + when :s3 + upgrade_storage_s3(progress, attachment, style) + when :fog + upgrade_storage_fog(progress, attachment, style) + when :filesystem + upgrade_storage_filesystem(progress, attachment, style) + end upgraded = true if style == :original && success diff --git a/lib/paperclip/color_extractor.rb b/lib/paperclip/color_extractor.rb index d8a042c90..733dcba80 100644 --- a/lib/paperclip/color_extractor.rb +++ b/lib/paperclip/color_extractor.rb @@ -161,13 +161,11 @@ module Paperclip def lighten_or_darken(color, by) hue, saturation, light = rgb_to_hsl(color.r, color.g, color.b) - light = begin - if light < 50 - [100, light + by].min - else - [0, light - by].max - end - end + light = if light < 50 + [100, light + by].min + else + [0, light - by].max + end ColorDiff::Color::RGB.new(*hsl_to_rgb(hue, saturation, light)) end diff --git a/lib/sanitize_ext/sanitize_config.rb b/lib/sanitize_ext/sanitize_config.rb index baf652662..d5e62897f 100644 --- a/lib/sanitize_ext/sanitize_config.rb +++ b/lib/sanitize_ext/sanitize_config.rb @@ -41,13 +41,11 @@ class Sanitize current_node = env[:node] - scheme = begin - if current_node['href'] =~ Sanitize::REGEX_PROTOCOL - Regexp.last_match(1).downcase - else - :relative - end - end + scheme = if current_node['href'] =~ Sanitize::REGEX_PROTOCOL + Regexp.last_match(1).downcase + else + :relative + end current_node.replace(Nokogiri::XML::Text.new(current_node.text, current_node.document)) unless LINK_PROTOCOLS.include?(scheme) end diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index ca939fd1f..e8a64b8fb 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -4,16 +4,14 @@ namespace :db do namespace :migrate do desc 'Setup the db or migrate depending on state of db' task setup: :environment do - begin - if ActiveRecord::Migrator.current_version.zero? - Rake::Task['db:migrate'].invoke - Rake::Task['db:seed'].invoke - end - rescue ActiveRecord::NoDatabaseError - Rake::Task['db:setup'].invoke - else + if ActiveRecord::Migrator.current_version.zero? Rake::Task['db:migrate'].invoke + Rake::Task['db:seed'].invoke end + rescue ActiveRecord::NoDatabaseError + Rake::Task['db:setup'].invoke + else + Rake::Task['db:migrate'].invoke end end -- cgit From 597767a9f7ca846be9fbca826aba362f8387a60f Mon Sep 17 00:00:00 2001 From: Nick Schonning Date: Sun, 19 Feb 2023 20:19:40 -0500 Subject: Autofix Rubocop Rails/RedundantForeignKey (#23731) --- .rubocop_todo.yml | 12 ------------ app/models/custom_filter.rb | 4 ++-- app/models/follow_recommendation.rb | 2 +- app/models/report.rb | 2 +- app/models/status.rb | 2 +- app/models/user_ip.rb | 2 +- db/post_migrate/20221101190723_backfill_admin_action_logs.rb | 2 +- .../20221206114142_backfill_admin_action_logs_again.rb | 2 +- 8 files changed, 8 insertions(+), 20 deletions(-) (limited to 'app/models/status.rb') diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6fb471600..1a524e6fd 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -2459,18 +2459,6 @@ Rails/RakeEnvironment: - 'lib/tasks/repo.rake' - 'lib/tasks/statistics.rake' -# Offense count: 8 -# This cop supports safe autocorrection (--autocorrect). -Rails/RedundantForeignKey: - Exclude: - - 'app/models/custom_filter.rb' - - 'app/models/follow_recommendation.rb' - - 'app/models/report.rb' - - 'app/models/status.rb' - - 'app/models/user_ip.rb' - - 'db/post_migrate/20221101190723_backfill_admin_action_logs.rb' - - 'db/post_migrate/20221206114142_backfill_admin_action_logs_again.rb' - # Offense count: 29 # Configuration parameters: Include. # Include: db/**/*.rb diff --git a/app/models/custom_filter.rb b/app/models/custom_filter.rb index 5a4a974be..b70e53bd5 100644 --- a/app/models/custom_filter.rb +++ b/app/models/custom_filter.rb @@ -33,8 +33,8 @@ class CustomFilter < ApplicationRecord enum action: [:warn, :hide], _suffix: :action belongs_to :account - has_many :keywords, class_name: 'CustomFilterKeyword', foreign_key: :custom_filter_id, inverse_of: :custom_filter, dependent: :destroy - has_many :statuses, class_name: 'CustomFilterStatus', foreign_key: :custom_filter_id, inverse_of: :custom_filter, dependent: :destroy + has_many :keywords, class_name: 'CustomFilterKeyword', inverse_of: :custom_filter, dependent: :destroy + has_many :statuses, class_name: 'CustomFilterStatus', inverse_of: :custom_filter, dependent: :destroy accepts_nested_attributes_for :keywords, reject_if: :all_blank, allow_destroy: true validates :title, :context, presence: true diff --git a/app/models/follow_recommendation.rb b/app/models/follow_recommendation.rb index e552b5a88..501f8ecb6 100644 --- a/app/models/follow_recommendation.rb +++ b/app/models/follow_recommendation.rb @@ -12,7 +12,7 @@ class FollowRecommendation < ApplicationRecord self.primary_key = :account_id belongs_to :account_summary, foreign_key: :account_id - belongs_to :account, foreign_key: :account_id + belongs_to :account scope :localized, ->(locale) { joins(:account_summary).merge(AccountSummary.localized(locale)) } diff --git a/app/models/report.rb b/app/models/report.rb index 525d22ad5..fe6c292c5 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -32,7 +32,7 @@ class Report < ApplicationRecord belongs_to :action_taken_by_account, class_name: 'Account', optional: true belongs_to :assigned_account, class_name: 'Account', optional: true - has_many :notes, class_name: 'ReportNote', foreign_key: :report_id, inverse_of: :report, dependent: :destroy + has_many :notes, class_name: 'ReportNote', inverse_of: :report, dependent: :destroy has_many :notifications, as: :activity, dependent: :destroy scope :unresolved, -> { where(action_taken_at: nil) } diff --git a/app/models/status.rb b/app/models/status.rb index 102dfa994..e0ad29103 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -53,7 +53,7 @@ class Status < ApplicationRecord belongs_to :application, class_name: 'Doorkeeper::Application', optional: true belongs_to :account, inverse_of: :statuses - belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account', optional: true + belongs_to :in_reply_to_account, class_name: 'Account', optional: true belongs_to :conversation, optional: true belongs_to :preloadable_poll, class_name: 'Poll', foreign_key: 'poll_id', optional: true diff --git a/app/models/user_ip.rb b/app/models/user_ip.rb index a8e802e13..1da615762 100644 --- a/app/models/user_ip.rb +++ b/app/models/user_ip.rb @@ -11,7 +11,7 @@ class UserIp < ApplicationRecord self.primary_key = :user_id - belongs_to :user, foreign_key: :user_id + belongs_to :user def readonly? true diff --git a/db/post_migrate/20221101190723_backfill_admin_action_logs.rb b/db/post_migrate/20221101190723_backfill_admin_action_logs.rb index 48ef1e6e3..6ab76a8f7 100644 --- a/db/post_migrate/20221101190723_backfill_admin_action_logs.rb +++ b/db/post_migrate/20221101190723_backfill_admin_action_logs.rb @@ -62,7 +62,7 @@ class BackfillAdminActionLogs < ActiveRecord::Migration[6.1] # Cannot use usual polymorphic support because of namespacing issues belongs_to :status, foreign_key: :target_id belongs_to :account, foreign_key: :target_id - belongs_to :user, foreign_key: :user_id + belongs_to :user belongs_to :domain_block, foreign_key: :target_id belongs_to :domain_allow, foreign_key: :target_id belongs_to :email_domain_block, foreign_key: :target_id diff --git a/db/post_migrate/20221206114142_backfill_admin_action_logs_again.rb b/db/post_migrate/20221206114142_backfill_admin_action_logs_again.rb index 279053ab9..42b7f3625 100644 --- a/db/post_migrate/20221206114142_backfill_admin_action_logs_again.rb +++ b/db/post_migrate/20221206114142_backfill_admin_action_logs_again.rb @@ -62,7 +62,7 @@ class BackfillAdminActionLogsAgain < ActiveRecord::Migration[6.1] # Cannot use usual polymorphic support because of namespacing issues belongs_to :status, foreign_key: :target_id belongs_to :account, foreign_key: :target_id - belongs_to :user, foreign_key: :user_id + belongs_to :user belongs_to :domain_block, foreign_key: :target_id belongs_to :domain_allow, foreign_key: :target_id belongs_to :email_domain_block, foreign_key: :target_id -- cgit From 63e63538861bd850a38a282e37de046639afa993 Mon Sep 17 00:00:00 2001 From: Nick Schonning Date: Sun, 19 Feb 2023 23:00:36 -0500 Subject: Autofix Rubocop Rails/EnumHash (#23737) --- .rubocop_todo.yml | 16 ---------------- app/models/account.rb | 4 ++-- app/models/custom_filter.rb | 2 +- app/models/domain_block.rb | 2 +- app/models/import.rb | 2 +- app/models/list.rb | 2 +- app/models/media_attachment.rb | 4 ++-- app/models/preview_card.rb | 4 ++-- app/models/relay.rb | 2 +- app/models/status.rb | 2 +- 10 files changed, 12 insertions(+), 28 deletions(-) (limited to 'app/models/status.rb') diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index c452d1bd2..7cb4d55e0 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1871,22 +1871,6 @@ Rails/DuplicateAssociation: - 'app/serializers/activitypub/collection_serializer.rb' - 'app/serializers/activitypub/note_serializer.rb' -# Offense count: 12 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: Include. -# Include: app/models/**/*.rb -Rails/EnumHash: - Exclude: - - 'app/models/account.rb' - - 'app/models/custom_filter.rb' - - 'app/models/domain_block.rb' - - 'app/models/import.rb' - - 'app/models/list.rb' - - 'app/models/media_attachment.rb' - - 'app/models/preview_card.rb' - - 'app/models/relay.rb' - - 'app/models/status.rb' - # Offense count: 76 # Configuration parameters: EnforcedStyle. # SupportedStyles: slashes, arguments diff --git a/app/models/account.rb b/app/models/account.rb index 2c0cd577e..d33110d55 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -78,8 +78,8 @@ class Account < ApplicationRecord include DomainMaterializable include AccountMerging - enum protocol: [:ostatus, :activitypub] - enum suspension_origin: [:local, :remote], _prefix: true + enum protocol: { ostatus: 0, activitypub: 1 } + enum suspension_origin: { local: 0, remote: 1 }, _prefix: true validates :username, presence: true validates_with UniqueUsernameValidator, if: -> { will_save_change_to_username? } diff --git a/app/models/custom_filter.rb b/app/models/custom_filter.rb index b70e53bd5..781bf4db8 100644 --- a/app/models/custom_filter.rb +++ b/app/models/custom_filter.rb @@ -30,7 +30,7 @@ class CustomFilter < ApplicationRecord include Expireable include Redisable - enum action: [:warn, :hide], _suffix: :action + enum action: { warn: 0, hide: 1 }, _suffix: :action belongs_to :account has_many :keywords, class_name: 'CustomFilterKeyword', inverse_of: :custom_filter, dependent: :destroy diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb index 8e298ac9d..190f5ba2e 100644 --- a/app/models/domain_block.rb +++ b/app/models/domain_block.rb @@ -20,7 +20,7 @@ class DomainBlock < ApplicationRecord include DomainNormalizable include DomainMaterializable - enum severity: [:silence, :suspend, :noop] + enum severity: { silence: 0, suspend: 1, noop: 2 } validates :domain, presence: true, uniqueness: true, domain: true diff --git a/app/models/import.rb b/app/models/import.rb index 00a54892e..cd33eb07b 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -24,7 +24,7 @@ class Import < ApplicationRecord belongs_to :account - enum type: [:following, :blocking, :muting, :domain_blocking, :bookmarks] + enum type: { following: 0, blocking: 1, muting: 2, domain_blocking: 3, bookmarks: 4 } validates :type, presence: true validates_with ImportValidator, on: :create diff --git a/app/models/list.rb b/app/models/list.rb index cdc6ebdb3..7b8cf6636 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -16,7 +16,7 @@ class List < ApplicationRecord PER_ACCOUNT_LIMIT = 50 - enum replies_policy: [:list, :followed, :none], _prefix: :show + enum replies_policy: { list: 0, followed: 1, none: 2 }, _prefix: :show belongs_to :account, optional: true diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index 5916b0b4b..c6f2352e0 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -33,8 +33,8 @@ class MediaAttachment < ApplicationRecord include Attachmentable - enum type: [:image, :gifv, :video, :unknown, :audio] - enum processing: [:queued, :in_progress, :complete, :failed], _prefix: true + enum type: { :image => 0, :gifv => 1, :video => 2, :unknown => 3, :audio => 4 } + enum processing: { :queued => 0, :in_progress => 1, :complete => 2, :failed => 3 }, _prefix: true MAX_DESCRIPTION_LENGTH = 1_500 diff --git a/app/models/preview_card.rb b/app/models/preview_card.rb index 56ca62d5e..d25fe6dad 100644 --- a/app/models/preview_card.rb +++ b/app/models/preview_card.rb @@ -44,8 +44,8 @@ class PreviewCard < ApplicationRecord self.inheritance_column = false - enum type: [:link, :photo, :video, :rich] - enum link_type: [:unknown, :article] + enum type: { link: 0, photo: 1, video: 2, rich: 3 } + enum link_type: { unknown: 0, article: 1 } has_and_belongs_to_many :statuses has_one :trend, class_name: 'PreviewCardTrend', inverse_of: :preview_card, dependent: :destroy diff --git a/app/models/relay.rb b/app/models/relay.rb index c66bfe4ff..e9c425743 100644 --- a/app/models/relay.rb +++ b/app/models/relay.rb @@ -14,7 +14,7 @@ class Relay < ApplicationRecord validates :inbox_url, presence: true, uniqueness: true, url: true, if: :will_save_change_to_inbox_url? - enum state: [:idle, :pending, :accepted, :rejected] + enum state: { idle: 0, pending: 1, accepted: 2, rejected: 3 } scope :enabled, -> { accepted } diff --git a/app/models/status.rb b/app/models/status.rb index e0ad29103..44a297a08 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -48,7 +48,7 @@ class Status < ApplicationRecord update_index('statuses', :proper) - enum visibility: [:public, :unlisted, :private, :direct, :limited], _suffix: :visibility + enum visibility: { public: 0, unlisted: 1, private: 2, direct: 3, limited: 4 }, _suffix: :visibility belongs_to :application, class_name: 'Doorkeeper::Application', optional: true -- cgit From 717683d1c39d2fe85d1cc3f5223e1f4cf43f1900 Mon Sep 17 00:00:00 2001 From: Nick Schonning Date: Mon, 20 Feb 2023 00:58:28 -0500 Subject: Autofix Rubocop remaining Layout rules (#23679) --- .rubocop_todo.yml | 148 --------------------- Capfile | 1 + app/controllers/api/v1/tags_controller.rb | 1 + app/controllers/application_controller.rb | 1 + .../concerns/session_tracking_concern.rb | 1 + app/controllers/concerns/signature_verification.rb | 1 + app/helpers/application_helper.rb | 1 + app/helpers/languages_helper.rb | 1 + app/lib/activitypub/activity.rb | 1 + app/lib/activitypub/linked_data_signature.rb | 2 +- app/lib/activitypub/tag_manager.rb | 2 + app/lib/ostatus/tag_manager.rb | 32 ++--- app/lib/request.rb | 1 + app/lib/settings/scoped_settings.rb | 2 + app/lib/status_filter.rb | 1 + app/lib/tag_manager.rb | 1 + app/lib/webfinger.rb | 1 + app/models/account.rb | 2 + app/models/account/field.rb | 4 +- app/models/account_conversation.rb | 2 + app/models/account_domain_block.rb | 1 + app/models/account_moderation_note.rb | 1 + app/models/account_note.rb | 1 + app/models/account_pin.rb | 1 + app/models/account_stat.rb | 1 + app/models/account_summary.rb | 1 + app/models/account_warning.rb | 13 +- app/models/admin/import.rb | 1 + app/models/backup.rb | 1 + app/models/block.rb | 1 + app/models/bookmark.rb | 1 + app/models/canonical_email_block.rb | 1 + app/models/conversation.rb | 1 + app/models/conversation_mute.rb | 1 + app/models/custom_emoji.rb | 1 + app/models/custom_filter.rb | 3 + app/models/custom_filter_keyword.rb | 1 + app/models/custom_filter_status.rb | 1 + app/models/device.rb | 1 + app/models/domain_block.rb | 1 + app/models/email_domain_block.rb | 1 + app/models/encrypted_message.rb | 1 + app/models/favourite.rb | 2 + app/models/featured_tag.rb | 1 + app/models/follow.rb | 1 + app/models/follow_recommendation.rb | 1 + app/models/follow_recommendation_suppression.rb | 1 + app/models/follow_request.rb | 1 + app/models/form/admin_settings.rb | 1 + app/models/identity.rb | 1 + app/models/import.rb | 1 + app/models/instance.rb | 1 + app/models/invite.rb | 1 + app/models/ip_block.rb | 1 + app/models/list.rb | 1 + app/models/list_account.rb | 1 + app/models/login_activity.rb | 1 + app/models/media_attachment.rb | 3 +- app/models/mention.rb | 1 + app/models/mute.rb | 1 + app/models/notification.rb | 11 +- app/models/one_time_key.rb | 1 + app/models/poll.rb | 8 +- app/models/poll_vote.rb | 1 + app/models/preview_card.rb | 1 + app/models/preview_card_provider.rb | 1 + app/models/relay.rb | 1 + app/models/report.rb | 1 + app/models/report_note.rb | 1 + app/models/session_activation.rb | 2 + app/models/setting.rb | 4 + app/models/site_upload.rb | 1 + app/models/status.rb | 1 + app/models/status_edit.rb | 2 + app/models/status_pin.rb | 1 + app/models/status_stat.rb | 1 + app/models/tag.rb | 1 + app/models/unavailable_domain.rb | 1 + app/models/user.rb | 4 + app/models/user_ip.rb | 1 + app/models/user_role.rb | 2 + app/models/web/push_subscription.rb | 1 + app/models/web/setting.rb | 1 + app/models/webauthn_credential.rb | 1 + app/presenters/account_relationships_presenter.rb | 18 +-- .../activitypub/fetch_remote_actor_service.rb | 1 + .../activitypub/fetch_remote_status_service.rb | 1 + app/services/activitypub/fetch_replies_service.rb | 1 + .../activitypub/process_account_service.rb | 2 + app/services/favourite_service.rb | 1 + app/services/keys/claim_service.rb | 6 +- app/services/keys/query_service.rb | 6 +- app/services/notify_service.rb | 1 + app/services/post_status_service.rb | 3 + app/services/vote_service.rb | 2 + app/validators/follow_limit_validator.rb | 1 + app/validators/unreserved_username_validator.rb | 2 + .../accounts_statuses_cleanup_scheduler.rb | 2 + app/workers/web/push_notification_worker.rb | 12 +- config.ru | 1 + ...314181829_migrate_open_registrations_setting.rb | 2 + ...43559_preserve_old_layout_for_existing_users.rb | 1 + ...431_add_case_insensitive_btree_index_to_tags.rb | 1 + .../20220613110834_add_action_to_custom_filters.rb | 1 + .../20200917193528_migrate_notifications_type.rb | 10 +- ...110802_remove_whole_word_from_custom_filters.rb | 1 + ...0903_remove_irreversible_from_custom_filters.rb | 1 + .../20221101190723_backfill_admin_action_logs.rb | 13 ++ ...21206114142_backfill_admin_action_logs_again.rb | 13 ++ lib/mastodon/domains_cli.rb | 2 + lib/sanitize_ext/sanitize_config.rb | 12 +- lib/tasks/auto_annotate_models.rake | 70 +++++----- lib/tasks/mastodon.rake | 16 +-- .../api/v1/accounts/statuses_controller_spec.rb | 1 + spec/models/admin/account_action_spec.rb | 4 +- spec/models/concerns/account_interactions_spec.rb | 4 +- spec/models/tag_spec.rb | 1 + 117 files changed, 265 insertions(+), 261 deletions(-) (limited to 'app/models/status.rb') diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0f98d25d6..e667a7786 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -14,154 +14,6 @@ Bundler/OrderedGems: Exclude: - 'Gemfile' -# Offense count: 81 -# This cop supports safe autocorrection (--autocorrect). -Layout/EmptyLineAfterGuardClause: - Exclude: - - 'app/controllers/api/v1/tags_controller.rb' - - 'app/controllers/application_controller.rb' - - 'app/controllers/concerns/session_tracking_concern.rb' - - 'app/controllers/concerns/signature_verification.rb' - - 'app/helpers/application_helper.rb' - - 'app/lib/activitypub/activity.rb' - - 'app/lib/activitypub/tag_manager.rb' - - 'app/lib/request.rb' - - 'app/lib/settings/scoped_settings.rb' - - 'app/lib/status_filter.rb' - - 'app/lib/tag_manager.rb' - - 'app/lib/webfinger.rb' - - 'app/models/account.rb' - - 'app/models/account_conversation.rb' - - 'app/models/admin/import.rb' - - 'app/models/custom_filter.rb' - - 'app/models/favourite.rb' - - 'app/models/form/admin_settings.rb' - - 'app/models/poll.rb' - - 'app/models/session_activation.rb' - - 'app/models/setting.rb' - - 'app/models/status_edit.rb' - - 'app/models/user.rb' - - 'app/models/user_role.rb' - - 'app/services/activitypub/fetch_remote_actor_service.rb' - - 'app/services/activitypub/fetch_remote_status_service.rb' - - 'app/services/activitypub/fetch_replies_service.rb' - - 'app/services/activitypub/process_account_service.rb' - - 'app/services/favourite_service.rb' - - 'app/services/notify_service.rb' - - 'app/services/post_status_service.rb' - - 'app/services/vote_service.rb' - - 'app/validators/follow_limit_validator.rb' - - 'app/validators/unreserved_username_validator.rb' - - 'app/workers/scheduler/accounts_statuses_cleanup_scheduler.rb' - - 'db/migrate/20190314181829_migrate_open_registrations_setting.rb' - - 'db/migrate/20190529143559_preserve_old_layout_for_existing_users.rb' - - 'db/migrate/20210421121431_add_case_insensitive_btree_index_to_tags.rb' - - 'db/post_migrate/20221101190723_backfill_admin_action_logs.rb' - - 'db/post_migrate/20221206114142_backfill_admin_action_logs_again.rb' - - 'lib/mastodon/domains_cli.rb' - -# Offense count: 71 -# This cop supports safe autocorrection (--autocorrect). -Layout/EmptyLineAfterMagicComment: - Exclude: - - 'Capfile' - - 'app/helpers/languages_helper.rb' - - 'app/models/account.rb' - - 'app/models/account_conversation.rb' - - 'app/models/account_domain_block.rb' - - 'app/models/account_moderation_note.rb' - - 'app/models/account_note.rb' - - 'app/models/account_pin.rb' - - 'app/models/account_stat.rb' - - 'app/models/account_summary.rb' - - 'app/models/account_warning.rb' - - 'app/models/backup.rb' - - 'app/models/block.rb' - - 'app/models/bookmark.rb' - - 'app/models/canonical_email_block.rb' - - 'app/models/conversation.rb' - - 'app/models/conversation_mute.rb' - - 'app/models/custom_emoji.rb' - - 'app/models/custom_filter.rb' - - 'app/models/custom_filter_keyword.rb' - - 'app/models/custom_filter_status.rb' - - 'app/models/device.rb' - - 'app/models/domain_block.rb' - - 'app/models/email_domain_block.rb' - - 'app/models/encrypted_message.rb' - - 'app/models/favourite.rb' - - 'app/models/featured_tag.rb' - - 'app/models/follow.rb' - - 'app/models/follow_recommendation.rb' - - 'app/models/follow_recommendation_suppression.rb' - - 'app/models/follow_request.rb' - - 'app/models/identity.rb' - - 'app/models/import.rb' - - 'app/models/instance.rb' - - 'app/models/invite.rb' - - 'app/models/ip_block.rb' - - 'app/models/list.rb' - - 'app/models/list_account.rb' - - 'app/models/login_activity.rb' - - 'app/models/media_attachment.rb' - - 'app/models/mention.rb' - - 'app/models/mute.rb' - - 'app/models/notification.rb' - - 'app/models/one_time_key.rb' - - 'app/models/poll.rb' - - 'app/models/poll_vote.rb' - - 'app/models/preview_card.rb' - - 'app/models/preview_card_provider.rb' - - 'app/models/relay.rb' - - 'app/models/report.rb' - - 'app/models/report_note.rb' - - 'app/models/session_activation.rb' - - 'app/models/setting.rb' - - 'app/models/site_upload.rb' - - 'app/models/status.rb' - - 'app/models/status_edit.rb' - - 'app/models/status_pin.rb' - - 'app/models/status_stat.rb' - - 'app/models/tag.rb' - - 'app/models/unavailable_domain.rb' - - 'app/models/user.rb' - - 'app/models/user_ip.rb' - - 'app/models/web/push_subscription.rb' - - 'app/models/web/setting.rb' - - 'app/models/webauthn_credential.rb' - - 'config.ru' - - 'db/migrate/20220613110834_add_action_to_custom_filters.rb' - - 'db/post_migrate/20220613110802_remove_whole_word_from_custom_filters.rb' - - 'db/post_migrate/20220613110903_remove_irreversible_from_custom_filters.rb' - - 'spec/controllers/api/v1/accounts/statuses_controller_spec.rb' - - 'spec/models/tag_spec.rb' - -# Offense count: 113 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle. -# SupportedHashRocketStyles: key, separator, table -# SupportedColonStyles: key, separator, table -# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit -Layout/HashAlignment: - Exclude: - - 'app/lib/activitypub/linked_data_signature.rb' - - 'app/lib/ostatus/tag_manager.rb' - - 'app/models/account/field.rb' - - 'app/models/account_warning.rb' - - 'app/models/media_attachment.rb' - - 'app/models/notification.rb' - - 'app/models/poll.rb' - - 'app/presenters/account_relationships_presenter.rb' - - 'app/services/keys/claim_service.rb' - - 'app/services/keys/query_service.rb' - - 'app/workers/web/push_notification_worker.rb' - - 'db/post_migrate/20200917193528_migrate_notifications_type.rb' - - 'lib/sanitize_ext/sanitize_config.rb' - - 'lib/tasks/auto_annotate_models.rake' - - 'lib/tasks/mastodon.rake' - - 'spec/models/admin/account_action_spec.rb' - - 'spec/models/concerns/account_interactions_spec.rb' - # Offense count: 581 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. diff --git a/Capfile b/Capfile index bf3ae7e24..86efa5bac 100644 --- a/Capfile +++ b/Capfile @@ -1,4 +1,5 @@ # frozen_string_literal: true + require 'capistrano/setup' require 'capistrano/deploy' require 'capistrano/scm/git' diff --git a/app/controllers/api/v1/tags_controller.rb b/app/controllers/api/v1/tags_controller.rb index 272362c31..a08fd2187 100644 --- a/app/controllers/api/v1/tags_controller.rb +++ b/app/controllers/api/v1/tags_controller.rb @@ -25,6 +25,7 @@ class Api::V1::TagsController < Api::BaseController def set_or_create_tag return not_found unless Tag::HASHTAG_NAME_RE.match?(params[:id]) + @tag = Tag.find_normalized(params[:id]) || Tag.new(name: Tag.normalize(params[:id]), display_name: params[:id]) end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 615536b96..ad70e28ab 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -128,6 +128,7 @@ class ApplicationController < ActionController::Base def current_theme return Setting.theme unless Themes.instance.names.include? current_user&.setting_theme + current_user.setting_theme end diff --git a/app/controllers/concerns/session_tracking_concern.rb b/app/controllers/concerns/session_tracking_concern.rb index eaaa4ac59..3f56c0d02 100644 --- a/app/controllers/concerns/session_tracking_concern.rb +++ b/app/controllers/concerns/session_tracking_concern.rb @@ -13,6 +13,7 @@ module SessionTrackingConcern def set_session_activity return unless session_needs_update? + current_session.touch end diff --git a/app/controllers/concerns/signature_verification.rb b/app/controllers/concerns/signature_verification.rb index 9c04ab4ca..b0a087d53 100644 --- a/app/controllers/concerns/signature_verification.rb +++ b/app/controllers/concerns/signature_verification.rb @@ -165,6 +165,7 @@ module SignatureVerification end raise SignatureVerificationError, "Invalid Digest value. The provided Digest value is not a SHA-256 digest. Given digest: #{sha256[1]}" if digest_size != 32 + raise SignatureVerificationError, "Invalid Digest value. Computed SHA-256 digest: #{body_digest}; given: #{sha256[1]}" end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 1f93b33f5..08020a65a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -105,6 +105,7 @@ module ApplicationHelper def can?(action, record) return false if record.nil? + policy(record).public_send("#{action}?") end diff --git a/app/helpers/languages_helper.rb b/app/helpers/languages_helper.rb index bb35ce08c..584394758 100644 --- a/app/helpers/languages_helper.rb +++ b/app/helpers/languages_helper.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # rubocop:disable Metrics/ModuleLength, Style/WordArray module LanguagesHelper diff --git a/app/lib/activitypub/activity.rb b/app/lib/activitypub/activity.rb index 900428e92..5d9596254 100644 --- a/app/lib/activitypub/activity.rb +++ b/app/lib/activitypub/activity.rb @@ -153,6 +153,7 @@ class ActivityPub::Activity def fetch_remote_original_status if object_uri.start_with?('http') return if ActivityPub::TagManager.instance.local_uri?(object_uri) + ActivityPub::FetchRemoteStatusService.new.call(object_uri, id: true, on_behalf_of: @account.followers.local.first, request_id: @options[:request_id]) elsif @object['url'].present? ::FetchRemoteStatusService.new.call(@object['url'], request_id: @options[:request_id]) diff --git a/app/lib/activitypub/linked_data_signature.rb b/app/lib/activitypub/linked_data_signature.rb index 61759649a..ea59879f3 100644 --- a/app/lib/activitypub/linked_data_signature.rb +++ b/app/lib/activitypub/linked_data_signature.rb @@ -32,7 +32,7 @@ class ActivityPub::LinkedDataSignature def sign!(creator, sign_with: nil) options = { - 'type' => 'RsaSignature2017', + 'type' => 'RsaSignature2017', 'creator' => ActivityPub::TagManager.instance.key_uri_for(creator), 'created' => Time.now.utc.iso8601, } diff --git a/app/lib/activitypub/tag_manager.rb b/app/lib/activitypub/tag_manager.rb index 3d6b28ef5..a65a9565a 100644 --- a/app/lib/activitypub/tag_manager.rb +++ b/app/lib/activitypub/tag_manager.rb @@ -26,6 +26,7 @@ class ActivityPub::TagManager target.instance_actor? ? about_more_url(instance_actor: true) : short_account_url(target) when :note, :comment, :activity return activity_account_status_url(target.account, target) if target.reblog? + short_account_status_url(target.account, target) end end @@ -38,6 +39,7 @@ class ActivityPub::TagManager target.instance_actor? ? instance_actor_url : account_url(target) when :note, :comment, :activity return activity_account_status_url(target.account, target) if target.reblog? + account_status_url(target.account, target) when :emoji emoji_url(target) diff --git a/app/lib/ostatus/tag_manager.rb b/app/lib/ostatus/tag_manager.rb index 4f4501312..7d8131622 100644 --- a/app/lib/ostatus/tag_manager.rb +++ b/app/lib/ostatus/tag_manager.rb @@ -5,27 +5,27 @@ class OStatus::TagManager include RoutingHelper VERBS = { - post: 'http://activitystrea.ms/schema/1.0/post', - share: 'http://activitystrea.ms/schema/1.0/share', - favorite: 'http://activitystrea.ms/schema/1.0/favorite', - unfavorite: 'http://activitystrea.ms/schema/1.0/unfavorite', - delete: 'http://activitystrea.ms/schema/1.0/delete', - follow: 'http://activitystrea.ms/schema/1.0/follow', + post: 'http://activitystrea.ms/schema/1.0/post', + share: 'http://activitystrea.ms/schema/1.0/share', + favorite: 'http://activitystrea.ms/schema/1.0/favorite', + unfavorite: 'http://activitystrea.ms/schema/1.0/unfavorite', + delete: 'http://activitystrea.ms/schema/1.0/delete', + follow: 'http://activitystrea.ms/schema/1.0/follow', request_friend: 'http://activitystrea.ms/schema/1.0/request-friend', - authorize: 'http://activitystrea.ms/schema/1.0/authorize', - reject: 'http://activitystrea.ms/schema/1.0/reject', - unfollow: 'http://ostatus.org/schema/1.0/unfollow', - block: 'http://mastodon.social/schema/1.0/block', - unblock: 'http://mastodon.social/schema/1.0/unblock', + authorize: 'http://activitystrea.ms/schema/1.0/authorize', + reject: 'http://activitystrea.ms/schema/1.0/reject', + unfollow: 'http://ostatus.org/schema/1.0/unfollow', + block: 'http://mastodon.social/schema/1.0/block', + unblock: 'http://mastodon.social/schema/1.0/unblock', }.freeze TYPES = { - activity: 'http://activitystrea.ms/schema/1.0/activity', - note: 'http://activitystrea.ms/schema/1.0/note', - comment: 'http://activitystrea.ms/schema/1.0/comment', - person: 'http://activitystrea.ms/schema/1.0/person', + activity: 'http://activitystrea.ms/schema/1.0/activity', + note: 'http://activitystrea.ms/schema/1.0/note', + comment: 'http://activitystrea.ms/schema/1.0/comment', + person: 'http://activitystrea.ms/schema/1.0/person', collection: 'http://activitystrea.ms/schema/1.0/collection', - group: 'http://activitystrea.ms/schema/1.0/group', + group: 'http://activitystrea.ms/schema/1.0/group', }.freeze COLLECTIONS = { diff --git a/app/lib/request.rb b/app/lib/request.rb index be6a69b3f..85716f999 100644 --- a/app/lib/request.rb +++ b/app/lib/request.rb @@ -182,6 +182,7 @@ class Request contents = truncated_body(limit) raise Mastodon::LengthValidationError if contents.bytesize > limit + contents end end diff --git a/app/lib/settings/scoped_settings.rb b/app/lib/settings/scoped_settings.rb index 1e18d6d46..3ad57cc1e 100644 --- a/app/lib/settings/scoped_settings.rb +++ b/app/lib/settings/scoped_settings.rb @@ -34,6 +34,7 @@ module Settings Setting.default_settings.each do |key, default_value| next if records.key?(key) || default_value.is_a?(Hash) + records[key] = Setting.new(var: key, value: default_value) end @@ -54,6 +55,7 @@ module Settings if db_val default_value = ScopedSettings.default_settings[key] return default_value.with_indifferent_access.merge!(db_val.value) if default_value.is_a?(Hash) + db_val.value else ScopedSettings.default_settings[key] diff --git a/app/lib/status_filter.rb b/app/lib/status_filter.rb index b6c80b801..c0e6f3331 100644 --- a/app/lib/status_filter.rb +++ b/app/lib/status_filter.rb @@ -11,6 +11,7 @@ class StatusFilter def filtered? return false if !account.nil? && account.id == status.account_id + blocked_by_policy? || (account_present? && filtered_status?) || silenced_account? end diff --git a/app/lib/tag_manager.rb b/app/lib/tag_manager.rb index a1d12a654..7fbf4437d 100644 --- a/app/lib/tag_manager.rb +++ b/app/lib/tag_manager.rb @@ -25,6 +25,7 @@ class TagManager def local_url?(url) uri = Addressable::URI.parse(url).normalize return false unless uri.host + domain = uri.host + (uri.port ? ":#{uri.port}" : '') TagManager.instance.web_domain?(domain) diff --git a/app/lib/webfinger.rb b/app/lib/webfinger.rb index 42ddef47b..ae8a3b1ea 100644 --- a/app/lib/webfinger.rb +++ b/app/lib/webfinger.rb @@ -57,6 +57,7 @@ class Webfinger if res.code == 200 body = res.body_with_limit raise Webfinger::Error, "Request for #{@uri} returned empty response" if body.empty? + body elsif res.code == 404 && use_fallback body_from_host_meta diff --git a/app/models/account.rb b/app/models/account.rb index d33110d55..09c450f2a 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: accounts @@ -539,6 +540,7 @@ class Account < ApplicationRecord def ensure_keys! return unless local? && private_key.blank? && public_key.blank? + generate_keys save! end diff --git a/app/models/account/field.rb b/app/models/account/field.rb index 98c29726d..2bada6954 100644 --- a/app/models/account/field.rb +++ b/app/models/account/field.rb @@ -14,8 +14,8 @@ class Account::Field < ActiveModelSerializers::Model @account = account super( - name: sanitize(attributes['name']), - value: sanitize(attributes['value']), + name: sanitize(attributes['name']), + value: sanitize(attributes['value']), verified_at: attributes['verified_at']&.to_datetime, ) end diff --git a/app/models/account_conversation.rb b/app/models/account_conversation.rb index 45e74bbeb..b3ddc04c1 100644 --- a/app/models/account_conversation.rb +++ b/app/models/account_conversation.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: account_conversations @@ -107,6 +108,7 @@ class AccountConversation < ApplicationRecord def push_to_streaming_api return if destroyed? || !subscribed_to_timeline? + PushConversationWorker.perform_async(id) end diff --git a/app/models/account_domain_block.rb b/app/models/account_domain_block.rb index 3aaffde9a..af1e6a68d 100644 --- a/app/models/account_domain_block.rb +++ b/app/models/account_domain_block.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: account_domain_blocks diff --git a/app/models/account_moderation_note.rb b/app/models/account_moderation_note.rb index 22e312bb2..ff399bab0 100644 --- a/app/models/account_moderation_note.rb +++ b/app/models/account_moderation_note.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: account_moderation_notes diff --git a/app/models/account_note.rb b/app/models/account_note.rb index b338bc92f..9bc704d98 100644 --- a/app/models/account_note.rb +++ b/app/models/account_note.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: account_notes diff --git a/app/models/account_pin.rb b/app/models/account_pin.rb index b51d3d4cd..6c78e8c44 100644 --- a/app/models/account_pin.rb +++ b/app/models/account_pin.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: account_pins diff --git a/app/models/account_stat.rb b/app/models/account_stat.rb index a5d71a5b8..834f8ba4c 100644 --- a/app/models/account_stat.rb +++ b/app/models/account_stat.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: account_stats diff --git a/app/models/account_summary.rb b/app/models/account_summary.rb index 3a3cebc55..0d8835b83 100644 --- a/app/models/account_summary.rb +++ b/app/models/account_summary.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: account_summaries diff --git a/app/models/account_warning.rb b/app/models/account_warning.rb index a181cd18d..4f8cc5320 100644 --- a/app/models/account_warning.rb +++ b/app/models/account_warning.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: account_warnings @@ -17,13 +18,13 @@ class AccountWarning < ApplicationRecord enum action: { - none: 0, - disable: 1_000, + none: 0, + disable: 1_000, mark_statuses_as_sensitive: 1_250, - delete_statuses: 1_500, - sensitive: 2_000, - silence: 3_000, - suspend: 4_000, + delete_statuses: 1_500, + sensitive: 2_000, + silence: 3_000, + suspend: 4_000, }, _suffix: :action before_validation :before_validate diff --git a/app/models/admin/import.rb b/app/models/admin/import.rb index fecde4878..0fd4bdb82 100644 --- a/app/models/admin/import.rb +++ b/app/models/admin/import.rb @@ -56,6 +56,7 @@ class Admin::Import def validate_data return if data.nil? + errors.add(:data, I18n.t('imports.errors.over_rows_processing_limit', count: ROWS_PROCESSING_LIMIT)) if csv_row_count > ROWS_PROCESSING_LIMIT rescue CSV::MalformedCSVError => e errors.add(:data, I18n.t('imports.errors.invalid_csv_file', error: e.message)) diff --git a/app/models/backup.rb b/app/models/backup.rb index 277b9395b..bec3cbfe5 100644 --- a/app/models/backup.rb +++ b/app/models/backup.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: backups diff --git a/app/models/block.rb b/app/models/block.rb index bf3e07600..b42c1569b 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: blocks diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb index 6334ef0df..04b660372 100644 --- a/app/models/bookmark.rb +++ b/app/models/bookmark.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: bookmarks diff --git a/app/models/canonical_email_block.rb b/app/models/canonical_email_block.rb index 1eb69ac67..d09df6f5e 100644 --- a/app/models/canonical_email_block.rb +++ b/app/models/canonical_email_block.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: canonical_email_blocks diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 4dfaea889..5de259962 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: conversations diff --git a/app/models/conversation_mute.rb b/app/models/conversation_mute.rb index 52c1a33e0..31f8e1966 100644 --- a/app/models/conversation_mute.rb +++ b/app/models/conversation_mute.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: conversation_mutes diff --git a/app/models/custom_emoji.rb b/app/models/custom_emoji.rb index 304805659..3d7900226 100644 --- a/app/models/custom_emoji.rb +++ b/app/models/custom_emoji.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: custom_emojis diff --git a/app/models/custom_filter.rb b/app/models/custom_filter.rb index 781bf4db8..d85e196e9 100644 --- a/app/models/custom_filter.rb +++ b/app/models/custom_filter.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: custom_filters @@ -101,6 +102,7 @@ class CustomFilter < ApplicationRecord status_matches = [status.id, status.reblog_of_id].compact & rules[:status_ids] if rules[:status_ids].present? next if keyword_matches.blank? && status_matches.blank? + FilterResultPresenter.new(filter: filter, keyword_matches: keyword_matches, status_matches: status_matches) end end @@ -111,6 +113,7 @@ class CustomFilter < ApplicationRecord def invalidate_cache! return unless @should_invalidate_cache + @should_invalidate_cache = false Rails.cache.delete("filters:v3:#{account_id}") diff --git a/app/models/custom_filter_keyword.rb b/app/models/custom_filter_keyword.rb index e0d0289ae..3158b3b79 100644 --- a/app/models/custom_filter_keyword.rb +++ b/app/models/custom_filter_keyword.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: custom_filter_keywords diff --git a/app/models/custom_filter_status.rb b/app/models/custom_filter_status.rb index e748d6963..0a5650204 100644 --- a/app/models/custom_filter_status.rb +++ b/app/models/custom_filter_status.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: custom_filter_statuses diff --git a/app/models/device.rb b/app/models/device.rb index 97d0d2774..5dc6cf1e6 100644 --- a/app/models/device.rb +++ b/app/models/device.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: devices diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb index 190f5ba2e..fbb045416 100644 --- a/app/models/domain_block.rb +++ b/app/models/domain_block.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: domain_blocks diff --git a/app/models/email_domain_block.rb b/app/models/email_domain_block.rb index 3a56e4f2a..276e7d31a 100644 --- a/app/models/email_domain_block.rb +++ b/app/models/email_domain_block.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: email_domain_blocks diff --git a/app/models/encrypted_message.rb b/app/models/encrypted_message.rb index 7b4e32283..3e7e95594 100644 --- a/app/models/encrypted_message.rb +++ b/app/models/encrypted_message.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: encrypted_messages diff --git a/app/models/favourite.rb b/app/models/favourite.rb index 2f355739a..042f72bea 100644 --- a/app/models/favourite.rb +++ b/app/models/favourite.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: favourites @@ -38,6 +39,7 @@ class Favourite < ApplicationRecord def decrement_cache_counters return if association(:status).loaded? && status.marked_for_destruction? + status&.decrement_count!(:favourites_count) end diff --git a/app/models/featured_tag.rb b/app/models/featured_tag.rb index 70f949b6a..587dcf991 100644 --- a/app/models/featured_tag.rb +++ b/app/models/featured_tag.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: featured_tags diff --git a/app/models/follow.rb b/app/models/follow.rb index e5cecbbc1..108f5c5d5 100644 --- a/app/models/follow.rb +++ b/app/models/follow.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: follows diff --git a/app/models/follow_recommendation.rb b/app/models/follow_recommendation.rb index 501f8ecb6..602d32985 100644 --- a/app/models/follow_recommendation.rb +++ b/app/models/follow_recommendation.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: follow_recommendations diff --git a/app/models/follow_recommendation_suppression.rb b/app/models/follow_recommendation_suppression.rb index 170506b85..a9dbbfc18 100644 --- a/app/models/follow_recommendation_suppression.rb +++ b/app/models/follow_recommendation_suppression.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: follow_recommendation_suppressions diff --git a/app/models/follow_request.rb b/app/models/follow_request.rb index 9034250c0..78f79c18f 100644 --- a/app/models/follow_request.rb +++ b/app/models/follow_request.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: follow_requests diff --git a/app/models/form/admin_settings.rb b/app/models/form/admin_settings.rb index 95c53084a..de965cb0b 100644 --- a/app/models/form/admin_settings.rb +++ b/app/models/form/admin_settings.rb @@ -128,6 +128,7 @@ class Form::AdminSettings def validate_site_uploads UPLOAD_KEYS.each do |key| next unless instance_variable_defined?("@#{key}") + upload = instance_variable_get("@#{key}") next if upload.valid? diff --git a/app/models/identity.rb b/app/models/identity.rb index 8cc65aef4..6f10fed4d 100644 --- a/app/models/identity.rb +++ b/app/models/identity.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: identities diff --git a/app/models/import.rb b/app/models/import.rb index cd33eb07b..21634005e 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: imports diff --git a/app/models/instance.rb b/app/models/instance.rb index edbf02a6d..1f96d3728 100644 --- a/app/models/instance.rb +++ b/app/models/instance.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: instances diff --git a/app/models/invite.rb b/app/models/invite.rb index 7ea4e2f98..8e816cef0 100644 --- a/app/models/invite.rb +++ b/app/models/invite.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: invites diff --git a/app/models/ip_block.rb b/app/models/ip_block.rb index 31343f0e1..99783050b 100644 --- a/app/models/ip_block.rb +++ b/app/models/ip_block.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: ip_blocks diff --git a/app/models/list.rb b/app/models/list.rb index 7b8cf6636..bd1bdbd24 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: lists diff --git a/app/models/list_account.rb b/app/models/list_account.rb index 785923c4c..a5767d3d8 100644 --- a/app/models/list_account.rb +++ b/app/models/list_account.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: list_accounts diff --git a/app/models/login_activity.rb b/app/models/login_activity.rb index 52a0fd01d..2b7b37f8e 100644 --- a/app/models/login_activity.rb +++ b/app/models/login_activity.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: login_activities diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index c6f2352e0..08abd4e43 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: media_attachments @@ -372,7 +373,7 @@ class MediaAttachment < ApplicationRecord return {} if width.nil? { - width: width, + width: width, height: height, size: "#{width}x#{height}", aspect: width.to_f / height, diff --git a/app/models/mention.rb b/app/models/mention.rb index d01a88e32..2348b2905 100644 --- a/app/models/mention.rb +++ b/app/models/mention.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: mentions diff --git a/app/models/mute.rb b/app/models/mute.rb index 578345ef6..8fc542262 100644 --- a/app/models/mute.rb +++ b/app/models/mute.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: mutes diff --git a/app/models/notification.rb b/app/models/notification.rb index 01155c363..3eaf557b0 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: notifications @@ -19,12 +20,12 @@ class Notification < ApplicationRecord include Paginable LEGACY_TYPE_CLASS_MAP = { - 'Mention' => :mention, - 'Status' => :reblog, - 'Follow' => :follow, + 'Mention' => :mention, + 'Status' => :reblog, + 'Follow' => :follow, 'FollowRequest' => :follow_request, - 'Favourite' => :favourite, - 'Poll' => :poll, + 'Favourite' => :favourite, + 'Poll' => :poll, }.freeze TYPES = %i( diff --git a/app/models/one_time_key.rb b/app/models/one_time_key.rb index 8ada34824..23604e2f7 100644 --- a/app/models/one_time_key.rb +++ b/app/models/one_time_key.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: one_time_keys diff --git a/app/models/poll.rb b/app/models/poll.rb index af3b09315..dd35e953b 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: polls @@ -74,9 +75,9 @@ class Poll < ApplicationRecord def initialize(poll, id, title, votes_count) super( - poll: poll, - id: id, - title: title, + poll: poll, + id: id, + title: title, votes_count: votes_count, ) end @@ -105,6 +106,7 @@ class Poll < ApplicationRecord def reset_parent_cache return if status_id.nil? + Rails.cache.delete("statuses/#{status_id}") end diff --git a/app/models/poll_vote.rb b/app/models/poll_vote.rb index ad24eb691..00eaedd12 100644 --- a/app/models/poll_vote.rb +++ b/app/models/poll_vote.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: poll_votes diff --git a/app/models/preview_card.rb b/app/models/preview_card.rb index d25fe6dad..6bce16562 100644 --- a/app/models/preview_card.rb +++ b/app/models/preview_card.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: preview_cards diff --git a/app/models/preview_card_provider.rb b/app/models/preview_card_provider.rb index d61fe6020..1dd95fc91 100644 --- a/app/models/preview_card_provider.rb +++ b/app/models/preview_card_provider.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: preview_card_providers diff --git a/app/models/relay.rb b/app/models/relay.rb index e9c425743..a5fa03a99 100644 --- a/app/models/relay.rb +++ b/app/models/relay.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: relays diff --git a/app/models/report.rb b/app/models/report.rb index fe6c292c5..a9940459d 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: reports diff --git a/app/models/report_note.rb b/app/models/report_note.rb index 6d7167e0e..74b46027e 100644 --- a/app/models/report_note.rb +++ b/app/models/report_note.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: report_notes diff --git a/app/models/session_activation.rb b/app/models/session_activation.rb index 0b7fa6fe4..10c3a6c25 100644 --- a/app/models/session_activation.rb +++ b/app/models/session_activation.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: session_activations @@ -51,6 +52,7 @@ class SessionActivation < ApplicationRecord def deactivate(id) return unless id + where(session_id: id).destroy_all end diff --git a/app/models/setting.rb b/app/models/setting.rb index c6558d692..3bdc6ffb4 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: settings @@ -30,6 +31,7 @@ class Setting < RailsSettings::Base default_value = default_settings[key] return default_value.with_indifferent_access.merge!(db_val.value) if default_value.is_a?(Hash) + db_val.value else default_settings[key] @@ -43,6 +45,7 @@ class Setting < RailsSettings::Base default_settings.each do |key, default_value| next if records.key?(key) || default_value.is_a?(Hash) + records[key] = Setting.new(var: key, value: default_value) end @@ -51,6 +54,7 @@ class Setting < RailsSettings::Base def default_settings return {} unless RailsSettings::Default.enabled? + RailsSettings::Default.instance end end diff --git a/app/models/site_upload.rb b/app/models/site_upload.rb index 167131fdd..e17668110 100644 --- a/app/models/site_upload.rb +++ b/app/models/site_upload.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: site_uploads diff --git a/app/models/status.rb b/app/models/status.rb index 44a297a08..2eb47d72c 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: statuses diff --git a/app/models/status_edit.rb b/app/models/status_edit.rb index dd2d5fc1e..683441bb5 100644 --- a/app/models/status_edit.rb +++ b/app/models/status_edit.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: status_edits @@ -45,6 +46,7 @@ class StatusEdit < ApplicationRecord def emojis return @emojis if defined?(@emojis) + @emojis = CustomEmoji.from_text([spoiler_text, text].join(' '), status.account.domain) end diff --git a/app/models/status_pin.rb b/app/models/status_pin.rb index 93a0ea1c0..dae4a5b4e 100644 --- a/app/models/status_pin.rb +++ b/app/models/status_pin.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: status_pins diff --git a/app/models/status_stat.rb b/app/models/status_stat.rb index 437861d1c..d101cc178 100644 --- a/app/models/status_stat.rb +++ b/app/models/status_stat.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: status_stats diff --git a/app/models/tag.rb b/app/models/tag.rb index 98001d60a..554a92d90 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: tags diff --git a/app/models/unavailable_domain.rb b/app/models/unavailable_domain.rb index dfc0ef14e..c3f2f20e9 100644 --- a/app/models/unavailable_domain.rb +++ b/app/models/unavailable_domain.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: unavailable_domains diff --git a/app/models/user.rb b/app/models/user.rb index c767f8984..5e106dee5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: users @@ -492,12 +493,14 @@ class User < ApplicationRecord def sanitize_languages return if chosen_languages.nil? + chosen_languages.reject!(&:blank?) self.chosen_languages = nil if chosen_languages.empty? end def sanitize_role return if role.nil? + self.role = nil if role.everyone? end @@ -516,6 +519,7 @@ class User < ApplicationRecord def notify_staff_about_pending_account! User.those_who_can(:manage_users).includes(:account).find_each do |u| next unless u.allows_pending_account_emails? + AdminMailer.new_pending_account(u.account, self).deliver_later end end diff --git a/app/models/user_ip.rb b/app/models/user_ip.rb index 1da615762..38287c2a6 100644 --- a/app/models/user_ip.rb +++ b/app/models/user_ip.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: user_ips diff --git a/app/models/user_role.rb b/app/models/user_role.rb index 74dfdc220..a1b91dc0f 100644 --- a/app/models/user_role.rb +++ b/app/models/user_role.rb @@ -163,6 +163,7 @@ class UserRole < ApplicationRecord def in_permissions?(privilege) raise ArgumentError, "Unknown privilege: #{privilege}" unless FLAGS.key?(privilege) + computed_permissions & FLAGS[privilege] == FLAGS[privilege] end @@ -172,6 +173,7 @@ class UserRole < ApplicationRecord def validate_own_role_edition return unless defined?(@current_account) && @current_account.user_role.id == id + errors.add(:permissions_as_keys, :own_role) if permissions_changed? errors.add(:position, :own_role) if position_changed? end diff --git a/app/models/web/push_subscription.rb b/app/models/web/push_subscription.rb index dfaadf5cc..0ffbe068e 100644 --- a/app/models/web/push_subscription.rb +++ b/app/models/web/push_subscription.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: web_push_subscriptions diff --git a/app/models/web/setting.rb b/app/models/web/setting.rb index 99588d26c..3d5efe664 100644 --- a/app/models/web/setting.rb +++ b/app/models/web/setting.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: web_settings diff --git a/app/models/webauthn_credential.rb b/app/models/webauthn_credential.rb index 48abfc1d4..4fa31ece5 100644 --- a/app/models/webauthn_credential.rb +++ b/app/models/webauthn_credential.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + # == Schema Information # # Table name: webauthn_credentials diff --git a/app/presenters/account_relationships_presenter.rb b/app/presenters/account_relationships_presenter.rb index ab8bac412..5d2b5435d 100644 --- a/app/presenters/account_relationships_presenter.rb +++ b/app/presenters/account_relationships_presenter.rb @@ -70,16 +70,16 @@ class AccountRelationshipsPresenter def cache_uncached! @uncached_account_ids.each do |account_id| maps_for_account = { - following: { account_id => following[account_id] }, - followed_by: { account_id => followed_by[account_id] }, - blocking: { account_id => blocking[account_id] }, - blocked_by: { account_id => blocked_by[account_id] }, - muting: { account_id => muting[account_id] }, - requested: { account_id => requested[account_id] }, - requested_by: { account_id => requested_by[account_id] }, + following: { account_id => following[account_id] }, + followed_by: { account_id => followed_by[account_id] }, + blocking: { account_id => blocking[account_id] }, + blocked_by: { account_id => blocked_by[account_id] }, + muting: { account_id => muting[account_id] }, + requested: { account_id => requested[account_id] }, + requested_by: { account_id => requested_by[account_id] }, domain_blocking: { account_id => domain_blocking[account_id] }, - endorsed: { account_id => endorsed[account_id] }, - account_note: { account_id => account_note[account_id] }, + endorsed: { account_id => endorsed[account_id] }, + account_note: { account_id => account_note[account_id] }, } Rails.cache.write("relationship:#{@current_account_id}:#{account_id}", maps_for_account, expires_in: 1.day) diff --git a/app/services/activitypub/fetch_remote_actor_service.rb b/app/services/activitypub/fetch_remote_actor_service.rb index e8992b845..ee0eaff08 100644 --- a/app/services/activitypub/fetch_remote_actor_service.rb +++ b/app/services/activitypub/fetch_remote_actor_service.rb @@ -50,6 +50,7 @@ class ActivityPub::FetchRemoteActorService < BaseService if @username.casecmp(confirmed_username).zero? && @domain.casecmp(confirmed_domain).zero? raise Error, "Webfinger response for #{@username}@#{@domain} does not loop back to #{@uri}" if webfinger.link('self', 'href') != @uri + return end diff --git a/app/services/activitypub/fetch_remote_status_service.rb b/app/services/activitypub/fetch_remote_status_service.rb index aea80f078..ab0acf7f0 100644 --- a/app/services/activitypub/fetch_remote_status_service.rb +++ b/app/services/activitypub/fetch_remote_status_service.rb @@ -56,6 +56,7 @@ class ActivityPub::FetchRemoteStatusService < BaseService def trustworthy_attribution?(uri, attributed_to) return false if uri.nil? || attributed_to.nil? + Addressable::URI.parse(uri).normalized_host.casecmp(Addressable::URI.parse(attributed_to).normalized_host).zero? end diff --git a/app/services/activitypub/fetch_replies_service.rb b/app/services/activitypub/fetch_replies_service.rb index 4128df9ca..3fe150ba2 100644 --- a/app/services/activitypub/fetch_replies_service.rb +++ b/app/services/activitypub/fetch_replies_service.rb @@ -36,6 +36,7 @@ class ActivityPub::FetchRepliesService < BaseService return collection_or_uri if collection_or_uri.is_a?(Hash) return unless @allow_synchronous_requests return if invalid_origin?(collection_or_uri) + fetch_resource_without_id_validation(collection_or_uri, nil, true) end diff --git a/app/services/activitypub/process_account_service.rb b/app/services/activitypub/process_account_service.rb index 2da9096c7..603e4cf48 100644 --- a/app/services/activitypub/process_account_service.rb +++ b/app/services/activitypub/process_account_service.rb @@ -226,6 +226,7 @@ class ActivityPub::ProcessAccountService < BaseService def property_values return unless @json['attachment'].is_a?(Array) + as_array(@json['attachment']).select { |attachment| attachment['type'] == 'PropertyValue' }.map { |attachment| attachment.slice('name', 'value') } end @@ -289,6 +290,7 @@ class ActivityPub::ProcessAccountService < BaseService def domain_block return @domain_block if defined?(@domain_block) + @domain_block = DomainBlock.rule_for(@domain) end diff --git a/app/services/favourite_service.rb b/app/services/favourite_service.rb index dc7fe8855..6fdc92a17 100644 --- a/app/services/favourite_service.rb +++ b/app/services/favourite_service.rb @@ -40,6 +40,7 @@ class FavouriteService < BaseService def bump_potential_friendship(account, status) ActivityTracker.increment('activity:interactions') return if account.following?(status.account_id) + PotentialFriendshipTracker.record(account.id, status.account_id, :favourite) end diff --git a/app/services/keys/claim_service.rb b/app/services/keys/claim_service.rb index 0451c3cb1..ebce9cce7 100644 --- a/app/services/keys/claim_service.rb +++ b/app/services/keys/claim_service.rb @@ -9,10 +9,10 @@ class Keys::ClaimService < BaseService def initialize(account, device_id, key_attributes = {}) super( - account: account, + account: account, device_id: device_id, - key_id: key_attributes[:key_id], - key: key_attributes[:key], + key_id: key_attributes[:key_id], + key: key_attributes[:key], signature: key_attributes[:signature], ) end diff --git a/app/services/keys/query_service.rb b/app/services/keys/query_service.rb index 404854c9f..14c9d9205 100644 --- a/app/services/keys/query_service.rb +++ b/app/services/keys/query_service.rb @@ -23,9 +23,9 @@ class Keys::QueryService < BaseService def initialize(attributes = {}) super( - device_id: attributes[:device_id], - name: attributes[:name], - identity_key: attributes[:identity_key], + device_id: attributes[:device_id], + name: attributes[:name], + identity_key: attributes[:identity_key], fingerprint_key: attributes[:fingerprint_key], ) @claim_url = attributes[:claim_url] diff --git a/app/services/notify_service.rb b/app/services/notify_service.rb index c7454fc60..4c7acbcac 100644 --- a/app/services/notify_service.rb +++ b/app/services/notify_service.rb @@ -31,6 +31,7 @@ class NotifyService < BaseService def following_sender? return @following_sender if defined?(@following_sender) + @following_sender = @recipient.following?(@notification.from_account) || @recipient.requested?(@notification.from_account) end diff --git a/app/services/post_status_service.rb b/app/services/post_status_service.rb index 258af8827..ea27f374e 100644 --- a/app/services/post_status_service.rb +++ b/app/services/post_status_service.rb @@ -86,6 +86,7 @@ class PostStatusService < BaseService def safeguard_mentions!(status) return if @options[:allowed_mentions].nil? + expected_account_ids = @options[:allowed_mentions].map(&:to_i) unexpected_accounts = status.mentions.map(&:account).to_a.reject { |mentioned_account| expected_account_ids.include?(mentioned_account.id) } @@ -175,8 +176,10 @@ class PostStatusService < BaseService def bump_potential_friendship! return if !@status.reply? || @account.id == @status.in_reply_to_account_id + ActivityTracker.increment('activity:interactions') return if @account.following?(@status.in_reply_to_account_id) + PotentialFriendshipTracker.record(@account.id, @status.in_reply_to_account_id, :reply) end diff --git a/app/services/vote_service.rb b/app/services/vote_service.rb index 114ec285c..9ebf5a98d 100644 --- a/app/services/vote_service.rb +++ b/app/services/vote_service.rb @@ -44,11 +44,13 @@ class VoteService < BaseService def distribute_poll! return if @poll.hide_totals? + ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, @poll.status.id) end def queue_final_poll_check! return unless @poll.expires? + PollExpirationNotifyWorker.perform_at(@poll.expires_at + 5.minutes, @poll.id) end diff --git a/app/validators/follow_limit_validator.rb b/app/validators/follow_limit_validator.rb index 409bf0176..c619cb9a3 100644 --- a/app/validators/follow_limit_validator.rb +++ b/app/validators/follow_limit_validator.rb @@ -6,6 +6,7 @@ class FollowLimitValidator < ActiveModel::Validator def validate(follow) return if follow.account.nil? || !follow.account.local? + follow.errors.add(:base, I18n.t('users.follow_limit_reached', limit: self.class.limit_for_account(follow.account))) if limit_reached?(follow.account) end diff --git a/app/validators/unreserved_username_validator.rb b/app/validators/unreserved_username_validator.rb index 974f3ba62..f82f4b91d 100644 --- a/app/validators/unreserved_username_validator.rb +++ b/app/validators/unreserved_username_validator.rb @@ -13,12 +13,14 @@ class UnreservedUsernameValidator < ActiveModel::Validator def pam_controlled? return false unless Devise.pam_authentication && Devise.pam_controlled_service + Rpam2.account(Devise.pam_controlled_service, @username).present? end def reserved_username? return true if pam_controlled? return false unless Setting.reserved_usernames + Setting.reserved_usernames.include?(@username.downcase) end end diff --git a/app/workers/scheduler/accounts_statuses_cleanup_scheduler.rb b/app/workers/scheduler/accounts_statuses_cleanup_scheduler.rb index cc5b6e137..09e0b37f0 100644 --- a/app/workers/scheduler/accounts_statuses_cleanup_scheduler.rb +++ b/app/workers/scheduler/accounts_statuses_cleanup_scheduler.rb @@ -62,6 +62,7 @@ class Scheduler::AccountsStatusesCleanupScheduler # The idea here is to loop through all policies at least once until the budget is exhausted # and start back after the last processed account otherwise break if budget.zero? || (num_processed_accounts.zero? && first_policy_id.nil?) + first_policy_id = nil end end @@ -73,6 +74,7 @@ class Scheduler::AccountsStatusesCleanupScheduler def under_load? return true if Sidekiq::Stats.new.retry_size > MAX_RETRY_SIZE + queue_under_load?('default', MAX_DEFAULT_SIZE, MAX_DEFAULT_LATENCY) || queue_under_load?('push', MAX_PUSH_SIZE, MAX_PUSH_LATENCY) || queue_under_load?('pull', MAX_PULL_SIZE, MAX_PULL_LATENCY) end diff --git a/app/workers/web/push_notification_worker.rb b/app/workers/web/push_notification_worker.rb index 1ed5bb9e0..7e9691aab 100644 --- a/app/workers/web/push_notification_worker.rb +++ b/app/workers/web/push_notification_worker.rb @@ -22,13 +22,13 @@ class Web::PushNotificationWorker request = Request.new(:post, @subscription.endpoint, body: payload.fetch(:ciphertext), http_client: http_client) request.add_headers( - 'Content-Type' => 'application/octet-stream', - 'Ttl' => TTL, - 'Urgency' => URGENCY, + 'Content-Type' => 'application/octet-stream', + 'Ttl' => TTL, + 'Urgency' => URGENCY, 'Content-Encoding' => 'aesgcm', - 'Encryption' => "salt=#{Webpush.encode64(payload.fetch(:salt)).delete('=')}", - 'Crypto-Key' => "dh=#{Webpush.encode64(payload.fetch(:server_public_key)).delete('=')};#{@subscription.crypto_key_header}", - 'Authorization' => @subscription.authorization_header + 'Encryption' => "salt=#{Webpush.encode64(payload.fetch(:salt)).delete('=')}", + 'Crypto-Key' => "dh=#{Webpush.encode64(payload.fetch(:server_public_key)).delete('=')};#{@subscription.crypto_key_header}", + 'Authorization' => @subscription.authorization_header ) request.perform do |response| diff --git a/config.ru b/config.ru index 5e071f530..afd13e211 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,5 @@ # frozen_string_literal: true + # This file is used by Rack-based servers to start the application. require File.expand_path('config/environment', __dir__) diff --git a/db/migrate/20190314181829_migrate_open_registrations_setting.rb b/db/migrate/20190314181829_migrate_open_registrations_setting.rb index e5fe95009..d2f6bf2c1 100644 --- a/db/migrate/20190314181829_migrate_open_registrations_setting.rb +++ b/db/migrate/20190314181829_migrate_open_registrations_setting.rb @@ -2,6 +2,7 @@ class MigrateOpenRegistrationsSetting < ActiveRecord::Migration[5.2] def up open_registrations = Setting.find_by(var: 'open_registrations') return if open_registrations.nil? || open_registrations.value + setting = Setting.where(var: 'registrations_mode').first_or_initialize(var: 'registrations_mode') setting.update(value: 'none') end @@ -9,6 +10,7 @@ class MigrateOpenRegistrationsSetting < ActiveRecord::Migration[5.2] def down registrations_mode = Setting.find_by(var: 'registrations_mode') return if registrations_mode.nil? + setting = Setting.where(var: 'open_registrations').first_or_initialize(var: 'open_registrations') setting.update(value: registrations_mode.value == 'open') end diff --git a/db/migrate/20190529143559_preserve_old_layout_for_existing_users.rb b/db/migrate/20190529143559_preserve_old_layout_for_existing_users.rb index 19e86fbfe..1c18b85cb 100644 --- a/db/migrate/20190529143559_preserve_old_layout_for_existing_users.rb +++ b/db/migrate/20190529143559_preserve_old_layout_for_existing_users.rb @@ -8,6 +8,7 @@ class PreserveOldLayoutForExistingUsers < ActiveRecord::Migration[5.2] User.where(User.arel_table[:current_sign_in_at].gteq(1.month.ago)).find_each do |user| next if Setting.unscoped.where(thing_type: 'User', thing_id: user.id, var: 'advanced_layout').exists? + user.settings.advanced_layout = true end end diff --git a/db/migrate/20210421121431_add_case_insensitive_btree_index_to_tags.rb b/db/migrate/20210421121431_add_case_insensitive_btree_index_to_tags.rb index 7f6a2c6dd..a3cc854d7 100644 --- a/db/migrate/20210421121431_add_case_insensitive_btree_index_to_tags.rb +++ b/db/migrate/20210421121431_add_case_insensitive_btree_index_to_tags.rb @@ -11,6 +11,7 @@ class AddCaseInsensitiveBtreeIndexToTags < ActiveRecord::Migration[5.2] rescue ActiveRecord::StatementInvalid => e remove_index :tags, name: 'index_tags_on_name_lower_btree' raise CorruptionError, 'index_tags_on_name_lower_btree' if e.is_a?(ActiveRecord::RecordNotUnique) + raise e end diff --git a/db/migrate/20220613110834_add_action_to_custom_filters.rb b/db/migrate/20220613110834_add_action_to_custom_filters.rb index 9427a66fc..c1daf3c94 100644 --- a/db/migrate/20220613110834_add_action_to_custom_filters.rb +++ b/db/migrate/20220613110834_add_action_to_custom_filters.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require Rails.root.join('lib', 'mastodon', 'migration_helpers') class AddActionToCustomFilters < ActiveRecord::Migration[6.1] diff --git a/db/post_migrate/20200917193528_migrate_notifications_type.rb b/db/post_migrate/20200917193528_migrate_notifications_type.rb index 88e423084..9dc9ecd48 100644 --- a/db/post_migrate/20200917193528_migrate_notifications_type.rb +++ b/db/post_migrate/20200917193528_migrate_notifications_type.rb @@ -4,12 +4,12 @@ class MigrateNotificationsType < ActiveRecord::Migration[5.2] disable_ddl_transaction! TYPES_TO_MIGRATE = { - 'Mention' => :mention, - 'Status' => :reblog, - 'Follow' => :follow, + 'Mention' => :mention, + 'Status' => :reblog, + 'Follow' => :follow, 'FollowRequest' => :follow_request, - 'Favourite' => :favourite, - 'Poll' => :poll, + 'Favourite' => :favourite, + 'Poll' => :poll, }.freeze def up diff --git a/db/post_migrate/20220613110802_remove_whole_word_from_custom_filters.rb b/db/post_migrate/20220613110802_remove_whole_word_from_custom_filters.rb index 7ef0749e5..99c3366a2 100644 --- a/db/post_migrate/20220613110802_remove_whole_word_from_custom_filters.rb +++ b/db/post_migrate/20220613110802_remove_whole_word_from_custom_filters.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require Rails.root.join('lib', 'mastodon', 'migration_helpers') class RemoveWholeWordFromCustomFilters < ActiveRecord::Migration[6.1] diff --git a/db/post_migrate/20220613110903_remove_irreversible_from_custom_filters.rb b/db/post_migrate/20220613110903_remove_irreversible_from_custom_filters.rb index 6ed8bcfee..1c366ee53 100644 --- a/db/post_migrate/20220613110903_remove_irreversible_from_custom_filters.rb +++ b/db/post_migrate/20220613110903_remove_irreversible_from_custom_filters.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require Rails.root.join('lib', 'mastodon', 'migration_helpers') class RemoveIrreversibleFromCustomFilters < ActiveRecord::Migration[6.1] diff --git a/db/post_migrate/20221101190723_backfill_admin_action_logs.rb b/db/post_migrate/20221101190723_backfill_admin_action_logs.rb index 6ab76a8f7..fa2ddbbca 100644 --- a/db/post_migrate/20221101190723_backfill_admin_action_logs.rb +++ b/db/post_migrate/20221101190723_backfill_admin_action_logs.rb @@ -79,11 +79,13 @@ class BackfillAdminActionLogs < ActiveRecord::Migration[6.1] safety_assured do AdminActionLog.includes(:account).where(target_type: 'Account', human_identifier: nil).find_each do |log| next if log.account.nil? + log.update_attribute('human_identifier', log.account.acct) end AdminActionLog.includes(user: :account).where(target_type: 'User', human_identifier: nil).find_each do |log| next if log.user.nil? + log.update_attribute('human_identifier', log.user.account.acct) log.update_attribute('route_param', log.user.account_id) end @@ -92,57 +94,68 @@ class BackfillAdminActionLogs < ActiveRecord::Migration[6.1] AdminActionLog.includes(:domain_block).where(target_type: 'DomainBlock').find_each do |log| next if log.domain_block.nil? + log.update_attribute('human_identifier', log.domain_block.domain) end AdminActionLog.includes(:domain_allow).where(target_type: 'DomainAllow').find_each do |log| next if log.domain_allow.nil? + log.update_attribute('human_identifier', log.domain_allow.domain) end AdminActionLog.includes(:email_domain_block).where(target_type: 'EmailDomainBlock').find_each do |log| next if log.email_domain_block.nil? + log.update_attribute('human_identifier', log.email_domain_block.domain) end AdminActionLog.includes(:unavailable_domain).where(target_type: 'UnavailableDomain').find_each do |log| next if log.unavailable_domain.nil? + log.update_attribute('human_identifier', log.unavailable_domain.domain) end AdminActionLog.includes(status: :account).where(target_type: 'Status', human_identifier: nil).find_each do |log| next if log.status.nil? + log.update_attribute('human_identifier', log.status.account.acct) log.update_attribute('permalink', log.status.uri) end AdminActionLog.includes(account_warning: :account).where(target_type: 'AccountWarning', human_identifier: nil).find_each do |log| next if log.account_warning.nil? + log.update_attribute('human_identifier', log.account_warning.account.acct) end AdminActionLog.includes(:announcement).where(target_type: 'Announcement', human_identifier: nil).find_each do |log| next if log.announcement.nil? + log.update_attribute('human_identifier', log.announcement.text) end AdminActionLog.includes(:ip_block).where(target_type: 'IpBlock', human_identifier: nil).find_each do |log| next if log.ip_block.nil? + log.update_attribute('human_identifier', "#{log.ip_block.ip}/#{log.ip_block.ip.prefix}") end AdminActionLog.includes(:custom_emoji).where(target_type: 'CustomEmoji', human_identifier: nil).find_each do |log| next if log.custom_emoji.nil? + log.update_attribute('human_identifier', log.custom_emoji.shortcode) end AdminActionLog.includes(:canonical_email_block).where(target_type: 'CanonicalEmailBlock', human_identifier: nil).find_each do |log| next if log.canonical_email_block.nil? + log.update_attribute('human_identifier', log.canonical_email_block.canonical_email_hash) end AdminActionLog.includes(appeal: :account).where(target_type: 'Appeal', human_identifier: nil).find_each do |log| next if log.appeal.nil? + log.update_attribute('human_identifier', log.appeal.account.acct) log.update_attribute('route_param', log.appeal.account_warning_id) end diff --git a/db/post_migrate/20221206114142_backfill_admin_action_logs_again.rb b/db/post_migrate/20221206114142_backfill_admin_action_logs_again.rb index 42b7f3625..9c7ac7120 100644 --- a/db/post_migrate/20221206114142_backfill_admin_action_logs_again.rb +++ b/db/post_migrate/20221206114142_backfill_admin_action_logs_again.rb @@ -79,11 +79,13 @@ class BackfillAdminActionLogsAgain < ActiveRecord::Migration[6.1] safety_assured do AdminActionLog.includes(:account).where(target_type: 'Account', human_identifier: nil).find_each do |log| next if log.account.nil? + log.update_attribute('human_identifier', log.account.acct) end AdminActionLog.includes(user: :account).where(target_type: 'User', human_identifier: nil).find_each do |log| next if log.user.nil? + log.update_attribute('human_identifier', log.user.account.acct) log.update_attribute('route_param', log.user.account_id) end @@ -92,57 +94,68 @@ class BackfillAdminActionLogsAgain < ActiveRecord::Migration[6.1] AdminActionLog.includes(:domain_block).where(target_type: 'DomainBlock').find_each do |log| next if log.domain_block.nil? + log.update_attribute('human_identifier', log.domain_block.domain) end AdminActionLog.includes(:domain_allow).where(target_type: 'DomainAllow').find_each do |log| next if log.domain_allow.nil? + log.update_attribute('human_identifier', log.domain_allow.domain) end AdminActionLog.includes(:email_domain_block).where(target_type: 'EmailDomainBlock').find_each do |log| next if log.email_domain_block.nil? + log.update_attribute('human_identifier', log.email_domain_block.domain) end AdminActionLog.includes(:unavailable_domain).where(target_type: 'UnavailableDomain').find_each do |log| next if log.unavailable_domain.nil? + log.update_attribute('human_identifier', log.unavailable_domain.domain) end AdminActionLog.includes(status: :account).where(target_type: 'Status', human_identifier: nil).find_each do |log| next if log.status.nil? + log.update_attribute('human_identifier', log.status.account.acct) log.update_attribute('permalink', log.status.uri) end AdminActionLog.includes(account_warning: :account).where(target_type: 'AccountWarning', human_identifier: nil).find_each do |log| next if log.account_warning.nil? + log.update_attribute('human_identifier', log.account_warning.account.acct) end AdminActionLog.includes(:announcement).where(target_type: 'Announcement', human_identifier: nil).find_each do |log| next if log.announcement.nil? + log.update_attribute('human_identifier', log.announcement.text) end AdminActionLog.includes(:ip_block).where(target_type: 'IpBlock', human_identifier: nil).find_each do |log| next if log.ip_block.nil? + log.update_attribute('human_identifier', "#{log.ip_block.ip}/#{log.ip_block.ip.prefix}") end AdminActionLog.includes(:custom_emoji).where(target_type: 'CustomEmoji', human_identifier: nil).find_each do |log| next if log.custom_emoji.nil? + log.update_attribute('human_identifier', log.custom_emoji.shortcode) end AdminActionLog.includes(:canonical_email_block).where(target_type: 'CanonicalEmailBlock', human_identifier: nil).find_each do |log| next if log.canonical_email_block.nil? + log.update_attribute('human_identifier', log.canonical_email_block.canonical_email_hash) end AdminActionLog.includes(appeal: :account).where(target_type: 'Appeal', human_identifier: nil).find_each do |log| next if log.appeal.nil? + log.update_attribute('human_identifier', log.appeal.account.acct) log.update_attribute('route_param', log.appeal.account_warning_id) end diff --git a/lib/mastodon/domains_cli.rb b/lib/mastodon/domains_cli.rb index f24a54e7e..41ea5b152 100644 --- a/lib/mastodon/domains_cli.rb +++ b/lib/mastodon/domains_cli.rb @@ -148,6 +148,7 @@ module Mastodon begin Request.new(:get, "https://#{domain}/api/v1/instance").perform do |res| next unless res.code == 200 + stats[domain] = Oj.load(res.to_s) end @@ -161,6 +162,7 @@ module Mastodon Request.new(:get, "https://#{domain}/api/v1/instance/activity").perform do |res| next unless res.code == 200 + stats[domain]['activity'] = Oj.load(res.to_s) end rescue StandardError diff --git a/lib/sanitize_ext/sanitize_config.rb b/lib/sanitize_ext/sanitize_config.rb index d5e62897f..dc39e9c90 100644 --- a/lib/sanitize_ext/sanitize_config.rb +++ b/lib/sanitize_ext/sanitize_config.rb @@ -72,7 +72,7 @@ class Sanitize elements: %w(p br span a), attributes: { - 'a' => %w(href rel class), + 'a' => %w(href rel class), 'span' => %w(class), }, @@ -98,17 +98,17 @@ class Sanitize attributes: merge( RELAXED[:attributes], - 'audio' => %w(controls), - 'embed' => %w(height src type width), + 'audio' => %w(controls), + 'embed' => %w(height src type width), 'iframe' => %w(allowfullscreen frameborder height scrolling src width), 'source' => %w(src type), - 'video' => %w(controls height loop width), - 'div' => [:data] + 'video' => %w(controls height loop width), + 'div' => [:data] ), protocols: merge( RELAXED[:protocols], - 'embed' => { 'src' => HTTP_PROTOCOLS }, + 'embed' => { 'src' => HTTP_PROTOCOLS }, 'iframe' => { 'src' => HTTP_PROTOCOLS }, 'source' => { 'src' => HTTP_PROTOCOLS } ) diff --git a/lib/tasks/auto_annotate_models.rake b/lib/tasks/auto_annotate_models.rake index a374e33ad..4b5997920 100644 --- a/lib/tasks/auto_annotate_models.rake +++ b/lib/tasks/auto_annotate_models.rake @@ -3,42 +3,42 @@ if Rails.env.development? task :set_annotation_options do Annotate.set_defaults( - 'routes' => 'false', - 'models' => 'true', - 'position_in_routes' => 'before', - 'position_in_class' => 'before', - 'position_in_test' => 'before', - 'position_in_fixture' => 'before', - 'position_in_factory' => 'before', - 'position_in_serializer' => 'before', - 'show_foreign_keys' => 'false', - 'show_indexes' => 'false', - 'simple_indexes' => 'false', - 'model_dir' => 'app/models', - 'root_dir' => '', - 'include_version' => 'false', - 'require' => '', - 'exclude_tests' => 'true', - 'exclude_fixtures' => 'true', - 'exclude_factories' => 'true', - 'exclude_serializers' => 'true', - 'exclude_scaffolds' => 'true', - 'exclude_controllers' => 'true', - 'exclude_helpers' => 'true', - 'ignore_model_sub_dir' => 'false', - 'ignore_columns' => nil, - 'ignore_routes' => nil, - 'ignore_unknown_models' => 'false', + 'routes' => 'false', + 'models' => 'true', + 'position_in_routes' => 'before', + 'position_in_class' => 'before', + 'position_in_test' => 'before', + 'position_in_fixture' => 'before', + 'position_in_factory' => 'before', + 'position_in_serializer' => 'before', + 'show_foreign_keys' => 'false', + 'show_indexes' => 'false', + 'simple_indexes' => 'false', + 'model_dir' => 'app/models', + 'root_dir' => '', + 'include_version' => 'false', + 'require' => '', + 'exclude_tests' => 'true', + 'exclude_fixtures' => 'true', + 'exclude_factories' => 'true', + 'exclude_serializers' => 'true', + 'exclude_scaffolds' => 'true', + 'exclude_controllers' => 'true', + 'exclude_helpers' => 'true', + 'ignore_model_sub_dir' => 'false', + 'ignore_columns' => nil, + 'ignore_routes' => nil, + 'ignore_unknown_models' => 'false', 'hide_limit_column_types' => 'integer,boolean', - 'skip_on_db_migrate' => 'false', - 'format_bare' => 'true', - 'format_rdoc' => 'false', - 'format_markdown' => 'false', - 'sort' => 'false', - 'force' => 'false', - 'trace' => 'false', - 'wrapper_open' => nil, - 'wrapper_close' => nil + 'skip_on_db_migrate' => 'false', + 'format_bare' => 'true', + 'format_rdoc' => 'false', + 'format_markdown' => 'false', + 'sort' => 'false', + 'force' => 'false', + 'trace' => 'false', + 'wrapper_open' => nil, + 'wrapper_close' => nil ) end diff --git a/lib/tasks/mastodon.rake b/lib/tasks/mastodon.rake index 179a730bc..f919ba989 100644 --- a/lib/tasks/mastodon.rake +++ b/lib/tasks/mastodon.rake @@ -399,14 +399,14 @@ namespace :mastodon do end ActionMailer::Base.smtp_settings = { - port: env['SMTP_PORT'], - address: env['SMTP_SERVER'], - user_name: env['SMTP_LOGIN'].presence, - password: env['SMTP_PASSWORD'].presence, - domain: env['LOCAL_DOMAIN'], - authentication: env['SMTP_AUTH_METHOD'] == 'none' ? nil : env['SMTP_AUTH_METHOD'] || :plain, - openssl_verify_mode: env['SMTP_OPENSSL_VERIFY_MODE'], - enable_starttls: enable_starttls, + port: env['SMTP_PORT'], + address: env['SMTP_SERVER'], + user_name: env['SMTP_LOGIN'].presence, + password: env['SMTP_PASSWORD'].presence, + domain: env['LOCAL_DOMAIN'], + authentication: env['SMTP_AUTH_METHOD'] == 'none' ? nil : env['SMTP_AUTH_METHOD'] || :plain, + openssl_verify_mode: env['SMTP_OPENSSL_VERIFY_MODE'], + enable_starttls: enable_starttls, enable_starttls_auto: enable_starttls_auto, } diff --git a/spec/controllers/api/v1/accounts/statuses_controller_spec.rb b/spec/controllers/api/v1/accounts/statuses_controller_spec.rb index 4630fac90..e57c37179 100644 --- a/spec/controllers/api/v1/accounts/statuses_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/statuses_controller_spec.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require 'rails_helper' describe Api::V1::Accounts::StatusesController do diff --git a/spec/models/admin/account_action_spec.rb b/spec/models/admin/account_action_spec.rb index 4516df2c2..7248356e5 100644 --- a/spec/models/admin/account_action_spec.rb +++ b/spec/models/admin/account_action_spec.rb @@ -12,9 +12,9 @@ RSpec.describe Admin::AccountAction, type: :model do before do account_action.assign_attributes( - type: type, + type: type, current_account: account, - target_account: target_account + target_account: target_account ) end diff --git a/spec/models/concerns/account_interactions_spec.rb b/spec/models/concerns/account_interactions_spec.rb index ed3fc056b..50ff0b149 100644 --- a/spec/models/concerns/account_interactions_spec.rb +++ b/spec/models/concerns/account_interactions_spec.rb @@ -149,8 +149,8 @@ describe AccountInteractions do let(:mute) do Fabricate(:mute, - account: account, - target_account: target_account, + account: account, + target_account: target_account, hide_notifications: hide_notifications) end diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 7043449c5..4d6e5c380 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require 'rails_helper' RSpec.describe Tag do -- cgit From 9909b4b653d2f1cef662211a4b8e0b02cbb42fc1 Mon Sep 17 00:00:00 2001 From: Nick Schonning Date: Mon, 20 Feb 2023 13:20:56 -0500 Subject: Autofix Rubocop Rails/WhereEquals (#23759) --- .rubocop_todo.yml | 7 ------- app/models/announcement.rb | 2 +- app/models/status.rb | 2 +- 3 files changed, 2 insertions(+), 9 deletions(-) (limited to 'app/models/status.rb') diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index bb214a70b..2a272b095 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1916,13 +1916,6 @@ Rails/UnusedIgnoredColumns: - 'app/models/status_edit.rb' - 'app/models/user.rb' -# Offense count: 2 -# This cop supports unsafe autocorrection (--autocorrect-all). -Rails/WhereEquals: - Exclude: - - 'app/models/announcement.rb' - - 'app/models/status.rb' - # Offense count: 61 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle. diff --git a/app/models/announcement.rb b/app/models/announcement.rb index 898bf3efa..339f5ae70 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -20,7 +20,7 @@ class Announcement < ApplicationRecord scope :unpublished, -> { where(published: false) } scope :published, -> { where(published: true) } - scope :without_muted, ->(account) { joins("LEFT OUTER JOIN announcement_mutes ON announcement_mutes.announcement_id = announcements.id AND announcement_mutes.account_id = #{account.id}").where('announcement_mutes.id IS NULL') } + scope :without_muted, ->(account) { joins("LEFT OUTER JOIN announcement_mutes ON announcement_mutes.announcement_id = announcements.id AND announcement_mutes.account_id = #{account.id}").where(announcement_mutes: { id: nil }) } scope :chronological, -> { order(Arel.sql('COALESCE(announcements.starts_at, announcements.scheduled_at, announcements.published_at, announcements.created_at) ASC')) } scope :reverse_chronological, -> { order(Arel.sql('COALESCE(announcements.starts_at, announcements.scheduled_at, announcements.published_at, announcements.created_at) DESC')) } diff --git a/app/models/status.rb b/app/models/status.rb index 2eb47d72c..e7ea191a8 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -95,7 +95,7 @@ class Status < ApplicationRecord scope :local, -> { where(local: true).or(where(uri: nil)) } scope :with_accounts, ->(ids) { where(id: ids).includes(:account) } scope :without_replies, -> { where('statuses.reply = FALSE OR statuses.in_reply_to_account_id = statuses.account_id') } - scope :without_reblogs, -> { where('statuses.reblog_of_id IS NULL') } + scope :without_reblogs, -> { where(statuses: { reblog_of_id: nil }) } scope :with_public_visibility, -> { where(visibility: :public) } scope :tagged_with, ->(tag_ids) { joins(:statuses_tags).where(statuses_tags: { tag_id: tag_ids }) } scope :excluding_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced_at: nil }) } -- cgit From 5a8c651e8f0252c7135042e79396f782361302d9 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Fri, 3 Mar 2023 21:06:31 +0100 Subject: Only offer translation for supported languages (#23879) --- .rubocop.yml | 4 + .../mastodon/components/status_content.jsx | 4 +- app/javascript/mastodon/initial_state.js | 2 - app/lib/translation_service.rb | 4 + app/lib/translation_service/deepl.rb | 46 +++++++--- app/lib/translation_service/libre_translate.rb | 38 +++++--- app/models/status.rb | 10 +++ app/serializers/initial_state_serializer.rb | 1 - app/serializers/rest/status_serializer.rb | 6 +- app/services/translate_status_service.rb | 2 +- spec/lib/translation_service/deepl_spec.rb | 100 +++++++++++++++++++++ .../translation_service/libre_translate_spec.rb | 71 +++++++++++++++ spec/models/status_spec.rb | 79 ++++++++++++++++ 13 files changed, 336 insertions(+), 31 deletions(-) create mode 100644 spec/lib/translation_service/deepl_spec.rb create mode 100644 spec/lib/translation_service/libre_translate_spec.rb (limited to 'app/models/status.rb') diff --git a/.rubocop.yml b/.rubocop.yml index 27d778edf..0a41c54b9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -97,6 +97,10 @@ Rails/Exit: - 'lib/mastodon/cli_helper.rb' - 'lib/cli.rb' +RSpec/FilePath: + CustomTransform: + DeepL: deepl + RSpec/NotToNot: EnforcedStyle: to_not diff --git a/app/javascript/mastodon/components/status_content.jsx b/app/javascript/mastodon/components/status_content.jsx index a1c38171f..f9c9fe079 100644 --- a/app/javascript/mastodon/components/status_content.jsx +++ b/app/javascript/mastodon/components/status_content.jsx @@ -6,7 +6,7 @@ import { Link } from 'react-router-dom'; import classnames from 'classnames'; import PollContainer from 'mastodon/containers/poll_container'; import Icon from 'mastodon/components/icon'; -import { autoPlayGif, languages as preloadedLanguages, translationEnabled } from 'mastodon/initial_state'; +import { autoPlayGif, languages as preloadedLanguages } from 'mastodon/initial_state'; const MAX_HEIGHT = 706; // 22px * 32 (+ 2px padding at the top) @@ -220,7 +220,7 @@ class StatusContent extends React.PureComponent { const hidden = this.props.onExpandedToggle ? !this.props.expanded : this.state.hidden; const renderReadMore = this.props.onClick && status.get('collapsed'); - const renderTranslate = translationEnabled && this.context.identity.signedIn && this.props.onTranslate && ['public', 'unlisted'].includes(status.get('visibility')) && status.get('contentHtml').length > 0 && status.get('language') !== null && intl.locale !== status.get('language'); + const renderTranslate = this.props.onTranslate && status.get('translatable'); const content = { __html: status.get('translation') ? status.getIn(['translation', 'content']) : status.get('contentHtml') }; const spoilerContent = { __html: status.get('spoilerHtml') }; diff --git a/app/javascript/mastodon/initial_state.js b/app/javascript/mastodon/initial_state.js index d04c4a42d..919e0fc28 100644 --- a/app/javascript/mastodon/initial_state.js +++ b/app/javascript/mastodon/initial_state.js @@ -80,7 +80,6 @@ * @property {boolean} use_blurhash * @property {boolean=} use_pending_items * @property {string} version - * @property {boolean} translation_enabled */ /** @@ -132,7 +131,6 @@ export const unfollowModal = getMeta('unfollow_modal'); export const useBlurhash = getMeta('use_blurhash'); export const usePendingItems = getMeta('use_pending_items'); export const version = getMeta('version'); -export const translationEnabled = getMeta('translation_enabled'); export const languages = initialState?.languages; export const statusPageUrl = getMeta('status_page_url'); diff --git a/app/lib/translation_service.rb b/app/lib/translation_service.rb index 285f30939..5ff93674a 100644 --- a/app/lib/translation_service.rb +++ b/app/lib/translation_service.rb @@ -21,6 +21,10 @@ class TranslationService ENV['DEEPL_API_KEY'].present? || ENV['LIBRE_TRANSLATE_ENDPOINT'].present? end + def supported?(_source_language, _target_language) + false + 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 151d33d90..deff95a1d 100644 --- a/app/lib/translation_service/deepl.rb +++ b/app/lib/translation_service/deepl.rb @@ -11,33 +11,53 @@ class TranslationService::DeepL < TranslationService end def translate(text, source_language, target_language) - request(text, source_language, target_language).perform do |res| + form = { text: text, source_lang: source_language&.upcase, target_lang: target_language, tag_handling: 'html' } + request(:post, '/v2/translate', form: form) do |res| + transform_response(res.body_with_limit) + end + end + + def supported?(source_language, target_language) + source_language.in?(languages('source')) && target_language.in?(languages('target')) + end + + private + + def languages(type) + Rails.cache.fetch("translation_service/deepl/languages/#{type}", expires_in: 7.days, race_condition_ttl: 1.minute) do + request(:get, "/v2/languages?type=#{type}") do |res| + # In DeepL, EN and PT are deprecated in favor of EN-GB/EN-US and PT-BR/PT-PT, so + # they are supported but not returned by the API. + extra = type == 'source' ? [nil] : %w(en pt) + languages = Oj.load(res.body_with_limit).map { |language| language['language'].downcase } + + languages + extra + end + end + end + + def request(verb, path, **options) + req = Request.new(verb, "#{base_url}#{path}", **options) + req.add_headers(Authorization: "DeepL-Auth-Key #{@api_key}") + req.perform do |res| case res.code when 429 raise TooManyRequestsError when 456 raise QuotaExceededError when 200...300 - transform_response(res.body_with_limit) + yield res else raise UnexpectedResponseError end end end - private - - def request(text, source_language, target_language) - req = Request.new(:post, endpoint_url, form: { text: text, source_lang: source_language&.upcase, target_lang: target_language, tag_handling: 'html' }) - req.add_headers(Authorization: "DeepL-Auth-Key #{@api_key}") - req - end - - def endpoint_url + def base_url if @plan == 'free' - 'https://api-free.deepl.com/v2/translate' + 'https://api-free.deepl.com' else - 'https://api.deepl.com/v2/translate' + 'https://api.deepl.com' end end diff --git a/app/lib/translation_service/libre_translate.rb b/app/lib/translation_service/libre_translate.rb index 4ebe21e45..743e4d77f 100644 --- a/app/lib/translation_service/libre_translate.rb +++ b/app/lib/translation_service/libre_translate.rb @@ -9,29 +9,45 @@ class TranslationService::LibreTranslate < TranslationService end def translate(text, source_language, target_language) - request(text, source_language, target_language).perform do |res| + body = Oj.dump(q: text, source: source_language.presence || 'auto', target: target_language, format: 'html', api_key: @api_key) + request(:post, '/translate', body: body) do |res| + transform_response(res.body_with_limit, source_language) + end + end + + def supported?(source_language, target_language) + languages.key?(source_language) && languages[source_language].include?(target_language) + end + + private + + def languages + Rails.cache.fetch('translation_service/libre_translate/languages', expires_in: 7.days, race_condition_ttl: 1.minute) do + request(:get, '/languages') do |res| + languages = Oj.load(res.body_with_limit).to_h { |language| [language['code'], language['targets']] } + languages[nil] = languages.values.flatten.uniq + languages + end + end + end + + def request(verb, path, **options) + req = Request.new(verb, "#{@base_url}#{path}", allow_local: true, **options) + req.add_headers('Content-Type': 'application/json') + req.perform do |res| case res.code when 429 raise TooManyRequestsError when 403 raise QuotaExceededError when 200...300 - transform_response(res.body_with_limit, source_language) + yield res else raise UnexpectedResponseError end end end - private - - def request(text, source_language, target_language) - body = Oj.dump(q: text, source: source_language.presence || 'auto', target: target_language, format: 'html', api_key: @api_key) - req = Request.new(:post, "#{@base_url}/translate", body: body, allow_local: true) - req.add_headers('Content-Type': 'application/json') - req - end - def transform_response(str, source_language) json = Oj.load(str, mode: :strict) diff --git a/app/models/status.rb b/app/models/status.rb index e7ea191a8..dd7ac2edb 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -232,6 +232,16 @@ class Status < ApplicationRecord public_visibility? || unlisted_visibility? end + def translatable? + translate_target_locale = I18n.locale.to_s.split(/[_-]/).first + + distributable? && + content.present? && + language != translate_target_locale && + TranslationService.configured? && + TranslationService.configured.supported?(language, translate_target_locale) + end + alias sign? distributable? def with_media? diff --git a/app/serializers/initial_state_serializer.rb b/app/serializers/initial_state_serializer.rb index 7905444e9..769ba653e 100644 --- a/app/serializers/initial_state_serializer.rb +++ b/app/serializers/initial_state_serializer.rb @@ -30,7 +30,6 @@ class InitialStateSerializer < ActiveModel::Serializer timeline_preview: Setting.timeline_preview, activity_api_enabled: Setting.activity_api_enabled, single_user_mode: Rails.configuration.x.single_user_mode, - translation_enabled: TranslationService.configured?, trends_as_landing_page: Setting.trends_as_landing_page, status_page_url: Setting.status_page_url, } diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb index e0b8f32a6..a422f5b25 100644 --- a/app/serializers/rest/status_serializer.rb +++ b/app/serializers/rest/status_serializer.rb @@ -4,7 +4,7 @@ class REST::StatusSerializer < ActiveModel::Serializer include FormattingHelper attributes :id, :created_at, :in_reply_to_id, :in_reply_to_account_id, - :sensitive, :spoiler_text, :visibility, :language, + :sensitive, :spoiler_text, :visibility, :language, :translatable, :uri, :url, :replies_count, :reblogs_count, :favourites_count, :edited_at @@ -50,6 +50,10 @@ class REST::StatusSerializer < ActiveModel::Serializer object.account.user_shows_application? || (current_user? && current_user.account_id == object.account_id) end + def translatable + current_user? && object.translatable? + end + def visibility # This visibility is masked behind "private" # to avoid API changes because there are no diff --git a/app/services/translate_status_service.rb b/app/services/translate_status_service.rb index 539a0d9db..92d8b62a0 100644 --- a/app/services/translate_status_service.rb +++ b/app/services/translate_status_service.rb @@ -6,7 +6,7 @@ class TranslateStatusService < BaseService include FormattingHelper def call(status, target_language) - raise Mastodon::NotPermittedError unless status.public_visibility? || status.unlisted_visibility? + raise Mastodon::NotPermittedError unless status.translatable? @status = status @content = status_content_format(@status) diff --git a/spec/lib/translation_service/deepl_spec.rb b/spec/lib/translation_service/deepl_spec.rb new file mode 100644 index 000000000..aa2473186 --- /dev/null +++ b/spec/lib/translation_service/deepl_spec.rb @@ -0,0 +1,100 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe TranslationService::DeepL do + subject(:service) { described_class.new(plan, 'my-api-key') } + + let(:plan) { 'advanced' } + + before do + stub_request(:get, 'https://api.deepl.com/v2/languages?type=source').to_return( + body: '[{"language":"EN","name":"English"},{"language":"UK","name":"Ukrainian"}]' + ) + stub_request(:get, 'https://api.deepl.com/v2/languages?type=target').to_return( + body: '[{"language":"EN-GB","name":"English (British)"},{"language":"ZH","name":"Chinese"}]' + ) + end + + describe '#supported?' do + it 'supports included languages as source and target languages' do + expect(service.supported?('uk', 'en')).to be true + end + + it 'supports auto-detecting source language' do + expect(service.supported?(nil, 'en')).to be true + end + + it 'supports "en" and "pt" as target languages though not included in language list' do + expect(service.supported?('uk', 'en')).to be true + expect(service.supported?('uk', 'pt')).to be true + end + + it 'does not support non-included language as target language' do + expect(service.supported?('uk', 'nl')).to be false + end + + it 'does not support non-included language as source language' do + expect(service.supported?('da', 'en')).to be false + end + end + + describe '#translate' do + it 'returns translation with specified source language' do + stub_request(:post, 'https://api.deepl.com/v2/translate') + .with(body: 'text=Hasta+la+vista&source_lang=ES&target_lang=en&tag_handling=html') + .to_return(body: '{"translations":[{"detected_source_language":"ES","text":"See you soon"}]}') + + translation = service.translate('Hasta la vista', 'es', 'en') + expect(translation.detected_source_language).to eq 'es' + expect(translation.provider).to eq 'DeepL.com' + expect(translation.text).to eq 'See you soon' + end + + it 'returns translation with auto-detected source language' do + stub_request(:post, 'https://api.deepl.com/v2/translate') + .with(body: 'text=Guten+Tag&source_lang&target_lang=en&tag_handling=html') + .to_return(body: '{"translations":[{"detected_source_language":"DE","text":"Good Morning"}]}') + + translation = service.translate('Guten Tag', nil, 'en') + expect(translation.detected_source_language).to eq 'de' + expect(translation.provider).to eq 'DeepL.com' + expect(translation.text).to eq 'Good Morning' + end + end + + describe '#languages?' do + it 'returns source languages' do + expect(service.send(:languages, 'source')).to eq ['en', 'uk', nil] + end + + it 'returns target languages' do + expect(service.send(:languages, 'target')).to eq %w(en-gb zh en pt) + end + end + + describe '#request' do + before do + stub_request(:any, //) + # rubocop:disable Lint/EmptyBlock + service.send(:request, :get, '/v2/languages') { |res| } + # rubocop:enable Lint/EmptyBlock + end + + it 'uses paid plan base URL' do + expect(a_request(:get, 'https://api.deepl.com/v2/languages')).to have_been_made.once + end + + context 'with free plan' do + let(:plan) { 'free' } + + it 'uses free plan base URL' do + expect(a_request(:get, 'https://api-free.deepl.com/v2/languages')).to have_been_made.once + end + end + + it 'sends API key' do + expect(a_request(:get, 'https://api.deepl.com/v2/languages').with(headers: { Authorization: 'DeepL-Auth-Key my-api-key' })).to have_been_made.once + end + end +end diff --git a/spec/lib/translation_service/libre_translate_spec.rb b/spec/lib/translation_service/libre_translate_spec.rb new file mode 100644 index 000000000..a6cb01884 --- /dev/null +++ b/spec/lib/translation_service/libre_translate_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe TranslationService::LibreTranslate do + subject(:service) { described_class.new('https://libretranslate.example.com', 'my-api-key') } + + before do + stub_request(:get, 'https://libretranslate.example.com/languages').to_return( + body: '[{"code": "en","name": "English","targets": ["de","es"]},{"code": "da","name": "Danish","targets": ["en","de"]}]' + ) + end + + describe '#supported?' do + it 'supports included language pair' do + expect(service.supported?('en', 'de')).to be true + end + + it 'does not support reversed language pair' do + expect(service.supported?('de', 'en')).to be false + end + + it 'supports auto-detecting source language' do + expect(service.supported?(nil, 'de')).to be true + end + + it 'does not support auto-detecting for unsupported target language' do + expect(service.supported?(nil, 'pt')).to be false + end + end + + describe '#languages' do + subject(:languages) { service.send(:languages) } + + it 'includes supported source languages' do + expect(languages.keys).to eq ['en', 'da', nil] + end + + it 'includes supported target languages for source language' do + expect(languages['en']).to eq %w(de es) + end + + it 'includes supported target languages for auto-detected language' do + expect(languages[nil]).to eq %w(de es en) + end + end + + describe '#translate' do + it 'returns translation with specified source language' do + stub_request(:post, 'https://libretranslate.example.com/translate') + .with(body: '{"q":"Hasta la vista","source":"es","target":"en","format":"html","api_key":"my-api-key"}') + .to_return(body: '{"translatedText": "See you"}') + + translation = service.translate('Hasta la vista', 'es', 'en') + expect(translation.detected_source_language).to eq 'es' + expect(translation.provider).to eq 'LibreTranslate' + expect(translation.text).to eq 'See you' + end + + it 'returns translation with auto-detected source language' do + stub_request(:post, 'https://libretranslate.example.com/translate') + .with(body: '{"q":"Guten Morgen","source":"auto","target":"en","format":"html","api_key":"my-api-key"}') + .to_return(body: '{"detectedLanguage":{"confidence":92,"language":"de"},"translatedText":"Good morning"}') + + translation = service.translate('Guten Morgen', nil, 'en') + expect(translation.detected_source_language).to be_nil + expect(translation.provider).to eq 'LibreTranslate' + expect(translation.text).to eq 'Good morning' + end + end +end diff --git a/spec/models/status_spec.rb b/spec/models/status_spec.rb index 1e58c6d0d..1f6cfc796 100644 --- a/spec/models/status_spec.rb +++ b/spec/models/status_spec.rb @@ -114,6 +114,85 @@ RSpec.describe Status, type: :model do end end + describe '#translatable?' do + before do + allow(TranslationService).to receive(:configured?).and_return(true) + allow(TranslationService).to receive(:configured).and_return(TranslationService.new) + allow(TranslationService.configured).to receive(:supported?).with('es', 'en').and_return(true) + + subject.language = 'es' + subject.visibility = :public + end + + context 'all conditions are satisfied' do + it 'returns true' do + expect(subject.translatable?).to be true + end + end + + context 'translation service is not configured' do + it 'returns false' do + allow(TranslationService).to receive(:configured?).and_return(false) + allow(TranslationService).to receive(:configured).and_raise(TranslationService::NotConfiguredError) + expect(subject.translatable?).to be false + end + end + + context 'status language is nil' do + it 'returns true' do + subject.language = nil + allow(TranslationService.configured).to receive(:supported?).with(nil, 'en').and_return(true) + expect(subject.translatable?).to be true + end + end + + context 'status language is same as default locale' do + it 'returns false' do + subject.language = I18n.locale + expect(subject.translatable?).to be false + end + end + + context 'status language is unsupported' do + it 'returns false' do + subject.language = 'af' + allow(TranslationService.configured).to receive(:supported?).with('af', 'en').and_return(false) + expect(subject.translatable?).to be false + end + end + + context 'default locale is unsupported' do + it 'returns false' do + allow(TranslationService.configured).to receive(:supported?).with('es', 'af').and_return(false) + I18n.with_locale('af') do + expect(subject.translatable?).to be false + end + end + end + + context 'default locale has region' do + it 'returns true' do + I18n.with_locale('en-GB') do + expect(subject.translatable?).to be true + end + end + end + + context 'status text is blank' do + it 'returns false' do + subject.text = ' ' + expect(subject.translatable?).to be false + end + end + + context 'status visiblity is hidden' do + it 'returns false' do + subject.visibility = 'limited' + expect(subject.translatable?).to be false + end + end + end + describe '#content' do it 'returns the text of the status if it is not a reblog' do expect(subject.content).to eql subject.text -- cgit From bd047acc356671727c112336bb237f979bba517d Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Thu, 16 Mar 2023 11:07:24 +0100 Subject: Replace `Status#translatable?` with language matrix in separate endpoint (#24037) --- .rubocop_todo.yml | 1 - .../instances/translation_languages_controller.rb | 23 +++++++ app/javascript/mastodon/actions/server.js | 27 ++++++++ .../mastodon/components/status_content.jsx | 13 +++- app/javascript/mastodon/features/ui/index.jsx | 3 +- app/javascript/mastodon/reducers/server.js | 9 +++ app/lib/translation_service.rb | 4 +- app/lib/translation_service/deepl.rb | 30 ++++---- app/lib/translation_service/libre_translate.rb | 18 ++--- app/models/status.rb | 10 --- app/serializers/rest/status_serializer.rb | 6 +- app/services/translate_status_service.rb | 16 ++++- config/routes.rb | 1 + .../translation_languages_controller_spec.rb | 31 +++++++++ .../v1/statuses/translations_controller_spec.rb | 3 +- spec/lib/translation_service/deepl_spec.rb | 36 +++------- .../translation_service/libre_translate_spec.rb | 31 ++------- spec/models/status_spec.rb | 79 ---------------------- spec/presenters/instance_presenter_spec.rb | 2 +- 19 files changed, 164 insertions(+), 179 deletions(-) create mode 100644 app/controllers/api/v1/instances/translation_languages_controller.rb create mode 100644 spec/controllers/api/v1/instances/translation_languages_controller_spec.rb (limited to 'app/models/status.rb') diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e79f4f8e9..85f078dcf 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -484,7 +484,6 @@ RSpec/DescribedClass: - 'spec/models/user_spec.rb' - 'spec/policies/account_moderation_note_policy_spec.rb' - 'spec/presenters/account_relationships_presenter_spec.rb' - - 'spec/presenters/instance_presenter_spec.rb' - 'spec/presenters/status_relationships_presenter_spec.rb' - 'spec/serializers/activitypub/note_serializer_spec.rb' - 'spec/serializers/activitypub/update_poll_serializer_spec.rb' diff --git a/app/controllers/api/v1/instances/translation_languages_controller.rb b/app/controllers/api/v1/instances/translation_languages_controller.rb new file mode 100644 index 000000000..3910a499e --- /dev/null +++ b/app/controllers/api/v1/instances/translation_languages_controller.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class Api::V1::Instances::TranslationLanguagesController < Api::BaseController + skip_before_action :require_authenticated_user!, unless: :whitelist_mode? + + before_action :set_languages + + def show + expires_in 1.day, public: true + render json: @languages + end + + private + + def set_languages + if TranslationService.configured? + @languages = Rails.cache.fetch('translation_service/languages', expires_in: 7.days, race_condition_ttl: 1.hour) { TranslationService.configured.languages } + @languages['und'] = @languages.delete(nil) if @languages.key?(nil) + else + @languages = {} + end + end +end diff --git a/app/javascript/mastodon/actions/server.js b/app/javascript/mastodon/actions/server.js index 31d4aea10..091af0f0f 100644 --- a/app/javascript/mastodon/actions/server.js +++ b/app/javascript/mastodon/actions/server.js @@ -5,6 +5,10 @@ export const SERVER_FETCH_REQUEST = 'Server_FETCH_REQUEST'; export const SERVER_FETCH_SUCCESS = 'Server_FETCH_SUCCESS'; export const SERVER_FETCH_FAIL = 'Server_FETCH_FAIL'; +export const SERVER_TRANSLATION_LANGUAGES_FETCH_REQUEST = 'SERVER_TRANSLATION_LANGUAGES_FETCH_REQUEST'; +export const SERVER_TRANSLATION_LANGUAGES_FETCH_SUCCESS = 'SERVER_TRANSLATION_LANGUAGES_FETCH_SUCCESS'; +export const SERVER_TRANSLATION_LANGUAGES_FETCH_FAIL = 'SERVER_TRANSLATION_LANGUAGES_FETCH_FAIL'; + export const EXTENDED_DESCRIPTION_REQUEST = 'EXTENDED_DESCRIPTION_REQUEST'; export const EXTENDED_DESCRIPTION_SUCCESS = 'EXTENDED_DESCRIPTION_SUCCESS'; export const EXTENDED_DESCRIPTION_FAIL = 'EXTENDED_DESCRIPTION_FAIL'; @@ -37,6 +41,29 @@ const fetchServerFail = error => ({ error, }); +export const fetchServerTranslationLanguages = () => (dispatch, getState) => { + dispatch(fetchServerTranslationLanguagesRequest()); + + api(getState) + .get('/api/v1/instance/translation_languages').then(({ data }) => { + dispatch(fetchServerTranslationLanguagesSuccess(data)); + }).catch(err => dispatch(fetchServerTranslationLanguagesFail(err))); +}; + +const fetchServerTranslationLanguagesRequest = () => ({ + type: SERVER_TRANSLATION_LANGUAGES_FETCH_REQUEST, +}); + +const fetchServerTranslationLanguagesSuccess = translationLanguages => ({ + type: SERVER_TRANSLATION_LANGUAGES_FETCH_SUCCESS, + translationLanguages, +}); + +const fetchServerTranslationLanguagesFail = error => ({ + type: SERVER_TRANSLATION_LANGUAGES_FETCH_FAIL, + error, +}); + export const fetchExtendedDescription = () => (dispatch, getState) => { dispatch(fetchExtendedDescriptionRequest()); diff --git a/app/javascript/mastodon/components/status_content.jsx b/app/javascript/mastodon/components/status_content.jsx index f9c9fe079..67a487b00 100644 --- a/app/javascript/mastodon/components/status_content.jsx +++ b/app/javascript/mastodon/components/status_content.jsx @@ -3,6 +3,7 @@ import ImmutablePropTypes from 'react-immutable-proptypes'; import PropTypes from 'prop-types'; import { FormattedMessage, injectIntl } from 'react-intl'; import { Link } from 'react-router-dom'; +import { connect } from 'react-redux'; import classnames from 'classnames'; import PollContainer from 'mastodon/containers/poll_container'; import Icon from 'mastodon/components/icon'; @@ -47,7 +48,12 @@ class TranslateButton extends React.PureComponent { } -export default @injectIntl +const mapStateToProps = state => ({ + languages: state.getIn(['server', 'translationLanguages', 'items']), +}); + +export default @connect(mapStateToProps) +@injectIntl class StatusContent extends React.PureComponent { static contextTypes = { @@ -63,6 +69,7 @@ class StatusContent extends React.PureComponent { onClick: PropTypes.func, collapsable: PropTypes.bool, onCollapsedToggle: PropTypes.func, + languages: ImmutablePropTypes.map, intl: PropTypes.object, }; @@ -220,7 +227,9 @@ class StatusContent extends React.PureComponent { const hidden = this.props.onExpandedToggle ? !this.props.expanded : this.state.hidden; const renderReadMore = this.props.onClick && status.get('collapsed'); - const renderTranslate = this.props.onTranslate && status.get('translatable'); + const contentLocale = intl.locale.replace(/[_-].*/, ''); + const targetLanguages = this.props.languages?.get(status.get('language') || 'und'); + const renderTranslate = this.props.onTranslate && this.context.identity.signedIn && ['public', 'unlisted'].includes(status.get('visibility')) && status.get('contentHtml').length > 0 && targetLanguages?.includes(contentLocale); const content = { __html: status.get('translation') ? status.getIn(['translation', 'content']) : status.get('contentHtml') }; const spoilerContent = { __html: status.get('spoilerHtml') }; diff --git a/app/javascript/mastodon/features/ui/index.jsx b/app/javascript/mastodon/features/ui/index.jsx index 2dd59f95d..083707220 100644 --- a/app/javascript/mastodon/features/ui/index.jsx +++ b/app/javascript/mastodon/features/ui/index.jsx @@ -13,7 +13,7 @@ import { debounce } from 'lodash'; import { uploadCompose, resetCompose, changeComposeSpoilerness } from '../../actions/compose'; import { expandHomeTimeline } from '../../actions/timelines'; import { expandNotifications } from '../../actions/notifications'; -import { fetchServer } from '../../actions/server'; +import { fetchServer, fetchServerTranslationLanguages } from '../../actions/server'; import { clearHeight } from '../../actions/height_cache'; import { focusApp, unfocusApp, changeLayout } from 'mastodon/actions/app'; import { synchronouslySubmitMarkers, submitMarkers, fetchMarkers } from 'mastodon/actions/markers'; @@ -399,6 +399,7 @@ class UI extends React.PureComponent { this.props.dispatch(fetchMarkers()); this.props.dispatch(expandHomeTimeline()); this.props.dispatch(expandNotifications()); + this.props.dispatch(fetchServerTranslationLanguages()); setTimeout(() => this.props.dispatch(fetchServer()), 3000); } diff --git a/app/javascript/mastodon/reducers/server.js b/app/javascript/mastodon/reducers/server.js index db9f2b5e6..909ab2a66 100644 --- a/app/javascript/mastodon/reducers/server.js +++ b/app/javascript/mastodon/reducers/server.js @@ -2,6 +2,9 @@ import { SERVER_FETCH_REQUEST, SERVER_FETCH_SUCCESS, SERVER_FETCH_FAIL, + SERVER_TRANSLATION_LANGUAGES_FETCH_REQUEST, + SERVER_TRANSLATION_LANGUAGES_FETCH_SUCCESS, + SERVER_TRANSLATION_LANGUAGES_FETCH_FAIL, EXTENDED_DESCRIPTION_REQUEST, EXTENDED_DESCRIPTION_SUCCESS, EXTENDED_DESCRIPTION_FAIL, @@ -35,6 +38,12 @@ export default function server(state = initialState, action) { return state.set('server', fromJS(action.server)).setIn(['server', 'isLoading'], false); case SERVER_FETCH_FAIL: return state.setIn(['server', 'isLoading'], false); + case SERVER_TRANSLATION_LANGUAGES_FETCH_REQUEST: + return state.setIn(['translationLanguages', 'isLoading'], true); + case SERVER_TRANSLATION_LANGUAGES_FETCH_SUCCESS: + return state.setIn(['translationLanguages', 'items'], fromJS(action.translationLanguages)).setIn(['translationLanguages', 'isLoading'], false); + case SERVER_TRANSLATION_LANGUAGES_FETCH_FAIL: + return state.setIn(['translationLanguages', 'isLoading'], false); case EXTENDED_DESCRIPTION_REQUEST: return state.setIn(['extendedDescription', 'isLoading'], true); case EXTENDED_DESCRIPTION_SUCCESS: diff --git a/app/lib/translation_service.rb b/app/lib/translation_service.rb index 5ff93674a..bfe5de44f 100644 --- a/app/lib/translation_service.rb +++ b/app/lib/translation_service.rb @@ -21,8 +21,8 @@ class TranslationService ENV['DEEPL_API_KEY'].present? || ENV['LIBRE_TRANSLATE_ENDPOINT'].present? end - def supported?(_source_language, _target_language) - false + def languages + {} end def translate(_text, _source_language, _target_language) diff --git a/app/lib/translation_service/deepl.rb b/app/lib/translation_service/deepl.rb index deff95a1d..afcb7ecb2 100644 --- a/app/lib/translation_service/deepl.rb +++ b/app/lib/translation_service/deepl.rb @@ -17,25 +17,31 @@ class TranslationService::DeepL < TranslationService end end - def supported?(source_language, target_language) - source_language.in?(languages('source')) && target_language.in?(languages('target')) + def languages + source_languages = [nil] + fetch_languages('source') + + # In DeepL, EN and PT are deprecated in favor of EN-GB/EN-US and PT-BR/PT-PT, so + # they are supported but not returned by the API. + target_languages = %w(en pt) + fetch_languages('target') + + source_languages.index_with { |language| target_languages.without(nil, language) } end private - def languages(type) - Rails.cache.fetch("translation_service/deepl/languages/#{type}", expires_in: 7.days, race_condition_ttl: 1.minute) do - request(:get, "/v2/languages?type=#{type}") do |res| - # In DeepL, EN and PT are deprecated in favor of EN-GB/EN-US and PT-BR/PT-PT, so - # they are supported but not returned by the API. - extra = type == 'source' ? [nil] : %w(en pt) - languages = Oj.load(res.body_with_limit).map { |language| language['language'].downcase } - - languages + extra - end + def fetch_languages(type) + request(:get, "/v2/languages?type=#{type}") do |res| + Oj.load(res.body_with_limit).map { |language| normalize_language(language['language']) } end end + def normalize_language(language) + subtags = language.split(/[_-]/) + subtags[0].downcase! + subtags[1]&.upcase! + subtags.join('-') + end + def request(verb, path, **options) req = Request.new(verb, "#{base_url}#{path}", **options) req.add_headers(Authorization: "DeepL-Auth-Key #{@api_key}") diff --git a/app/lib/translation_service/libre_translate.rb b/app/lib/translation_service/libre_translate.rb index 743e4d77f..8bb194a9c 100644 --- a/app/lib/translation_service/libre_translate.rb +++ b/app/lib/translation_service/libre_translate.rb @@ -15,22 +15,18 @@ class TranslationService::LibreTranslate < TranslationService end end - def supported?(source_language, target_language) - languages.key?(source_language) && languages[source_language].include?(target_language) - end - - private - def languages - Rails.cache.fetch('translation_service/libre_translate/languages', expires_in: 7.days, race_condition_ttl: 1.minute) do - request(:get, '/languages') do |res| - languages = Oj.load(res.body_with_limit).to_h { |language| [language['code'], language['targets']] } - languages[nil] = languages.values.flatten.uniq - languages + request(:get, '/languages') do |res| + languages = Oj.load(res.body_with_limit).to_h do |language| + [language['code'], language['targets'].without(language['code'])] end + languages[nil] = languages.values.flatten.uniq.sort + languages end end + private + def request(verb, path, **options) req = Request.new(verb, "#{@base_url}#{path}", allow_local: true, **options) req.add_headers('Content-Type': 'application/json') diff --git a/app/models/status.rb b/app/models/status.rb index dd7ac2edb..e7ea191a8 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -232,16 +232,6 @@ class Status < ApplicationRecord public_visibility? || unlisted_visibility? end - def translatable? - translate_target_locale = I18n.locale.to_s.split(/[_-]/).first - - distributable? && - content.present? && - language != translate_target_locale && - TranslationService.configured? && - TranslationService.configured.supported?(language, translate_target_locale) - end - alias sign? distributable? def with_media? diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb index a422f5b25..e0b8f32a6 100644 --- a/app/serializers/rest/status_serializer.rb +++ b/app/serializers/rest/status_serializer.rb @@ -4,7 +4,7 @@ class REST::StatusSerializer < ActiveModel::Serializer include FormattingHelper attributes :id, :created_at, :in_reply_to_id, :in_reply_to_account_id, - :sensitive, :spoiler_text, :visibility, :language, :translatable, + :sensitive, :spoiler_text, :visibility, :language, :uri, :url, :replies_count, :reblogs_count, :favourites_count, :edited_at @@ -50,10 +50,6 @@ class REST::StatusSerializer < ActiveModel::Serializer object.account.user_shows_application? || (current_user? && current_user.account_id == object.account_id) end - def translatable - current_user? && object.translatable? - end - def visibility # This visibility is masked behind "private" # to avoid API changes because there are no diff --git a/app/services/translate_status_service.rb b/app/services/translate_status_service.rb index 92d8b62a0..796f13a0d 100644 --- a/app/services/translate_status_service.rb +++ b/app/services/translate_status_service.rb @@ -6,19 +6,29 @@ class TranslateStatusService < BaseService include FormattingHelper def call(status, target_language) - raise Mastodon::NotPermittedError unless status.translatable? - @status = status @content = status_content_format(@status) @target_language = target_language + raise Mastodon::NotPermittedError unless permitted? + Rails.cache.fetch("translations/#{@status.language}/#{@target_language}/#{content_hash}", expires_in: CACHE_TTL) { translation_backend.translate(@content, @status.language, @target_language) } end private def translation_backend - TranslationService.configured + @translation_backend ||= TranslationService.configured + end + + def permitted? + return false unless @status.distributable? && @status.content.present? && TranslationService.configured? + + languages[@status.language]&.include?(@target_language) + end + + def languages + Rails.cache.fetch('translation_service/languages', expires_in: 7.days, race_condition_ttl: 1.hour) { TranslationService.configured.languages } end def content_hash diff --git a/config/routes.rb b/config/routes.rb index 530b46a5a..ea595e1e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -546,6 +546,7 @@ Rails.application.routes.draw do resources :domain_blocks, only: [:index], controller: 'instances/domain_blocks' resource :privacy_policy, only: [:show], controller: 'instances/privacy_policies' resource :extended_description, only: [:show], controller: 'instances/extended_descriptions' + resource :translation_languages, only: [:show], controller: 'instances/translation_languages' resource :activity, only: [:show], controller: 'instances/activity' end diff --git a/spec/controllers/api/v1/instances/translation_languages_controller_spec.rb b/spec/controllers/api/v1/instances/translation_languages_controller_spec.rb new file mode 100644 index 000000000..5b7e4abb6 --- /dev/null +++ b/spec/controllers/api/v1/instances/translation_languages_controller_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe Api::V1::Instances::TranslationLanguagesController do + describe 'GET #show' do + context 'when no translation service is configured' do + it 'returns empty language matrix' do + get :show + + expect(response).to have_http_status(200) + expect(body_as_json).to eq({}) + end + end + + context 'when a translation service is configured' do + before do + service = instance_double(TranslationService::DeepL, languages: { nil => %w(en de), 'en' => ['de'] }) + allow(TranslationService).to receive(:configured?).and_return(true) + allow(TranslationService).to receive(:configured).and_return(service) + end + + it 'returns language matrix' do + get :show + + expect(response).to have_http_status(200) + expect(body_as_json).to eq({ und: %w(en de), en: ['de'] }) + end + end + end +end diff --git a/spec/controllers/api/v1/statuses/translations_controller_spec.rb b/spec/controllers/api/v1/statuses/translations_controller_spec.rb index 2deea9fc0..8495779bf 100644 --- a/spec/controllers/api/v1/statuses/translations_controller_spec.rb +++ b/spec/controllers/api/v1/statuses/translations_controller_spec.rb @@ -19,9 +19,10 @@ describe Api::V1::Statuses::TranslationsController do before do translation = TranslationService::Translation.new(text: 'Hello') - service = instance_double(TranslationService::DeepL, translate: translation, supported?: true) + service = instance_double(TranslationService::DeepL, translate: translation) allow(TranslationService).to receive(:configured?).and_return(true) allow(TranslationService).to receive(:configured).and_return(service) + Rails.cache.write('translation_service/languages', { 'es' => ['en'] }) post :create, params: { status_id: status.id } end diff --git a/spec/lib/translation_service/deepl_spec.rb b/spec/lib/translation_service/deepl_spec.rb index aa2473186..2363f8f13 100644 --- a/spec/lib/translation_service/deepl_spec.rb +++ b/spec/lib/translation_service/deepl_spec.rb @@ -16,29 +16,6 @@ RSpec.describe TranslationService::DeepL do ) end - describe '#supported?' do - it 'supports included languages as source and target languages' do - expect(service.supported?('uk', 'en')).to be true - end - - it 'supports auto-detecting source language' do - expect(service.supported?(nil, 'en')).to be true - end - - it 'supports "en" and "pt" as target languages though not included in language list' do - expect(service.supported?('uk', 'en')).to be true - expect(service.supported?('uk', 'pt')).to be true - end - - it 'does not support non-included language as target language' do - expect(service.supported?('uk', 'nl')).to be false - end - - it 'does not support non-included language as source language' do - expect(service.supported?('da', 'en')).to be false - end - end - describe '#translate' do it 'returns translation with specified source language' do stub_request(:post, 'https://api.deepl.com/v2/translate') @@ -63,13 +40,18 @@ RSpec.describe TranslationService::DeepL do end end - describe '#languages?' do + describe '#languages' do it 'returns source languages' do - expect(service.send(:languages, 'source')).to eq ['en', 'uk', nil] + expect(service.languages.keys).to eq [nil, 'en', 'uk'] + end + + it 'returns target languages for each source language' do + expect(service.languages['en']).to eq %w(pt en-GB zh) + expect(service.languages['uk']).to eq %w(en pt en-GB zh) end - it 'returns target languages' do - expect(service.send(:languages, 'target')).to eq %w(en-gb zh en pt) + it 'returns target languages for auto-detection' do + expect(service.languages[nil]).to eq %w(en pt en-GB zh) end end diff --git a/spec/lib/translation_service/libre_translate_spec.rb b/spec/lib/translation_service/libre_translate_spec.rb index a6cb01884..fbd726a7e 100644 --- a/spec/lib/translation_service/libre_translate_spec.rb +++ b/spec/lib/translation_service/libre_translate_spec.rb @@ -7,41 +7,24 @@ RSpec.describe TranslationService::LibreTranslate do before do stub_request(:get, 'https://libretranslate.example.com/languages').to_return( - body: '[{"code": "en","name": "English","targets": ["de","es"]},{"code": "da","name": "Danish","targets": ["en","de"]}]' + body: '[{"code": "en","name": "English","targets": ["de","en","es"]},{"code": "da","name": "Danish","targets": ["en","pt"]}]' ) end - describe '#supported?' do - it 'supports included language pair' do - expect(service.supported?('en', 'de')).to be true - end - - it 'does not support reversed language pair' do - expect(service.supported?('de', 'en')).to be false - end - - it 'supports auto-detecting source language' do - expect(service.supported?(nil, 'de')).to be true - end - - it 'does not support auto-detecting for unsupported target language' do - expect(service.supported?(nil, 'pt')).to be false - end - end - describe '#languages' do - subject(:languages) { service.send(:languages) } + subject(:languages) { service.languages } - it 'includes supported source languages' do + it 'returns source languages' do expect(languages.keys).to eq ['en', 'da', nil] end - it 'includes supported target languages for source language' do + it 'returns target languages for each source language' do expect(languages['en']).to eq %w(de es) + expect(languages['da']).to eq %w(en pt) end - it 'includes supported target languages for auto-detected language' do - expect(languages[nil]).to eq %w(de es en) + it 'returns target languages for auto-detected language' do + expect(languages[nil]).to eq %w(de en es pt) end end diff --git a/spec/models/status_spec.rb b/spec/models/status_spec.rb index 1f6cfc796..1e58c6d0d 100644 --- a/spec/models/status_spec.rb +++ b/spec/models/status_spec.rb @@ -114,85 +114,6 @@ RSpec.describe Status, type: :model do end end - describe '#translatable?' do - before do - allow(TranslationService).to receive(:configured?).and_return(true) - allow(TranslationService).to receive(:configured).and_return(TranslationService.new) - allow(TranslationService.configured).to receive(:supported?).with('es', 'en').and_return(true) - - subject.language = 'es' - subject.visibility = :public - end - - context 'all conditions are satisfied' do - it 'returns true' do - expect(subject.translatable?).to be true - end - end - - context 'translation service is not configured' do - it 'returns false' do - allow(TranslationService).to receive(:configured?).and_return(false) - allow(TranslationService).to receive(:configured).and_raise(TranslationService::NotConfiguredError) - expect(subject.translatable?).to be false - end - end - - context 'status language is nil' do - it 'returns true' do - subject.language = nil - allow(TranslationService.configured).to receive(:supported?).with(nil, 'en').and_return(true) - expect(subject.translatable?).to be true - end - end - - context 'status language is same as default locale' do - it 'returns false' do - subject.language = I18n.locale - expect(subject.translatable?).to be false - end - end - - context 'status language is unsupported' do - it 'returns false' do - subject.language = 'af' - allow(TranslationService.configured).to receive(:supported?).with('af', 'en').and_return(false) - expect(subject.translatable?).to be false - end - end - - context 'default locale is unsupported' do - it 'returns false' do - allow(TranslationService.configured).to receive(:supported?).with('es', 'af').and_return(false) - I18n.with_locale('af') do - expect(subject.translatable?).to be false - end - end - end - - context 'default locale has region' do - it 'returns true' do - I18n.with_locale('en-GB') do - expect(subject.translatable?).to be true - end - end - end - - context 'status text is blank' do - it 'returns false' do - subject.text = ' ' - expect(subject.translatable?).to be false - end - end - - context 'status visiblity is hidden' do - it 'returns false' do - subject.visibility = 'limited' - expect(subject.translatable?).to be false - end - end - end - describe '#content' do it 'returns the text of the status if it is not a reblog' do expect(subject.content).to eql subject.text diff --git a/spec/presenters/instance_presenter_spec.rb b/spec/presenters/instance_presenter_spec.rb index 795abd8b4..2a1d668ce 100644 --- a/spec/presenters/instance_presenter_spec.rb +++ b/spec/presenters/instance_presenter_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' describe InstancePresenter do - let(:instance_presenter) { InstancePresenter.new } + let(:instance_presenter) { described_class.new } describe '#description' do around do |example| -- cgit From 94cbd808b5b3e7999c7e77dc724b7e8c9dd2bdec Mon Sep 17 00:00:00 2001 From: Vyr Cossont Date: Sat, 18 Mar 2023 23:47:54 -0700 Subject: Webhooks for local status.create, status.update, account.update (#24133) --- .rubocop_todo.yml | 2 +- app/models/account.rb | 7 +++++++ app/models/status.rb | 11 +++++++++++ app/models/webhook.rb | 3 +++ app/workers/webhooks/delivery_worker.rb | 2 +- 5 files changed, 23 insertions(+), 2 deletions(-) (limited to 'app/models/status.rb') diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a3536e202..b2f8bf52c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -245,7 +245,7 @@ Metrics/BlockNesting: # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 368 + Max: 375 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/CyclomaticComplexity: diff --git a/app/models/account.rb b/app/models/account.rb index 1ff083e54..c4df48878 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -122,6 +122,8 @@ class Account < ApplicationRecord scope :not_excluded_by_account, ->(account) { where.not(id: account.excluded_from_timeline_account_ids) } scope :not_domain_blocked_by_account, ->(account) { where(arel_table[:domain].eq(nil).or(arel_table[:domain].not_in(account.excluded_from_timeline_domains))) } + after_update_commit :trigger_update_webhooks + delegate :email, :unconfirmed_email, :current_sign_in_at, @@ -593,4 +595,9 @@ class Account < ApplicationRecord CanonicalEmailBlock.where(reference_account: self).delete_all end + + # NOTE: the `account.created` webhook is triggered by the `User` model, not `Account`. + def trigger_update_webhooks + TriggerWebhookWorker.perform_async('account.updated', 'Account', id) if local? + end end diff --git a/app/models/status.rb b/app/models/status.rb index e7ea191a8..2e32c3f16 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -111,6 +111,9 @@ class Status < ApplicationRecord where('NOT EXISTS (SELECT * FROM statuses_tags forbidden WHERE forbidden.status_id = statuses.id AND forbidden.tag_id IN (?))', tag_ids) } + after_create_commit :trigger_create_webhooks + after_update_commit :trigger_update_webhooks + cache_associated :application, :media_attachments, :conversation, @@ -535,4 +538,12 @@ class Status < ApplicationRecord reblog&.decrement_count!(:reblogs_count) if reblog? thread&.decrement_count!(:replies_count) if in_reply_to_id.present? && distributable? end + + def trigger_create_webhooks + TriggerWebhookWorker.perform_async('status.created', 'Status', id) if local? + end + + def trigger_update_webhooks + TriggerWebhookWorker.perform_async('status.updated', 'Status', id) if local? + end end diff --git a/app/models/webhook.rb b/app/models/webhook.rb index 4aafb1257..9a056a386 100644 --- a/app/models/webhook.rb +++ b/app/models/webhook.rb @@ -17,7 +17,10 @@ class Webhook < ApplicationRecord EVENTS = %w( account.approved account.created + account.updated report.created + status.created + status.updated ).freeze scope :enabled, -> { where(enabled: true) } diff --git a/app/workers/webhooks/delivery_worker.rb b/app/workers/webhooks/delivery_worker.rb index b1e345c5e..f8ed669fb 100644 --- a/app/workers/webhooks/delivery_worker.rb +++ b/app/workers/webhooks/delivery_worker.rb @@ -19,7 +19,7 @@ class Webhooks::DeliveryWorker private def perform_request - request = Request.new(:post, @webhook.url, body: @body) + request = Request.new(:post, @webhook.url, body: @body, allow_local: true) request.add_headers( 'Content-Type' => 'application/json', -- cgit From 2626097869c9520c21aaba73c9bcfe72c0f25f28 Mon Sep 17 00:00:00 2001 From: Claire Date: Wed, 22 Mar 2023 15:47:44 +0100 Subject: Fix Rails cache namespace being overriden with `v2` for cached statuses (#24202) --- app/controllers/concerns/cache_concern.rb | 4 ++-- app/models/status.rb | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'app/models/status.rb') diff --git a/app/controllers/concerns/cache_concern.rb b/app/controllers/concerns/cache_concern.rb index e606218ac..a5a9ba3e1 100644 --- a/app/controllers/concerns/cache_concern.rb +++ b/app/controllers/concerns/cache_concern.rb @@ -187,7 +187,7 @@ module CacheConcern return [] if raw.empty? cached_keys_with_value = begin - Rails.cache.read_multi(*raw, namespace: 'v2').transform_keys(&:id).transform_values { |r| ActiveRecordCoder.load(r) } + Rails.cache.read_multi(*raw).transform_keys(&:id).transform_values { |r| ActiveRecordCoder.load(r) } rescue ActiveRecordCoder::Error {} # The serialization format may have changed, let's pretend it's a cache miss. end @@ -200,7 +200,7 @@ module CacheConcern uncached = klass.where(id: uncached_ids).with_includes.index_by(&:id) uncached.each_value do |item| - Rails.cache.write(item, ActiveRecordCoder.dump(item), namespace: 'v2') + Rails.cache.write(item, ActiveRecordCoder.dump(item)) end end diff --git a/app/models/status.rb b/app/models/status.rb index 2e32c3f16..2757497db 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -140,6 +140,10 @@ class Status < ApplicationRecord REAL_TIME_WINDOW = 6.hours + def cache_key + "v2:#{super}" + end + def searchable_by(preloaded = nil) ids = [] -- cgit From ff168ef2024626f37fa776fde5739dcd58ecb9f2 Mon Sep 17 00:00:00 2001 From: Claire Date: Sun, 9 Apr 2023 11:25:30 +0200 Subject: Fix most rubocop issues (#2165) * Run rubocop --autocorrect on app/, config/ and lib/, also manually fix some remaining style issues * Run rubocop --autocorrect-all on db/ * Run rubocop --autocorrect-all on `spec/` and fix remaining issues --- .rubocop_todo.yml | 2 + .../api/v1/timelines/public_controller.rb | 2 +- app/controllers/auth/confirmations_controller.rb | 13 ++-- app/controllers/settings/flavours_controller.rb | 4 +- app/helpers/accounts_helper.rb | 2 +- app/lib/advanced_text_formatter.rb | 1 + app/lib/feed_manager.rb | 2 + app/lib/themes.rb | 45 ++++++------ app/models/direct_feed.rb | 9 +-- app/models/status.rb | 19 ++--- app/models/user.rb | 1 - app/serializers/activitypub/note_serializer.rb | 1 + app/serializers/rest/account_serializer.rb | 2 +- app/serializers/rest/mute_serializer.rb | 4 +- app/serializers/rest/status_serializer.rb | 4 +- app/services/backup_service.rb | 2 +- app/services/fan_out_on_write_service.rb | 2 +- app/services/post_status_service.rb | 25 ++++--- app/validators/status_pin_validator.rb | 2 +- config/initializers/0_duplicate_migrations.rb | 24 ++++--- config/initializers/simple_form.rb | 1 + db/migrate/20171009222537_create_keyword_mutes.rb | 2 + ...900_move_keyword_mutes_into_glitch_namespace.rb | 2 + ...171210213213_add_local_only_flag_to_statuses.rb | 2 + db/migrate/20180410220657_create_bookmarks.rb | 2 + ..._add_apply_to_mentions_flag_to_keyword_mutes.rb | 2 + db/migrate/20180707193142_migrate_filters.rb | 12 ++-- .../20190512200918_add_content_type_to_statuses.rb | 2 + ...20209175231_add_content_type_to_status_edits.rb | 2 + .../20180813160548_post_migrate_filters.rb | 6 +- lib/sanitize_ext/sanitize_config.rb | 8 +-- lib/tasks/assets.rake | 12 ++-- lib/tasks/glitchsoc.rake | 8 ++- .../api/v1/accounts/credentials_controller_spec.rb | 4 +- .../api/v1/timelines/direct_controller_spec.rb | 2 +- spec/controllers/application_controller_spec.rb | 4 +- .../settings/flavours_controller_spec.rb | 3 +- spec/lib/advanced_text_formatter_spec.rb | 82 +++++++++++----------- spec/models/concerns/account_interactions_spec.rb | 39 +--------- spec/models/public_feed_spec.rb | 12 ++-- spec/models/status_spec.rb | 42 +++++------ spec/models/tag_feed_spec.rb | 2 +- spec/policies/status_policy_spec.rb | 4 +- spec/services/notify_service_spec.rb | 2 +- spec/validators/status_length_validator_spec.rb | 2 +- 45 files changed, 210 insertions(+), 215 deletions(-) (limited to 'app/models/status.rb') diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 2e4801a55..dc7e21dc5 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -473,6 +473,7 @@ RSpec/ContextWording: - 'spec/lib/activitypub/activity/create_spec.rb' - 'spec/lib/activitypub/activity/follow_spec.rb' - 'spec/lib/activitypub/activity/reject_spec.rb' + - 'spec/lib/advanced_text_formatter_spec.rb' - 'spec/lib/emoji_formatter_spec.rb' - 'spec/lib/entity_cache_spec.rb' - 'spec/lib/feed_manager_spec.rb' @@ -1321,6 +1322,7 @@ Rails/FilePath: - 'app/models/setting.rb' - 'app/validators/reaction_validator.rb' - 'config/environments/test.rb' + - 'config/initializers/locale.rb' - 'db/migrate/20170716191202_add_hide_notifications_to_mute.rb' - 'db/migrate/20171005171936_add_disabled_to_custom_emojis.rb' - 'db/migrate/20171028221157_add_reblogs_to_follows.rb' diff --git a/app/controllers/api/v1/timelines/public_controller.rb b/app/controllers/api/v1/timelines/public_controller.rb index 493fe4776..4675af921 100644 --- a/app/controllers/api/v1/timelines/public_controller.rb +++ b/app/controllers/api/v1/timelines/public_controller.rb @@ -40,7 +40,7 @@ class Api::V1::Timelines::PublicController < Api::BaseController only_media: truthy_param?(:only_media), allow_local_only: truthy_param?(:allow_local_only), with_replies: Setting.show_replies_in_public_timelines, - with_reblogs: Setting.show_reblogs_in_public_timelines, + with_reblogs: Setting.show_reblogs_in_public_timelines ) end diff --git a/app/controllers/auth/confirmations_controller.rb b/app/controllers/auth/confirmations_controller.rb index 0817a905c..620fb621d 100644 --- a/app/controllers/auth/confirmations_controller.rb +++ b/app/controllers/auth/confirmations_controller.rb @@ -15,12 +15,6 @@ class Auth::ConfirmationsController < Devise::ConfirmationsController skip_before_action :require_functional! - def new - super - - resource.email = current_user.unconfirmed_email || current_user.email if user_signed_in? - end - def show old_session_values = session.to_hash reset_session @@ -29,6 +23,12 @@ class Auth::ConfirmationsController < Devise::ConfirmationsController super end + def new + super + + resource.email = current_user.unconfirmed_email || current_user.email if user_signed_in? + end + def confirm_captcha check_captcha! do |message| flash.now[:alert] = message @@ -51,6 +51,7 @@ class Auth::ConfirmationsController < Devise::ConfirmationsController # step. confirmation_token = params[:confirmation_token] return if confirmation_token.nil? + @confirmation_user = User.find_first_by_auth_conditions(confirmation_token: confirmation_token) end diff --git a/app/controllers/settings/flavours_controller.rb b/app/controllers/settings/flavours_controller.rb index c1172598b..b179b9429 100644 --- a/app/controllers/settings/flavours_controller.rb +++ b/app/controllers/settings/flavours_controller.rb @@ -12,9 +12,7 @@ class Settings::FlavoursController < Settings::BaseController end def show - unless Themes.instance.flavours.include?(params[:flavour]) || (params[:flavour] == current_flavour) - redirect_to action: 'show', flavour: current_flavour - end + redirect_to action: 'show', flavour: current_flavour unless Themes.instance.flavours.include?(params[:flavour]) || (params[:flavour] == current_flavour) @listing = Themes.instance.flavours @selected = params[:flavour] diff --git a/app/helpers/accounts_helper.rb b/app/helpers/accounts_helper.rb index 91c3a116b..b8277ee17 100644 --- a/app/helpers/accounts_helper.rb +++ b/app/helpers/accounts_helper.rb @@ -28,7 +28,7 @@ module AccountsHelper end def hide_followers_count?(account) - Setting.hide_followers_count || account.user&.settings['hide_followers_count'] + Setting.hide_followers_count || account.user&.settings&.[]('hide_followers_count') end def account_description(account) diff --git a/app/lib/advanced_text_formatter.rb b/app/lib/advanced_text_formatter.rb index 21e81d4d1..cdf1e2d9c 100644 --- a/app/lib/advanced_text_formatter.rb +++ b/app/lib/advanced_text_formatter.rb @@ -15,6 +15,7 @@ class AdvancedTextFormatter < TextFormatter def autolink(link, link_type) return link if link_type == :email + @format_link.call(link) end end diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb index 4ce888fc9..15ff6d15f 100644 --- a/app/lib/feed_manager.rb +++ b/app/lib/feed_manager.rb @@ -306,6 +306,7 @@ class FeedManager statuses.each do |status| next if filter_from_direct?(status, account) + added += 1 if add_to_feed(:direct, account.id, status) end @@ -459,6 +460,7 @@ class FeedManager # @return [Boolean] def filter_from_direct?(status, receiver_id) return false if receiver_id == status.account_id + filter_from_mentions?(status, receiver_id) end diff --git a/app/lib/themes.rb b/app/lib/themes.rb index 81e016d4a..45ba47780 100644 --- a/app/lib/themes.rb +++ b/app/lib/themes.rb @@ -7,24 +7,23 @@ class Themes include Singleton def initialize - core = YAML.load_file(Rails.root.join('app', 'javascript', 'core', 'theme.yml')) - core['pack'] = Hash.new unless core['pack'] + core['pack'] = {} unless core['pack'] - result = Hash.new - Dir.glob(Rails.root.join('app', 'javascript', 'flavours', '*', 'theme.yml')) do |path| - data = YAML.load_file(path) + result = {} + Rails.root.glob('app/javascript/flavours/*/theme.yml') do |pathname| + data = YAML.load_file(pathname) next unless data['pack'] - dir = File.dirname(path) - name = File.basename(dir) + dir = pathname.dirname + name = dir.basename.to_s locales = [] screenshots = [] if data['locales'] Dir.glob(File.join(dir, data['locales'], '*.{js,json}')) do |locale| - localeName = File.basename(locale, File.extname(locale)) - locales.push(localeName) unless localeName.match(/defaultMessages|whitelist|index/) + locale_name = File.basename(locale, File.extname(locale)) + locales.push(locale_name) unless /defaultMessages|whitelist|index/.match?(locale_name) end end @@ -43,34 +42,30 @@ class Themes result[name] = data end - Dir.glob(Rails.root.join('app', 'javascript', 'skins', '*', '*')) do |path| - ext = File.extname(path) - skin = File.basename(path) - name = File.basename(File.dirname(path)) + Rails.root.glob('app/javascript/skins/*/*') do |pathname| + ext = pathname.extname.to_s + skin = pathname.basename.to_s + name = pathname.dirname.basename.to_s next unless result[name] - if File.directory?(path) + if pathname.directory? pack = [] - Dir.glob(File.join(path, '*.{css,scss}')) do |sheet| - pack.push(File.basename(sheet, File.extname(sheet))) + pathname.glob('*.{css,scss}') do |sheet| + pack.push(sheet.basename(sheet.extname).to_s) end - elsif ext.match(/^\.s?css$/i) - skin = File.basename(path, ext) + elsif /^\.s?css$/i.match?(ext) + skin = pathname.basename(ext).to_s pack = ['common'] end - if skin != 'default' - result[name]['skin'][skin] = pack - end + result[name]['skin'][skin] = pack if skin != 'default' end @core = core @conf = result end - def core - @core - end + attr_reader :core def flavour(name) @conf[name] @@ -86,7 +81,7 @@ class Themes def flavours_and_skins flavours.map do |flavour| - [flavour, skins_for(flavour).map{ |skin| [flavour, skin] }] + [flavour, skins_for(flavour).map { |skin| [flavour, skin] }] end end end diff --git a/app/models/direct_feed.rb b/app/models/direct_feed.rb index 1f2448070..689a735b3 100644 --- a/app/models/direct_feed.rb +++ b/app/models/direct_feed.rb @@ -4,9 +4,8 @@ class DirectFeed < Feed include Redisable def initialize(account) - @type = :direct - @id = account.id @account = account + super(:direct, account.id) end def get(limit, max_id = nil, since_id = nil, min_id = nil) @@ -19,10 +18,12 @@ class DirectFeed < Feed private - def from_database(limit, max_id, since_id, min_id) + # TODO: _min_id is not actually handled by `as_direct_timeline` + def from_database(limit, max_id, since_id, _min_id) loop do - statuses = Status.as_direct_timeline(@account, limit, max_id, since_id, min_id) + statuses = Status.as_direct_timeline(@account, limit, max_id, since_id) return statuses if statuses.empty? + max_id = statuses.last.id statuses = statuses.reject { |status| FeedManager.instance.filter?(:direct, status, @account) } return statuses unless statuses.empty? diff --git a/app/models/status.rb b/app/models/status.rb index e01ddb5c5..8a58e5d68 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -338,7 +338,7 @@ class Status < ApplicationRecord visibilities.keys - %w(direct limited) end - def as_direct_timeline(account, limit = 20, max_id = nil, since_id = nil, cache_ids = false) + def as_direct_timeline(account, limit = 20, max_id = nil, since_id = nil) # direct timeline is mix of direct message from_me and to_me. # 2 queries are executed with pagination. # constant expression using arel_table is required for partial index @@ -369,14 +369,9 @@ class Status < ApplicationRecord query_to_me = query_to_me.where('mentions.status_id > ?', since_id) end - if cache_ids - # returns array of cache_ids object that have id and updated_at - (query_from_me.cache_ids.to_a + query_to_me.cache_ids.to_a).uniq(&:id).sort_by(&:id).reverse.take(limit) - else - # returns ActiveRecord.Relation - items = (query_from_me.select(:id).to_a + query_to_me.select(:id).to_a).uniq(&:id).sort_by(&:id).reverse.take(limit) - Status.where(id: items.map(&:id)) - end + # returns ActiveRecord.Relation + items = (query_from_me.select(:id).to_a + query_to_me.select(:id).to_a).uniq(&:id).sort_by(&:id).reverse.take(limit) + Status.where(id: items.map(&:id)) end def favourites_map(status_ids, account_id) @@ -553,9 +548,9 @@ class Status < ApplicationRecord end def set_locality - if account.domain.nil? && !attribute_changed?(:local_only) - self.local_only = marked_local_only? - end + return unless account.domain.nil? && !attribute_changed?(:local_only) + + self.local_only = marked_local_only? end def set_conversation diff --git a/app/models/user.rb b/app/models/user.rb index 3471bb2c1..daf8768e8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -244,7 +244,6 @@ class User < ApplicationRecord end def functional? - functional_or_moved? end diff --git a/app/serializers/activitypub/note_serializer.rb b/app/serializers/activitypub/note_serializer.rb index ca067ed9b..52ffaf717 100644 --- a/app/serializers/activitypub/note_serializer.rb +++ b/app/serializers/activitypub/note_serializer.rb @@ -32,6 +32,7 @@ class ActivityPub::NoteSerializer < ActivityPub::Serializer def id raise Mastodon::NotPermittedError, 'Local-only statuses should not be serialized' if object.local_only? && !instance_options[:allow_local_only] + ActivityPub::TagManager.instance.uri_for(object) end diff --git a/app/serializers/rest/account_serializer.rb b/app/serializers/rest/account_serializer.rb index e6c8fe4b2..d4e7ac974 100644 --- a/app/serializers/rest/account_serializer.rb +++ b/app/serializers/rest/account_serializer.rb @@ -91,7 +91,7 @@ class REST::AccountSerializer < ActiveModel::Serializer end def followers_count - (Setting.hide_followers_count || object.user&.setting_hide_followers_count) ? -1 : object.followers_count + Setting.hide_followers_count || object.user&.setting_hide_followers_count ? -1 : object.followers_count end def display_name diff --git a/app/serializers/rest/mute_serializer.rb b/app/serializers/rest/mute_serializer.rb index 043a2f059..c9b55ff16 100644 --- a/app/serializers/rest/mute_serializer.rb +++ b/app/serializers/rest/mute_serializer.rb @@ -2,7 +2,7 @@ class REST::MuteSerializer < ActiveModel::Serializer include RoutingHelper - + attributes :id, :account, :target_account, :created_at, :hide_notifications def account @@ -12,4 +12,4 @@ class REST::MuteSerializer < ActiveModel::Serializer def target_account REST::AccountSerializer.new(object.target_account) end -end \ No newline at end of file +end diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb index 659c45b83..eb5f3c3ea 100644 --- a/app/serializers/rest/status_serializer.rb +++ b/app/serializers/rest/status_serializer.rb @@ -13,7 +13,7 @@ class REST::StatusSerializer < ActiveModel::Serializer attribute :muted, if: :current_user? attribute :bookmarked, if: :current_user? attribute :pinned, if: :pinnable? - attribute :local_only if :local? + attribute :local_only, if: :local? has_many :filtered, serializer: REST::FilterResultSerializer, if: :current_user? attribute :content, unless: :source_requested? @@ -32,6 +32,8 @@ class REST::StatusSerializer < ActiveModel::Serializer has_one :preview_card, key: :card, serializer: REST::PreviewCardSerializer has_one :preloadable_poll, key: :poll, serializer: REST::PollSerializer + delegate :local?, to: :object + def id object.id.to_s end diff --git a/app/services/backup_service.rb b/app/services/backup_service.rb index a9d740211..c5e7a8e58 100644 --- a/app/services/backup_service.rb +++ b/app/services/backup_service.rb @@ -154,7 +154,7 @@ class BackupService < BaseService object, serializer: serializer, adapter: ActivityPub::Adapter, - allow_local_only: true, + allow_local_only: true ).as_json end diff --git a/app/services/fan_out_on_write_service.rb b/app/services/fan_out_on_write_service.rb index 8e74e152e..3b14a6748 100644 --- a/app/services/fan_out_on_write_service.rb +++ b/app/services/fan_out_on_write_service.rb @@ -116,7 +116,7 @@ class FanOutOnWriteService < BaseService end def deliver_to_direct_timelines! - FeedInsertWorker.push_bulk(@status.mentions.includes(:account).map(&:account).select { |mentioned_account| mentioned_account.local? }) do |account| + FeedInsertWorker.push_bulk(@status.mentions.includes(:account).map(&:account).select(&:local?)) do |account| [@status.id, account.id, 'direct', { 'update' => update? }] end end diff --git a/app/services/post_status_service.rb b/app/services/post_status_service.rb index bca3b3ff7..74ec47a33 100644 --- a/app/services/post_status_service.rb +++ b/app/services/post_status_service.rb @@ -61,17 +61,22 @@ class PostStatusService < BaseService private - def preprocess_attributes! - if @text.blank? && @options[:spoiler_text].present? - @text = '.' - if @media&.find(&:video?) || @media&.find(&:gifv?) - @text = '📹' - elsif @media&.find(&:audio?) - @text = '🎵' - elsif @media&.find(&:image?) - @text = '🖼' - end + def fill_blank_text! + return unless @text.blank? && @options[:spoiler_text].present? + + if @media&.any?(&:video?) || @media&.any?(&:gifv?) + @text = '📹' + elsif @media&.any?(&:audio?) + @text = '🎵' + elsif @media&.any?(&:image?) + @text = '🖼' + else + @text = '.' end + end + + def preprocess_attributes! + fill_blank_text! @sensitive = (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present? @visibility = @options[:visibility] || @account.user&.setting_default_privacy @visibility = :unlisted if @visibility&.to_sym == :public && @account.silenced? diff --git a/app/validators/status_pin_validator.rb b/app/validators/status_pin_validator.rb index 9466a81fe..4af7bd295 100644 --- a/app/validators/status_pin_validator.rb +++ b/app/validators/status_pin_validator.rb @@ -7,6 +7,6 @@ class StatusPinValidator < ActiveModel::Validator pin.errors.add(:base, I18n.t('statuses.pin_errors.reblog')) if pin.status.reblog? pin.errors.add(:base, I18n.t('statuses.pin_errors.ownership')) if pin.account_id != pin.status.account_id pin.errors.add(:base, I18n.t('statuses.pin_errors.direct')) if pin.status.direct_visibility? - pin.errors.add(:base, I18n.t('statuses.pin_errors.limit')) if pin.account.status_pins.count >= MAX_PINNED && pin.account.local? + pin.errors.add(:base, I18n.t('statuses.pin_errors.limit')) if pin.account.status_pins.count >= MAX_PINNED && pin.account.local? end end diff --git a/config/initializers/0_duplicate_migrations.rb b/config/initializers/0_duplicate_migrations.rb index 6c45e4bd2..1b8b59025 100644 --- a/config/initializers/0_duplicate_migrations.rb +++ b/config/initializers/0_duplicate_migrations.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Some migrations have been present in glitch-soc for a long time and have then # been merged in upstream Mastodon, under a different version number. # @@ -12,24 +14,26 @@ # we decided monkey-patching Rails' Migrator to completely ignore the duplicate, # keeping only the one that has run, or an arbitrary one. -ALLOWED_DUPLICATES = [20180410220657, 20180831171112].freeze +ALLOWED_DUPLICATES = [2018_04_10_220657, 2018_08_31_171112].freeze module ActiveRecord class Migrator def self.new(direction, migrations, schema_migration, target_version = nil) migrated = Set.new(Base.connection.migration_context.get_all_versions) - migrations.group_by(&:name).each do |name, duplicates| - if duplicates.length > 1 && duplicates.all? { |m| ALLOWED_DUPLICATES.include?(m.version) } - # We have a set of allowed duplicates. Keep the migrated one, if any. - non_migrated = duplicates.reject { |m| migrated.include?(m.version.to_i) } + migrations.group_by(&:name).each do |_name, duplicates| + next unless duplicates.length > 1 && duplicates.all? { |m| ALLOWED_DUPLICATES.include?(m.version) } + + # We have a set of allowed duplicates. Keep the migrated one, if any. + non_migrated = duplicates.reject { |m| migrated.include?(m.version.to_i) } - if duplicates.length == non_migrated.length || non_migrated.length == 0 + migrations = begin + if duplicates.length == non_migrated.length || non_migrated.empty? # There weren't any migrated one, so we have to pick one “canonical” migration - migrations = migrations - duplicates[1..-1] + migrations - duplicates[1..] else # Just reject every duplicate which hasn't been migrated yet - migrations = migrations - non_migrated + migrations - non_migrated end end end @@ -43,10 +47,10 @@ module ActiveRecord # A set of duplicated migrations is considered migrated if at least one of # them is migrated. migrated = get_all_versions - migrations.group_by(&:name).each do |name, duplicates| + migrations.group_by(&:name).each do |_name, duplicates| return true unless duplicates.any? { |m| migrated.include?(m.version.to_i) } end - return false + false end end end diff --git a/config/initializers/simple_form.rb b/config/initializers/simple_form.rb index d167a1600..fff4f538e 100644 --- a/config/initializers/simple_form.rb +++ b/config/initializers/simple_form.rb @@ -22,6 +22,7 @@ end module GlitchOnlyComponent def glitch_only(_wrapper_options = nil) return unless options[:glitch_only] + options[:label_text] = ->(raw_label_text, _required_label_text, _label_present) { safe_join([raw_label_text, ' ', content_tag(:span, I18n.t('simple_form.glitch_only'), class: 'glitch_only')]) } nil end diff --git a/db/migrate/20171009222537_create_keyword_mutes.rb b/db/migrate/20171009222537_create_keyword_mutes.rb index 66411ba1d..77c88b0a5 100644 --- a/db/migrate/20171009222537_create_keyword_mutes.rb +++ b/db/migrate/20171009222537_create_keyword_mutes.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreateKeywordMutes < ActiveRecord::Migration[5.1] def change create_table :keyword_mutes do |t| diff --git a/db/migrate/20171021191900_move_keyword_mutes_into_glitch_namespace.rb b/db/migrate/20171021191900_move_keyword_mutes_into_glitch_namespace.rb index 269bb49d6..b6ea537c2 100644 --- a/db/migrate/20171021191900_move_keyword_mutes_into_glitch_namespace.rb +++ b/db/migrate/20171021191900_move_keyword_mutes_into_glitch_namespace.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class MoveKeywordMutesIntoGlitchNamespace < ActiveRecord::Migration[5.1] def change safety_assured do diff --git a/db/migrate/20171210213213_add_local_only_flag_to_statuses.rb b/db/migrate/20171210213213_add_local_only_flag_to_statuses.rb index af1e29d6a..010503b10 100644 --- a/db/migrate/20171210213213_add_local_only_flag_to_statuses.rb +++ b/db/migrate/20171210213213_add_local_only_flag_to_statuses.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddLocalOnlyFlagToStatuses < ActiveRecord::Migration[5.1] def change add_column :statuses, :local_only, :boolean diff --git a/db/migrate/20180410220657_create_bookmarks.rb b/db/migrate/20180410220657_create_bookmarks.rb index bc79022e4..aba21f5ea 100644 --- a/db/migrate/20180410220657_create_bookmarks.rb +++ b/db/migrate/20180410220657_create_bookmarks.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This migration is a duplicate of 20180831171112 and may get ignored, see # config/initializers/0_duplicate_migrations.rb diff --git a/db/migrate/20180604000556_add_apply_to_mentions_flag_to_keyword_mutes.rb b/db/migrate/20180604000556_add_apply_to_mentions_flag_to_keyword_mutes.rb index cd97d0f20..8078a07bf 100644 --- a/db/migrate/20180604000556_add_apply_to_mentions_flag_to_keyword_mutes.rb +++ b/db/migrate/20180604000556_add_apply_to_mentions_flag_to_keyword_mutes.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'mastodon/migration_helpers' class AddApplyToMentionsFlagToKeywordMutes < ActiveRecord::Migration[5.2] diff --git a/db/migrate/20180707193142_migrate_filters.rb b/db/migrate/20180707193142_migrate_filters.rb index 067c53357..8f6b3e1bb 100644 --- a/db/migrate/20180707193142_migrate_filters.rb +++ b/db/migrate/20180707193142_migrate_filters.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + class MigrateFilters < ActiveRecord::Migration[5.2] class GlitchKeywordMute < ApplicationRecord # Dummy class, as we removed Glitch::KeywordMute - belongs_to :account, required: true + belongs_to :account, optional: false validates_presence_of :keyword end @@ -15,7 +17,7 @@ class MigrateFilters < ActiveRecord::Migration[5.2] private def clean_up_contexts - self.context = Array(context).map(&:strip).map(&:presence).compact + self.context = Array(context).map(&:strip).filter_map(&:presence) end end @@ -27,7 +29,8 @@ class MigrateFilters < ActiveRecord::Migration[5.2] phrase: filter.keyword, context: filter.apply_to_mentions ? %w(home public notifications) : %w(home public), whole_word: filter.whole_word, - irreversible: true) + irreversible: true + ) end end @@ -48,7 +51,8 @@ class MigrateFilters < ActiveRecord::Migration[5.2] GlitchKeywordMute.where(account: filter.account).create!( keyword: filter.phrase, whole_word: filter.whole_word, - apply_to_mentions: filter.context.include?('notifications')) + apply_to_mentions: filter.context.include?('notifications') + ) end end end diff --git a/db/migrate/20190512200918_add_content_type_to_statuses.rb b/db/migrate/20190512200918_add_content_type_to_statuses.rb index efbe2caa7..31c1a4f17 100644 --- a/db/migrate/20190512200918_add_content_type_to_statuses.rb +++ b/db/migrate/20190512200918_add_content_type_to_statuses.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddContentTypeToStatuses < ActiveRecord::Migration[5.2] def change add_column :statuses, :content_type, :string diff --git a/db/migrate/20220209175231_add_content_type_to_status_edits.rb b/db/migrate/20220209175231_add_content_type_to_status_edits.rb index 0e4e52fcb..bb414535d 100644 --- a/db/migrate/20220209175231_add_content_type_to_status_edits.rb +++ b/db/migrate/20220209175231_add_content_type_to_status_edits.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddContentTypeToStatusEdits < ActiveRecord::Migration[6.1] def change add_column :status_edits, :content_type, :string diff --git a/db/post_migrate/20180813160548_post_migrate_filters.rb b/db/post_migrate/20180813160548_post_migrate_filters.rb index 588548c1d..82acf13d5 100644 --- a/db/post_migrate/20180813160548_post_migrate_filters.rb +++ b/db/post_migrate/20180813160548_post_migrate_filters.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class PostMigrateFilters < ActiveRecord::Migration[5.2] disable_ddl_transaction! @@ -5,7 +7,5 @@ class PostMigrateFilters < ActiveRecord::Migration[5.2] drop_table :glitch_keyword_mutes if table_exists? :glitch_keyword_mutes end - def down - end + def down; end end - diff --git a/lib/sanitize_ext/sanitize_config.rb b/lib/sanitize_ext/sanitize_config.rb index dfc586561..4c0e9b858 100644 --- a/lib/sanitize_ext/sanitize_config.rb +++ b/lib/sanitize_ext/sanitize_config.rb @@ -48,9 +48,9 @@ class Sanitize node.content = "[🖼 #{node['alt']}]" else url = node['href'] - prefix = url.match(/\Ahttps?:\/\/(www\.)?/).to_s + prefix = url.match(%r{\Ahttps?://(www\.)?}).to_s text = url[prefix.length, 30] - text = text + "…" if url[prefix.length..-1].length > 30 + text += '…' if url.length - prefix.length > 30 node.content = "[🖼 #{text}]" end end @@ -88,7 +88,7 @@ class Sanitize }, protocols: { - 'a' => { 'href' => LINK_PROTOCOLS }, + 'a' => { 'href' => LINK_PROTOCOLS }, 'blockquote' => { 'cite' => LINK_PROTOCOLS }, }, @@ -126,7 +126,7 @@ class Sanitize node = env[:node] - rel = (node['rel'] || '').split(' ') & ['tag'] + rel = (node['rel'] || '').split & ['tag'] rel += ['nofollow', 'noopener', 'noreferrer'] unless TagManager.instance.local_url?(node['href']) if rel.empty? diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake index e1102af33..76e190f70 100644 --- a/lib/tasks/assets.rake +++ b/lib/tasks/assets.rake @@ -3,14 +3,14 @@ namespace :assets do desc 'Generate static pages' task generate_static_pages: :environment do - class StaticApplicationController < ApplicationController - def current_user - nil + def render_static_page(action, dest:, **opts) + renderer = Class.new(ApplicationController) do + def current_user + nil + end end - end - def render_static_page(action, dest:, **opts) - html = StaticApplicationController.render(action, opts) + html = renderer.render(action, opts) File.write(dest, html) end diff --git a/lib/tasks/glitchsoc.rake b/lib/tasks/glitchsoc.rake index 79e864648..72558fa19 100644 --- a/lib/tasks/glitchsoc.rake +++ b/lib/tasks/glitchsoc.rake @@ -1,8 +1,12 @@ +# frozen_string_literal: true + namespace :glitchsoc do desc 'Backfill local-only flag on statuses table' task backfill_local_only: :environment do - Status.local.where(local_only: nil).find_each do |st| - ActiveRecord::Base.logger.silence { st.update_attribute(:local_only, st.marked_local_only?) } + Status.local.where(local_only: nil).find_each do |status| + ActiveRecord::Base.logger.silence do + status.update_attribute(:local_only, status.marked_local_only?) # rubocop:disable Rails/SkipsModelValidations + end end end end diff --git a/spec/controllers/api/v1/accounts/credentials_controller_spec.rb b/spec/controllers/api/v1/accounts/credentials_controller_spec.rb index de08dd524..a677aaad0 100644 --- a/spec/controllers/api/v1/accounts/credentials_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/credentials_controller_spec.rb @@ -75,10 +75,10 @@ describe Api::V1::Accounts::CredentialsController do end end - describe 'with invalid data' do + describe 'with a too long profile bio' do before do note = 'This is too long. ' - note = note + 'a' * (Account::MAX_NOTE_LENGTH - note.length + 1) + note += 'a' * (Account::MAX_NOTE_LENGTH - note.length + 1) patch :update, params: { note: note } end diff --git a/spec/controllers/api/v1/timelines/direct_controller_spec.rb b/spec/controllers/api/v1/timelines/direct_controller_spec.rb index a22c2cbea..def67a0fe 100644 --- a/spec/controllers/api/v1/timelines/direct_controller_spec.rb +++ b/spec/controllers/api/v1/timelines/direct_controller_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe Api::V1::Timelines::DirectController, type: :controller do +RSpec.describe Api::V1::Timelines::DirectController do let(:user) { Fabricate(:user) } let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read:statuses') } diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 1aabae0ea..82455d874 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -75,8 +75,8 @@ describe ApplicationController, type: :controller do describe 'helper_method :current_flavour' do it 'returns "glitch" when theme wasn\'t changed in admin settings' do - allow(Setting).to receive(:default_settings).and_return({'skin' => 'default'}) - allow(Setting).to receive(:default_settings).and_return({'flavour' => 'glitch'}) + allow(Setting).to receive(:default_settings).and_return({ 'skin' => 'default' }) + allow(Setting).to receive(:default_settings).and_return({ 'flavour' => 'glitch' }) expect(controller.view_context.current_flavour).to eq 'glitch' end diff --git a/spec/controllers/settings/flavours_controller_spec.rb b/spec/controllers/settings/flavours_controller_spec.rb index f89bde1f9..8c7d4a768 100644 --- a/spec/controllers/settings/flavours_controller_spec.rb +++ b/spec/controllers/settings/flavours_controller_spec.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true + require 'rails_helper' -RSpec.describe Settings::FlavoursController, type: :controller do +RSpec.describe Settings::FlavoursController do let(:user) { Fabricate(:user) } before do diff --git a/spec/lib/advanced_text_formatter_spec.rb b/spec/lib/advanced_text_formatter_spec.rb index c1e469606..8b27b56a1 100644 --- a/spec/lib/advanced_text_formatter_spec.rb +++ b/spec/lib/advanced_text_formatter_spec.rb @@ -1,12 +1,14 @@ +# frozen_string_literal: true + require 'rails_helper' RSpec.describe AdvancedTextFormatter do describe '#to_s' do + subject { described_class.new(text, preloaded_accounts: preloaded_accounts, content_type: content_type).to_s } + let(:preloaded_accounts) { nil } let(:content_type) { 'text/markdown' } - subject { described_class.new(text, preloaded_accounts: preloaded_accounts, content_type: content_type).to_s } - context 'given a markdown source' do let(:content_type) { 'text/markdown' } @@ -14,7 +16,7 @@ RSpec.describe AdvancedTextFormatter do let(:text) { 'text' } it 'paragraphizes the text' do - is_expected.to eq '

text

' + expect(subject).to eq '

text

' end end @@ -22,7 +24,7 @@ RSpec.describe AdvancedTextFormatter do let(:text) { "line\nfeed" } it 'removes line feeds' do - is_expected.not_to include "\n" + expect(subject).to_not include "\n" end end @@ -30,7 +32,7 @@ RSpec.describe AdvancedTextFormatter do let(:text) { 'test `foo` bar' } it 'formats code using ' do - is_expected.to include 'test foo bar' + expect(subject).to include 'test foo bar' end end @@ -38,15 +40,15 @@ RSpec.describe AdvancedTextFormatter do let(:text) { "test\n\n```\nint main(void) {\n return 0; // https://joinmastodon.org/foo\n}\n```\n" } it 'formats code using
 and ' do
-          is_expected.to include '
int main'
+          expect(subject).to include '
int main'
         end
 
         it 'does not strip leading spaces' do
-          is_expected.to include '>  return 0'
+          expect(subject).to include '>  return 0'
         end
 
         it 'does not format links' do
-          is_expected.to include 'return 0; // https://joinmastodon.org/foo'
+          expect(subject).to include 'return 0; // https://joinmastodon.org/foo'
         end
       end
 
@@ -54,7 +56,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'test `https://foo.bar/bar` bar' }
 
         it 'does not rewrite the link' do
-          is_expected.to include 'test https://foo.bar/bar bar'
+          expect(subject).to include 'test https://foo.bar/bar bar'
         end
       end
 
@@ -62,7 +64,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'foo https://cb6e6126.ngrok.io/about/more' }
 
         it 'creates a link' do
-          is_expected.to include '@alice'
+          expect(subject).to include '@alice'
         end
       end
 
@@ -80,7 +82,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { '@alice' }
 
         it 'does not create a mention link' do
-          is_expected.to include '@alice'
+          expect(subject).to include '@alice'
         end
       end
 
@@ -88,7 +90,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4' }
 
         it 'matches the full URL' do
-          is_expected.to include 'href="https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4"'
+          expect(subject).to include 'href="https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4"'
         end
       end
 
@@ -96,7 +98,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'http://google.com' }
 
         it 'matches the full URL' do
-          is_expected.to include 'href="http://google.com"'
+          expect(subject).to include 'href="http://google.com"'
         end
       end
 
@@ -104,7 +106,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'http://example.gay' }
 
         it 'matches the full URL' do
-          is_expected.to include 'href="http://example.gay"'
+          expect(subject).to include 'href="http://example.gay"'
         end
       end
 
@@ -112,11 +114,11 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'https://nic.みんな/' }
 
         it 'matches the full URL' do
-          is_expected.to include 'href="https://nic.みんな/"'
+          expect(subject).to include 'href="https://nic.みんな/"'
         end
 
         it 'has display URL' do
-          is_expected.to include 'nic.みんな/'
+          expect(subject).to include 'nic.みんな/'
         end
       end
 
@@ -124,7 +126,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona. ' }
 
         it 'matches the full URL but not the period' do
-          is_expected.to include 'href="http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona"'
+          expect(subject).to include 'href="http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona"'
         end
       end
 
@@ -132,7 +134,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { '(http://google.com/)' }
 
         it 'matches the full URL but not the parentheses' do
-          is_expected.to include 'href="http://google.com/"'
+          expect(subject).to include 'href="http://google.com/"'
         end
       end
 
@@ -140,7 +142,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'http://www.google.com!' }
 
         it 'matches the full URL but not the exclamation point' do
-          is_expected.to include 'href="http://www.google.com"'
+          expect(subject).to include 'href="http://www.google.com"'
         end
       end
 
@@ -148,7 +150,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { "http://www.google.com'" }
 
         it 'matches the full URL but not the single quote' do
-          is_expected.to include 'href="http://www.google.com"'
+          expect(subject).to include 'href="http://www.google.com"'
         end
       end
     end
@@ -157,7 +159,7 @@ RSpec.describe AdvancedTextFormatter do
       let(:text) { 'http://www.google.com>' }
 
       it 'matches the full URL but not the angle bracket' do
-        is_expected.to include 'href="http://www.google.com"'
+        expect(subject).to include 'href="http://www.google.com"'
       end
     end
 
@@ -166,7 +168,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink' }
 
         it 'matches the full URL' do
-          is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink"'
+          expect(subject).to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink"'
         end
       end
 
@@ -174,7 +176,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'https://www.ruby-toolbox.com/search?utf8=✓&q=autolink' }
 
         it 'matches the full URL' do
-          is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=✓&q=autolink"'
+          expect(subject).to include 'href="https://www.ruby-toolbox.com/search?utf8=✓&q=autolink"'
         end
       end
 
@@ -182,7 +184,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'https://www.ruby-toolbox.com/search?utf8=✓' }
 
         it 'matches the full URL' do
-          is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=✓"'
+          expect(subject).to include 'href="https://www.ruby-toolbox.com/search?utf8=✓"'
         end
       end
 
@@ -190,7 +192,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&utf81=✓&q=autolink' }
 
         it 'preserves escaped unicode characters' do
-          is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&utf81=✓&q=autolink"'
+          expect(subject).to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&utf81=✓&q=autolink"'
         end
       end
 
@@ -198,7 +200,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { 'https://en.wikipedia.org/wiki/Diaspora_(software)' }
 
         it 'matches the full URL' do
-          is_expected.to include 'href="https://en.wikipedia.org/wiki/Diaspora_(software)"'
+          expect(subject).to include 'href="https://en.wikipedia.org/wiki/Diaspora_(software)"'
         end
       end
 
@@ -206,7 +208,7 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { '"https://example.com/"' }
 
         it 'does not match the quotation marks' do
-          is_expected.to include 'href="https://example.com/"'
+          expect(subject).to include 'href="https://example.com/"'
         end
       end
 
@@ -214,19 +216,19 @@ RSpec.describe AdvancedTextFormatter do
         let(:text) { '' }
 
         it 'does not match the angle brackets' do
-          is_expected.to include 'href="https://example.com/"'
+          expect(subject).to include 'href="https://example.com/"'
         end
       end
 
       context 'given a URL containing unsafe code (XSS attack, invisible part)' do
-        let(:text) { %q{http://example.com/blahblahblahblah/a} }
+        let(:text) { 'http://example.com/blahblahblahblah/a' }
 
         it 'does not include the HTML in the URL' do
-          is_expected.to include '"http://example.com/blahblahblahblah/a"'
+          expect(subject).to include '"http://example.com/blahblahblahblah/a"'
         end
 
         it 'does not include a script tag' do
-          is_expected.to_not include '' }
 
         it 'does not include a script tag' do
-          is_expected.to_not include '