2019-02-04 20:48:08 -06:00
|
|
|
import $ from 'cafy';
|
2019-04-07 07:50:36 -05:00
|
|
|
import { ID } from '../../../../misc/cafy-id';
|
2019-01-19 04:16:48 -06:00
|
|
|
import define from '../../define';
|
2019-02-04 23:14:23 -06:00
|
|
|
import { publishAdminStream } from '../../../../services/stream';
|
2019-02-21 20:46:58 -06:00
|
|
|
import { ApiError } from '../../error';
|
2019-02-21 23:02:56 -06:00
|
|
|
import { getUser } from '../../common/getters';
|
2019-04-07 07:50:36 -05:00
|
|
|
import { AbuseUserReports, Users } from '../../../../models';
|
|
|
|
import { genId } from '../../../../misc/gen-id';
|
2019-01-19 04:16:48 -06:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '指定したユーザーを迷惑なユーザーであると報告します。'
|
|
|
|
},
|
|
|
|
|
2019-02-22 20:20:58 -06:00
|
|
|
tags: ['users'],
|
|
|
|
|
2020-02-15 06:33:32 -06:00
|
|
|
requireCredential: true as const,
|
2019-01-19 04:16:48 -06:00
|
|
|
|
|
|
|
params: {
|
|
|
|
userId: {
|
|
|
|
validator: $.type(ID),
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '対象のユーザーのID',
|
|
|
|
'en-US': 'Target user ID'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
comment: {
|
|
|
|
validator: $.str.range(1, 3000),
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '迷惑行為の詳細'
|
|
|
|
}
|
|
|
|
},
|
2019-02-21 20:46:58 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
2019-01-19 04:16:48 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-21 20:46:58 -06:00
|
|
|
export default define(meta, async (ps, me) => {
|
2019-01-19 04:16:48 -06:00
|
|
|
// Lookup user
|
2019-02-21 23:02:56 -06:00
|
|
|
const user = await getUser(ps.userId).catch(e => {
|
|
|
|
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
|
|
|
|
throw e;
|
2019-01-19 04:16:48 -06:00
|
|
|
});
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (user.id === me.id) {
|
2019-02-21 20:46:58 -06:00
|
|
|
throw new ApiError(meta.errors.cannotReportYourself);
|
2019-01-19 04:16:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (user.isAdmin) {
|
2019-02-21 20:46:58 -06:00
|
|
|
throw new ApiError(meta.errors.cannotReportAdmin);
|
2019-01-19 04:16:48 -06:00
|
|
|
}
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
const report = await AbuseUserReports.save({
|
|
|
|
id: genId(),
|
2019-01-19 04:16:48 -06:00
|
|
|
createdAt: new Date(),
|
2019-04-07 07:50:36 -05:00
|
|
|
userId: user.id,
|
|
|
|
reporterId: me.id,
|
2019-01-19 04:16:48 -06:00
|
|
|
comment: ps.comment
|
|
|
|
});
|
|
|
|
|
2019-01-26 23:55:02 -06:00
|
|
|
// Publish event to moderators
|
|
|
|
setTimeout(async () => {
|
2019-04-07 07:50:36 -05:00
|
|
|
const moderators = await Users.find({
|
|
|
|
where: [{
|
2019-01-26 23:55:02 -06:00
|
|
|
isAdmin: true
|
|
|
|
}, {
|
|
|
|
isModerator: true
|
|
|
|
}]
|
|
|
|
});
|
2019-02-21 20:46:58 -06:00
|
|
|
|
2019-01-26 23:55:02 -06:00
|
|
|
for (const moderator of moderators) {
|
2019-04-07 07:50:36 -05:00
|
|
|
publishAdminStream(moderator.id, 'newAbuseUserReport', {
|
|
|
|
id: report.id,
|
2019-01-26 23:55:02 -06:00
|
|
|
userId: report.userId,
|
|
|
|
reporterId: report.reporterId,
|
|
|
|
comment: report.comment
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 1);
|
2019-02-21 20:46:58 -06:00
|
|
|
});
|