diff options
author | Fire Demon <firedemon@creature.cafe> | 2020-08-15 03:26:55 -0500 |
---|---|---|
committer | Fire Demon <firedemon@creature.cafe> | 2020-08-30 05:45:18 -0500 |
commit | db8e5524db15e19c16b63943ada83b3ac7b75ec6 (patch) | |
tree | 6f366cef23a31194f51494a3c2da8287ea7b15dd /app/controllers/api/v1 | |
parent | 8efbea953c436af514f6b39eb22316100e522c90 (diff) |
[Profiles] Add category filters
Diffstat (limited to 'app/controllers/api/v1')
-rw-r--r-- | app/controllers/api/v1/accounts/statuses_controller.rb | 91 |
1 files changed, 52 insertions, 39 deletions
diff --git a/app/controllers/api/v1/accounts/statuses_controller.rb b/app/controllers/api/v1/accounts/statuses_controller.rb index 3e0feed7e..61633ce36 100644 --- a/app/controllers/api/v1/accounts/statuses_controller.rb +++ b/app/controllers/api/v1/accounts/statuses_controller.rb @@ -17,6 +17,10 @@ class Api::V1::Accounts::StatusesController < Api::BaseController @account = Account.find(params[:account_id]) end + def owner? + @account.id == current_account&.id + end + def load_statuses cached_account_statuses end @@ -26,62 +30,75 @@ class Api::V1::Accounts::StatusesController < Api::BaseController end def account_statuses - return [] if (@account.private && !following?(@account)) || (@account.require_auth && !current_account?) - statuses = truthy_param?(:pinned) ? pinned_scope : permitted_account_statuses - statuses.merge!(only_media_scope) if truthy_param?(:only_media) - statuses.merge!(no_replies_scope) if (current_account&.id != @account.id && !@account.show_replies?) || truthy_param?(:exclude_replies) - statuses.merge!(no_reblogs_scope) if truthy_param?(:exclude_reblogs) - statuses.merge!(hashtag_scope) if params[:tagged].present? - statuses.paginate_by_id(limit_param(DEFAULT_STATUSES_LIMIT), params_slice(:max_id, :since_id, :min_id)) end def permitted_account_statuses - @account.statuses.permitted_for(@account, current_account, user_signed_in: authenticated_or_following?(@account)) + return mentions_scope if truthy_param?(:mentions) + return Status.none if unauthorized? + + @account.statuses.permitted_for( + @account, + current_account, + include_reblogs: include_reblogs?, + include_replies: include_replies?, + only_reblogs: only_reblogs?, + only_replies: only_replies?, + include_unpublished: owner?, + tag: params[:tagged] + ) end - def only_media_scope - Status.where(id: account_media_status_ids) + def unauthorized? + (@account.private && !following?(@account)) || (@account.require_auth && !current_account?) end - def account_media_status_ids - # `SELECT DISTINCT id, updated_at` is too slow, so pluck ids at first, and then select id, updated_at with ids. - # Also, Avoid getting slow by not narrowing down by `statuses.account_id`. - # When narrowing down by `statuses.account_id`, `index_statuses_20180106` will be used - # and the table will be joined by `Merge Semi Join`, so the query will be slow. - @account.statuses.joins(:media_attachments).merge(@account.media_attachments).permitted_for(@account, current_account, user_signed_in: authenticated_or_following?(@account)) - .paginate_by_max_id(limit_param(DEFAULT_STATUSES_LIMIT), params[:max_id], params[:since_id]) - .reorder(id: :desc).distinct(:id).pluck(:id) + def include_reblogs? + params[:include_reblogs].present? ? truthy_param?(:include_reblogs) : !truthy_param?(:exclude_reblogs) end - def pinned_scope - return Status.none if @account.blocking?(current_account) + def include_replies? + return false unless owner? || @account.show_replies? - @account.pinned_statuses + params[:include_replies].present? ? truthy_param?(:include_replies) : !truthy_param?(:exclude_replies) + end + + def only_reblogs? + truthy_param?(:only_reblogs).presence || false end - def no_replies_scope - Status.without_replies + def only_replies? + return false unless owner? || @account.show_replies? + + truthy_param?(:only_replies).presence || false end - def no_reblogs_scope - Status.without_reblogs + def mentions_scope + return Status.none unless current_account? + + Status.mentions_between(@account, current_account) end - def hashtag_scope - tag = Tag.find_normalized(params[:tagged]) + def only_media_scope + Status.where(id: account_media_status_ids) + end - if tag - Status.tagged_with(tag.id) - else - Status.none - end + def account_media_status_ids + @account.media_attachments.attached.reorder(nil).select(:status_id).distinct + end + + def pinned_scope + return Status.none if @account.blocking?(current_account) + + @account.pinned_statuses end def pagination_params(core_params) - params.slice(:limit, :only_media, :exclude_replies).permit(:limit, :only_media, :exclude_replies).merge(core_params) + params.slice(:limit, :only_media, :include_replies, :exclude_replies, :only_replies, :include_reblogs, :exclude_reblogs, :only_relogs, :mentions) + .permit(:limit, :only_media, :include_replies, :exclude_replies, :only_replies, :include_reblogs, :exclude_reblogs, :only_relogs, :mentions) + .merge(core_params) end def insert_pagination_headers @@ -89,15 +106,11 @@ class Api::V1::Accounts::StatusesController < Api::BaseController end def next_path - if records_continue? - api_v1_account_statuses_url pagination_params(max_id: pagination_max_id) - end + api_v1_account_statuses_url pagination_params(max_id: pagination_max_id) if records_continue? end def prev_path - unless @statuses.empty? - api_v1_account_statuses_url pagination_params(min_id: pagination_since_id) - end + api_v1_account_statuses_url pagination_params(min_id: pagination_since_id) unless @statuses.empty? end def records_continue? |