blob: 1fe33f034233a921f67e8c245bcb32e6a7ed8955 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
import * as mongo from 'mongodb';
const deepcopy = require('deepcopy');
import db from '../db/mongodb';
import isObjectId from '../misc/is-objectid';
import { pack as packUser } from './user';
const AbuseUserReport = db.get<IAbuseUserReport>('abuseUserReports');
AbuseUserReport.createIndex('userId');
AbuseUserReport.createIndex('reporterId');
AbuseUserReport.createIndex(['userId', 'reporterId'], { unique: true });
export default AbuseUserReport;
export interface IAbuseUserReport {
_id: mongo.ObjectID;
createdAt: Date;
userId: mongo.ObjectID;
reporterId: mongo.ObjectID;
comment: string;
}
export const packMany = (
reports: (string | mongo.ObjectID | IAbuseUserReport)[]
) => {
return Promise.all(reports.map(x => pack(x)));
};
export const pack = (
report: any
) => new Promise<any>(async (resolve, reject) => {
let _report: any;
if (isObjectId(report)) {
_report = await AbuseUserReport.findOne({
_id: report
});
} else if (typeof report === 'string') {
_report = await AbuseUserReport.findOne({
_id: new mongo.ObjectID(report)
});
} else {
_report = deepcopy(report);
}
// Rename _id to id
_report.id = _report._id;
delete _report._id;
_report.reporter = await packUser(_report.reporterId, null, { detail: true });
_report.user = await packUser(_report.userId, null, { detail: true });
resolve(_report);
});
|