Hello, I am currently working on a specific plugin but couldn’t figure where to start yet (I read the introductory material here and installed some simple plugins and played with their source a bit).
The setup: I have a csv file that contains several usernames from the system and some tags they are related to. For example, an entry “user1, tag#1” reads: “user1 knows about tag#1”. Many users can be related to one tag and one user can be related to many tags. Moreover, this csv will be updated time to time (once almost everyday). The file is in the same server that hosts the discourse instance.
How would the plugin ideally work: I will explain this using an example csv and a picture.
Given this CSV, whenever there is a topic created with any of the tags included in the CSV (The topic author might not be on the list). The ideal output should be as follows:
One thing you’ll need to do on the rails side is add those user/tag relations to the serializer so that those data are available to the front end. Look for some other plugins that call add_to_serializer or search for it in the discourse source, as it’s well documented there.
Then you’ll be able to use a plugin outlet in ember to add that to the page. The theme component documentation has examples of that.
I would probably add a route to manage the custom user fields rather than use a csv file, but if you want to go the csv way, you might want a job that reads it every hour or something, so find a plugin that has something in jobs/scheduled.
There is a Discourse repo called all_the_plugins that I sometimes grep to find examples of stuff. If you wade through github.com/discourse you should be able to find it.
Thank you for the information. The mental model that I have right now is, I should add a custom field to tags (like tag_description) called attached_users. For the above example, tag#1 should have attached_users: [“userOP”, “user1”].
Then, I should somehow add this to the serializer and figure how to display it in the post.
While thinking about this, I had another idea. If I use discourse assign (Discourse Assign) to assign these users to topics based on tags. I can already retrieve this information from the topic and I guess displaying it would be easier. However, discourse assign doesn’t provide any way of programmatically adding users.
I wonder if it would put too much weight on the server. Because there will be many assignments and in the previous idea, it is essentially just an assignment to tags and doesn’t have to be assigned to each topic seperately.
Add a user custom field called known_tags, a list of strings.
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.
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
Thanks a lot for providing a very nice roadmap, I am on it! Small question:
Does this part have to be manual using this approach (by manual, I mean adding entries one by one via an interface)? Ideally, I would like to add this using my admin API key since the csv file is a bit loaded and will grow over time.
No it doesn’t need to be manual. There are quite a few different ways you could fill out the user custom fields based on the CSV. It really depends on how you want to handle the updates. I’d suggest doing the other parts first and just testing this out with a small dataset. Then if you’re happy with how it’s working add an automatic import for the CSV. Making that part efficient is a secondary step.