Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@grammyjs/parse-mode": "^1.11.1",
"@grammyjs/runner": "^2.0.3",
"@influxdata/influxdb-client": "^1.35.0",
"@polinetwork/backend": "^0.15.8",
"@polinetwork/backend": "^0.15.9",
"@t3-oss/env-core": "^0.13.4",
"@trpc/client": "^11.5.1",
"@types/ssdeep.js": "^0.0.2",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 35 additions & 7 deletions src/modules/moderation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Chat, ChatMember, Message, User } from "grammy/types"
import { err, ok, type Result } from "neverthrow"
import { type ApiInput, api } from "@/backend"
import { logger } from "@/logger"
import { MessageUserStorage } from "@/middlewares/message-user-storage"
import { groupMessagesByChat, RestrictPermissions } from "@/utils/chat"
import { type Duration, duration } from "@/utils/duration"
import { fmt, fmtUser } from "@/utils/format"
Expand Down Expand Up @@ -142,6 +143,30 @@ class ModerationClass<C extends Context> implements MiddlewareObj<C> {
})
}

/**
* Mass deletes the last 100 messages of a user in a specific chat, on best effort basis.
*
* Used when banning a user to delete all their messages in the chat
*/
private async deleteLastMessages(userId: number, chatId: number): Promise<void> {
await MessageUserStorage.getInstance()
.sync()
.catch(() => {})

// both the limit of tRPC endpoint and Telegram API hard limit: https://core.telegram.org/bots/api#deletemessages
const messages = await api.tg.messages.getLastByUser
.query({ userId, chatId, limit: 100 })
.then((res) => res.messages ?? [])
.catch(() => [])

await modules.shared.api
.deleteMessages(
chatId,
messages.map((m) => m.messageId)
)
.catch(() => {})
}

private async perform(p: ModerationAction) {
switch (p.action) {
case "SILENT":
Expand All @@ -153,13 +178,16 @@ class ModerationClass<C extends Context> implements MiddlewareObj<C> {
revoke_messages: true,
})
.catch(() => false)
case "BAN":
return modules.shared.api
.banChatMember(p.chat.id, p.target.id, {
until_date: p.duration?.timestamp_s,
revoke_messages: true,
})
.catch(() => false)
case "BAN": {
const [success] = await Promise.all([
modules.shared.api
.banChatMember(p.chat.id, p.target.id, { until_date: p.duration?.timestamp_s })
.catch(() => false),
this.deleteLastMessages(p.target.id, p.chat.id),
])
return success
}
Comment thread
toto04 marked this conversation as resolved.

case "UNBAN":
return modules.shared.api.unbanChatMember(p.chat.id, p.target.id, { only_if_banned: true }).catch(() => false)
case "MUTE":
Expand Down
Loading