Files
doneit-web/src/app/services/chat/message.service.ts
T

1057 lines
24 KiB
TypeScript
Raw Normal View History

2022-01-10 23:52:33 +01:00
import { Injectable } from '@angular/core';
2022-01-13 10:52:03 +01:00
import { Message } from 'src/app/models/chatMethod';
2022-02-03 21:01:53 +01:00
import { SessionStore } from 'src/app/store/session.service';
2023-02-01 09:08:46 +01:00
import { capitalizeTxt } from 'src/plugin/text';
import { NfService } from 'src/app/services/chat/nf.service';
2022-09-30 15:13:36 +01:00
import { RochetChatConnectorService } from 'src/app/services/chat/rochet-chat-connector.service';
2022-02-10 15:50:11 +01:00
import { showDateDuration } from 'src/plugin/showDateDuration';
import { showTimeDuration } from 'src/plugin/showTimeDuration';
2023-02-01 09:08:46 +01:00
import { ChatMethodsService } from './chat-methods.service';
2023-06-29 16:04:44 +01:00
import { MessageModel, attachments } from '../../models/beast-orm';
2023-02-01 09:08:46 +01:00
import { AESEncrypt } from '../aesencrypt.service';
2022-05-27 13:36:37 +01:00
import { HttpEventType } from '@angular/common/http';
2022-03-22 16:11:30 +01:00
import { AttachmentsService } from 'src/app/services/attachments.service';
2022-03-29 16:48:24 +01:00
import { NetworkServiceService , ConnectionStatus} from 'src/app/services/network-service.service';
2022-12-17 12:34:56 +01:00
import { ChatSystemService } from './chat-system.service';
2023-01-09 10:49:58 +01:00
import { v4 as uuidv4 } from 'uuid'
2023-02-02 18:24:26 +01:00
import { NotificationsService } from 'src/app/services/notifications.service';
2023-01-09 10:49:58 +01:00
2022-01-10 23:52:33 +01:00
@Injectable({
providedIn: 'root'
})
export class MessageService {
2022-01-13 21:24:43 +01:00
customFields
2022-01-11 15:43:09 +01:00
channels = []
mentions = []
msg = ''
rid = ''
2022-01-11 16:07:54 +01:00
ts = {}
2022-03-27 15:18:54 +01:00
u = {
name: '',
username: '',
2022-02-16 15:52:59 +01:00
_id: ""
}
t = ''
2022-03-03 22:57:33 +01:00
_id = ''
2022-03-10 23:08:29 +01:00
id = '' // table id
2022-02-03 21:01:53 +01:00
_updatedAt
2022-01-13 10:52:03 +01:00
file
attachments
2022-02-04 00:22:35 +01:00
displayType = ''
2023-06-29 16:04:44 +01:00
temporaryData: any
2022-02-07 17:55:00 +01:00
hasFile = false
hasSendAttachment = false
2022-02-08 17:44:15 +01:00
sendAttempt = 0
uploadingFile = false
errorUploadingAttachment = false
2022-02-10 14:07:16 +01:00
loadHistory = false
2023-09-11 21:57:14 +01:00
from: 'Offline'|'History'|'stream'| 'send'
2022-02-10 15:50:11 +01:00
duration = ''
time= '';
2022-02-10 18:07:06 +01:00
localReference = null
2023-01-09 10:49:58 +01:00
viewed: string[] = []
received: string[]= []
2022-03-10 23:08:29 +01:00
addToDb = false
2022-01-11 16:07:54 +01:00
2022-03-03 22:57:33 +01:00
messageSend = false
2022-03-15 15:49:59 +01:00
delate = false
delateRequest = false
2022-08-04 16:13:11 +01:00
downloadLoader: boolean = false
2022-08-05 14:42:19 +01:00
downloadAttachments = false;
downloadAttachmentsTemp = 0;
2022-09-28 21:05:28 +01:00
UploadAttachmentsTemp = 0;
2022-12-26 15:41:11 +01:00
manualRetry = false
2023-01-24 15:56:47 +01:00
origin: 'history' | 'stream' | 'local'
2023-06-29 16:04:44 +01:00
messageModelInstance: MessageModel
attachmentsModelData: any
2023-09-11 21:57:14 +01:00
dateLabel = false
2022-03-03 22:57:33 +01:00
2022-09-30 15:27:07 +01:00
constructor(
2022-02-07 20:18:48 +01:00
private NfService: NfService,
2022-09-30 15:13:36 +01:00
private RochetChatConnectorService: RochetChatConnectorService,
2022-03-10 23:08:29 +01:00
private ChatMethodsService: ChatMethodsService,
2022-03-22 16:11:30 +01:00
private AESEncrypt: AESEncrypt,
2022-03-29 16:48:24 +01:00
private AttachmentsService: AttachmentsService,
2022-12-17 12:34:56 +01:00
private NetworkServiceService: NetworkServiceService,
2023-02-02 18:24:26 +01:00
private ChatSystemService: ChatSystemService,
private notificationService: NotificationsService) {
2022-02-03 21:01:53 +01:00
}
2022-01-11 16:07:54 +01:00
2023-08-20 00:26:24 +01:00
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, UploadAttachmentsTemp = 0 }:Message) {
2022-03-03 22:57:33 +01:00
2022-02-03 21:01:53 +01:00
this.channels = channels || []
this.mentions = mentions || []
2022-03-23 23:08:49 +01:00
this.msg = msg || ""
2022-01-11 16:07:54 +01:00
this.rid = rid
this.ts = ts
2022-04-19 16:03:59 +01:00
this.u = u || { name: this.usernameToDisplayName(SessionStore.user.UserName), username: SessionStore.user.UserName, _id: ""}
this.t = t
2022-07-22 17:15:32 +01:00
this._id = _id || ""
2022-02-03 21:01:53 +01:00
this._updatedAt = _updatedAt || new Date().getTime()
2022-01-13 10:52:03 +01:00
this.file = file
2022-02-07 20:18:48 +01:00
this.temporaryData = temporaryData
2022-02-10 18:07:06 +01:00
this.localReference = localReference || null
2022-03-10 23:08:29 +01:00
this.id = id
2022-03-15 15:49:59 +01:00
this.delate = delate
this.delateRequest = delateRequest
2022-12-26 17:04:20 +01:00
this.sendAttempt = 0
2023-01-24 15:56:47 +01:00
this.origin = origin
2023-06-29 16:04:44 +01:00
this.attachmentsModelData = attachmentsModelData
2023-08-20 00:26:24 +01:00
this.UploadAttachmentsTemp = UploadAttachmentsTemp
2022-01-28 15:28:21 +01:00
2022-03-25 17:39:51 +01:00
if(this.attachments?.length >= 1 && attachments?.length >= 1) {
this.attachments[0] = Object.assign(this.attachments[0], attachments[0])
} else {
this.attachments = attachments
}
2022-03-03 22:57:33 +01:00
this.viewed = [...new Set([...viewed,...this.viewed])];
this.received = [...new Set([...received,...this.received])];
2022-02-03 21:01:53 +01:00
if(!this.ts) {
2022-03-03 22:57:33 +01:00
this.messageSend = false
2022-02-03 21:01:53 +01:00
} else {
2022-03-03 22:57:33 +01:00
this.messageSend = true
2022-02-03 21:01:53 +01:00
}
2023-06-29 16:04:44 +01:00
this.hasFile = hasFile
if (this.instanceHasAttachment && !this.hasFile) {
this.hasFile = true
2022-01-28 15:28:21 +01:00
}
2022-02-02 20:16:12 +01:00
2022-02-07 17:55:00 +01:00
if(this.hasFile) {
if(this.file.type != 'application/webtrix') {
this.displayType = this.file.type.replace('application/','').toUpperCase()
}
}
2022-02-10 15:50:11 +01:00
2022-12-17 12:34:56 +01:00
if(!this.u.name && this.u.username) {
const user = this.ChatSystemService.users.find((u)=> u.username == this.u.username)
if(user) {
this.u.name = user.name
} else if( this.u.username == SessionStore.user.UserName) {
this.u.name = SessionStore.user.FullName
} else {
}
}
2022-02-10 15:50:11 +01:00
this.calDateDuration()
this.calTime()
2022-01-11 16:07:54 +01:00
}
2023-01-09 10:49:58 +01:00
get offline () {
if(!this._id) {
return true
}
if(!this.ts) {
return true
}
return false
}
/**
* Message is on the server
*/
get online() {
return !this.offline
}
/**
* if Message is already saved on database
*/
get save() {
return this.id != ''
}
2022-02-03 21:01:53 +01:00
private usernameToDisplayName(username) {
2022-12-19 15:59:52 +01:00
try {
const firstName = capitalizeTxt(username.split('.')[0])
const lastName = capitalizeTxt(username.split('.')[1])
return firstName + ' ' + lastName
} catch (error) {
return username
2023-09-11 21:57:14 +01:00
}
2022-12-19 15:59:52 +01:00
2022-02-09 18:18:27 +01:00
}
2022-02-03 21:01:53 +01:00
2022-02-10 14:56:06 +01:00
async send(): Promise<any> {
2023-01-03 22:04:33 +01:00
if(this.messageSend) {
return new Promise((resolve, reject) => {
2023-01-30 15:08:00 +01:00
resolve('solve')
2023-01-03 22:04:33 +01:00
})
}
2022-02-08 17:44:15 +01:00
this.sendAttempt++;
2022-12-26 17:04:20 +01:00
this.manualRetry = false
2022-02-08 17:44:15 +01:00
if(!this.hasFile) {
2022-03-03 22:57:33 +01:00
2023-09-11 21:57:14 +01:00
const params = {
roomId:this.rid,
msg:this.msg,
2022-05-27 13:36:37 +01:00
localReference: this.localReference
}
2022-03-27 15:18:54 +01:00
2022-03-14 14:16:23 +01:00
await this.sendRequest(params)
2022-02-10 14:07:16 +01:00
2022-02-07 20:18:48 +01:00
} else {
2023-01-03 22:04:33 +01:00
let uploadSuccessfully = await this.sendRequestAttachment()
2022-02-07 20:18:48 +01:00
2022-03-25 14:32:15 +01:00
if(uploadSuccessfully) {
2022-09-28 21:05:28 +01:00
2022-03-04 14:20:39 +01:00
const params = {roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file, localReference: this.localReference}
2022-03-14 14:16:23 +01:00
await this.sendRequest(params)
2022-02-10 14:56:06 +01:00
2022-09-30 15:13:36 +01:00
} else if(this.RochetChatConnectorService.isLogin == false) {
2022-09-28 21:05:28 +01:00
2022-09-30 15:13:36 +01:00
this.RochetChatConnectorService.registerCallback({
2022-02-08 17:44:15 +01:00
type: 'reConnect',
2023-01-09 10:49:58 +01:00
funx: async () => {
2023-09-29 16:40:50 +01:00
console.log('send now have login')
2022-12-20 17:06:19 +01:00
this.send().catch((error) =>{
console.error(error)
})
2022-03-04 14:20:39 +01:00
return true
2022-02-08 17:44:15 +01:00
}
2022-02-07 20:18:48 +01:00
})
2022-02-08 17:44:15 +01:00
2023-09-29 16:40:50 +01:00
console.log('no login')
} else if (this.UploadAttachmentsTemp <= 3) {
console.log('temp'+ this.UploadAttachmentsTemp)
setTimeout(async () => {
return await this.send()
}, 4000)
2023-01-03 22:04:33 +01:00
} else if(this.NetworkServiceService.getCurrentNetworkStatus() == ConnectionStatus.Offline) {
2023-09-29 16:40:50 +01:00
console.log('recooonect')
2023-01-03 22:04:33 +01:00
this.RochetChatConnectorService.registerCallback({
type: 'reConnect',
funx: async ()=> {
2023-09-11 21:57:14 +01:00
2023-01-03 22:04:33 +01:00
await this.send().catch((error) => {
console.error(error)
})
return true
}
2022-02-10 18:07:06 +01:00
})
2023-01-03 22:04:33 +01:00
} else if (this.NetworkServiceService.getCurrentNetworkStatus() == ConnectionStatus.Online) {
2023-09-29 16:40:50 +01:00
console.log('manual')
2023-01-03 22:04:33 +01:00
this.manualRetry = true
2022-02-07 20:18:48 +01:00
}
2022-02-09 18:18:27 +01:00
2022-02-07 20:18:48 +01:00
}
2022-02-18 15:08:13 +01:00
2022-02-07 20:18:48 +01:00
}
2022-12-26 15:41:11 +01:00
functionTimer = null;
2023-09-27 08:44:35 +01:00
functionTimer2 = null;
2022-12-26 15:41:11 +01:00
2023-01-03 22:04:33 +01:00
async sendRequestAttachment() {
2023-06-29 16:04:44 +01:00
2023-09-29 16:40:50 +01:00
console.log('sendRequestAttachment')
2023-01-03 22:04:33 +01:00
this.uploadingFile = true
let uploadSuccessfully = false
2023-06-29 16:04:44 +01:00
if(!this.instanceHasAttachmentBase64 && !this.temporaryData) {
try {
await this.getFileFromDB()
} catch (error) {}
2023-09-11 21:57:14 +01:00
}
2023-06-29 16:04:44 +01:00
if(!this.instanceHasTemporaryData) {
2023-09-11 21:57:14 +01:00
2023-06-29 16:04:44 +01:00
await this.generateTemporaryData()
}
2023-09-29 16:40:50 +01:00
console.log('send temp')
2023-01-03 22:04:33 +01:00
if(this.hasSendAttachment == false) {
try {
2023-09-29 16:40:50 +01:00
this.manualRetry = false
this.errorUploadingAttachment = false
2023-01-03 22:04:33 +01:00
this.UploadAttachmentsTemp++
2023-09-29 16:40:50 +01:00
uploadSuccessfully = await this.NfService.beforeSendAttachment(this)
2023-01-03 22:04:33 +01:00
this.uploadingFile = false
2023-09-29 16:40:50 +01:00
this.hasSendAttachment = uploadSuccessfully
2023-08-20 00:26:24 +01:00
2023-09-29 16:40:50 +01:00
if(this.hasSendAttachment) {
console.log('send uploadSuccessfully')
} else {
console.log('not send uploadSuccessfully')
}
2023-08-20 00:26:24 +01:00
this.saveChanges()
2023-01-03 22:04:33 +01:00
} catch (error) {
this.uploadingFile = false
2023-09-11 21:57:14 +01:00
2023-09-29 16:40:50 +01:00
console.log('send not uploadSuccessfully')
2023-01-03 22:04:33 +01:00
this.errorUploadingAttachment = true
2023-08-20 00:26:24 +01:00
this.saveChanges()
2023-01-03 22:04:33 +01:00
}
2023-09-11 21:57:14 +01:00
2023-01-03 22:04:33 +01:00
}
return uploadSuccessfully
}
2023-09-27 08:44:35 +01:00
sendToRocketChatAttemp = 0
2022-03-14 14:16:23 +01:00
async sendRequest(params) {
2023-09-27 08:44:35 +01:00
this.sendToRocketChatAttemp++;
2022-04-13 21:54:31 +01:00
if(params?.attachments) {
if(params?.attachments[0]?.image_url) {
delete params?.attachments[0]?.image_url
delete params?.file?.image_url
// delete
2022-04-13 21:54:31 +01:00
}
2022-03-22 16:11:30 +01:00
}
2022-03-29 16:48:24 +01:00
if(this.NetworkServiceService.getCurrentNetworkStatus() == ConnectionStatus.Online) {
2022-12-26 17:04:20 +01:00
if(this.msg == '<script></script>') {
if(this.sendAttempt >= 4) {
this.RochetChatConnectorService.send(params).then(
(ChatMessage: any) => {
ChatMessage = ChatMessage.message.result
clearTimeout(this.functionTimer);
2023-09-11 21:57:14 +01:00
2022-12-26 17:04:20 +01:00
this.redefinedMessage(ChatMessage)
}
).catch((error) => {
clearTimeout(this.functionTimer);
console.error(error)
})
2022-03-29 16:48:24 +01:00
}
2022-12-26 17:04:20 +01:00
} else {
2023-09-27 08:44:35 +01:00
// console.log('online')
2023-01-09 10:49:58 +01:00
2022-12-26 17:04:20 +01:00
this.RochetChatConnectorService.send(params).then(
(ChatMessage: any) => {
ChatMessage = ChatMessage.message.result
clearTimeout(this.functionTimer);
2023-09-11 21:57:14 +01:00
2022-12-26 17:04:20 +01:00
this.redefinedMessage(ChatMessage)
2023-09-11 21:57:14 +01:00
2022-12-26 17:04:20 +01:00
}
).catch((error) => {
clearTimeout(this.functionTimer);
console.error(error)
})
}
2023-09-11 21:57:14 +01:00
2022-12-26 15:41:11 +01:00
this.functionTimer = setTimeout(() => {
2023-09-27 08:44:35 +01:00
//console.log('functionTimer')
2022-12-26 15:41:11 +01:00
this.RochetChatConnectorService.registerCallback({
type:'Onmessage',
key:'ping-pong-message',
funx:(message: any) => {
2023-09-27 08:44:35 +01:00
// clearTimeout(this.functionTimer2);
2022-12-26 15:41:11 +01:00
if(message.msg == "ping") {
2023-09-27 08:44:35 +01:00
this.sendRequest(params)
2022-12-26 15:41:11 +01:00
return true
}
2023-01-09 10:49:58 +01:00
this.saveChanges()
2022-12-26 15:41:11 +01:00
}
})
2023-09-27 08:44:35 +01:00
// this.functionTimer2 = setTimeout(() => {
// console.log('set to callback')
// this.RochetChatConnectorService.registerCallback({
// type: 'reConnect',
// funx: async ()=> {
// this.sendRequest(params)
// return true
// }
// })
// }, 4000)
2022-12-26 15:41:11 +01:00
this.RochetChatConnectorService.wsSend({message:{msg:"pong"}, loginRequired: false})
2023-09-27 08:44:35 +01:00
}, 5000)
2022-03-29 16:48:24 +01:00
} else {
2023-09-27 08:44:35 +01:00
// console.log('offline')
2022-09-30 15:13:36 +01:00
this.RochetChatConnectorService.registerCallback({
2022-03-29 16:48:24 +01:00
type: 'reConnect',
funx: async ()=> {
2022-12-20 17:06:19 +01:00
this.send().catch((error) =>{
console.error(error)
})
2022-03-29 16:48:24 +01:00
return true
}
})
}
2022-03-14 14:16:23 +01:00
}
2022-03-03 22:57:33 +01:00
async redefinedMessage(ChatMessage , update = true) {
2022-02-10 18:07:06 +01:00
ChatMessage = this.NfService.fix_updatedAt(ChatMessage)
2022-03-27 15:18:54 +01:00
2022-03-03 22:57:33 +01:00
const message = this.getChatObj()
2022-12-26 17:04:20 +01:00
this.manualRetry = false
2023-01-09 10:49:58 +01:00
this.messageSend = true
2022-12-26 17:04:20 +01:00
2022-03-03 22:57:33 +01:00
ChatMessage = Object.assign(message, ChatMessage)
2022-02-07 20:18:48 +01:00
this.setData(ChatMessage)
2022-03-10 23:08:29 +01:00
2023-01-09 10:49:58 +01:00
const roomObject = this.ChatSystemService.getRoomById(this.rid)
const users = roomObject.getUsersByStatus('online')
for(const user of users) {
this.addReceived(user._id)
}
2023-02-02 18:24:26 +01:00
if(!roomObject.isGroup) {
var memeberTosend = this.ChatSystemService.getRoomById(this.rid).membersExcludeMe
2023-09-29 16:40:50 +01:00
this.notificationService.ChatSendMessageNotification(memeberTosend[0].username,ChatMessage.u.name,ChatMessage.msg || "Anexo",this.rid)
2023-02-06 15:34:46 +01:00
} else {
var memeberTosend = this.ChatSystemService.getRoomById(this.rid).membersExcludeMe
var usersNames = [];
for(let i = 0; i < memeberTosend.length; i++) {
usersNames.push(memeberTosend[i].username)
}
2023-09-29 16:40:50 +01:00
this.notificationService.ChatSendMessageNotificationGrup(usersNames,ChatMessage.u.name,ChatMessage.msg || "Anexo",this.rid)
2023-02-02 18:24:26 +01:00
}
2023-09-11 21:57:14 +01:00
2023-01-09 10:49:58 +01:00
await this.saveChanges()
2022-02-07 15:51:21 +01:00
}
2023-02-02 18:24:26 +01:00
sendNotificaton
2022-03-22 16:11:30 +01:00
downloadFileMsg() {
2022-02-08 17:44:15 +01:00
this.downloadLoader = true;
2022-03-22 16:11:30 +01:00
let downloadFile = "";
2024-03-18 11:13:34 +01:00
let downloadUrl = "";
2022-03-22 16:11:30 +01:00
this.AttachmentsService.downloadFile(this.file.guid).subscribe(async (event) => {
2024-03-11 13:21:44 +01:00
console.log('download file',event)
2022-03-22 16:11:30 +01:00
if (event.type === HttpEventType.DownloadProgress) {
} else if (event.type === HttpEventType.Response) {
if (this.file.type == "application/img") {
downloadFile = 'data:image/jpeg;base64,' + btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), ''));
} else if (this.file.type != "application/img") {
2024-03-18 11:13:34 +01:00
downloadFile = btoa(new Uint8Array(event.body).reduce((data, byte) => data + String.fromCharCode(byte), ''));
downloadUrl = event.url
2024-03-11 13:21:44 +01:00
console.log('downloaded file', downloadFile)
2024-03-14 09:10:17 +01:00
console.log('event body downloaded file', event.url)
2023-09-11 21:57:14 +01:00
2022-05-27 13:36:37 +01:00
}
2022-03-22 16:11:30 +01:00
this.attachments[0] = {
image_url: downloadFile,
name: this.attachments[0].name,
title: this.attachments[0].title,
title_link: downloadFile,
title_link_download: this.attachments[0].title_link_download,
2024-03-18 11:13:34 +01:00
ts: this.attachments[0].ts,
attachmentsUrl: downloadUrl
2022-03-22 16:11:30 +01:00
}
2022-03-27 15:18:54 +01:00
2022-03-22 16:11:30 +01:00
// save the changes to the storage
2023-01-09 10:49:58 +01:00
this.saveChanges()
2023-06-29 16:04:44 +01:00
this.addFileToDb()
this.downloadLoader = false;
2022-08-05 14:42:19 +01:00
this.downloadAttachments = true
this.downloadAttachmentsTemp++;
2022-03-22 16:11:30 +01:00
}
2022-08-05 14:42:19 +01:00
}, ()=>{
// error
this.downloadLoader = false;
2022-08-05 15:36:09 +01:00
this.downloadAttachments = false
2022-08-05 14:42:19 +01:00
this.downloadAttachmentsTemp++;
2022-03-22 16:11:30 +01:00
});
2022-02-08 17:44:15 +01:00
}
2022-02-10 15:50:11 +01:00
private calDateDuration(date = null) {
this.duration = showDateDuration(date || this._updatedAt);
}
private calTime(time = null) {
this.time = showTimeDuration(time || this._updatedAt);
}
2022-03-10 23:08:29 +01:00
2022-03-18 12:32:21 +01:00
async delateStatusFalse() {
2022-03-15 15:49:59 +01:00
this.delate = true
2023-01-09 10:49:58 +01:00
this.saveChanges()
}
addViewed(id: string) {
if(this.messageOwner()) {
let found = this.viewed.find((UserId) => UserId == id)
if(!found) {
this.viewed.push(id)
return true
}
}
2023-09-11 21:57:14 +01:00
2023-01-09 10:49:58 +01:00
return false
}
addReceived(id: string) {
if(this.messageOwner()) {
let found = this.received.find((UserId) => UserId == id)
if(!found) {
this.received.push(id)
return true
}
}
return false
2022-03-03 22:57:33 +01:00
}
2022-02-16 15:52:59 +01:00
2022-03-18 12:32:21 +01:00
async delateDB() {
2022-03-27 15:18:54 +01:00
2023-06-29 16:04:44 +01:00
if(!this.messageModelInstance) {
this.messageModelInstance = await this.getRowInstance()
2023-01-24 15:56:47 +01:00
}
2022-03-18 12:32:21 +01:00
2023-06-29 16:04:44 +01:00
await this.messageModelInstance.delete()
2022-03-18 12:32:21 +01:00
}
2022-03-15 15:49:59 +01:00
2022-03-03 22:57:33 +01:00
isSenderIsNotMe(ChatMessage) {
2023-01-09 10:49:58 +01:00
return SessionStore.user.ChatData.data.userId != ChatMessage.u._id
2022-03-03 22:57:33 +01:00
}
2023-01-09 10:49:58 +01:00
messageOwner() {
return SessionStore.user.ChatData.data.userId == this.u._id
2022-03-03 22:57:33 +01:00
}
2023-02-08 16:06:14 +01:00
getChatObj() {
2022-03-03 22:57:33 +01:00
return {
channels: this.channels,
mentions: this.mentions,
2023-01-09 10:49:58 +01:00
// msg: this.AESEncrypt.encrypt(this.msg, SessionStore.user.UserName),
2022-12-26 15:41:11 +01:00
sendAttempt: this.sendAttempt,
2022-03-27 15:18:54 +01:00
msg:this.msg,
2022-03-03 22:57:33 +01:00
rid: this.rid,
ts: this.ts,
u: this.u,
_id: this._id,
2023-08-08 09:43:26 +01:00
t: this.t,
2022-03-10 23:08:29 +01:00
id: this.id,
2023-06-29 16:04:44 +01:00
hasFile: this.hasFile,
2023-01-24 15:56:47 +01:00
origin: this.origin,
2022-03-03 22:57:33 +01:00
_updatedAt: this._updatedAt,
messageSend: this.messageSend,
offline: this.offline,
viewed: this.viewed,
received: this.received,
2022-03-10 23:08:29 +01:00
localReference: this.localReference,
attachments: this.attachments,
2022-03-15 15:49:59 +01:00
file: this.file,
2023-08-20 00:26:24 +01:00
delate: this.delate,
UploadAttachmentsTemp: this.UploadAttachmentsTemp
2022-02-16 15:52:59 +01:00
}
2022-03-03 22:57:33 +01:00
}
2023-01-09 10:49:58 +01:00
earlySave = false
2023-09-11 21:57:14 +01:00
2022-03-03 22:57:33 +01:00
async addMessageDB() {
2023-01-09 10:49:58 +01:00
if(!this.addToDb && this.save) {
2022-07-25 14:48:28 +01:00
this.addToDb = true
2022-03-10 23:08:29 +01:00
const message = this.getChatObj()
2022-03-03 22:57:33 +01:00
2023-06-29 16:04:44 +01:00
if(this.instanceHasAttachment) {
2023-09-11 21:57:14 +01:00
this.hasFile = true
2023-06-29 16:04:44 +01:00
}
2022-03-10 23:08:29 +01:00
delete message.id
const createdMessage = await MessageModel.create(message)
2022-02-16 15:52:59 +01:00
2023-06-29 16:04:44 +01:00
if(this.instanceHasAttachment) {
2023-09-11 21:57:14 +01:00
this.addFileToDb()
2023-06-29 16:04:44 +01:00
}
this.messageModelInstance = createdMessage
2022-03-10 23:08:29 +01:00
this.id = createdMessage.id
2023-01-09 10:49:58 +01:00
if(this.earlySave) {
this.saveChanges();
}
2022-03-10 23:08:29 +01:00
}
2022-02-16 15:52:59 +01:00
}
2023-06-29 16:04:44 +01:00
async addFileToDb() {
if(!this.messageModelInstance) {
this.messageModelInstance = await this.getRowInstance()
}
2023-09-11 21:57:14 +01:00
2023-06-29 16:04:44 +01:00
const createdMessage: any = this.messageModelInstance
try {
let file = {}
if(this.attachmentsModelData) {
file = {image_url: this.attachmentsModelData?.fileBase64}
}
2023-09-11 21:57:14 +01:00
2023-06-29 16:04:44 +01:00
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()
}
2023-09-11 21:57:14 +01:00
2023-06-29 16:04:44 +01:00
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)
2023-09-11 21:57:14 +01:00
2023-06-29 16:04:44 +01:00
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)
})
}
2023-01-09 10:49:58 +01:00
2023-01-24 15:56:47 +01:00
async getRowInstance () {
2022-03-03 22:57:33 +01:00
2023-01-09 10:49:58 +01:00
if(this.save) {
const message = this.getChatObj()
let a
2023-01-12 15:27:09 +01:00
if(!message.id) {
delete message.id
}
2023-09-11 21:57:14 +01:00
2023-01-09 10:49:58 +01:00
if (this.localReference) {
a = await MessageModel.get({localReference: this.localReference})
} else if (this._id) {
a = await MessageModel.get({_id: this._id})
2023-01-12 15:27:09 +01:00
} else if(this.id) {
a = await MessageModel.get({id: this.id})
2023-01-09 10:49:58 +01:00
}
2022-02-16 15:52:59 +01:00
2023-01-24 15:56:47 +01:00
return a
}
2023-09-11 21:57:14 +01:00
2023-01-24 15:56:47 +01:00
}
async saveChanges() {
2023-06-29 16:04:44 +01:00
if(!this.messageModelInstance) {
this.messageModelInstance = await this.getRowInstance()
2023-01-24 15:56:47 +01:00
}
2023-06-29 16:04:44 +01:00
if(this.save && this.messageModelInstance) {
2023-01-24 15:56:47 +01:00
const message = this.getChatObj()
if(!message.id) {
delete message.id
}
for( const [name, value] of Object.entries(message)) {
2023-02-27 09:31:10 +01:00
try {
2023-06-29 16:04:44 +01:00
this.messageModelInstance[name] = value
2023-02-27 09:31:10 +01:00
} catch (error) {}
2023-01-09 10:49:58 +01:00
}
2023-09-11 21:57:14 +01:00
2023-06-29 16:04:44 +01:00
await this.messageModelInstance.save()
2023-09-11 21:57:14 +01:00
2023-01-09 10:49:58 +01:00
} else {
this.earlySave = true
}
2023-09-11 21:57:14 +01:00
}
decryptMessage() {
try {
// this.msg = this.AESEncrypt.decrypt(this.msg, SessionStore.user.UserName)
} catch (error) {}
}
UIdata() {
this.incomingFromCurrentUser = this.u.username!= SessionStore.user.UserName
this.hasMessage = this.msg !=''
}
incomingFromCurrentUser = false
hasMessage = false
}
export class MessageServiceDateLabel {
customFields
channels = []
mentions = []
msg = ''
rid = ''
ts = {}
u = {
name: '',
username: '',
_id: ""
}
t = ''
_id = ''
id = '' // table id
_updatedAt
file
attachments
displayType = ''
temporaryData: any
hasFile = false
hasSendAttachment = false
sendAttempt = 0
uploadingFile = false
errorUploadingAttachment = false
loadHistory = false
from: 'Offline'|'History'|'stream'| 'send'
duration = ''
time = ''
2023-09-11 21:57:14 +01:00
localReference = null
viewed: string[] = []
received: string[]= []
addToDb = false
messageSend = false
delate = false
delateRequest = false
downloadLoader: boolean = false
downloadAttachments = false;
downloadAttachmentsTemp = 0;
UploadAttachmentsTemp = 0;
manualRetry = false
origin: 'history' | 'stream' | 'local'
messageModelInstance: MessageModel
attachmentsModelData: any
ChatSystemService
dateLabel = false
constructor() {}
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, UploadAttachmentsTemp = 0 }:Message) {
this.channels = []
this.mentions = []
this.rid = rid
this.ts = ts
this.u = u || { name: this.usernameToDisplayName(SessionStore.user.UserName), username: SessionStore.user.UserName, _id: ""}
this.t = t
this._id = ""
this._updatedAt = _updatedAt || new Date().getTime()
this.file = file
this.temporaryData = temporaryData
this.localReference = localReference || null
this.id = id
this.delate = delate
this.delateRequest = delateRequest
this.sendAttempt = 0
this.origin = origin
this.attachmentsModelData = attachmentsModelData
this.UploadAttachmentsTemp = UploadAttachmentsTemp
if(this.attachments?.length >= 1 && attachments?.length >= 1) {
this.attachments[0] = Object.assign(this.attachments[0], attachments[0])
} else {
this.attachments = attachments
}
this.viewed = [];
this.received = [];
this.messageSend = true
this.hasFile = hasFile
this.hasFile = false
if(!this.u.name && this.u.username) {
const user = this.ChatSystemService.users.find((u)=> u.username == this.u.username)
if(user) {
this.u.name = user.name
} else if( this.u.username == SessionStore.user.UserName) {
this.u.name = SessionStore.user.FullName
} else {
}
}
this.calDateDuration()
this.calTime();
2023-09-11 21:57:14 +01:00
}
get offline () {
if(!this._id) {
return true
}
if(!this.ts) {
return true
}
return false
}
/**
* Message is on the server
*/
get online() {
return !this.offline
}
/**
* if Message is already saved on database
*/
get save() {
return this.id != ''
}
private usernameToDisplayName(username) {
try {
const firstName = capitalizeTxt(username.split('.')[0])
const lastName = capitalizeTxt(username.split('.')[1])
return firstName + ' ' + lastName
} catch (error) {
return username
}
}
async send(): Promise<any> {
}
functionTimer = null;
async sendRequestAttachment() {
}
async sendRequest(params) {
}
async redefinedMessage(ChatMessage , update = true) {
}
sendNotificaton
downloadFileMsg() {
}
private calDateDuration(date = null) {
this.duration = showDateDuration(date || this._updatedAt);
}
private calTime(time = null) {
this.time = showTimeDuration(time || this._updatedAt);
}
2023-09-11 21:57:14 +01:00
async delateStatusFalse() {
}
addViewed(id: string) {
}
addReceived(id: string) {
}
async delateDB() {
}
isSenderIsNotMe(ChatMessage) {
return SessionStore.user.ChatData.data.userId != ChatMessage.u._id
}
messageOwner() {
return SessionStore.user.ChatData.data.userId == this.u._id
}
getChatObj() {
}
earlySave = false
async addMessageDB() {
}
async addFileToDb() {
}
async getFileFromDB() {
}
async generateTemporaryData () {
}
get instanceHasAttachment() {
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) {
}
async getRowInstance () {
}
async saveChanges() {
2022-02-16 15:52:59 +01:00
}
2022-02-10 18:07:06 +01:00
2022-03-10 23:08:29 +01:00
decryptMessage() {
try {
2022-04-19 16:03:59 +01:00
// this.msg = this.AESEncrypt.decrypt(this.msg, SessionStore.user.UserName)
2022-03-10 23:08:29 +01:00
} catch (error) {}
}
2023-08-24 22:19:40 +01:00
UIdata() {
this.incomingFromCurrentUser = this.u.username!= SessionStore.user.UserName
this.hasMessage = this.msg !=''
}
incomingFromCurrentUser = false
hasMessage = false
2022-01-10 23:52:33 +01:00
}