refactor: use ajv instead of cafy (#8324)

* wip

* wip

* Update abuse-user-reports.ts

* Update files.ts

* Update list-remote.ts

* Update list.ts

* Update show-users.ts

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Update update.ts

* Update search.ts

* Update reactions.ts

* Update search.ts

* wip

* wip

* wip

* wip

* Update update.ts

* Update relation.ts

* Update available.ts

* wip

* wip

* wip

* Update packages/backend/src/server/api/define.ts

Co-authored-by: Johann150 <johann.galle@protonmail.com>

* Update define.ts

* Update define.ts

* typo

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Update update.ts

* wip

* Update signup.ts

* Update call.ts

* minimum for limit

* type

* remove needless annotation

* wip

* Update signup.ts

* wip

* wip

* fix

* Update create.ts

Co-authored-by: Johann150 <johann.galle@protonmail.com>
This commit is contained in:
syuilo 2022-02-19 14:05:32 +09:00 committed by GitHub
parent 59785ea04c
commit 510de87607
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
320 changed files with 4395 additions and 5939 deletions

View file

@ -1,11 +1,9 @@
import $ from 'cafy';
import ms from 'ms';
import { length } from 'stringz';
import create from '@/services/note/create';
import define from '../../define';
import { fetchMeta } from '@/misc/fetch-meta';
import { ApiError } from '../../error';
import { ID } from '@/misc/cafy-id';
import { User } from '@/models/entities/user';
import { Users, DriveFiles, Notes, Channels, Blockings } from '@/models/index';
import { DriveFile } from '@/models/entities/drive-file';
@ -34,84 +32,6 @@ export const meta = {
kind: 'write:notes',
params: {
visibility: {
validator: $.optional.str.or(noteVisibilities as unknown as string[]),
default: 'public',
},
visibleUserIds: {
validator: $.optional.arr($.type(ID)).unique().min(0),
},
text: {
validator: $.optional.nullable.str.pipe(text =>
text.trim() != ''
&& length(text.trim()) <= maxNoteTextLength
&& Array.from(text.trim()).length <= DB_MAX_NOTE_TEXT_LENGTH, // DB limit
),
default: null,
},
cw: {
validator: $.optional.nullable.str.pipe(Notes.validateCw),
},
localOnly: {
validator: $.optional.bool,
default: false,
},
noExtractMentions: {
validator: $.optional.bool,
default: false,
},
noExtractHashtags: {
validator: $.optional.bool,
default: false,
},
noExtractEmojis: {
validator: $.optional.bool,
default: false,
},
fileIds: {
validator: $.optional.arr($.type(ID)).unique().range(1, 16),
},
mediaIds: {
validator: $.optional.arr($.type(ID)).unique().range(1, 16),
deprecated: true,
},
replyId: {
validator: $.optional.nullable.type(ID),
},
renoteId: {
validator: $.optional.nullable.type(ID),
},
channelId: {
validator: $.optional.nullable.type(ID),
},
poll: {
validator: $.optional.nullable.obj({
choices: $.arr($.str)
.unique()
.range(2, 10)
.each(c => c.length > 0 && c.length < 50),
multiple: $.optional.bool,
expiresAt: $.optional.nullable.num.int(),
expiredAfter: $.optional.nullable.num.int().min(1),
}).strict(),
ref: 'poll',
},
},
res: {
type: 'object',
optional: false, nullable: false,
@ -175,8 +95,49 @@ export const meta = {
},
} as const;
const paramDef = {
type: 'object',
properties: {
visibility: { type: 'string', enum: ['public', 'home', 'followers', 'specified'], default: "public" },
visibleUserIds: { type: 'array', uniqueItems: true, items: {
type: 'string', format: 'misskey:id',
} },
text: { type: 'string', nullable: true, maxLength: 3000, default: null },
cw: { type: 'string', nullable: true, maxLength: 100 },
localOnly: { type: 'boolean', default: false },
noExtractMentions: { type: 'boolean', default: false },
noExtractHashtags: { type: 'boolean', default: false },
noExtractEmojis: { type: 'boolean', default: false },
fileIds: { type: 'array', uniqueItems: true, minItems: 1, maxItems: 16, items: {
type: 'string', format: 'misskey:id',
} },
mediaIds: { type: 'array', uniqueItems: true, minItems: 1, maxItems: 16, items: {
type: 'string', format: 'misskey:id',
} },
replyId: { type: 'string', format: 'misskey:id', nullable: true },
renoteId: { type: 'string', format: 'misskey:id', nullable: true },
channelId: { type: 'string', format: 'misskey:id', nullable: true },
poll: {
type: 'object', nullable: true,
properties: {
choices: {
type: 'array', uniqueItems: true, minItems: 2, maxItems: 10,
items: {
type: 'string', minLength: 1, maxLength: 50,
},
},
multiple: { type: 'boolean', default: false },
expiresAt: { type: 'integer', nullable: true },
expiredAfter: { type: 'integer', nullable: true, minimum: 1 },
},
required: ['choices'],
},
},
required: [],
} as const;
// eslint-disable-next-line import/no-default-export
export default define(meta, async (ps, user) => {
export default define(meta, paramDef, async (ps, user) => {
let visibleUsers: User[] = [];
if (ps.visibleUserIds) {
visibleUsers = (await Promise.all(ps.visibleUserIds.map(id => Users.findOne(id))))