From 29ba2fe436bdd3a351a00818715a03e163846b5e Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Sat, 7 Dec 2024 17:30:25 +0100 Subject: [PATCH] first commit --- .../rewinds_assets_controller.rb | 32 +++++++++++++++++++ .../discourse_rewind/rewinds_controller.rb | 16 ++++++++++ .../my_plugin_module/examples_controller.rb | 11 ------- .../discourse_rewind/rewinds_helper.rb | 30 +++++++++++++++++ .../discourse_rewind/rewinds/show.html.erb | 18 +++++++++++ config/locales/client.en.yml | 6 ++-- config/routes.rb | 8 ++--- config/settings.yml | 4 +-- .../engine.rb | 4 +-- plugin.rb | 18 ++++++----- public/css/rewind.css | 3 ++ 11 files changed, 120 insertions(+), 30 deletions(-) create mode 100644 app/controllers/discourse_rewind/rewinds_assets_controller.rb create mode 100644 app/controllers/discourse_rewind/rewinds_controller.rb delete mode 100644 app/controllers/my_plugin_module/examples_controller.rb create mode 100644 app/helpers/discourse_rewind/rewinds_helper.rb create mode 100644 app/views/discourse_rewind/rewinds/show.html.erb rename lib/{my_plugin_module => discourse_rewind}/engine.rb (84%) create mode 100644 public/css/rewind.css diff --git a/app/controllers/discourse_rewind/rewinds_assets_controller.rb b/app/controllers/discourse_rewind/rewinds_assets_controller.rb new file mode 100644 index 0000000..d6ddbba --- /dev/null +++ b/app/controllers/discourse_rewind/rewinds_assets_controller.rb @@ -0,0 +1,32 @@ +# 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 new file mode 100644 index 0000000..e6d4cb6 --- /dev/null +++ b/app/controllers/discourse_rewind/rewinds_controller.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +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: true + response.headers["X-Robots-Tag"] = "noindex" + + render "show", layout: false + end + end +end diff --git a/app/controllers/my_plugin_module/examples_controller.rb b/app/controllers/my_plugin_module/examples_controller.rb deleted file mode 100644 index 9ca421b..0000000 --- a/app/controllers/my_plugin_module/examples_controller.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -module ::MyPluginModule - class ExamplesController < ::ApplicationController - requires_plugin PLUGIN_NAME - - def index - render json: { hello: "world" } - end - end -end diff --git a/app/helpers/discourse_rewind/rewinds_helper.rb b/app/helpers/discourse_rewind/rewinds_helper.rb new file mode 100644 index 0000000..ce399a8 --- /dev/null +++ b/app/helpers/discourse_rewind/rewinds_helper.rb @@ -0,0 +1,30 @@ +# 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] + p "-------------" + # return url if url + + content = File.read(DiscourseRewind.public_asset_path("css/#{asset_name}")) + + p content + sha1 = Digest::SHA1.hexdigest(content) + + url = "/rewinds/assets/#{sha1}/#{asset_name}" + + p @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/views/discourse_rewind/rewinds/show.html.erb b/app/views/discourse_rewind/rewinds/show.html.erb new file mode 100644 index 0000000..590d2de --- /dev/null +++ b/app/views/discourse_rewind/rewinds/show.html.erb @@ -0,0 +1,18 @@ + + + + <%= I18n.t("discourse_rewind.title", title: "RETRO TITLE", site_name: SiteSetting.title) %> + "> + + + + + "> + + + "> + + + TEST + + diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index e68cda5..aa12530 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -3,7 +3,7 @@ en: admin: site_settings: categories: - TODO_plugin_name: "Plugin Name" + discourse_rewind: "Discourse Rewind" js: - discourse_plugin_name: - placeholder: placeholder + discourse_rewind: + placeholder: Discourse Rewind diff --git a/config/routes.rb b/config/routes.rb index 25a67c9..96fa8e4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -MyPluginModule::Engine.routes.draw do - get "/examples" => "examples#index" - # define routes here +DiscourseRewind::Engine.routes.draw do + get "/rewinds/:year/:username" => "rewinds#show" + get "/rewinds/assets/:version/:name" => "rewinds_assets#show" end -Discourse::Application.routes.draw { mount ::MyPluginModule::Engine, at: "my-plugin" } +Discourse::Application.routes.draw { mount ::DiscourseRewind::Engine, at: "/" } diff --git a/config/settings.yml b/config/settings.yml index b9bac20..10506df 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -1,4 +1,4 @@ -TODO_plugin_name: - plugin_name_enabled: +discourse_rewind: + discourse_rewind_enabled: default: false client: true diff --git a/lib/my_plugin_module/engine.rb b/lib/discourse_rewind/engine.rb similarity index 84% rename from lib/my_plugin_module/engine.rb rename to lib/discourse_rewind/engine.rb index 9b5710c..fbe2499 100644 --- a/lib/my_plugin_module/engine.rb +++ b/lib/discourse_rewind/engine.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true -module ::MyPluginModule +module ::DiscourseRewind class Engine < ::Rails::Engine engine_name PLUGIN_NAME - isolate_namespace MyPluginModule + isolate_namespace DiscourseRewind config.autoload_paths << File.join(config.root, "lib") scheduled_job_dir = "#{config.root}/app/jobs/scheduled" config.to_prepare do diff --git a/plugin.rb b/plugin.rb index fc57c6f..20acf71 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -# name: discourse-plugin-name +# name: discourse-rewind # about: TODO # meta_topic_id: TODO # version: 0.0.1 @@ -8,14 +8,16 @@ # url: TODO # required_version: 2.7.0 -enabled_site_setting :plugin_name_enabled +enabled_site_setting :discourse_rewind_enabled -module ::MyPluginModule - PLUGIN_NAME = "discourse-plugin-name" +module ::DiscourseRewind + PLUGIN_NAME = "discourse-rewind" + + def self.public_asset_path(name) + File.expand_path(File.join(__dir__, "public", name)) + end end -require_relative "lib/my_plugin_module/engine" +require_relative "lib/discourse_rewind/engine" -after_initialize do - # Code which should run after Rails has finished booting -end +after_initialize {} diff --git a/public/css/rewind.css b/public/css/rewind.css new file mode 100644 index 0000000..fbf883e --- /dev/null +++ b/public/css/rewind.css @@ -0,0 +1,3 @@ +body { + background: #444; +}