From 5ef759118523675a1e7556fc27eeb579a8996803 Mon Sep 17 00:00:00 2001 From: saint Date: Sat, 7 Sep 2024 22:20:10 +1000 Subject: [PATCH] 0.28 reconfigure whole plugin to more advanced layout --- app/controllers/custom_post_controller.rb | 14 ++ config/routes.rb | 5 + lib/discourse_legacy_links/engine.rb | 13 ++ plugin.bck | 154 ++++++++++++++++++++++ plugin.rb | 146 ++------------------ 5 files changed, 194 insertions(+), 138 deletions(-) create mode 100644 app/controllers/custom_post_controller.rb create mode 100644 config/routes.rb create mode 100644 lib/discourse_legacy_links/engine.rb create mode 100644 plugin.bck diff --git a/app/controllers/custom_post_controller.rb b/app/controllers/custom_post_controller.rb new file mode 100644 index 0000000..d0ed258 --- /dev/null +++ b/app/controllers/custom_post_controller.rb @@ -0,0 +1,14 @@ +# plugins/discourse_legacy_links/app/controllers/custom_post_controller.rb +module DiscourseLegacyLinks + class CustomPostController < ::ApplicationController + layout false + + def index2 + Rails.logger.warn "[WARN WARN WARN WARN WARN WARN WARN WARN WARN WARN AMDG JMJ PAX WARN WARN WARN WARN]" + render plain: 'Test route' + end + + # Other actions and methods + end +end + diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000..1e22045 --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,5 @@ +# plugins/discourse_legacy_links/config/routes.rb +DiscourseLegacyLinks::Engine.routes.draw do + get '/forum2', to: 'custom_post#index2' +end + diff --git a/lib/discourse_legacy_links/engine.rb b/lib/discourse_legacy_links/engine.rb new file mode 100644 index 0000000..67a8e7c --- /dev/null +++ b/lib/discourse_legacy_links/engine.rb @@ -0,0 +1,13 @@ +# plugins/discourse_legacy_links/lib/discourse_legacy_links/engine.rb +module DiscourseLegacyLinks + class Engine < ::Rails::Engine + engine_name 'discourse_legacy_links' + isolate_namespace DiscourseLegacyLinks + + # Optional: Load additional files or initializers + initializer "discourse_legacy_links.load_app_instance_data" do |app| + # Example: Add custom middleware or configure settings + # app.middleware.use SomeMiddleware + end + end +end diff --git a/plugin.bck b/plugin.bck new file mode 100644 index 0000000..5f7f25b --- /dev/null +++ b/plugin.bck @@ -0,0 +1,154 @@ +# plugins/discourse-legacy_links/plugin.rb + +# frozen_string_literal: true + +# name: discourse-legacy_links +# about: A plugin to handle legacy Gossamer Forums URLs +# version: 0.27 +# authors: saint@federated.computer +# url: https://gitea.federated.computer/saint/discourse-legacy_links.git + +# require 'digest' + +# enabled_site_setting :discourse_legacy_links_enabled + +# after_initialize do +after_initialize do + module ::DiscourseLegacyLinks + class Engine < ::Rails::Engine + engine_name "discourse_legacy_links" + isolate_namespace DiscourseLegacyLinks + end + + # Define the custom controller + class DiscourseLegacyLinks::CustomPostController < ::ApplicationController + layout false +# skip_before_action :verify_authenticity_token +# skip_before_action :ensure_logged_in +# skip_before_action :redirect_to_login_if_required +# skip_before_action :check_xhr +# skip_before_action :ensure_staff + + def index2 + Rails.logger.warn "[WARN WARN WARN WARN WARN WARN WARN WARN WARN WARN AMDG JMJ PAX WARN WARN WARN WARN]" + render plain: 'Test route' + end + + # Match URLs that include a post_id at the end or query parameter + # Main action triggered when the route is matched. + def index + Rails.logger.warn "[DEBUG] Reached the index action" + + # Log all incoming parameters to see what's being passed + Rails.logger.warn "[DEBUG] Params: #{params.inspect}" + post_id = extract_post_id_from_request + + if post_id + Rails.logger.warn "[DEBUG] Processing request for post_id: #{post_id}" + post = find_post_by_custom_field(post_id) + + if post + # Redirect to the post URL if found + Rails.logger.warn "[DEBUG] Redirecting to post with ID: #{post.id}" + # redirect_to post_url(post) + redirect_to_post(post) + else + # Return 404 if the post is not found + Rails.logger.warn "[ERROR] Post with original_gossamer_id #{post_id} not found" + render plain: 'Post not found', status: 404 + end + else + # Handle cases where the post_id cannot be extracted + Rails.logger.warn "[ERROR] Could not extract post_id from the request" + render plain: 'Invalid URL', status: 400 + end + end + +# private + + # Extracts the post ID from the request URL or query string. + # Supports various formats of legacy forum URLs. + # @return [Integer, nil] The extracted post ID or nil if not found + def extract_post_id_from_request + # Check if post_id is passed as a query parameter (?post=post_id) + if params[:post_id] + post_id = params[:post_id].to_i + Rails.logger.warn "[DEBUG] Extracted post_id from query param: #{post_id}" + return post_id + end + + # Check for post_id in the URL path (e.g., /forum/.../P12345/) + if match = request.path.match(%r{/forum/[^/]+/P(\d+)/?}) + post_id = match[1].to_i + Rails.logger.warn "[DEBUG] Extracted post_id from URL path: #{post_id}" + return post_id + end + + # Check for post_id in the query string (e.g., ?post=12345) + if match = request.query_string.match(/post=(\d+)/) + post_id = match[1].to_i + Rails.logger.warn "[DEBUG] Extracted post_id from query string (?post=): #{post_id}" + return post_id + end + + # Check for parent_post_id in the query string (e.g., ?parent_post_id=12345) + if match = request.query_string.match(/parent_post_id=(\d+)/) + post_id = match[1].to_i + Rails.logger.warn "[DEBUG] Extracted parent_post_id from query string: #{post_id}" + return post_id + end + + # If no post_id was found in any format + Rails.logger.warn "[DEBUG] Could not extract post_id from request" + nil + end + + # Finds the Discourse post that has the specified custom field value. + # The custom field used is 'original_gossamer_id'. + # @param post_id [Integer] The extracted Gossamer forum post ID + # @return [Post, nil] The corresponding Discourse post or nil if not found + def find_post_by_custom_field(post_id) + Rails.logger.warn "[DEBUG] Searching for post with custom field 'original_gossamer_id' and value #{post_id}" + + # Use ActiveRecord query to find the post by the custom field value + post = Post.joins(:topic_custom_fields) + .where(topic_custom_fields: { name: 'original_gossamer_id', value: post_id.to_s }) + .first + + if post + Rails.logger.warn "[DEBUG] Found post with ID: #{post.id} for original_gossamer_id: #{post_id}" + else + Rails.logger.warn "[DEBUG] No post found for original_gossamer_id: #{post_id}" + end + + post + end + + # Redirects to the specified Discourse post. + # @param post [Post] The Discourse post to redirect to + def redirect_to_post(post) + topic_url = "#{Discourse.base_url}/t/#{post.topic_id}/#{post.post_number}" + Rails.logger.warn "[DEBUG] Redirecting to topic URL: #{topic_url}" + redirect topic_url + end + end + + # Register custom routes to handle legacy URLs + Rails.logger.warn "[DEBUG DEBUG DEBUG DEBUG DEBUG WOW WOW WOW]" + Rails.application.routes.routes.each do |route| + Rails.logger.warn "[ROUTE] #{route.path.spec}" + end +# Discourse::Application.routes.append do + Discourse::Application.routes.prepend do +# get '/forum/*path', to: 'discourse_legacy_links/custom_post#index', constraints: lambda { |req| !Permalink.exists?(url: req.path) } +# get '/forum', to: 'discourse_legacy_links/custom_post#index', constraints: lambda { |request| Rails.logger.warn("[DEBUG] NoStarForum Route matched: #{request.path}") } +# get '/forum/*path', to: 'discourse_legacy_links/custom_post#index', constraints: lambda { |request| Rails.logger.warn("[DEBUG] StarForum Route matched: #{request.path}") } +# get '/forum2', to: 'discourse_legacy_links/custom_post#index2', constraints: lambda { |request| Rails.logger.warn("[DEBUG] NoStarTestRoute Route matched: #{request.path}") } +# get '/forum2/*path', to: 'discourse_legacy_links/custom_post#index2', constraints: lambda { |request| Rails.logger.warn("[DEBUG] StarTestRoute Route matched: #{request.path}") } + get '/forum2' => 'discourse_legacy_links/custom_post#index2' +# get '/forum/*path' => 'discourse_legacy_links/custom_post#index' +# get '/forum' => 'discourse_legacy_links/custom_post#index' + end + end +end + diff --git a/plugin.rb b/plugin.rb index 5f7f25b..7adb743 100644 --- a/plugin.rb +++ b/plugin.rb @@ -4,7 +4,7 @@ # name: discourse-legacy_links # about: A plugin to handle legacy Gossamer Forums URLs -# version: 0.27 +# version: 0.28 # authors: saint@federated.computer # url: https://gitea.federated.computer/saint/discourse-legacy_links.git @@ -12,143 +12,13 @@ # enabled_site_setting :discourse_legacy_links_enabled -# after_initialize do -after_initialize do - module ::DiscourseLegacyLinks - class Engine < ::Rails::Engine - engine_name "discourse_legacy_links" - isolate_namespace DiscourseLegacyLinks - end - - # Define the custom controller - class DiscourseLegacyLinks::CustomPostController < ::ApplicationController - layout false -# skip_before_action :verify_authenticity_token -# skip_before_action :ensure_logged_in -# skip_before_action :redirect_to_login_if_required -# skip_before_action :check_xhr -# skip_before_action :ensure_staff - - def index2 - Rails.logger.warn "[WARN WARN WARN WARN WARN WARN WARN WARN WARN WARN AMDG JMJ PAX WARN WARN WARN WARN]" - render plain: 'Test route' - end - - # Match URLs that include a post_id at the end or query parameter - # Main action triggered when the route is matched. - def index - Rails.logger.warn "[DEBUG] Reached the index action" - - # Log all incoming parameters to see what's being passed - Rails.logger.warn "[DEBUG] Params: #{params.inspect}" - post_id = extract_post_id_from_request - - if post_id - Rails.logger.warn "[DEBUG] Processing request for post_id: #{post_id}" - post = find_post_by_custom_field(post_id) - - if post - # Redirect to the post URL if found - Rails.logger.warn "[DEBUG] Redirecting to post with ID: #{post.id}" - # redirect_to post_url(post) - redirect_to_post(post) - else - # Return 404 if the post is not found - Rails.logger.warn "[ERROR] Post with original_gossamer_id #{post_id} not found" - render plain: 'Post not found', status: 404 - end - else - # Handle cases where the post_id cannot be extracted - Rails.logger.warn "[ERROR] Could not extract post_id from the request" - render plain: 'Invalid URL', status: 400 - end - end - -# private - - # Extracts the post ID from the request URL or query string. - # Supports various formats of legacy forum URLs. - # @return [Integer, nil] The extracted post ID or nil if not found - def extract_post_id_from_request - # Check if post_id is passed as a query parameter (?post=post_id) - if params[:post_id] - post_id = params[:post_id].to_i - Rails.logger.warn "[DEBUG] Extracted post_id from query param: #{post_id}" - return post_id - end - - # Check for post_id in the URL path (e.g., /forum/.../P12345/) - if match = request.path.match(%r{/forum/[^/]+/P(\d+)/?}) - post_id = match[1].to_i - Rails.logger.warn "[DEBUG] Extracted post_id from URL path: #{post_id}" - return post_id - end - - # Check for post_id in the query string (e.g., ?post=12345) - if match = request.query_string.match(/post=(\d+)/) - post_id = match[1].to_i - Rails.logger.warn "[DEBUG] Extracted post_id from query string (?post=): #{post_id}" - return post_id - end - - # Check for parent_post_id in the query string (e.g., ?parent_post_id=12345) - if match = request.query_string.match(/parent_post_id=(\d+)/) - post_id = match[1].to_i - Rails.logger.warn "[DEBUG] Extracted parent_post_id from query string: #{post_id}" - return post_id - end - - # If no post_id was found in any format - Rails.logger.warn "[DEBUG] Could not extract post_id from request" - nil - end - - # Finds the Discourse post that has the specified custom field value. - # The custom field used is 'original_gossamer_id'. - # @param post_id [Integer] The extracted Gossamer forum post ID - # @return [Post, nil] The corresponding Discourse post or nil if not found - def find_post_by_custom_field(post_id) - Rails.logger.warn "[DEBUG] Searching for post with custom field 'original_gossamer_id' and value #{post_id}" - - # Use ActiveRecord query to find the post by the custom field value - post = Post.joins(:topic_custom_fields) - .where(topic_custom_fields: { name: 'original_gossamer_id', value: post_id.to_s }) - .first - - if post - Rails.logger.warn "[DEBUG] Found post with ID: #{post.id} for original_gossamer_id: #{post_id}" - else - Rails.logger.warn "[DEBUG] No post found for original_gossamer_id: #{post_id}" - end - - post - end - - # Redirects to the specified Discourse post. - # @param post [Post] The Discourse post to redirect to - def redirect_to_post(post) - topic_url = "#{Discourse.base_url}/t/#{post.topic_id}/#{post.post_number}" - Rails.logger.warn "[DEBUG] Redirecting to topic URL: #{topic_url}" - redirect topic_url - end - end - - # Register custom routes to handle legacy URLs - Rails.logger.warn "[DEBUG DEBUG DEBUG DEBUG DEBUG WOW WOW WOW]" - Rails.application.routes.routes.each do |route| - Rails.logger.warn "[ROUTE] #{route.path.spec}" - end -# Discourse::Application.routes.append do - Discourse::Application.routes.prepend do -# get '/forum/*path', to: 'discourse_legacy_links/custom_post#index', constraints: lambda { |req| !Permalink.exists?(url: req.path) } -# get '/forum', to: 'discourse_legacy_links/custom_post#index', constraints: lambda { |request| Rails.logger.warn("[DEBUG] NoStarForum Route matched: #{request.path}") } -# get '/forum/*path', to: 'discourse_legacy_links/custom_post#index', constraints: lambda { |request| Rails.logger.warn("[DEBUG] StarForum Route matched: #{request.path}") } -# get '/forum2', to: 'discourse_legacy_links/custom_post#index2', constraints: lambda { |request| Rails.logger.warn("[DEBUG] NoStarTestRoute Route matched: #{request.path}") } -# get '/forum2/*path', to: 'discourse_legacy_links/custom_post#index2', constraints: lambda { |request| Rails.logger.warn("[DEBUG] StarTestRoute Route matched: #{request.path}") } - get '/forum2' => 'discourse_legacy_links/custom_post#index2' -# get '/forum/*path' => 'discourse_legacy_links/custom_post#index' -# get '/forum' => 'discourse_legacy_links/custom_post#index' - end +module DiscourseLegacyLinks + class Engine < ::Rails::Engine + engine_name 'discourse_legacy_links' + isolate_namespace DiscourseLegacyLinks end end +# Load the plugin's engine and routes +require_relative 'lib/discourse_legacy_links/engine' +require_relative 'config/routes'