mirror of
https://github.com/koreanbots/core.git
synced 2025-12-15 06:10:22 +00:00
* feat: ensure djs clients are singleton instances * feat: add votes table * feat: vote notification * chore: reduce vote cooldown to 15 min * feat: add SetNotification to server * chore: add debug logs * fix: do not add notification when token and voteid already exists * feat: add loading indicator * feat: refresh notification when voted * feat: add opt-out * feat: add debug log * fix: initialize firebase app * fix: remove app on messaging * feat: show notifications only with service worker * fix: state improperly used * fix: schedule notification if notification is newly added * chore: remove duplicated notification * chore: add spacing * chore: get token if notification is granted * chore: change vote cooldown to 12 hours * chore: remove logging
91 lines
2.8 KiB
TypeScript
91 lines
2.8 KiB
TypeScript
import { NextApiRequest } from 'next'
|
|
|
|
import { CaptchaVerify, get, put } from '@utils/Query'
|
|
import RequestHandler from '@utils/RequestHandler'
|
|
import ResponseWrapper from '@utils/ResponseWrapper'
|
|
import { checkToken } from '@utils/Csrf'
|
|
import Yup from '@utils/Yup'
|
|
import { VOTE_COOLDOWN } from '@utils/Constants'
|
|
import { sendWebhook } from '@utils/Webhook'
|
|
import { WebhookType } from '@types'
|
|
|
|
const ServerVote = RequestHandler()
|
|
.get(async (req: GetApiRequest, res) => {
|
|
const server = await get.ServerAuthorization(req.headers.authorization)
|
|
if (!server) return ResponseWrapper(res, { code: 401 })
|
|
if (req.query.id !== server) return ResponseWrapper(res, { code: 403 })
|
|
const userID = await Yup.string()
|
|
.required()
|
|
.label('userID')
|
|
.validate(req.query.userID)
|
|
.then((el) => el)
|
|
.catch((e) => {
|
|
ResponseWrapper(res, { code: 400, errors: e.errors })
|
|
return null
|
|
})
|
|
if (!userID) return ResponseWrapper(res, { code: 400 })
|
|
const result = await get.vote(userID, server, 'server')
|
|
return ResponseWrapper(res, {
|
|
code: 200,
|
|
data: { voted: +new Date() < result + VOTE_COOLDOWN, lastVote: result },
|
|
})
|
|
})
|
|
.post(async (req: PostApiRequest, res) => {
|
|
const user = await get.Authorization(req.cookies.token)
|
|
if (!user) return ResponseWrapper(res, { code: 401 })
|
|
const server = await get.server.load(req.query.id)
|
|
if (!server) return ResponseWrapper(res, { code: 404, message: '존재하지 않는 서버입니다.' })
|
|
const csrfValidated = checkToken(req, res, req.body._csrf)
|
|
if (!csrfValidated) return
|
|
const captcha = await CaptchaVerify(req.body._captcha)
|
|
if (!captcha) return ResponseWrapper(res, { code: 400, message: '캡챠 검증에 실패하였습니다.' })
|
|
|
|
const vote = await put.voteServer(user, server.id)
|
|
|
|
const token = req.body.firebaseToken
|
|
let notificationSet = false
|
|
|
|
if (token) {
|
|
const result = await get.notifications.token(token, server.id)
|
|
notificationSet = !!result
|
|
}
|
|
|
|
if (vote === null) return ResponseWrapper(res, { code: 401 })
|
|
else if (vote === true) {
|
|
get.server.clear(req.query.id)
|
|
sendWebhook(server, {
|
|
type: 'server',
|
|
data: {
|
|
guildId: server.id,
|
|
type: WebhookType.HeartChange,
|
|
before: server.votes,
|
|
after: server.votes + 1,
|
|
userId: user,
|
|
},
|
|
timestamp: Date.now(),
|
|
})
|
|
return ResponseWrapper(res, { code: 200, data: { notificationSet } })
|
|
} else return ResponseWrapper(res, { code: 429, data: { retryAfter: vote, notificationSet } })
|
|
})
|
|
|
|
interface ApiRequest extends NextApiRequest {
|
|
query: {
|
|
id: string
|
|
}
|
|
}
|
|
|
|
interface GetApiRequest extends ApiRequest {
|
|
query: {
|
|
id: string
|
|
userID: string
|
|
}
|
|
}
|
|
interface PostApiRequest extends ApiRequest {
|
|
body: {
|
|
_captcha: string
|
|
_csrf: string
|
|
firebaseToken?: string | null
|
|
}
|
|
}
|
|
export default ServerVote
|