improvements

This commit is contained in:
tiago.kayaya
2021-01-13 10:02:30 +01:00
parent 6596941cdd
commit 586651e95e
18 changed files with 591 additions and 205 deletions
+101 -35
View File
@@ -1,4 +1,4 @@
import { HttpHeaders } from '@angular/common/http';
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from "rxjs"
import { AuthService } from './auth.service';
@@ -14,6 +14,7 @@ import { Storage } from '@ionic/storage';
export class ChatService {
headers: HttpHeaders;
options:any;
options1:any;
X_User_Id:any;
X_Auth_Token:any;
@@ -25,48 +26,113 @@ export class ChatService {
private storage: Storage,
private storageService:StorageService,) {
this.headers = new HttpHeaders();
this.storage.get('user').then((val) => {
console.log(JSON.parse(unescape(atob(val))).data.userId);
let user = JSON.parse(unescape(atob(val))).data;
this.headers = this.headers.set('X-User-Id', user.userId);
console.log(user.userId);
this.X_User_Id = user.userId;
this.headers = this.headers.set('X-Auth-Token', user.authToken);
console.log(user.authToken);
this.X_Auth_Token = user.authToken;
this.options = {
headers: {
'X-User-Id': this.X_User_Id,
'X-Auth-Token': this.X_Auth_Token
},
};
this.getAllConnectedUsers(this.options);
});
/* this.authService.userData$.subscribe((res:any)=>{
/* this.headers = this.headers.set('X-User-Id', 'GqjNWiLrGEHRna7Zn');
this.headers = this.headers.set('X-Auth-Token', 'dAM0ZOTAy8jzQA_vS25z2IrnSc6sYLfi5rmaa35YNUz'); */
-
this.authService.userData$.subscribe((res:any)=>{
this.headers = this.headers.set('X-User-Id', res.userId);
this.headers = this.headers.set('X-Auth-Token', res.authToken);
}); */
});
this.options = {
headers: {
'X-User-Id': this.X_User_Id,
'X-Auth-Token': this.X_Auth_Token
},
headers: this.headers,
};
}
getAllUsers(){
console.log(this.headers);
/* return this.http.get(environment.apiChatUrl+'users.list', this.options); */
/* getUser(){
this.storage.get('user').then((val) => {
let user = JSON.parse(unescape(atob(val))).data;
this.headers = this.headers.set('X-User-Id', user.userId);
this.headers = this.headers.set('X-Auth-Token', user.authToken);
this.options1 = {
headers: this.headers,
};
console.log(this.options1);
this.http.get(environment.apiChatUrl+'users.presence', this.options1).subscribe(res => {
console.log(res);
});
});
} */
getAllDirectMessages(){
return this.http.get(environment.apiChatUrl+'im.list', this.options);
}
getAllConnectedUsers(opts){
console.log(opts);
return this.http.get(environment.apiChatUrl+'users.presence', opts);
getAllChannels(){
return this.http.get(environment.apiChatUrl+'channels.list', this.options);
}
getAllUserChannels(){
return this.http.get(environment.apiChatUrl+'channels.list.joined', this.options);
}
getAllRooms(){
return this.http.get(environment.apiChatUrl+'rooms.get', this.options);
}
getAllPrivateGroups(){
/* return this.http.get(environment.apiChatUrl+'groups.list', this.options); */
return this.http.get(environment.apiChatUrl+'groups.list', this.options);
}
getAllUsers(){
return this.http.get(environment.apiChatUrl+'users.list', this.options);
}
getAllConnectedUsers(){
return this.http.get(environment.apiChatUrl+'users.presence', this.options);
}
//Check whether user is online or offline
getUserPresence(id:any){
let params = new HttpParams();
params = params.set("userId", id);
let opts = {
headers: this.headers,
params: params
}
return this.http.get(environment.apiChatUrl+'users.getPresence', opts);
}
//Load messages from roomId
loadJoinedRooms(){
return this.http.get(environment.apiChatUrl+'im.list', this.options);
}
//Load messages from roomId
getRoomMessages(roomId:any){
let params = new HttpParams();
params = params.set("roomId", roomId);
let opts = {
headers: this.headers,
params: params
}
return this.http.get(environment.apiChatUrl+'im.history', opts);
}
sendMessage(body:any){
let opts = {
headers: this.headers,
}
return this.http.post(environment.apiChatUrl+'chat.sendMessage', body, opts);
}
//Load members from a chat
getMembers(roomId:any){
let params = new HttpParams();
params = params.set("roomId", roomId);
let opts = {
headers: this.headers,
params: params
}
return this.http.get(environment.apiChatUrl+'im.members', opts);
}
removeChatRoom(body:any){
let opts = {
headers: this.headers,
}
return this.http.post(environment.apiChatUrl+'im.close', body, opts);
}
}