Right now, in order to create a tag with the API, I create a temporary topic with the new tag, then delete the temporary topic. Is there a cleaner way?
I don’t know, but a slightly less painful solution would be to create a post in the staff category (where Assets for site design is) and just add the new tags there.
The reason is that you want to create a bunch of tags so that users can use them in the future, but your community doesn’t yet have posts with those tags?
Thanks @pfaffman.
You mean manually adding the new tag? Or is it possible to use the “Update a single topic” API to add a tag to an existing topic? But in that case I would have to store the topic id, which is painful…
I guess this would be a legit use case. But mine is less legit: I use tags to store metadata (object ids from an external app).
Yeah, that’s not a good idea, especially if each tag is only used once.
You should either have a separate database mapping the external IDs to Discourse topic IDs, or write a plugin and use topic custom fields (this is a problem since there’s no existing mechanism to deliver these to the client, I think).
Another solution is to store the value in the post text.
Since there is a legit use case, I believe adding a “new tag” API in the future would be a plus.
I can feel the danger of using tags the way I do. But as I need to display, in Discourse, a list of the topics that relate to a specific external object, the “tags.show” route seems the only way…
Well, what are the external objects? How many topics will one get? will a topic relate to multiple of these things?
You may be able to get away with using search.
The external objects are document subsections. One topic relates to one and only one subsection. A minimum guess is: 100 documents x 100 subsections per document = 10,000 subsections = 10,000 tags in a single Discourse instance. I know this is dangerous, but since I’m in the prototype phase, tags are good enough for me.
Now I didn’t think about the search page before… I’ve just done some tests, but sadly, the results are not formatted like a topic list (with the ‘Replies’, ‘Views’ and ‘Activity’ columns). I guess a “topics only” checkbox that turns the result page into a standard Discourse topic list would be exactly what I need. 
Consider not pre-creating that many topics - there’s often not much to discuss about the Bibliography. You can have a small service that takes a document & subsection ID and redirects you to either the existing topic or opens the composer pre-filled with the document link, subsection ID, etc.
title: Procedures for Smelling, section Keyboards
body: <https://link.to/document/1234#sec-26>
[write your text here]
lookup key: DISCUSS_1234_26
Then it polls /latest.json for new topics and checks them for the magic ID reference phrase I put in on the last line of the example.
Yes, I already have something like this. I don’t create the subsection tags beforehand when a document is created. I’ve changed the “tag not found” page of a subsection so that it looks like an empty topic list with a “New Topic” button at the top. When user clicks the button, I create the tag on the fly using the API and open the composer pre-filled with the generated tag.
Yes there is: create a tag group populated with the tag you want to create (see the API here).
上述通过创建标签组来使用 API 创建标签的方法有一个缺点:仅管理员可以使用。
还有另一种通过 API 创建标签的方法(但仍仅限管理员使用):未公开的 /tags/upload.json 接口,由“上传标签”菜单使用。用法如下:
const tags = ['tag1', 'tag2']
const file = new Blob([tags.join('\n')], { type: 'text/plain' })
const formData = new FormData()
formData.append('file', file)
$.ajax({
type: 'POST',
url: '/tags/upload.json',
data: formData,
contentType: false,
processData: false
})
如果我计划使用成千上万个标签,为每个标签创建一个组是否仍然是一个好的变通方案?标签组是否和标签一样灵活?
如果是这样,那就能解决我当前的难题。如果不是,我会尽量避免重复(即仅为一个标签同时创建标签和标签组)。
抱歉,我不清楚。
我好像记得,API 允许在一次操作中为单个组创建多个标签。
谢谢。是的,它允许我们在创建时于同一组内创建多个标签,但仅限于创建阶段。我们可以进行更新,但为此我需要先获取该组,追加内容,然后再将其发送回去。目前没有用于追加的端点。
关于你提出的第二个想法,即使用 /tags/upload.json 端点的那个方案:
我找到了界面中的上传按钮。我创建了一个 CSV 文件,在那里可以正常工作。但我无法在 Postman 或 Golang 中使其生效(如果你熟悉的话,我使用的是 csv.NewWriter)。
有什么建议吗?
抱歉,我不知道如何用这些工具创建并发布 blob。
已经知道怎么做了。
如果有人需要,请私信我。我不在这里发布是因为内容很长,而且可能对 99.99% 的社区成员来说并不相关。