(
message: Element,
{
stamp,
stanzaId,
isArchived = false,
}: { stamp?: string; stanzaId?: string; isArchived?: boolean } = {},
)
| 96 | // A groupchat message with a body, live or from the room's archive. The |
| 97 | // archive passes its own stamp and id, since the forwarded copy may lack them. |
| 98 | export const parseRoomMessage = ( |
| 99 | message: Element, |
| 100 | { |
| 101 | stamp, |
| 102 | stanzaId, |
| 103 | isArchived = false, |
| 104 | }: { stamp?: string; stanzaId?: string; isArchived?: boolean } = {}, |
| 105 | ): RawRoomMessage | null => { |
| 106 | if (message.getAttribute('type') !== 'groupchat') { |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | const body = childOf(message, 'body')?.textContent; |
| 111 | const occupant = splitOccupant(message.getAttribute('from')); |
| 112 | // Subject changes and bodyless stanzas (chat states) are not messages. |
| 113 | if (!body || !occupant || childOf(message, 'subject')) { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | const createdAt = |
| 118 | stamp ?? |
| 119 | childOf(message, 'delay', NS_DELAY)?.getAttribute('stamp') ?? |
| 120 | new Date().toISOString(); |
| 121 | const originId = childOf(message, 'origin-id', NS_SID)?.getAttribute('id'); |
| 122 | |
| 123 | return { |
| 124 | // The origin id is what the sender's optimistic copy carries, so the |
| 125 | // room's reflection of it replaces that copy instead of doubling it. |
| 126 | id: originId ?? message.getAttribute('id') ?? stanzaId ?? createdAt, |
| 127 | roomJid: occupant.roomJid, |
| 128 | nick: occupant.nick, |
| 129 | occupantId: occupantIdOf(message), |
| 130 | realJid: isArchived ? realJidOf(message) : undefined, |
| 131 | isArchived, |
| 132 | body, |
| 133 | createdAt, |
| 134 | }; |
| 135 | }; |
| 136 | |
| 137 | export type RoomJoinFailure = 'conflict' | 'forbidden' | 'gone' | 'unknown'; |
| 138 |
no test coverage detected