Initial commit 🍀
This commit is contained in:
102
src/api/endpoints/users/followers.js
Normal file
102
src/api/endpoints/users/followers.js
Normal file
@ -0,0 +1,102 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import * as mongo from 'mongodb';
|
||||
import User from '../../models/user';
|
||||
import Following from '../../models/following';
|
||||
import serialize from '../../serializers/user';
|
||||
import getFriends from '../../common/get-friends';
|
||||
|
||||
/**
|
||||
* Get followers of a user
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} me
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = (params, me) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'user_id' parameter
|
||||
const userId = params.user_id;
|
||||
if (userId === undefined || userId === null) {
|
||||
return rej('user_id is required');
|
||||
}
|
||||
|
||||
// Get 'iknow' parameter
|
||||
const iknow = params.iknow === 'true';
|
||||
|
||||
// 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 'cursor' parameter
|
||||
const cursor = params.cursor || null;
|
||||
|
||||
// Lookup user
|
||||
const user = await User.findOne({
|
||||
_id: new mongo.ObjectID(userId)
|
||||
});
|
||||
|
||||
if (user === null) {
|
||||
return rej('user not found');
|
||||
}
|
||||
|
||||
// Construct query
|
||||
const query = {
|
||||
followee_id: user._id,
|
||||
deleted_at: { $exists: false }
|
||||
};
|
||||
|
||||
// ログインしていてかつ iknow フラグがあるとき
|
||||
if (me && iknow) {
|
||||
// Get my friends
|
||||
const myFriends = await getFriends(me._id);
|
||||
|
||||
query.follower_id = {
|
||||
$in: myFriends
|
||||
};
|
||||
}
|
||||
|
||||
// カーソルが指定されている場合
|
||||
if (cursor) {
|
||||
query._id = {
|
||||
$lt: new mongo.ObjectID(cursor)
|
||||
};
|
||||
}
|
||||
|
||||
// Get followers
|
||||
const following = await Following
|
||||
.find(query, {}, {
|
||||
limit: limit + 1,
|
||||
sort: { _id: -1 }
|
||||
})
|
||||
.toArray();
|
||||
|
||||
// 「次のページ」があるかどうか
|
||||
const inStock = following.length === limit + 1;
|
||||
if (inStock) {
|
||||
following.pop();
|
||||
}
|
||||
|
||||
// Serialize
|
||||
const users = await Promise.all(following.map(async f =>
|
||||
await serialize(f.follower_id, me, { detail: true })));
|
||||
|
||||
// Response
|
||||
res({
|
||||
users: users,
|
||||
next: inStock ? following[following.length - 1]._id : null,
|
||||
});
|
||||
});
|
102
src/api/endpoints/users/following.js
Normal file
102
src/api/endpoints/users/following.js
Normal file
@ -0,0 +1,102 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import * as mongo from 'mongodb';
|
||||
import User from '../../models/user';
|
||||
import Following from '../../models/following';
|
||||
import serialize from '../../serializers/user';
|
||||
import getFriends from '../../common/get-friends';
|
||||
|
||||
/**
|
||||
* Get following users of a user
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} me
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = (params, me) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'user_id' parameter
|
||||
const userId = params.user_id;
|
||||
if (userId === undefined || userId === null) {
|
||||
return rej('user_id is required');
|
||||
}
|
||||
|
||||
// Get 'iknow' parameter
|
||||
const iknow = params.iknow === 'true';
|
||||
|
||||
// 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 'cursor' parameter
|
||||
const cursor = params.cursor || null;
|
||||
|
||||
// Lookup user
|
||||
const user = await User.findOne({
|
||||
_id: new mongo.ObjectID(userId)
|
||||
});
|
||||
|
||||
if (user === null) {
|
||||
return rej('user not found');
|
||||
}
|
||||
|
||||
// Construct query
|
||||
const query = {
|
||||
follower_id: user._id,
|
||||
deleted_at: { $exists: false }
|
||||
};
|
||||
|
||||
// ログインしていてかつ iknow フラグがあるとき
|
||||
if (me && iknow) {
|
||||
// Get my friends
|
||||
const myFriends = await getFriends(me._id);
|
||||
|
||||
query.followee_id = {
|
||||
$in: myFriends
|
||||
};
|
||||
}
|
||||
|
||||
// カーソルが指定されている場合
|
||||
if (cursor) {
|
||||
query._id = {
|
||||
$lt: new mongo.ObjectID(cursor)
|
||||
};
|
||||
}
|
||||
|
||||
// Get followers
|
||||
const following = await Following
|
||||
.find(query, {}, {
|
||||
limit: limit + 1,
|
||||
sort: { _id: -1 }
|
||||
})
|
||||
.toArray();
|
||||
|
||||
// 「次のページ」があるかどうか
|
||||
const inStock = following.length === limit + 1;
|
||||
if (inStock) {
|
||||
following.pop();
|
||||
}
|
||||
|
||||
// Serialize
|
||||
const users = await Promise.all(following.map(async f =>
|
||||
await serialize(f.followee_id, me, { detail: true })));
|
||||
|
||||
// Response
|
||||
res({
|
||||
users: users,
|
||||
next: inStock ? following[following.length - 1]._id : null,
|
||||
});
|
||||
});
|
114
src/api/endpoints/users/posts.js
Normal file
114
src/api/endpoints/users/posts.js
Normal file
@ -0,0 +1,114 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import * as mongo from 'mongodb';
|
||||
import Post from '../../models/post';
|
||||
import User from '../../models/user';
|
||||
import serialize from '../../serializers/post';
|
||||
|
||||
/**
|
||||
* Get posts of a user
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} me
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = (params, me) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'user_id' parameter
|
||||
const userId = params.user_id;
|
||||
if (userId === undefined || userId === null) {
|
||||
return rej('user_id is required');
|
||||
}
|
||||
|
||||
// Get 'with_replies' parameter
|
||||
let withReplies = params.with_replies;
|
||||
if (withReplies !== undefined && withReplies !== null && withReplies === 'true') {
|
||||
withReplies = true;
|
||||
} else {
|
||||
withReplies = false;
|
||||
}
|
||||
|
||||
// Get 'with_media' parameter
|
||||
let withMedia = params.with_media;
|
||||
if (withMedia !== undefined && withMedia !== null && withMedia === 'true') {
|
||||
withMedia = true;
|
||||
} else {
|
||||
withMedia = false;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
const since = params.since_id || null;
|
||||
const max = params.max_id || null;
|
||||
|
||||
// Check if both of since_id and max_id is specified
|
||||
if (since !== null && max !== null) {
|
||||
return rej('cannot set since_id and max_id');
|
||||
}
|
||||
|
||||
// Lookup user
|
||||
const user = await User.findOne({
|
||||
_id: new mongo.ObjectID(userId)
|
||||
});
|
||||
|
||||
if (user === null) {
|
||||
return rej('user not found');
|
||||
}
|
||||
|
||||
// Construct query
|
||||
const sort = {
|
||||
_id: -1
|
||||
};
|
||||
const query = {
|
||||
user_id: user._id
|
||||
};
|
||||
if (since !== null) {
|
||||
sort._id = 1;
|
||||
query._id = {
|
||||
$gt: new mongo.ObjectID(since)
|
||||
};
|
||||
} else if (max !== null) {
|
||||
query._id = {
|
||||
$lt: new mongo.ObjectID(max)
|
||||
};
|
||||
}
|
||||
|
||||
if (!withReplies) {
|
||||
query.reply_to_id = null;
|
||||
}
|
||||
|
||||
if (withMedia) {
|
||||
query.media_ids = {
|
||||
$exists: true,
|
||||
$ne: null
|
||||
};
|
||||
}
|
||||
|
||||
// Issue query
|
||||
const posts = await Post
|
||||
.find(query, {}, {
|
||||
limit: limit,
|
||||
sort: sort
|
||||
})
|
||||
.toArray();
|
||||
|
||||
// Serialize
|
||||
res(await Promise.all(posts.map(async (post) =>
|
||||
await serialize(post, me)
|
||||
)));
|
||||
});
|
61
src/api/endpoints/users/recommendation.js
Normal file
61
src/api/endpoints/users/recommendation.js
Normal file
@ -0,0 +1,61 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import User from '../../models/user';
|
||||
import serialize from '../../serializers/user';
|
||||
import getFriends from '../../common/get-friends';
|
||||
|
||||
/**
|
||||
* Get recommended users
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} me
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = (params, me) =>
|
||||
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;
|
||||
}
|
||||
|
||||
// ID list of the user itself and other users who the user follows
|
||||
const followingIds = await getFriends(me._id);
|
||||
|
||||
const users = await User
|
||||
.find({
|
||||
_id: {
|
||||
$nin: followingIds
|
||||
}
|
||||
}, {}, {
|
||||
limit: limit,
|
||||
skip: offset,
|
||||
sort: {
|
||||
followers_count: -1
|
||||
}
|
||||
})
|
||||
.toArray();
|
||||
|
||||
// Serialize
|
||||
res(await Promise.all(users.map(async user =>
|
||||
await serialize(user, me, { detail: true }))));
|
||||
});
|
116
src/api/endpoints/users/search.js
Normal file
116
src/api/endpoints/users/search.js
Normal file
@ -0,0 +1,116 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import * as mongo from 'mongodb';
|
||||
import User from '../../models/user';
|
||||
import serialize from '../../serializers/user';
|
||||
const escapeRegexp = require('escape-regexp');
|
||||
|
||||
/**
|
||||
* Search a user
|
||||
*
|
||||
* @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');
|
||||
}
|
||||
|
||||
// Get 'offset' parameter
|
||||
let offset = params.offset;
|
||||
if (offset !== undefined && offset !== null) {
|
||||
offset = parseInt(offset, 10);
|
||||
} else {
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
// Get 'max' parameter
|
||||
let max = params.max;
|
||||
if (max !== undefined && max !== null) {
|
||||
max = parseInt(max, 10);
|
||||
|
||||
// From 1 to 30
|
||||
if (!(1 <= max && max <= 30)) {
|
||||
return rej('invalid max range');
|
||||
}
|
||||
} else {
|
||||
max = 10;
|
||||
}
|
||||
|
||||
// If Elasticsearch is available, search by it
|
||||
// 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
|
||||
const users = await User
|
||||
.find({
|
||||
$or: [{
|
||||
username_lower: new RegExp(escapedQuery.toLowerCase())
|
||||
}, {
|
||||
name: new RegExp(escapedQuery)
|
||||
}]
|
||||
})
|
||||
.toArray();
|
||||
|
||||
// Serialize
|
||||
res(await Promise.all(users.map(async user =>
|
||||
await serialize(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
|
||||
}
|
||||
})
|
||||
.toArray();
|
||||
|
||||
// Serialize
|
||||
res(await Promise.all(users.map(async user =>
|
||||
await serialize(user, me, { detail: true }))));
|
||||
});
|
||||
}
|
65
src/api/endpoints/users/search_by_username.js
Normal file
65
src/api/endpoints/users/search_by_username.js
Normal file
@ -0,0 +1,65 @@
|
||||
'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 }))));
|
||||
});
|
49
src/api/endpoints/users/show.js
Normal file
49
src/api/endpoints/users/show.js
Normal file
@ -0,0 +1,49 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import * as mongo from 'mongodb';
|
||||
import User from '../../models/user';
|
||||
import serialize from '../../serializers/user';
|
||||
|
||||
/**
|
||||
* Show a user
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} me
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = (params, me) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'user_id' parameter
|
||||
let userId = params.user_id;
|
||||
if (userId === undefined || userId === null || userId === '') {
|
||||
userId = null;
|
||||
}
|
||||
|
||||
// Get 'username' parameter
|
||||
let username = params.username;
|
||||
if (username === undefined || username === null || username === '') {
|
||||
username = null;
|
||||
}
|
||||
|
||||
if (userId === null && username === null) {
|
||||
return rej('user_id or username is required');
|
||||
}
|
||||
|
||||
// Lookup user
|
||||
const user = userId !== null
|
||||
? await User.findOne({ _id: new mongo.ObjectID(userId) })
|
||||
: await User.findOne({ username_lower: username.toLowerCase() });
|
||||
|
||||
if (user === null) {
|
||||
return rej('user not found');
|
||||
}
|
||||
|
||||
// Send response
|
||||
res(await serialize(user, me, {
|
||||
detail: true
|
||||
}));
|
||||
});
|
Reference in New Issue
Block a user