Merge remote-tracking branch 'parent/main' into upstream-20231204
This commit is contained in:
commit
94c2396a34
179 changed files with 1036 additions and 775 deletions
72
app/models/concerns/status/safe_reblog_insert.rb
Normal file
72
app/models/concerns/status/safe_reblog_insert.rb
Normal file
|
@ -0,0 +1,72 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Status::SafeReblogInsert
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
class_methods do
|
||||
# This patch overwrites the built-in ActiveRecord `_insert_record` method to
|
||||
# ensure that no reblogs of discarded statuses are created, as this cannot be
|
||||
# enforced through DB constraints the same way as reblogs of deleted statuses
|
||||
#
|
||||
# We redefine the internal method responsible for issuing the `INSERT`
|
||||
# statement and replace the `INSERT INTO ... VALUES ...` query with an `INSERT
|
||||
# INTO ... SELECT ...` query with a `WHERE deleted_at IS NULL` clause on the
|
||||
# reblogged status to ensure consistency at the database level.
|
||||
#
|
||||
# The code is kept similar to ActiveRecord::Persistence code and calls it
|
||||
# directly when we are not handling a reblog.
|
||||
def _insert_record(values, returning)
|
||||
return super unless values.is_a?(Hash) && values['reblog_of_id']&.value.present?
|
||||
|
||||
primary_key = self.primary_key
|
||||
primary_key_value = nil
|
||||
|
||||
if prefetch_primary_key? && primary_key
|
||||
values[primary_key] ||= begin
|
||||
primary_key_value = next_sequence_value
|
||||
_default_attributes[primary_key].with_cast_value(primary_key_value)
|
||||
end
|
||||
end
|
||||
|
||||
# The following line departs from stock ActiveRecord
|
||||
# Original code was:
|
||||
# im.insert(values.transform_keys { |name| arel_table[name] })
|
||||
# Instead, we use a custom builder when a reblog is happening:
|
||||
im = _compile_reblog_insert(values)
|
||||
|
||||
connection.insert(im, "#{self} Create", primary_key || false, primary_key_value, returning: returning).tap do |result|
|
||||
# Since we are using SELECT instead of VALUES, a non-error `nil` return is possible.
|
||||
# For our purposes, it's equivalent to a foreign key constraint violation
|
||||
raise ActiveRecord::InvalidForeignKey, "(reblog_of_id)=(#{values['reblog_of_id'].value}) is not present in table \"statuses\"" if result.nil?
|
||||
end
|
||||
end
|
||||
|
||||
def _compile_reblog_insert(values)
|
||||
# This is somewhat equivalent to the following code of ActiveRecord::Persistence:
|
||||
# `arel_table.compile_insert(_substitute_values(values))`
|
||||
# The main difference is that we use a `SELECT` instead of a `VALUES` clause,
|
||||
# which means we have to build the `SELECT` clause ourselves and do a bit more
|
||||
# manual work.
|
||||
|
||||
# Instead of using Arel::InsertManager#values, we are going to use Arel::InsertManager#select
|
||||
im = Arel::InsertManager.new
|
||||
im.into(arel_table)
|
||||
|
||||
binds = []
|
||||
reblog_bind = nil
|
||||
values.each do |name, attribute|
|
||||
attr = arel_table[name]
|
||||
bind = predicate_builder.build_bind_attribute(attr.name, attribute.value)
|
||||
|
||||
im.columns << attr
|
||||
binds << bind
|
||||
|
||||
reblog_bind = bind if name == 'reblog_of_id'
|
||||
end
|
||||
|
||||
im.select(arel_table.where(arel_table[:id].eq(reblog_bind)).where(arel_table[:deleted_at].eq(nil)).project(*binds))
|
||||
|
||||
im
|
||||
end
|
||||
end
|
||||
end
|
86
app/models/concerns/status/search_concern.rb
Normal file
86
app/models/concerns/status/search_concern.rb
Normal file
|
@ -0,0 +1,86 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Status::SearchConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
scope :indexable, -> { without_reblogs.where(visibility: [:public, :login], searchability: nil).joins(:account).where(account: { indexable: true }) }
|
||||
scope :remote_dynamic_searchability, -> { remote.where(searchability: [:public, :public_unlisted, :private]) }
|
||||
end
|
||||
|
||||
def searchable_by
|
||||
@searchable_by ||= begin
|
||||
ids = []
|
||||
|
||||
ids << account_id if local?
|
||||
|
||||
ids += mentioned_by
|
||||
ids += favourited_by
|
||||
ids += reblogged_by
|
||||
ids += bookmarked_by
|
||||
ids += emoji_reacted_by
|
||||
ids += referenced_by
|
||||
ids += voted_by if preloadable_poll.present?
|
||||
|
||||
ids.uniq
|
||||
end
|
||||
end
|
||||
|
||||
def mentioned_by
|
||||
@mentioned_by ||= local_mentioned.pluck(:id)
|
||||
end
|
||||
|
||||
def favourited_by
|
||||
@favourited_by ||= local_favorited.pluck(:id)
|
||||
end
|
||||
|
||||
def reblogged_by
|
||||
@reblogged_by ||= local_reblogged.pluck(:id)
|
||||
end
|
||||
|
||||
def bookmarked_by
|
||||
@bookmarked_by ||= local_bookmarked.pluck(:id)
|
||||
end
|
||||
|
||||
def bookmark_categoried_by
|
||||
@bookmark_categoried_by ||= local_bookmark_categoried.pluck(:id).uniq
|
||||
end
|
||||
|
||||
def emoji_reacted_by
|
||||
@emoji_reacted_by ||= local_emoji_reacted.pluck(:id)
|
||||
end
|
||||
|
||||
def referenced_by
|
||||
@referenced_by ||= local_referenced.pluck(:id)
|
||||
end
|
||||
|
||||
def voted_by
|
||||
return [] if preloadable_poll.blank?
|
||||
|
||||
@voted_by ||= preloadable_poll.local_voters.pluck(:id)
|
||||
end
|
||||
|
||||
def searchable_text
|
||||
[
|
||||
spoiler_text,
|
||||
FormattingHelper.extract_status_plain_text(self),
|
||||
preloadable_poll&.options&.join("\n\n"),
|
||||
ordered_media_attachments.map(&:description).join("\n\n"),
|
||||
].compact.join("\n\n")
|
||||
end
|
||||
|
||||
def searchable_properties
|
||||
[].tap do |properties|
|
||||
properties << 'image' if ordered_media_attachments.any?(&:image?)
|
||||
properties << 'video' if ordered_media_attachments.any?(&:video?)
|
||||
properties << 'audio' if ordered_media_attachments.any?(&:audio?)
|
||||
properties << 'media' if with_media?
|
||||
properties << 'poll' if with_poll?
|
||||
properties << 'link' if with_preview_card?
|
||||
properties << 'embed' if preview_card&.video?
|
||||
properties << 'sensitive' if sensitive?
|
||||
properties << 'reply' if reply?
|
||||
properties << 'reference' if with_status_reference?
|
||||
end
|
||||
end
|
||||
end
|
36
app/models/concerns/status/snapshot_concern.rb
Normal file
36
app/models/concerns/status/snapshot_concern.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Status::SnapshotConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many :edits, class_name: 'StatusEdit', inverse_of: :status, dependent: :destroy
|
||||
end
|
||||
|
||||
def edited?
|
||||
edited_at.present?
|
||||
end
|
||||
|
||||
def build_snapshot(account_id: nil, at_time: nil, rate_limit: true)
|
||||
# We don't use `edits#new` here to avoid it having saved when the
|
||||
# status is saved, since we want to control that manually
|
||||
|
||||
StatusEdit.new(
|
||||
status_id: id,
|
||||
text: text,
|
||||
spoiler_text: spoiler_text,
|
||||
markdown: markdown,
|
||||
sensitive: sensitive,
|
||||
ordered_media_attachment_ids: ordered_media_attachment_ids&.dup || media_attachments.pluck(:id),
|
||||
media_descriptions: ordered_media_attachments.map(&:description),
|
||||
poll_options: preloadable_poll&.options&.dup,
|
||||
account_id: account_id || self.account_id,
|
||||
created_at: at_time || edited_at,
|
||||
rate_limit: rate_limit
|
||||
)
|
||||
end
|
||||
|
||||
def snapshot!(**options)
|
||||
build_snapshot(**options).save!
|
||||
end
|
||||
end
|
120
app/models/concerns/status/threading_concern.rb
Normal file
120
app/models/concerns/status/threading_concern.rb
Normal file
|
@ -0,0 +1,120 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Status::ThreadingConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def ancestors(limit, account = nil)
|
||||
find_statuses_from_tree_path(ancestor_ids(limit), account)
|
||||
end
|
||||
|
||||
def descendants(limit, account = nil, depth = nil)
|
||||
find_statuses_from_tree_path(descendant_ids(limit, depth), account, promote: true)
|
||||
end
|
||||
|
||||
def readable_references(account = nil)
|
||||
statuses = references.to_a
|
||||
account_ids = statuses.map(&:account_id).uniq
|
||||
domains = statuses.filter_map(&:account_domain).uniq
|
||||
relations = account&.relations_map(account_ids, domains) || {}
|
||||
statuses.reject! { |status| StatusFilter.new(status, account, relations).filtered? }
|
||||
statuses
|
||||
end
|
||||
|
||||
def self_replies(limit)
|
||||
account.statuses.where(in_reply_to_id: id, visibility: [:public, :unlisted, :public_unlisted, :login]).reorder(id: :asc).limit(limit)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ancestor_ids(limit)
|
||||
key = "ancestors:#{id}"
|
||||
ancestors = Rails.cache.fetch(key)
|
||||
|
||||
if ancestors.nil? || ancestors[:limit] < limit
|
||||
ids = ancestor_statuses(limit).pluck(:id).reverse!
|
||||
Rails.cache.write key, limit: limit, ids: ids
|
||||
ids
|
||||
else
|
||||
ancestors[:ids].last(limit)
|
||||
end
|
||||
end
|
||||
|
||||
def ancestor_statuses(limit)
|
||||
Status.find_by_sql([<<-SQL.squish, id: in_reply_to_id, limit: limit])
|
||||
WITH RECURSIVE search_tree(id, in_reply_to_id, path)
|
||||
AS (
|
||||
SELECT id, in_reply_to_id, ARRAY[id]
|
||||
FROM statuses
|
||||
WHERE id = :id
|
||||
UNION ALL
|
||||
SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id
|
||||
FROM search_tree
|
||||
JOIN statuses ON statuses.id = search_tree.in_reply_to_id
|
||||
WHERE NOT statuses.id = ANY(path)
|
||||
)
|
||||
SELECT id
|
||||
FROM search_tree
|
||||
ORDER BY path
|
||||
LIMIT :limit
|
||||
SQL
|
||||
end
|
||||
|
||||
def descendant_ids(limit, depth)
|
||||
# use limit + 1 and depth + 1 because 'self' is included
|
||||
depth += 1 if depth.present?
|
||||
limit += 1 if limit.present?
|
||||
|
||||
descendants_with_self = Status.find_by_sql([<<-SQL.squish, id: id, limit: limit, depth: depth])
|
||||
WITH RECURSIVE search_tree(id, path) AS (
|
||||
SELECT id, ARRAY[id]
|
||||
FROM statuses
|
||||
WHERE id = :id
|
||||
UNION ALL
|
||||
SELECT statuses.id, path || statuses.id
|
||||
FROM search_tree
|
||||
JOIN statuses ON statuses.in_reply_to_id = search_tree.id
|
||||
WHERE COALESCE(array_length(path, 1) < :depth, TRUE) AND NOT statuses.id = ANY(path)
|
||||
)
|
||||
SELECT id
|
||||
FROM search_tree
|
||||
ORDER BY path
|
||||
LIMIT :limit
|
||||
SQL
|
||||
|
||||
descendants_with_self.pluck(:id) - [id]
|
||||
end
|
||||
|
||||
def find_statuses_from_tree_path(ids, account, promote: false)
|
||||
statuses = Status.with_accounts(ids).to_a
|
||||
account_ids = statuses.map(&:account_id).uniq
|
||||
domains = statuses.filter_map(&:account_domain).uniq
|
||||
relations = account&.relations_map(account_ids, domains) || {}
|
||||
|
||||
statuses.reject! { |status| StatusFilter.new(status, account, relations).filtered? }
|
||||
|
||||
# Order ancestors/descendants by tree path
|
||||
statuses.sort_by! { |status| ids.index(status.id) }
|
||||
|
||||
# Bring self-replies to the top
|
||||
if promote
|
||||
promote_by!(statuses) { |status| status.in_reply_to_account_id == status.account_id }
|
||||
else
|
||||
statuses
|
||||
end
|
||||
end
|
||||
|
||||
def promote_by!(arr)
|
||||
insert_at = arr.find_index { |item| !yield(item) }
|
||||
|
||||
return arr if insert_at.nil?
|
||||
|
||||
arr.each_with_index do |item, index|
|
||||
next if index <= insert_at || !yield(item)
|
||||
|
||||
arr.insert(insert_at, arr.delete_at(index))
|
||||
insert_at += 1
|
||||
end
|
||||
|
||||
arr
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue