egirlskey/src/server/api/endpoints/notes/create.ts

189 lines
5 KiB
TypeScript
Raw Normal View History

2018-04-24 09:13:06 +00:00
import $ from 'cafy'; import ID from '../../../../cafy-id';
2018-04-07 17:30:37 +00:00
import Note, { INote, isValidText, isValidCw, pack } from '../../../../models/note';
2018-06-18 00:54:53 +00:00
import User, { ILocalUser, IUser } from '../../../../models/user';
2018-04-07 17:30:37 +00:00
import DriveFile from '../../../../models/drive-file';
import create from '../../../../services/note/create';
import { IApp } from '../../../../models/app';
2018-07-05 13:35:35 +00:00
import getParams from '../../get-params';
export const meta = {
params: {
visibility: {
def: $.str.optional().or(['public', 'home', 'followers', 'specified', 'private']),
default: 'public',
desc: {
ja: '投稿の公開範囲'
}
},
visibleUserIds: {
def: $.arr($.type(ID)).optional().unique().min(1),
desc: {
ja: '(投稿の公開範囲が specified の場合)投稿を閲覧できるユーザー'
}
},
text: {
def: $.str.optional().nullable().pipe(isValidText),
default: null,
desc: {
ja: '投稿内容'
}
},
}
};
2018-04-07 17:30:37 +00:00
/**
* Create a note
*/
2018-06-18 00:54:53 +00:00
module.exports = (params: any, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => {
2018-07-05 13:35:35 +00:00
const [ps, psErr] = getParams(meta, params);
if (psErr) return rej(psErr);
2018-04-28 19:30:51 +00:00
2018-06-18 00:54:53 +00:00
let visibleUsers: IUser[] = [];
2018-07-05 13:35:35 +00:00
if (ps.visibleUserIds !== undefined) {
visibleUsers = await Promise.all(ps.visibleUserIds.map(id => User.findOne({
2018-04-28 19:30:51 +00:00
_id: id
})));
}
2018-04-07 17:30:37 +00:00
// Get 'text' parameter
2018-05-02 09:06:16 +00:00
const [text = null, textErr] = $.str.optional().nullable().pipe(isValidText).get(params.text);
2018-04-07 17:30:37 +00:00
if (textErr) return rej('invalid text');
// Get 'cw' parameter
2018-05-02 09:06:16 +00:00
const [cw, cwErr] = $.str.optional().nullable().pipe(isValidCw).get(params.cw);
2018-04-07 17:30:37 +00:00
if (cwErr) return rej('invalid cw');
// Get 'viaMobile' parameter
2018-05-02 09:06:16 +00:00
const [viaMobile = false, viaMobileErr] = $.bool.optional().get(params.viaMobile);
2018-04-07 17:30:37 +00:00
if (viaMobileErr) return rej('invalid viaMobile');
// Get 'tags' parameter
2018-05-03 10:49:04 +00:00
const [tags = [], tagsErr] = $.arr($.str.range(1, 32)).optional().unique().get(params.tags);
2018-04-07 17:30:37 +00:00
if (tagsErr) return rej('invalid tags');
// Get 'geo' parameter
2018-05-02 09:06:16 +00:00
const [geo, geoErr] = $.obj.optional().nullable().strict()
.have('coordinates', $.arr().length(2)
.item(0, $.num.range(-180, 180))
.item(1, $.num.range(-90, 90)))
.have('altitude', $.num.nullable())
.have('accuracy', $.num.nullable())
.have('altitudeAccuracy', $.num.nullable())
.have('heading', $.num.nullable().range(0, 360))
.have('speed', $.num.nullable())
.get(params.geo);
2018-04-07 17:30:37 +00:00
if (geoErr) return rej('invalid geo');
// Get 'mediaIds' parameter
2018-05-02 09:06:16 +00:00
const [mediaIds, mediaIdsErr] = $.arr($.type(ID)).optional().unique().range(1, 4).get(params.mediaIds);
2018-04-07 17:30:37 +00:00
if (mediaIdsErr) return rej('invalid mediaIds');
let files = [];
if (mediaIds !== undefined) {
// Fetch files
// forEach だと途中でエラーなどがあっても return できないので
// 敢えて for を使っています。
for (const mediaId of mediaIds) {
// Fetch file
// SELECT _id
const entity = await DriveFile.findOne({
_id: mediaId,
'metadata.userId': user._id
});
if (entity === null) {
return rej('file not found');
} else {
files.push(entity);
}
}
} else {
files = null;
}
// Get 'renoteId' parameter
2018-05-02 09:06:16 +00:00
const [renoteId, renoteIdErr] = $.type(ID).optional().get(params.renoteId);
2018-04-07 17:30:37 +00:00
if (renoteIdErr) return rej('invalid renoteId');
let renote: INote = null;
if (renoteId !== undefined) {
// Fetch renote to note
renote = await Note.findOne({
_id: renoteId
});
if (renote == null) {
return rej('renoteee is not found');
} else if (renote.renoteId && !renote.text && !renote.mediaIds) {
return rej('cannot renote to renote');
}
}
// Get 'replyId' parameter
2018-05-02 09:06:16 +00:00
const [replyId, replyIdErr] = $.type(ID).optional().get(params.replyId);
2018-04-07 17:30:37 +00:00
if (replyIdErr) return rej('invalid replyId');
let reply: INote = null;
if (replyId !== undefined) {
// Fetch reply
reply = await Note.findOne({
_id: replyId
});
if (reply === null) {
return rej('in reply to note is not found');
}
// 返信対象が引用でないRenoteだったらエラー
if (reply.renoteId && !reply.text && !reply.mediaIds) {
return rej('cannot reply to renote');
}
}
// Get 'poll' parameter
2018-05-02 09:06:16 +00:00
const [poll, pollErr] = $.obj.optional().strict()
.have('choices', $.arr($.str)
2018-04-07 17:30:37 +00:00
.unique()
.range(2, 10)
.each(c => c.length > 0 && c.length < 50))
2018-05-02 09:06:16 +00:00
.get(params.poll);
2018-04-07 17:30:37 +00:00
if (pollErr) return rej('invalid poll');
if (poll) {
2018-06-18 00:54:53 +00:00
(poll as any).choices = (poll as any).choices.map((choice: string, i: number) => ({
2018-04-07 17:30:37 +00:00
id: i, // IDを付与
text: choice.trim(),
votes: 0
}));
}
// テキストが無いかつ添付ファイルが無いかつRenoteも無いかつ投票も無かったらエラー
2018-06-08 13:04:07 +00:00
if ((text === undefined || text === null) && files === null && renote === null && poll === undefined) {
2018-04-07 17:30:37 +00:00
return rej('text, mediaIds, renoteId or poll is required');
}
// 投稿を作成
const note = await create(user, {
createdAt: new Date(),
media: files,
2018-04-22 08:04:52 +00:00
poll,
text,
2018-04-07 17:30:37 +00:00
reply,
renote,
2018-04-22 08:04:52 +00:00
cw,
tags,
app,
viaMobile,
2018-04-07 17:30:37 +00:00
visibility,
2018-04-28 19:30:51 +00:00
visibleUsers,
2018-04-07 17:30:37 +00:00
geo
});
const noteObj = await pack(note, user);
// Reponse
res({
createdNote: noteObj
});
});