From 398ebe927199780e70f8f3bffbfa6c8d9b51e289 Mon Sep 17 00:00:00 2001 From: Nick Wynja Date: Wed, 3 Mar 2021 12:47:28 -0500 Subject: [PATCH] Adds subscribed channels to Topics Any subscribed channels now appear at the top of Topics. This gives you easy access to view subscribed channels using the existing hashtags view. --- src/index.js | 7 ++++++- src/models.js | 43 +++++++++++++++++++++++++++++++++++++++++++ src/views/index.js | 3 ++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 65a7ebf..b8abd41 100755 --- a/src/index.js +++ b/src/index.js @@ -471,7 +471,12 @@ router }) .get("/public/latest/topics", async (ctx) => { const messages = await post.latestTopics(); - ctx.body = await topicsView({ messages }); + const channels = await post.channels(); + const list = channels.map((c) => { + return li(a({ href: `/hashtag/${c}` }, `#${c}`)); + }); + const prefix = nav(ul(list)); + ctx.body = await topicsView({ messages, prefix }); }) .get("/public/latest/summaries", async (ctx) => { const messages = await post.latestSummaries(); diff --git a/src/models.js b/src/models.js index 8992101..3a1fca5 100644 --- a/src/models.js +++ b/src/models.js @@ -1795,6 +1795,49 @@ module.exports = ({ cooler, isPublic }) => { return keys; }, + channels: async () => { + const ssb = await cooler.open(); + + const source = ssb.createUserStream({ id: ssb.id }); + + const messages = await new Promise((resolve, reject) => { + pull( + source, + pull.filter((message) => { + return lodash.get(message, "value.content.type") === "channel" + ? true + : false; + }), + pull.collect((err, collectedMessages) => { + if (err) { + reject(err); + } else { + resolve(transform(ssb, collectedMessages, ssb.id)); + } + }) + ); + }); + + const channels = messages.map((msg) => { + return { + channel: msg.value.content.channel, + subscribed: msg.value.content.subscribed, + }; + }); + + let subbedChannels = []; + + channels.forEach((ch) => { + if (ch.subscribed && !subbedChannels.includes(ch.channel)) { + subbedChannels.push(ch.channel); + } + if (ch.subscribed === false && subbedChannels.includes(ch.channel)) { + subbedChannels = lodash.pull(subbedChannels, ch.channel); + } + }); + + return subbedChannels; + }, inbox: async (customOptions = {}) => { const ssb = await cooler.open(); diff --git a/src/views/index.js b/src/views/index.js index dcd825c..1070d41 100644 --- a/src/views/index.js +++ b/src/views/index.js @@ -1225,11 +1225,12 @@ exports.latestView = ({ messages }) => { }); }; -exports.topicsView = ({ messages }) => { +exports.topicsView = ({ messages, prefix }) => { return messageListView({ messages, viewTitle: i18n.topics, viewDescription: i18n.topicsDescription, + viewElements: prefix, }); };