improve chat

This commit is contained in:
Peter Maquiran
2022-02-03 21:01:53 +01:00
parent 33a2982805
commit 61de7b937b
13 changed files with 1147 additions and 668 deletions
+26 -18
View File
@@ -2,6 +2,8 @@ import { Injectable } from '@angular/core';
import { Message } from 'src/app/models/chatMethod';
import { chatHistory, ChatMessage, File } from 'src/app/models/chatMethod'
import { Storage } from '@ionic/storage';
import { SessionStore } from 'src/app/store/session.service';
import { capitalizeTxt } from 'src/plugin/text'
@Injectable({
providedIn: 'root'
@@ -17,28 +19,34 @@ export class MessageService {
u = {}
t = ''
_id =''
_updatedAt = ''
_updatedAt
file
attachments
offline = false
offline = true
constructor(private storage: Storage) {
}
}
setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments}:Message) {
this.customFields = customFields
this.channels = channels
this.mentions = mentions
this.msg = msg
this.customFields = customFields
this.channels = channels || []
this.mentions = mentions || []
this.msg = msg || ""
this.rid = rid
this.ts = ts
this.u = u
this.u = u || { name: this.usernameToDisplayName(SessionStore.user.RochetChatUser), username: SessionStore.user.RochetChatUser, _id: ""}
this.t = t
this._id = _id
this._updatedAt = _updatedAt
this._updatedAt = _updatedAt || new Date().getTime()
this.file = file
this.attachments = attachments
if(!this.ts) {
this.offline = true
} else {
this.offline = false
}
if (this.file) {
if (this.file.guid) {
@@ -49,22 +57,22 @@ export class MessageService {
}
}
if(this.rid == 'offline') {
this.offline = true
} else {
this.offline = false
}
}
delete() {}
showDateDuration() {}
resend() {
}
private usernameToDisplayName(username) {
const firstName = capitalizeTxt(username.split('.')[0])
const lastName = capitalizeTxt(username.split('.')[1])
return firstName + ' ' + lastName
}
}
+63 -25
View File
@@ -14,6 +14,7 @@ import { capitalizeTxt } from 'src/plugin/text'
import { SortService } from '../functions/sort.service';
import { chatUser } from 'src/app/models/chatMethod';
import { environment } from 'src/environments/environment';
import { ChatService } from 'src/app/services/chat.service';
@Injectable({
providedIn: 'root'
@@ -62,6 +63,7 @@ export class RoomService {
private sqlservice: SqliteService,
private NativeNotificationService: NativeNotificationService,
private sortService: SortService,
private chatService: ChatService,
) {
this.NativeNotificationService.askForPermission()
}
@@ -88,9 +90,10 @@ export class RoomService {
this.id,
"stream-room-messages",
(ChatMessage) => {
ChatMessage = ChatMessage.fields.args[0]
if(environment.chatOffline == false || this.isSenderIsNotMe(ChatMessage)) {
ChatMessage = ChatMessage.fields.args[0]
ChatMessage = this.fix_updatedAt(ChatMessage)
console.log('recivemessage', ChatMessage)
@@ -211,15 +214,15 @@ export class RoomService {
const offlineChatMessage = {
channels: [],
mentions: [],
rid: 'offline',
rid: this.id,
msg: this.message,
ts: { $date: new Date().getTime() },
u: {
name: this.usernameToDisplayName(SessionStore.user.RochetChatUser),
username: SessionStore.user.RochetChatUser,
_id: ""
},
_updatedAt: new Date().getTime()
_updatedAt: new Date().getTime(),
offline: true
}
this.addMessageDB(offlineChatMessage)
@@ -231,31 +234,60 @@ export class RoomService {
this.lastMessage = message
this.redefinedMessage(message)
this.WsChatService.send(this.id, message.msg).then((data: any) => {
let ChatMessage = data.result
this.redefinedMessage(message, ChatMessage)
})
this.calDateDuration(message._updatedAt)
this.sortRoomList()
this.message= ''
}
}
redefinedMessage = (message: MessageService) => {
this.WsChatService.send(this.id, message.msg).then((data: any) => {
let ChatMessage = data.result
sendFile({file, attachments, alias = 'document'}) {
let offlineChatMessage = {
rid: this.id,
msg: this.message,
attachments,
file,
}
this.addMessageDB(offlineChatMessage)
const message: MessageService = this.prepareMessage(offlineChatMessage)
ChatMessage = this.fix_updatedAt(ChatMessage)
setTimeout(() => {
this.scrollDown()
}, 150)
message.setData(ChatMessage)
this.lastMessage = message
if( new Date(this.lastMessage._updatedAt).getTime() < new Date(message._updatedAt).getTime()) {
this.lastMessage = message
this.calDateDuration(message._updatedAt)
}
this.sortRoomList()
})
this.calDateDuration(message._updatedAt)
this.sortRoomList()
this.chatService.sendMessage({message:offlineChatMessage}).subscribe((res:any) => {
let ChatMessage = res.message
console.log('ChatMessage', ChatMessage)
this.redefinedMessage(message, ChatMessage)
}, (error) => {
});
}
redefinedMessage (message: MessageService, ChatMessage) {
ChatMessage = this.fix_updatedAt(ChatMessage)
message.setData(ChatMessage)
if( new Date(this.lastMessage._updatedAt).getTime() < new Date(message._updatedAt).getTime()) {
this.lastMessage = message
this.calDateDuration(message._updatedAt)
}
this.sortRoomList()
}
@@ -330,11 +362,15 @@ export class RoomService {
let localMessages = []
messages.forEach((message = []) => {
const wewMessage = this.prepareMessage(message)
messages.forEach((ChatMessage, index) => {
const wewMessage = this.prepareMessage(ChatMessage)
if(wewMessage.rid == 'offline') {
this.redefinedMessage(wewMessage)
if(wewMessage.offline == true) {
this.WsChatService.send(this.id, wewMessage.msg).then((data: any) => {
let _ChatMessage = data.result
this.redefinedMessage(wewMessage, _ChatMessage)
this.storage.set('chatmsg' + this.id, messages)
})
}
localMessages.push(wewMessage)
@@ -381,7 +417,7 @@ export class RoomService {
prepareMessage(message) {
prepareMessage(message): MessageService {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage)
wewMessage.setData(message)
@@ -412,9 +448,11 @@ export class RoomService {
if (message.result) {
//console.log('FIX UPDATE ', message.result)
message.result._updatedAt = message.result._updatedAt['$date']
} else if(message._updatedAt.hasOwnProperty('$date')) {
// console.log('FIX UPDATE 11', message)
message._updatedAt = message._updatedAt['$date']
} else if(message._updatedAt) {
if(message._updatedAt.hasOwnProperty('$date')) {
// console.log('FIX UPDATE 11', message)
message._updatedAt = message._updatedAt['$date']
}
}
return message
}
@@ -193,7 +193,7 @@ export class WsChatMethodsService {
prepareRoom(roomData) {
let room:RoomService;
room = new RoomService(this.WsChatService, new MessageService(this.storage), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService)
room = new RoomService(this.WsChatService, new MessageService(this.storage), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService, this.ChatService)
room.setData({
customFields: roomData.customFields,