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

233 lines
6.8 KiB
TypeScript
Raw Normal View History

2021-01-13 10:02:30 +01:00
import { HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from "rxjs"
import { AuthService } from './auth.service';
import { HttpService } from './http.service';
import { StorageService } from './storage.service';
import { HttpClient, HttpHeaderResponse } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import { Storage } from '@ionic/storage';
@Injectable({
providedIn: 'root'
})
export class ChatService {
headers: HttpHeaders;
options:any;
2021-01-13 10:02:30 +01:00
options1:any;
2021-01-07 09:39:36 +01:00
X_User_Id:any;
X_Auth_Token:any;
constructor(
private http:HttpClient,
private httpService: HttpService,
private authService: AuthService,
private storage: Storage,
private storageService:StorageService,) {
this.headers = new HttpHeaders();
2021-01-13 10:02:30 +01:00
/* 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: this.headers,
};
}
2021-01-13 10:02:30 +01:00
/* getUser(){
this.storage.get('user').then((val) => {
let user = JSON.parse(unescape(atob(val))).data;
2021-01-13 10:02:30 +01:00
this.headers = this.headers.set('X-User-Id', user.userId);
this.headers = this.headers.set('X-Auth-Token', user.authToken);
2021-01-13 10:02:30 +01:00
this.options1 = {
headers: this.headers,
2021-01-07 09:39:36 +01:00
};
2021-01-13 10:02:30 +01:00
console.log(this.options1);
2021-01-07 09:39:36 +01:00
2021-01-13 10:02:30 +01:00
this.http.get(environment.apiChatUrl+'users.presence', this.options1).subscribe(res => {
console.log(res);
});
});
2021-01-13 10:02:30 +01:00
} */
2021-01-13 10:02:30 +01:00
getAllChannels(){
return this.http.get(environment.apiChatUrl+'channels.list', this.options);
}
getAllUserChannels(){
return this.http.get(environment.apiChatUrl+'channels.list.joined', this.options);
}
2021-01-13 10:02:30 +01:00
getAllRooms(){
return this.http.get(environment.apiChatUrl+'rooms.get', this.options);
}
2021-01-20 13:17:54 +01:00
customsRooms(params:any){
let opts = {
headers: this.headers,
params: params
}
return this.http.get(environment.apiChatUrl+'rooms.get', opts);
}
2021-01-13 10:02:30 +01:00
getAllPrivateGroups(){
return this.http.get(environment.apiChatUrl+'groups.list', this.options);
2020-11-03 11:52:21 +01:00
}
getAllUsers(){
2021-01-13 10:02:30 +01:00
return this.http.get(environment.apiChatUrl+'users.list', this.options);
}
2021-01-13 10:02:30 +01:00
getAllConnectedUsers(){
return this.http.get(environment.apiChatUrl+'users.presence', this.options);
2020-11-03 11:52:21 +01:00
}
2021-01-13 10:02:30 +01:00
//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);
}
getAllDirectMessages(){
return this.http.get(environment.apiChatUrl+'im.list', this.options);
}
2021-01-13 10:02:30 +01:00
//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, this.options);
}
createRoom(body:any){
return this.http.post(environment.apiChatUrl+'im.create', body, this.options);
}
getDirectMessage(roomId:string){
let params = new HttpParams();
params = params.set("roomId", roomId);
let opts = {
headers: this.headers,
params: params
}
return this.http.get(environment.apiChatUrl+'im.messages', opts);
}
/* GROUPS */
2021-01-21 16:27:04 +01:00
addGroup(body:any){
return this.http.post(environment.apiChatUrl+'groups.create', body, this.options);
}
getGroupMembers(roomId:string){
let params = new HttpParams();
let url=environment.apiChatUrl+'groups.members';
params = params.set("roomId", roomId);
let opts = {
headers: this.headers,
params: params
}
return this.http.get(url, opts);
}
getChannelMembers(roomId:string){
let params = new HttpParams();
let url=environment.apiChatUrl+'channels.members';
params = params.set("roomId", roomId);
2021-01-13 10:02:30 +01:00
let opts = {
headers: this.headers,
params: params
}
return this.http.get(url, opts);
}
2021-01-20 10:23:59 +01:00
/* GROUP MESSAGES */
getPrivateGroupMessages(roomId:any){
let params = new HttpParams();
params = params.set("roomId", roomId);
let opts = {
headers: this.headers,
params: params
}
return this.http.get(environment.apiChatUrl+'groups.history', opts);
}
getPublicGroupMessages(roomId:any){
let params = new HttpParams();
params = params.set("roomId", roomId);
let opts = {
headers: this.headers,
params: params
}
return this.http.get(environment.apiChatUrl+'channels.history', opts);
}
2021-01-20 10:23:59 +01:00
closeGroup(body:any){
return this.http.post(environment.apiChatUrl+'groups.close', body, this.options);
}
closeChannel(body:any){
return this.http.post(environment.apiChatUrl+'channels.close', body, this.options);
}
deleteGroup(body:any){
return this.http.post(environment.apiChatUrl+'groups.delete', body, this.options);
}
deleteChannel(body:any){
return this.http.post(environment.apiChatUrl+'channels.delete', body, this.options);
}
2021-01-22 15:28:52 +01:00
addUserToGroup(body:any){
return this.http.post(environment.apiChatUrl+'groups.invite', body, this.options);
}
getGroupInfo(roomId:any){
let params = new HttpParams();
params = params.set("roomId", roomId);
let opts = {
headers: this.headers,
params: params
}
return this.http.get(environment.apiChatUrl+'groups.info', opts);
}
}