christmas/routes/index.js

69 lines
2 KiB
JavaScript
Raw Normal View History

2020-11-08 14:23:51 -08:00
const publicRoute = require('../middlewares/publicRoute')
2020-11-08 13:54:08 -08:00
const express = require('express')
const path = require('path')
2021-09-14 23:41:44 -07:00
const fs = require('fs/promises')
2018-11-20 11:19:58 -08:00
2019-11-17 12:46:08 -08:00
module.exports = ({ db, config }) => {
2021-09-14 23:41:44 -07:00
async function ensurePfp (username) {
if (!config.pfp) return
2021-09-14 23:41:44 -07:00
const user = await db.get(username)
if (user.pfp) return
const { rows } = await db.allDocs({ include_docs: true })
const unfilteredPool = await fs.readdir('static/img/default-pfps')
const filteredPool = unfilteredPool.filter(file => !rows.find(row => row.doc.pfp === `${_CC.config.base}img/default-pfps/${file}`))
const pool = filteredPool.length ? filteredPool : unfilteredPool
user.pfp = `${_CC.config.base}img/default-pfps/${_CC._.sample(pool)}`
await db.put(user)
}
2021-09-14 23:57:28 -07:00
;(async () => {
const { rows } = await db.allDocs({ include_docs: true })
for (const row of rows) {
await ensurePfp(row.id)
}
})()
2020-11-08 13:54:08 -08:00
const router = express.Router()
2018-11-20 11:19:58 -08:00
2020-11-08 13:54:08 -08:00
router.use('/', express.static(path.join(__dirname, '../static')))
2022-10-03 08:03:02 -07:00
router.use(require('cookie-parser')())
2018-11-20 11:19:58 -08:00
router.get('/',
async (req, res, next) => {
2020-11-08 13:54:08 -08:00
const dbInfo = await db.info()
2018-11-20 11:19:58 -08:00
if (dbInfo.doc_count === 0) {
2020-11-08 13:54:08 -08:00
res.redirect('/setup')
2018-11-20 11:19:58 -08:00
} else {
2020-11-08 13:54:08 -08:00
next()
2018-11-20 11:19:58 -08:00
}
},
2020-11-08 14:23:51 -08:00
publicRoute(),
2018-11-20 11:19:58 -08:00
(req, res) => {
2020-11-08 13:54:08 -08:00
res.redirect('/wishlist')
2018-11-20 11:19:58 -08:00
}
2020-11-08 13:54:08 -08:00
)
2018-11-20 11:19:58 -08:00
2019-11-17 15:05:45 -08:00
router.use('/api', require('./api')({ db }))
2020-11-08 13:54:08 -08:00
router.use('/setup', require('./setup')(db))
2018-11-20 11:19:58 -08:00
2021-09-14 23:41:44 -07:00
router.use('/login', require('./login')({ ensurePfp }))
2020-11-08 13:54:08 -08:00
router.use('/logout', require('./logout')())
router.use('/resetpw', require('./resetpw')(db))
router.use('/confirm-account', require('./confirm-account')(db))
2018-11-20 11:19:58 -08:00
2020-11-08 13:54:08 -08:00
router.use('/wishlist', require('./wishlist')(db))
2020-11-05 14:42:24 -08:00
router.use('/supported-sites', require('./supported-sites')())
2018-11-20 11:19:58 -08:00
router.use('/profile', require('./profile')({ db, config, ensurePfp }))
2018-11-20 11:19:58 -08:00
2021-09-14 23:41:44 -07:00
router.use('/admin-settings', require('./adminSettings')({ db, ensurePfp }))
2018-11-20 11:19:58 -08:00
2019-11-17 12:46:08 -08:00
router.use('/manifest.json', require('./manifest.json')({ config }))
2020-11-08 13:54:08 -08:00
return router
}