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
25 changes: 16 additions & 9 deletions src/commands/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ import { CommandsCollection } from "@/lib/managed-commands"
import { logger } from "@/logger"
import { modules } from "@/modules"
import { fmt } from "@/utils/format"
import { ephemeral } from "@/utils/messages"
import type { Role } from "@/utils/types"

export const logReport = async (context: Filter<Context, "message"> | CommandScopedContext, repliedTo: Message) => {
const reportSent = await modules.get("tgLogger").report(repliedTo, context.from)
await context.reply(
reportSent
? fmt(({ b, n }) => [b`✅ Message reported!`, n`Moderators have been notified.`], { sep: "\n" })
: fmt(({ b, n }) => [b`⚠️ Report not sent`, n`Please try again in a moment.`], { sep: "\n" }),
{
disable_notification: false,
reply_parameters: { message_id: repliedTo.message_id },
}
)

let msg: string = ""
if (reportSent === "SENT")
msg = fmt(({ b, n }) => [b`✅ Message reported!`, n`Moderators have been notified.`], { sep: "\n" })
else if (reportSent === "ALREADY_SENT")
msg = fmt(({ b, n }) => [b`☑️ Message already reported!`, n`Moderators have been notified.`], { sep: "\n" })
else if (reportSent === "ERROR")
msg = fmt(({ b, n }) => [b`⚠️ Report not sent`, n`Please try again in a moment.`], { sep: "\n" })

const feedback = await context.reply(msg, {
disable_notification: false,
reply_parameters: { message_id: repliedTo.message_id },
})

if (reportSent !== "SENT") void ephemeral(feedback)
}

export const report = new CommandsCollection<Role>().createCommand({
Expand Down
24 changes: 20 additions & 4 deletions src/modules/tg-logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { GrammyError, InlineKeyboard } from "grammy"
import type { Message, User } from "grammy/types"
import { Module } from "@/lib/modules"
import { RedisFallbackAdapter } from "@/lib/redis-fallback-adapter"
import { logger } from "@/logger"
import { redis } from "@/redis"
import { groupMessagesByChat, stripChatId } from "@/utils/chat"
import { fmt, fmtChat, fmtDate, fmtUser } from "@/utils/format"
import type { ModuleShared } from "@/utils/types"
Expand All @@ -13,6 +15,8 @@ import { grantCreatedMenu, grantMessageMenu } from "./grants"
import { getReportText, type Report, reportMenu } from "./report"
import type * as Types from "./types"

type REPORT_RESULT = "SENT" | "ALREADY_SENT" | "ERROR"

type Topics = {
actionRequired: number
banAll: number
Expand All @@ -36,6 +40,13 @@ const MOD_ACTION_TITLE = (props: ModerationAction) =>
})[props.action]

export class TgLogger extends Module<ModuleShared> {
private reportStorage = new RedisFallbackAdapter<boolean>({
redis,
logger,
prefix: "report",
ttl: 900,
})

constructor(
public readonly groupId: number,
private topics: Topics
Expand Down Expand Up @@ -97,10 +108,15 @@ export class TgLogger extends Module<ModuleShared> {
return []
}

public async report(message: Message, reporter: User): Promise<boolean> {
if (message.from === undefined) return false // should be impossible
public async report(message: Message, reporter: User): Promise<REPORT_RESULT> {
if (message.from === undefined) return "ERROR" // should be impossible
const { invite_link } = await this.shared.api.getChat(message.chat.id)

const reportKey = `${message.chat.id}_${message.message_id}`

if (await this.reportStorage.has(reportKey).catch(() => false)) return "ALREADY_SENT"
await this.reportStorage.write(reportKey, true).catch(() => {})

const report: Report = { message, reporter } as Report
const reportText = getReportText(report, invite_link)
const reply_markup = await reportMenu(report)
Expand All @@ -109,10 +125,10 @@ export class TgLogger extends Module<ModuleShared> {
disable_notification: false,
})

if (!reportMsg) return false
if (!reportMsg) return "ERROR"

await this.forward(this.topics.actionRequired, message.chat.id, [message.message_id])
return true
return "SENT"
}

// NOTE: this does not delete the messages
Expand Down
Loading