git ssb

7+

dinoworm 🐛 / patchcore



Commit ebe8d562f226c534e1f12af64c355eb6ba6dba13

use `backlinks.obs.for` on likes, references and rollup (shared cache)

Matt McKegg committed on 6/28/2017, 7:37:23 AM
Parent: d05e2f3f94a5847f7a6ef694ef45f7d8d15cc025

Files changed

feed/pull/rollup.jschanged
message/obs/backlinks.jschanged
message/obs/likes.jschanged
feed/pull/rollup.jsView
@@ -5,11 +5,13 @@
55 var pull = require('pull-stream')
66 var nest = require('depnest')
77 var extend = require('xtend')
88 var HLRU = require('hashlru')
9 +var resolve = require('mutant/resolve')
10 +var onceTrue = require('mutant/once-true')
911
1012 exports.needs = nest({
11- 'sbot.pull.backlinks': 'first',
13 + 'backlinks.obs.for': 'first',
1214 'sbot.async.get': 'first',
1315 'message.sync.root': 'first',
1416 'message.sync.unbox': 'first'
1517 })
@@ -84,18 +86,16 @@
8486 pull.filter(rootFilter || (() => true)),
8587
8688 // ADD REPLIES
8789 pull.asyncMap((rootMessage, cb) => {
88- pull(
89- api.sbot.pull.backlinks({
90- query: [{$filter: { dest: rootMessage.key }}]
91- }),
92- pull.filter(msg => (api.message.sync.root(msg) || rootMessage.key) === rootMessage.key),
93- pull.collect((err, replies) => {
94- if (err) return cb(err)
95- cb(null, extend(rootMessage, { replies }))
90 + // use global backlinks cache
91 + var backlinks = api.backlinks.obs.for(rootMessage.key)
92 + onceTrue(backlinks.sync, () => {
93 + var replies = resolve(backlinks).filter((msg) => {
94 + return (api.message.sync.root(msg) || rootMessage.key) === rootMessage.key
9695 })
97- )
96 + cb(null, extend(rootMessage, { replies }))
97 + })
9898 })
9999 )
100100 })
101101 }
message/obs/backlinks.jsView
@@ -1,40 +1,38 @@
11 var nest = require('depnest')
2-var MutantPullReduce = require('mutant-pull-reduce')
2 +var computed = require('mutant/computed')
33
44 exports.needs = nest({
5- 'sbot.pull.backlinks': 'first'
5 + 'backlinks.obs.for': 'first'
66 })
77
88 exports.gives = nest('message.obs.backlinks', true)
99
1010 exports.create = function (api) {
1111 return nest({
12 + // DEPRECATED: should use backlinks.obs.for
1213 'message.obs.backlinks': (id) => backlinks(id)
1314 })
1415
1516 function backlinks (id) {
16- return MutantPullReduce(api.sbot.pull.backlinks({
17- query: [
18- {$filter: {
19- dest: id
20- }},
21- {$map: {
22- dest: 'dest',
23- id: 'key',
24- timestamp: 'timestamp',
25- type: ['value', 'content', 'type'],
26- root: ['value', 'content', 'root'],
27- branch: ['value', 'content', 'branch'],
28- author: ['value', 'author']
29- }}
30- ]
31- }), (result, msg) => {
32- if (msg.type !== 'vote' && msg.type !== 'about') {
33- result.push(msg)
34- }
35- return result
17 + return computed([api.backlinks.obs.for(id)], (msgs) => {
18 + return msgs.map(map).filter((backlink) => {
19 + return backlink.type !== 'vote' && backlink.type !== 'about'
20 + })
3621 }, {
37- startValue: []
22 + // objects coming down this stream will be immutable
23 + comparer: (a, b) => a === b
3824 })
3925 }
26 +
27 + function map (msg) {
28 + return {
29 + dest: msg.dest,
30 + id: msg.key,
31 + timestamp: msg.timestamp,
32 + type: msg.value.content.type,
33 + root: msg.value.content.root,
34 + branch: msg.value.content.branch,
35 + author: msg.value.author
36 + }
37 + }
4038 }
message/obs/likes.jsView
@@ -1,14 +1,15 @@
11 var nest = require('depnest')
22 var ref = require('ssb-ref')
3-var MutantPullReduce = require('mutant-pull-reduce')
4-var SortedArray = require('sorted-array-functions')
3 +var MutantArray = require('mutant/array')
4 +var concat = require('mutant/concat')
5 +var watch = require('mutant/watch')
56
67 var { computed } = require('mutant')
78
89 exports.needs = nest({
910 'message.sync.unbox': 'first',
10- 'sbot.pull.backlinks': 'first'
11 + 'backlinks.obs.for': 'first'
1112 })
1213
1314 exports.gives = nest({
1415 'sbot.hook.publish': true,
@@ -30,74 +31,57 @@
3031 if (!c.vote || !c.vote.link) return
3132
3233 activeLikes.forEach((likes) => {
3334 if (likes.id === c.vote.link) {
34- likes.push({
35- dest: c.vote.link,
36- id: msg.key,
37- expression: c.vote.expression,
38- value: c.vote.value,
39- timestamp: msg.value.timestamp,
40- author: msg.value.author
41- })
35 + likes.push(msg)
4236 }
4337 })
4438 },
4539 'message.obs.likes': (id) => {
4640 if (!ref.isLink(id)) throw new Error('an id must be specified')
4741 var obs = get(id)
4842 obs.id = id
49- return computed(obs, getLikes, {
43 + var result = computed(obs, getLikes, {
5044 // allow manual append for simulated realtime
5145 onListen: () => activeLikes.add(obs),
5246 onUnlisten: () => activeLikes.delete(obs)
5347 })
48 + result.sync = obs.sync
49 + return result
5450 }
5551 })
5652
5753 function get (id) {
58- var likes = MutantPullReduce(api.sbot.pull.backlinks({
59- live: true,
60- query: [
61- {$filter: {
62- dest: id,
63- value: {
64- content: {
65- type: 'vote',
66- vote: { link: id }
54 + var backlinks = api.backlinks.obs.for(id)
55 + var merge = MutantArray()
56 +
57 + var likes = computed([backlinks.sync, concat([backlinks, merge])], (sync, backlinks) => {
58 + if (sync) {
59 + return backlinks.reduce((result, msg) => {
60 + var c = msg.value.content
61 + if (c.type === 'vote' && c.vote && c.vote.link === id) {
62 + var value = result[msg.value.author]
63 + if (!value || value[0] < msg.value.timestamp) {
64 + result[msg.value.author] = [msg.value.timestamp, c.vote.value, c.vote.expression]
6765 }
6866 }
69- }},
70- {$map: {
71- dest: 'dest',
72- id: 'key',
73- expression: ['value', 'content', 'vote', 'expression'],
74- value: ['value', 'content', 'vote', 'value'],
75- timestamp: 'timestamp',
76- author: ['value', 'author']
77- }}
78- ]
79- }), (result, msg) => {
80- if (!result[msg.author]) {
81- result[msg.author] = []
67 + return result
68 + }, {})
69 + } else {
70 + return {}
8271 }
83- SortedArray.add(result[msg.author], msg, mostRecent)
84- return result
85- }, {
86- startValue: []
8772 })
73 +
74 + likes.push = merge.push
75 + likes.sync = backlinks.sync
8876 return likes
8977 }
9078 }
9179
9280 function getLikes (likes) {
9381 return Object.keys(likes).reduce((result, id) => {
94- if (likes[id][0].value) {
82 + if (likes[id][1] > 0) {
9583 result.push(id)
9684 }
9785 return result
9886 }, [])
9987 }
100-
101-function mostRecent (a, b) {
102- return b.timestamp - a.timestamp
103-}

Built with git-ssb-web