v0.6 Huge re-write... hoping it may work... added debug output
This commit is contained in:
		
							
								
								
									
										24
									
								
								plugin.rb
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								plugin.rb
									
									
									
									
									
								
							@@ -4,7 +4,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
# name: discourse-legacy_links
 | 
					# name: discourse-legacy_links
 | 
				
			||||||
# about: A plugin to handle legacy Gossamer Forums URLs
 | 
					# about: A plugin to handle legacy Gossamer Forums URLs
 | 
				
			||||||
# version: 0.5
 | 
					# version: 0.6
 | 
				
			||||||
# authors: saint@federated.computer
 | 
					# authors: saint@federated.computer
 | 
				
			||||||
# url: https://gitea.federated.computer/saint/discourse-legacy_links.git
 | 
					# url: https://gitea.federated.computer/saint/discourse-legacy_links.git
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -28,22 +28,22 @@ after_initialize do
 | 
				
			|||||||
        post_id = extract_post_id_from_request
 | 
					        post_id = extract_post_id_from_request
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if post_id
 | 
					        if post_id
 | 
				
			||||||
          logger.info "[DEBUG] Processing request for post_id: #{post_id}"
 | 
					          logger.warn "[DEBUG] Processing request for post_id: #{post_id}"
 | 
				
			||||||
          post = find_post_by_custom_field(post_id)
 | 
					          post = find_post_by_custom_field(post_id)
 | 
				
			||||||
          
 | 
					          
 | 
				
			||||||
          if post
 | 
					          if post
 | 
				
			||||||
            # Redirect to the post URL if found
 | 
					            # Redirect to the post URL if found
 | 
				
			||||||
            logger.info "[DEBUG] Redirecting to post with ID: #{post.id}"
 | 
					            logger.warn "[DEBUG] Redirecting to post with ID: #{post.id}"
 | 
				
			||||||
            # redirect_to post_url(post)
 | 
					            # redirect_to post_url(post)
 | 
				
			||||||
            redirect_to_post(post)
 | 
					            redirect_to_post(post)
 | 
				
			||||||
          else
 | 
					          else
 | 
				
			||||||
            # Return 404 if the post is not found
 | 
					            # Return 404 if the post is not found
 | 
				
			||||||
            logger.error "[ERROR] Post with original_gossamer_id #{post_id} not found"
 | 
					            logger.warn "[ERROR] Post with original_gossamer_id #{post_id} not found"
 | 
				
			||||||
            render plain: 'Post not found', status: 404
 | 
					            render plain: 'Post not found', status: 404
 | 
				
			||||||
          end
 | 
					          end
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
          # Handle cases where the post_id cannot be extracted
 | 
					          # Handle cases where the post_id cannot be extracted
 | 
				
			||||||
          logger.error "[ERROR] Could not extract post_id from the request"
 | 
					          logger.warn "[ERROR] Could not extract post_id from the request"
 | 
				
			||||||
          render plain: 'Invalid URL', status: 400
 | 
					          render plain: 'Invalid URL', status: 400
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
@@ -57,28 +57,28 @@ after_initialize do
 | 
				
			|||||||
        # Check if post_id is passed as a query parameter (?post=post_id)
 | 
					        # Check if post_id is passed as a query parameter (?post=post_id)
 | 
				
			||||||
        if params[:post_id]
 | 
					        if params[:post_id]
 | 
				
			||||||
          post_id = params[:post_id].to_i
 | 
					          post_id = params[:post_id].to_i
 | 
				
			||||||
          logger.info "[DEBUG] Extracted post_id from query param: #{post_id}"
 | 
					          logger.warn "[DEBUG] Extracted post_id from query param: #{post_id}"
 | 
				
			||||||
          return post_id
 | 
					          return post_id
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Check for post_id in the URL path (e.g., /forum/.../P12345/)
 | 
					        # Check for post_id in the URL path (e.g., /forum/.../P12345/)
 | 
				
			||||||
        if match = request.path.match(%r{/forum/[^/]+/P(\d+)/?})
 | 
					        if match = request.path.match(%r{/forum/[^/]+/P(\d+)/?})
 | 
				
			||||||
          post_id = match[1].to_i
 | 
					          post_id = match[1].to_i
 | 
				
			||||||
          logger.info "[DEBUG] Extracted post_id from URL path: #{post_id}"
 | 
					          logger.warn "[DEBUG] Extracted post_id from URL path: #{post_id}"
 | 
				
			||||||
          return post_id
 | 
					          return post_id
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Check for post_id in the query string (e.g., ?post=12345)
 | 
					        # Check for post_id in the query string (e.g., ?post=12345)
 | 
				
			||||||
        if match = request.query_string.match(/post=(\d+)/)
 | 
					        if match = request.query_string.match(/post=(\d+)/)
 | 
				
			||||||
          post_id = match[1].to_i
 | 
					          post_id = match[1].to_i
 | 
				
			||||||
          logger.info "[DEBUG] Extracted post_id from query string (?post=): #{post_id}"
 | 
					          logger.warn "[DEBUG] Extracted post_id from query string (?post=): #{post_id}"
 | 
				
			||||||
          return post_id
 | 
					          return post_id
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Check for parent_post_id in the query string (e.g., ?parent_post_id=12345)
 | 
					        # 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+)/)
 | 
					        if match = request.query_string.match(/parent_post_id=(\d+)/)
 | 
				
			||||||
          post_id = match[1].to_i
 | 
					          post_id = match[1].to_i
 | 
				
			||||||
          logger.info "[DEBUG] Extracted parent_post_id from query string: #{post_id}"
 | 
					          logger.warn "[DEBUG] Extracted parent_post_id from query string: #{post_id}"
 | 
				
			||||||
          return post_id
 | 
					          return post_id
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -92,7 +92,7 @@ after_initialize do
 | 
				
			|||||||
      # @param post_id [Integer] The extracted Gossamer forum post ID
 | 
					      # @param post_id [Integer] The extracted Gossamer forum post ID
 | 
				
			||||||
      # @return [Post, nil] The corresponding Discourse post or nil if not found
 | 
					      # @return [Post, nil] The corresponding Discourse post or nil if not found
 | 
				
			||||||
      def find_post_by_custom_field(post_id)
 | 
					      def find_post_by_custom_field(post_id)
 | 
				
			||||||
        logger.info "[DEBUG] Searching for post with custom field 'original_gossamer_id' and value #{post_id}"
 | 
					        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
 | 
					        # Use ActiveRecord query to find the post by the custom field value
 | 
				
			||||||
        post = Post.joins(:topic_custom_fields)
 | 
					        post = Post.joins(:topic_custom_fields)
 | 
				
			||||||
@@ -100,7 +100,7 @@ after_initialize do
 | 
				
			|||||||
                   .first
 | 
					                   .first
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if post
 | 
					        if post
 | 
				
			||||||
          logger.info "[DEBUG] Found post with ID: #{post.id} for original_gossamer_id: #{post_id}"
 | 
					          logger.warn "[DEBUG] Found post with ID: #{post.id} for original_gossamer_id: #{post_id}"
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
          logger.warn "[DEBUG] No post found for original_gossamer_id: #{post_id}"
 | 
					          logger.warn "[DEBUG] No post found for original_gossamer_id: #{post_id}"
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
@@ -112,7 +112,7 @@ after_initialize do
 | 
				
			|||||||
      # @param post [Post] The Discourse post to redirect to
 | 
					      # @param post [Post] The Discourse post to redirect to
 | 
				
			||||||
      def redirect_to_post(post)
 | 
					      def redirect_to_post(post)
 | 
				
			||||||
        topic_url = "#{Discourse.base_url}/t/#{post.topic_id}/#{post.post_number}"
 | 
					        topic_url = "#{Discourse.base_url}/t/#{post.topic_id}/#{post.post_number}"
 | 
				
			||||||
        logger.info "[DEBUG] Redirecting to topic URL: #{topic_url}"
 | 
					        logger.warn "[DEBUG] Redirecting to topic URL: #{topic_url}"
 | 
				
			||||||
        redirect topic_url
 | 
					        redirect topic_url
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user