mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
Merge with websocket branch
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
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';
|
||||
import { Storage } from '@ionic/storage';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class WsChatMethodsService {
|
||||
|
||||
|
||||
|
||||
dm: {[key: string]: RoomService} = {}
|
||||
group: {[key: string]: RoomService} = {}
|
||||
|
||||
loadingWholeList = false
|
||||
|
||||
dmCount = 0;
|
||||
groupCount = 0;
|
||||
|
||||
constructor(
|
||||
private WsChatService: WsChatService,
|
||||
private storage: Storage
|
||||
) {
|
||||
(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(), this.storage)
|
||||
room.setData({
|
||||
customFields: roomData.customFields,
|
||||
id: this.getRoomId(roomData),
|
||||
name: this.getRoomName(roomData),
|
||||
t: roomData.t,
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
this.WsChatService.streamNotifyLogged().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
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user