git ssb

16+

Dominic / patchbay



Tree: e33a98abcc85d3a3b35c0b2db5b98200b0614954

Files: e33a98abcc85d3a3b35c0b2db5b98200b0614954 / modules_basic / follow.js

2711 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('span')
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
84 return h('div', state,
85 h('a', {href:'#', onclick: function () {
86 api.message_confirm({
87 type: 'contact',
88 contact: id,
89 following: !you_follow
90 }, function (err, msg) {
91 if (err) return console.error(err)
92 you_follow = msg.value.content.following
93 update()
94 })
95 }}, h('br'), label)
96 )
97 }
98 return exports
99}
100

Built with git-ssb-web