Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to ssb-db2 version 5.0.0 #68

Merged
merged 3 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
# SPDX-License-Identifier: Unlicense

node_modules
pnpm-lock.yaml
pnpm-lock.yaml
coverage
.nyc_output
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ classic `main` feed, this library will auto-link that main feed in as a "subfeed
**Prerequisites:**

- Requires **Node.js 10** or higher
- Requires `ssb-db2`
- Requires `ssb-db2` version 5.0 or higher
- Requires `ssb-bendy-butt` version 1.0 or higher

```
npm install --save ssb-meta-feeds
Expand Down
92 changes: 26 additions & 66 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

const run = require('promisify-tuple')
const debug = require('debug')('ssb:meta-feeds')
const validate = require('./validate')

const alwaysTrue = () => true

Expand Down Expand Up @@ -76,43 +75,19 @@ exports.init = function (sbot, config) {
const cb = maybeCB
if (!details.feedpurpose) return cb(new Error('Missing feedpurpose'))
if (!details.feedformat) return cb(new Error('Missing feedformat'))
sbot.metafeeds.query.getLatest(metafeed.keys.id, (err, latest) => {
if (err) return cb(err)
const msgVal = sbot.metafeeds.messages.getMsgValAddDerived(
metafeed.keys,
latest,
details.feedpurpose,
metafeed.seed,
details.feedformat,
details.metadata
)

const encrypted = typeof msgVal.content === 'string'

if (!encrypted) {
const contentSection = [msgVal.content, msgVal.contentSignature]
const validationResult = validate.validateSingle(contentSection)
if (validationResult instanceof Error) return cb(validationResult)
}

sbot.db.add(msgVal, (err, addedMsg) => {
if (err) return cb(err)

if (encrypted)
sbot.db.get(addedMsg.key, (err, msgVal) => {
const msg = {
key: addedMsg.key,
value: msgVal,
}
cb(null, sbot.metafeeds.query.hydrateFromMsg(msg, metafeed.seed))
})
else {
cb(
null,
sbot.metafeeds.query.hydrateFromMsg(addedMsg, metafeed.seed)
)
}
})
const { keys, seed } = metafeed
const { feedpurpose, feedformat, metadata } = details
const opts = sbot.metafeeds.messages.optsForAddDerived(
keys,
feedpurpose,
seed,
feedformat,
metadata
)
sbot.db.create(opts, (err) => {
if (err) return cb(err)
cb(null, sbot.metafeeds.query.hydrateFromCreateOpts(opts, seed))
})
}
}
Expand All @@ -136,31 +111,18 @@ exports.init = function (sbot, config) {
}

function findAndTombstone(metafeed, visit, reason, cb) {
const { getLatest } = sbot.metafeeds.query
const { getMsgValTombstone } = sbot.metafeeds.messages
const { optsForTombstone } = sbot.metafeeds.messages

find(metafeed, visit, (err, found) => {
if (err) return cb(err)
if (!found) return cb(new Error('Cannot find subfeed to tombstone'))

getLatest(metafeed.keys.id, (err, latest) => {
optsForTombstone(metafeed.keys, found.keys, reason, (err, opts) => {
if (err) return cb(err)

getMsgValTombstone(
metafeed.keys,
latest,
found.keys,
reason,
(err, msgVal) => {
if (err) return cb(err)

sbot.db.add(msgVal, (err, msg) => {
if (err) return cb(err)

cb(null, true)
})
}
)
sbot.db.create(opts, (err, msg) => {
if (err) return cb(err)
cb(null, true)
})
})
})
}
Expand Down Expand Up @@ -202,8 +164,8 @@ exports.init = function (sbot, config) {

// Pluck relevant internal APIs
const { deriveRootMetaFeedKeyFromSeed } = sbot.metafeeds.keys
const { getSeed, getAnnounces, getLatest } = sbot.metafeeds.query
const { getContentSeed, getContentAnnounce, getMsgValAddExisting } =
const { getSeed, getAnnounces } = sbot.metafeeds.query
const { optsForSeed, optsForAnnounce, optsForAddExisting } =
sbot.metafeeds.messages

// Ensure seed exists
Expand All @@ -214,8 +176,8 @@ exports.init = function (sbot, config) {
else debug('generating a seed')
const seed = sbot.metafeeds.keys.generateSeed()
const mfKeys = deriveRootMetaFeedKeyFromSeed(seed)
const content = getContentSeed(mfKeys.id, sbot.id, seed)
const [err2] = await run(sbot.db.publish)(content)
const opts = optsForSeed(mfKeys, sbot.id, seed)
const [err2] = await run(sbot.db.create)(opts)
if (err2) return cb(err2)
mf = { seed, keys: mfKeys }
} else {
Expand All @@ -229,9 +191,9 @@ exports.init = function (sbot, config) {
if (err2 || !announcements || announcements.length === 0) {
if (err2) debug('announcing meta feed on main feed because %o', err2)
else debug('announcing meta feed on main feed')
const [err3, content] = await run(getContentAnnounce)(mf.keys)
const [err3, opts] = await run(optsForAnnounce)(mf.keys, config.keys)
if (err3) return cb(err3)
const [err4] = await run(sbot.db.publish)(content)
const [err4] = await run(sbot.db.create)(opts)
if (err4) return cb(err4)
} else {
debug('announce post exists on main feed')
Expand All @@ -241,11 +203,9 @@ exports.init = function (sbot, config) {
const [err3, added] = await run(find)(mf, (f) => f.feedpurpose === 'main')
if (err3) return cb(err3)
if (!added) {
const [err4, latest] = await run(getLatest)(mf.keys.id)
if (err4) return cb(err4)
debug('adding main feed to root meta feed')
const msgVal = getMsgValAddExisting(mf.keys, latest, 'main', config.keys)
const [err5] = await run(sbot.db.add)(msgVal)
const opts = optsForAddExisting(mf.keys, 'main', config.keys)
const [err5] = await run(sbot.db.create)(opts)
if (err5) return cb(err5)
} else {
debug('main feed already added to root meta feed')
Expand Down
Loading