mirror of https://codeberg.org/pzp/pzp-db.git
initializeFeed() API
This commit is contained in:
parent
65622ac961
commit
6ab3182613
20
lib/index.js
20
lib/index.js
|
@ -208,6 +208,22 @@ exports.init = function initDB(peer, config) {
|
|||
})
|
||||
}
|
||||
|
||||
function initializeFeed(opts, cb) {
|
||||
if (!opts.type) return cb(new Error('initializeFeed() requires a `type`'))
|
||||
const keys = opts.keys ?? config.keys
|
||||
const type = opts.type
|
||||
|
||||
const feedRootHash = getFeedRoot(FeedV1.stripAuthor(keys.id), type)
|
||||
if (feedRootHash) return cb(null, feedRootHash)
|
||||
|
||||
const feedRoot = FeedV1.createRoot(keys, type)
|
||||
add(feedRoot, FeedV1.getMsgHash(feedRoot), (err, rec) => {
|
||||
// prettier-ignore
|
||||
if (err) return cb(new Error('initializeFeed() failed to add root', { cause: err }));
|
||||
cb(null, rec.hash)
|
||||
})
|
||||
}
|
||||
|
||||
function create(opts, cb) {
|
||||
const keys = opts.keys ?? config.keys
|
||||
|
||||
|
@ -276,7 +292,8 @@ exports.init = function initDB(peer, config) {
|
|||
})
|
||||
}
|
||||
|
||||
function getFeedRoot(findWho, findType) {
|
||||
function getFeedRoot(authorId, findType) {
|
||||
const findWho = FeedV1.stripAuthor(authorId)
|
||||
for (const rec of records()) {
|
||||
if (FeedV1.isFeedRoot(rec.msg, findWho, findType)) return rec.hash
|
||||
}
|
||||
|
@ -354,6 +371,7 @@ exports.init = function initDB(peer, config) {
|
|||
installEncryptionFormat,
|
||||
loaded,
|
||||
add,
|
||||
initializeFeed,
|
||||
create,
|
||||
getFeedRoot,
|
||||
getRecord,
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
const test = require('tape')
|
||||
const path = require('path')
|
||||
const os = require('os')
|
||||
const rimraf = require('rimraf')
|
||||
const SecretStack = require('secret-stack')
|
||||
const caps = require('ssb-caps')
|
||||
const p = require('util').promisify
|
||||
const FeedV1 = require('../lib/feed-v1')
|
||||
const { generateKeypair } = require('./util')
|
||||
|
||||
const DIR = path.join(os.tmpdir(), 'ppppp-db-initializeFeed')
|
||||
rimraf.sync(DIR)
|
||||
|
||||
test('initializeFeed()', async (t) => {
|
||||
const keys = generateKeypair('alice')
|
||||
const peer = SecretStack({ appKey: caps.shs })
|
||||
.use(require('../lib'))
|
||||
.use(require('ssb-box'))
|
||||
.call(null, { keys, path: DIR })
|
||||
|
||||
await peer.db.loaded()
|
||||
|
||||
t.notOk(peer.db.getFeedRoot(keys.id, 'profile'), 'no profile feed')
|
||||
const rootHash = await p(peer.db.initializeFeed)({ type: 'profile' })
|
||||
t.pass('initialized feed')
|
||||
|
||||
const rootMsg = FeedV1.createRoot(keys, 'profile')
|
||||
t.equals(rootHash, FeedV1.getMsgHash(rootMsg), 'root hash is consistent')
|
||||
|
||||
t.ok(peer.db.getFeedRoot(keys.id, 'profile'), 'has profile feed')
|
||||
|
||||
await p(peer.close)(true)
|
||||
})
|
Loading…
Reference in New Issue