Creating a User - Tag relation plugin

You can do this with a simple plugin.

What you need to do

The plugin will need to do the following:

  1. Add a user custom field called known_tags, a list of strings.

  2. Add an interface in the user profile where the user can edit known_tags. I don’t think it’s necessary to add an entirely new profile tab for this, but you could if you wanted to. If you don’t want users editing it themselves, just make it only editable by admins and just go into users profiles and update it based on your CSV file.

  3. Add an event hook which uses the before_create_post or post_created events in the PostCreator to add the content you want to the post based on the tags in the topic.

How to do it

Parts 1 and 2 are very similar to the example plugins for other models in the topic linked below. See if you can figure it out by analogy. If you get really stuck ask me for a pointer there.

Part 3 will also go in your plugin.rb file. It’ll look something like this

on(:post_created) |post, opts, user|
  if post.is_first_post? && post.topic.tags.present?
     user_ids = UserCustomField.where(name: 'known_tags', value: post.topic.tags).pluck(:user_id)
     usernames = User.where(id: user_ids).pluck(:username)
     new_raw = post.raw + "something something #{usernames}"
     PostRevisor.new(post).revise!(
      user,
      {
        raw: new_raw,
        edit_reason: "some reason"
      },
      skip_validations: true,
      bypass_bump: true
    )
  end 
end

Give it a royal go yourself. If you get really stuck I’ll help. I’m always more inclined to help if there’s strong evidence you’re trying to figure it out yourself :slight_smile:

2 Likes