1
0
Fork 0

chore: port email normalisation routine

dufisgsd
Paul Makles 2023-08-10 12:26:36 +01:00
parent 626b490912
commit 70133ff82b
No known key found for this signature in database
GPG Key ID: 5059F398521BB0F6
1 changed files with 32 additions and 20 deletions

View File

@ -197,6 +197,12 @@ export async function changeAccountEmail(userId: string, email: string) {
if (RESTRICT_ACCESS_LIST.includes(userId)) throw "restricted access"; if (RESTRICT_ACCESS_LIST.includes(userId)) throw "restricted access";
await checkPermission("accounts/update/email", userId); await checkPermission("accounts/update/email", userId);
const SPLIT_RE = /([^@]+)(@.+)/;
const SYMBOL_RE = /\+.+|\./g;
const segments = SPLIT_RE.exec(email);
if (!segments) throw "invalid email";
await mongo() await mongo()
.db("revolt") .db("revolt")
.collection<Account>("accounts") .collection<Account>("accounts")
@ -205,11 +211,11 @@ export async function changeAccountEmail(userId: string, email: string) {
{ {
$set: { $set: {
email: email, email: email,
email_normalised: email, email_normalised: segments[1].replace(SYMBOL_RE, "") + segments[2],
verification: { status: "Verified" }, verification: { status: "Verified" },
} },
} }
) );
} }
export async function verifyAccountEmail(userId: string) { export async function verifyAccountEmail(userId: string) {
@ -219,7 +225,8 @@ export async function verifyAccountEmail(userId: string) {
const account = await fetchAccountById(userId); const account = await fetchAccountById(userId);
if (!account) throw new Error("couldn't find account"); if (!account) throw new Error("couldn't find account");
if (account.verification.status == "Verified") throw new Error("already verified"); if (account.verification.status == "Verified")
throw new Error("already verified");
let email = account.email; let email = account.email;
if (account.verification.status == "Moving") { if (account.verification.status == "Moving") {
@ -236,9 +243,9 @@ export async function verifyAccountEmail(userId: string) {
email: email, email: email,
email_normalised: email, // <-- should be fine but someone should fix this in the future email_normalised: email, // <-- should be fine but someone should fix this in the future
verification: { status: "Verified" }, verification: { status: "Verified" },
} },
} }
) );
} }
export async function suspendUser(userId: string) { export async function suspendUser(userId: string) {
@ -274,7 +281,10 @@ export async function suspendUser(userId: string) {
} }
} }
export async function updateUserBadges(userId: string, badges: number): Promise<{ updatePublished: boolean }> { export async function updateUserBadges(
userId: string,
badges: number
): Promise<{ updatePublished: boolean }> {
await checkPermission("users/update/badges", userId, { badges }); await checkPermission("users/update/badges", userId, { badges });
await mongo().db("revolt").collection<User>("users").updateOne( await mongo().db("revolt").collection<User>("users").updateOne(
{ {
@ -299,7 +309,7 @@ export async function updateUserBadges(userId: string, badges: number): Promise<
clear: [], clear: [],
}); });
} }
} catch(e) { } catch (e) {
return { updatePublished: false }; return { updatePublished: false };
} }
@ -434,11 +444,11 @@ export async function unsuspendUser(userId: string) {
export async function wipeUserProfile( export async function wipeUserProfile(
userId: string, userId: string,
fields: { fields: {
banner: boolean, banner: boolean;
avatar: boolean, avatar: boolean;
bio: boolean, bio: boolean;
displayName: boolean, displayName: boolean;
status: boolean, status: boolean;
} }
) { ) {
await checkPermission("users/action/wipe-profile", userId); await checkPermission("users/action/wipe-profile", userId);
@ -457,15 +467,17 @@ export async function wipeUserProfile(
...(fields.banner ? { "profile.background": 1 } : {}), ...(fields.banner ? { "profile.background": 1 } : {}),
...(fields.bio ? { "profile.content": 1 } : {}), ...(fields.bio ? { "profile.content": 1 } : {}),
...(fields.status ? { "status.text": 1 } : {}), ...(fields.status ? { "status.text": 1 } : {}),
...(fields.avatar ? { "avatar": 1 } : {}), ...(fields.avatar ? { avatar: 1 } : {}),
}, },
...(fields.displayName ? { ...(fields.displayName
$set: { ? {
display_name: newDisplayName, $set: {
}, display_name: newDisplayName,
} : {}), },
}
: {}),
} }
) );
} }
export async function updateServerFlags(serverId: string, flags: number) { export async function updateServerFlags(serverId: string, flags: number) {