(
message: Element,
{ ownBareJid, stanzaId, stamp }: ParseOptions,
)
| 107 | }; |
| 108 | |
| 109 | export 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 | |
| 152 | const maxEmojiLength = 32; |
| 153 | const emojiPattern = |
no test coverage detected