feat: log if the shout was sent via js

This commit is contained in:
alina 🌸 2024-08-31 22:56:10 +03:00
parent 749ac1f43a
commit b30d90319e
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
2 changed files with 10 additions and 7 deletions

View file

@ -148,6 +148,7 @@ export async function createShout(params: {
fromIp: string
private: boolean
text: string
isFormSubmit: boolean
}): Promise<boolean | string> {
let { text } = params
@ -155,15 +156,16 @@ export async function createShout(params: {
const validateResult = validateShout(text, !params.private)
const kindText = params.private ? 'private message' : 'shout'
const header = html`${params.private ? 'private message' : 'shout'} from <code>${params.fromIp}</code>`
const subheader = html`<br>via: ${params.isFormSubmit ? '#form' : '#api'}<br><br>`
if (params.private || validateResult !== true) {
const was = params.private ? '' : ` was auto-declined (${validateResult})`
await tg.sendText(
env.TG_CHAT_ID,
html`
${kindText} from <code>${params.fromIp}</code>${was}:
<br><br>
${header}${was}:
${subheader}
${text}
`,
)
@ -179,8 +181,8 @@ export async function createShout(params: {
await tg.sendText(
env.TG_CHAT_ID,
html`
${kindText} from <code>${params.fromIp}</code>:
<br><br>
${header}:
${subheader}
${text}
`,
{

View file

@ -11,7 +11,7 @@ import { HttpResponse } from '~/backend/utils/response'
const schema = z.object({
_csrf: z.string(),
message: z.string(),
private: z.literal('').optional(),
private: z.string().optional(),
})
const rateLimitPerIp = new RateLimiterMemory({ points: 3, duration: 300 })
@ -71,8 +71,9 @@ export const POST: APIRoute = async (ctx) => {
const result = await createShout({
fromIp: ip,
private: body.data.private === '',
private: body.data.private !== undefined,
text: body.data.message,
isFormSubmit,
})
await rateLimitPerIp.penalty(ip, 1)