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

Function parseRoomMessage

packages/shared/src/features/messages/muc.ts:98–135  ·  view source on GitHub ↗
(
  message: Element,
  {
    stamp,
    stanzaId,
    isArchived = false,
  }: { stamp?: string; stanzaId?: string; isArchived?: boolean } = {},
)

Source from the content-addressed store, hash-verified

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.
98export 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
137export type RoomJoinFailure = 'conflict' | 'forbidden' | 'gone' | 'unknown';
138

Callers 3

muc.spec.tsFile · 0.90
onMessageFunction · 0.90
queryRoomArchiveFunction · 0.90

Calls 4

splitOccupantFunction · 0.85
occupantIdOfFunction · 0.85
realJidOfFunction · 0.85
childOfFunction · 0.70

Tested by

no test coverage detected