UNPKG

@xmtp/content-type-reaction

Version:

An XMTP content type to support reactions to messages

64 lines (61 loc) 2 kB
import { ContentTypeId } from '@xmtp/content-type-primitives'; const ContentTypeReaction = new ContentTypeId({ authorityId: "xmtp.org", typeId: "reaction", versionMajor: 1, versionMinor: 0, }); class ReactionCodec { get contentType() { return ContentTypeReaction; } encode(reaction) { const { action, reference, referenceInboxId, schema, content } = reaction; return { type: this.contentType, parameters: {}, content: new TextEncoder().encode(JSON.stringify({ action, reference, referenceInboxId, schema, content, })), }; } decode(encodedContent) { const decodedContent = new TextDecoder().decode(encodedContent.content); // First try to decode it in the canonical form. try { const reaction = JSON.parse(decodedContent); const { action, reference, referenceInboxId, schema, content } = reaction; return { action, reference, referenceInboxId, schema, content }; } catch { // ignore, fall through to legacy decoding } // If that fails, try to decode it in the legacy form. const parameters = encodedContent.parameters; return { action: parameters.action, reference: parameters.reference, schema: parameters.schema, content: decodedContent, }; } fallback(content) { switch (content.action) { case "added": return `Reacted “${content.content}” to an earlier message`; case "removed": return `Removed “${content.content}” from an earlier message`; default: return undefined; } } shouldPush() { return false; } } export { ContentTypeReaction, ReactionCodec }; //# sourceMappingURL=index.js.map