Fix bug where private messages were liked publicly

This commit is contained in:
Christian Bundy
2019-08-12 14:13:49 -07:00
parent f610c17168
commit e87cbd37d0
4 changed files with 29 additions and 7 deletions

View File

@ -5,6 +5,7 @@
"backlinks", "backlinks",
"hyperaxe", "hyperaxe",
"msgs", "msgs",
"whoami" "whoami",
"recps"
] ]
} }

View File

@ -90,6 +90,8 @@ module.exports = (config) => {
}) })
.post('/like/:message', koaBody(), async (ctx) => { .post('/like/:message', koaBody(), async (ctx) => {
const { message } = ctx.params const { message } = ctx.params
// TODO: convert all so `message` is full message and `messageKey` is key
const messageKey = message
const voteValue = Number(ctx.request.body.voteValue) const voteValue = Number(ctx.request.body.voteValue)
@ -99,7 +101,7 @@ module.exports = (config) => {
} }
referer.hash = `centered-footer-${encoded.message}` referer.hash = `centered-footer-${encoded.message}`
ctx.body = await like({ message, voteValue }) ctx.body = await like({ messageKey, voteValue })
ctx.redirect(referer) ctx.redirect(referer)
}) })

View File

@ -1,7 +1,25 @@
'use strict' 'use strict'
const vote = require('./models/vote') const vote = require('./models/vote')
const post = require('./models/post')
module.exports = async function like ({ message, voteValue }) { module.exports = async function like ({ messageKey, voteValue }) {
const value = Number(voteValue) const value = Number(voteValue)
return vote.publish(message, value) const message = await post.get(messageKey)
const isPrivate = message.value.meta.private === true
const messageRecipients = isPrivate ? message.value.content.recps : []
const normalized = messageRecipients.map(recipient => {
if (typeof recipient === 'string') {
return recipient
}
if (typeof recipient.link === 'string') {
return recipient.link
}
})
const recipients = normalized.length > 0 ? normalized : undefined
return vote.publish({ messageKey, value, recps: recipients })
} }

View File

@ -2,14 +2,15 @@
const cooler = require('./lib/cooler') const cooler = require('./lib/cooler')
module.exports = { module.exports = {
publish: async (messageId, value) => { publish: async ({ messageKey, value, recps }) => {
const ssb = await cooler.connect() const ssb = await cooler.connect()
await cooler.get(ssb.publish, { await cooler.get(ssb.publish, {
type: 'vote', type: 'vote',
vote: { vote: {
link: messageId, link: messageKey,
value: Number(value) value: Number(value)
} },
recps
}) })
} }
} }