Trying to understand mention handling

I am trying to write a bot and it is possible to tag multiple users in a note to a bot. The issue I am running into is, as I understand it:

  1. mention tags are optional
  2. mention tags don’t have to have strict order matching the in-band mentioning (observed)
  3. recipient to, cc don’t have to be in strict order matching the in-band mentioning (observed)
  4. in-band mentioning structure is arbitrary and servers all do it differently

Is there a good way to deal with situations where two mentioned people have the same username, eg:

@bot do-something-to @mruser@example1.tld @mruser@example2.tld

depending on the server, the plaintext rendering is:

@bot do-something-to @mruser @mruser

The information is in the raw HTML (if I am sent HTML.) Am I stuck just writing separate html parsers for every server formatting I know about?

What are you trying to do with the mentions? Mention tags are represented in ActivityPub as a separate tags object, like this:

        "content": "<p><span class=\"h-card\" translate=\"no\"><a href=\"https://mastodon.social/@photomatt\" class=\"u-url mention\">@<span>photomatt</span></a></span> 👋</p>",
        "attachment": [
          
        ],
        "tag": [
          {
            "type": "Mention",
            "href": "https://mastodon.social/users/photomatt",
            "name": "@photomatt"
          }
        ],

For example, if you’re trying to figure out if a blog is mentioned, you look up the id of the blog in the tag array to see if a Mention tag exists for that blog.

It seems like you know this already, so I’m not sure I understand what you’re looking to accomplish.

The Note I receive is a command to my bot, where the order of mentions is significant, and I was attempting to use the structured activitypub object as a shortcut to get at the mentions. However the arrays don’t necessarily retain the order of the mentions as present in the content, in my testing. I am realizing now that this isn’t a solvable problem because the note content isn’t obligated to contain a textual representation of mentions either and the field is by definition unstructured. The answer is parse the Note content to the best of your ability and accept there will be failures sometimes.

This isn’t directly an activitypub question, I should have put this post under the programming subforum. apologies.

If the order of mentions is significant to you, then you can use the url or id of the ActivityPub actor to associate them with any appropriate textual content as follows:

{
  "type": "Mention",
  "href": "https://mastodon.social/users/photomatt",
  "name": "@photomatt"
}
<p>
  <span class="h-card" translate="no">
    <a href="https://mastodon.social/@photomatt" class="u-url mention">
      @<span>photomatt</span>
    </a>
  </span> 👋
</p>

If you resolve the Actor for the user https://mastodon.social/users/photomatt, you’ll see that it has the url property https://mastodon.social/@photomatt. That URL property will be in the HTML document. Some implementations use the id property instead, mostly ones that don’t have a url property.

So the logic would be something like:

  1. Get list of users with a Mention tag
  2. Create a map from url → user and id → user.
    (e.g. const urlMap = Object.fromEntries(mentionedUsers.flatMap(user => [[user.id, user], [user.url, user]]);)
  3. Process the HTML in whatever way you wish
  4. When you come across an a tag, you can look up the href of the a tag to figure out which user is being referenced.

Does that make sense?