Skip to content

Commit da6a43f

Browse files
Gargronhiyuki2578
authored andcommitted
Fix blurhash and autoplay not working on public pages (mastodon#11585)
1 parent d3177f5 commit da6a43f

13 files changed

Lines changed: 32 additions & 63 deletions

File tree

app/controllers/home_controller.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
class HomeController < ApplicationController
44
before_action :authenticate_user!
55
before_action :set_referrer_policy_header
6-
before_action :set_initial_state_json
76

87
def index
98
@body_classes = 'app-body'
@@ -39,21 +38,6 @@ def authenticate_user!
3938
redirect_to(matches ? tag_path(CGI.unescape(matches[:tag])) : default_redirect_path)
4039
end
4140

42-
def set_initial_state_json
43-
serializable_resource = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(initial_state_params), serializer: InitialStateSerializer)
44-
@initial_state_json = serializable_resource.to_json
45-
end
46-
47-
def initial_state_params
48-
{
49-
settings: Web::Setting.find_by(user: current_user)&.data || {},
50-
push_subscription: current_account.user.web_push_subscription(current_session),
51-
current_account: current_account,
52-
token: current_session.token,
53-
admin: Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, '')),
54-
}
55-
end
56-
5741
def default_redirect_path
5842
if request.path.start_with?('/web') || whitelist_mode?
5943
new_user_session_path

app/controllers/public_timelines_controller.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ class PublicTimelinesController < ApplicationController
88
before_action :set_body_classes
99
before_action :set_instance_presenter
1010

11-
def show
12-
@initial_state_json = ActiveModelSerializers::SerializableResource.new(
13-
InitialStatePresenter.new(settings: { known_fediverse: Setting.show_known_fediverse_at_about_page }, token: current_session&.token),
14-
serializer: InitialStateSerializer
15-
).to_json
16-
end
11+
def show; end
1712

1813
private
1914

app/controllers/shares_controller.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,10 @@ class SharesController < ApplicationController
66
before_action :authenticate_user!
77
before_action :set_body_classes
88

9-
def show
10-
serializable_resource = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(initial_state_params), serializer: InitialStateSerializer)
11-
@initial_state_json = serializable_resource.to_json
12-
end
9+
def show; end
1310

1411
private
1512

16-
def initial_state_params
17-
text = [params[:title], params[:text], params[:url]].compact.join(' ')
18-
19-
{
20-
settings: Web::Setting.find_by(user: current_user)&.data || {},
21-
push_subscription: current_account.user.web_push_subscription(current_session),
22-
current_account: current_account,
23-
token: current_session.token,
24-
admin: Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, '')),
25-
text: text,
26-
}
27-
end
28-
2913
def set_body_classes
3014
@body_classes = 'modal-layout compose-standalone'
3115
end

app/controllers/tags_controller.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ def show
1717
respond_to do |format|
1818
format.html do
1919
expires_in 0, public: true
20-
21-
@initial_state_json = ActiveModelSerializers::SerializableResource.new(
22-
InitialStatePresenter.new(settings: {}, token: current_session&.token),
23-
serializer: InitialStateSerializer
24-
).to_json
2520
end
2621

2722
format.rss do

app/helpers/application_helper.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,25 @@ def quote_wrap(text, line_width: 80, break_sequence: "\n")
122122
text = word_wrap(text, line_width: line_width - 2, break_sequence: break_sequence)
123123
text.split("\n").map { |line| '> ' + line }.join("\n")
124124
end
125+
126+
def render_initial_state
127+
state_params = {
128+
settings: {
129+
known_fediverse: Setting.show_known_fediverse_at_about_page,
130+
},
131+
132+
text: [params[:title], params[:text], params[:url]].compact.join(' '),
133+
}
134+
135+
if user_signed_in?
136+
state_params[:settings] = state_params[:settings].merge(Web::Setting.find_by(user: current_user)&.data || {})
137+
state_params[:push_subscription] = current_account.user.web_push_subscription(current_session)
138+
state_params[:current_account] = current_account
139+
state_params[:token] = current_session.token
140+
state_params[:admin] = Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
141+
end
142+
143+
json = ActiveModelSerializers::SerializableResource.new(InitialStatePresenter.new(state_params), serializer: InitialStateSerializer).to_json
144+
content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
145+
end
125146
end

app/serializers/initial_state_serializer.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ def meta
4343
store[:use_pending_items] = object.current_account.user.setting_use_pending_items
4444
store[:is_staff] = object.current_account.user.staff?
4545
store[:trends] = Setting.trends && object.current_account.user.setting_trends
46+
else
47+
store[:auto_play_gif] = Setting.auto_play_gif
48+
store[:display_media] = Setting.display_media
49+
store[:reduce_motion] = Setting.reduce_motion
50+
store[:use_blurhash] = Setting.use_blurhash
4651
end
4752

4853
store

app/views/home/index.html.haml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
= preload_link_tag asset_pack_path('features/notifications.js'), crossorigin: 'anonymous'
66

77
%meta{name: 'applicationServerKey', content: Rails.configuration.x.vapid_public_key}
8-
%script#initial-state{ type: 'application/json' }!= json_escape(@initial_state_json)
9-
8+
= render_initial_state
109
= javascript_pack_tag 'application', integrity: true, crossorigin: 'anonymous'
1110

1211
.app-holder#mastodon{ data: { props: Oj.dump(default_props) } }

app/views/layouts/public.html.haml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
- content_for :header_tags do
2+
= render_initial_state
23
= javascript_pack_tag 'public', integrity: true, crossorigin: 'anonymous'
34

45
- content_for :content do

app/views/public_timelines/show.html.haml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
- content_for :header_tags do
55
%meta{ name: 'robots', content: 'noindex' }/
6-
%script#initial-state{ type: 'application/json' }!= json_escape(@initial_state_json)
76
= javascript_pack_tag 'about', integrity: true, crossorigin: 'anonymous'
87

98
.page-header

app/views/shares/show.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
- content_for :header_tags do
2-
%script#initial-state{ type: 'application/json' }!= json_escape(@initial_state_json)
2+
= render_initial_state
33
= javascript_pack_tag 'share', integrity: true, crossorigin: 'anonymous'
44

55
#mastodon-compose{ data: { props: Oj.dump(default_props) } }

0 commit comments

Comments
 (0)