2024-09-06 14:06:57 +00:00
|
|
|
# plugins/discourse-legacy_links/plugin.rb
|
|
|
|
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# name: discourse-legacy_links
|
|
|
|
# about: A plugin to handle legacy Gossamer Forums URLs
|
2024-09-07 04:58:40 +00:00
|
|
|
# version: 0.15
|
2024-09-06 14:06:57 +00:00
|
|
|
# authors: saint@federated.computer
|
|
|
|
# url: https://gitea.federated.computer/saint/discourse-legacy_links.git
|
|
|
|
|
|
|
|
# require 'digest'
|
|
|
|
|
2024-09-06 16:59:53 +00:00
|
|
|
# enabled_site_setting :discourse_legacy_links_enabled
|
2024-09-06 14:06:57 +00:00
|
|
|
|
|
|
|
# after_initialize do
|
|
|
|
after_initialize do
|
2024-09-06 14:51:22 +00:00
|
|
|
module ::DiscourseLegacyLinks
|
2024-09-06 14:06:57 +00:00
|
|
|
class Engine < ::Rails::Engine
|
2024-09-06 14:51:22 +00:00
|
|
|
engine_name "discourse_legacy_links"
|
|
|
|
isolate_namespace DiscourseLegacyLinks
|
2024-09-06 14:06:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Define the custom controller
|
2024-09-06 16:49:01 +00:00
|
|
|
class CustomPostController < ::ApplicationController
|
2024-09-06 14:06:57 +00:00
|
|
|
# Match URLs that include a post_id at the end or query parameter
|
2024-09-06 14:51:22 +00:00
|
|
|
# Main action triggered when the route is matched.
|
2024-09-06 16:20:45 +00:00
|
|
|
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
|
|
|
|
|
2024-09-06 14:06:57 +00:00
|
|
|
def index
|
2024-09-06 15:54:00 +00:00
|
|
|
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}"
|
2024-09-06 14:06:57 +00:00
|
|
|
post_id = extract_post_id_from_request
|
|
|
|
|
|
|
|
if post_id
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[DEBUG] Processing request for post_id: #{post_id}"
|
2024-09-06 14:06:57 +00:00
|
|
|
post = find_post_by_custom_field(post_id)
|
|
|
|
|
|
|
|
if post
|
|
|
|
# Redirect to the post URL if found
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[DEBUG] Redirecting to post with ID: #{post.id}"
|
2024-09-06 15:14:22 +00:00
|
|
|
# redirect_to post_url(post)
|
|
|
|
redirect_to_post(post)
|
2024-09-06 14:06:57 +00:00
|
|
|
else
|
|
|
|
# Return 404 if the post is not found
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[ERROR] Post with original_gossamer_id #{post_id} not found"
|
2024-09-06 14:06:57 +00:00
|
|
|
render plain: 'Post not found', status: 404
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# Handle cases where the post_id cannot be extracted
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[ERROR] Could not extract post_id from the request"
|
2024-09-06 14:06:57 +00:00
|
|
|
render plain: 'Invalid URL', status: 400
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-09-06 14:51:22 +00:00
|
|
|
# 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
|
2024-09-06 14:06:57 +00:00
|
|
|
def extract_post_id_from_request
|
2024-09-06 14:51:22 +00:00
|
|
|
# Check if post_id is passed as a query parameter (?post=post_id)
|
2024-09-06 14:06:57 +00:00
|
|
|
if params[:post_id]
|
2024-09-06 14:51:22 +00:00
|
|
|
post_id = params[:post_id].to_i
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[DEBUG] Extracted post_id from query param: #{post_id}"
|
2024-09-06 14:51:22 +00:00
|
|
|
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
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[DEBUG] Extracted post_id from URL path: #{post_id}"
|
2024-09-06 14:51:22 +00:00
|
|
|
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
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[DEBUG] Extracted post_id from query string (?post=): #{post_id}"
|
2024-09-06 14:51:22 +00:00
|
|
|
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
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[DEBUG] Extracted parent_post_id from query string: #{post_id}"
|
2024-09-06 14:51:22 +00:00
|
|
|
return post_id
|
2024-09-06 14:06:57 +00:00
|
|
|
end
|
2024-09-06 14:51:22 +00:00
|
|
|
|
|
|
|
# If no post_id was found in any format
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[DEBUG] Could not extract post_id from request"
|
2024-09-06 14:51:22 +00:00
|
|
|
nil
|
2024-09-06 14:06:57 +00:00
|
|
|
end
|
|
|
|
|
2024-09-06 14:51:22 +00:00
|
|
|
# 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
|
2024-09-06 14:06:57 +00:00
|
|
|
def find_post_by_custom_field(post_id)
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[DEBUG] Searching for post with custom field 'original_gossamer_id' and value #{post_id}"
|
2024-09-06 14:51:22 +00:00
|
|
|
|
|
|
|
# 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
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[DEBUG] Found post with ID: #{post.id} for original_gossamer_id: #{post_id}"
|
2024-09-06 14:51:22 +00:00
|
|
|
else
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[DEBUG] No post found for original_gossamer_id: #{post_id}"
|
2024-09-06 14:51:22 +00:00
|
|
|
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}"
|
2024-09-06 15:34:11 +00:00
|
|
|
Rails.logger.warn "[DEBUG] Redirecting to topic URL: #{topic_url}"
|
2024-09-06 14:51:22 +00:00
|
|
|
redirect topic_url
|
2024-09-06 14:06:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-06 14:51:22 +00:00
|
|
|
# Register custom routes to handle legacy URLs
|
2024-09-06 16:59:53 +00:00
|
|
|
Rails.logger.warn "[DEBUG DEBUG DEBUG DEBUG DEBUG WOW WOW WOW]"
|
2024-09-06 16:49:01 +00:00
|
|
|
# Discourse::Application.routes.append do
|
2024-09-07 04:58:37 +00:00
|
|
|
Discourse::Application.routes.prepend do
|
2024-09-06 16:25:08 +00:00
|
|
|
get '/test_route' => 'discourse_legacy_links/custom_post#test'
|
2024-09-06 15:14:22 +00:00
|
|
|
get '/forum/*path' => 'discourse_legacy_links/custom_post#index'
|
2024-09-06 15:34:11 +00:00
|
|
|
get '/forum' => 'discourse_legacy_links/custom_post#index'
|
|
|
|
end
|
2024-09-06 14:06:57 +00:00
|
|
|
end
|
|
|
|
end
|
2024-09-06 14:51:22 +00:00
|
|
|
|