diff options
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/admin/pubsubhubbub_controller.rb | 11 | ||||
-rw-r--r-- | app/controllers/api/push_controller.rb | 37 | ||||
-rw-r--r-- | app/controllers/api/v1/accounts_controller.rb | 31 | ||||
-rw-r--r-- | app/controllers/api/v1/notifications_controller.rb | 23 | ||||
-rw-r--r-- | app/controllers/api/v1/statuses_controller.rb | 10 | ||||
-rw-r--r-- | app/controllers/api/v1/timelines_controller.rb | 25 | ||||
-rw-r--r-- | app/controllers/api_controller.rb | 2 | ||||
-rw-r--r-- | app/controllers/application_controller.rb | 24 | ||||
-rw-r--r-- | app/controllers/settings/preferences_controller.rb | 7 |
9 files changed, 90 insertions, 80 deletions
diff --git a/app/controllers/admin/pubsubhubbub_controller.rb b/app/controllers/admin/pubsubhubbub_controller.rb new file mode 100644 index 000000000..7e6bc75ea --- /dev/null +++ b/app/controllers/admin/pubsubhubbub_controller.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class Admin::PubsubhubbubController < ApplicationController + before_action :require_admin! + + layout 'public' + + def index + @subscriptions = Subscription.order('id desc').includes(:account).paginate(page: params[:page], per_page: 40) + end +end diff --git a/app/controllers/api/push_controller.rb b/app/controllers/api/push_controller.rb new file mode 100644 index 000000000..78d4e36e6 --- /dev/null +++ b/app/controllers/api/push_controller.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +class Api::PushController < ApiController + def update + mode = params['hub.mode'] + topic = params['hub.topic'] + callback = params['hub.callback'] + lease_seconds = params['hub.lease_seconds'] + secret = params['hub.secret'] + + case mode + when 'subscribe' + response, status = Pubsubhubbub::SubscribeService.new.call(topic_to_account(topic), callback, secret, lease_seconds) + when 'unsubscribe' + response, status = Pubsubhubbub::UnsubscribeService.new.call(topic_to_account(topic), callback) + else + response = "Unknown mode: #{mode}" + status = 422 + end + + render plain: response, status: status + end + + private + + def topic_to_account(topic_url) + return if topic_url.blank? + + uri = Addressable::URI.parse(topic_url) + params = Rails.application.routes.recognize_path(uri.path) + domain = uri.host + (uri.port ? ":#{uri.port}" : '') + + return unless TagManager.instance.local_domain?(domain) && params[:controller] == 'accounts' && params[:action] == 'show' && params[:format] == 'atom' + + Account.find_local(params[:username]) + end +end diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb index 4ae900583..9a356196c 100644 --- a/app/controllers/api/v1/accounts_controller.rb +++ b/app/controllers/api/v1/accounts_controller.rb @@ -46,19 +46,9 @@ class Api::V1::AccountsController < ApiController render action: :index end - def common_followers - @accounts = @account.common_followers_with(current_user.account) - render action: :index - end - - def suggestions - @accounts = FollowSuggestion.get(current_user.account_id) - render action: :index - end - def statuses @statuses = @account.statuses.paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a - @statuses = cache(@statuses) + @statuses = cache_collection(@statuses, Status) set_maps(@statuses) set_counters_maps(@statuses) @@ -121,23 +111,4 @@ class Api::V1::AccountsController < ApiController @followed_by = Account.followed_by_map([@account.id], current_user.account_id) @blocking = Account.blocking_map([@account.id], current_user.account_id) end - - def cache(raw) - uncached_ids = [] - cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key)) - - raw.each do |status| - uncached_ids << status.id unless cached_keys_with_value.key?(status.cache_key) - end - - unless uncached_ids.empty? - uncached = Status.where(id: uncached_ids).with_includes.map { |s| [s.id, s] }.to_h - - uncached.values.each do |status| - Rails.cache.write(status.cache_key, status) - end - end - - raw.map { |status| cached_keys_with_value[status.cache_key] || uncached[status.id] } - end end diff --git a/app/controllers/api/v1/notifications_controller.rb b/app/controllers/api/v1/notifications_controller.rb index d74b99a86..a24e0beb7 100644 --- a/app/controllers/api/v1/notifications_controller.rb +++ b/app/controllers/api/v1/notifications_controller.rb @@ -8,7 +8,7 @@ class Api::V1::NotificationsController < ApiController def index @notifications = Notification.where(account: current_account).paginate_by_max_id(20, params[:max_id], params[:since_id]) - @notifications = cache(@notifications) + @notifications = cache_collection(@notifications, Notification) statuses = @notifications.select { |n| !n.target_status.nil? }.map(&:target_status) set_maps(statuses) @@ -20,25 +20,4 @@ class Api::V1::NotificationsController < ApiController set_pagination_headers(next_path, prev_path) end - - private - - def cache(raw) - uncached_ids = [] - cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key)) - - raw.each do |notification| - uncached_ids << notification.id unless cached_keys_with_value.key?(notification.cache_key) - end - - unless uncached_ids.empty? - uncached = Notification.where(id: uncached_ids).with_includes.map { |n| [n.id, n] }.to_h - - uncached.values.each do |notification| - Rails.cache.write(notification.cache_key, notification) - end - end - - raw.map { |notification| cached_keys_with_value[notification.cache_key] || uncached[notification.id] } - end end diff --git a/app/controllers/api/v1/statuses_controller.rb b/app/controllers/api/v1/statuses_controller.rb index a693ce00d..a0b15cfbc 100644 --- a/app/controllers/api/v1/statuses_controller.rb +++ b/app/controllers/api/v1/statuses_controller.rb @@ -58,7 +58,7 @@ class Api::V1::StatusesController < ApiController def destroy @status = Status.where(account_id: current_user.account).find(params[:id]) - RemoveStatusService.new.call(@status) + RemovalWorker.perform_async(@status.id) render_empty end @@ -68,8 +68,12 @@ class Api::V1::StatusesController < ApiController end def unreblog - RemoveStatusService.new.call(Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first!) - @status = Status.find(params[:id]) + reblog = Status.where(account_id: current_user.account, reblog_of_id: params[:id]).first! + @status = reblog.reblog + @reblogged_map = { @status.id => false } + + RemovalWorker.perform_async(reblog.id) + render action: :show end diff --git a/app/controllers/api/v1/timelines_controller.rb b/app/controllers/api/v1/timelines_controller.rb index b1d7c3052..89e54e2cf 100644 --- a/app/controllers/api/v1/timelines_controller.rb +++ b/app/controllers/api/v1/timelines_controller.rb @@ -8,6 +8,7 @@ class Api::V1::TimelinesController < ApiController def home @statuses = Feed.new(:home, current_account).get(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a + @statuses = cache_collection(@statuses) set_maps(@statuses) set_counters_maps(@statuses) @@ -23,6 +24,7 @@ class Api::V1::TimelinesController < ApiController def mentions @statuses = Feed.new(:mentions, current_account).get(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a + @statuses = cache_collection(@statuses) set_maps(@statuses) set_counters_maps(@statuses) @@ -38,7 +40,7 @@ class Api::V1::TimelinesController < ApiController def public @statuses = Status.as_public_timeline(current_account).paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a - @statuses = cache(@statuses) + @statuses = cache_collection(@statuses) set_maps(@statuses) set_counters_maps(@statuses) @@ -55,7 +57,7 @@ class Api::V1::TimelinesController < ApiController def tag @tag = Tag.find_by(name: params[:id].downcase) @statuses = @tag.nil? ? [] : Status.as_tag_timeline(@tag, current_account).paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a - @statuses = cache(@statuses) + @statuses = cache_collection(@statuses) set_maps(@statuses) set_counters_maps(@statuses) @@ -71,22 +73,7 @@ class Api::V1::TimelinesController < ApiController private - def cache(raw) - uncached_ids = [] - cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key)) - - raw.each do |status| - uncached_ids << status.id unless cached_keys_with_value.key?(status.cache_key) - end - - unless uncached_ids.empty? - uncached = Status.where(id: uncached_ids).with_includes.map { |s| [s.id, s] }.to_h - - uncached.values.each do |status| - Rails.cache.write(status.cache_key, status) - end - end - - raw.map { |status| cached_keys_with_value[status.cache_key] || uncached[status.id] } + def cache_collection(raw) + super(raw, Status) end end diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index a3a2a3275..d2d3bc4a4 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -48,7 +48,7 @@ class ApiController < ApplicationController response.headers['X-RateLimit-Limit'] = match_data[:limit].to_s response.headers['X-RateLimit-Remaining'] = (match_data[:limit] - match_data[:count]).to_s - response.headers['X-RateLimit-Reset'] = (now + (match_data[:period] - now.to_i % match_data[:period])).to_s + response.headers['X-RateLimit-Reset'] = (now + (match_data[:period] - now.to_i % match_data[:period])).iso8601(6) end def set_pagination_headers(next_path = nil, prev_path = nil) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index effb4ed78..ba0098c71 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,7 +14,6 @@ class ApplicationController < ActionController::Base before_action :store_current_location, except: :raise_not_found, unless: :devise_controller? before_action :set_locale - before_action :check_rack_mini_profiler def raise_not_found raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}" @@ -32,8 +31,8 @@ class ApplicationController < ActionController::Base I18n.locale = I18n.default_locale end - def check_rack_mini_profiler - Rack::MiniProfiler.authorize_request if current_user && current_user.admin? + def require_admin! + redirect_to root_path unless current_user&.admin? end protected @@ -53,4 +52,23 @@ class ApplicationController < ActionController::Base def current_account @current_account ||= current_user.try(:account) end + + def cache_collection(raw, klass) + uncached_ids = [] + cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key)) + + raw.each do |item| + uncached_ids << item.id unless cached_keys_with_value.key?(item.cache_key) + end + + unless uncached_ids.empty? + uncached = klass.where(id: uncached_ids).with_includes.map { |item| [item.id, item] }.to_h + + uncached.values.each do |item| + Rails.cache.write(item.cache_key, item) + end + end + + raw.map { |item| cached_keys_with_value[item.cache_key] || uncached[item.id] }.compact + end end diff --git a/app/controllers/settings/preferences_controller.rb b/app/controllers/settings/preferences_controller.rb index 5be8719ae..cacc03b65 100644 --- a/app/controllers/settings/preferences_controller.rb +++ b/app/controllers/settings/preferences_controller.rb @@ -14,7 +14,10 @@ class Settings::PreferencesController < ApplicationController current_user.settings(:notification_emails).favourite = user_params[:notification_emails][:favourite] == '1' current_user.settings(:notification_emails).mention = user_params[:notification_emails][:mention] == '1' - if current_user.update(user_params.except(:notification_emails)) + current_user.settings(:interactions).must_be_follower = user_params[:interactions][:must_be_follower] == '1' + current_user.settings(:interactions).must_be_following = user_params[:interactions][:must_be_following] == '1' + + if current_user.update(user_params.except(:notification_emails, :interactions)) redirect_to settings_preferences_path, notice: I18n.t('generic.changes_saved_msg') else render action: :show @@ -24,6 +27,6 @@ class Settings::PreferencesController < ApplicationController private def user_params - params.require(:user).permit(:locale, notification_emails: [:follow, :reblog, :favourite, :mention]) + params.require(:user).permit(:locale, notification_emails: [:follow, :reblog, :favourite, :mention], interactions: [:must_be_follower, :must_be_following]) end end |