diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/account.rb | 10 | ||||
-rw-r--r-- | app/models/account_filter.rb | 27 | ||||
-rw-r--r-- | app/models/account_warning_preset.rb | 3 | ||||
-rw-r--r-- | app/models/admin/account_action.rb | 12 | ||||
-rw-r--r-- | app/models/admin/action_log_filter.rb | 81 | ||||
-rw-r--r-- | app/models/announcement.rb | 11 | ||||
-rw-r--r-- | app/models/concerns/account_interactions.rb | 16 | ||||
-rw-r--r-- | app/models/concerns/attachmentable.rb | 2 | ||||
-rw-r--r-- | app/models/concerns/rate_limitable.rb | 36 | ||||
-rw-r--r-- | app/models/email_domain_block.rb | 14 | ||||
-rw-r--r-- | app/models/follow.rb | 3 | ||||
-rw-r--r-- | app/models/follow_request.rb | 3 | ||||
-rw-r--r-- | app/models/media_attachment.rb | 122 | ||||
-rw-r--r-- | app/models/report.rb | 11 | ||||
-rw-r--r-- | app/models/status.rb | 20 |
15 files changed, 305 insertions, 66 deletions
diff --git a/app/models/account.rb b/app/models/account.rb index 0fcf897c9..82d4d10de 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -46,6 +46,7 @@ # silenced_at :datetime # suspended_at :datetime # trust_level :integer +# hide_collections :boolean # class Account < ApplicationRecord @@ -106,6 +107,7 @@ class Account < ApplicationRecord scope :discoverable, -> { searchable.without_silenced.where(discoverable: true).left_outer_joins(:account_stat) } scope :tagged_with, ->(tag) { joins(:accounts_tags).where(accounts_tags: { tag_id: tag }) } scope :by_recent_status, -> { order(Arel.sql('(case when account_stats.last_status_at is null then 1 else 0 end) asc, account_stats.last_status_at desc, accounts.id desc')) } + scope :by_recent_sign_in, -> { order(Arel.sql('(case when users.current_sign_in_at is null then 1 else 0 end) asc, users.current_sign_in_at desc, accounts.id desc')) } scope :popular, -> { order('account_stats.followers_count desc') } scope :by_domain_and_subdomains, ->(domain) { where(domain: domain).or(where(arel_table[:domain].matches('%.' + domain))) } scope :not_excluded_by_account, ->(account) { where.not(id: account.excluded_from_timeline_account_ids) } @@ -324,6 +326,14 @@ class Account < ApplicationRecord save! end + def hides_followers? + hide_collections? || user_hides_network? + end + + def hides_following? + hide_collections? || user_hides_network? + end + def object_type :person end diff --git a/app/models/account_filter.rb b/app/models/account_filter.rb index c7bf07787..7b6012e0f 100644 --- a/app/models/account_filter.rb +++ b/app/models/account_filter.rb @@ -14,6 +14,7 @@ class AccountFilter email ip staff + order ).freeze attr_reader :params @@ -24,7 +25,7 @@ class AccountFilter end def results - scope = Account.recent.includes(:user) + scope = Account.includes(:user).reorder(nil) params.each do |key, value| scope.merge!(scope_for(key, value.to_s.strip)) if value.present? @@ -38,6 +39,7 @@ class AccountFilter def set_defaults! params['local'] = '1' if params['remote'].blank? params['active'] = '1' if params['suspended'].blank? && params['silenced'].blank? && params['pending'].blank? + params['order'] = 'recent' if params['order'].blank? end def scope_for(key, value) @@ -51,9 +53,9 @@ class AccountFilter when 'active' Account.without_suspended when 'pending' - accounts_with_users.merge User.pending + accounts_with_users.merge(User.pending) when 'disabled' - accounts_with_users.merge User.disabled + accounts_with_users.merge(User.disabled) when 'silenced' Account.silenced when 'suspended' @@ -63,16 +65,31 @@ class AccountFilter when 'display_name' Account.matches_display_name(value) when 'email' - accounts_with_users.merge User.matches_email(value) + accounts_with_users.merge(User.matches_email(value)) when 'ip' valid_ip?(value) ? accounts_with_users.merge(User.matches_ip(value)) : Account.none when 'staff' - accounts_with_users.merge User.staff + accounts_with_users.merge(User.staff) + when 'order' + order_scope(value) else raise "Unknown filter: #{key}" end end + def order_scope(value) + case value + when 'active' + params['remote'] ? Account.joins(:account_stat).by_recent_status : Account.joins(:user).by_recent_sign_in + when 'recent' + Account.recent + when 'alphabetic' + Account.alphabetic + else + raise "Unknown order: #{value}" + end + end + def accounts_with_users Account.joins(:user) end diff --git a/app/models/account_warning_preset.rb b/app/models/account_warning_preset.rb index ba8ceabb3..c20f683cf 100644 --- a/app/models/account_warning_preset.rb +++ b/app/models/account_warning_preset.rb @@ -8,8 +8,11 @@ # text :text default(""), not null # created_at :datetime not null # updated_at :datetime not null +# title :string default(""), not null # class AccountWarningPreset < ApplicationRecord validates :text, presence: true + + scope :alphabetic, -> { order(title: :asc, text: :asc) } end diff --git a/app/models/admin/account_action.rb b/app/models/admin/account_action.rb index e9da003a3..b30a82369 100644 --- a/app/models/admin/account_action.rb +++ b/app/models/admin/account_action.rb @@ -62,8 +62,6 @@ class Admin::AccountAction def process_action! case type - when 'none' - handle_resolve! when 'disable' handle_disable! when 'silence' @@ -105,16 +103,6 @@ class Admin::AccountAction end end - def handle_resolve! - if with_report? && report.account_id == -99 && target_account.trust_level == Account::TRUST_LEVELS[:untrusted] - # This is an automated report and it is being dismissed, so it's - # a false positive, in which case update the account's trust level - # to prevent further spam checks - - target_account.update(trust_level: Account::TRUST_LEVELS[:trusted]) - end - end - def handle_disable! authorize(target_account.user, :disable?) log_action(:disable, target_account.user) diff --git a/app/models/admin/action_log_filter.rb b/app/models/admin/action_log_filter.rb new file mode 100644 index 000000000..0ba7e1609 --- /dev/null +++ b/app/models/admin/action_log_filter.rb @@ -0,0 +1,81 @@ +# frozen_string_literal: true + +class Admin::ActionLogFilter + KEYS = %i( + action_type + account_id + target_account_id + ).freeze + + ACTION_TYPE_MAP = { + assigned_to_self_report: { target_type: 'Report', action: 'assigned_to_self' }.freeze, + change_email_user: { target_type: 'User', action: 'change_email' }.freeze, + confirm_user: { target_type: 'User', action: 'confirm' }.freeze, + create_account_warning: { target_type: 'AccountWarning', action: 'create' }.freeze, + create_announcement: { target_type: 'Announcement', action: 'create' }.freeze, + create_custom_emoji: { target_type: 'CustomEmoji', action: 'create' }.freeze, + create_domain_allow: { target_type: 'DomainAllow', action: 'create' }.freeze, + create_domain_block: { target_type: 'DomainBlock', action: 'create' }.freeze, + create_email_domain_block: { target_type: 'EmailDomainBlock', action: 'create' }.freeze, + demote_user: { target_type: 'User', action: 'demote' }.freeze, + destroy_announcement: { target_type: 'Announcement', action: 'destroy' }.freeze, + destroy_custom_emoji: { target_type: 'CustomEmoji', action: 'destroy' }.freeze, + destroy_domain_allow: { target_type: 'DomainAllow', action: 'destroy' }.freeze, + destroy_domain_block: { target_type: 'DomainBlock', action: 'destroy' }.freeze, + destroy_email_domain_block: { target_type: 'EmailDomainBlock', action: 'destroy' }.freeze, + destroy_status: { target_type: 'Status', action: 'destroy' }.freeze, + disable_2fa_user: { target_type: 'User', action: 'disable' }.freeze, + disable_custom_emoji: { target_type: 'CustomEmoji', action: 'disable' }.freeze, + disable_user: { target_type: 'User', action: 'disable' }.freeze, + enable_custom_emoji: { target_type: 'CustomEmoji', action: 'enable' }.freeze, + enable_user: { target_type: 'User', action: 'enable' }.freeze, + memorialize_account: { target_type: 'Account', action: 'memorialize' }.freeze, + promote_user: { target_type: 'User', action: 'promote' }.freeze, + remove_avatar_user: { target_type: 'User', action: 'remove_avatar' }.freeze, + reopen_report: { target_type: 'Report', action: 'reopen' }.freeze, + reset_password_user: { target_type: 'User', action: 'reset_password' }.freeze, + resolve_report: { target_type: 'Report', action: 'resolve' }.freeze, + silence_account: { target_type: 'Account', action: 'silence' }.freeze, + suspend_account: { target_type: 'Account', action: 'suspend' }.freeze, + unassigned_report: { target_type: 'Report', action: 'unassigned' }.freeze, + unsilence_account: { target_type: 'Account', action: 'unsilence' }.freeze, + unsuspend_account: { target_type: 'Account', action: 'unsuspend' }.freeze, + update_announcement: { target_type: 'Announcement', action: 'update' }.freeze, + update_custom_emoji: { target_type: 'CustomEmoji', action: 'update' }.freeze, + update_status: { target_type: 'Status', action: 'update' }.freeze, + }.freeze + + attr_reader :params + + def initialize(params) + @params = params + end + + def results + scope = Admin::ActionLog.includes(:target) + + params.each do |key, value| + next if key.to_s == 'page' + + scope.merge!(scope_for(key.to_s, value.to_s.strip)) if value.present? + end + + scope + end + + private + + def scope_for(key, value) + case key + when 'action_type' + Admin::ActionLog.where(ACTION_TYPE_MAP[value.to_sym]) + when 'account_id' + Admin::ActionLog.where(account_id: value) + when 'target_account_id' + account = Account.find(value) + Admin::ActionLog.where(target: [account, account.user].compact) + else + raise "Unknown filter: #{key}" + end + end +end diff --git a/app/models/announcement.rb b/app/models/announcement.rb index d99502f44..a4e427b49 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -14,6 +14,7 @@ # created_at :datetime not null # updated_at :datetime not null # published_at :datetime +# status_ids :bigint is an Array # class Announcement < ApplicationRecord @@ -48,6 +49,16 @@ class Announcement < ApplicationRecord @mentions ||= Account.from_text(text) end + def statuses + @statuses ||= begin + if status_ids.nil? + [] + else + Status.where(id: status_ids, visibility: [:public, :unlisted]) + end + end + end + def tags @tags ||= Tag.find_or_create_by_names(Extractor.extract_hashtags(text)) end diff --git a/app/models/concerns/account_interactions.rb b/app/models/concerns/account_interactions.rb index 14bcf7bb1..32fcb5397 100644 --- a/app/models/concerns/account_interactions.rb +++ b/app/models/concerns/account_interactions.rb @@ -87,10 +87,10 @@ module AccountInteractions has_many :announcement_mutes, dependent: :destroy end - def follow!(other_account, reblogs: nil, uri: nil) + def follow!(other_account, reblogs: nil, uri: nil, rate_limit: false) reblogs = true if reblogs.nil? - rel = active_relationships.create_with(show_reblogs: reblogs, uri: uri) + rel = active_relationships.create_with(show_reblogs: reblogs, uri: uri, rate_limit: rate_limit) .find_or_create_by!(target_account: other_account) rel.update!(show_reblogs: reblogs) @@ -99,6 +99,18 @@ module AccountInteractions rel end + def request_follow!(other_account, reblogs: nil, uri: nil, rate_limit: false) + reblogs = true if reblogs.nil? + + rel = follow_requests.create_with(show_reblogs: reblogs, uri: uri, rate_limit: rate_limit) + .find_or_create_by!(target_account: other_account) + + rel.update!(show_reblogs: reblogs) + remove_potential_friendship(other_account) + + rel + end + def block!(other_account, uri: nil) remove_potential_friendship(other_account) block_relationships.create_with(uri: uri) diff --git a/app/models/concerns/attachmentable.rb b/app/models/concerns/attachmentable.rb index 43ff8ac12..18b872c1e 100644 --- a/app/models/concerns/attachmentable.rb +++ b/app/models/concerns/attachmentable.rb @@ -74,7 +74,7 @@ module Attachmentable self.class.attachment_definitions.each_key do |attachment_name| attachment = send(attachment_name) - next if attachment.blank? || attachment.queued_for_write[:original].blank? + next if attachment.blank? || attachment.queued_for_write[:original].blank? || attachment.options[:preserve_files] attachment.instance_write :file_name, SecureRandom.hex(8) + File.extname(attachment.instance_read(:file_name)) end diff --git a/app/models/concerns/rate_limitable.rb b/app/models/concerns/rate_limitable.rb new file mode 100644 index 000000000..ad1b5e44e --- /dev/null +++ b/app/models/concerns/rate_limitable.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module RateLimitable + extend ActiveSupport::Concern + + def rate_limit=(value) + @rate_limit = value + end + + def rate_limit? + @rate_limit + end + + def rate_limiter(by, options = {}) + return @rate_limiter if defined?(@rate_limiter) + + @rate_limiter = RateLimiter.new(by, options) + end + + class_methods do + def rate_limit(options = {}) + after_create do + by = public_send(options[:by]) + + if rate_limit? && by&.local? + rate_limiter(by, options).record! + @rate_limit_recorded = true + end + end + + after_rollback do + rate_limiter(public_send(options[:by]), options).rollback! if @rate_limit_recorded + end + end + end +end diff --git a/app/models/email_domain_block.rb b/app/models/email_domain_block.rb index bc70dea25..f50fa46ba 100644 --- a/app/models/email_domain_block.rb +++ b/app/models/email_domain_block.rb @@ -7,13 +7,27 @@ # domain :string default(""), not null # created_at :datetime not null # updated_at :datetime not null +# parent_id :bigint(8) # class EmailDomainBlock < ApplicationRecord include DomainNormalizable + belongs_to :parent, class_name: 'EmailDomainBlock', optional: true + has_many :children, class_name: 'EmailDomainBlock', foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy + validates :domain, presence: true, uniqueness: true, domain: true + def with_dns_records=(val) + @with_dns_records = ActiveModel::Type::Boolean.new.cast(val) + end + + def with_dns_records? + @with_dns_records + end + + alias with_dns_records with_dns_records? + def self.block?(email) _, domain = email.split('@', 2) diff --git a/app/models/follow.rb b/app/models/follow.rb index 87fa11425..f3e48a2ed 100644 --- a/app/models/follow.rb +++ b/app/models/follow.rb @@ -15,6 +15,9 @@ class Follow < ApplicationRecord include Paginable include RelationshipCacheable + include RateLimitable + + rate_limit by: :account, family: :follows belongs_to :account belongs_to :target_account, class_name: 'Account' diff --git a/app/models/follow_request.rb b/app/models/follow_request.rb index 96ac7eaa5..3325e264c 100644 --- a/app/models/follow_request.rb +++ b/app/models/follow_request.rb @@ -15,6 +15,9 @@ class FollowRequest < ApplicationRecord include Paginable include RelationshipCacheable + include RateLimitable + + rate_limit by: :account, family: :follows belongs_to :account belongs_to :target_account, class_name: 'Account' diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index 6a0b892f6..40624c73c 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -19,12 +19,14 @@ # description :text # scheduled_status_id :bigint(8) # blurhash :string +# processing :integer # class MediaAttachment < ApplicationRecord self.inheritance_column = nil enum type: [:image, :gifv, :video, :unknown, :audio] + enum processing: [:queued, :in_progress, :complete, :failed], _prefix: true MAX_DESCRIPTION_LENGTH = 1_500 @@ -55,6 +57,43 @@ class MediaAttachment < ApplicationRecord }, }.freeze + VIDEO_FORMAT = { + format: 'mp4', + content_type: 'video/mp4', + convert_options: { + output: { + 'loglevel' => 'fatal', + 'movflags' => 'faststart', + 'pix_fmt' => 'yuv420p', + 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'', + 'vsync' => 'cfr', + 'c:v' => 'h264', + 'maxrate' => '1300K', + 'bufsize' => '1300K', + 'frames:v' => 60 * 60 * 3, + 'crf' => 18, + 'map_metadata' => '-1', + }, + }, + }.freeze + + VIDEO_PASSTHROUGH_OPTIONS = { + video_codecs: ['h264'], + audio_codecs: ['aac', nil], + colorspaces: ['yuv420p'], + options: { + format: 'mp4', + convert_options: { + output: { + 'loglevel' => 'fatal', + 'map_metadata' => '-1', + 'c:v' => 'copy', + 'c:a' => 'copy', + }, + }, + }, + }.freeze + VIDEO_STYLES = { small: { convert_options: { @@ -69,17 +108,7 @@ class MediaAttachment < ApplicationRecord blurhash: BLURHASH_OPTIONS, }, - original: { - keep_same_format: true, - convert_options: { - output: { - 'loglevel' => 'fatal', - 'map_metadata' => '-1', - 'c:v' => 'copy', - 'c:a' => 'copy', - }, - }, - }, + original: VIDEO_FORMAT.merge(passthrough_options: VIDEO_PASSTHROUGH_OPTIONS), }.freeze AUDIO_STYLES = { @@ -96,26 +125,6 @@ class MediaAttachment < ApplicationRecord }, }.freeze - VIDEO_FORMAT = { - format: 'mp4', - content_type: 'video/mp4', - convert_options: { - output: { - 'loglevel' => 'fatal', - 'movflags' => 'faststart', - 'pix_fmt' => 'yuv420p', - 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'', - 'vsync' => 'cfr', - 'c:v' => 'h264', - 'maxrate' => '1300K', - 'bufsize' => '1300K', - 'frames:v' => 60 * 60 * 3, - 'crf' => 18, - 'map_metadata' => '-1', - }, - }, - }.freeze - VIDEO_CONVERTED_STYLES = { small: VIDEO_STYLES[:small], original: VIDEO_FORMAT, @@ -124,6 +133,9 @@ class MediaAttachment < ApplicationRecord IMAGE_LIMIT = (ENV['MAX_IMAGE_SIZE'] || 10.megabytes).to_i VIDEO_LIMIT = (ENV['MAX_VIDEO_SIZE'] || 40.megabytes).to_i + MAX_VIDEO_MATRIX_LIMIT = 2_304_000 # 1920x1200px + MAX_VIDEO_FRAME_RATE = 60 + belongs_to :account, inverse_of: :media_attachments, optional: true belongs_to :status, inverse_of: :media_attachments, optional: true belongs_to :scheduled_status, inverse_of: :media_attachments, optional: true @@ -156,6 +168,10 @@ class MediaAttachment < ApplicationRecord remote_url.blank? end + def not_processed? + processing.present? && !processing_complete? + end + def needs_redownload? file.blank? && remote_url.present? end @@ -168,18 +184,6 @@ class MediaAttachment < ApplicationRecord audio? || video? end - def variant?(other_file_name) - return true if file_file_name == other_file_name - - formats = file.styles.values.map(&:format).compact - - return false if formats.empty? - - extension = File.extname(other_file_name) - - formats.include?(extension.delete('.')) && File.basename(other_file_name, extension) == File.basename(file_file_name, File.extname(file_file_name)) - end - def to_param shortcode end @@ -202,12 +206,21 @@ class MediaAttachment < ApplicationRecord "#{x},#{y}" end + attr_writer :delay_processing + + def delay_processing? + @delay_processing + end + + after_commit :enqueue_processing, on: :create after_commit :reset_parent_cache, on: :update before_create :prepare_description, unless: :local? before_create :set_shortcode + before_create :set_processing before_post_process :set_type_and_extension + before_post_process :check_video_dimensions before_save :set_meta @@ -276,6 +289,21 @@ class MediaAttachment < ApplicationRecord end end + def set_processing + self.processing = delay_processing? ? :queued : :complete + end + + def check_video_dimensions + return unless (video? || gifv?) && file.queued_for_write[:original].present? + + movie = FFMPEG::Movie.new(file.queued_for_write[:original].path) + + return unless movie.valid? + + raise Mastodon::DimensionsValidationError, "#{movie.width}x#{movie.height} videos are not supported" if movie.width * movie.height > MAX_VIDEO_MATRIX_LIMIT + raise Mastodon::DimensionsValidationError, "#{movie.frame_rate.to_i}fps videos are not supported" if movie.frame_rate > MAX_VIDEO_FRAME_RATE + end + def set_meta meta = populate_meta @@ -321,9 +349,11 @@ class MediaAttachment < ApplicationRecord }.compact end - def reset_parent_cache - return if status_id.nil? + def enqueue_processing + PostProcessMediaWorker.perform_async(id) if delay_processing? + end - Rails.cache.delete("statuses/#{status_id}") + def reset_parent_cache + Rails.cache.delete("statuses/#{status_id}") if status_id.present? end end diff --git a/app/models/report.rb b/app/models/report.rb index fb2e040ee..f31bcfd2e 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -18,6 +18,9 @@ class Report < ApplicationRecord include Paginable + include RateLimitable + + rate_limit by: :account, family: :reports belongs_to :account belongs_to :target_account, class_name: 'Account' @@ -59,6 +62,14 @@ class Report < ApplicationRecord end def resolve!(acting_account) + if account_id == -99 && target_account.trust_level == Account::TRUST_LEVELS[:untrusted] + # This is an automated report and it is being dismissed, so it's + # a false positive, in which case update the account's trust level + # to prevent further spam checks + + target_account.update(trust_level: Account::TRUST_LEVELS[:trusted]) + end + RemovalWorker.push_bulk(Status.with_discarded.discarded.where(id: status_ids).pluck(:id)) { |status_id| [status_id, { immediate: true }] } update!(action_taken: true, action_taken_by_account_id: acting_account.id) end diff --git a/app/models/status.rb b/app/models/status.rb index f4284f771..31e77770d 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -35,6 +35,9 @@ class Status < ApplicationRecord include Paginable include Cacheable include StatusThreadingConcern + include RateLimitable + + rate_limit by: :account, family: :statuses self.discard_column = :deleted_at @@ -145,10 +148,12 @@ class Status < ApplicationRecord ids += mentions.where(account: Account.local).pluck(:account_id) ids += favourites.where(account: Account.local).pluck(:account_id) ids += reblogs.where(account: Account.local).pluck(:account_id) + ids += bookmarks.where(account: Account.local).pluck(:account_id) else ids += preloaded.mentions[id] || [] ids += preloaded.favourites[id] || [] ids += preloaded.reblogs[id] || [] + ids += preloaded.bookmarks[id] || [] end ids.uniq @@ -416,6 +421,21 @@ class Status < ApplicationRecord end end + def from_text(text) + return [] if text.blank? + + text.scan(FetchLinkCardService::URL_PATTERN).map(&:first).uniq.map do |url| + status = begin + if TagManager.instance.local_url?(url) + ActivityPub::TagManager.instance.uri_to_resource(url, Status) + else + EntityCache.instance.status(url) + end + end + status&.distributable? ? status : nil + end.compact + end + private def timeline_scope(local_only = false) |