Skip to content

Commit

Permalink
[mirotalkc2c] - add mattermost integration
Browse files Browse the repository at this point in the history
  • Loading branch information
miroslavpejic85 committed Oct 26, 2024
1 parent 0b06734 commit a8c9830
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 5 deletions.
19 changes: 18 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,21 @@ OIDC_BASE_URL='http://localhost:8080' # https://c2c.mirotalk.com
OIDC_CLIENT_ID='ClientID'
OIDC_CLIENT_SECRET='ClientSecret'
OIDC_AUTH_REUIRED=false # set to true if authentication is required for all routes
SESSION_SECRET='mirotalk-c2c-oidc-secret'
SESSION_SECRET='mirotalk-c2c-oidc-secret'

# Mattermost Integration (optional): https://mattermost.com
# 1. Navigate to Main Menu > Integrations > Slash Commands in Mattermost.
# 2. Click on Add Slash Command and configure the following settings:
# - Title: Enter a descriptive title (e.g., `C2C Command`).
# - Command Trigger Word: Set the trigger word to `c2c`.
# - Callback URLs: Enter the URL for your Express server (e.g., `https://yourserver.com/mattermost`).
# - Request Method: Select POST.
# - Enable Autocomplete: Check the box for Autocomplete.
# - Autocomplete Description: Provide a brief description (e.g., `Get MiroTalk C2C meeting room`).
# 3. Save the slash command and copy the generated token here as MATTERMOST_TOKEN.

MATTERMOST_ENABLED=false # true or false
MATTERMOST_SERVER_URL=YourMattermostServerUrl
MATTERMOST_USERNAME=YourMattermostUsername
MATTERMOST_PASSWORD=YourMattermostPassword
MATTERMOST_TOKEN=YourMettarmostToken
79 changes: 79 additions & 0 deletions backend/mattermost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict';

const { Client4 } = require('@mattermost/client');

const { v4: uuidV4 } = require('uuid');

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;

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

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

async authenticate() {
try {
const user = await this.client.login(this.username, this.password);
log.debug('--------> Logged into Mattermost as', user.username);
} catch (error) {
log.error('Failed to log into Mattermost:', error);
}
}

setupEventHandlers() {
this.app.post('/mattermost', (req, res) => {
//
const { token, text, command, channel_id } = req.body;
if (token !== this.token) {
return res.status(403).send('Invalid token');
}

if (command.trim() === '/c2c' || text.trim() === '/c2c') {
const meetingUrl = this.getMeetingURL(req);
return res.json({
text: `Here is your meeting room: ${meetingUrl}`,
channel_id: channel_id,
});
}

return res.status(200).send('Command not recognized');
});
}

getMeetingURL(req) {
const host = req.headers.host;
const protocol = host.includes('localhost') ? 'http' : 'https';
return `${protocol}://${host}/?room=${uuidV4()}`;
}
}

module.exports = mattermost;
9 changes: 7 additions & 2 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: license.mirotalk@gmail.com 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 - miroslav.pejic.85@gmail.com
* @version 1.1.37
* @version 1.1.40
*/

require('dotenv').config();
Expand All @@ -31,6 +31,7 @@ const log = new logs('server');
const isHttps = process.env.HTTPS == 'true';
const port = process.env.PORT || 8080;
const ServerApi = require('./api');
const mattermostCli = require('./mattermost.js');
const yaml = require('js-yaml');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = yaml.load(fs.readFileSync(path.join(__dirname, '/api/swagger.yaml'), 'utf8'));
Expand Down Expand Up @@ -162,10 +163,11 @@ const channels = {};
const sockets = {};
const peers = {};

app.use(express.static(frontendDir));
app.use(cors(corsOptions));
app.use(compression());
app.use(express.json()); // Api parse body data as json
app.use(express.static(frontendDir));
app.use(express.urlencoded({ extended: false })); // Mattermost
app.use(apiBasePath + '/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); // api docs

// Logs requests
Expand All @@ -178,6 +180,9 @@ app.use((req, res, next) => {
next();
});

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

app.post('*', function (next) {
next();
});
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: license.mirotalk@gmail.com 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 - miroslav.pejic.85@gmail.com
* @version 1.1.37
* @version 1.1.40
*/

const roomId = new URLSearchParams(window.location.search).get('room');
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mirotalkc2c",
"version": "1.1.37",
"version": "1.1.40",
"description": "A free WebRTC Cam-2-Cam browser-based video calls",
"main": "server.js",
"scripts": {
Expand Down Expand Up @@ -36,6 +36,7 @@
"license": "AGPL-3.0",
"homepage": "https://github.com/miroslavpejic85/mirotalkc2c",
"dependencies": {
"@mattermost/client": "^10.0.0",
"colors": "^1.4.0",
"compression": "^1.7.4",
"cors": "^2.8.5",
Expand Down

0 comments on commit a8c9830

Please sign in to comment.