Auto Tagging feature plugin

Hi everyone! I’m creating a system which tags are created automatically, so every person could create tags!

About this matter:

  • Tags are create in form of topic creation
  • If there isn’t a tag in which the person mentioned, it will ignore by default in post

My goal was to create a way of auto tagging, so if #test1 doesn’t exist, create it automatically and refresh cooked of post to update the link to tags and so on.

So, i was trying some ways to perform this demand, so i would like to show you and ask if there is a better way to achieve my goal.

after_initialize do
   DiscourseEvent.on(:post_created) do |post, _, user|
      # Passing from category tags (#test:item1)
      tags= post.raw.split(' ').select{|t| t =~ /#/ && !t.include?(':')}
      tags.each do |t|
        tName= t.gsub('#', '')
        Tag.create_or_find_by(name: tName);
      end

      postAnalyzer= PostAnalyzer.new(post.raw, post.topic_id)
      cookedRaw= postAnalyzer.cook(post.raw)

      post.cooked= cookedRaw
      post.save

      post.publish_change_to_clients!(:revised, reload_topic: true)
   end
end

Appreciate your help!

1 Like