discourse-legacy_links/plugin.rb

113 lines
3.7 KiB
Ruby
Raw Normal View History

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
# version: 0.47
2024-09-06 14:06:57 +00:00
# authors: saint@federated.computer
# url: https://gitea.federated.computer/saint/discourse-legacy_links.git
# enabled_site_setting :discourse_legacy_links_enabled
2024-09-06 14:06:57 +00:00
2024-09-07 12:59:50 +00:00
after_initialize do
module ::DiscourseLegacyLinks
class Engine < ::Rails::Engine
engine_name "discourse_legacy_links"
isolate_namespace DiscourseLegacyLinks
end
end
2024-09-07 12:59:50 +00:00
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
2024-09-07 12:59:50 +00:00
def index
Rails.logger.info("DiscourseLegacyLinks: Handling request for URL: #{request.original_url}")
2024-09-07 12:59:50 +00:00
post_id = extract_post_id_from_request
2024-09-07 12:59:50 +00:00
if post_id
Rails.logger.info("DiscourseLegacyLinks: Extracted post_id: #{post_id}")
post = find_post_by_custom_field(post_id)
2024-09-07 12:59:50 +00:00
if post
Rails.logger.info("DiscourseLegacyLinks: Found matching post with id: #{post.id}")
redirect_to_post(post)
else
Rails.logger.warn("DiscourseLegacyLinks: No matching post found for post_id: #{post_id}")
render_not_found
2024-09-07 12:59:50 +00:00
end
else
Rails.logger.warn("DiscourseLegacyLinks: Unable to extract post_id from request")
render_bad_request
end
end
2024-09-07 12:59:50 +00:00
private
2024-09-07 12:59:50 +00:00
# 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)
2024-09-07 12:59:50 +00:00
Rails.logger.info("DiscourseLegacyLinks: Extracted raw post_id: #{post_id || 'nil'}")
post_id.to_i if post_id
end
2024-09-07 12:59:50 +00:00
# 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}")
topic = Topic.joins(:custom_fields)
2024-09-07 12:59:50 +00:00
.where(topic_custom_fields: { name: 'original_gossamer_id', value: post_id.to_s })
.first
if topic
post = topic.posts.first
Rails.logger.info("DiscourseLegacyLinks: Found topic #{topic.id}, first post id: #{post&.id || 'nil'}")
2024-09-07 12:59:50 +00:00
post
else
Rails.logger.warn("DiscourseLegacyLinks: No topic found with original_gossamer_id: #{post_id}")
nil
2024-09-07 12:59:50 +00:00
end
end
2024-09-07 12:59:50 +00:00
# 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
2024-09-07 12:59:50 +00:00
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
2024-09-06 14:06:57 +00:00
end