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

409 lines
9.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';
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-19 09:12:30 +01:00
import { Storage } from '@ionic/storage';
2022-01-21 16:55:05 +01:00
import { Platform } from '@ionic/angular';
import { SqliteService } from 'src/app/services/sqlite.service';
2022-01-29 19:24:46 +01:00
import { ChatService } from 'src/app/services/chat.service';
2022-01-26 17:28:55 +01:00
import { NativeNotificationService } from 'src/app/services/native-notification.service';
2022-01-28 15:59:01 +01:00
import { SortService } from '../functions/sort.service';
2022-01-29 19:21:46 +01:00
import { chatUser } from 'src/app/models/chatMethod';
2022-02-07 17:55:00 +01:00
import { NfService } from 'src/app/services/chat/nf.service'
2022-02-08 17:44:15 +01:00
2022-01-12 12:44:51 +01:00
@Injectable({
providedIn: 'root'
})
export class WsChatMethodsService {
2022-02-08 17:44:15 +01:00
dm = {}
group = {}
2022-01-12 12:44:51 +01:00
2022-01-28 19:01:24 +01:00
_dm = []
_group = []
2022-01-12 12:44:51 +01:00
loadingWholeList = false
dmCount = 0;
groupCount = 0;
2022-01-12 14:48:17 +01:00
2022-01-29 19:21:46 +01:00
currentRoom = null
users: chatUser[] = []
2022-01-12 12:44:51 +01:00
constructor(
2022-01-19 09:12:30 +01:00
private WsChatService: WsChatService,
2022-01-21 16:55:05 +01:00
private storage: Storage,
private platform: Platform,
private sqlservice: SqliteService,
2022-01-28 16:26:21 +01:00
private NativeNotificationService: NativeNotificationService,
2022-01-29 19:24:46 +01:00
private sortService: SortService,
2022-02-07 17:55:00 +01:00
private ChatService: ChatService,
2022-02-08 17:44:15 +01:00
private NfService: NfService,
2022-01-12 12:44:51 +01:00
) {
(async()=>{
2022-02-08 17:44:15 +01:00
await this.restoreRooms()
2022-01-12 12:44:51 +01:00
await this.getAllRooms();
this.subscribeToRoom()
2022-01-29 19:21:46 +01:00
//
await this.getUser()
this.getUserStatus()
2022-01-12 12:44:51 +01:00
})()
2022-01-12 14:48:17 +01:00
2022-01-31 14:44:22 +01:00
/**
* @description when the phone is in the background for a long time it could disconnects from the socket then the socket reconnects automatically,
* when the connection is lost the subscribe is also lost, so we have to subscribe again when reconnection is establish.
*/
this.WsChatService.registerCallback({
type: 'reConnect',
funx: ()=>{
this.subscribeToRoom()
}
})
2022-01-12 12:44:51 +01:00
}
2022-01-28 19:02:44 +01:00
getRoomFromDb() {
this.storage.get('Rooms').then((rooms) => {
rooms.result.update.forEach((roomData: room) => {
this.prepareRoom(roomData);
});
})
};
2022-01-29 19:21:46 +01:00
openRoom(roomId) {
if(this.currentRoom) {
if(this.getDmRoom(this.currentRoom)) {
this.getDmRoom(this.currentRoom).roomLeave()
} else if(this.getGroupRoom(this.currentRoom)) {
this.getGroupRoom(this.currentRoom).roomLeave()
}
}
this.currentRoom = roomId
if(this.currentRoom) {
if(this.getDmRoom(this.currentRoom)) {
this.getDmRoom(this.currentRoom).open()
} else if(this.getGroupRoom(this.currentRoom)) {
this.getGroupRoom(this.currentRoom).open()
}
}
}
2022-01-28 19:02:44 +01:00
2022-02-08 17:44:15 +01:00
async restoreRooms() {
try {
const rooms = await this.storage.get('Rooms');
if(rooms) {
await rooms.result.update.forEach( async (roomData: room) => {
await this.prepareRoom(roomData);
});
}
} catch(e){}
this.sortRoomList()
}
2022-01-12 12:44:51 +01:00
async getAllRooms () {
this.loadingWholeList = true
2022-02-02 10:37:57 +01:00
//this.getRoomFromDb();
2022-01-13 12:52:05 +01:00
const rooms = await this.WsChatService.getRooms();
2022-02-02 10:37:57 +01:00
await this.storage.remove('Rooms');
await this.storage.set('Rooms', rooms);
2022-01-28 19:02:44 +01:00
2022-02-08 17:44:15 +01:00
console.log('rooms', rooms)
this.dm = {}
this.group = {}
this._dm = []
this._group = []
2022-01-13 12:52:05 +01:00
// console.log("ROOMS" + JSON.stringify(rooms))
2022-01-28 17:33:26 +01:00
this.WsChatService.registerCallback({
type:'Onmessage',
funx:(message)=>{
if(message.msg =='changed' && message.collection == "stream-room-messages") {
if(message.fields.args[0].rid) {
2022-01-28 19:01:24 +01:00
setTimeout(()=>{
2022-01-29 19:44:20 +01:00
this.sortRoomList()
2022-01-28 19:01:24 +01:00
2022-01-29 19:44:20 +01:00
}, 100)
2022-01-28 19:01:24 +01:00
}
}
2022-01-28 19:01:24 +01:00
if(message.msg =='changed' && message.collection == "stream-notify-room") {
if(message.fields.eventName.includes('deleteMessage')){
setTimeout(()=>{
2022-01-29 19:44:20 +01:00
this.sortRoomList()
2022-01-28 19:01:24 +01:00
}, 100)
2022-01-28 17:33:26 +01:00
}
}
}
})
2022-01-28 19:01:24 +01:00
await rooms.result.update.forEach( async (roomData: room) => {
await this.prepareRoom(roomData);
2022-01-12 15:22:24 +01:00
});
2022-01-12 12:44:51 +01:00
2022-01-29 19:44:20 +01:00
this.sortRoomList()
2022-01-12 12:44:51 +01:00
this.loadingWholeList = false
}
2022-01-31 14:44:22 +01:00
/**
* @description sort room list by last message date
*/
2022-02-02 20:16:12 +01:00
sortRoomList =() => {
2022-01-29 19:44:20 +01:00
this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse()
this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse()
}
2022-01-31 14:44:22 +01:00
/**
* @description subscribe all room
*/
2022-01-12 12:44:51 +01:00
subscribeToRoom() {
2022-01-14 11:47:55 +01:00
2022-01-12 12:44:51 +01:00
for (const id in this.dm) {
2022-01-29 19:54:38 +01:00
this.defaultSubtribe(id)
2022-01-12 12:44:51 +01:00
}
for (const id in this.group) {
2022-01-29 19:54:38 +01:00
this.defaultSubtribe(id)
2022-01-12 12:44:51 +01:00
}
2022-01-29 19:54:38 +01:00
2022-01-17 14:39:12 +01:00
this.WsChatService.streamNotifyLogged().then((subscription=>{
console.log('streamRoomMessages', subscription)
}))
2022-01-12 12:44:51 +01:00
}
2022-01-29 20:16:39 +01:00
/**
* @description when a new room is create, needs to subtribe in order to receive updates
2022-02-02 10:28:36 +01:00
* @param id
* @param roomData
2022-01-29 20:16:39 +01:00
*/
2022-01-26 16:37:59 +01:00
subscribeToRoomUpdate(id, roomData) {
2022-02-02 10:28:36 +01:00
2022-01-29 19:54:38 +01:00
this.defaultSubtribe(id)
2022-01-26 16:37:59 +01:00
this.prepareRoom(roomData);
this.getGroupRoom(id).loadHistory();
}
2022-01-29 19:54:38 +01:00
/**
* @deprecated things a room need to subscribe on
2022-01-29 20:01:16 +01:00
* @param id room id
2022-01-29 19:54:38 +01:00
*/
2022-01-29 20:01:16 +01:00
private defaultSubtribe(id: any) {
2022-01-29 19:54:38 +01:00
this.WsChatService.streamRoomMessages(id).then((subscription)=>{
2022-02-08 15:41:40 +01:00
//console.log('streamRoomMessages', subscription)
2022-01-29 19:54:38 +01:00
})
this.WsChatService.subStreamNotifyRoom(id, 'typing', false)
this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{
2022-02-08 15:41:40 +01:00
//console.log('streamNotifyRoomDeleteMessage', subscription);
2022-01-29 19:54:38 +01:00
})
}
2022-02-02 10:28:36 +01:00
2022-01-29 20:16:39 +01:00
/**
2022-02-02 10:28:36 +01:00
* @description create a representation of an room in these instance this.dm, this.group ...
* @param roomData
2022-01-29 20:16:39 +01:00
*/
2022-01-29 17:06:25 +01:00
prepareRoom(roomData) {
2022-01-26 16:37:59 +01:00
let room:RoomService;
2022-02-07 20:18:48 +01:00
room = new RoomService(this.WsChatService, new MessageService(this.storage, this.NfService, this.WsChatService), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService, this.ChatService, this.NfService)
2022-01-28 16:30:30 +01:00
2022-01-26 16:37:59 +01:00
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()
2022-01-29 19:21:46 +01:00
room.getAllUsers = this.getUsers
2022-01-28 15:31:52 +01:00
room.receiveMessageDelete();
2022-02-02 20:16:12 +01:00
room.sortRoomList = this.sortRoomList
2022-01-26 16:37:59 +01:00
let roomId = this.getRoomId(roomData)
if(this.isIndividual(roomData)) {
this.dm[roomId] = room
2022-01-28 19:01:24 +01:00
this._dm.push(room)
2022-01-26 16:37:59 +01:00
this.dmCount++
} else {
this.group[roomId] = room
2022-01-28 19:01:24 +01:00
this._group.push(room)
2022-01-26 16:37:59 +01:00
this.groupCount++
}
}
2022-01-29 19:21:46 +01:00
getReceptorName(roomData) {
try {
return roomData.usernames.find((e)=> e != SessionStore.user.RochetChatUser)
} catch(e) {
return '*'
}
}
2022-01-29 19:54:38 +01:00
/**
2022-01-29 20:01:16 +01:00
* @description update user status. this method is called once only
2022-01-29 19:54:38 +01:00
* @param id user ID
*/
2022-01-29 20:01:16 +01:00
private getUserStatus(id?:string) {
2022-02-02 10:28:36 +01:00
2022-01-29 19:21:46 +01:00
this.WsChatService.getUserStatus((d) => {
const username = d.fields.args[0][1]
const statusNum = d.fields.args[0][2]
2022-02-02 10:28:36 +01:00
2022-01-29 19:21:46 +01:00
const statusText = this.statusNumberToText(statusNum)
2022-01-29 19:33:32 +01:00
const user = this.getUserByName(username)
if(user) {
user.status = statusText
}
2022-01-29 19:21:46 +01:00
})
}
getUserByName(username) {
return this.users.find((user)=> user.username == username)
}
2022-01-29 20:16:39 +01:00
/**
* @description convert rocketchat statues num to readable string
2022-02-02 10:28:36 +01:00
* @param text
* @returns
2022-01-29 20:16:39 +01:00
*/
2022-01-29 19:21:46 +01:00
statusNumberToText(text) {
if(text == '0') {
return "offline"
}
else if(text == '1') {
return "online"
}
else if(text == '2') {
return "away"
}
else if(text == '3') {
return "busy"
}
}
2022-02-02 10:28:36 +01:00
2022-01-29 19:21:46 +01:00
2022-01-28 15:31:52 +01:00
deleteMessage(id?) {
return this.WsChatService.deleteMessage(id);
}
2022-01-26 16:37:59 +01:00
2022-01-26 09:19:54 +01:00
leaveRoom(id?) {
return this.WsChatService.leaveRoom(id);
}
hidingRoom(id?) {
return this.WsChatService.hidingRoom(id);
}
addRoomOwner(roomid, userId){
return this.WsChatService.addRoomOwner(roomid, userId);
}
2022-01-26 16:37:59 +01:00
createPrivateRoom(groupName, username, customFields){
return this.WsChatService.createPrivateRoom(groupName, username, customFields);
}
2022-01-14 10:35:54 +01:00
getDmRoom(id): RoomService {
2022-01-12 12:44:51 +01:00
try {
return this.dm[id]
2022-01-14 10:35:54 +01:00
} catch(e) {}
}
getGroupRoom(id): RoomService {
try {
2022-01-12 12:44:51 +01:00
return this.group[id]
2022-01-14 10:35:54 +01:00
} catch(e) {}
2022-01-12 12:44:51 +01:00
}
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-14 10:46:44 +01:00
return roomData._id
2022-01-12 12:44:51 +01:00
}
2022-01-26 16:37:59 +01:00
getRoomLastMessage(roomData: room):any {
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
}
2022-01-29 19:21:46 +01:00
getUsers = () =>{
return this.users
}
async getUser() {
2022-01-29 19:24:46 +01:00
let _res = await this.ChatService.getAllUsers().toPromise()
2022-01-29 19:21:46 +01:00
2022-01-29 19:24:46 +01:00
let user = _res['users'].filter(data => data.username != SessionStore.user.RochetChatUser);
2022-01-29 19:21:46 +01:00
2022-01-29 19:24:46 +01:00
user = user.sort((a,b) => {
if(a.name < b.name) {
return -1;
}
if(a.name > b.name) {
return 1;
}
return 0;
});
2022-01-29 19:21:46 +01:00
2022-01-29 19:24:46 +01:00
this.users = user
2022-01-29 19:21:46 +01:00
}
getUserOfRoom(roomId){
return this.WsChatService.getUserOfRoom(roomId);
}
2022-01-29 19:21:46 +01:00
2022-01-12 12:44:51 +01:00
}