From 62bd34b5532dd54eafb75ccf13a49eae17c8cf08 Mon Sep 17 00:00:00 2001 From: Christian Bundy Date: Sat, 7 Dec 2019 14:58:08 -0800 Subject: [PATCH] Add relationship info to author pages --- docs/roadmap.md | 6 ++-- src/pages/author.js | 5 +++- src/pages/models/friend.js | 53 ++++++++++++++++++++++++++++++++++ src/pages/models/lib/cooler.js | 6 +++- src/pages/profile.js | 3 +- src/pages/views/author.js | 7 +++-- 6 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 src/pages/models/friend.js diff --git a/docs/roadmap.md b/docs/roadmap.md index 6ba5210..a2dae4b 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -1,12 +1,12 @@ # Roadmap - [ ] Get - - [ ] Author + - [x] Author - [x] Name - [x] Description - [x] Image - - [ ] Follow / unfollow (public) - - [ ] Block / unblock (public) + - [x] Follow / unfollow (public) + - [x] Block / unblock (public) - [x] Likes - [x] Metadata ("about" messages) - [x] Markdown mention diff --git a/src/pages/author.js b/src/pages/author.js index abad260..6e14b2a 100644 --- a/src/pages/author.js +++ b/src/pages/author.js @@ -2,6 +2,7 @@ const about = require('./models/about') const post = require('./models/post') +const friend = require('./models/friend') const authorView = require('./views/author') module.exports = async function authorPage (feedId) { @@ -10,6 +11,7 @@ module.exports = async function authorPage (feedId) { const image = await about.image(feedId) const aboutPairs = await about.all(feedId) const messages = await post.fromFeed(feedId) + const relationship = await friend.getRelationship(feedId) const avatarUrl = `/image/128/${encodeURIComponent(image)}` @@ -19,6 +21,7 @@ module.exports = async function authorPage (feedId) { name, description, avatarUrl, - aboutPairs + aboutPairs, + relationship }) } diff --git a/src/pages/models/friend.js b/src/pages/models/friend.js new file mode 100644 index 0000000..5b2ca71 --- /dev/null +++ b/src/pages/models/friend.js @@ -0,0 +1,53 @@ +'use strict' + +const cooler = require('./lib/cooler') + +module.exports = { + isFollowing: async (feedId) => { + const ssb = await cooler.connect() + const { id } = await cooler.get(ssb.whoami) + + const isFollowing = await cooler.get( + ssb.friends.isFollowing, + { + source: id, + dest: feedId + } + ) + return isFollowing + }, + getRelationship: async (feedId) => { + const ssb = await cooler.connect() + const { id } = await cooler.get(ssb.whoami) + + if (feedId === id) { + return 'this is you' + } + + const isFollowing = await cooler.get( + ssb.friends.isFollowing, + { + source: id, + dest: feedId + } + ) + + const isBlocking = await cooler.get( + ssb.friends.isBlocking, + { + source: id, + dest: feedId + } + ) + + if (isFollowing === true && isBlocking === false) { + return 'you are following' + } else if (isFollowing === false && isBlocking === true) { + return 'you are blocking' + } else if (isFollowing === false && isBlocking === false) { + return 'you are not following or blocking' + } else { + return 'you are following and blocking (!)' + } + } +} diff --git a/src/pages/models/lib/cooler.js b/src/pages/models/lib/cooler.js index 6f87c0c..9bf131a 100644 --- a/src/pages/models/lib/cooler.js +++ b/src/pages/models/lib/cooler.js @@ -31,7 +31,11 @@ const rawConnect = () => new Promise((resolve, reject) => { status: 'async', whoami: 'sync', tangle: { branch: 'async' }, - query: { read: 'source' } + query: { read: 'source' }, + friends: { + isFollowing: 'async', + isBlocking: 'async' + } } }, (err, api) => { if (err) { diff --git a/src/pages/profile.js b/src/pages/profile.js index 4e23b42..f6d3436 100644 --- a/src/pages/profile.js +++ b/src/pages/profile.js @@ -23,6 +23,7 @@ module.exports = async function profilePage () { name, description, avatarUrl, - aboutPairs + aboutPairs, + relationship: null }) } diff --git a/src/pages/views/author.js b/src/pages/views/author.js index 3e68952..33b8f67 100644 --- a/src/pages/views/author.js +++ b/src/pages/views/author.js @@ -10,6 +10,7 @@ const { img, pre, section, + span, table, tbody, td, @@ -27,7 +28,8 @@ module.exports = ({ description, feedId, messages, - name + name, + relationship }) => { const mention = `[@${name}](${feedId})` const markdownMention = highlightJs.highlight('markdown', mention).value @@ -63,7 +65,8 @@ module.exports = ({ : null, metaTable, footer( - a({ href: `/likes/${encodeURIComponent(feedId)}` }, 'view likes') + a({ href: `/likes/${encodeURIComponent(feedId)}` }, 'view likes'), + span(relationship) ) )