-
Notifications
You must be signed in to change notification settings - Fork 0
feat: group-specific actions middleware #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f8fd868
chore: add useful info to README.md
lorenzocorallo 290afbf
feat: create group-specific actions middleware
lorenzocorallo 76e3749
fix: add group-specific actions middleware in stack
lorenzocorallo c35477b
chore: update TODO.md
lorenzocorallo d890b9b
fix: add permissions check
lorenzocorallo 765a7e8
perf: create group ids Set once
lorenzocorallo ad6710c
Merge branch 'main' into group-specific
lorenzocorallo 7f88bf1
chore: reduced code redundancy
toto04 36e8d53
Merge branch 'group-specific' of https://github.com/polinetworkorg/te…
toto04 106377b
chore: change method visibility
toto04 9b507c5
fix: await delete call
toto04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { Composer, type Filter, type MiddlewareObj } from "grammy" | ||
| import { err, ok, type Result } from "neverthrow" | ||
| import { api } from "@/backend" | ||
| import { logger } from "@/logger" | ||
| import { modules } from "@/modules" | ||
| import { fmt, fmtUser } from "@/utils/format" | ||
| import type { Context } from "@/utils/types" | ||
| import { wait } from "@/utils/wait" | ||
|
|
||
| const TARGET_GROUPS: Record<string, number> = { | ||
| alloggi: -1001175999519, | ||
| ripetizioni: -1001495422899, | ||
| books: -1001164044303, | ||
| } as const | ||
|
toto04 marked this conversation as resolved.
|
||
|
|
||
|
lorenzocorallo marked this conversation as resolved.
|
||
| const TARGET_GROUP_IDS_SET = new Set(Object.values(TARGET_GROUPS)) | ||
|
|
||
| function groupSpecificHashtags(groupId: number): string[] { | ||
| switch (groupId) { | ||
| case TARGET_GROUPS.alloggi: | ||
| return ["#cerco", "#searching", "#search", "#offro", "#offering", "#offer"] | ||
| case TARGET_GROUPS.ripetizioni: | ||
| return ["#richiesta", "#offerta", "#request", "#offer"] | ||
| case TARGET_GROUPS.books: | ||
| return ["#cerco", "#vendo"] | ||
| default: | ||
| return [] | ||
| } | ||
| } | ||
|
|
||
| export class GroupSpecificActions<C extends Context> implements MiddlewareObj<C> { | ||
| private composer = new Composer<C>() | ||
|
|
||
| constructor() { | ||
| this.composer | ||
| .filter((ctx) => !!ctx.chatId && TARGET_GROUP_IDS_SET.has(ctx.chatId)) | ||
| .on("message", async (ctx, next) => { | ||
| if (ctx.from.id === ctx.me.id) return next() // skip if bot | ||
| const { roles } = await api.tg.permissions.getRoles.query({ userId: ctx.from.id }) | ||
| if (roles && roles.length > 0) return next() // skip if admin or other roles | ||
|
|
||
| const chatMember = await ctx.getChatMember(ctx.from.id) | ||
| if (chatMember.status === "administrator" || chatMember.status === "creator") return next() // skip if group-admin | ||
|
toto04 marked this conversation as resolved.
|
||
|
|
||
| let check: Result<void, string> | ||
| switch (ctx.chatId) { | ||
| case TARGET_GROUPS.alloggi: | ||
| check = this.checkAlloggi(ctx) | ||
| break | ||
|
|
||
| case TARGET_GROUPS.ripetizioni: | ||
| check = this.checkRipetizioni(ctx) | ||
| break | ||
|
|
||
| case TARGET_GROUPS.books: | ||
| check = this.checkBooks(ctx) | ||
| break | ||
|
|
||
| default: | ||
| logger.error( | ||
| { chatId: ctx.chatId, targetGroupsMap: TARGET_GROUPS }, | ||
| "GroupSpecificActions: target group matched, but no handler set. This is an unimplemented feature" | ||
| ) | ||
| return next() | ||
| } | ||
|
|
||
| if (check.isOk()) return next() | ||
|
|
||
| await modules.get("tgLogger").delete([ctx.message], `User did not follow group rules:\n${check.error}`, ctx.me) | ||
| const reply = await ctx.reply( | ||
| fmt(({ b, n }) => [b`${fmtUser(ctx.from)} you sent an invalid message`, b`Reason:`, n`${check.error}`], { | ||
| sep: "\n", | ||
| }), | ||
| { disable_notification: false, reply_markup: { force_reply: true } } | ||
| ) | ||
|
|
||
| // delete error msg after 2 min without blocking the middleware stack | ||
| void wait(120_000) | ||
| .then(() => reply.delete()) | ||
| .catch(() => {}) | ||
| }) | ||
| } | ||
|
|
||
| private checkAlloggi(ctx: Filter<C, "message">): Result<void, string> { | ||
| return this.checkHashtags(ctx, groupSpecificHashtags(ctx.chatId)) | ||
| } | ||
|
|
||
| private checkRipetizioni(ctx: Filter<C, "message">): Result<void, string> { | ||
| return this.checkHashtags(ctx, groupSpecificHashtags(ctx.chatId)) | ||
| } | ||
|
|
||
| private checkBooks(ctx: Filter<C, "message">): Result<void, string> { | ||
| return this.checkHashtags(ctx, groupSpecificHashtags(ctx.chatId)) | ||
| } | ||
|
lorenzocorallo marked this conversation as resolved.
|
||
|
|
||
| private checkHashtags(ctx: Filter<C, "message">, requiredHashtags: string[]): Result<void, string> { | ||
| const hashtags = ctx.entities("hashtag").map((e) => e.text.toLowerCase()) | ||
| const hasRequired = requiredHashtags.some((tag) => hashtags.includes(tag.toLowerCase())) | ||
|
lorenzocorallo marked this conversation as resolved.
|
||
| if (!hasRequired) { | ||
| return err(`You must include one of the following hashtags in your message: ${requiredHashtags.join(", ")}`) | ||
| } | ||
| return ok() | ||
| } | ||
|
|
||
| middleware() { | ||
| return this.composer.middleware() | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.