2020-08-30 13:54:59 +00:00
|
|
|
const {redirect} = require("pinski/plugins")
|
2020-09-23 12:05:02 +00:00
|
|
|
const db = require("../utils/db")
|
|
|
|
const constants = require("../utils/constants")
|
2020-12-29 03:21:48 +00:00
|
|
|
const {getUser, setToken} = require("../utils/getuser")
|
2020-09-23 12:05:02 +00:00
|
|
|
const validate = require("../utils/validate")
|
2020-08-30 13:54:59 +00:00
|
|
|
const V = validate.V
|
2020-09-23 12:05:02 +00:00
|
|
|
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 = {}
|
2020-08-31 13:51:24 +00:00
|
|
|
const user = getUser(req, responseHeaders)
|
|
|
|
const settings = user.getSettingsOrDefaults()
|
|
|
|
const token = user.token
|
2020-08-30 13:54:59 +00:00
|
|
|
|
|
|
|
if (add) {
|
2022-08-22 22:37:43 +00:00
|
|
|
await fetchChannel("channel", ucid, settings.instance)
|
2021-08-23 11:43:23 +00:00
|
|
|
db.prepare(
|
|
|
|
"INSERT INTO Subscriptions (token, ucid) VALUES (?, ?)"
|
|
|
|
).run(token, ucid)
|
2020-08-30 13:54:59 +00:00
|
|
|
} else {
|
|
|
|
db.prepare("DELETE FROM Subscriptions WHERE token = ? AND ucid = ?").run(token, ucid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.has("referrer")) {
|
|
|
|
return {
|
|
|
|
statusCode: 303,
|
2021-01-09 01:09:59 +00:00
|
|
|
contentType: "text/plain",
|
2020-08-30 13:54:59 +00:00
|
|
|
headers: Object.assign(responseHeaders, {
|
|
|
|
Location: params.get("referrer")
|
|
|
|
}),
|
2021-01-09 01:09:59 +00:00
|
|
|
content: "Success, redirecting..."
|
2020-08-30 13:54:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
statusCode: 200,
|
2021-01-09 01:09:59 +00:00
|
|
|
contentType: "text/plain",
|
2020-08-30 13:54:59 +00:00
|
|
|
headers: responseHeaders,
|
2021-01-09 01:09:59 +00:00
|
|
|
content: "Success."
|
2020-08-30 13:54:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.go()
|
|
|
|
}
|
2020-12-29 03:21:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-09 01:09:59 +00:00
|
|
|
route: `/formapi/markwatched/(${constants.regex.video_id})`, methods: ["POST"], code: async ({req, fill}) => {
|
|
|
|
const videoID = fill[0]
|
|
|
|
const user = getUser(req)
|
|
|
|
const token = user.token
|
|
|
|
if (token) {
|
|
|
|
db.prepare("INSERT OR IGNORE INTO WatchedVideos (token, videoID) VALUES (?, ?)").run(token, videoID)
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
statusCode: 303,
|
|
|
|
contentType: "text/plain",
|
|
|
|
headers: {
|
|
|
|
Location: "/subscriptions"
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
status: "Success, redirecting..."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
route: "/formapi/erase", methods: ["POST"], upload: true, code: async ({req, fill, body}) => {
|
2020-12-29 03:21:48 +00:00
|
|
|
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,
|
2021-01-09 01:09:59 +00:00
|
|
|
contentType: "text/plain",
|
2020-12-29 03:21:48 +00:00
|
|
|
headers: {
|
|
|
|
location: "/",
|
|
|
|
"set-cookie": `token=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax`
|
|
|
|
},
|
2021-01-09 01:09:59 +00:00
|
|
|
content: "Success, redirecting..."
|
2020-12-29 03:21:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.go()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
route: "/formapi/importsession/(\\w+)", methods: ["GET"], code: async ({req, fill}) => {
|
|
|
|
return {
|
|
|
|
statusCode: 303,
|
|
|
|
headers: setToken({
|
|
|
|
location: "/subscriptions"
|
2021-01-08 12:49:22 +00:00
|
|
|
}, fill[0]).responseHeaders,
|
2021-01-09 01:09:59 +00:00
|
|
|
contentType: "text/plain",
|
|
|
|
content: "Success, redirecting..."
|
2020-12-29 03:21:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-30 13:54:59 +00:00
|
|
|
}
|
|
|
|
]
|