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

359 lines
10 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 { 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';
2022-04-07 14:24:07 +01:00
import { PermissionService } from './permission.service';
2022-03-10 23:08:29 +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';
2022-01-12 09:29:48 +01:00
//public messages: Subject<any>;
2021-06-04 11:37:56 +01:00
loggedUserChat:any;
2021-07-26 10:52:14 +01:00
bindOnMessage: any;
constructor(
private http:HttpClient,
private httpService: HttpService,
private authService: AuthService,
private storage: Storage,
2021-05-18 14:37:43 +01:00
private storageService:StorageService,
2022-04-07 14:40:37 +01:00
public p: PermissionService) {
2022-04-07 14:21:43 +01:00
if(this.p.userPermission(this.p.permissionList.Chat.access)){
2022-02-24 12:10:09 +01:00
this.loggedUserChat = authService.ValidatedUserChat;
this.headers = new HttpHeaders();
2022-04-07 14:24:07 +01:00
2022-04-07 14:40:37 +01:00
2022-04-07 14:24:07 +01:00
if(this.p.userPermission(this.p.permissionList.Chat.access)) {
2022-04-28 09:32:27 +01:00
//
2022-04-16 19:23:44 +01:00
this.headers = this.headers.set('X-User-Id', this.loggedUserChat.userId);
this.headers = this.headers.set('X-Auth-Token', this.loggedUserChat.authToken);
2022-04-07 14:24:07 +01:00
this.options = {
headers: this.headers,
};
}
2021-05-18 14:37:43 +01:00
2021-01-13 10:02:30 +01:00
}
2021-07-26 09:44:11 +01:00
2022-04-07 14:40:37 +01:00
}
2021-10-04 13:49:35 +01:00
getDocumentDetails(url:string){
let headersc = new HttpHeaders();
headersc = headersc.set('X-User-Id', this.loggedUserChat['data'].userId);
headersc = headersc.set('X-Auth-Token', this.loggedUserChat['data'].authToken);
2021-10-19 14:31:14 +01:00
headersc = headersc.set('Sec-Fetch-Dest', 'attachment');
headersc = headersc.set('Sec-Fetch-Mode', 'navigate');
headersc = headersc.set('Cookie', 'rc_uid=fsMwcNdufWvdnChj7');
headersc = headersc.set('Cookie', 'rc_token=MLbhikLQI4xo9_vL43HqheKPPbxjag7hKfwxe9AjcvY');
2021-10-04 13:49:35 +01:00
// headersc = headersc.set("Host", "www.tabularium.pt");
// headersc = headersc.set("Origin", "http://localhost:8100");
2021-10-19 14:31:14 +01:00
headersc = headersc.set('Referer', 'http://localhost:8100/');
2021-10-04 13:49:35 +01:00
let optionsc = {
headers: headersc,
2021-10-19 14:31:14 +01:00
withCredentials: true
2021-10-04 13:49:35 +01:00
};
2021-10-19 14:31:14 +01:00
//let fullUrl = "https://www.tabularium.pt/" + url;
return this.http.get(url, optionsc).subscribe(()=>{
2021-10-05 10:18:38 +01:00
//this.fileService.viewDocumentByUrl(url)
2021-10-04 13:49:35 +01:00
});
}
2021-01-13 10:02:30 +01:00
getAllChannels(){
return this.http.get(environment.apiChatUrl+'channels.list', this.options);
}
2021-09-28 15:23:51 +01:00
2021-01-13 10:02:30 +01:00
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-09-28 15:23:51 +01:00
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);
}
2022-02-11 17:15:01 +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);
}
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
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,
2021-09-01 17:14:57 +01:00
params: params,
2021-01-13 10:02:30 +01:00
}
return this.http.get(environment.apiChatUrl+'im.history', opts);
}
2022-03-15 15:49:59 +01:00
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-09-28 15:23:51 +01:00
uploadFile(formData:any, rid:string){
let url = environment.apiChatUrl+'rooms.upload/'+rid;
let opts = {
headers: this.headers,
}
return this.http.post(url, formData, opts);
}
2021-09-28 15:23:51 +01:00
deleteMessage(body:any){
let opts = {
headers: this.headers,
}
return this.http.post(environment.apiChatUrl+'chat.delete', 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-08-23 16:31:06 +01:00
setUserStatus(body:any){
let opts = {
headers: this.headers,
}
return this.http.post(environment.apiChatUrl+'users.setStatus', body, this.options);
}
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.delete', body, this.options);
}
2021-08-23 16:31:06 +01:00
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);
}
/* GROUPS */
2021-01-21 16:27:04 +01:00
addGroup(body:any){
2022-04-28 09:32:27 +01:00
2021-01-21 16:27:04 +01:00
return this.http.post(environment.apiChatUrl+'groups.create', body, this.options);
}
2021-10-27 08:45:37 +01:00
setGroupCustomFields(body:any){
return this.http.post(environment.apiChatUrl+'groups.setCustomFields', body, this.options);
}
2021-08-23 16:31:06 +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
}
return this.http.get(url, opts);
}
2021-10-27 08:45:37 +01:00
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
}
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);
2021-07-26 09:44:11 +01:00
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);
2021-07-26 09:44:11 +01:00
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);
}
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-10-27 08:45:37 +01:00
2022-01-24 19:09:26 +01:00
addChannelOwner(body:any){
return this.http.post(environment.apiChatUrl+'channels.addOwner', body, this.options);
}
addGroupOwner(body:any){
return this.http.post(environment.apiChatUrl+'groups.addOwner', body, this.options);
}
deleteGroup(body:any){
return this.http.post(environment.apiChatUrl+'groups.delete', body, this.options);
}
2021-10-27 08:45:37 +01:00
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-07-26 10:52:14 +01:00
async subscribe(roomId:any) {
2022-04-28 09:32:27 +01:00
2021-07-26 10:52:14 +01:00
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 => {
2022-04-28 09:32:27 +01:00
2021-07-26 10:52:14 +01:00
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
}
}