diff --git a/apps/chat/src/store/publication/publication.epics.ts b/apps/chat/src/store/publication/publication.epics.ts index 4546990bb9..f889fffc16 100644 --- a/apps/chat/src/store/publication/publication.epics.ts +++ b/apps/chat/src/store/publication/publication.epics.ts @@ -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'; @@ -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) => { @@ -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), + ); }), ); diff --git a/apps/chat/src/store/share/share.epics.ts b/apps/chat/src/store/share/share.epics.ts index 0a5a10bae5..3795c283b8 100644 --- a/apps/chat/src/store/share/share.epics.ts +++ b/apps/chat/src/store/share/share.epics.ts @@ -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'; @@ -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: [ @@ -133,7 +145,7 @@ const shareConversationEpic: AppEpic = (action$) => }), catchError((err) => { console.error(err); - return of(ShareActions.shareFail(err.message)); + return of(ShareActions.shareFail()); }), ); }), @@ -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: [ @@ -188,7 +208,7 @@ const shareConversationFolderEpic: AppEpic = (action$) => }), catchError((err) => { console.error(err); - return of(ShareActions.shareFail(err.message)); + return of(ShareActions.shareFail()); }), ); }), @@ -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), + ); }), ); diff --git a/apps/chat/src/utils/app/file.ts b/apps/chat/src/utils/app/file.ts index d9b4b91eec..3405074ca1 100644 --- a/apps/chat/src/utils/app/file.ts +++ b/apps/chat/src/utils/app/file.ts @@ -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'; @@ -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'; @@ -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; + }); +};