Skip to content

Commit

Permalink
[mirotalkc2c] - refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
miroslavpejic85 committed Oct 27, 2024
1 parent e0a08f9 commit c8cc67d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 54 deletions.
32 changes: 10 additions & 22 deletions backend/mattermost.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,25 @@ const Logger = require('./logs');
const log = new Logger('Mattermost');

class mattermost {
constructor(app) {
const {
MATTERMOST_ENABLED,
MATTERMOST_TOKEN,
MATTERMOST_SERVER_URL,
MATTERMOST_USERNAME,
MATTERMOST_PASSWORD,
} = process.env;

log.debug('Mattermost config', {
enabled: MATTERMOST_ENABLED,
token: MATTERMOST_TOKEN,
server: MATTERMOST_SERVER_URL,
username: MATTERMOST_USERNAME,
password: MATTERMOST_PASSWORD,
});

if (MATTERMOST_ENABLED !== 'true') return;
constructor(app, mattermostCfg) {
if (!this.isConfigValid(mattermostCfg)) return;

this.app = app;
this.token = MATTERMOST_TOKEN;
this.serverUrl = MATTERMOST_SERVER_URL;
this.username = MATTERMOST_USERNAME;
this.password = MATTERMOST_PASSWORD;
this.token = mattermostCfg.token;
this.serverUrl = mattermostCfg.server_url;
this.username = mattermostCfg.username;
this.password = mattermostCfg.password;

this.client = new Client4();
this.client.setUrl(this.serverUrl);
this.authenticate();
this.setupEventHandlers();
}

isConfigValid(config) {
return config.enabled && config.server_url && config.token && config.username && config.password;
}

async authenticate() {
try {
const user = await this.client.login(this.username, this.password);
Expand Down
73 changes: 44 additions & 29 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license For private project or commercial purposes contact us at: [email protected] or purchase it directly via Code Canyon:
* @license https://codecanyon.net/item/mirotalk-c2c-webrtc-real-time-cam-2-cam-video-conferences-and-screen-sharing/43383005
* @author Miroslav Pejic - [email protected]
* @version 1.1.40
* @version 1.1.41
*/

require('dotenv').config();
Expand Down Expand Up @@ -122,6 +122,14 @@ if (turnServerEnabled && turnServerUrl && turnServerUsername && turnServerCreden
iceServers.push({ urls: turnServerUrl, username: turnServerUsername, credential: turnServerCredential });
}

const mattermostCfg = {
enabled: getEnvBoolean(process.env.MATTERMOST_ENABLED),
server_url: process.env.MATTERMOST_SERVER_URL,
username: process.env.MATTERMOST_USERNAME,
password: process.env.MATTERMOST_PASSWORD,
token: process.env.MATTERMOST_TOKEN,
};

const surveyURL = process.env.SURVEY_URL || false;
const redirectURL = process.env.REDIRECT_URL || false;

Expand Down Expand Up @@ -181,7 +189,7 @@ app.use((req, res, next) => {
});

// Mattermost
const mattermost = new mattermostCli(app);
const mattermost = new mattermostCli(app, mattermostCfg);

app.post('*', function (next) {
next();
Expand Down Expand Up @@ -304,26 +312,46 @@ function getEnvBoolean(key, force_true_if_undefined = false) {
return key == 'true' ? true : false;
}

function getServerConfig(tunnelHttps = false) {
// configurations
const server = {
home: host,
room: host + queryRoom,
join: host + queryJoin,
};

const server_tunnel = tunnelHttps
? {
ngrokHome: tunnelHttps,
ngrokRoom: tunnelHttps + queryRoom,
ngrokJoin: tunnelHttps + queryJoin,
ngrokToken: ngrokAuthToken,
}
: false;

return {
server: server,
server_tunnel: server_tunnel,
oidc: OIDC.enabled ? OIDC : false,
iceServers: iceServers,
cors: corsOptions,
apiDocs: apiDocs,
apiKeySecret: apiKeySecret,
mattermost: mattermostCfg.enabled ? mattermostCfg : false,
redirectURL: redirectURL,
app_version: packageJson.version,
nodeVersion: process.versions.node,
};
}

async function ngrokStart() {
try {
await ngrok.authtoken(ngrokAuthToken);
await ngrok.connect(port);
const api = ngrok.getApi();
const list = await api.listTunnels();
const tunnelHttps = list.tunnels[0].public_url;
log.debug('settings', {
ngrokAuthToken: ngrokAuthToken,
iceServers: iceServers,
cors: corsOptions,
ngrokHome: tunnelHttps,
ngrokRoom: tunnelHttps + queryRoom,
ngrokJoin: tunnelHttps + queryJoin,
apiDocs: apiDocs,
apiKeySecret: apiKeySecret,
redirectURL: redirectURL,
nodeVersion: process.versions.node,
app_version: packageJson.version,
});
log.debug('settings', getServerConfig(tunnelHttps));
} catch (err) {
log.warn('[Error] ngrokStart', err);
process.exit(1);
Expand All @@ -334,20 +362,7 @@ server.listen(port, null, () => {
if (!isHttps && ngrokEnabled && ngrokAuthToken) {
ngrokStart();
} else {
log.debug('settings', {
oidc: OIDC.enabled ? OIDC : false,
iceServers: iceServers,
cors: corsOptions,
home: host,
room: host + queryRoom,
join: host + queryJoin,
apiDocs: apiDocs,
apiKeySecret: apiKeySecret,
redirectURL: redirectURL,
surveyURL: surveyURL,
nodeVersion: process.versions.node,
app_version: packageJson.version,
});
log.debug('settings', getServerConfig());
}
});

Expand Down
2 changes: 1 addition & 1 deletion frontend/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @license For private project or commercial purposes contact us at: [email protected] or purchase it directly via Code Canyon:
* @license https://codecanyon.net/item/mirotalk-c2c-webrtc-real-time-cam-2-cam-video-conferences-and-screen-sharing/43383005
* @author Miroslav Pejic - [email protected]
* @version 1.1.40
* @version 1.1.41
*/

const roomId = new URLSearchParams(window.location.search).get('room');
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mirotalkc2c",
"version": "1.1.40",
"version": "1.1.41",
"description": "A free WebRTC Cam-2-Cam browser-based video calls",
"main": "server.js",
"scripts": {
Expand Down Expand Up @@ -48,7 +48,7 @@
"nodemailer": "^6.9.15",
"socket.io": "^4.8.1",
"swagger-ui-express": "5.0.1",
"uuid": "10.0.0",
"uuid": "11.0.0",
"xss": "^1.0.15"
},
"devDependencies": {
Expand Down

0 comments on commit c8cc67d

Please sign in to comment.