git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: 890435b264f902c0b56c9dd63ab300d7b56a8644

Files: 890435b264f902c0b56c9dd63ab300d7b56a8644 / modules / follow.js

2054 bytesRaw
1var h = require('hyperscript')
2var u = require('../util')
3var avatar = require('../plugs').first(exports.avatar = [])
4var pull = require('pull-stream')
5var plugs = require('../plugs')
6
7//render a message when someone follows someone,
8//so you see new users
9exports.message_content = function (msg) {
10
11 if(msg.value.content.type == 'contact' && msg.value.content.contact) {
12 return h('div.contact',
13 'follows',
14 avatar(msg.value.content.contact)
15 )
16 }
17}
18
19var sbot_links2 = plugs.first(exports.sbot_links2 = [])
20var sbot_whoami = plugs.first(exports.sbot_whoami = [])
21var message_confirm = plugs.first(exports.message_confirm = [])
22
23function follows (source, dest, cb) {
24 pull(
25 sbot_links2({query:[
26 {$filter: {
27 source: source,
28 dest: dest,
29 rel: ['contact', {$gt: null}]
30 }},
31 {$map: {
32 timestamp: 'timestamp', follows: ["rel", 1]
33 }}
34 ]}),
35 pull.collect(function (err, ary) {
36 if(err) return cb(err)
37 cb(null,
38 ary.length ? ary.sort(function (a, b) {
39 return a.timestamp - b.timestamp
40 }).pop().follows : false
41 )
42 })
43 )
44}
45
46exports.avatar_action = function (id) {
47 var follows_you, you_follow
48
49 sbot_whoami(function (err, me) {
50 follows(me.id, id, function (err, f) {
51 you_follow = f
52 update()
53 })
54 follows(id, me.id, function (err, f) {
55 follows_you = f
56 update()
57 })
58 })
59
60 var state = h('label')
61 var label = h('label')
62
63 function update () {
64 state.textContent = (
65 follows_you && you_follow ? 'friend'
66 : follows_you ? 'follows you'
67 : you_follow ? 'you follow'
68 : ''
69 )
70
71 label.textContent = you_follow ? 'unfollow' : 'follow'
72 }
73
74 return h('div', state,
75 h('a', {href:'#', onclick: function () {
76 message_confirm({
77 type: 'contact',
78 contact: id,
79 following: !you_follow
80 }, function (err) {
81 //TODO: update after following.
82 })
83 }}, h('br'), label)
84 )
85}
86
87
88
89

Built with git-ssb-web