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

390 lines
11 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 { HttpService } from './http.service';
import { StorageService } from './storage.service';
2022-07-21 18:05:29 +01:00
import { HttpClient } 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-05-09 15:01:30 +01:00
import { SessionStore } from '../store/session.service';
2022-07-21 18:05:29 +01:00
import { ChangeProfileService } from 'src/app/services/change-profile.service';
@Injectable({
providedIn: 'root'
})
export class ChatService {
headers: HttpHeaders;
2022-06-09 10:53:25 +01:00
options: any;
options1: any;
X_User_Id: any;
X_Auth_Token: any;
loggedUserChat: any;
2021-07-26 10:52:14 +01:00
bindOnMessage: any;
constructor(
2022-06-09 10:53:25 +01:00
private http: HttpClient,
private httpService: HttpService,
private storage: Storage,
2022-06-09 10:53:25 +01:00
private storageService: StorageService,
2022-07-21 18:05:29 +01:00
public p: PermissionService,
private changeProfileService: ChangeProfileService,) {
2022-04-07 14:40:37 +01:00
2022-07-21 18:05:29 +01:00
this.changeProfileService.registerCallback(() => {
this.setheader();
})
2021-07-26 09:44:11 +01:00
2022-04-07 14:40:37 +01:00
}
2022-06-09 10:53:25 +01:00
getDocumentDetails(url: string) {
let headersc = new HttpHeaders();
2022-07-21 18:05:29 +01:00
headersc = headersc.set('X-User-Id', SessionStore.user.ChatData.data.userId);
headersc = headersc.set('X-Auth-Token', SessionStore.user.ChatData.data.authToken);
2022-06-09 10:53:25 +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');
// headersc = headersc.set("Host", "www.tabularium.pt");
// headersc = headersc.set("Origin", "http://localhost:8100");
headersc = headersc.set('Referer', 'http://localhost:8100/');
let optionsc = {
headers: headersc,
withCredentials: true
};
// let fullUrl = "https://www.tabularium.pt/" + url;
return this.http.get(url, optionsc).subscribe(() => {
// this.fileService.viewDocumentByUrl(url)
});
}
2021-10-04 13:49:35 +01:00
2022-06-09 10:53:25 +01:00
getAllChannels() {
return this.http.get(environment.apiChatUrl + 'channels.list', this.options);
}
2021-09-28 15:23:51 +01:00
2022-06-09 10:53:25 +01:00
getAllUserChannels() {
return this.http.get(environment.apiChatUrl + 'channels.list.joined', this.options);
}
2022-06-09 10:53:25 +01:00
getAllRooms() {
return this.http.get(environment.apiChatUrl + 'rooms.get', this.options);
}
2021-09-28 15:23:51 +01:00
2022-06-09 10:53:25 +01:00
getRoomInfo(roomId: any) {
let params = new HttpParams();
params = params.set("roomId", roomId);
let opts = {
headers: this.headers,
params: params
2021-03-11 16:21:09 +01:00
}
2022-06-09 10:53:25 +01:00
return this.http.get(environment.apiChatUrl + 'rooms.info', opts);
}
2022-02-11 17:15:01 +01:00
2022-06-09 10:53:25 +01:00
customsRooms(params: any) {
let opts = {
headers: this.headers,
params: params
2020-11-03 11:52:21 +01:00
}
2022-06-09 10:53:25 +01:00
return this.http.get(environment.apiChatUrl + 'rooms.get', opts);
}
getAllPrivateGroups() {
return this.http.get(environment.apiChatUrl + 'groups.list', this.options);
}
2020-11-03 11:52:21 +01:00
2022-06-09 10:53:25 +01:00
getAllUsers() {
2022-06-29 15:51:28 +01:00
// console.log(this.options)
2022-06-09 10:53:25 +01:00
return this.http.get(environment.apiChatUrl + 'users.list', this.options);
}
getAllConnectedUsers() {
return this.http.get(environment.apiChatUrl + 'users.presence', this.options);
}
2021-01-13 10:02:30 +01:00
2022-06-09 10:53:25 +01:00
//Load messages from roomId
getAllDirectMessages() {
return this.http.get(environment.apiChatUrl + 'im.list', this.options);
}
//Load messages from roomId
getRoomMessages(roomId: any) {
2021-01-13 10:02:30 +01:00
2022-06-09 10:53:25 +01:00
let params = new HttpParams();
params = params.set("roomId", roomId);
2021-07-26 09:44:11 +01:00
2022-06-09 10:53:25 +01:00
let opts = {
headers: this.headers,
params: params,
2021-01-13 10:02:30 +01:00
}
2022-06-09 10:53:25 +01:00
return this.http.get(environment.apiChatUrl + 'im.history', opts);
}
2021-01-13 10:02:30 +01:00
2022-06-09 10:53:25 +01:00
sendMessage(body: any) {
let opts = {
headers: this.headers,
2021-01-13 10:02:30 +01:00
}
2022-06-09 10:53:25 +01:00
return this.http.post(environment.apiChatUrl + 'chat.sendMessage', body, opts);
}
2021-09-28 15:23:51 +01:00
2022-06-09 10:53:25 +01:00
uploadFile(formData: any, rid: string) {
let url = environment.apiChatUrl + 'rooms.upload/' + rid;
let opts = {
headers: this.headers,
}
2022-06-09 10:53:25 +01:00
return this.http.post(url, formData, opts);
}
2022-06-09 10:53:25 +01:00
deleteMessage(body: any) {
let opts = {
headers: this.headers,
2021-09-28 15:23:51 +01:00
}
2022-06-09 10:53:25 +01:00
return this.http.post(environment.apiChatUrl + 'chat.delete', body, opts);
}
2021-09-28 15:23:51 +01:00
2022-06-09 10:53:25 +01:00
leaveRoom(body: any) {
let opts = {
headers: this.headers,
2021-04-20 00:28:16 +01:00
}
2022-06-09 10:53:25 +01:00
return this.http.post(environment.apiChatUrl + 'rooms.leave', body, opts);
}
//Load members from a chat
getMembers(roomId: any) {
2021-01-13 10:02:30 +01:00
2022-06-09 10:53:25 +01:00
let params = new HttpParams();
params = params.set("roomId", roomId);
2021-07-26 09:44:11 +01:00
2022-06-09 10:53:25 +01:00
let opts = {
headers: this.headers,
params: params
2021-01-13 10:02:30 +01:00
}
2022-06-09 10:53:25 +01:00
return this.http.get(environment.apiChatUrl + 'im.members', opts);
}
2021-08-19 18:54:50 +01:00
2022-06-09 10:53:25 +01:00
getMemberInfo(userId: string) {
let params = new HttpParams();
params = params.set("userId", userId);
2021-08-19 18:54:50 +01:00
2022-06-09 10:53:25 +01:00
let opts = {
headers: this.headers,
params: params
2021-08-19 18:54:50 +01:00
}
2022-06-09 10:53:25 +01:00
return this.http.get(environment.apiChatUrl + 'users.info', opts);
}
2021-08-19 18:54:50 +01:00
2022-06-09 10:53:25 +01:00
setUserStatus(body: any) {
let opts = {
headers: this.headers,
2021-08-23 16:31:06 +01:00
}
2022-06-09 10:53:25 +01:00
return this.http.post(environment.apiChatUrl + 'users.setStatus', body, this.options);
}
2021-08-23 16:31:06 +01:00
2022-06-09 10:53:25 +01:00
removeChatRoom(body: any) {
let opts = {
headers: this.headers,
}
2022-06-09 10:53:25 +01:00
return this.http.post(environment.apiChatUrl + 'im.delete', body, this.options);
}
2021-08-23 16:31:06 +01:00
2022-06-09 10:53:25 +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
2022-06-09 10:53:25 +01:00
let opts = {
headers: this.headers,
params: params
}
2022-06-09 10:53:25 +01:00
return this.http.get(environment.apiChatUrl + 'im.messages', opts);
}
2022-06-09 10:53:25 +01:00
/* GROUPS */
addGroup(body: any) {
return this.http.post(environment.apiChatUrl + 'groups.create', body, this.options);
}
setGroupCustomFields(body: any) {
return this.http.post(environment.apiChatUrl + 'groups.setCustomFields', body, this.options);
}
2021-08-23 16:31:06 +01:00
2022-06-09 10:53:25 +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
2022-06-09 10:53:25 +01:00
let opts = {
headers: this.headers,
params: params
}
2022-06-09 10:53:25 +01:00
return this.http.get(url, opts);
}
2021-10-27 08:45:37 +01:00
2022-06-09 10:53:25 +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
2022-06-09 10:53:25 +01:00
let opts = {
headers: this.headers,
params: params
}
2022-06-09 10:53:25 +01:00
return this.http.get(url, opts);
}
/* GROUP MESSAGES */
getPrivateGroupMessages(roomId: any) {
2022-06-09 10:53:25 +01:00
let params = new HttpParams();
params = params.set("roomId", roomId);
2021-07-26 09:44:11 +01:00
2022-06-09 10:53:25 +01:00
let opts = {
headers: this.headers,
params: params
}
2022-06-09 10:53:25 +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
2022-06-09 10:53:25 +01:00
let opts = {
headers: this.headers,
params: params
2021-01-20 10:23:59 +01:00
}
2022-06-09 10:53:25 +01:00
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,
2021-04-20 00:28:16 +01:00
}
2022-06-09 10:53:25 +01:00
return this.http.post(environment.apiChatUrl + 'channels.kick', body, opts);
}
2021-10-27 08:45:37 +01:00
2022-06-09 10:53:25 +01:00
addChannelOwner(body: any) {
return this.http.post(environment.apiChatUrl + 'channels.addOwner', body, this.options);
}
2022-01-24 19:09:26 +01:00
2022-06-09 10:53:25 +01:00
addGroupOwner(body: any) {
return this.http.post(environment.apiChatUrl + 'groups.addOwner', body, this.options);
}
2022-01-24 19:09:26 +01:00
2022-06-09 10:53:25 +01:00
deleteGroup(body: any) {
return this.http.post(environment.apiChatUrl + 'groups.delete', body, this.options);
}
2021-10-27 08:45:37 +01:00
2022-06-09 10:53:25 +01:00
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);
2021-07-26 09:44:11 +01:00
2022-06-09 10:53:25 +01:00
let opts = {
headers: this.headers,
params: params
2021-01-27 11:23:57 +01:00
}
2022-06-09 10:53:25 +01:00
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,
2021-04-20 00:28:16 +01:00
}
2022-06-09 10:53:25 +01:00
return this.http.post(environment.apiChatUrl + 'groups.kick', body, opts);
}
2022-06-09 10:53:25 +01:00
async subscribe(roomId: any) {
let params = new HttpParams();
params = params.set("roomId", roomId);
let opts = {
headers: this.headers,
params: params
}
this.http.get(environment.apiChatUrl + 'im.messages', opts).subscribe(async 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-07-26 10:52:14 +01:00
}
2022-06-09 10:53:25 +01:00
})
2021-07-26 10:52:14 +01:00
2021-08-19 18:54:50 +01:00
2022-06-09 10:53:25 +01:00
}
2021-08-19 18:54:50 +01:00
2022-06-09 10:53:25 +01:00
setheader() {
2022-06-10 13:44:48 +01:00
try {
2022-06-09 10:53:25 +01:00
if (this.p.userPermission(this.p.permissionList.Chat.access)) {
2022-06-10 13:44:48 +01:00
this.headers = new HttpHeaders();
if (this.p.userPermission(this.p.permissionList.Chat.access)) {
//
this.headers = this.headers.set('X-User-Id', SessionStore.user.ChatData.data.userId);
this.headers = this.headers.set('X-Auth-Token', SessionStore.user.ChatData.data.authToken);
this.options = {
headers: this.headers,
};
}
2022-06-09 10:53:25 +01:00
}
2022-06-10 13:44:48 +01:00
} catch (error) {
console.log(error)
2021-07-26 10:52:14 +01:00
}
2022-06-09 10:53:25 +01:00
}
2021-07-26 10:52:14 +01:00
2022-06-09 10:53:25 +01:00
refreshtoken() {
2022-07-22 15:52:10 +01:00
if(this.headers) {
this.headers = this.headers.set('Authorization', SessionStore.user.BasicAuthKey);
let options = {
headers: this.headers
};
return this.http.get(environment.apiURL + 'UserAuthentication/RegenereChatToken', options).subscribe(async res => {
let data = {
status: res['status'],
data: {
userId: res['data'].userId,
authToken: res['data'].authToken
}
2022-06-08 11:56:56 +01:00
}
2022-07-22 15:52:10 +01:00
SessionStore.user.ChatData = data
SessionStore.save()
this.setheader()
// console.log(res)
// console.log(SessionStore.user.ChatData)
});
} else {
2022-06-09 10:53:25 +01:00
this.setheader()
2022-07-22 15:52:10 +01:00
}
2022-06-09 10:53:25 +01:00
}
2022-06-08 11:56:56 +01:00
}