refactor: use initializeFeed() inside create()

This commit is contained in:
Andre Staltz 2023-04-28 11:27:15 +03:00
parent 9dd5f92ce8
commit d39ee6b017
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
1 changed files with 45 additions and 51 deletions

View File

@ -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)
})
})
}