Merge pull request #626 from nickwynja/subscribed-channels

Adds subscribed channels to Topics
This commit is contained in:
Christian Bundy 2021-03-07 13:01:56 -08:00 committed by GitHub
commit 4c576a51c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 2 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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,
});
};