git ssb

16+

Dominic / patchbay



Tree: ca58f0e7a14d980e607b0d0886ec4b0ef76b9dc1

Files: ca58f0e7a14d980e607b0d0886ec4b0ef76b9dc1 / modules / follow.js

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

Built with git-ssb-web