diff --git a/controllers/statsController.js b/controllers/statsController.js index 6f3d4475..68215767 100644 --- a/controllers/statsController.js +++ b/controllers/statsController.js @@ -70,25 +70,23 @@ module.exports = { }); }, getTrendingClaims (startDate) { - logger.debug('retrieving trending requests'); + logger.debug('retrieving trending'); return new Promise((resolve, reject) => { // get the raw requests data - db.getTrendingClaims(startDate) - .then(results => { - if (results) { - results.forEach(element => { - const fileExtenstion = element.fileType.substring(element.fileType.lastIndexOf('/') + 1); - element['showUrlLong'] = `/${element.claimId}/${element.name}`; - element['directUrlLong'] = `/${element.claimId}/${element.name}.${fileExtenstion}`; - element['directUrlShort'] = `/${element.claimId}/${element.name}.${fileExtenstion}`; - element['contentType'] = element.fileType; - element['thumbnail'] = 'https://spee.ch/assets/img/video_thumb_default.png'; + db.getTrendingFiles(startDate) + .then(fileArray => { + let claimsPromiseArray = []; + if (fileArray) { + fileArray.forEach(file => { + claimsPromiseArray.push(db.Claim.resolveClaim(file.name, file.claimId)); }); + return Promise.all(claimsPromiseArray); } - resolve(results); + }) + .then(claimsArray => { + resolve(claimsArray); }) .catch(error => { - logger.error('sequelize error >>', error); reject(error); }); }); diff --git a/models/index.js b/models/index.js index 0d709b7c..b959586c 100644 --- a/models/index.js +++ b/models/index.js @@ -73,8 +73,8 @@ db.upsert = (Model, values, condition, tableName) => { }); }; -// add a 'getTrendingClaims' method to the db object -db.getTrendingClaims = (startDate) => { +// add a 'getTrendingFiles' method to the db object. note: change this to get claims directly. might need new association between Request and Claim +db.getTrendingFiles = (startDate) => { return db.sequelize.query(`SELECT COUNT(*), File.* FROM Request LEFT JOIN File ON Request.FileId = File.id WHERE FileId IS NOT NULL AND nsfw != 1 AND trendingEligible = 1 AND Request.createdAt > "${startDate}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }); }; diff --git a/routes/page-routes.js b/routes/page-routes.js index 2218c419..450f55e0 100644 --- a/routes/page-routes.js +++ b/routes/page-routes.js @@ -30,7 +30,6 @@ module.exports = (app) => { const dateTime = startDate.toISOString().slice(0, 19).replace('T', ' '); getTrendingClaims(dateTime) .then(result => { - // logger.debug(result); res.status(200).render('popular', { trendingAssets: result, });