Skip to content

Commit 9c751fb

Browse files
committed
feat: provider checks protection
1 parent 8c1191a commit 9c751fb

File tree

13 files changed

+70
-70
lines changed

13 files changed

+70
-70
lines changed

apps/frontend/src/components/new-launch/providers/bluesky/bluesky.provider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ export default withProvider({
1818
dto: undefined,
1919
checkValidity: async (posts) => {
2020
if (
21-
posts.some(
22-
(p) => p.some((a) => a.path.indexOf('mp4') > -1) && p.length > 1
21+
posts?.some(
22+
(p) => p?.some((a) => (a?.path?.indexOf?.('mp4') ?? -1) > -1) && (p?.length ?? 0) > 1
2323
)
2424
) {
2525
return 'You can only upload one video per post.';
2626
}
2727

28-
if (posts.some((p) => p.length > 4)) {
28+
if (posts?.some((p) => (p?.length ?? 0) > 4)) {
2929
return 'There can be maximum 4 pictures in a post.';
3030
}
3131
return true;

apps/frontend/src/components/new-launch/providers/dribbble/dribbble.provider.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export default withProvider({
2424
SettingsComponent: DribbbleSettings,
2525
CustomPreviewComponent: undefined,
2626
dto: DribbbleDto,
27-
checkValidity: async ([firstItem, ...otherItems]) => {
28-
const isMp4 = firstItem?.find((item) => item.path.indexOf('mp4') > -1);
29-
if (firstItem.length !== 1) {
27+
checkValidity: async ([firstItem, ...otherItems] = []) => {
28+
const isMp4 = firstItem?.find((item) => (item?.path?.indexOf?.('mp4') ?? -1) > -1);
29+
if (firstItem?.length !== 1) {
3030
return 'Requires one item';
3131
}
3232
if (isMp4) {
@@ -41,7 +41,7 @@ export default withProvider({
4141
// @ts-ignore
4242
resolve({ width: this.width, height: this.height });
4343
};
44-
url.src = firstItem[0].path;
44+
url.src = firstItem?.[0]?.path;
4545
});
4646
if (
4747
(details?.width === 400 && details?.height === 300) ||

apps/frontend/src/components/new-launch/providers/gmb/gmb.provider.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,20 @@ export default withProvider({
167167
dto: GmbSettingsDto,
168168
checkValidity: async (items, settings: any) => {
169169
// GMB posts can have text only, or text with one image
170-
if (items.length > 0 && items[0].length > 1) {
170+
if ((items?.length ?? 0) > 0 && (items?.[0]?.length ?? 0) > 1) {
171171
return 'Google My Business posts can only have one image';
172172
}
173173

174174
// Check for video - GMB doesn't support video in local posts
175-
if (items.length > 0 && items[0].length > 0) {
176-
const media = items[0][0];
177-
if (media.path.indexOf('mp4') > -1) {
175+
if ((items?.length ?? 0) > 0 && (items?.[0]?.length ?? 0) > 0) {
176+
const media = items?.[0]?.[0];
177+
if ((media?.path?.indexOf?.('mp4') ?? -1) > -1) {
178178
return 'Google My Business posts do not support video attachments';
179179
}
180180
}
181181

182182
// Event posts require a title
183-
if (settings.topicType === 'EVENT' && !settings.eventTitle) {
183+
if (settings?.topicType === 'EVENT' && !settings?.eventTitle) {
184184
return 'Event posts require an event title';
185185
}
186186

apps/frontend/src/components/new-launch/providers/instagram/instagram.collaborators.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,18 @@ export default withProvider<InstagramDto>({
6060
SettingsComponent: InstagramCollaborators,
6161
CustomPreviewComponent: InstagramPreview,
6262
dto: InstagramDto,
63-
checkValidity: async ([firstPost, ...otherPosts], settings) => {
64-
if (!firstPost.length) {
63+
checkValidity: async ([firstPost, ...otherPosts] = [], settings) => {
64+
if (!firstPost?.length) {
6565
return 'Should have at least one media';
6666
}
67-
if (firstPost.length > 1 && settings.post_type === 'story') {
67+
if ((firstPost?.length ?? 0) > 1 && settings?.post_type === 'story') {
6868
return 'Stories can only have one media';
6969
}
7070
const checkVideosLength = await Promise.all(
7171
firstPost
72-
.filter((f) => f.path.indexOf('mp4') > -1)
73-
.flatMap((p) => p.path)
74-
.map((p) => {
72+
?.filter((f) => (f?.path?.indexOf?.('mp4') ?? -1) > -1)
73+
?.flatMap((p) => p?.path)
74+
?.map((p) => {
7575
return new Promise<number>((res) => {
7676
const video = document.createElement('video');
7777
video.preload = 'metadata';
@@ -80,13 +80,13 @@ export default withProvider<InstagramDto>({
8080
res(video.duration);
8181
});
8282
});
83-
})
83+
}) ?? []
8484
);
8585
for (const video of checkVideosLength) {
86-
if (video > 60 && settings.post_type === 'story') {
86+
if (video > 60 && settings?.post_type === 'story') {
8787
return 'Stories should be maximum 60 seconds';
8888
}
89-
if (video > 180 && settings.post_type === 'post') {
89+
if (video > 180 && settings?.post_type === 'post') {
9090
return 'Reel should be maximum 180 seconds';
9191
}
9292
}

apps/frontend/src/components/new-launch/providers/lemmy/lemmy.provider.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ export default withProvider({
7373
CustomPreviewComponent: undefined,
7474
dto: LemmySettingsDto,
7575
checkValidity: async (items) => {
76-
const [firstItems] = items;
76+
const [firstItems] = items ?? [];
7777
if (
78-
firstItems.length &&
79-
firstItems[0].path.indexOf('png') === -1 &&
80-
firstItems[0].path.indexOf('jpg') === -1 &&
81-
firstItems[0].path.indexOf('jpef') === -1 &&
82-
firstItems[0].path.indexOf('gif') === -1
78+
firstItems?.length &&
79+
(firstItems?.[0]?.path?.indexOf?.('png') ?? -1) === -1 &&
80+
(firstItems?.[0]?.path?.indexOf?.('jpg') ?? -1) === -1 &&
81+
(firstItems?.[0]?.path?.indexOf?.('jpef') ?? -1) === -1 &&
82+
(firstItems?.[0]?.path?.indexOf?.('gif') ?? -1) === -1
8383
) {
8484
return 'You can set only one picture for a cover';
8585
}

apps/frontend/src/components/new-launch/providers/linkedin/linkedin.provider.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ export default withProvider<LinkedinDto>({
3333
CustomPreviewComponent: LinkedinPreview,
3434
dto: LinkedinDto,
3535
checkValidity: async (posts, vals) => {
36-
const [firstPost, ...restPosts] = posts;
36+
const [firstPost, ...restPosts] = posts ?? [];
3737

3838
if (
39-
vals.post_as_images_carousel &&
40-
(firstPost.length < 2 ||
41-
firstPost.some((p) => p.path.indexOf('mp4') > -1))
39+
vals?.post_as_images_carousel &&
40+
((firstPost?.length ?? 0) < 2 ||
41+
firstPost?.some((p) => (p?.path?.indexOf?.('mp4') ?? -1) > -1))
4242
) {
4343
return 'Carousel can only be created with 2 or more images and no videos.';
4444
}
4545

4646
if (
47-
firstPost.length > 1 &&
48-
firstPost.some((p) => p.path.indexOf('mp4') > -1)
47+
(firstPost?.length ?? 0) > 1 &&
48+
firstPost?.some((p) => (p?.path?.indexOf?.('mp4') ?? -1) > -1)
4949
) {
5050
return 'Can have maximum 1 media when selecting a video.';
5151
}
52-
if (restPosts.some((p) => p.length > 0)) {
52+
if (restPosts?.some((p) => (p?.length ?? 0) > 0)) {
5353
return 'Comments can only contain text.';
5454
}
5555
return true;

apps/frontend/src/components/new-launch/providers/pinterest/pinterest.provider.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,42 +34,42 @@ export default withProvider({
3434
SettingsComponent: PinterestSettings,
3535
CustomPreviewComponent: PinterestPreview,
3636
dto: PinterestSettingsDto,
37-
checkValidity: async ([firstItem, ...otherItems]) => {
38-
const isMp4 = firstItem?.find((item) => item.path.indexOf('mp4') > -1);
37+
checkValidity: async ([firstItem, ...otherItems] = []) => {
38+
const isMp4 = firstItem?.find((item) => (item?.path?.indexOf?.('mp4') ?? -1) > -1);
3939
const isPicture = firstItem?.find(
40-
(item) => item.path.indexOf('mp4') === -1
40+
(item) => (item?.path?.indexOf?.('mp4') ?? -1) === -1
4141
);
42-
if (firstItem.length === 0) {
42+
if ((firstItem?.length ?? 0) === 0) {
4343
return 'Requires at least one media';
4444
}
45-
if (isMp4 && firstItem.length !== 2 && !isPicture) {
45+
if (isMp4 && firstItem?.length !== 2 && !isPicture) {
4646
return 'If posting a video you have to also include a cover image as second media';
4747
}
48-
if (isMp4 && firstItem.length > 2) {
48+
if (isMp4 && (firstItem?.length ?? 0) > 2) {
4949
return 'If posting a video you can only have two media items';
5050
}
5151

5252
if (
53-
firstItem.length > 1 &&
54-
firstItem.every((p) => p.path.indexOf('mp4') == -1)
53+
(firstItem?.length ?? 0) > 1 &&
54+
firstItem?.every((p) => (p?.path?.indexOf?.('mp4') ?? -1) == -1)
5555
) {
5656
const loadAll: Array<{
5757
width: number;
5858
height: number;
5959
}> = (await Promise.all(
60-
firstItem.map((p) => {
60+
firstItem?.map((p) => {
6161
return new Promise((resolve, reject) => {
6262
const url = new Image();
6363
url.onload = function () {
6464
// @ts-ignore
6565
resolve({ width: this.width, height: this.height });
6666
};
67-
url.src = p.path;
67+
url.src = p?.path;
6868
});
69-
})
69+
}) ?? []
7070
)) as any;
71-
const checkAllTheSameWidthHeight = loadAll.every((p, i, arr) => {
72-
return p.width === arr[0].width && p.height === arr[0].height;
71+
const checkAllTheSameWidthHeight = loadAll?.every((p, i, arr) => {
72+
return p?.width === arr?.[0]?.width && p?.height === arr?.[0]?.height;
7373
});
7474
if (!checkAllTheSameWidthHeight) {
7575
return 'Requires all images to have the same width and height';

apps/frontend/src/components/new-launch/providers/reddit/reddit.provider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,15 @@ export default withProvider({
218218
if (
219219
settings?.subreddit?.some(
220220
(p: any, index: number) =>
221-
p?.value?.type === 'media' && posts[0].length !== 1
221+
p?.value?.type === 'media' && posts?.[0]?.length !== 1
222222
)
223223
) {
224224
return 'When posting a media post, you must attached exactly one media file.';
225225
}
226226

227227
if (
228-
posts.some((p) =>
229-
p.some((a) => !a.thumbnail && a.path.indexOf('mp4') > -1)
228+
posts?.some((p) =>
229+
p?.some((a) => !a?.thumbnail && (a?.path?.indexOf?.('mp4') ?? -1) > -1)
230230
)
231231
) {
232232
return 'You must attach a thumbnail to your video post.';

apps/frontend/src/components/new-launch/providers/threads/threads.provider.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ export default withProvider({
1515
SettingsComponent: SettingsComponent,
1616
CustomPreviewComponent: undefined,
1717
dto: undefined,
18-
checkValidity: async ([firstPost, ...otherPosts], settings) => {
18+
checkValidity: async ([firstPost, ...otherPosts] = [], settings) => {
1919
const checkVideosLength = await Promise.all(
2020
firstPost
21-
.filter((f) => f.path.indexOf('mp4') > -1)
22-
.flatMap((p) => p.path)
23-
.map((p) => {
21+
?.filter((f) => (f?.path?.indexOf?.('mp4') ?? -1) > -1)
22+
?.flatMap((p) => p?.path)
23+
?.map((p) => {
2424
return new Promise<number>((res) => {
2525
const video = document.createElement('video');
2626
video.preload = 'metadata';
@@ -29,7 +29,7 @@ export default withProvider({
2929
res(video.duration);
3030
});
3131
});
32-
})
32+
}) ?? []
3333
);
3434

3535
for (const video of checkVideosLength) {

apps/frontend/src/components/new-launch/providers/tiktok/tiktok.provider.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const TikTokSettings: FC<{
2626
const t = useT();
2727

2828
const isTitle = useMemo(() => {
29-
return value?.[0].image.some((p) => p.path.indexOf('mp4') === -1);
29+
return value?.[0]?.image?.some((p) => (p?.path?.indexOf?.('mp4') ?? -1) === -1);
3030
}, [value]);
3131

3232
const disclose = watch('disclose');
@@ -300,18 +300,18 @@ export default withProvider({
300300
CustomPreviewComponent: TiktokPreview,
301301
dto: TikTokDto,
302302
checkValidity: async (items) => {
303-
const [firstItems] = items;
304-
if (firstItems.length === 0) {
303+
const [firstItems] = items ?? [];
304+
if ((firstItems?.length ?? 0) === 0) {
305305
return 'No video / images selected';
306306
}
307307
if (
308-
firstItems.length > 1 &&
309-
firstItems?.some((p) => p?.path?.indexOf('mp4') > -1)
308+
(firstItems?.length ?? 0) > 1 &&
309+
firstItems?.some((p) => (p?.path?.indexOf?.('mp4') ?? -1) > -1)
310310
) {
311311
return 'Only pictures are supported when selecting multiple items';
312312
} else if (
313313
firstItems?.length !== 1 &&
314-
firstItems?.[0]?.path?.indexOf('mp4') > -1
314+
(firstItems?.[0]?.path?.indexOf?.('mp4') ?? -1) > -1
315315
) {
316316
return 'You need one media';
317317
}

0 commit comments

Comments
 (0)