diff --git a/README.md b/README.md index 7144774..7632898 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,4 @@ Display stats on your last year of Discourse usage. ## Usage -- add `http://127.0.0.1:4200/rewinds?` in `SiteSetting.allowed_iframes` -- example embed of a rewind in a post: - -``` - -``` +Navigate to `/my/activity/rewind` diff --git a/app/controllers/discourse_rewind/rewinds_assets_controller.rb b/app/controllers/discourse_rewind/rewinds_assets_controller.rb deleted file mode 100644 index d6ddbba..0000000 --- a/app/controllers/discourse_rewind/rewinds_assets_controller.rb +++ /dev/null @@ -1,32 +0,0 @@ -# frozen_string_literal: true - -module ::DiscourseRewind - class RewindsAssetsController < ::ApplicationController - requires_plugin PLUGIN_NAME - - skip_before_action :preload_json, :check_xhr, only: %i[show] - skip_before_action :verify_authenticity_token, only: %i[show] - - def show - no_cookies - - name = params[:name] - path, content_type = - if name == "rewind" - %w[rewind.css text/css] - else - raise Discourse::NotFound - end - - content = File.read(DiscourseRewind.public_asset_path("css/#{path}")) - - # note, path contains a ":version" which automatically busts the cache - # based on file content, so this is safe - response.headers["Last-Modified"] = 10.years.ago.httpdate - response.headers["Content-Length"] = content.bytesize.to_s - immutable_for 1.year - - render plain: content, disposition: :nil, content_type: content_type - end - end -end diff --git a/app/controllers/discourse_rewind/rewinds_controller.rb b/app/controllers/discourse_rewind/rewinds_controller.rb index 9b57adf..f1a9cf7 100644 --- a/app/controllers/discourse_rewind/rewinds_controller.rb +++ b/app/controllers/discourse_rewind/rewinds_controller.rb @@ -4,16 +4,11 @@ module ::DiscourseRewind class RewindsController < ::ApplicationController requires_plugin PLUGIN_NAME - skip_before_action :preload_json, :check_xhr, :redirect_to_login_if_required, only: %i[show] - def show - # expires_in 1.minute, public: false - response.headers["X-Robots-Tag"] = "noindex" - DiscourseRewind::Rewind::Fetch.call(service_params) do on_success do |reports:| @reports = reports - render "show", layout: false + render json: MultiJson.dump(reports), status: 200 end end end diff --git a/app/helpers/discourse_rewind/rewinds_helper.rb b/app/helpers/discourse_rewind/rewinds_helper.rb deleted file mode 100644 index 9d2c9a6..0000000 --- a/app/helpers/discourse_rewind/rewinds_helper.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -module DiscourseRewind - module RewindsHelper - # keeping it here for caching - def self.rewind_asset_url(asset_name) - if !%w[rewind.css].include?(asset_name) - raise StandardError, "unknown asset type #{asset_name}" - end - - @urls ||= {} - url = @urls[asset_name] - return url if url - - content = File.read(DiscourseRewind.public_asset_path("css/#{asset_name}")) - sha1 = Digest::SHA1.hexdigest(content) - url = "/rewinds/assets/#{sha1}/#{asset_name}" - @urls[asset_name] = GlobalPath.cdn_path(url) - end - - def rewind_asset_url(asset_name) - DiscourseRewind::RewindsHelper.rewind_asset_url(asset_name) - end - end -end diff --git a/app/services/discourse_rewind/rewind/action/base.rb b/app/services/discourse_rewind/rewind/action/base_report.rb similarity index 68% rename from app/services/discourse_rewind/rewind/action/base.rb rename to app/services/discourse_rewind/rewind/action/base_report.rb index 4c39f70..2e66c66 100644 --- a/app/services/discourse_rewind/rewind/action/base.rb +++ b/app/services/discourse_rewind/rewind/action/base_report.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module DiscourseRewind - class Rewind::Action::Base < Service::ActionBase + class Rewind::Action::BaseReport < Service::ActionBase option :user option :date @@ -9,7 +9,7 @@ module DiscourseRewind raise NotImplementedError end - def enabled? + def self.enabled? true end end diff --git a/app/services/discourse_rewind/rewind/action/fbff.rb b/app/services/discourse_rewind/rewind/action/fbff.rb index 93e499c..14de726 100644 --- a/app/services/discourse_rewind/rewind/action/fbff.rb +++ b/app/services/discourse_rewind/rewind/action/fbff.rb @@ -3,7 +3,7 @@ # Forum Best Friend Forever ranking # Score is informative only, do not show in UI module DiscourseRewind - class Rewind::Action::Fbff < Action::Base + class Rewind::Action::Fbff < Rewind::Action::BaseReport MAX_SUMMARY_RESULTS = 50 LIKE_SCORE = 1 REPLY_SCORE = 10 diff --git a/app/services/discourse_rewind/rewind/action/posting_calendar.rb b/app/services/discourse_rewind/rewind/action/posting_calendar.rb index a55dd30..51921e7 100644 --- a/app/services/discourse_rewind/rewind/action/posting_calendar.rb +++ b/app/services/discourse_rewind/rewind/action/posting_calendar.rb @@ -3,7 +3,7 @@ # For a GitHub like calendar # https://docs.github.com/assets/cb-35216/mw-1440/images/help/profile/contributions-graph.webp module DiscourseRewind - class Rewind::Action::PostingCalendar < Action::Base + class Rewind::Action::PostingCalendar < Rewind::Action::BaseReport def call calendar = Post diff --git a/app/services/discourse_rewind/rewind/action/reactions.rb b/app/services/discourse_rewind/rewind/action/reactions.rb index 59859b2..1bd03b2 100644 --- a/app/services/discourse_rewind/rewind/action/reactions.rb +++ b/app/services/discourse_rewind/rewind/action/reactions.rb @@ -2,7 +2,7 @@ # For a most user / received reactions cards module DiscourseRewind - class Rewind::Action::Reactions < Action::Base + class Rewind::Action::Reactions < Rewind::Action::BaseReport def call post_used_reactions = {} post_received_reactions = {} diff --git a/app/services/discourse_rewind/rewind/action/reading_time.rb b/app/services/discourse_rewind/rewind/action/reading_time.rb index ebeeb41..c5d1fd9 100644 --- a/app/services/discourse_rewind/rewind/action/reading_time.rb +++ b/app/services/discourse_rewind/rewind/action/reading_time.rb @@ -3,9 +3,9 @@ # For showcasing the reading time of a user # Should we show book covers or just the names? module DiscourseRewind - class Rewind::Action::ReadingTime < Action::Base + class Rewind::Action::ReadingTime < Rewind::Action::BaseReport def call - reading_time = UserVisit.where(user: user).where(visited_at: date).sum(:time_read) + reading_time = UserVisit.where(user_id: user.id).where(visited_at: date).sum(:time_read) { data: { @@ -38,7 +38,7 @@ module DiscourseRewind "And Then There Were None" => 16_200, "The Alchemist" => 10_800, "The Hitchhiker's Guide to the Galaxy" => 12_600, - } + }.symbolize_keys end def best_book_fit(reading_time) diff --git a/app/services/discourse_rewind/rewind/fetch.rb b/app/services/discourse_rewind/rewind/fetch.rb index 0933721..830a58d 100644 --- a/app/services/discourse_rewind/rewind/fetch.rb +++ b/app/services/discourse_rewind/rewind/fetch.rb @@ -5,11 +5,7 @@ module DiscourseRewind # # @example # ::DiscourseRewind::Rewind::Fetch.call( - # guardian: guardian, - # params: { - # username: "falco", - # year: 2024, - # } + # guardian: guardian # ) # class Rewind::Fetch @@ -22,18 +18,9 @@ module DiscourseRewind # @option params [Integer] :username of the rewind # @return [Service::Base::Context] - # Do we need a custom order? - available_reports = DiscourseRewind::Rewind::Action::Base.descendants - CACHE_DURATION = 5.minutes - params do - attribute :year, :integer - attribute :username, :string - - validates :year, presence: true - validates :username, presence: true - end + YEAR = 2024 model :user model :date @@ -41,23 +28,24 @@ module DiscourseRewind private - def fetch_user(params:) - User.find_by_username(params.username) + def fetch_user(guardian:) + User.find_by_username(guardian.user.username) end def fetch_date(params:) - Date.new(params.year).all_year + Date.new(YEAR).all_year end - def fetch_reports(params:, date:, user:, guardian:) - key = "rewind:#{params.username}:#{params.year}" + def fetch_reports(date:, user:, guardian:) + key = "rewind:#{guardian.user.username}:#{YEAR}" reports = Discourse.redis.get(key) if Rails.env.development? || !reports reports = - available_reports + ::DiscourseRewind::Rewind::Action::BaseReport + .descendants .filter { _1.enabled? } - .map { |report| report.call(params:, date:, user:, guardian:) } + .map { |report| report.call(date:, user:, guardian:) } Discourse.redis.setex(key, CACHE_DURATION, MultiJson.dump(reports)) else reports = MultiJson.load(reports, symbolize_keys: true) diff --git a/app/views/discourse_rewind/rewinds/show.html.erb b/app/views/discourse_rewind/rewinds/show.html.erb deleted file mode 100644 index ca5e6a3..0000000 --- a/app/views/discourse_rewind/rewinds/show.html.erb +++ /dev/null @@ -1,31 +0,0 @@ - - -
-{{report.identifier}}
+ {{/each}} +