git ssb

2+

mixmix / ticktack



Tree: dd39c2da66f15ca736f6e708ea28a6fafe860475

Files: dd39c2da66f15ca736f6e708ea28a6fafe860475 / unread.js

1371 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 localStorage.unread = JSON.stringify(unread)
35 }, 2e3)
36 }
37
38 function isUnread(msg) {
39 if(msg.timestamp && msg.timestamp < unread.timestamp) return false
40 return !unread.filter[msg.key]
41 }
42
43 function markRead(msg) {
44 if(msg && 'string' === typeof msg.key) {
45 //note: there is a quirk where some messages don't have a timestamp
46 if(isUnread(msg)) {
47 var userUser
48 unread.filter[msg.key] = true
49 save()
50 return true
51 }
52 }
53 }
54
55 function userMessages(feedId) {
56
57 }
58
59 document.body.onunload = save
60
61 return nest({
62 'unread.sync.isUnread': isUnread,
63 'unread.sync.markRead': markRead,
64 'unread.obs.userMessages': userMessages
65 })
66}
67
68

Built with git-ssb-web