diff --git a/app/controllers/discourse_rewind/rewinds_controller.rb b/app/controllers/discourse_rewind/rewinds_controller.rb index 15cf132..9b57adf 100644 --- a/app/controllers/discourse_rewind/rewinds_controller.rb +++ b/app/controllers/discourse_rewind/rewinds_controller.rb @@ -10,7 +10,12 @@ module ::DiscourseRewind # expires_in 1.minute, public: false response.headers["X-Robots-Tag"] = "noindex" - render "show", layout: false + DiscourseRewind::Rewind::Fetch.call(service_params) do + on_success do |reports:| + @reports = reports + render "show", layout: false + end + end end end end diff --git a/app/services/discourse_rewind/reports/posts_count.rb b/app/services/discourse_rewind/reports/posts_count.rb new file mode 100644 index 0000000..891653c --- /dev/null +++ b/app/services/discourse_rewind/reports/posts_count.rb @@ -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 diff --git a/app/services/discourse_rewind/rewind/action/create_posts_count_report.rb b/app/services/discourse_rewind/rewind/action/create_posts_count_report.rb new file mode 100644 index 0000000..c7b5bcd --- /dev/null +++ b/app/services/discourse_rewind/rewind/action/create_posts_count_report.rb @@ -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 diff --git a/app/services/discourse_rewind/rewind/fetch.rb b/app/services/discourse_rewind/rewind/fetch.rb new file mode 100644 index 0000000..31240aa --- /dev/null +++ b/app/services/discourse_rewind/rewind/fetch.rb @@ -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 diff --git a/app/views/discourse_rewind/rewinds/show.html.erb b/app/views/discourse_rewind/rewinds/show.html.erb index 84c5925..318ff65 100644 --- a/app/views/discourse_rewind/rewinds/show.html.erb +++ b/app/views/discourse_rewind/rewinds/show.html.erb @@ -13,8 +13,19 @@ "> - TEST - - <%= current_user.username %> +
+
+ <%- @reports.each do |report| %> +
+
+ <%= report['identifier'] %> +
+
+ <%= report['data']['count'] %> +
+
+ <%- end %> +
+
diff --git a/public/css/rewind.css b/public/css/rewind.css index fbf883e..9d35916 100644 --- a/public/css/rewind.css +++ b/public/css/rewind.css @@ -1,3 +1,16 @@ -body { - background: #444; +/* RESET CSS */ +*, *::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; }