From 20cc71c8c10505a1bebc7edc149d6d40f6cf357c Mon Sep 17 00:00:00 2001 From: Evert Date: Thu, 31 Aug 2017 20:24:38 +0300 Subject: [PATCH] latest donations endpoint for other services --- server/api/index.js | 60 +++++++++++++++++++++++++++++++++++--------- server/routes/api.js | 20 ++++++++++++++- 2 files changed, 67 insertions(+), 13 deletions(-) diff --git a/server/api/index.js b/server/api/index.js index 37a54c5..4b291e8 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -49,6 +49,43 @@ function keysAvailable (object, required) { return found } +// Clean up the donation endpoint for ease of use +async function cleanUpDonation (obj, mcOnly, timeframe) { + if (timeframe && new Date(obj.created_at).getTime() < timeframe) { + return null + } + + let user + + if (obj.user_id) { + user = await API.User.get(obj.user_id) + } + + let result = { + trackId: obj.id, + amount: obj.amount, + donated: obj.created_at + } + + if (user) { + result.name = user.display_name + } + + let sources = obj.source.split(';') + for (let i in sources) { + if (sources[i].indexOf('mcu:') === 0) { + let mcu = sources[i].split(':')[1] + if (mcu.match(/^([\w_]{2,16})$/i)) { + result.minecraft_username = mcu + } + } + } + + if (!result.minecraft_username && mcOnly) return null + + return result +} + let txnStore = [] const API = { @@ -459,23 +496,22 @@ const API = { userContributions: async function (user) { user = await API.User.ensureObject(user) - let dbq = await models.Donation.query().where('user_id', user.id) + let dbq = await models.Donation.query().orderBy('created_at', 'desc').where('user_id', user.id) let contribs = [] for (let i in dbq) { - let contrib = dbq[i] - let obj = { - amount: contrib.amount, - donated: contrib.created_at - } + contribs.push(await cleanUpDonation(dbq[i])) + } - let srcs = contrib.source.split(';') - for (let j in srcs) { - if (srcs[j].indexOf('mcu') === 0) { - obj.minecraft_username = srcs[j].split(':')[1] - } - } + return contribs + }, + allContributions: async function (count, mcOnly, timeframe = 0) { + let dbq = await models.Donation.query().orderBy('created_at', 'desc').limit(count) + let contribs = [] + for (let i in dbq) { + let obj = await cleanUpDonation(dbq[i], mcOnly, timeframe) + if (!obj) continue contribs.push(obj) } diff --git a/server/routes/api.js b/server/routes/api.js index 66534af..4adbdb8 100644 --- a/server/routes/api.js +++ b/server/routes/api.js @@ -447,13 +447,31 @@ router.post('/paypal/ipn', wrap(async (req, res) => { res.status(204).end() })) -router.get('/user/donations', wrap(async (req, res, next) => { +router.get('/donations/user', wrap(async (req, res, next) => { if (!req.session.user) return next() let contribs = await API.Payment.userContributions(req.session.user) res.jsonp(contribs) })) +router.get('/donations', wrap(async (req, res, next) => { + let count = parseInt(req.query.count) + if (isNaN(count)) { + count = 10 + } + + if (count > 10) { + count = 10 + } + + let mcu = req.query.mcu === '1' || req.query.mcu === 'true' + let timeFrame = parseInt(req.query.timeFrame) + if (isNaN(timeFrame)) timeFrame = 0 + + let contribs = await API.Payment.allContributions(count, mcu, timeFrame) + res.jsonp(contribs) +})) + // 404 router.use((req, res) => { res.status(404).jsonp({error: 'Not found'})