diff --git a/.cspell.json b/.cspell.json index ad3c9fe..cf04d5f 100644 --- a/.cspell.json +++ b/.cspell.json @@ -5,6 +5,7 @@ "backlinks", "hyperaxe", "msgs", - "whoami" + "whoami", + "recps" ] } diff --git a/src/app.js b/src/app.js index 0a2fadf..4956497 100644 --- a/src/app.js +++ b/src/app.js @@ -90,6 +90,8 @@ module.exports = (config) => { }) .post('/like/:message', koaBody(), async (ctx) => { 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) @@ -99,7 +101,7 @@ module.exports = (config) => { } referer.hash = `centered-footer-${encoded.message}` - ctx.body = await like({ message, voteValue }) + ctx.body = await like({ messageKey, voteValue }) ctx.redirect(referer) }) diff --git a/src/pages/like.js b/src/pages/like.js index 59bb366..435350f 100644 --- a/src/pages/like.js +++ b/src/pages/like.js @@ -1,7 +1,25 @@ 'use strict' 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) - 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 }) } diff --git a/src/pages/models/vote.js b/src/pages/models/vote.js index e34c636..578abf8 100644 --- a/src/pages/models/vote.js +++ b/src/pages/models/vote.js @@ -2,14 +2,15 @@ const cooler = require('./lib/cooler') module.exports = { - publish: async (messageId, value) => { + publish: async ({ messageKey, value, recps }) => { const ssb = await cooler.connect() await cooler.get(ssb.publish, { type: 'vote', vote: { - link: messageId, + link: messageKey, value: Number(value) - } + }, + recps }) } }