Fix blurhash and autoplay not working on public pages (#11585)

This commit is contained in:
Eugen Rochko 2019-08-16 19:15:05 +02:00 committed by GitHub
parent 70da6d6630
commit e5cee8062f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 32 additions and 63 deletions

View File

@ -3,7 +3,6 @@
class HomeController < ApplicationController
before_action :authenticate_user!
before_action :set_referrer_policy_header
before_action :set_initial_state_json
def index
@body_classes = 'app-body'
@ -39,21 +38,6 @@ class HomeController < ApplicationController
redirect_to(matches ? tag_path(CGI.unescape(matches[:tag])) : default_redirect_path)
end
def set_initial_state_json
serializable_resource = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(initial_state_params), serializer: InitialStateSerializer)
@initial_state_json = serializable_resource.to_json
end
def initial_state_params
{
settings: Web::Setting.find_by(user: current_user)&.data || {},
push_subscription: current_account.user.web_push_subscription(current_session),
current_account: current_account,
token: current_session.token,
admin: Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, '')),
}
end
def default_redirect_path
if request.path.start_with?('/web') || whitelist_mode?
new_user_session_path

View File

@ -8,12 +8,7 @@ class PublicTimelinesController < ApplicationController
before_action :set_body_classes
before_action :set_instance_presenter
def show
@initial_state_json = ActiveModelSerializers::SerializableResource.new(
InitialStatePresenter.new(settings: { known_fediverse: Setting.show_known_fediverse_at_about_page }, token: current_session&.token),
serializer: InitialStateSerializer
).to_json
end
def show; end
private

View File

@ -6,26 +6,10 @@ class SharesController < ApplicationController
before_action :authenticate_user!
before_action :set_body_classes
def show
serializable_resource = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(initial_state_params), serializer: InitialStateSerializer)
@initial_state_json = serializable_resource.to_json
end
def show; end
private
def initial_state_params
text = [params[:title], params[:text], params[:url]].compact.join(' ')
{
settings: Web::Setting.find_by(user: current_user)&.data || {},
push_subscription: current_account.user.web_push_subscription(current_session),
current_account: current_account,
token: current_session.token,
admin: Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, '')),
text: text,
}
end
def set_body_classes
@body_classes = 'modal-layout compose-standalone'
end

View File

@ -17,11 +17,6 @@ class TagsController < ApplicationController
respond_to do |format|
format.html do
expires_in 0, public: true
@initial_state_json = ActiveModelSerializers::SerializableResource.new(
InitialStatePresenter.new(settings: {}, token: current_session&.token),
serializer: InitialStateSerializer
).to_json
end
format.rss do

View File

@ -122,4 +122,25 @@ module ApplicationHelper
text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
text.split("\n").map { |line| '> ' + line }.join("\n")
end
def render_initial_state
state_params = {
settings: {
known_fediverse: Setting.show_known_fediverse_at_about_page,
},
text: [params[:title], params[:text], params[:url]].compact.join(' '),
}
if user_signed_in?
state_params[:settings] = state_params[:settings].merge(Web::Setting.find_by(user: current_user)&.data || {})
state_params[:push_subscription] = current_account.user.web_push_subscription(current_session)
state_params[:current_account] = current_account
state_params[:token] = current_session.token
state_params[:admin] = Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
end
json = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(state_params), serializer: InitialStateSerializer).to_json
content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
end
end

View File

@ -38,6 +38,11 @@ class InitialStateSerializer < ActiveModel::Serializer
store[:use_pending_items] = object.current_account.user.setting_use_pending_items
store[:is_staff] = object.current_account.user.staff?
store[:trends] = Setting.trends && object.current_account.user.setting_trends
else
store[:auto_play_gif] = Setting.auto_play_gif
store[:display_media] = Setting.display_media
store[:reduce_motion] = Setting.reduce_motion
store[:use_blurhash] = Setting.use_blurhash
end
store

View File

@ -5,8 +5,7 @@
= preload_link_tag asset_pack_path('features/notifications.js'), crossorigin: 'anonymous'
%meta{name: 'applicationServerKey', content: Rails.configuration.x.vapid_public_key}
%script#initial-state{ type: 'application/json' }!= json_escape(@initial_state_json)
= render_initial_state
= javascript_pack_tag 'application', integrity: true, crossorigin: 'anonymous'
.app-holder#mastodon{ data: { props: Oj.dump(default_props) } }

View File

@ -1,4 +1,5 @@
- content_for :header_tags do
= render_initial_state
= javascript_pack_tag 'public', integrity: true, crossorigin: 'anonymous'
- content_for :content do

View File

@ -3,7 +3,6 @@
- content_for :header_tags do
%meta{ name: 'robots', content: 'noindex' }/
%script#initial-state{ type: 'application/json' }!= json_escape(@initial_state_json)
= javascript_pack_tag 'about', integrity: true, crossorigin: 'anonymous'
.page-header

View File

@ -1,5 +1,5 @@
- content_for :header_tags do
%script#initial-state{ type: 'application/json' }!= json_escape(@initial_state_json)
= render_initial_state
= javascript_pack_tag 'share', integrity: true, crossorigin: 'anonymous'
#mastodon-compose{ data: { props: Oj.dump(default_props) } }

View File

@ -5,7 +5,6 @@
%meta{ name: 'robots', content: 'noindex' }/
%link{ rel: 'alternate', type: 'application/rss+xml', href: tag_url(@tag, format: 'rss') }/
%script#initial-state{ type: 'application/json' }!= json_escape(@initial_state_json)
= javascript_pack_tag 'about', integrity: true, crossorigin: 'anonymous'
= render 'og'

View File

@ -27,16 +27,6 @@ RSpec.describe HomeController, type: :controller do
subject
expect(assigns(:body_classes)).to eq 'app-body'
end
it 'assigns @initial_state_json' do
subject
initial_state_json = json_str_to_hash(assigns(:initial_state_json))
expect(initial_state_json[:meta]).to_not be_nil
expect(initial_state_json[:compose]).to_not be_nil
expect(initial_state_json[:accounts]).to_not be_nil
expect(initial_state_json[:settings]).to_not be_nil
expect(initial_state_json[:media_attachments]).to_not be_nil
end
end
end
end

View File

@ -7,15 +7,12 @@ describe SharesController do
before { sign_in user }
describe 'GTE #show' do
subject(:initial_state_json) { JSON.parse(assigns(:initial_state_json), symbolize_names: true) }
subject(:body_classes) { assigns(:body_classes) }
before { get :show, params: { title: 'test title', text: 'test text', url: 'url1 url2' } }
it 'assigns json' do
it 'returns http success' do
expect(response).to have_http_status :ok
expect(initial_state_json[:compose][:text]).to eq 'test title test text url1 url2'
expect(initial_state_json[:meta][:me]).to eq user.account.id.to_s
expect(body_classes).to eq 'modal-layout compose-standalone'
end
end