git ssb

0+

ev / minbase



Tree: b62ecd6f843b26f37e70c7b6d65c71db35bbf922

Files: b62ecd6f843b26f37e70c7b6d65c71db35bbf922 / modules / follow.js

2369 bytesRaw
1var h = require('hyperscript')
2var u = require('../util')
3var pull = require('pull-stream')
4
5//render a message when someone follows someone,
6//so you see new users
7function isRelated(value, name) {
8 return value ? name : value === false ? 'un'+name : ''
9}
10
11exports.needs = {
12 avatar: 'first',
13 avatar_name: 'first',
14 avatar_link: 'first',
15 message_confirm: 'first',
16 follower_of: 'first'
17}
18
19exports.gives = {
20 message_content: true,
21 message_content_mini: true,
22 avatar_action: true,
23}
24
25exports.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 ? 'Follower'
70 : you_follow ? 'Following'
71 : ''
72 )
73
74 label.textContent = you_follow ? 'Unfollow' : 'Follow'
75 }
76
77 return h('a', {href:'#', onclick: function () {
78 api.message_confirm({
79 type: 'contact',
80 contact: id,
81 following: !you_follow
82 }, function (err, msg) {
83 if (err) return console.error(err)
84 you_follow = msg.value.content.following
85 update()
86 })
87 }}, h('br'), h('button', label))
88 }
89 return exports
90}
91

Built with git-ssb-web