Commit 6b0e3d2a1e43d3efa7bfab274588c5b8acd70f16
pollish for the inital release
mixmix committed on 8/15/2018, 9:25:24 AMParent: 1e2909813f2b8817d7b212f23e16c6469125a36b
Files changed
README.md | changed |
index.js | changed |
package-lock.json | changed |
package.json | changed |
README.md | |||
---|---|---|---|
@@ -1,1 +1,28 @@ | |||
1 | -# ssb-unread | ||
1 … | +# ssb-unread | ||
2 … | + | ||
3 … | +A scuttlebot plugin which tracks read / unread state of all received messages in a mutable level db. | ||
4 … | +The intention is to provide a persistent store which can be referenced and contributed to by a plurality of client interfaces. | ||
5 … | + | ||
6 … | +## Install | ||
7 … | + | ||
8 … | +```js | ||
9 … | +var sbot = require('scuttlebot') | ||
10 … | + .use(require('scuttlebot/plugins/master')) | ||
11 … | + .use(require('scuttlebot/plugins/gossip')) | ||
12 … | + .use(require('scuttlebot/plugins/replicate')) | ||
13 … | + .use(require('scuttlebot/plugins/invite')) | ||
14 … | + .use(require('scuttlebot/plugins/local')) | ||
15 … | + .use(require('ssb-unread')) // << | ||
16 … | + .call(null, config) | ||
17 … | +``` | ||
18 … | + | ||
19 … | +## API | ||
20 … | + | ||
21 … | +### `sbot.unread.isRead(key, cb)` | ||
22 … | + | ||
23 … | +provide a message key and get a boolean response back in the callback | ||
24 … | + | ||
25 … | +### `sbot.unread.markRead(key, cb)` | ||
26 … | + | ||
27 … | +mark a message key as read | ||
28 … | + |
index.js | ||
---|---|---|
@@ -2,17 +2,16 @@ | ||
2 | 2 … | const mkdirp = require('mkdirp') |
3 | 3 … | const { join } = require('path') |
4 | 4 … | const level = require('level') |
5 | 5 … | const charwise = require('charwise') |
6 | -const Value = require('mutant/value') | |
6 … | +const { isMsg } = require('ssb-ref') | |
7 | 7 … | |
8 | 8 … | module.exports = { |
9 | 9 … | name: 'unread', |
10 | 10 … | version: require('./package.json').version, |
11 | 11 … | manifest: { |
12 | 12 … | isRead: 'async', |
13 | 13 … | markRead: 'async', |
14 | - isReadObs: 'sync', | |
15 | 14 … | // isUnreadThrough: 'source' // stream via this module to check unread state of msgs as you go? |
16 | 15 … | }, |
17 | 16 … | init: function (server, config) { |
18 | 17 … | |
@@ -20,21 +19,11 @@ | ||
20 | 19 … | const db = level(join(config.path, 'unread'), { |
21 | 20 … | valueEncoding: charwise |
22 | 21 … | }) |
23 | 22 … | |
24 | - const STARTED_AT = 'startedAt' | |
25 | - var startedAt | |
26 | - db.get(STARTED_AT, (err, ts) => { | |
27 | - if (ts) { | |
28 | - startedAt = ts | |
29 | - return | |
30 | - } | |
23 … | + markDbBirth(db) | |
31 | 24 … | |
32 | - startedAt = Date.now | |
33 | - db.put(STARTED_AT, startedAt) | |
34 | - }) | |
35 | - | |
36 | - const VERSION = 0 | |
25 … | + const VERSION = 1 | |
37 | 26 … | server._flumeUse('unread-dummy-index', flumeView( |
38 | 27 … | VERSION, |
39 | 28 … | (_, msg) => { |
40 | 29 … | db.put(msg.key, null, noop) |
@@ -43,38 +32,41 @@ | ||
43 | 32 … | // HACK: leveraging flume to access stream of newest messages |
44 | 33 … | } |
45 | 34 … | )) |
46 | 35 … | |
47 | - // should take key? | |
48 | 36 … | function isRead (key, cb) { |
37 … | + if (!isMsg(key)) return cb(null, new Error('ssb-unread requires a valid message key') | |
38 … | + | |
49 | 39 … | db.get(key, (err, ts) => { |
50 | 40 … | if (err) cb(err) |
51 | 41 … | else cb(null, Boolean(ts)) |
52 | 42 … | }) |
53 | 43 … | } |
54 | 44 … | |
55 | 45 … | function markRead (key, cb = noop) { |
46 … | + if (!isMsg(key)) return cb(null, new Error('ssb-unread requires a valid message key') | |
47 … | + | |
56 | 48 … | db.put(key, Date.now(), cb) |
57 | 49 … | } |
58 | 50 … | |
59 | - function isReadObs (key) { | |
60 | - const obs = Value(null) | |
61 | - isRead(key, (err, state) => { | |
62 | - if (err) console.error(err) | |
63 | - else obs.set(state) | |
64 | - }) | |
65 | - | |
66 | - // remember pull-level | |
67 | - // - could use for live updating ? | |
68 | - | |
69 | - return obs | |
70 | - } | |
71 | - | |
72 | 51 … | return { |
73 | 52 … | isRead, |
74 | 53 … | markRead, |
75 | - isReadObs | |
76 | 54 … | } |
77 | 55 … | } |
78 | 56 … | } |
79 | 57 … | |
58 … | +function markDbBirth (db) { | |
59 … | + const STARTED_AT = 'startedAt' | |
60 … | + var startedAt | |
61 … | + db.get(STARTED_AT, (err, ts) => { | |
62 … | + if (ts) { | |
63 … | + startedAt = ts | |
64 … | + return | |
65 … | + } | |
66 … | + | |
67 … | + startedAt = Date.now | |
68 … | + db.put(STARTED_AT, startedAt) | |
69 … | + }) | |
70 … | +} | |
71 … | + | |
80 | 72 … | function noop () {} |
package-lock.json | ||
---|---|---|
@@ -54,13 +54,8 @@ | ||
54 | 54 … | "readable-stream": "^2.3.5", |
55 | 55 … | "safe-buffer": "^5.1.1" |
56 | 56 … | } |
57 | 57 … | }, |
58 | - "browser-split": { | |
59 | - "version": "0.0.1", | |
60 | - "resolved": "https://registry.npmjs.org/browser-split/-/browser-split-0.0.1.tgz", | |
61 | - "integrity": "sha1-ewl1dPjj6tYG+0Zk5krf3aKYGpM=" | |
62 | - }, | |
63 | 58 … | "buffer-alloc": { |
64 | 59 … | "version": "1.2.0", |
65 | 60 … | "resolved": "https://registry.npmjs.org/buffer-alloc/-/buffer-alloc-1.2.0.tgz", |
66 | 61 … | "integrity": "sha512-CFsHQgjtW1UChdXgbyJGtnm+O/uLQeZdtbDo8mfUgYXCHSM1wgrVxXm6bSyrUuErEb+4sYVGCzASBRot7zyrow==", |
@@ -247,16 +242,26 @@ | ||
247 | 242 … | "version": "1.3.5", |
248 | 243 … | "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.5.tgz", |
249 | 244 … | "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==" |
250 | 245 … | }, |
246 … | + "ip": { | |
247 … | + "version": "1.1.5", | |
248 … | + "resolved": "https://registry.npmjs.org/ip/-/ip-1.1.5.tgz", | |
249 … | + "integrity": "sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=" | |
250 … | + }, | |
251 | 251 … | "is-fullwidth-code-point": { |
252 | 252 … | "version": "1.0.0", |
253 | 253 … | "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", |
254 | 254 … | "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", |
255 | 255 … | "requires": { |
256 | 256 … | "number-is-nan": "^1.0.0" |
257 | 257 … | } |
258 | 258 … | }, |
259 … | + "is-valid-domain": { | |
260 … | + "version": "0.0.5", | |
261 … | + "resolved": "https://registry.npmjs.org/is-valid-domain/-/is-valid-domain-0.0.5.tgz", | |
262 … | + "integrity": "sha1-SOcDGfy0MAkjbpazf5hDiJzntRM=" | |
263 … | + }, | |
259 | 264 … | "isarray": { |
260 | 265 … | "version": "1.0.0", |
261 | 266 … | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", |
262 | 267 … | "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" |
@@ -350,17 +355,8 @@ | ||
350 | 355 … | "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" |
351 | 356 … | } |
352 | 357 … | } |
353 | 358 … | }, |
354 | - "mutant": { | |
355 | - "version": "3.22.1", | |
356 | - "resolved": "https://registry.npmjs.org/mutant/-/mutant-3.22.1.tgz", | |
357 | - "integrity": "sha1-kEh1RvcAs8KKqApD0c99M48wdYE=", | |
358 | - "requires": { | |
359 | - "browser-split": "0.0.1", | |
360 | - "xtend": "^4.0.1" | |
361 | - } | |
362 | - }, | |
363 | 359 … | "nan": { |
364 | 360 … | "version": "2.10.0", |
365 | 361 … | "resolved": "https://registry.npmjs.org/nan/-/nan-2.10.0.tgz", |
366 | 362 … | "integrity": "sha512-bAdJv7fBLhWC+/Bls0Oza+mvTaNQtP+1RyhhhvD95pgUJz6XM5IzgmxOkItJ9tkoCiplvAnXI1tNmmUD/eScyA==" |
@@ -540,8 +536,17 @@ | ||
540 | 536 … | "once": "^1.3.1", |
541 | 537 … | "simple-concat": "^1.0.0" |
542 | 538 … | } |
543 | 539 … | }, |
540 … | + "ssb-ref": { | |
541 … | + "version": "2.11.1", | |
542 … | + "resolved": "https://registry.npmjs.org/ssb-ref/-/ssb-ref-2.11.1.tgz", | |
543 … | + "integrity": "sha512-K3L9hJ1v0HrH8abtEKiBkdeabHVaws+CS81mZqUfhR84i0dlYhiIIDwqeLxUj/1mLcsZPF3gMKPsFCUr7UAdMA==", | |
544 … | + "requires": { | |
545 … | + "ip": "^1.1.3", | |
546 … | + "is-valid-domain": "~0.0.1" | |
547 … | + } | |
548 … | + }, | |
544 | 549 … | "string-width": { |
545 | 550 … | "version": "1.0.2", |
546 | 551 … | "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", |
547 | 552 … | "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", |
Built with git-ssb-web