v0.2 Brand new plugin
This commit is contained in:
parent
235ea35edf
commit
8dfb85e36f
@ -0,0 +1,2 @@
|
||||
A custom Federated Computer plugin to redirect legacy Gossamer Forums URLs
|
||||
|
75
plugin.rb
Normal file
75
plugin.rb
Normal file
@ -0,0 +1,75 @@
|
||||
# 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.2
|
||||
# authors: saint@federated.computer
|
||||
# url: https://gitea.federated.computer/saint/discourse-legacy_links.git
|
||||
|
||||
# require 'digest'
|
||||
|
||||
# enabled_site_setting :discourse_legacy_routing_enabled
|
||||
|
||||
# after_initialize do
|
||||
after_initialize do
|
||||
module ::DiscourseCustomRouting
|
||||
class Engine < ::Rails::Engine
|
||||
engine_name "discourse_custom_routing"
|
||||
isolate_namespace DiscourseCustomRouting
|
||||
end
|
||||
|
||||
# Define the custom controller
|
||||
class CustomPostController < ::ApplicationController
|
||||
# Match URLs that include a post_id at the end or query parameter
|
||||
def index
|
||||
post_id = extract_post_id_from_request
|
||||
|
||||
if post_id
|
||||
post = find_post_by_custom_field(post_id)
|
||||
|
||||
if post
|
||||
# Redirect to the post URL if found
|
||||
redirect_to post_url(post)
|
||||
else
|
||||
# Return 404 if the post is not found
|
||||
render plain: 'Post not found', status: 404
|
||||
end
|
||||
else
|
||||
# Handle cases where the post_id cannot be extracted
|
||||
render plain: 'Invalid URL', status: 400
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def extract_post_id_from_request
|
||||
# Extract post_id from various URL formats
|
||||
if params[:post_id]
|
||||
params[:post_id].to_i
|
||||
elsif request.path.match(%r{/forum/[^/]+/P(\d+)/})
|
||||
$1.to_i
|
||||
elsif request.query_string.match(/post=(\d+)/)
|
||||
$1.to_i
|
||||
elsif request.query_string.match(/parent_post_id=(\d+)/)
|
||||
$1.to_i
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def find_post_by_custom_field(post_id)
|
||||
# Find the post with the specified custom field
|
||||
Post.joins(:topic_custom_fields)
|
||||
.where(topic_custom_fields: { name: 'original_gossamer_id', value: post_id.to_s })
|
||||
.first
|
||||
end
|
||||
end
|
||||
|
||||
# Register custom routes
|
||||
Discourse::Application.routes.append do
|
||||
get '/forum/*path' => 'custom_post#index'
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user