Zoom integration using the JWT API

This commit is contained in:
bumi 2020-04-15 21:29:21 +02:00
parent 5cc0116163
commit 70ea031b31

View File

@ -1,47 +1,85 @@
const fetch = require('node-fetch'); const fetch = require('node-fetch');
module.exports = async function(robot, kredits) { module.exports = async function(robot, kredits) {
const Contributor = kredits.Contributor;
const Contribution = kredits.Contribution;
function createContributionFor (participant, meeting) { const kreditsContributionAmount = 500;
// TODO const kreditsContributionKind = 'community';
const zoomAccessToken = process.env.KREDITS_ZOOM_JWT;
const walletTransactionCount = await kredits.provider.getTransactionCount(kredits.signer.address);
let nonce = walletTransactionCount;
function createContributionFor(participant, meeting) {
const displayName = participant.name;
return getContributorByZoomDisplayName(displayName)
.then(contributor => {
let contribution = {
contributorId: contributor.id,
contributorIpfsHash: contributor.ipfsHash,
amount: kreditsContributionAmount,
kind: kreditsContributionKind,
description: `Team meeting: ${meeting.topic}`,
date: meeting.end_time.split('T')[0],
time: meeting.end_time.split('T')[1]
}
return Contribution.addContribution(contribution, { nonce: nonce++ })
.catch(error => {
robot.logger.error(`[hubot-kredits] Adding contribution failed:`, error);
});
})
} }
function getContributorByZoomUserId(userId) { function getContributorByZoomDisplayName(displayName) {
return Contributor.findByAccount({ zoomId: userId }); return Contributor.findByAccount({ site: 'zoom.us', username: displayName });
}
function request(path) {
return fetch(
`https://api.zoom.us/v2${path}`,
{headers: {authorization: `Bearer ${zoomAccessToken}`}}
);
} }
function getMeetingParticipants(meetingUUID) { function getMeetingParticipants(meetingUUID) {
fetch(`https://api.zoom.us/v2/past_meetings/${meetingUUID}/participants`) return request(`/past_meetings/${meetingUUID}/participants`)
.then(response => response.json()) .then(response => response.json())
.then(json => json.participants) .then(json => json.participants)
} }
function handleZoomMeetingEnded(data) { function getMeetingDetails(meetingUUID) {
const meetingUUID = data.uuid; return request(`/past_meetings/${meetingUUID}`)
const topic = data.topic; .then(r => r.json());
const duration = data.duration; }
const meeting = { async function handleZoomMeetingEnded(data) {
// TODO const meetingDetails = await getMeetingDetails(data.uuid);
} const participants = await getMeetingParticipants(data.uuid);
if (duration < 15) { if (meetingDetails.duration < 15 || meetingDetails.participants_count < 3) {
robot.logger.info('[hubot-kredits] ignoring short calls'); robot.logger.info(`[hubot-kredits] ignoring meeting: uuid:${data.uuid} duration:${meetingDetails.duration} participants_count:${meetingDetails.participants_count}`);
return; return;
} }
const participants = await getMeetingParticipants(meetingUUID);
participants.forEach(p => { participants.forEach(p => {
createContributionFor(p, meeting); createContributionFor(p, meetingDetails)
.then(tx => {
robot.logger.info(`[hubot-kredits] contribution created: ${tx.hash}`);
})
}); });
} }
robot.router.post('/incoming/kredits/zoom'+process.env.KREDITS_WEBHOOK_TOKEN), (req, res) => { robot.router.post('/incoming/kredits/zoom'+process.env.KREDITS_WEBHOOK_TOKEN, (req, res) => {
let data = req.body; let data = req.body;
const eventName = data.event; const eventName = data.event;
const payload = data.payload; const payload = data.payload;
const object = payload.object;
if (eventName === 'meeting.ended') { if (eventName === 'meeting.ended') {
handleZoomMeetingEnded(payload); handleZoomMeetingEnded(object);
} }
res.sendStatus(200); res.sendStatus(200);