cloud_bbb/ts/Common/Api.ts

308 lines
7.2 KiB
TypeScript
Raw Permalink Normal View History

2020-04-26 11:36:41 +02:00
import axios from '@nextcloud/axios';
export enum ShareType {
User = 0, // OC.Share.SHARE_TYPE_USER
Group = 1, // OC.Share.SHARE_TYPE_GROUP
Circle = 7, // OC.Share.SHARE_TYPE_CIRCLE
2020-09-01 09:48:24 +02:00
}
2020-09-01 09:48:24 +02:00
export enum Permission { Admin, Moderator, User }
export enum Access {
Public = 'public',
Password = 'password',
WaitingRoom = 'waiting_room',
WaitingRoomAll = 'waiting_room_all',
Internal = 'internal',
InternalRestricted = 'internal_restricted',
}
export interface Restriction {
id: number;
groupId: string;
maxRooms: number;
roomTypes: string[];
maxParticipants: number;
allowRecording: boolean;
}
2020-04-26 11:36:41 +02:00
export interface Room {
2020-04-26 22:41:06 +02:00
id: number;
uid: string;
2020-06-17 10:56:28 +02:00
userId: string;
2020-04-26 22:41:06 +02:00
name: string;
welcome: string;
maxParticipants: number;
record: boolean;
access: Access;
password?: string;
everyoneIsModerator: boolean;
requireModerator: boolean;
2020-09-08 17:01:50 +02:00
shared: boolean;
2021-01-22 19:12:55 +01:00
moderatorToken: string;
listenOnly: boolean,
mediaCheck: boolean,
cleanLayout: boolean,
joinMuted: boolean,
running: boolean,
2020-04-26 11:36:41 +02:00
}
export interface RoomShare {
id: number;
roomId: number;
shareType: ShareType;
shareWith: string;
shareWithDisplayName?: string;
permission: Permission;
}
export type Recording = {
id: string;
name: string;
published: boolean;
state: 'processing' | 'processed' | 'published' | 'unpublished' | 'deleted';
startTime: number;
participants: number;
type: string;
length: number;
url: string;
meta: any;
}
export interface ShareWithOption {
label: string;
value: {
shareType: ShareType;
shareWith: string;
};
}
export interface ShareWith {
users: ShareWithOption[];
groups: ShareWithOption[];
circles: ShareWithOption[];
exact: {
users: ShareWithOption[];
groups: ShareWithOption[];
circles: ShareWithOption[];
}
}
2020-04-26 11:36:41 +02:00
class Api {
public getUrl(endpoint: string): string {
2020-04-26 13:26:34 +02:00
return OC.generateUrl(`apps/bbb/${endpoint}`);
2020-04-26 11:36:41 +02:00
}
public async getRestriction(): Promise<Restriction> {
const response = await axios.get(this.getUrl('restrictions/user'));
return response.data;
}
public async getRestrictions(): Promise<Restriction[]> {
const response = await axios.get(this.getUrl('restrictions'));
return response.data;
}
public async createRestriction(groupId: string) {
const response = await axios.post(this.getUrl('restrictions'), {
groupId,
});
return response.data;
}
public async updateRestriction(restriction: Restriction) {
if (!restriction.id) {
const newRestriction = await this.createRestriction(
restriction.groupId
);
restriction.id = newRestriction.id;
}
const response = await axios.put(this.getUrl(`restrictions/${restriction.id}`), restriction);
return response.data;
}
public async deleteRestriction(id: number) {
const response = await axios.delete(this.getUrl(`restrictions/${id}`));
return response.data;
}
public async isRunning(uid: string): Promise<boolean> {
const response = await axios.get(this.getUrl(`server/${uid}/isRunning`));
return response.data;
}
public async insertDocument(uid: string, url: string, filename: string): Promise<boolean> {
const response = await axios.post(this.getUrl(`server/${uid}/insertDocument`), { url, filename });
return response.data;
}
2021-01-22 19:12:55 +01:00
public getRoomUrl(room: Room, forModerator = false) {
const shortener = document.getElementById('bbb-root')?.getAttribute('data-shortener') || '';
2021-01-22 19:12:55 +01:00
const token = (forModerator && room.moderatorToken) ? `${room.uid}/${room.moderatorToken}` : room.uid;
if (shortener) {
2021-01-22 19:12:55 +01:00
return shortener
.replace(/\{user\}/g, room.userId)
.replace(/\{token\}/g, token);
}
2021-01-22 19:12:55 +01:00
return window.location.origin + api.getUrl(`b/${token}`);
}
2020-04-26 11:36:41 +02:00
public async getRooms(): Promise<Room[]> {
const response = await axios.get(this.getUrl('rooms'));
return response.data;
}
public async createRoom(name: string, access: Access = Access.Public, maxParticipants = 0) {
2020-04-26 11:36:41 +02:00
const response = await axios.post(this.getUrl('rooms'), {
name,
welcome: '',
maxParticipants,
2020-04-26 11:36:41 +02:00
record: false,
access,
2020-04-26 11:36:41 +02:00
});
return response.data;
}
public async updateRoom(room: Room) {
const response = await axios.put(this.getUrl(`rooms/${room.id}`), room);
return response.data;
}
public async deleteRoom(id: number) {
const response = await axios.delete(this.getUrl(`rooms/${id}`));
2020-04-26 11:36:41 +02:00
return response.data;
}
public async getRecordings(uid: string) {
const response = await axios.get(this.getUrl(`server/${uid}/records`));
return response.data;
}
public async deleteRecording(id: string) {
const response = await axios.delete(this.getUrl(`server/record/${id}`));
return response.data;
}
public async storeRecording(recording: Recording, path: string) {
const startDate = new Date(recording.startTime);
const filename = `${encodeURIComponent(recording.name + ' ' + startDate.toISOString())}.url`;
2020-11-19 16:21:55 +01:00
const url = OC.linkToRemote(`dav/files/${OC.currentUser}${path}/${filename}`);
await axios.put(url, `[InternetShortcut]\nURL=${recording.url}`);
return filename;
}
public async storeRoom(room: Room, path: string) {
const filename = `${encodeURIComponent(room.name)}.url`;
2020-11-19 16:21:55 +01:00
const url = OC.linkToRemote(`dav/files/${OC.currentUser}${path}/${filename}`);
await axios.put(url, `[InternetShortcut]\nURL=${this.getRoomUrl(room)}`);
return filename;
}
public async checkServer(url: string, secret: string): Promise<'success' | 'invalid-url' | 'invalid:secret'> {
const response = await axios.post(this.getUrl('server/check'), {
url,
secret,
});
return response.data;
}
public async getRoomShares(roomId: number): Promise<RoomShare[]> {
const response = await axios.get(this.getUrl('roomShares'), {
params: {
id: roomId,
},
});
return response.data;
}
public async createRoomShare(roomId: number, shareType: ShareType, shareWith: string, permission: Permission): Promise<RoomShare> {
const response = await axios.post(this.getUrl('roomShares'), {
roomId,
shareType,
shareWith,
permission,
});
return response.data;
}
public async deleteRoomShare(id: number) {
const response = await axios.delete(this.getUrl(`roomShares/${id}`));
return response.data;
}
public async getRecommendedShareWith(shareType: ShareType[] = [OC.Share.SHARE_TYPE_USER, OC.Share.SHARE_TYPE_GROUP]): Promise<ShareWith> {
const url = OC.linkToOCS('apps/files_sharing/api/v1', 1) + 'sharees_recommended';
const response = await axios.get(url, {
params: {
shareType,
itemType: 'room',
format: 'json',
},
});
return {
users: [],
groups: [],
circles: [],
exact: {
users: response.data.ocs.data.exact.users,
groups: response.data.ocs.data.exact.groups,
circles: response.data.ocs.data.exact.circles || [],
},
};
}
public async searchShareWith(search = '', shareType: ShareType[] = [OC.Share.SHARE_TYPE_USER, OC.Share.SHARE_TYPE_GROUP]): Promise<ShareWith> {
const url = OC.linkToOCS('apps/files_sharing/api/v1', 1) + 'sharees';
const response = await axios.get(url, {
params: {
search,
shareType,
itemType: 'room',
format: 'json',
lookup: false,
},
});
const data = response.data.ocs.data;
return {
users: data.users,
groups: data.groups,
circles: data.circles || [],
exact: {
users: data.exact.users,
groups: data.exact.groups,
circles: data.exact.circles || [],
},
};
}
2020-04-26 11:36:41 +02:00
}
export const api = new Api();