Basic WS sync
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import dayjs from "dayjs";
|
||||
import type {
|
||||
MatrixEvent,
|
||||
MatrixEventData,
|
||||
MatrixEventsList,
|
||||
} from "../api/matrix/MatrixApiEvent";
|
||||
import type { Room } from "../api/matrix/MatrixApiRoom";
|
||||
import type { WsMessage } from "../api/WsApi";
|
||||
|
||||
export interface MessageReaction {
|
||||
event_id: string;
|
||||
@@ -45,7 +47,62 @@ export class RoomEventsManager {
|
||||
this.rebuildMessagesList();
|
||||
}
|
||||
|
||||
processWsMessage(m: WsMessage) {
|
||||
if (m.room_id !== this.room.id) return false;
|
||||
|
||||
let data: MatrixEventData;
|
||||
if (m.type === "RoomReactionEvent") {
|
||||
data = {
|
||||
type: "m.reaction",
|
||||
content: {
|
||||
"m.relates_to": {
|
||||
key: m.data["m.relates_to"].key,
|
||||
event_id: m.data["m.relates_to"].event_id,
|
||||
},
|
||||
},
|
||||
};
|
||||
} else if (m.type === "RoomRedactionEvent") {
|
||||
data = {
|
||||
type: "m.room.redaction",
|
||||
redacts: m.data.redacts,
|
||||
};
|
||||
} else if (m.type === "RoomMessageEvent") {
|
||||
data = {
|
||||
type: "m.room.message",
|
||||
content: {
|
||||
body: m.data["m.new_content"]?.body ?? m.data.body,
|
||||
msgtype: m.data.msgtype,
|
||||
"m.relates_to":
|
||||
m.data["m.relates_to"] && m.data["m.relates_to"].event_id
|
||||
? {
|
||||
event_id: m.data["m.relates_to"].event_id!,
|
||||
rel_type: m.data["m.relates_to"].rel_type ?? "",
|
||||
}
|
||||
: undefined,
|
||||
file: m.data.file,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
// Ignore event
|
||||
console.info("Event not supported => ignored");
|
||||
return false;
|
||||
}
|
||||
|
||||
this.events.push({
|
||||
sender: m.sender,
|
||||
id: m.event_id,
|
||||
time: m.origin_server_ts,
|
||||
data,
|
||||
});
|
||||
|
||||
this.rebuildMessagesList();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private rebuildMessagesList() {
|
||||
this.messages = [];
|
||||
|
||||
// Sorts events list to process oldest events first
|
||||
this.events.sort((a, b) => a.time - b.time);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user