This commit is contained in:
syuilo 2018-04-12 03:46:32 +09:00
parent fba36561cd
commit 553fccd719
6 changed files with 184 additions and 28 deletions

View File

@ -1,12 +1,12 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
const AccessToken = db.get<IAccessTokens>('accessTokens'); const AccessToken = db.get<IAccessToken>('accessTokens');
AccessToken.createIndex('token'); AccessToken.createIndex('token');
AccessToken.createIndex('hash'); AccessToken.createIndex('hash');
export default AccessToken; export default AccessToken;
export type IAccessTokens = { export type IAccessToken = {
_id: mongo.ObjectID; _id: mongo.ObjectID;
createdAt: Date; createdAt: Date;
appId: mongo.ObjectID; appId: mongo.ObjectID;
@ -14,3 +14,30 @@ export type IAccessTokens = {
token: string; token: string;
hash: 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
});
}

View File

@ -1,8 +1,8 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
const Favorites = db.get<IFavorite>('favorites'); const Favorite = db.get<IFavorite>('favorites');
export default Favorites; export default Favorite;
export type IFavorite = { export type IFavorite = {
_id: mongo.ObjectID; _id: mongo.ObjectID;
@ -10,3 +10,30 @@ export type IFavorite = {
userId: mongo.ObjectID; userId: mongo.ObjectID;
noteId: 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
});
}

View File

@ -16,12 +16,35 @@ export interface INoteReaction {
reaction: string; 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 * Pack a reaction for API response
*
* @param {any} reaction
* @param {any} me?
* @return {Promise<any>}
*/ */
export const pack = ( export const pack = (
reaction: any, reaction: any,

View File

@ -11,3 +11,30 @@ export interface INoteWatching {
userId: mongo.ObjectID; userId: mongo.ObjectID;
noteId: 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
});
}

View File

@ -6,8 +6,11 @@ import { IUser, pack as packUser } from './user';
import { pack as packApp } from './app'; import { pack as packApp } from './app';
import { pack as packChannel } from './channel'; import { pack as packChannel } from './channel';
import Vote from './poll-vote'; import Vote from './poll-vote';
import Reaction from './note-reaction'; import Reaction, { deleteNoteReaction } from './note-reaction';
import { pack as packFile } from './drive-file'; 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<INote>('notes'); const Note = db.get<INote>('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; let n: INote;
// Populate // Populate
@ -88,17 +93,35 @@ export async function physicalDelete(note: string | mongo.ObjectID | INote) {
if (n == null) return; if (n == null) return;
// この投稿の返信をすべて削除 // このNoteへの返信をすべて削除
const replies = await Note.find({ await Promise.all((
replyId: n._id await Note.find({ replyId: n._id })
}); ).map(x => deleteNote(x)));
await Promise.all(replies.map(r => physicalDelete(r)));
// この投稿の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をすべて削除 // この投稿に対するFavoriteをすべて削除
await Promise.all((
await Favorite.find({ noteId: n._id })
).map(x => deleteFavorite(x)));
// このNoteを削除
await Note.remove({
_id: n._id
});
} }
/** /**

View File

@ -2,11 +2,15 @@ import * as mongo from 'mongodb';
import deepcopy = require('deepcopy'); import deepcopy = require('deepcopy');
import rap from '@prezzemolo/rap'; import rap from '@prezzemolo/rap';
import db from '../db/mongodb'; 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 Following from './following';
import Mute from './mute'; import Mute from './mute';
import getFriends from '../server/api/common/get-friends'; import getFriends from '../server/api/common/get-friends';
import config from '../config'; 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<IUser>('users'); const User = db.get<IUser>('users');
@ -122,8 +126,10 @@ export function init(user): IUser {
return user; 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; let u: IUser;
// Populate // Populate
@ -141,17 +147,40 @@ export async function physicalDelete(user: string | mongo.ObjectID | IUser) {
if (u == null) return; if (u == null) return;
// このユーザーが行った投稿をすべて削除 // このユーザーのAccessTokenをすべて削除
const notes = await Note.find({ userId: u._id }); await Promise.all((
await Promise.all(notes.map(n => physicalDeleteNote(n))); 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をすべて削除
// このユーザーを削除 // このユーザーを削除
} }