From 7e90efd48dde26d06e036300ae310056c5f9a1a5 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Sat, 15 Apr 2023 16:29:01 +0300 Subject: [PATCH] plugin.js knows less about feed-v1 --- lib/feed-v1/index.js | 8 ++++++++ lib/feed-v1/strip.js | 5 +++++ lib/plugin.js | 10 ++-------- lib/utils.js | 11 +++++------ 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/feed-v1/index.js b/lib/feed-v1/index.js index aebe136..d694381 100644 --- a/lib/feed-v1/index.js +++ b/lib/feed-v1/index.js @@ -16,6 +16,7 @@ const { validateBatch, validateMsgHash, } = require('./validation') +const { isEmptyObject } = require('../utils') const Tangle = require('../tangle') /** @@ -91,6 +92,12 @@ const Tangle = require('../tangle') // 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) { return Buffer.from(stringify(opts.content), 'utf8') } @@ -190,6 +197,7 @@ function fromPlaintextBuffer(plaintextBuf, msg) { module.exports = { getMsgHash, getMsgId, + isFeedRoot, // getFeedId, // isFeedId, // isMsg, diff --git a/lib/feed-v1/strip.js b/lib/feed-v1/strip.js index fda8afa..36fe310 100644 --- a/lib/feed-v1/strip.js +++ b/lib/feed-v1/strip.js @@ -13,7 +13,12 @@ function stripMsgKey(msgKey) { } } +/** + * @param {string} id + * @returns {string} + */ function stripAuthor(id) { + if (id.startsWith('ppppp:feed/v1/') === false) return id const withoutPrefix = id.replace('ppppp:feed/v1/', '') return withoutPrefix.split('/')[0] } diff --git a/lib/plugin.js b/lib/plugin.js index dd3fc60..69cf7d5 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -5,7 +5,7 @@ const promisify = require('promisify-4loc') const Obz = require('obz') const FeedV1 = require('./feed-v1') const Tangle = require('./tangle') -const { ReadyGate, isEmptyObject } = require('./utils') +const { ReadyGate } = require('./utils') const { decrypt } = require('./encryption') /** @@ -169,14 +169,8 @@ exports.init = function initDB(peer, config) { } function getFeedRoot(findWho, findType) { - const findWho_ = findWho.startsWith('ppppp:feed/v1/') - ? findWho.substring(16) - : findWho for (const rec of records()) { - const { who, type, tangles } = rec.msg.metadata - if (who === findWho_ && type === findType && isEmptyObject(tangles)) { - return rec.hash - } + if (FeedV1.isFeedRoot(rec.msg, findWho, findType)) return rec.hash } return null } diff --git a/lib/utils.js b/lib/utils.js index 6d7ce5a..d2cb0ef 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -18,12 +18,11 @@ class ReadyGate { } } - function isEmptyObject(obj) { - for (const _key in obj) { - return false - } - return true +function isEmptyObject(obj) { + for (const _key in obj) { + return false } - + return true +} module.exports = { ReadyGate, isEmptyObject }