diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2019-01-19 19:16:48 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2019-01-19 19:16:48 +0900 |
| commit | 048b9c295ed77f665c22f4b3f923013d0d9be990 (patch) | |
| tree | 94fcc3b915b6dab60ff66f2e08d1c65d8a25e0c0 /src/models | |
| parent | Fix typo (diff) | |
| download | sharkey-048b9c295ed77f665c22f4b3f923013d0d9be990.tar.gz sharkey-048b9c295ed77f665c22f4b3f923013d0d9be990.tar.bz2 sharkey-048b9c295ed77f665c22f4b3f923013d0d9be990.zip | |
スパム報告機能
Resolve #1970
Diffstat (limited to 'src/models')
| -rw-r--r-- | src/models/abuse-user-report.ts | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/models/abuse-user-report.ts b/src/models/abuse-user-report.ts new file mode 100644 index 0000000000..1fe33f0342 --- /dev/null +++ b/src/models/abuse-user-report.ts @@ -0,0 +1,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); +}); |