Files: 6712e94224526c0480342ad6ae1f205266d341a3 / app / html / context.js
5127 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, computed, map, when, Dict, dictToCollection, Array: MutantArray, resolve } = require('mutant') |
3 | const pull = require('pull-stream') |
4 | const next = require('pull-next-step') |
5 | const get = require('lodash/get') |
6 | const isEmpty = require('lodash/isEmpty') |
7 | |
8 | exports.gives = nest('app.html.context') |
9 | |
10 | exports.needs = nest({ |
11 | 'about.html.image': 'first', |
12 | 'about.obs.name': 'first', |
13 | 'feed.pull.private': 'first', |
14 | 'feed.pull.rollup': 'first', |
15 | 'keys.sync.id': 'first', |
16 | 'history.sync.push': 'first', |
17 | 'message.html.subject': 'first', |
18 | 'sbot.obs.localPeers': 'first', |
19 | 'translations.sync.strings': 'first', |
20 | }) |
21 | |
22 | |
23 | exports.create = (api) => { |
24 | return nest('app.html.context', (location) => { |
25 | |
26 | const strings = api.translations.sync.strings() |
27 | const myKey = api.keys.sync.id() |
28 | |
29 | var nearby = api.sbot.obs.localPeers() |
30 | var recentPeersContacted = Dict() |
31 | // TODO - extract as contact.obs.recentPrivate or something |
32 | |
33 | pull( |
34 | next(api.feed.pull.private, {reverse: true, limit: 100, live: false}, ['value', 'timestamp']), |
35 | pull.filter(msg => msg.value.content.recps), |
36 | pull.filter(msg => msg.value.content.type === 'post'), // TODO is this the best way to protect against votes? |
37 | pull.drain(msg => { |
38 | msg.value.content.recps |
39 | .map(recp => typeof recp === 'object' ? recp.link : recp) |
40 | .filter(recp => recp != myKey) |
41 | .forEach(recp => { |
42 | if (recentPeersContacted.has(recp)) return |
43 | |
44 | recentPeersContacted.put(recp, msg) |
45 | }) |
46 | }) |
47 | ) |
48 | |
49 | return h('Context -feed', [ |
50 | LevelOneContext(), |
51 | LevelTwoContext() |
52 | ]) |
53 | |
54 | function LevelOneContext () { |
55 | |
56 | return h('div.level.-one', [ |
57 | // Nearby |
58 | computed(nearby, n => !isEmpty(n) ? h('header', strings.peopleNearby) : null), |
59 | map(nearby, feedId => Option({ |
60 | notifications: Math.random() > 0.7 ? Math.floor(Math.random()*9+1) : 0, // TODO |
61 | imageEl: api.about.html.image(feedId), // TODO make avatar |
62 | label: api.about.obs.name(feedId), |
63 | selected: location.feed === feedId, |
64 | location: computed(recentPeersContacted, recent => { |
65 | const lastMsg = recent[feedId] |
66 | return lastMsg |
67 | ? Object.assign(lastMsg, { feed: feedId }) |
68 | : { page: 'threadNew', feed: feedId } |
69 | }), |
70 | })), |
71 | computed(nearby, n => !isEmpty(n) ? h('hr') : null), |
72 | |
73 | // Discover |
74 | Option({ |
75 | notifications: Math.floor(Math.random()*5+1), |
76 | imageEl: h('i.fa.fa-binoculars'), |
77 | label: strings.blogIndex.title, |
78 | selected: ['blogIndex', 'home'].includes(location.page), |
79 | location: { page: 'blogIndex' }, |
80 | }), |
81 | |
82 | // Recent Messages |
83 | map(dictToCollection(recentPeersContacted), ({ key, value }) => { |
84 | const feedId = key() |
85 | const lastMsg = value() |
86 | if (nearby.has(feedId)) return |
87 | |
88 | return Option({ |
89 | notifications: Math.random() > 0.7 ? Math.floor(Math.random()*9+1) : 0, // TODO |
90 | imageEl: api.about.html.image(feedId), // TODO make avatar |
91 | label: api.about.obs.name(feedId), |
92 | selected: location.feed === feedId, |
93 | location: Object.assign(lastMsg, { feed: feedId }) // TODO make obs? |
94 | }) |
95 | }) |
96 | ]) |
97 | } |
98 | |
99 | function LevelTwoContext () { |
100 | const { key, value, feed: targetUser, page } = location |
101 | const root = get(value, 'content.root', key) |
102 | if (!targetUser) return |
103 | |
104 | var threads = MutantArray() |
105 | |
106 | pull( |
107 | next(api.feed.pull.private, {reverse: true, limit: 100, live: false}, ['value', 'timestamp']), |
108 | pull.filter(msg => msg.value.content.recps), |
109 | pull.filter(msg => msg.value.content.recps |
110 | .map(recp => typeof recp === 'object' ? recp.link : recp) |
111 | .some(recp => recp === targetUser) |
112 | ), |
113 | api.feed.pull.rollup(), |
114 | pull.drain(thread => threads.push(thread)) |
115 | ) |
116 | |
117 | return h('div.level.-two', [ |
118 | Option({ |
119 | selected: page === 'threadNew', |
120 | location: {page: 'threadNew', feed: targetUser}, |
121 | label: h('Button', strings.threadNew.action.new), |
122 | }), |
123 | map(threads, thread => { |
124 | return Option({ |
125 | label: api.message.html.subject(thread), |
126 | selected: thread.key === root, |
127 | location: Object.assign(thread, { feed: targetUser }), |
128 | }) |
129 | }) |
130 | ]) |
131 | } |
132 | |
133 | function Option ({ notifications = 0, imageEl, label, location, selected }) { |
134 | const className = selected ? '-selected' : '' |
135 | const goToLocation = () => { |
136 | api.history.sync.push(resolve(location)) |
137 | } |
138 | |
139 | if (!imageEl) { |
140 | return h('Option', { className, 'ev-click': goToLocation }, [ |
141 | h('div.label', label) |
142 | ]) |
143 | } |
144 | |
145 | return h('Option', { className }, [ |
146 | h('div.circle', [ |
147 | when(notifications, h('div.alert', notifications)), |
148 | imageEl |
149 | ]), |
150 | h('div.label', { 'ev-click': goToLocation }, label) |
151 | ]) |
152 | } |
153 | }) |
154 | } |
155 | |
156 |
Built with git-ssb-web