Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add references to room.info endpoint #33011

Merged
merged 39 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
6ae9ef2
feat: add references to room.info endpoint
sampaiodiego Aug 8, 2024
8cb49a3
feat: display header based on new data from rooms endpoint
sampaiodiego Aug 8, 2024
1b7dcd8
fix endpoint behavior
sampaiodiego Aug 8, 2024
f5af8c1
belong to team
sampaiodiego Aug 8, 2024
1bcb25c
return less data
sampaiodiego Aug 8, 2024
813c81a
add hasSubscription
sampaiodiego Aug 8, 2024
96c442f
revert fields
sampaiodiego Aug 8, 2024
4af1a2d
Merge branch 'develop' into improve-rooms-info-endpoint
juliajforesti Aug 19, 2024
33eff92
feat: `rooms.info.teamAndParent` method
juliajforesti Aug 19, 2024
8f73838
test: add `/rooms.info.teamAndParent` test case
juliajforesti Aug 20, 2024
67bf0b5
Merge branch 'develop' into improve-rooms-info-endpoint
KevLehman Aug 27, 2024
6f2211f
update rooms.info endp
KevLehman Aug 27, 2024
d8a293b
move logic to service
KevLehman Aug 27, 2024
af437b6
lint
KevLehman Aug 27, 2024
308e4da
ts
KevLehman Aug 27, 2024
0b871bc
dang
KevLehman Aug 27, 2024
70601af
feat: replicate changes to `apps/meteor/client/views/room/HeaderV2/*`
juliajforesti Aug 29, 2024
de93d48
Discard changes to apps/meteor/client/lib/rooms/roomCoordinator.tsx
KevLehman Sep 2, 2024
501f239
Discard changes to apps/meteor/client/views/room/Header/ParentRoom.tsx
KevLehman Sep 2, 2024
7156a6a
Discard changes to apps/meteor/client/views/room/Header/ParentRoomWit…
KevLehman Sep 2, 2024
afd2bf2
Discard changes to apps/meteor/client/views/room/Header/ParentRoomWit…
KevLehman Sep 2, 2024
4f2abda
Discard changes to apps/meteor/client/views/room/Header/ParentTeam.tsx
KevLehman Sep 2, 2024
bb167f4
Discard changes to apps/meteor/client/views/room/Header/RoomHeader.tsx
KevLehman Sep 2, 2024
3e8c021
Discard changes to apps/meteor/client/views/room/HeaderV2/ParentRoom.tsx
KevLehman Sep 2, 2024
b47f50e
Discard changes to apps/meteor/client/views/room/HeaderV2/ParentRoomW…
KevLehman Sep 2, 2024
1834458
Discard changes to apps/meteor/client/views/room/HeaderV2/ParentRoomW…
KevLehman Sep 2, 2024
5c9cbc7
Discard changes to apps/meteor/client/views/room/HeaderV2/ParentTeam.tsx
KevLehman Sep 2, 2024
7db9b2d
Discard changes to apps/meteor/client/views/room/HeaderV2/RoomHeader.tsx
KevLehman Sep 2, 2024
93cdb34
Discard changes to apps/meteor/definition/IRoomTypeConfig.ts
KevLehman Sep 2, 2024
66da4a4
Create quiet-cherries-punch.md
KevLehman Sep 2, 2024
2c740ca
Merge branch 'develop' into improve-rooms-info-endpoint
KevLehman Sep 2, 2024
2e22028
Merge branch 'develop' into improve-rooms-info-endpoint
MarcosSpessatto Sep 2, 2024
cf375a4
Update apps/meteor/server/services/team/service.ts
KevLehman Sep 2, 2024
9d80321
yet another test
KevLehman Sep 4, 2024
1bf0c93
yet another test
KevLehman Sep 4, 2024
48e1109
Merge branch 'develop' into improve-rooms-info-endpoint
kodiakhq[bot] Sep 5, 2024
5abf3b4
Merge branch 'develop' into improve-rooms-info-endpoint
KevLehman Sep 5, 2024
008565d
Merge branch 'develop' into improve-rooms-info-endpoint
kodiakhq[bot] Sep 5, 2024
4731573
Merge branch 'develop' into improve-rooms-info-endpoint
kodiakhq[bot] Sep 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions apps/meteor/app/api/server/v1/rooms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Media } from '@rocket.chat/core-services';
import { Media, Team } from '@rocket.chat/core-services';
import type { IRoom, IUpload } from '@rocket.chat/core-typings';
import { Messages, Rooms, Users, Uploads, Subscriptions } from '@rocket.chat/models';
import type { Notifications } from '@rocket.chat/rest-typings';
Expand Down Expand Up @@ -416,7 +416,19 @@ API.v1.addRoute(
return API.v1.failure('not-allowed', 'Not Allowed');
}

return API.v1.success({ room: (await Rooms.findOneByIdOrName(room._id, { projection: fields })) ?? undefined });
const discussionParent =
room.prid &&
(await Rooms.findOneById<Pick<IRoom, 'name' | 'fname' | 't' | 'prid' | 'u'>>(room.prid, {
projection: { name: 1, fname: 1, t: 1, prid: 1, u: 1 },
}));
const { team, parentRoom } = await Team.getRoomInfo(room);
const parent = discussionParent || parentRoom;

return API.v1.success({
room: (await Rooms.findOneByIdOrName(room._id, { projection: fields })) ?? undefined,
...(team && { team }),
...(parent && { parent }),
});
},
},
);
Expand Down
3 changes: 3 additions & 0 deletions apps/meteor/client/lib/rooms/roomCoordinator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class RoomCoordinatorClient extends RoomCoordinator {
canSendMessage(rid: string): boolean {
return ChatSubscription.find({ rid }).count() > 0;
},
hasSubscription(rid: string): boolean {
return ChatSubscription.find({ rid }).count() > 0;
},
...directives,
config: roomConfig,
});
Expand Down
7 changes: 6 additions & 1 deletion apps/meteor/client/views/room/Header/ParentRoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ type ParentRoomProps = {
const ParentRoom = ({ room }: ParentRoomProps): ReactElement => {
const icon = useRoomIcon(room);

const handleRedirect = (): void => roomCoordinator.openRouteLink(room.t, { rid: room._id, ...room });
const handleRedirect = (): void => {
if (!roomCoordinator.getRoomDirectives(room.t).hasSubscription(room._id)) {
return;
}
roomCoordinator.openRouteLink(room.t, { rid: room._id, ...room });
};

return (
<HeaderTag
Expand Down
21 changes: 14 additions & 7 deletions apps/meteor/client/views/room/Header/ParentRoomWithData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ type ParentRoomWithDataProps = {
room: IRoom;
};

const ParentRoomWithData = ({ room }: ParentRoomWithDataProps): ReactElement => {
const { prid } = room;
const getParentId = ({ prid, teamId }: IRoom): string => {
if (prid) {
return prid;
}

if (!prid) {
throw new Error('Parent room ID is missing');
if (teamId) {
return teamId;
}

const subscription = useUserSubscription(prid);
throw new Error('Parent room ID is missing');
};

const ParentRoomWithData = ({ room }: ParentRoomWithDataProps): ReactElement => {
const parentId = getParentId(room);

const subscription = useUserSubscription(parentId);
if (subscription) {
return <ParentRoom room={subscription} />;
return <ParentRoom room={{ ...subscription, _id: subscription.rid }} />;
}

return <ParentRoomWithEndpointData rid={prid} />;
return <ParentRoomWithEndpointData rid={room._id} />;
};

export default ParentRoomWithData;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React from 'react';
import { HeaderTagSkeleton } from '../../../components/Header';
import { useRoomInfoEndpoint } from '../../../hooks/useRoomInfoEndpoint';
import ParentRoom from './ParentRoom';
import ParentTeam from './ParentTeam';

type ParentRoomWithEndpointDataProps = {
rid: IRoom['_id'];
Expand All @@ -21,6 +22,17 @@ const ParentRoomWithEndpointData = ({ rid }: ParentRoomWithEndpointDataProps): R
return null;
}

if (data.parent) {
return <ParentRoom room={data.parent} />;
}

if (data.team) {
if (data.team.roomId === rid) {
return null;
}
return <ParentTeam team={data.team} />;
}

return <ParentRoom room={data.room} />;
};

Expand Down
54 changes: 11 additions & 43 deletions apps/meteor/client/views/room/Header/ParentTeam.tsx
Original file line number Diff line number Diff line change
@@ -1,65 +1,33 @@
import type { IRoom } from '@rocket.chat/core-typings';
import type { ITeam } from '@rocket.chat/core-typings';
import { TEAM_TYPE } from '@rocket.chat/core-typings';
import { useUserId, useEndpoint } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';
import { useUserSubscription } from '@rocket.chat/ui-contexts';
import type { ReactElement } from 'react';
import React from 'react';

import { HeaderTag, HeaderTagIcon, HeaderTagSkeleton } from '../../../components/Header';
import { HeaderTag, HeaderTagIcon } from '../../../components/Header';
import { goToRoomById } from '../../../lib/utils/goToRoomById';

type APIErrorResult = { success: boolean; error: string };
const ParentTeam = ({ team }: { team: Pick<ITeam, 'name' | 'roomId' | 'type'> }): ReactElement | null => {
const isTeamPublic = team.type === TEAM_TYPE.PUBLIC;

const ParentTeam = ({ room }: { room: IRoom }): ReactElement | null => {
const { teamId } = room;
const userId = useUserId();

if (!teamId) {
throw new Error('invalid rid');
}

if (!userId) {
throw new Error('invalid uid');
}

const teamsInfoEndpoint = useEndpoint('GET', '/v1/teams.info');
const userTeamsListEndpoint = useEndpoint('GET', '/v1/users.listTeams');

const {
data: teamInfoData,
isLoading: teamInfoLoading,
isError: teamInfoError,
} = useQuery(['teamId', teamId], async () => teamsInfoEndpoint({ teamId }), {
keepPreviousData: true,
retry: (_, error) => (error as APIErrorResult)?.error === 'unauthorized' && false,
});

const { data: userTeams, isLoading: userTeamsLoading } = useQuery(['userId', userId], async () => userTeamsListEndpoint({ userId }));

const userBelongsToTeam = userTeams?.teams?.find((team) => team._id === teamId) || false;
const isTeamPublic = teamInfoData?.teamInfo.type === TEAM_TYPE.PUBLIC;
const subscription = useUserSubscription(team.roomId);

const redirectToMainRoom = (): void => {
const rid = teamInfoData?.teamInfo.roomId;
const rid = team.roomId;
if (!rid) {
return;
}

const isTeamPublic = team.type === TEAM_TYPE.PUBLIC;
const userBelongsToTeam = !!subscription;

if (!(isTeamPublic || userBelongsToTeam)) {
return;
}

goToRoomById(rid);
};

if (teamInfoLoading || userTeamsLoading) {
return <HeaderTagSkeleton />;
}

if (teamInfoError) {
return null;
}

return (
<HeaderTag
role='button'
Expand All @@ -68,7 +36,7 @@ const ParentTeam = ({ room }: { room: IRoom }): ReactElement | null => {
onClick={redirectToMainRoom}
>
<HeaderTagIcon icon={{ name: isTeamPublic ? 'team' : 'team-lock' }} />
{teamInfoData?.teamInfo.name}
{team.name}
</HeaderTag>
);
};
Expand Down
4 changes: 1 addition & 3 deletions apps/meteor/client/views/room/Header/RoomHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Header, HeaderAvatar, HeaderContent, HeaderContentRow, HeaderSubtitle,
import MarkdownText from '../../../components/MarkdownText';
import FederatedRoomOriginServer from './FederatedRoomOriginServer';
import ParentRoomWithData from './ParentRoomWithData';
import ParentTeam from './ParentTeam';
import RoomTitle from './RoomTitle';
import RoomToolbox from './RoomToolbox';
import Encrypted from './icons/Encrypted';
Expand Down Expand Up @@ -47,8 +46,7 @@ const RoomHeader = ({ room, topic = '', slots = {}, roomToolbox }: RoomHeaderPro
<HeaderContentRow>
<RoomTitle room={room} />
<Favorite room={room} />
{room.prid && <ParentRoomWithData room={room} />}
{room.teamId && !room.teamMain && <ParentTeam room={room} />}
{(room.prid || room.teamId) && <ParentRoomWithData room={room} />}
{isRoomFederated(room) && <FederatedRoomOriginServer room={room} />}
<Encrypted room={room} />
<Translate room={room} />
Expand Down
7 changes: 6 additions & 1 deletion apps/meteor/client/views/room/HeaderV2/ParentRoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ type ParentRoomProps = {
const ParentRoom = ({ room }: ParentRoomProps) => {
const icon = useRoomIcon(room);

const handleRedirect = (): void => roomCoordinator.openRouteLink(room.t, { rid: room._id, ...room });
const handleRedirect = (): void => {
if (!roomCoordinator.getRoomDirectives(room.t).hasSubscription(room._id)) {
return;
}
roomCoordinator.openRouteLink(room.t, { rid: room._id, ...room });
};

return (
<HeaderTag
Expand Down
21 changes: 14 additions & 7 deletions apps/meteor/client/views/room/HeaderV2/ParentRoomWithData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@ type ParentRoomWithDataProps = {
room: IRoom;
};

const ParentRoomWithData = ({ room }: ParentRoomWithDataProps) => {
const { prid } = room;
const getParentId = ({ prid, teamId }: IRoom): string => {
if (prid) {
return prid;
}

if (!prid) {
throw new Error('Parent room ID is missing');
if (teamId) {
return teamId;
}

const subscription = useUserSubscription(prid);
throw new Error('Parent room ID is missing');
};

const ParentRoomWithData = ({ room }: ParentRoomWithDataProps) => {
const parentId = getParentId(room);

const subscription = useUserSubscription(parentId);
if (subscription) {
return <ParentRoom room={subscription} />;
return <ParentRoom room={{ ...subscription, _id: subscription.rid }} />;
}

return <ParentRoomWithEndpointData rid={prid} />;
return <ParentRoomWithEndpointData rid={room._id} />;
};

export default ParentRoomWithData;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react';
import { HeaderTagSkeleton } from '../../../components/Header';
import { useRoomInfoEndpoint } from '../../../hooks/useRoomInfoEndpoint';
import ParentRoom from './ParentRoom';
import ParentTeam from './ParentTeam';

type ParentRoomWithEndpointDataProps = {
rid: IRoom['_id'];
Expand All @@ -20,6 +21,17 @@ const ParentRoomWithEndpointData = ({ rid }: ParentRoomWithEndpointDataProps) =>
return null;
}

if (data.parent) {
return <ParentRoom room={data.parent} />;
}

if (data.team) {
if (data.team.roomId === rid) {
return null;
}
return <ParentTeam team={data.team} />;
}

return <ParentRoom room={data.room} />;
};

Expand Down
59 changes: 12 additions & 47 deletions apps/meteor/client/views/room/HeaderV2/ParentTeam.tsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,33 @@
import type { IRoom } from '@rocket.chat/core-typings';
import type { ITeam } from '@rocket.chat/core-typings';
import { TEAM_TYPE } from '@rocket.chat/core-typings';
import { useUserId, useEndpoint } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';
import { useUserSubscription } from '@rocket.chat/ui-contexts';
import type { ReactElement } from 'react';
import React from 'react';

import { HeaderTag, HeaderTagIcon, HeaderTagSkeleton } from '../../../components/Header';
import { HeaderTag, HeaderTagIcon } from '../../../components/Header';
import { goToRoomById } from '../../../lib/utils/goToRoomById';

type APIErrorResult = { success: boolean; error: string };
const ParentTeam = ({ team }: { team: Pick<ITeam, 'name' | 'roomId' | 'type'> }): ReactElement | null => {
const isTeamPublic = team.type === TEAM_TYPE.PUBLIC;

type ParentTeamProps = {
room: IRoom;
};

const ParentTeam = ({ room }: ParentTeamProps) => {
const { teamId } = room;
const userId = useUserId();

if (!teamId) {
throw new Error('invalid rid');
}

if (!userId) {
throw new Error('invalid uid');
}

const teamsInfoEndpoint = useEndpoint('GET', '/v1/teams.info');
const userTeamsListEndpoint = useEndpoint('GET', '/v1/users.listTeams');

const {
data: teamInfoData,
isLoading: teamInfoLoading,
isError: teamInfoError,
} = useQuery(['teamId', teamId], async () => teamsInfoEndpoint({ teamId }), {
keepPreviousData: true,
retry: (_, error) => (error as APIErrorResult)?.error === 'unauthorized' && false,
});

const { data: userTeams, isLoading: userTeamsLoading } = useQuery(['userId', userId], async () => userTeamsListEndpoint({ userId }));

const userBelongsToTeam = userTeams?.teams?.find((team) => team._id === teamId) || false;
const isTeamPublic = teamInfoData?.teamInfo.type === TEAM_TYPE.PUBLIC;
const subscription = useUserSubscription(team.roomId);

const redirectToMainRoom = (): void => {
const rid = teamInfoData?.teamInfo.roomId;
const rid = team.roomId;
if (!rid) {
return;
}

const isTeamPublic = team.type === TEAM_TYPE.PUBLIC;
const userBelongsToTeam = !!subscription;

if (!(isTeamPublic || userBelongsToTeam)) {
return;
}

goToRoomById(rid);
};

if (teamInfoLoading || userTeamsLoading) {
return <HeaderTagSkeleton />;
}

if (teamInfoError) {
return null;
}

return (
<HeaderTag
role='button'
Expand All @@ -71,7 +36,7 @@ const ParentTeam = ({ room }: ParentTeamProps) => {
onClick={redirectToMainRoom}
>
<HeaderTagIcon icon={{ name: isTeamPublic ? 'team' : 'team-lock' }} />
{teamInfoData?.teamInfo.name}
{team.name}
</HeaderTag>
);
};
Expand Down
4 changes: 1 addition & 3 deletions apps/meteor/client/views/room/HeaderV2/RoomHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import React, { Suspense } from 'react';
import { Header, HeaderAvatar, HeaderContent, HeaderContentRow, HeaderToolbar } from '../../../components/Header';
import FederatedRoomOriginServer from './FederatedRoomOriginServer';
import ParentRoomWithData from './ParentRoomWithData';
import ParentTeam from './ParentTeam';
import RoomTitle from './RoomTitle';
import RoomToolbox from './RoomToolbox';
import Encrypted from './icons/Encrypted';
Expand Down Expand Up @@ -45,8 +44,7 @@ const RoomHeader = ({ room, slots = {}, roomToolbox }: RoomHeaderProps) => {
<HeaderContentRow>
<RoomTitle room={room} />
<Favorite room={room} />
{room.prid && <ParentRoomWithData room={room} />}
{room.teamId && !room.teamMain && <ParentTeam room={room} />}
{(room.prid || room.teamId) && <ParentRoomWithData room={room} />}
{isRoomFederated(room) && <FederatedRoomOriginServer room={room} />}
<Encrypted room={room} />
<Translate room={room} />
Expand Down
Loading
Loading