From a755d08d1cdfde6a9a48de38d8fdd779148709b8 Mon Sep 17 00:00:00 2001 From: cel Date: Wed, 12 Apr 2017 17:53:27 -0700 Subject: Add (un)follow button --- lib/app.js | 15 +++++++++++++++ lib/serve.js | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/app.js b/lib/app.js index 6cc71a0..465e45b 100644 --- a/lib/app.js +++ b/lib/app.js @@ -201,6 +201,21 @@ App.prototype.streamPeers = function (opts) { }) } +App.prototype.getFollow = function (source, dest, cb) { + var self = this + pull( + self.sbot.links({source: source, dest: dest, rel: 'contact', values: true}), + pull.filter(function (msg) { + var c = msg && msg.value && msg.value.content + return c && c.type === 'contact' + }), + pull.reduce(function (doesFollow, msg) { + var c = msg.value.content + return !!c.following + }, false, cb) + ) +} + App.prototype.getFollows = function (id, cb) { var self = this pull( diff --git a/lib/serve.js b/lib/serve.js index ec63b59..9137bc5 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -116,6 +116,7 @@ Serve.prototype.go = function () { if (err) next(err) else if (data.action === 'publish') self.publishJSON(next) else if (data.action === 'vote') self.publishVote(next) + else if (data.action === 'contact') self.publishContact(next) else next() } @@ -152,6 +153,15 @@ Serve.prototype.publishVote = function (cb) { this.publish(content, cb) } +Serve.prototype.publishContact = function (cb) { + var content = { + type: 'contact', + contact: this.data.contact, + following: !!this.data.following + } + this.publish(content, cb) +} + Serve.prototype.publish = function (content, cb) { var self = this var done = multicb({pluck: 1, spread: true}) @@ -827,14 +837,32 @@ Serve.prototype.friendList = function (friends, cb) { return list } +var relationships = [ + '', + 'followed', + 'follows you', + 'friend' +] + +var relationshipActions = [ + 'follow', + 'unfollow', + 'follow back', + 'unfriend' +] + Serve.prototype.wrapUserFeed = function (isScrolled, id) { var self = this + var myId = self.app.sbot.id return u.hyperwrap(function (thread, cb) { var done = multicb({pluck: 1, spread: true}) self.app.getAbout(id, done()) + self.app.getFollow(myId, id, done()) + self.app.getFollow(id, myId, done()) if (!isScrolled) self.app.getFriendInfo(id, done()) - done(function (err, about, info) { + done(function (err, about, weFollowThem, theyFollowUs, info) { if (err) return cb(err) + var relationshipI = weFollowThem | theyFollowUs<<1 var done = multicb({pluck: 1, spread: true}) done()(null, [ h('section.ssb-feed', @@ -848,6 +876,18 @@ Serve.prototype.wrapUserFeed = function (isScrolled, id) { {innerHTML: self.app.render.markdown(about.description)}) : '' )), isScrolled ? '' : [ + id === myId ? '' : h('tr', + h('td'), + h('td.follow-info', h('form', {action: '', method: 'post'}, + relationships[relationshipI], ' ', + h('input', {type: 'hidden', name: 'action', value: 'contact'}), + h('input', {type: 'hidden', name: 'contact', value: id}), + h('input', {type: 'hidden', name: 'following', + value: weFollowThem ? '' : 'following'}), + h('input', {type: 'submit', + value: relationshipActions[relationshipI]}) + )) + ), h('tr', h('td', 'friends:'), h('td', self.friendList(info.friends, done())) -- cgit v1.2.3