Skip to content

Commit d5d5d3b

Browse files
committed
refactor(v1): bind email send message support language (#763)
* refactor(v1): bind email send message support language * refactor: move email template to a single file
1 parent 3a25f5d commit d5d5d3b

File tree

3 files changed

+47
-39
lines changed

3 files changed

+47
-39
lines changed

src/utils/Email.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,35 @@ export class Email {
173173
});
174174
}
175175
}
176+
177+
export class EmailUtils {
178+
public static getSubject(_type: "register" | "reset" | "bind", language?: string): string {
179+
if (language && language.startsWith("zh")) {
180+
return "Flat 验证码";
181+
} else {
182+
return "Flat Verification Code";
183+
}
184+
}
185+
186+
public static getMessage(
187+
type: "register" | "reset" | "bind",
188+
email: string,
189+
code: string,
190+
language?: string,
191+
): string {
192+
const name = email.split("@")[0];
193+
if (language && language.startsWith("zh")) {
194+
if (type === "register") {
195+
return `${name},你好!<br><br>感谢注册 <a href="http://flat.whiteboard.agora.io/">Flat 在线教室</a>,请在10分钟内输入验证码:<br><br><h1 style="text-align:center">${code}</h1><br><br>Flat 是一款<a href="https://github.com/netless-io/flat">开源</a>的在线授课软件,专为个人老师设计。我们努力克制保持简单、清爽、专注课中互动体验,希望可以给你带来愉悦的上课体验。<br><br>目前 Flat 正在积极开发中,如果你在使用过程中遇到问题,欢迎联系我进行反馈。它在一天天长大,我们很高兴与你分享这份喜悦。<br><br>Leo Yang<br>Flat 产品经理<br><a href="mailto:[email protected]">[email protected]</a>`;
196+
} else {
197+
return `${name},你好!请在10分钟内输入验证码:<br><br><h1 style="text-align:center">${code}</h1><br><br>目前 Flat 正在积极开发中,如果你在使用过程中遇到问题,欢迎联系我进行反馈。它在一天天长大,我们很高兴与你分享这份喜悦。<br><br>Leo Yang<br>Flat 产品经理<br><a href="mailto:[email protected]">[email protected]</a>`;
198+
}
199+
} else {
200+
if (type === "register") {
201+
return `Hello, ${name}! <br><br>Thank you for registering with <a href="http://flat.whiteboard.agora.io/">Flat Online Classroom</a>. Please enter the verification code within 10 minutes:<br><br><h1 style="text-align:center">${code}</h1><br><br>Flat is an <a href="https://github.com/netless-io/flat">open-source</a> online teaching software designed specifically for freelance teachers. We strive to maintain a simple, refreshing, and focused in-class interactive experience, aiming to provide you with a pleasant teaching experience.<br><br>Currently, Flat is actively under development. If you encounter any issues during usage, please feel free to contact me for feedback. It is growing day by day, and we are delighted to share this joy with you.<br><br>Thanks and Regards,<br>Leo Yang<br>Flat PM<br><a href="mailto:[email protected]">[email protected]</a>`;
202+
} else {
203+
return `Hello, ${name}! Please enter the verification code within 10 minutes:<br><br><h1 style="text-align:center">${code}</h1><br><br><Currently, Flat is actively under development. If you encounter any issues during usage, please feel free to contact me for feedback. It is growing day by day, and we are delighted to share this joy with you.<br><br>Thanks and Regards,<br>Leo Yang<br>Flat PM<br><a href="mailto:[email protected]">[email protected]</a>`;
204+
}
205+
}
206+
}
207+
}

src/v1/controller/user/binding/platform/email/SendMessage.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AbstractController } from "../../../../../../abstract/controller";
44
import { FastifySchema, Response, ResponseError } from "../../../../../../types/Server";
55
import RedisService from "../../../../../../thirdPartyService/RedisService";
66
import { RedisKey } from "../../../../../../utils/Redis";
7-
import { Email } from "../../../../../../utils/Email";
7+
import { Email, EmailUtils } from "../../../../../../utils/Email";
88
import { Status } from "../../../../../../constants/Project";
99
import { MessageExpirationSecond, MessageIntervalSecond } from "./Constants";
1010
import { ServiceUserEmail } from "../../../../../service/user/UserEmail";
@@ -27,6 +27,10 @@ export class SendMessage extends AbstractController<RequestType, ResponseType> {
2727
type: "string",
2828
format: "email",
2929
},
30+
language: {
31+
type: "string",
32+
nullable: true,
33+
},
3034
},
3135
},
3236
};
@@ -36,12 +40,13 @@ export class SendMessage extends AbstractController<RequestType, ResponseType> {
3640
};
3741

3842
public async execute(): Promise<Response<ResponseType>> {
39-
const { email } = this.body;
43+
const { email, language } = this.body;
44+
4045
const sms = new Email(email, {
4146
tagName: "bind",
42-
subject: "Flat Verification Code",
47+
subject: EmailUtils.getSubject("bind", language),
4348
htmlBody: (email: string, code: string) =>
44-
`Hello, ${email}! Please enter the verification code within 10 minutes:<br><br><h1 style="text-align:center">${code}</h1><br><br><Currently, Flat is actively under development. If you encounter any issues during usage, please feel free to contact me for feedback. It is growing day by day, and we are delighted to share this joy with you.<br><br>Thanks and Regards,<br>Leo Yang<br>Flat PM<br><a href="mailto:[email protected]">[email protected]</a>`,
49+
EmailUtils.getMessage("bind", email, code, language),
4550
});
4651

4752
if (await SendMessage.canSend(email)) {
@@ -93,6 +98,7 @@ export class SendMessage extends AbstractController<RequestType, ResponseType> {
9398
interface RequestType {
9499
body: {
95100
email: string;
101+
language?: string;
96102
};
97103
}
98104

src/v2/services/user/email.ts

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { EmailSMS, Server } from "../../../constants/Config";
66
import { FError } from "../../../error/ControllerError";
77
import { ErrorCode } from "../../../ErrorCode";
88
import { createLoggerService } from "../../../logger";
9-
import { Email } from "../../../utils/Email";
9+
import { Email, EmailUtils } from "../../../utils/Email";
1010
import { hash } from "../../../utils/Hash";
1111
import { RedisKey } from "../../../utils/Redis";
1212
import { MessageExpirationSecond, MessageIntervalSecond } from "../../constants";
@@ -25,9 +25,9 @@ export class UserEmailService {
2525
public async sendMessageForRegister(email: string, language?: string): Promise<void> {
2626
const sms = new Email(email, {
2727
tagName: "register",
28-
subject: this.getSubject("register", language),
28+
subject: EmailUtils.getSubject("register", language),
2929
htmlBody: (email: string, code: string) =>
30-
this.getMessage("register", email, code, language),
30+
EmailUtils.getMessage("register", email, code, language),
3131
});
3232

3333
if (await UserEmailService.canSend(email)) {
@@ -56,9 +56,9 @@ export class UserEmailService {
5656
public async sendMessageForReset(email: string, language?: string): Promise<void> {
5757
const sms = new Email(email, {
5858
tagName: "reset",
59-
subject: this.getSubject("reset", language),
59+
subject: EmailUtils.getSubject("reset", language),
6060
htmlBody: (email: string, code: string) =>
61-
this.getMessage("reset", email, code, language),
61+
EmailUtils.getMessage("reset", email, code, language),
6262
});
6363

6464
if (await UserEmailService.canSend(email)) {
@@ -207,36 +207,6 @@ export class UserEmailService {
207207
};
208208
}
209209

210-
private getSubject(_type: "register" | "reset", language?: string): string {
211-
if (language && language.startsWith("zh")) {
212-
return "Flat 验证码";
213-
} else {
214-
return "Flat Verification Code";
215-
}
216-
}
217-
218-
private getMessage(
219-
type: "register" | "reset" | "bind",
220-
email: string,
221-
code: string,
222-
language?: string,
223-
): string {
224-
const name = email.split("@")[0];
225-
if (language && language.startsWith("zh")) {
226-
if (type === "register") {
227-
return `${name},你好!<br><br>感谢注册 <a href="http://flat.whiteboard.agora.io/">Flat 在线教室</a>,请在10分钟内输入验证码:<br><br><h1 style="text-align:center">${code}</h1><br><br>Flat 是一款<a href="https://github.com/netless-io/flat">开源</a>的在线授课软件,专为个人老师设计。我们努力克制保持简单、清爽、专注课中互动体验,希望可以给你带来愉悦的上课体验。<br><br>目前 Flat 正在积极开发中,如果你在使用过程中遇到问题,欢迎联系我进行反馈。它在一天天长大,我们很高兴与你分享这份喜悦。<br><br>Leo Yang<br>Flat 产品经理<br><a href="mailto:[email protected]">[email protected]</a>`;
228-
} else {
229-
return `${name},你好!请在10分钟内输入验证码:<br><br><h1 style="text-align:center">${code}</h1><br><br>目前 Flat 正在积极开发中,如果你在使用过程中遇到问题,欢迎联系我进行反馈。它在一天天长大,我们很高兴与你分享这份喜悦。<br><br>Leo Yang<br>Flat 产品经理<br><a href="mailto:[email protected]">[email protected]</a>`;
230-
}
231-
} else {
232-
if (type === "register") {
233-
return `Hello, ${name}! <br><br>Thank you for registering with <a href="http://flat.whiteboard.agora.io/">Flat Online Classroom</a>. Please enter the verification code within 10 minutes:<br><br><h1 style="text-align:center">${code}</h1><br><br>Flat is an <a href="https://github.com/netless-io/flat">open-source</a> online teaching software designed specifically for freelance teachers. We strive to maintain a simple, refreshing, and focused in-class interactive experience, aiming to provide you with a pleasant teaching experience.<br><br>Currently, Flat is actively under development. If you encounter any issues during usage, please feel free to contact me for feedback. It is growing day by day, and we are delighted to share this joy with you.<br><br>Thanks and Regards,<br>Leo Yang<br>Flat PM<br><a href="mailto:[email protected]">[email protected]</a>`;
234-
} else {
235-
return `Hello, ${name}! Please enter the verification code within 10 minutes:<br><br><h1 style="text-align:center">${code}</h1><br><br><Currently, Flat is actively under development. If you encounter any issues during usage, please feel free to contact me for feedback. It is growing day by day, and we are delighted to share this joy with you.<br><br>Thanks and Regards,<br>Leo Yang<br>Flat PM<br><a href="mailto:[email protected]">[email protected]</a>`;
236-
}
237-
}
238-
}
239-
240210
private async hasPhone(userUUID: string): Promise<boolean> {
241211
const exist = await userPhoneDAO.findOne(this.DBTransaction, ["id"], {
242212
user_uuid: userUUID,

0 commit comments

Comments
 (0)