git ssb

2+

mixmix / ticktack



Commit 24e311f82c556a4266e092dbd07d8cea99aac0c8

add cache for unread counters in context bar

mix irving committed on 11/21/2017, 9:09:29 AM
Parent: 52acefb26cfdc545a5bef98c649d3cfbec4ac257

Files changed

app/html/context.jschanged
unread.jschanged
app/html/context.jsView
@@ -1,6 +1,6 @@
11 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')
33 const pull = require('pull-stream')
44 const next = require('pull-next-step')
55 const get = require('lodash/get')
66 const isEmpty = require('lodash/isEmpty')
@@ -23,9 +23,10 @@
2323 })
2424
2525 exports.create = (api) => {
2626 var recentMsgCache = MutantArray()
27- var usersMsgCache = Dict() // { id: [ msgs ] }
27+ var usersLastMsgCache = Dict() // { id: [ msgs ] }
28+ var usersUnreadMsgsCache = Dict() // { id: [ msgs ] }
2829
2930 return nest('app.html.context', context)
3031
3132 function context (location) {
@@ -33,32 +34,30 @@
3334 const myKey = api.keys.sync.id()
3435
3536 var nearby = api.sbot.obs.localPeers()
3637
37- var unreadCache = Dict()
38-
3938 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']),
4140 pull.filter(msg => msg.value.content.type === 'post'), // TODO is this the best way to protect against votes?
4241 pull.filter(msg => msg.value.content.recps),
42+ pull.filter(msg => msg.value.author !== myKey),
4343 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)
4948 else
50- unreadCache.put(author, 0)
49+ cache.delete(msg.key)
5150 })
5251 )
5352
54-//TODO: calculate unread state for public threads/blogs
55-// pull(
56-// next(api.feed.pull.public, {reverse: true, limit: 100, live: false}, ['value', 'timestamp']),
57-// pull.drain(msg => {
58-//
59-// })
60-// )
53+ //TODO: calculate unread state for public threads/blogs
54+ // pull(
55+ // next(api.feed.pull.public, {reverse: true, limit: 100, live: false}, ['value', 'timestamp']),
56+ // pull.drain(msg => {
57+ //
58+ // })
59+ // )
6160
6261 return h('Context -feed', [
6362 LevelOneContext(),
6463 LevelTwoContext()
@@ -75,9 +74,9 @@
7574 const prepend = [
7675 // Nearby
7776 computed(nearby, n => !isEmpty(n) ? h('header', strings.peopleNearby) : null),
7877 map(nearby, feedId => Option({
79- notifications: unreadCache.get(feedId),
78+ notifications: notifications(feedId),
8079 imageEl: api.about.html.avatar(feedId, 'small'),
8180 label: api.about.obs.name(feedId),
8281 selected: location.feed === feedId,
8382 location: computed(recentMsgCache, recent => {
@@ -92,10 +91,9 @@
9291 computed(nearby, n => !isEmpty(n) ? h('hr') : null),
9392
9493 // Discover
9594 Option({
96- //TODO - count this!
97- notifications: null,
95+ notifications: '!', //TODO - count this!
9896 imageEl: h('i.fa.fa-binoculars'),
9997 label: strings.blogIndex.title,
10098 selected: isDiscoverContext(location),
10199 location: { page: 'blogIndex' },
@@ -120,9 +118,9 @@
120118 if (nearby.has(author)) return
121119
122120 return Option({
123121 //the number of threads with each peer
124- notifications: computed(unreadCache, cache => cache[author]),
122+ notifications: notifications(author),
125123 imageEl: api.about.html.avatar(author),
126124 label: api.about.obs.name(author),
127125 selected: location.feed === author,
128126 location: Object.assign({}, msg, { feed: author }) // TODO make obs?
@@ -159,8 +157,25 @@
159157 }
160158
161159 }
162160
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+ // TODO find out why this doesn't work
174+ // return getUserUnreadMsgsCache(feedId)
175+ // .getLength
176+ }
177+
163178 function LevelTwoContext () {
164179 const { key, value, feed: targetUser, page } = location
165180 const root = get(value, 'content.root', key)
166181 if (!targetUser) return
@@ -171,12 +186,12 @@
171186 location: {page: 'threadNew', feed: targetUser},
172187 label: h('Button', strings.threadNew.action.new),
173188 })
174189
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)
179194 }
180195
181196 return api.app.html.scroller({
182197 classList: [ 'level', '-two' ],
@@ -190,11 +205,11 @@
190205 .map(recp => typeof recp === 'object' ? recp.link : recp)
191206 .some(recp => recp === targetUser)
192207 )
193208 ),
194- store: userMsgCache,
195- updateTop: updateUserMsgCache,
196- updateBottom: updateUserMsgCache,
209+ store: userLastMsgCache,
210+ updateTop: updateLastMsgCache,
211+ updateBottom: updateLastMsgCache,
197212 render: (rootMsgObs) => {
198213 const rootMsg = resolve(rootMsgObs)
199214 return Option({
200215 label: api.message.html.subject(rootMsg),
@@ -203,9 +218,9 @@
203218 })
204219 }
205220 })
206221
207- function updateUserMsgCache (soFar, newMsg) {
222+ function updateLastMsgCache (soFar, newMsg) {
208223 soFar.transaction(() => {
209224 const { timestamp } = newMsg.value
210225 const index = indexOf(soFar, (msg) => timestamp === resolve(msg).value.timestamp)
211226
unread.jsView
@@ -1,9 +1,11 @@
11 var nest = require('depnest')
2+var { Dict, Set } = require('mutant')
23
34 exports.gives = nest({
45 'unread.sync.isUnread': true,
56 'unread.sync.markRead': true,
7+ 'unread.obs.userMessages': true
68 })
79
810 //load current state of unread messages.
911
@@ -42,21 +44,25 @@
4244 function markRead(msg) {
4345 if(msg && 'string' === typeof msg.key) {
4446 //note: there is a quirk where some messages don't have a timestamp
4547 if(isUnread(msg)) {
48+ var userUser
4649 unread.filter[msg.key] = true
4750 save()
4851 return true
4952 }
5053 }
5154 }
5255
56+ function userMessages(feedId) {
57+
58+ }
59+
5360 document.body.onunload = save
5461
5562 return nest({
5663 'unread.sync.isUnread': isUnread,
57- 'unread.sync.markRead': markRead
64+ 'unread.sync.markRead': markRead,
65+ 'unread.obs.userMessages': userMessages
5866 })
5967 }
6068
61-
62-

Built with git-ssb-web