2023-05-04 12:06:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-12-01 11:00:41 +00:00
|
|
|
module Status::SafeReblogInsert
|
2023-05-04 12:06:20 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class_methods do
|
2023-07-13 07:36:07 +00:00
|
|
|
# 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
|
2023-05-04 12:06:20 +00:00
|
|
|
#
|
2023-07-13 07:36:07 +00:00
|
|
|
# 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.
|
2023-05-04 12:06:20 +00:00
|
|
|
#
|
2023-07-13 07:36:07 +00:00
|
|
|
# The code is kept similar to ActiveRecord::Persistence code and calls it
|
|
|
|
# directly when we are not handling a reblog.
|
2024-11-07 14:58:20 +00:00
|
|
|
#
|
|
|
|
# https://github.com/rails/rails/blob/v7.2.1.1/activerecord/lib/active_record/persistence.rb#L238-L263
|
|
|
|
def _insert_record(connection, values, returning)
|
2023-07-13 07:36:07 +00:00
|
|
|
return super unless values.is_a?(Hash) && values['reblog_of_id']&.value.present?
|
2023-05-04 12:06:20 +00:00
|
|
|
|
|
|
|
primary_key = self.primary_key
|
|
|
|
primary_key_value = nil
|
|
|
|
|
2023-07-13 07:36:07 +00:00
|
|
|
if prefetch_primary_key? && primary_key
|
|
|
|
values[primary_key] ||= begin
|
2023-05-04 12:06:20 +00:00
|
|
|
primary_key_value = next_sequence_value
|
2023-07-13 07:36:07 +00:00
|
|
|
_default_attributes[primary_key].with_cast_value(primary_key_value)
|
2023-05-04 12:06:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-13 07:36:07 +00:00
|
|
|
# The following line departs from stock ActiveRecord
|
|
|
|
# Original code was:
|
2024-11-07 14:58:20 +00:00
|
|
|
# im = Arel::InsertManager.new(arel_table)
|
2023-07-13 07:36:07 +00:00
|
|
|
# Instead, we use a custom builder when a reblog is happening:
|
2023-05-04 12:06:20 +00:00
|
|
|
im = _compile_reblog_insert(values)
|
|
|
|
|
2024-11-07 14:58:20 +00:00
|
|
|
with_connection do |_c|
|
|
|
|
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
|
2023-07-13 07:36:07 +00:00
|
|
|
end
|
2023-05-04 12:06:20 +00:00
|
|
|
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
|
2023-07-13 07:36:07 +00:00
|
|
|
values.each do |name, attribute|
|
2023-05-04 12:06:20 +00:00
|
|
|
attr = arel_table[name]
|
2023-07-13 07:36:07 +00:00
|
|
|
bind = predicate_builder.build_bind_attribute(attr.name, attribute.value)
|
2023-05-04 12:06:20 +00:00
|
|
|
|
|
|
|
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
|