Topic ID als variabele?

Is er een manier om de topic-ID als variabele binnen een topic te krijgen?

Gebruiksscenario: we hebben categorieën voor support- en bugtrackingtickets. Ik wil ze instellen met sjablonen zoals deze.
Dit:

> Verwijder deze regel **niet**: Supportaanvraag #$(topicid)

Ziet er als volgt uit:

Verwijder deze regel niet: Supportaanvraag #246

Sta open voor andere manieren om de informatie te verkrijgen. Weet alleen niet hoe ik dit moet doen.

1 like

Je probeert de topic-ID in de ruwe tekst van het topic te plaatsen?

Welk probleem gaat dat oplossen?

1 like

Het zal het probleem oplossen dat ik mijn ondersteunend personeel vertel dat ze eerst een nieuw onderwerp moeten aanmaken, het vervolgens moeten opslaan, dan naar de URL moeten kijken en het ID # moeten ophalen, dan terug moeten gaan en het onderwerp moeten bewerken om de # handmatig in de body te plaatsen.

Waarvoor gebruikt u de topic ID# in de body van de OP in uw use case hier?

1 like

Een unieke id # voor een ticket/bug.

Waar gaat dat ticket/bug naartoe? De ID staat in de URL; daar is het persistent en nooit fout. Hoe helpt het om de topic ID in de tekst van het topic te hebben? En, zoals u aangeeft, als het in de hoofdtekst van het bericht staat, kan iedereen het veranderen, verwijderen of verkeerd typen.

Gebruikt iets anders de topic ID? Zo ja, wat en hoe?

1 like

It will be easier to create a custom component that appends this to the correct posts rather than filling a template.

If you edit the code of your theme under admin > themes & components… and add this to the JS tab, it should get you what you’re after with some adjustment:

import { apiInitializer } from "discourse/lib/api";
import Component from "@glimmer/component";

class TopicIdentifier extends Component {
  get topicId() {
    return this.args.post?.topic?.id;
  }

  get shouldShow() {
    const firstPost = this.args.post?.post_number === 1; // first post in topic
    const desiredCategories = [45, 3]; // a comma separated list of category IDs you want this to appear in 
    const isInCategory = desiredCategories.includes(
      this.args.post?.topic.category.id
    );

    return firstPost && isInCategory;
  }

  <template>
    {{#if this.shouldShow}}
      <!-- you can edit the content below, {{this.topicId}} is where the topic ID will be filled -->
      This topic's ID is
      {{this.topicId}} 
      <!-- you can edit the content above -->
    {{/if}}
  </template>
}

export default apiInitializer((api) => {
  api.decorateCookedElement((element, helper) => {
    const wrapper = document.createElement("div");
    wrapper.className = "custom-topic-id"
 
    helper?.renderGlimmer(
      wrapper,
      <template><TopicIdentifier @post={{helper.model}} /></template>
    );

    element.appendChild(wrapper);
  });
});

You can find the category ID from its URL to configure desiredCategories, for example, the support category here on Meta has the ID of 6

You can add CSS to style this how you’d like with the CSS tab… for example

.custom-topic-id {
  display: inline-block;
  background: yellow;
}

3 likes

Ja, het kan verkeerd getypt worden. Daarom hoef je je geen zorgen te maken over het verkeerd typen als je een variabele gebruikt.

Het helpt omdat ik de gebruiker ben en zo wil ik dat mijn workflow is ingesteld.

Je kunt momenteel Discourse niet doorzoeken op topic-ID. Als het # in de body van het topic staat, dan maakt het nu deel uit van de zoekresultaten.

Dus ik kan een collega vertellen: “Hé, zoek naar bug #138 en laat me weten wat je ervan vindt.”

Dan is het voor Nancy heel gemakkelijk om naar de site te gaan en te zoeken naar 138 en een resultaat te krijgen.

2 likes

You don’t need to search:

/t/-/id – but really /t/id works.

They can just type /t/138 and not have to search. But if you are talking to a person, wouldn’t saying "search for bug about “some words they know” make more sense to use in conversation than a number?

And that was going to be my other suggestion.

I think I’d have used renderInOutlet rather than decorateCookedElement, but that’s probably because I don’t fully understand decorateCookedElement.

But this will don’t let you search for a bug/topic by topic_id.

1 like

Ik zal dit uitproberen. Bedankt voor de informatie. Is dat deel van het lichaam dat doorzoekbaar wordt?

But I want to search. It’s easier.
Go to site, then append /t/id to the URL at the top of the window VS go to site and type ID into the search field that’s right in the center of the page.

If I’m talking to a person, or typing a message, yes a number is more concise and less error prone than “Search for exact title that might be 5 words long”

I will look into that, but if it kills being able to use in a search field, then it won’t work for me.

I really love Discourse, and I have gotten a TON of useful feedback and help from everyone here.
What I don’t understand is that occasionally folks tell me that “You shouldn’t do it that way because this way is better.” Well, no, this way is better for you, but that way is better for me. And neither is wrong.

3 likes

Yeah don’t bother then, unfortunately it only appends content to the topic when it’s rendered… it doesn’t add it to the database to make it searchable. I think what you’d ultimately need is a custom plugin or a custom script added to Discourse Automation for it to be fully automatic.

Searching by topic ID or including it in a template isn’t a common request, so it’s not something we support out of the box.

I think it’s that regulars here know it’s not a feature we currently have, so we’re trying to support you by figuring out some potential work-arounds.

2 likes

And often if you go back you can find a better solution.

If you really want to embed them topic id in the text so you can find it in search then you’d need a plugin and it could add ticket123 to the raw text (I think search looks at raw?). The topic id doesn’t exist before you create the topc, so you can’t insert it when you create it.

Oh! Maybe what you want is a plugin that would add a tag bug123 when a topic in a bug category gets created. Then you could refer to it as #bug123 and have it link anywhere you mention it and be able to search. If you’re self hosted or on enterprise that wouldn’t be too hard. I imagine someone in Marketplace would do it for $250 to $500.

Or, if you want to be able to “search” for a bug by number, you could add a theme component that let you type it in a ‘bug box’ and go straight there. Maybe that’s a solution that would work and it wouldn’t require a plugin.

1 like

Het resultaat is een meer discrete weergave van de topic-ID. Het is nog steeds niet doorzoekbaar, maar ik kan ermee leven totdat wat ik echt nodig heb werkelijkheid wordt.

<script type="text/discourse-plugin" version="0.5">
api.decorateWidget('post-meta-data:after', dec => {
  if (dec.attrs.post_number === 1) {
    const topic = dec.getModel().get('topic');
    return dec.rawHtml(`<div class="post-info topic-id"><b>&nbsp#${topic.get('id')}</b></div>`);
  }
});
</script>

Het probleem is dat deze code nu verouderd is en ik weet niet hoe ik deze moet repareren.
Waarmee moet deze worden bijgewerkt om compatibel te zijn?

I created GitHub - literatecomputing/discourse-custom-components as an example of how to do that.

Is a trivial example of inserting a component. So you would add some code before <template> to get the topic id and logic for whether to display.

I think you might want topic-list-before-link as the plugin outlet.

I don’t understand any of that. I’m not a coder.
The code I posted does this:


How would I do that with your code?

If I’m not wrong, inputting topic:id into the searchbar will show the topic in the results.

E.g. searching topic:374168 this brings up posts from this topic only.

I wonder… would a TC that sends an ajax request to update the topic’s raw suffice?

1 like

That only works for this topic, try a different random one. Try searching for “one two four two four zero” that’s the tag banners topic ID.

Bedoel je topic:124240? Het stopwoord (‘hallo’) was slechts een voorbeeld. In het geval van Tag Banners werkt ‘hoi’. Misschien als je een veelvoorkomend woord hebt in supporttickets, of gewoon een veelvoorkomend woord in het algemeen, kun je dat proberen?

Did you try the code in Topic ID as a variable? - #7 by awesomerobot

Also, if what you want to do is “search” for a topic id, then you could probably make a component that accepted a topic ID and then just jumped to that topic.

1 like