2020-08-31 13:22:16 +00:00
|
|
|
const {render, redirect} = require("pinski/plugins")
|
2020-09-23 12:05:02 +00:00
|
|
|
const db = require("../utils/db")
|
|
|
|
const {getToken, getUser} = require("../utils/getuser")
|
|
|
|
const constants = require("../utils/constants")
|
2021-03-01 10:35:09 +00:00
|
|
|
const {instancesList} = require("../background/instances")
|
2020-09-23 12:05:02 +00:00
|
|
|
const validate = require("../utils/validate")
|
2020-08-31 13:22:16 +00:00
|
|
|
const V = validate.V
|
|
|
|
|
|
|
|
module.exports = [
|
|
|
|
{
|
|
|
|
route: "/settings", methods: ["GET"], code: async ({req}) => {
|
|
|
|
const user = getUser(req)
|
|
|
|
const settings = user.getSettings()
|
2021-03-01 10:35:09 +00:00
|
|
|
const instances = instancesList.get()
|
2021-01-13 10:48:18 +00:00
|
|
|
return render(200, "pug/settings.pug", {constants, user, settings, instances})
|
2020-08-31 13:22:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
route: "/settings", methods: ["POST"], upload: true, code: async ({req, body}) => {
|
|
|
|
return new V()
|
|
|
|
.with(validate.presetLoad({body}))
|
|
|
|
.with(validate.presetURLParamsBody())
|
|
|
|
.last(async state => {
|
|
|
|
const {params} = state
|
|
|
|
const responseHeaders = {
|
|
|
|
Location: "/settings"
|
|
|
|
}
|
|
|
|
const token = getToken(req, responseHeaders)
|
|
|
|
const data = {}
|
|
|
|
|
|
|
|
for (const key of Object.keys(constants.user_settings)) {
|
|
|
|
const setting = constants.user_settings[key]
|
|
|
|
if (params.has(key)) {
|
2021-02-06 06:56:50 +00:00
|
|
|
let provided = params.get(key)
|
|
|
|
|
2021-04-25 12:00:56 +00:00
|
|
|
// Correct common errors in instance URL
|
2021-02-06 06:56:50 +00:00
|
|
|
if (key === "instance") {
|
|
|
|
provided = provided.replace(/\/+$/, "")
|
|
|
|
}
|
|
|
|
|
2021-04-25 12:00:56 +00:00
|
|
|
// Delete existing watch history when it's disabled
|
|
|
|
if (key === "save_history") {
|
|
|
|
if (provided !== "1") {
|
|
|
|
db.prepare("DELETE FROM WatchedVideos WHERE token = ?").run(token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 13:22:16 +00:00
|
|
|
if (setting.type === "string") {
|
|
|
|
if (provided) data[key] = provided
|
|
|
|
else data[key] = null
|
|
|
|
} else if (setting.type === "integer") {
|
|
|
|
if (isNaN(provided)) data[key] = null
|
|
|
|
else data[key] = +provided
|
|
|
|
} else if (setting.type === "boolean") {
|
2020-10-26 07:29:05 +00:00
|
|
|
if (provided === "1") data[key] = 1
|
|
|
|
else data[key] = 0
|
2020-08-31 13:22:16 +00:00
|
|
|
} else {
|
|
|
|
throw new Error("Unsupported setting type: "+setting.type)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data[key] = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
db.prepare("DELETE FROM Settings WHERE token = ?").run(token)
|
|
|
|
const keys = ["token", ...Object.keys(constants.user_settings)]
|
|
|
|
const baseFields = keys.join(", ")
|
|
|
|
const atFields = keys.map(k => "@"+k).join(", ")
|
|
|
|
db.prepare(`INSERT INTO Settings (${baseFields}) VALUES (${atFields})`).run({token, ...data})
|
|
|
|
|
|
|
|
return {
|
|
|
|
statusCode: 303,
|
|
|
|
headers: responseHeaders,
|
|
|
|
contentType: "text/html",
|
|
|
|
content: "Redirecting..."
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.go()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|