From 7b05e01819e214f8eadc64e8cca259f0af95157a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=93=E3=81=B4=E3=81=AA=E3=81=9F=E3=81=BF=E3=81=BD?= Date: Thu, 12 Apr 2018 07:25:46 +0900 Subject: [PATCH] wip --- src/models/followed-log.ts | 33 ++++++++++++++++++++++++++++++--- src/models/following-log.ts | 33 ++++++++++++++++++++++++++++++--- src/models/user.ts | 8 ++++++++ 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/src/models/followed-log.ts b/src/models/followed-log.ts index 9e3ca17822..7d488b9cd3 100644 --- a/src/models/followed-log.ts +++ b/src/models/followed-log.ts @@ -1,12 +1,39 @@ -import { ObjectID } from 'mongodb'; +import * as mongo from 'mongodb'; import db from '../db/mongodb'; const FollowedLog = db.get('followedLogs'); export default FollowedLog; export type IFollowedLog = { - _id: ObjectID; + _id: mongo.ObjectID; createdAt: Date; - userId: ObjectID; + userId: mongo.ObjectID; count: number; }; + +/** + * FollowedLogを物理削除します + */ +export async function deleteFollowedLog(followedLog: string | mongo.ObjectID | IFollowedLog) { + let f: IFollowedLog; + + // Populate + if (mongo.ObjectID.prototype.isPrototypeOf(followedLog)) { + f = await FollowedLog.findOne({ + _id: followedLog + }); + } else if (typeof followedLog === 'string') { + f = await FollowedLog.findOne({ + _id: new mongo.ObjectID(followedLog) + }); + } else { + f = followedLog as IFollowedLog; + } + + if (f == null) return; + + // このFollowedLogを削除 + await FollowedLog.remove({ + _id: f._id + }); +} diff --git a/src/models/following-log.ts b/src/models/following-log.ts index 045ff7bf02..c06a337fd4 100644 --- a/src/models/following-log.ts +++ b/src/models/following-log.ts @@ -1,12 +1,39 @@ -import { ObjectID } from 'mongodb'; +import * as mongo from 'mongodb'; import db from '../db/mongodb'; const FollowingLog = db.get('followingLogs'); export default FollowingLog; export type IFollowingLog = { - _id: ObjectID; + _id: mongo.ObjectID; createdAt: Date; - userId: ObjectID; + userId: mongo.ObjectID; count: number; }; + +/** + * FollowingLogを物理削除します + */ +export async function deleteFollowingLog(followingLog: string | mongo.ObjectID | IFollowingLog) { + let f: IFollowingLog; + + // Populate + if (mongo.ObjectID.prototype.isPrototypeOf(followingLog)) { + f = await FollowingLog.findOne({ + _id: followingLog + }); + } else if (typeof followingLog === 'string') { + f = await FollowingLog.findOne({ + _id: new mongo.ObjectID(followingLog) + }); + } else { + f = followingLog as IFollowingLog; + } + + if (f == null) return; + + // このFollowingLogを削除 + await FollowingLog.remove({ + _id: f._id + }); +} diff --git a/src/models/user.ts b/src/models/user.ts index d7249c944d..a4b7becbd2 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -16,6 +16,8 @@ import MessagingHistory, { deleteMessagingHistory } from './messaging-history'; import DriveFile, { deleteDriveFile } from './drive-file'; import DriveFolder, { deleteDriveFolder } from './drive-folder'; import PollVote, { deletePollVote } from './poll-vote'; +import FollowingLog, { deleteFollowingLog } from './following-log'; +import FollowedLog, { deleteFollowedLog } from './followed-log'; const User = db.get('users'); @@ -228,8 +230,14 @@ export async function deleteUser(user: string | mongo.ObjectID | IUser) { ).map(x => deleteFollowing(x))); // このユーザーのFollowingLogをすべて削除 + await Promise.all(( + await FollowingLog.find({ userId: u._id }) + ).map(x => deleteFollowingLog(x))); // このユーザーのFollowedLogをすべて削除 + await Promise.all(( + await FollowedLog.find({ userId: u._id }) + ).map(x => deleteFollowedLog(x))); // このユーザーを削除 }