Skip to content

Commit dad9c42

Browse files
Defer loading of page link inline macro
The main motivation for this is to not include user-specific information in the HTML-rendered formatted text of API responses, because we cache those responses across requests for different users. This works around the caching issue by ensuring the relevant information is not part of the cache. An alternative approach would be to ensure the caching is safe across different users.
1 parent 291cedb commit dad9c42

4 files changed

Lines changed: 99 additions & 15 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<%#-- copyright
2+
OpenProject is an open source project management software.
3+
Copyright (C) the OpenProject GmbH
4+
5+
This program is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU General Public License version 3.
7+
8+
OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
9+
Copyright (C) 2006-2013 Jean-Philippe Lang
10+
Copyright (C) 2010-2013 the ChiliProject Team
11+
12+
This program is free software; you can redistribute it and/or
13+
modify it under the terms of the GNU General Public License
14+
as published by the Free Software Foundation; either version 2
15+
of the License, or (at your option) any later version.
16+
17+
This program is distributed in the hope that it will be useful,
18+
but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
GNU General Public License for more details.
21+
22+
You should have received a copy of the GNU General Public License
23+
along with this program; if not, write to the Free Software
24+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25+
26+
See COPYRIGHT and LICENSE files for more details.
27+
28+
++#%>
29+
30+
<%= content_tag("turbo-frame", id: frame_id, src: frame_src, data: { provider_id:, page_identifier: identifier, type: "wiki-page-link" }) do %>
31+
<%= render(OpPrimer::InlineMacroComponent.new) do |component| %>
32+
<% component.with_leading_visual_icon(icon: :"op-file-doc") %>
33+
34+
<%= render Primer::Beta::Spinner.new(size: :small, mr: 2) %>
35+
<%= render(Primer::Beta::Text.new(color: :muted)) { t(".loading") } %>
36+
<% end %>
37+
<% end %>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
31+
module Wikis
32+
class DeferredInlinePageLinkMacroComponent < ApplicationComponent
33+
include ApplicationHelper
34+
include OpPrimer::ComponentHelpers
35+
36+
attr_reader :identifier, :provider_id
37+
38+
def initialize(identifier:, provider_id:, **)
39+
super(nil, **)
40+
41+
@identifier = identifier
42+
@provider_id = provider_id
43+
end
44+
45+
def frame_id
46+
@frame_id ||= SecureRandom.uuid
47+
end
48+
49+
def frame_src
50+
url_helpers.load_wiki_page_link_macro_path(
51+
provider_id:,
52+
page_identifier: identifier,
53+
turbo_frame_id: frame_id
54+
)
55+
end
56+
end
57+
end

modules/wikis/app/services/wikis/text_formatting/wiki_link_matcher.rb

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def process_match(match, _matched_string, _context)
5252
end
5353
end
5454

55+
attr_reader :provider_id, :identifier
56+
5557
def initialize(provider_id:, identifier:)
5658
super()
5759

@@ -60,23 +62,9 @@ def initialize(provider_id:, identifier:)
6062
end
6163

6264
def process
63-
provider = Provider.find_by(id: @provider_id)
6465
view_context = ApplicationController.new.view_context
65-
page_info_result = resolve_page(provider, @identifier)
66-
67-
InlinePageLinkMacroComponent.new(page_info_result).render_in(view_context)
68-
end
6966

70-
private
71-
72-
def resolve_page(provider, identifier)
73-
return Failure() if provider.nil?
74-
75-
Adapters::Input::PageInfo.build(identifier:).bind do |input_data|
76-
provider.auth_strategy_for(User.current).bind do |auth_strategy|
77-
provider.resolve("queries.page_info").call(input_data:, auth_strategy:)
78-
end
79-
end
67+
DeferredInlinePageLinkMacroComponent.new(provider_id:, identifier:).render_in(view_context)
8068
end
8169
end
8270
end

modules/wikis/config/locales/en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ en:
131131
wiki_page: Wiki page
132132
create_new_wiki_page_dialog:
133133
title: Create new wiki page
134+
deferred_inline_page_link_macro_component:
135+
loading: Loading…
134136
delete_relation_page_link_confirmation_dialog:
135137
heading: Delete related wiki page link?
136138
title: Delete related wiki page link

0 commit comments

Comments
 (0)