mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
131 lines
2.9 KiB
TypeScript
131 lines
2.9 KiB
TypeScript
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';
|
|
import { capitalizeTxt } from 'src/plugin/text'
|
|
import { Rooms, Update as room } from 'src/app/models/chatMethod';
|
|
|
|
@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 = await this.WsChatService.getRooms();
|
|
|
|
// console.log("ROOMS" + JSON.stringify(rooms))
|
|
|
|
rooms.result.update.forEach((roomData: room) => {
|
|
let room:RoomService;
|
|
|
|
// console.log(roomData);
|
|
|
|
|
|
room = new RoomService(this.WsChatService, new MessageService())
|
|
room.setData({
|
|
customFields: roomData.customFields,
|
|
id: this.getRoomId(roomData),
|
|
name: this.getRoomName(roomData),
|
|
lastMessage: this.getRoomLastMessage(roomData),
|
|
_updatedAt: new Date(roomData._updatedAt['$date'])
|
|
})
|
|
|
|
room.receiveMessage()
|
|
|
|
let roomId = this.getRoomId(roomData)
|
|
|
|
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.streamRoomMessages(id).then((subscription)=>{
|
|
console.log('streamRoomMessages', subscription)
|
|
})
|
|
}
|
|
|
|
for (const id in this.group) {
|
|
this.WsChatService.streamRoomMessages(id).then((subscription)=>{
|
|
console.log('streamRoomMessages', subscription)
|
|
})
|
|
}
|
|
}
|
|
|
|
getDmRoom(id): RoomService {
|
|
try {
|
|
return this.dm[id]
|
|
} catch(e) {}
|
|
}
|
|
|
|
getGroupRoom(id): RoomService {
|
|
try {
|
|
return this.group[id]
|
|
} catch(e) {}
|
|
}
|
|
|
|
getRoomName(roomData: room) {
|
|
if(this.isIndividual(roomData)) {
|
|
const names: String[] = roomData.usernames
|
|
const roomName = names.filter((name)=>{
|
|
return name != SessionStore.user.RochetChatUser
|
|
})[0]
|
|
|
|
const firstName = capitalizeTxt(roomName.split('.')[0])
|
|
const lastName = capitalizeTxt(roomName.split('.')[1])
|
|
return firstName + ' ' + lastName
|
|
} else {
|
|
return roomData.fname
|
|
}
|
|
}
|
|
|
|
getRoomId(roomData:room) {
|
|
return roomData._id
|
|
}
|
|
|
|
getRoomLastMessage(roomData: room) {
|
|
return roomData.lastMessage
|
|
}
|
|
|
|
private isIndividual(roomData: room) {
|
|
return !roomData.fname
|
|
}
|
|
|
|
}
|