Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into fix/sys
Browse files Browse the repository at this point in the history
* 'develop' of github.com:RocketChat/Rocket.Chat:
  Regression: Unify isLastDocDelayed logic (#27120)
  [FIX] starting conferences with mic and cam on their default states do not sync the state with the provider (#26966)
  [NEW] Matrix federation events coverage expansion (adding support for 3 extra events) (#26859)
  Regression: Quote colors (#27110)
  Regression: Toolbox Divider Color (#27116)
  • Loading branch information
gabriellsh committed Oct 31, 2022
2 parents 3b3596b + 205a64c commit e63420a
Show file tree
Hide file tree
Showing 77 changed files with 2,007 additions and 256 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { IUser } from '@rocket.chat/core-typings';

import { FederatedUser } from '../domain/FederatedUser';
import type { IFederationBridge } from '../domain/IFederationBridge';
import type { RocketChatFileAdapter } from '../infrastructure/rocket-chat/adapters/File';
import type { RocketChatSettingsAdapter } from '../infrastructure/rocket-chat/adapters/Settings';
import type { RocketChatUserAdapter } from '../infrastructure/rocket-chat/adapters/User';

Expand All @@ -9,6 +12,7 @@ export abstract class FederationService {
constructor(
protected bridge: IFederationBridge,
protected internalUserAdapter: RocketChatUserAdapter,
protected internalFileAdapter: RocketChatFileAdapter,
protected internalSettingsAdapter: RocketChatSettingsAdapter,
) {
this.internalHomeServerDomain = this.internalSettingsAdapter.getHomeServerDomain();
Expand All @@ -34,6 +38,26 @@ export abstract class FederationService {
});
}
await this.internalUserAdapter.createFederatedUser(federatedUser);
const insertedUser = await this.internalUserAdapter.getFederatedUserByExternalId(externalUserId);
if (!insertedUser) {
return;
}
await this.updateUserAvatarInternally(insertedUser);
}

protected async updateUserAvatarInternally(federatedUser: FederatedUser): Promise<void> {
const externalUserProfileInformation = await this.bridge.getUserProfileInformation(federatedUser.getExternalId());
if (!externalUserProfileInformation?.avatarUrl) {
return;
}
if (!federatedUser.isRemote() || !federatedUser.shouldUpdateFederationAvatar(externalUserProfileInformation.avatarUrl)) {
return;
}
await this.internalUserAdapter.setAvatar(
federatedUser,
this.bridge.convertMatrixUrlToHttp(federatedUser.getExternalId(), externalUserProfileInformation.avatarUrl),
);
await this.internalUserAdapter.updateFederationAvatar(federatedUser.getInternalId(), externalUserProfileInformation.avatarUrl);
}

protected async createFederatedUserForInviterUsingLocalInformation(internalInviterId: string): Promise<string> {
Expand All @@ -45,7 +69,34 @@ export abstract class FederationService {
const externalInviterId = await this.bridge.createUser(internalUser.username, name, this.internalHomeServerDomain);
const existsOnlyOnProxyServer = true;
await this.createFederatedUser(externalInviterId, internalUser.username, existsOnlyOnProxyServer, name);
await this.updateUserAvatarExternally(
internalUser,
(await this.internalUserAdapter.getFederatedUserByExternalId(externalInviterId)) as FederatedUser,
);

return externalInviterId;
}

protected async updateUserAvatarExternally(internalUser: IUser, externalInviter: FederatedUser): Promise<void> {
if (!internalUser.username) {
return;
}
const buffer = await this.internalFileAdapter.getBufferForAvatarFile(internalUser.username);
if (!buffer) {
return;
}
const avatarFileRecord = await this.internalFileAdapter.getFileMetadataForAvatarFile(internalUser.username);
if (!avatarFileRecord?.type || !avatarFileRecord?.name) {
return;
}
const externalFileUri = await this.bridge.uploadContent(externalInviter.getExternalId(), buffer, {
type: avatarFileRecord.type,
name: avatarFileRecord.name,
});
if (!externalFileUri) {
return;
}
await this.internalUserAdapter.updateFederationAvatar(internalUser._id, externalFileUri);
await this.bridge.setUserAvatar(externalInviter.getExternalId(), externalFileUri);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isMessageFromMatrixFederation } from '@rocket.chat/core-typings';

import type { IFederationBridge } from '../domain/IFederationBridge';
import type { RocketChatFileAdapter } from '../infrastructure/rocket-chat/adapters/File';
import type { RocketChatMessageAdapter } from '../infrastructure/rocket-chat/adapters/Message';
import type { RocketChatRoomAdapter } from '../infrastructure/rocket-chat/adapters/Room';
import type { RocketChatSettingsAdapter } from '../infrastructure/rocket-chat/adapters/Settings';
Expand All @@ -13,10 +14,11 @@ export class FederationMessageServiceListener extends FederationService {
protected internalRoomAdapter: RocketChatRoomAdapter,
protected internalUserAdapter: RocketChatUserAdapter,
protected internalMessageAdapter: RocketChatMessageAdapter,
protected internalFileAdapter: RocketChatFileAdapter,
protected internalSettingsAdapter: RocketChatSettingsAdapter,
protected bridge: IFederationBridge,
) {
super(bridge, internalUserAdapter, internalSettingsAdapter);
super(bridge, internalUserAdapter, internalFileAdapter, internalSettingsAdapter);
}

public async onMessageReaction(messageReactionEventInput: FederationMessageReactionEventDto): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export class FederationRoomServiceListener extends FederationService {
protected internalRoomAdapter: RocketChatRoomAdapter,
protected internalUserAdapter: RocketChatUserAdapter,
protected internalMessageAdapter: RocketChatMessageAdapter,
protected internalSettingsAdapter: RocketChatSettingsAdapter,
protected internalFileAdapter: RocketChatFileAdapter,
protected internalSettingsAdapter: RocketChatSettingsAdapter,
protected bridge: IFederationBridge,
) {
super(bridge, internalUserAdapter, internalSettingsAdapter);
super(bridge, internalUserAdapter, internalFileAdapter, internalSettingsAdapter);
}

public async onCreateRoom(roomCreateInput: FederationRoomCreateInputDto): Promise<void> {
Expand Down Expand Up @@ -92,12 +92,19 @@ export class FederationRoomServiceListener extends FederationService {
eventOrigin,
roomType,
leave,
userAvatarUrl,
} = roomChangeMembershipInput;
const wasGeneratedOnTheProxyServer = eventOrigin === EVENT_ORIGIN.LOCAL;
const affectedFederatedRoom = await this.internalRoomAdapter.getFederatedRoomByExternalId(externalRoomId);

if (userAvatarUrl) {
const federatedUser = await this.internalUserAdapter.getFederatedUserByExternalId(externalInviteeId);
federatedUser && (await this.updateUserAvatarInternally(federatedUser));
return;
}

if (wasGeneratedOnTheProxyServer && !affectedFederatedRoom) {
throw new Error(`Could not find room with external room id: ${externalRoomId}`);
return;
}

const isInviterFromTheSameHomeServer = FederatedUser.isOriginalFromTheProxyServer(
Expand Down Expand Up @@ -152,7 +159,7 @@ export class FederationRoomServiceListener extends FederationService {

const federatedRoom = affectedFederatedRoom || (await this.internalRoomAdapter.getFederatedRoomByExternalId(externalRoomId));
if (!federatedRoom) {
throw new Error(`Could not find room with external room id: ${externalRoomId}`);
return;
}

if (leave) {
Expand Down Expand Up @@ -186,7 +193,7 @@ export class FederationRoomServiceListener extends FederationService {
}

public async onExternalMessageReceived(roomReceiveExternalMessageInput: FederationRoomReceiveExternalMessageDto): Promise<void> {
const { externalRoomId, externalSenderId, messageText, externalEventId } = roomReceiveExternalMessageInput;
const { externalRoomId, externalSenderId, messageText, externalEventId, replyToEventId } = roomReceiveExternalMessageInput;

const federatedRoom = await this.internalRoomAdapter.getFederatedRoomByExternalId(externalRoomId);
if (!federatedRoom) {
Expand All @@ -197,6 +204,26 @@ export class FederationRoomServiceListener extends FederationService {
if (!senderUser) {
return;
}
const message = await this.internalMessageAdapter.getMessageByFederationId(externalEventId);
if (message) {
return;
}

if (replyToEventId) {
const messageToReplyTo = await this.internalMessageAdapter.getMessageByFederationId(replyToEventId);
if (!messageToReplyTo) {
return;
}
await this.internalMessageAdapter.sendQuoteMessage(
senderUser,
federatedRoom,
messageText,
externalEventId,
messageToReplyTo,
this.internalHomeServerDomain,
);
return;
}

await this.internalMessageAdapter.sendMessage(senderUser, federatedRoom, messageText, externalEventId);
}
Expand Down Expand Up @@ -227,7 +254,7 @@ export class FederationRoomServiceListener extends FederationService {
}

public async onExternalFileMessageReceived(roomReceiveExternalMessageInput: FederationRoomReceiveExternalFileMessageDto): Promise<void> {
const { externalRoomId, externalSenderId, messageBody, externalEventId } = roomReceiveExternalMessageInput;
const { externalRoomId, externalSenderId, messageBody, externalEventId, replyToEventId } = roomReceiveExternalMessageInput;

const federatedRoom = await this.internalRoomAdapter.getFederatedRoomByExternalId(externalRoomId);
if (!federatedRoom) {
Expand All @@ -238,6 +265,10 @@ export class FederationRoomServiceListener extends FederationService {
if (!senderUser) {
return;
}
const message = await this.internalMessageAdapter.getMessageByFederationId(externalEventId);
if (message) {
return;
}
const fileDetails = {
name: messageBody.filename,
size: messageBody.size,
Expand All @@ -253,6 +284,23 @@ export class FederationRoomServiceListener extends FederationService {
fileDetails,
);

if (replyToEventId) {
const messageToReplyTo = await this.internalMessageAdapter.getMessageByFederationId(replyToEventId);
if (!messageToReplyTo) {
return;
}
await this.internalMessageAdapter.sendQuoteFileMessage(
senderUser,
federatedRoom,
files,
attachments,
externalEventId,
messageToReplyTo,
this.internalHomeServerDomain,
);
return;
}

await this.internalMessageAdapter.sendFileMessage(senderUser, federatedRoom, files, attachments, externalEventId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export interface IFederationChangeMembershipInputDto extends IFederationReceiver
leave?: boolean;
roomType?: RoomType;
externalRoomName?: string;
userAvatarUrl?: string;
}

export interface IFederationSendInternalMessageInputDto extends IFederationReceiverBaseRoomInputDto {
externalSenderId: string;
normalizedSenderId: string;
messageText: string;
replyToEventId?: string;
}

export interface IFederationRoomChangeJoinRulesDtoInputDto extends IFederationReceiverBaseRoomInputDto {
Expand Down Expand Up @@ -125,6 +133,7 @@ export class FederationRoomChangeMembershipDto extends FederationBaseRoomInputDt
leave,
roomType,
externalRoomName,
userAvatarUrl,
externalEventId,
}: IFederationChangeMembershipInputDto) {
super({ externalRoomId, normalizedRoomId, externalEventId });
Expand All @@ -138,6 +147,7 @@ export class FederationRoomChangeMembershipDto extends FederationBaseRoomInputDt
this.leave = leave;
this.roomType = roomType;
this.externalRoomName = externalRoomName;
this.userAvatarUrl = userAvatarUrl;
}

externalInviterId: string;
Expand All @@ -159,6 +169,8 @@ export class FederationRoomChangeMembershipDto extends FederationBaseRoomInputDt
leave?: boolean;

externalRoomName?: string;

userAvatarUrl?: string;
}

class ExternalMessageBaseDto extends FederationBaseRoomInputDto {
Expand All @@ -181,18 +193,23 @@ export class FederationRoomReceiveExternalMessageDto extends ExternalMessageBase
normalizedSenderId,
messageText,
externalEventId,
}: IFederationSendInternalMessageBaseInputDto & { messageText: string }) {
super({ externalRoomId, normalizedRoomId, externalEventId });
replyToEventId,
}: IFederationSendInternalMessageInputDto) {
super({ externalRoomId, normalizedRoomId });
this.externalSenderId = externalSenderId;
this.normalizedSenderId = normalizedSenderId;
this.messageText = messageText;
this.replyToEventId = replyToEventId;
this.externalEventId = externalEventId;
}

externalSenderId: string;

normalizedSenderId: string;

messageText: string;

replyToEventId?: string;
}

export class FederationRoomEditExternalMessageDto extends ExternalMessageBaseDto {
Expand Down Expand Up @@ -227,6 +244,7 @@ export interface IFederationFileMessageInputDto {
size: number;
messageText: string;
url: string;
replyToEventId?: string;
}

class FederationFileMessageInputDto {
Expand Down Expand Up @@ -261,10 +279,12 @@ export class FederationRoomReceiveExternalFileMessageDto extends ExternalMessage
messageText,
url,
externalEventId,
replyToEventId,
}: IFederationSendInternalMessageBaseInputDto & IFederationFileMessageInputDto) {
super({ externalRoomId, normalizedRoomId, externalEventId });
this.externalSenderId = externalSenderId;
this.normalizedSenderId = normalizedSenderId;
this.replyToEventId = replyToEventId;
this.messageBody = new FederationFileMessageInputDto({ filename, mimetype, size, messageText, url });
}

Expand All @@ -273,6 +293,8 @@ export class FederationRoomReceiveExternalFileMessageDto extends ExternalMessage
normalizedSenderId: string;

messageBody: FederationFileMessageInputDto;

replyToEventId?: string;
}

export class FederationRoomChangeJoinRulesDto extends FederationBaseRoomInputDto {
Expand Down
Loading

0 comments on commit e63420a

Please sign in to comment.