fix(notifications): harden senders, bound status worker, surface save errors3830548a month ago207 lines · 8.9 KB | 1 | import { eq } from "drizzle-orm"; |
| 2 | import { z } |
| 3 | import { ORPCError } |
| 4 | import { db } |
| 5 | import { settings } |
| 6 | import { user } |
| 7 | import { encrypt } |
| 8 | import { recordActivity } |
| 9 | import { adminProcedure, protectedProcedure |
| 10 | import { |
| 11 | SENTINEL, |
| 12 | buildDiscordComponents, |
| 13 | buildMessage, |
| 14 | getAdminNotificationConfig, |
| 15 | getUserNotificationConfig, |
| 16 | isValidDiscordWebhookUrl, |
| 17 | isValidTelegramBotToken, |
| 18 | isValidTelegramChatId, |
| 19 | postDiscord, |
| 20 | postTelegram, |
| 21 | } from "../services/notifications"; |
| 22 | |
| 23 | async function upsertSetting( |
| 24 | await db |
| 25 | .insert(settings) |
| 26 | .values({ key |
| 27 | .onDuplicateKeyUpdate({ set: |
| 28 | } |
| 29 | |
| 30 | function resolveSecret( |
| 31 | input: string |
| 32 | validate: (v |
| 33 | ): "keep" | " |
| 34 | if (input === |
| 35 | if (input === |
| 36 | validate(input); |
| 37 | return input; |
| 38 | } |
| 39 | |
| 40 | export const notificationsRouter = |
| 41 | getAdminConfig: adminProcedure.handler |
| 42 | const rows = |
| 43 | const s = |
| 44 | return { |
| 45 | discordEnabled: s.notifications_discord_enabled |
| 46 | discordWebhookSet: !!s.notifications_discord_webhook_url |
| 47 | telegramEnabled: s.notifications_telegram_enabled |
| 48 | telegramTokenSet: !!s.notifications_telegram_bot_token |
| 49 | telegramChatId: s.notifications_telegram_chat_id |
| 50 | userConfigEnabled: s.notifications_user_config_enabled |
| 51 | }; |
| 52 | }), |
| 53 | |
| 54 | saveAdminConfig: adminProcedure |
| 55 | .input( |
| 56 | z.object({ |
| 57 | discordEnabled: z.boolean |
| 58 | discordWebhookUrl: z.string |
| 59 | telegramEnabled: z.boolean |
| 60 | telegramBotToken: z.string |
| 61 | telegramChatId: z.string |
| 62 | userConfigEnabled: z.boolean |
| 63 | }), |
| 64 | ) |
| 65 | .handler(async ( |
| 66 | const pairs: |
| 67 | if (input.discordEnabled |
| 68 | if (input.telegramEnabled |
| 69 | if (input.userConfigEnabled |
| 70 | |
| 71 | const discordWebhook = |
| 72 | if (!isValidDiscordWebhookUrl(v |
| 73 | }); |
| 74 | if (discordWebhook === |
| 75 | await db.delete |
| 76 | } else if (discordWebhook |
| 77 | pairs.push({ |
| 78 | } |
| 79 | |
| 80 | const telegramToken = |
| 81 | if (!isValidTelegramBotToken(v |
| 82 | }); |
| 83 | if (telegramToken === |
| 84 | await db.delete |
| 85 | } else if (telegramToken |
| 86 | pairs.push({ |
| 87 | } |
| 88 | |
| 89 | const telegramChatId = |
| 90 | if (!isValidTelegramChatId(v |
| 91 | }); |
| 92 | if (telegramChatId === |
| 93 | await db.delete |
| 94 | } else if (telegramChatId |
| 95 | pairs.push({ |
| 96 | } |
| 97 | |
| 98 | for (const { |
| 99 | await upsertSetting(key |
| 100 | } |
| 101 | context.revalidate?.(); |
| 102 | recordActivity({ eventType: |
| 103 | }), |
| 104 | |
| 105 | test: adminProcedure |
| 106 | .input(z.object |
| 107 | .handler(async ( |
| 108 | const cfg = |
| 109 | const message = |
| 110 | try { |
| 111 | if (input.channel |
| 112 | if (!cfg.discordEnabled |
| 113 | return { ok: |
| 114 | } |
| 115 | await postDiscord(cfg |
| 116 | } else { |
| 117 | if (!cfg.telegramEnabled |
| 118 | return { ok: |
| 119 | } |
| 120 | await postTelegram(cfg |
| 121 | } |
| 122 | return { ok: |
| 123 | } catch (err) |
| 124 | return { ok: |
| 125 | } |
| 126 | }), |
| 127 | |
| 128 | getUserConfig: protectedProcedure.handler |
| 129 | const row = |
| 130 | where: eq(settings |
| 131 | }); |
| 132 | if (row?.value |
| 133 | const me = |
| 134 | where: eq(user |
| 135 | }); |
| 136 | return { |
| 137 | enabled: true as const |
| 138 | discordWebhookSet: !!me?.notificationDiscordWebhook |
| 139 | telegramTokenSet: !!me?.notificationTelegramToken |
| 140 | telegramChatId: me?.notificationTelegramChatId |
| 141 | }; |
| 142 | }), |
| 143 | |
| 144 | saveUserConfig: protectedProcedure |
| 145 | .input( |
| 146 | z.object({ |
| 147 | discordWebhookUrl: z.string |
| 148 | telegramBotToken: z.string |
| 149 | telegramChatId: z.string |
| 150 | }), |
| 151 | ) |
| 152 | .handler(async ( |
| 153 | const row = |
| 154 | where: eq(settings |
| 155 | }); |
| 156 | if (row?.value |
| 157 | throw new ORPCError( |
| 158 | } |
| 159 | |
| 160 | const updates: |
| 161 | const discordWebhook = |
| 162 | if (!isValidDiscordWebhookUrl(v |
| 163 | }); |
| 164 | if (discordWebhook === |
| 165 | else if (discordWebhook |
| 166 | |
| 167 | const telegramToken = |
| 168 | if (!isValidTelegramBotToken(v |
| 169 | }); |
| 170 | if (telegramToken === |
| 171 | else if (telegramToken |
| 172 | |
| 173 | const telegramChatId = |
| 174 | if (!isValidTelegramChatId(v |
| 175 | }); |
| 176 | if (telegramChatId === |
| 177 | else if (telegramChatId |
| 178 | |
| 179 | if (Object.keys |
| 180 | await db.update |
| 181 | } |
| 182 | }), |
| 183 | |
| 184 | testUser: protectedProcedure |
| 185 | .input(z.object |
| 186 | .handler(async ( |
| 187 | const cfg = |
| 188 | if (!cfg) |
| 189 | const message = |
| 190 | try { |
| 191 | if (input.channel |
| 192 | if (!cfg.discordWebhookUrl |
| 193 | return { ok: |
| 194 | } |
| 195 | await postDiscord(cfg |
| 196 | } else { |
| 197 | if (!cfg.telegramToken |
| 198 | return { ok: |
| 199 | } |
| 200 | await postTelegram(cfg |
| 201 | } |
| 202 | return { ok: |
| 203 | } catch (err) |
| 204 | return { ok: |
| 205 | } |
| 206 | }), |
| 207 | }; |
from
"
zod
"
;
from
"
@orpc/server
"
;
from
"
@struxa/db
"
;
from
"
@struxa/db
"
;
from
"
@struxa/db
"
;
from
"
../lib/crypto
"
;
from
"
../services/activity
"
;
}
from
"
../index
"
;
key
:
string
,
value
:
string
)
{
,
value
,
updatedAt
:
new
Date
()
}
)
{
value
,
updatedAt
:
new
Date
()
}
}
)
;
|
undefined
,
:
string
)
=>
void
,
clear
"
|
string
{
undefined
||
input
===
SENTINEL
)
return
"
keep
"
;
""
)
return
"
clear
"
;
{
(
async
()
=>
{
await
db
.
select
()
.
from
(
settings
)
;
Object
.
fromEntries
(
rows
.
map
(
(
r
)
=>
[
r
.
key
,
r
.
value
??
""
]))
;
===
"
true
"
,
,
===
"
true
"
,
,
??
""
,
===
"
true
"
,
()
.
optional
()
,
()
.
max
(
500
)
.
optional
()
,
()
.
optional
()
,
()
.
max
(
300
)
.
optional
()
,
()
.
max
(
64
)
.
optional
()
,
()
.
optional
()
,
{
context
,
input
}
)
=>
{
Array
<
{
key
:
string
;
value
:
string
}
>
=
[]
;
!==
undefined
)
pairs
.
push
(
{
key
:
"
notifications_discord_enabled
"
,
value
:
String
(
input
.
discordEnabled
)
}
)
;
!==
undefined
)
pairs
.
push
(
{
key
:
"
notifications_telegram_enabled
"
,
value
:
String
(
input
.
telegramEnabled
)
}
)
;
!==
undefined
)
pairs
.
push
(
{
key
:
"
notifications_user_config_enabled
"
,
value
:
String
(
input
.
userConfigEnabled
)
}
)
;
resolveSecret
(
input
.
discordWebhookUrl
,
(
v
)
=>
{
))
throw
new
ORPCError
(
"
BAD_REQUEST
"
,
{
message
:
"
Invalid Discord webhook URL
"
}
)
;
"
clear
"
)
{
(
settings
)
.
where
(
eq
(
settings
.
key
,
"
notifications_discord_webhook_url
"
))
;
!==
"
keep
"
)
{
key
:
"
notifications_discord_webhook_url
"
,
value
:
encrypt
(
discordWebhook
)
}
)
;
resolveSecret
(
input
.
telegramBotToken
,
(
v
)
=>
{
))
throw
new
ORPCError
(
"
BAD_REQUEST
"
,
{
message
:
"
Invalid Telegram bot token
"
}
)
;
"
clear
"
)
{
(
settings
)
.
where
(
eq
(
settings
.
key
,
"
notifications_telegram_bot_token
"
))
;
!==
"
keep
"
)
{
key
:
"
notifications_telegram_bot_token
"
,
value
:
encrypt
(
telegramToken
)
}
)
;
resolveSecret
(
input
.
telegramChatId
,
(
v
)
=>
{
))
throw
new
ORPCError
(
"
BAD_REQUEST
"
,
{
message
:
"
Invalid Telegram chat id
"
}
)
;
"
clear
"
)
{
(
settings
)
.
where
(
eq
(
settings
.
key
,
"
notifications_telegram_chat_id
"
))
;
!==
"
keep
"
)
{
key
:
"
notifications_telegram_chat_id
"
,
value
:
telegramChatId
}
)
;
key
,
value
}
of
pairs
)
{
,
value
)
;
"
admin:settings.update
"
,
userId
:
context
.
session
.
user
.
id
,
ip
:
context
.
ip
,
properties
:
{
keys
:
pairs
.
map
(
(
p
)
=>
p
.
key
)
}
}
)
;
(
{
channel
:
z
.
enum
([
"
discord
"
,
"
telegram
"
])
}
))
{
input
}
)
=>
{
await
getAdminNotificationConfig
()
;
buildMessage
(
"
test
"
,
{
appName
:
cfg
.
appName
}
)
;
===
"
discord
"
)
{
||
!
cfg
.
discordWebhookUrl
)
{
false
as
const
,
error
:
"
Discord is not configured or disabled
"
}
;
.
discordWebhookUrl
,
buildDiscordComponents
(
"
test
"
,
{
appName
:
cfg
.
appName
}
))
;
||
!
cfg
.
telegramToken
||
!
cfg
.
telegramChatId
)
{
false
as
const
,
error
:
"
Telegram is not configured or disabled
"
}
;
.
telegramToken
,
cfg
.
telegramChatId
,
message
)
;
true
as
const
}
;
{
false
as
const
,
error
:
err
instanceof
Error
?
err
.
message
:
"
Send failed
"
}
;
(
async
(
{
context
}
)
=>
{
await
db
.
query
.
settings
.
findFirst
(
{
.
key
,
"
notifications_user_config_enabled
"
)
,
!==
"
true
"
)
return
{
enabled
:
false
as
const
}
;
await
db
.
query
.
user
.
findFirst
(
{
.
id
,
context
.
session
.
user
.
id
)
,
,
,
,
??
""
,
()
.
max
(
500
)
.
optional
()
,
()
.
max
(
300
)
.
optional
()
,
()
.
max
(
64
)
.
optional
()
,
{
context
,
input
}
)
=>
{
await
db
.
query
.
settings
.
findFirst
(
{
.
key
,
"
notifications_user_config_enabled
"
)
,
!==
"
true
"
)
{
"
FORBIDDEN
"
,
{
message
:
"
User notifications are disabled
"
}
)
;
Record
<
string
,
string
|
null
>
=
{}
;
resolveSecret
(
input
.
discordWebhookUrl
,
(
v
)
=>
{
))
throw
new
ORPCError
(
"
BAD_REQUEST
"
,
{
message
:
"
Invalid Discord webhook URL
"
}
)
;
"
clear
"
)
updates
.
notificationDiscordWebhook
=
null
;
!==
"
keep
"
)
updates
.
notificationDiscordWebhook
=
encrypt
(
discordWebhook
)
;
resolveSecret
(
input
.
telegramBotToken
,
(
v
)
=>
{
))
throw
new
ORPCError
(
"
BAD_REQUEST
"
,
{
message
:
"
Invalid Telegram bot token
"
}
)
;
"
clear
"
)
updates
.
notificationTelegramToken
=
null
;
!==
"
keep
"
)
updates
.
notificationTelegramToken
=
encrypt
(
telegramToken
)
;
resolveSecret
(
input
.
telegramChatId
,
(
v
)
=>
{
))
throw
new
ORPCError
(
"
BAD_REQUEST
"
,
{
message
:
"
Invalid Telegram chat id
"
}
)
;
"
clear
"
)
updates
.
notificationTelegramChatId
=
null
;
!==
"
keep
"
)
updates
.
notificationTelegramChatId
=
telegramChatId
;
(
updates
)
.
length
>
0
)
{
(
user
)
.
set
(
updates
)
.
where
(
eq
(
user
.
id
,
context
.
session
.
user
.
id
))
;
(
{
channel
:
z
.
enum
([
"
discord
"
,
"
telegram
"
])
}
))
{
context
,
input
}
)
=>
{
await
getUserNotificationConfig
(
context
.
session
.
user
.
id
)
;
return
{
ok
:
false
as
const
,
error
:
"
User notifications are disabled
"
}
;
buildMessage
(
"
test
"
,
{
appName
:
cfg
.
appName
}
)
;
===
"
discord
"
)
{
)
{
false
as
const
,
error
:
"
Discord webhook is not configured
"
}
;
.
discordWebhookUrl
,
buildDiscordComponents
(
"
test
"
,
{
appName
:
cfg
.
appName
}
))
;
||
!
cfg
.
telegramChatId
)
{
false
as
const
,
error
:
"
Telegram is not configured
"
}
;
.
telegramToken
,
cfg
.
telegramChatId
,
message
)
;
true
as
const
}
;
{
false
as
const
,
error
:
err
instanceof
Error
?
err
.
message
:
"
Send failed
"
}
;