From c3455ec1832f186d4cc86674f9bfd3b4f652a5db Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 7 Jul 2020 22:08:19 +1200 Subject: [PATCH] Deleted accounts become 404 instead of error Fixes #95. --- src/lib/cache.js | 9 +++++---- src/lib/collectors.js | 15 ++++++++++++--- src/lib/constants.js | 3 ++- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/lib/cache.js b/src/lib/cache.js index 09d3aaf..4c6db87 100644 --- a/src/lib/cache.js +++ b/src/lib/cache.js @@ -140,7 +140,7 @@ class RequestCache extends TtlCache { class UserRequestCache extends TtlCache { constructor(ttl) { super(ttl) - /** @type {Map} */ + /** @type {Map} */ this.cache } @@ -153,7 +153,7 @@ class UserRequestCache extends TtlCache { const existing = this.cache.get(key) // Preserve html failure status if now requesting as reel const htmlFailed = isReel && existing && existing.htmlFailed - this.cache.set(key, {data, isReel, isFailedPromise: false, htmlFailed, time: Date.now()}) + this.cache.set(key, {data, isReel, isFailedPromise: false, htmlFailed, reelFailed: false, time: Date.now()}) } /** @@ -180,7 +180,7 @@ class UserRequestCache extends TtlCache { } } } else { // (existing.isFailedPromise ~= true): the existing entry is a failed request - if (existing.htmlFailed && !willFetchReel) { // it's no use! the HTML attempt will fail again: abandon it. + if (existing.reelFailed || (existing.htmlFailed && !willFetchReel)) { // it's no use! the attempt will fail again; don't try. return Promise.resolve(existing.data) // this is actually a promise rejection } } @@ -191,7 +191,8 @@ class UserRequestCache extends TtlCache { } return result }).catch(error => { - this.cache.get(key).htmlFailed = true + if (willFetchReel) this.cache.get(key).reelFailed = true + else this.cache.get(key).htmlFailed = true this.cache.get(key).isFailedPromise = true throw error }) diff --git a/src/lib/collectors.js b/src/lib/collectors.js index 79f806a..eaed6f4 100644 --- a/src/lib/collectors.js +++ b/src/lib/collectors.js @@ -206,15 +206,17 @@ function fetchUserFromCombined(userID, username) { return res }).then(res => res.json()).then(root => { const result = root.data.user - if (!result) throw constants.symbols.NOT_FOUND + if (!result) { + // user ID doesn't exist. + db.prepare("DELETE FROM Users WHERE user_id = ?").run(userID) // deleting the entry makes sense to me; the username might be claimed by somebody else later + throw constants.symbols.NOT_FOUND // this should cascade down and show the user not found page + } // require down here or have to deal with require loop. require cache will take care of it anyway. // ReelUser -> Timeline -> TimelineEntry -> collectors -/> User const ReelUser = require("./structures/ReelUser") const user = new ReelUser(result.reel.user) history.report("reel", true) return user - }).catch(error => { - throw error }) }).then(async user => { // Add first timeline page @@ -274,6 +276,12 @@ function fetchTimelinePage(userID, after) { return switcher.request("timeline_graphql", `https://www.instagram.com/graphql/query/?${p.toString()}`, async res => { if (res.status === 429) throw constants.symbols.RATE_LIMITED }).then(g => g.json()).then(root => { + if (root.data.user === null) { + // user ID doesn't exist. + db.prepare("DELETE FROM Users WHERE user_id = ?").run(userID) // deleting the entry makes sense to me; the username might be claimed by somebody else later + requestCache + throw constants.symbols.NOT_FOUND // this should cascade down and show the user not found page + } /** @type {import("./types").PagedEdges} */ const timeline = root.data.user.edge_owner_to_timeline_media history.report("timeline", true) @@ -425,6 +433,7 @@ module.exports.fetchTimelinePage = fetchTimelinePage module.exports.fetchIGTVPage = fetchIGTVPage module.exports.getOrCreateShortcode = getOrCreateShortcode module.exports.fetchShortcodeData = fetchShortcodeData +module.exports.requestCache = requestCache module.exports.userRequestCache = userRequestCache module.exports.timelineEntryCache = timelineEntryCache module.exports.getOrFetchShortcode = getOrFetchShortcode diff --git a/src/lib/constants.js b/src/lib/constants.js index adca553..deb3bbf 100644 --- a/src/lib/constants.js +++ b/src/lib/constants.js @@ -237,7 +237,8 @@ let constants = { }, resources: { - instances_wiki_raw: "https://raw.githubusercontent.com/wiki/cloudrac3r/bibliogram/Instances.md" + instances_wiki_raw: "https://raw.githubusercontent.com/wiki/cloudrac3r/bibliogram/Instances.md", + saved_requests_location: "https://meta.bibliogram.art/saved_requests/" }, // My code uses this stuff. Server owners have no reason to change it.