Room list: extract getTagsForRoom from old room list store (#32716)
* refactor: extract `getTagsForRoom` from old rls `getTagsForRoom` doesn't rely on the rls state. We can extract it safely to an external function. The function is not moved into the rls v3 because the rls is for the room list and not for other ui elements. * refactor: remove dead code * test: add more tests for `getTagsForRoom` * refactor: `getTagsForRoom` in old rls * doc: add missing tsdoc for `room`
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2026 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { JoinRule, type Room } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import { DefaultTagID, type TagID } from "../../stores/room-list-v3/skip-list/tag";
|
||||
import { EffectiveMembership, getEffectiveMembershipTag } from "../membership";
|
||||
import DMRoomMap from "../DMRoomMap";
|
||||
|
||||
/**
|
||||
* Get the tags for a room.
|
||||
* @param room - the room to get the tags for
|
||||
* @returns an array of tags for the room. If the room has no tags, it will return an array with the DefaultTagID.Untagged tag.
|
||||
*/
|
||||
export function getTagsForRoom(room: Room): TagID[] {
|
||||
const tags: TagID[] = [];
|
||||
|
||||
const membership = getEffectiveMembershipTag(room);
|
||||
|
||||
if (membership === EffectiveMembership.Invite) {
|
||||
tags.push(DefaultTagID.Invite);
|
||||
} else if (membership === EffectiveMembership.Leave) {
|
||||
tags.push(DefaultTagID.Archived);
|
||||
} else {
|
||||
tags.push(...getTagsOfJoinedRoom(room));
|
||||
}
|
||||
|
||||
if (!tags.length) tags.push(DefaultTagID.Untagged);
|
||||
|
||||
return tags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the tags for a room that the user has joined. It checks for user defined tags first, then checks if it's a DM, and finally checks if it's a conference room.
|
||||
* @param room - the room to get the tags for
|
||||
* @returns an array of tags for the room. If the room has no user defined tags, is not a DM, and is not a conference room, it will return an empty array.
|
||||
*/
|
||||
export function getTagsOfJoinedRoom(room: Room): TagID[] {
|
||||
let tags = Object.keys(room.tags || {});
|
||||
|
||||
if (tags.length === 0) {
|
||||
// Check to see if it's a DM if it isn't anything else
|
||||
if (DMRoomMap.shared().getUserIdForRoomId(room.roomId)) {
|
||||
tags = [DefaultTagID.DM];
|
||||
}
|
||||
}
|
||||
if (room.isCallRoom() && (room.getJoinRule() === JoinRule.Public || room.getJoinRule() === JoinRule.Knock)) {
|
||||
tags.push(DefaultTagID.Conference);
|
||||
}
|
||||
|
||||
return tags;
|
||||
}
|
||||
@@ -9,10 +9,10 @@ Please see LICENSE files in the repository root for full details.
|
||||
import { type Room } from "matrix-js-sdk/src/matrix";
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
|
||||
import RoomListStore from "../../stores/room-list/RoomListStore";
|
||||
import { DefaultTagID, type TagID } from "../../stores/room-list-v3/skip-list/tag";
|
||||
import RoomListActions from "../../actions/RoomListActions";
|
||||
import dis from "../../dispatcher/dispatcher";
|
||||
import { getTagsForRoom } from "./getTagsForRoom";
|
||||
|
||||
/**
|
||||
* Toggle tag for a given room
|
||||
@@ -22,7 +22,7 @@ import dis from "../../dispatcher/dispatcher";
|
||||
export function tagRoom(room: Room, tagId: TagID): void {
|
||||
if (tagId === DefaultTagID.Favourite || tagId === DefaultTagID.LowPriority) {
|
||||
const inverseTag = tagId === DefaultTagID.Favourite ? DefaultTagID.LowPriority : DefaultTagID.Favourite;
|
||||
const isApplied = RoomListStore.instance.getTagsForRoom(room).includes(tagId);
|
||||
const isApplied = getTagsForRoom(room).includes(tagId);
|
||||
const removeTag = isApplied ? tagId : inverseTag;
|
||||
const addTag = isApplied ? null : tagId;
|
||||
dis.dispatch(RoomListActions.tagRoom(room.client, room, removeTag, addTag, 0));
|
||||
|
||||
Reference in New Issue
Block a user