2019-02-05 03:48:08 +01:00
|
|
|
import $ from 'cafy';
|
2021-11-12 13:11:15 +01:00
|
|
|
import * as sanitizeHtml from 'sanitize-html';
|
2021-08-19 14:55:45 +02:00
|
|
|
import { ID } from '@/misc/cafy-id';
|
|
|
|
import define from '../../define';
|
|
|
|
import { publishAdminStream } from '@/services/stream';
|
|
|
|
import { ApiError } from '../../error';
|
|
|
|
import { getUser } from '../../common/getters';
|
|
|
|
import { AbuseUserReports, Users } from '@/models/index';
|
|
|
|
import { genId } from '@/misc/gen-id';
|
2021-11-12 13:11:15 +01:00
|
|
|
import { sendEmail } from '@/services/send-email';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta';
|
2019-01-19 11:16:48 +01:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['users'],
|
|
|
|
|
2020-02-15 13:33:32 +01:00
|
|
|
requireCredential: true as const,
|
2019-01-19 11:16:48 +01:00
|
|
|
|
|
|
|
params: {
|
|
|
|
userId: {
|
|
|
|
validator: $.type(ID),
|
|
|
|
},
|
|
|
|
|
|
|
|
comment: {
|
2020-10-19 12:29:04 +02:00
|
|
|
validator: $.str.range(1, 2048),
|
2019-01-19 11:16:48 +01:00
|
|
|
},
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-11-12 13:11:15 +01:00
|
|
|
id: '1acefcb5-0959-43fd-9685-b48305736cb5',
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
cannotReportYourself: {
|
|
|
|
message: 'Cannot report yourself.',
|
|
|
|
code: 'CANNOT_REPORT_YOURSELF',
|
2021-11-12 13:11:15 +01:00
|
|
|
id: '1e13149e-b1e8-43cf-902e-c01dbfcb202f',
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
cannotReportAdmin: {
|
|
|
|
message: 'Cannot report the admin.',
|
|
|
|
code: 'CANNOT_REPORT_THE_ADMIN',
|
2021-11-12 13:11:15 +01:00
|
|
|
id: '35e166f5-05fb-4f87-a2d5-adb42676d48f',
|
|
|
|
},
|
|
|
|
},
|
2019-01-19 11:16:48 +01:00
|
|
|
};
|
|
|
|
|
2021-11-12 13:11:15 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2019-02-22 03:46:58 +01:00
|
|
|
export default define(meta, async (ps, me) => {
|
2019-01-19 11:16:48 +01:00
|
|
|
// Lookup user
|
2019-02-22 06:02:56 +01: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 11:16:48 +01:00
|
|
|
});
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (user.id === me.id) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.cannotReportYourself);
|
2019-01-19 11:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (user.isAdmin) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.cannotReportAdmin);
|
2019-01-19 11:16:48 +01:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const report = await AbuseUserReports.save({
|
|
|
|
id: genId(),
|
2019-01-19 11:16:48 +01:00
|
|
|
createdAt: new Date(),
|
2020-10-19 12:29:04 +02:00
|
|
|
targetUserId: user.id,
|
|
|
|
targetUserHost: user.host,
|
2019-04-07 14:50:36 +02:00
|
|
|
reporterId: me.id,
|
2020-10-19 12:29:04 +02:00
|
|
|
reporterHost: null,
|
|
|
|
comment: ps.comment,
|
2019-01-19 11:16:48 +01:00
|
|
|
});
|
|
|
|
|
2019-01-27 06:55:02 +01:00
|
|
|
// Publish event to moderators
|
|
|
|
setTimeout(async () => {
|
2019-04-07 14:50:36 +02:00
|
|
|
const moderators = await Users.find({
|
|
|
|
where: [{
|
2021-11-12 13:11:15 +01:00
|
|
|
isAdmin: true,
|
2019-01-27 06:55:02 +01:00
|
|
|
}, {
|
2021-11-12 13:11:15 +01:00
|
|
|
isModerator: true,
|
|
|
|
}],
|
2019-01-27 06:55:02 +01:00
|
|
|
});
|
2019-02-22 03:46:58 +01:00
|
|
|
|
2019-01-27 06:55:02 +01:00
|
|
|
for (const moderator of moderators) {
|
2019-04-07 14:50:36 +02:00
|
|
|
publishAdminStream(moderator.id, 'newAbuseUserReport', {
|
|
|
|
id: report.id,
|
2020-10-19 12:29:04 +02:00
|
|
|
targetUserId: report.targetUserId,
|
2019-01-27 06:55:02 +01:00
|
|
|
reporterId: report.reporterId,
|
2021-11-12 13:11:15 +01:00
|
|
|
comment: report.comment,
|
2019-01-27 06:55:02 +01:00
|
|
|
});
|
|
|
|
}
|
2021-11-12 13:11:15 +01:00
|
|
|
|
|
|
|
const meta = await fetchMeta();
|
|
|
|
if (meta.email) {
|
|
|
|
sendEmail(meta.email, 'New abuse report',
|
|
|
|
sanitizeHtml(ps.comment),
|
|
|
|
sanitizeHtml(ps.comment));
|
|
|
|
}
|
2019-01-27 06:55:02 +01:00
|
|
|
}, 1);
|
2019-02-22 03:46:58 +01:00
|
|
|
});
|