2017-09-07 21:13:01 +02:00
|
|
|
import * as mongo from 'mongodb';
|
2018-06-18 02:54:53 +02:00
|
|
|
const deepcopy = require('deepcopy');
|
2018-02-02 00:06:01 +01:00
|
|
|
import rap from '@prezzemolo/rap';
|
2018-03-29 13:32:18 +02:00
|
|
|
import db from '../db/mongodb';
|
2018-10-16 04:38:09 +02:00
|
|
|
import isObjectId from '../misc/is-objectid';
|
2018-10-29 13:06:23 +01:00
|
|
|
import { packMany as packNoteMany } from './note';
|
|
|
|
import Following from './following';
|
|
|
|
import Blocking from './blocking';
|
|
|
|
import Mute from './mute';
|
2018-04-19 05:43:25 +02:00
|
|
|
import { getFriendIds } from '../server/api/common/get-friends';
|
2018-04-02 06:15:53 +02:00
|
|
|
import config from '../config';
|
2018-10-29 13:06:23 +01:00
|
|
|
import FollowRequest from './follow-request';
|
2017-01-17 01:12:33 +01:00
|
|
|
|
2018-02-02 00:06:01 +01:00
|
|
|
const User = db.get<IUser>('users');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-04-14 07:42:18 +02:00
|
|
|
User.createIndex('username');
|
|
|
|
User.createIndex('usernameLower');
|
2018-04-14 07:29:18 +02:00
|
|
|
User.createIndex(['username', 'host'], { unique: true });
|
|
|
|
User.createIndex(['usernameLower', 'host'], { unique: true });
|
2018-04-15 05:40:48 +02:00
|
|
|
User.createIndex('token', { sparse: true, unique: true });
|
2018-04-08 16:29:27 +02:00
|
|
|
User.createIndex('uri', { sparse: true, unique: true });
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-02-02 00:06:01 +01:00
|
|
|
export default User;
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-04-01 21:01:34 +02:00
|
|
|
type IUserBase = {
|
2017-09-07 21:13:01 +02:00
|
|
|
_id: mongo.ObjectID;
|
2018-03-29 07:48:47 +02:00
|
|
|
createdAt: Date;
|
2018-04-26 13:22:51 +02:00
|
|
|
deletedAt?: Date;
|
2018-03-29 07:48:47 +02:00
|
|
|
followersCount: number;
|
|
|
|
followingCount: number;
|
2018-04-05 18:36:34 +02:00
|
|
|
name?: string;
|
2018-04-07 19:30:37 +02:00
|
|
|
notesCount: number;
|
2017-09-07 21:13:01 +02:00
|
|
|
username: string;
|
2018-03-29 07:48:47 +02:00
|
|
|
usernameLower: string;
|
|
|
|
avatarId: mongo.ObjectID;
|
|
|
|
bannerId: mongo.ObjectID;
|
2018-06-10 01:41:57 +02:00
|
|
|
avatarUrl?: string;
|
|
|
|
bannerUrl?: string;
|
2018-06-06 22:14:37 +02:00
|
|
|
wallpaperId: mongo.ObjectID;
|
2018-07-28 00:52:48 +02:00
|
|
|
wallpaperUrl?: string;
|
2017-09-07 21:13:01 +02:00
|
|
|
data: any;
|
|
|
|
description: string;
|
2018-09-17 23:29:47 +02:00
|
|
|
pinnedNoteIds: mongo.ObjectID[];
|
2018-05-31 11:08:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 凍結されているか否か
|
|
|
|
*/
|
2018-03-29 07:48:47 +02:00
|
|
|
isSuspended: boolean;
|
2018-05-31 11:08:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 鍵アカウントか否か
|
|
|
|
*/
|
|
|
|
isLocked: boolean;
|
|
|
|
|
2018-10-13 13:11:00 +02:00
|
|
|
/**
|
|
|
|
* Botか否か
|
|
|
|
*/
|
|
|
|
isBot: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Botからのフォローを承認制にするか
|
|
|
|
*/
|
|
|
|
carefulBot: boolean;
|
|
|
|
|
2018-05-31 18:12:02 +02:00
|
|
|
/**
|
|
|
|
* このアカウントに届いているフォローリクエストの数
|
|
|
|
*/
|
|
|
|
pendingReceivedFollowRequestsCount: number;
|
|
|
|
|
2018-04-02 04:14:45 +02:00
|
|
|
host: string;
|
2017-09-07 21:13:01 +02:00
|
|
|
};
|
2017-10-07 00:23:00 +02:00
|
|
|
|
2018-04-01 21:48:38 +02:00
|
|
|
export interface ILocalUser extends IUserBase {
|
|
|
|
host: null;
|
2018-04-07 20:58:11 +02:00
|
|
|
keypair: string;
|
|
|
|
email: string;
|
|
|
|
password: string;
|
|
|
|
token: string;
|
|
|
|
twitter: {
|
|
|
|
accessToken: string;
|
|
|
|
accessTokenSecret: string;
|
|
|
|
userId: string;
|
|
|
|
screenName: string;
|
2018-04-01 21:48:38 +02:00
|
|
|
};
|
2018-04-07 20:58:11 +02:00
|
|
|
line: {
|
|
|
|
userId: string;
|
|
|
|
};
|
|
|
|
profile: {
|
|
|
|
location: string;
|
|
|
|
birthday: string; // 'YYYY-MM-DD'
|
|
|
|
tags: string[];
|
|
|
|
};
|
|
|
|
lastUsedAt: Date;
|
2018-05-21 04:08:08 +02:00
|
|
|
isCat: boolean;
|
2018-07-24 18:14:38 +02:00
|
|
|
isAdmin?: boolean;
|
|
|
|
isVerified?: boolean;
|
2018-04-07 20:58:11 +02:00
|
|
|
twoFactorSecret: string;
|
|
|
|
twoFactorEnabled: boolean;
|
2018-04-26 13:22:51 +02:00
|
|
|
twoFactorTempSecret?: string;
|
2018-04-07 20:58:11 +02:00
|
|
|
clientSettings: any;
|
2018-09-14 13:11:01 +02:00
|
|
|
settings: {
|
|
|
|
autoWatch: boolean;
|
|
|
|
alwaysMarkNsfw?: boolean;
|
|
|
|
};
|
2018-05-28 18:25:54 +02:00
|
|
|
hasUnreadNotification: boolean;
|
|
|
|
hasUnreadMessagingMessage: boolean;
|
2018-04-01 21:48:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface IRemoteUser extends IUserBase {
|
2018-04-07 20:58:11 +02:00
|
|
|
inbox: string;
|
2018-07-19 15:40:44 +02:00
|
|
|
sharedInbox?: string;
|
2018-10-02 09:27:36 +02:00
|
|
|
featured?: string;
|
2018-05-16 10:06:12 +02:00
|
|
|
endpoints: string[];
|
2018-04-07 20:58:11 +02:00
|
|
|
uri: string;
|
2018-04-15 11:38:40 +02:00
|
|
|
url?: string;
|
2018-04-07 20:58:11 +02:00
|
|
|
publicKey: {
|
|
|
|
id: string;
|
|
|
|
publicKeyPem: string;
|
2018-04-01 21:48:38 +02:00
|
|
|
};
|
2018-04-17 08:37:18 +02:00
|
|
|
updatedAt: Date;
|
2018-08-20 18:03:58 +02:00
|
|
|
isAdmin: false;
|
2018-04-01 21:48:38 +02:00
|
|
|
}
|
2018-04-01 21:01:34 +02:00
|
|
|
|
2018-04-01 21:52:11 +02:00
|
|
|
export type IUser = ILocalUser | IRemoteUser;
|
|
|
|
|
2018-04-01 21:01:34 +02: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 12:55:00 +02:00
|
|
|
|
2018-04-02 04:14:45 +02:00
|
|
|
//#region Validators
|
2018-04-01 21:52:11 +02:00
|
|
|
export function validateUsername(username: string): boolean {
|
2018-04-09 10:09:57 +02:00
|
|
|
return typeof username == 'string' && /^[a-zA-Z0-9_]{1,20}$/.test(username);
|
2018-04-01 21:52:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function validatePassword(password: string): boolean {
|
|
|
|
return typeof password == 'string' && password != '';
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:36:34 +02:00
|
|
|
export function isValidName(name?: string): boolean {
|
2018-05-16 10:04:39 +02:00
|
|
|
return name === null || (typeof name == 'string' && name.length < 50 && name.trim() != '');
|
2018-04-01 21:52:11 +02: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-02 04:14:45 +02:00
|
|
|
//#endregion
|
2018-04-01 21:52:11 +02:00
|
|
|
|
2018-02-02 00:06:01 +01: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,
|
2018-09-19 07:18:34 +02:00
|
|
|
includeSecrets?: boolean,
|
|
|
|
includeHasUnreadNotes?: boolean
|
2018-02-02 00:06:01 +01:00
|
|
|
}
|
|
|
|
) => 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 20:58:11 +02:00
|
|
|
settings: false,
|
|
|
|
clientSettings: false,
|
|
|
|
profile: false,
|
|
|
|
keywords: false,
|
|
|
|
domains: false
|
2018-02-02 00:06:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Populate the user if 'user' is ID
|
2018-10-16 04:38:09 +02:00
|
|
|
if (isObjectId(user)) {
|
2018-02-02 00:06:01 +01:00
|
|
|
_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-10-04 18:43:47 +02:00
|
|
|
// (データベースの欠損などで)ユーザーがデータベース上に見つからなかったとき
|
2018-10-03 17:39:11 +02:00
|
|
|
if (_user == null) {
|
|
|
|
console.warn(`user not found on database: ${user}`);
|
2018-10-04 21:50:23 +02:00
|
|
|
return resolve(null);
|
2018-10-03 17:39:11 +02:00
|
|
|
}
|
2018-02-02 00:06:01 +01:00
|
|
|
|
|
|
|
// Me
|
|
|
|
const meId: mongo.ObjectID = me
|
2018-10-16 04:38:09 +02:00
|
|
|
? isObjectId(me)
|
2018-02-02 00:06:01 +01:00
|
|
|
? 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;
|
|
|
|
|
2018-04-07 20:58:11 +02:00
|
|
|
if (_user.host == null) {
|
2018-03-27 09:51:12 +02:00
|
|
|
// Remove private properties
|
2018-04-07 20:58:11 +02:00
|
|
|
delete _user.keypair;
|
|
|
|
delete _user.password;
|
|
|
|
delete _user.token;
|
|
|
|
delete _user.twoFactorTempSecret;
|
|
|
|
delete _user.twoFactorSecret;
|
2018-03-29 07:48:47 +02:00
|
|
|
delete _user.usernameLower;
|
2018-04-07 20:58:11 +02:00
|
|
|
if (_user.twitter) {
|
|
|
|
delete _user.twitter.accessToken;
|
|
|
|
delete _user.twitter.accessTokenSecret;
|
2018-03-27 09:51:12 +02:00
|
|
|
}
|
2018-04-07 20:58:11 +02:00
|
|
|
delete _user.line;
|
2018-03-27 09:51:12 +02:00
|
|
|
|
|
|
|
// Visible via only the official client
|
|
|
|
if (!opts.includeSecrets) {
|
2018-04-07 20:58:11 +02:00
|
|
|
delete _user.email;
|
|
|
|
delete _user.settings;
|
|
|
|
delete _user.clientSettings;
|
2018-03-27 09:51:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.detail) {
|
2018-04-07 20:58:11 +02:00
|
|
|
delete _user.twoFactorEnabled;
|
2018-03-27 09:51:12 +02:00
|
|
|
}
|
2018-04-17 12:43:49 +02:00
|
|
|
} else {
|
|
|
|
delete _user.publicKey;
|
2018-02-02 00:06:01 +01:00
|
|
|
}
|
|
|
|
|
2018-06-10 01:34:46 +02:00
|
|
|
if (_user.avatarUrl == null) {
|
2018-07-28 00:52:48 +02:00
|
|
|
_user.avatarUrl = `${config.drive_url}/default-avatar.jpg`;
|
2018-02-02 00:06:01 +01:00
|
|
|
|
2018-07-28 00:52:48 +02:00
|
|
|
// 互換性のため
|
|
|
|
if (_user.avatarId) {
|
|
|
|
_user.avatarUrl = `${config.drive_url}/${_user.avatarId}`;
|
|
|
|
}
|
2018-06-10 01:34:46 +02:00
|
|
|
}
|
2018-02-02 00:06:01 +01:00
|
|
|
|
2018-07-28 00:52:48 +02:00
|
|
|
// 互換性のため
|
|
|
|
if (_user.bannerId && _user.bannerUrl == null) {
|
|
|
|
_user.bannerUrl = `${config.drive_url}/${_user.bannerId}`;
|
|
|
|
}
|
2018-06-06 22:14:37 +02:00
|
|
|
|
2018-02-02 00:06:01 +01:00
|
|
|
if (!meId || !meId.equals(_user.id) || !opts.detail) {
|
2018-03-29 07:48:47 +02:00
|
|
|
delete _user.avatarId;
|
|
|
|
delete _user.bannerId;
|
2018-05-28 18:25:54 +02:00
|
|
|
delete _user.hasUnreadMessagingMessage;
|
|
|
|
delete _user.hasUnreadNotification;
|
2018-02-02 00:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (meId && !meId.equals(_user.id)) {
|
2018-10-29 12:32:42 +01:00
|
|
|
const [following1, following2, followReq1, followReq2, toBlocking, fromBlocked, mute] = await Promise.all([
|
2018-04-19 05:43:25 +02:00
|
|
|
Following.findOne({
|
2018-03-29 07:48:47 +02:00
|
|
|
followerId: meId,
|
2018-04-02 14:57:36 +02:00
|
|
|
followeeId: _user.id
|
2018-04-19 05:43:25 +02:00
|
|
|
}),
|
|
|
|
Following.findOne({
|
2018-03-29 07:48:47 +02:00
|
|
|
followerId: _user.id,
|
2018-04-02 14:57:36 +02:00
|
|
|
followeeId: meId
|
2018-04-19 05:43:25 +02:00
|
|
|
}),
|
2018-09-04 11:33:16 +02:00
|
|
|
FollowRequest.findOne({
|
2018-05-31 17:42:37 +02:00
|
|
|
followerId: meId,
|
|
|
|
followeeId: _user.id
|
2018-09-04 11:33:16 +02:00
|
|
|
}),
|
2018-05-31 17:42:37 +02:00
|
|
|
FollowRequest.findOne({
|
|
|
|
followerId: _user.id,
|
|
|
|
followeeId: meId
|
|
|
|
}),
|
2018-10-29 12:32:42 +01:00
|
|
|
Blocking.findOne({
|
|
|
|
blockerId: meId,
|
|
|
|
blockeeId: _user.id
|
|
|
|
}),
|
|
|
|
Blocking.findOne({
|
|
|
|
blockerId: _user.id,
|
|
|
|
blockeeId: meId
|
|
|
|
}),
|
2018-04-19 05:43:25 +02:00
|
|
|
Mute.findOne({
|
|
|
|
muterId: meId,
|
|
|
|
muteeId: _user.id
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Whether the user is following
|
|
|
|
_user.isFollowing = following1 !== null;
|
|
|
|
_user.isStalking = following1 && following1.stalk;
|
|
|
|
|
2018-05-31 17:42:37 +02:00
|
|
|
_user.hasPendingFollowRequestFromYou = followReq1 !== null;
|
|
|
|
_user.hasPendingFollowRequestToYou = followReq2 !== null;
|
|
|
|
|
2018-04-19 05:43:25 +02:00
|
|
|
// Whether the user is followed
|
|
|
|
_user.isFollowed = following2 !== null;
|
2018-02-02 00:06:01 +01:00
|
|
|
|
2018-10-29 12:32:42 +01:00
|
|
|
// Whether the user is blocking
|
|
|
|
_user.isBlocking = toBlocking !== null;
|
|
|
|
|
|
|
|
// Whether the user is blocked
|
|
|
|
_user.isBlocked = fromBlocked !== null;
|
|
|
|
|
2018-02-02 00:06:01 +01:00
|
|
|
// Whether the user is muted
|
2018-04-19 05:43:25 +02:00
|
|
|
_user.isMuted = mute !== null;
|
2018-02-02 00:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.detail) {
|
2018-09-17 23:29:47 +02:00
|
|
|
if (_user.pinnedNoteIds) {
|
|
|
|
// Populate pinned notes
|
2018-10-03 17:39:11 +02:00
|
|
|
_user.pinnedNotes = packNoteMany(_user.pinnedNoteIds, meId, {
|
2018-02-02 00:06:01 +01:00
|
|
|
detail: true
|
2018-10-03 17:39:11 +02:00
|
|
|
});
|
2018-02-02 00:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (meId && !meId.equals(_user.id)) {
|
2018-04-19 05:43:25 +02:00
|
|
|
const myFollowingIds = await getFriendIds(meId);
|
2018-02-02 00:06:01 +01:00
|
|
|
|
|
|
|
// Get following you know count
|
2018-03-29 07:48:47 +02:00
|
|
|
_user.followingYouKnowCount = Following.count({
|
|
|
|
followeeId: { $in: myFollowingIds },
|
2018-04-02 14:57:36 +02:00
|
|
|
followerId: _user.id
|
2018-02-02 00:06:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Get followers you know count
|
2018-03-29 07:48:47 +02:00
|
|
|
_user.followersYouKnowCount = Following.count({
|
|
|
|
followeeId: _user.id,
|
2018-04-02 14:57:36 +02:00
|
|
|
followerId: { $in: myFollowingIds }
|
2018-02-02 00:06:01 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 07:18:34 +02:00
|
|
|
if (!opts.includeHasUnreadNotes) {
|
|
|
|
delete _user.hasUnreadSpecifiedNotes;
|
|
|
|
delete _user.hasUnreadMentions;
|
|
|
|
}
|
|
|
|
|
2018-02-02 00:06:01 +01:00
|
|
|
// resolve promises in _user object
|
|
|
|
_user = await rap(_user);
|
|
|
|
|
|
|
|
resolve(_user);
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
function img(url) {
|
|
|
|
return {
|
|
|
|
thumbnail: {
|
|
|
|
large: `${url}`,
|
|
|
|
medium: '',
|
|
|
|
small: ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
*/
|
2018-04-26 13:30:49 +02:00
|
|
|
|
|
|
|
export function getGhost(): Promise<ILocalUser> {
|
|
|
|
return User.findOne({ _id: new mongo.ObjectId(config.ghost) });
|
|
|
|
}
|