egirlskey/src/server/api/endpoints/users/report-abuse.ts
syuilo b9cb6d1c10 refactor: refactoring imports
将来ESMに移行しやすいように
Related: #7658

なんかmochaが起動しなくなってるけど理由不明
すぐ直したい
2021-08-19 18:33:41 +09:00

90 lines
2 KiB
TypeScript

import $ from 'cafy';
import { ID } from '@/misc/cafy-id.js';
import define from '../../define.js';
import { publishAdminStream } from '@/services/stream.js';
import { ApiError } from '../../error.js';
import { getUser } from '../../common/getters.js';
import { AbuseUserReports, Users } from '@/models/index.js';
import { genId } from '@/misc/gen-id.js';
export const meta = {
tags: ['users'],
requireCredential: true as const,
params: {
userId: {
validator: $.type(ID),
},
comment: {
validator: $.str.range(1, 2048),
},
},
errors: {
noSuchUser: {
message: 'No such user.',
code: 'NO_SUCH_USER',
id: '1acefcb5-0959-43fd-9685-b48305736cb5'
},
cannotReportYourself: {
message: 'Cannot report yourself.',
code: 'CANNOT_REPORT_YOURSELF',
id: '1e13149e-b1e8-43cf-902e-c01dbfcb202f'
},
cannotReportAdmin: {
message: 'Cannot report the admin.',
code: 'CANNOT_REPORT_THE_ADMIN',
id: '35e166f5-05fb-4f87-a2d5-adb42676d48f'
}
}
};
export default define(meta, async (ps, me) => {
// Lookup user
const user = await getUser(ps.userId).catch(e => {
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
throw e;
});
if (user.id === me.id) {
throw new ApiError(meta.errors.cannotReportYourself);
}
if (user.isAdmin) {
throw new ApiError(meta.errors.cannotReportAdmin);
}
const report = await AbuseUserReports.save({
id: genId(),
createdAt: new Date(),
targetUserId: user.id,
targetUserHost: user.host,
reporterId: me.id,
reporterHost: null,
comment: ps.comment,
});
// Publish event to moderators
setTimeout(async () => {
const moderators = await Users.find({
where: [{
isAdmin: true
}, {
isModerator: true
}]
});
for (const moderator of moderators) {
publishAdminStream(moderator.id, 'newAbuseUserReport', {
id: report.id,
targetUserId: report.targetUserId,
reporterId: report.reporterId,
comment: report.comment
});
}
}, 1);
});