Skip to content

Commit

Permalink
Merge pull request glitch-soc#2579 from ClearlyClaire/glitch-soc/merg…
Browse files Browse the repository at this point in the history
…e-upstream

Merge upstream changes up to f866413
  • Loading branch information
ClearlyClaire authored Jan 19, 2024
2 parents 07b6777 + 528a8fa commit 915cd36
Show file tree
Hide file tree
Showing 25 changed files with 225 additions and 299 deletions.
3 changes: 0 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,9 @@ Rails/WhereExists:
- 'app/lib/feed_manager.rb'
- 'app/lib/status_cache_hydrator.rb'
- 'app/lib/suspicious_sign_in_detector.rb'
- 'app/models/concerns/account/interactions.rb'
- 'app/models/featured_tag.rb'
- 'app/models/poll.rb'
- 'app/models/session_activation.rb'
- 'app/models/status.rb'
- 'app/models/user.rb'
- 'app/policies/status_policy.rb'
- 'app/serializers/rest/announcement_serializer.rb'
- 'app/serializers/rest/tag_serializer.rb'
Expand Down
15 changes: 11 additions & 4 deletions app/controllers/api/v1/peers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def set_domains
@domains = InstancesIndex.query(function_score: {
query: {
prefix: {
domain: TagManager.instance.normalize_domain(params[:q].strip),
domain: normalized_domain,
},
},

Expand All @@ -37,11 +37,18 @@ def set_domains
},
}).limit(10).pluck(:domain)
else
domain = params[:q].strip
domain = TagManager.instance.normalize_domain(domain)
@domains = Instance.searchable.where(Instance.arel_table[:domain].matches("#{Instance.sanitize_sql_like(domain)}%", false, true)).limit(10).pluck(:domain)
domain = normalized_domain
@domains = Instance.searchable.domain_starts_with(domain).limit(10).pluck(:domain)
end
rescue Addressable::URI::InvalidURIError
@domains = []
end

def normalized_domain
TagManager.instance.normalize_domain(query_value)
end

def query_value
params[:q].strip
end
end
1 change: 0 additions & 1 deletion app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ class Account < ApplicationRecord
scope :alphabetic, -> { order(domain: :asc, username: :asc) }
scope :matches_username, ->(value) { where('lower((username)::text) LIKE lower(?)', "#{value}%") }
scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) }
scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
scope :without_unapproved, -> { left_outer_joins(:user).merge(User.approved.confirmed).or(remote) }
scope :searchable, -> { without_unapproved.without_suspended.where(moved_to_account_id: nil) }
scope :discoverable, -> { searchable.without_silenced.where(discoverable: true).joins(:account_stat) }
Expand Down
7 changes: 5 additions & 2 deletions app/models/appeal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ class Appeal < ApplicationRecord

belongs_to :account
belongs_to :strike, class_name: 'AccountWarning', foreign_key: 'account_warning_id', inverse_of: :appeal
belongs_to :approved_by_account, class_name: 'Account', optional: true
belongs_to :rejected_by_account, class_name: 'Account', optional: true

with_options class_name: 'Account', optional: true do
belongs_to :approved_by_account
belongs_to :rejected_by_account
end

validates :text, presence: true, length: { maximum: 2_000 }
validates :account_warning_id, uniqueness: true
Expand Down
26 changes: 13 additions & 13 deletions app/models/concerns/account/interactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def unblock_domain!(other_domain)
end

def following?(other_account)
active_relationships.where(target_account: other_account).exists?
active_relationships.exists?(target_account: other_account)
end

def following_anyone?
Expand All @@ -199,51 +199,51 @@ def followed_by?(other_account)
end

def blocking?(other_account)
block_relationships.where(target_account: other_account).exists?
block_relationships.exists?(target_account: other_account)
end

def domain_blocking?(other_domain)
domain_blocks.where(domain: other_domain).exists?
domain_blocks.exists?(domain: other_domain)
end

def muting?(other_account)
mute_relationships.where(target_account: other_account).exists?
mute_relationships.exists?(target_account: other_account)
end

def muting_conversation?(conversation)
conversation_mutes.where(conversation: conversation).exists?
conversation_mutes.exists?(conversation: conversation)
end

def muting_notifications?(other_account)
mute_relationships.where(target_account: other_account, hide_notifications: true).exists?
mute_relationships.exists?(target_account: other_account, hide_notifications: true)
end

def muting_reblogs?(other_account)
active_relationships.where(target_account: other_account, show_reblogs: false).exists?
active_relationships.exists?(target_account: other_account, show_reblogs: false)
end

def requested?(other_account)
follow_requests.where(target_account: other_account).exists?
follow_requests.exists?(target_account: other_account)
end

def favourited?(status)
status.proper.favourites.where(account: self).exists?
status.proper.favourites.exists?(account: self)
end

def bookmarked?(status)
status.proper.bookmarks.where(account: self).exists?
status.proper.bookmarks.exists?(account: self)
end

def reblogged?(status)
status.proper.reblogs.where(account: self).exists?
status.proper.reblogs.exists?(account: self)
end

def pinned?(status)
status_pins.where(status: status).exists?
status_pins.exists?(status: status)
end

def endorsed?(account)
account_pins.where(target_account: account).exists?
account_pins.exists?(target_account: account)
end

def status_matches_filters(status)
Expand Down
2 changes: 0 additions & 2 deletions app/models/domain_allow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class DomainAllow < ApplicationRecord

validates :domain, presence: true, uniqueness: true, domain: true

scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }

def to_log_human_identifier
domain
end
Expand Down
1 change: 0 additions & 1 deletion app/models/domain_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class DomainBlock < ApplicationRecord
has_many :accounts, foreign_key: :domain, primary_key: :domain, inverse_of: false, dependent: nil
delegate :count, to: :accounts, prefix: true

scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
scope :with_user_facing_limitations, -> { where(severity: [:silence, :suspend]) }
scope :with_limitations, -> { where(severity: [:silence, :suspend]).or(where(reject_media: true)) }
scope :by_severity, -> { in_order_of(:severity, %w(noop silence suspend)).order(:domain) }
Expand Down
6 changes: 4 additions & 2 deletions app/models/email_domain_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class EmailDomainBlock < ApplicationRecord
include DomainNormalizable
include Paginable

belongs_to :parent, class_name: 'EmailDomainBlock', optional: true
has_many :children, class_name: 'EmailDomainBlock', foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
with_options class_name: 'EmailDomainBlock' do
belongs_to :parent, optional: true
has_many :children, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
end

validates :domain, presence: true, uniqueness: true, domain: true

Expand Down
16 changes: 12 additions & 4 deletions app/models/featured_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def increment(timestamp)
end

def decrement(deleted_status_id)
update(statuses_count: [0, statuses_count - 1].max, last_status_at: account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag).where.not(id: deleted_status_id).select(:created_at).first&.created_at)
update(statuses_count: [0, statuses_count - 1].max, last_status_at: visible_tagged_account_statuses.where.not(id: deleted_status_id).select(:created_at).first&.created_at)
end

private
Expand All @@ -55,8 +55,8 @@ def set_tag
end

def reset_data
self.statuses_count = account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag).count
self.last_status_at = account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag).select(:created_at).first&.created_at
self.statuses_count = visible_tagged_account_statuses.count
self.last_status_at = visible_tagged_account_statuses.select(:created_at).first&.created_at
end

def validate_featured_tags_limit
Expand All @@ -66,6 +66,14 @@ def validate_featured_tags_limit
end

def validate_tag_uniqueness
errors.add(:name, :taken) if FeaturedTag.by_name(name).where(account_id: account_id).exists?
errors.add(:name, :taken) if tag_already_featured_for_account?
end

def tag_already_featured_for_account?
FeaturedTag.by_name(name).exists?(account_id: account_id)
end

def visible_tagged_account_statuses
account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag)
end
end
1 change: 1 addition & 0 deletions app/models/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Instance < ApplicationRecord

scope :searchable, -> { where.not(domain: DomainBlock.select(:domain)) }
scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
scope :domain_starts_with, ->(value) { where(arel_table[:domain].matches("#{sanitize_sql_like(value)}%", false, true)) }
scope :by_domain_and_subdomains, ->(domain) { where("reverse('.' || domain) LIKE reverse(?)", "%.#{domain}") }

def self.refresh
Expand Down
7 changes: 5 additions & 2 deletions app/models/poll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ class Poll < ApplicationRecord
belongs_to :status

has_many :votes, class_name: 'PollVote', inverse_of: :poll, dependent: :delete_all
has_many :voters, -> { group('accounts.id') }, through: :votes, class_name: 'Account', source: :account
has_many :local_voters, -> { group('accounts.id').merge(Account.local) }, through: :votes, class_name: 'Account', source: :account

with_options class_name: 'Account', source: :account, through: :votes do
has_many :voters, -> { group('accounts.id') }
has_many :local_voters, -> { group('accounts.id').merge(Account.local) }
end

has_many :notifications, as: :activity, dependent: :destroy

Expand Down
9 changes: 6 additions & 3 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ class Report < ApplicationRecord
rate_limit by: :account, family: :reports

belongs_to :account
belongs_to :target_account, class_name: 'Account'
belongs_to :action_taken_by_account, class_name: 'Account', optional: true
belongs_to :assigned_account, class_name: 'Account', optional: true

with_options class_name: 'Account' do
belongs_to :target_account
belongs_to :action_taken_by_account, optional: true
belongs_to :assigned_account, optional: true
end

has_many :notes, class_name: 'ReportNote', inverse_of: :report, dependent: :destroy
has_many :notifications, as: :activity, dependent: :destroy
Expand Down
6 changes: 4 additions & 2 deletions app/models/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ class Status < ApplicationRecord
belongs_to :conversation, optional: true
belongs_to :preloadable_poll, class_name: 'Poll', foreign_key: 'poll_id', optional: true, inverse_of: false

belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies, optional: true
belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, optional: true
with_options class_name: 'Status', optional: true do
belongs_to :thread, foreign_key: 'in_reply_to_id', inverse_of: :replies
belongs_to :reblog, foreign_key: 'reblog_of_id', inverse_of: :reblogs
end

has_many :favourites, inverse_of: :status, dependent: :destroy
has_many :bookmarks, inverse_of: :status, dependent: :destroy
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def wrap_email_confirmation
end

def sign_up_from_ip_requires_approval?
!sign_up_ip.nil? && IpBlock.where(severity: :sign_up_requires_approval).where('ip >>= ?', sign_up_ip.to_s).exists?
sign_up_ip.present? && IpBlock.sign_up_requires_approval.exists?(['ip >>= ?', sign_up_ip.to_s])
end

def sign_up_email_requires_approval?
Expand Down
3 changes: 1 addition & 2 deletions db/migrate/20180812173710_copy_status_stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def down
private

def supports_upsert?
version = select_one("SELECT current_setting('server_version_num') AS v")['v'].to_i
version >= 90_500
ActiveRecord::Base.connection.database_version >= 90_500
end

def up_fast
Expand Down
3 changes: 1 addition & 2 deletions db/migrate/20181116173541_copy_account_stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def down
private

def supports_upsert?
version = select_one("SELECT current_setting('server_version_num') AS v")['v'].to_i
version >= 90_500
ActiveRecord::Base.connection.database_version >= 90_500
end

def up_fast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def down

def supports_concurrent_reindex?
@supports_concurrent_reindex ||= begin
version = select_one("SELECT current_setting('server_version_num') AS v")['v'].to_i
version >= 120_000
ActiveRecord::Base.connection.database_version >= 120_000
end
end

Expand Down
Loading

0 comments on commit 915cd36

Please sign in to comment.