]> cat aescling's git repositories - mastodon.git/commitdiff
Fix blurhash and autoplay not working on public pages (#11585)
authorEugen Rochko <eugen@zeonfederated.com>
Fri, 16 Aug 2019 17:15:05 +0000 (19:15 +0200)
committerGitHub <noreply@github.com>
Fri, 16 Aug 2019 17:15:05 +0000 (19:15 +0200)
13 files changed:
app/controllers/home_controller.rb
app/controllers/public_timelines_controller.rb
app/controllers/shares_controller.rb
app/controllers/tags_controller.rb
app/helpers/application_helper.rb
app/serializers/initial_state_serializer.rb
app/views/home/index.html.haml
app/views/layouts/public.html.haml
app/views/public_timelines/show.html.haml
app/views/shares/show.html.haml
app/views/tags/show.html.haml
spec/controllers/home_controller_spec.rb
spec/controllers/shares_controller_spec.rb

index 22d507e7797fee68f0102c27fd9c325311c6a8f4..7c8a18d17cc48a452cccb5ba804887e3f390d06a 100644 (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
index 324bdc50880fff4c30da0fcbe0a9b58bca911165..1332ba16c2bac4b75664c03d3d641907689a3d50 100644 (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
 
index af605b98f701fce34dfbc5f15815d48134f5f888..6546b8497808c46e4ff95dbcf3a87ebb50d4f146 100644 (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
index 5a6fcc8fdd834f6da956ad08691f8c5afb9a4507..4dfa0526413579063001824e4952d058a1c3fb5b 100644 (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
index 9d113263dfda1f4bb5a1ac84384c620fe5adde24..23cbb1d937092ba36be8bd95b7d04451627afc62 100644 (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
index 2cebef2c00d38756851c78b72fd940a16f38c1ca..fb53ea3145e8449e64d0992f5c35818db2ef330a 100644 (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
index 4c7fac0b6ae3f7f5afb034211facec0addd74c2d..30c7aab194f3aa6365f1839ebe5634cfbc184051 100644 (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) } }
index 69738a2f759643df9496209a8be8e412513c7132..b9179e23d17c528ab0530800e64a14e500395d50 100644 (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
index 913d5d855d2eb2e57d1daa20315775c158c7910f..07215efdfdf389dab83c03d409fcac9e3e06223f 100644 (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
index 44b6f145f68a639cf4bbee17e48005f5244a9e5b..f2f5479a79c4a6d9f4331d9f486e37d6ec189e10 100644 (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) } }
index cf42468224fa801c3c3e0b94092ef0b551d40ba9..6307022779422530707c1adef13dfcd3d130d169 100644 (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'
 
index f43cf0c27ec510ab3d06d736f819430815ee73ef..941f1dd9146cb8ba461cb6172ee1eed466a659bc 100644 (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
index a74e9af561cc66b81f26079db1cbbc60bc611023..d6de3016ae47c3a5234b70a9a2447c285b896ba5 100644 (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