This repository has been archived by the owner on May 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.js
169 lines (155 loc) · 4.38 KB
/
query.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// SPDX-FileCopyrightText: 2021 Anders Rune Jensen
//
// SPDX-License-Identifier: LGPL-3.0-only
const validate = require('./validate')
const {
and,
author,
type,
where,
toCallback,
paginate,
descending,
} = require('ssb-db2/operators')
const SSBURI = require('ssb-uri2')
exports.init = function (sbot, config) {
const self = {
/**
* Gets the stored seed message (on the main feed) for the root meta feed.
*
* ```js
* sbot.metafeeds.query.getSeed((err, seed) => {
* console.log("seed buffer", seed)
* })
* ```
*/
getSeed(cb) {
// FIXME: maybe use metafeed id
sbot.db.query(
where(and(author(sbot.id), type('metafeed/seed'))),
paginate(1),
descending(),
toCallback((err, answer) => {
if (err) return cb(err)
const msgs = answer.results
if (msgs.length === 0) return cb(null, null)
const msg = msgs[0]
const seedBuf = Buffer.from(msg.value.content.seed, 'hex')
cb(null, seedBuf)
})
)
},
/**
* Gets the meta feed announce messages on main feed.
*
* ```js
* sbot.metafeeds.query.getAnnounces((err, msg) => {
* console.log("announce msg", msg)
* })
* ```
*/
getAnnounces(cb) {
sbot.db.query(
where(and(author(sbot.id), type('metafeed/announce'))),
toCallback(cb)
)
},
/**
* Gets the latest message on the given feed, typically a meta feed, but
* other feed types work too.
*/
getLatest(feedId, cb) {
sbot.db.query(
where(author(feedId)),
paginate(1),
descending(),
toCallback((err, answer) => {
if (err) return cb(err)
const msgs = answer.results
if (msgs.length !== 1) return cb(null, null)
const msg = msgs[0]
cb(null, msg)
})
)
},
collectMetadata(msg) {
const metadata = {}
const ignored = [
'feedpurpose',
'subfeed',
'nonce',
'metafeed',
'tangles',
'type',
]
for (const key of Object.keys(msg.value.content)) {
if (ignored.includes(key)) continue
metadata[key] = msg.value.content[key]
}
return metadata
},
/**
* Gets the current state of a subfeed based on the meta feed message that
* "added" the subfeed
*/
hydrateFromMsg(msg, seed) {
const { type, feedpurpose, subfeed, nonce } = msg.value.content
const metadata = self.collectMetadata(msg)
const feedformat = SSBURI.isBendyButtV1FeedSSBURI(subfeed)
? 'bendybutt-v1'
: 'classic'
const existing = type === 'metafeed/add/existing'
const keys = existing
? config.keys
: sbot.metafeeds.keys.deriveFeedKeyFromSeed(
seed,
nonce.toString('base64'),
feedformat
)
return {
metafeed: msg.value.author,
feedformat,
feedpurpose,
subfeed,
keys,
metadata,
seed: !existing ? seed : undefined,
}
},
/**
* Gets the current state (active feeds) of a meta feed.
*
* ```js
* sbot.metafeeds.query.hydrate(mfKey.id, (err, hydrated) => {
* console.log(hydrated.feeds) // the feeds
* console.log(hydrated.feeds[0].feedpurpose) // 'main'
* })
* ```
*/
hydrate(feedId, seed, cb) {
sbot.db.query(
where(author(feedId)),
toCallback((err, msgs) => {
if (err) return cb(err)
const validatedMsgs = msgs.filter((msg) => validate.isValid(msg))
const addedFeeds = validatedMsgs
.filter((msg) => msg.value.content.type.startsWith('metafeed/add/'))
.map((msg) => self.hydrateFromMsg(msg, seed))
const tombstoned = validatedMsgs
.filter((msg) => msg.value.content.type === 'metafeed/tombstone')
.map((msg) => {
const { feedpurpose, subfeed } = msg.value.content
const metadata = self.collectMetadata(msg)
return { feedpurpose, subfeed, metadata }
})
const feeds = addedFeeds.filter(
// allow only feeds that have not been tombstoned
(feed) => !tombstoned.find((t) => t.subfeed === feed.subfeed)
)
cb(null, { feeds, tombstoned })
})
)
},
}
return self
}