summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@protonmail.com>2023-02-24 07:02:36 -0500
committerPuyodead1 <puyodead@protonmail.com>2023-02-24 07:02:36 -0500
commit770217b4b20bbc249605bd67bc5b4056621ed1f9 (patch)
treec0e97e4e9246769cb2f596963b90bbb947ab17a2 /src
parentfirst batch of requested changes (diff)
downloadserver-770217b4b20bbc249605bd67bc5b4056621ed1f9.tar.xz
simplify replacer function
Diffstat (limited to 'src')
-rw-r--r--src/util/util/Email.ts54
1 files changed, 20 insertions, 34 deletions
diff --git a/src/util/util/Email.ts b/src/util/util/Email.ts
index 714b3db2..45919f9e 100644
--- a/src/util/util/Email.ts
+++ b/src/util/util/Email.ts
@@ -250,40 +250,26 @@ export const Email: {
 		},
 	) {
 		const { instanceName } = Config.get().general;
-		template = template.replaceAll("{instanceName}", instanceName);
-		template = template.replaceAll("{userUsername}", user.username);
-		template = template.replaceAll(
-			"{userDiscriminator}",
-			user.discriminator,
-		);
-		template = template.replaceAll("{userId}", user.id);
-		if (user.phone)
-			template = template.replaceAll(
-				"{phoneNumber}",
-				user.phone.slice(-4),
-			);
-		if (user.email)
-			template = template.replaceAll("{userEmail}", user.email);
-
-		// template specific replacements
-		if (emailVerificationUrl)
-			template = template.replaceAll(
-				"{emailVerificationUrl}",
-				emailVerificationUrl,
-			);
-		if (passwordResetUrl)
-			template = template.replaceAll(
-				"{passwordResetUrl}",
-				passwordResetUrl,
-			);
-		if (ipInfo) {
-			template = template.replaceAll("{ipAddress}", ipInfo.ip);
-			template = template.replaceAll("{locationCity}", ipInfo.city);
-			template = template.replaceAll("{locationRegion}", ipInfo.region);
-			template = template.replaceAll(
-				"{locationCountryName}",
-				ipInfo.country_name,
-			);
+
+		const replacements = [
+			["{instanceName}", instanceName],
+			["{userUsername}", user.username],
+			["{userDiscriminator}", user.discriminator],
+			["{userId}", user.id],
+			["{phoneNumber}", user.phone?.slice(-4)],
+			["{userEmail}", user.email],
+			["{emailVerificationUrl}", emailVerificationUrl],
+			["{passwordResetUrl}", passwordResetUrl],
+			["{ipAddress}", ipInfo?.ip],
+			["{locationCity}", ipInfo?.city],
+			["{locationRegion}", ipInfo?.region],
+			["{locationCountryName}", ipInfo?.country_name],
+		];
+
+		// loop through all replacements and replace them in the template
+		for (const [key, value] of Object.values(replacements)) {
+			if (!value) continue;
+			template = template.replace(key as string, value);
 		}
 
 		return template;