diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/account.rb | 16 | ||||
-rw-r--r-- | app/models/custom_emoji.rb | 7 | ||||
-rw-r--r-- | app/models/direct_feed.rb | 31 | ||||
-rw-r--r-- | app/models/form/admin_settings.rb | 27 | ||||
-rw-r--r-- | app/models/media_attachment.rb | 4 | ||||
-rw-r--r-- | app/models/mute.rb | 1 | ||||
-rw-r--r-- | app/models/public_feed.rb | 14 | ||||
-rw-r--r-- | app/models/status.rb | 70 | ||||
-rw-r--r-- | app/models/tag_feed.rb | 1 | ||||
-rw-r--r-- | app/models/user.rb | 8 |
10 files changed, 162 insertions, 17 deletions
diff --git a/app/models/account.rb b/app/models/account.rb index 291d3e571..d6300a13f 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -74,6 +74,10 @@ class Account < ApplicationRecord include DomainMaterializable include AccountMerging + MAX_DISPLAY_NAME_LENGTH = (ENV['MAX_DISPLAY_NAME_CHARS'] || 30).to_i + MAX_NOTE_LENGTH = (ENV['MAX_BIO_CHARS'] || 500).to_i + MAX_FIELDS = (ENV['MAX_PROFILE_FIELDS'] || 4).to_i + TRUST_LEVELS = { untrusted: 0, trusted: 1, @@ -91,9 +95,9 @@ class Account < ApplicationRecord # Local user validations validates :username, format: { with: /\A[a-z0-9_]+\z/i }, length: { maximum: 30 }, if: -> { local? && will_save_change_to_username? && actor_type != 'Application' } validates_with UnreservedUsernameValidator, if: -> { local? && will_save_change_to_username? } - validates :display_name, length: { maximum: 30 }, if: -> { local? && will_save_change_to_display_name? } - validates :note, note_length: { maximum: 500 }, if: -> { local? && will_save_change_to_note? } - validates :fields, length: { maximum: 4 }, if: -> { local? && will_save_change_to_fields? } + validates :display_name, length: { maximum: MAX_DISPLAY_NAME_LENGTH }, if: -> { local? && will_save_change_to_display_name? } + validates :note, note_length: { maximum: MAX_NOTE_LENGTH }, if: -> { local? && will_save_change_to_note? } + validates :fields, length: { maximum: MAX_FIELDS }, if: -> { local? && will_save_change_to_fields? } scope :remote, -> { where.not(domain: nil) } scope :local, -> { where(domain: nil) } @@ -325,15 +329,13 @@ class Account < ApplicationRecord self[:fields] = fields end - DEFAULT_FIELDS_SIZE = 4 - def build_fields - return if fields.size >= DEFAULT_FIELDS_SIZE + return if fields.size >= MAX_FIELDS tmp = self[:fields] || [] tmp = [] if tmp.is_a?(Hash) - (DEFAULT_FIELDS_SIZE - tmp.size).times do + (MAX_FIELDS - tmp.size).times do tmp << { name: '', value: '' } end diff --git a/app/models/custom_emoji.rb b/app/models/custom_emoji.rb index a85feb73a..d7349dc6a 100644 --- a/app/models/custom_emoji.rb +++ b/app/models/custom_emoji.rb @@ -23,7 +23,8 @@ class CustomEmoji < ApplicationRecord include Attachmentable - LIMIT = 50.kilobytes + LOCAL_LIMIT = (ENV['MAX_EMOJI_SIZE'] || 50.kilobytes).to_i + LIMIT = [LOCAL_LIMIT, (ENV['MAX_REMOTE_EMOJI_SIZE'] || 200.kilobytes).to_i].max SHORTCODE_RE_FRAGMENT = '[a-zA-Z0-9_]{2,}' @@ -40,7 +41,9 @@ class CustomEmoji < ApplicationRecord before_validation :downcase_domain - validates_attachment :image, content_type: { content_type: IMAGE_MIME_TYPES }, presence: true, size: { less_than: LIMIT } + validates_attachment :image, content_type: { content_type: IMAGE_MIME_TYPES }, presence: true + validates_attachment_size :image, less_than: LIMIT, unless: :local? + validates_attachment_size :image, less_than: LOCAL_LIMIT, if: :local? validates :shortcode, uniqueness: { scope: :domain }, format: { with: /\A#{SHORTCODE_RE_FRAGMENT}\z/ }, length: { minimum: 2 } scope :local, -> { where(domain: nil) } diff --git a/app/models/direct_feed.rb b/app/models/direct_feed.rb new file mode 100644 index 000000000..1f2448070 --- /dev/null +++ b/app/models/direct_feed.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +class DirectFeed < Feed + include Redisable + + def initialize(account) + @type = :direct + @id = account.id + @account = account + end + + def get(limit, max_id = nil, since_id = nil, min_id = nil) + unless redis.exists("account:#{@account.id}:regeneration") + statuses = super + return statuses unless statuses.empty? + end + from_database(limit, max_id, since_id, min_id) + end + + private + + 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) + 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? + end + end +end diff --git a/app/models/form/admin_settings.rb b/app/models/form/admin_settings.rb index 6fc7c56fd..0276ec058 100644 --- a/app/models/form/admin_settings.rb +++ b/app/models/form/admin_settings.rb @@ -17,7 +17,8 @@ class Form::AdminSettings timeline_preview show_staff_badge bootstrap_timeline_accounts - theme + flavour + skin min_invite_role activity_api_enabled peers_api_enabled @@ -25,14 +26,20 @@ class Form::AdminSettings preview_sensitive_media custom_css profile_directory + hide_followers_count + enable_keybase + flavour_and_skin thumbnail hero mascot + show_reblogs_in_public_timelines + show_replies_in_public_timelines trends trendable_by_default show_domain_blocks show_domain_blocks_rationale noindex + outgoing_spoilers require_invite_text ).freeze @@ -45,6 +52,10 @@ class Form::AdminSettings show_known_fediverse_at_about_page preview_sensitive_media profile_directory + hide_followers_count + enable_keybase + show_reblogs_in_public_timelines + show_replies_in_public_timelines trends trendable_by_default noindex @@ -57,6 +68,10 @@ class Form::AdminSettings mascot ).freeze + PSEUDO_KEYS = %i( + flavour_and_skin + ).freeze + attr_accessor(*KEYS) validates :site_short_description, :site_description, html: { wrap_with: :p } @@ -78,6 +93,7 @@ class Form::AdminSettings return false unless valid? KEYS.each do |key| + next if PSEUDO_KEYS.include?(key) value = instance_variable_get("@#{key}") if UPLOAD_KEYS.include?(key) && !value.nil? @@ -90,10 +106,19 @@ class Form::AdminSettings end end + def flavour_and_skin + "#{Setting.flavour}/#{Setting.skin}" + end + + def flavour_and_skin=(value) + @flavour, @skin = value.split('/', 2) + end + private def initialize_attributes KEYS.each do |key| + next if PSEUDO_KEYS.include?(key) instance_variable_set("@#{key}", Setting.public_send(key)) if instance_variable_get("@#{key}").nil? end end diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index 1741d20e9..14e6cabae 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -152,8 +152,8 @@ class MediaAttachment < ApplicationRecord all: '-quality 90 -strip +set modify-date +set create-date', }.freeze - IMAGE_LIMIT = 10.megabytes - VIDEO_LIMIT = 40.megabytes + 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 diff --git a/app/models/mute.rb b/app/models/mute.rb index 578345ef6..fe8b6f42c 100644 --- a/app/models/mute.rb +++ b/app/models/mute.rb @@ -6,6 +6,7 @@ # id :bigint(8) not null, primary key # created_at :datetime not null # updated_at :datetime not null +# hide_notifications :boolean default(TRUE), not null # account_id :bigint(8) not null # target_account_id :bigint(8) not null # hide_notifications :boolean default(TRUE), not null diff --git a/app/models/public_feed.rb b/app/models/public_feed.rb index 5e4c3e1ce..2528ef1b6 100644 --- a/app/models/public_feed.rb +++ b/app/models/public_feed.rb @@ -8,6 +8,7 @@ class PublicFeed # @option [Boolean] :local # @option [Boolean] :remote # @option [Boolean] :only_media + # @option [Boolean] :allow_local_only def initialize(account, options = {}) @account = account @options = options @@ -21,6 +22,7 @@ class PublicFeed def get(limit, max_id = nil, since_id = nil, min_id = nil) scope = public_scope + scope.merge!(without_local_only_scope) unless allow_local_only? scope.merge!(without_replies_scope) unless with_replies? scope.merge!(without_reblogs_scope) unless with_reblogs? scope.merge!(local_only_scope) if local_only? @@ -35,6 +37,10 @@ class PublicFeed attr_reader :account, :options + def allow_local_only? + local_account? && (local_only? || options[:allow_local_only]) + end + def with_reblogs? options[:with_reblogs] end @@ -55,6 +61,10 @@ class PublicFeed account.present? end + def local_account? + account&.local? + end + def media_only? options[:only_media] end @@ -83,6 +93,10 @@ class PublicFeed Status.joins(:media_attachments).group(:id) end + def without_local_only_scope + Status.not_local_only + end + def account_filters_scope Status.not_excluded_by_account(account).tap do |scope| scope.merge!(Status.not_domain_blocked_by_account(account)) unless local_only? diff --git a/app/models/status.rb b/app/models/status.rb index 3acf759f9..7b11709fa 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -21,7 +21,10 @@ # account_id :bigint(8) not null # application_id :bigint(8) # in_reply_to_account_id :bigint(8) +# local_only :boolean +# full_status_text :text default(""), not null # poll_id :bigint(8) +# content_type :string # deleted_at :datetime # @@ -77,6 +80,7 @@ class Status < ApplicationRecord validates_with DisallowedHashtagsValidator validates :reblog, uniqueness: { scope: :account }, if: :reblog? validates :visibility, exclusion: { in: %w(direct limited) }, if: :reblog? + validates :content_type, inclusion: { in: %w(text/plain text/markdown text/html) }, allow_nil: true accepts_nested_attributes_for :poll @@ -107,6 +111,8 @@ class Status < ApplicationRecord end } + scope :not_local_only, -> { where(local_only: [false, nil]) } + cache_associated :application, :media_attachments, :conversation, @@ -260,6 +266,8 @@ class Status < ApplicationRecord around_create Mastodon::Snowflake::Callbacks + before_create :set_locality + before_validation :prepare_contents, if: :local? before_validation :set_reblog before_validation :set_visibility @@ -273,6 +281,51 @@ class Status < ApplicationRecord visibilities.keys - %w(direct limited) end + def in_chosen_languages(account) + where(language: nil).or where(language: account.chosen_languages) + end + + def as_direct_timeline(account, limit = 20, max_id = nil, since_id = nil, cache_ids = false) + # 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 + + # _from_me part does not require any timeline filters + query_from_me = where(account_id: account.id) + .where(Status.arel_table[:visibility].eq(3)) + .limit(limit) + .order('statuses.id DESC') + + # _to_me part requires mute and block filter. + # FIXME: may we check mutes.hide_notifications? + query_to_me = Status + .joins(:mentions) + .merge(Mention.where(account_id: account.id)) + .where(Status.arel_table[:visibility].eq(3)) + .limit(limit) + .order('mentions.status_id DESC') + .not_excluded_by_account(account) + + if max_id.present? + query_from_me = query_from_me.where('statuses.id < ?', max_id) + query_to_me = query_to_me.where('mentions.status_id < ?', max_id) + end + + if since_id.present? + query_from_me = query_from_me.where('statuses.id > ?', since_id) + 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 + end + def favourites_map(status_ids, account_id) Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |f, h| h[f.status_id] = true } end @@ -317,7 +370,7 @@ class Status < ApplicationRecord visibility = [:public, :unlisted] if account.nil? - where(visibility: visibility) + where(visibility: visibility).not_local_only elsif target_account.blocking?(account) || (account.domain.present? && target_account.domain_blocking?(account.domain)) # get rid of blocked peeps none elsif account.id == target_account.id # author can see own stuff @@ -351,6 +404,15 @@ class Status < ApplicationRecord end end + def marked_local_only? + # match both with and without U+FE0F (the emoji variation selector) + /#{local_only_emoji}\ufe0f?\z/.match?(content) + end + + def local_only_emoji + '👁' + end + def status_stat super || build_status_stat end @@ -386,6 +448,12 @@ class Status < ApplicationRecord self.sensitive = false if sensitive.nil? end + def set_locality + if account.domain.nil? && !attribute_changed?(:local_only) + self.local_only = marked_local_only? + end + end + def set_conversation self.thread = thread.reblog if thread&.reblog? diff --git a/app/models/tag_feed.rb b/app/models/tag_feed.rb index b8cd63557..fbbdbaae2 100644 --- a/app/models/tag_feed.rb +++ b/app/models/tag_feed.rb @@ -25,6 +25,7 @@ class TagFeed < PublicFeed def get(limit, max_id = nil, since_id = nil, min_id = nil) scope = public_scope + scope.merge!(without_local_only_scope) unless local_account? scope.merge!(tagged_with_any_scope) scope.merge!(tagged_with_all_scope) scope.merge!(tagged_with_none_scope) diff --git a/app/models/user.rb b/app/models/user.rb index 4059c96b5..a1a278004 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -121,11 +121,11 @@ 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, :hide_network, + delegate :auto_play_gif, :default_sensitive, :unfollow_modal, :boost_modal, :favourite_modal, :delete_modal, + :reduce_motion, :system_font_ui, :noindex, :flavour, :skin, :display_media, :hide_network, :hide_followers_count, :expand_spoilers, :default_language, :aggregate_reblogs, :show_application, :advanced_layout, :use_blurhash, :use_pending_items, :trends, :crop_images, - :disable_swiping, + :disable_swiping, :default_content_type, :system_emoji_font, to: :settings, prefix: :setting, allow_nil: false attr_reader :invite_code, :sign_in_token_attempt @@ -205,7 +205,7 @@ class User < ApplicationRecord end def functional? - confirmed? && approved? && !disabled? && !account.suspended? && !account.memorial? && account.moved_to_account_id.nil? + confirmed? && approved? && !disabled? && !account.suspended? && !account.memorial? end def unconfirmed_or_pending? |