1
0
Fork 0
mirror of https://github.com/mastodon/mastodon.git synced 2024-08-20 21:08:15 -07:00

Update safe reblog insert for Rails 7.2

This commit is contained in:
Matt Jankowski 2024-06-06 10:53:43 -04:00
parent 25cb8c3a99
commit dd6d932f19

View file

@ -15,7 +15,9 @@ module Status::SafeReblogInsert
# #
# The code is kept similar to ActiveRecord::Persistence code and calls it # The code is kept similar to ActiveRecord::Persistence code and calls it
# directly when we are not handling a reblog. # directly when we are not handling a reblog.
def _insert_record(values, returning) #
# https://github.com/rails/rails/blob/7-2-stable/activerecord/lib/active_record/persistence.rb#L238-L263
def _insert_record(connection, values, returning)
return super unless values.is_a?(Hash) && values['reblog_of_id']&.value.present? return super unless values.is_a?(Hash) && values['reblog_of_id']&.value.present?
primary_key = self.primary_key primary_key = self.primary_key
@ -34,12 +36,17 @@ module Status::SafeReblogInsert
# Instead, we use a custom builder when a reblog is happening: # Instead, we use a custom builder when a reblog is happening:
im = _compile_reblog_insert(values) im = _compile_reblog_insert(values)
connection.insert(im, "#{self} Create", primary_key || false, primary_key_value, returning: returning).tap do |result| 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. # 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 # 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? raise ActiveRecord::InvalidForeignKey, "(reblog_of_id)=(#{values['reblog_of_id'].value}) is not present in table \"statuses\"" if result.nil?
end end
end end
end
def _compile_reblog_insert(values) def _compile_reblog_insert(values)
# This is somewhat equivalent to the following code of ActiveRecord::Persistence: # This is somewhat equivalent to the following code of ActiveRecord::Persistence: