git ssb

0+

ev / microbay



forked from Dominic / patchbay

Tree: cac493633ab8c5ac065aecbac0779e1a94abe6b3

Files: cac493633ab8c5ac065aecbac0779e1a94abe6b3 / modules / follow.js

2211 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
9function isRelated(value, name) {
10 return value ? name : value === false ? 'un'+name : ''
11}
12
13exports.message_content = function (msg) {
14
15 var content = msg.value.content
16 if(content.type == 'contact' && content.contact) {
17 var relation = isRelated(content.following, 'follows')
18 if(content.blocking) relation = 'blocks'
19 return h('div.contact', relation, avatar(msg.value.content.contact, 'thumbnail'))
20 }
21}
22
23var sbot_links2 = plugs.first(exports.sbot_links2 = [])
24var message_confirm = plugs.first(exports.message_confirm = [])
25
26function follows (source, dest, cb) {
27 pull(
28 sbot_links2({query:[
29 {$filter: {
30 source: source,
31 dest: dest,
32 rel: ['contact', {$gt: null}]
33 }},
34 {$map: {
35 timestamp: 'timestamp', follows: ["rel", 1]
36 }}
37 ]}),
38 pull.collect(function (err, ary) {
39 if(err) return cb(err)
40 cb(null,
41 ary.length ? ary.sort(function (a, b) {
42 return a.timestamp - b.timestamp
43 }).pop().follows : false
44 )
45 })
46 )
47}
48
49exports.avatar_action = function (id) {
50 var follows_you, you_follow
51
52 var self_id = require('../keys').id
53 follows(self_id, id, function (err, f) {
54 you_follow = f
55 update()
56 })
57 follows(id, self_id, function (err, f) {
58 follows_you = f
59 update()
60 })
61
62 var state = h('label')
63 var label = h('label')
64
65 function update () {
66 state.textContent = (
67 follows_you && you_follow ? 'friend'
68 : follows_you ? 'follows you'
69 : you_follow ? 'you follow'
70 : ''
71 )
72
73 label.textContent = you_follow ? 'unfollow' : 'follow'
74 }
75
76 return h('div', state,
77 h('a', {href:'#', onclick: function () {
78 message_confirm({
79 type: 'contact',
80 contact: id,
81 following: !you_follow
82 }, function (err) {
83 //TODO: update after following.
84 })
85 }}, h('br'), label)
86 )
87}
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

Built with git-ssb-web