Handle error where post parent cannot be found
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
const lodash = require('lodash')
|
const lodash = require('lodash')
|
||||||
const pull = require('pull-stream')
|
const pull = require('pull-stream')
|
||||||
const prettyMs = require('pretty-ms')
|
const prettyMs = require('pretty-ms')
|
||||||
const debug = require('debug')('oasis')
|
const debug = require('debug')('oasis:model-post')
|
||||||
|
|
||||||
const cooler = require('./lib/cooler')
|
const cooler = require('./lib/cooler')
|
||||||
const configure = require('./lib/configure')
|
const configure = require('./lib/configure')
|
||||||
@ -181,37 +181,52 @@ module.exports = {
|
|||||||
return messages
|
return messages
|
||||||
},
|
},
|
||||||
fromThread: async (msgId, customOptions) => {
|
fromThread: async (msgId, customOptions) => {
|
||||||
|
debug('thread: %s', msgId)
|
||||||
const ssb = await cooler.connect()
|
const ssb = await cooler.connect()
|
||||||
const options = configure({ id: msgId }, customOptions)
|
const options = configure({ id: msgId }, customOptions)
|
||||||
const rawMsg = await cooler.get(ssb.get, options)
|
const rawMsg = await cooler.get(ssb.get, options)
|
||||||
|
debug('got raw message')
|
||||||
|
|
||||||
const parents = []
|
const parents = []
|
||||||
|
|
||||||
const getRootAncestor = (msg) => new Promise(async (resolve, reject) => {
|
const getRootAncestor = (msg) => new Promise(async (resolve, reject) => {
|
||||||
|
debug('getting root ancestor of %s', msg.key)
|
||||||
if (typeof msg.value.content === 'string') {
|
if (typeof msg.value.content === 'string') {
|
||||||
|
debug('private message')
|
||||||
// Private message we can't decrypt, stop looking for parents.
|
// Private message we can't decrypt, stop looking for parents.
|
||||||
return resolve(parents)
|
return resolve(parents)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof msg.value.content.fork === 'string') {
|
if (typeof msg.value.content.fork === 'string') {
|
||||||
// It's a message reply, get the parent!
|
debug('fork, get the parent')
|
||||||
const fork = await cooler.get(ssb.get, {
|
try {
|
||||||
id: msg.value.content.fork,
|
// It's a message reply, get the parent!
|
||||||
meta: true,
|
const fork = await cooler.get(ssb.get, {
|
||||||
private: true
|
id: msg.value.content.fork,
|
||||||
})
|
meta: true,
|
||||||
|
private: true
|
||||||
resolve(getRootAncestor(fork))
|
})
|
||||||
|
resolve(getRootAncestor(fork))
|
||||||
|
} catch (e) {
|
||||||
|
debug(e)
|
||||||
|
resolve(msg)
|
||||||
|
}
|
||||||
} else if (typeof msg.value.content.root === 'string') {
|
} else if (typeof msg.value.content.root === 'string') {
|
||||||
// It's a thread reply, get the parent!
|
debug('thread reply')
|
||||||
const root = await cooler.get(ssb.get, {
|
try {
|
||||||
id: msg.value.content.root,
|
// It's a thread reply, get the parent!
|
||||||
meta: true,
|
const root = await cooler.get(ssb.get, {
|
||||||
private: true
|
id: msg.value.content.root,
|
||||||
})
|
meta: true,
|
||||||
|
private: true
|
||||||
resolve(getRootAncestor(root))
|
})
|
||||||
|
resolve(getRootAncestor(root))
|
||||||
|
} catch (e) {
|
||||||
|
debug(e)
|
||||||
|
resolve(msg)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
debug('got root ancestor')
|
||||||
resolve(msg)
|
resolve(msg)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -288,10 +303,11 @@ module.exports = {
|
|||||||
resolve(deepReplies)
|
resolve(deepReplies)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
debug('about to get root ancestor')
|
||||||
const rootAncestor = await getRootAncestor(rawMsg)
|
const rootAncestor = await getRootAncestor(rawMsg)
|
||||||
|
debug('got root ancestors')
|
||||||
const deepReplies = await getDeepReplies(rootAncestor.key)
|
const deepReplies = await getDeepReplies(rootAncestor.key)
|
||||||
|
debug('got deep replies')
|
||||||
debug('deep replies: %O', deepReplies)
|
|
||||||
|
|
||||||
const allMessages = [rootAncestor, ...deepReplies].map(message => {
|
const allMessages = [rootAncestor, ...deepReplies].map(message => {
|
||||||
const isThreadTarget = message.key === msgId
|
const isThreadTarget = message.key === msgId
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
|
const debug = require('debug')('oasis:route-thread')
|
||||||
|
|
||||||
const listView = require('./views/list')
|
const listView = require('./views/list')
|
||||||
const post = require('./models/post')
|
const post = require('./models/post')
|
||||||
|
|
||||||
module.exports = async function thread (ctx) {
|
module.exports = async function thread (ctx) {
|
||||||
|
debug('called thread!')
|
||||||
const msgId = ctx.params.id
|
const msgId = ctx.params.id
|
||||||
|
debug('thread ID: %s', msgId)
|
||||||
const messages = await post.fromThread(msgId)
|
const messages = await post.fromThread(msgId)
|
||||||
|
debug('got %i messages', messages.length)
|
||||||
|
|
||||||
ctx.body = listView({ messages })
|
ctx.body = listView({ messages })
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user