pzp-db/lib/msg-v3/strip.js

37 lines
727 B
JavaScript

const { getMsgHash } = require('./get-msg-id')
/**
* @typedef {import('.').Msg} Msg
*/
/**
* @param {any} msgKey
*/
function stripMsgKey(msgKey) {
if (typeof msgKey === 'object') {
if (msgKey.key) return stripMsgKey(msgKey.key)
else return getMsgHash(msgKey)
}
if (msgKey.startsWith('ppppp:message/v3/')) {
const parts = msgKey.split('/')
return parts[parts.length - 1]
} else {
return msgKey
}
}
/**
* @param {string} id
* @returns {string}
*/
function stripAccount(id) {
if (id.startsWith('ppppp:account/v3/') === false) return id
const withoutPrefix = id.replace('ppppp:account/v3/', '')
return withoutPrefix.split('/')[0]
}
module.exports = {
stripMsgKey,
stripAccount,
}