1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-09-19 18:57:30 +00:00
cloudtube/api/formapi.js

99 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-08-30 13:54:59 +00:00
const {redirect} = require("pinski/plugins")
const db = require("../utils/db")
const constants = require("../utils/constants")
2020-12-29 03:21:48 +00:00
const {getUser, setToken} = require("../utils/getuser")
const validate = require("../utils/validate")
2020-08-30 13:54:59 +00:00
const V = validate.V
const {fetchChannel} = require("../utils/youtube")
2020-08-30 13:54:59 +00:00
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 user = getUser(req, responseHeaders)
const settings = user.getSettingsOrDefaults()
const token = user.token
2020-08-30 13:54:59 +00:00
if (add) {
await fetchChannel(ucid, settings.instance)
2020-08-30 13:54:59 +00:00
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()
}
2020-12-29 03:21:48 +00:00
},
{
route: `/formapi/erase`, methods: ["POST"], upload: true, code: async ({req, fill, body}) => {
return new V()
.with(validate.presetLoad({body}))
.with(validate.presetURLParamsBody())
.with(validate.presetEnsureParams(["token"]))
.last(async state => {
const {params} = state
const token = params.get("token")
;["Subscriptions", "Settings", "SeenTokens", "WatchedVideos"].forEach(table => {
db.prepare(`DELETE FROM ${table} WHERE token = ?`).run(token)
})
return {
statusCode: 303,
headers: {
location: "/",
"set-cookie": `token=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax`
},
content: {
status: "ok"
}
}
})
.go()
}
},
{
route: "/formapi/importsession/(\\w+)", methods: ["GET"], code: async ({req, fill}) => {
return {
statusCode: 303,
headers: setToken({
location: "/subscriptions"
}, fill[0]),
contentType: "application/json",
content: {
status: "ok"
}
}
}
2020-08-30 13:54:59 +00:00
}
]