2024-08-17 22:05:57 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
import { MessageAttachmentByMessageIdInput } from './message-attachment-by-message-id.service';
|
2024-08-27 09:14:59 +01:00
|
|
|
import { AttachmentRemoteDataSourceService } from 'src/app/module/chat/data/repository/attachment/attachment-remote-repository.service'
|
|
|
|
|
import { AttachmentLocalDataSource } from 'src/app/module/chat/data/repository/attachment/attachment-local-repository.service'
|
2024-08-17 22:05:57 +01:00
|
|
|
import { err, Result } from 'neverthrow';
|
2024-08-29 12:13:15 +01:00
|
|
|
import { AttachmentTableSchema } from 'src/app/infra/database/dexie/instance/chat/schema/attachment';
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
import { createBlobUrl } from 'src/app/utils/ToBase64';
|
|
|
|
|
|
|
|
|
|
const GetMessageAttachmentLocallyByMessageIdSchema = AttachmentTableSchema.pick({
|
|
|
|
|
$messageId: true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export type GetMessageAttachmentLocallyByMessageId = z.infer<typeof GetMessageAttachmentLocallyByMessageIdSchema>
|
|
|
|
|
|
|
|
|
|
|
2024-08-17 22:05:57 +01:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class GetMessageAttachmentLocallyUseCaseService {
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
private AttachmentRemoteDataSourceService: AttachmentRemoteDataSourceService,
|
|
|
|
|
private AttachmentLocalDataSource: AttachmentLocalDataSource
|
|
|
|
|
) { }
|
|
|
|
|
|
|
|
|
|
|
2024-08-29 12:13:15 +01:00
|
|
|
async execute(input: GetMessageAttachmentLocallyByMessageId): Promise<Result<string, any>> {
|
2024-08-17 22:05:57 +01:00
|
|
|
|
|
|
|
|
const getLocalAttachment = await this.AttachmentLocalDataSource.findOne({
|
|
|
|
|
$messageId: input.$messageId
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if(getLocalAttachment.isOk()) {
|
|
|
|
|
if(getLocalAttachment.value) {
|
2024-08-29 12:13:15 +01:00
|
|
|
const dataUrl = await createBlobUrl(getLocalAttachment.value.file)
|
|
|
|
|
|
|
|
|
|
return dataUrl
|
2024-08-17 22:05:57 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return err(getLocalAttachment.error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|