From b2103e3e8c0c4aaaedde926f240b90586946b177 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 12 Apr 2018 03:46:32 +0900 Subject: [PATCH] wip --- src/models/access-token.ts | 31 +++++++++++++++++++++-- src/models/favorite.ts | 31 +++++++++++++++++++++-- src/models/note-reaction.ts | 31 ++++++++++++++++++++--- src/models/note-watching.ts | 27 ++++++++++++++++++++ src/models/note.ts | 43 ++++++++++++++++++++++++-------- src/models/user.ts | 49 +++++++++++++++++++++++++++++-------- 6 files changed, 184 insertions(+), 28 deletions(-) diff --git a/src/models/access-token.ts b/src/models/access-token.ts index 4451ca140..9909ea01a 100644 --- a/src/models/access-token.ts +++ b/src/models/access-token.ts @@ -1,12 +1,12 @@ import * as mongo from 'mongodb'; import db from '../db/mongodb'; -const AccessToken = db.get('accessTokens'); +const AccessToken = db.get('accessTokens'); AccessToken.createIndex('token'); AccessToken.createIndex('hash'); export default AccessToken; -export type IAccessTokens = { +export type IAccessToken = { _id: mongo.ObjectID; createdAt: Date; appId: mongo.ObjectID; @@ -14,3 +14,30 @@ export type IAccessTokens = { token: string; hash: string; }; + +/** + * AccessTokenを物理削除します + */ +export async function deleteAccessToken(accessToken: string | mongo.ObjectID | IAccessToken) { + let a: IAccessToken; + + // Populate + if (mongo.ObjectID.prototype.isPrototypeOf(accessToken)) { + a = await AccessToken.findOne({ + _id: accessToken + }); + } else if (typeof accessToken === 'string') { + a = await AccessToken.findOne({ + _id: new mongo.ObjectID(accessToken) + }); + } else { + a = accessToken as IAccessToken; + } + + if (a == null) return; + + // このAccessTokenを削除 + await AccessToken.remove({ + _id: a._id + }); +} diff --git a/src/models/favorite.ts b/src/models/favorite.ts index 73f888192..b2c582808 100644 --- a/src/models/favorite.ts +++ b/src/models/favorite.ts @@ -1,8 +1,8 @@ import * as mongo from 'mongodb'; import db from '../db/mongodb'; -const Favorites = db.get('favorites'); -export default Favorites; +const Favorite = db.get('favorites'); +export default Favorite; export type IFavorite = { _id: mongo.ObjectID; @@ -10,3 +10,30 @@ export type IFavorite = { userId: mongo.ObjectID; noteId: mongo.ObjectID; }; + +/** + * Favoriteを物理削除します + */ +export async function deleteFavorite(favorite: string | mongo.ObjectID | IFavorite) { + let f: IFavorite; + + // Populate + if (mongo.ObjectID.prototype.isPrototypeOf(favorite)) { + f = await Favorite.findOne({ + _id: favorite + }); + } else if (typeof favorite === 'string') { + f = await Favorite.findOne({ + _id: new mongo.ObjectID(favorite) + }); + } else { + f = favorite as IFavorite; + } + + if (f == null) return; + + // このFavoriteを削除 + await Favorite.remove({ + _id: f._id + }); +} diff --git a/src/models/note-reaction.ts b/src/models/note-reaction.ts index d499442de..9bf467f22 100644 --- a/src/models/note-reaction.ts +++ b/src/models/note-reaction.ts @@ -16,12 +16,35 @@ export interface INoteReaction { reaction: string; } +/** + * NoteReactionを物理削除します + */ +export async function deleteNoteReaction(noteReaction: string | mongo.ObjectID | INoteReaction) { + let n: INoteReaction; + + // Populate + if (mongo.ObjectID.prototype.isPrototypeOf(noteReaction)) { + n = await NoteReaction.findOne({ + _id: noteReaction + }); + } else if (typeof noteReaction === 'string') { + n = await NoteReaction.findOne({ + _id: new mongo.ObjectID(noteReaction) + }); + } else { + n = noteReaction as INoteReaction; + } + + if (n == null) return; + + // このNoteReactionを削除 + await NoteReaction.remove({ + _id: n._id + }); +} + /** * Pack a reaction for API response - * - * @param {any} reaction - * @param {any} me? - * @return {Promise} */ export const pack = ( reaction: any, diff --git a/src/models/note-watching.ts b/src/models/note-watching.ts index b5ef3b61b..479f92dd4 100644 --- a/src/models/note-watching.ts +++ b/src/models/note-watching.ts @@ -11,3 +11,30 @@ export interface INoteWatching { userId: mongo.ObjectID; noteId: mongo.ObjectID; } + +/** + * NoteWatchingを物理削除します + */ +export async function deleteNoteWatching(noteWatching: string | mongo.ObjectID | INoteWatching) { + let n: INoteWatching; + + // Populate + if (mongo.ObjectID.prototype.isPrototypeOf(noteWatching)) { + n = await NoteWatching.findOne({ + _id: noteWatching + }); + } else if (typeof noteWatching === 'string') { + n = await NoteWatching.findOne({ + _id: new mongo.ObjectID(noteWatching) + }); + } else { + n = noteWatching as INoteWatching; + } + + if (n == null) return; + + // このNoteWatchingを削除 + await NoteWatching.remove({ + _id: n._id + }); +} diff --git a/src/models/note.ts b/src/models/note.ts index a11da196c..6e7b6cee7 100644 --- a/src/models/note.ts +++ b/src/models/note.ts @@ -6,8 +6,11 @@ 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 Reaction from './note-reaction'; +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'; const Note = db.get('notes'); @@ -69,8 +72,10 @@ export type INote = { }; }; -// TODO -export async function physicalDelete(note: string | mongo.ObjectID | INote) { +/** + * Noteを物理削除します + */ +export async function deleteNote(note: string | mongo.ObjectID | INote) { let n: INote; // Populate @@ -88,17 +93,35 @@ export async function physicalDelete(note: string | mongo.ObjectID | INote) { if (n == null) return; - // この投稿の返信をすべて削除 - const replies = await Note.find({ - replyId: n._id - }); - await Promise.all(replies.map(r => physicalDelete(r))); + // このNoteへの返信をすべて削除 + await Promise.all(( + await Note.find({ replyId: n._id }) + ).map(x => deleteNote(x))); - // この投稿のWatchをすべて削除 + // このNoteのRenoteをすべて削除 + await Promise.all(( + await Note.find({ renoteId: n._id }) + ).map(x => deleteNote(x))); - // この投稿のReactionをすべて削除 + // この投稿に対するNoteWatchingをすべて削除 + await Promise.all(( + await NoteWatching.find({ noteId: n._id }) + ).map(x => deleteNoteWatching(x))); + + // この投稿に対するNoteReactionをすべて削除 + await Promise.all(( + await NoteReaction.find({ noteId: n._id }) + ).map(x => deleteNoteReaction(x))); // この投稿に対するFavoriteをすべて削除 + await Promise.all(( + await Favorite.find({ noteId: n._id }) + ).map(x => deleteFavorite(x))); + + // このNoteを削除 + await Note.remove({ + _id: n._id + }); } /** diff --git a/src/models/user.ts b/src/models/user.ts index cdf9a564f..b1a68b082 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -2,11 +2,15 @@ import * as mongo from 'mongodb'; import deepcopy = require('deepcopy'); import rap from '@prezzemolo/rap'; import db from '../db/mongodb'; -import Note, { INote, pack as packNote, physicalDelete as physicalDeleteNote } from './note'; +import Note, { INote, pack as packNote, deleteNote } from './note'; import Following from './following'; import Mute from './mute'; import getFriends from '../server/api/common/get-friends'; import config from '../config'; +import AccessToken, { deleteAccessToken } from './access-token'; +import NoteWatching, { deleteNoteWatching } from './note-watching'; +import Favorite, { deleteFavorite } from './favorite'; +import NoteReaction, { deleteNoteReaction } from './note-reaction'; const User = db.get('users'); @@ -122,8 +126,10 @@ export function init(user): IUser { return user; } -// TODO -export async function physicalDelete(user: string | mongo.ObjectID | IUser) { +/** + * Userを物理削除します + */ +export async function deleteUser(user: string | mongo.ObjectID | IUser) { let u: IUser; // Populate @@ -141,17 +147,40 @@ export async function physicalDelete(user: string | mongo.ObjectID | IUser) { if (u == null) return; - // このユーザーが行った投稿をすべて削除 - const notes = await Note.find({ userId: u._id }); - await Promise.all(notes.map(n => physicalDeleteNote(n))); + // このユーザーのAccessTokenをすべて削除 + await Promise.all(( + await AccessToken.find({ userId: u._id }) + ).map(x => deleteAccessToken(x))); - // このユーザーのお気に入りをすべて削除 + // このユーザーのNoteをすべて削除 + await Promise.all(( + await Note.find({ userId: u._id }) + ).map(x => deleteNote(x))); - // このユーザーが行ったメッセージをすべて削除 + // このユーザーのNoteReactionをすべて削除 + await Promise.all(( + await NoteReaction.find({ userId: u._id }) + ).map(x => deleteNoteReaction(x))); - // このユーザーのドライブのファイルをすべて削除 + // このユーザーのNoteWatchingをすべて削除 + await Promise.all(( + await NoteWatching.find({ userId: u._id }) + ).map(x => deleteNoteWatching(x))); - // このユーザーに関するfollowingをすべて削除 + // このユーザーのFavoriteをすべて削除 + await Promise.all(( + await Favorite.find({ userId: u._id }) + ).map(x => deleteFavorite(x))); + + // このユーザーのMessageをすべて削除 + + // このユーザーへのMessageをすべて削除 + + // このユーザーのDriveFileをすべて削除 + + // このユーザーのFollowingをすべて削除 + + // このユーザーへのFollowingをすべて削除 // このユーザーを削除 }