Skip to content

Fixed topic redirects for topics with ? in name #12122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion libs/schemas/src/entities/topic.schemas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DISALLOWED_TOPIC_NAMES_REGEX } from '@hicommonwealth/shared';
import { z } from 'zod';
import { PG_INT } from '../utils';

Expand All @@ -15,7 +16,7 @@ export const Topic = z.object({
.max(255)
.default('General')
.refine(
(v) => !v.match(/["<>%{}|\\/^`]/g),
(v) => !v.match(DISALLOWED_TOPIC_NAMES_REGEX),
'Name must not contain special characters',
),
community_id: z.string().max(255),
Expand Down
9 changes: 8 additions & 1 deletion libs/shared/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,18 @@ export const generateTopicIdentifiersFromUrl = (url: string) => {
return generateTopicIdentifiersFromUrlPart(splitURLPath?.[2] || '');
};

export const DISALLOWED_TOPIC_NAMES_REGEX = /["<>%{}|\\/^`?]/g;

export const sanitizeTopicName = (name: string) => {
return name.replaceAll(`?`, '');
};

export const generateUrlPartForTopicIdentifiers = (
topicId: string | number | undefined,
topicName: string,
) => {
return topicId ? `${topicId}-${topicName}` : `${topicName}`;
const _topicName = sanitizeTopicName(topicName);
return topicId ? `${topicId}-${_topicName}` : `${_topicName}`;
};

// WARN: Using process.env to avoid webpack failures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '../components/component_kit/new_designs/CWModal';
import { openConfirmation } from './confirmation_modal';

import { DISALLOWED_TOPIC_NAMES_REGEX } from '@hicommonwealth/shared';
import clsx from 'clsx';
import { notifySuccess } from 'controllers/app/notifications';
import { DeltaStatic } from 'quill';
Expand Down Expand Up @@ -199,7 +200,9 @@ export const EditTopicModal = ({
inputValidationFn={(text: string) => {
let newErrorMsg;

const disallowedCharMatches = text.match(/["<>%{}|\\/^`]/g);
const disallowedCharMatches = text.match(
DISALLOWED_TOPIC_NAMES_REGEX,
);
if (disallowedCharMatches) {
newErrorMsg = `The ${pluralizeWithoutNumberPrefix(
disallowedCharMatches.length,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DISALLOWED_TOPIC_NAMES_REGEX } from '@hicommonwealth/shared';
import { pluralizeWithoutNumberPrefix } from 'helpers';
import { VALIDATION_MESSAGES } from 'helpers/formValidations/messages';
import z from 'zod';
Expand All @@ -7,13 +8,13 @@ export const topicCreationValidationSchema = z.object({
.string({ invalid_type_error: VALIDATION_MESSAGES.NO_INPUT })
.min(1, { message: VALIDATION_MESSAGES.NO_INPUT })
.superRefine((value, ctx) => {
const disallowedCharMatches = value.match(/["<>%{}|\\/^`]/g);
const disallowedCharMatches = value.match(DISALLOWED_TOPIC_NAMES_REGEX);

if (disallowedCharMatches) {
const errMsg = `The ${pluralizeWithoutNumberPrefix(
const errMsg = `These ${pluralizeWithoutNumberPrefix(
disallowedCharMatches.length,
'char',
)} ${disallowedCharMatches.join(', ')} are not permitted`;
)} are not permitted: ${disallowedCharMatches.join(', ')}`;

ctx.addIssue({
code: z.ZodIssueCode.custom,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
formatDecimalToWei,
generateTopicIdentifiersFromUrl,
generateUrlPartForTopicIdentifiers,
sanitizeTopicName,
} from '@hicommonwealth/shared';
import { useGetUserEthBalanceQuery } from 'client/scripts/state/api/communityStake';
import useUserStore from 'client/scripts/state/ui/user';
Expand Down Expand Up @@ -127,7 +128,8 @@ const DiscussionsPage = () => {
window.location.href,
);
const topicObj = topics?.find(
({ name }) => name === topicIdentifiersFromURL?.topicName,
({ name }) =>
sanitizeTopicName(name) === topicIdentifiersFromURL?.topicName,
);
const topicId = topicObj?.id;

Expand Down Expand Up @@ -227,7 +229,8 @@ const DiscussionsPage = () => {
}

const validTopic = topics?.find(
(topic) => topic?.name === topicIdentifiersFromURL.topicName,
(topic) =>
sanitizeTopicName(topic?.name) === topicIdentifiersFromURL.topicName,
);
if (!validTopic) {
navigate('/discussions');
Expand Down
Loading