Skip to content

Commit

Permalink
chore: Using a more generic bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustrb committed Aug 13, 2024
1 parent 4f1aa31 commit b3c29ab
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/definition/accessors/IEmailCreator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { IEmailDescriptor } from '../email';

export interface IEmailCreator {
/**
* Sends an OTP through the configured SMTP server within Rocket.Chat
* Sends an email through Rocket.Chat
*
* @param email the email that will recieve the TOTP
* @param code is the code that is going to be sent via email
* @param language is the language that is going to be used within Rocket.Chat
* @param email the email data
*/
sendOTPThroughSMTP(email: string, code: string, language: string): Promise<any>;
send(email: IEmailDescriptor): Promise<void>;
}
5 changes: 3 additions & 2 deletions src/server/accessors/EmailCreator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { AppBridges } from '../bridges';
import type { IEmailCreator } from '../../definition/accessors/IEmailCreator';
import type { IEmailDescriptor } from '../../definition/email';

export class EmailCreator implements IEmailCreator {
constructor(private readonly bridges: AppBridges, private readonly appId: string) {}

public async sendOTPThroughSMTP(email: string, code: string, language: string): Promise<void> {
return this.bridges.getEmailBridge().doSendOtpCodeThroughSMTP(email, code, language, this.appId);
public async send(email: IEmailDescriptor): Promise<void> {
return this.bridges.getEmailBridge().doSendEmail(email, this.appId);
}
}
11 changes: 6 additions & 5 deletions src/server/bridges/EmailBridge.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import type { IEmailDescriptor } from '../../definition/email';
import { PermissionDeniedError } from '../errors/PermissionDeniedError';
import { AppPermissionManager } from '../managers/AppPermissionManager';
import { AppPermissions } from '../permissions/AppPermissions';
import { BaseBridge } from './BaseBridge';

export abstract class EmailBridge extends BaseBridge {
public async doSendOtpCodeThroughSMTP(email: string, code: string, language: string, appId: string): Promise<void> {
public async doSendEmail(email: IEmailDescriptor, appId: string): Promise<void> {
if (this.hasWritePermission(appId)) {
return this.sendOtpCodeThroughSMTP(email, code, language, appId);
return this.sendEmail(email, appId);
}
}

protected abstract sendOtpCodeThroughSMTP(email: string, code: string, language: string, appId: string): Promise<void>;
protected abstract sendEmail(email: IEmailDescriptor, appId: string): Promise<void>;

private hasWritePermission(appId: string): boolean {
if (AppPermissionManager.hasPermission(appId, AppPermissions.email.sendOTP)) {
if (AppPermissionManager.hasPermission(appId, AppPermissions.email.send)) {
return true;
}

AppPermissionManager.notifyAboutError(
new PermissionDeniedError({
appId,
missingPermissions: [AppPermissions.email.sendOTP],
missingPermissions: [AppPermissions.email.send],
}),
);

Expand Down
2 changes: 2 additions & 0 deletions src/server/bridges/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AppBridges } from './AppBridges';
import { AppDetailChangesBridge } from './AppDetailChangesBridge';
import { CloudWorkspaceBridge } from './CloudWorkspaceBridge';
import { CommandBridge } from './CommandBridge';
import { EmailBridge } from './EmailBridge';
import { EnvironmentalVariableBridge } from './EnvironmentalVariableBridge';
import { HttpBridge, IHttpBridgeRequestInfo } from './HttpBridge';
import { IInternalBridge } from './IInternalBridge';
Expand Down Expand Up @@ -40,6 +41,7 @@ export {
ServerSettingBridge,
UserBridge,
UploadBridge,
EmailBridge,
UiInteractionBridge,
SchedulerBridge,
AppBridges,
Expand Down
2 changes: 1 addition & 1 deletion src/server/permissions/AppPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const AppPermissions = {
write: { name: 'upload.write' },
},
email: {
sendOTP: { name: 'email.sendOTP' },
send: { name: 'email.send' },
},
ui: {
interaction: { name: 'ui.interact' },
Expand Down
3 changes: 2 additions & 1 deletion tests/test-data/bridges/emailBridge.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { IEmailDescriptor } from '../../../src/definition/email';
import { EmailBridge } from '../../../src/server/bridges/EmailBridge';

export class TestsEmailBridge extends EmailBridge {
protected sendOtpCodeThroughSMTP(email: string, code: string, language: string): Promise<void> {
protected sendEmail(email: IEmailDescriptor, appId: string): Promise<void> {
throw new Error('Method not implemented.');
}
}

0 comments on commit b3c29ab

Please sign in to comment.