From 9110db41c53a2f3f22affc23b364362133997d3e Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 4 Mar 2018 09:19:11 +0100 Subject: Federate pinned statuses over ActivityPub (#6610) * Federate pinned statuses over ActivityPub * Display pinned toots in web UI Fix #6117 * Fix migration * Fix tests * Update outbox_serializer.rb * Update remove_serializer.rb * Update add_serializer.rb * Update fetch_featured_collection_service.rb --- .../activitypub/collections_controller.rb | 57 ++++++++++++++++++++++ app/controllers/activitypub/outboxes_controller.rb | 2 +- app/controllers/api/v1/statuses/pins_controller.rb | 28 ++++++++++- 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 app/controllers/activitypub/collections_controller.rb (limited to 'app/controllers') diff --git a/app/controllers/activitypub/collections_controller.rb b/app/controllers/activitypub/collections_controller.rb new file mode 100644 index 000000000..081914016 --- /dev/null +++ b/app/controllers/activitypub/collections_controller.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +class ActivityPub::CollectionsController < Api::BaseController + include SignatureVerification + + before_action :set_account + before_action :set_size + before_action :set_statuses + + def show + render json: collection_presenter, + serializer: ActivityPub::CollectionSerializer, + adapter: ActivityPub::Adapter, + content_type: 'application/activity+json', + skip_activities: true + end + + private + + def set_account + @account = Account.find_local!(params[:account_username]) + end + + def set_statuses + @statuses = scope_for_collection.paginate_by_max_id(20, params[:max_id], params[:since_id]) + @statuses = cache_collection(@statuses, Status) + end + + def set_size + case params[:id] + when 'featured' + @account.pinned_statuses.count + else + raise ActiveRecord::NotFound + end + end + + def scope_for_collection + case params[:id] + when 'featured' + @account.statuses.permitted_for(@account, signed_request_account).tap do |scope| + scope.merge!(@account.pinned_statuses) + end + else + raise ActiveRecord::NotFound + end + end + + def collection_presenter + ActivityPub::CollectionPresenter.new( + id: account_collection_url(@account, params[:id]), + type: :ordered, + size: @size, + items: @statuses + ) + end +end diff --git a/app/controllers/activitypub/outboxes_controller.rb b/app/controllers/activitypub/outboxes_controller.rb index a431e3557..9ed700c1e 100644 --- a/app/controllers/activitypub/outboxes_controller.rb +++ b/app/controllers/activitypub/outboxes_controller.rb @@ -9,7 +9,7 @@ class ActivityPub::OutboxesController < Api::BaseController @statuses = @account.statuses.permitted_for(@account, signed_request_account).paginate_by_max_id(20, params[:max_id], params[:since_id]) @statuses = cache_collection(@statuses, Status) - render json: outbox_presenter, serializer: ActivityPub::CollectionSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json' + render json: outbox_presenter, serializer: ActivityPub::OutboxSerializer, adapter: ActivityPub::Adapter, content_type: 'application/activity+json' end private diff --git a/app/controllers/api/v1/statuses/pins_controller.rb b/app/controllers/api/v1/statuses/pins_controller.rb index 3de1009b8..bba6a6f48 100644 --- a/app/controllers/api/v1/statuses/pins_controller.rb +++ b/app/controllers/api/v1/statuses/pins_controller.rb @@ -11,12 +11,18 @@ class Api::V1::Statuses::PinsController < Api::BaseController def create StatusPin.create!(account: current_account, status: @status) + distribute_add_activity! render json: @status, serializer: REST::StatusSerializer end def destroy pin = StatusPin.find_by(account: current_account, status: @status) - pin&.destroy! + + if pin + pin.destroy! + distribute_remove_activity! + end + render json: @status, serializer: REST::StatusSerializer end @@ -25,4 +31,24 @@ class Api::V1::Statuses::PinsController < Api::BaseController def set_status @status = Status.find(params[:status_id]) end + + def distribute_add_activity! + json = ActiveModelSerializers::SerializableResource.new( + @status, + serializer: ActivityPub::AddSerializer, + adapter: ActivityPub::Adapter + ).as_json + + ActivityPub::RawDistributionWorker.perform_async(Oj.dump(json), current_account) + end + + def distribute_remove_activity! + json = ActiveModelSerializers::SerializableResource.new( + @status, + serializer: ActivityPub::RemoveSerializer, + adapter: ActivityPub::Adapter + ).as_json + + ActivityPub::RawDistributionWorker.perform_async(Oj.dump(json), current_account) + end end -- cgit From 51d760960ccf0866eba32944434ba08b2cbe5092 Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Sun, 4 Mar 2018 17:21:35 +0900 Subject: Set the default locale in config (#6580) Previously the default locale was set by Localized concern for controllers, but it was not enforced for mailers. config is enforced throughout the application and an appropriate place to set the default locale. --- app/controllers/concerns/localized.rb | 12 ++----- config/application.rb | 9 +++-- config/environments/test.rb | 3 ++ spec/controllers/concerns/localized_spec.rb | 53 ++++++++--------------------- 4 files changed, 27 insertions(+), 50 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/concerns/localized.rb b/app/controllers/concerns/localized.rb index a9ea60f7d..e697284a8 100644 --- a/app/controllers/concerns/localized.rb +++ b/app/controllers/concerns/localized.rb @@ -17,11 +17,7 @@ module Localized end def default_locale - request_locale || env_locale || I18n.default_locale - end - - def env_locale - ENV['DEFAULT_LOCALE'] + request_locale || I18n.default_locale end def request_locale @@ -29,12 +25,10 @@ module Localized end def preferred_locale - http_accept_language.preferred_language_from([env_locale]) || - http_accept_language.preferred_language_from(I18n.available_locales) + http_accept_language.preferred_language_from(I18n.available_locales) end def compatible_locale - http_accept_language.compatible_language_from([env_locale]) || - http_accept_language.compatible_language_from(I18n.available_locales) + http_accept_language.compatible_language_from(I18n.available_locales) end end diff --git a/config/application.rb b/config/application.rb index 34b9dcf48..097cbf567 100644 --- a/config/application.rb +++ b/config/application.rb @@ -31,7 +31,7 @@ module Mastodon # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # config.time_zone = 'Central Time (US & Canada)' - # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. + # All translations from config/locales/*.rb,yml are auto loaded. # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] config.i18n.available_locales = [ :en, @@ -73,7 +73,12 @@ module Mastodon :'zh-TW', ] - config.i18n.default_locale = :en + config.i18n.default_locale = ENV['DEFAULT_LOCALE']&.to_sym + if config.i18n.available_locales.include?(config.i18n.default_locale) + config.i18n.fallbacks = [:en] + else + config.i18n.default_locale = :en + end # config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb') # config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')] diff --git a/config/environments/test.rb b/config/environments/test.rb index 20fe5f813..74e7fa694 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -51,6 +51,9 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + config.i18n.default_locale = :en + config.i18n.fallbacks = true end Paperclip::Attachment.default_options[:path] = "#{Rails.root}/spec/test_files/:class/:id_partition/:style.:extension" diff --git a/spec/controllers/concerns/localized_spec.rb b/spec/controllers/concerns/localized_spec.rb index c917ce85e..f71c96aff 100644 --- a/spec/controllers/concerns/localized_spec.rb +++ b/spec/controllers/concerns/localized_spec.rb @@ -16,49 +16,24 @@ describe ApplicationController, type: :controller do end shared_examples 'default locale' do - context 'when DEFAULT_LOCALE environment variable is set' do - around do |example| - ClimateControl.modify 'DEFAULT_LOCALE' => 'ca', &example.method(:run) - I18n.locale = I18n.default_locale - end + after { I18n.locale = I18n.default_locale } - it 'sets language specified by ENV if preferred' do - request.headers['Accept-Language'] = 'ca, fa' - get 'success' - expect(I18n.locale).to eq :ca - end - - it 'sets available and preferred language if language specified by ENV is not preferred' do - request.headers['Accept-Language'] = 'ca-ES, fa' - get 'success' - expect(I18n.locale).to eq :fa - end - - it 'sets language specified by ENV if it is compatible and none of available languages are preferred' do - request.headers['Accept-Language'] = 'ca-ES, fa-IR' - get 'success' - expect(I18n.locale).to eq :ca - end - - it 'sets available and compatible langauge if language specified by ENV is not compatible none of available languages are preferred' do - request.headers['Accept-Language'] = 'fa-IR' - get 'success' - expect(I18n.locale).to eq :fa - end + it 'sets available and preferred language' do + request.headers['Accept-Language'] = 'ca-ES, fa' + get 'success' + expect(I18n.locale).to eq :fa + end - it 'sets language specified by ENV if none of available languages are compatible' do - request.headers['Accept-Language'] = '' - get 'success' - expect(I18n.locale).to eq :ca - end + it 'sets available and compatible langauge if none of available languages are preferred' do + request.headers['Accept-Language'] = 'fa-IR' + get 'success' + expect(I18n.locale).to eq :fa end - context 'when DEFAULT_LOCALE environment variable is not set' do - it 'sets default locale if none of available languages are compatible' do - request.headers['Accept-Language'] = '' - get 'success' - expect(I18n.locale).to eq :en - end + it 'sets default locale if none of available languages are compatible' do + request.headers['Accept-Language'] = '' + get 'success' + expect(I18n.locale).to eq :en end end -- cgit From a38dbd9c8a5de4626f55d1a0dcd19ccb4a7e2c91 Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Tue, 6 Mar 2018 03:29:36 +0900 Subject: Redirect from Web tag timeline to public tag timeline if not signed in (#6633) This is also implemented in Pawoo: https://github.com/pixiv/mastodon/commit/ceafdbd1bbf30fe20a2a814df0f8cae429a4e9db --- app/controllers/home_controller.rb | 3 ++- spec/controllers/home_controller_spec.rb | 15 +++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 21dde20ce..b1f8f1ad9 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -34,7 +34,8 @@ class HomeController < ApplicationController end end - redirect_to(default_redirect_path) + matches = request.path.match(%r{\A/web/timelines/tag/(?.+)\z}) + redirect_to(matches ? tag_path(CGI.unescape(matches[:tag])) : default_redirect_path) end def set_initial_state_json diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index 1077a7288..f43cf0c27 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -4,21 +4,24 @@ RSpec.describe HomeController, type: :controller do render_views describe 'GET #index' do + subject { get :index } + context 'when not signed in' do + context 'when requested path is tag timeline' do + before { @request.path = '/web/timelines/tag/name' } + it { is_expected.to redirect_to '/tags/name' } + end + it 'redirects to about page' do @request.path = '/' - get :index - expect(response).to redirect_to(about_path) + is_expected.to redirect_to(about_path) end end context 'when signed in' do let(:user) { Fabricate(:user) } - subject do - sign_in(user) - get :index - end + before { sign_in(user) } it 'assigns @body_classes' do subject -- cgit From a29d409e20e74c9a4f168c5c45fec9092e03ac61 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 8 Mar 2018 11:18:26 +0100 Subject: If login redirects to omniauth, redirect logout to root_path (#6694) Fix #6670 --- app/controllers/auth/sessions_controller.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/auth/sessions_controller.rb b/app/controllers/auth/sessions_controller.rb index 02447dde0..c1ebe760c 100644 --- a/app/controllers/auth/sessions_controller.rb +++ b/app/controllers/auth/sessions_controller.rb @@ -12,10 +12,9 @@ class Auth::SessionsController < Devise::SessionsController def new Devise.omniauth_configs.each do |provider, config| - if config.strategy.redirect_at_sign_in - return redirect_to(omniauth_authorize_path(resource_name, provider)) - end + return redirect_to(omniauth_authorize_path(resource_name, provider)) if config.strategy.redirect_at_sign_in end + super end @@ -59,6 +58,14 @@ class Auth::SessionsController < Devise::SessionsController end end + def after_sign_out_path_for(_resource_or_scope) + Devise.omniauth_configs.each_value do |config| + return root_path if config.strategy.redirect_at_sign_in + end + + super + end + def two_factor_enabled? find_user.try(:otp_required_for_login?) end -- cgit