This commit is contained in:
syuilo
2018-06-18 09:54:53 +09:00
parent a766faeae9
commit 80e5645a84
150 changed files with 305 additions and 2334 deletions

View File

@ -1,8 +1,5 @@
/**
* Module dependencies
*/
import $ from 'cafy'; import ID from '../../../../cafy-id';
import User from '../../../../models/user';
import User, { ILocalUser } from '../../../../models/user';
import Following from '../../../../models/following';
import { pack } from '../../../../models/user';
import { getFriendIds } from '../../common/get-friends';
@ -10,7 +7,7 @@ import { getFriendIds } from '../../common/get-friends';
/**
* Get followers of a user
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
module.exports = (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
// Get 'userId' parameter
const [userId, userIdErr] = $.type(ID).get(params.userId);
if (userIdErr) return rej('invalid userId param');

View File

@ -1,20 +1,13 @@
/**
* Module dependencies
*/
import $ from 'cafy'; import ID from '../../../../cafy-id';
import User from '../../../../models/user';
import User, { ILocalUser } from '../../../../models/user';
import Following from '../../../../models/following';
import { pack } from '../../../../models/user';
import { getFriendIds } from '../../common/get-friends';
/**
* Get following users of a user
*
* @param {any} params
* @param {any} me
* @return {Promise<any>}
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
module.exports = (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
// Get 'userId' parameter
const [userId, userIdErr] = $.type(ID).get(params.userId);
if (userIdErr) return rej('invalid userId param');

View File

@ -1,11 +1,8 @@
/**
* Module dependencies
*/
import $ from 'cafy'; import ID from '../../../../cafy-id';
import Note from '../../../../models/note';
import User, { pack } from '../../../../models/user';
import User, { pack, ILocalUser } from '../../../../models/user';
module.exports = (params, me) => new Promise(async (res, rej) => {
module.exports = (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
// Get 'userId' parameter
const [userId, userIdErr] = $.type(ID).get(params.userId);
if (userIdErr) return rej('invalid userId param');
@ -64,7 +61,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
}
});
const repliedUsers = {};
const repliedUsers: any = {};
// Extract replies from recent notes
replyTargetNotes.forEach(note => {

View File

@ -1,13 +1,11 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import UserList, { pack } from '../../../../../models/user-list';
import { ILocalUser } from '../../../../../models/user';
/**
* Create a user list
*/
module.exports = async (params, user) => new Promise(async (res, rej) => {
module.exports = async (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'title' parameter
const [title, titleErr] = $.str.range(1, 100).get(params.title);
if (titleErr) return rej('invalid title param');

View File

@ -1,9 +1,10 @@
import UserList, { pack } from '../../../../../models/user-list';
import { ILocalUser } from '../../../../../models/user';
/**
* Add a user to a user list
*/
module.exports = async (params, me) => new Promise(async (res, rej) => {
module.exports = async (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
// Fetch lists
const userLists = await UserList.find({
userId: me._id,

View File

@ -1,6 +1,6 @@
import $ from 'cafy'; import ID from '../../../../../cafy-id';
import UserList from '../../../../../models/user-list';
import User, { pack as packUser, isRemoteUser, getGhost } from '../../../../../models/user';
import User, { pack as packUser, isRemoteUser, getGhost, ILocalUser } from '../../../../../models/user';
import { publishUserListStream } from '../../../../../publishers/stream';
import ap from '../../../../../remote/activitypub/renderer';
import renderFollow from '../../../../../remote/activitypub/renderer/follow';
@ -9,7 +9,7 @@ import { deliver } from '../../../../../queue';
/**
* Add a user to a user list
*/
module.exports = async (params, me) => new Promise(async (res, rej) => {
module.exports = async (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
// Get 'listId' parameter
const [listId, listIdErr] = $.type(ID).get(params.listId);
if (listIdErr) return rej('invalid listId param');

View File

@ -1,10 +1,11 @@
import $ from 'cafy'; import ID from '../../../../../cafy-id';
import UserList, { pack } from '../../../../../models/user-list';
import { ILocalUser } from '../../../../../models/user';
/**
* Show a user list
*/
module.exports = async (params, me) => new Promise(async (res, rej) => {
module.exports = async (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
// Get 'listId' parameter
const [listId, listIdErr] = $.type(ID).get(params.listId);
if (listIdErr) return rej('invalid listId param');

View File

@ -1,15 +1,12 @@
/**
* Module dependencies
*/
import $ from 'cafy'; import ID from '../../../../cafy-id';
import getHostLower from '../../common/get-host-lower';
import Note, { pack } from '../../../../models/note';
import User from '../../../../models/user';
import User, { ILocalUser } from '../../../../models/user';
/**
* Get notes of a user
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
module.exports = (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
// Get 'userId' parameter
const [userId, userIdErr] = $.type(ID).optional().get(params.userId);
if (userIdErr) return rej('invalid userId param');

View File

@ -1,20 +1,13 @@
/**
* Module dependencies
*/
const ms = require('ms');
import $ from 'cafy';
import User, { pack } from '../../../../models/user';
import User, { pack, ILocalUser } from '../../../../models/user';
import { getFriendIds } from '../../common/get-friends';
import Mute from '../../../../models/mute';
/**
* Get recommended users
*
* @param {any} params
* @param {any} me
* @return {Promise<any>}
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
module.exports = (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $.num.optional().range(1, 100).get(params.limit);
if (limitErr) return rej('invalid limit param');

View File

@ -1,40 +1,19 @@
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import $ from 'cafy';
import User, { pack } from '../../../../models/user';
import config from '../../../../config';
import User, { pack, ILocalUser } from '../../../../models/user';
const escapeRegexp = require('escape-regexp');
/**
* Search a user
*
* @param {any} params
* @param {any} me
* @return {Promise<any>}
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
module.exports = (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
// Get 'query' parameter
const [query, queryError] = $.str.pipe(x => x != '').get(params.query);
if (queryError) return rej('invalid query param');
// Get 'offset' parameter
const [offset = 0, offsetErr] = $.num.optional().min(0).get(params.offset);
if (offsetErr) return rej('invalid offset param');
// Get 'max' parameter
const [max = 10, maxErr] = $.num.optional().range(1, 30).get(params.max);
if (maxErr) return rej('invalid max param');
// If Elasticsearch is available, search by $
// If not, search by MongoDB
(config.elasticsearch.enable ? byElasticsearch : byNative)
(res, rej, me, query, offset, max);
});
// Search by MongoDB
async function byNative(res, rej, me, query, offset, max) {
const escapedQuery = escapeRegexp(query);
// Search users
@ -51,49 +30,5 @@ async function byNative(res, rej, me, query, offset, max) {
});
// Serialize
res(await Promise.all(users.map(async user =>
await pack(user, me, { detail: true }))));
}
// Search by Elasticsearch
async function byElasticsearch(res, rej, me, query, offset, max) {
const es = require('../../db/elasticsearch');
es.search({
index: 'misskey',
type: 'user',
body: {
size: max,
from: offset,
query: {
simple_query_string: {
fields: ['username', 'name', 'bio'],
query: query,
default_operator: 'and'
}
}
}
}, async (error, response) => {
if (error) {
console.error(error);
return res(500);
}
if (response.hits.total === 0) {
return res([]);
}
const hits = response.hits.hits.map(hit => new mongo.ObjectID(hit._id));
const users = await User
.find({
_id: {
$in: hits
}
});
// Serialize
res(await Promise.all(users.map(async user =>
await pack(user, me, { detail: true }))));
});
}
res(await Promise.all(users.map(user => pack(user, me, { detail: true }))));
});

View File

@ -1,13 +1,10 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User, { pack } from '../../../../models/user';
import User, { pack, ILocalUser } from '../../../../models/user';
/**
* Search a user by username
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
module.exports = (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
// Get 'query' parameter
const [query, queryError] = $.str.get(params.query);
if (queryError) return rej('invalid query param');

View File

@ -1,8 +1,5 @@
/**
* Module dependencies
*/
import $ from 'cafy'; import ID from '../../../../cafy-id';
import User, { pack } from '../../../../models/user';
import User, { pack, ILocalUser } from '../../../../models/user';
import resolveRemoteUser from '../../../../remote/resolve-user';
const cursorOption = { fields: { data: false } };
@ -10,7 +7,7 @@ const cursorOption = { fields: { data: false } };
/**
* Show user(s)
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
module.exports = (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
let user;
// Get 'userId' parameter
@ -49,7 +46,7 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
return rej('failed to resolve remote user');
}
} else {
const q = userId !== undefined
const q: any = userId !== undefined
? { _id: userId }
: { usernameLower: username.toLowerCase(), host: null };