135 lines
2.6 KiB
TypeScript
135 lines
2.6 KiB
TypeScript
import { APIClient } from "../ApiClient";
|
|
import type { Room } from "./MatrixApiRoom";
|
|
|
|
export interface MatrixRoomMessage {
|
|
type: "m.room.message";
|
|
content: {
|
|
body: string;
|
|
msgtype: "m.text" | "m.image" | string;
|
|
"m.relates_to"?: {
|
|
event_id: string;
|
|
rel_type: "m.replace" | string;
|
|
};
|
|
file?: {
|
|
url: string;
|
|
};
|
|
};
|
|
}
|
|
|
|
export interface MatrixReaction {
|
|
type: "m.reaction";
|
|
content: {
|
|
"m.relates_to": {
|
|
event_id: string;
|
|
key: string;
|
|
};
|
|
};
|
|
}
|
|
|
|
export interface MatrixRoomRedaction {
|
|
type: "m.room.redaction";
|
|
redacts: string;
|
|
}
|
|
|
|
export type MatrixEventData =
|
|
| MatrixRoomMessage
|
|
| MatrixReaction
|
|
| MatrixRoomRedaction
|
|
| { type: "other" };
|
|
|
|
export interface MatrixEvent {
|
|
id: string;
|
|
time: number;
|
|
sender: string;
|
|
data: MatrixEventData;
|
|
}
|
|
|
|
export interface MatrixEventsList {
|
|
start: string;
|
|
end?: string;
|
|
events: MatrixEvent[];
|
|
}
|
|
|
|
export class MatrixApiEvent {
|
|
/**
|
|
* Get Matrix room events
|
|
*/
|
|
static async GetRoomEvents(
|
|
room: Room,
|
|
from?: string
|
|
): Promise<MatrixEventsList> {
|
|
return (
|
|
await APIClient.exec({
|
|
method: "GET",
|
|
uri:
|
|
`/matrix/room/${encodeURIComponent(room.id)}/events` +
|
|
(from ? `?from=${from}` : ""),
|
|
})
|
|
).data;
|
|
}
|
|
|
|
/**
|
|
* Get Matrix event file URL
|
|
*/
|
|
static GetEventFileURL(
|
|
room: Room,
|
|
event_id: string,
|
|
thumbnail: boolean
|
|
): string {
|
|
return `${APIClient.ActualBackendURL()}/matrix/room/${
|
|
room.id
|
|
}/event/${event_id}/file?thumbnail=${thumbnail}`;
|
|
}
|
|
|
|
/**
|
|
* Send text message
|
|
*/
|
|
static async SendTextMessage(room: Room, content: string): Promise<void> {
|
|
await APIClient.exec({
|
|
method: "POST",
|
|
uri: `/matrix/room/${room.id}/send_text_message`,
|
|
jsonData: { content },
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Edit text message content
|
|
*/
|
|
static async SetTextMessageContent(
|
|
room: Room,
|
|
event_id: string,
|
|
content: string
|
|
): Promise<void> {
|
|
await APIClient.exec({
|
|
method: "POST",
|
|
uri: `/matrix/room/${room.id}/event/${event_id}/set_text_content`,
|
|
jsonData: { content },
|
|
});
|
|
}
|
|
|
|
/**
|
|
* React to event
|
|
*/
|
|
static async ReactToEvent(
|
|
room: Room,
|
|
event_id: string,
|
|
key: string
|
|
): Promise<void> {
|
|
await APIClient.exec({
|
|
method: "POST",
|
|
uri: `/matrix/room/${room.id}/event/${event_id}/react`,
|
|
jsonData: { key },
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Delete an event
|
|
*/
|
|
static async DeleteEvent(room: Room, event_id: string): Promise<void> {
|
|
await APIClient.exec({
|
|
method: "DELETE",
|
|
uri: `/matrix/room/${room.id}/event/${event_id}`,
|
|
});
|
|
}
|
|
}
|