From 83dec0c7abd99263856fa2f9130565f80c8992d9 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 24 Sep 2020 00:48:32 +1200 Subject: [PATCH] Display how recently channels were refreshed --- api/subscriptions.js | 19 ++++++++++--------- background/feed-update.js | 5 +++++ pug/subscriptions.pug | 10 ++++++++++ utils/upgradedb.js | 7 ++++++- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/api/subscriptions.js b/api/subscriptions.js index 00d1227..ef1d8c5 100644 --- a/api/subscriptions.js +++ b/api/subscriptions.js @@ -2,7 +2,7 @@ const {render} = require("pinski/plugins") const db = require("../utils/db") const {fetchChannelLatest} = require("../utils/youtube") const {getUser} = require("../utils/getuser") -const converters = require("../utils/converters") +const {timeToPastText} = require("../utils/converters") module.exports = [ { @@ -11,25 +11,26 @@ module.exports = [ let hasSubscriptions = false let videos = [] let channels = [] + let refreshed = null if (user.token) { + // get channels const subscriptions = user.getSubscriptions() - const channelPrepared = db.prepare("SELECT * FROM Channels WHERE ucid = ?") - channels = subscriptions.map(id => channelPrepared.get(id)).sort((a, b) => { - if (a.name < b.name) return -1 - else if (b.name > a.name) return 1 - else return 0 - }) + const template = Array(subscriptions.length).fill("?").join(", ") + channels = db.prepare(`SELECT * FROM Channels WHERE ucid IN (${template}) ORDER BY name`).all(subscriptions) + // get refreshed status + refreshed = db.prepare(`SELECT min(refreshed) as min, max(refreshed) as max, count(refreshed) as count FROM Channels WHERE ucid IN (${template})`).get(subscriptions) + // get videos if (subscriptions.length) { hasSubscriptions = true const template = Array(subscriptions.length).fill("?").join(", ") videos = db.prepare(`SELECT * FROM Videos WHERE authorId IN (${template}) ORDER BY published DESC LIMIT 60`).all(subscriptions) .map(video => { - video.publishedText = converters.timeToPastText(video.published * 1000) + video.publishedText = timeToPastText(video.published * 1000) return video }) } } - return render(200, "pug/subscriptions.pug", {hasSubscriptions, videos, channels}) + return render(200, "pug/subscriptions.pug", {hasSubscriptions, videos, channels, refreshed, timeToPastText}) } } ] diff --git a/background/feed-update.js b/background/feed-update.js index 9d7bb51..cacc72d 100644 --- a/background/feed-update.js +++ b/background/feed-update.js @@ -9,6 +9,9 @@ const prepared = { + " ( videoId, title, author, authorId, published, viewCountText, descriptionHtml)" + " VALUES" + " (@videoId, @title, @author, @authorId, @published, @viewCountText, @descriptionHtml)" + ), + channel_refreshed_update: db.prepare( + "UPDATE Channels SET refreshed = ? WHERE ucid = ?" ) } @@ -70,6 +73,8 @@ function refreshChannel(ucid) { // store prepared.video_insert.run(video) }) + // update channel refreshed + prepared.channel_refreshed_update.run(Date.now(), ucid) console.log(`updated ${root.length} videos for channel ${ucid}`) } else if (root.identifier === "PUBLISHED_DATES_NOT_PROVIDED") { return [] // nothing we can do. skip this iteration. diff --git a/pug/subscriptions.pug b/pug/subscriptions.pug index b0cd771..58fcc82 100644 --- a/pug/subscriptions.pug +++ b/pug/subscriptions.pug @@ -17,6 +17,16 @@ block content img(src=channel.icon_url width=512 height=512 alt="").thumbnail span.name= channel.name + if refreshed + section + details.channels-details + summary Last refreshed + div Oldest channel was refreshed #{timeToPastText(refreshed.min)} + div Newest channel was refreshed #{timeToPastText(refreshed.max)} + - const notLoaded = channels.length - refreshed.count + if notLoaded + div #{notLoaded} subscriptions have not been refreshed at all + each video in videos .subscriptions-video +video_list_item(video) diff --git a/utils/upgradedb.js b/utils/upgradedb.js index 11c59b8..c3f38b2 100644 --- a/utils/upgradedb.js +++ b/utils/upgradedb.js @@ -18,13 +18,18 @@ const deltas = [ .run() db.prepare("CREATE TABLE Settings (token TEXT NOT NULL, instance TEXT, save_history INTEGER, PRIMARY KEY (token))") .run() + }, + // 1: Channels +refreshed + function() { + db.prepare("ALTER TABLE Channels ADD COLUMN refreshed INTEGER") + .run() } ] async function createBackup(entry) { const filename = `db/backups/cloudtube.db.bak-v${entry-1}` process.stdout.write(`Backing up current to ${filename}... `) - await db.backup(pj(__dirname, "../../", filename)) + await db.backup(pj(__dirname, "../", filename)) process.stdout.write("done.\n") }