git ssb

16+

Dominic / patchbay



Tree: 5574f0c5ea6c0534e2b1485fe6526447bf39a9cc

Files: 5574f0c5ea6c0534e2b1485fe6526447bf39a9cc / contact / html / relationships.js

2854 bytesRaw
1const nest = require('depnest')
2const { h, map, computed, when } = require('mutant')
3
4exports.gives = nest('contact.html.relationships')
5
6exports.needs = nest({
7 about: {
8 'html.image': 'first',
9 'obs.name': 'first'
10 },
11 contact: {
12 async: {
13 follow: 'first',
14 unfollow: 'first'
15 },
16 obs: {
17 followers: 'first',
18 following: 'first'
19 }
20 },
21 'keys.sync.id': 'first'
22})
23
24exports.create = function (api) {
25 return nest({
26 'contact.html.relationships': relationships
27 })
28
29 function relationships (id) {
30 var rawFollowing = api.contact.obs.following(id)
31 var rawFollowers = api.contact.obs.followers(id)
32
33 var friends = computed([rawFollowing, rawFollowers], (following, followers) => {
34 return [...following].filter(follow => followers.has(follow))
35 })
36 var following = computed([rawFollowing, friends], (following, friends) => {
37 return [...following].filter(follow => !friends.includes(follow))
38 })
39 var followers = computed([rawFollowers, friends], (followers, friends) => {
40 return [...followers].filter(follower => !friends.includes(follower))
41 })
42
43 var myId = api.keys.sync.id()
44 var ImFollowing = api.contact.obs.following(myId)
45 var IFollowThem = computed([ImFollowing], ImFollowing => ImFollowing.has(id))
46 var theyFollowMe = computed([rawFollowing], following => following.has(myId))
47
48 var relationshipStatus = computed([IFollowThem, theyFollowMe], (IFollowThem, theyFollowMe) => {
49 return IFollowThem && theyFollowMe ? '- you are friends'
50 : IFollowThem ? '- you follow them'
51 : theyFollowMe ? '- they follow you'
52 : ''
53 })
54
55 function imageLink (id) {
56 return h('a',
57 { href: id, title: computed(api.about.obs.name(id), name => '@' + name) },
58 api.about.html.image(id)
59 )
60 }
61
62 return h('Relationships', [
63 h('header', 'Relationships'),
64 when(id !== myId,
65 h('div.your-status', [
66 h('header', 'Your status'),
67 h('section.action', [
68 when(ImFollowing.sync,
69 when(IFollowThem,
70 h('button', { 'ev-click': () => api.contact.async.unfollow(id) }, 'Unfollow'),
71 h('button', { 'ev-click': () => api.contact.async.follow(id) }, 'Follow')
72 ),
73 h('button', { disabled: 'disabled' }, 'Loading...')
74 )
75 ]),
76 when(ImFollowing.sync, h('section.status', relationshipStatus))
77 ])
78 ),
79 h('div.friends', [
80 h('header', 'Friends'),
81 h('section', map(friends, imageLink))
82 ]),
83 h('div.follows', [
84 h('header', 'Follows'),
85 h('section', map(following, imageLink))
86 ]),
87 h('div.followers', [
88 h('header', 'Followers'),
89 h('section', map(followers, imageLink))
90 ])
91 ])
92 }
93}
94
95

Built with git-ssb-web