Introduce processor

This commit is contained in:
Akihiko Odaki
2018-03-29 01:20:40 +09:00
parent 68ce6d5748
commit 90f8fe7e53
582 changed files with 246 additions and 188 deletions

View File

@ -0,0 +1,56 @@
import * as express from 'express';
import { Endpoint } from './endpoints';
import authenticate from './authenticate';
import { IAuthContext } from './authenticate';
import _reply from './reply';
import limitter from './limitter';
export default async (endpoint: Endpoint, req: express.Request, res: express.Response) => {
const reply = _reply.bind(null, res);
let ctx: IAuthContext;
// Authentication
try {
ctx = await authenticate(req);
} catch (e) {
return reply(403, 'AUTHENTICATION_FAILED');
}
if (endpoint.secure && !ctx.isSecure) {
return reply(403, 'ACCESS_DENIED');
}
if (endpoint.withCredential && ctx.user == null) {
return reply(401, 'PLZ_SIGNIN');
}
if (ctx.app && endpoint.kind) {
if (!ctx.app.permission.some(p => p === endpoint.kind)) {
return reply(403, 'ACCESS_DENIED');
}
}
if (endpoint.withCredential && endpoint.limit) {
try {
await limitter(endpoint, ctx); // Rate limit
} catch (e) {
// drop request if limit exceeded
return reply(429);
}
}
let exec = require(`${__dirname}/endpoints/${endpoint.name}`);
if (endpoint.withFile) {
exec = exec.bind(null, req.file);
}
// API invoking
try {
const res = await exec(req.body, ctx.user, ctx.app, ctx.isSecure);
reply(res);
} catch (e) {
reply(400, e);
}
};

View File

@ -0,0 +1,69 @@
import * as express from 'express';
import App from './models/app';
import { default as User, IUser } from './models/user';
import AccessToken from './models/access-token';
import isNativeToken from './common/is-native-token';
export interface IAuthContext {
/**
* App which requested
*/
app: any;
/**
* Authenticated user
*/
user: IUser;
/**
* Whether requested with a User-Native Token
*/
isSecure: boolean;
}
export default (req: express.Request) => new Promise<IAuthContext>(async (resolve, reject) => {
const token = req.body['i'] as string;
if (token == null) {
return resolve({
app: null,
user: null,
isSecure: false
});
}
if (isNativeToken(token)) {
const user: IUser = await User
.findOne({ 'account.token': token });
if (user === null) {
return reject('user not found');
}
return resolve({
app: null,
user: user,
isSecure: true
});
} else {
const accessToken = await AccessToken.findOne({
hash: token.toLowerCase()
});
if (accessToken === null) {
return reject('invalid signature');
}
const app = await App
.findOne({ _id: accessToken.app_id });
const user = await User
.findOne({ _id: accessToken.user_id });
return resolve({
app: app,
user: user,
isSecure: false
});
}
});

438
src/server/api/bot/core.ts Normal file
View File

@ -0,0 +1,438 @@
import * as EventEmitter from 'events';
import * as bcrypt from 'bcryptjs';
import User, { ILocalAccount, IUser, init as initUser } from '../models/user';
import getPostSummary from '../../common/get-post-summary';
import getUserSummary from '../../common/user/get-summary';
import parseAcct from '../../common/user/parse-acct';
import getNotificationSummary from '../../common/get-notification-summary';
const hmm = [
'',
'ふぅ~む...',
'ちょっと何言ってるかわからないです',
'「ヘルプ」と言うと利用可能な操作が確認できますよ'
];
/**
* Botの頭脳
*/
export default class BotCore extends EventEmitter {
public user: IUser = null;
private context: Context = null;
constructor(user?: IUser) {
super();
this.user = user;
}
public clearContext() {
this.setContext(null);
}
public setContext(context: Context) {
this.context = context;
this.emit('updated');
if (context) {
context.on('updated', () => {
this.emit('updated');
});
}
}
public export() {
return {
user: this.user,
context: this.context ? this.context.export() : null
};
}
protected _import(data) {
this.user = data.user ? initUser(data.user) : null;
this.setContext(data.context ? Context.import(this, data.context) : null);
}
public static import(data) {
const bot = new BotCore();
bot._import(data);
return bot;
}
public async q(query: string): Promise<string> {
if (this.context != null) {
return await this.context.q(query);
}
if (/^@[a-zA-Z0-9-]+$/.test(query)) {
return await this.showUserCommand(query);
}
switch (query) {
case 'ping':
return 'PONG';
case 'help':
case 'ヘルプ':
return '利用可能なコマンド一覧です:\n' +
'help: これです\n' +
'me: アカウント情報を見ます\n' +
'login, signin: サインインします\n' +
'logout, signout: サインアウトします\n' +
'post: 投稿します\n' +
'tl: タイムラインを見ます\n' +
'no: 通知を見ます\n' +
'@<ユーザー名>: ユーザーを表示します\n' +
'\n' +
'タイムラインや通知を見た後、「次」というとさらに遡ることができます。';
case 'me':
return this.user ? `${this.user.name}としてサインインしています。\n\n${getUserSummary(this.user)}` : 'サインインしていません';
case 'login':
case 'signin':
case 'ログイン':
case 'サインイン':
if (this.user != null) return '既にサインインしていますよ!';
this.setContext(new SigninContext(this));
return await this.context.greet();
case 'logout':
case 'signout':
case 'ログアウト':
case 'サインアウト':
if (this.user == null) return '今はサインインしてないですよ!';
this.signout();
return 'ご利用ありがとうございました <3';
case 'post':
case '投稿':
if (this.user == null) return 'まずサインインしてください。';
this.setContext(new PostContext(this));
return await this.context.greet();
case 'tl':
case 'タイムライン':
if (this.user == null) return 'まずサインインしてください。';
this.setContext(new TlContext(this));
return await this.context.greet();
case 'no':
case 'notifications':
case '通知':
if (this.user == null) return 'まずサインインしてください。';
this.setContext(new NotificationsContext(this));
return await this.context.greet();
case 'guessing-game':
case '数当てゲーム':
this.setContext(new GuessingGameContext(this));
return await this.context.greet();
default:
return hmm[Math.floor(Math.random() * hmm.length)];
}
}
public signin(user: IUser) {
this.user = user;
this.emit('signin', user);
this.emit('updated');
}
public signout() {
const user = this.user;
this.user = null;
this.emit('signout', user);
this.emit('updated');
}
public async refreshUser() {
this.user = await User.findOne({
_id: this.user._id
}, {
fields: {
data: false
}
});
this.emit('updated');
}
public async showUserCommand(q: string): Promise<string> {
try {
const user = await require('../endpoints/users/show')(parseAcct(q.substr(1)), this.user);
const text = getUserSummary(user);
return text;
} catch (e) {
return `問題が発生したようです...: ${e}`;
}
}
}
abstract class Context extends EventEmitter {
protected bot: BotCore;
public abstract async greet(): Promise<string>;
public abstract async q(query: string): Promise<string>;
public abstract export(): any;
constructor(bot: BotCore) {
super();
this.bot = bot;
}
public static import(bot: BotCore, data: any) {
if (data.type == 'guessing-game') return GuessingGameContext.import(bot, data.content);
if (data.type == 'post') return PostContext.import(bot, data.content);
if (data.type == 'tl') return TlContext.import(bot, data.content);
if (data.type == 'notifications') return NotificationsContext.import(bot, data.content);
if (data.type == 'signin') return SigninContext.import(bot, data.content);
return null;
}
}
class SigninContext extends Context {
private temporaryUser: IUser = null;
public async greet(): Promise<string> {
return 'まずユーザー名を教えてください:';
}
public async q(query: string): Promise<string> {
if (this.temporaryUser == null) {
// Fetch user
const user: IUser = await User.findOne({
username_lower: query.toLowerCase(),
host: null
}, {
fields: {
data: false
}
});
if (user === null) {
return `${query}というユーザーは存在しませんでした... もう一度教えてください:`;
} else {
this.temporaryUser = user;
this.emit('updated');
return `パスワードを教えてください:`;
}
} else {
// Compare password
const same = await bcrypt.compare(query, (this.temporaryUser.account as ILocalAccount).password);
if (same) {
this.bot.signin(this.temporaryUser);
this.bot.clearContext();
return `${this.temporaryUser.name}さん、おかえりなさい!`;
} else {
return `パスワードが違います... もう一度教えてください:`;
}
}
}
public export() {
return {
type: 'signin',
content: {
temporaryUser: this.temporaryUser
}
};
}
public static import(bot: BotCore, data: any) {
const context = new SigninContext(bot);
context.temporaryUser = data.temporaryUser;
return context;
}
}
class PostContext extends Context {
public async greet(): Promise<string> {
return '内容:';
}
public async q(query: string): Promise<string> {
await require('../endpoints/posts/create')({
text: query
}, this.bot.user);
this.bot.clearContext();
return '投稿しましたよ!';
}
public export() {
return {
type: 'post'
};
}
public static import(bot: BotCore, data: any) {
const context = new PostContext(bot);
return context;
}
}
class TlContext extends Context {
private next: string = null;
public async greet(): Promise<string> {
return await this.getTl();
}
public async q(query: string): Promise<string> {
if (query == '次') {
return await this.getTl();
} else {
this.bot.clearContext();
return await this.bot.q(query);
}
}
private async getTl() {
const tl = await require('../endpoints/posts/timeline')({
limit: 5,
until_id: this.next ? this.next : undefined
}, this.bot.user);
if (tl.length > 0) {
this.next = tl[tl.length - 1].id;
this.emit('updated');
const text = tl
.map(post => `${post.user.name}\n「${getPostSummary(post)}`)
.join('\n-----\n');
return text;
} else {
return 'タイムラインに表示するものがありません...';
}
}
public export() {
return {
type: 'tl',
content: {
next: this.next,
}
};
}
public static import(bot: BotCore, data: any) {
const context = new TlContext(bot);
context.next = data.next;
return context;
}
}
class NotificationsContext extends Context {
private next: string = null;
public async greet(): Promise<string> {
return await this.getNotifications();
}
public async q(query: string): Promise<string> {
if (query == '次') {
return await this.getNotifications();
} else {
this.bot.clearContext();
return await this.bot.q(query);
}
}
private async getNotifications() {
const notifications = await require('../endpoints/i/notifications')({
limit: 5,
until_id: this.next ? this.next : undefined
}, this.bot.user);
if (notifications.length > 0) {
this.next = notifications[notifications.length - 1].id;
this.emit('updated');
const text = notifications
.map(notification => getNotificationSummary(notification))
.join('\n-----\n');
return text;
} else {
return '通知はありません';
}
}
public export() {
return {
type: 'notifications',
content: {
next: this.next,
}
};
}
public static import(bot: BotCore, data: any) {
const context = new NotificationsContext(bot);
context.next = data.next;
return context;
}
}
class GuessingGameContext extends Context {
private secret: number;
private history: number[] = [];
public async greet(): Promise<string> {
this.secret = Math.floor(Math.random() * 100);
this.emit('updated');
return '0~100の秘密の数を当ててみてください:';
}
public async q(query: string): Promise<string> {
if (query == 'やめる') {
this.bot.clearContext();
return 'やめました。';
}
const guess = parseInt(query, 10);
if (isNaN(guess)) {
return '整数で推測してください。「やめる」と言うとゲームをやめます。';
}
const firsttime = this.history.indexOf(guess) === -1;
this.history.push(guess);
this.emit('updated');
if (this.secret < guess) {
return firsttime ? `${guess}よりも小さいですね` : `もう一度言いますが${guess}より小さいですよ`;
} else if (this.secret > guess) {
return firsttime ? `${guess}よりも大きいですね` : `もう一度言いますが${guess}より大きいですよ`;
} else {
this.bot.clearContext();
return `正解です🎉 (${this.history.length}回目で当てました)`;
}
}
public export() {
return {
type: 'guessing-game',
content: {
secret: this.secret,
history: this.history
}
};
}
public static import(bot: BotCore, data: any) {
const context = new GuessingGameContext(bot);
context.secret = data.secret;
context.history = data.history;
return context;
}
}

View File

@ -0,0 +1,238 @@
import * as EventEmitter from 'events';
import * as express from 'express';
import * as request from 'request';
import * as crypto from 'crypto';
import User from '../../models/user';
import config from '../../../../conf';
import BotCore from '../core';
import _redis from '../../../../db/redis';
import prominence = require('prominence');
import getAcct from '../../../common/user/get-acct';
import parseAcct from '../../../common/user/parse-acct';
import getPostSummary from '../../../common/get-post-summary';
const redis = prominence(_redis);
// SEE: https://developers.line.me/media/messaging-api/messages/sticker_list.pdf
const stickers = [
'297',
'298',
'299',
'300',
'301',
'302',
'303',
'304',
'305',
'306',
'307'
];
class LineBot extends BotCore {
private replyToken: string;
private reply(messages: any[]) {
request.post({
url: 'https://api.line.me/v2/bot/message/reply',
headers: {
'Authorization': `Bearer ${config.line_bot.channel_access_token}`
},
json: {
replyToken: this.replyToken,
messages: messages
}
}, (err, res, body) => {
if (err) {
console.error(err);
return;
}
});
}
public async react(ev: any): Promise<void> {
this.replyToken = ev.replyToken;
switch (ev.type) {
// メッセージ
case 'message':
switch (ev.message.type) {
// テキスト
case 'text':
const res = await this.q(ev.message.text);
if (res == null) return;
// 返信
this.reply([{
type: 'text',
text: res
}]);
break;
// スタンプ
case 'sticker':
// スタンプで返信
this.reply([{
type: 'sticker',
packageId: '4',
stickerId: stickers[Math.floor(Math.random() * stickers.length)]
}]);
break;
}
break;
// postback
case 'postback':
const data = ev.postback.data;
const cmd = data.split('|')[0];
const arg = data.split('|')[1];
switch (cmd) {
case 'showtl':
this.showUserTimelinePostback(arg);
break;
}
break;
}
}
public static import(data) {
const bot = new LineBot();
bot._import(data);
return bot;
}
public async showUserCommand(q: string) {
const user = await require('../../endpoints/users/show')(parseAcct(q.substr(1)), this.user);
const acct = getAcct(user);
const actions = [];
actions.push({
type: 'postback',
label: 'タイムラインを見る',
data: `showtl|${user.id}`
});
if (user.account.twitter) {
actions.push({
type: 'uri',
label: 'Twitterアカウントを見る',
uri: `https://twitter.com/${user.account.twitter.screen_name}`
});
}
actions.push({
type: 'uri',
label: 'Webで見る',
uri: `${config.url}/@${acct}`
});
this.reply([{
type: 'template',
altText: await super.showUserCommand(q),
template: {
type: 'buttons',
thumbnailImageUrl: `${user.avatar_url}?thumbnail&size=1024`,
title: `${user.name} (@${acct})`,
text: user.description || '(no description)',
actions: actions
}
}]);
return null;
}
public async showUserTimelinePostback(userId: string) {
const tl = await require('../../endpoints/users/posts')({
user_id: userId,
limit: 5
}, this.user);
const text = `${tl[0].user.name}さんのタイムラインはこちらです:\n\n` + tl
.map(post => getPostSummary(post))
.join('\n-----\n');
this.reply([{
type: 'text',
text: text
}]);
}
}
module.exports = async (app: express.Application) => {
if (config.line_bot == null) return;
const handler = new EventEmitter();
handler.on('event', async (ev) => {
const sourceId = ev.source.userId;
const sessionId = `line-bot-sessions:${sourceId}`;
const session = await redis.get(sessionId);
let bot: LineBot;
if (session == null) {
const user = await User.findOne({
host: null,
'account.line': {
user_id: sourceId
}
});
bot = new LineBot(user);
bot.on('signin', user => {
User.update(user._id, {
$set: {
'account.line': {
user_id: sourceId
}
}
});
});
bot.on('signout', user => {
User.update(user._id, {
$set: {
'account.line': {
user_id: null
}
}
});
});
redis.set(sessionId, JSON.stringify(bot.export()));
} else {
bot = LineBot.import(JSON.parse(session));
}
bot.on('updated', () => {
redis.set(sessionId, JSON.stringify(bot.export()));
});
if (session != null) bot.refreshUser();
bot.react(ev);
});
app.post('/hooks/line', (req, res, next) => {
// req.headers['x-line-signature'] は常に string ですが、型定義の都合上
// string | string[] になっているので string を明示しています
const sig1 = req.headers['x-line-signature'] as string;
const hash = crypto.createHmac('SHA256', config.line_bot.channel_secret)
.update((req as any).rawBody);
const sig2 = hash.digest('base64');
// シグネチャ比較
if (sig1 === sig2) {
req.body.events.forEach(ev => {
handler.emit('event', ev);
});
res.sendStatus(200);
} else {
res.sendStatus(400);
}
});
};

View File

@ -0,0 +1,307 @@
import { Buffer } from 'buffer';
import * as fs from 'fs';
import * as tmp from 'tmp';
import * as stream from 'stream';
import * as mongodb from 'mongodb';
import * as crypto from 'crypto';
import * as _gm from 'gm';
import * as debug from 'debug';
import fileType = require('file-type');
import prominence = require('prominence');
import DriveFile, { getGridFSBucket } from '../../models/drive-file';
import DriveFolder from '../../models/drive-folder';
import { pack } from '../../models/drive-file';
import event, { publishDriveStream } from '../../event';
import getAcct from '../../../common/user/get-acct';
import config from '../../../../conf';
const gm = _gm.subClass({
imageMagick: true
});
const log = debug('misskey:drive:add-file');
const tmpFile = (): Promise<string> => new Promise((resolve, reject) => {
tmp.file((e, path) => {
if (e) return reject(e);
resolve(path);
});
});
const addToGridFS = (name: string, readable: stream.Readable, type: string, metadata: any): Promise<any> =>
getGridFSBucket()
.then(bucket => new Promise((resolve, reject) => {
const writeStream = bucket.openUploadStream(name, { contentType: type, metadata });
writeStream.once('finish', (doc) => { resolve(doc); });
writeStream.on('error', reject);
readable.pipe(writeStream);
}));
const addFile = async (
user: any,
path: string,
name: string = null,
comment: string = null,
folderId: mongodb.ObjectID = null,
force: boolean = false
) => {
log(`registering ${name} (user: ${getAcct(user)}, path: ${path})`);
// Calculate hash, get content type and get file size
const [hash, [mime, ext], size] = await Promise.all([
// hash
((): Promise<string> => new Promise((res, rej) => {
const readable = fs.createReadStream(path);
const hash = crypto.createHash('md5');
const chunks = [];
readable
.on('error', rej)
.pipe(hash)
.on('error', rej)
.on('data', (chunk) => chunks.push(chunk))
.on('end', () => {
const buffer = Buffer.concat(chunks);
res(buffer.toString('hex'));
});
}))(),
// mime
((): Promise<[string, string | null]> => new Promise((res, rej) => {
const readable = fs.createReadStream(path);
readable
.on('error', rej)
.once('data', (buffer: Buffer) => {
readable.destroy();
const type = fileType(buffer);
if (type) {
return res([type.mime, type.ext]);
} else {
// 種類が同定できなかったら application/octet-stream にする
return res(['application/octet-stream', null]);
}
});
}))(),
// size
((): Promise<number> => new Promise((res, rej) => {
fs.stat(path, (err, stats) => {
if (err) return rej(err);
res(stats.size);
});
}))()
]);
log(`hash: ${hash}, mime: ${mime}, ext: ${ext}, size: ${size}`);
// detect name
const detectedName: string = name || (ext ? `untitled.${ext}` : 'untitled');
if (!force) {
// Check if there is a file with the same hash
const much = await DriveFile.findOne({
md5: hash,
'metadata.user_id': user._id
});
if (much !== null) {
log('file with same hash is found');
return much;
} else {
log('file with same hash is not found');
}
}
const [wh, averageColor, folder] = await Promise.all([
// Width and height (when image)
(async () => {
// 画像かどうか
if (!/^image\/.*$/.test(mime)) {
return null;
}
const imageType = mime.split('/')[1];
// 画像でもPNGかJPEGかGIFでないならスキップ
if (imageType != 'png' && imageType != 'jpeg' && imageType != 'gif') {
return null;
}
log('calculate image width and height...');
// Calculate width and height
const g = gm(fs.createReadStream(path), name);
const size = await prominence(g).size();
log(`image width and height is calculated: ${size.width}, ${size.height}`);
return [size.width, size.height];
})(),
// average color (when image)
(async () => {
// 画像かどうか
if (!/^image\/.*$/.test(mime)) {
return null;
}
const imageType = mime.split('/')[1];
// 画像でもPNGかJPEGでないならスキップ
if (imageType != 'png' && imageType != 'jpeg') {
return null;
}
log('calculate average color...');
const buffer = await prominence(gm(fs.createReadStream(path), name)
.setFormat('ppm')
.resize(1, 1)) // 1pxのサイズに縮小して平均色を取得するというハック
.toBuffer();
const r = buffer.readUInt8(buffer.length - 3);
const g = buffer.readUInt8(buffer.length - 2);
const b = buffer.readUInt8(buffer.length - 1);
log(`average color is calculated: ${r}, ${g}, ${b}`);
return [r, g, b];
})(),
// folder
(async () => {
if (!folderId) {
return null;
}
const driveFolder = await DriveFolder.findOne({
_id: folderId,
user_id: user._id
});
if (!driveFolder) {
throw 'folder-not-found';
}
return driveFolder;
})(),
// usage checker
(async () => {
// Calculate drive usage
const usage = await DriveFile
.aggregate([{
$match: { 'metadata.user_id': user._id }
}, {
$project: {
length: true
}
}, {
$group: {
_id: null,
usage: { $sum: '$length' }
}
}])
.then((aggregates: any[]) => {
if (aggregates.length > 0) {
return aggregates[0].usage;
}
return 0;
});
log(`drive usage is ${usage}`);
// If usage limit exceeded
if (usage + size > user.drive_capacity) {
throw 'no-free-space';
}
})()
]);
const readable = fs.createReadStream(path);
const properties = {};
if (wh) {
properties['width'] = wh[0];
properties['height'] = wh[1];
}
if (averageColor) {
properties['average_color'] = averageColor;
}
return addToGridFS(detectedName, readable, mime, {
user_id: user._id,
folder_id: folder !== null ? folder._id : null,
comment: comment,
properties: properties
});
};
/**
* Add file to drive
*
* @param user User who wish to add file
* @param file File path or readableStream
* @param comment Comment
* @param type File type
* @param folderId Folder ID
* @param force If set to true, forcibly upload the file even if there is a file with the same hash.
* @return Object that represents added file
*/
export default (user: any, file: string | stream.Readable, ...args) => new Promise<any>((resolve, reject) => {
// Get file path
new Promise((res: (v: [string, boolean]) => void, rej) => {
if (typeof file === 'string') {
res([file, false]);
return;
}
if (typeof file === 'object' && typeof file.read === 'function') {
tmpFile()
.then(path => {
const readable: stream.Readable = file;
const writable = fs.createWriteStream(path);
readable
.on('error', rej)
.on('end', () => {
res([path, true]);
})
.pipe(writable)
.on('error', rej);
})
.catch(rej);
}
rej(new Error('un-compatible file.'));
})
.then(([path, shouldCleanup]): Promise<any> => new Promise((res, rej) => {
addFile(user, path, ...args)
.then(file => {
res(file);
if (shouldCleanup) {
fs.unlink(path, (e) => {
if (e) log(e.stack);
});
}
})
.catch(rej);
}))
.then(file => {
log(`drive file has been created ${file._id}`);
resolve(file);
pack(file).then(serializedFile => {
// Publish drive_file_created event
event(user._id, 'drive_file_created', serializedFile);
publishDriveStream(user._id, 'file_created', serializedFile);
// Register to search database
if (config.elasticsearch.enable) {
const es = require('../../db/elasticsearch');
es.index({
index: 'misskey',
type: 'drive_file',
id: file._id.toString(),
body: {
name: file.name,
user_id: user._id.toString()
}
});
}
});
})
.catch(reject);
});

View File

@ -0,0 +1,46 @@
import * as URL from 'url';
import { IDriveFile, validateFileName } from '../../models/drive-file';
import create from './add-file';
import * as debug from 'debug';
import * as tmp from 'tmp';
import * as fs from 'fs';
import * as request from 'request';
const log = debug('misskey:common:drive:upload_from_url');
export default async (url, user, folderId = null): Promise<IDriveFile> => {
let name = URL.parse(url).pathname.split('/').pop();
if (!validateFileName(name)) {
name = null;
}
// Create temp file
const path = await new Promise((res: (string) => void, rej) => {
tmp.file((e, path) => {
if (e) return rej(e);
res(path);
});
});
// write content at URL to temp file
await new Promise((res, rej) => {
const writable = fs.createWriteStream(path);
request(url)
.on('error', rej)
.on('end', () => {
writable.close();
res(path);
})
.pipe(writable)
.on('error', rej);
});
const driveFile = await create(user, path, name, null, folderId);
// clean-up
fs.unlink(path, (e) => {
if (e) log(e.stack);
});
return driveFile;
};

View File

@ -0,0 +1,3 @@
import rndstr from 'rndstr';
export default () => `!${rndstr('a-zA-Z0-9', 32)}`;

View File

@ -0,0 +1,26 @@
import * as mongodb from 'mongodb';
import Following from '../models/following';
export default async (me: mongodb.ObjectID, includeMe: boolean = true) => {
// Fetch relation to other users who the I follows
// SELECT followee
const myfollowing = await Following
.find({
follower_id: me,
// 削除されたドキュメントは除く
deleted_at: { $exists: false }
}, {
fields: {
followee_id: true
}
});
// ID list of other users who the I follows
const myfollowingIds = myfollowing.map(follow => follow.followee_id);
if (includeMe) {
myfollowingIds.push(me);
}
return myfollowingIds;
};

View File

@ -0,0 +1,5 @@
import { toUnicode } from 'punycode';
export default host => {
return toUnicode(host).replace(/[A-Z]+/, match => match.toLowerCase());
};

View File

@ -0,0 +1 @@
export default (token: string) => token[0] == '!';

View File

@ -0,0 +1,50 @@
import * as mongo from 'mongodb';
import Notification from '../models/notification';
import Mute from '../models/mute';
import event from '../event';
import { pack } from '../models/notification';
export default (
notifiee: mongo.ObjectID,
notifier: mongo.ObjectID,
type: string,
content?: any
) => new Promise<any>(async (resolve, reject) => {
if (notifiee.equals(notifier)) {
return resolve();
}
// Create notification
const notification = await Notification.insert(Object.assign({
created_at: new Date(),
notifiee_id: notifiee,
notifier_id: notifier,
type: type,
is_read: false
}, content));
resolve(notification);
// Publish notification event
event(notifiee, 'notification',
await pack(notification));
// 3秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する
setTimeout(async () => {
const fresh = await Notification.findOne({ _id: notification._id }, { is_read: true });
if (!fresh.is_read) {
//#region ただしミュートしているユーザーからの通知なら無視
const mute = await Mute.find({
muter_id: notifiee,
deleted_at: { $exists: false }
});
const mutedUserIds = mute.map(m => m.mutee_id.toString());
if (mutedUserIds.indexOf(notifier.toString()) != -1) {
return;
}
//#endregion
event(notifiee, 'unread_notification', await pack(notification));
}
}, 3000);
});

View File

@ -0,0 +1,52 @@
const push = require('web-push');
import * as mongo from 'mongodb';
import Subscription from '../models/sw-subscription';
import config from '../../../conf';
if (config.sw) {
// アプリケーションの連絡先と、サーバーサイドの鍵ペアの情報を登録
push.setVapidDetails(
config.maintainer.url,
config.sw.public_key,
config.sw.private_key);
}
export default async function(userId: mongo.ObjectID | string, type, body?) {
if (!config.sw) return;
if (typeof userId === 'string') {
userId = new mongo.ObjectID(userId);
}
// Fetch
const subscriptions = await Subscription.find({
user_id: userId
});
subscriptions.forEach(subscription => {
const pushSubscription = {
endpoint: subscription.endpoint,
keys: {
auth: subscription.auth,
p256dh: subscription.publickey
}
};
push.sendNotification(pushSubscription, JSON.stringify({
type, body
})).catch(err => {
//console.log(err.statusCode);
//console.log(err.headers);
//console.log(err.body);
if (err.statusCode == 410) {
Subscription.remove({
user_id: userId,
endpoint: subscription.endpoint,
auth: subscription.auth,
publickey: subscription.publickey
});
}
});
});
}

View File

@ -0,0 +1,66 @@
import * as mongo from 'mongodb';
import Message from '../models/messaging-message';
import { IMessagingMessage as IMessage } from '../models/messaging-message';
import publishUserStream from '../event';
import { publishMessagingStream } from '../event';
import { publishMessagingIndexStream } from '../event';
/**
* Mark as read message(s)
*/
export default (
user: string | mongo.ObjectID,
otherparty: string | mongo.ObjectID,
message: string | string[] | IMessage | IMessage[] | mongo.ObjectID | mongo.ObjectID[]
) => new Promise<any>(async (resolve, reject) => {
const userId = mongo.ObjectID.prototype.isPrototypeOf(user)
? user
: new mongo.ObjectID(user);
const otherpartyId = mongo.ObjectID.prototype.isPrototypeOf(otherparty)
? otherparty
: new mongo.ObjectID(otherparty);
const ids: mongo.ObjectID[] = Array.isArray(message)
? mongo.ObjectID.prototype.isPrototypeOf(message[0])
? (message as mongo.ObjectID[])
: typeof message[0] === 'string'
? (message as string[]).map(m => new mongo.ObjectID(m))
: (message as IMessage[]).map(m => m._id)
: mongo.ObjectID.prototype.isPrototypeOf(message)
? [(message as mongo.ObjectID)]
: typeof message === 'string'
? [new mongo.ObjectID(message)]
: [(message as IMessage)._id];
// Update documents
await Message.update({
_id: { $in: ids },
user_id: otherpartyId,
recipient_id: userId,
is_read: false
}, {
$set: {
is_read: true
}
}, {
multi: true
});
// Publish event
publishMessagingStream(otherpartyId, userId, 'read', ids.map(id => id.toString()));
publishMessagingIndexStream(userId, 'read', ids.map(id => id.toString()));
// Calc count of my unread messages
const count = await Message
.count({
recipient_id: userId,
is_read: false
});
if (count == 0) {
// 全ての(いままで未読だった)自分宛てのメッセージを(これで)読みましたよというイベントを発行
publishUserStream(userId, 'read_all_messaging_messages');
}
});

View File

@ -0,0 +1,52 @@
import * as mongo from 'mongodb';
import { default as Notification, INotification } from '../models/notification';
import publishUserStream from '../event';
/**
* Mark as read notification(s)
*/
export default (
user: string | mongo.ObjectID,
message: string | string[] | INotification | INotification[] | mongo.ObjectID | mongo.ObjectID[]
) => new Promise<any>(async (resolve, reject) => {
const userId = mongo.ObjectID.prototype.isPrototypeOf(user)
? user
: new mongo.ObjectID(user);
const ids: mongo.ObjectID[] = Array.isArray(message)
? mongo.ObjectID.prototype.isPrototypeOf(message[0])
? (message as mongo.ObjectID[])
: typeof message[0] === 'string'
? (message as string[]).map(m => new mongo.ObjectID(m))
: (message as INotification[]).map(m => m._id)
: mongo.ObjectID.prototype.isPrototypeOf(message)
? [(message as mongo.ObjectID)]
: typeof message === 'string'
? [new mongo.ObjectID(message)]
: [(message as INotification)._id];
// Update documents
await Notification.update({
_id: { $in: ids },
is_read: false
}, {
$set: {
is_read: true
}
}, {
multi: true
});
// Calc count of my unread notifications
const count = await Notification
.count({
notifiee_id: userId,
is_read: false
});
if (count == 0) {
// 全ての(いままで未読だった)通知を(これで)読みましたよというイベントを発行
publishUserStream(userId, 'read_all_notifications');
}
});

View File

@ -0,0 +1,19 @@
import config from '../../../conf';
export default function(res, user, redirect: boolean) {
const expires = 1000 * 60 * 60 * 24 * 365; // One Year
res.cookie('i', user.account.token, {
path: '/',
domain: `.${config.hostname}`,
secure: config.url.substr(0, 5) === 'https',
httpOnly: false,
expires: new Date(Date.now() + expires),
maxAge: expires
});
if (redirect) {
res.redirect(config.url);
} else {
res.sendStatus(204);
}
}

View File

@ -0,0 +1,334 @@
function escape(text) {
return text
.replace(/>/g, '&gt;')
.replace(/</g, '&lt;');
}
// 文字数が多い順にソートします
// そうしないと、「function」という文字列が与えられたときに「func」が先にマッチしてしまう可能性があるためです
const _keywords = [
'true',
'false',
'null',
'nil',
'undefined',
'void',
'var',
'const',
'let',
'mut',
'dim',
'if',
'then',
'else',
'switch',
'match',
'case',
'default',
'for',
'each',
'in',
'while',
'loop',
'continue',
'break',
'do',
'goto',
'next',
'end',
'sub',
'throw',
'try',
'catch',
'finally',
'enum',
'delegate',
'function',
'func',
'fun',
'fn',
'return',
'yield',
'async',
'await',
'require',
'include',
'import',
'imports',
'export',
'exports',
'from',
'as',
'using',
'use',
'internal',
'module',
'namespace',
'where',
'select',
'struct',
'union',
'new',
'delete',
'this',
'super',
'base',
'class',
'interface',
'abstract',
'static',
'public',
'private',
'protected',
'virtual',
'partial',
'override',
'extends',
'implements',
'constructor'
];
const keywords = _keywords
.concat(_keywords.map(k => k[0].toUpperCase() + k.substr(1)))
.concat(_keywords.map(k => k.toUpperCase()))
.sort((a, b) => b.length - a.length);
const symbols = [
'=',
'+',
'-',
'*',
'/',
'%',
'~',
'^',
'&',
'|',
'>',
'<',
'!',
'?'
];
const elements = [
// comment
code => {
if (code.substr(0, 2) != '//') return null;
const match = code.match(/^\/\/(.+?)(\n|$)/);
if (!match) return null;
const comment = match[0];
return {
html: `<span class="comment">${escape(comment)}</span>`,
next: comment.length
};
},
// block comment
code => {
const match = code.match(/^\/\*([\s\S]+?)\*\//);
if (!match) return null;
return {
html: `<span class="comment">${escape(match[0])}</span>`,
next: match[0].length
};
},
// string
code => {
if (!/^['"`]/.test(code)) return null;
const begin = code[0];
let str = begin;
let thisIsNotAString = false;
for (let i = 1; i < code.length; i++) {
const char = code[i];
if (char == '\\') {
str += char;
str += code[i + 1] || '';
i++;
continue;
} else if (char == begin) {
str += char;
break;
} else if (char == '\n' || i == (code.length - 1)) {
thisIsNotAString = true;
break;
} else {
str += char;
}
}
if (thisIsNotAString) {
return null;
} else {
return {
html: `<span class="string">${escape(str)}</span>`,
next: str.length
};
}
},
// regexp
code => {
if (code[0] != '/') return null;
let regexp = '';
let thisIsNotARegexp = false;
for (let i = 1; i < code.length; i++) {
const char = code[i];
if (char == '\\') {
regexp += char;
regexp += code[i + 1] || '';
i++;
continue;
} else if (char == '/') {
break;
} else if (char == '\n' || i == (code.length - 1)) {
thisIsNotARegexp = true;
break;
} else {
regexp += char;
}
}
if (thisIsNotARegexp) return null;
if (regexp == '') return null;
if (regexp[0] == ' ' && regexp[regexp.length - 1] == ' ') return null;
return {
html: `<span class="regexp">/${escape(regexp)}/</span>`,
next: regexp.length + 2
};
},
// label
code => {
if (code[0] != '@') return null;
const match = code.match(/^@([a-zA-Z_-]+?)\n/);
if (!match) return null;
const label = match[0];
return {
html: `<span class="label">${label}</span>`,
next: label.length
};
},
// number
(code, i, source) => {
const prev = source[i - 1];
if (prev && /[a-zA-Z]/.test(prev)) return null;
if (!/^[\-\+]?[0-9\.]+/.test(code)) return null;
const match = code.match(/^[\-\+]?[0-9\.]+/)[0];
if (match) {
return {
html: `<span class="number">${match}</span>`,
next: match.length
};
} else {
return null;
}
},
// nan
(code, i, source) => {
const prev = source[i - 1];
if (prev && /[a-zA-Z]/.test(prev)) return null;
if (code.substr(0, 3) == 'NaN') {
return {
html: `<span class="nan">NaN</span>`,
next: 3
};
} else {
return null;
}
},
// method
code => {
const match = code.match(/^([a-zA-Z_-]+?)\(/);
if (!match) return null;
if (match[1] == '-') return null;
return {
html: `<span class="method">${match[1]}</span>`,
next: match[1].length
};
},
// property
(code, i, source) => {
const prev = source[i - 1];
if (prev != '.') return null;
const match = code.match(/^[a-zA-Z0-9_-]+/);
if (!match) return null;
return {
html: `<span class="property">${match[0]}</span>`,
next: match[0].length
};
},
// keyword
(code, i, source) => {
const prev = source[i - 1];
if (prev && /[a-zA-Z]/.test(prev)) return null;
const match = keywords.filter(k => code.substr(0, k.length) == k)[0];
if (match) {
if (/^[a-zA-Z]/.test(code.substr(match.length))) return null;
return {
html: `<span class="keyword ${match}">${match}</span>`,
next: match.length
};
} else {
return null;
}
},
// symbol
code => {
const match = symbols.filter(s => code[0] == s)[0];
if (match) {
return {
html: `<span class="symbol">${match}</span>`,
next: 1
};
} else {
return null;
}
}
];
// specify lang is todo
export default (source: string, lang?: string) => {
let code = source;
let html = '';
let i = 0;
function push(token) {
html += token.html;
code = code.substr(token.next);
i += token.next;
}
while (code != '') {
const parsed = elements.some(el => {
const e = el(code, i, source);
if (e) {
push(e);
return true;
} else {
return false;
}
});
if (!parsed) {
push({
html: escape(code[0]),
next: 1
});
}
}
return html;
};

View File

@ -0,0 +1,14 @@
/**
* Bold
*/
module.exports = text => {
const match = text.match(/^\*\*(.+?)\*\*/);
if (!match) return null;
const bold = match[0];
return {
type: 'bold',
content: bold,
bold: bold.substr(2, bold.length - 4)
};
};

View File

@ -0,0 +1,17 @@
/**
* Code (block)
*/
import genHtml from '../core/syntax-highlighter';
module.exports = text => {
const match = text.match(/^```([\s\S]+?)```/);
if (!match) return null;
const code = match[0];
return {
type: 'code',
content: code,
code: code.substr(3, code.length - 6).trim(),
html: genHtml(code.substr(3, code.length - 6).trim())
};
};

View File

@ -0,0 +1,14 @@
/**
* Emoji
*/
module.exports = text => {
const match = text.match(/^:[a-zA-Z0-9+-_]+:/);
if (!match) return null;
const emoji = match[0];
return {
type: 'emoji',
content: emoji,
emoji: emoji.substr(1, emoji.length - 2)
};
};

View File

@ -0,0 +1,19 @@
/**
* Hashtag
*/
module.exports = (text, i) => {
if (!(/^\s#[^\s]+/.test(text) || (i == 0 && /^#[^\s]+/.test(text)))) return null;
const isHead = text[0] == '#';
const hashtag = text.match(/^\s?#[^\s]+/)[0];
const res: any[] = !isHead ? [{
type: 'text',
content: text[0]
}] : [];
res.push({
type: 'hashtag',
content: isHead ? hashtag : hashtag.substr(1),
hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2)
});
return res;
};

View File

@ -0,0 +1,17 @@
/**
* Code (inline)
*/
import genHtml from '../core/syntax-highlighter';
module.exports = text => {
const match = text.match(/^`(.+?)`/);
if (!match) return null;
const code = match[0];
return {
type: 'inline-code',
content: code,
code: code.substr(1, code.length - 2).trim(),
html: genHtml(code.substr(1, code.length - 2).trim())
};
};

View File

@ -0,0 +1,19 @@
/**
* Link
*/
module.exports = text => {
const match = text.match(/^\??\[([^\[\]]+?)\]\((https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+?)\)/);
if (!match) return null;
const silent = text[0] == '?';
const link = match[0];
const title = match[1];
const url = match[2];
return {
type: 'link',
content: link,
title: title,
url: url,
silent: silent
};
};

View File

@ -0,0 +1,17 @@
/**
* Mention
*/
import parseAcct from '../../../../common/user/parse-acct';
module.exports = text => {
const match = text.match(/^(?:@[a-zA-Z0-9\-]+){1,2}/);
if (!match) return null;
const mention = match[0];
const { username, host } = parseAcct(mention.substr(1));
return {
type: 'mention',
content: mention,
username,
host
};
};

View File

@ -0,0 +1,14 @@
/**
* Quoted text
*/
module.exports = text => {
const match = text.match(/^"([\s\S]+?)\n"/);
if (!match) return null;
const quote = match[0];
return {
type: 'quote',
content: quote,
quote: quote.substr(1, quote.length - 2).trim(),
};
};

View File

@ -0,0 +1,14 @@
/**
* URL
*/
module.exports = text => {
const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/);
if (!match) return null;
const url = match[0];
return {
type: 'url',
content: url,
url: url
};
};

View File

@ -0,0 +1,72 @@
/**
* Misskey Text Analyzer
*/
const elements = [
require('./elements/bold'),
require('./elements/url'),
require('./elements/link'),
require('./elements/mention'),
require('./elements/hashtag'),
require('./elements/code'),
require('./elements/inline-code'),
require('./elements/quote'),
require('./elements/emoji')
];
export default (source: string) => {
if (source == '') {
return null;
}
const tokens = [];
function push(token) {
if (token != null) {
tokens.push(token);
source = source.substr(token.content.length);
}
}
let i = 0;
// パース
while (source != '') {
const parsed = elements.some(el => {
let _tokens = el(source, i);
if (_tokens) {
if (!Array.isArray(_tokens)) {
_tokens = [_tokens];
}
_tokens.forEach(push);
return true;
} else {
return false;
}
});
if (!parsed) {
push({
type: 'text',
content: source[0]
});
}
i++;
}
// テキストを纏める
tokens[0] = [tokens[0]];
return tokens.reduce((a, b) => {
if (a[a.length - 1].type == 'text' && b.type == 'text') {
const tail = a.pop();
return a.concat({
type: 'text',
content: tail.content + b.content
});
} else {
return a.concat(b);
}
});
};

View File

@ -0,0 +1,26 @@
import * as mongodb from 'mongodb';
import Watching from '../models/post-watching';
export default async (me: mongodb.ObjectID, post: object) => {
// 自分の投稿はwatchできない
if (me.equals((post as any).user_id)) {
return;
}
// if watching now
const exist = await Watching.findOne({
post_id: (post as any)._id,
user_id: me,
deleted_at: { $exists: false }
});
if (exist !== null) {
return;
}
await Watching.insert({
created_at: new Date(),
post_id: (post as any)._id,
user_id: me
});
};

584
src/server/api/endpoints.ts Normal file
View File

@ -0,0 +1,584 @@
const ms = require('ms');
/**
* エンドポイントを表します。
*/
export type Endpoint = {
/**
* エンドポイント名
*/
name: string;
/**
* このエンドポイントにリクエストするのにユーザー情報が必須か否か
* 省略した場合は false として解釈されます。
*/
withCredential?: boolean;
/**
* エンドポイントのリミテーションに関するやつ
* 省略した場合はリミテーションは無いものとして解釈されます。
* また、withCredential が false の場合はリミテーションを行うことはできません。
*/
limit?: {
/**
* 複数のエンドポイントでリミットを共有したい場合に指定するキー
*/
key?: string;
/**
* リミットを適用する期間(ms)
* このプロパティを設定する場合、max プロパティも設定する必要があります。
*/
duration?: number;
/**
* durationで指定した期間内にいくつまでリクエストできるのか
* このプロパティを設定する場合、duration プロパティも設定する必要があります。
*/
max?: number;
/**
* 最低でもどれくらいの間隔を開けてリクエストしなければならないか(ms)
*/
minInterval?: number;
};
/**
* ファイルの添付を必要とするか否か
* 省略した場合は false として解釈されます。
*/
withFile?: boolean;
/**
* サードパーティアプリからはリクエストすることができないか否か
* 省略した場合は false として解釈されます。
*/
secure?: boolean;
/**
* エンドポイントの種類
* パーミッションの実現に利用されます。
*/
kind?: string;
};
const endpoints: Endpoint[] = [
{
name: 'meta'
},
{
name: 'stats'
},
{
name: 'username/available'
},
{
name: 'my/apps',
withCredential: true
},
{
name: 'app/create',
withCredential: true,
limit: {
duration: ms('1day'),
max: 3
}
},
{
name: 'app/show'
},
{
name: 'app/name_id/available'
},
{
name: 'auth/session/generate'
},
{
name: 'auth/session/show'
},
{
name: 'auth/session/userkey'
},
{
name: 'auth/accept',
withCredential: true,
secure: true
},
{
name: 'auth/deny',
withCredential: true,
secure: true
},
{
name: 'aggregation/posts',
},
{
name: 'aggregation/users',
},
{
name: 'aggregation/users/activity',
},
{
name: 'aggregation/users/post',
},
{
name: 'aggregation/users/followers'
},
{
name: 'aggregation/users/following'
},
{
name: 'aggregation/users/reaction'
},
{
name: 'aggregation/posts/repost'
},
{
name: 'aggregation/posts/reply'
},
{
name: 'aggregation/posts/reaction'
},
{
name: 'aggregation/posts/reactions'
},
{
name: 'sw/register',
withCredential: true
},
{
name: 'i',
withCredential: true
},
{
name: 'i/2fa/register',
withCredential: true,
secure: true
},
{
name: 'i/2fa/unregister',
withCredential: true,
secure: true
},
{
name: 'i/2fa/done',
withCredential: true,
secure: true
},
{
name: 'i/update',
withCredential: true,
limit: {
duration: ms('1day'),
max: 50
},
kind: 'account-write'
},
{
name: 'i/update_home',
withCredential: true,
secure: true
},
{
name: 'i/update_mobile_home',
withCredential: true,
secure: true
},
{
name: 'i/change_password',
withCredential: true,
secure: true
},
{
name: 'i/regenerate_token',
withCredential: true,
secure: true
},
{
name: 'i/update_client_setting',
withCredential: true,
secure: true
},
{
name: 'i/pin',
kind: 'account-write'
},
{
name: 'i/appdata/get',
withCredential: true
},
{
name: 'i/appdata/set',
withCredential: true
},
{
name: 'i/signin_history',
withCredential: true,
kind: 'account-read'
},
{
name: 'i/authorized_apps',
withCredential: true,
secure: true
},
{
name: 'i/notifications',
withCredential: true,
kind: 'notification-read'
},
{
name: 'othello/match',
withCredential: true
},
{
name: 'othello/match/cancel',
withCredential: true
},
{
name: 'othello/invitations',
withCredential: true
},
{
name: 'othello/games',
withCredential: true
},
{
name: 'othello/games/show'
},
{
name: 'mute/create',
withCredential: true,
kind: 'account/write'
},
{
name: 'mute/delete',
withCredential: true,
kind: 'account/write'
},
{
name: 'mute/list',
withCredential: true,
kind: 'account/read'
},
{
name: 'notifications/get_unread_count',
withCredential: true,
kind: 'notification-read'
},
{
name: 'notifications/delete',
withCredential: true,
kind: 'notification-write'
},
{
name: 'notifications/delete_all',
withCredential: true,
kind: 'notification-write'
},
{
name: 'notifications/mark_as_read_all',
withCredential: true,
kind: 'notification-write'
},
{
name: 'drive',
withCredential: true,
kind: 'drive-read'
},
{
name: 'drive/stream',
withCredential: true,
kind: 'drive-read'
},
{
name: 'drive/files',
withCredential: true,
kind: 'drive-read'
},
{
name: 'drive/files/create',
withCredential: true,
limit: {
duration: ms('1hour'),
max: 100
},
withFile: true,
kind: 'drive-write'
},
{
name: 'drive/files/upload_from_url',
withCredential: true,
limit: {
duration: ms('1hour'),
max: 10
},
kind: 'drive-write'
},
{
name: 'drive/files/show',
withCredential: true,
kind: 'drive-read'
},
{
name: 'drive/files/find',
withCredential: true,
kind: 'drive-read'
},
{
name: 'drive/files/delete',
withCredential: true,
kind: 'drive-write'
},
{
name: 'drive/files/update',
withCredential: true,
kind: 'drive-write'
},
{
name: 'drive/folders',
withCredential: true,
kind: 'drive-read'
},
{
name: 'drive/folders/create',
withCredential: true,
limit: {
duration: ms('1hour'),
max: 50
},
kind: 'drive-write'
},
{
name: 'drive/folders/show',
withCredential: true,
kind: 'drive-read'
},
{
name: 'drive/folders/find',
withCredential: true,
kind: 'drive-read'
},
{
name: 'drive/folders/update',
withCredential: true,
kind: 'drive-write'
},
{
name: 'users'
},
{
name: 'users/show'
},
{
name: 'users/search'
},
{
name: 'users/search_by_username'
},
{
name: 'users/posts'
},
{
name: 'users/following'
},
{
name: 'users/followers'
},
{
name: 'users/recommendation',
withCredential: true,
kind: 'account-read'
},
{
name: 'users/get_frequently_replied_users'
},
{
name: 'following/create',
withCredential: true,
limit: {
duration: ms('1hour'),
max: 100
},
kind: 'following-write'
},
{
name: 'following/delete',
withCredential: true,
limit: {
duration: ms('1hour'),
max: 100
},
kind: 'following-write'
},
{
name: 'posts'
},
{
name: 'posts/show'
},
{
name: 'posts/replies'
},
{
name: 'posts/context'
},
{
name: 'posts/create',
withCredential: true,
limit: {
duration: ms('1hour'),
max: 120,
minInterval: ms('1second')
},
kind: 'post-write'
},
{
name: 'posts/reposts'
},
{
name: 'posts/search'
},
{
name: 'posts/timeline',
withCredential: true,
limit: {
duration: ms('10minutes'),
max: 100
}
},
{
name: 'posts/mentions',
withCredential: true,
limit: {
duration: ms('10minutes'),
max: 100
}
},
{
name: 'posts/trend',
withCredential: true
},
{
name: 'posts/categorize',
withCredential: true
},
{
name: 'posts/reactions',
withCredential: true
},
{
name: 'posts/reactions/create',
withCredential: true,
limit: {
duration: ms('1hour'),
max: 100
},
kind: 'reaction-write'
},
{
name: 'posts/reactions/delete',
withCredential: true,
limit: {
duration: ms('1hour'),
max: 100
},
kind: 'reaction-write'
},
{
name: 'posts/favorites/create',
withCredential: true,
limit: {
duration: ms('1hour'),
max: 100
},
kind: 'favorite-write'
},
{
name: 'posts/favorites/delete',
withCredential: true,
limit: {
duration: ms('1hour'),
max: 100
},
kind: 'favorite-write'
},
{
name: 'posts/polls/vote',
withCredential: true,
limit: {
duration: ms('1hour'),
max: 100
},
kind: 'vote-write'
},
{
name: 'posts/polls/recommendation',
withCredential: true
},
{
name: 'messaging/history',
withCredential: true,
kind: 'messaging-read'
},
{
name: 'messaging/unread',
withCredential: true,
kind: 'messaging-read'
},
{
name: 'messaging/messages',
withCredential: true,
kind: 'messaging-read'
},
{
name: 'messaging/messages/create',
withCredential: true,
kind: 'messaging-write'
},
{
name: 'channels/create',
withCredential: true,
limit: {
duration: ms('1hour'),
max: 3,
minInterval: ms('10seconds')
}
},
{
name: 'channels/show'
},
{
name: 'channels/posts'
},
{
name: 'channels/watch',
withCredential: true
},
{
name: 'channels/unwatch',
withCredential: true
},
{
name: 'channels'
},
];
export default endpoints;

View File

@ -0,0 +1,90 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Post from '../../models/post';
/**
* Aggregate posts
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = params => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 365, limitErr] = $(params.limit).optional.number().range(1, 365).$;
if (limitErr) return rej('invalid limit param');
const datas = await Post
.aggregate([
{ $project: {
repost_id: '$repost_id',
reply_id: '$reply_id',
created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST
}},
{ $project: {
date: {
year: { $year: '$created_at' },
month: { $month: '$created_at' },
day: { $dayOfMonth: '$created_at' }
},
type: {
$cond: {
if: { $ne: ['$repost_id', null] },
then: 'repost',
else: {
$cond: {
if: { $ne: ['$reply_id', null] },
then: 'reply',
else: 'post'
}
}
}
}}
},
{ $group: { _id: {
date: '$date',
type: '$type'
}, count: { $sum: 1 } } },
{ $group: {
_id: '$_id.date',
data: { $addToSet: {
type: '$_id.type',
count: '$count'
}}
} }
]);
datas.forEach(data => {
data.date = data._id;
delete data._id;
data.posts = (data.data.filter(x => x.type == 'post')[0] || { count: 0 }).count;
data.reposts = (data.data.filter(x => x.type == 'repost')[0] || { count: 0 }).count;
data.replies = (data.data.filter(x => x.type == 'reply')[0] || { count: 0 }).count;
delete data.data;
});
const graph = [];
for (let i = 0; i < limit; i++) {
const day = new Date(new Date().setDate(new Date().getDate() - i));
const data = datas.filter(d =>
d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate()
)[0];
if (data) {
graph.push(data);
} else {
graph.push({
posts: 0,
reposts: 0,
replies: 0
});
}
}
res(graph);
});

View File

@ -0,0 +1,76 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Post from '../../../models/post';
import Reaction from '../../../models/post-reaction';
/**
* Aggregate reaction of a post
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = (params) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Lookup post
const post = await Post.findOne({
_id: postId
});
if (post === null) {
return rej('post not found');
}
const datas = await Reaction
.aggregate([
{ $match: { post_id: post._id } },
{ $project: {
created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST
}},
{ $project: {
date: {
year: { $year: '$created_at' },
month: { $month: '$created_at' },
day: { $dayOfMonth: '$created_at' }
}
}},
{ $group: {
_id: '$date',
count: { $sum: 1 }
}}
]);
datas.forEach(data => {
data.date = data._id;
delete data._id;
});
const graph = [];
for (let i = 0; i < 30; i++) {
const day = new Date(new Date().setDate(new Date().getDate() - i));
const data = datas.filter(d =>
d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate()
)[0];
if (data) {
graph.push(data);
} else {
graph.push({
date: {
year: day.getFullYear(),
month: day.getMonth() + 1, // In JavaScript, month is zero-based.
day: day.getDate()
},
count: 0
});
}
}
res(graph);
});

View File

@ -0,0 +1,72 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Post from '../../../models/post';
import Reaction from '../../../models/post-reaction';
/**
* Aggregate reactions of a post
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = (params) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Lookup post
const post = await Post.findOne({
_id: postId
});
if (post === null) {
return rej('post not found');
}
const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1));
const reactions = await Reaction
.find({
post_id: post._id,
$or: [
{ deleted_at: { $exists: false } },
{ deleted_at: { $gt: startTime } }
]
}, {
sort: {
_id: -1
},
fields: {
_id: false,
post_id: false
}
});
const graph = [];
for (let i = 0; i < 30; i++) {
let day = new Date(new Date().setDate(new Date().getDate() - i));
day = new Date(day.setMilliseconds(999));
day = new Date(day.setSeconds(59));
day = new Date(day.setMinutes(59));
day = new Date(day.setHours(23));
// day = day.getTime();
const count = reactions.filter(r =>
r.created_at < day && (r.deleted_at == null || r.deleted_at > day)
).length;
graph.push({
date: {
year: day.getFullYear(),
month: day.getMonth() + 1, // In JavaScript, month is zero-based.
day: day.getDate()
},
count: count
});
}
res(graph);
});

View File

@ -0,0 +1,75 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Post from '../../../models/post';
/**
* Aggregate reply of a post
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = (params) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Lookup post
const post = await Post.findOne({
_id: postId
});
if (post === null) {
return rej('post not found');
}
const datas = await Post
.aggregate([
{ $match: { reply: post._id } },
{ $project: {
created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST
}},
{ $project: {
date: {
year: { $year: '$created_at' },
month: { $month: '$created_at' },
day: { $dayOfMonth: '$created_at' }
}
}},
{ $group: {
_id: '$date',
count: { $sum: 1 }
}}
]);
datas.forEach(data => {
data.date = data._id;
delete data._id;
});
const graph = [];
for (let i = 0; i < 30; i++) {
const day = new Date(new Date().setDate(new Date().getDate() - i));
const data = datas.filter(d =>
d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate()
)[0];
if (data) {
graph.push(data);
} else {
graph.push({
date: {
year: day.getFullYear(),
month: day.getMonth() + 1, // In JavaScript, month is zero-based.
day: day.getDate()
},
count: 0
});
}
}
res(graph);
});

View File

@ -0,0 +1,75 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Post from '../../../models/post';
/**
* Aggregate repost of a post
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = (params) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Lookup post
const post = await Post.findOne({
_id: postId
});
if (post === null) {
return rej('post not found');
}
const datas = await Post
.aggregate([
{ $match: { repost_id: post._id } },
{ $project: {
created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST
}},
{ $project: {
date: {
year: { $year: '$created_at' },
month: { $month: '$created_at' },
day: { $dayOfMonth: '$created_at' }
}
}},
{ $group: {
_id: '$date',
count: { $sum: 1 }
}}
]);
datas.forEach(data => {
data.date = data._id;
delete data._id;
});
const graph = [];
for (let i = 0; i < 30; i++) {
const day = new Date(new Date().setDate(new Date().getDate() - i));
const data = datas.filter(d =>
d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate()
)[0];
if (data) {
graph.push(data);
} else {
graph.push({
date: {
year: day.getFullYear(),
month: day.getMonth() + 1, // In JavaScript, month is zero-based.
day: day.getDate()
},
count: 0
});
}
}
res(graph);
});

View File

@ -0,0 +1,61 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User from '../../models/user';
/**
* Aggregate users
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = params => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 365, limitErr] = $(params.limit).optional.number().range(1, 365).$;
if (limitErr) return rej('invalid limit param');
const users = await User
.find({}, {
sort: {
_id: -1
},
fields: {
_id: false,
created_at: true,
deleted_at: true
}
});
const graph = [];
for (let i = 0; i < limit; i++) {
let dayStart = new Date(new Date().setDate(new Date().getDate() - i));
dayStart = new Date(dayStart.setMilliseconds(0));
dayStart = new Date(dayStart.setSeconds(0));
dayStart = new Date(dayStart.setMinutes(0));
dayStart = new Date(dayStart.setHours(0));
let dayEnd = new Date(new Date().setDate(new Date().getDate() - i));
dayEnd = new Date(dayEnd.setMilliseconds(999));
dayEnd = new Date(dayEnd.setSeconds(59));
dayEnd = new Date(dayEnd.setMinutes(59));
dayEnd = new Date(dayEnd.setHours(23));
// day = day.getTime();
const total = users.filter(u =>
u.created_at < dayEnd && (u.deleted_at == null || u.deleted_at > dayEnd)
).length;
const created = users.filter(u =>
u.created_at < dayEnd && u.created_at > dayStart
).length;
graph.push({
total: total,
created: created
});
}
res(graph);
});

View File

@ -0,0 +1,116 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User from '../../../models/user';
import Post from '../../../models/post';
// TODO: likeやfollowも集計
/**
* Aggregate activity of a user
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = (params) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 365, limitErr] = $(params.limit).optional.number().range(1, 365).$;
if (limitErr) return rej('invalid limit param');
// Get 'user_id' parameter
const [userId, userIdErr] = $(params.user_id).id().$;
if (userIdErr) return rej('invalid user_id param');
// Lookup user
const user = await User.findOne({
_id: userId
}, {
fields: {
_id: true
}
});
if (user === null) {
return rej('user not found');
}
const datas = await Post
.aggregate([
{ $match: { user_id: user._id } },
{ $project: {
repost_id: '$repost_id',
reply_id: '$reply_id',
created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST
}},
{ $project: {
date: {
year: { $year: '$created_at' },
month: { $month: '$created_at' },
day: { $dayOfMonth: '$created_at' }
},
type: {
$cond: {
if: { $ne: ['$repost_id', null] },
then: 'repost',
else: {
$cond: {
if: { $ne: ['$reply_id', null] },
then: 'reply',
else: 'post'
}
}
}
}}
},
{ $group: { _id: {
date: '$date',
type: '$type'
}, count: { $sum: 1 } } },
{ $group: {
_id: '$_id.date',
data: { $addToSet: {
type: '$_id.type',
count: '$count'
}}
} }
]);
datas.forEach(data => {
data.date = data._id;
delete data._id;
data.posts = (data.data.filter(x => x.type == 'post')[0] || { count: 0 }).count;
data.reposts = (data.data.filter(x => x.type == 'repost')[0] || { count: 0 }).count;
data.replies = (data.data.filter(x => x.type == 'reply')[0] || { count: 0 }).count;
delete data.data;
});
const graph = [];
for (let i = 0; i < limit; i++) {
const day = new Date(new Date().setDate(new Date().getDate() - i));
const data = datas.filter(d =>
d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate()
)[0];
if (data) {
graph.push(data);
} else {
graph.push({
date: {
year: day.getFullYear(),
month: day.getMonth() + 1, // In JavaScript, month is zero-based.
day: day.getDate()
},
posts: 0,
reposts: 0,
replies: 0
});
}
}
res(graph);
});

View File

@ -0,0 +1,74 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User from '../../../models/user';
import Following from '../../../models/following';
/**
* Aggregate followers of a user
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = (params) => new Promise(async (res, rej) => {
// Get 'user_id' parameter
const [userId, userIdErr] = $(params.user_id).id().$;
if (userIdErr) return rej('invalid user_id param');
// Lookup user
const user = await User.findOne({
_id: userId
}, {
fields: {
_id: true
}
});
if (user === null) {
return rej('user not found');
}
const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1));
const following = await Following
.find({
followee_id: user._id,
$or: [
{ deleted_at: { $exists: false } },
{ deleted_at: { $gt: startTime } }
]
}, {
_id: false,
follower_id: false,
followee_id: false
}, {
sort: { created_at: -1 }
});
const graph = [];
for (let i = 0; i < 30; i++) {
let day = new Date(new Date().setDate(new Date().getDate() - i));
day = new Date(day.setMilliseconds(999));
day = new Date(day.setSeconds(59));
day = new Date(day.setMinutes(59));
day = new Date(day.setHours(23));
// day = day.getTime();
const count = following.filter(f =>
f.created_at < day && (f.deleted_at == null || f.deleted_at > day)
).length;
graph.push({
date: {
year: day.getFullYear(),
month: day.getMonth() + 1, // In JavaScript, month is zero-based.
day: day.getDate()
},
count: count
});
}
res(graph);
});

View File

@ -0,0 +1,73 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User from '../../../models/user';
import Following from '../../../models/following';
/**
* Aggregate following of a user
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = (params) => new Promise(async (res, rej) => {
// Get 'user_id' parameter
const [userId, userIdErr] = $(params.user_id).id().$;
if (userIdErr) return rej('invalid user_id param');
// Lookup user
const user = await User.findOne({
_id: userId
}, {
fields: {
_id: true
}
});
if (user === null) {
return rej('user not found');
}
const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1));
const following = await Following
.find({
follower_id: user._id,
$or: [
{ deleted_at: { $exists: false } },
{ deleted_at: { $gt: startTime } }
]
}, {
_id: false,
follower_id: false,
followee_id: false
}, {
sort: { created_at: -1 }
});
const graph = [];
for (let i = 0; i < 30; i++) {
let day = new Date(new Date().setDate(new Date().getDate() - i));
day = new Date(day.setMilliseconds(999));
day = new Date(day.setSeconds(59));
day = new Date(day.setMinutes(59));
day = new Date(day.setHours(23));
const count = following.filter(f =>
f.created_at < day && (f.deleted_at == null || f.deleted_at > day)
).length;
graph.push({
date: {
year: day.getFullYear(),
month: day.getMonth() + 1, // In JavaScript, month is zero-based.
day: day.getDate()
},
count: count
});
}
res(graph);
});

View File

@ -0,0 +1,110 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User from '../../../models/user';
import Post from '../../../models/post';
/**
* Aggregate post of a user
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = (params) => new Promise(async (res, rej) => {
// Get 'user_id' parameter
const [userId, userIdErr] = $(params.user_id).id().$;
if (userIdErr) return rej('invalid user_id param');
// Lookup user
const user = await User.findOne({
_id: userId
}, {
fields: {
_id: true
}
});
if (user === null) {
return rej('user not found');
}
const datas = await Post
.aggregate([
{ $match: { user_id: user._id } },
{ $project: {
repost_id: '$repost_id',
reply_id: '$reply_id',
created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST
}},
{ $project: {
date: {
year: { $year: '$created_at' },
month: { $month: '$created_at' },
day: { $dayOfMonth: '$created_at' }
},
type: {
$cond: {
if: { $ne: ['$repost_id', null] },
then: 'repost',
else: {
$cond: {
if: { $ne: ['$reply_id', null] },
then: 'reply',
else: 'post'
}
}
}
}}
},
{ $group: { _id: {
date: '$date',
type: '$type'
}, count: { $sum: 1 } } },
{ $group: {
_id: '$_id.date',
data: { $addToSet: {
type: '$_id.type',
count: '$count'
}}
} }
]);
datas.forEach(data => {
data.date = data._id;
delete data._id;
data.posts = (data.data.filter(x => x.type == 'post')[0] || { count: 0 }).count;
data.reposts = (data.data.filter(x => x.type == 'repost')[0] || { count: 0 }).count;
data.replies = (data.data.filter(x => x.type == 'reply')[0] || { count: 0 }).count;
delete data.data;
});
const graph = [];
for (let i = 0; i < 30; i++) {
const day = new Date(new Date().setDate(new Date().getDate() - i));
const data = datas.filter(d =>
d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate()
)[0];
if (data) {
graph.push(data);
} else {
graph.push({
date: {
year: day.getFullYear(),
month: day.getMonth() + 1, // In JavaScript, month is zero-based.
day: day.getDate()
},
posts: 0,
reposts: 0,
replies: 0
});
}
}
res(graph);
});

View File

@ -0,0 +1,80 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User from '../../../models/user';
import Reaction from '../../../models/post-reaction';
/**
* Aggregate reaction of a user
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = (params) => new Promise(async (res, rej) => {
// Get 'user_id' parameter
const [userId, userIdErr] = $(params.user_id).id().$;
if (userIdErr) return rej('invalid user_id param');
// Lookup user
const user = await User.findOne({
_id: userId
}, {
fields: {
_id: true
}
});
if (user === null) {
return rej('user not found');
}
const datas = await Reaction
.aggregate([
{ $match: { user_id: user._id } },
{ $project: {
created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST
}},
{ $project: {
date: {
year: { $year: '$created_at' },
month: { $month: '$created_at' },
day: { $dayOfMonth: '$created_at' }
}
}},
{ $group: {
_id: '$date',
count: { $sum: 1 }
}}
]);
datas.forEach(data => {
data.date = data._id;
delete data._id;
});
const graph = [];
for (let i = 0; i < 30; i++) {
const day = new Date(new Date().setDate(new Date().getDate() - i));
const data = datas.filter(d =>
d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate()
)[0];
if (data) {
graph.push(data);
} else {
graph.push({
date: {
year: day.getFullYear(),
month: day.getMonth() + 1, // In JavaScript, month is zero-based.
day: day.getDate()
},
count: 0
});
}
}
res(graph);
});

View File

@ -0,0 +1,108 @@
/**
* Module dependencies
*/
import rndstr from 'rndstr';
import $ from 'cafy';
import App, { isValidNameId, pack } from '../../models/app';
/**
* @swagger
* /app/create:
* post:
* summary: Create an application
* parameters:
* - $ref: "#/parameters/AccessToken"
* -
* name: name_id
* description: Application unique name
* in: formData
* required: true
* type: string
* -
* name: name
* description: Application name
* in: formData
* required: true
* type: string
* -
* name: description
* description: Application description
* in: formData
* required: true
* type: string
* -
* name: permission
* description: Permissions that application has
* in: formData
* required: true
* type: array
* items:
* type: string
* collectionFormat: csv
* -
* name: callback_url
* description: URL called back after authentication
* in: formData
* required: false
* type: string
*
* responses:
* 200:
* description: Created application's information
* schema:
* $ref: "#/definitions/Application"
*
* default:
* description: Failed
* schema:
* $ref: "#/definitions/Error"
*/
/**
* Create an app
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = async (params, user) => new Promise(async (res, rej) => {
// Get 'name_id' parameter
const [nameId, nameIdErr] = $(params.name_id).string().pipe(isValidNameId).$;
if (nameIdErr) return rej('invalid name_id param');
// Get 'name' parameter
const [name, nameErr] = $(params.name).string().$;
if (nameErr) return rej('invalid name param');
// Get 'description' parameter
const [description, descriptionErr] = $(params.description).string().$;
if (descriptionErr) return rej('invalid description param');
// Get 'permission' parameter
const [permission, permissionErr] = $(params.permission).array('string').unique().$;
if (permissionErr) return rej('invalid permission param');
// Get 'callback_url' parameter
// TODO: Check it is valid url
const [callbackUrl = null, callbackUrlErr] = $(params.callback_url).optional.nullable.string().$;
if (callbackUrlErr) return rej('invalid callback_url param');
// Generate secret
const secret = rndstr('a-zA-Z0-9', 32);
// Create account
const app = await App.insert({
created_at: new Date(),
user_id: user._id,
name: name,
name_id: nameId,
name_id_lower: nameId.toLowerCase(),
description: description,
permission: permission,
callback_url: callbackUrl,
secret: secret
});
// Response
res(await pack(app));
});

View File

@ -0,0 +1,60 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import App from '../../../models/app';
import { isValidNameId } from '../../../models/app';
/**
* @swagger
* /app/name_id/available:
* post:
* summary: Check available name_id on creation an application
* parameters:
* -
* name: name_id
* description: Application unique name
* in: formData
* required: true
* type: string
*
* responses:
* 200:
* description: Success
* schema:
* type: object
* properties:
* available:
* description: Whether name_id is available
* type: boolean
*
* default:
* description: Failed
* schema:
* $ref: "#/definitions/Error"
*/
/**
* Check available name_id of app
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = async (params) => new Promise(async (res, rej) => {
// Get 'name_id' parameter
const [nameId, nameIdErr] = $(params.name_id).string().pipe(isValidNameId).$;
if (nameIdErr) return rej('invalid name_id param');
// Get exist
const exist = await App
.count({
name_id_lower: nameId.toLowerCase()
}, {
limit: 1
});
// Reply
res({
available: exist === 0
});
});

View File

@ -0,0 +1,72 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import App, { pack } from '../../models/app';
/**
* @swagger
* /app/show:
* post:
* summary: Show an application's information
* description: Require app_id or name_id
* parameters:
* -
* name: app_id
* description: Application ID
* in: formData
* type: string
* -
* name: name_id
* description: Application unique name
* in: formData
* type: string
*
* responses:
* 200:
* description: Success
* schema:
* $ref: "#/definitions/Application"
*
* default:
* description: Failed
* schema:
* $ref: "#/definitions/Error"
*/
/**
* Show an app
*
* @param {any} params
* @param {any} user
* @param {any} _
* @param {any} isSecure
* @return {Promise<any>}
*/
module.exports = (params, user, _, isSecure) => new Promise(async (res, rej) => {
// Get 'app_id' parameter
const [appId, appIdErr] = $(params.app_id).optional.id().$;
if (appIdErr) return rej('invalid app_id param');
// Get 'name_id' parameter
const [nameId, nameIdErr] = $(params.name_id).optional.string().$;
if (nameIdErr) return rej('invalid name_id param');
if (appId === undefined && nameId === undefined) {
return rej('app_id or name_id is required');
}
// Lookup app
const app = appId !== undefined
? await App.findOne({ _id: appId })
: await App.findOne({ name_id_lower: nameId.toLowerCase() });
if (app === null) {
return rej('app not found');
}
// Send response
res(await pack(app, user, {
includeSecret: isSecure && app.user_id.equals(user._id)
}));
});

View File

@ -0,0 +1,93 @@
/**
* Module dependencies
*/
import rndstr from 'rndstr';
const crypto = require('crypto');
import $ from 'cafy';
import App from '../../models/app';
import AuthSess from '../../models/auth-session';
import AccessToken from '../../models/access-token';
/**
* @swagger
* /auth/accept:
* post:
* summary: Accept a session
* parameters:
* - $ref: "#/parameters/NativeToken"
* -
* name: token
* description: Session Token
* in: formData
* required: true
* type: string
* responses:
* 204:
* description: OK
*
* default:
* description: Failed
* schema:
* $ref: "#/definitions/Error"
*/
/**
* Accept
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'token' parameter
const [token, tokenErr] = $(params.token).string().$;
if (tokenErr) return rej('invalid token param');
// Fetch token
const session = await AuthSess
.findOne({ token: token });
if (session === null) {
return rej('session not found');
}
// Generate access token
const accessToken = rndstr('a-zA-Z0-9', 32);
// Fetch exist access token
const exist = await AccessToken.findOne({
app_id: session.app_id,
user_id: user._id,
});
if (exist === null) {
// Lookup app
const app = await App.findOne({
_id: session.app_id
});
// Generate Hash
const sha256 = crypto.createHash('sha256');
sha256.update(accessToken + app.secret);
const hash = sha256.digest('hex');
// Insert access token doc
await AccessToken.insert({
created_at: new Date(),
app_id: session.app_id,
user_id: user._id,
token: accessToken,
hash: hash
});
}
// Update session
await AuthSess.update(session._id, {
$set: {
user_id: user._id
}
});
// Response
res();
});

View File

@ -0,0 +1,76 @@
/**
* Module dependencies
*/
import * as uuid from 'uuid';
import $ from 'cafy';
import App from '../../../models/app';
import AuthSess from '../../../models/auth-session';
import config from '../../../../../conf';
/**
* @swagger
* /auth/session/generate:
* post:
* summary: Generate a session
* parameters:
* -
* name: app_secret
* description: App Secret
* in: formData
* required: true
* type: string
*
* responses:
* 200:
* description: OK
* schema:
* type: object
* properties:
* token:
* type: string
* description: Session Token
* url:
* type: string
* description: Authentication form's URL
* default:
* description: Failed
* schema:
* $ref: "#/definitions/Error"
*/
/**
* Generate a session
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = (params) => new Promise(async (res, rej) => {
// Get 'app_secret' parameter
const [appSecret, appSecretErr] = $(params.app_secret).string().$;
if (appSecretErr) return rej('invalid app_secret param');
// Lookup app
const app = await App.findOne({
secret: appSecret
});
if (app == null) {
return rej('app not found');
}
// Generate token
const token = uuid.v4();
// Create session token document
const doc = await AuthSess.insert({
created_at: new Date(),
app_id: app._id,
token: token
});
// Response
res({
token: doc.token,
url: `${config.auth_url}/${doc.token}`
});
});

View File

@ -0,0 +1,70 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import AuthSess, { pack } from '../../../models/auth-session';
/**
* @swagger
* /auth/session/show:
* post:
* summary: Show a session information
* parameters:
* -
* name: token
* description: Session Token
* in: formData
* required: true
* type: string
*
* responses:
* 200:
* description: OK
* schema:
* type: object
* properties:
* created_at:
* type: string
* format: date-time
* description: Date and time of the session creation
* app_id:
* type: string
* description: Application ID
* token:
* type: string
* description: Session Token
* user_id:
* type: string
* description: ID of user who create the session
* app:
* $ref: "#/definitions/Application"
* default:
* description: Failed
* schema:
* $ref: "#/definitions/Error"
*/
/**
* Show a session
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'token' parameter
const [token, tokenErr] = $(params.token).string().$;
if (tokenErr) return rej('invalid token param');
// Lookup session
const session = await AuthSess.findOne({
token: token
});
if (session == null) {
return rej('session not found');
}
// Response
res(await pack(session, user));
});

View File

@ -0,0 +1,109 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import App from '../../../models/app';
import AuthSess from '../../../models/auth-session';
import AccessToken from '../../../models/access-token';
import { pack } from '../../../models/user';
/**
* @swagger
* /auth/session/userkey:
* post:
* summary: Get an access token(userkey)
* parameters:
* -
* name: app_secret
* description: App Secret
* in: formData
* required: true
* type: string
* -
* name: token
* description: Session Token
* in: formData
* required: true
* type: string
*
* responses:
* 200:
* description: OK
* schema:
* type: object
* properties:
* userkey:
* type: string
* description: Access Token
* user:
* $ref: "#/definitions/User"
* default:
* description: Failed
* schema:
* $ref: "#/definitions/Error"
*/
/**
* Generate a session
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = (params) => new Promise(async (res, rej) => {
// Get 'app_secret' parameter
const [appSecret, appSecretErr] = $(params.app_secret).string().$;
if (appSecretErr) return rej('invalid app_secret param');
// Lookup app
const app = await App.findOne({
secret: appSecret
});
if (app == null) {
return rej('app not found');
}
// Get 'token' parameter
const [token, tokenErr] = $(params.token).string().$;
if (tokenErr) return rej('invalid token param');
// Fetch token
const session = await AuthSess
.findOne({
token: token,
app_id: app._id
});
if (session === null) {
return rej('session not found');
}
if (session.user_id == null) {
return rej('this session is not allowed yet');
}
// Lookup access token
const accessToken = await AccessToken.findOne({
app_id: app._id,
user_id: session.user_id
});
// Delete session
/* https://github.com/Automattic/monk/issues/178
AuthSess.deleteOne({
_id: session._id
});
*/
AuthSess.remove({
_id: session._id
});
// Response
res({
access_token: accessToken.token,
user: await pack(session.user_id, null, {
detail: true
})
});
});

View File

@ -0,0 +1,58 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Channel, { pack } from '../models/channel';
/**
* Get all channels
*
* @param {any} params
* @param {any} me
* @return {Promise<any>}
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
// Get 'until_id' parameter
const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
if (untilIdErr) return rej('invalid until_id param');
// Check if both of since_id and until_id is specified
if (sinceId && untilId) {
return rej('cannot set since_id and until_id');
}
// Construct query
const sort = {
_id: -1
};
const query = {} as any;
if (sinceId) {
sort._id = 1;
query._id = {
$gt: sinceId
};
} else if (untilId) {
query._id = {
$lt: untilId
};
}
// Issue query
const channels = await Channel
.find(query, {
limit: limit,
sort: sort
});
// Serialize
res(await Promise.all(channels.map(async channel =>
await pack(channel, me))));
});

View File

@ -0,0 +1,39 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Channel from '../../models/channel';
import Watching from '../../models/channel-watching';
import { pack } from '../../models/channel';
/**
* Create a channel
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = async (params, user) => new Promise(async (res, rej) => {
// Get 'title' parameter
const [title, titleErr] = $(params.title).string().range(1, 100).$;
if (titleErr) return rej('invalid title param');
// Create a channel
const channel = await Channel.insert({
created_at: new Date(),
user_id: user._id,
title: title,
index: 0,
watching_count: 1
});
// Response
res(await pack(channel));
// Create Watching
await Watching.insert({
created_at: new Date(),
user_id: user._id,
channel_id: channel._id
});
});

View File

@ -0,0 +1,78 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import { default as Channel, IChannel } from '../../models/channel';
import Post, { pack } from '../../models/post';
/**
* Show a posts of a channel
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 1000, limitErr] = $(params.limit).optional.number().range(1, 1000).$;
if (limitErr) return rej('invalid limit param');
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
// Get 'until_id' parameter
const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
if (untilIdErr) return rej('invalid until_id param');
// Check if both of since_id and until_id is specified
if (sinceId && untilId) {
return rej('cannot set since_id and until_id');
}
// Get 'channel_id' parameter
const [channelId, channelIdErr] = $(params.channel_id).id().$;
if (channelIdErr) return rej('invalid channel_id param');
// Fetch channel
const channel: IChannel = await Channel.findOne({
_id: channelId
});
if (channel === null) {
return rej('channel not found');
}
//#region Construct query
const sort = {
_id: -1
};
const query = {
channel_id: channel._id
} as any;
if (sinceId) {
sort._id = 1;
query._id = {
$gt: sinceId
};
} else if (untilId) {
query._id = {
$lt: untilId
};
}
//#endregion Construct query
// Issue query
const posts = await Post
.find(query, {
limit: limit,
sort: sort
});
// Serialize
res(await Promise.all(posts.map(async (post) =>
await pack(post, user)
)));
});

View File

@ -0,0 +1,30 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Channel, { IChannel, pack } from '../../models/channel';
/**
* Show a channel
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'channel_id' parameter
const [channelId, channelIdErr] = $(params.channel_id).id().$;
if (channelIdErr) return rej('invalid channel_id param');
// Fetch channel
const channel: IChannel = await Channel.findOne({
_id: channelId
});
if (channel === null) {
return rej('channel not found');
}
// Serialize
res(await pack(channel, user));
});

View File

@ -0,0 +1,60 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Channel from '../../models/channel';
import Watching from '../../models/channel-watching';
/**
* Unwatch a channel
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'channel_id' parameter
const [channelId, channelIdErr] = $(params.channel_id).id().$;
if (channelIdErr) return rej('invalid channel_id param');
//#region Fetch channel
const channel = await Channel.findOne({
_id: channelId
});
if (channel === null) {
return rej('channel not found');
}
//#endregion
//#region Check whether not watching
const exist = await Watching.findOne({
user_id: user._id,
channel_id: channel._id,
deleted_at: { $exists: false }
});
if (exist === null) {
return rej('already not watching');
}
//#endregion
// Delete watching
await Watching.update({
_id: exist._id
}, {
$set: {
deleted_at: new Date()
}
});
// Send response
res();
// Decrement watching count
Channel.update(channel._id, {
$inc: {
watching_count: -1
}
});
});

View File

@ -0,0 +1,58 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Channel from '../../models/channel';
import Watching from '../../models/channel-watching';
/**
* Watch a channel
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'channel_id' parameter
const [channelId, channelIdErr] = $(params.channel_id).id().$;
if (channelIdErr) return rej('invalid channel_id param');
//#region Fetch channel
const channel = await Channel.findOne({
_id: channelId
});
if (channel === null) {
return rej('channel not found');
}
//#endregion
//#region Check whether already watching
const exist = await Watching.findOne({
user_id: user._id,
channel_id: channel._id,
deleted_at: { $exists: false }
});
if (exist !== null) {
return rej('already watching');
}
//#endregion
// Create Watching
await Watching.insert({
created_at: new Date(),
user_id: user._id,
channel_id: channel._id
});
// Send response
res();
// Increment watching count
Channel.update(channel._id, {
$inc: {
watching_count: 1
}
});
});

View File

@ -0,0 +1,37 @@
/**
* Module dependencies
*/
import DriveFile from '../models/drive-file';
/**
* Get drive information
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Calculate drive usage
const usage = ((await DriveFile
.aggregate([
{ $match: { 'metadata.user_id': user._id } },
{
$project: {
length: true
}
},
{
$group: {
_id: null,
usage: { $sum: '$length' }
}
}
]))[0] || {
usage: 0
}).usage;
res({
capacity: user.drive_capacity,
usage: usage
});
});

View File

@ -0,0 +1,73 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import DriveFile, { pack } from '../../models/drive-file';
/**
* Get drive files
*
* @param {any} params
* @param {any} user
* @param {any} app
* @return {Promise<any>}
*/
module.exports = async (params, user, app) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) throw 'invalid limit param';
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) throw 'invalid since_id param';
// Get 'until_id' parameter
const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
if (untilIdErr) throw 'invalid until_id param';
// Check if both of since_id and until_id is specified
if (sinceId && untilId) {
throw 'cannot set since_id and until_id';
}
// Get 'folder_id' parameter
const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
if (folderIdErr) throw 'invalid folder_id param';
// Get 'type' parameter
const [type, typeErr] = $(params.type).optional.string().match(/^[a-zA-Z\/\-\*]+$/).$;
if (typeErr) throw 'invalid type param';
// Construct query
const sort = {
_id: -1
};
const query = {
'metadata.user_id': user._id,
'metadata.folder_id': folderId
} as any;
if (sinceId) {
sort._id = 1;
query._id = {
$gt: sinceId
};
} else if (untilId) {
query._id = {
$lt: untilId
};
}
if (type) {
query.contentType = new RegExp(`^${type.replace(/\*/g, '.+?')}$`);
}
// Issue query
const files = await DriveFile
.find(query, {
limit: limit,
sort: sort
});
// Serialize
const _files = await Promise.all(files.map(file => pack(file)));
return _files;
};

View File

@ -0,0 +1,51 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import { validateFileName, pack } from '../../../models/drive-file';
import create from '../../../common/drive/add-file';
/**
* Create a file
*
* @param {any} file
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = async (file, params, user): Promise<any> => {
if (file == null) {
throw 'file is required';
}
// Get 'name' parameter
let name = file.originalname;
if (name !== undefined && name !== null) {
name = name.trim();
if (name.length === 0) {
name = null;
} else if (name === 'blob') {
name = null;
} else if (!validateFileName(name)) {
throw 'invalid name';
}
} else {
name = null;
}
// Get 'folder_id' parameter
const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
if (folderIdErr) throw 'invalid folder_id param';
try {
// Create file
const driveFile = await create(user, file.path, name, null, folderId);
// Serialize
return pack(driveFile);
} catch (e) {
console.error(e);
throw e;
}
};

View File

@ -0,0 +1,34 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import DriveFile, { pack } from '../../../models/drive-file';
/**
* Find a file(s)
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'name' parameter
const [name, nameErr] = $(params.name).string().$;
if (nameErr) return rej('invalid name param');
// Get 'folder_id' parameter
const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
if (folderIdErr) return rej('invalid folder_id param');
// Issue query
const files = await DriveFile
.find({
filename: name,
'metadata.user_id': user._id,
'metadata.folder_id': folderId
});
// Serialize
res(await Promise.all(files.map(async file =>
await pack(file))));
});

View File

@ -0,0 +1,36 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import DriveFile, { pack } from '../../../models/drive-file';
/**
* Show a file
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = async (params, user) => {
// Get 'file_id' parameter
const [fileId, fileIdErr] = $(params.file_id).id().$;
if (fileIdErr) throw 'invalid file_id param';
// Fetch file
const file = await DriveFile
.findOne({
_id: fileId,
'metadata.user_id': user._id
});
if (file === null) {
throw 'file-not-found';
}
// Serialize
const _file = await pack(file, {
detail: true
});
return _file;
};

View File

@ -0,0 +1,75 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import DriveFolder from '../../../models/drive-folder';
import DriveFile, { validateFileName, pack } from '../../../models/drive-file';
import { publishDriveStream } from '../../../event';
/**
* Update a file
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'file_id' parameter
const [fileId, fileIdErr] = $(params.file_id).id().$;
if (fileIdErr) return rej('invalid file_id param');
// Fetch file
const file = await DriveFile
.findOne({
_id: fileId,
'metadata.user_id': user._id
});
if (file === null) {
return rej('file-not-found');
}
// Get 'name' parameter
const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).$;
if (nameErr) return rej('invalid name param');
if (name) file.filename = name;
// Get 'folder_id' parameter
const [folderId, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
if (folderIdErr) return rej('invalid folder_id param');
if (folderId !== undefined) {
if (folderId === null) {
file.metadata.folder_id = null;
} else {
// Fetch folder
const folder = await DriveFolder
.findOne({
_id: folderId,
user_id: user._id
});
if (folder === null) {
return rej('folder-not-found');
}
file.metadata.folder_id = folder._id;
}
}
await DriveFile.update(file._id, {
$set: {
filename: file.filename,
'metadata.folder_id': file.metadata.folder_id
}
});
// Serialize
const fileObj = await pack(file);
// Response
res(fileObj);
// Publish file_updated event
publishDriveStream(user._id, 'file_updated', fileObj);
});

View File

@ -0,0 +1,26 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import { pack } from '../../../models/drive-file';
import uploadFromUrl from '../../../common/drive/upload_from_url';
/**
* Create a file from a URL
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = async (params, user): Promise<any> => {
// Get 'url' parameter
// TODO: Validate this url
const [url, urlErr] = $(params.url).string().$;
if (urlErr) throw 'invalid url param';
// Get 'folder_id' parameter
const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
if (folderIdErr) throw 'invalid folder_id param';
return pack(await uploadFromUrl(url, user, folderId));
};

View File

@ -0,0 +1,66 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import DriveFolder, { pack } from '../../models/drive-folder';
/**
* Get drive folders
*
* @param {any} params
* @param {any} user
* @param {any} app
* @return {Promise<any>}
*/
module.exports = (params, user, app) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
// Get 'until_id' parameter
const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
if (untilIdErr) return rej('invalid until_id param');
// Check if both of since_id and until_id is specified
if (sinceId && untilId) {
return rej('cannot set since_id and until_id');
}
// Get 'folder_id' parameter
const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
if (folderIdErr) return rej('invalid folder_id param');
// Construct query
const sort = {
_id: -1
};
const query = {
user_id: user._id,
parent_id: folderId
} as any;
if (sinceId) {
sort._id = 1;
query._id = {
$gt: sinceId
};
} else if (untilId) {
query._id = {
$lt: untilId
};
}
// Issue query
const folders = await DriveFolder
.find(query, {
limit: limit,
sort: sort
});
// Serialize
res(await Promise.all(folders.map(async folder =>
await pack(folder))));
});

View File

@ -0,0 +1,55 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import DriveFolder, { isValidFolderName, pack } from '../../../models/drive-folder';
import { publishDriveStream } from '../../../event';
/**
* Create drive folder
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'name' parameter
const [name = '無題のフォルダー', nameErr] = $(params.name).optional.string().pipe(isValidFolderName).$;
if (nameErr) return rej('invalid name param');
// Get 'parent_id' parameter
const [parentId = null, parentIdErr] = $(params.parent_id).optional.nullable.id().$;
if (parentIdErr) return rej('invalid parent_id param');
// If the parent folder is specified
let parent = null;
if (parentId) {
// Fetch parent folder
parent = await DriveFolder
.findOne({
_id: parentId,
user_id: user._id
});
if (parent === null) {
return rej('parent-not-found');
}
}
// Create folder
const folder = await DriveFolder.insert({
created_at: new Date(),
name: name,
parent_id: parent !== null ? parent._id : null,
user_id: user._id
});
// Serialize
const folderObj = await pack(folder);
// Response
res(folderObj);
// Publish folder_created event
publishDriveStream(user._id, 'folder_created', folderObj);
});

View File

@ -0,0 +1,33 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import DriveFolder, { pack } from '../../../models/drive-folder';
/**
* Find a folder(s)
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'name' parameter
const [name, nameErr] = $(params.name).string().$;
if (nameErr) return rej('invalid name param');
// Get 'parent_id' parameter
const [parentId = null, parentIdErr] = $(params.parent_id).optional.nullable.id().$;
if (parentIdErr) return rej('invalid parent_id param');
// Issue query
const folders = await DriveFolder
.find({
name: name,
user_id: user._id,
parent_id: parentId
});
// Serialize
res(await Promise.all(folders.map(folder => pack(folder))));
});

View File

@ -0,0 +1,34 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import DriveFolder, { pack } from '../../../models/drive-folder';
/**
* Show a folder
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'folder_id' parameter
const [folderId, folderIdErr] = $(params.folder_id).id().$;
if (folderIdErr) return rej('invalid folder_id param');
// Get folder
const folder = await DriveFolder
.findOne({
_id: folderId,
user_id: user._id
});
if (folder === null) {
return rej('folder-not-found');
}
// Serialize
res(await pack(folder, {
detail: true
}));
});

View File

@ -0,0 +1,99 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import DriveFolder, { isValidFolderName, pack } from '../../../models/drive-folder';
import { publishDriveStream } from '../../../event';
/**
* Update a folder
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'folder_id' parameter
const [folderId, folderIdErr] = $(params.folder_id).id().$;
if (folderIdErr) return rej('invalid folder_id param');
// Fetch folder
const folder = await DriveFolder
.findOne({
_id: folderId,
user_id: user._id
});
if (folder === null) {
return rej('folder-not-found');
}
// Get 'name' parameter
const [name, nameErr] = $(params.name).optional.string().pipe(isValidFolderName).$;
if (nameErr) return rej('invalid name param');
if (name) folder.name = name;
// Get 'parent_id' parameter
const [parentId, parentIdErr] = $(params.parent_id).optional.nullable.id().$;
if (parentIdErr) return rej('invalid parent_id param');
if (parentId !== undefined) {
if (parentId === null) {
folder.parent_id = null;
} else {
// Get parent folder
const parent = await DriveFolder
.findOne({
_id: parentId,
user_id: user._id
});
if (parent === null) {
return rej('parent-folder-not-found');
}
// Check if the circular reference will occur
async function checkCircle(folderId) {
// Fetch folder
const folder2 = await DriveFolder.findOne({
_id: folderId
}, {
_id: true,
parent_id: true
});
if (folder2._id.equals(folder._id)) {
return true;
} else if (folder2.parent_id) {
return await checkCircle(folder2.parent_id);
} else {
return false;
}
}
if (parent.parent_id !== null) {
if (await checkCircle(parent.parent_id)) {
return rej('detected-circular-definition');
}
}
folder.parent_id = parent._id;
}
}
// Update
DriveFolder.update(folder._id, {
$set: {
name: folder.name,
parent_id: folder.parent_id
}
});
// Serialize
const folderObj = await pack(folder);
// Response
res(folderObj);
// Publish folder_updated event
publishDriveStream(user._id, 'folder_updated', folderObj);
});

View File

@ -0,0 +1,67 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import DriveFile, { pack } from '../../models/drive-file';
/**
* Get drive stream
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
// Get 'until_id' parameter
const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
if (untilIdErr) return rej('invalid until_id param');
// Check if both of since_id and until_id is specified
if (sinceId && untilId) {
return rej('cannot set since_id and until_id');
}
// Get 'type' parameter
const [type, typeErr] = $(params.type).optional.string().match(/^[a-zA-Z\/\-\*]+$/).$;
if (typeErr) return rej('invalid type param');
// Construct query
const sort = {
_id: -1
};
const query = {
'metadata.user_id': user._id
} as any;
if (sinceId) {
sort._id = 1;
query._id = {
$gt: sinceId
};
} else if (untilId) {
query._id = {
$lt: untilId
};
}
if (type) {
query.contentType = new RegExp(`^${type.replace(/\*/g, '.+?')}$`);
}
// Issue query
const files = await DriveFile
.find(query, {
limit: limit,
sort: sort
});
// Serialize
res(await Promise.all(files.map(async file =>
await pack(file))));
});

View File

@ -0,0 +1,84 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User, { pack as packUser } from '../../models/user';
import Following from '../../models/following';
import notify from '../../common/notify';
import event from '../../event';
/**
* Follow a user
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
const follower = user;
// Get 'user_id' parameter
const [userId, userIdErr] = $(params.user_id).id().$;
if (userIdErr) return rej('invalid user_id param');
// 自分自身
if (user._id.equals(userId)) {
return rej('followee is yourself');
}
// Get followee
const followee = await User.findOne({
_id: userId
}, {
fields: {
data: false,
'account.profile': false
}
});
if (followee === null) {
return rej('user not found');
}
// Check if already following
const exist = await Following.findOne({
follower_id: follower._id,
followee_id: followee._id,
deleted_at: { $exists: false }
});
if (exist !== null) {
return rej('already following');
}
// Create following
await Following.insert({
created_at: new Date(),
follower_id: follower._id,
followee_id: followee._id
});
// Send response
res();
// Increment following count
User.update(follower._id, {
$inc: {
following_count: 1
}
});
// Increment followers count
User.update({ _id: followee._id }, {
$inc: {
followers_count: 1
}
});
// Publish follow event
event(follower._id, 'follow', await packUser(followee, follower));
event(followee._id, 'followed', await packUser(follower, followee));
// Notify
notify(followee._id, follower._id, 'follow');
});

View File

@ -0,0 +1,81 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User, { pack as packUser } from '../../models/user';
import Following from '../../models/following';
import event from '../../event';
/**
* Unfollow a user
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
const follower = user;
// Get 'user_id' parameter
const [userId, userIdErr] = $(params.user_id).id().$;
if (userIdErr) return rej('invalid user_id param');
// Check if the followee is yourself
if (user._id.equals(userId)) {
return rej('followee is yourself');
}
// Get followee
const followee = await User.findOne({
_id: userId
}, {
fields: {
data: false,
'account.profile': false
}
});
if (followee === null) {
return rej('user not found');
}
// Check not following
const exist = await Following.findOne({
follower_id: follower._id,
followee_id: followee._id,
deleted_at: { $exists: false }
});
if (exist === null) {
return rej('already not following');
}
// Delete following
await Following.update({
_id: exist._id
}, {
$set: {
deleted_at: new Date()
}
});
// Send response
res();
// Decrement following count
User.update({ _id: follower._id }, {
$inc: {
following_count: -1
}
});
// Decrement followers count
User.update({ _id: followee._id }, {
$inc: {
followers_count: -1
}
});
// Publish follow event
event(follower._id, 'unfollow', await packUser(followee, follower));
});

View File

@ -0,0 +1,28 @@
/**
* Module dependencies
*/
import User, { pack } from '../models/user';
/**
* Show myself
*
* @param {any} params
* @param {any} user
* @param {any} app
* @param {Boolean} isSecure
* @return {Promise<any>}
*/
module.exports = (params, user, _, isSecure) => new Promise(async (res, rej) => {
// Serialize
res(await pack(user, user, {
detail: true,
includeSecrets: isSecure
}));
// Update lastUsedAt
User.update({ _id: user._id }, {
$set: {
'account.last_used_at': new Date()
}
});
});

View File

@ -0,0 +1,37 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import * as speakeasy from 'speakeasy';
import User from '../../../models/user';
module.exports = async (params, user) => new Promise(async (res, rej) => {
// Get 'token' parameter
const [token, tokenErr] = $(params.token).string().$;
if (tokenErr) return rej('invalid token param');
const _token = token.replace(/\s/g, '');
if (user.two_factor_temp_secret == null) {
return rej('二段階認証の設定が開始されていません');
}
const verified = (speakeasy as any).totp.verify({
secret: user.two_factor_temp_secret,
encoding: 'base32',
token: _token
});
if (!verified) {
return rej('not verified');
}
await User.update(user._id, {
$set: {
'account.two_factor_secret': user.two_factor_temp_secret,
'account.two_factor_enabled': true
}
});
res();
});

View File

@ -0,0 +1,48 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import * as bcrypt from 'bcryptjs';
import * as speakeasy from 'speakeasy';
import * as QRCode from 'qrcode';
import User from '../../../models/user';
import config from '../../../../../conf';
module.exports = async (params, user) => new Promise(async (res, rej) => {
// Get 'password' parameter
const [password, passwordErr] = $(params.password).string().$;
if (passwordErr) return rej('invalid password param');
// Compare password
const same = await bcrypt.compare(password, user.account.password);
if (!same) {
return rej('incorrect password');
}
// Generate user's secret key
const secret = speakeasy.generateSecret({
length: 32
});
await User.update(user._id, {
$set: {
two_factor_temp_secret: secret.base32
}
});
// Get the data URL of the authenticator URL
QRCode.toDataURL(speakeasy.otpauthURL({
secret: secret.base32,
encoding: 'base32',
label: user.username,
issuer: config.host
}), (err, data_url) => {
res({
qr: data_url,
secret: secret.base32,
label: user.username,
issuer: config.host
});
});
});

View File

@ -0,0 +1,28 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import * as bcrypt from 'bcryptjs';
import User from '../../../models/user';
module.exports = async (params, user) => new Promise(async (res, rej) => {
// Get 'password' parameter
const [password, passwordErr] = $(params.password).string().$;
if (passwordErr) return rej('invalid password param');
// Compare password
const same = await bcrypt.compare(password, user.account.password);
if (!same) {
return rej('incorrect password');
}
await User.update(user._id, {
$set: {
'account.two_factor_secret': null,
'account.two_factor_enabled': false
}
});
res();
});

View File

@ -0,0 +1,39 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Appdata from '../../../models/appdata';
/**
* Get app data
*
* @param {any} params
* @param {any} user
* @param {any} app
* @param {Boolean} isSecure
* @return {Promise<any>}
*/
module.exports = (params, user, app) => new Promise(async (res, rej) => {
if (app == null) return rej('このAPIはサードパーティAppからのみ利用できます');
// Get 'key' parameter
const [key = null, keyError] = $(params.key).optional.nullable.string().match(/[a-z_]+/).$;
if (keyError) return rej('invalid key param');
const select = {};
if (key !== null) {
select[`data.${key}`] = true;
}
const appdata = await Appdata.findOne({
app_id: app._id,
user_id: user._id
}, {
fields: select
});
if (appdata) {
res(appdata.data);
} else {
res();
}
});

View File

@ -0,0 +1,58 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Appdata from '../../../models/appdata';
/**
* Set app data
*
* @param {any} params
* @param {any} user
* @param {any} app
* @param {Boolean} isSecure
* @return {Promise<any>}
*/
module.exports = (params, user, app) => new Promise(async (res, rej) => {
if (app == null) return rej('このAPIはサードパーティAppからのみ利用できます');
// Get 'data' parameter
const [data, dataError] = $(params.data).optional.object()
.pipe(obj => {
const hasInvalidData = Object.entries(obj).some(([k, v]) =>
$(k).string().match(/^[a-z_]+$/).nok() && $(v).string().nok());
return !hasInvalidData;
}).$;
if (dataError) return rej('invalid data param');
// Get 'key' parameter
const [key, keyError] = $(params.key).optional.string().match(/[a-z_]+/).$;
if (keyError) return rej('invalid key param');
// Get 'value' parameter
const [value, valueError] = $(params.value).optional.string().$;
if (valueError) return rej('invalid value param');
const set = {};
if (data) {
Object.entries(data).forEach(([k, v]) => {
set[`data.${k}`] = v;
});
} else {
set[`data.${key}`] = value;
}
await Appdata.update({
app_id: app._id,
user_id: user._id
}, Object.assign({
app_id: app._id,
user_id: user._id
}, {
$set: set
}), {
upsert: true
});
res(204);
});

View File

@ -0,0 +1,43 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import AccessToken from '../../models/access-token';
import { pack } from '../../models/app';
/**
* Get authorized apps of my account
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'offset' parameter
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
if (offsetErr) return rej('invalid offset param');
// Get 'sort' parameter
const [sort = 'desc', sortError] = $(params.sort).optional.string().or('desc asc').$;
if (sortError) return rej('invalid sort param');
// Get tokens
const tokens = await AccessToken
.find({
user_id: user._id
}, {
limit: limit,
skip: offset,
sort: {
_id: sort == 'asc' ? 1 : -1
}
});
// Serialize
res(await Promise.all(tokens.map(async token =>
await pack(token.app_id))));
});

View File

@ -0,0 +1,42 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import * as bcrypt from 'bcryptjs';
import User from '../../models/user';
/**
* Change password
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = async (params, user) => new Promise(async (res, rej) => {
// Get 'current_password' parameter
const [currentPassword, currentPasswordErr] = $(params.current_password).string().$;
if (currentPasswordErr) return rej('invalid current_password param');
// Get 'new_password' parameter
const [newPassword, newPasswordErr] = $(params.new_password).string().$;
if (newPasswordErr) return rej('invalid new_password param');
// Compare password
const same = await bcrypt.compare(currentPassword, user.account.password);
if (!same) {
return rej('incorrect password');
}
// Generate hash of password
const salt = await bcrypt.genSalt(8);
const hash = await bcrypt.hash(newPassword, salt);
await User.update(user._id, {
$set: {
'account.password': hash
}
});
res();
});

View File

@ -0,0 +1,44 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Favorite from '../../models/favorite';
import { pack } from '../../models/post';
/**
* Get followers of a user
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'offset' parameter
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
if (offsetErr) return rej('invalid offset param');
// Get 'sort' parameter
const [sort = 'desc', sortError] = $(params.sort).optional.string().or('desc asc').$;
if (sortError) return rej('invalid sort param');
// Get favorites
const favorites = await Favorite
.find({
user_id: user._id
}, {
limit: limit,
skip: offset,
sort: {
_id: sort == 'asc' ? 1 : -1
}
});
// Serialize
res(await Promise.all(favorites.map(async favorite =>
await pack(favorite.post)
)));
});

View File

@ -0,0 +1,110 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Notification from '../../models/notification';
import Mute from '../../models/mute';
import { pack } from '../../models/notification';
import getFriends from '../../common/get-friends';
import read from '../../common/read-notification';
/**
* Get notifications
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'following' parameter
const [following = false, followingError] =
$(params.following).optional.boolean().$;
if (followingError) return rej('invalid following param');
// Get 'mark_as_read' parameter
const [markAsRead = true, markAsReadErr] = $(params.mark_as_read).optional.boolean().$;
if (markAsReadErr) return rej('invalid mark_as_read param');
// Get 'type' parameter
const [type, typeErr] = $(params.type).optional.array('string').unique().$;
if (typeErr) return rej('invalid type param');
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
// Get 'until_id' parameter
const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
if (untilIdErr) return rej('invalid until_id param');
// Check if both of since_id and until_id is specified
if (sinceId && untilId) {
return rej('cannot set since_id and until_id');
}
const mute = await Mute.find({
muter_id: user._id,
deleted_at: { $exists: false }
});
const query = {
notifiee_id: user._id,
$and: [{
notifier_id: {
$nin: mute.map(m => m.mutee_id)
}
}]
} as any;
const sort = {
_id: -1
};
if (following) {
// ID list of the user itself and other users who the user follows
const followingIds = await getFriends(user._id);
query.$and.push({
notifier_id: {
$in: followingIds
}
});
}
if (type) {
query.type = {
$in: type
};
}
if (sinceId) {
sort._id = 1;
query._id = {
$gt: sinceId
};
} else if (untilId) {
query._id = {
$lt: untilId
};
}
// Issue query
const notifications = await Notification
.find(query, {
limit: limit,
sort: sort
});
// Serialize
res(await Promise.all(notifications.map(async notification =>
await pack(notification))));
// Mark as read all
if (notifications.length > 0 && markAsRead) {
read(user._id, notifications);
}
});

View File

@ -0,0 +1,44 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User from '../../models/user';
import Post from '../../models/post';
import { pack } from '../../models/user';
/**
* Pin post
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = async (params, user) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Fetch pinee
const post = await Post.findOne({
_id: postId,
user_id: user._id
});
if (post === null) {
return rej('post not found');
}
await User.update(user._id, {
$set: {
pinned_post_id: post._id
}
});
// Serialize
const iObj = await pack(user, user, {
detail: true
});
// Send response
res(iObj);
});

View File

@ -0,0 +1,42 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import * as bcrypt from 'bcryptjs';
import User from '../../models/user';
import event from '../../event';
import generateUserToken from '../../common/generate-native-user-token';
/**
* Regenerate native token
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = async (params, user) => new Promise(async (res, rej) => {
// Get 'password' parameter
const [password, passwordErr] = $(params.password).string().$;
if (passwordErr) return rej('invalid password param');
// Compare password
const same = await bcrypt.compare(password, user.account.password);
if (!same) {
return rej('incorrect password');
}
// Generate secret
const secret = generateUserToken();
await User.update(user._id, {
$set: {
'account.token': secret
}
});
res();
// Publish event
event(user._id, 'my_token_regenerated');
});

View File

@ -0,0 +1,61 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Signin, { pack } from '../../models/signin';
/**
* Get signin history of my account
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
// Get 'until_id' parameter
const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
if (untilIdErr) return rej('invalid until_id param');
// Check if both of since_id and until_id is specified
if (sinceId && untilId) {
return rej('cannot set since_id and until_id');
}
const query = {
user_id: user._id
} as any;
const sort = {
_id: -1
};
if (sinceId) {
sort._id = 1;
query._id = {
$gt: sinceId
};
} else if (untilId) {
query._id = {
$lt: untilId
};
}
// Issue query
const history = await Signin
.find(query, {
limit: limit,
sort: sort
});
// Serialize
res(await Promise.all(history.map(async record =>
await pack(record))));
});

View File

@ -0,0 +1,97 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack } from '../../models/user';
import event from '../../event';
import config from '../../../../conf';
/**
* Update myself
*
* @param {any} params
* @param {any} user
* @param {any} _
* @param {boolean} isSecure
* @return {Promise<any>}
*/
module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => {
// Get 'name' parameter
const [name, nameErr] = $(params.name).optional.string().pipe(isValidName).$;
if (nameErr) return rej('invalid name param');
if (name) user.name = name;
// Get 'description' parameter
const [description, descriptionErr] = $(params.description).optional.nullable.string().pipe(isValidDescription).$;
if (descriptionErr) return rej('invalid description param');
if (description !== undefined) user.description = description;
// Get 'location' parameter
const [location, locationErr] = $(params.location).optional.nullable.string().pipe(isValidLocation).$;
if (locationErr) return rej('invalid location param');
if (location !== undefined) user.account.profile.location = location;
// Get 'birthday' parameter
const [birthday, birthdayErr] = $(params.birthday).optional.nullable.string().pipe(isValidBirthday).$;
if (birthdayErr) return rej('invalid birthday param');
if (birthday !== undefined) user.account.profile.birthday = birthday;
// Get 'avatar_id' parameter
const [avatarId, avatarIdErr] = $(params.avatar_id).optional.id().$;
if (avatarIdErr) return rej('invalid avatar_id param');
if (avatarId) user.avatar_id = avatarId;
// Get 'banner_id' parameter
const [bannerId, bannerIdErr] = $(params.banner_id).optional.id().$;
if (bannerIdErr) return rej('invalid banner_id param');
if (bannerId) user.banner_id = bannerId;
// Get 'is_bot' parameter
const [isBot, isBotErr] = $(params.is_bot).optional.boolean().$;
if (isBotErr) return rej('invalid is_bot param');
if (isBot != null) user.account.is_bot = isBot;
// Get 'auto_watch' parameter
const [autoWatch, autoWatchErr] = $(params.auto_watch).optional.boolean().$;
if (autoWatchErr) return rej('invalid auto_watch param');
if (autoWatch != null) user.account.settings.auto_watch = autoWatch;
await User.update(user._id, {
$set: {
name: user.name,
description: user.description,
avatar_id: user.avatar_id,
banner_id: user.banner_id,
'account.profile': user.account.profile,
'account.is_bot': user.account.is_bot,
'account.settings': user.account.settings
}
});
// Serialize
const iObj = await pack(user, user, {
detail: true,
includeSecrets: isSecure
});
// Send response
res(iObj);
// Publish i updated event
event(user._id, 'i_updated', iObj);
// Update search index
if (config.elasticsearch.enable) {
const es = require('../../../db/elasticsearch');
es.index({
index: 'misskey',
type: 'user',
id: user._id.toString(),
body: {
name: user.name,
bio: user.bio
}
});
}
});

View File

@ -0,0 +1,43 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User, { pack } from '../../models/user';
import event from '../../event';
/**
* Update myself
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = async (params, user) => new Promise(async (res, rej) => {
// Get 'name' parameter
const [name, nameErr] = $(params.name).string().$;
if (nameErr) return rej('invalid name param');
// Get 'value' parameter
const [value, valueErr] = $(params.value).nullable.any().$;
if (valueErr) return rej('invalid value param');
const x = {};
x[`account.client_settings.${name}`] = value;
await User.update(user._id, {
$set: x
});
// Serialize
user.account.client_settings[name] = value;
const iObj = await pack(user, user, {
detail: true,
includeSecrets: true
});
// Send response
res(iObj);
// Publish i updated event
event(user._id, 'i_updated', iObj);
});

View File

@ -0,0 +1,60 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User from '../../models/user';
import event from '../../event';
module.exports = async (params, user) => new Promise(async (res, rej) => {
// Get 'home' parameter
const [home, homeErr] = $(params.home).optional.array().each(
$().strict.object()
.have('name', $().string())
.have('id', $().string())
.have('place', $().string())
.have('data', $().object())).$;
if (homeErr) return rej('invalid home param');
// Get 'id' parameter
const [id, idErr] = $(params.id).optional.string().$;
if (idErr) return rej('invalid id param');
// Get 'data' parameter
const [data, dataErr] = $(params.data).optional.object().$;
if (dataErr) return rej('invalid data param');
if (home) {
await User.update(user._id, {
$set: {
'account.client_settings.home': home
}
});
res();
event(user._id, 'home_updated', {
home
});
} else {
if (id == null && data == null) return rej('you need to set id and data params if home param unset');
const _home = user.account.client_settings.home;
const widget = _home.find(w => w.id == id);
if (widget == null) return rej('widget not found');
widget.data = data;
await User.update(user._id, {
$set: {
'account.client_settings.home': _home
}
});
res();
event(user._id, 'home_updated', {
id, data
});
}
});

View File

@ -0,0 +1,59 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User from '../../models/user';
import event from '../../event';
module.exports = async (params, user) => new Promise(async (res, rej) => {
// Get 'home' parameter
const [home, homeErr] = $(params.home).optional.array().each(
$().strict.object()
.have('name', $().string())
.have('id', $().string())
.have('data', $().object())).$;
if (homeErr) return rej('invalid home param');
// Get 'id' parameter
const [id, idErr] = $(params.id).optional.string().$;
if (idErr) return rej('invalid id param');
// Get 'data' parameter
const [data, dataErr] = $(params.data).optional.object().$;
if (dataErr) return rej('invalid data param');
if (home) {
await User.update(user._id, {
$set: {
'account.client_settings.mobile_home': home
}
});
res();
event(user._id, 'mobile_home_updated', {
home
});
} else {
if (id == null && data == null) return rej('you need to set id and data params if home param unset');
const _home = user.account.client_settings.mobile_home || [];
const widget = _home.find(w => w.id == id);
if (widget == null) return rej('widget not found');
widget.data = data;
await User.update(user._id, {
$set: {
'account.client_settings.mobile_home': _home
}
});
res();
event(user._id, 'mobile_home_updated', {
id, data
});
}
});

View File

@ -0,0 +1,43 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import History from '../../models/messaging-history';
import Mute from '../../models/mute';
import { pack } from '../../models/messaging-message';
/**
* Show messaging history
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
const mute = await Mute.find({
muter_id: user._id,
deleted_at: { $exists: false }
});
// Get history
const history = await History
.find({
user_id: user._id,
partner: {
$nin: mute.map(m => m.mutee_id)
}
}, {
limit: limit,
sort: {
updated_at: -1
}
});
// Serialize
res(await Promise.all(history.map(async h =>
await pack(h.message, user))));
});

View File

@ -0,0 +1,102 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Message from '../../models/messaging-message';
import User from '../../models/user';
import { pack } from '../../models/messaging-message';
import read from '../../common/read-messaging-message';
/**
* Get messages
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'user_id' parameter
const [recipientId, recipientIdErr] = $(params.user_id).id().$;
if (recipientIdErr) return rej('invalid user_id param');
// Fetch recipient
const recipient = await User.findOne({
_id: recipientId
}, {
fields: {
_id: true
}
});
if (recipient === null) {
return rej('user not found');
}
// Get 'mark_as_read' parameter
const [markAsRead = true, markAsReadErr] = $(params.mark_as_read).optional.boolean().$;
if (markAsReadErr) return rej('invalid mark_as_read param');
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
// Get 'until_id' parameter
const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
if (untilIdErr) return rej('invalid until_id param');
// Check if both of since_id and until_id is specified
if (sinceId && untilId) {
return rej('cannot set since_id and until_id');
}
const query = {
$or: [{
user_id: user._id,
recipient_id: recipient._id
}, {
user_id: recipient._id,
recipient_id: user._id
}]
} as any;
const sort = {
_id: -1
};
if (sinceId) {
sort._id = 1;
query._id = {
$gt: sinceId
};
} else if (untilId) {
query._id = {
$lt: untilId
};
}
// Issue query
const messages = await Message
.find(query, {
limit: limit,
sort: sort
});
// Serialize
res(await Promise.all(messages.map(async message =>
await pack(message, user, {
populateRecipient: false
}))));
if (messages.length === 0) {
return;
}
// Mark as read all
if (markAsRead) {
read(user._id, recipient._id, messages);
}
});

View File

@ -0,0 +1,156 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Message from '../../../models/messaging-message';
import { isValidText } from '../../../models/messaging-message';
import History from '../../../models/messaging-history';
import User from '../../../models/user';
import Mute from '../../../models/mute';
import DriveFile from '../../../models/drive-file';
import { pack } from '../../../models/messaging-message';
import publishUserStream from '../../../event';
import { publishMessagingStream, publishMessagingIndexStream, pushSw } from '../../../event';
import config from '../../../../../conf';
/**
* Create a message
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'user_id' parameter
const [recipientId, recipientIdErr] = $(params.user_id).id().$;
if (recipientIdErr) return rej('invalid user_id param');
// Myself
if (recipientId.equals(user._id)) {
return rej('cannot send message to myself');
}
// Fetch recipient
const recipient = await User.findOne({
_id: recipientId
}, {
fields: {
_id: true
}
});
if (recipient === null) {
return rej('user not found');
}
// Get 'text' parameter
const [text, textErr] = $(params.text).optional.string().pipe(isValidText).$;
if (textErr) return rej('invalid text');
// Get 'file_id' parameter
const [fileId, fileIdErr] = $(params.file_id).optional.id().$;
if (fileIdErr) return rej('invalid file_id param');
let file = null;
if (fileId !== undefined) {
file = await DriveFile.findOne({
_id: fileId,
'metadata.user_id': user._id
});
if (file === null) {
return rej('file not found');
}
}
// テキストが無いかつ添付ファイルも無かったらエラー
if (text === undefined && file === null) {
return rej('text or file is required');
}
// メッセージを作成
const message = await Message.insert({
created_at: new Date(),
file_id: file ? file._id : undefined,
recipient_id: recipient._id,
text: text ? text : undefined,
user_id: user._id,
is_read: false
});
// Serialize
const messageObj = await pack(message);
// Reponse
res(messageObj);
// 自分のストリーム
publishMessagingStream(message.user_id, message.recipient_id, 'message', messageObj);
publishMessagingIndexStream(message.user_id, 'message', messageObj);
publishUserStream(message.user_id, 'messaging_message', messageObj);
// 相手のストリーム
publishMessagingStream(message.recipient_id, message.user_id, 'message', messageObj);
publishMessagingIndexStream(message.recipient_id, 'message', messageObj);
publishUserStream(message.recipient_id, 'messaging_message', messageObj);
// 3秒経っても(今回作成した)メッセージが既読にならなかったら「未読のメッセージがありますよ」イベントを発行する
setTimeout(async () => {
const freshMessage = await Message.findOne({ _id: message._id }, { is_read: true });
if (!freshMessage.is_read) {
//#region ただしミュートされているなら発行しない
const mute = await Mute.find({
muter_id: recipient._id,
deleted_at: { $exists: false }
});
const mutedUserIds = mute.map(m => m.mutee_id.toString());
if (mutedUserIds.indexOf(user._id.toString()) != -1) {
return;
}
//#endregion
publishUserStream(message.recipient_id, 'unread_messaging_message', messageObj);
pushSw(message.recipient_id, 'unread_messaging_message', messageObj);
}
}, 3000);
// Register to search database
if (message.text && config.elasticsearch.enable) {
const es = require('../../../db/elasticsearch');
es.index({
index: 'misskey',
type: 'messaging_message',
id: message._id.toString(),
body: {
text: message.text
}
});
}
// 履歴作成(自分)
History.update({
user_id: user._id,
partner: recipient._id
}, {
updated_at: new Date(),
user_id: user._id,
partner: recipient._id,
message: message._id
}, {
upsert: true
});
// 履歴作成(相手)
History.update({
user_id: recipient._id,
partner: user._id
}, {
updated_at: new Date(),
user_id: recipient._id,
partner: user._id,
message: message._id
}, {
upsert: true
});
});

View File

@ -0,0 +1,33 @@
/**
* Module dependencies
*/
import Message from '../../models/messaging-message';
import Mute from '../../models/mute';
/**
* Get count of unread messages
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
const mute = await Mute.find({
muter_id: user._id,
deleted_at: { $exists: false }
});
const mutedUserIds = mute.map(m => m.mutee_id);
const count = await Message
.count({
user_id: {
$nin: mutedUserIds
},
recipient_id: user._id,
is_read: false
});
res({
count: count
});
});

View File

@ -0,0 +1,59 @@
/**
* Module dependencies
*/
import * as os from 'os';
import version from '../../../version';
import config from '../../../conf';
import Meta from '../models/meta';
/**
* @swagger
* /meta:
* post:
* summary: Show the misskey's information
* responses:
* 200:
* description: Success
* schema:
* type: object
* properties:
* maintainer:
* description: maintainer's name
* type: string
* commit:
* description: latest commit's hash
* type: string
* secure:
* description: whether the server supports secure protocols
* type: boolean
*
* default:
* description: Failed
* schema:
* $ref: "#/definitions/Error"
*/
/**
* Show core info
*
* @param {any} params
* @return {Promise<any>}
*/
module.exports = (params) => new Promise(async (res, rej) => {
const meta = (await Meta.findOne()) || {};
res({
maintainer: config.maintainer,
version: version,
secure: config.https != null,
machine: os.hostname(),
os: os.platform(),
node: process.version,
cpu: {
model: os.cpus()[0].model,
cores: os.cpus().length
},
top_image: meta.top_image,
broadcasts: meta.broadcasts
});
});

View File

@ -0,0 +1,61 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User from '../../models/user';
import Mute from '../../models/mute';
/**
* Mute a user
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
const muter = user;
// Get 'user_id' parameter
const [userId, userIdErr] = $(params.user_id).id().$;
if (userIdErr) return rej('invalid user_id param');
// 自分自身
if (user._id.equals(userId)) {
return rej('mutee is yourself');
}
// Get mutee
const mutee = await User.findOne({
_id: userId
}, {
fields: {
data: false,
'account.profile': false
}
});
if (mutee === null) {
return rej('user not found');
}
// Check if already muting
const exist = await Mute.findOne({
muter_id: muter._id,
mutee_id: mutee._id,
deleted_at: { $exists: false }
});
if (exist !== null) {
return rej('already muting');
}
// Create mute
await Mute.insert({
created_at: new Date(),
muter_id: muter._id,
mutee_id: mutee._id,
});
// Send response
res();
});

View File

@ -0,0 +1,63 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import User from '../../models/user';
import Mute from '../../models/mute';
/**
* Unmute a user
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
const muter = user;
// Get 'user_id' parameter
const [userId, userIdErr] = $(params.user_id).id().$;
if (userIdErr) return rej('invalid user_id param');
// Check if the mutee is yourself
if (user._id.equals(userId)) {
return rej('mutee is yourself');
}
// Get mutee
const mutee = await User.findOne({
_id: userId
}, {
fields: {
data: false,
'account.profile': false
}
});
if (mutee === null) {
return rej('user not found');
}
// Check not muting
const exist = await Mute.findOne({
muter_id: muter._id,
mutee_id: mutee._id,
deleted_at: { $exists: false }
});
if (exist === null) {
return rej('already not muting');
}
// Delete mute
await Mute.update({
_id: exist._id
}, {
$set: {
deleted_at: new Date()
}
});
// Send response
res();
});

View File

@ -0,0 +1,73 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import Mute from '../../models/mute';
import { pack } from '../../models/user';
import getFriends from '../../common/get-friends';
/**
* Get muted users of a user
*
* @param {any} params
* @param {any} me
* @return {Promise<any>}
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
// Get 'iknow' parameter
const [iknow = false, iknowErr] = $(params.iknow).optional.boolean().$;
if (iknowErr) return rej('invalid iknow param');
// Get 'limit' parameter
const [limit = 30, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'cursor' parameter
const [cursor = null, cursorErr] = $(params.cursor).optional.id().$;
if (cursorErr) return rej('invalid cursor param');
// Construct query
const query = {
muter_id: me._id,
deleted_at: { $exists: false }
} as any;
if (iknow) {
// Get my friends
const myFriends = await getFriends(me._id);
query.mutee_id = {
$in: myFriends
};
}
// カーソルが指定されている場合
if (cursor) {
query._id = {
$lt: cursor
};
}
// Get mutes
const mutes = await Mute
.find(query, {
limit: limit + 1,
sort: { _id: -1 }
});
// 「次のページ」があるかどうか
const inStock = mutes.length === limit + 1;
if (inStock) {
mutes.pop();
}
// Serialize
const users = await Promise.all(mutes.map(async m =>
await pack(m.mutee_id, me, { detail: true })));
// Response
res({
users: users,
next: inStock ? mutes[mutes.length - 1]._id : null,
});
});

View File

@ -0,0 +1,40 @@
/**
* Module dependencies
*/
import $ from 'cafy';
import App, { pack } from '../../models/app';
/**
* Get my apps
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'offset' parameter
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
if (offsetErr) return rej('invalid offset param');
const query = {
user_id: user._id
};
// Execute query
const apps = await App
.find(query, {
limit: limit,
skip: offset,
sort: {
_id: -1
}
});
// Reply
res(await Promise.all(apps.map(async app =>
await pack(app))));
});

View File

@ -0,0 +1,33 @@
/**
* Module dependencies
*/
import Notification from '../../models/notification';
import Mute from '../../models/mute';
/**
* Get count of unread notifications
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
const mute = await Mute.find({
muter_id: user._id,
deleted_at: { $exists: false }
});
const mutedUserIds = mute.map(m => m.mutee_id);
const count = await Notification
.count({
notifiee_id: user._id,
notifier_id: {
$nin: mutedUserIds
},
is_read: false
});
res({
count: count
});
});

View File

@ -0,0 +1,32 @@
/**
* Module dependencies
*/
import Notification from '../../models/notification';
import event from '../../event';
/**
* Mark as read all notifications
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Update documents
await Notification.update({
notifiee_id: user._id,
is_read: false
}, {
$set: {
is_read: true
}
}, {
multi: true
});
// Response
res();
// 全ての通知を読みましたよというイベントを発行
event(user._id, 'read_all_notifications');
});

View File

@ -0,0 +1,62 @@
import $ from 'cafy';
import Game, { pack } from '../../models/othello-game';
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'my' parameter
const [my = false, myErr] = $(params.my).optional.boolean().$;
if (myErr) return rej('invalid my param');
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
// Get 'until_id' parameter
const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
if (untilIdErr) return rej('invalid until_id param');
// Check if both of since_id and until_id is specified
if (sinceId && untilId) {
return rej('cannot set since_id and until_id');
}
const q: any = my ? {
is_started: true,
$or: [{
user1_id: user._id
}, {
user2_id: user._id
}]
} : {
is_started: true
};
const sort = {
_id: -1
};
if (sinceId) {
sort._id = 1;
q._id = {
$gt: sinceId
};
} else if (untilId) {
q._id = {
$lt: untilId
};
}
// Fetch games
const games = await Game.find(q, {
sort,
limit
});
// Reponse
res(Promise.all(games.map(async (g) => await pack(g, user, {
detail: false
}))));
});

View File

@ -0,0 +1,32 @@
import $ from 'cafy';
import Game, { pack } from '../../../models/othello-game';
import Othello from '../../../../common/othello/core';
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'game_id' parameter
const [gameId, gameIdErr] = $(params.game_id).id().$;
if (gameIdErr) return rej('invalid game_id param');
const game = await Game.findOne({ _id: gameId });
if (game == null) {
return rej('game not found');
}
const o = new Othello(game.settings.map, {
isLlotheo: game.settings.is_llotheo,
canPutEverywhere: game.settings.can_put_everywhere,
loopedBoard: game.settings.looped_board
});
game.logs.forEach(log => {
o.put(log.color, log.pos);
});
const packed = await pack(game, user);
res(Object.assign({
board: o.board,
turn: o.turn
}, packed));
});

View File

@ -0,0 +1,15 @@
import Matching, { pack as packMatching } from '../../models/othello-matching';
module.exports = (params, user) => new Promise(async (res, rej) => {
// Find session
const invitations = await Matching.find({
child_id: user._id
}, {
sort: {
_id: -1
}
});
// Reponse
res(Promise.all(invitations.map(async (i) => await packMatching(i, user))));
});

View File

@ -0,0 +1,95 @@
import $ from 'cafy';
import Matching, { pack as packMatching } from '../../models/othello-matching';
import Game, { pack as packGame } from '../../models/othello-game';
import User from '../../models/user';
import publishUserStream, { publishOthelloStream } from '../../event';
import { eighteight } from '../../../common/othello/maps';
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'user_id' parameter
const [childId, childIdErr] = $(params.user_id).id().$;
if (childIdErr) return rej('invalid user_id param');
// Myself
if (childId.equals(user._id)) {
return rej('invalid user_id param');
}
// Find session
const exist = await Matching.findOne({
parent_id: childId,
child_id: user._id
});
if (exist) {
// Destroy session
Matching.remove({
_id: exist._id
});
// Create game
const game = await Game.insert({
created_at: new Date(),
user1_id: exist.parent_id,
user2_id: user._id,
user1_accepted: false,
user2_accepted: false,
is_started: false,
is_ended: false,
logs: [],
settings: {
map: eighteight.data,
bw: 'random',
is_llotheo: false
}
});
// Reponse
res(await packGame(game, user));
publishOthelloStream(exist.parent_id, 'matched', await packGame(game, exist.parent_id));
const other = await Matching.count({
child_id: user._id
});
if (other == 0) {
publishUserStream(user._id, 'othello_no_invites');
}
} else {
// Fetch child
const child = await User.findOne({
_id: childId
}, {
fields: {
_id: true
}
});
if (child === null) {
return rej('user not found');
}
// 以前のセッションはすべて削除しておく
await Matching.remove({
parent_id: user._id
});
// セッションを作成
const matching = await Matching.insert({
created_at: new Date(),
parent_id: user._id,
child_id: child._id
});
// Reponse
res();
const packed = await packMatching(matching, child);
// 招待
publishOthelloStream(child._id, 'invited', packed);
publishUserStream(child._id, 'othello_invited', packed);
}
});

View File

@ -0,0 +1,9 @@
import Matching from '../../../models/othello-matching';
module.exports = (params, user) => new Promise(async (res, rej) => {
await Matching.remove({
parent_id: user._id
});
res();
});

Some files were not shown because too many files have changed in this diff Show More