Compare commits

...

33 Commits

Author SHA1 Message Date
1fde7e3874 0.52 Refine regex for extra case 2024-09-09 13:05:00 +10:00
d88413ec23 0.51 Final touches 2024-09-09 12:50:50 +10:00
29a80f4e76 0.50 Final touches 2024-09-09 12:34:53 +10:00
78ebc1ca92 0.49 Final touches 2024-09-09 12:29:04 +10:00
edb05aa37b 0.48 More rewrite based on recent success with blocker. 2024-09-09 12:11:39 +10:00
66ff59a915 0.46 More rewrite based on recent success with blocker. 2024-09-09 12:01:00 +10:00
5a750b947a 0.46 More rewrite based on recent success with blocker. 2024-09-09 11:54:02 +10:00
e7d6d2d877 0.41 Tweaks to see if we can get this stable. 2024-09-09 09:56:19 +10:00
03440aeb0c 0.40 Tweaks to see if we can get this stable. 2024-09-09 09:44:38 +10:00
0fd28b960b 0.39 Tweaks to see if we can get this stable. 2024-09-09 09:37:53 +10:00
0fbb75007a 0.38 Tweaks to see if we can get this stable. 2024-09-08 02:42:29 +10:00
cd7e636a10 0.37 Significnat changes after more research, for testing. 2024-09-08 02:24:38 +10:00
f28ead070c 0.35 Significnat changes after more research, for testing. 2024-09-08 02:16:45 +10:00
4db0962aa7 0.35 Significnat changes after more research, for testing. 2024-09-08 02:04:38 +10:00
bafdc2f93e 0.31 more attempts... 2024-09-08 00:30:58 +10:00
a4067d2f04 0.31 more attempts... 2024-09-07 23:33:54 +10:00
293b1a19d0 0.31 more attempts... 2024-09-07 23:22:30 +10:00
8abc394b3f 0.30 prep for debug enabling 2024-09-07 22:59:50 +10:00
5ef7591185 0.28 reconfigure whole plugin to more advanced layout 2024-09-07 22:20:10 +10:00
59cfc9828b v0.16 So close, but it doesn't work still... 2024-09-07 21:12:52 +10:00
a554ac50ee v0.16 So close, but it doesn't work still... 2024-09-07 21:11:03 +10:00
94fcd228a5 v0.16 So close, but it doesn't work still... 2024-09-07 21:08:27 +10:00
8509a6590e v0.16 So close, but it doesn't work still... 2024-09-07 17:37:20 +10:00
8b189e5e9e v0.16 So close, but it doesn't work still... 2024-09-07 17:31:57 +10:00
2fc149d593 v0.16 So close, but it doesn't work still... 2024-09-07 17:26:24 +10:00
dd24920058 v0.16 So close, but it doesn't work still... 2024-09-07 17:25:12 +10:00
92d62c721b v0.16 So close, but it doesn't work still... 2024-09-07 17:12:18 +10:00
82ca88caeb v0.16 So close, but it doesn't work still... 2024-09-07 17:11:18 +10:00
0cbc04a447 v0.16 So close, but it doesn't work still... 2024-09-07 17:03:48 +10:00
384ebb87cf v0.16 So close, but it doesn't work still... 2024-09-07 17:01:52 +10:00
76caab622f v0.16 So close, but it doesn't work still... 2024-09-07 16:59:48 +10:00
89e0b0a081 v0.15 So close, but it doesn't work still... 2024-09-07 14:58:40 +10:00
8dfae46151 v0.15 So close, but it doesn't work still... 2024-09-07 14:58:37 +10:00
6 changed files with 328 additions and 105 deletions

View File

@@ -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

5
config/routes.rb Normal file
View File

@@ -0,0 +1,5 @@
# plugins/discourse_legacy_links/config/routes.rb
DiscourseLegacyLinks::Engine.routes.draw do
get '/forum2', to: 'custom_post#index2'
end

View File

@@ -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

24
plugin.breakout Normal file
View File

@@ -0,0 +1,24 @@
# 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.28
# authors: saint@federated.computer
# url: https://gitea.federated.computer/saint/discourse-legacy_links.git
# require 'digest'
# enabled_site_setting :discourse_legacy_links_enabled
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'

174
plugin.first Normal file
View File

@@ -0,0 +1,174 @@
# 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.44
# authors: saint@federated.computer
# url: https://gitea.federated.computer/saint/discourse-legacy_links.git
# enabled_site_setting :discourse_legacy_links_enabled
after_initialize do
module ::DiscourseLegacyLinks
class Engine < ::Rails::Engine
engine_name "discourse_legacy_links"
isolate_namespace DiscourseLegacyLinks
end
end
require_dependency "application_controller"
# Define the custom controller, inheriting from Discourse's CustomPostController
# This gives us access to post-related functionality and user authentication
# class DiscourseLegacyLinks::CustomPostController < ::ApplicationController
# class LegacyLinksController::CustomPostController < ::ApplicationController
# class LegacyLinksController < ::ApplicationController::CustomPostController
# layout false
# Skip certain before_actions for our index method.
# This allows the route to be accessed directly via a browser
## skip_before_action :check_xhr, only: [:index2]
## skip_before_action :verify_authenticity_token, only: [:index2]
# 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 JMJ"
# render html: '<h1>JMJ</h1>'
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
end
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'
# # Register custom routes to handle legacy URLs
# DiscourseLegacyLinks::Engine.routes.draw do
# get "/forum2" => "legacy_links#index2"
# end
# Discourse::Application.routes.append do
# mount ::DiscourseLegacyLinks::Engine, at: "/"
# end
end

203
plugin.rb
View File

@@ -4,136 +4,129 @@
# name: discourse-legacy_links
# about: A plugin to handle legacy Gossamer Forums URLs
# version: 0.14
# version: 0.52
# 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
end
# Define the custom controller
class CustomPostController < ::ApplicationController
# Match URLs that include a post_id at the end or query parameter
# Main action triggered when the route is matched.
def test
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
require_dependency "application_controller"
class ::DiscourseLegacyLinks::LegacyLinksController < ::ApplicationController
skip_before_action :check_xhr, only: [:index]
def index2
render plain: "Legacy Links from Discourse plugin! Current user: #{current_user&.username || 'Guest'}"
end
def index
Rails.logger.warn "[DEBUG] Reached the index action"
def index
Rails.logger.info("DiscourseLegacyLinks: Handling request for URL: #{request.original_url}")
# Log all incoming parameters to see what's being passed
Rails.logger.warn "[DEBUG] Params: #{params.inspect}"
post_id = extract_post_id_from_request
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_id
Rails.logger.info("DiscourseLegacyLinks: Extracted post_id: #{post_id}")
post = find_post_by_custom_field(post_id)
if post
Rails.logger.warn "[DEBUG] Found post with ID: #{post.id} for original_gossamer_id: #{post_id}"
Rails.logger.info("DiscourseLegacyLinks: Found matching post with id: #{post.id}")
redirect_to_post(post)
else
Rails.logger.warn "[DEBUG] No post found for original_gossamer_id: #{post_id}"
Rails.logger.warn("DiscourseLegacyLinks: No matching post found for post_id: #{post_id}")
render_not_found
end
else
Rails.logger.warn("DiscourseLegacyLinks: Unable to extract post_id from request")
render_bad_request
end
end
private
# Attempts to extract a post ID from various parts of the request
def extract_post_id_from_request
# post_id = params[:post_id].presence ||
# request.path.match(%r{/forum/[^/]+/P(\d+)/?})&.[](1) ||
# request.query_string.match(/post=(\d+)/)&.[](1) ||
# request.query_string.match(/parent_post_id=(\d+)/)&.[](1)
# post_id = params[:post_id].presence ||
# request.path.match(%r{/P(\d+)/?})&. || # Matches URLs like /P40796/
# request.path.match(%r{/forum/ || # Matches URLs like /forum/.../P6751045/
# request.query_string.match(/post=(\d+)/)&. || # Matches URLs with ?post= in query string
# request.query_string.match(/parent_post_id=(\d+)/)&. || # Matches URLs with ?parent_post_id= in query string
# request.path.match(%r{/forum/.*?_P(\d+)/?})&. # Matches URLs like /forum/..._P6751045/
# post_id = params[:post_id].presence ||
# request.path.match(%r{/P(\d+)(?:/|$)})&. || # Matches URLs like /P40796/ or /P40796 (no trailing slash)
# request.path.match(%r{/forum/.*_P(\d+)(?:/|$)})&. || # Matches URLs like /forum/.../P6751045/ or /forum/.../P6751045
# request.query_string.match(/post=(\d+)/)&. || # Matches URLs with ?post= in query string
# request.query_string.match(/parent_post_id=(\d+)/)&. # Matches URLs with ?parent_post_id= in query string
# post_id = params[:post_id].presence ||
# request.path.match(%r{/P(\d+)(?:/|$)})&.[](1) || # Matches URLs like /P40796/ or /P40796 (no trailing slash)
# request.path.match(%r{/forum/.*_P(\d+)(?:/|$)})&.[](1) || # Matches URLs like /forum/.../P6751045/ or /forum/.../P6751045
# request.query_string.match(/post=(\d+)/)&.[](1) || # Matches URLs with ?post= in query string
# request.query_string.match(/parent_post_id=(\d+)/)&.[](1) # Matches URLs with ?parent_post_id= in query string
post_id = params[:post_id].presence ||
request.path.match(%r{/P(\d+)(?:-\d+)?(?:/|$)})&.[](1) || # Matches URLs like /P40796/, /P40796, or /P40796-5/
request.path.match(%r{/forum/.*_P(\d+)(?:-\d+)?(?:/|$)})&.[](1) || # Matches URLs like /forum/.../P6751045/, /forum/.../P6751045, or /forum/.../P6751045-5/
request.query_string.match(/post=(\d+)/)&.[](1) || # Matches URLs with ?post= in query string
request.query_string.match(/parent_post_id=(\d+)/)&.[](1) # Matches URLs with ?parent_post_id= in query string
Rails.logger.info("DiscourseLegacyLinks: Extracted raw post_id: #{post_id || 'nil'}")
post_id.to_i if post_id
end
# Finds a post by looking up the custom field on the associated topic
def find_post_by_custom_field(post_id)
Rails.logger.info("DiscourseLegacyLinks: Searching for post with original_gossamer_id: #{post_id}")
post_custom_field = PostCustomField.find_by(name: 'original_gossamer_id', value: post_id.to_s)
if post_custom_field
post = post_custom_field.post
Rails.logger.info("DiscourseLegacyLinks: Found post with id: #{post.id}")
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
else
Rails.logger.warn("DiscourseLegacyLinks: No post found with original_gossamer_id: #{post_id}")
nil
end
end
# Register custom routes to handle legacy URLs
Rails.logger.warn "[DEBUG DEBUG DEBUG DEBUG DEBUG WOW WOW WOW]"
# Discourse::Application.routes.append do
Discourse::Application.routes.append do
get '/test_route' => 'discourse_legacy_links/custom_post#test'
get '/forum/*path' => 'discourse_legacy_links/custom_post#index'
get '/forum' => 'discourse_legacy_links/custom_post#index'
# Redirects to the URL of the found post
def redirect_to_post(post)
Rails.logger.info("DiscourseLegacyLinks: Redirecting to post URL: #{post.url}")
redirect_to post.url, status: :moved_permanently
end
# Renders a 404 Not Found response
def render_not_found
Rails.logger.info("DiscourseLegacyLinks: Rendering 404 Not Found")
render plain: 'Post not found', status: :not_found
end
# Renders a 400 Bad Request response
def render_bad_request
Rails.logger.info("DiscourseLegacyLinks: Rendering 400 Bad Request")
render plain: 'Invalid URL', status: :bad_request
end
end
# Rails.logger.info("DiscourseLegacyLinks: initialistaion")
DiscourseLegacyLinks::Engine.routes.draw do
get "/forum" => "legacy_links#index"
get "/forum/*path" => "legacy_links#index"
end
Discourse::Application.routes.append do
mount ::DiscourseLegacyLinks::Engine, at: "/"
end
end