Files: 010c28ba45ecc8a029b813a3cd9e7edc8e098059 / modules_basic / follow.js
2388 bytesRaw
1 | var h = require('hyperscript') |
2 | var u = require('../util') |
3 | var pull = require('pull-stream') |
4 | |
5 | //render a message when someone follows someone, |
6 | //so you see new users |
7 | function isRelated(value, name) { |
8 | return value ? name : value === false ? 'un'+name : '' |
9 | } |
10 | |
11 | exports.needs = { |
12 | avatar: 'first', |
13 | avatar_name: 'first', |
14 | avatar_link: 'first', |
15 | message_confirm: 'first', |
16 | follower_of: 'first' |
17 | } |
18 | |
19 | exports.gives = { |
20 | message_content: true, |
21 | message_content_mini: true, |
22 | avatar_action: true, |
23 | } |
24 | |
25 | exports.create = function (api) { |
26 | var exports = {} |
27 | exports.message_content = |
28 | exports.message_content_mini = function (msg) { |
29 | var content = msg.value.content |
30 | if(content.type == 'contact' && content.contact) { |
31 | var relation = isRelated(content.following, 'follows') |
32 | if(content.blocking) relation = 'blocks' |
33 | return [ |
34 | relation, ' ', |
35 | api.avatar_link(content.contact, api.avatar_name(content.contact), '') |
36 | ] |
37 | } |
38 | } |
39 | |
40 | exports.message_content = function (msg) { |
41 | |
42 | var content = msg.value.content |
43 | if(content.type == 'contact' && content.contact) { |
44 | var relation = isRelated(content.following, 'follows') |
45 | if(content.blocking) relation = 'blocks' |
46 | return h('div.contact', relation, api.avatar(msg.value.content.contact, 'thumbnail')) |
47 | } |
48 | } |
49 | |
50 | exports.avatar_action = function (id) { |
51 | var follows_you, you_follow |
52 | |
53 | var self_id = require('../keys').id |
54 | api.follower_of(self_id, id, function (err, f) { |
55 | you_follow = f |
56 | update() |
57 | }) |
58 | api.follower_of(id, self_id, function (err, f) { |
59 | follows_you = f |
60 | update() |
61 | }) |
62 | |
63 | var state = h('label') |
64 | var label = h('span') |
65 | |
66 | function update () { |
67 | state.textContent = ( |
68 | follows_you && you_follow ? 'friend' |
69 | : follows_you ? 'follows you' |
70 | : you_follow ? 'you follow' |
71 | : '' |
72 | ) |
73 | |
74 | label.textContent = you_follow ? 'unfollow' : 'follow' |
75 | } |
76 | |
77 | return h('div', state, |
78 | h('a', {href:'#', onclick: function () { |
79 | api.message_confirm({ |
80 | type: 'contact', |
81 | contact: id, |
82 | following: !you_follow |
83 | }, function (err, msg) { |
84 | if (err) return console.error(err) |
85 | you_follow = msg.value.content.following |
86 | update() |
87 | }) |
88 | }}, h('br'), label) |
89 | ) |
90 | } |
91 | return exports |
92 | } |
93 |
Built with git-ssb-web