Merge commit '0d7340380cf3094335d8bc67f7c465e2b154566a' into glitch-soc/merge-upstream

Conflicts:
- app/controllers/application_controller.rb:
  Upstream added an `include` where we had an extra `include` due to
  glitch-soc's theming system.
  Added upstream's new `include`.
This commit is contained in:
Claire
2023-07-13 13:35:15 +02:00
37 changed files with 320 additions and 185 deletions

View File

@@ -7,7 +7,10 @@ class Api::V1::MarkersController < Api::BaseController
before_action :require_user!
def index
@markers = current_user.markers.where(timeline: Array(params[:timeline])).index_by(&:timeline)
with_read_replica do
@markers = current_user.markers.where(timeline: Array(params[:timeline])).index_by(&:timeline)
end
render json: serialize_map(@markers)
end

View File

@@ -9,8 +9,12 @@ class Api::V1::NotificationsController < Api::BaseController
DEFAULT_NOTIFICATIONS_LIMIT = 40
def index
@notifications = load_notifications
render json: @notifications, each_serializer: REST::NotificationSerializer, relationships: StatusRelationshipsPresenter.new(target_statuses_from_notifications, current_user&.account_id)
with_read_replica do
@notifications = load_notifications
@relationships = StatusRelationshipsPresenter.new(target_statuses_from_notifications, current_user&.account_id)
end
render json: @notifications, each_serializer: REST::NotificationSerializer, relationships: @relationships
end
def show

View File

@@ -6,7 +6,7 @@ class Api::V1::Timelines::HomeController < Api::BaseController
after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
def show
ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
with_read_replica do
@statuses = load_statuses
@relationships = StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
end

View File

@@ -11,6 +11,7 @@ class ApplicationController < ActionController::Base
include CacheConcern
include DomainControlHelper
include ThemingConcern
include DatabaseHelper
helper_method :current_account
helper_method :current_session

View File

@@ -0,0 +1,11 @@
# frozen_string_literal: true
module DatabaseHelper
def with_read_replica(&block)
ApplicationRecord.connected_to(role: :read, prevent_writes: true, &block)
end
def with_primary(&block)
ApplicationRecord.connected_to(role: :primary, &block)
end
end

View File

@@ -2,7 +2,7 @@
module DomainControlHelper
def domain_not_allowed?(uri_or_domain)
return if uri_or_domain.blank?
return false if uri_or_domain.blank?
domain = if uri_or_domain.include?('://')
Addressable::URI.parse(uri_or_domain).host

View File

@@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'connection_pool'
require_relative './shared_timed_stack'
require_relative 'shared_timed_stack'
class ConnectionPool::SharedConnectionPool < ConnectionPool
def initialize(options = {}, &block)

View File

@@ -37,7 +37,7 @@ class InlineRenderer
private
def preload_associations_for_status
ActiveRecord::Associations::Preloader.new.preload(@object, {
ActiveRecord::Associations::Preloader.new(records: @object, associations: {
active_mentions: :account,
reblog: {

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true
require_relative './connection_pool/shared_connection_pool'
require_relative 'connection_pool/shared_connection_pool'
class RequestPool
def self.current

View File

@@ -16,7 +16,7 @@ class RSS::Channel < RSS::Element
end
def last_build_date(date)
append_element('lastBuildDate', date.to_formatted_s(:rfc822))
append_element('lastBuildDate', date.to_fs(:rfc822))
end
def image(url, title, link)

View File

@@ -20,7 +20,7 @@ class RSS::Item < RSS::Element
end
def pub_date(date)
append_element('pubDate', date.to_formatted_s(:rfc822))
append_element('pubDate', date.to_fs(:rfc822))
end
def description(str)

View File

@@ -80,7 +80,7 @@ class Announcement < ApplicationRecord
end
end
ActiveRecord::Associations::Preloader.new.preload(records, :custom_emoji)
ActiveRecord::Associations::Preloader.new(records: records, associations: :custom_emoji)
records
end

View File

@@ -122,7 +122,7 @@ module AccountSearch
tsquery = generate_query_for_search(terms)
find_by_sql([BASIC_SEARCH_SQL, { limit: limit, offset: offset, tsquery: tsquery }]).tap do |records|
ActiveRecord::Associations::Preloader.new.preload(records, :account_stat)
ActiveRecord::Associations::Preloader.new(records: records, associations: :account_stat)
end
end
@@ -131,7 +131,7 @@ module AccountSearch
sql_template = following ? ADVANCED_SEARCH_WITH_FOLLOWING : ADVANCED_SEARCH_WITHOUT_FOLLOWING
find_by_sql([sql_template, { id: account.id, limit: limit, offset: offset, tsquery: tsquery }]).tap do |records|
ActiveRecord::Associations::Preloader.new.preload(records, :account_stat)
ActiveRecord::Associations::Preloader.new(records: records, associations: :account_stat)
end
end

View File

@@ -4,41 +4,41 @@ module StatusSafeReblogInsert
extend ActiveSupport::Concern
class_methods do
# This is a hack to ensure that no reblogs of discarded statuses are created,
# as this cannot be enforced through database constraints the same way we do
# for reblogs of deleted statuses.
# 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
#
# To achieve this, 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.
# 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.
#
# Otherwise, the code is kept as close as possible to ActiveRecord::Persistence
# code, and actually calls it if we are not handling a reblog.
# The code is kept similar to ActiveRecord::Persistence code and calls it
# directly when we are not handling a reblog.
def _insert_record(values)
return super unless values.is_a?(Hash) && values['reblog_of_id'].present?
return super unless values.is_a?(Hash) && values['reblog_of_id']&.value.present?
primary_key = self.primary_key
primary_key_value = nil
if primary_key
primary_key_value = values[primary_key]
if !primary_key_value && prefetch_primary_key?
if prefetch_primary_key? && primary_key
values[primary_key] ||= begin
primary_key_value = next_sequence_value
values[primary_key] = primary_key_value
_default_attributes[primary_key].with_cast_value(primary_key_value)
end
end
# The following line is where we differ from stock ActiveRecord implementation
# 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)
# 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
result = connection.insert(im, "#{self} Create", primary_key || false, primary_key_value)
raise ActiveRecord::InvalidForeignKey, "(reblog_of_id)=(#{values['reblog_of_id']}) is not present in table \"statuses\"" if result.nil?
result
connection.insert(im, "#{self} Create", primary_key || false, primary_key_value).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)
@@ -54,9 +54,9 @@ module StatusSafeReblogInsert
binds = []
reblog_bind = nil
values.each do |name, value|
values.each do |name, attribute|
attr = arel_table[name]
bind = predicate_builder.build_bind_attribute(attr.name, value)
bind = predicate_builder.build_bind_attribute(attr.name, attribute.value)
im.columns << attr
binds << bind

View File

@@ -111,7 +111,7 @@ class Notification < ApplicationRecord
# Instead of using the usual `includes`, manually preload each type.
# If polymorphic associations are loaded with the usual `includes`, other types of associations will be loaded more.
ActiveRecord::Associations::Preloader.new.preload(grouped_notifications, associations)
ActiveRecord::Associations::Preloader.new(records: grouped_notifications, associations: associations)
end
unique_target_statuses = notifications.filter_map(&:target_status).uniq

View File

@@ -100,7 +100,10 @@ class InitialStateSerializer < ActiveModel::Serializer
def accounts
store = {}
ActiveRecord::Associations::Preloader.new.preload([object.current_account, object.admin, object.owner, object.disabled_account, object.moved_to_account].compact, [:account_stat, :user, { moved_to_account: [:account_stat, :user] }])
ActiveRecord::Associations::Preloader.new(
records: [object.current_account, object.admin, object.owner, object.disabled_account, object.moved_to_account].compact,
associations: [:account_stat, :user, { moved_to_account: [:account_stat, :user] }]
)
store[object.current_account.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.current_account, serializer: REST::AccountSerializer) if object.current_account
store[object.admin.id.to_s] = ActiveModelSerializers::SerializableResource.new(object.admin, serializer: REST::AccountSerializer) if object.admin

View File

@@ -93,7 +93,7 @@ class AccountSearchService < BaseService
.objects
.compact
ActiveRecord::Associations::Preloader.new.preload(records, :account_stat)
ActiveRecord::Associations::Preloader.new(records: records, associations: :account_stat)
records
rescue Faraday::ConnectionFailed, Parslet::ParseFailed

View File

@@ -8,7 +8,10 @@ class BatchedRemoveStatusService < BaseService
# @param [Hash] options
# @option [Boolean] :skip_side_effects Do not modify feeds and send updates to streaming API
def call(statuses, **options)
ActiveRecord::Associations::Preloader.new.preload(statuses, options[:skip_side_effects] ? :reblogs : [:account, :tags, reblogs: :account])
ActiveRecord::Associations::Preloader.new(
records: statuses,
associations: options[:skip_side_effects] ? :reblogs : [:account, :tags, reblogs: :account]
)
statuses_and_reblogs = statuses.flat_map { |status| [status] + status.reblogs }
@@ -17,7 +20,10 @@ class BatchedRemoveStatusService < BaseService
# rely on direct visibility statuses being relatively rare.
statuses_with_account_conversations = statuses.select(&:direct_visibility?)
ActiveRecord::Associations::Preloader.new.preload(statuses_with_account_conversations, [mentions: :account])
ActiveRecord::Associations::Preloader.new(
records: statuses_with_account_conversations,
associations: [mentions: :account]
)
statuses_with_account_conversations.each do |status|
status.unlink_from_conversations!

View File

@@ -26,7 +26,7 @@
- Trends::PreviewCardProviderFilter::KEYS.each do |key|
= hidden_field_tag key, params[key] if params[key].present?
.batch-table.optional
.batch-table
.batch-table__toolbar
%label.batch-table__toolbar__select.batch-checkbox-all
= check_box_tag :batch_checkbox_all, nil, false

View File

@@ -2,9 +2,10 @@
class FeedInsertWorker
include Sidekiq::Worker
include DatabaseHelper
def perform(status_id, id, type = 'home', options = {})
ApplicationRecord.connected_to(role: :primary) do
with_primary do
@type = type.to_sym
@status = Status.find(status_id)
@options = options.symbolize_keys
@@ -20,7 +21,7 @@ class FeedInsertWorker
end
end
ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
with_read_replica do
check_and_insert
end
rescue ActiveRecord::RecordNotFound

View File

@@ -3,14 +3,15 @@
class MergeWorker
include Sidekiq::Worker
include Redisable
include DatabaseHelper
def perform(from_account_id, into_account_id)
ApplicationRecord.connected_to(role: :primary) do
with_primary do
@from_account = Account.find(from_account_id)
@into_account = Account.find(into_account_id)
end
ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
with_read_replica do
FeedManager.instance.merge_into_home(@from_account, @into_account)
end
rescue ActiveRecord::RecordNotFound

View File

@@ -2,15 +2,16 @@
class RegenerationWorker
include Sidekiq::Worker
include DatabaseHelper
sidekiq_options lock: :until_executed
def perform(account_id, _ = :home)
ApplicationRecord.connected_to(role: :primary) do
with_primary do
@account = Account.find(account_id)
end
ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
with_read_replica do
PrecomputeFeedService.new.call(@account)
end
rescue ActiveRecord::RecordNotFound

View File

@@ -2,16 +2,17 @@
class UnmergeWorker
include Sidekiq::Worker
include DatabaseHelper
sidekiq_options queue: 'pull'
def perform(from_account_id, into_account_id)
ApplicationRecord.connected_to(role: :primary) do
with_primary do
@from_account = Account.find(from_account_id)
@into_account = Account.find(into_account_id)
end
ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
with_read_replica do
FeedManager.instance.unmerge_from_home(@from_account, @into_account)
end
rescue ActiveRecord::RecordNotFound