merge developer

This commit is contained in:
Eudes Inácio
2022-01-31 05:50:11 +01:00
21 changed files with 730 additions and 284 deletions
+157 -43
View File
@@ -8,8 +8,10 @@ import { Rooms, Update as room } from 'src/app/models/chatMethod';
import { Storage } from '@ionic/storage';
import { Platform } from '@ionic/angular';
import { SqliteService } from 'src/app/services/sqlite.service';
import { ChatService } from 'src/app/services/chat.service';
import { NativeNotificationService } from 'src/app/services/native-notification.service';
import { SortService } from '../functions/sort.service';
import { chatUser } from 'src/app/models/chatMethod';
@Injectable({
providedIn: 'root'
@@ -28,18 +30,28 @@ export class WsChatMethodsService {
dmCount = 0;
groupCount = 0;
currentRoom = null
users: chatUser[] = []
constructor(
private WsChatService: WsChatService,
private storage: Storage,
private platform: Platform,
private sqlservice: SqliteService,
private NativeNotificationService: NativeNotificationService,
private sortService: SortService
private sortService: SortService,
private ChatService: ChatService
) {
(async()=>{
await this.getAllRooms();
this.subscribeToRoom()
//
await this.getUser()
this.getUserStatus()
})()
}
@@ -51,6 +63,27 @@ export class WsChatMethodsService {
});
})
};
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()
}
}
}
async getAllRooms () {
this.loadingWholeList = true
@@ -68,82 +101,59 @@ export class WsChatMethodsService {
if(message.fields.args[0].rid) {
setTimeout(()=>{
console.log('sort this._dm', this._dm)
this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse()
this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse()
this.sortRoomList()
}, 100)
}
}
if(message.msg =='changed' && message.collection == "stream-notify-room") {
if(message.fields.eventName.includes('deleteMessage')){
setTimeout(()=>{
console.log('sort this._dm', this._dm)
this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse()
this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse()
this.sortRoomList()
}, 100)
}
}
}
})
await rooms.result.update.forEach( async (roomData: room) => {
await this.prepareRoom(roomData);
});
this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse()
this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse()
this.sortRoomList()
this.loadingWholeList = false
}
sortRoomList() {
this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse()
this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse()
}
subscribeToRoom() {
for (const id in this.dm) {
this.WsChatService.streamRoomMessages(id).then((subscription)=>{
console.log('streamRoomMessages', subscription)
})
this.WsChatService.subStreamNotifyRoom(id, 'typing', false)
this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{
console.log('streamNotifyRoomDeleteMessage', subscription);
})
this.defaultSubtribe(id)
}
for (const id in this.group) {
this.WsChatService.streamRoomMessages(id).then((subscription)=>{
console.log('streamRoomMessages', subscription)
})
this.WsChatService.subStreamNotifyRoom(id, 'typing', false)
this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{
console.log('streamNotifyRoomDeleteMessage', subscription);
})
this.defaultSubtribe(id)
}
this.WsChatService.streamNotifyLogged().then((subscription=>{
console.log('streamRoomMessages', subscription)
}))
}
/**
* @description when a new room is create, needs to subtribe in order to receive updates
* @param id
* @param roomData
*/
subscribeToRoomUpdate(id, roomData) {
this.WsChatService.streamRoomMessages(id).then((subscription)=> {
console.log('streamRoomMessages', subscription)
})
this.WsChatService.streamRoomMessages(id).then((subscription) => {
console.log('streamRoomMessages', subscription)
})
this.WsChatService.streamNotifyLogged().then((subscription=> {
console.log('streamRoomMessages', subscription)
}))
this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{
console.log('streamNotifyRoomDeleteMessage', subscription);
})
this.defaultSubtribe(id)
this.prepareRoom(roomData);
@@ -151,6 +161,27 @@ export class WsChatMethodsService {
}
/**
* @deprecated things a room need to subscribe on
* @param id room id
*/
private defaultSubtribe(id: any) {
this.WsChatService.streamRoomMessages(id).then((subscription)=>{
console.log('streamRoomMessages', subscription)
})
this.WsChatService.subStreamNotifyRoom(id, 'typing', false)
this.WsChatService.streamNotifyRoomDeleteMessage(id).then((subscription)=>{
console.log('streamNotifyRoomDeleteMessage', subscription);
})
}
/**
* @description create a representation of an room in these instance this.dm, this.group ...
* @param roomData
*/
prepareRoom(roomData) {
let room:RoomService;
@@ -166,6 +197,7 @@ export class WsChatMethodsService {
})
room.receiveMessage()
room.getAllUsers = this.getUsers
room.receiveMessageDelete();
let roomId = this.getRoomId(roomData)
@@ -181,6 +213,64 @@ export class WsChatMethodsService {
}
}
getReceptorName(roomData) {
try {
return roomData.usernames.find((e)=> e != SessionStore.user.RochetChatUser)
} catch(e) {
return '*'
}
}
/**
* @description update user status. this method is called once only
* @param id user ID
*/
private getUserStatus(id?:string) {
this.WsChatService.getUserStatus((d) => {
const username = d.fields.args[0][1]
const statusNum = d.fields.args[0][2]
const statusText = this.statusNumberToText(statusNum)
const user = this.getUserByName(username)
if(user) {
user.status = statusText
}
})
}
getUserByName(username) {
return this.users.find((user)=> user.username == username)
}
/**
* @description convert rocketchat statues num to readable string
* @param text
* @returns
*/
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"
}
}
deleteMessage(id?) {
return this.WsChatService.deleteMessage(id);
}
@@ -240,4 +330,28 @@ export class WsChatMethodsService {
return !roomData.fname
}
getUsers = () =>{
return this.users
}
async getUser() {
let _res = await this.ChatService.getAllUsers().toPromise()
let user = _res['users'].filter(data => data.username != SessionStore.user.RochetChatUser);
user = user.sort((a,b) => {
if(a.name < b.name) {
return -1;
}
if(a.name > b.name) {
return 1;
}
return 0;
});
this.users = user
}
}