basic architecture

This commit is contained in:
Joffrey JAFFEUX 2024-12-07 18:40:23 +01:00
parent 3a710f4c1e
commit 1944875b57
6 changed files with 137 additions and 6 deletions

View File

@ -10,7 +10,12 @@ module ::DiscourseRewind
# expires_in 1.minute, public: false # expires_in 1.minute, public: false
response.headers["X-Robots-Tag"] = "noindex" response.headers["X-Robots-Tag"] = "noindex"
DiscourseRewind::Rewind::Fetch.call(service_params) do
on_success do |reports:|
@reports = reports
render "show", layout: false render "show", layout: false
end end
end end
end
end
end end

View File

@ -0,0 +1,33 @@
# frozen_string_literal: true
class Reports::PostsCount
include Service::Base
policy :current_user_is_admin
params do
attribute :setting_name, :string
validates :setting_name, presence: true
end
policy :setting_is_available
transaction { step :toggle }
private
def current_user_is_admin(guardian:)
guardian.is_admin?
end
def setting_is_available(params:)
SiteSetting.respond_to?(params.setting_name)
end
def toggle(params:, guardian:)
SiteSetting.set_and_log(
params.setting_name,
!SiteSetting.public_send(params.setting_name),
guardian.user,
)
end
end

View File

@ -0,0 +1,14 @@
# frozen_string_literal: true
module DiscourseRewind
class Rewind::Action::CreatePostsCountReport < Service::ActionBase
option :params
option :guardian
delegate :username, :year, to: :params, private: true
def call
{ data: { count: 5 }, identifier: "posts-count-report" }
end
end
end

View File

@ -0,0 +1,55 @@
# frozen_string_literal: true
module DiscourseRewind
# Service responsible to fetch a rewind for a username/year.
#
# @example
# ::DiscourseRewind::Rewind::Fetch.call(
# guardian: guardian,
# params: {
# username: "falco",
# year: 2024,
# }
# )
#
class Rewind::Fetch
include Service::Base
# @!method self.call(guardian:, params:)
# @param [Guardian] guardian
# @param [Hash] params
# @option params [Integer] :year of the rewind
# @option params [Integer] :username of the rewind
# @return [Service::Base::Context]
REPORTS = [DiscourseRewind::Rewind::Action::CreatePostsCountReport]
CACHE_DURATION = 5.minutes
params do
attribute :year, :integer
attribute :username, :string
validates :year, presence: true
validates :username, presence: true
end
model :reports
private
def fetch_reports(params:, guardian:)
key = "rewind:#{params.username}:#{params.year}"
reports = Discourse.redis.get(key)
if Rails.env.development? || !reports
reports = REPORTS.map { |report| report.call(params:, guardian:) }
Discourse.redis.setex(key, CACHE_DURATION, MultiJson.dump(reports))
else
reports = MultiJson.load(reports)
end
reports
end
end
end

View File

@ -13,8 +13,19 @@
<link rel="stylesheet" href="<%= rewind_asset_url("rewind.css") %>"> <link rel="stylesheet" href="<%= rewind_asset_url("rewind.css") %>">
</head> </head>
<body> <body>
TEST <div class="rewind__wrapper">
<div class="rewind">
<%= current_user.username %> <%- @reports.each do |report| %>
<div class="rewind__report">
<div class="rewind__report__header">
<%= report['identifier'] %>
</div>
<div class="rewind__report__body">
<%= report['data']['count'] %>
</div>
</div>
<%- end %>
</div>
</div>
</body> </body>
</html> </html>

View File

@ -1,3 +1,16 @@
body { /* RESET CSS */
background: #444; *, *::before, *::after { box-sizing: border-box; }
* { margin: 0; padding: 0; }
ul[role='list'], ol[role='list'] { list-style: none; }
html:focus-within { scroll-behavior: smooth; }
a:not([class]) { text-decoration-skip-ink: auto; }
input, button, textarea, select { font: inherit; }
body, html { height: 100%; scroll-behavior: smooth; }
/* END RESET CSS */
.rewind {
display: flex;
height: 100vh;
background-color: #f5f5f5;
border-radius: 5px;
} }