git ssb

16+

Dominic / patchbay



Tree: 8645df8d5c4edf3b8fe98b8e3b257e80ab48d09d

Files: 8645df8d5c4edf3b8fe98b8e3b257e80ab48d09d / modules_basic / follow.js

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

Built with git-ssb-web