diff --git a/plugin.rb b/plugin.rb index c7c2eae..981da48 100644 --- a/plugin.rb +++ b/plugin.rb @@ -4,7 +4,7 @@ # name: discourse-legacy_links # about: A plugin to handle legacy Gossamer Forums URLs -# version: 0.47 +# version: 0.48 # authors: saint@federated.computer # url: https://gitea.federated.computer/saint/discourse-legacy_links.git @@ -64,20 +64,34 @@ after_initialize do # 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) - .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'}") + + 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 else - Rails.logger.warn("DiscourseLegacyLinks: No topic found with original_gossamer_id: #{post_id}") + Rails.logger.warn("DiscourseLegacyLinks: No post found with original_gossamer_id: #{post_id}") nil end end +# 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) +# .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'}") +# post +# else +# Rails.logger.warn("DiscourseLegacyLinks: No topic found with original_gossamer_id: #{post_id}") +# nil +# end +# end # Redirects to the URL of the found post def redirect_to_post(post)