2020-05-14 03:07:29 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2020-05-14 03:07:29 +00:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2018 New Vector Ltd
|
2020-05-14 03:07:29 +00:00
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2020-05-14 03:07:29 +00:00
|
|
|
*/
|
|
|
|
|
2023-08-04 07:36:16 +00:00
|
|
|
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
2021-10-22 22:23:32 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
2020-05-14 03:07:29 +00:00
|
|
|
import { asyncAction } from "./actionCreators";
|
|
|
|
import Modal from "../Modal";
|
|
|
|
import * as Rooms from "../Rooms";
|
|
|
|
import { _t } from "../languageHandler";
|
2020-05-14 02:46:00 +00:00
|
|
|
import { AsyncActionPayload } from "../dispatcher/payloads";
|
2020-07-17 21:11:34 +00:00
|
|
|
import RoomListStore from "../stores/room-list/RoomListStore";
|
2020-07-17 20:25:09 +00:00
|
|
|
import { SortAlgorithm } from "../stores/room-list/algorithms/models";
|
2023-03-10 14:55:06 +00:00
|
|
|
import { DefaultTagID, TagID } from "../stores/room-list/models";
|
2021-07-02 15:08:27 +00:00
|
|
|
import ErrorDialog from "../components/views/dialogs/ErrorDialog";
|
2020-05-14 03:07:29 +00:00
|
|
|
|
|
|
|
export default class RoomListActions {
|
|
|
|
/**
|
|
|
|
* Creates an action thunk that will do an asynchronous request to
|
|
|
|
* tag room.
|
|
|
|
*
|
|
|
|
* @param {MatrixClient} matrixClient the matrix client to set the
|
|
|
|
* account data on.
|
|
|
|
* @param {Room} room the room to tag.
|
|
|
|
* @param {string} oldTag the tag to remove (unless oldTag ==== newTag)
|
|
|
|
* @param {string} newTag the tag with which to tag the room.
|
|
|
|
* @param {?number} oldIndex the previous position of the room in the
|
|
|
|
* list of rooms.
|
|
|
|
* @param {?number} newIndex the new position of the room in the list
|
|
|
|
* of rooms.
|
|
|
|
* @returns {AsyncActionPayload} an async action payload
|
|
|
|
* @see asyncAction
|
|
|
|
*/
|
|
|
|
public static tagRoom(
|
|
|
|
matrixClient: MatrixClient,
|
|
|
|
room: Room,
|
2023-03-10 14:55:06 +00:00
|
|
|
oldTag: TagID | null,
|
|
|
|
newTag: TagID | null,
|
2023-04-21 10:50:42 +00:00
|
|
|
newIndex: number,
|
2020-05-14 03:07:29 +00:00
|
|
|
): AsyncActionPayload {
|
2023-05-09 17:24:40 +00:00
|
|
|
let metaData: Parameters<MatrixClient["setRoomTag"]>[2] | undefined;
|
2020-05-14 03:07:29 +00:00
|
|
|
|
|
|
|
// Is the tag ordered manually?
|
2020-07-17 20:25:09 +00:00
|
|
|
const store = RoomListStore.instance;
|
|
|
|
if (newTag && store.getTagSorting(newTag) === SortAlgorithm.Manual) {
|
|
|
|
const newList = [...store.orderedLists[newTag]];
|
2020-05-14 03:07:29 +00:00
|
|
|
|
|
|
|
newList.sort((a, b) => a.tags[newTag].order - b.tags[newTag].order);
|
|
|
|
|
2023-04-21 10:50:42 +00:00
|
|
|
const indexBefore = newIndex - 1;
|
|
|
|
const indexAfter = newIndex;
|
2020-05-14 03:07:29 +00:00
|
|
|
|
|
|
|
const prevOrder = indexBefore <= 0 ? 0 : newList[indexBefore].tags[newTag].order;
|
|
|
|
const nextOrder = indexAfter >= newList.length ? 1 : newList[indexAfter].tags[newTag].order;
|
|
|
|
|
|
|
|
metaData = {
|
|
|
|
order: (prevOrder + nextOrder) / 2.0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return asyncAction(
|
|
|
|
"RoomListActions.tagRoom",
|
|
|
|
() => {
|
2023-02-13 11:39:16 +00:00
|
|
|
const promises: Promise<any>[] = [];
|
2020-05-14 03:07:29 +00:00
|
|
|
const roomId = room.roomId;
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2020-05-14 03:07:29 +00:00
|
|
|
// Evil hack to get DMs behaving
|
2020-07-17 20:26:56 +00:00
|
|
|
if (
|
|
|
|
(oldTag === undefined && newTag === DefaultTagID.DM) ||
|
|
|
|
(oldTag === DefaultTagID.DM && newTag === undefined)
|
2020-05-14 03:07:29 +00:00
|
|
|
) {
|
2020-07-17 20:26:56 +00:00
|
|
|
return Rooms.guessAndSetDMRoom(room, newTag === DefaultTagID.DM).catch((err) => {
|
2022-05-25 14:20:46 +00:00
|
|
|
logger.error("Failed to set DM tag " + err);
|
2022-06-14 16:51:51 +00:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
2023-09-19 16:16:38 +00:00
|
|
|
title: _t("room_list|failed_set_dm_tag"),
|
2023-09-22 15:39:40 +00:00
|
|
|
description: err && err.message ? err.message : _t("invite|failed_generic"),
|
2022-12-12 11:24:14 +00:00
|
|
|
});
|
2020-05-14 03:07:29 +00:00
|
|
|
});
|
|
|
|
}
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2020-05-14 03:07:29 +00:00
|
|
|
const hasChangedSubLists = oldTag !== newTag;
|
2022-12-12 11:24:14 +00:00
|
|
|
|
2020-05-14 03:07:29 +00:00
|
|
|
// More evilness: We will still be dealing with moving to favourites/low prio,
|
|
|
|
// but we avoid ever doing a request with TAG_DM.
|
|
|
|
//
|
|
|
|
// if we moved lists, remove the old tag
|
2020-07-17 20:26:56 +00:00
|
|
|
if (oldTag && oldTag !== DefaultTagID.DM && hasChangedSubLists) {
|
2020-06-23 15:41:36 +00:00
|
|
|
const promiseToDelete = matrixClient.deleteRoomTag(roomId, oldTag).catch(function (err) {
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error("Failed to remove tag " + oldTag + " from room: " + err);
|
2022-06-14 16:51:51 +00:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
2023-09-19 16:16:38 +00:00
|
|
|
title: _t("room_list|failed_remove_tag", { tagName: oldTag }),
|
2023-09-22 15:39:40 +00:00
|
|
|
description: err && err.message ? err.message : _t("invite|failed_generic"),
|
2022-12-12 11:24:14 +00:00
|
|
|
});
|
2020-05-14 03:07:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
promises.push(promiseToDelete);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we moved lists or the ordering changed, add the new tag
|
2020-07-17 20:26:56 +00:00
|
|
|
if (newTag && newTag !== DefaultTagID.DM && (hasChangedSubLists || metaData)) {
|
2020-05-14 03:07:29 +00:00
|
|
|
const promiseToAdd = matrixClient.setRoomTag(roomId, newTag, metaData).catch(function (err) {
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error("Failed to add tag " + newTag + " to room: " + err);
|
2022-06-14 16:51:51 +00:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
2023-09-19 16:16:38 +00:00
|
|
|
title: _t("room_list|failed_add_tag", { tagName: newTag }),
|
2023-09-22 15:39:40 +00:00
|
|
|
description: err && err.message ? err.message : _t("invite|failed_generic"),
|
2022-12-12 11:24:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
|
2020-05-14 03:07:29 +00:00
|
|
|
promises.push(promiseToAdd);
|
2022-12-12 11:24:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 03:07:29 +00:00
|
|
|
return Promise.all(promises);
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
// For an optimistic update
|
|
|
|
return {
|
|
|
|
room,
|
|
|
|
oldTag,
|
|
|
|
newTag,
|
|
|
|
metaData,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|