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