1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-22 18:48:34 +09:00
misskey/src/api/endpoints/users/search_by_username.js

66 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
'use strict';
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import User from '../../models/user';
import serialize from '../../serializers/user';
/**
* Search a user by username
*
* @param {Object} params
* @param {Object} me
* @return {Promise<object>}
*/
module.exports = (params, me) =>
new Promise(async (res, rej) =>
{
// Get 'query' parameter
let query = params.query;
if (query === undefined || query === null || query.trim() === '') {
return rej('query is required');
}
query = query.trim();
if (!/^[a-zA-Z0-9-]+$/.test(query)) {
return rej('invalid query');
}
// 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;
}
const users = await User
.find({
username_lower: new RegExp(query.toLowerCase())
}, {
limit: limit,
skip: offset
})
.toArray();
// Serialize
res(await Promise.all(users.map(async user =>
await serialize(user, me, { detail: true }))));
});