mirror of https://codeberg.org/pzp/pzp-db.git
refactor: use initializeFeed() inside create()
This commit is contained in:
parent
9dd5f92ce8
commit
d39ee6b017
96
lib/index.js
96
lib/index.js
|
@ -237,58 +237,52 @@ exports.init = function initDB(peer, config) {
|
|||
if (!opts.content) return cb(new Error('create() requires a `content`'))
|
||||
if (!opts.type) return cb(new Error('create() requires a `type`'))
|
||||
|
||||
const feedRootHash = getFeedRoot(FeedV1.stripAuthor(keys.id), opts.type)
|
||||
if (!feedRootHash) {
|
||||
const feedRoot = FeedV1.createRoot(keys, opts.type)
|
||||
add(feedRoot, FeedV1.getMsgHash(feedRoot), (err) => {
|
||||
// prettier-ignore
|
||||
if (err) return cb(new Error('create() failed to create root', {cause: err}));
|
||||
create(opts, cb)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Fill-in tangle opts:
|
||||
const tangleTemplates = opts.tangles ?? []
|
||||
tangleTemplates.push(feedRootHash)
|
||||
const tangles = populateTangles(tangleTemplates)
|
||||
const fullOpts = { ...opts, tangles, keys }
|
||||
|
||||
// If opts ask for encryption, encrypt and put ciphertext in opts.content
|
||||
const recps = fullOpts.content.recps
|
||||
if (Array.isArray(recps) && recps.length > 0) {
|
||||
const plaintext = FeedV1.toPlaintextBuffer(fullOpts)
|
||||
const encryptOpts = { ...fullOpts, recps }
|
||||
let ciphertextBuf
|
||||
try {
|
||||
ciphertextBuf = encryptionFormat.encrypt(plaintext, encryptOpts)
|
||||
} catch (err) {
|
||||
// prettier-ignore
|
||||
return cb(new Error('create() failed to encrypt content', {cause: err}));
|
||||
}
|
||||
if (!ciphertextBuf) {
|
||||
// prettier-ignore
|
||||
return cb(new Error('create() failed to encrypt with ' + encryptionFormat.name))
|
||||
}
|
||||
const ciphertextBase64 = ciphertextBuf.toString('base64')
|
||||
fullOpts.content = ciphertextBase64 + '.' + encryptionFormat.name
|
||||
}
|
||||
|
||||
// Create the actual message:
|
||||
let msg
|
||||
try {
|
||||
msg = FeedV1.create(fullOpts)
|
||||
} catch (err) {
|
||||
return cb(new Error('create() failed', { cause: err }))
|
||||
}
|
||||
const msgHash = FeedV1.getMsgHash(msg)
|
||||
|
||||
// Encode the native message and append it to the log:
|
||||
logAppend(msgHash, msg, (err, rec) => {
|
||||
initializeFeed(opts, (err, feedRootHash) => {
|
||||
// prettier-ignore
|
||||
if (err) return cb(new Error('create() failed to append the log', { cause: err }))
|
||||
onRecordAdded.set(rec)
|
||||
cb(null, rec)
|
||||
if (err) return cb(new Error('create() failed to initialize feed', { cause: err }));
|
||||
|
||||
// Fill-in tangle opts:
|
||||
const tangleTemplates = opts.tangles ?? []
|
||||
tangleTemplates.push(feedRootHash)
|
||||
const tangles = populateTangles(tangleTemplates)
|
||||
const fullOpts = { ...opts, tangles, keys }
|
||||
|
||||
// If opts ask for encryption, encrypt and put ciphertext in opts.content
|
||||
const recps = fullOpts.content.recps
|
||||
if (Array.isArray(recps) && recps.length > 0) {
|
||||
const plaintext = FeedV1.toPlaintextBuffer(fullOpts)
|
||||
const encryptOpts = { ...fullOpts, recps }
|
||||
let ciphertextBuf
|
||||
try {
|
||||
ciphertextBuf = encryptionFormat.encrypt(plaintext, encryptOpts)
|
||||
} catch (err) {
|
||||
// prettier-ignore
|
||||
return cb(new Error('create() failed to encrypt content', {cause: err}));
|
||||
}
|
||||
if (!ciphertextBuf) {
|
||||
// prettier-ignore
|
||||
return cb(new Error('create() failed to encrypt with ' + encryptionFormat.name))
|
||||
}
|
||||
const ciphertextBase64 = ciphertextBuf.toString('base64')
|
||||
fullOpts.content = ciphertextBase64 + '.' + encryptionFormat.name
|
||||
}
|
||||
|
||||
// Create the actual message:
|
||||
let msg
|
||||
try {
|
||||
msg = FeedV1.create(fullOpts)
|
||||
} catch (err) {
|
||||
return cb(new Error('create() failed', { cause: err }))
|
||||
}
|
||||
const msgHash = FeedV1.getMsgHash(msg)
|
||||
|
||||
// Encode the native message and append it to the log:
|
||||
logAppend(msgHash, msg, (err, rec) => {
|
||||
// prettier-ignore
|
||||
if (err) return cb(new Error('create() failed to append the log', { cause: err }))
|
||||
onRecordAdded.set(rec)
|
||||
cb(null, rec)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue