git ssb

2+

mixmix / ticktack



Tree: 24e311f82c556a4266e092dbd07d8cea99aac0c8

Files: 24e311f82c556a4266e092dbd07d8cea99aac0c8 / unread.js

1433 bytesRaw
1var nest = require('depnest')
2var { Dict, Set } = require('mutant')
3
4exports.gives = nest({
5 'unread.sync.isUnread': true,
6 'unread.sync.markRead': true,
7 'unread.obs.userMessages': true
8})
9
10//load current state of unread messages.
11
12exports.create = function (api) {
13
14 var unread = null
15 if(localStorage.unread) {
16 try {
17 unread = JSON.parse(localStorage.unread)
18 } catch (err) {}
19 }
20 if(!unread)
21 unread = {timestamp: Date.now()}
22
23 unread.timestamp = unread.timestamp || Date.now()
24
25 if(!unread.filter)
26 unread.filter = {}
27
28 var timer
29 function save () {
30 if(timer) return
31
32 timer = setTimeout(function () {
33 timer = null
34 console.log('save!', Object.keys(unread.filter).length)
35 localStorage.unread = JSON.stringify(unread)
36 }, 2e3)
37 }
38
39 function isUnread(msg) {
40 if(msg.timestamp && msg.timestamp < unread.timestamp) return false
41 return !unread.filter[msg.key]
42 }
43
44 function markRead(msg) {
45 if(msg && 'string' === typeof msg.key) {
46 //note: there is a quirk where some messages don't have a timestamp
47 if(isUnread(msg)) {
48 var userUser
49 unread.filter[msg.key] = true
50 save()
51 return true
52 }
53 }
54 }
55
56 function userMessages(feedId) {
57
58 }
59
60 document.body.onunload = save
61
62 return nest({
63 'unread.sync.isUnread': isUnread,
64 'unread.sync.markRead': markRead,
65 'unread.obs.userMessages': userMessages
66 })
67}
68
69

Built with git-ssb-web