git ssb

16+

Dominic / patchbay



Tree: 39a3fdd490156a44a50029332ebf5c046b40274a

Files: 39a3fdd490156a44a50029332ebf5c046b40274a / modules_basic / follow.js

2603 bytesRaw
1const fs = require('fs')
2const h = require('../h')
3
4//render a message when someone follows someone,
5//so you see new users
6function isRelated(value, name) {
7 return value ? name : value === false ? 'un'+name : ''
8}
9
10exports.needs = {
11 avatar: 'first',
12 avatar_name: 'first',
13 avatar_link: 'first',
14 message_confirm: 'first',
15 follower_of: 'first'
16}
17
18exports.gives = {
19 message_content: true,
20 message_content_mini: true,
21 avatar_action: true,
22 mcss: true
23}
24
25exports.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
64 update()
65 })
66 api.follower_of(id, self_id, (err, f) => {
67 follows_you = f
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 ? 'follows you'
78 : you_follow ? 'you follow'
79 : ''
80 )
81
82 if (you_follow === undefined) return
83 followBtn.textContent = you_follow ? 'unfollow' : 'follow'
84 }
85
86 return h('Follow', [
87 followBtn,
88 state
89 ])
90
91 function toggleFollow () {
92 if (followBtn.textContent === 'loading') return
93 const msg = {
94 type: 'contact',
95 contact: id,
96 following: !you_follow
97 }
98
99 api.message_confirm(msg, (err, msg) => {
100 if (err) return console.error(err)
101
102 you_follow = msg.value.content.following
103 update()
104 })
105 }
106
107 }
108 return exports
109}
110

Built with git-ssb-web