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

refactor: federation Promise.await 1/N #28591

Merged
merged 22 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { settings } from '../../../settings/server';
import { roomCoordinator } from '../../../../server/lib/rooms/roomCoordinator';
import { RoomSettingsEnum } from '../../../../definition/IRoomTypeConfig';

export const saveRoomType = function (rid, roomType, user, sendMessage = true) {
export const saveRoomType = async function (rid, roomType, user, sendMessage = true) {
if (!Match.test(rid, String)) {
throw new Meteor.Error('invalid-room', 'Invalid room', {
function: 'RocketChat.saveRoomType',
Expand All @@ -27,7 +27,7 @@ export const saveRoomType = function (rid, roomType, user, sendMessage = true) {
});
}

if (!roomCoordinator.getRoomDirectives(room.t).allowRoomSettingChange(room, RoomSettingsEnum.TYPE)) {
if (!(await roomCoordinator.getRoomDirectives(room.t)?.allowRoomSettingChange(room, RoomSettingsEnum.TYPE))) {
throw new Meteor.Error('error-direct-room', "Can't change type of direct rooms", {
function: 'RocketChat.saveRoomType',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const validators: RoomSettingsValidators = {
},
async encrypted({ userId, value, room, rid }) {
if (value !== room.encrypted) {
if (!roomCoordinator.getRoomDirectives(room.t).allowRoomSettingChange(room, RoomSettingsEnum.E2E)) {
if (!(await roomCoordinator.getRoomDirectives(room.t)?.allowRoomSettingChange(room, RoomSettingsEnum.E2E))) {
throw new Meteor.Error('error-action-not-allowed', 'Only groups or direct channels can enable encryption', {
method: 'saveRoomSettings',
action: 'Change_Room_Encrypted',
Expand Down Expand Up @@ -253,12 +253,12 @@ const settingSavers: RoomSettingsSavers = {
saveRoomDescription(rid, value, user);
}
},
roomType({ value, room, rid, user }) {
async roomType({ value, room, rid, user }) {
if (value === room.t) {
return;
}

if (!saveRoomType(rid, value, user)) {
if (!(await saveRoomType(rid, value, user))) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbstractFederationApplicationService } from '../../../../../server/services/federation/application/AbstractFederationService';
import { AbstractFederationApplicationService } from '../../../../../server/services/federation/application/AbstractFederationApplicationService';
import type { RocketChatFileAdapter } from '../../../../../server/services/federation/infrastructure/rocket-chat/adapters/File';
import type { RocketChatSettingsAdapter } from '../../../../../server/services/federation/infrastructure/rocket-chat/adapters/Settings';
import { FederatedUserEE } from '../domain/FederatedUser';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IFederationBridgeEE } from '../domain/IFederationBridge';
import type { RocketChatUserAdapterEE } from '../infrastructure/rocket-chat/adapters/User';
import { AbstractFederationApplicationServiceEE } from './AbstractFederationService';
import { AbstractFederationApplicationServiceEE } from './AbstractFederationApplicationServiceEE';
import type { RocketChatSettingsAdapter } from '../../../../../server/services/federation/infrastructure/rocket-chat/adapters/Settings';
import type { RocketChatFileAdapter } from '../../../../../server/services/federation/infrastructure/rocket-chat/adapters/File';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
FederationOnDirectMessageRoomCreationDto,
FederationRoomInviteUserDto,
} from './input/RoomSenderDto';
import { AbstractFederationApplicationServiceEE } from '../../AbstractFederationService';
import { AbstractFederationApplicationServiceEE } from '../../AbstractFederationApplicationServiceEE';
import type { RocketChatFileAdapter } from '../../../../../../../server/services/federation/infrastructure/rocket-chat/adapters/File';
import type { RocketChatSettingsAdapter } from '../../../../../../../server/services/federation/infrastructure/rocket-chat/adapters/Settings';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
FederationSetupRoomDto,
IFederationInviteeDto,
} from './input/RoomSenderDto';
import { AbstractFederationApplicationServiceEE } from '../../AbstractFederationService';
import { AbstractFederationApplicationServiceEE } from '../../AbstractFederationApplicationServiceEE';
import type { RocketChatFileAdapter } from '../../../../../../../server/services/federation/infrastructure/rocket-chat/adapters/File';
import type { RocketChatSettingsAdapter } from '../../../../../../../server/services/federation/infrastructure/rocket-chat/adapters/Settings';
import type { RocketChatMessageAdapter } from '../../../../../../../server/services/federation/infrastructure/rocket-chat/adapters/Message';
Expand Down
6 changes: 6 additions & 0 deletions apps/meteor/ee/server/local-services/federation/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,10 @@ export class FederationServiceEE extends AbstractBaseFederationServiceEE impleme
FederationRoomSenderConverterEE.toJoinExternalPublicRoomDto(internalUserId, externalRoomId),
);
}

static async createFederationService(): Promise<FederationServiceEE> {
const federationService = new FederationServiceEE();
await federationService.initialize();
return federationService;
}
}
13 changes: 8 additions & 5 deletions apps/meteor/ee/server/startup/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ if (!isRunningMs()) {
}

let federationService: FederationService;
if (!isEnterprise()) {
federationService = new FederationService();
api.registerService(federationService);
}

onLicense('federation', () => {
void (async () => {
if (!isEnterprise()) {
federationService = await FederationService.createFederationService();
api.registerService(federationService);
}
})();

onLicense('federation', async () => {
const federationServiceEE = new FederationServiceEE();
if (federationService) {
api.destroyService(federationService);
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/lib/rooms/roomCoordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { settings } from '../../../app/settings/server';
class RoomCoordinatorServer extends RoomCoordinator {
add(roomConfig: IRoomTypeConfig, directives: Partial<IRoomTypeServerDirectives>): void {
this.addRoomType(roomConfig, {
allowRoomSettingChange(_room: IRoom, _setting: ValueOf<typeof RoomSettingsEnum>): boolean {
allowRoomSettingChange(_room: IRoom, _setting: ValueOf<typeof RoomSettingsEnum>) {
return true;
},
allowMemberAction(_room: IRoom, _action: ValueOf<typeof RoomMemberActions>, _userId?: IUser['_id']): boolean {
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/lib/rooms/roomTypes/direct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ roomCoordinator.add(DirectMessageRoomType, {

allowMemberAction(room: IRoom, action, userId) {
if (isRoomFederated(room)) {
return Federation.actionAllowed(room, action, userId);
return Promise.await(Federation.actionAllowed(room, action, userId));
}
switch (action) {
case RoomMemberActions.BLOCK:
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/lib/rooms/roomTypes/private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ roomCoordinator.add(PrivateRoomType, {

allowMemberAction(_room, action, userId) {
if (isRoomFederated(_room as IRoom)) {
return Federation.actionAllowed(_room, action, userId);
return Promise.await(Federation.actionAllowed(_room, action, userId));
}
switch (action) {
case RoomMemberActions.BLOCK:
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/server/lib/rooms/roomTypes/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ roomCoordinator.add(PublicRoomType, {
}
},

allowMemberAction(_room, action, userId) {
allowMemberAction(_room, action, userId): boolean {
if (isRoomFederated(_room as IRoom)) {
return Federation.actionAllowed(_room, action, userId);
return Promise.await(Federation.actionAllowed(_room, action, userId));
}
switch (action) {
case RoomMemberActions.BLOCK:
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/server/services/federation/Federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const allowedActionsForModerators = allowedActionsInFederatedRooms.filter((actio
const allowedRoomSettingsChangesInFederatedRooms: ValueOf<typeof RoomSettingsEnum>[] = [RoomSettingsEnum.NAME, RoomSettingsEnum.TOPIC];

export class Federation {
public static actionAllowed(room: IRoom, action: ValueOf<typeof RoomMemberActions>, userId?: IUser['_id']): boolean {
public static async actionAllowed(room: IRoom, action: ValueOf<typeof RoomMemberActions>, userId?: IUser['_id']): Promise<boolean> {
if (!isRoomFederated(room)) {
return false;
}
Expand All @@ -33,7 +33,7 @@ export class Federation {
return true;
}

const userSubscription = Promise.await(Subscriptions.findOneByRoomIdAndUserId(room._id, userId));
const userSubscription = await Subscriptions.findOneByRoomIdAndUserId(room._id, userId);
if (!userSubscription) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { RocketChatMessageAdapter } from '../../../../infrastructure/rocket
import type { RocketChatRoomAdapter } from '../../../../infrastructure/rocket-chat/adapters/Room';
import type { RocketChatSettingsAdapter } from '../../../../infrastructure/rocket-chat/adapters/Settings';
import type { RocketChatUserAdapter } from '../../../../infrastructure/rocket-chat/adapters/User';
import { AbstractFederationApplicationService } from '../../../AbstractFederationService';
import { AbstractFederationApplicationService } from '../../../AbstractFederationApplicationService';
import type { FederationMessageReactionEventDto } from '../../input/MessageReceiverDto';

export class FederationMessageServiceReceiver extends AbstractFederationApplicationService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
FederationRoomEditExternalMessageDto,
FederationRoomRoomChangePowerLevelsEventDto,
} from '../input/RoomReceiverDto';
import { AbstractFederationApplicationService } from '../../AbstractFederationService';
import { AbstractFederationApplicationService } from '../../AbstractFederationApplicationService';
import type { RocketChatFileAdapter } from '../../../infrastructure/rocket-chat/adapters/File';
import type { RocketChatNotificationAdapter } from '../../../infrastructure/rocket-chat/adapters/Notification';
import type { InMemoryQueue } from '../../../infrastructure/queue/InMemoryQueue';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { RocketChatFileAdapter } from '../../../infrastructure/rocket-chat/
import type { RocketChatRoomAdapter } from '../../../infrastructure/rocket-chat/adapters/Room';
import type { RocketChatSettingsAdapter } from '../../../infrastructure/rocket-chat/adapters/Settings';
import type { RocketChatUserAdapter } from '../../../infrastructure/rocket-chat/adapters/User';
import { AbstractFederationApplicationService } from '../../AbstractFederationService';
import { AbstractFederationApplicationService } from '../../AbstractFederationApplicationService';

export class FederationRoomInternalValidator extends AbstractFederationApplicationService {
constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { RocketChatNotificationAdapter } from '../../../infrastructure/rock
import type { RocketChatRoomAdapter } from '../../../infrastructure/rocket-chat/adapters/Room';
import type { RocketChatSettingsAdapter } from '../../../infrastructure/rocket-chat/adapters/Settings';
import type { RocketChatUserAdapter } from '../../../infrastructure/rocket-chat/adapters/User';
import { AbstractFederationApplicationService } from '../../AbstractFederationService';
import { AbstractFederationApplicationService } from '../../AbstractFederationApplicationService';
import { getExternalMessageSender } from '../message/sender/message-sender-helper';
import { MATRIX_POWER_LEVELS } from '../../../infrastructure/matrix/definitions/MatrixPowerLevels';
import { ROCKET_CHAT_FEDERATION_ROLES } from '../../../infrastructure/rocket-chat/definitions/FederatedRoomInternalRoles';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { RocketChatNotificationAdapter } from '../../../infrastructure/rock
import type { RocketChatRoomAdapter } from '../../../infrastructure/rocket-chat/adapters/Room';
import type { RocketChatSettingsAdapter } from '../../../infrastructure/rocket-chat/adapters/Settings';
import type { RocketChatUserAdapter } from '../../../infrastructure/rocket-chat/adapters/User';
import { AbstractFederationApplicationService } from '../../AbstractFederationService';
import { AbstractFederationApplicationService } from '../../AbstractFederationApplicationService';
import type { FederationUserTypingStatusEventDto } from '../../room/input/UserReceiverDto';

export class FederationUserServiceReceiver extends AbstractFederationApplicationService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { RocketChatFileAdapter } from '../../../infrastructure/rocket-chat/
import type { RocketChatRoomAdapter } from '../../../infrastructure/rocket-chat/adapters/Room';
import type { RocketChatSettingsAdapter } from '../../../infrastructure/rocket-chat/adapters/Settings';
import type { RocketChatUserAdapter } from '../../../infrastructure/rocket-chat/adapters/User';
import { AbstractFederationApplicationService } from '../../AbstractFederationService';
import { AbstractFederationApplicationService } from '../../AbstractFederationApplicationService';

export class FederationUserServiceSender extends AbstractFederationApplicationService {
constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const EVERYTHING_REGEX = '.*';
const LISTEN_RULES = EVERYTHING_REGEX;

export class RocketChatSettingsAdapter {
public initialize(): void {
public async initialize() {
this.addFederationSettings();
this.watchChangesAndUpdateRegistrationFile();
this.updateSettingsWithProvidedConfigFileIfNecessary();
await this.updateSettingsWithProvidedConfigFileIfNecessary();
}

public getApplicationServiceId(): string {
Expand Down Expand Up @@ -92,16 +92,14 @@ export class RocketChatSettingsAdapter {
'Federation_Matrix_bridge_localpart',
],
([enabled]) =>
Promise.await(
callback(
enabled === true,
this.getApplicationServiceId(),
this.getHomeServerUrl(),
this.getHomeServerDomain(),
this.getBridgeUrl(),
this.getBridgePort(),
this.generateRegistrationFileObject(),
),
callback(
enabled === true,
this.getApplicationServiceId(),
this.getHomeServerUrl(),
this.getHomeServerDomain(),
this.getBridgeUrl(),
this.getBridgePort(),
this.generateRegistrationFileObject(),
),
);
}
Expand Down Expand Up @@ -272,20 +270,22 @@ export class RocketChatSettingsAdapter {
: resolve(process.cwd(), '../../../matrix-federation-config/registration.yaml');
}

private updateSettingsWithProvidedConfigFileIfNecessary(): void {
private async updateSettingsWithProvidedConfigFileIfNecessary() {
const existingConfiguration = this.getRegistrationFileFromHomeserver();
if (!existingConfiguration) {
return;
}

Promise.await(Settings.updateValueById('Federation_Matrix_enabled', true));
Promise.await(Settings.updateValueById('Federation_Matrix_id', existingConfiguration.id));
Promise.await(Settings.updateValueById('Federation_Matrix_hs_token', existingConfiguration.homeserverToken));
Promise.await(Settings.updateValueById('Federation_Matrix_as_token', existingConfiguration.applicationServiceToken));
Promise.await(Settings.updateValueById('Federation_Matrix_homeserver_url', existingConfiguration.rocketchat?.homeServerUrl));
Promise.await(Settings.updateValueById('Federation_Matrix_homeserver_domain', existingConfiguration.rocketchat?.domainName));
Promise.await(Settings.updateValueById('Federation_Matrix_bridge_url', existingConfiguration.bridgeUrl));
Promise.await(Settings.updateValueById('Federation_Matrix_bridge_localpart', existingConfiguration.botName));
Promise.await(Settings.update({ _id: 'Federation_Matrix_registration_file' }, { $set: { hidden: Boolean(existingConfiguration) } }));
await Promise.all([
Settings.updateValueById('Federation_Matrix_enabled', true),
Settings.updateValueById('Federation_Matrix_id', existingConfiguration.id),
Settings.updateValueById('Federation_Matrix_hs_token', existingConfiguration.homeserverToken),
Settings.updateValueById('Federation_Matrix_as_token', existingConfiguration.applicationServiceToken),
Settings.updateValueById('Federation_Matrix_homeserver_url', existingConfiguration.rocketchat?.homeServerUrl),
Settings.updateValueById('Federation_Matrix_homeserver_domain', existingConfiguration.rocketchat?.domainName),
Settings.updateValueById('Federation_Matrix_bridge_url', existingConfiguration.bridgeUrl),
Settings.updateValueById('Federation_Matrix_bridge_localpart', existingConfiguration.botName),
Settings.update({ _id: 'Federation_Matrix_registration_file' }, { $set: { hidden: Boolean(existingConfiguration) } }),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class FederationHooks {
);
}

public static afterRoomRoleChanged(federationRoomService: FederationRoomServiceSender, data?: Record<string, any>): void {
public static async afterRoomRoleChanged(federationRoomService: FederationRoomServiceSender, data?: Record<string, any>) {
if (!data || !settings.get('Federation_Matrix_enabled')) {
return;
}
Expand Down Expand Up @@ -218,7 +218,7 @@ export class FederationHooks {
if (!handlers[`${role}-${action}`]) {
return;
}
Promise.await(handlers[`${role}-${action}`](internalUserId, internalTargetUserId, internalRoomId));
await handlers[`${role}-${action}`](internalUserId, internalTargetUserId, internalRoomId);
}

public static afterRoomNameChanged(callback: (roomId: string, changedRoomName: string) => Promise<void>): void {
Expand Down
13 changes: 9 additions & 4 deletions apps/meteor/server/services/federation/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export abstract class AbstractFederationService extends ServiceClassInternal {
this.internalQueueInstance = internalQueueInstance;
this.internalSettingsAdapter = internalSettingsAdapter;
this.bridge = federationBridge;
this.initialize();
this.internalFileAdapter = FederationFactory.buildInternalFileAdapter();
this.internalRoomAdapter = FederationFactory.buildInternalRoomAdapter();
this.internalUserAdapter = FederationFactory.buildInternalUserAdapter();
Expand Down Expand Up @@ -129,9 +128,9 @@ export abstract class AbstractFederationService extends ServiceClassInternal {
return this.onDisableFederation();
}

private initialize(): void {
public async initialize() {
this.internalSettingsAdapter = FederationFactory.buildInternalSettingsAdapter();
this.internalSettingsAdapter.initialize();
await this.internalSettingsAdapter.initialize();
this.cancelSettingsObserver = this.internalSettingsAdapter.onFederationEnabledStatusChanged(
this.onFederationEnabledSettingChange.bind(this),
);
Expand All @@ -152,7 +151,7 @@ export abstract class AbstractFederationService extends ServiceClassInternal {
this.internalQueueInstance,
this.bridge,
);
const federationMessageServiceReceiver = FederationFactory.buildMessageServiceReceiver(
const federationMessageServiceReceiver = await FederationFactory.buildMessageServiceReceiver(
this.internalRoomAdapter,
this.internalUserAdapter,
this.internalMessageAdapter,
Expand Down Expand Up @@ -317,4 +316,10 @@ export class FederationService extends AbstractBaseFederationService implements
FederationRoomSenderConverter.toCreateDirectMessageRoomDto(internalInviterId, internalRoomId, externalInviteeId),
);
}

static async createFederationService(): Promise<FederationService> {
const federationService = new FederationService();
await federationService.initialize();
return federationService;
}
}
2 changes: 1 addition & 1 deletion apps/meteor/server/services/team/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class TeamService extends ServiceClassInternal implements ITeamService {
}

if (updateRoom && typeof type !== 'undefined') {
saveRoomType(team.roomId, type === TEAM_TYPE.PRIVATE ? 'p' : 'c', user);
await saveRoomType(team.roomId, type === TEAM_TYPE.PRIVATE ? 'p' : 'c', user);
}

await Team.updateNameAndType(teamId, updateData);
Expand Down
Loading