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

fix(chat): fix error message for publish/share external files (Issue #1984) #2090

Merged
merged 8 commits into from
Sep 10, 2024
30 changes: 23 additions & 7 deletions apps/chat/src/store/publication/publication.epics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { AnyAction } from '@reduxjs/toolkit';

import { combineEpics } from 'redux-observable';

import { BucketService } from '@/src/utils/app/data/bucket-service';
import { ConversationService } from '@/src/utils/app/data/conversation-service';
import { PromptService } from '@/src/utils/app/data/prompt-service';
import { PublicationService } from '@/src/utils/app/data/publication-service';
Expand Down Expand Up @@ -86,6 +87,25 @@ const publishEpic: AppEpic = (action$) =>
action$.pipe(
filter(PublicationActions.publish.match),
switchMap(({ payload }) => {
const fileIds = payload.resources
.map(({ sourceUrl }) => sourceUrl)
.filter((id) => id && isFileId(id));
const userBucket = BucketService.getBucket();

const isPublishingExternalFiles = fileIds.some((id) => {
const { bucket: fileBucket } = splitEntityId(id as string);

return fileBucket !== userBucket;
});

if (isPublishingExternalFiles) {
return of(
PublicationActions.publishFail(
errorsMessages.publicationWithExternalFilesFailed,
),
);
}

return PublicationService.createPublicationRequest(payload).pipe(
switchMap(() => EMPTY),
catchError((err) => {
Expand All @@ -100,13 +120,9 @@ const publishFailEpic: AppEpic = (action$) =>
action$.pipe(
filter(PublicationActions.publishFail.match),
map(({ payload }) => {
let msg = payload ?? errorsMessages.publicationFailed;

if (payload?.toLowerCase()?.trim()?.startsWith('not private url')) {
msg = errorsMessages.publicationWithExternalFilesFailed;
}

return UIActions.showErrorToast(translate(msg));
return UIActions.showErrorToast(
translate(payload ?? errorsMessages.publicationFailed),
);
}),
);

Expand Down
37 changes: 26 additions & 11 deletions apps/chat/src/store/share/share.epics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import { combineEpics } from 'redux-observable';

import { ConversationService } from '@/src/utils/app/data/conversation-service';
import { ShareService } from '@/src/utils/app/data/share-service';
import { constructPath, isAttachmentLink } from '@/src/utils/app/file';
import {
constructPath,
isAttachmentLink,
isConversationHasExternalAttachments,
} from '@/src/utils/app/file';
import { splitEntityId } from '@/src/utils/app/folders';
import { isConversationId, isFolderId, isPromptId } from '@/src/utils/app/id';
import { EnumMapper } from '@/src/utils/app/mappers';
Expand Down Expand Up @@ -115,6 +119,14 @@ const shareConversationEpic: AppEpic = (action$) =>
res?.playback?.messagesStack || res?.messages,
);

if (res && isConversationHasExternalAttachments(res)) {
return of(
ShareActions.shareFail(
errorsMessages.shareWithExternalFilesFailed,
),
);
}

return ShareService.share({
invitationType: ShareRequestType.link,
resources: [
Expand All @@ -133,7 +145,7 @@ const shareConversationEpic: AppEpic = (action$) =>
}),
catchError((err) => {
console.error(err);
return of(ShareActions.shareFail(err.message));
return of(ShareActions.shareFail());
}),
);
}),
Expand Down Expand Up @@ -172,6 +184,14 @@ const shareConversationFolderEpic: AppEpic = (action$) =>
)
.map((url) => ({ url }));

if (conversations.some(isConversationHasExternalAttachments)) {
return of(
ShareActions.shareFail(
errorsMessages.shareWithExternalFilesFailed,
),
);
}

return ShareService.share({
invitationType: ShareRequestType.link,
resources: [
Expand All @@ -188,7 +208,7 @@ const shareConversationFolderEpic: AppEpic = (action$) =>
}),
catchError((err) => {
console.error(err);
return of(ShareActions.shareFail(err.message));
return of(ShareActions.shareFail());
}),
);
}),
Expand Down Expand Up @@ -253,14 +273,9 @@ const shareFailEpic: AppEpic = (action$) =>
action$.pipe(
filter(ShareActions.shareFail.match),
map(({ payload }) => {
const msg = payload
?.toLowerCase()
?.trim()
?.startsWith('incorrect resource link provided')
? errorsMessages.shareWithExternalFilesFailed
: errorsMessages.shareFailed;

return UIActions.showErrorToast(translate(msg));
return UIActions.showErrorToast(
translate(payload ?? errorsMessages.shareFailed),
);
}),
);

Expand Down
31 changes: 30 additions & 1 deletion apps/chat/src/utils/app/file.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { TFunction } from 'next-i18next';

import { BucketService } from '@/src/utils/app/data/bucket-service';

import { Attachment, Conversation } from '@/src/types/chat';
import { UploadStatus } from '@/src/types/common';
import { DialFile, DialLink, FileFolderAttachment } from '@/src/types/files';
Expand All @@ -9,7 +11,7 @@ import { FOLDER_ATTACHMENT_CONTENT_TYPE } from '@/src/constants/folders';

import { ApiUtils } from '../server/api';
import { doesHaveDotsInTheEnd } from './common';
import { getPathToFolderById } from './folders';
import { getPathToFolderById, splitEntityId } from './folders';
import { isFolderId } from './id';

import escapeRegExp from 'lodash-es/escapeRegExp';
Expand Down Expand Up @@ -434,3 +436,30 @@ export const isAbsoluteUrl = (url: string): boolean => {

export const getDownloadPath = (file: DialFile) =>
file.absolutePath ? constructPath(file.absolutePath, file.name) : file.id;

export const isConversationHasExternalAttachments = (
conversation: Conversation,
): boolean => {
const userBucket = BucketService.getBucket();
const messages =
conversation.playback?.messagesStack ?? conversation.isReplay
? [
...(conversation.replay?.replayUserMessagesStack ?? []),
...conversation.messages,
]
: conversation.messages;

const attachments = messages
.flatMap((message) => message.custom_content?.attachments ?? [])
.filter(
(attachment) => attachment.url && !isAttachmentLink(attachment.url),
);

return attachments.some((attachment) => {
const { bucket: attachmentBucket } = splitEntityId(
attachment.url as string,
);

return attachmentBucket !== userBucket;
});
};