Files: 1d62bb3c81414c6538c54c367803bce3f2516a68 / modules_basic / follow.js
2689 bytesRaw
1 | const fs = require('fs') |
2 | const h = require('../h') |
3 | |
4 | //render a message when someone follows someone, |
5 | //so you see new users |
6 | function isRelated(value, name) { |
7 | return value ? name : value === false ? 'un'+name : '' |
8 | } |
9 | |
10 | exports.needs = { |
11 | avatar: 'first', |
12 | avatar_name: 'first', |
13 | avatar_link: 'first', |
14 | message_confirm: 'first', |
15 | follower_of: 'first' |
16 | } |
17 | |
18 | exports.gives = { |
19 | message_content: true, |
20 | message_content_mini: true, |
21 | avatar_action: true, |
22 | mcss: true |
23 | } |
24 | |
25 | exports.create = function (api) { |
26 | return { |
27 | message_content_mini, |
28 | message_content, |
29 | avatar_action, |
30 | mcss: () => fs.readFileSync(__filename.replace(/js$/, 'mcss'), 'utf8') |
31 | } |
32 | |
33 | function message_content_mini (msg) { |
34 | const { type, contact, following, blocking } = msg.value.content |
35 | if(type == 'contact' && contact) { |
36 | var relation = isRelated(following, 'follows') |
37 | if(blocking) relation = 'blocks' |
38 | return [ |
39 | relation, |
40 | ' ', |
41 | api.avatar_link(contact, api.avatar_name(contact), '') |
42 | ] |
43 | } |
44 | } |
45 | |
46 | function message_content (msg) { |
47 | const { type, contact, following, blocking } = msg.value.content |
48 | if(type == 'contact' && contact) { |
49 | var relation = isRelated(following, 'follows') |
50 | if(blocking) relation = 'blocks' |
51 | return h('div.contact', [ |
52 | relation, |
53 | api.avatar(contact, 'thumbnail') |
54 | ]) |
55 | } |
56 | } |
57 | |
58 | function avatar_action (id) { |
59 | var follows_you, you_follow |
60 | |
61 | var self_id = require('../keys').id |
62 | api.follower_of(self_id, id, (err, f) => { |
63 | you_follow = f || false |
64 | update() |
65 | }) |
66 | api.follower_of(id, self_id, (err, f) => { |
67 | follows_you = f || false |
68 | update() |
69 | }) |
70 | |
71 | var followBtn = h('button', { 'ev-click': toggleFollow }, 'loading') |
72 | var state = h('label', 'loading') |
73 | |
74 | function update () { |
75 | state.textContent = ( |
76 | follows_you && you_follow ? '- you are friends' |
77 | : follows_you ? '- they follow you' |
78 | : you_follow ? '- you are following' |
79 | : '' |
80 | ) |
81 | |
82 | // wait till finished loading before offering follow options |
83 | if (you_follow === undefined) return |
84 | followBtn.textContent = you_follow ? 'unfollow' : 'follow' |
85 | } |
86 | |
87 | return h('Follow', [ |
88 | followBtn, |
89 | state |
90 | ]) |
91 | |
92 | function toggleFollow () { |
93 | if (followBtn.textContent === 'loading') return |
94 | const msg = { |
95 | type: 'contact', |
96 | contact: id, |
97 | following: !you_follow |
98 | } |
99 | |
100 | api.message_confirm(msg, (err, msg) => { |
101 | if (err) return console.error(err) |
102 | |
103 | you_follow = !you_follow |
104 | update() |
105 | }) |
106 | } |
107 | |
108 | } |
109 | return exports |
110 | } |
111 |
Built with git-ssb-web