aboutsummaryrefslogtreecommitdiff
path: root/src/server/routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/routes')
-rw-r--r--src/server/routes/getSchedule.js76
-rw-r--r--src/server/routes/index.js31
-rw-r--r--src/server/routes/manifest.js19
-rw-r--r--src/server/routes/opensearch.js12
-rw-r--r--src/server/routes/slack.js60
5 files changed, 198 insertions, 0 deletions
diff --git a/src/server/routes/getSchedule.js b/src/server/routes/getSchedule.js
new file mode 100644
index 0000000..7850918
--- /dev/null
+++ b/src/server/routes/getSchedule.js
@@ -0,0 +1,76 @@
+const express = require('express')
+const router = express.Router()
+const request = require('request')
+const iconv = require('iconv-lite')
+const webshot = require('webshot')
+
+const getUserIndex = require('../lib/getUserIndex')
+const getURLOfUser = require('../lib/getURLOfUser')
+
+// copied from http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/untisscripts.js,
+// were using the same code as they do to be sure that we always get the same
+// week number.
+function getWeekNumber (target) {
+ const dayNr = (target.getDay() + 6) % 7
+ target.setDate(target.getDate() - dayNr + 3)
+ const firstThursday = target.valueOf()
+ target.setMonth(0, 1)
+ if (target.getDay() !== 4) {
+ target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7)
+ }
+
+ return 1 + Math.ceil((firstThursday - target) / 604800000)
+}
+
+router.get('/:type/:value.png', function (req, res, next) {
+ port = process.env.PORT || 3000;
+ const { type, value } = req.params
+ const stream = webshot(
+ `http://localhost:${port}/get/${type}/${value}`,
+ { customCSS: "body { background-color: white; }" }
+ )
+ stream.pipe(res)
+})
+
+router.get('/:type/:value.jpg', function (req, res, next) {
+ port = process.env.PORT || 3000;
+ const { type, value } = req.params
+ const stream = webshot(
+ `http://localhost:${port}/get/${type}/${value}`,
+ { customCSS: "body { background-color: white; }", streamType: 'jpg' }
+ )
+ stream.pipe(res)
+})
+
+router.get('/:type/:value', function (req, res, next) {
+ getUserIndex().then(users => {
+ const { type, value } = req.params
+ let { week } = req.query
+ const user =
+ users.filter(user => user.type === type && user.value === value)[0]
+
+ if (!user) {
+ next(new Error(`${type}${value} is not in the user index.`))
+ }
+
+ if (!week) {
+ week = getWeekNumber(new Date())
+ }
+
+ const { index } = user
+
+ const url = getURLOfUser(type, index, week)
+
+ request(url, { encoding: null }, function (err, data) {
+ if (err) {
+ next(err)
+ return
+ }
+
+ const utf8Body = iconv.decode(data.body, 'ISO-8859-1')
+ res.status(data.statusCode).end(utf8Body)
+ })
+ })
+})
+
+module.exports = router
diff --git a/src/server/routes/index.js b/src/server/routes/index.js
new file mode 100644
index 0000000..d2267ba
--- /dev/null
+++ b/src/server/routes/index.js
@@ -0,0 +1,31 @@
+'use strict'
+
+const express = require('express')
+const router = express.Router()
+const getMeetingpointData = require('../lib/getMeetingpointData')
+
+/* GET home page. */
+router.get(['/', '/s/*', '/t/*', '/r/*', '/c/*'], function (req, res, next) {
+ getMeetingpointData().then(data => {
+ const isBeta = process.env.BETA === '1'
+
+ let flags = []
+ if (isBeta) {
+ flags.push('BETA')
+ flags.push('NO_FEATURE_DETECT')
+ } else if (req.query.nfd != null) {
+ flags.push('NO_FEATURE_DETECT')
+ }
+
+ const flagsStr = `var FLAGS = ${JSON.stringify(flags)};`
+ const usersStr = `var USERS = ${JSON.stringify(data.users)};`
+ const validWeekNumbersStr = `var VALID_WEEK_NUMBERS = ${JSON.stringify(data.validWeekNumbers)}`
+
+ res.render('index', { flagsStr, usersStr, validWeekNumbersStr })
+ }).catch(function () {
+ console.error('Unable to get user info, emergency redirect!')
+ res.render('redirect')
+ })
+})
+
+module.exports = router
diff --git a/src/server/routes/manifest.js b/src/server/routes/manifest.js
new file mode 100644
index 0000000..b2ce55f
--- /dev/null
+++ b/src/server/routes/manifest.js
@@ -0,0 +1,19 @@
+'use strict'
+
+const express = require('express')
+const router = express.Router()
+const path = require('path')
+
+router.get('/', function (req, res, next) {
+ console.log('got a request')
+
+ const isBeta = process.env.BETA === '1'
+
+ if (isBeta) {
+ res.sendFile('manifest.beta.webmanifest', { root: path.join(__dirname, '../public') })
+ } else {
+ res.sendFile('manifest.webmanifest', { root: path.join(__dirname, '../public') })
+ }
+})
+
+module.exports = router
diff --git a/src/server/routes/opensearch.js b/src/server/routes/opensearch.js
new file mode 100644
index 0000000..c3e2e57
--- /dev/null
+++ b/src/server/routes/opensearch.js
@@ -0,0 +1,12 @@
+'use strict'
+
+const express = require('express')
+const router = express.Router()
+const path = require('path')
+
+router.get('/', function (req, res, next) {
+ res.setHeader('content-type', 'application/opensearchdescription+xml')
+ res.sendFile('opensearch.xml', { root: path.join(__dirname, '../public') })
+})
+
+module.exports = router
diff --git a/src/server/routes/slack.js b/src/server/routes/slack.js
new file mode 100644
index 0000000..e256f14
--- /dev/null
+++ b/src/server/routes/slack.js
@@ -0,0 +1,60 @@
+const express = require('express')
+const router = express.Router()
+
+const getUserIndex = require('../lib/getUserIndex')
+
+function generateResponse(query) {
+ return getUserIndex().then(users => {
+ const user =
+ users.filter(user => user.value === query)[0]
+
+ if (!user) {
+ return {
+ "response_type": "ephemeral",
+ "mrkdwn": true,
+ "text": `Sorry, I tried my best, but I couldn't find _${query}_`
+ }
+ }
+
+ return {
+ "response_type": "in_channel",
+ "text": `Here is the schedule of _${query}_`,
+ "mrkdwn": true,
+ "attachments": [
+ {
+ "fallback": `https://beta.rooster.hetmml.nl/${user.type}/${user.value}`,
+ "image_url": `https://beta.rooster.hetmml.nl/get/${user.type}/${user.value}.png`
+ }
+ ]
+ }
+ })
+}
+
+router.all('/', function (req, res, next) {
+ const query = req.body.text || req.query.text
+
+ if (query.indexOf('!') === 0) {
+ switch (query) {
+ case "!help":
+ res.json({
+ "response_type": "ephemeral",
+ "mrkdwn": true,
+ "text": "Here are some examples on how you can use me\n>/rooster 18561\n\n>/rooster akh\n\n>/rooster 6-5H2\n\n>/rooster 008-mk\n\nPlease note that the following does not work (yet)",
+ "attachments": [{
+ "text": "/rooster 5h2",
+ "color": "danger"
+ }]
+ })
+ default:
+ res.json({
+ "response_type": "ephemeral",
+ "text": "Unrecognized command, try !help"
+ })
+ break;
+ }
+ }
+
+ generateResponse(query).then((json) => res.json(json))
+})
+
+module.exports = router