1
0
Fork 0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2026-05-26 12:32:25 +00:00

Add database and subscribe button

This commit is contained in:
Cadence Ember 2020-08-31 01:54:59 +12:00
parent 2af05e43a9
commit f24e1bb39c
No known key found for this signature in database
GPG key ID: 128B99B1B74A6412
25 changed files with 972 additions and 44 deletions

56
api/formapi.js Normal file
View file

@ -0,0 +1,56 @@
const {redirect} = require("pinski/plugins")
const db = require("./utils/db")
const constants = require("./utils/constants")
const {getToken} = require("./utils/getuser")
const validate = require("./utils/validate")
const V = validate.V
const {fetchChannel} = require("./utils/youtube")
module.exports = [
{
route: `/formapi/(un|)subscribe/(${constants.regex.ucid})`, methods: ["POST"], upload: true, code: async ({req, fill, body}) => {
const add = !fill[0]
const ucid = fill[1]
return new V()
.with(validate.presetLoad({body}))
.with(validate.presetURLParamsBody())
.last(async state => {
const {params} = state
const responseHeaders = {}
const token = getToken(req, responseHeaders)
if (add) {
await fetchChannel(ucid)
db.prepare("INSERT OR IGNORE INTO Subscriptions (token, ucid) VALUES (?, ?)").run(token, ucid)
} else {
db.prepare("DELETE FROM Subscriptions WHERE token = ? AND ucid = ?").run(token, ucid)
}
if (params.has("referrer")) {
return {
statusCode: 303,
contentType: "application/json",
headers: Object.assign(responseHeaders, {
Location: params.get("referrer")
}),
content: {
status: "ok"
}
}
return redirect(params.get("referrer"), 303)
} else {
return {
statusCode: 200,
contentType: "application/json",
headers: responseHeaders,
content: {
status: "ok"
}
}
}
})
.go()
}
}
]