Add thread schema module to confirm assumptions about replies

This commit is contained in:
Christian Bundy 2019-09-28 15:53:23 -07:00
parent 4fd95918c9
commit fd55969db9
No known key found for this signature in database
GPG Key ID: EB541AAEF4366237
6 changed files with 57 additions and 13 deletions

View File

@ -26,6 +26,7 @@
"ssb-mentions": "^0.5.0",
"ssb-msgs": "^5.2.0",
"ssb-ref": "^2.13.9",
"ssb-thread-schema": "^1.0.3",
"yargs": "14.0.0"
},
"bin": {

View File

@ -334,8 +334,6 @@ summary {
-webkit-user-select: all;
user-select: all;
background: none;
margin: 0;
padding: 0;
border: 0;
font-size: 50%;
text-align: center;
}

View File

@ -3,12 +3,12 @@
const lodash = require('lodash')
const pull = require('pull-stream')
const prettyMs = require('pretty-ms')
const { isRoot, isNestedReply, isReply } = require('ssb-thread-schema')
const debug = require('debug')('oasis:model-post')
const cooler = require('./lib/cooler')
const configure = require('./lib/configure')
const markdown = require('./lib/markdown')
const { isMsg } = require('ssb-ref')
const getMessages = async ({ myFeedId, customOptions, ssb, query }) => {
const options = configure({ query, index: 'DTA' }, customOptions)
@ -272,7 +272,11 @@ const post = {
resolve(parents)
}
if (typeof msg.value.content.fork === 'string' && isMsg(msg.value.content.fork)) {
if (msg.value.content.type !== 'post') {
resolve(msg)
}
if (isNestedReply(msg)) {
debug('fork, get the parent')
try {
// It's a message reply, get the parent!
@ -287,7 +291,7 @@ const post = {
debug(e)
resolve(msg)
}
} else if (typeof msg.value.content.root === 'string' && isMsg(msg.value.content.root)) {
} else if (isReply(msg)) {
debug('thread reply: %s', msg.value.content.root)
try {
// It's a thread reply, get the parent!
@ -302,9 +306,14 @@ const post = {
debug(e)
resolve(msg)
}
} else {
} else if (isRoot(msg)) {
debug('got root ancestor')
resolve(msg)
} else {
// type !== "post", probably
// this should show up as JSON
debug('got mysterious root ancestor')
resolve(msg)
}
}
})
@ -426,14 +435,29 @@ const post = {
return cooler.get(ssb.publish, body)
},
reply: async ({ parent, message }) => {
message.root = parent
message.branch = parent
message.root = parent.key
message.fork = lodash.get(parent, 'value.content.root')
message.branch = await post.branch({ root: parent.key })
message.type = 'post' // redundant but used for validation
if (isNestedReply(message) !== true) {
const messageString = JSON.stringify(message, null, 2)
throw new Error(`message should be valid reply: ${messageString}`)
}
return post.publish(message)
},
replyAll: async ({ parent, message }) => {
message.root = lodash.get(parent, 'value.content.root', parent.key)
const fork = parent.key
const root = lodash.get(parent, 'value.content.root', parent.key)
message.root = fork || root
message.branch = await post.branch({ root: parent.key })
message.type = 'post' // redundant but used for validation
if (isReply(message) !== true) {
const messageString = JSON.stringify(message, null, 2)
throw new Error(`message should be valid replyAll: ${messageString}`)
}
return post.publish(message)
},

View File

@ -7,8 +7,9 @@ module.exports = async function publishReplyPage ({ message, text }) {
// TODO: rename `message` to `parent` or `ancestor` or similar
const mentions = ssbMentions(text) || undefined
const parent = await post.get(message)
return post.reply({
parent: message,
parent,
message: { text, mentions }
})
}

View File

@ -10,7 +10,6 @@ module.exports = async function replyPage (parentId) {
const messages = [parentMessage]
const hasRoot = typeof parentMessage.value.content.root === 'string' && ssbRef.isMsg(parentMessage.value.content.root)
console.log('got root ', parentMessage.value.content)
if (hasRoot) {
messages.push(await post.get(parentMessage.value.content.root))
}

View File

@ -6718,6 +6718,11 @@ ssb-mentions@^0.5.0:
ssb-marked "^0.7.0"
ssb-ref "^2.11.0"
ssb-msg-content@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/ssb-msg-content/-/ssb-msg-content-1.0.1.tgz#08d61181ea2b4676185ed0ae7062a32e67773dfa"
integrity sha512-M6W0Ef+jif829USmGvh6XeS4lYb/F2lgFhfEoCE/md7ESILNOGidp8frJE2uVOzSr2wVRA265tPrnVb7rYHkug==
ssb-msgs@^5.0.0, ssb-msgs@^5.2.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/ssb-msgs/-/ssb-msgs-5.2.0.tgz#c681da5cd70c574c922dca4f03c521538135c243"
@ -6781,7 +6786,7 @@ ssb-query@^2.4.3:
flumeview-query "^7.0.0"
pull-stream "^3.6.2"
ssb-ref@^2.0.0, ssb-ref@^2.11.0, ssb-ref@^2.12.0, ssb-ref@^2.13.3, ssb-ref@^2.13.9, ssb-ref@^2.3.0, ssb-ref@^2.6.2, ssb-ref@^2.7.1, ssb-ref@^2.9.0, ssb-ref@^2.9.1, ssb-ref@~2.13.9:
ssb-ref@^2.0.0, ssb-ref@^2.11.0, ssb-ref@^2.12.0, ssb-ref@^2.13.3, ssb-ref@^2.13.6, ssb-ref@^2.13.9, ssb-ref@^2.3.0, ssb-ref@^2.6.2, ssb-ref@^2.7.1, ssb-ref@^2.9.0, ssb-ref@^2.9.1, ssb-ref@~2.13.9:
version "2.13.9"
resolved "https://registry.yarnpkg.com/ssb-ref/-/ssb-ref-2.13.9.tgz#1de8c5b4f12e5b743be95705656ad2706c0e3cd0"
integrity sha512-TfatNqLvoP+eW/pMIbCmNcaoDq4R2k8jCtWkwDKx4AtluN/LwtyP931d5Mh+2gmzA04W7kxkr6f5ENGgdadMYg==
@ -6807,6 +6812,13 @@ ssb-replicate@^1.3.0:
pull-stream "^3.6.9"
ssb-ref "^2.13.9"
ssb-schema-definitions@^1.1.5:
version "1.2.0"
resolved "https://registry.yarnpkg.com/ssb-schema-definitions/-/ssb-schema-definitions-1.2.0.tgz#13325886c1e00398956bb0b269e0d30e43acc55f"
integrity sha512-/7wTiguWX26c5n14lm2SpDkIgPPoyQ2AGAhX/3mnJXVgBrI1wcWiCI8YubSH6zQkllbLFj+pCUuYQHrmMxNRqw==
dependencies:
ssb-ref "^2.13.6"
ssb-social-index@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ssb-social-index/-/ssb-social-index-1.0.0.tgz#308ca27e20f3afe023332b4f0ce1fe2a30c8baab"
@ -6834,6 +6846,15 @@ ssb-tangle@^1.0.1:
ssb-ref "^2.13.9"
ssb-sort "^1.1.3"
ssb-thread-schema@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/ssb-thread-schema/-/ssb-thread-schema-1.0.3.tgz#55f6b026babfeb9a872dddf13a51c2a9bd89cb7f"
integrity sha512-pq2hIeyZdRNjJpCCp6tONVIJezyMWtmIYCnbp3fP6L1w2/qnzUtTqYr9jvw+KpFJzuprN1BwyL5Kri57slMekg==
dependencies:
is-my-json-valid "^2.20.0"
ssb-msg-content "^1.0.1"
ssb-schema-definitions "^1.1.5"
ssb-typescript@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/ssb-typescript/-/ssb-typescript-1.4.0.tgz#9bdb9d45e269ebdb5c2e346c6bd23852782424f4"