2017-09-07 14:13:01 -05:00
|
|
|
import * as mongo from 'mongodb';
|
2018-02-01 17:06:01 -06:00
|
|
|
import deepcopy = require('deepcopy');
|
|
|
|
import rap from '@prezzemolo/rap';
|
2018-03-29 06:32:18 -05:00
|
|
|
import db from '../db/mongodb';
|
2018-04-11 13:46:32 -05:00
|
|
|
import Note, { INote, pack as packNote, deleteNote } from './note';
|
2018-04-11 17:13:15 -05:00
|
|
|
import Following, { deleteFollowing } from './following';
|
2018-04-11 15:50:45 -05:00
|
|
|
import Mute, { deleteMute } from './mute';
|
2018-03-29 06:32:18 -05:00
|
|
|
import getFriends from '../server/api/common/get-friends';
|
2018-04-01 23:15:53 -05:00
|
|
|
import config from '../config';
|
2018-04-11 13:46:32 -05:00
|
|
|
import AccessToken, { deleteAccessToken } from './access-token';
|
|
|
|
import NoteWatching, { deleteNoteWatching } from './note-watching';
|
|
|
|
import Favorite, { deleteFavorite } from './favorite';
|
|
|
|
import NoteReaction, { deleteNoteReaction } from './note-reaction';
|
2018-04-11 14:05:03 -05:00
|
|
|
import MessagingMessage, { deleteMessagingMessage } from './messaging-message';
|
|
|
|
import MessagingHistory, { deleteMessagingHistory } from './messaging-history';
|
2018-04-11 14:22:06 -05:00
|
|
|
import DriveFile, { deleteDriveFile } from './drive-file';
|
|
|
|
import DriveFolder, { deleteDriveFolder } from './drive-folder';
|
2017-01-16 18:12:33 -06:00
|
|
|
|
2018-02-01 17:06:01 -06:00
|
|
|
const User = db.get<IUser>('users');
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-02-01 17:06:01 -06:00
|
|
|
User.createIndex('username');
|
2018-04-11 12:53:48 -05:00
|
|
|
User.createIndex('usernameLower');
|
2018-04-07 13:58:11 -05:00
|
|
|
User.createIndex('token');
|
2018-04-08 09:29:27 -05:00
|
|
|
User.createIndex('uri', { sparse: true, unique: true });
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-02-01 17:06:01 -06:00
|
|
|
export default User;
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-04-01 14:01:34 -05:00
|
|
|
type IUserBase = {
|
2017-09-07 14:13:01 -05:00
|
|
|
_id: mongo.ObjectID;
|
2018-03-29 00:48:47 -05:00
|
|
|
createdAt: Date;
|
|
|
|
deletedAt: Date;
|
|
|
|
followersCount: number;
|
|
|
|
followingCount: number;
|
2018-04-05 11:36:34 -05:00
|
|
|
name?: string;
|
2018-04-07 12:30:37 -05:00
|
|
|
notesCount: number;
|
2018-03-29 00:48:47 -05:00
|
|
|
driveCapacity: number;
|
2017-09-07 14:13:01 -05:00
|
|
|
username: string;
|
2018-03-29 00:48:47 -05:00
|
|
|
usernameLower: string;
|
|
|
|
avatarId: mongo.ObjectID;
|
|
|
|
bannerId: mongo.ObjectID;
|
2017-09-07 14:13:01 -05:00
|
|
|
data: any;
|
|
|
|
description: string;
|
2018-04-07 12:30:37 -05:00
|
|
|
latestNote: INote;
|
|
|
|
pinnedNoteId: mongo.ObjectID;
|
2018-03-29 00:48:47 -05:00
|
|
|
isSuspended: boolean;
|
2017-09-07 14:13:01 -05:00
|
|
|
keywords: string[];
|
2018-04-01 21:14:45 -05:00
|
|
|
host: string;
|
2018-03-29 00:48:47 -05:00
|
|
|
hostLower: string;
|
2017-09-07 14:13:01 -05:00
|
|
|
};
|
2017-10-06 17:23:00 -05:00
|
|
|
|
2018-04-01 14:48:38 -05:00
|
|
|
export interface ILocalUser extends IUserBase {
|
|
|
|
host: null;
|
2018-04-07 13:58:11 -05:00
|
|
|
keypair: string;
|
|
|
|
email: string;
|
|
|
|
links: string[];
|
|
|
|
password: string;
|
|
|
|
token: string;
|
|
|
|
twitter: {
|
|
|
|
accessToken: string;
|
|
|
|
accessTokenSecret: string;
|
|
|
|
userId: string;
|
|
|
|
screenName: string;
|
2018-04-01 14:48:38 -05:00
|
|
|
};
|
2018-04-07 13:58:11 -05:00
|
|
|
line: {
|
|
|
|
userId: string;
|
|
|
|
};
|
|
|
|
profile: {
|
|
|
|
location: string;
|
|
|
|
birthday: string; // 'YYYY-MM-DD'
|
|
|
|
tags: string[];
|
|
|
|
};
|
|
|
|
lastUsedAt: Date;
|
|
|
|
isBot: boolean;
|
|
|
|
isPro: boolean;
|
|
|
|
twoFactorSecret: string;
|
|
|
|
twoFactorEnabled: boolean;
|
|
|
|
twoFactorTempSecret: string;
|
|
|
|
clientSettings: any;
|
|
|
|
settings: any;
|
2018-04-01 14:48:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface IRemoteUser extends IUserBase {
|
2018-04-07 13:58:11 -05:00
|
|
|
inbox: string;
|
|
|
|
uri: string;
|
|
|
|
publicKey: {
|
|
|
|
id: string;
|
|
|
|
publicKeyPem: string;
|
2018-04-01 14:48:38 -05:00
|
|
|
};
|
|
|
|
}
|
2018-04-01 14:01:34 -05:00
|
|
|
|
2018-04-01 14:52:11 -05:00
|
|
|
export type IUser = ILocalUser | IRemoteUser;
|
|
|
|
|
2018-04-01 14:01:34 -05:00
|
|
|
export const isLocalUser = (user: any): user is ILocalUser =>
|
|
|
|
user.host === null;
|
|
|
|
|
|
|
|
export const isRemoteUser = (user: any): user is IRemoteUser =>
|
|
|
|
!isLocalUser(user);
|
2018-03-31 05:55:00 -05:00
|
|
|
|
2018-04-01 21:14:45 -05:00
|
|
|
//#region Validators
|
2018-04-01 14:52:11 -05:00
|
|
|
export function validateUsername(username: string): boolean {
|
2018-04-09 03:09:57 -05:00
|
|
|
return typeof username == 'string' && /^[a-zA-Z0-9_]{1,20}$/.test(username);
|
2018-04-01 14:52:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function validatePassword(password: string): boolean {
|
|
|
|
return typeof password == 'string' && password != '';
|
|
|
|
}
|
|
|
|
|
2018-04-05 11:36:34 -05:00
|
|
|
export function isValidName(name?: string): boolean {
|
|
|
|
return name === null || (typeof name == 'string' && name.length < 30 && name.trim() != '');
|
2018-04-01 14:52:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isValidDescription(description: string): boolean {
|
|
|
|
return typeof description == 'string' && description.length < 500 && description.trim() != '';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isValidLocation(location: string): boolean {
|
|
|
|
return typeof location == 'string' && location.length < 50 && location.trim() != '';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isValidBirthday(birthday: string): boolean {
|
|
|
|
return typeof birthday == 'string' && /^([0-9]{4})\-([0-9]{2})-([0-9]{2})$/.test(birthday);
|
|
|
|
}
|
2018-04-01 21:14:45 -05:00
|
|
|
//#endregion
|
2018-04-01 14:52:11 -05:00
|
|
|
|
2017-10-06 17:23:00 -05:00
|
|
|
export function init(user): IUser {
|
|
|
|
user._id = new mongo.ObjectID(user._id);
|
2018-03-29 00:48:47 -05:00
|
|
|
user.avatarId = new mongo.ObjectID(user.avatarId);
|
|
|
|
user.bannerId = new mongo.ObjectID(user.bannerId);
|
2018-04-07 12:30:37 -05:00
|
|
|
user.pinnedNoteId = new mongo.ObjectID(user.pinnedNoteId);
|
2017-10-06 17:34:35 -05:00
|
|
|
return user;
|
2017-10-06 17:23:00 -05:00
|
|
|
}
|
2018-02-01 17:06:01 -06:00
|
|
|
|
2018-04-11 13:46:32 -05:00
|
|
|
/**
|
|
|
|
* Userを物理削除します
|
|
|
|
*/
|
|
|
|
export async function deleteUser(user: string | mongo.ObjectID | IUser) {
|
2018-04-11 04:24:42 -05:00
|
|
|
let u: IUser;
|
|
|
|
|
|
|
|
// Populate
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(user)) {
|
|
|
|
u = await User.findOne({
|
|
|
|
_id: user
|
|
|
|
});
|
|
|
|
} else if (typeof user === 'string') {
|
|
|
|
u = await User.findOne({
|
|
|
|
_id: new mongo.ObjectID(user)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
u = user as IUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u == null) return;
|
|
|
|
|
2018-04-11 13:46:32 -05:00
|
|
|
// このユーザーの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)));
|
|
|
|
|
|
|
|
// このユーザーのFavoriteをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await Favorite.find({ userId: u._id })
|
|
|
|
).map(x => deleteFavorite(x)));
|
|
|
|
|
|
|
|
// このユーザーのMessageをすべて削除
|
2018-04-11 14:05:03 -05:00
|
|
|
await Promise.all((
|
|
|
|
await MessagingMessage.find({ userId: u._id })
|
|
|
|
).map(x => deleteMessagingMessage(x)));
|
2018-04-11 04:24:42 -05:00
|
|
|
|
2018-04-11 13:46:32 -05:00
|
|
|
// このユーザーへのMessageをすべて削除
|
2018-04-11 14:05:03 -05:00
|
|
|
await Promise.all((
|
|
|
|
await MessagingMessage.find({ recipientId: u._id })
|
|
|
|
).map(x => deleteMessagingMessage(x)));
|
|
|
|
|
|
|
|
// このユーザーの関わるMessagingHistoryをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await MessagingHistory.find({ $or: [{ partnerId: u._id }, { userId: u._id }] })
|
|
|
|
).map(x => deleteMessagingHistory(x)));
|
2018-04-11 04:24:42 -05:00
|
|
|
|
2018-04-11 13:46:32 -05:00
|
|
|
// このユーザーのDriveFileをすべて削除
|
2018-04-11 14:22:06 -05:00
|
|
|
await Promise.all((
|
|
|
|
await DriveFile.find({ 'metadata.userId': u._id })
|
|
|
|
).map(x => deleteDriveFile(x)));
|
|
|
|
|
|
|
|
// このユーザーのDriveFolderをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await DriveFolder.find({ userId: u._id })
|
|
|
|
).map(x => deleteDriveFolder(x)));
|
2018-04-11 04:24:42 -05:00
|
|
|
|
2018-04-11 15:50:45 -05:00
|
|
|
// このユーザーのMuteをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await Mute.find({ muterId: u._id })
|
|
|
|
).map(x => deleteMute(x)));
|
|
|
|
|
|
|
|
// このユーザーへのMuteをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await Mute.find({ muteeId: u._id })
|
|
|
|
).map(x => deleteMute(x)));
|
|
|
|
|
2018-04-11 13:46:32 -05:00
|
|
|
// このユーザーのFollowingをすべて削除
|
2018-04-11 17:13:15 -05:00
|
|
|
await Promise.all((
|
|
|
|
await Following.find({ followerId: u._id })
|
|
|
|
).map(x => deleteFollowing(x)));
|
2018-04-11 04:24:42 -05:00
|
|
|
|
2018-04-11 13:46:32 -05:00
|
|
|
// このユーザーへのFollowingをすべて削除
|
2018-04-11 17:13:15 -05:00
|
|
|
await Promise.all((
|
|
|
|
await Following.find({ followeeId: u._id })
|
|
|
|
).map(x => deleteFollowing(x)));
|
2018-04-11 04:24:42 -05:00
|
|
|
|
2018-04-11 15:50:45 -05:00
|
|
|
// このユーザーのFollowingLogをすべて削除
|
|
|
|
|
|
|
|
// このユーザーのFollowedLogをすべて削除
|
|
|
|
|
2018-04-11 04:24:42 -05:00
|
|
|
// このユーザーを削除
|
|
|
|
}
|
|
|
|
|
2018-02-01 17:06:01 -06:00
|
|
|
/**
|
|
|
|
* Pack a user for API response
|
|
|
|
*
|
|
|
|
* @param user target
|
|
|
|
* @param me? serializee
|
|
|
|
* @param options? serialize options
|
|
|
|
* @return Packed user
|
|
|
|
*/
|
|
|
|
export const pack = (
|
|
|
|
user: string | mongo.ObjectID | IUser,
|
|
|
|
me?: string | mongo.ObjectID | IUser,
|
|
|
|
options?: {
|
|
|
|
detail?: boolean,
|
|
|
|
includeSecrets?: boolean
|
|
|
|
}
|
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
|
|
|
|
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false,
|
|
|
|
includeSecrets: false
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
let _user: any;
|
|
|
|
|
|
|
|
const fields = opts.detail ? {
|
|
|
|
} : {
|
2018-04-07 13:58:11 -05:00
|
|
|
settings: false,
|
|
|
|
clientSettings: false,
|
|
|
|
profile: false,
|
|
|
|
keywords: false,
|
|
|
|
domains: false
|
2018-02-01 17:06:01 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Populate the user if 'user' is ID
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(user)) {
|
|
|
|
_user = await User.findOne({
|
|
|
|
_id: user
|
|
|
|
}, { fields });
|
|
|
|
} else if (typeof user === 'string') {
|
|
|
|
_user = await User.findOne({
|
|
|
|
_id: new mongo.ObjectID(user)
|
|
|
|
}, { fields });
|
|
|
|
} else {
|
|
|
|
_user = deepcopy(user);
|
|
|
|
}
|
|
|
|
|
2018-04-09 15:02:44 -05:00
|
|
|
// TODO: ここでエラーにするのではなくダミーのユーザーデータを返す
|
|
|
|
// SEE: https://github.com/syuilo/misskey/issues/1432
|
2018-02-01 17:06:01 -06:00
|
|
|
if (!_user) return reject('invalid user arg.');
|
|
|
|
|
|
|
|
// Me
|
|
|
|
const meId: mongo.ObjectID = me
|
|
|
|
? mongo.ObjectID.prototype.isPrototypeOf(me)
|
|
|
|
? me as mongo.ObjectID
|
|
|
|
: typeof me === 'string'
|
|
|
|
? new mongo.ObjectID(me)
|
|
|
|
: (me as IUser)._id
|
|
|
|
: null;
|
|
|
|
|
|
|
|
// Rename _id to id
|
|
|
|
_user.id = _user._id;
|
|
|
|
delete _user._id;
|
|
|
|
|
|
|
|
// Remove needless properties
|
2018-04-07 12:30:37 -05:00
|
|
|
delete _user.latestNote;
|
2018-02-01 17:06:01 -06:00
|
|
|
|
2018-04-07 13:58:11 -05:00
|
|
|
if (_user.host == null) {
|
2018-03-27 02:51:12 -05:00
|
|
|
// Remove private properties
|
2018-04-07 13:58:11 -05:00
|
|
|
delete _user.keypair;
|
|
|
|
delete _user.password;
|
|
|
|
delete _user.token;
|
|
|
|
delete _user.twoFactorTempSecret;
|
|
|
|
delete _user.twoFactorSecret;
|
2018-03-29 00:48:47 -05:00
|
|
|
delete _user.usernameLower;
|
2018-04-07 13:58:11 -05:00
|
|
|
if (_user.twitter) {
|
|
|
|
delete _user.twitter.accessToken;
|
|
|
|
delete _user.twitter.accessTokenSecret;
|
2018-03-27 02:51:12 -05:00
|
|
|
}
|
2018-04-07 13:58:11 -05:00
|
|
|
delete _user.line;
|
2018-03-27 02:51:12 -05:00
|
|
|
|
|
|
|
// Visible via only the official client
|
|
|
|
if (!opts.includeSecrets) {
|
2018-04-07 13:58:11 -05:00
|
|
|
delete _user.email;
|
|
|
|
delete _user.settings;
|
|
|
|
delete _user.clientSettings;
|
2018-03-27 02:51:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.detail) {
|
2018-04-07 13:58:11 -05:00
|
|
|
delete _user.twoFactorEnabled;
|
2018-03-27 02:51:12 -05:00
|
|
|
}
|
2018-02-01 17:06:01 -06:00
|
|
|
}
|
|
|
|
|
2018-03-29 00:48:47 -05:00
|
|
|
_user.avatarUrl = _user.avatarId != null
|
|
|
|
? `${config.drive_url}/${_user.avatarId}`
|
2018-02-01 17:06:01 -06:00
|
|
|
: `${config.drive_url}/default-avatar.jpg`;
|
|
|
|
|
2018-03-29 00:48:47 -05:00
|
|
|
_user.bannerUrl = _user.bannerId != null
|
|
|
|
? `${config.drive_url}/${_user.bannerId}`
|
2018-02-01 17:06:01 -06:00
|
|
|
: null;
|
|
|
|
|
|
|
|
if (!meId || !meId.equals(_user.id) || !opts.detail) {
|
2018-03-29 00:48:47 -05:00
|
|
|
delete _user.avatarId;
|
|
|
|
delete _user.bannerId;
|
2018-02-01 17:06:01 -06:00
|
|
|
|
2018-03-29 00:48:47 -05:00
|
|
|
delete _user.driveCapacity;
|
2018-02-01 17:06:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (meId && !meId.equals(_user.id)) {
|
|
|
|
// Whether the user is following
|
2018-03-29 00:48:47 -05:00
|
|
|
_user.isFollowing = (async () => {
|
2018-02-01 17:06:01 -06:00
|
|
|
const follow = await Following.findOne({
|
2018-03-29 00:48:47 -05:00
|
|
|
followerId: meId,
|
2018-04-02 07:57:36 -05:00
|
|
|
followeeId: _user.id
|
2018-02-01 17:06:01 -06:00
|
|
|
});
|
|
|
|
return follow !== null;
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Whether the user is followed
|
2018-03-29 00:48:47 -05:00
|
|
|
_user.isFollowed = (async () => {
|
2018-02-01 17:06:01 -06:00
|
|
|
const follow2 = await Following.findOne({
|
2018-03-29 00:48:47 -05:00
|
|
|
followerId: _user.id,
|
2018-04-02 07:57:36 -05:00
|
|
|
followeeId: meId
|
2018-02-01 17:06:01 -06:00
|
|
|
});
|
|
|
|
return follow2 !== null;
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Whether the user is muted
|
2018-03-29 00:48:47 -05:00
|
|
|
_user.isMuted = (async () => {
|
2018-02-01 17:06:01 -06:00
|
|
|
const mute = await Mute.findOne({
|
2018-03-29 00:48:47 -05:00
|
|
|
muterId: meId,
|
|
|
|
muteeId: _user.id,
|
|
|
|
deletedAt: { $exists: false }
|
2018-02-01 17:06:01 -06:00
|
|
|
});
|
|
|
|
return mute !== null;
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.detail) {
|
2018-04-07 12:30:37 -05:00
|
|
|
if (_user.pinnedNoteId) {
|
|
|
|
// Populate pinned note
|
|
|
|
_user.pinnedNote = packNote(_user.pinnedNoteId, meId, {
|
2018-02-01 17:06:01 -06:00
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (meId && !meId.equals(_user.id)) {
|
|
|
|
const myFollowingIds = await getFriends(meId);
|
|
|
|
|
|
|
|
// Get following you know count
|
2018-03-29 00:48:47 -05:00
|
|
|
_user.followingYouKnowCount = Following.count({
|
|
|
|
followeeId: { $in: myFollowingIds },
|
2018-04-02 07:57:36 -05:00
|
|
|
followerId: _user.id
|
2018-02-01 17:06:01 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// Get followers you know count
|
2018-03-29 00:48:47 -05:00
|
|
|
_user.followersYouKnowCount = Following.count({
|
|
|
|
followeeId: _user.id,
|
2018-04-02 07:57:36 -05:00
|
|
|
followerId: { $in: myFollowingIds }
|
2018-02-01 17:06:01 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve promises in _user object
|
|
|
|
_user = await rap(_user);
|
|
|
|
|
|
|
|
resolve(_user);
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
function img(url) {
|
|
|
|
return {
|
|
|
|
thumbnail: {
|
|
|
|
large: `${url}`,
|
|
|
|
medium: '',
|
|
|
|
small: ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
*/
|