Skip to content

Commit

Permalink
ov-components: Moved recording and broadcasting middleware logic
Browse files Browse the repository at this point in the history
  • Loading branch information
CSantosM committed Jan 7, 2025
1 parent c11d5b1 commit 6096b57
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 47 deletions.
28 changes: 28 additions & 0 deletions backend/src/middlewares/broadcasting.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Request, Response, NextFunction } from 'express';
import { GlobalPreferencesService } from '../services/global-preferences.service.js';
import { RoomPreferences } from '@openvidu/call-common-types';
import { LoggerService } from '../services/logger.service.js';

export const withBroadcastingEnabled = async (req: Request, res: Response, next: NextFunction) => {
try {
const preferences = await GlobalPreferencesService.getInstance().getRoomPreferences();

if (preferences) {
const { broadcastingPreferences } = preferences.value as RoomPreferences;

if (!broadcastingPreferences.enabled) {
return res.status(403).json({ message: 'Broadcasting is disabled in this room.' });
}

return next();
}

LoggerService.getInstance().error(
'No room preferences found checking broadcasting preferences. Refusing access.'
);
return res.status(403).json({ message: 'Broadcasting is disabled in this room.' });
} catch (error) {
LoggerService.getInstance().error('Error checking broadcasting preferences:' + error);
return res.status(403).json({ message: 'Broadcasting is disabled in this room.' });
}
};
4 changes: 3 additions & 1 deletion backend/src/middlewares/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './auth.middleware.js';
export * from './auth.middleware.js';
export * from './recording.middleware.js';
export * from './broadcasting.middleware.js';
26 changes: 26 additions & 0 deletions backend/src/middlewares/recording.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Request, Response, NextFunction } from 'express';
import { GlobalPreferencesService } from '../services/global-preferences.service.js';
import { RoomPreferences } from '@openvidu/call-common-types';
import { LoggerService } from '../services/logger.service.js';

export const withRecordingEnabled = async (req: Request, res: Response, next: NextFunction) => {
try {
const preferences = await GlobalPreferencesService.getInstance().getRoomPreferences();

if (preferences) {
const { recordingPreferences } = preferences.value as RoomPreferences;

if (!recordingPreferences.enabled) {
return res.status(403).json({ message: 'Recording is disabled in this room.' });
}

return next();
}

LoggerService.getInstance().error('No room preferences found checking recording preferences. Refusing access.');
return res.status(403).json({ message: 'Recording is disabled in this room.' });
} catch (error) {
LoggerService.getInstance().error('Error checking recording preferences:' + error);
return res.status(403).json({ message: 'Recording is disabled in this room.' });
}
};
4 changes: 3 additions & 1 deletion backend/src/routes/api.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import * as authCtrl from '../controllers/auth.controller.js';
import { getConfig } from '../controllers/global-preferences/global-preferences.controller.js';
import { healthCheck } from '../controllers/healthcheck.controller.js';
import { withAdminAndUserBasicAuth, withAdminBasicAuth, withUserBasicAuth } from '../middlewares/auth.middleware.js';
import { withBroadcastingEnabled, withRecordingEnabled } from '../services/global-preferences.service.js';
import { withRecordingEnabled } from '../middlewares/recording.middleware.js';
import { withBroadcastingEnabled } from '../middlewares/broadcasting.middleware.js';

import {
getRoomPreferences,
updateRoomPreferences
Expand Down
45 changes: 0 additions & 45 deletions backend/src/services/global-preferences.service.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,6 @@
import { RoomPreferences } from '@openvidu/call-common-types';
import { LoggerService } from './logger.service.js';
import { GlobalPreferencesModel } from '../models/global-preferences.model.js';
import { Request, Response, NextFunction } from 'express';

export const withRecordingEnabled = async (req: Request, res: Response, next: NextFunction) => {
try {
const preferences = await GlobalPreferencesService.getInstance().getRoomPreferences();

if (preferences) {
const { recordingPreferences } = preferences.value as RoomPreferences;

if (!recordingPreferences.enabled) {
return res.status(403).json({ message: 'Recording is disabled in this room.' });
}

return next();
}

LoggerService.getInstance().error('No room preferences found checking recording preferences. Refusing access.');
return res.status(403).json({ message: 'Recording is disabled in this room.' });
} catch (error) {
LoggerService.getInstance().error('Error checking recording preferences:' + error);
return res.status(403).json({ message: 'Recording is disabled in this room.' });
}
};

export const withBroadcastingEnabled = async (req: Request, res: Response, next: NextFunction) => {
try {
const preferences = await GlobalPreferencesService.getInstance().getRoomPreferences();

if (preferences) {
const { broadcastingPreferences } = preferences.value as RoomPreferences;

if (!broadcastingPreferences.enabled) {
return res.status(403).json({ message: 'Broadcasting is disabled in this room.' });
}

return next();
}

LoggerService.getInstance().error('No room preferences found checking broadcasting preferences. Refusing access.');
return res.status(403).json({ message: 'Broadcasting is disabled in this room.' });
} catch (error) {
LoggerService.getInstance().error('Error checking broadcasting preferences:' + error);
return res.status(403).json({ message: 'Broadcasting is disabled in this room.' });
}
};

export class GlobalPreferencesService {
protected logger = LoggerService.getInstance();
Expand Down

0 comments on commit 6096b57

Please sign in to comment.