git ssb

0+

mixmix / scuttle-gathering



Tree: 9e830272505d9e2ac2dae4bf6e6b34e6e05ab905

Files: 9e830272505d9e2ac2dae4bf6e6b34e6e05ab905 / lib / get-backlinks.js

1186 bytesRaw
1const pull = require('pull-stream')
2const sort = require('ssb-sort')
3const { isMsg } = require('ssb-ref')
4const { isGathering } = require('ssb-gathering-schema')
5
6module.exports = function getBacklinks (server) {
7 if (arguments.length !== 1 || typeof arguments[0] !== 'object') throw new Error('getBacklinks needs a server as it\'s first argument!')
8
9 return function (gathering, cb) {
10 if (!isGathering(gathering)) return cb(new Error('getBacklinks requires a valid gathering message'))
11
12 const key = gathering.key
13 if (!isMsg(key)) return cb(new Error('getBacklinks requires a valid gathering message'))
14 const query = [{
15 $filter: { dest: key }
16 }]
17 pull(
18 server.backlinks.read({ query }),
19 pull.filter(m => {
20 return (m.value.content.about === key) || (m.value.content.root === key)
21 }),
22 pull.collect((err, backlinks) => {
23 if (err) return cb(err)
24
25 const sorted = sort([ gathering, ...backlinks ])
26 sorted.shift()
27 // remove gathering after sort
28 // putting gathering into the sort is paranoid and is thinking ahead to a time without timestamps
29
30 cb(null, sorted)
31 })
32 )
33 }
34}
35

Built with git-ssb-web