fixe chat message

This commit is contained in:
Peter Maquiran
2023-06-29 16:04:44 +01:00
parent 8c02c5abb8
commit 246ea2e4a6
16 changed files with 356 additions and 227 deletions
+135 -22
View File
@@ -6,7 +6,7 @@ import { NfService } from 'src/app/services/chat/nf.service';
import { RochetChatConnectorService } from 'src/app/services/chat/rochet-chat-connector.service';
import { showDateDuration } from 'src/plugin/showDateDuration';
import { ChatMethodsService } from './chat-methods.service';
import { MessageModel } from '../../models/beast-orm';
import { MessageModel, attachments } from '../../models/beast-orm';
import { AESEncrypt } from '../aesencrypt.service';
import { HttpEventType } from '@angular/common/http';
import { AttachmentsService } from 'src/app/services/attachments.service';
@@ -40,7 +40,7 @@ export class MessageService {
file
attachments
displayType = ''
temporaryData: any = {}
temporaryData: any
hasFile = false
hasSendAttachment = false
sendAttempt = 0
@@ -64,7 +64,9 @@ export class MessageService {
manualRetry = false
origin: 'history' | 'stream' | 'local'
rowInstance: MessageModel
messageModelInstance: MessageModel
attachmentsModelData: any
constructor(
private NfService: NfService,
@@ -77,7 +79,7 @@ export class MessageService {
private notificationService: NotificationsService) {
}
setData({customFields = {}, channels, mentions, msg ,rid ,ts, u, t, _id, id, _updatedAt, file, attachments, temporaryData, localReference = 'out-'+uuidv4() , viewed = [], received = [], delate = false, delateRequest =false, from, sendAttempt = 0, origin }:Message) {
setData({customFields = {}, channels, mentions, msg ,rid ,ts, u, t, _id, id, _updatedAt, file, attachments, temporaryData, localReference = 'out-'+uuidv4() , viewed = [], received = [], delate = false, delateRequest =false, from, sendAttempt = 0, origin, attachmentsModelData, hasFile = false }:Message) {
this.channels = channels || []
this.mentions = mentions || []
@@ -96,6 +98,7 @@ export class MessageService {
this.delateRequest = delateRequest
this.sendAttempt = 0
this.origin = origin
this.attachmentsModelData = attachmentsModelData
if(this.attachments?.length >= 1 && attachments?.length >= 1) {
this.attachments[0] = Object.assign(this.attachments[0], attachments[0])
@@ -112,12 +115,10 @@ export class MessageService {
this.messageSend = true
}
if (this.file) {
if(this.file.type) {
if(typeof(this.file.type) == 'string') {
this.hasFile = true
}
}
this.hasFile = hasFile
if (this.instanceHasAttachment && !this.hasFile) {
this.hasFile = true
}
if(this.hasFile) {
@@ -201,6 +202,7 @@ export class MessageService {
} else {
console.log('send', this)
let uploadSuccessfully = await this.sendRequestAttachment()
if(uploadSuccessfully) {
@@ -247,24 +249,35 @@ export class MessageService {
functionTimer = null;
async sendRequestAttachment() {
this.uploadingFile = true
let uploadSuccessfully = false
if(!this.instanceHasAttachmentBase64 && !this.temporaryData) {
try {
await this.getFileFromDB()
} catch (error) {}
}
if(!this.instanceHasTemporaryData) {
await this.generateTemporaryData()
}
if(this.hasSendAttachment == false) {
try {
uploadSuccessfully = await this.NfService.beforeSendAttachment(this)
this.UploadAttachmentsTemp++
this.uploadingFile = false
this.manualRetry = false
this.errorUploadingAttachment = false
this.hasSendAttachment = true
this.temporaryData = {}
} catch (error) {
this.uploadingFile = false
this.errorUploadingAttachment = true
this.UploadAttachmentsTemp++
console.error('beforeSendAttachment error:', error)
}
}
@@ -411,6 +424,7 @@ export class MessageService {
// save the changes to the storage
this.saveChanges()
this.addFileToDb()
this.downloadLoader = false;
this.downloadAttachments = true
this.downloadAttachmentsTemp++;
@@ -461,11 +475,11 @@ export class MessageService {
async delateDB() {
if(!this.rowInstance) {
this.rowInstance = await this.getRowInstance()
if(!this.messageModelInstance) {
this.messageModelInstance = await this.getRowInstance()
}
await this.rowInstance.delete()
await this.messageModelInstance.delete()
}
@@ -489,6 +503,7 @@ export class MessageService {
u: this.u,
_id: this._id,
id: this.id,
hasFile: this.hasFile,
origin: this.origin,
_updatedAt: this._updatedAt,
messageSend: this.messageSend,
@@ -509,10 +524,18 @@ export class MessageService {
this.addToDb = true
const message = this.getChatObj()
if(this.instanceHasAttachment) {
this.hasFile = true
}
delete message.id
const createdMessage = await MessageModel.create(message)
this.rowInstance = createdMessage
if(this.instanceHasAttachment) {
this.addFileToDb()
}
this.messageModelInstance = createdMessage
this.id = createdMessage.id
if(this.earlySave) {
@@ -521,6 +544,96 @@ export class MessageService {
}
}
async addFileToDb() {
if(!this.messageModelInstance) {
this.messageModelInstance = await this.getRowInstance()
}
const createdMessage: any = this.messageModelInstance
try {
let file = {}
if(this.attachmentsModelData) {
file = {image_url: this.attachmentsModelData?.fileBase64}
}
await attachments.create({messageId: createdMessage.id, attachments: this.attachments, file: Object.assign(this.file, file) })
} catch (error) {
console.log(error)
}
}
async getFileFromDB() {
if(!this.messageModelInstance) {
this.messageModelInstance = await this.getRowInstance()
}
const data = await this.messageModelInstance.getAttachments()
console.log('data', data);
this.attachments = data.attachments
this.file = data.file
}
async generateTemporaryData () {
const blob: any = await this.base64StringToBlob(this.file.image_url)
const formData = new FormData();
formData.append("blobFile", blob);
this.temporaryData = formData
}
get instanceHasAttachment() {
if (this.file) {
if(this.file.type) {
if(typeof(this.file.type) == 'string') {
return true
}
}
}
return false
}
get instanceHasAttachmentBase64() {
if (this.file) {
if(this.file.type) {
if(this.file?.image_url) {
return true
}
}
}
return false
}
get instanceHasTemporaryData() {
if (!this.temporaryData) {
return false
}
return true
}
base64StringToBlob(base64Data) {
return new Promise((resolve, reject) => {
fetch(base64Data)
.then(res => resolve(res.blob()))
.then(console.log)
})
}
async getRowInstance () {
@@ -547,11 +660,11 @@ export class MessageService {
async saveChanges() {
if(!this.rowInstance) {
this.rowInstance = await this.getRowInstance()
if(!this.messageModelInstance) {
this.messageModelInstance = await this.getRowInstance()
}
if(this.save && this.rowInstance) {
if(this.save && this.messageModelInstance) {
const message = this.getChatObj()
if(!message.id) {
@@ -560,11 +673,11 @@ export class MessageService {
for( const [name, value] of Object.entries(message)) {
try {
this.rowInstance[name] = value
this.messageModelInstance[name] = value
} catch (error) {}
}
await this.rowInstance.save()
await this.messageModelInstance.save()
} else {
this.earlySave = true