From 3c771da8ff66d21875e7c59a4e97df8c17f64630 Mon Sep 17 00:00:00 2001 From: Cecilia Lindskog Date: Tue, 26 Jan 2021 10:33:29 +0100 Subject: [PATCH 1/2] Issue 126: Show recipients of private messages --- src/models.js | 43 +++++++++++++++++++++++++++++++------------ src/views/index.js | 15 +++++++++++++++ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/models.js b/src/models.js index a0f20fb..92a0750 100644 --- a/src/models.js +++ b/src/models.js @@ -691,6 +691,26 @@ module.exports = ({ cooler, isPublic }) => { } }); }; + + const getUserInfo = async (feedId) => { + const id = feedId; + + const pendingName = models.about.name(feedId); + const pendingAvatarMsg = models.about.image(feedId); + + const pending = [pendingName, pendingAvatarMsg]; + const [name, avatarMsg] = await Promise.all(pending); + + const avatarId = + avatarMsg != null && typeof avatarMsg.link === "string" + ? avatarMsg.link || nullImage + : avatarMsg || nullImage; + + const avatarUrl = `/image/64/${encodeURIComponent(avatarId)}`; + + return { name, id, avatarId, avatarUrl }; + }; + const transform = (ssb, messages, myFeedId) => Promise.all( messages.map(async (msg) => { @@ -767,11 +787,9 @@ module.exports = ({ cooler, isPublic }) => { ); const voterNames = await Promise.all(pendingVoterNames); - const pendingName = models.about.name(msg.value.author); - const pendingAvatarMsg = models.about.image(msg.value.author); - - const pending = [pendingName, pendingAvatarMsg]; - const [name, avatarMsg] = await Promise.all(pending); + const { name, avatarId, avatarUrl } = await getUserInfo( + msg.value.author + ); if (isPublic) { const publicOptIn = await models.about.publicWebHosting( @@ -797,13 +815,6 @@ module.exports = ({ cooler, isPublic }) => { msg.value.content.text += `\n\n#${channel}`; } - const avatarId = - avatarMsg != null && typeof avatarMsg.link === "string" - ? avatarMsg.link || nullImage - : avatarMsg || nullImage; - - const avatarUrl = `/image/64/${encodeURIComponent(avatarId)}`; - const ts = new Date(msg.value.timestamp); let isoTs; @@ -840,6 +851,14 @@ module.exports = ({ cooler, isPublic }) => { lodash.set(msg, "value.meta.votes", voterNames); lodash.set(msg, "value.meta.voted", voters.includes(myFeedId)); + if (isPrivate(msg)) { + msg.value.meta.recpsInfo = await Promise.all( + msg.value.content.recps.map((feedId) => { + return getUserInfo(feedId); + }) + ); + } + return msg; }) ); diff --git a/src/views/index.js b/src/views/index.js index ea45233..df26d14 100644 --- a/src/views/index.js +++ b/src/views/index.js @@ -393,8 +393,22 @@ const post = ({ msg, aside = false }) => { const messageClasses = ["post"]; + const recps = []; + + const addRecps = (recpsInfo) => { + recpsInfo.forEach(function (recp) { + recps.push( + a( + { href: `/author/${encodeURIComponent(recp.id)}` }, + img({ class: "avatar", src: recp.avatarUrl, alt: "" }) + ) + ); + }); + }; + if (isPrivate) { messageClasses.push("private"); + addRecps(msg.value.meta.recpsInfo); } if (isThreadTarget) { @@ -449,6 +463,7 @@ const post = ({ msg, aside = false }) => { title: timeAbsolute, }, isPrivate ? "🔒" : null, + isPrivate ? recps : null, a({ href: url.link }, nbsp, timeAgo) ) ) From 29dcaad89c49126b5d0664b1066583c35e275edd Mon Sep 17 00:00:00 2001 From: Cecilia Lindskog Date: Wed, 27 Jan 2021 10:57:04 +0100 Subject: [PATCH 2/2] Fix issue with feedId --- src/models.js | 16 +++++++++++----- src/views/index.js | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/models.js b/src/models.js index 92a0750..42ad705 100644 --- a/src/models.js +++ b/src/models.js @@ -693,8 +693,6 @@ module.exports = ({ cooler, isPublic }) => { }; const getUserInfo = async (feedId) => { - const id = feedId; - const pendingName = models.about.name(feedId); const pendingAvatarMsg = models.about.image(feedId); @@ -708,9 +706,17 @@ module.exports = ({ cooler, isPublic }) => { const avatarUrl = `/image/64/${encodeURIComponent(avatarId)}`; - return { name, id, avatarId, avatarUrl }; + return { name, feedId, avatarId, avatarUrl }; }; + function getRecipientFeedId(recipient) { + if (typeof recipient === "string") { + return recipient; + } else { + return recipient.link; + } + } + const transform = (ssb, messages, myFeedId) => Promise.all( messages.map(async (msg) => { @@ -853,8 +859,8 @@ module.exports = ({ cooler, isPublic }) => { if (isPrivate(msg)) { msg.value.meta.recpsInfo = await Promise.all( - msg.value.content.recps.map((feedId) => { - return getUserInfo(feedId); + msg.value.content.recps.map((recipient) => { + return getUserInfo(getRecipientFeedId(recipient)); }) ); } diff --git a/src/views/index.js b/src/views/index.js index df26d14..d144ad6 100644 --- a/src/views/index.js +++ b/src/views/index.js @@ -399,7 +399,7 @@ const post = ({ msg, aside = false }) => { recpsInfo.forEach(function (recp) { recps.push( a( - { href: `/author/${encodeURIComponent(recp.id)}` }, + { href: `/author/${encodeURIComponent(recp.feedId)}` }, img({ class: "avatar", src: recp.avatarUrl, alt: "" }) ) );