Add custom state event for "room deleted"

This commit is contained in:
N-Pex 2023-09-29 10:28:33 +02:00
parent 9e8ac6ad67
commit 876b79bee6
4 changed files with 44 additions and 8 deletions

View file

@ -91,6 +91,7 @@
</template> </template>
<script> <script>
import roomInfoMixin from "./roomInfoMixin"; import roomInfoMixin from "./roomInfoMixin";
import { STATE_EVENT_ROOM_DELETION_NOTICE } from "../plugins/utils";
export default { export default {
name: "LeaveRoomDialog", name: "LeaveRoomDialog",
@ -136,7 +137,7 @@ export default {
// Cancel the state event for deletion // Cancel the state event for deletion
this.$matrix.matrixClient.sendStateEvent( this.$matrix.matrixClient.sendStateEvent(
this.room.roomId, this.room.roomId,
"im.keanu.room_deletion_notice", STATE_EVENT_ROOM_DELETION_NOTICE,
{ status: "cancel" } { status: "cancel" }
); );
@ -146,7 +147,7 @@ export default {
// Send custom state event! // Send custom state event!
this.$matrix.matrixClient.sendStateEvent( this.$matrix.matrixClient.sendStateEvent(
this.room.roomId, this.room.roomId,
"im.keanu.room_deletion_notice", STATE_EVENT_ROOM_DELETION_NOTICE,
{ status: "delete" } { status: "delete" }
); );

View file

@ -1,4 +1,4 @@
import util from "../plugins/utils"; import util, { STATE_EVENT_ROOM_DELETION_NOTICE } from "../plugins/utils";
import MessageIncomingText from "./messages/MessageIncomingText"; import MessageIncomingText from "./messages/MessageIncomingText";
import MessageIncomingFile from "./messages/MessageIncomingFile"; import MessageIncomingFile from "./messages/MessageIncomingFile";
import MessageIncomingImage from "./messages/MessageIncomingImage.vue"; import MessageIncomingImage from "./messages/MessageIncomingImage.vue";
@ -296,9 +296,9 @@ export default {
return MessageOutgoingPoll; return MessageOutgoingPoll;
} }
case "im.keanu.room_deletion_notice": { case STATE_EVENT_ROOM_DELETION_NOTICE: {
// Custom event for notice 30 seconds before a room is deleted/purged. // Custom event for notice 30 seconds before a room is deleted/purged.
const deletionNotices = this.room.currentState.getStateEvents("im.keanu.room_deletion_notice"); const deletionNotices = this.room.currentState.getStateEvents(STATE_EVENT_ROOM_DELETION_NOTICE);
if (deletionNotices && deletionNotices.length > 0 && deletionNotices[deletionNotices.length - 1] == event) { if (deletionNotices && deletionNotices.length > 0 && deletionNotices[deletionNotices.length - 1] == event) {
// This is the latest/last one. Look at the status flag. Show nothing if it is "cancel". // This is the latest/last one. Look at the status flag. Show nothing if it is "cancel".
if (event.getContent().status != "cancel") { if (event.getContent().status != "cancel") {

View file

@ -3,6 +3,9 @@ import * as ContentHelpers from "matrix-js-sdk/lib/content-helpers";
import dataUriToBuffer from "data-uri-to-buffer"; import dataUriToBuffer from "data-uri-to-buffer";
import ImageResize from "image-resize"; import ImageResize from "image-resize";
export const STATE_EVENT_ROOM_DELETION_NOTICE = "im.keanu.room_deletion_notice";
export const STATE_EVENT_ROOM_DELETED = "im.keanu.room_deleted";
export const ROOM_TYPE_DEFAULT = "im.keanu.room_type_default"; export const ROOM_TYPE_DEFAULT = "im.keanu.room_type_default";
export const ROOM_TYPE_VOICE_MODE = "im.keanu.room_type_voice"; export const ROOM_TYPE_VOICE_MODE = "im.keanu.room_type_voice";
export const ROOM_TYPE_FILE_MODE = "im.keanu.room_type_file"; export const ROOM_TYPE_FILE_MODE = "im.keanu.room_type_file";

View file

@ -2,7 +2,7 @@ import olm from "@matrix-org/olm/olm";
global.Olm = olm; global.Olm = olm;
import * as sdk from "matrix-js-sdk"; import * as sdk from "matrix-js-sdk";
import { TimelineWindow, EventTimeline } from "matrix-js-sdk"; import { TimelineWindow, EventTimeline } from "matrix-js-sdk";
import util from "../plugins/utils"; import util, { STATE_EVENT_ROOM_DELETED } from "../plugins/utils";
import User from "../models/user"; import User from "../models/user";
const LocalStorageCryptoStore = const LocalStorageCryptoStore =
@ -376,6 +376,24 @@ export default {
} }
} }
break; break;
case STATE_EVENT_ROOM_DELETED:
{
const room = this.matrixClient.getRoom(event.getRoomId());
if (room && room.currentState) {
// Before we do anything, make sure the sender is an admin!
// Also, do not react if WE are the sender, since we are probably
// busy doing the rest of the purging process...
if (room.currentState.maySendStateEvent("m.room.power_levels", event.getSender())) {
if (event.getSender() !== this.currentUserId) {
this.leaveRoomAndNavigate(room.roomId).then(() => {
this.matrixClient.store.removeRoom(room.roomId);
})
}
}
}
}
break;
} }
this.updateNotificationCount(); this.updateNotificationCount();
}, },
@ -725,6 +743,13 @@ export default {
history_visibility: "joined", history_visibility: "joined",
}); });
}) })
.then(() => {
return this.matrixClient.sendStateEvent(
roomId,
STATE_EVENT_ROOM_DELETED,
{ status: "deleted" }
);
})
.then(() => { .then(() => {
//console.log("Purge: create timeline"); //console.log("Purge: create timeline");
return timelineWindow.load(null, 100); return timelineWindow.load(null, 100);
@ -808,6 +833,13 @@ export default {
return kickFirstMember(allMembers); return kickFirstMember(allMembers);
}) })
.then(() => {
return this.matrixClient.sendStateEvent(
roomId,
STATE_EVENT_ROOM_DELETED,
{ status: "deleted" }
);
})
.then(() => { .then(() => {
statusCallback(null); statusCallback(null);
this.matrixClient.setGlobalErrorOnUnknownDevices(oldGlobalErrorSetting); this.matrixClient.setGlobalErrorOnUnknownDevices(oldGlobalErrorSetting);
@ -908,7 +940,7 @@ export default {
* @param {*} userId * @param {*} userId
*/ */
isDirectRoomWith(room, userId) { isDirectRoomWith(room, userId) {
if (room.getJoinRule() == "invite" && room.getMembers().length == 2) { if (room && room.getJoinRule() == "invite" && room.getMembers().length == 2) {
let other = room.getMember(userId); let other = room.getMember(userId);
if (other) { if (other) {
if (room.getMyMembership() == "invite" && other.membership == "join") { if (room.getMyMembership() == "invite" && other.membership == "join") {
@ -931,7 +963,7 @@ export default {
isDirectRoom(room) { isDirectRoom(room) {
// TODO - Use the is_direct accountData flag (m.direct). WE (as the client) // TODO - Use the is_direct accountData flag (m.direct). WE (as the client)
// apprently need to set this... // apprently need to set this...
if (room.getJoinRule() == "invite" && room.getMembers().length == 2) { if (room && room.getJoinRule() == "invite" && room.getMembers().length == 2) {
return true; return true;
} }
return false; return false;