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

117 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-01-12 12:44:51 +01:00
import { Injectable } from '@angular/core';
import { RoomService } from './room.service';
import { WsChatService } from 'src/app/services/chat/ws-chat.service';
import { MessageService } from 'src/app/services/chat/message.service'
import { SessionStore } from 'src/app/store/session.service';
@Injectable({
providedIn: 'root'
})
export class WsChatMethodsService {
dm: {[key: string]: RoomService} = {}
group: {[key: string]: RoomService} = {}
loadingWholeList = false
dmCount = 0;
groupCount = 0;
constructor(
private WsChatService: WsChatService
) {
(async()=>{
await this.getAllRooms();
this.subscribeToRoom()
})()
}
async getAllRooms () {
this.loadingWholeList = true
const rooms: any = await this.WsChatService.getRooms();
console.log(rooms)
rooms.result.update.forEach((roomData:any) => {
let room:RoomService;
room = new RoomService(this.WsChatService, new MessageService())
room.setData({
id: this.getRoomId(roomData),
name: this.getRoomName(roomData),
lastMessage: this.getRoomLastMessage(roomData),
_updatedAt: roomData._updatedAt['$date']
})
room.receiveMessage()
let roomId = roomData.lastMessage.rid
if(this.isIndividual(roomData)) {
this.dm[roomId] = room
this.dmCount++
} else {
this.group[roomId] = room
this.groupCount++
}
});
this.loadingWholeList = false
}
subscribeToRoom() {
for (const id in this.dm) {
this.WsChatService.subscribeNotifyRoom(id).then((subscription)=>{
console.log('subscription', subscription)
})
}
for (const id in this.group) {
this.WsChatService.subscribeNotifyRoom(id).then((subscription)=>{
console.log('subscription', subscription)
})
}
}
getRoom(id): RoomService {
try {
return this.dm[id]
} catch(e) {
return this.group[id]
}
}
getRoomName(roomData) {
if(this.isIndividual(roomData)) {
const names: String[] = roomData.usernames
const roomName = names.filter((name)=>{
return name != SessionStore.user.RochetChatUser
})[0]
return roomName
} else {
return roomData.fName
}
}
getRoomId(roomData) {
return roomData.lastMessage.rid
}
getRoomLastMessage(roomData) {
return roomData.lastMessage
}
private isIndividual(roomData) {
return !roomData.fname
}
}