2021-01-12 10:49:00 +00:00
|
|
|
const {request} = require("./request")
|
2020-08-30 13:54:59 +00:00
|
|
|
const db = require("./db")
|
|
|
|
|
2020-08-31 13:22:16 +00:00
|
|
|
async function fetchChannel(ucid, instance) {
|
2022-01-10 01:18:45 +00:00
|
|
|
function updateGoodData(channel) {
|
|
|
|
const bestIcon = channel.authorThumbnails.slice(-1)[0]
|
|
|
|
const iconURL = bestIcon ? bestIcon.url : null
|
|
|
|
db.prepare("REPLACE INTO Channels (ucid, name, icon_url, missing, missing_reason) VALUES (?, ?, ?, 0, NULL)").run(channel.authorId, channel.author, iconURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateBadData(channel) {
|
|
|
|
if (channel.identifier === "NOT_FOUND" || channel.identifier === "ACCOUNT_TERMINATED") {
|
|
|
|
db.prepare("UPDATE Channels SET missing = 1, missing_reason = ? WHERE ucid = ?").run(channel.error, channel.authorId)
|
|
|
|
return {
|
|
|
|
missing: true,
|
|
|
|
message: channel.error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
missing: false,
|
|
|
|
message: channel.error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 13:40:34 +00:00
|
|
|
if (!instance) throw new Error("No instance parameter provided")
|
2022-01-10 01:18:45 +00:00
|
|
|
|
|
|
|
const row = db.prepare("SELECT * FROM Channels WHERE ucid = ?").get(ucid)
|
|
|
|
|
|
|
|
// handle the case where the channel has a known error
|
|
|
|
if (row && row.missing_reason) {
|
|
|
|
return {
|
|
|
|
error: true,
|
|
|
|
ucid,
|
|
|
|
row,
|
|
|
|
missing: true,
|
|
|
|
message: row.missing_reason
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @type {any} */
|
2021-01-12 10:49:00 +00:00
|
|
|
const channel = await request(`${instance}/api/v1/channels/${ucid}`).then(res => res.json())
|
2022-01-10 01:18:45 +00:00
|
|
|
|
|
|
|
// handle the case where the channel has a newly discovered error
|
|
|
|
if (channel.error) {
|
|
|
|
const missingData = updateBadData(channel)
|
|
|
|
return {
|
|
|
|
error: true,
|
|
|
|
ucid,
|
|
|
|
row,
|
|
|
|
...missingData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle the case where the channel returns good data (this is the only remaining scenario)
|
|
|
|
updateGoodData(channel)
|
2020-08-30 13:54:59 +00:00
|
|
|
return channel
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.fetchChannel = fetchChannel
|