Fix reblogged/favourited caching; add API endpoints for who favd/reblogged status

This commit is contained in:
Eugen Rochko 2016-11-03 14:50:22 +01:00
parent c003e70758
commit e4671adc25
5 changed files with 30 additions and 6 deletions

View File

@ -2,18 +2,28 @@ class Api::V1::StatusesController < ApiController
before_action -> { doorkeeper_authorize! :read }, except: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite] before_action -> { doorkeeper_authorize! :read }, except: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite]
before_action -> { doorkeeper_authorize! :write }, only: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite] before_action -> { doorkeeper_authorize! :write }, only: [:create, :destroy, :reblog, :unreblog, :favourite, :unfavourite]
respond_to :json before_action :set_status, only: [:show, :context, :reblogged_by, :favourited_by]
respond_to :json
def show def show
@status = Status.find(params[:id])
end end
def context def context
@status = Status.find(params[:id])
@context = OpenStruct.new({ ancestors: @status.ancestors, descendants: @status.descendants }) @context = OpenStruct.new({ ancestors: @status.ancestors, descendants: @status.descendants })
set_maps([@status] + @context[:ancestors] + @context[:descendants]) set_maps([@status] + @context[:ancestors] + @context[:descendants])
end end
def reblogged_by
@accounts = @status.reblogs.includes(:account).limit(40).map(&:account)
render action: :accounts
end
def favourited_by
@accounts = @status.favourites.includes(:account).limit(40).map(&:account)
render action: :accounts
end
def create def create
@status = PostStatusService.new.call(current_user.account, params[:status], params[:in_reply_to_id].blank? ? nil : Status.find(params[:in_reply_to_id]), params[:media_ids]) @status = PostStatusService.new.call(current_user.account, params[:status], params[:in_reply_to_id].blank? ? nil : Status.find(params[:in_reply_to_id]), params[:media_ids])
render action: :show render action: :show
@ -63,4 +73,10 @@ class Api::V1::StatusesController < ApiController
set_maps(@statuses) set_maps(@statuses)
render action: :index render action: :index
end end
private
def set_status
@status = Status.find(params[:id])
end
end end

View File

@ -1,3 +1,4 @@
cache
attributes :id, :created_at, :in_reply_to_id attributes :id, :created_at, :in_reply_to_id
node(:uri) { |status| TagManager.instance.uri_for(status) } node(:uri) { |status| TagManager.instance.uri_for(status) }
@ -5,8 +6,6 @@ node(:content) { |status| Formatter.instance.format(status) }
node(:url) { |status| TagManager.instance.url_for(status) } node(:url) { |status| TagManager.instance.url_for(status) }
node(:reblogs_count) { |status| status.reblogs_count } node(:reblogs_count) { |status| status.reblogs_count }
node(:favourites_count) { |status| status.favourites_count } node(:favourites_count) { |status| status.favourites_count }
node(:favourited, if: proc { !current_account.nil? }) { |status| defined?(@favourites_map) ? !!@favourites_map[status.id] : current_account.favourited?(status) }
node(:reblogged, if: proc { !current_account.nil? }) { |status| defined?(@reblogs_map) ? !!@reblogs_map[status.id] : current_account.reblogged?(status) }
child :account do child :account do
extends 'api/v1/accounts/show' extends 'api/v1/accounts/show'

View File

@ -0,0 +1,2 @@
collection @accounts
extends 'api/v1/accounts/show'

View File

@ -1,8 +1,13 @@
object @status object @status
cache
extends 'api/v1/statuses/_show' extends 'api/v1/statuses/_show'
node(:favourited, if: proc { !current_account.nil? }) { |status| defined?(@favourites_map) ? !!@favourites_map[status.id] : current_account.favourited?(status) }
node(:reblogged, if: proc { !current_account.nil? }) { |status| defined?(@reblogs_map) ? !!@reblogs_map[status.id] : current_account.reblogged?(status) }
child :reblog => :reblog do child :reblog => :reblog do
extends 'api/v1/statuses/_show' extends 'api/v1/statuses/_show'
node(:favourited, if: proc { !current_account.nil? }) { |status| defined?(@favourites_map) ? !!@favourites_map[status.id] : current_account.favourited?(status) }
node(:reblogged, if: proc { !current_account.nil? }) { |status| defined?(@reblogs_map) ? !!@reblogs_map[status.id] : current_account.reblogged?(status) }
end end

View File

@ -59,6 +59,8 @@ Rails.application.routes.draw do
member do member do
get :context get :context
get :reblogged_by
get :favourited_by
post :reblog post :reblog
post :unreblog post :unreblog