1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Keep unknown fields when adding extensible formats (#2105)

* Keep unknown fields when adding extensible formats

This also properly handles `m.new_content` to also be extensible.

* appease the linter
This commit is contained in:
Travis Ralston
2022-01-13 13:17:46 -07:00
committed by GitHub
parent 016e24472a
commit 129fb7f10f

View File

@@ -20,7 +20,7 @@ limitations under the License.
*/ */
import { EventEmitter } from "events"; import { EventEmitter } from "events";
import { EmoteEvent, MessageEvent, NoticeEvent } from "matrix-events-sdk"; import { EmoteEvent, MessageEvent, NoticeEvent, IPartialEvent } from "matrix-events-sdk";
import { ISyncStateData, SyncApi, SyncState } from "./sync"; import { ISyncStateData, SyncApi, SyncState } from "./sync";
import { EventStatus, IContent, IDecryptOptions, IEvent, MatrixEvent } from "./models/event"; import { EventStatus, IContent, IDecryptOptions, IEvent, MatrixEvent } from "./models/event";
@@ -3948,18 +3948,39 @@ export class MatrixClient extends EventEmitter {
// reasonably large pool of messages to parse. // reasonably large pool of messages to parse.
let eventType: string = EventType.RoomMessage; let eventType: string = EventType.RoomMessage;
let sendContent: IContent = content as IContent; let sendContent: IContent = content as IContent;
if (sendContent['msgtype'] === MsgType.Text) { const makeContentExtensible = (content: IContent, recurse = true): IPartialEvent<object> => {
const serialized = MessageEvent.from(sendContent['body'], sendContent['formatted_body']).serialize(); let newEvent: IPartialEvent<object> = null;
eventType = serialized.type;
sendContent = serialized.content; if (content['msgtype'] === MsgType.Text) {
} else if (sendContent['msgtype'] === MsgType.Emote) { newEvent = MessageEvent.from(content['body'], content['formatted_body']).serialize();
const serialized = EmoteEvent.from(sendContent['body'], sendContent['formatted_body']).serialize(); } else if (content['msgtype'] === MsgType.Emote) {
eventType = serialized.type; newEvent = EmoteEvent.from(content['body'], content['formatted_body']).serialize();
sendContent = serialized.content; } else if (content['msgtype'] === MsgType.Notice) {
} else if (sendContent['msgtype'] === MsgType.Notice) { newEvent = NoticeEvent.from(content['body'], content['formatted_body']).serialize();
const serialized = NoticeEvent.from(sendContent['body'], sendContent['formatted_body']).serialize(); }
eventType = serialized.type;
sendContent = serialized.content; if (newEvent && content['m.new_content'] && recurse) {
const newContent = makeContentExtensible(content['m.new_content'], false);
if (newContent) {
newEvent.content['m.new_content'] = newContent.content;
}
}
if (newEvent) {
// copy over all other fields we don't know about
for (const [k, v] of Object.entries(content)) {
if (!newEvent.content.hasOwnProperty(k)) {
newEvent.content[k] = v;
}
}
}
return newEvent;
};
const result = makeContentExtensible(sendContent);
if (result) {
eventType = result.type;
sendContent = result.content;
} }
return this.sendEvent( return this.sendEvent(