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-01-28 15:28:21 +01:00
|
|
|
import { Storage } from '@ionic/storage';
|
2022-02-03 21:01:53 +01:00
|
|
|
import { SessionStore } from 'src/app/store/session.service';
|
|
|
|
|
import { capitalizeTxt } from 'src/plugin/text'
|
2022-02-07 17:55:00 +01:00
|
|
|
import { NfService } from 'src/app/services/chat/nf.service'
|
2022-02-07 20:18:48 +01:00
|
|
|
import { WsChatService } from 'src/app/services/chat/ws-chat.service';
|
2022-02-10 15:50:11 +01:00
|
|
|
import { showDateDuration } from 'src/plugin/showDateDuration';
|
2022-03-03 22:57:33 +01:00
|
|
|
import { ChatStorageService } from './chat-storage.service'
|
2022-03-04 14:20:39 +01:00
|
|
|
import { ChatMethodsService } from './chat-methods.service'
|
2022-03-10 23:08:29 +01:00
|
|
|
import { MessageModel, DeleteMessageModel } from '../../models/beast-orm'
|
|
|
|
|
import { AESEncrypt } from '../aesencrypt.service'
|
2022-03-03 22:57:33 +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-02-16 15:52:59 +01:00
|
|
|
|
|
|
|
|
u = {
|
|
|
|
|
name: '',
|
|
|
|
|
username: '',
|
|
|
|
|
_id: ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 11:27:25 +01:00
|
|
|
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-03 21:01:53 +01:00
|
|
|
offline = true
|
2022-02-04 00:22:35 +01:00
|
|
|
displayType = ''
|
2022-02-07 17:55:00 +01:00
|
|
|
temporaryData: any = {}
|
|
|
|
|
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
|
2022-02-10 15:50:11 +01:00
|
|
|
duration = ''
|
2022-02-10 18:07:06 +01:00
|
|
|
localReference = null
|
2022-02-16 15:52:59 +01:00
|
|
|
viewed = []
|
|
|
|
|
received = []
|
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-03-03 22:57:33 +01:00
|
|
|
|
2022-02-07 17:55:00 +01:00
|
|
|
constructor(private storage: Storage,
|
2022-02-07 20:18:48 +01:00
|
|
|
private NfService: NfService,
|
2022-03-03 22:57:33 +01:00
|
|
|
private WsChatService: WsChatService,
|
2022-03-04 14:20:39 +01:00
|
|
|
private ChatStorageService: ChatStorageService,
|
2022-03-10 23:08:29 +01:00
|
|
|
private ChatMethodsService: ChatMethodsService,
|
|
|
|
|
private AESEncrypt: AESEncrypt) {
|
2022-02-03 21:01:53 +01:00
|
|
|
}
|
2022-01-11 16:07:54 +01:00
|
|
|
|
2022-03-15 15:49:59 +01:00
|
|
|
setData({customFields = {}, channels, mentions, msg ,rid ,ts, u, t, _id, id, _updatedAt, file, attachments, temporaryData, localReference , viewed = [], received = [], delate = false, delateRequest =false, }:Message) {
|
2022-03-03 22:57:33 +01:00
|
|
|
|
2022-02-03 21:01:53 +01:00
|
|
|
this.channels = channels || []
|
|
|
|
|
this.mentions = mentions || []
|
|
|
|
|
this.msg = msg || ""
|
2022-01-11 16:07:54 +01:00
|
|
|
this.rid = rid
|
|
|
|
|
this.ts = ts
|
2022-02-03 21:01:53 +01:00
|
|
|
this.u = u || { name: this.usernameToDisplayName(SessionStore.user.RochetChatUser), username: SessionStore.user.RochetChatUser, _id: ""}
|
2022-01-17 11:27:25 +01:00
|
|
|
this.t = t
|
2022-01-11 16:07:54 +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
|
|
|
|
|
this.attachments = attachments
|
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-01-28 15:28:21 +01:00
|
|
|
|
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) {
|
|
|
|
|
this.offline = true
|
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
|
|
|
this.offline = false
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-28 15:28:21 +01:00
|
|
|
if (this.file) {
|
2022-02-04 00:22:35 +01:00
|
|
|
if(this.file.type) {
|
2022-02-07 17:55:00 +01:00
|
|
|
if(typeof(this.file.type) == 'string') {
|
|
|
|
|
this.hasFile = true
|
2022-02-04 00:22:35 +01:00
|
|
|
}
|
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) {
|
2022-03-15 16:28:31 +01:00
|
|
|
// this.getFileFromDb()
|
2022-02-07 17:55:00 +01:00
|
|
|
if(this.file.type != 'application/webtrix') {
|
|
|
|
|
this.displayType = this.file.type.replace('application/','').toUpperCase()
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-10 15:50:11 +01:00
|
|
|
|
|
|
|
|
this.calDateDuration()
|
2022-01-11 16:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-03 21:01:53 +01:00
|
|
|
private usernameToDisplayName(username) {
|
|
|
|
|
|
|
|
|
|
const firstName = capitalizeTxt(username.split('.')[0])
|
|
|
|
|
const lastName = capitalizeTxt(username.split('.')[1])
|
|
|
|
|
return firstName + ' ' + lastName
|
2022-02-09 18:18:27 +01:00
|
|
|
}
|
2022-02-03 21:01:53 +01:00
|
|
|
|
2022-03-15 16:28:31 +01:00
|
|
|
// getFileFromDb() {
|
|
|
|
|
|
|
|
|
|
// if(this.hasFile) {
|
|
|
|
|
// if (this.file.guid) {
|
|
|
|
|
// this.storage.get(this.file.guid).then((image) => {
|
|
|
|
|
// if(image != null) {
|
|
|
|
|
// this.file.image_url = image
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2022-02-04 00:22:35 +01:00
|
|
|
|
2022-02-10 14:56:06 +01:00
|
|
|
async send(): Promise<any> {
|
2022-02-08 17:44:15 +01:00
|
|
|
|
|
|
|
|
this.sendAttempt++;
|
|
|
|
|
|
|
|
|
|
if(!this.hasFile) {
|
2022-03-03 22:57:33 +01:00
|
|
|
|
2022-03-04 14:20:39 +01:00
|
|
|
const params = {roomId:this.rid, msg:this.msg, localReference: this.localReference}
|
|
|
|
|
|
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 {
|
|
|
|
|
|
2022-02-08 17:44:15 +01:00
|
|
|
this.uploadingFile = true
|
|
|
|
|
|
|
|
|
|
let uploadSuccessfully = false
|
|
|
|
|
if(this.hasSendAttachment == false) {
|
|
|
|
|
uploadSuccessfully = await this.NfService.beforeSendAttachment(this)
|
|
|
|
|
}
|
2022-02-09 18:18:27 +01:00
|
|
|
|
2022-02-08 17:44:15 +01:00
|
|
|
this.uploadingFile = false
|
2022-02-07 20:18:48 +01:00
|
|
|
|
2022-02-08 17:44:15 +01:00
|
|
|
if(uploadSuccessfully || this.hasSendAttachment == false) {
|
2022-02-07 20:18:48 +01:00
|
|
|
this.hasSendAttachment = true
|
2022-02-08 17:44:15 +01:00
|
|
|
this.errorUploadingAttachment = false
|
|
|
|
|
this.temporaryData = {}
|
2022-02-09 18:18:27 +01:00
|
|
|
|
2022-02-10 21:52:02 +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-03-03 22:57:33 +01:00
|
|
|
|
2022-02-08 17:44:15 +01:00
|
|
|
} else if(this.WsChatService.isLogin == false) {
|
2022-02-08 19:17:44 +01:00
|
|
|
|
2022-02-08 17:44:15 +01:00
|
|
|
this.WsChatService.registerCallback({
|
|
|
|
|
type: 'reConnect',
|
2022-02-10 14:56:06 +01:00
|
|
|
funx: async ()=> {
|
2022-03-04 14:20:39 +01:00
|
|
|
this.send()
|
|
|
|
|
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
|
|
|
|
|
|
|
|
} else if(uploadSuccessfully == false) {
|
|
|
|
|
this.errorUploadingAttachment = true
|
2022-02-10 19:07:38 +01:00
|
|
|
|
2022-02-10 18:07:06 +01:00
|
|
|
return new Promise((resolve, reject)=>{
|
|
|
|
|
reject(false)
|
|
|
|
|
})
|
|
|
|
|
|
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-03-14 14:16:23 +01:00
|
|
|
async sendRequest(params) {
|
|
|
|
|
this.ChatMethodsService.send(params).subscribe(
|
|
|
|
|
(response: any) => {
|
|
|
|
|
const ChatMessage = response.message
|
|
|
|
|
this.messageSend = true
|
|
|
|
|
this.redefinedMessage(ChatMessage)
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
this.WsChatService.registerCallback({
|
|
|
|
|
type: 'reConnect',
|
|
|
|
|
funx: async ()=> {
|
|
|
|
|
|
|
|
|
|
this.WsChatService.send(params).then(({message, requestId}) => {
|
|
|
|
|
let ChatMessage = message.result
|
|
|
|
|
this.messageSend = true
|
|
|
|
|
this.redefinedMessage(ChatMessage)
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
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-03 22:57:33 +01:00
|
|
|
|
|
|
|
|
const message = this.getChatObj()
|
|
|
|
|
|
|
|
|
|
ChatMessage = Object.assign(message, ChatMessage)
|
2022-02-07 20:18:48 +01:00
|
|
|
this.setData(ChatMessage)
|
2022-03-10 23:08:29 +01:00
|
|
|
|
|
|
|
|
await this.save()
|
2022-02-07 15:51:21 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-08 17:44:15 +01:00
|
|
|
async downloadFileMsg() {
|
|
|
|
|
const result = await this.NfService.beforeSendAttachment(this)
|
|
|
|
|
if(result) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 15:50:11 +01:00
|
|
|
private calDateDuration(date = null) {
|
|
|
|
|
this.duration = showDateDuration(date || this._updatedAt);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-10 23:08:29 +01:00
|
|
|
|
2022-03-15 15:49:59 +01:00
|
|
|
async deleteFromDB() {
|
|
|
|
|
this.delate = true
|
|
|
|
|
this.save()
|
2022-03-03 22:57:33 +01:00
|
|
|
}
|
2022-02-16 15:52:59 +01:00
|
|
|
|
2022-03-15 15:49:59 +01:00
|
|
|
|
2022-03-03 22:57:33 +01:00
|
|
|
isSenderIsNotMe(ChatMessage) {
|
|
|
|
|
return SessionStore.user.RochetChatUser != ChatMessage.u.username
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
messageOwnerById(id) {
|
|
|
|
|
return SessionStore.user.RochetChatUser != this.u.username
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getChatObj() {
|
|
|
|
|
return {
|
|
|
|
|
channels: this.channels,
|
|
|
|
|
mentions: this.mentions,
|
2022-03-10 23:08:29 +01:00
|
|
|
//msg: this.AESEncrypt.encrypt(this.msg, SessionStore.user.RochetChatUser),
|
|
|
|
|
msg:this.msg,
|
2022-03-03 22:57:33 +01:00
|
|
|
rid: this.rid,
|
|
|
|
|
ts: this.ts,
|
|
|
|
|
u: this.u,
|
|
|
|
|
_id: this._id,
|
2022-03-10 23:08:29 +01:00
|
|
|
id: this.id,
|
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,
|
|
|
|
|
delate: this.delate
|
2022-02-16 15:52:59 +01:00
|
|
|
}
|
2022-03-03 22:57:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addMessageDB() {
|
2022-03-10 23:08:29 +01:00
|
|
|
if(!this.addToDb) {
|
|
|
|
|
this.addToDb= true
|
|
|
|
|
const message = this.getChatObj()
|
2022-03-03 22:57:33 +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
|
|
|
|
2022-03-10 23:08:29 +01:00
|
|
|
this.id = createdMessage.id
|
|
|
|
|
}
|
2022-02-16 15:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-03 22:57:33 +01:00
|
|
|
async save() {
|
|
|
|
|
const message = this.getChatObj()
|
|
|
|
|
|
2022-03-10 23:08:29 +01:00
|
|
|
await MessageModel.update(message)
|
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 {
|
|
|
|
|
// this.msg = this.AESEncrypt.decrypt(this.msg, SessionStore.user.RochetChatUser)
|
|
|
|
|
} catch (error) {}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 23:52:33 +01:00
|
|
|
}
|