git ssb

16+

Dominic / patchbay



Tree: ebf65e8c8947bcad79b243e4d8cd5674ae1b6777

Files: ebf65e8c8947bcad79b243e4d8cd5674ae1b6777 / modules_basic / follow.js

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

Built with git-ssb-web