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; options1:any; 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(); /* 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, }; } /* 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); }); }); } */ 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); } getRoomInfo(roomId:any){ let params = new HttpParams(); params = params.set("roomId", roomId); let opts = { headers: this.headers, params: params } return this.http.get(environment.apiChatUrl+'rooms.info', opts); } customsRooms(params:any){ let opts = { headers: this.headers, params: params } return this.http.get(environment.apiChatUrl+'rooms.get', opts); } getAllPrivateGroups(){ 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); } //Load messages from roomId getAllDirectMessages(){ console.log(this.options); 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); } leaveRoom(body:any){ let opts = { headers: this.headers, } return this.http.post(environment.apiChatUrl+'rooms.leave', 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 */ 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); let opts = { headers: this.headers, params: params } return this.http.get(url, opts); } /* 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); } 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); } leaveGroup(body:any){ return this.http.post(environment.apiChatUrl+'groups.leave', body, this.options); } leaveChannel(body:any){ return this.http.post(environment.apiChatUrl+'channels.leave', body, this.options); } removeChannelMember(body:any){ let opts = { headers: this.headers, } return this.http.post(environment.apiChatUrl+'channels.kick', body, opts); } 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); } 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); } renameGroup(body:any){ return this.http.post(environment.apiChatUrl+'groups.rename', body, this.options); } removeGroupMember(body:any){ let opts = { headers: this.headers, } return this.http.post(environment.apiChatUrl+'groups.kick', body, opts); } }