MCPcopy Create free account
hub / github.com/dailydotdev/apps / parseChatMessage

Function parseChatMessage

packages/shared/src/features/messages/stanzas.ts:109–150  ·  view source on GitHub ↗
(
  message: Element,
  { ownBareJid, stanzaId, stamp }: ParseOptions,
)

Source from the content-addressed store, hash-verified

107};
108
109export const parseChatMessage = (
110 message: Element,
111 { ownBareJid, stanzaId, stamp }: ParseOptions,
112): DmMessage | null => {
113 const body = textOf(message, 'body');
114 const parties = body && getParties(message, ownBareJid);
115
116 // A correction (XEP-0308) changes an earlier message instead of adding one;
117 // parseCorrection reads it.
118 if (!parties || childOf(message, 'replace', NS_CORRECT)) {
119 return null;
120 }
121
122 const context = parseCommentRef(message);
123 const originId = childOf(message, 'origin-id', NS_SID)?.getAttribute('id');
124 const serverId = Array.from(message.children)
125 .find(
126 (child) =>
127 child.localName === 'stanza-id' &&
128 bareJid(child.getAttribute('by') ?? '') === ownBareJid,
129 )
130 ?.getAttribute('id');
131
132 return {
133 // The origin id is ours and survives archiving, so it matches the
134 // optimistic bubble; the server id is the fallback for other clients.
135 id:
136 originId ??
137 stanzaId ??
138 serverId ??
139 message.getAttribute('id') ??
140 `${parties.senderId}-${stamp ?? Date.now()}`,
141 ...parties,
142 body,
143 createdAt:
144 stamp ??
145 childOf(message, 'delay', NS_DELAY)?.getAttribute('stamp') ??
146 new Date().toISOString(),
147 status: DmMessageStatus.Sent,
148 ...(context && { context }),
149 };
150};
151
152const maxEmojiLength = 32;
153const emojiPattern =

Callers 3

stanzas.spec.tsFile · 0.90
onMessageFunction · 0.90
queryArchiveFunction · 0.90

Calls 6

textOfFunction · 0.85
getPartiesFunction · 0.85
childOfFunction · 0.85
parseCommentRefFunction · 0.85
bareJidFunction · 0.85
nowMethod · 0.80

Tested by

no test coverage detected