2015-12-18 00:13:49 +00:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2021-06-07 11:03:39 +01:00
|
|
|
Copyright 2015-2021 The Matrix.org Foundation C.I.C.
|
2015-12-18 00:13:49 +00:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2015-12-18 00:13:49 +00:00
|
|
|
*/
|
|
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type MatrixEvent, EventStatus, type Room, type MatrixClient } from "matrix-js-sdk/src/matrix";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-06-07 11:03:39 +01:00
|
|
|
|
2020-05-13 20:41:41 -06:00
|
|
|
import dis from "./dispatcher/dispatcher";
|
2015-11-30 17:15:57 +00:00
|
|
|
|
2019-12-19 17:57:50 -07:00
|
|
|
export default class Resend {
|
2022-01-20 09:32:15 +00:00
|
|
|
public static resendUnsentEvents(room: Room): Promise<void[]> {
|
2021-06-07 11:03:39 +01:00
|
|
|
return Promise.all(
|
|
|
|
|
room
|
|
|
|
|
.getPendingEvents()
|
|
|
|
|
.filter(function (ev: MatrixEvent) {
|
2017-03-03 10:02:08 +00:00
|
|
|
return ev.status === EventStatus.NOT_SENT;
|
2021-06-07 11:03:39 +01:00
|
|
|
})
|
|
|
|
|
.map(function (event: MatrixEvent) {
|
2023-05-30 10:36:34 +01:00
|
|
|
return Resend.resend(room.client, event);
|
2021-04-21 13:49:58 -06:00
|
|
|
}),
|
|
|
|
|
);
|
2019-12-19 17:57:50 -07:00
|
|
|
}
|
|
|
|
|
|
2022-01-20 09:32:15 +00:00
|
|
|
public static cancelUnsentEvents(room: Room): void {
|
2021-06-07 11:03:39 +01:00
|
|
|
room.getPendingEvents()
|
|
|
|
|
.filter(function (ev: MatrixEvent) {
|
2017-03-03 10:02:08 +00:00
|
|
|
return ev.status === EventStatus.NOT_SENT;
|
2021-06-07 11:03:39 +01:00
|
|
|
})
|
|
|
|
|
.forEach(function (event: MatrixEvent) {
|
2023-05-30 10:36:34 +01:00
|
|
|
Resend.removeFromQueue(room.client, event);
|
2017-02-27 13:39:12 +00:00
|
|
|
});
|
2019-12-19 17:57:50 -07:00
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:36:34 +01:00
|
|
|
public static resend(client: MatrixClient, event: MatrixEvent): Promise<void> {
|
|
|
|
|
const room = client.getRoom(event.getRoomId())!;
|
|
|
|
|
return client.resendEvent(event, room).then(
|
|
|
|
|
function (res) {
|
|
|
|
|
dis.dispatch({
|
|
|
|
|
action: "message_sent",
|
|
|
|
|
event: event,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
function (err: Error) {
|
|
|
|
|
// XXX: temporary logging to try to diagnose
|
|
|
|
|
// https://github.com/vector-im/element-web/issues/3148
|
|
|
|
|
logger.log("Resend got send failure: " + err.name + "(" + err + ")");
|
|
|
|
|
},
|
|
|
|
|
);
|
2019-12-19 17:57:50 -07:00
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:36:34 +01:00
|
|
|
public static removeFromQueue(client: MatrixClient, event: MatrixEvent): void {
|
|
|
|
|
client.cancelPendingEvent(event);
|
2019-12-19 17:57:50 -07:00
|
|
|
}
|
|
|
|
|
}
|