-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
139 lines (111 loc) · 3.97 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var config = require('./config.js');
var Podio = require('./podio.js');
var express = require('express');
var util = require('util');
var Promise = require('bluebird');
var MeetingParser = require('./meetingparser.js');
var moment = require('moment');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.use(require('body-parser').json());
app.use(express.static(__dirname + '/public'));
app.get('/auth', function (req, res) {
if (req.query.error) {
res.send(util.format('Access denied: %s %s %s', req.query.error_reason, req.query.error, error_description));
} else {
var podio = new Podio(config.apiKey, config.appName, config.callbackUrl, req.query.code);
var start = moment.utc().subtract(1, 'hour').toDate();
var end = moment.utc().add(1, 'hour').toDate();
podio.authorize().then( (access_token) => {
podio.getAllCalendars(start, end).then( (calendarResult) => {
MeetingParser.getAllMeetingUrls(calendarResult, start, end).then((urls) => res.send(urls))
});
});
}
});
app.get('/user/:user_id', function (req, res) {
if (req.headers.authorization) {
var auth_token = req.headers.authorization.match(/OAuth2 (.*)/i)[1];
var podio = new Podio(config.apiKey, config.appName, config.callbackUrl, req.query.code);
podio.authorize(auth_token).then( () => {
podio.getUserInfo().then( (userInfo) => {
res.json(userInfo);
});
});
} else {
res.json([]);
}
});
app.get('/spaces/:user_id', function (req, res) {
console.log("Get spaces", req.params.user_id);
if (req.headers.authorization) {
var auth_token = req.headers.authorization.match(/OAuth2 (.*)/i)[1];
var podio = new Podio(config.apiKey, config.appName, config.callbackUrl, req.query.code);
podio.authorize(auth_token).then( () => {
podio.getWorkspacesForallOrgs(req.params.user_id).then( (allWorkspaces) => {
res.json(allWorkspaces);
});
});
} else {
res.json([]);
}
});
app.get('/rooms/:space_id', function (req, res) {
console.log("Get rooms", req.params.space_id);
if (req.headers.authorization) {
var auth_token = req.headers.authorization.match(/OAuth2 (.*)/i)[1];
var podio = new Podio(config.apiKey, config.appName, config.callbackUrl, req.query.code);
podio.authorize(auth_token).then( () => {
podio.getRooms(req.params.space_id).then( (rooms) => {
res.json(rooms);
});
});
} else {
res.json([]);
}
});
app.get('/meetings', function (req, res) {
console.log("Get meetings", req.headers);
if (req.headers.authorization) {
var auth_token = req.headers.authorization.match(/OAuth2 (.*)/i)[1];
var podio = new Podio(config.apiKey, config.appName, config.callbackUrl, req.query.code);
var start = moment.utc().subtract(1, 'hour').toDate();
var end = moment.utc().add(1, 'hour').toDate();
podio.authorize(auth_token).then( (access_token) => {
podio.getAllCalendars(start, end).then( (calendarResult) => {
console.log(calendarResult.map((c) => {
return {
start_utc: c.start_utc,
end_utc: c.end_utc,
title: c.title
}
}));
MeetingParser.getAllMeetingUrls(calendarResult, start, end).then((allUrls) => res.json(allUrls));
});
});
} else {
res.json([]);
}
});
app.post('/presence', function(req, res){
var topic = 'presence/' + req.body.room.Identifier;
io.emit(topic, req.body);
res.send("OK");
});
app.post('/join', function(req, res){
var topic = 'join/' + req.body.room.Identifier;
io.emit(topic, { meeting: req.body.meeting, user: req.body.user });
res.send("OK");
});
app.get('/login', function (req, res) {
var loginUrl = util.format('https://podio.com/oauth/authorize?client_id=%s&redirect_uri=%s', config.appName, config.callbackUrl);
res.redirect(loginUrl);
});
io.on('connection', function (socket) {
});
var server = server.listen(process.env.PORT || 3000, function () {
var host = 'localhost';
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});