1
0
Fork 0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2026-03-02 02:31:35 +00:00

Rework subscribing to deleted channels

This commit is contained in:
Cadence Ember 2022-01-10 14:18:45 +13:00
parent 15e3f06ad6
commit 109dcd22de
No known key found for this signature in database
GPG key ID: BC1C2C61CF521B17
12 changed files with 167 additions and 22 deletions

View file

@ -50,6 +50,7 @@ let constants = {
csrf_time: 4*60*60*1000,
seen_token_subscriptions_eligible: 40*60*60*1000,
subscriptions_refresh_loop_min: 5*60*1000,
subscriptions_refesh_fake_not_found_cooldown: 10*60*1000,
},
// Pattern matching.

View file

@ -54,7 +54,7 @@ class User {
getSubscriptions() {
if (this.token) {
return db.prepare("SELECT ucid FROM Subscriptions WHERE token = ? AND channel_missing = 0").pluck().all(this.token)
return db.prepare("SELECT ucid FROM Subscriptions WHERE token = ?").pluck().all(this.token)
} else {
return []
}
@ -62,7 +62,7 @@ class User {
isSubscribed(ucid) {
if (this.token) {
return !!db.prepare("SELECT * FROM Subscriptions WHERE token = ? AND ucid = ? AND channel_missing = 0").get([this.token, ucid])
return !!db.prepare("SELECT * FROM Subscriptions WHERE token = ? AND ucid = ?").get([this.token, ucid])
} else {
return false
}

View file

@ -75,6 +75,26 @@ const deltas = [
function() {
db.prepare("ALTER TABLE Settings ADD COLUMN theme INTEGER DEFAULT 0")
.run()
},
// 12: Channels +missing +missing_reason, Subscriptions -
// Better management for missing channels
// We totally discard the existing Subscriptions.channel_missing since it is unreliable.
function() {
db.prepare("ALTER TABLE Channels ADD COLUMN missing INTEGER NOT NULL DEFAULT 0")
.run()
db.prepare("ALTER TABLE Channels ADD COLUMN missing_reason TEXT")
.run()
// https://www.sqlite.org/lang_altertable.html#making_other_kinds_of_table_schema_changes
db.transaction(() => {
db.prepare("CREATE TABLE NEW_Subscriptions (token TEXT NOT NULL, ucid TEXT NOT NULL, PRIMARY KEY (token, ucid))")
.run()
db.prepare("INSERT INTO NEW_Subscriptions (token, ucid) SELECT token, ucid FROM Subscriptions")
.run()
db.prepare("DROP TABLE Subscriptions")
.run()
db.prepare("ALTER TABLE NEW_Subscriptions RENAME TO Subscriptions")
.run()
})()
}
]

View file

@ -2,14 +2,58 @@ const {request} = require("./request")
const db = require("./db")
async function fetchChannel(ucid, instance) {
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
}
}
}
if (!instance) throw new Error("No instance parameter provided")
// fetch
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} */
const channel = await request(`${instance}/api/v1/channels/${ucid}`).then(res => res.json())
// update database
const bestIcon = channel.authorThumbnails.slice(-1)[0]
const iconURL = bestIcon ? bestIcon.url : null
db.prepare("REPLACE INTO Channels (ucid, name, icon_url) VALUES (?, ?, ?)").run([channel.authorId, channel.author, iconURL])
// return
// 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)
return channel
}