forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor how public and tag timelines are queried (mastodon#14728)
- Loading branch information
1 parent
39dc1d2
commit b2a61c0
Showing
11 changed files
with
429 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# frozen_string_literal: true | ||
|
||
class PublicFeed < Feed | ||
# @param [Account] account | ||
# @param [Hash] options | ||
# @option [Boolean] :with_replies | ||
# @option [Boolean] :with_reblogs | ||
# @option [Boolean] :local | ||
# @option [Boolean] :remote | ||
# @option [Boolean] :only_media | ||
def initialize(account, options = {}) | ||
@account = account | ||
@options = options | ||
end | ||
|
||
# @param [Integer] limit | ||
# @param [Integer] max_id | ||
# @param [Integer] since_id | ||
# @param [Integer] min_id | ||
# @return [Array<Status>] | ||
def get(limit, max_id = nil, since_id = nil, min_id = nil) | ||
scope = public_scope | ||
|
||
scope.merge!(without_replies_scope) unless with_replies? | ||
scope.merge!(without_reblogs_scope) unless with_reblogs? | ||
scope.merge!(local_only_scope) if local_only? | ||
scope.merge!(remote_only_scope) if remote_only? | ||
scope.merge!(account_filters_scope) if account? | ||
scope.merge!(media_only_scope) if media_only? | ||
|
||
scope.cache_ids.to_a_paginated_by_id(limit, max_id: max_id, since_id: since_id, min_id: min_id) | ||
end | ||
|
||
private | ||
|
||
def with_reblogs? | ||
@options[:with_reblogs] | ||
end | ||
|
||
def with_replies? | ||
@options[:with_replies] | ||
end | ||
|
||
def local_only? | ||
@options[:local] | ||
end | ||
|
||
def remote_only? | ||
@options[:remote] | ||
end | ||
|
||
def account? | ||
@account.present? | ||
end | ||
|
||
def media_only? | ||
@options[:only_media] | ||
end | ||
|
||
def public_scope | ||
Status.with_public_visibility.joins(:account).merge(Account.without_suspended.without_silenced) | ||
end | ||
|
||
def local_only_scope | ||
Status.local | ||
end | ||
|
||
def remote_only_scope | ||
Status.remote | ||
end | ||
|
||
def without_replies_scope | ||
Status.without_replies | ||
end | ||
|
||
def without_reblogs_scope | ||
Status.without_reblogs | ||
end | ||
|
||
def media_only_scope | ||
Status.joins(:media_attachments).group(:id) | ||
end | ||
|
||
def account_filters_scope | ||
Status.not_excluded_by_account(@account).tap do |scope| | ||
scope.merge!(Status.not_domain_blocked_by_account(@account)) unless local_only? | ||
scope.merge!(Status.in_chosen_languages(@account)) if @account.chosen_languages.present? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
class TagFeed < PublicFeed | ||
LIMIT_PER_MODE = 4 | ||
|
||
# @param [Tag] tag | ||
# @param [Account] account | ||
# @param [Hash] options | ||
# @option [Enumerable<String>] :any | ||
# @option [Enumerable<String>] :all | ||
# @option [Enumerable<String>] :none | ||
# @option [Boolean] :local | ||
# @option [Boolean] :remote | ||
# @option [Boolean] :only_media | ||
def initialize(tag, account, options = {}) | ||
@tag = tag | ||
@account = account | ||
@options = options | ||
end | ||
|
||
# @param [Integer] limit | ||
# @param [Integer] max_id | ||
# @param [Integer] since_id | ||
# @param [Integer] min_id | ||
# @return [Array<Status>] | ||
def get(limit, max_id = nil, since_id = nil, min_id = nil) | ||
scope = public_scope | ||
|
||
scope.merge!(tagged_with_any_scope) | ||
scope.merge!(tagged_with_all_scope) | ||
scope.merge!(tagged_with_none_scope) | ||
scope.merge!(local_only_scope) if local_only? | ||
scope.merge!(remote_only_scope) if remote_only? | ||
scope.merge!(account_filters_scope) if account? | ||
scope.merge!(media_only_scope) if media_only? | ||
|
||
scope.cache_ids.to_a_paginated_by_id(limit, max_id: max_id, since_id: since_id, min_id: min_id) | ||
end | ||
|
||
private | ||
|
||
def tagged_with_any_scope | ||
Status.group(:id).tagged_with(tags_for(Array(@tag.name) | Array(@options[:any]))) | ||
end | ||
|
||
def tagged_with_all_scope | ||
Status.group(:id).tagged_with_all(tags_for(@options[:all])) | ||
end | ||
|
||
def tagged_with_none_scope | ||
Status.group(:id).tagged_with_none(tags_for(@options[:none])) | ||
end | ||
|
||
def tags_for(names) | ||
Tag.matching_name(Array(names).take(LIMIT_PER_MODE)) if names.present? | ||
end | ||
end |
Oops, something went wrong.