diff --git a/README.md b/README.md index e329100..e1e4081 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,10 @@ $ docker-compose down # The response will give you a entrypoint / Room URL for your meeting. $ curl -X POST "http://localhost:3010/api/v1/meeting" -H "authorization: mirotalksfu_default_secret" -H "Content-Type: application/json" $ curl -X POST "https://sfu.mirotalk.org/api/v1/meeting" -H "authorization: mirotalksfu_default_secret" -H "Content-Type: application/json" + +# The response will give you a entrypoint / URL for the direct join. +$ curl -X POST "http://localhost:3010/api/v1/join" -H "authorization: mirotalksfu_default_secret" -H "Content-Type: application/json" --data '{"room":"test","name":"mirotalksfu","audio":"0","video":"0"}' +$ curl -X POST "https://sfu.mirotalk.org/api/v1/join" -H "authorization: mirotalksfu_default_secret" -H "Content-Type: application/json" --data '{"room":"test","name":"mirotalksfu","audio":"0","video":"0"}' ``` ## Direct Join diff --git a/app/api/README.md b/app/api/README.md index d5b890f..10d98b3 100644 --- a/app/api/README.md +++ b/app/api/README.md @@ -7,12 +7,19 @@ Create a meeting with a `HTTP request` containing the `API_KEY` sent to MiroTalk ```bash # js node meeting.js +node join.js + # php php meeting.php +php join.php + # python -python meeting.py +python3 meeting.py +python3 join.py + # bash ./meeting.sh +./join.sh ``` ## Embed a meeting diff --git a/app/api/join/join.js b/app/api/join/join.js new file mode 100644 index 0000000..1e9c0bb --- /dev/null +++ b/app/api/join/join.js @@ -0,0 +1,28 @@ +'use strict'; + +const fetch = require('node-fetch'); + +const API_KEY = 'mirotalksfu_default_secret'; +const MIROTALK_URL = 'http://localhost:3010/api/v1/join'; + +function getResponse() { + return fetch(MIROTALK_URL, { + method: 'POST', + headers: { + authorization: API_KEY, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + room: 'test', + name: 'mirotalksfu', + audio: true, + video: true, + }), + }); +} + +getResponse().then(async (res) => { + console.log('Status code:', res.status); + const data = await res.json(); + console.log('join:', data.join); +}); diff --git a/app/api/join/join.php b/app/api/join/join.php new file mode 100644 index 0000000..4ebc08f --- /dev/null +++ b/app/api/join/join.php @@ -0,0 +1,34 @@ + "test", + "name" => "mirotalksfu", + "audio" => true, + "video" => true, +); +$data_string = json_encode($data); + +curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); +$response = curl_exec($ch); +$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + +curl_close($ch); + +echo "Status code: $httpcode \n"; +$data = json_decode($response); +echo "join: ", $data->{'join'}, "\n"; diff --git a/app/api/join/join.py b/app/api/join/join.py new file mode 100644 index 0000000..ef0e3d7 --- /dev/null +++ b/app/api/join/join.py @@ -0,0 +1,27 @@ +import requests +import json + +API_KEY = "mirotalksfu_default_secret" +MIROTALK_URL = "http://localhost:3010/api/v1/join" + +headers = { + "authorization": API_KEY, + "Content-Type": "application/json", +} + +data = { + "room": "test", + "name": "mirotalksfu", + "audio": "true", + "video": "true", +} + +response = requests.post( + MIROTALK_URL, + headers=headers, + json=data, +) + +print("Status code:", response.status_code) +data = json.loads(response.text) +print("join:", data["join"]) diff --git a/app/api/join/join.sh b/app/api/join/join.sh new file mode 100755 index 0000000..86a49b5 --- /dev/null +++ b/app/api/join/join.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +API_KEY="mirotalksfu_default_secret" +MIROTALK_URL="http://localhost:3010/api/v1/join" + +curl $MIROTALK_URL \ + --header "authorization: $API_KEY" \ + --header "Content-Type: application/json" \ + --data '{"room":"test","name":"mirotalksfu","audio":"1","video":"1"}' \ + --request POST \ No newline at end of file diff --git a/app/api/meeting.js b/app/api/meeting/meeting.js similarity index 100% rename from app/api/meeting.js rename to app/api/meeting/meeting.js diff --git a/app/api/meeting.php b/app/api/meeting/meeting.php similarity index 100% rename from app/api/meeting.php rename to app/api/meeting/meeting.php diff --git a/app/api/meeting.py b/app/api/meeting/meeting.py similarity index 100% rename from app/api/meeting.py rename to app/api/meeting/meeting.py diff --git a/app/api/meeting.sh b/app/api/meeting/meeting.sh similarity index 100% rename from app/api/meeting.sh rename to app/api/meeting/meeting.sh diff --git a/app/api/swagger.yaml b/app/api/swagger.yaml index 7fb99de..176b1c7 100644 --- a/app/api/swagger.yaml +++ b/app/api/swagger.yaml @@ -31,6 +31,45 @@ paths: $ref: '#/definitions/MeetingResponse' '403': description: 'Unauthorized!' + /join: + post: + tags: + - 'join' + summary: 'Create direct join' + description: 'Create join' + parameters: + - in: body + name: Join + description: Custom Join URL. + schema: + type: object + required: + - room + - name + - audio + - video + properties: + room: + type: string + name: + type: string + audio: + type: boolean + video: + type: boolean + consumes: + - 'application/json' + produces: + - 'application/json' + security: + - secretApiKey: [] + responses: + '200': + description: 'Direct join created' + schema: + $ref: '#/definitions/JoinResponse' + '403': + description: 'Unauthorized!' securityDefinitions: secretApiKey: @@ -45,3 +84,8 @@ definitions: properties: meeting: type: 'string' + JoinResponse: + type: 'object' + properties: + join: + type: 'string' diff --git a/app/src/Server.js b/app/src/Server.js index b4ab0ac..0d622f0 100644 --- a/app/src/Server.js +++ b/app/src/Server.js @@ -176,6 +176,31 @@ app.post(['/api/v1/meeting'], (req, res) => { }); }); +// request join room endpoint +app.post(['/api/v1/join'], (req, res) => { + // check if user was authorized for the api call + let host = req.headers.host; + let authorization = req.headers.authorization; + let api = new ServerApi(host, authorization); + if (!api.isAuthorized()) { + log.debug('MiroTalk get join - Unauthorized', { + header: req.headers, + body: req.body, + }); + return res.status(403).json({ error: 'Unauthorized!' }); + } + // setup Join URL + let joinURL = api.getJoinURL(req.body); + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify({ join: joinURL })); + // log.debug the output if all done + log.debug('MiroTalk get join - Authorized', { + header: req.headers, + body: req.body, + join: joinURL, + }); +}); + // not match any of page before, so 404 not found app.get('*', function (req, res) { res.sendFile(path.join(__dirname, '../../', 'public/view/404.html')); diff --git a/app/src/ServerApi.js b/app/src/ServerApi.js index ac424e3..06ca684 100644 --- a/app/src/ServerApi.js +++ b/app/src/ServerApi.js @@ -18,4 +18,19 @@ module.exports = class ServerApi { getMeetingURL() { return 'https://' + this._host + '/join/' + uuidV4(); } + + getJoinURL(data) { + return ( + 'https://' + + this._host + + '/join?room=' + + data.room + + '&name=' + + data.name + + '&audio=' + + data.audio + + '&video=' + + data.video + ); + } }; diff --git a/package.json b/package.json index 6b495f3..b7ee11c 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "yamljs": "0.3.0" }, "devDependencies": { - "node-fetch": "3.1.0", + "node-fetch": "2.6.6", "prettier": "2.5.1" } }