From c3d1594576227fcbc45d1da97f474acbd23d831b Mon Sep 17 00:00:00 2001 From: Ben Lubar Date: Sun, 17 Mar 2019 09:39:25 -0500 Subject: Reduce server load caused by anonymous viewing. (#9059) Do not start a session if the current user is not logged in for public-facing pages. Mark pages that don't care about sessions as publicly cacheable. Keep the max age as 0 so proxies and browsers will still try to retrieve an updated version but can still fall back to the stale version if the site is down or too slow. Fixes #9035. --- app/controllers/accounts_controller.rb | 8 +++++++- app/controllers/application_controller.rb | 5 +++++ app/controllers/follower_accounts_controller.rb | 2 ++ app/controllers/statuses_controller.rb | 4 +++- 4 files changed, 17 insertions(+), 2 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index a3410c1ef..abc68d2a4 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -10,6 +10,8 @@ class AccountsController < ApplicationController def show respond_to do |format| format.html do + mark_cacheable! unless user_signed_in? + @body_classes = 'with-modals' @pinned_statuses = [] @endorsed_accounts = @account.endorsed_accounts.to_a.sample(4) @@ -30,17 +32,21 @@ class AccountsController < ApplicationController end format.atom do + mark_cacheable! + @entries = @account.stream_entries.where(hidden: false).with_includes.paginate_by_max_id(PAGE_SIZE, params[:max_id], params[:since_id]) render xml: OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.feed(@account, @entries.reject { |entry| entry.status.nil? })) end format.rss do + mark_cacheable! + @statuses = cache_collection(default_statuses.without_reblogs.without_replies.limit(PAGE_SIZE), Status) render xml: RSS::AccountSerializer.render(@account, @statuses) end format.json do - skip_session! + mark_cacheable! render_cached_json(['activitypub', 'actor', @account], content_type: 'application/activity+json') do ActiveModelSerializers::SerializableResource.new(@account, serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b54e7b008..990aff857 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -151,6 +151,11 @@ class ApplicationController < ActionController::Base response.headers['Vary'] = 'Accept' end + def mark_cacheable! + skip_session! + expires_in 0, public: true + end + def skip_session! request.session_options[:skip] = true end diff --git a/app/controllers/follower_accounts_controller.rb b/app/controllers/follower_accounts_controller.rb index 99cb3676f..aea42bd63 100644 --- a/app/controllers/follower_accounts_controller.rb +++ b/app/controllers/follower_accounts_controller.rb @@ -6,6 +6,8 @@ class FollowerAccountsController < ApplicationController def index respond_to do |format| format.html do + mark_cacheable! unless user_signed_in? + next if @account.user_hides_network? follows diff --git a/app/controllers/statuses_controller.rb b/app/controllers/statuses_controller.rb index 3686bd9fd..fc44d5fb1 100644 --- a/app/controllers/statuses_controller.rb +++ b/app/controllers/statuses_controller.rb @@ -27,6 +27,8 @@ class StatusesController < ApplicationController def show respond_to do |format| format.html do + mark_cacheable! unless user_signed_in? + @body_classes = 'with-modals' set_ancestors @@ -36,7 +38,7 @@ class StatusesController < ApplicationController end format.json do - skip_session! unless @stream_entry.hidden? + mark_cacheable! unless @stream_entry.hidden? render_cached_json(['activitypub', 'note', @status], content_type: 'application/activity+json', public: !@stream_entry.hidden?) do ActiveModelSerializers::SerializableResource.new(@status, serializer: ActivityPub::NoteSerializer, adapter: ActivityPub::Adapter) -- cgit From b8e4c85e69443abb141e666b36606056e86e116c Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 18 Mar 2019 03:53:17 +0100 Subject: Add dormant filter to relationship manager, rename other filters (#10308) Rename "abandoned" to "moved", and "active" to "primary" --- app/controllers/relationships_controller.rb | 19 ++++++++++++------- app/helpers/admin/filter_helper.rb | 2 +- app/views/relationships/show.html.haml | 11 +++++++++-- config/locales/en.yml | 6 ++++-- 4 files changed, 26 insertions(+), 12 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/relationships_controller.rb b/app/controllers/relationships_controller.rb index e6dd65e44..84cb178a6 100644 --- a/app/controllers/relationships_controller.rb +++ b/app/controllers/relationships_controller.rb @@ -31,17 +31,18 @@ class RelationshipsController < ApplicationController def relationships_scope scope = begin if following_relationship? - current_account.following.includes(:account_stat) + current_account.following.joins(:account_stat) else - current_account.followers.includes(:account_stat) + current_account.followers.joins(:account_stat) end end scope.merge!(Follow.recent) scope.merge!(mutual_relationship_scope) if mutual_relationship? - scope.merge!(abandoned_account_scope) if params[:status] == 'abandoned' - scope.merge!(active_account_scope) if params[:status] == 'active' + scope.merge!(moved_account_scope) if params[:status] == 'moved' + scope.merge!(primary_account_scope) if params[:status] == 'primary' scope.merge!(by_domain_scope) if params[:by_domain].present? + scope.merge!(dormant_account_scope) if params[:activity] == 'dormant' scope end @@ -50,14 +51,18 @@ class RelationshipsController < ApplicationController Account.where(id: current_account.following) end - def abandoned_account_scope + def moved_account_scope Account.where.not(moved_to_account_id: nil) end - def active_account_scope + def primary_account_scope Account.where(moved_to_account_id: nil) end + def dormant_account_scope + AccountStat.where(last_status_at: nil).or(AccountStat.where(AccountStat.arel_table[:last_status_at].lt(1.month.ago))) + end + def by_domain_scope Account.where(domain: params[:by_domain]) end @@ -79,7 +84,7 @@ class RelationshipsController < ApplicationController end def current_params - params.slice(:page, :status, :relationship, :by_domain).permit(:page, :status, :relationship, :by_domain) + params.slice(:page, :status, :relationship, :by_domain, :activity).permit(:page, :status, :relationship, :by_domain, :activity) end def action_from_button diff --git a/app/helpers/admin/filter_helper.rb b/app/helpers/admin/filter_helper.rb index 09a356296..4fd36ef42 100644 --- a/app/helpers/admin/filter_helper.rb +++ b/app/helpers/admin/filter_helper.rb @@ -7,7 +7,7 @@ module Admin::FilterHelper CUSTOM_EMOJI_FILTERS = %i(local remote by_domain shortcode).freeze TAGS_FILTERS = %i(hidden).freeze INSTANCES_FILTERS = %i(limited by_domain).freeze - FOLLOWERS_FILTERS = %i(relationship status by_domain).freeze + FOLLOWERS_FILTERS = %i(relationship status by_domain activity).freeze FILTERS = ACCOUNT_FILTERS + REPORT_FILTERS + INVITE_FILTER + CUSTOM_EMOJI_FILTERS + TAGS_FILTERS + INSTANCES_FILTERS + FOLLOWERS_FILTERS diff --git a/app/views/relationships/show.html.haml b/app/views/relationships/show.html.haml index 33a43f1a8..63745119a 100644 --- a/app/views/relationships/show.html.haml +++ b/app/views/relationships/show.html.haml @@ -16,13 +16,20 @@ %strong= t 'relationships.status' %ul %li= filter_link_to t('generic.all'), status: nil - %li= filter_link_to t('relationships.active'), status: 'active' - %li= filter_link_to t('relationships.abandoned'), status: 'abandoned' + %li= filter_link_to t('relationships.primary'), status: 'primary' + %li= filter_link_to t('relationships.moved'), status: 'moved' + + .filter-subset + %strong= t 'relationships.activity' + %ul + %li= filter_link_to t('generic.all'), activity: nil + %li= filter_link_to t('relationships.dormant'), activity: 'dormant' = form_for(@form, url: relationships_path, method: :patch) do |f| = hidden_field_tag :page, params[:page] || 1 = hidden_field_tag :relationship, params[:relationship] = hidden_field_tag :status, params[:status] + = hidden_field_tag :activity, params[:activity] .batch-table .batch-table__toolbar diff --git a/config/locales/en.yml b/config/locales/en.yml index 4f9104eea..9f1081fb8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -751,9 +751,11 @@ en: publishing: Publishing web: Web relationships: - abandoned: Abandoned - active: Active + activity: Account activity + dormant: Dormant + moved: Moved mutual: Mutual + primary: Primary relationship: Relationship remove_selected_domains: Remove all followers from the selected domains remove_selected_followers: Remove selected followers -- cgit