feat: add attach from documents
Signed-off-by: Sebastian <krupinski01@gmail.com>
This commit is contained in:
@@ -14,6 +14,7 @@ import type {
|
||||
import { ComposerMode } from '@/types/composer'
|
||||
import type { ComposerDraft, ComposerDraftAttachment, ComposerDraftMessage, ComposerSenderIdentity } from '@/types/composer'
|
||||
import { EntityObject, MessageAddressObject, ServiceObject } from '@MailManager/models'
|
||||
import type { EntityIdentifier } from '@MailManager/types/common'
|
||||
import type { MessageAddressInterface } from '@MailManager/types/message'
|
||||
|
||||
export const useMailCompositionStore = defineStore('mailCompositionStore', () => {
|
||||
@@ -274,7 +275,7 @@ export const useMailCompositionStore = defineStore('mailCompositionStore', () =>
|
||||
queueSave(false)
|
||||
}
|
||||
|
||||
async function addAttachments(files: File[]) {
|
||||
async function addAttachmentsFromDevice(files: File[]) {
|
||||
if (!activeDraft.value || files.length === 0) {
|
||||
return
|
||||
}
|
||||
@@ -288,7 +289,7 @@ export const useMailCompositionStore = defineStore('mailCompositionStore', () =>
|
||||
return [identifier, {
|
||||
identifier,
|
||||
composition,
|
||||
origin: 'upload',
|
||||
origin: 'device',
|
||||
name: file.name,
|
||||
type: file.type || 'application/octet-stream',
|
||||
size: file.size,
|
||||
@@ -327,6 +328,66 @@ export const useMailCompositionStore = defineStore('mailCompositionStore', () =>
|
||||
})
|
||||
}
|
||||
|
||||
async function addAttachmentsFromDocuments(items: { identifier: string; name: string; mimeType?: string; size?: number }[]) {
|
||||
if (!activeDraft.value || items.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const composition = activeDraft.value.identifier
|
||||
const attachments = Object.fromEntries(
|
||||
items.map(item => {
|
||||
const identifier = createIdentifier()
|
||||
return [identifier, {
|
||||
identifier,
|
||||
composition,
|
||||
origin: 'documents',
|
||||
name: item.name,
|
||||
type: item.mimeType || 'application/octet-stream',
|
||||
size: item.size ?? 0,
|
||||
source: item.identifier as EntityIdentifier,
|
||||
} satisfies CompositionAttachmentInterface]
|
||||
}),
|
||||
)
|
||||
|
||||
const request: CompositionAttachmentAddRequest = {
|
||||
composition,
|
||||
attachments,
|
||||
}
|
||||
|
||||
const response = await compositionService.attachmentAdd(request)
|
||||
|
||||
if (!activeDraft.value || response.composition !== activeDraft.value.identifier) {
|
||||
return
|
||||
}
|
||||
|
||||
if (response.disposition === 'error') {
|
||||
console.error('[Mail][Composer] Failed to add attachments from documents:', response.error)
|
||||
mailUiStore.notify(`Failed to attach documents: ${response.error?.message || 'Unknown error'}`, 'error')
|
||||
return
|
||||
}
|
||||
|
||||
Object.values(response.attachments).forEach(attachment => {
|
||||
activeDraft.value!.attachments[attachment.identifier] = {
|
||||
identifier: attachment.identifier,
|
||||
composition: attachment.composition,
|
||||
origin: attachment.origin,
|
||||
name: attachment.name,
|
||||
type: attachment.type,
|
||||
size: attachment.size,
|
||||
source: attachment.source,
|
||||
}
|
||||
})
|
||||
|
||||
if (response.failed?.length) {
|
||||
mailUiStore.notify(
|
||||
response.failed.length === 1
|
||||
? '1 document could not be attached'
|
||||
: `${response.failed.length} documents could not be attached`,
|
||||
'warning',
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
async function removeAttachment(identifier: string) {
|
||||
if (!activeDraft.value) {
|
||||
return
|
||||
@@ -424,17 +485,14 @@ export const useMailCompositionStore = defineStore('mailCompositionStore', () =>
|
||||
updateRecipients,
|
||||
updateSubject,
|
||||
updateBody,
|
||||
addAttachments,
|
||||
addAttachmentsFromDevice,
|
||||
addAttachmentsFromDocuments,
|
||||
removeAttachment,
|
||||
flushSave,
|
||||
}
|
||||
})
|
||||
|
||||
function buildDraft(
|
||||
mode: ComposerMode,
|
||||
sender: ComposerSenderIdentity,
|
||||
source?: EntityObject | MessageAddressObject | null,
|
||||
): ComposerDraft {
|
||||
function buildDraft(mode: ComposerMode, sender: ComposerSenderIdentity, source?: EntityObject | MessageAddressObject | null): ComposerDraft {
|
||||
const composition = createIdentifier()
|
||||
const freshMessage = buildMessage(mode, source)
|
||||
const freshAttachments = buildAttachments(composition, mode, source)
|
||||
@@ -452,10 +510,7 @@ function buildDraft(
|
||||
}
|
||||
}
|
||||
|
||||
function buildMessage(
|
||||
mode: ComposerMode,
|
||||
source: EntityObject | MessageAddressObject | null | undefined,
|
||||
): ComposerDraftMessage {
|
||||
function buildMessage(mode: ComposerMode, source: EntityObject | MessageAddressObject | null | undefined): ComposerDraftMessage {
|
||||
|
||||
if (!source) {
|
||||
return emptyMessage()
|
||||
@@ -521,7 +576,7 @@ function buildAttachments(composition: string, mode: ComposerMode, source?: Enti
|
||||
attachments[id] = {
|
||||
identifier: id,
|
||||
composition: composition,
|
||||
origin: 'source',
|
||||
origin: 'message',
|
||||
name: attachment.name || '',
|
||||
type: attachment.type || 'application/octet-stream',
|
||||
size: attachment.size || 0,
|
||||
|
||||
Reference in New Issue
Block a user