mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 21:06:06 +00:00
113 lines
2.5 KiB
TypeScript
113 lines
2.5 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { RoomService } from './room.service';
|
|
import { RocketChatClientService } from 'src/app/services/socket/rocket-chat-client.service';
|
|
import { MessageService } from 'src/app/services/chat/message.service'
|
|
import { SessionStore } from 'src/app/store/session.service';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class ChatService {
|
|
|
|
|
|
individual: {[key: string]: RoomService} = {}
|
|
group: {[key: string]: RoomService} = {}
|
|
|
|
loadingWholeList = false
|
|
|
|
individualCount = 0;
|
|
groupCount = 0;
|
|
|
|
constructor(
|
|
private RocketChatClientService: RocketChatClientService
|
|
) {
|
|
|
|
(async()=>{
|
|
await this.getAllRoom();
|
|
this.subscribeToRoom()
|
|
})()
|
|
|
|
}
|
|
|
|
async getAllRoom () {
|
|
this.loadingWholeList = true
|
|
|
|
const rooms: any = await this.RocketChatClientService.getRooms();
|
|
|
|
rooms.result.update.forEach((roomData:any) => {
|
|
let room:RoomService;
|
|
|
|
room = new RoomService(this.RocketChatClientService, new MessageService())
|
|
room.setData({
|
|
id: this.getRoomId(roomData),
|
|
name: this.getChatName(roomData),
|
|
lastMessage: this.getRoomLastMessage(roomData),
|
|
_updatedAt: roomData._updatedAt['$date']
|
|
})
|
|
|
|
room.receiveMessage()
|
|
|
|
let roomId = roomData.lastMessage.rid
|
|
|
|
if(this.isIndividual(roomData)) {
|
|
this.individual[roomId] = room
|
|
this.individualCount++
|
|
} else {
|
|
this.group[roomId] = room
|
|
this.groupCount++
|
|
}
|
|
|
|
});
|
|
|
|
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) {
|
|
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
|
|
}
|
|
|
|
}
|