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 e7f0885..2e5dbac 100644 --- a/src/models.js +++ b/src/models.js @@ -1800,6 +1800,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 c033856..ac0a114 100644 --- a/src/views/index.js +++ b/src/views/index.js @@ -1243,11 +1243,12 @@ exports.latestView = ({ messages }) => { }); }; -exports.topicsView = ({ messages }) => { +exports.topicsView = ({ messages, prefix }) => { return messageListView({ messages, viewTitle: i18n.topics, viewDescription: i18n.topicsDescription, + viewElements: prefix, }); };