Skip to content

Commit

Permalink
Refactoring relations_map (mastodon#24195)
Browse files Browse the repository at this point in the history
  • Loading branch information
noellabo committed Mar 21, 2023
1 parent 9f8d160 commit 38c84f5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 35 deletions.
15 changes: 15 additions & 0 deletions app/models/concerns/account_interactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,21 @@ def local_followers_hash
end
end

def relations_map(account_ids, domains = nil, **options)
relations = {
blocked_by: Account.blocked_by_map(account_ids, id),
following: Account.following_map(account_ids, id),
}

return relations if options[:skip_blocking_and_muting]

relations.merge!({
blocking: Account.blocking_map(account_ids, id),
muting: Account.muting_map(account_ids, id),
domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, id),
})
end

private

def remove_potential_friendship(other_account)
Expand Down
14 changes: 1 addition & 13 deletions app/models/concerns/status_threading_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ 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 = relations_map_for_account(account, account_ids, domains)
relations = account&.relations_map(account_ids, domains) || {}

statuses.reject! { |status| StatusFilter.new(status, account, relations).filtered? }

Expand Down Expand Up @@ -108,16 +108,4 @@ def promote_by!(arr)

arr
end

def relations_map_for_account(account, account_ids, domains)
return {} if account.nil?

{
blocking: Account.blocking_map(account_ids, account.id),
blocked_by: Account.blocked_by_map(account_ids, account.id),
muting: Account.muting_map(account_ids, account.id),
following: Account.following_map(account_ids, account.id),
domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, account.id),
}
end
end
12 changes: 1 addition & 11 deletions app/services/import_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def import_bookmarks!
end

account_ids = statuses.map(&:account_id)
preloaded_relations = relations_map_for_account(@account, account_ids)
preloaded_relations = @account.relations_map(account_ids, skip_blocking_and_muting: true)

statuses.keep_if { |status| StatusPolicy.new(@account, status, preloaded_relations).show? }

Expand All @@ -138,14 +138,4 @@ def parse_import_data!(default_headers)
def import_data
Paperclip.io_adapters.for(@import.data).read.force_encoding(Encoding::UTF_8)
end

def relations_map_for_account(account, account_ids)
{
blocking: {},
blocked_by: Account.blocked_by_map(account_ids, account.id),
muting: {},
following: Account.following_map(account_ids, account.id),
domain_blocking_by_domain: {},
}
end
end
12 changes: 1 addition & 11 deletions app/services/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def perform_statuses_search!
results = definition.limit(@limit).offset(@offset).objects.compact
account_ids = results.map(&:account_id)
account_domains = results.map(&:account_domain)
preloaded_relations = relations_map_for_account(@account, account_ids, account_domains)
preloaded_relations = @account.relations_map(account_ids, account_domains)

results.reject { |status| StatusFilter.new(status, @account, preloaded_relations).filtered? }
rescue Faraday::ConnectionFailed, Parslet::ParseFailed
Expand Down Expand Up @@ -111,16 +111,6 @@ def statuses_search?
@options[:type].blank? || @options[:type] == 'statuses'
end

def relations_map_for_account(account, account_ids, domains)
{
blocking: Account.blocking_map(account_ids, account.id),
blocked_by: Account.blocked_by_map(account_ids, account.id),
muting: Account.muting_map(account_ids, account.id),
following: Account.following_map(account_ids, account.id),
domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, account.id),
}
end

def parsed_query
SearchQueryTransformer.new.apply(SearchQueryParser.new.parse(@query))
end
Expand Down

0 comments on commit 38c84f5

Please sign in to comment.