Skip to content

Commit

Permalink
refactor: DRY out usage of isProfane
Browse files Browse the repository at this point in the history
  • Loading branch information
ojeytonwilliams committed Jun 11, 2024
1 parent e691fed commit a06a559
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
12 changes: 12 additions & 0 deletions api/src/routes/helpers/is-restricted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { isProfane } from 'no-profanity';

import { blocklistedUsernames } from '../../../../shared/config/constants';

/**
* Checks if a username is restricted (i.e. It's profane or reserved).
* @param username - The username to check.
* @returns True if the username is restricted, false otherwise.
*/
export const isRestricted = (username: string): boolean => {
return isProfane(username) || blocklistedUsernames.includes(username);
};
8 changes: 2 additions & 6 deletions api/src/routes/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import type {
} from 'fastify';
import { ResolveFastifyReplyType } from 'fastify/types/type-provider';
import { differenceInMinutes } from 'date-fns';
import { isProfane } from 'no-profanity';

import { blocklistedUsernames } from '../../../shared/config/constants';
import { isValidUsername } from '../../../shared/utils/validate';
import * as schemas from '../schemas';
import { createAuthToken } from '../utils/tokens';
import { API_LOCATION } from '../utils/env';
import { isRestricted } from './helpers/is-restricted';

type WaitMesssageArgs = {
sentAt: Date | null;
Expand Down Expand Up @@ -401,17 +400,14 @@ ${isLinkSentWithinLimitTTL}`
});
}

const isUserNameProfane = isProfane(newUsername);
const onBlocklist = blocklistedUsernames.includes(newUsername);

const usernameTaken =
newUsername === oldUsername
? false
: await fastify.prisma.user.count({
where: { username: newUsername }
});

if (usernameTaken || isUserNameProfane || onBlocklist) {
if (usernameTaken || isRestricted(newUsername)) {
void reply.code(400);
return reply.send({
message: 'flash.username-taken',
Expand Down
10 changes: 4 additions & 6 deletions api/src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { type FastifyPluginCallbackTypebox } from '@fastify/type-provider-typebo
import { Portfolio } from '@prisma/client';
import { ObjectId } from 'mongodb';
import _ from 'lodash';
import { isProfane } from 'no-profanity';

import * as schemas from '../schemas';
// Loopback creates a 64 character string for the user id, this customizes
Expand All @@ -28,6 +27,7 @@ import { trimTags } from '../utils/validation';
import { generateReportEmail } from '../utils/email-templates';
import { createResetProperties } from '../utils/create-user';
import { challengeTypes } from '../../../shared/config/challenge-types';
import { isRestricted } from './helpers/is-restricted';

// user flags that the api-server returns as false if they're missing in the
// user document. Since Prisma returns null for missing fields, we need to
Expand Down Expand Up @@ -64,7 +64,8 @@ const nullableFlags = [
] as const;

type NullableFlag = (typeof nullableFlags)[number];
import { blocklistedUsernames } from '../../../shared/config/constants';



/**
* Helper function to get the api url from the shared transcript link.
Expand Down Expand Up @@ -802,10 +803,7 @@ export const userPublicGetRoutes: FastifyPluginCallbackTypebox = (

const username = req.query.username.toLowerCase();

const isRestricted =
blocklistedUsernames.includes(username) || isProfane(username);

if (isRestricted) return await reply.send({ exists: true });
if (isRestricted(username)) return await reply.send({ exists: true });

const exists =
(await fastify.prisma.user.count({
Expand Down

0 comments on commit a06a559

Please sign in to comment.