plugin.js knows less about feed-v1

This commit is contained in:
Andre Staltz 2023-04-15 16:29:01 +03:00
parent babfd5e39f
commit 7e90efd48d
4 changed files with 20 additions and 14 deletions

View File

@ -16,6 +16,7 @@ const {
validateBatch, validateBatch,
validateMsgHash, validateMsgHash,
} = require('./validation') } = require('./validation')
const { isEmptyObject } = require('../utils')
const Tangle = require('../tangle') const Tangle = require('../tangle')
/** /**
@ -91,6 +92,12 @@ const Tangle = require('../tangle')
// return author.startsWith('ppppp:feed/v1/') // return author.startsWith('ppppp:feed/v1/')
// } // }
function isFeedRoot(msg, authorId, findType) {
const findWho = stripAuthor(authorId)
const { who, type, tangles } = msg.metadata
return who === findWho && type === findType && isEmptyObject(tangles)
}
function toPlaintextBuffer(opts) { function toPlaintextBuffer(opts) {
return Buffer.from(stringify(opts.content), 'utf8') return Buffer.from(stringify(opts.content), 'utf8')
} }
@ -190,6 +197,7 @@ function fromPlaintextBuffer(plaintextBuf, msg) {
module.exports = { module.exports = {
getMsgHash, getMsgHash,
getMsgId, getMsgId,
isFeedRoot,
// getFeedId, // getFeedId,
// isFeedId, // isFeedId,
// isMsg, // isMsg,

View File

@ -13,7 +13,12 @@ function stripMsgKey(msgKey) {
} }
} }
/**
* @param {string} id
* @returns {string}
*/
function stripAuthor(id) { function stripAuthor(id) {
if (id.startsWith('ppppp:feed/v1/') === false) return id
const withoutPrefix = id.replace('ppppp:feed/v1/', '') const withoutPrefix = id.replace('ppppp:feed/v1/', '')
return withoutPrefix.split('/')[0] return withoutPrefix.split('/')[0]
} }

View File

@ -5,7 +5,7 @@ const promisify = require('promisify-4loc')
const Obz = require('obz') const Obz = require('obz')
const FeedV1 = require('./feed-v1') const FeedV1 = require('./feed-v1')
const Tangle = require('./tangle') const Tangle = require('./tangle')
const { ReadyGate, isEmptyObject } = require('./utils') const { ReadyGate } = require('./utils')
const { decrypt } = require('./encryption') const { decrypt } = require('./encryption')
/** /**
@ -169,14 +169,8 @@ exports.init = function initDB(peer, config) {
} }
function getFeedRoot(findWho, findType) { function getFeedRoot(findWho, findType) {
const findWho_ = findWho.startsWith('ppppp:feed/v1/')
? findWho.substring(16)
: findWho
for (const rec of records()) { for (const rec of records()) {
const { who, type, tangles } = rec.msg.metadata if (FeedV1.isFeedRoot(rec.msg, findWho, findType)) return rec.hash
if (who === findWho_ && type === findType && isEmptyObject(tangles)) {
return rec.hash
}
} }
return null return null
} }

View File

@ -25,5 +25,4 @@ class ReadyGate {
return true return true
} }
module.exports = { ReadyGate, isEmptyObject } module.exports = { ReadyGate, isEmptyObject }