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] 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 })