From 0160d1d9b5195e5d8fcb94b29f74634b129439b4 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 3 Nov 2016 13:28:36 +0100 Subject: [PATCH] Enable caching for some rabl views --- Gemfile.lock | 2 +- app/models/favourite.rb | 2 +- app/models/status.rb | 2 +- app/views/accounts/followers.html.haml | 2 +- app/views/accounts/following.html.haml | 2 +- app/views/accounts/show.html.haml | 2 +- app/views/api/v1/accounts/show.rabl | 1 + app/views/api/v1/statuses/_media.rabl | 4 ++++ app/views/api/v1/statuses/_mention.rabl | 3 +++ app/views/api/v1/statuses/_show.rabl | 21 ++++++++++++++++++ app/views/api/v1/statuses/show.rabl | 29 +++---------------------- 11 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 app/views/api/v1/statuses/_media.rabl create mode 100644 app/views/api/v1/statuses/_mention.rabl create mode 100644 app/views/api/v1/statuses/_show.rabl diff --git a/Gemfile.lock b/Gemfile.lock index 9afb47788f..c9756f057d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -205,7 +205,7 @@ GEM pry-rails (0.3.4) pry (>= 0.9.10) puma (3.6.0) - rabl (0.13.0) + rabl (0.13.1) activesupport (>= 2.3.14) rack (2.0.1) rack-attack (5.0.1) diff --git a/app/models/favourite.rb b/app/models/favourite.rb index 4f9b209425..8b8082f1ec 100644 --- a/app/models/favourite.rb +++ b/app/models/favourite.rb @@ -2,7 +2,7 @@ class Favourite < ApplicationRecord include Streamable belongs_to :account, inverse_of: :favourites - belongs_to :status, inverse_of: :favourites + belongs_to :status, inverse_of: :favourites, touch: true validates :status_id, uniqueness: { scope: :account_id } diff --git a/app/models/status.rb b/app/models/status.rb index 19c0488d01..1cd56c97bd 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -5,7 +5,7 @@ class Status < ApplicationRecord belongs_to :account, -> { with_counters }, inverse_of: :statuses belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies - belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs + belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, touch: true has_many :favourites, inverse_of: :status, dependent: :destroy has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy diff --git a/app/views/accounts/followers.html.haml b/app/views/accounts/followers.html.haml index 5bd3d08092..7a6e270ac6 100644 --- a/app/views/accounts/followers.html.haml +++ b/app/views/accounts/followers.html.haml @@ -8,6 +8,6 @@ - if @followers.empty? = render partial: 'nothing_here' - else - = render partial: 'grid_card', collection: @followers, as: :account + = render partial: 'grid_card', collection: @followers, as: :account, cached: true = will_paginate @followers, pagination_options diff --git a/app/views/accounts/following.html.haml b/app/views/accounts/following.html.haml index b20ee5318f..95843f64d0 100644 --- a/app/views/accounts/following.html.haml +++ b/app/views/accounts/following.html.haml @@ -9,6 +9,6 @@ - if @following.empty? = render partial: 'nothing_here' - else - = render partial: 'grid_card', collection: @following, as: :account + = render partial: 'grid_card', collection: @following, as: :account, cached: true = will_paginate @following, pagination_options diff --git a/app/views/accounts/show.html.haml b/app/views/accounts/show.html.haml index 563b438495..b11a59513f 100644 --- a/app/views/accounts/show.html.haml +++ b/app/views/accounts/show.html.haml @@ -12,6 +12,6 @@ = render partial: 'nothing_here' - else .activity-stream - = render partial: 'stream_entries/status', collection: @statuses, as: :status + = render partial: 'stream_entries/status', collection: @statuses, as: :status, cached: true = will_paginate @statuses, pagination_options diff --git a/app/views/api/v1/accounts/show.rabl b/app/views/api/v1/accounts/show.rabl index 0d9e268a19..08199ec215 100644 --- a/app/views/api/v1/accounts/show.rabl +++ b/app/views/api/v1/accounts/show.rabl @@ -1,4 +1,5 @@ object @account +cache @account attributes :id, :username, :acct, :display_name, :note diff --git a/app/views/api/v1/statuses/_media.rabl b/app/views/api/v1/statuses/_media.rabl new file mode 100644 index 0000000000..e4ceef7630 --- /dev/null +++ b/app/views/api/v1/statuses/_media.rabl @@ -0,0 +1,4 @@ +attributes :id, :remote_url, :type + +node(:url) { |media| full_asset_url(media.file.url) } +node(:preview_url) { |media| full_asset_url(media.file.url(:small)) } diff --git a/app/views/api/v1/statuses/_mention.rabl b/app/views/api/v1/statuses/_mention.rabl new file mode 100644 index 0000000000..07b3d1f616 --- /dev/null +++ b/app/views/api/v1/statuses/_mention.rabl @@ -0,0 +1,3 @@ +node(:url) { |mention| TagManager.instance.url_for(mention.account) } +node(:acct) { |mention| mention.account.acct } +node(:id) { |mention| mention.account_id } diff --git a/app/views/api/v1/statuses/_show.rabl b/app/views/api/v1/statuses/_show.rabl new file mode 100644 index 0000000000..8e7cd97eed --- /dev/null +++ b/app/views/api/v1/statuses/_show.rabl @@ -0,0 +1,21 @@ +attributes :id, :created_at, :in_reply_to_id + +node(:uri) { |status| TagManager.instance.uri_for(status) } +node(:content) { |status| Formatter.instance.format(status) } +node(:url) { |status| TagManager.instance.url_for(status) } +node(:reblogs_count) { |status| status.reblogs_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 + extends 'api/v1/accounts/show' +end + +child :media_attachments, object_root: false do + extends 'api/v1/statuses/_media' +end + +child :mentions, object_root: false do + extends 'api/v1/statuses/_mention' +end diff --git a/app/views/api/v1/statuses/show.rabl b/app/views/api/v1/statuses/show.rabl index 69dcd10584..f78716e1b9 100644 --- a/app/views/api/v1/statuses/show.rabl +++ b/app/views/api/v1/statuses/show.rabl @@ -1,31 +1,8 @@ object @status -attributes :id, :created_at, :in_reply_to_id +cache @status -node(:uri) { |status| TagManager.instance.uri_for(status) } -node(:content) { |status| Formatter.instance.format(status) } -node(:url) { |status| TagManager.instance.url_for(status) } -node(:reblogs_count) { |status| status.reblogs_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) } +extends 'api/v1/statuses/_show' child :reblog => :reblog do - extends('api/v1/statuses/show') -end - -child :account do - extends('api/v1/accounts/show') -end - -child :media_attachments, object_root: false do - attributes :id, :remote_url, :type - - node(:url) { |media| full_asset_url(media.file.url) } - node(:preview_url) { |media| full_asset_url(media.file.url(:small)) } -end - -child :mentions, object_root: false do - node(:url) { |mention| TagManager.instance.url_for(mention.account) } - node(:acct) { |mention| mention.account.acct } - node(:id) { |mention| mention.account_id } + extends 'api/v1/statuses/_show' end