|
1 | 1 | import { config, assertRuntimeConfig } from './config.js'; |
2 | | -import { htmlToBasic } from './basic.js'; |
| 2 | +import { htmlToBasic, leadingMentions } from './basic.js'; |
3 | 3 | import { extractProjectRef } from './project.js'; |
4 | 4 | import { parseDirectives } from './directives.js'; |
5 | 5 | import { allowRequest } from './ratelimit.js'; |
@@ -28,11 +28,31 @@ function truncate(text: string, max: number): string { |
28 | 28 | return text.length <= max ? text : `${text.slice(0, max - 1)}…`; |
29 | 29 | } |
30 | 30 |
|
| 31 | +// True when the bot's own handle is the first @mention in the toot, i.e. the |
| 32 | +// post is addressed to the bot rather than merely referencing it in passing. |
| 33 | +// The rendered mention text is the bare "@username"; we also accept a |
| 34 | +// "username@domain" form defensively in case an instance renders the full acct. |
| 35 | +function addressesBot(content: string, self: MastodonAccount): boolean { |
| 36 | + const first = leadingMentions(content)[0]; |
| 37 | + if (!first) return false; |
| 38 | + const username = self.username.toLowerCase(); |
| 39 | + return first === username || first === self.acct.toLowerCase() || first.startsWith(`${username}@`); |
| 40 | +} |
| 41 | + |
31 | 42 | async function handleMention(self: MastodonAccount, n: MastodonNotification): Promise<void> { |
32 | 43 | const status = n.status; |
33 | 44 | if (!status) return; |
34 | 45 | if (status.account.id === self.id) return; // never answer ourselves |
35 | 46 |
|
| 47 | + // Only act when the toot is addressed to the bot, i.e. our handle is the |
| 48 | + // first thing in it. A mid-sentence mention ("love what @bot does") is |
| 49 | + // conversation, not a submission, so ignore it rather than parse the |
| 50 | + // chatter as a program and reply with a compile error. |
| 51 | + if (!addressesBot(status.content, self)) { |
| 52 | + console.log(`Mention ${n.id} from @${status.account.acct}: not addressed to the bot, skipping`); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
36 | 56 | // A project link takes precedence over inline source: the program already |
37 | 57 | // lives on the site, so render it directly rather than parsing the toot. |
38 | 58 | // Directives (#128/#48 machine, #asm language) are read from the toot text |
|
0 commit comments