improve chat

This commit is contained in:
Peter Maquiran
2023-01-09 10:49:58 +01:00
parent 56c1733945
commit a73dde467c
21 changed files with 436 additions and 373 deletions
+108 -30
View File
@@ -12,8 +12,8 @@ import { HttpEventType } from '@angular/common/http';
import { AttachmentsService } from 'src/app/services/attachments.service';
import { NetworkServiceService , ConnectionStatus} from 'src/app/services/network-service.service';
import { ChatSystemService } from './chat-system.service';
import { resolve } from 'dns';
import { async } from '@angular/core/testing';
import { v4 as uuidv4 } from 'uuid'
@Injectable({
providedIn: 'root'
})
@@ -38,7 +38,6 @@ export class MessageService {
_updatedAt
file
attachments
offline = true
displayType = ''
temporaryData: any = {}
hasFile = false
@@ -50,8 +49,8 @@ export class MessageService {
from: 'Offline'|'History'|'stream'| 'send'
duration = ''
localReference = null
viewed = []
received = []
viewed: string[] = []
received: string[]= []
addToDb = false
messageSend = false
@@ -73,7 +72,7 @@ export class MessageService {
private ChatSystemService: ChatSystemService) {
}
setData({customFields = {}, channels, mentions, msg ,rid ,ts, u, t, _id, id, _updatedAt, file, attachments, temporaryData, localReference , viewed = [], received = [], delate = false, delateRequest =false, from, sendAttempt = 0 }: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 }:Message) {
this.channels = channels || []
this.mentions = mentions || []
@@ -102,11 +101,9 @@ export class MessageService {
this.received = [...new Set([...received,...this.received])];
if(!this.ts) {
this.offline = true
this.messageSend = false
} else {
this.messageSend = true
this.offline = false
}
if (this.file) {
@@ -137,6 +134,34 @@ export class MessageService {
this.calDateDuration()
}
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 {
@@ -150,7 +175,6 @@ export class MessageService {
}
async send(): Promise<any> {
if(this.messageSend) {
return new Promise((resolve, reject) => {
resolve('solve')
@@ -183,7 +207,7 @@ export class MessageService {
this.RochetChatConnectorService.registerCallback({
type: 'reConnect',
funx: async ()=> {
funx: async () => {
this.send().catch((error) =>{
console.error(error)
})
@@ -250,7 +274,6 @@ export class MessageService {
}
if(this.NetworkServiceService.getCurrentNetworkStatus() == ConnectionStatus.Online) {
// console.log('online send')
if(this.msg == '<script></script>') {
if(this.sendAttempt >= 4) {
@@ -259,7 +282,6 @@ export class MessageService {
ChatMessage = ChatMessage.message.result
clearTimeout(this.functionTimer);
this.messageSend = true
this.redefinedMessage(ChatMessage)
}
@@ -270,12 +292,12 @@ export class MessageService {
}
} else {
this.RochetChatConnectorService.send(params).then(
(ChatMessage: any) => {
ChatMessage = ChatMessage.message.result
clearTimeout(this.functionTimer);
this.messageSend = true
this.redefinedMessage(ChatMessage)
}
@@ -286,7 +308,6 @@ export class MessageService {
}
this.functionTimer = setTimeout(() => {
this.RochetChatConnectorService.registerCallback({
type:'Onmessage',
key:'ping-pong-message',
@@ -300,7 +321,7 @@ export class MessageService {
return true
}
this.save()
this.saveChanges()
}
})
@@ -312,7 +333,6 @@ export class MessageService {
this.RochetChatConnectorService.registerCallback({
type: 'reConnect',
funx: async ()=> {
this.send().catch((error) =>{
console.error(error)
})
@@ -329,11 +349,18 @@ export class MessageService {
const message = this.getChatObj()
this.manualRetry = false
this.messageSend = true
ChatMessage = Object.assign(message, ChatMessage)
this.setData(ChatMessage)
await this.save()
const roomObject = this.ChatSystemService.getRoomById(this.rid)
const users = roomObject.getUsersByStatus('online')
for(const user of users) {
this.addReceived(user._id)
}
await this.saveChanges()
}
downloadFileMsg() {
@@ -363,7 +390,7 @@ export class MessageService {
}
// save the changes to the storage
this.save()
this.saveChanges()
this.downloadLoader = false;
this.downloadAttachments = true
this.downloadAttachmentsTemp++;
@@ -384,30 +411,55 @@ export class MessageService {
async delateStatusFalse() {
this.delate = true
this.save()
this.saveChanges()
}
addViewed(id: string) {
if(this.messageOwner()) {
let found = this.viewed.find((UserId) => UserId == id)
if(!found) {
this.viewed.push(id)
return true
}
}
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
}
async delateDB() {
const message = await MessageModel.get({_id: this._id})
const message = await MessageModel.get({id: this.id})
await message.delete()
}
isSenderIsNotMe(ChatMessage) {
return SessionStore.user.UserName != ChatMessage.u.username
return SessionStore.user.ChatData.data.userId != ChatMessage.u._id
}
messageOwnerById(id) {
return SessionStore.user.UserName != this.u.username
messageOwner() {
return SessionStore.user.ChatData.data.userId == this.u._id
}
private getChatObj() {
return {
channels: this.channels,
mentions: this.mentions,
//msg: this.AESEncrypt.encrypt(this.msg, SessionStore.user.UserName),
// msg: this.AESEncrypt.encrypt(this.msg, SessionStore.user.UserName),
sendAttempt: this.sendAttempt,
msg:this.msg,
rid: this.rid,
@@ -427,8 +479,10 @@ export class MessageService {
}
}
earlySave = false
async addMessageDB() {
if(!this.addToDb) {
if(!this.addToDb && this.save) {
this.addToDb = true
const message = this.getChatObj()
@@ -436,15 +490,39 @@ export class MessageService {
const createdMessage = await MessageModel.create(message)
this.id = createdMessage.id
if(this.earlySave) {
this.saveChanges();
}
}
}
async save() {
const message = this.getChatObj()
async saveChanges() {
if(this.save) {
const message = this.getChatObj()
let a
if (this.localReference) {
a = await MessageModel.get({localReference: this.localReference})
} else if (this._id) {
a = await MessageModel.get({id: this.id})
} else if(this.id) {
a = await MessageModel.get({_id: this._id})
}
for( const [name, value] of Object.entries(message)) {
a[name] = value
}
await a.save()
} else {
this.earlySave = true
console.log('save change to early')
}
await MessageModel.update(message)
}
decryptMessage() {