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';
|
2022-01-13 11:43:36 +01:00
|
|
|
import { capitalizeTxt } from 'src/plugin/text'
|
2022-01-13 12:04:33 +01:00
|
|
|
import { Rooms, Update as room } from 'src/app/models/chatMethod';
|
2022-01-12 12:44:51 +01:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class WsChatMethodsService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dm: {[key: string]: RoomService} = {}
|
|
|
|
|
group: {[key: string]: RoomService} = {}
|
|
|
|
|
|
|
|
|
|
loadingWholeList = false
|
|
|
|
|
|
|
|
|
|
dmCount = 0;
|
|
|
|
|
groupCount = 0;
|
2022-01-12 14:48:17 +01:00
|
|
|
|
2022-01-12 12:44:51 +01:00
|
|
|
constructor(
|
|
|
|
|
private WsChatService: WsChatService
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
(async()=>{
|
|
|
|
|
await this.getAllRooms();
|
|
|
|
|
this.subscribeToRoom()
|
|
|
|
|
})()
|
2022-01-12 14:48:17 +01:00
|
|
|
|
2022-01-12 12:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getAllRooms () {
|
|
|
|
|
this.loadingWholeList = true
|
|
|
|
|
|
2022-01-13 12:52:05 +01:00
|
|
|
const rooms = await this.WsChatService.getRooms();
|
2022-01-12 12:44:51 +01:00
|
|
|
|
2022-01-13 12:52:05 +01:00
|
|
|
// console.log("ROOMS" + JSON.stringify(rooms))
|
2022-01-12 12:44:51 +01:00
|
|
|
|
2022-01-13 12:04:33 +01:00
|
|
|
rooms.result.update.forEach((roomData: room) => {
|
2022-01-12 12:44:51 +01:00
|
|
|
let room:RoomService;
|
2022-01-12 14:48:17 +01:00
|
|
|
|
|
|
|
|
console.log(roomData);
|
|
|
|
|
|
|
|
|
|
|
2022-01-12 12:44:51 +01:00
|
|
|
room = new RoomService(this.WsChatService, new MessageService())
|
|
|
|
|
room.setData({
|
2022-01-13 21:24:43 +01:00
|
|
|
customFields: roomData.customFields,
|
2022-01-12 12:44:51 +01:00
|
|
|
id: this.getRoomId(roomData),
|
|
|
|
|
name: this.getRoomName(roomData),
|
|
|
|
|
lastMessage: this.getRoomLastMessage(roomData),
|
2022-01-13 21:24:43 +01:00
|
|
|
_updatedAt: new Date(roomData._updatedAt['$date'])
|
2022-01-12 12:44:51 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
room.receiveMessage()
|
|
|
|
|
|
|
|
|
|
let roomId = roomData.lastMessage.rid
|
|
|
|
|
|
|
|
|
|
if(this.isIndividual(roomData)) {
|
|
|
|
|
this.dm[roomId] = room
|
|
|
|
|
this.dmCount++
|
|
|
|
|
} else {
|
|
|
|
|
this.group[roomId] = room
|
|
|
|
|
this.groupCount++
|
|
|
|
|
}
|
2022-01-12 14:48:17 +01:00
|
|
|
|
2022-01-13 13:46:56 +01:00
|
|
|
|
2022-01-12 12:44:51 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-12 15:22:24 +01:00
|
|
|
console.log('this.group', this.group)
|
2022-01-12 12:44:51 +01:00
|
|
|
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]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 12:04:33 +01:00
|
|
|
getRoomName(roomData: room) {
|
2022-01-12 12:44:51 +01:00
|
|
|
if(this.isIndividual(roomData)) {
|
|
|
|
|
const names: String[] = roomData.usernames
|
|
|
|
|
const roomName = names.filter((name)=>{
|
|
|
|
|
return name != SessionStore.user.RochetChatUser
|
|
|
|
|
})[0]
|
|
|
|
|
|
2022-01-13 12:01:56 +01:00
|
|
|
const firstName = capitalizeTxt(roomName.split('.')[0])
|
|
|
|
|
const lastName = capitalizeTxt(roomName.split('.')[1])
|
|
|
|
|
return firstName + ' ' + lastName
|
2022-01-12 12:44:51 +01:00
|
|
|
} else {
|
2022-01-12 14:48:17 +01:00
|
|
|
return roomData.fname
|
2022-01-12 12:44:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 12:04:33 +01:00
|
|
|
getRoomId(roomData:room) {
|
2022-01-12 12:44:51 +01:00
|
|
|
return roomData.lastMessage.rid
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 12:04:33 +01:00
|
|
|
getRoomLastMessage(roomData: room) {
|
2022-01-12 12:44:51 +01:00
|
|
|
return roomData.lastMessage
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 12:04:33 +01:00
|
|
|
private isIndividual(roomData: room) {
|
2022-01-12 12:44:51 +01:00
|
|
|
return !roomData.fname
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|