From c0464a893804d3a7480bb73ba08e714da271321d Mon Sep 17 00:00:00 2001 From: Christian Bundy Date: Sat, 29 Jun 2019 14:14:09 -0700 Subject: [PATCH] Handle error where post parent cannot be found --- src/routes/models/post.js | 54 +++++++++++++++++++++++++-------------- src/routes/thread.js | 5 ++++ 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/routes/models/post.js b/src/routes/models/post.js index 81f9885..1a9800b 100644 --- a/src/routes/models/post.js +++ b/src/routes/models/post.js @@ -1,7 +1,7 @@ const lodash = require('lodash') const pull = require('pull-stream') const prettyMs = require('pretty-ms') -const debug = require('debug')('oasis') +const debug = require('debug')('oasis:model-post') const cooler = require('./lib/cooler') const configure = require('./lib/configure') @@ -181,37 +181,52 @@ module.exports = { return messages }, fromThread: async (msgId, customOptions) => { + debug('thread: %s', msgId) const ssb = await cooler.connect() const options = configure({ id: msgId }, customOptions) const rawMsg = await cooler.get(ssb.get, options) + debug('got raw message') const parents = [] const getRootAncestor = (msg) => new Promise(async (resolve, reject) => { + debug('getting root ancestor of %s', msg.key) if (typeof msg.value.content === 'string') { + debug('private message') // Private message we can't decrypt, stop looking for parents. return resolve(parents) } if (typeof msg.value.content.fork === 'string') { - // It's a message reply, get the parent! - const fork = await cooler.get(ssb.get, { - id: msg.value.content.fork, - meta: true, - private: true - }) - - resolve(getRootAncestor(fork)) + debug('fork, get the parent') + try { + // It's a message reply, get the parent! + const fork = await cooler.get(ssb.get, { + id: msg.value.content.fork, + meta: true, + private: true + }) + resolve(getRootAncestor(fork)) + } catch (e) { + debug(e) + resolve(msg) + } } else if (typeof msg.value.content.root === 'string') { - // It's a thread reply, get the parent! - const root = await cooler.get(ssb.get, { - id: msg.value.content.root, - meta: true, - private: true - }) - - resolve(getRootAncestor(root)) + debug('thread reply') + try { + // It's a thread reply, get the parent! + const root = await cooler.get(ssb.get, { + id: msg.value.content.root, + meta: true, + private: true + }) + resolve(getRootAncestor(root)) + } catch (e) { + debug(e) + resolve(msg) + } } else { + debug('got root ancestor') resolve(msg) } }) @@ -288,10 +303,11 @@ module.exports = { resolve(deepReplies) }) + debug('about to get root ancestor') const rootAncestor = await getRootAncestor(rawMsg) + debug('got root ancestors') const deepReplies = await getDeepReplies(rootAncestor.key) - - debug('deep replies: %O', deepReplies) + debug('got deep replies') const allMessages = [rootAncestor, ...deepReplies].map(message => { const isThreadTarget = message.key === msgId diff --git a/src/routes/thread.js b/src/routes/thread.js index 1316988..05a7732 100644 --- a/src/routes/thread.js +++ b/src/routes/thread.js @@ -1,9 +1,14 @@ +const debug = require('debug')('oasis:route-thread') + const listView = require('./views/list') const post = require('./models/post') module.exports = async function thread (ctx) { + debug('called thread!') const msgId = ctx.params.id + debug('thread ID: %s', msgId) const messages = await post.fromThread(msgId) + debug('got %i messages', messages.length) ctx.body = listView({ messages }) }