app/html/context.jsView |
---|
1 | 1 | const nest = require('depnest') |
2 | | -const { h, computed, map, when, Dict, dictToCollection, Array: MutantArray, Value, resolve } = require('mutant') |
| 2 | +const { h, computed, map, when, Dict, Array: MutantArray, Value, Set, resolve } = require('mutant') |
3 | 3 | const pull = require('pull-stream') |
4 | 4 | const next = require('pull-next-step') |
5 | 5 | const get = require('lodash/get') |
6 | 6 | const isEmpty = require('lodash/isEmpty') |
23 | 23 | }) |
24 | 24 | |
25 | 25 | exports.create = (api) => { |
26 | 26 | var recentMsgCache = MutantArray() |
27 | | - var usersMsgCache = Dict() |
| 27 | + var usersLastMsgCache = Dict() |
| 28 | + var usersUnreadMsgsCache = Dict() |
28 | 29 | |
29 | 30 | return nest('app.html.context', context) |
30 | 31 | |
31 | 32 | function context (location) { |
33 | 34 | const myKey = api.keys.sync.id() |
34 | 35 | |
35 | 36 | var nearby = api.sbot.obs.localPeers() |
36 | 37 | |
37 | | - var unreadCache = Dict() |
38 | | - |
39 | 38 | pull( |
40 | | - next(api.feed.pull.private, {reverse: true, limit: 10000, live: false}, ['value', 'timestamp']), |
| 39 | + next(api.feed.pull.private, {reverse: true, limit: 1000, live: false}, ['value', 'timestamp']), |
41 | 40 | pull.filter(msg => msg.value.content.type === 'post'), |
42 | 41 | pull.filter(msg => msg.value.content.recps), |
| 42 | + pull.filter(msg => msg.value.author !== myKey), |
43 | 43 | pull.drain(msg => { |
44 | | - var author = msg.value.author |
45 | | - if(api.unread.sync.isUnread(msg)) { |
46 | | - var seen = author === myKey ? 0 : 1 |
47 | | - unreadCache.put(author, (unreadCache.get(author)||0)+seen) |
48 | | - } |
| 44 | + var cache = getUserUnreadMsgsCache(msg.value.author) |
| 45 | + |
| 46 | + if(api.unread.sync.isUnread(msg)) |
| 47 | + cache.add(msg.key) |
49 | 48 | else |
50 | | - unreadCache.put(author, 0) |
| 49 | + cache.delete(msg.key) |
51 | 50 | }) |
52 | 51 | ) |
53 | 52 | |
54 | | - |
55 | | - |
56 | | - |
57 | | - |
58 | | - |
59 | | - |
60 | | - |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
61 | 60 | |
62 | 61 | return h('Context -feed', [ |
63 | 62 | LevelOneContext(), |
64 | 63 | LevelTwoContext() |
75 | 74 | const prepend = [ |
76 | 75 | |
77 | 76 | computed(nearby, n => !isEmpty(n) ? h('header', strings.peopleNearby) : null), |
78 | 77 | map(nearby, feedId => Option({ |
79 | | - notifications: unreadCache.get(feedId), |
| 78 | + notifications: notifications(feedId), |
80 | 79 | imageEl: api.about.html.avatar(feedId, 'small'), |
81 | 80 | label: api.about.obs.name(feedId), |
82 | 81 | selected: location.feed === feedId, |
83 | 82 | location: computed(recentMsgCache, recent => { |
92 | 91 | computed(nearby, n => !isEmpty(n) ? h('hr') : null), |
93 | 92 | |
94 | 93 | |
95 | 94 | Option({ |
96 | | - |
97 | | - notifications: null, |
| 95 | + notifications: '!', |
98 | 96 | imageEl: h('i.fa.fa-binoculars'), |
99 | 97 | label: strings.blogIndex.title, |
100 | 98 | selected: isDiscoverContext(location), |
101 | 99 | location: { page: 'blogIndex' }, |
120 | 118 | if (nearby.has(author)) return |
121 | 119 | |
122 | 120 | return Option({ |
123 | 121 | |
124 | | - notifications: computed(unreadCache, cache => cache[author]), |
| 122 | + notifications: notifications(author), |
125 | 123 | imageEl: api.about.html.avatar(author), |
126 | 124 | label: api.about.obs.name(author), |
127 | 125 | selected: location.feed === author, |
128 | 126 | location: Object.assign({}, msg, { feed: author }) |
159 | 157 | } |
160 | 158 | |
161 | 159 | } |
162 | 160 | |
| 161 | + function getUserUnreadMsgsCache (author) { |
| 162 | + var cache = usersUnreadMsgsCache.get(author) |
| 163 | + if (!cache) { |
| 164 | + cache = Set () |
| 165 | + usersUnreadMsgsCache.put(author, cache) |
| 166 | + } |
| 167 | + return cache |
| 168 | + } |
| 169 | + |
| 170 | + function notifications (author) { |
| 171 | + return computed(getUserUnreadMsgsCache(author), cache => cache.length) |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + } |
| 177 | + |
163 | 178 | function LevelTwoContext () { |
164 | 179 | const { key, value, feed: targetUser, page } = location |
165 | 180 | const root = get(value, 'content.root', key) |
166 | 181 | if (!targetUser) return |
171 | 186 | location: {page: 'threadNew', feed: targetUser}, |
172 | 187 | label: h('Button', strings.threadNew.action.new), |
173 | 188 | }) |
174 | 189 | |
175 | | - var userMsgCache = usersMsgCache.get(targetUser) |
176 | | - if (!userMsgCache) { |
177 | | - userMsgCache = MutantArray() |
178 | | - usersMsgCache.put(targetUser, userMsgCache) |
| 190 | + var userLastMsgCache = usersLastMsgCache.get(targetUser) |
| 191 | + if (!userLastMsgCache) { |
| 192 | + userLastMsgCache = MutantArray() |
| 193 | + usersLastMsgCache.put(targetUser, userLastMsgCache) |
179 | 194 | } |
180 | 195 | |
181 | 196 | return api.app.html.scroller({ |
182 | 197 | classList: [ 'level', '-two' ], |
190 | 205 | .map(recp => typeof recp === 'object' ? recp.link : recp) |
191 | 206 | .some(recp => recp === targetUser) |
192 | 207 | ) |
193 | 208 | ), |
194 | | - store: userMsgCache, |
195 | | - updateTop: updateUserMsgCache, |
196 | | - updateBottom: updateUserMsgCache, |
| 209 | + store: userLastMsgCache, |
| 210 | + updateTop: updateLastMsgCache, |
| 211 | + updateBottom: updateLastMsgCache, |
197 | 212 | render: (rootMsgObs) => { |
198 | 213 | const rootMsg = resolve(rootMsgObs) |
199 | 214 | return Option({ |
200 | 215 | label: api.message.html.subject(rootMsg), |
203 | 218 | }) |
204 | 219 | } |
205 | 220 | }) |
206 | 221 | |
207 | | - function updateUserMsgCache (soFar, newMsg) { |
| 222 | + function updateLastMsgCache (soFar, newMsg) { |
208 | 223 | soFar.transaction(() => { |
209 | 224 | const { timestamp } = newMsg.value |
210 | 225 | const index = indexOf(soFar, (msg) => timestamp === resolve(msg).value.timestamp) |
211 | 226 | |