From 6883fddb19b1319a378aa5dc2034670c3b27ce39 Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 24 Jan 2023 19:40:21 +0100 Subject: Fix account activation being triggered before email confirmation (#23245) * Add tests * Fix account activation being triggered before email confirmation Fixes #23098 --- app/models/user.rb | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'app/models/user.rb') diff --git a/app/models/user.rb b/app/models/user.rb index 2a42ffaa1..d40044da3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -195,10 +195,16 @@ class User < ApplicationRecord super - if new_user && approved? - prepare_new_user! - elsif new_user - notify_staff_about_pending_account! + if new_user + # Avoid extremely unlikely race condition when approving and confirming + # the user at the same time + reload unless approved? + + if approved? + prepare_new_user! + else + notify_staff_about_pending_account! + end end end @@ -209,7 +215,13 @@ class User < ApplicationRecord skip_confirmation! save! - prepare_new_user! if new_user && approved? + if new_user + # Avoid extremely unlikely race condition when approving and confirming + # the user at the same time + reload unless approved? + + prepare_new_user! if approved? + end end def update_sign_in!(new_sign_in: false) @@ -260,7 +272,11 @@ class User < ApplicationRecord return if approved? update!(approved: true) - prepare_new_user! + + # Avoid extremely unlikely race condition when approving and confirming + # the user at the same time + reload unless confirmed? + prepare_new_user! if confirmed? end def otp_enabled? -- cgit From f68bb52556fe90d7adad8db9ba8b801a22281f30 Mon Sep 17 00:00:00 2001 From: Nick Schonning Date: Wed, 8 Feb 2023 01:07:36 -0500 Subject: Apply Rubocop Style/NegatedIfElseCondition (#23451) --- app/controllers/api/v1/streaming_controller.rb | 6 +++--- app/models/user.rb | 2 +- app/services/fetch_oembed_service.rb | 2 +- app/services/verify_link_service.rb | 2 +- lib/tasks/mastodon.rake | 12 ++++++------ 5 files changed, 12 insertions(+), 12 deletions(-) (limited to 'app/models/user.rb') diff --git a/app/controllers/api/v1/streaming_controller.rb b/app/controllers/api/v1/streaming_controller.rb index 7cd60615a..b23a60170 100644 --- a/app/controllers/api/v1/streaming_controller.rb +++ b/app/controllers/api/v1/streaming_controller.rb @@ -2,10 +2,10 @@ class Api::V1::StreamingController < Api::BaseController def index - if Rails.configuration.x.streaming_api_base_url != request.host - redirect_to streaming_api_url, status: 301 - else + if Rails.configuration.x.streaming_api_base_url == request.host not_found + else + redirect_to streaming_api_url, status: 301 end end diff --git a/app/models/user.rb b/app/models/user.rb index d40044da3..c767f8984 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -265,7 +265,7 @@ class User < ApplicationRecord end def inactive_message - !approved? ? :pending : super + approved? ? super : :pending end def approve! diff --git a/app/services/fetch_oembed_service.rb b/app/services/fetch_oembed_service.rb index 7d0879c79..9851ac098 100644 --- a/app/services/fetch_oembed_service.rb +++ b/app/services/fetch_oembed_service.rb @@ -82,7 +82,7 @@ class FetchOEmbedService return if @endpoint_url.blank? body = Request.new(:get, @endpoint_url).perform do |res| - res.code != 200 ? nil : res.body_with_limit + res.code == 200 ? res.body_with_limit : nil end validate(parse_for_format(body)) if body.present? diff --git a/app/services/verify_link_service.rb b/app/services/verify_link_service.rb index f83a664d4..707aeb4e0 100644 --- a/app/services/verify_link_service.rb +++ b/app/services/verify_link_service.rb @@ -19,7 +19,7 @@ class VerifyLinkService < BaseService def perform_request! @body = Request.new(:get, @url).add_headers('Accept' => 'text/html').perform do |res| - res.code != 200 ? nil : res.body_with_limit + res.code == 200 ? res.body_with_limit : nil end end diff --git a/lib/tasks/mastodon.rake b/lib/tasks/mastodon.rake index 876383d7f..1184e5273 100644 --- a/lib/tasks/mastodon.rake +++ b/lib/tasks/mastodon.rake @@ -463,10 +463,10 @@ namespace :mastodon do prompt.say 'Running `RAILS_ENV=production rails db:setup` ...' prompt.say "\n\n" - if !system(env.transform_values(&:to_s).merge({ 'RAILS_ENV' => 'production', 'SAFETY_ASSURED' => '1' }), 'rails db:setup') - prompt.error 'That failed! Perhaps your configuration is not right' - else + if system(env.transform_values(&:to_s).merge({ 'RAILS_ENV' => 'production', 'SAFETY_ASSURED' => '1' }), 'rails db:setup') prompt.ok 'Done!' + else + prompt.error 'That failed! Perhaps your configuration is not right' end end @@ -479,10 +479,10 @@ namespace :mastodon do prompt.say 'Running `RAILS_ENV=production rails assets:precompile` ...' prompt.say "\n\n" - if !system(env.transform_values(&:to_s).merge({ 'RAILS_ENV' => 'production' }), 'rails assets:precompile') - prompt.error 'That failed! Maybe you need swap space?' - else + if system(env.transform_values(&:to_s).merge({ 'RAILS_ENV' => 'production' }), 'rails assets:precompile') prompt.say 'Done!' + else + prompt.error 'That failed! Maybe you need swap space?' end end end -- 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/user.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 86f8aa2db2bf4f19f482b5857c4e49b8791eb514 Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 6 Mar 2023 16:00:08 +0100 Subject: Fix unconfirmed accounts being registered as active users (#23803) --- app/models/user.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'app/models/user.rb') diff --git a/app/models/user.rb b/app/models/user.rb index 5e106dee5..d56a9b9ca 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -507,11 +507,14 @@ class User < ApplicationRecord def prepare_new_user! BootstrapTimelineWorker.perform_async(account_id) ActivityTracker.increment('activity:accounts:local') + ActivityTracker.record('activity:logins', id) UserMailer.welcome(self).deliver_later TriggerWebhookWorker.perform_async('account.approved', 'Account', account_id) end def prepare_returning_user! + return unless confirmed? + ActivityTracker.record('activity:logins', id) regenerate_feed! if needs_feed_update? end -- cgit From a9b5598c97fc4d3302b61b260097ef41c2ebe377 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 30 Mar 2023 14:44:00 +0200 Subject: Change user settings to be stored in a more optimal way (#23630) Co-authored-by: Claire --- .rubocop.yml | 8 ++ .rubocop_todo.yml | 1 + .../api/v1/accounts/credentials_controller.rb | 12 +- app/controllers/settings/preferences_controller.rb | 40 +----- app/lib/user_settings_decorator.rb | 155 --------------------- app/lib/user_settings_serializer.rb | 19 +++ app/models/concerns/has_user_settings.rb | 141 +++++++++++++++++++ app/models/user.rb | 59 +------- app/models/user_settings.rb | 99 +++++++++++++ app/models/user_settings/dsl.rb | 37 +++++ app/models/user_settings/glue.rb | 23 +++ app/models/user_settings/namespace.rb | 21 +++ app/models/user_settings/setting.rb | 48 +++++++ app/services/notify_service.rb | 13 +- .../settings/preferences/appearance/show.html.haml | 65 ++++----- .../preferences/notifications/show.html.haml | 36 ++--- .../settings/preferences/other/show.html.haml | 29 ++-- config/initializers/inflections.rb | 1 + config/settings.yml | 34 ----- db/migrate/20230215074327_add_settings_to_users.rb | 7 + db/migrate/20230215074423_move_user_settings.rb | 84 +++++++++++ db/schema.rb | 3 +- lib/tasks/tests.rake | 2 +- .../api/v1/accounts/credentials_controller_spec.rb | 1 + spec/controllers/application_controller_spec.rb | 10 +- .../preferences/notifications_controller_spec.rb | 14 +- .../settings/preferences/other_controller_spec.rb | 14 +- spec/lib/settings/extend_spec.rb | 16 --- spec/lib/settings/scoped_settings_spec.rb | 35 ----- spec/lib/user_settings_decorator_spec.rb | 84 ----------- spec/models/user_settings/namespace_spec.rb | 25 ++++ spec/models/user_settings/setting_spec.rb | 74 ++++++++++ spec/models/user_settings_spec.rb | 110 +++++++++++++++ spec/models/user_spec.rb | 14 +- spec/services/notify_service_spec.rb | 7 +- spec/services/report_service_spec.rb | 3 +- 36 files changed, 818 insertions(+), 526 deletions(-) delete mode 100644 app/lib/user_settings_decorator.rb create mode 100644 app/lib/user_settings_serializer.rb create mode 100644 app/models/concerns/has_user_settings.rb create mode 100644 app/models/user_settings.rb create mode 100644 app/models/user_settings/dsl.rb create mode 100644 app/models/user_settings/glue.rb create mode 100644 app/models/user_settings/namespace.rb create mode 100644 app/models/user_settings/setting.rb create mode 100644 db/migrate/20230215074327_add_settings_to_users.rb create mode 100644 db/migrate/20230215074423_move_user_settings.rb delete mode 100644 spec/lib/settings/extend_spec.rb delete mode 100644 spec/lib/settings/scoped_settings_spec.rb delete mode 100644 spec/lib/user_settings_decorator_spec.rb create mode 100644 spec/models/user_settings/namespace_spec.rb create mode 100644 spec/models/user_settings/setting_spec.rb create mode 100644 spec/models/user_settings_spec.rb (limited to 'app/models/user.rb') diff --git a/.rubocop.yml b/.rubocop.yml index 1033db92d..b5598b65a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -185,3 +185,11 @@ Style/TrailingCommaInHashLiteral: Style/SymbolArray: Enabled: false + +# Reason: Prefer less intendation in conditional assignments +# https://docs.rubocop.org/rubocop/cops_style.html#styleredundantbegin +Style/RedundantBegin: + Enabled: false + +RSpec/NamedSubject: + EnforcedStyle: named_only diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 64a6b6b33..4ad266c8c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -259,6 +259,7 @@ Metrics/ModuleLength: - 'app/helpers/jsonld_helper.rb' - 'app/helpers/statuses_helper.rb' - 'app/models/concerns/account_interactions.rb' + - 'app/models/concerns/has_user_settings.rb' # Configuration parameters: Max, CountKeywordArgs, MaxOptionalParameters. Metrics/ParameterLists: diff --git a/app/controllers/api/v1/accounts/credentials_controller.rb b/app/controllers/api/v1/accounts/credentials_controller.rb index 94b707771..7c7d70fd3 100644 --- a/app/controllers/api/v1/accounts/credentials_controller.rb +++ b/app/controllers/api/v1/accounts/credentials_controller.rb @@ -13,7 +13,7 @@ class Api::V1::Accounts::CredentialsController < Api::BaseController def update @account = current_account UpdateAccountService.new.call(@account, account_params, raise_error: true) - UserSettingsDecorator.new(current_user).update(user_settings_params) if user_settings_params + current_user.update(user_params) if user_params ActivityPub::UpdateDistributionWorker.perform_async(@account.id) render json: @account, serializer: REST::CredentialAccountSerializer end @@ -34,15 +34,17 @@ class Api::V1::Accounts::CredentialsController < Api::BaseController ) end - def user_settings_params + def user_params return nil if params[:source].blank? source_params = params.require(:source) { - 'setting_default_privacy' => source_params.fetch(:privacy, @account.user.setting_default_privacy), - 'setting_default_sensitive' => source_params.fetch(:sensitive, @account.user.setting_default_sensitive), - 'setting_default_language' => source_params.fetch(:language, @account.user.setting_default_language), + settings_attributes: { + default_privacy: source_params.fetch(:privacy, @account.user.setting_default_privacy), + default_sensitive: source_params.fetch(:sensitive, @account.user.setting_default_sensitive), + default_language: source_params.fetch(:language, @account.user.setting_default_language), + }, } end end diff --git a/app/controllers/settings/preferences_controller.rb b/app/controllers/settings/preferences_controller.rb index f5d5c1244..281deb64d 100644 --- a/app/controllers/settings/preferences_controller.rb +++ b/app/controllers/settings/preferences_controller.rb @@ -4,8 +4,6 @@ class Settings::PreferencesController < Settings::BaseController def show; end def update - user_settings.update(user_settings_params.to_h) - if current_user.update(user_params) I18n.locale = current_user.locale redirect_to after_update_redirect_path, notice: I18n.t('generic.changes_saved_msg') @@ -20,43 +18,7 @@ class Settings::PreferencesController < Settings::BaseController settings_preferences_path end - def user_settings - UserSettingsDecorator.new(current_user) - end - def user_params - params.require(:user).permit( - :locale, - chosen_languages: [] - ) - end - - def user_settings_params - params.require(:user).permit( - :setting_default_privacy, - :setting_default_sensitive, - :setting_default_language, - :setting_unfollow_modal, - :setting_boost_modal, - :setting_delete_modal, - :setting_auto_play_gif, - :setting_display_media, - :setting_expand_spoilers, - :setting_reduce_motion, - :setting_disable_swiping, - :setting_system_font_ui, - :setting_noindex, - :setting_theme, - :setting_aggregate_reblogs, - :setting_show_application, - :setting_advanced_layout, - :setting_use_blurhash, - :setting_use_pending_items, - :setting_trends, - :setting_crop_images, - :setting_always_send_emails, - notification_emails: %i(follow follow_request reblog favourite mention report pending_account trending_tag appeal), - interactions: %i(must_be_follower must_be_following must_be_following_dm) - ) + params.require(:user).permit(:locale, chosen_languages: [], settings_attributes: UserSettings.keys) end end diff --git a/app/lib/user_settings_decorator.rb b/app/lib/user_settings_decorator.rb deleted file mode 100644 index 5fb7655a9..000000000 --- a/app/lib/user_settings_decorator.rb +++ /dev/null @@ -1,155 +0,0 @@ -# frozen_string_literal: true - -class UserSettingsDecorator - attr_reader :user, :settings - - def initialize(user) - @user = user - end - - def update(settings) - @settings = settings - process_update - end - - private - - def process_update - user.settings['notification_emails'] = merged_notification_emails if change?('notification_emails') - user.settings['interactions'] = merged_interactions if change?('interactions') - user.settings['default_privacy'] = default_privacy_preference if change?('setting_default_privacy') - user.settings['default_sensitive'] = default_sensitive_preference if change?('setting_default_sensitive') - user.settings['default_language'] = default_language_preference if change?('setting_default_language') - user.settings['unfollow_modal'] = unfollow_modal_preference if change?('setting_unfollow_modal') - user.settings['boost_modal'] = boost_modal_preference if change?('setting_boost_modal') - user.settings['delete_modal'] = delete_modal_preference if change?('setting_delete_modal') - user.settings['auto_play_gif'] = auto_play_gif_preference if change?('setting_auto_play_gif') - user.settings['display_media'] = display_media_preference if change?('setting_display_media') - user.settings['expand_spoilers'] = expand_spoilers_preference if change?('setting_expand_spoilers') - user.settings['reduce_motion'] = reduce_motion_preference if change?('setting_reduce_motion') - user.settings['disable_swiping'] = disable_swiping_preference if change?('setting_disable_swiping') - user.settings['system_font_ui'] = system_font_ui_preference if change?('setting_system_font_ui') - user.settings['noindex'] = noindex_preference if change?('setting_noindex') - user.settings['theme'] = theme_preference if change?('setting_theme') - user.settings['aggregate_reblogs'] = aggregate_reblogs_preference if change?('setting_aggregate_reblogs') - user.settings['show_application'] = show_application_preference if change?('setting_show_application') - user.settings['advanced_layout'] = advanced_layout_preference if change?('setting_advanced_layout') - user.settings['use_blurhash'] = use_blurhash_preference if change?('setting_use_blurhash') - user.settings['use_pending_items'] = use_pending_items_preference if change?('setting_use_pending_items') - user.settings['trends'] = trends_preference if change?('setting_trends') - user.settings['crop_images'] = crop_images_preference if change?('setting_crop_images') - user.settings['always_send_emails'] = always_send_emails_preference if change?('setting_always_send_emails') - end - - def merged_notification_emails - user.settings['notification_emails'].merge coerced_settings('notification_emails').to_h - end - - def merged_interactions - user.settings['interactions'].merge coerced_settings('interactions').to_h - end - - def default_privacy_preference - settings['setting_default_privacy'] - end - - def default_sensitive_preference - boolean_cast_setting 'setting_default_sensitive' - end - - def unfollow_modal_preference - boolean_cast_setting 'setting_unfollow_modal' - end - - def boost_modal_preference - boolean_cast_setting 'setting_boost_modal' - end - - def delete_modal_preference - boolean_cast_setting 'setting_delete_modal' - end - - def system_font_ui_preference - boolean_cast_setting 'setting_system_font_ui' - end - - def auto_play_gif_preference - boolean_cast_setting 'setting_auto_play_gif' - end - - def display_media_preference - settings['setting_display_media'] - end - - def expand_spoilers_preference - boolean_cast_setting 'setting_expand_spoilers' - end - - def reduce_motion_preference - boolean_cast_setting 'setting_reduce_motion' - end - - def disable_swiping_preference - boolean_cast_setting 'setting_disable_swiping' - end - - def noindex_preference - boolean_cast_setting 'setting_noindex' - end - - def show_application_preference - boolean_cast_setting 'setting_show_application' - end - - def theme_preference - settings['setting_theme'] - end - - def default_language_preference - settings['setting_default_language'] - end - - def aggregate_reblogs_preference - boolean_cast_setting 'setting_aggregate_reblogs' - end - - def advanced_layout_preference - boolean_cast_setting 'setting_advanced_layout' - end - - def use_blurhash_preference - boolean_cast_setting 'setting_use_blurhash' - end - - def use_pending_items_preference - boolean_cast_setting 'setting_use_pending_items' - end - - def trends_preference - boolean_cast_setting 'setting_trends' - end - - def crop_images_preference - boolean_cast_setting 'setting_crop_images' - end - - def always_send_emails_preference - boolean_cast_setting 'setting_always_send_emails' - end - - def boolean_cast_setting(key) - ActiveModel::Type::Boolean.new.cast(settings[key]) - end - - def coerced_settings(key) - coerce_values settings.fetch(key, {}) - end - - def coerce_values(params_hash) - params_hash.transform_values { |x| ActiveModel::Type::Boolean.new.cast(x) } - end - - def change?(key) - !settings[key].nil? - end -end diff --git a/app/lib/user_settings_serializer.rb b/app/lib/user_settings_serializer.rb new file mode 100644 index 000000000..10d1be04d --- /dev/null +++ b/app/lib/user_settings_serializer.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class UserSettingsSerializer + def self.load(value) + json = begin + if value.blank? + {} + else + Oj.load(value, symbol_keys: true) + end + end + + UserSettings.new(json) + end + + def self.dump(value) + Oj.dump(value.as_json) + end +end diff --git a/app/models/concerns/has_user_settings.rb b/app/models/concerns/has_user_settings.rb new file mode 100644 index 000000000..b3fa1f683 --- /dev/null +++ b/app/models/concerns/has_user_settings.rb @@ -0,0 +1,141 @@ +# frozen_string_literal: true + +module HasUserSettings + extend ActiveSupport::Concern + + included do + serialize :settings, UserSettingsSerializer + end + + def settings_attributes=(attributes) + settings.update(attributes) + end + + def prefers_noindex? + settings['noindex'] + end + + def preferred_posting_language + valid_locale_cascade(settings['default_language'], locale, I18n.locale) + end + + def setting_auto_play_gif + settings['web.auto_play'] + end + + def setting_default_sensitive + settings['default_sensitive'] + end + + def setting_unfollow_modal + settings['web.unfollow_modal'] + end + + def setting_boost_modal + settings['web.reblog_modal'] + end + + def setting_delete_modal + settings['web.delete_modal'] + end + + def setting_reduce_motion + settings['web.reduce_motion'] + end + + def setting_system_font_ui + settings['web.use_system_font'] + end + + def setting_noindex + settings['noindex'] + end + + def setting_theme + settings['theme'] + end + + def setting_display_media + settings['web.display_media'] + end + + def setting_expand_spoilers + settings['web.expand_content_warnings'] + end + + def setting_default_language + settings['default_language'] + end + + def setting_aggregate_reblogs + settings['aggregate_reblogs'] + end + + def setting_show_application + settings['show_application'] + end + + def setting_advanced_layout + settings['web.advanced_layout'] + end + + def setting_use_blurhash + settings['web.use_blurhash'] + end + + def setting_use_pending_items + settings['web.use_pending_items'] + end + + def setting_trends + settings['web.trends'] + end + + def setting_crop_images + settings['web.crop_images'] + end + + def setting_disable_swiping + settings['web.disable_swiping'] + end + + def setting_always_send_emails + settings['always_send_emails'] + end + + def setting_default_privacy + settings['default_privacy'] || (account.locked? ? 'private' : 'public') + end + + def allows_report_emails? + settings['notification_emails.report'] + end + + def allows_pending_account_emails? + settings['notification_emails.pending_account'] + end + + def allows_appeal_emails? + settings['notification_emails.appeal'] + end + + def allows_trends_review_emails? + settings['notification_emails.trends'] + end + + def aggregates_reblogs? + settings['aggregate_reblogs'] + end + + def shows_application? + settings['show_application'] + end + + def show_all_media? + settings['web.display_media'] == 'show_all' + end + + def hide_all_media? + settings['web.display_media'] == 'hide_all' + end +end diff --git a/app/models/user.rb b/app/models/user.rb index d56a9b9ca..9b225d75f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -39,10 +39,11 @@ # webauthn_id :string # sign_up_ip :inet # role_id :bigint(8) +# settings :text # class User < ApplicationRecord - self.ignored_columns = %w( + self.ignored_columns += %w( remember_created_at remember_token current_sign_in_ip @@ -51,9 +52,9 @@ class User < ApplicationRecord filtered_languages ) - include Settings::Extend include Redisable include LanguagesHelper + include HasUserSettings # The home and list feeds will be stored in Redis for this amount # of time, and status fan-out to followers will include only people @@ -132,13 +133,6 @@ class User < ApplicationRecord has_many :session_activations, dependent: :destroy - delegate :auto_play_gif, :default_sensitive, :unfollow_modal, :boost_modal, :delete_modal, - :reduce_motion, :system_font_ui, :noindex, :theme, :display_media, - :expand_spoilers, :default_language, :aggregate_reblogs, :show_application, - :advanced_layout, :use_blurhash, :use_pending_items, :trends, :crop_images, - :disable_swiping, :always_send_emails, - to: :settings, prefix: :setting, allow_nil: false - delegate :can?, to: :role attr_reader :invite_code @@ -302,42 +296,6 @@ class User < ApplicationRecord save! end - def prefers_noindex? - setting_noindex - end - - def preferred_posting_language - valid_locale_cascade(settings.default_language, locale, I18n.locale) - end - - def setting_default_privacy - settings.default_privacy || (account.locked? ? 'private' : 'public') - end - - def allows_report_emails? - settings.notification_emails['report'] - end - - def allows_pending_account_emails? - settings.notification_emails['pending_account'] - end - - def allows_appeal_emails? - settings.notification_emails['appeal'] - end - - def allows_trends_review_emails? - settings.notification_emails['trending_tag'] - end - - def aggregates_reblogs? - @aggregates_reblogs ||= settings.aggregate_reblogs - end - - def shows_application? - @shows_application ||= settings.show_application - end - def token_for_app(app) return nil if app.nil? || app.owner != self @@ -417,14 +375,6 @@ class User < ApplicationRecord send_reset_password_instructions end - def show_all_media? - setting_display_media == 'show_all' - end - - def hide_all_media? - setting_display_media == 'hide_all' - end - protected def send_devise_notification(notification, *args, **kwargs) @@ -494,7 +444,8 @@ class User < ApplicationRecord def sanitize_languages return if chosen_languages.nil? - chosen_languages.reject!(&:blank?) + chosen_languages.compact_blank! + self.chosen_languages = nil if chosen_languages.empty? end diff --git a/app/models/user_settings.rb b/app/models/user_settings.rb new file mode 100644 index 000000000..2c025d6c5 --- /dev/null +++ b/app/models/user_settings.rb @@ -0,0 +1,99 @@ +# frozen_string_literal: true + +class UserSettings + class Error < StandardError; end + class KeyError < Error; end + + include UserSettings::DSL + include UserSettings::Glue + + setting :always_send_emails, default: false + setting :aggregate_reblogs, default: true + setting :theme, default: -> { ::Setting.theme } + setting :noindex, default: -> { ::Setting.noindex } + setting :show_application, default: true + setting :default_language, default: nil + setting :default_sensitive, default: false + setting :default_privacy, default: nil + + namespace :web do + setting :crop_images, default: true + setting :advanced_layout, default: false + setting :trends, default: true + setting :use_blurhash, default: true + setting :use_pending_items, default: false + setting :use_system_font, default: false + setting :disable_swiping, default: false + setting :delete_modal, default: true + setting :reblog_modal, default: false + setting :unfollow_modal, default: true + setting :reduce_motion, default: false + setting :expand_content_warnings, default: false + setting :display_media, default: 'default', in: %w(default show_all hide_all) + setting :auto_play, default: false + end + + namespace :notification_emails do + setting :follow, default: true + setting :reblog, default: false + setting :favourite, default: false + setting :mention, default: true + setting :follow_request, default: true + setting :report, default: true + setting :pending_account, default: true + setting :trends, default: true + setting :appeal, default: true + end + + namespace :interactions do + setting :must_be_follower, default: false + setting :must_be_following, default: false + setting :must_be_following_dm, default: false + end + + def initialize(original_hash) + @original_hash = original_hash || {} + end + + def [](key) + key = key.to_sym + + raise KeyError, "Undefined setting: #{key}" unless self.class.definition_for?(key) + + if @original_hash.key?(key) + @original_hash[key] + else + self.class.definition_for(key).default_value + end + end + + def []=(key, value) + key = key.to_sym + + raise KeyError, "Undefined setting: #{key}" unless self.class.definition_for?(key) + + typecast_value = self.class.definition_for(key).type_cast(value) + + if typecast_value.nil? + @original_hash.delete(key) + else + @original_hash[key] = typecast_value + end + end + + def update(params) + params.each do |k, v| + self[k] = v unless v.nil? + end + end + + keys.each do |key| + define_method(key) do + self[key] + end + end + + def as_json + @original_hash + end +end diff --git a/app/models/user_settings/dsl.rb b/app/models/user_settings/dsl.rb new file mode 100644 index 000000000..26238bbbe --- /dev/null +++ b/app/models/user_settings/dsl.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module UserSettings::DSL + module ClassMethods + def setting(key, options = {}) + @definitions ||= {} + + UserSettings::Setting.new(key, options).tap do |s| + @definitions[s.key] = s + end + end + + def namespace(key, &block) + @definitions ||= {} + + UserSettings::Namespace.new(key).configure(&block).tap do |n| + @definitions.merge!(n.definitions) + end + end + + def keys + @definitions.keys + end + + def definition_for(key) + @definitions[key.to_sym] + end + + def definition_for?(key) + @definitions.key?(key.to_sym) + end + end + + def self.included(base) + base.extend ClassMethods + end +end diff --git a/app/models/user_settings/glue.rb b/app/models/user_settings/glue.rb new file mode 100644 index 000000000..02066a411 --- /dev/null +++ b/app/models/user_settings/glue.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module UserSettings::Glue + def to_model + self + end + + def to_key + '' + end + + def persisted? + false + end + + def type_for_attribute(key) + self.class.definition_for(key)&.type + end + + def has_attribute?(key) # rubocop:disable Naming/PredicateName + self.class.definition_for?(key) + end +end diff --git a/app/models/user_settings/namespace.rb b/app/models/user_settings/namespace.rb new file mode 100644 index 000000000..b8f7e092e --- /dev/null +++ b/app/models/user_settings/namespace.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class UserSettings::Namespace + attr_reader :name, :definitions + + def initialize(name) + @name = name.to_sym + @definitions = {} + end + + def configure(&block) + instance_eval(&block) + self + end + + def setting(key, options = {}) + UserSettings::Setting.new(key, options.merge(namespace: name)).tap do |s| + @definitions[s.key] = s + end + end +end diff --git a/app/models/user_settings/setting.rb b/app/models/user_settings/setting.rb new file mode 100644 index 000000000..c359c593b --- /dev/null +++ b/app/models/user_settings/setting.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +class UserSettings::Setting + attr_reader :name, :namespace, :in + + def initialize(name, options = {}) + @name = name.to_sym + @default_value = options[:default] + @namespace = options[:namespace] + @in = options[:in] + end + + def default_value + if @default_value.respond_to?(:call) + @default_value.call + else + @default_value + end + end + + def type + if @default_value.is_a?(TrueClass) || @default_value.is_a?(FalseClass) + ActiveModel::Type::Boolean.new + else + ActiveModel::Type::String.new + end + end + + def type_cast(value) + if type.respond_to?(:cast) + type.cast(value) + else + value + end + end + + def to_a + [key, default_value] + end + + def key + if namespace + "#{namespace}.#{name}".to_sym + else + name + end + end +end diff --git a/app/services/notify_service.rb b/app/services/notify_service.rb index 4c7acbcac..994ca588a 100644 --- a/app/services/notify_service.rb +++ b/app/services/notify_service.rb @@ -3,6 +3,11 @@ class NotifyService < BaseService include Redisable + NON_EMAIL_TYPES = %i( + admin.report + admin.sign_up + ).freeze + def call(recipient, type, activity) @recipient = recipient @activity = activity @@ -36,11 +41,11 @@ class NotifyService < BaseService end def optional_non_follower? - @recipient.user.settings.interactions['must_be_follower'] && !@notification.from_account.following?(@recipient) + @recipient.user.settings['interactions.must_be_follower'] && !@notification.from_account.following?(@recipient) end def optional_non_following? - @recipient.user.settings.interactions['must_be_following'] && !following_sender? + @recipient.user.settings['interactions.must_be_following'] && !following_sender? end def message? @@ -82,7 +87,7 @@ class NotifyService < BaseService def optional_non_following_and_direct? direct_message? && - @recipient.user.settings.interactions['must_be_following_dm'] && + @recipient.user.settings['interactions.must_be_following_dm'] && !following_sender? && !response_to_recipient? end @@ -171,6 +176,6 @@ class NotifyService < BaseService end def send_email_for_notification_type? - @recipient.user.settings.notification_emails[@notification.type.to_s] + NON_EMAIL_TYPES.exclude?(@notification.type) && @recipient.user.settings["notification_emails.#{@notification.type}"] end end diff --git a/app/views/settings/preferences/appearance/show.html.haml b/app/views/settings/preferences/appearance/show.html.haml index bc23df647..5358310e5 100644 --- a/app/views/settings/preferences/appearance/show.html.haml +++ b/app/views/settings/preferences/appearance/show.html.haml @@ -9,57 +9,58 @@ .fields-group.fields-row__column.fields-row__column-6 = f.input :locale, collection: I18n.available_locales, wrapper: :with_label, include_blank: false, label_method: lambda { |locale| native_locale_name(locale) }, selected: I18n.locale, hint: false .fields-group.fields-row__column.fields-row__column-6 - = f.input :setting_theme, collection: Themes.instance.names, label_method: lambda { |theme| I18n.t("themes.#{theme}", default: theme) }, wrapper: :with_label, include_blank: false, hint: false + = f.simple_fields_for :settings, current_user.settings do |ff| + = ff.input :theme, collection: Themes.instance.names, label_method: lambda { |theme| I18n.t("themes.#{theme}", default: theme) }, wrapper: :with_label, include_blank: false, hint: false - unless I18n.locale == :en .flash-message.translation-prompt #{t 'appearance.localization.body'} #{content_tag(:a, t('appearance.localization.guide_link_text'), href: t('appearance.localization.guide_link'), target: '_blank', rel: 'noopener')} - %h4= t 'appearance.advanced_web_interface' + = f.simple_fields_for :settings, current_user.settings do |ff| + %h4= t 'appearance.advanced_web_interface' - %p.hint= t 'appearance.advanced_web_interface_hint' + %p.hint= t 'appearance.advanced_web_interface_hint' - .fields-group - = f.input :setting_advanced_layout, as: :boolean, wrapper: :with_label, hint: false + .fields-group + = ff.input :'web.advanced_layout', wrapper: :with_label, hint: false, label: I18n.t('simple_form.labels.defaults.setting_advanced_layout') + %h4= t 'appearance.animations_and_accessibility' - %h4= t 'appearance.animations_and_accessibility' + .fields-group + = ff.input :'web.use_pending_items', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_use_pending_items'), hint: I18n.t('simple_form.hints.defaults.setting_use_pending_items') - .fields-group - = f.input :setting_use_pending_items, as: :boolean, wrapper: :with_label + .fields-group + = ff.input :'web.auto_play', wrapper: :with_label, recommended: true, label: I18n.t('simple_form.labels.defaults.setting_auto_play_gif') + = ff.input :'web.reduce_motion', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_reduce_motion') + = ff.input :'web.disable_swiping', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_disable_swiping') + = ff.input :'web.use_system_font', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_system_font_ui') - .fields-group - = f.input :setting_auto_play_gif, as: :boolean, wrapper: :with_label, recommended: true - = f.input :setting_reduce_motion, as: :boolean, wrapper: :with_label - = f.input :setting_disable_swiping, as: :boolean, wrapper: :with_label - = f.input :setting_system_font_ui, as: :boolean, wrapper: :with_label + %h4= t 'appearance.toot_layout' - %h4= t 'appearance.toot_layout' + .fields-group + = ff.input :'web.crop_images', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_crop_images') - .fields-group - = f.input :setting_crop_images, as: :boolean, wrapper: :with_label + %h4= t 'appearance.discovery' - %h4= t 'appearance.discovery' + .fields-group + = ff.input :'web.trends', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_trends') - .fields-group - = f.input :setting_trends, as: :boolean, wrapper: :with_label + %h4= t 'appearance.confirmation_dialogs' - %h4= t 'appearance.confirmation_dialogs' + .fields-group + = ff.input :'web.unfollow_modal', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_unfollow_modal') + = ff.input :'web.reblog_modal', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_boost_modal') + = ff.input :'web.delete_modal', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_delete_modal') - .fields-group - = f.input :setting_unfollow_modal, as: :boolean, wrapper: :with_label - = f.input :setting_boost_modal, as: :boolean, wrapper: :with_label - = f.input :setting_delete_modal, as: :boolean, wrapper: :with_label + %h4= t 'appearance.sensitive_content' - %h4= t 'appearance.sensitive_content' + .fields-group + = ff.input :'web.display_media', collection: ['default', 'show_all', 'hide_all'],label_method: lambda { |item| t("simple_form.hints.defaults.setting_display_media_#{item}") }, hint: false, as: :radio_buttons, collection_wrapper_tag: 'ul', item_wrapper_tag: 'li', wrapper: :with_floating_label, label: I18n.t('simple_form.labels.defaults.setting_display_media') - .fields-group - = f.input :setting_display_media, collection: ['default', 'show_all', 'hide_all'], label_method: lambda { |item| t("simple_form.hints.defaults.setting_display_media_#{item}") }, hint: false, as: :radio_buttons, collection_wrapper_tag: 'ul', item_wrapper_tag: 'li', wrapper: :with_floating_label + .fields-group + = ff.input :'web.use_blurhash', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_use_blurhash'), hint: I18n.t('simple_form.hints.defaults.setting_use_blurhash') - .fields-group - = f.input :setting_use_blurhash, as: :boolean, wrapper: :with_label - - .fields-group - = f.input :setting_expand_spoilers, as: :boolean, wrapper: :with_label + .fields-group + = ff.input :'web.expand_content_warnings', wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_expand_spoilers') .actions = f.button :button, t('generic.save_changes'), type: :submit diff --git a/app/views/settings/preferences/notifications/show.html.haml b/app/views/settings/preferences/notifications/show.html.haml index f00dbadd4..cb1ad0886 100644 --- a/app/views/settings/preferences/notifications/show.html.haml +++ b/app/views/settings/preferences/notifications/show.html.haml @@ -11,25 +11,25 @@ %p.hint= t 'notifications.email_events_hint' - .fields-group - = f.simple_fields_for :notification_emails, hash_to_object(current_user.settings.notification_emails) do |ff| - = ff.input :follow, as: :boolean, wrapper: :with_label - = ff.input :follow_request, as: :boolean, wrapper: :with_label - = ff.input :reblog, as: :boolean, wrapper: :with_label - = ff.input :favourite, as: :boolean, wrapper: :with_label - = ff.input :mention, as: :boolean, wrapper: :with_label - = ff.input :report, as: :boolean, wrapper: :with_label if current_user.can?(:manage_reports) - = ff.input :appeal, as: :boolean, wrapper: :with_label if current_user.can?(:manage_appeals) - = ff.input :pending_account, as: :boolean, wrapper: :with_label if current_user.can?(:manage_users) - = ff.input :trending_tag, as: :boolean, wrapper: :with_label if current_user.can?(:manage_taxonomies) - - .fields-group - = f.input :setting_always_send_emails, as: :boolean, wrapper: :with_label + = f.simple_fields_for :settings, current_user.settings do |ff| + .fields-group + = ff.input :'notification_emails.follow', wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.follow') + = ff.input :'notification_emails.follow_request', wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.follow_request') + = ff.input :'notification_emails.reblog', wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.reblog') + = ff.input :'notification_emails.favourite', wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.favourite') + = ff.input :'notification_emails.mention', wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.mention') + = ff.input :'notification_emails.report', wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.report') if current_user.can?(:manage_reports) + = ff.input :'notification_emails.appeal', as: :boolean, wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.appeal') if current_user.can?(:manage_appeals) + = ff.input :'notification_emails.pending_account', wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.pending_account') if current_user.can?(:manage_users) + = ff.input :'notification_emails.trends', wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.trending_tag') if current_user.can?(:manage_taxonomies) + + .fields-group + = ff.input :always_send_emails, wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_always_send_emails'), hint: I18n.t('simple_form.hints.defaults.setting_always_send_emails') %h4= t 'notifications.other_settings' .fields-group - = f.simple_fields_for :interactions, hash_to_object(current_user.settings.interactions) do |ff| - = ff.input :must_be_follower, as: :boolean, wrapper: :with_label - = ff.input :must_be_following, as: :boolean, wrapper: :with_label - = ff.input :must_be_following_dm, as: :boolean, wrapper: :with_label + = f.simple_fields_for :settings, current_user.settings do |ff| + = ff.input :'interactions.must_be_follower', wrapper: :with_label, label: I18n.t('simple_form.labels.interactions.must_be_follower') + = ff.input :'interactions.must_be_following', wrapper: :with_label, label: I18n.t('simple_form.labels.interactions.must_be_following') + = ff.input :'interactions.must_be_following_dm', wrapper: :with_label, label: I18n.t('simple_form.labels.interactions.must_be_following_dm') diff --git a/app/views/settings/preferences/other/show.html.haml b/app/views/settings/preferences/other/show.html.haml index 44f4af2eb..6590ec7c2 100644 --- a/app/views/settings/preferences/other/show.html.haml +++ b/app/views/settings/preferences/other/show.html.haml @@ -7,26 +7,27 @@ = simple_form_for current_user, url: settings_preferences_other_path, html: { method: :put, id: 'edit_preferences' } do |f| = render 'shared/error_messages', object: current_user - .fields-group - = f.input :setting_noindex, as: :boolean, wrapper: :with_label + = f.simple_fields_for :settings, current_user.settings do |ff| + .fields-group + = ff.input :noindex, wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_noindex'), hint: I18n.t('simple_form.hints.defaults.setting_noindex') - .fields-group - = f.input :setting_aggregate_reblogs, as: :boolean, wrapper: :with_label, recommended: true + .fields-group + = ff.input :aggregate_reblogs, wrapper: :with_label, recommended: true, label: I18n.t('simple_form.labels.defaults.setting_aggregate_reblogs'), hint: I18n.t('simple_form.hints.defaults.setting_aggregate_reblogs') - %h4= t 'preferences.posting_defaults' + %h4= t 'preferences.posting_defaults' - .fields-row - .fields-group.fields-row__column.fields-row__column-6 - = f.input :setting_default_privacy, collection: Status.selectable_visibilities, wrapper: :with_label, include_blank: false, label_method: lambda { |visibility| safe_join([I18n.t("statuses.visibilities.#{visibility}"), I18n.t("statuses.visibilities.#{visibility}_long")], ' - ') }, required: false, hint: false + .fields-row + .fields-group.fields-row__column.fields-row__column-6 + = ff.input :default_privacy, collection: Status.selectable_visibilities, wrapper: :with_label, include_blank: false, label_method: lambda { |visibility| safe_join([I18n.t("statuses.visibilities.#{visibility}"), I18n.t("statuses.visibilities.#{visibility}_long")], ' - ') }, required: false, hint: false, label: I18n.t('simple_form.labels.defaults.setting_default_privacy') - .fields-group.fields-row__column.fields-row__column-6 - = f.input :setting_default_language, collection: [nil] + filterable_languages, wrapper: :with_label, label_method: lambda { |locale| locale.nil? ? I18n.t('statuses.default_language') : native_locale_name(locale) }, required: false, include_blank: false, hint: false + .fields-group.fields-row__column.fields-row__column-6 + = ff.input :default_language, collection: [nil] + filterable_languages, wrapper: :with_label, label_method: lambda { |locale| locale.nil? ? I18n.t('statuses.default_language') : native_locale_name(locale) }, required: false, include_blank: false, hint: false, label: I18n.t('simple_form.labels.defaults.setting_default_language') - .fields-group - = f.input :setting_default_sensitive, as: :boolean, wrapper: :with_label + .fields-group + = ff.input :default_sensitive, wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_default_sensitive'), hint: I18n.t('simple_form.hints.defaults.setting_default_sensitive') - .fields-group - = f.input :setting_show_application, as: :boolean, wrapper: :with_label, recommended: true + .fields-group + = ff.input :show_application, wrapper: :with_label, recommended: true, label: I18n.t('simple_form.labels.defaults.setting_show_application'), hint: I18n.t('simple_form.hints.defaults.setting_show_application') %h4= t 'preferences.public_timelines' diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index a361cb0ec..95f0b5788 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -26,6 +26,7 @@ ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym 'URL' inflect.acronym 'ASCII' inflect.acronym 'DeepL' + inflect.acronym 'DSL' inflect.singular 'data', 'data' end diff --git a/config/settings.yml b/config/settings.yml index f0b09dd5c..4ac521a4b 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -12,48 +12,14 @@ defaults: &defaults registrations_mode: 'open' profile_directory: true closed_registrations_message: '' - open_deletion: true - min_invite_role: 'admin' timeline_preview: true show_staff_badge: true - default_sensitive: false - unfollow_modal: false - boost_modal: false - delete_modal: true - auto_play_gif: false - display_media: 'default' - expand_spoilers: false preview_sensitive_media: false - reduce_motion: false - disable_swiping: false - show_application: true - system_font_ui: false noindex: false theme: 'default' - aggregate_reblogs: true - advanced_layout: false - use_blurhash: true - use_pending_items: false trends: true trends_as_landing_page: true trendable_by_default: false - crop_images: true - notification_emails: - follow: true - reblog: false - favourite: false - mention: true - follow_request: true - digest: true - report: true - pending_account: true - trending_tag: true - appeal: true - always_send_emails: false - interactions: - must_be_follower: false - must_be_following: false - must_be_following_dm: false reserved_usernames: - admin - support diff --git a/db/migrate/20230215074327_add_settings_to_users.rb b/db/migrate/20230215074327_add_settings_to_users.rb new file mode 100644 index 000000000..ff5308f42 --- /dev/null +++ b/db/migrate/20230215074327_add_settings_to_users.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddSettingsToUsers < ActiveRecord::Migration[6.1] + def change + add_column :users, :settings, :text + end +end diff --git a/db/migrate/20230215074423_move_user_settings.rb b/db/migrate/20230215074423_move_user_settings.rb new file mode 100644 index 000000000..351a8b61d --- /dev/null +++ b/db/migrate/20230215074423_move_user_settings.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +class MoveUserSettings < ActiveRecord::Migration[6.1] + class User < ApplicationRecord; end + + MAPPING = { + default_privacy: 'default_privacy', + default_sensitive: 'web.default_sensitive', + default_language: 'default_language', + noindex: 'noindex', + theme: 'theme', + trends: 'web.trends', + unfollow_modal: 'web.unfollow_modal', + boost_modal: 'web.reblog_modal', + delete_modal: 'web.delete_modal', + auto_play_gif: 'web.auto_play', + display_media: 'web.display_media', + expand_spoilers: 'web.expand_content_warnings', + reduce_motion: 'web.reduce_motion', + disable_swiping: 'web.disable_swiping', + show_application: 'show_application', + system_font_ui: 'web.use_system_font', + aggregate_reblogs: 'aggregate_reblogs', + advanced_layout: 'web.advanced_layout', + use_blurhash: 'web.use_blurhash', + use_pending_items: 'web.use_pending_items', + crop_images: 'web.crop_images', + notification_emails: { + follow: 'notification_emails.follow', + reblog: 'notification_emails.reblog', + favourite: 'notification_emails.favourite', + mention: 'notification_emails.mention', + follow_request: 'notification_emails.follow_request', + report: 'notification_emails.report', + pending_account: 'notification_emails.pending_account', + trending_tag: 'notification_emails.trends', + appeal: 'notification_emails.appeal', + }.freeze, + always_send_emails: 'always_send_emails', + interactions: { + must_be_follower: 'interactions.must_be_follower', + must_be_following: 'interactions.must_be_following', + must_be_following_dm: 'interactions.must_be_following_dm', + }.freeze, + }.freeze + + class LegacySetting < ApplicationRecord + self.table_name = 'settings' + + def var + self[:var]&.to_sym + end + + def value + YAML.safe_load(self[:value], permitted_classes: [ActiveSupport::HashWithIndifferentAccess]) if self[:value].present? + end + end + + def up + User.find_each do |user| + previous_settings = LegacySetting.where(thing_type: 'User', thing_id: user.id).index_by(&:var) + + user_settings = {} + + MAPPING.each do |legacy_key, new_key| + value = previous_settings[legacy_key]&.value + + next if value.blank? + + if value.is_a?(Hash) + value.each do |nested_key, nested_value| + user_settings[MAPPING[legacy_key][nested_key.to_sym]] = nested_value + end + else + user_settings[new_key] = value + end + end + + user.update_column('settings', Oj.dump(user_settings)) # rubocop:disable Rails/SkipsModelValidations + end + end + + def down; end +end diff --git a/db/schema.rb b/db/schema.rb index 704cef122..620bed2bc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_12_06_114142) do +ActiveRecord::Schema.define(version: 2023_02_15_074423) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -1060,6 +1060,7 @@ ActiveRecord::Schema.define(version: 2022_12_06_114142) do t.inet "sign_up_ip" t.boolean "skip_sign_in_token" t.bigint "role_id" + t.text "settings" t.index ["account_id"], name: "index_users_on_account_id" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["created_by_application_id"], name: "index_users_on_created_by_application_id", where: "(created_by_application_id IS NOT NULL)" diff --git a/lib/tasks/tests.rake b/lib/tasks/tests.rake index 51a6ee0d7..35073b78b 100644 --- a/lib/tasks/tests.rake +++ b/lib/tasks/tests.rake @@ -54,7 +54,7 @@ namespace :tests do exit(1) end - unless User.find(1).settings.notification_emails['favourite'] == true && User.find(1).settings.notification_emails['mention'] == false + unless User.find(1).settings['notification_emails.favourite'] == true && User.find(1).settings['notification_emails.mention'] == false puts 'User settings not kept as expected' exit(1) end diff --git a/spec/controllers/api/v1/accounts/credentials_controller_spec.rb b/spec/controllers/api/v1/accounts/credentials_controller_spec.rb index 57fe0aee6..b5d5c37a9 100644 --- a/spec/controllers/api/v1/accounts/credentials_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/credentials_controller_spec.rb @@ -46,6 +46,7 @@ describe Api::V1::Accounts::CredentialsController do end it 'updates account info' do + user.reload user.account.reload expect(user.account.display_name).to eq("Alice Isn't Dead") diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 35c7326cb..bc6c6c0c5 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -88,21 +88,19 @@ describe ApplicationController, type: :controller do it 'returns instances\'s default theme when user didn\'t set theme' do current_user = Fabricate(:user) + current_user.settings.update(theme: 'contrast', noindex: false) + current_user.save sign_in current_user - allow(Setting).to receive(:[]).with('theme').and_return 'contrast' - allow(Setting).to receive(:[]).with('noindex').and_return false - expect(controller.view_context.current_theme).to eq 'contrast' end it 'returns user\'s theme when it is set' do current_user = Fabricate(:user) - current_user.settings['theme'] = 'mastodon-light' + current_user.settings.update(theme: 'mastodon-light') + current_user.save sign_in current_user - allow(Setting).to receive(:[]).with('theme').and_return 'contrast' - expect(controller.view_context.current_theme).to eq 'mastodon-light' end end diff --git a/spec/controllers/settings/preferences/notifications_controller_spec.rb b/spec/controllers/settings/preferences/notifications_controller_spec.rb index 66fb8c5eb..29b7b6aec 100644 --- a/spec/controllers/settings/preferences/notifications_controller_spec.rb +++ b/spec/controllers/settings/preferences/notifications_controller_spec.rb @@ -20,20 +20,22 @@ describe Settings::Preferences::NotificationsController do describe 'PUT #update' do it 'updates notifications settings' do - user.settings['notification_emails'] = user.settings['notification_emails'].merge('follow' => false) - user.settings['interactions'] = user.settings['interactions'].merge('must_be_follower' => true) + user.settings.update('notification_emails.follow': false, 'interactions.must_be_follower': true) + user.save put :update, params: { user: { - notification_emails: { follow: '1' }, - interactions: { must_be_follower: '0' }, + settings_attributes: { + 'notification_emails.follow': '1', + 'interactions.must_be_follower': '0', + }, }, } expect(response).to redirect_to(settings_preferences_notifications_path) user.reload - expect(user.settings['notification_emails']['follow']).to be true - expect(user.settings['interactions']['must_be_follower']).to be false + expect(user.settings['notification_emails.follow']).to be true + expect(user.settings['interactions.must_be_follower']).to be false end end end diff --git a/spec/controllers/settings/preferences/other_controller_spec.rb b/spec/controllers/settings/preferences/other_controller_spec.rb index 63eeefaf0..249d1b5b5 100644 --- a/spec/controllers/settings/preferences/other_controller_spec.rb +++ b/spec/controllers/settings/preferences/other_controller_spec.rb @@ -29,20 +29,22 @@ describe Settings::Preferences::OtherController do end it 'updates user settings' do - user.settings['boost_modal'] = false - user.settings['delete_modal'] = true + user.settings.update('web.reblog_modal': false, 'web.delete_modal': true) + user.save put :update, params: { user: { - setting_boost_modal: '1', - setting_delete_modal: '0', + settings_attributes: { + 'web.reblog_modal': '1', + 'web.delete_modal': '0', + }, }, } expect(response).to redirect_to(settings_preferences_other_path) user.reload - expect(user.settings['boost_modal']).to be true - expect(user.settings['delete_modal']).to be false + expect(user.settings['web.reblog_modal']).to be true + expect(user.settings['web.delete_modal']).to be false end end end diff --git a/spec/lib/settings/extend_spec.rb b/spec/lib/settings/extend_spec.rb deleted file mode 100644 index ea623137b..000000000 --- a/spec/lib/settings/extend_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Settings::Extend do - class User - include Settings::Extend - end - - describe '#settings' do - it 'sets @settings as an instance of Settings::ScopedSettings' do - user = Fabricate(:user) - expect(user.settings).to be_a Settings::ScopedSettings - end - end -end diff --git a/spec/lib/settings/scoped_settings_spec.rb b/spec/lib/settings/scoped_settings_spec.rb deleted file mode 100644 index 7566685b4..000000000 --- a/spec/lib/settings/scoped_settings_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Settings::ScopedSettings do - let(:object) { Fabricate(:user) } - let(:scoped_setting) { described_class.new(object) } - let(:val) { 'whatever' } - let(:methods) { %i(auto_play_gif default_sensitive unfollow_modal boost_modal delete_modal reduce_motion system_font_ui noindex theme) } - - describe '.initialize' do - it 'sets @object' do - scoped_setting = described_class.new(object) - expect(scoped_setting.instance_variable_get(:@object)).to be object - end - end - - describe '#method_missing' do - it 'sets scoped_setting.method_name = val' do - methods.each do |key| - scoped_setting.send("#{key}=", val) - expect(scoped_setting.send(key)).to eq val - end - end - end - - describe '#[]= and #[]' do - it 'sets [key] = val' do - methods.each do |key| - scoped_setting[key] = val - expect(scoped_setting[key]).to eq val - end - end - end -end diff --git a/spec/lib/user_settings_decorator_spec.rb b/spec/lib/user_settings_decorator_spec.rb deleted file mode 100644 index 3b9b7ee2b..000000000 --- a/spec/lib/user_settings_decorator_spec.rb +++ /dev/null @@ -1,84 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -describe UserSettingsDecorator do - describe 'update' do - let(:user) { Fabricate(:user) } - let(:settings) { described_class.new(user) } - - it 'updates the user settings value for email notifications' do - values = { 'notification_emails' => { 'follow' => '1' } } - - settings.update(values) - expect(user.settings['notification_emails']['follow']).to be true - end - - it 'updates the user settings value for interactions' do - values = { 'interactions' => { 'must_be_follower' => '0' } } - - settings.update(values) - expect(user.settings['interactions']['must_be_follower']).to be false - end - - it 'updates the user settings value for privacy' do - values = { 'setting_default_privacy' => 'public' } - - settings.update(values) - expect(user.settings['default_privacy']).to eq 'public' - end - - it 'updates the user settings value for sensitive' do - values = { 'setting_default_sensitive' => '1' } - - settings.update(values) - expect(user.settings['default_sensitive']).to be true - end - - it 'updates the user settings value for unfollow modal' do - values = { 'setting_unfollow_modal' => '0' } - - settings.update(values) - expect(user.settings['unfollow_modal']).to be false - end - - it 'updates the user settings value for boost modal' do - values = { 'setting_boost_modal' => '1' } - - settings.update(values) - expect(user.settings['boost_modal']).to be true - end - - it 'updates the user settings value for delete toot modal' do - values = { 'setting_delete_modal' => '0' } - - settings.update(values) - expect(user.settings['delete_modal']).to be false - end - - it 'updates the user settings value for gif auto play' do - values = { 'setting_auto_play_gif' => '0' } - - settings.update(values) - expect(user.settings['auto_play_gif']).to be false - end - - it 'updates the user settings value for system font in UI' do - values = { 'setting_system_font_ui' => '0' } - - settings.update(values) - expect(user.settings['system_font_ui']).to be false - end - - it 'decoerces setting values before applying' do - values = { - 'setting_delete_modal' => 'false', - 'setting_boost_modal' => 'true', - } - - settings.update(values) - expect(user.settings['delete_modal']).to be false - expect(user.settings['boost_modal']).to be true - end - end -end diff --git a/spec/models/user_settings/namespace_spec.rb b/spec/models/user_settings/namespace_spec.rb new file mode 100644 index 000000000..ae2fa7b48 --- /dev/null +++ b/spec/models/user_settings/namespace_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe UserSettings::Namespace do + subject { described_class.new(name) } + + let(:name) { :foo } + + describe '#setting' do + before do + subject.setting :bar, default: 'baz' + end + + it 'adds setting to definitions' do + expect(subject.definitions[:'foo.bar']).to have_attributes(name: :bar, namespace: :foo, default_value: 'baz') + end + end + + describe '#definitions' do + it 'returns a hash' do + expect(subject.definitions).to be_a Hash + end + end +end diff --git a/spec/models/user_settings/setting_spec.rb b/spec/models/user_settings/setting_spec.rb new file mode 100644 index 000000000..6e4ec6789 --- /dev/null +++ b/spec/models/user_settings/setting_spec.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe UserSettings::Setting do + subject { described_class.new(name, options) } + + let(:name) { :foo } + let(:options) { { default: default, namespace: namespace } } + let(:default) { false } + let(:namespace) { nil } + + describe '#default_value' do + context 'when default value is a primitive value' do + it 'returns default value' do + expect(subject.default_value).to eq default + end + end + + context 'when default value is a proc' do + let(:default) { -> { 'bar' } } + + it 'returns value from proc' do + expect(subject.default_value).to eq 'bar' + end + end + end + + describe '#type' do + it 'returns a type' do + expect(subject.type).to be_a ActiveModel::Type::Value + end + end + + describe '#type_cast' do + context 'when default value is a boolean' do + let(:default) { false } + + it 'returns boolean' do + expect(subject.type_cast('1')).to be true + end + end + + context 'when default value is a string' do + let(:default) { '' } + + it 'returns string' do + expect(subject.type_cast(1)).to eq '1' + end + end + end + + describe '#to_a' do + it 'returns an array' do + expect(subject.to_a).to eq [name, default] + end + end + + describe '#key' do + context 'when there is no namespace' do + it 'returnsn a symbol' do + expect(subject.key).to eq :foo + end + end + + context 'when there is a namespace' do + let(:namespace) { :bar } + + it 'returns a symbol' do + expect(subject.key).to eq :'bar.foo' + end + end + end +end diff --git a/spec/models/user_settings_spec.rb b/spec/models/user_settings_spec.rb new file mode 100644 index 000000000..f0e4272fd --- /dev/null +++ b/spec/models/user_settings_spec.rb @@ -0,0 +1,110 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe UserSettings do + subject { described_class.new(json) } + + let(:json) { {} } + + describe '#[]' do + context 'when setting is not set' do + it 'returns default value' do + expect(subject[:always_send_emails]).to be false + end + end + + context 'when setting is set' do + let(:json) { { default_language: 'fr' } } + + it 'returns value' do + expect(subject[:default_language]).to eq 'fr' + end + end + + context 'when setting was not defined' do + it 'raises error' do + expect { subject[:foo] }.to raise_error UserSettings::KeyError + end + end + end + + describe '#[]=' do + context 'when value matches type' do + before do + subject[:always_send_emails] = true + end + + it 'updates value' do + expect(subject[:always_send_emails]).to be true + end + end + + context 'when value needs to be type-cast' do + before do + subject[:always_send_emails] = '1' + end + + it 'updates value with a type-cast' do + expect(subject[:always_send_emails]).to be true + end + end + end + + describe '#update' do + before do + subject.update(always_send_emails: true, default_language: 'fr', default_privacy: nil) + end + + it 'updates values' do + expect(subject[:always_send_emails]).to be true + expect(subject[:default_language]).to eq 'fr' + end + + it 'does not set values that are nil' do + expect(subject.as_json).to_not include(default_privacy: nil) + end + end + + describe '#as_json' do + let(:json) { { default_language: 'fr' } } + + it 'returns hash' do + expect(subject.as_json).to eq json + end + end + + describe '.keys' do + it 'returns an array' do + expect(described_class.keys).to be_a Array + end + end + + describe '.definition_for' do + context 'when key is defined' do + it 'returns a setting' do + expect(described_class.definition_for(:always_send_emails)).to be_a UserSettings::Setting + end + end + + context 'when key is not defined' do + it 'returns nil' do + expect(described_class.definition_for(:foo)).to be_nil + end + end + end + + describe '.definition_for?' do + context 'when key is defined' do + it 'returns true' do + expect(described_class.definition_for?(:always_send_emails)).to be true + end + end + + context 'when key is not defined' do + it 'returns false' do + expect(described_class.definition_for?(:foo)).to be false + end + end + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3e7b59f17..ab883927a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -313,9 +313,9 @@ RSpec.describe User, type: :model do end describe 'settings' do - it 'is instance of Settings::ScopedSettings' do + it 'is instance of UserSettings' do user = Fabricate(:user) - expect(user.settings).to be_a Settings::ScopedSettings + expect(user.settings).to be_a UserSettings end end @@ -379,16 +379,6 @@ RSpec.describe User, type: :model do end end - it_behaves_like 'Settings-extended' do - def create! - User.create!(account: Fabricate(:account, user: nil), email: 'foo@mastodon.space', password: 'abcd1234', agreement: true) - end - - def fabricate - Fabricate(:user) - end - end - describe 'token_for_app' do let(:user) { Fabricate(:user) } let(:app) { Fabricate(:application, owner: user) } diff --git a/spec/services/notify_service_spec.rb b/spec/services/notify_service_spec.rb index c58cebbfb..616a7aa20 100644 --- a/spec/services/notify_service_spec.rb +++ b/spec/services/notify_service_spec.rb @@ -54,7 +54,8 @@ RSpec.describe NotifyService, type: :service do let(:type) { :mention } before do - user.settings.interactions = user.settings.interactions.merge('must_be_following_dm' => enabled) + user.settings.update('interactions.must_be_following_dm': enabled) + user.save end context 'if recipient is supposed to be following sender' do @@ -155,8 +156,8 @@ RSpec.describe NotifyService, type: :service do before do ActionMailer::Base.deliveries.clear - notification_emails = user.settings.notification_emails - user.settings.notification_emails = notification_emails.merge('follow' => enabled) + user.settings.update('notification_emails.follow': enabled) + user.save end context 'when email notification is enabled' do diff --git a/spec/services/report_service_spec.rb b/spec/services/report_service_spec.rb index 9d81bd971..452400f72 100644 --- a/spec/services/report_service_spec.rb +++ b/spec/services/report_service_spec.rb @@ -96,7 +96,8 @@ RSpec.describe ReportService, type: :service do before do ActionMailer::Base.deliveries.clear - source_account.user.settings.notification_emails['report'] = true + source_account.user.settings['notification_emails.report'] = true + source_account.user.save end it 'does not send an e-mail' do -- 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/user.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 '