MsgV3.isFeedMsg() API

This commit is contained in:
Andre Staltz 2023-12-14 15:58:49 +02:00
parent e86da204cb
commit f523df3a0e
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
2 changed files with 34 additions and 0 deletions

View File

@ -49,6 +49,26 @@ const { isEmptyObject } = require('./util')
* }} Msg * }} Msg
*/ */
/**
* @template [T=any]
* @typedef {{
* data: T;
* metadata: {
* dataHash: string;
* dataSize: number;
* account: string;
* accountTips: Array<string>;
* tangles: {
* [tangleID in string]: TangleMetadata
* };
* domain: string;
* v: 3;
* };
* pubkey: string;
* sig: string;
* }} FeedMsg
*/
/** /**
* @typedef {Iterator<Msg> & {values: () => Iterator<Msg>}} MsgIter * @typedef {Iterator<Msg> & {values: () => Iterator<Msg>}} MsgIter
* *
@ -280,6 +300,16 @@ function isRoot(msg) {
return isEmptyObject(msg.metadata.tangles) return isEmptyObject(msg.metadata.tangles)
} }
/**
* @template T
* @param {Msg<T>} msg
* @returns {msg is FeedMsg<T>}
*/
function isFeedMsg(msg) {
const {account, accountTips} = msg.metadata
return Array.isArray(accountTips) && account !== 'self' && account !== 'any'
}
/** /**
* @param {any} x * @param {any} x
* @returns {x is Msg} * @returns {x is Msg}
@ -292,6 +322,7 @@ module.exports = {
isMsg, isMsg,
isMoot, isMoot,
isRoot, isRoot,
isFeedMsg,
getMsgID, getMsgID,
getMootID, getMootID,
create, create,

View File

@ -32,6 +32,7 @@ test('MsgV3.createAccount()', (t) => {
assert.equal(accountMsg0.metadata.domain, 'person', 'domain') assert.equal(accountMsg0.metadata.domain, 'person', 'domain')
assert.equal(accountMsg0.metadata.v, 3, 'v') assert.equal(accountMsg0.metadata.v, 3, 'v')
assert.equal(accountMsg0.pubkey, keypair.public, 'pubkey') assert.equal(accountMsg0.pubkey, keypair.public, 'pubkey')
assert.equal(MsgV3.isFeedMsg(accountMsg0), false, 'not a feed msg')
account = MsgV3.getMsgID(accountMsg0) account = MsgV3.getMsgID(accountMsg0)
assert.equal(account, 'UQN1Qmxr4rr9nCMQKs9u8P', 'account ID') 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.domain, 'post', 'domain')
assert.equal(moot.metadata.v, 3, 'v') assert.equal(moot.metadata.v, 3, 'v')
assert.equal(moot.pubkey, keypair.public, 'pubkey') assert.equal(moot.pubkey, keypair.public, 'pubkey')
assert.equal(MsgV3.isFeedMsg(moot), false, 'not a feed msg')
mootID = MsgV3.getMsgID(moot) mootID = MsgV3.getMsgID(moot)
assert.equal(mootID, 'AP2rJSfm9TwpNcMmbUsnRa', 'moot ID') assert.equal(mootID, 'AP2rJSfm9TwpNcMmbUsnRa', 'moot ID')
@ -119,6 +121,7 @@ test('MsgV3.create()', (t) => {
'rh8bc8QY7ju7yi4rt6y9njCyS3TVV1SBjn5dWGpKKRrC3XDMBc9KeNJgVCJLK8b8uiU5F49avAWt35P9kNaWZYH', 'rh8bc8QY7ju7yi4rt6y9njCyS3TVV1SBjn5dWGpKKRrC3XDMBc9KeNJgVCJLK8b8uiU5F49avAWt35P9kNaWZYH',
'sig' 'sig'
) )
assert.equal(MsgV3.isFeedMsg(msg1), true, 'is a feed msg')
const msgID1 = 'MUvfNDk3gMPRy9CpTDEuvW' const msgID1 = 'MUvfNDk3gMPRy9CpTDEuvW'