Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into fix/…
Browse files Browse the repository at this point in the history
…message-body

* 'develop' of github.com:RocketChat/Rocket.Chat:
  [IMPROVE] Refactor + unit tests for federation-v2 (#25680)
  [FIX] user status Offline misnamed as Invisible in Custom Status edit dropdown menu (#24796)
  Chore: Messages raw model rewrite to ts (#25761)
  Chore: migrate katex to ts (#25501)
  Chore: AutoTranslate contextualBar rewrite  (#25751)
  Chore: Replace AnnouncementModal in favor of GenericModal (#25752)
  Chore: Keyboard shortcuts contextualBar rewrite (#25753)
  Chore: Prune Messages contextualBar rewrite (#25757)
  Chore: add Ajv JSON Schema to api/v1 (#25601)
  Update package.json (#25755)
  Update CODEOWNERS
  Chore: remove duplicated NotFoundPage.js (#25749)
  • Loading branch information
gabriellsh committed Jun 6, 2022
2 parents 5d3f63c + e682030 commit 2a75a00
Show file tree
Hide file tree
Showing 141 changed files with 7,281 additions and 1,874 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/_templates/ @RocketChat/chat-engine
/apps/meteor/client/ @RocketChat/frontend
/apps/meteor/tests/ @RocketChat/chat-engine
/apps/meteor/app/apps/ @RocketChat/apps
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ yarn-error.log*
!.yarn/sdks
!.yarn/versions

.nvmrc
.nvmrc
.idea/
15 changes: 11 additions & 4 deletions apps/meteor/app/api/server/v1/im.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
* Docs: https://github.com/RocketChat/developer-docs/blob/master/reference/api/rest-api/endpoints/team-collaboration-endpoints/im-endpoints
*/
import type { IMessage, IRoom, ISetting, ISubscription, IUpload, IUser } from '@rocket.chat/core-typings';
import { isDmDeleteProps, isDmFileProps, isDmMemberProps, isDmMessagesProps, isDmCreateProps } from '@rocket.chat/rest-typings';
import {
isDmDeleteProps,
isDmFileProps,
isDmMemberProps,
isDmMessagesProps,
isDmCreateProps,
isDmHistoryProps,
} from '@rocket.chat/rest-typings';
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';

Expand Down Expand Up @@ -240,7 +247,7 @@ API.v1.addRoute(

API.v1.addRoute(
['dm.history', 'im.history'],
{ authRequired: true },
{ authRequired: true, validateParams: isDmHistoryProps },
{
async get() {
const { offset = 0, count = 20 } = this.getPaginationItems();
Expand Down Expand Up @@ -351,7 +358,7 @@ API.v1.addRoute(
sort: sortObj,
skip: offset,
limit: count,
...(fields && { fields }),
...(fields && { projection: fields }),
}).toArray();

return API.v1.success({
Expand Down Expand Up @@ -403,7 +410,7 @@ API.v1.addRoute(
sort: sort || { ts: -1 },
skip: offset,
limit: count,
fields,
projection: fields,
}).toArray();

if (!msgs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
import { RoomType } from '@rocket.chat/apps-engine/definition/rooms';

import { FederatedRoom } from '../domain/FederatedRoom';
import { FederatedUser } from '../domain/FederatedUser';
import { EVENT_ORIGIN, IFederationBridge } from '../domain/IFederationBridge';
import { RocketChatMessageAdapter } from '../infrastructure/rocket-chat/adapters/Message';
import { RocketChatRoomAdapter } from '../infrastructure/rocket-chat/adapters/Room';
import { RocketChatSettingsAdapter } from '../infrastructure/rocket-chat/adapters/Settings';
import { RocketChatUserAdapter } from '../infrastructure/rocket-chat/adapters/User';
import {
FederationRoomCreateInputDto,
FederationRoomChangeMembershipDto,
FederationRoomSendInternalMessageDto,
FederationRoomChangeJoinRulesDto,
FederationRoomChangeNameDto,
FederationRoomChangeTopicDto,
} from './input/RoomReceiverDto';

export class FederationRoomServiceReceiver {
constructor(
private rocketRoomAdapter: RocketChatRoomAdapter,
private rocketUserAdapter: RocketChatUserAdapter,
private rocketMessageAdapter: RocketChatMessageAdapter,
private rocketSettingsAdapter: RocketChatSettingsAdapter,
private bridge: IFederationBridge,
) {} // eslint-disable-line no-empty-function

public async createRoom(roomCreateInput: FederationRoomCreateInputDto): Promise<void> {
const {
externalRoomId,
externalInviterId,
normalizedInviterId,
externalRoomName,
normalizedRoomId,
roomType,
wasInternallyProgramaticallyCreated = false,
} = roomCreateInput;

if ((await this.rocketRoomAdapter.getFederatedRoomByExternalId(externalRoomId)) || wasInternallyProgramaticallyCreated) {
return;
}

if (!(await this.rocketUserAdapter.getFederatedUserByExternalId(externalInviterId))) {
const externalUserProfileInformation = await this.bridge.getUserProfileInformation(externalInviterId);
const name = externalUserProfileInformation?.displayname || normalizedInviterId;
const federatedCreatorUser = FederatedUser.createInstance(externalInviterId, {
name,
username: normalizedInviterId,
existsOnlyOnProxyServer: false,
});

await this.rocketUserAdapter.createFederatedUser(federatedCreatorUser);
}
const creator = await this.rocketUserAdapter.getFederatedUserByExternalId(externalInviterId);
const newFederatedRoom = FederatedRoom.createInstance(
externalRoomId,
normalizedRoomId,
creator as FederatedUser,
roomType || RoomType.CHANNEL,
externalRoomName,
);
await this.rocketRoomAdapter.createFederatedRoom(newFederatedRoom);
}

public async changeRoomMembership(roomChangeMembershipInput: FederationRoomChangeMembershipDto): Promise<void> {
const {
externalRoomId,
normalizedInviteeId,
normalizedRoomId,
normalizedInviterId,
externalRoomName,
externalInviteeId,
externalInviterId,
inviteeUsernameOnly,
inviterUsernameOnly,
eventOrigin,
roomType,
leave,
} = roomChangeMembershipInput;
const affectedFederatedRoom = await this.rocketRoomAdapter.getFederatedRoomByExternalId(externalRoomId);
if (!affectedFederatedRoom && eventOrigin === EVENT_ORIGIN.LOCAL) {
throw new Error(`Could not find room with external room id: ${externalRoomId}`);
}
const isInviterFromTheSameHomeServer = await this.bridge.isUserIdFromTheSameHomeserver(
externalInviterId,
this.rocketSettingsAdapter.getHomeServerDomain(),
);
const isInviteeFromTheSameHomeServer = await this.bridge.isUserIdFromTheSameHomeserver(
externalInviteeId,
this.rocketSettingsAdapter.getHomeServerDomain(),
);

if (!(await this.rocketUserAdapter.getFederatedUserByExternalId(externalInviterId))) {
const externalUserProfileInformation = await this.bridge.getUserProfileInformation(externalInviterId);
const name = externalUserProfileInformation.displayname || normalizedInviterId;
const username = isInviterFromTheSameHomeServer ? inviterUsernameOnly : normalizedInviterId;
const federatedInviterUser = FederatedUser.createInstance(externalInviterId, {
name,
username,
existsOnlyOnProxyServer: isInviterFromTheSameHomeServer,
});

await this.rocketUserAdapter.createFederatedUser(federatedInviterUser);
}

if (!(await this.rocketUserAdapter.getFederatedUserByExternalId(externalInviteeId))) {
const externalUserProfileInformation = await this.bridge.getUserProfileInformation(externalInviteeId);
const name = externalUserProfileInformation.displayname || normalizedInviteeId;
const username = isInviteeFromTheSameHomeServer ? inviteeUsernameOnly : normalizedInviteeId;
const federatedInviteeUser = FederatedUser.createInstance(externalInviteeId, {
name,
username,
existsOnlyOnProxyServer: isInviteeFromTheSameHomeServer,
});

await this.rocketUserAdapter.createFederatedUser(federatedInviteeUser);
}

const federatedInviteeUser = await this.rocketUserAdapter.getFederatedUserByExternalId(externalInviteeId);
const federatedInviterUser = await this.rocketUserAdapter.getFederatedUserByExternalId(externalInviterId);

if (!affectedFederatedRoom && eventOrigin === EVENT_ORIGIN.REMOTE) {
const members = [federatedInviterUser, federatedInviteeUser] as any[];
const newFederatedRoom = FederatedRoom.createInstance(
externalRoomId,
normalizedRoomId,
federatedInviterUser as FederatedUser,
roomType,
externalRoomName,
members,
);

await this.rocketRoomAdapter.createFederatedRoom(newFederatedRoom);
await this.bridge.joinRoom(externalRoomId, externalInviteeId);
}
const federatedRoom = affectedFederatedRoom || (await this.rocketRoomAdapter.getFederatedRoomByExternalId(externalRoomId));

if (leave) {
return this.rocketRoomAdapter.removeUserFromRoom(
federatedRoom as FederatedRoom,
federatedInviteeUser as FederatedUser,
federatedInviterUser as FederatedUser,
);
}
await this.rocketRoomAdapter.addUserToRoom(
federatedRoom as FederatedRoom,
federatedInviteeUser as FederatedUser,
federatedInviterUser as FederatedUser,
);
}

public async receiveExternalMessage(roomSendInternalMessageInput: FederationRoomSendInternalMessageDto): Promise<void> {
const { externalRoomId, externalSenderId, text } = roomSendInternalMessageInput;

const federatedRoom = await this.rocketRoomAdapter.getFederatedRoomByExternalId(externalRoomId);
if (!federatedRoom) {
return;
}

const senderUser = await this.rocketUserAdapter.getFederatedUserByExternalId(externalSenderId);
if (!senderUser) {
return;
}

await this.rocketMessageAdapter.sendMessage(senderUser, text, federatedRoom);
}

public async changeJoinRules(roomJoinRulesChangeInput: FederationRoomChangeJoinRulesDto): Promise<void> {
const { externalRoomId, roomType } = roomJoinRulesChangeInput;

const federatedRoom = await this.rocketRoomAdapter.getFederatedRoomByExternalId(externalRoomId);
if (!federatedRoom) {
return;
}

if (federatedRoom.isDirectMessage()) {
return;
}

federatedRoom.setRoomType(roomType);
await this.rocketRoomAdapter.updateRoomType(federatedRoom);
}

public async changeRoomName(roomChangeNameInput: FederationRoomChangeNameDto): Promise<void> {
const { externalRoomId, normalizedRoomName } = roomChangeNameInput;

const federatedRoom = await this.rocketRoomAdapter.getFederatedRoomByExternalId(externalRoomId);
if (!federatedRoom) {
return;
}

if (federatedRoom.isDirectMessage()) {
return;
}

federatedRoom.changeRoomName(normalizedRoomName);

await this.rocketRoomAdapter.updateRoomName(federatedRoom);
}

public async changeRoomTopic(roomChangeTopicInput: FederationRoomChangeTopicDto): Promise<void> {
const { externalRoomId, roomTopic } = roomChangeTopicInput;

const federatedRoom = await this.rocketRoomAdapter.getFederatedRoomByExternalId(externalRoomId);
if (!federatedRoom) {
return;
}

if (federatedRoom.isDirectMessage()) {
return;
}

federatedRoom.changeRoomTopic(roomTopic);

await this.rocketRoomAdapter.updateRoomTopic(federatedRoom);
}
}
Loading

0 comments on commit 2a75a00

Please sign in to comment.