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

295 lines
8.6 KiB
TypeScript
Raw Normal View History

2021-01-13 10:02:30 +01:00
import { HttpHeaders, HttpParams } from '@angular/common/http';
2020-10-30 15:22:35 +01:00
import { Injectable } from '@angular/core';
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';
2020-12-21 16:37:44 +01:00
import { Storage } from '@ionic/storage';
2021-05-18 14:37:43 +01:00
import { Message } from 'src/app/models/message.model';
import { Observable, Subject } from "rxjs/Rx";
import { WebsocketService } from './websocket.service';
2020-10-30 15:22:35 +01:00
@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;
2021-05-18 14:37:43 +01:00
2021-07-26 09:44:11 +01:00
//SERVER_URL = 'wss://www.tabularium.pt/websocket';
//public messages: Subject<any>;
2021-06-04 11:37:56 +01:00
loggedUserChat:any;
2021-07-26 10:52:14 +01:00
bindOnMessage: any;
2020-10-30 15:22:35 +01:00
constructor(
private http:HttpClient,
private httpService: HttpService,
private authService: AuthService,
2020-12-21 16:37:44 +01:00
private storage: Storage,
2021-05-18 14:37:43 +01:00
private storageService:StorageService,
2021-07-26 09:44:11 +01:00
//private wsService: WebsocketService,
2021-05-18 14:37:43 +01:00
)
{
2021-06-04 11:37:56 +01:00
this.loggedUserChat = authService.ValidatedUserChat;
2020-10-30 15:22:35 +01:00
this.headers = new HttpHeaders();
2021-06-04 11:37:56 +01:00
this.headers = this.headers.set('X-User-Id', this.loggedUserChat['data'].userId);
this.headers = this.headers.set('X-Auth-Token', this.loggedUserChat['data'].authToken);
2021-01-13 10:02:30 +01:00
this.options = {
headers: this.headers,
};
2021-05-18 14:37:43 +01:00
2021-07-26 09:44:11 +01:00
/* this.messages = <Subject<any>>this.wsService.connect(this.SERVER_URL).map((response: MessageEvent): any => {
2021-05-18 14:37:43 +01:00
let data = JSON.parse(response.data);
console.log(data);
2021-05-19 09:37:43 +01:00
return (JSON.stringify(data));
2021-07-26 09:44:11 +01:00
}); */
2021-01-13 10:02:30 +01:00
}
2021-07-26 09:44:11 +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);
}
2020-12-21 16:37:44 +01:00
2021-01-13 10:02:30 +01:00
getAllRooms(){
return this.http.get(environment.apiChatUrl+'rooms.get', this.options);
}
2021-03-11 16:21:09 +01:00
getRoomInfo(roomId:any){
let params = new HttpParams();
params = params.set("roomId", roomId);
2021-07-26 09:44:11 +01:00
let opts = {
headers: this.headers,
params: params
2021-03-11 16:21:09 +01:00
}
return this.http.get(environment.apiChatUrl+'rooms.info', opts);
2021-07-26 09:44:11 +01:00
2021-03-11 16:21:09 +01:00
}
2021-01-20 13:17:54 +01:00
customsRooms(params:any){
2021-07-26 09:44:11 +01:00
let opts = {
headers: this.headers,
params: params
2021-01-20 13:17:54 +01:00
}
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);
2020-10-30 15:22:35 +01:00
}
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
//Load messages from roomId
2021-01-19 16:10:40 +01:00
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);
2021-07-26 09:44:11 +01:00
let opts = {
headers: this.headers,
params: params
2021-01-13 10:02:30 +01:00
}
return this.http.get(environment.apiChatUrl+'im.history', opts);
}
sendMessage(body:any){
2021-07-26 09:44:11 +01:00
let opts = {
2021-01-13 10:02:30 +01:00
headers: this.headers,
}
return this.http.post(environment.apiChatUrl+'chat.sendMessage', body, opts);
}
2021-04-20 00:28:16 +01:00
leaveRoom(body:any){
2021-07-26 09:44:11 +01:00
let opts = {
2021-04-20 00:28:16 +01:00
headers: this.headers,
}
return this.http.post(environment.apiChatUrl+'rooms.leave', body, opts);
}
2021-01-13 10:02:30 +01:00
//Load members from a chat
getMembers(roomId:any){
let params = new HttpParams();
params = params.set("roomId", roomId);
2021-07-26 09:44:11 +01:00
let opts = {
headers: this.headers,
params: params
2021-01-13 10:02:30 +01:00
}
return this.http.get(environment.apiChatUrl+'im.members', opts);
}
2021-08-19 18:54:50 +01:00
getMemberInfo(userId:string){
let params = new HttpParams();
params = params.set("userId", userId);
let opts = {
headers: this.headers,
params: params
}
return this.http.get(environment.apiChatUrl+'users.info', opts);
}
2021-01-13 10:02:30 +01:00
removeChatRoom(body:any){
2021-07-26 09:44:11 +01:00
let opts = {
2021-01-13 10:02:30 +01:00
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);
2021-07-26 09:44:11 +01:00
let opts = {
headers: this.headers,
params: params
}
return this.http.get(environment.apiChatUrl+'im.messages', opts);
2020-10-30 15:22:35 +01:00
}
2021-01-19 16:10:40 +01:00
/* GROUPS */
2021-01-21 16:27:04 +01:00
addGroup(body:any){
return this.http.post(environment.apiChatUrl+'groups.create', body, this.options);
}
2021-01-19 16:10:40 +01:00
getGroupMembers(roomId:string){
let params = new HttpParams();
let url=environment.apiChatUrl+'groups.members';
params = params.set("roomId", roomId);
2021-07-26 09:44:11 +01:00
let opts = {
headers: this.headers,
params: params
2021-01-19 16:10:40 +01:00
}
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-07-26 09:44:11 +01:00
let opts = {
headers: this.headers,
params: params
2021-01-19 16:10:40 +01:00
}
return this.http.get(url, opts);
}
2021-01-20 10:23:59 +01:00
/* GROUP MESSAGES */
2021-01-19 16:10:40 +01:00
getPrivateGroupMessages(roomId:any){
let params = new HttpParams();
params = params.set("roomId", roomId);
2021-07-26 09:44:11 +01:00
let opts = {
headers: this.headers,
params: params
2021-01-19 16:10:40 +01:00
}
return this.http.get(environment.apiChatUrl+'groups.history', opts);
}
getPublicGroupMessages(roomId:any){
let params = new HttpParams();
params = params.set("roomId", roomId);
2021-07-26 09:44:11 +01:00
let opts = {
headers: this.headers,
params: params
2021-01-19 16:10:40 +01:00
}
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);
}
2021-04-20 17:18:57 +01:00
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);
}
2021-04-20 00:28:16 +01:00
removeChannelMember(body:any){
2021-07-26 09:44:11 +01:00
let opts = {
2021-04-20 00:28:16 +01:00
headers: this.headers,
}
return this.http.post(environment.apiChatUrl+'channels.kick', body, opts);
}
2021-01-20 16:58:04 +01:00
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);
2021-07-26 09:44:11 +01:00
let opts = {
headers: this.headers,
params: params
2021-01-22 15:28:52 +01:00
}
return this.http.get(environment.apiChatUrl+'groups.info', opts);
}
2021-01-27 11:23:57 +01:00
renameGroup(body:any){
return this.http.post(environment.apiChatUrl+'groups.rename', body, this.options);
}
2021-04-20 00:28:16 +01:00
removeGroupMember(body:any){
2021-07-26 09:44:11 +01:00
let opts = {
2021-04-20 00:28:16 +01:00
headers: this.headers,
}
return this.http.post(environment.apiChatUrl+'groups.kick', body, opts);
}
2021-01-19 16:10:40 +01:00
2021-07-26 10:52:14 +01:00
async subscribe(roomId:any) {
console.log('Subcrive')
let params = new HttpParams();
params = params.set("roomId", roomId);
2021-08-19 18:54:50 +01:00
let opts = {
headers: this.headers,
params: params
2021-07-26 10:52:14 +01:00
}
this.http.get(environment.apiChatUrl+'im.messages', opts).subscribe(async res => {
console.log("Subcrive", res)
if (res == 502) {
// Connection timeout
// happens when the connection was pending for too long
// let's reconnect
await this.subscribe(roomId);
} else if (res != 200) {
// Show Error
//showMessage(response.statusText);
this.getRoomMessages(roomId)
// Reconnect in one second
await new Promise(resolve => setTimeout(resolve, 1000));
await this.subscribe(roomId);
} else {
// Got message
//let message = await response.text();
this.getRoomMessages(roomId)
await this.subscribe(roomId);
}
})
2021-08-19 18:54:50 +01:00
2021-07-26 10:52:14 +01:00
}
2020-10-30 15:22:35 +01:00
}