git ssb

0+

Josiah / scuttle-tag



Commit 73d53a413c5991111416c31c1d615c89aefe88af

Update for new ssb-tags index

Josiah Witt committed on 1/29/2018, 1:44:12 AM
Parent: a6ff6e81fe4c9b6ccd9cc84b5d8739674900b38f

Files changed

tag/obs/tagged.jschanged
tag/obs/all.jsdeleted
tag/obs/tagged.jsView
@@ -1,17 +1,19 @@
11 var { Value, computed } = require('mutant')
22 var pull = require('pull-stream')
33 var nest = require('depnest')
44 var ref = require('ssb-ref')
5 +var set = require('lodash/set')
56
67 exports.needs = nest({
78 'sbot.pull.stream': 'first'
89 })
910
1011 exports.gives = nest({
1112 'tag.obs': [
1213 'taggedMessages',
13- 'messagesTagged'
14 + 'messageTags',
15 + 'allTagsFrom'
1416 ]
1517 })
1618
1719 exports.create = function(api) {
@@ -21,9 +23,10 @@
2123
2224 return nest({
2325 'tag.obs': {
2426 taggedMessages,
25- messageTags
27 + messageTags,
28 + allTagsFrom
2629 }
2730 })
2831
2932 function taggedMessages(author, tagId) {
@@ -35,8 +38,13 @@
3538 if (!ref.isLink(tagId) || !ref.isLink(msgId)) throw new Error('Requires an ssb ref!')
3639 return withSync(computed([get(msgId, messagesCache), tagId], getMessageTags))
3740 }
3841
42 + function allTagsFrom(author) {
43 + if (!ref.isLink(author)) throw new Error('Requires an ssb ref!')
44 + return withSync(computed(get(author, tagsCache), lookup => Object.keys(lookup)))
45 + }
46 +
3947 function withSync(obs) {
4048 obs.sync = sync
4149 return obs
4250 }
@@ -57,23 +65,34 @@
5765 pull(
5866 api.sbot.pull.stream(sbot => sbot.tags.stream({ live: true })),
5967 pull.drain(item => {
6068 if (!sync()) {
61- // populate observable cache
62- for (const author in item.tags) {
63- update(author, item.tags[author], tagsCache)
69 + // populate tags observable cache
70 + const messageLookup = {}
71 + for (const author in item) {
72 + update(author, item[author], tagsCache)
73 +
74 + // generate message lookup
75 + for (const tag in item[author]) {
76 + for (const message in item[author][tag]) {
77 + set(messageLookup, [message, tag, author], item[message][author][tag])
78 + }
79 + }
6480 }
65- for (const message in item.messages) {
66- update(message, item.messages[message], messagesCache)
81 +
82 + // populate messages observable cache
83 + for (const message in messageLookup) {
84 + update(message, messageLookup[message], messagesCache)
6785 }
86 +
6887 if (!sync()) {
6988 sync.set(true)
7089 }
7190 } else if (item && ref.isLink(item.tag) && ref.isLink(item.author) && ref.isLink(item.message)) {
7291 // handle realtime updates
7392 const { tag, author, message, tagged, timestamp } = item
74- update(author, { [tag]: { [message]: { timestamp, tagged } } }, tagsCache)
75- update(message, { [tag]: { [author]: { timestamp, tagged } } }, messagesCache)
93 + update(author, { [tag]: { [message]: timestamp } }, tagsCache)
94 + update(message, { [tag]: { [author]: timestamp } }, messagesCache)
7695 }
7796 })
7897 )
7998 }
@@ -111,11 +130,11 @@
111130
112131 function getMessageTags(lookup, tagId) {
113132 const tags = {}
114133 for (const author in lookup[tagId]) {
115- if (lookup[tagId][author].tagged) {
134 + if (lookup[tagId][author]) {
116135 if (!tags[tagId]) tags[tagId] = {}
117- tags[tagId][author] = lookup[tagId][author].timestamp
136 + tags[tagId][author] = lookup[tagId][author]
118137 }
119138 }
120139 return tags
121140 }
tag/obs/all.jsView
@@ -1,45 +1,0 @@
1-var nest = require('depnest')
2-var { Dict, Array } = require('mutant')
3-var MutantPullReduce = require('mutant-pull-reduce')
4-
5-exports.needs = nest({
6- 'sbot.pull.stream': 'first'
7-})
8-
9-exports.gives = nest('tags.obs.all', true)
10-
11-exports.create = function (api) {
12- var cached = null
13- return nest('tags.obs.all', () => {
14- if (!cached) {
15- var stream = api.sbot.pull.stream(s => s.tags.stream({live: true}))
16- cached = MutantPullReduce(stream, (result, data) => {
17- if (data.tags) {
18- // handle initial state
19- for (var tag in data.tags) {
20- var messages = new Dict()
21- for (var message in data.tags[tag]) {
22- if (data.tags[tag][message].tagged) {
23- messages.put(message, data.tags[tag][message])
24- }
25- }
26- result.put(tag, messages)
27- }
28- } else if (data.tag) {
29- // handle realtime changes
30- var { author, tag, message, tagged, timestamp } = data
31- if (!result.get(tag)) result.put(tag, new Dict())
32- if (tagged && (!result.get(tag).get(message) || timestamp > result.get(tag).get(message).timestamp)) {
33- result.get(tag).put(message, { timestamp, tagged })
34- } else if (!tagged && result.get(tag).get(message)) {
35- result.get(tag).delete(message)
36- }
37- }
38- return result
39- }, {
40- startValue: new Dict()
41- })
42- }
43- return cached
44- })
45-}

Built with git-ssb-web