Files: 4aff4aca95f4ab571c23baeb7192f7516341f6fc / unread.js
1380 bytesRaw
1 | var nest = require('depnest') |
2 | var { Dict, Set } = require('mutant') |
3 | |
4 | exports.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 | |
12 | exports.create = function (api) { |
13 | var unread = null |
14 | if (localStorage.unread) { |
15 | try { |
16 | unread = JSON.parse(localStorage.unread) |
17 | } catch (err) {} |
18 | } |
19 | if (!unread) { unread = {timestamp: Date.now()} } |
20 | |
21 | unread.timestamp = unread.timestamp || Date.now() |
22 | |
23 | if (!unread.filter) { unread.filter = {} } |
24 | |
25 | var timer |
26 | function save () { |
27 | if (timer) return |
28 | |
29 | timer = setTimeout(function () { |
30 | timer = null |
31 | localStorage.unread = JSON.stringify(unread) |
32 | }, 2e3) |
33 | } |
34 | |
35 | function isUnread (msg) { |
36 | if (msg.timestamp && msg.timestamp < unread.timestamp) return false |
37 | return !unread.filter[msg.key] |
38 | } |
39 | |
40 | function markRead (msg) { |
41 | if (msg && typeof msg.key === 'string') { |
42 | // note: there is a quirk where some messages don't have a timestamp |
43 | if (isUnread(msg)) { |
44 | var userUser |
45 | unread.filter[msg.key] = true |
46 | save() |
47 | return true |
48 | } |
49 | } |
50 | } |
51 | |
52 | function userMessages (feedId) { |
53 | |
54 | } |
55 | |
56 | document.body.onunload = save |
57 | |
58 | return nest({ |
59 | 'unread.sync.isUnread': isUnread, |
60 | 'unread.sync.markRead': markRead, |
61 | 'unread.obs.userMessages': userMessages |
62 | }) |
63 | } |
64 |
Built with git-ssb-web