fix try_until

This commit is contained in:
Daniel Lugo 2020-08-04 12:25:49 -04:00
parent 5289a9e398
commit 48894b27c4

View file

@ -13,8 +13,7 @@ const Common = require('shock-common')
const isARealUsableNumber = require('lodash/isFinite') const isARealUsableNumber = require('lodash/isFinite')
const Big = require('big.js') const Big = require('big.js')
const size = require('lodash/size') const size = require('lodash/size')
const { range } = require('ramda') const { range, flatten } = require('ramda')
const flattenDeep = require('lodash/flattenDeep')
const getListPage = require('../utils/paginate') const getListPage = require('../utils/paginate')
const auth = require('../services/auth/auth') const auth = require('../services/auth/auth')
@ -2046,16 +2045,17 @@ module.exports = async (
*/ */
const apiGunFeedGet = async (req, res) => { const apiGunFeedGet = async (req, res) => {
try { try {
const { page: pageStr } = req.query
const page = Number(pageStr)
const MAX_PAGES_TO_FETCH_FOR_TRY_UNTIL = 4 const MAX_PAGES_TO_FETCH_FOR_TRY_UNTIL = 4
const { page: pageStr } = req.query
/** /**
* Similar to a "before" query param in cursor based pagination. We call * Similar to a "before" query param in cursor based pagination. We call
* it "try" because it is likely that this item lies beyond * it "try" because it is likely that this item lies beyond
* MAX_PAGES_TO_FETCH_FOR_TRY_UNTIL in which case we gracefully just send * MAX_PAGES_TO_FETCH_FOR_TRY_UNTIL in which case we gracefully just send
* 2 pages and 205 response. * 2 pages and 205 response.
*/ */
// eslint-disable-next-line prefer-destructuring
const try_until = req.query.try_until const try_until = req.query.try_until
if (pageStr) { if (pageStr) {
@ -2079,9 +2079,8 @@ module.exports = async (
posts: await GunGetters.getFeedPage(page) posts: await GunGetters.getFeedPage(page)
}) })
} else if (try_until) { } else if (try_until) {
const promises = range(1, MAX_PAGES_TO_FETCH_FOR_TRY_UNTIL).map(p => const pages = range(1, MAX_PAGES_TO_FETCH_FOR_TRY_UNTIL)
GunGetters.getFeedPage(p) const promises = pages.map(p => GunGetters.getFeedPage(p))
)
let results = await Promise.all(promises) let results = await Promise.all(promises)
@ -2090,10 +2089,10 @@ module.exports = async (
) )
if (idxIfFound > -1) { if (idxIfFound > -1) {
results = results.slice(0, idxIfFound) results = results.slice(0, idxIfFound + 1)
} }
const posts = flattenDeep(results) const posts = flatten(results)
return res.status(200).json({ return res.status(200).json({
posts posts