rudeshark.net/src/api/endpoints/i/favorites.js

60 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-12-28 23:49:51 +01:00
'use strict';
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import Favorite from '../../models/favorite';
import serialize from '../../serializers/post';
/**
* Get followers of a user
*
2017-03-01 09:37:01 +01:00
* @param {any} params
* @return {Promise<any>}
2016-12-28 23:49:51 +01:00
*/
module.exports = (params) =>
new Promise(async (res, rej) =>
{
// Get 'limit' parameter
let limit = params.limit;
if (limit !== undefined && limit !== null) {
limit = parseInt(limit, 10);
// From 1 to 100
if (!(1 <= limit && limit <= 100)) {
return rej('invalid limit range');
}
} else {
limit = 10;
}
// Get 'offset' parameter
let offset = params.offset;
if (offset !== undefined && offset !== null) {
offset = parseInt(offset, 10);
} else {
offset = 0;
}
// Get 'sort' parameter
let sort = params.sort || 'desc';
// Get favorites
const favorites = await Favorites
.find({
user_id: user._id
2017-01-17 03:11:22 +01:00
}, {
2016-12-28 23:49:51 +01:00
limit: limit,
skip: offset,
sort: {
_id: sort == 'asc' ? 1 : -1
}
2017-01-17 03:11:22 +01:00
});
2016-12-28 23:49:51 +01:00
// Serialize
res(await Promise.all(favorites.map(async favorite =>
await serialize(favorite.post)
)));
});