Files: 325cc232dd44d379082757ecd6a63f9305c81f9f / app / html / context.js
8896 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, computed, map, when, Dict, Array: MutantArray, Value, Set, 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 | 'app.html.scroller': 'first', |
12 | 'about.html.avatar': 'first', |
13 | 'about.obs.name': 'first', |
14 | 'feed.pull.private': 'first', |
15 | 'feed.pull.rollup': 'first', |
16 | 'feed.pull.public': 'first', |
17 | 'keys.sync.id': 'first', |
18 | 'history.sync.push': 'first', |
19 | 'message.html.subject': 'first', |
20 | 'sbot.obs.localPeers': 'first', |
21 | 'translations.sync.strings': 'first', |
22 | 'unread.sync.isUnread': 'first' |
23 | }) |
24 | |
25 | exports.create = (api) => { |
26 | var recentMsgCache = MutantArray() |
27 | var usersLastMsgCache = Dict() // { id: [ msgs ] } |
28 | var usersUnreadMsgsCache = Dict() // { id: [ msgs ] } |
29 | |
30 | return nest('app.html.context', context) |
31 | |
32 | function context (location) { |
33 | const strings = api.translations.sync.strings() |
34 | const myKey = api.keys.sync.id() |
35 | |
36 | var nearby = api.sbot.obs.localPeers() |
37 | |
38 | // Unread message counts |
39 | const updateUserUnreadMsgsCache = (msg) => { |
40 | var cache = getUserUnreadMsgsCache(msg.value.author) |
41 | |
42 | if(api.unread.sync.isUnread(msg)) |
43 | cache.add(msg.key) |
44 | else |
45 | cache.delete(msg.key) |
46 | } |
47 | pull( |
48 | next(api.feed.pull.private, {reverse: true, limit: 1000, live: false, property: ['value', 'timestamp']}), |
49 | privateMsgFilter(), |
50 | pull.drain(updateUserUnreadMsgsCache) |
51 | ) |
52 | |
53 | pull( |
54 | next(api.feed.pull.private, {old: false, live: true, property: ['value', 'timestamp']}), |
55 | privateMsgFilter(), |
56 | pull.drain(updateUserUnreadMsgsCache) |
57 | ) |
58 | |
59 | //TODO: calculate unread state for public threads/blogs |
60 | // pull( |
61 | // next(api.feed.pull.public, {reverse: true, limit: 100, live: false, property: ['value', 'timestamp']}), |
62 | // pull.drain(msg => { |
63 | // |
64 | // }) |
65 | // ) |
66 | |
67 | return h('Context -feed', [ |
68 | LevelOneContext(), |
69 | LevelTwoContext() |
70 | ]) |
71 | |
72 | function LevelOneContext () { |
73 | function isDiscoverContext (loc) { |
74 | const PAGES_UNDER_DISCOVER = ['blogIndex', 'blogShow', 'home'] |
75 | |
76 | return PAGES_UNDER_DISCOVER.includes(location.page) |
77 | || get(location, 'value.private') === undefined |
78 | } |
79 | |
80 | const prepend = [ |
81 | // Nearby |
82 | computed(nearby, n => !isEmpty(n) ? h('header', strings.peopleNearby) : null), |
83 | map(nearby, feedId => Option({ |
84 | notifications: notifications(feedId), |
85 | imageEl: api.about.html.avatar(feedId, 'small'), |
86 | label: api.about.obs.name(feedId), |
87 | selected: location.feed === feedId, |
88 | location: computed(recentMsgCache, recent => { |
89 | const lastMsg = recent.find(msg => msg.value.author === feedId) |
90 | return lastMsg |
91 | ? Object.assign(lastMsg, { feed: feedId }) |
92 | : { page: 'threadNew', feed: feedId } |
93 | }), |
94 | }), { comparer: (a, b) => a === b }), |
95 | |
96 | // --------------------- |
97 | computed(nearby, n => !isEmpty(n) ? h('hr') : null), |
98 | |
99 | // Discover |
100 | Option({ |
101 | // notifications: '!', //TODO - count this! |
102 | imageEl: h('i.fa.fa-binoculars'), |
103 | label: strings.blogIndex.title, |
104 | selected: isDiscoverContext(location), |
105 | location: { page: 'blogIndex' }, |
106 | }) |
107 | ] |
108 | |
109 | return api.app.html.scroller({ |
110 | classList: [ 'level', '-one' ], |
111 | prepend, |
112 | stream: api.feed.pull.private, |
113 | filter: privateMsgFilter, |
114 | store: recentMsgCache, |
115 | updateTop: updateRecentMsgCache, |
116 | updateBottom: updateRecentMsgCache, |
117 | render: (msgObs) => { |
118 | const msg = resolve(msgObs) |
119 | const { author } = msg.value |
120 | if (nearby.has(author)) return |
121 | |
122 | return Option({ |
123 | //the number of threads with each peer |
124 | notifications: notifications(author), |
125 | imageEl: api.about.html.avatar(author), |
126 | label: api.about.obs.name(author), |
127 | selected: location.feed === author, |
128 | location: Object.assign({}, msg, { feed: author }) // TODO make obs? |
129 | }) |
130 | } |
131 | }) |
132 | |
133 | function updateRecentMsgCache (soFar, newMsg) { |
134 | soFar.transaction(() => { |
135 | const { author, timestamp } = newMsg.value |
136 | const index = indexOf(soFar, (msg) => author === resolve(msg).value.author) |
137 | var object = Value() |
138 | |
139 | if (index >= 0) { |
140 | // reference already exists, lets use this instead! |
141 | const existingMsg = soFar.get(index) |
142 | |
143 | if (resolve(existingMsg).value.timestamp > timestamp) return |
144 | // but abort if the existing reference is newer |
145 | |
146 | object = existingMsg |
147 | soFar.deleteAt(index) |
148 | } |
149 | |
150 | object.set(newMsg) |
151 | |
152 | const justOlderPosition = indexOf(soFar, (msg) => timestamp > resolve(msg).value.timestamp) |
153 | if (justOlderPosition > -1) { |
154 | soFar.insert(object, justOlderPosition) |
155 | } else { |
156 | soFar.push(object) |
157 | } |
158 | }) |
159 | } |
160 | |
161 | } |
162 | |
163 | function getUserUnreadMsgsCache (author) { |
164 | var cache = usersUnreadMsgsCache.get(author) |
165 | if (!cache) { |
166 | cache = Set () |
167 | usersUnreadMsgsCache.put(author, cache) |
168 | } |
169 | return cache |
170 | } |
171 | |
172 | function notifications (author) { |
173 | return computed(getUserUnreadMsgsCache(author), cache => cache.length) |
174 | |
175 | // TODO find out why this doesn't work |
176 | // return getUserUnreadMsgsCache(feedId) |
177 | // .getLength |
178 | } |
179 | |
180 | function LevelTwoContext () { |
181 | const { key, value, feed: targetUser, page } = location |
182 | const root = get(value, 'content.root', key) |
183 | if (!targetUser) return |
184 | |
185 | |
186 | const prepend = Option({ |
187 | selected: page === 'threadNew', |
188 | location: {page: 'threadNew', feed: targetUser}, |
189 | label: h('Button', strings.threadNew.action.new), |
190 | }) |
191 | |
192 | var userLastMsgCache = usersLastMsgCache.get(targetUser) |
193 | if (!userLastMsgCache) { |
194 | userLastMsgCache = MutantArray() |
195 | usersLastMsgCache.put(targetUser, userLastMsgCache) |
196 | } |
197 | |
198 | return api.app.html.scroller({ |
199 | classList: [ 'level', '-two' ], |
200 | prepend, |
201 | stream: api.feed.pull.private, |
202 | filter: () => pull( |
203 | pull.filter(msg => !msg.value.content.root), |
204 | pull.filter(msg => msg.value.content.type === 'post'), |
205 | pull.filter(msg => msg.value.content.recps), |
206 | pull.filter(msg => msg.value.content.recps |
207 | .map(recp => typeof recp === 'object' ? recp.link : recp) |
208 | .some(recp => recp === targetUser) |
209 | ) |
210 | ), |
211 | store: userLastMsgCache, |
212 | updateTop: updateLastMsgCache, |
213 | updateBottom: updateLastMsgCache, |
214 | render: (rootMsgObs) => { |
215 | const rootMsg = resolve(rootMsgObs) |
216 | return Option({ |
217 | label: api.message.html.subject(rootMsg), |
218 | selected: rootMsg.key === root, |
219 | location: Object.assign(rootMsg, { feed: targetUser }), |
220 | }) |
221 | } |
222 | }) |
223 | |
224 | function updateLastMsgCache (soFar, newMsg) { |
225 | soFar.transaction(() => { |
226 | const { timestamp } = newMsg.value |
227 | const index = indexOf(soFar, (msg) => timestamp === resolve(msg).value.timestamp) |
228 | |
229 | if (index >= 0) return |
230 | // if reference already exists, abort |
231 | |
232 | var object = Value(newMsg) |
233 | |
234 | const justOlderPosition = indexOf(soFar, (msg) => timestamp > resolve(msg).value.timestamp) |
235 | if (justOlderPosition > -1) { |
236 | soFar.insert(object, justOlderPosition) |
237 | } else { |
238 | soFar.push(object) |
239 | } |
240 | }) |
241 | } |
242 | } |
243 | |
244 | function Option ({ notifications = 0, imageEl, label, location, selected }) { |
245 | const className = selected ? '-selected' : '' |
246 | const goToLocation = (e) => { |
247 | e.preventDefault() |
248 | e.stopPropagation() |
249 | api.history.sync.push(resolve(location)) |
250 | } |
251 | |
252 | if (!imageEl) { |
253 | return h('Option', { className, 'ev-click': goToLocation }, [ |
254 | h('div.label', label) |
255 | ]) |
256 | } |
257 | |
258 | return h('Option', { className }, [ |
259 | h('div.circle', [ |
260 | when(notifications, h('div.alert', notifications)), |
261 | imageEl |
262 | ]), |
263 | h('div.label', { 'ev-click': goToLocation }, label) |
264 | ]) |
265 | } |
266 | |
267 | function privateMsgFilter () { |
268 | return pull( |
269 | pull.filter(msg => msg.value.content.type === 'post'), |
270 | pull.filter(msg => msg.value.author != myKey), |
271 | pull.filter(msg => msg.value.content.recps) |
272 | ) |
273 | } |
274 | } |
275 | } |
276 | |
277 | function indexOf (array, fn) { |
278 | for (var i = 0; i < array.getLength(); i++) { |
279 | if (fn(array.get(i))) { |
280 | return i |
281 | } |
282 | } |
283 | return -1 |
284 | } |
285 | |
286 |
Built with git-ssb-web