From a015524cb5874dd33c884588dd2e35faa63ca08f 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:13:15 +0900 Subject: [PATCH 1/4] wip --- src/models/following.ts | 27 +++++++++++++++++++++++++++ src/models/user.ts | 8 +++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/models/following.ts b/src/models/following.ts index b4090d8c7e..f10e349ee9 100644 --- a/src/models/following.ts +++ b/src/models/following.ts @@ -11,3 +11,30 @@ export type IFollowing = { followeeId: mongo.ObjectID; followerId: mongo.ObjectID; }; + +/** + * Followingを物理削除します + */ +export async function deleteFollowing(following: string | mongo.ObjectID | IFollowing) { + let f: IFollowing; + + // Populate + if (mongo.ObjectID.prototype.isPrototypeOf(following)) { + f = await Following.findOne({ + _id: following + }); + } else if (typeof following === 'string') { + f = await Following.findOne({ + _id: new mongo.ObjectID(following) + }); + } else { + f = following as IFollowing; + } + + if (f == null) return; + + // このFollowingを削除 + await Following.remove({ + _id: f._id + }); +} diff --git a/src/models/user.ts b/src/models/user.ts index ff1c11e76c..cbc445256b 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -3,7 +3,7 @@ import deepcopy = require('deepcopy'); import rap from '@prezzemolo/rap'; import db from '../db/mongodb'; import Note, { INote, pack as packNote, deleteNote } from './note'; -import Following from './following'; +import Following, { deleteFollowing } from './following'; import Mute, { deleteMute } from './mute'; import getFriends from '../server/api/common/get-friends'; import config from '../config'; @@ -212,8 +212,14 @@ export async function deleteUser(user: string | mongo.ObjectID | IUser) { ).map(x => deleteMute(x))); // このユーザーのFollowingをすべて削除 + await Promise.all(( + await Following.find({ followerId: u._id }) + ).map(x => deleteFollowing(x))); // このユーザーへのFollowingをすべて削除 + await Promise.all(( + await Following.find({ followeeId: u._id }) + ).map(x => deleteFollowing(x))); // このユーザーのFollowingLogをすべて削除 From 991635f9190976433f9e03cef76eb36454145a19 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:19:28 +0900 Subject: [PATCH 2/4] wip --- src/models/note.ts | 8 +++++++- src/models/poll-vote.ts | 27 +++++++++++++++++++++++++++ src/models/user.ts | 6 ++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/models/note.ts b/src/models/note.ts index 6e7b6cee79..3c1c2e18e1 100644 --- a/src/models/note.ts +++ b/src/models/note.ts @@ -5,12 +5,13 @@ import db from '../db/mongodb'; import { IUser, pack as packUser } from './user'; import { pack as packApp } from './app'; import { pack as packChannel } from './channel'; -import Vote from './poll-vote'; +import Vote, { deletePollVote } from './poll-vote'; import Reaction, { deleteNoteReaction } from './note-reaction'; import { pack as packFile } from './drive-file'; import NoteWatching, { deleteNoteWatching } from './note-watching'; import NoteReaction from './note-reaction'; import Favorite, { deleteFavorite } from './favorite'; +import PollVote from './poll-vote'; const Note = db.get('notes'); @@ -113,6 +114,11 @@ export async function deleteNote(note: string | mongo.ObjectID | INote) { await NoteReaction.find({ noteId: n._id }) ).map(x => deleteNoteReaction(x))); + // この投稿に対するPollVoteをすべて削除 + await Promise.all(( + await PollVote.find({ noteId: n._id }) + ).map(x => deletePollVote(x))); + // この投稿に対するFavoriteをすべて削除 await Promise.all(( await Favorite.find({ noteId: n._id }) diff --git a/src/models/poll-vote.ts b/src/models/poll-vote.ts index 4d33b100e7..85c8454ddc 100644 --- a/src/models/poll-vote.ts +++ b/src/models/poll-vote.ts @@ -11,3 +11,30 @@ export interface IPollVote { noteId: mongo.ObjectID; choice: number; } + +/** + * PollVoteを物理削除します + */ +export async function deletePollVote(pollVote: string | mongo.ObjectID | IPollVote) { + let p: IPollVote; + + // Populate + if (mongo.ObjectID.prototype.isPrototypeOf(pollVote)) { + p = await PollVote.findOne({ + _id: pollVote + }); + } else if (typeof pollVote === 'string') { + p = await PollVote.findOne({ + _id: new mongo.ObjectID(pollVote) + }); + } else { + p = pollVote as IPollVote; + } + + if (p == null) return; + + // このPollVoteを削除 + await PollVote.remove({ + _id: p._id + }); +} diff --git a/src/models/user.ts b/src/models/user.ts index cbc445256b..d7249c944d 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -15,6 +15,7 @@ import MessagingMessage, { deleteMessagingMessage } from './messaging-message'; import MessagingHistory, { deleteMessagingHistory } from './messaging-history'; import DriveFile, { deleteDriveFile } from './drive-file'; import DriveFolder, { deleteDriveFolder } from './drive-folder'; +import PollVote, { deletePollVote } from './poll-vote'; const User = db.get('users'); @@ -171,6 +172,11 @@ export async function deleteUser(user: string | mongo.ObjectID | IUser) { await NoteWatching.find({ userId: u._id }) ).map(x => deleteNoteWatching(x))); + // このユーザーのPollVoteをすべて削除 + await Promise.all(( + await PollVote.find({ userId: u._id }) + ).map(x => deletePollVote(x))); + // このユーザーのFavoriteをすべて削除 await Promise.all(( await Favorite.find({ userId: u._id }) 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 3/4] 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))); // このユーザーを削除 } From 051ab451816edfb71f54d8aa887a13633cdda187 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:32:35 +0900 Subject: [PATCH 4/4] wip --- src/models/sw-subscription.ts | 28 ++++++++++++++++++++++++++++ src/models/user.ts | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/src/models/sw-subscription.ts b/src/models/sw-subscription.ts index 743d0d2dd9..621ac8a9b6 100644 --- a/src/models/sw-subscription.ts +++ b/src/models/sw-subscription.ts @@ -11,3 +11,31 @@ export interface ISwSubscription { auth: string; publickey: string; } + +/** + * SwSubscriptionを物理削除します + */ +export async function deleteSwSubscription(swSubscription: string | mongo.ObjectID | ISwSubscription) { + let s: ISwSubscription; + + // Populate + if (mongo.ObjectID.prototype.isPrototypeOf(swSubscription)) { + s = await SwSubscription.findOne({ + _id: swSubscription + }); + } else if (typeof swSubscription === 'string') { + s = await SwSubscription.findOne({ + _id: new mongo.ObjectID(swSubscription) + }); + } else { + s = swSubscription as ISwSubscription; + } + + if (s == null) return; + + // このSwSubscriptionを削除 + await SwSubscription.remove({ + _id: s._id + }); +} + diff --git a/src/models/user.ts b/src/models/user.ts index a4b7becbd2..c121790c31 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -18,6 +18,7 @@ import DriveFolder, { deleteDriveFolder } from './drive-folder'; import PollVote, { deletePollVote } from './poll-vote'; import FollowingLog, { deleteFollowingLog } from './following-log'; import FollowedLog, { deleteFollowedLog } from './followed-log'; +import SwSubscription, { deleteSwSubscription } from './sw-subscription'; const User = db.get('users'); @@ -239,6 +240,11 @@ export async function deleteUser(user: string | mongo.ObjectID | IUser) { await FollowedLog.find({ userId: u._id }) ).map(x => deleteFollowedLog(x))); + // このユーザーのSwSubscriptionをすべて削除 + await Promise.all(( + await SwSubscription.find({ userId: u._id }) + ).map(x => deleteSwSubscription(x))); + // このユーザーを削除 }