From f523df3a0ef93f75941d6d317d17daffb4713800 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Thu, 14 Dec 2023 15:58:49 +0200 Subject: [PATCH] MsgV3.isFeedMsg() API --- lib/msg-v3/index.js | 31 +++++++++++++++++++++++++++++++ test/msg-v3/create.test.js | 3 +++ 2 files changed, 34 insertions(+) diff --git a/lib/msg-v3/index.js b/lib/msg-v3/index.js index 6363068..b48d3d1 100644 --- a/lib/msg-v3/index.js +++ b/lib/msg-v3/index.js @@ -49,6 +49,26 @@ const { isEmptyObject } = require('./util') * }} Msg */ +/** + * @template [T=any] + * @typedef {{ + * data: T; + * metadata: { + * dataHash: string; + * dataSize: number; + * account: string; + * accountTips: Array; + * tangles: { + * [tangleID in string]: TangleMetadata + * }; + * domain: string; + * v: 3; + * }; + * pubkey: string; + * sig: string; + * }} FeedMsg + */ + /** * @typedef {Iterator & {values: () => Iterator}} MsgIter * @@ -280,6 +300,16 @@ function isRoot(msg) { return isEmptyObject(msg.metadata.tangles) } +/** + * @template T + * @param {Msg} msg + * @returns {msg is FeedMsg} + */ +function isFeedMsg(msg) { + const {account, accountTips} = msg.metadata + return Array.isArray(accountTips) && account !== 'self' && account !== 'any' +} + /** * @param {any} x * @returns {x is Msg} @@ -292,6 +322,7 @@ module.exports = { isMsg, isMoot, isRoot, + isFeedMsg, getMsgID, getMootID, create, diff --git a/test/msg-v3/create.test.js b/test/msg-v3/create.test.js index a5698b8..1055ce9 100644 --- a/test/msg-v3/create.test.js +++ b/test/msg-v3/create.test.js @@ -32,6 +32,7 @@ test('MsgV3.createAccount()', (t) => { assert.equal(accountMsg0.metadata.domain, 'person', 'domain') assert.equal(accountMsg0.metadata.v, 3, 'v') assert.equal(accountMsg0.pubkey, keypair.public, 'pubkey') + assert.equal(MsgV3.isFeedMsg(accountMsg0), false, 'not a feed msg') account = MsgV3.getMsgID(accountMsg0) assert.equal(account, 'UQN1Qmxr4rr9nCMQKs9u8P', 'account ID') @@ -54,6 +55,7 @@ test('MsgV3.createMoot()', (t) => { assert.equal(moot.metadata.domain, 'post', 'domain') assert.equal(moot.metadata.v, 3, 'v') assert.equal(moot.pubkey, keypair.public, 'pubkey') + assert.equal(MsgV3.isFeedMsg(moot), false, 'not a feed msg') mootID = MsgV3.getMsgID(moot) assert.equal(mootID, 'AP2rJSfm9TwpNcMmbUsnRa', 'moot ID') @@ -119,6 +121,7 @@ test('MsgV3.create()', (t) => { 'rh8bc8QY7ju7yi4rt6y9njCyS3TVV1SBjn5dWGpKKRrC3XDMBc9KeNJgVCJLK8b8uiU5F49avAWt35P9kNaWZYH', 'sig' ) + assert.equal(MsgV3.isFeedMsg(msg1), true, 'is a feed msg') const msgID1 = 'MUvfNDk3gMPRy9CpTDEuvW'