improve chat remove loop

This commit is contained in:
Peter Maquiran
2022-01-11 15:43:09 +01:00
parent a8a1307bec
commit dcee5aa2f4
9 changed files with 96 additions and 84 deletions
+35 -6
View File
@@ -21,33 +21,41 @@ export class ChatService {
constructor(
private RocketChatClientService: RocketChatClientService
) {
this.getAllRoomAndSubscribe()
(async()=>{
await this.getAllRoom();
this.subscribeToRoom()
})()
}
async getAllRoomAndSubscribe () {
async getAllRoom () {
this.loadingWholeList = true
const rooms: any = await this.RocketChatClientService.getRooms();
rooms.result.update.forEach((roomData:any) => {
let room
let room:RoomService;
let roomId = roomData.lastMessage.rid
if(this.isIndividual(roomData)) {
room = new RoomService(new RocketChatClientService(), new MessageService())
room = new RoomService(this.RocketChatClientService, new MessageService())
room.setData({
id: this.getRoomId(roomData),
name: this.getChatName(roomData),
lastMessage: this.getRoomLastMessage(roomData)
})
room.receiveMessage()
this.individual[roomId] = room
this.individualCount++
} else {
room = new RoomService(new RocketChatClientService(), new MessageService())
room = new RoomService(this.RocketChatClientService, new MessageService())
room.setData({
id: this.getRoomId(roomData),
name: this.getChatName(roomData),
lastMessage: this.getRoomLastMessage(roomData)
})
room.receiveMessage()
this.group[roomId] = room
this.groupCount++
}
@@ -55,7 +63,28 @@ export class ChatService {
});
this.loadingWholeList = false
}
subscribeToRoom() {
for (const id in this.individual) {
this.RocketChatClientService.subscribe(id).then((subscription)=>{
console.log('subscription', subscription)
})
}
for (const id in this.group) {
this.RocketChatClientService.subscribe(id).then((subscription)=>{
console.log('subscription', subscription)
})
}
}
getRoom(id): RoomService {
try {
return this.individual[id]
} catch(e) {
return this.group[id]
}
}
getChatName(roomData) {
+4
View File
@@ -5,5 +5,9 @@ import { Injectable } from '@angular/core';
})
export class MessageService {
channels = []
mentions = []
msg = ''
rid = ''
constructor() { }
}
+36 -4
View File
@@ -13,21 +13,53 @@ export class RoomService {
chatUser: ChatUserService[] = []
id = ''
name = ''
mmm = []
private hasLoadHistory = false
constructor(
private RocketChatClientService: RocketChatClientService,
public RocketChatClientService: RocketChatClientService,
private MessageService: MessageService
) {}
) {
}
setData({id, name, lastMessage}) {
this.id = id
this.name = name
this.lastMessage = lastMessage
}
receiveMessage() {
this.RocketChatClientService.receiveLiveMessageFromRoom(
this.id,
this.constructor.name+this.id,
(Chatmessage)=>{
this.hasLoadHistory = false
this.loadHistory()
}
)
}
send(msg) {
this.RocketChatClientService.send(this.id, msg)
}
// runs onces only
loadHistory(limit= 100) {
if(this.hasLoadHistory){ return false }
this.RocketChatClientService.loadHistory(this.id, limit).then((message:any)=>{
console.log('loadHistory', message)
this.mmm = message.result.messages.reverse()
})
this.hasLoadHistory = true
}
create() {}
sendMessage() {}
deleteMessage() {}
deleteMessage(msgId) {}
ReactToMessage() {}
}