-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from Yeghro/merge
Merge miro upstream
- Loading branch information
Showing
31 changed files
with
2,773 additions
and
1,218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ We would like to extend our gratitude to the following individuals for their res | |
| Name | Contact | | ||
| ----------------- | ------------------------------- | | ||
| `Hendrik Siewert` | [email protected] | | ||
| `Caio Fook` | caio.fook@gmail.com | | ||
| `Caio Fook` | https://github.com/caiofook | | ||
| `Nishant Jain` | https://twitter.com/realArcherL | | ||
|
||
Their dedication to security has contributed to the continuous improvement of our systems, ensuring the safety and privacy of our users and data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict'; | ||
|
||
const { Client, GatewayIntentBits } = require('discord.js'); | ||
|
||
const { v4: uuidV4 } = require('uuid'); | ||
|
||
const Logger = require('./Logger'); | ||
|
||
const log = new Logger('Discord'); | ||
|
||
// Discord Bot Class Implementation | ||
class Discord { | ||
constructor(token, commands) { | ||
this.token = token; | ||
this.commands = commands; | ||
this.discordClient = new Client({ | ||
intents: [ | ||
GatewayIntentBits.Guilds, | ||
GatewayIntentBits.GuildMessages, | ||
GatewayIntentBits.MessageContent, // Make sure this is enabled in your Discord Developer Portal | ||
], | ||
}); | ||
|
||
this.setupEventHandlers(); | ||
|
||
this.discordClient.login(this.token).catch((error) => { | ||
log.error('Failed to login to Discord:', error); | ||
}); | ||
} | ||
|
||
setupEventHandlers() { | ||
this.discordClient.once('ready', () => { | ||
log.info(`Discord Bot Logged in as ${this.discordClient.user.tag}!`, '😎'); | ||
}); | ||
|
||
this.discordClient.on('error', (error) => { | ||
log.error(`Discord Client Error: ${error.message}`, { stack: error.stack }); | ||
}); | ||
|
||
this.discordClient.on('messageCreate', async (message) => { | ||
if (message.author.bot) return; | ||
|
||
for (const command of this.commands) { | ||
if (message.content.startsWith(command.name)) { | ||
switch (command.name) { | ||
case '/sfu': | ||
const roomId = this.generateMeetingRoom(command.baseUrl); | ||
await this.sendMessage(message.channel, `${command.message} ${roomId}`); | ||
break; | ||
//.... | ||
default: | ||
await this.sendMessage(message.channel, command.message); | ||
break; | ||
} | ||
break; // Exit the loop after finding the command | ||
} | ||
} | ||
}); | ||
} | ||
|
||
generateMeetingRoom(baseUrl) { | ||
const roomId = uuidV4(); | ||
return `${baseUrl}${roomId}`; | ||
} | ||
|
||
async sendMessage(channel, content) { | ||
try { | ||
await channel.send(content); | ||
} catch (error) { | ||
log.error(`Failed to send message: ${error.message}`); | ||
} | ||
} | ||
} | ||
|
||
module.exports = Discord; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
'use strict'; | ||
|
||
const { Client4 } = require('@mattermost/client'); | ||
|
||
const { v4: uuidV4 } = require('uuid'); | ||
|
||
const config = require('./config'); | ||
|
||
const Logger = require('./Logger'); | ||
|
||
const log = new Logger('Mattermost'); | ||
|
||
class Mattermost { | ||
constructor(app) { | ||
const { | ||
enabled, | ||
token, | ||
serverUrl, | ||
username, | ||
password, | ||
commands = '/sfu', | ||
texts = '/sfu', | ||
} = config.mattermost || {}; | ||
|
||
if (!enabled) return; // Check if Mattermost integration is enabled | ||
|
||
this.app = app; | ||
this.allowed = config.api.allowed && config.api.allowed.mattermost; | ||
this.token = token; | ||
this.serverUrl = serverUrl; | ||
this.username = username; | ||
this.password = password; | ||
this.commands = commands; | ||
this.texts = texts; | ||
|
||
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) => { | ||
// | ||
if (!this.allowed) { | ||
return res | ||
.status(403) | ||
.send('This endpoint has been disabled. Please contact the administrator for further information.'); | ||
} | ||
|
||
log.debug('Mattermost request received:', { header: req.header, body: req.body }); | ||
|
||
const { token, text, command, channel_id } = req.body; | ||
if (token !== this.token) { | ||
log.error('Invalid token attempt', { token }); | ||
return res.status(403).send('Invalid token'); | ||
} | ||
|
||
const payload = { text: '', channel_id }; | ||
if (this.processInput(command, payload, req) || this.processInput(text, payload, req)) { | ||
return res.json(payload); | ||
} | ||
|
||
return res.status(200).send('Command not recognized'); | ||
}); | ||
} | ||
|
||
processInput(input, payload, req) { | ||
for (const cmd of [...this.commands, ...this.texts]) { | ||
if (input.trim() === cmd.name) { | ||
switch (cmd.name) { | ||
case '/sfu': | ||
payload.text = `${cmd.message} ${this.getMeetingURL(req)}`; | ||
break; | ||
default: | ||
break; | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
getMeetingURL(req) { | ||
const host = req.headers.host; | ||
const protocol = host.includes('localhost') ? 'http' : 'https'; | ||
return `${protocol}://${host}/join/${uuidV4()}`; | ||
} | ||
} | ||
|
||
module.exports = Mattermost; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.