Skip to content
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

feat/url validation #262

Merged
merged 2 commits into from
Jan 5, 2024
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
51 changes: 45 additions & 6 deletions packages/cms/src/collections/groupchats.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import payload, {type Payload} from 'payload';
import payload, {type GeneratedTypes, type Payload} from 'payload';
import {type CollectionConfig} from 'payload/types';
import {allowUpdateDeleteOwner} from '../access/allow-update-delete-owner';
import {requireOneOf} from '../access/require-one-of';
import {typesenseClient} from '../lib/typesense';
import {type GroupchatKeyword} from '../payload-types';

type GroupchatPlatform =
GeneratedTypes['collections']['groupchats']['platform'];
const platformUrlMatchers: Record<GroupchatPlatform, RegExp> = {
discord: /^https?:\/\/(?:www\.)?discord\.(com|gg)\//i,
instagram: /^https?:\/\/(?:www\.)?instagram\.com\//i,
facebook: /^https?:\/\/(?:www\.)?facebook\.com\//i,
signal: /^https?:\/\/(?:www\.)?signal\.group\//i,
telegram: /^https?:\/\/(?:www\.)?t\.me\//i,
whatsapp: /^https?:\/\/chat\.whatsapp\.com\//i,
};

export const Groupchats: CollectionConfig = {
slug: 'groupchats',
access: {
Expand All @@ -23,6 +34,8 @@ export const Groupchats: CollectionConfig = {
type: 'text',
required: true,
validate: async (nameValue, {data, payload}) => {
if (!nameValue) return 'This field is required.';

// We need to run local operations with certain auth, so we skip if payload is not available
if (!payload) return true;

Expand Down Expand Up @@ -70,10 +83,26 @@ export const Groupchats: CollectionConfig = {
return `https://${value}`;
}

if (value?.startsWith('http:')) {
return 'https' + value.substring(4);
}

return value;
},
],
},
validate: (url, {data}) => {
if (!url) return 'This field is required.';

const urlMatcher = platformUrlMatchers[data.platform];
const match = (url as string).match(urlMatcher);

if (!match) {
return 'The URL must be a valid invite link for the selected platform!';
}

return true;
},
},
{
name: 'keywords',
Expand Down Expand Up @@ -135,11 +164,21 @@ export const Groupchats: CollectionConfig = {
}

for (const chat of req.body) {
await req.payload.create({
collection: 'groupchats',
data: chat,
user: req.user,
});
try {
await req.payload.create({
collection: 'groupchats',
data: chat,
user: req.user,
});
} catch (e) {
req.payload.logger.error(
`Failed creating groupchat ${JSON.stringify(
chat,
null,
2,
)} in batch-endpoint: `,
);
}
}

res.status(200).send('Ok');
Expand Down
2 changes: 1 addition & 1 deletion packages/cms/src/dataseeder/seed-groupchats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const seedGroupchats = async () => {
name: 'Yes Famburg',
description: 'Hamburgs YesFam group on WhatsApp',
platform: 'whatsapp',
url: 'https://example.com',
url: 'https://chat.whatsapp.com/example',
keywords: [keywords['Hamburg'], keywords['Germany'], keywords['Europe']],
promoted: 0,
},
Expand Down
10 changes: 8 additions & 2 deletions packages/cms/src/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ export interface User {
export interface Groupchat {
id: number;
name: string;
platform: 'discord' | 'facebook' | 'signal' | 'telegram' | 'whatsapp';
platform:
| 'discord'
| 'facebook'
| 'instagram'
| 'signal'
| 'telegram'
| 'whatsapp';
description?: string | null;
url: string;
keywords: (number | GroupchatKeyword)[];
keywords?: (number | GroupchatKeyword)[] | null;
promoted: number;
owners?: (string | User)[] | null;
updatedAt: string;
Expand Down
Loading