Skip to content

feat: mention listener, handler for @admin#69

Merged
lorenzocorallo merged 6 commits intomainfrom
mention-listener
Mar 18, 2026
Merged

feat: mention listener, handler for @admin#69
lorenzocorallo merged 6 commits intomainfrom
mention-listener

Conversation

@lorenzocorallo
Copy link
Member

Closes #65

@lorenzocorallo lorenzocorallo marked this pull request as ready for review March 17, 2026 23:00
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 17, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0301380b-b8a4-49e0-a9ec-e5403b05c1eb

📥 Commits

Reviewing files that changed from the base of the PR and between 43c8186 and 1670376.

📒 Files selected for processing (2)
  • src/commands/report.ts
  • src/middlewares/mention-listener.ts

Walkthrough

Adds a new MentionListener middleware wired into the bot, extracts reporting logic into an exported logReport helper used by both the /report command and the middleware, and updates a TODO line (non-functional text change).

Changes

Cohort / File(s) Summary
Bot Middleware Integration
src/bot.ts
Imports MentionListener and registers it with bot.use(new MentionListener()).
Report Command & Helper
src/commands/report.ts
Extracts reporting logic into exported logReport(context, repliedTo) and updates the command handler to call this helper. Adds related type imports.
New Mention Middleware
src/middlewares/mention-listener.ts
Adds MentionListener<C extends Context> middleware: listens for message:entities:mention, filters mentions equal to @admin, deletes triggering message, validates replied-to message has .from, and calls logReport. Exposes middleware() method.
Docs/TODO
TODO.md
Minor text edit to /report TODO entry (non-functional).

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Bot
    participant MentionListener
    participant logReport
    participant tgLogger

    User->>Bot: Send message containing "@admin" in reply to a message
    Bot->>MentionListener: message:entities:mention event
    MentionListener->>MentionListener: Filter mentions for text == "@admin"
    MentionListener->>Bot: Delete triggering message
    MentionListener->>MentionListener: Verify replied-to message exists and has .from
    MentionListener->>logReport: Call logReport(context, repliedTo)
    logReport->>tgLogger: tgLogger.report(repliedTo, context.from)
    tgLogger-->>logReport: Success / Failure
    logReport->>Bot: Reply with success or failure message
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR addresses issue #65 by implementing @admin mention functionality via MentionListener, but does not implement the /admin command alias requirement. Add the /admin command alias to the command handlers to fully satisfy issue #65 requirements.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: introducing a mention listener that handles @admin mentions, which is the primary addition in this PR.
Out of Scope Changes check ✅ Passed All changes are in scope: MentionListener handles @admin mentions, report() function extracts reusable logic, and TODO.md is updated accordingly.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@lorenzocorallo lorenzocorallo requested a review from toto04 March 17, 2026 23:00
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/middlewares/mention-listener.ts (2)

10-18: Consider error handling implications with .fork().

Using .fork() runs the handler in a separate branch, meaning exceptions in handleReport (including from ctx.deleteMessage()) won't propagate through the main middleware chain to bot.catch(). This may be intentional, but verify that errors are still being captured appropriately.

If fail-fast behavior is desired per existing patterns, consider whether .fork() aligns with that intent.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/middlewares/mention-listener.ts` around lines 10 - 18, The current use of
.fork() on the composer branch prevents exceptions from handleReport (and calls
like ctx.deleteMessage) from bubbling to the main middleware error handler; to
fix this either remove .fork() so errors from the
composer.on("message:entities:mention") handler propagate normally, or keep
.fork() but wrap the call to this.handleReport(ctx) in an async try/catch that
forwards caught errors to the global error handler (e.g., rethrow or call the
bot.catch/global error emitter) so failures in handleReport/ctx.deleteMessage
are captured; locate the composer.on("message:entities:mention") chain and the
handleReport method to implement the change.

24-33: Silent failure when user doesn't reply to a message.

When a user mentions @admin without replying to a message, the function logs an error and returns silently. Consider providing user feedback so they know to reply to the message they want to report.

💡 Proposed enhancement to provide user feedback
   private async handleReport(ctx: MentionContext<C>) {
     await ctx.deleteMessage()
     const repliedTo = ctx.message.reply_to_message
     if (!repliedTo?.from) {
       logger.error("report: no repliedTo or repliedTo.from field (the msg was sent in a channel)")
+      await ctx.reply("⚠️ Please reply to the message you want to report.", {
+        disable_notification: true,
+      })
       return
     }

     await report(ctx, repliedTo)
   }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/middlewares/mention-listener.ts` around lines 24 - 33, handleReport
currently logs and returns when ctx.message.reply_to_message is missing, leaving
the user uninformed; instead, after ctx.deleteMessage() and before returning,
send a user-facing message (via ctx.reply or the project's standard reply/send
API) telling the user they must reply to the message they want to report, keep
the logger.error for server-side diagnostics, and only return afterward; update
the logic around ctx.message.reply_to_message in handleReport to perform this
user notification and then skip calling report(ctx, repliedTo).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/bot.ts`:
- Line 24: The import ordering in src/bot.ts is failing the pipeline because the
MentionListener import is out of alphabetical order; move the line importing
MentionListener so it is alphabetically ordered with the other local imports
(i.e., place the `import { MentionListener } from
"./middlewares/mention-listener"` line in the correct position among the other
local imports) to satisfy the linter.

In `@src/commands/report.ts`:
- Around line 9-23: The report function currently uses context.from without
checking for undefined before calling modules.get("tgLogger").report; add a null
check for context.from at the start of report (the function named report) and
handle the undefined case by not calling modules.get("tgLogger").report and
instead sending an explanatory reply to the user (similar style to the existing
reply messages) or otherwise passing an explicit fallback/identity if your
tgLogger.report accepts it; ensure you reference context.from and repliedTo when
forming the response so anonymous/channel-post cases are handled safely and no
undefined is passed into tgLogger.report.

---

Nitpick comments:
In `@src/middlewares/mention-listener.ts`:
- Around line 10-18: The current use of .fork() on the composer branch prevents
exceptions from handleReport (and calls like ctx.deleteMessage) from bubbling to
the main middleware error handler; to fix this either remove .fork() so errors
from the composer.on("message:entities:mention") handler propagate normally, or
keep .fork() but wrap the call to this.handleReport(ctx) in an async try/catch
that forwards caught errors to the global error handler (e.g., rethrow or call
the bot.catch/global error emitter) so failures in
handleReport/ctx.deleteMessage are captured; locate the
composer.on("message:entities:mention") chain and the handleReport method to
implement the change.
- Around line 24-33: handleReport currently logs and returns when
ctx.message.reply_to_message is missing, leaving the user uninformed; instead,
after ctx.deleteMessage() and before returning, send a user-facing message (via
ctx.reply or the project's standard reply/send API) telling the user they must
reply to the message they want to report, keep the logger.error for server-side
diagnostics, and only return afterward; update the logic around
ctx.message.reply_to_message in handleReport to perform this user notification
and then skip calling report(ctx, repliedTo).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: cac8639a-e88e-4ca5-a42d-3a39e8167a96

📥 Commits

Reviewing files that changed from the base of the PR and between 56e886b and 5a59d14.

📒 Files selected for processing (3)
  • src/bot.ts
  • src/commands/report.ts
  • src/middlewares/mention-listener.ts

@lorenzocorallo lorenzocorallo enabled auto-merge (squash) March 18, 2026 16:25
@lorenzocorallo lorenzocorallo merged commit bf2bea0 into main Mar 18, 2026
1 of 2 checks passed
@lorenzocorallo lorenzocorallo deleted the mention-listener branch March 18, 2026 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add alias for /admin to /report

2 participants