2017-09-08 04:13:01 +09:00
|
|
|
import * as mongo from 'mongodb';
|
2018-02-02 08:06:01 +09:00
|
|
|
import deepcopy = require('deepcopy');
|
|
|
|
import rap from '@prezzemolo/rap';
|
2018-03-29 20:32:18 +09:00
|
|
|
import db from '../db/mongodb';
|
2018-02-02 08:06:01 +09:00
|
|
|
import { IPost, pack as packPost } from './post';
|
|
|
|
import Following from './following';
|
|
|
|
import Mute from './mute';
|
2018-03-29 20:32:18 +09:00
|
|
|
import getFriends from '../server/api/common/get-friends';
|
|
|
|
import config from '../conf';
|
2017-01-17 09:12:33 +09:00
|
|
|
|
2018-02-02 08:06:01 +09:00
|
|
|
const User = db.get<IUser>('users');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-02-02 08:06:01 +09:00
|
|
|
User.createIndex('username');
|
2018-03-26 00:19:07 +09:00
|
|
|
User.createIndex('account.token');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-02-02 08:06:01 +09:00
|
|
|
export default User;
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
export function validateUsername(username: string): boolean {
|
2017-02-22 19:39:34 +09:00
|
|
|
return typeof username == 'string' && /^[a-zA-Z0-9\-]{3,20}$/.test(username);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validatePassword(password: string): boolean {
|
|
|
|
return typeof password == 'string' && password != '';
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
2017-01-06 15:35:07 +09:00
|
|
|
|
2017-03-01 20:35:54 +09:00
|
|
|
export function isValidName(name: string): boolean {
|
2017-03-01 20:38:27 +09:00
|
|
|
return typeof name == 'string' && name.length < 30 && name.trim() != '';
|
2017-03-01 20:35:54 +09:00
|
|
|
}
|
|
|
|
|
2017-03-03 09:24:17 +09: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() != '';
|
|
|
|
}
|
|
|
|
|
2017-01-06 15:35:07 +09:00
|
|
|
export function isValidBirthday(birthday: string): boolean {
|
2017-02-22 19:39:34 +09:00
|
|
|
return typeof birthday == 'string' && /^([0-9]{4})\-([0-9]{2})-([0-9]{2})$/.test(birthday);
|
|
|
|
}
|
2017-02-28 23:56:20 +09:00
|
|
|
|
2018-03-27 16:51:12 +09:00
|
|
|
export type ILocalAccount = {
|
|
|
|
keypair: string;
|
|
|
|
email: string;
|
|
|
|
links: string[];
|
|
|
|
password: string;
|
|
|
|
token: string;
|
|
|
|
twitter: {
|
2018-03-29 14:48:47 +09:00
|
|
|
accessToken: string;
|
|
|
|
accessTokenSecret: string;
|
|
|
|
userId: string;
|
|
|
|
screenName: string;
|
2018-03-27 16:51:12 +09:00
|
|
|
};
|
|
|
|
line: {
|
2018-03-29 14:48:47 +09:00
|
|
|
userId: string;
|
2018-03-27 16:51:12 +09:00
|
|
|
};
|
|
|
|
profile: {
|
|
|
|
location: string;
|
|
|
|
birthday: string; // 'YYYY-MM-DD'
|
|
|
|
tags: string[];
|
|
|
|
};
|
2018-03-29 14:48:47 +09:00
|
|
|
lastUsedAt: Date;
|
|
|
|
isBot: boolean;
|
|
|
|
isPro: boolean;
|
|
|
|
twoFactorSecret: string;
|
|
|
|
twoFactorEnabled: boolean;
|
|
|
|
twoFactorTempSecret: string;
|
|
|
|
clientSettings: any;
|
2018-03-27 16:51:12 +09:00
|
|
|
settings: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type IRemoteAccount = {
|
|
|
|
uri: string;
|
2018-04-01 15:58:49 +09:00
|
|
|
publicKey: {
|
|
|
|
id: string;
|
|
|
|
publicKeyPem: string;
|
|
|
|
};
|
2018-03-27 16:51:12 +09:00
|
|
|
};
|
|
|
|
|
2017-09-08 04:13:01 +09:00
|
|
|
export type IUser = {
|
|
|
|
_id: mongo.ObjectID;
|
2018-03-29 14:48:47 +09:00
|
|
|
createdAt: Date;
|
|
|
|
deletedAt: Date;
|
|
|
|
followersCount: number;
|
|
|
|
followingCount: number;
|
2017-02-28 23:56:20 +09:00
|
|
|
name: string;
|
2018-03-29 14:48:47 +09:00
|
|
|
postsCount: number;
|
|
|
|
driveCapacity: number;
|
2017-09-08 04:13:01 +09:00
|
|
|
username: string;
|
2018-03-29 14:48:47 +09:00
|
|
|
usernameLower: string;
|
|
|
|
avatarId: mongo.ObjectID;
|
|
|
|
bannerId: mongo.ObjectID;
|
2017-09-08 04:13:01 +09:00
|
|
|
data: any;
|
|
|
|
description: string;
|
2018-03-29 14:48:47 +09:00
|
|
|
latestPost: IPost;
|
|
|
|
pinnedPostId: mongo.ObjectID;
|
|
|
|
isSuspended: boolean;
|
2017-09-08 04:13:01 +09:00
|
|
|
keywords: string[];
|
2018-03-27 12:02:43 +09:00
|
|
|
host: string;
|
2018-03-29 14:48:47 +09:00
|
|
|
hostLower: string;
|
2018-03-27 16:51:12 +09:00
|
|
|
account: ILocalAccount | IRemoteAccount;
|
2017-09-08 04:13:01 +09:00
|
|
|
};
|
2017-10-07 07:23:00 +09:00
|
|
|
|
2018-03-31 19:55:00 +09:00
|
|
|
export type ILocalUser = IUser & { account: ILocalAccount };
|
|
|
|
export type IRemoteUser = IUser & { account: IRemoteAccount };
|
|
|
|
|
2017-10-07 07:23:00 +09:00
|
|
|
export function init(user): IUser {
|
|
|
|
user._id = new mongo.ObjectID(user._id);
|
2018-03-29 14:48:47 +09:00
|
|
|
user.avatarId = new mongo.ObjectID(user.avatarId);
|
|
|
|
user.bannerId = new mongo.ObjectID(user.bannerId);
|
|
|
|
user.pinnedPostId = new mongo.ObjectID(user.pinnedPostId);
|
2017-10-07 07:34:35 +09:00
|
|
|
return user;
|
2017-10-07 07:23:00 +09:00
|
|
|
}
|
2018-02-02 08:06:01 +09: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-03-26 00:19:07 +09:00
|
|
|
'account.settings': false,
|
2018-03-29 14:48:47 +09:00
|
|
|
'account.clientSettings': false,
|
2018-03-26 00:19:07 +09:00
|
|
|
'account.profile': false,
|
|
|
|
'account.keywords': false,
|
|
|
|
'account.domains': false
|
2018-02-02 08:06:01 +09: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-29 14:48:47 +09:00
|
|
|
delete _user.latestPost;
|
2018-02-02 08:06:01 +09:00
|
|
|
|
2018-03-27 16:51:12 +09:00
|
|
|
if (!_user.host) {
|
|
|
|
// Remove private properties
|
|
|
|
delete _user.account.keypair;
|
|
|
|
delete _user.account.password;
|
|
|
|
delete _user.account.token;
|
2018-03-29 14:48:47 +09:00
|
|
|
delete _user.account.twoFactorTempSecret;
|
|
|
|
delete _user.account.twoFactorSecret;
|
|
|
|
delete _user.usernameLower;
|
2018-03-27 16:51:12 +09:00
|
|
|
if (_user.account.twitter) {
|
2018-03-29 14:48:47 +09:00
|
|
|
delete _user.account.twitter.accessToken;
|
|
|
|
delete _user.account.twitter.accessTokenSecret;
|
2018-03-27 16:51:12 +09:00
|
|
|
}
|
|
|
|
delete _user.account.line;
|
|
|
|
|
|
|
|
// Visible via only the official client
|
|
|
|
if (!opts.includeSecrets) {
|
|
|
|
delete _user.account.email;
|
|
|
|
delete _user.account.settings;
|
2018-03-29 14:48:47 +09:00
|
|
|
delete _user.account.clientSettings;
|
2018-03-27 16:51:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.detail) {
|
2018-03-29 14:48:47 +09:00
|
|
|
delete _user.account.twoFactorEnabled;
|
2018-03-27 16:51:12 +09:00
|
|
|
}
|
2018-02-02 08:06:01 +09:00
|
|
|
}
|
|
|
|
|
2018-03-29 14:48:47 +09:00
|
|
|
_user.avatarUrl = _user.avatarId != null
|
|
|
|
? `${config.drive_url}/${_user.avatarId}`
|
2018-02-02 08:06:01 +09:00
|
|
|
: `${config.drive_url}/default-avatar.jpg`;
|
|
|
|
|
2018-03-29 14:48:47 +09:00
|
|
|
_user.bannerUrl = _user.bannerId != null
|
|
|
|
? `${config.drive_url}/${_user.bannerId}`
|
2018-02-02 08:06:01 +09:00
|
|
|
: null;
|
|
|
|
|
|
|
|
if (!meId || !meId.equals(_user.id) || !opts.detail) {
|
2018-03-29 14:48:47 +09:00
|
|
|
delete _user.avatarId;
|
|
|
|
delete _user.bannerId;
|
2018-02-02 08:06:01 +09:00
|
|
|
|
2018-03-29 14:48:47 +09:00
|
|
|
delete _user.driveCapacity;
|
2018-02-02 08:06:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (meId && !meId.equals(_user.id)) {
|
|
|
|
// Whether the user is following
|
2018-03-29 14:48:47 +09:00
|
|
|
_user.isFollowing = (async () => {
|
2018-02-02 08:06:01 +09:00
|
|
|
const follow = await Following.findOne({
|
2018-03-29 14:48:47 +09:00
|
|
|
followerId: meId,
|
|
|
|
followeeId: _user.id,
|
|
|
|
deletedAt: { $exists: false }
|
2018-02-02 08:06:01 +09:00
|
|
|
});
|
|
|
|
return follow !== null;
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Whether the user is followed
|
2018-03-29 14:48:47 +09:00
|
|
|
_user.isFollowed = (async () => {
|
2018-02-02 08:06:01 +09:00
|
|
|
const follow2 = await Following.findOne({
|
2018-03-29 14:48:47 +09:00
|
|
|
followerId: _user.id,
|
|
|
|
followeeId: meId,
|
|
|
|
deletedAt: { $exists: false }
|
2018-02-02 08:06:01 +09:00
|
|
|
});
|
|
|
|
return follow2 !== null;
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Whether the user is muted
|
2018-03-29 14:48:47 +09:00
|
|
|
_user.isMuted = (async () => {
|
2018-02-02 08:06:01 +09:00
|
|
|
const mute = await Mute.findOne({
|
2018-03-29 14:48:47 +09:00
|
|
|
muterId: meId,
|
|
|
|
muteeId: _user.id,
|
|
|
|
deletedAt: { $exists: false }
|
2018-02-02 08:06:01 +09:00
|
|
|
});
|
|
|
|
return mute !== null;
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.detail) {
|
2018-03-29 14:48:47 +09:00
|
|
|
if (_user.pinnedPostId) {
|
2018-02-02 08:06:01 +09:00
|
|
|
// Populate pinned post
|
2018-03-29 14:48:47 +09:00
|
|
|
_user.pinnedPost = packPost(_user.pinnedPostId, meId, {
|
2018-02-02 08:06:01 +09:00
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (meId && !meId.equals(_user.id)) {
|
|
|
|
const myFollowingIds = await getFriends(meId);
|
|
|
|
|
|
|
|
// Get following you know count
|
2018-03-29 14:48:47 +09:00
|
|
|
_user.followingYouKnowCount = Following.count({
|
|
|
|
followeeId: { $in: myFollowingIds },
|
|
|
|
followerId: _user.id,
|
|
|
|
deletedAt: { $exists: false }
|
2018-02-02 08:06:01 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
// Get followers you know count
|
2018-03-29 14:48:47 +09:00
|
|
|
_user.followersYouKnowCount = Following.count({
|
|
|
|
followeeId: _user.id,
|
|
|
|
followerId: { $in: myFollowingIds },
|
|
|
|
deletedAt: { $exists: false }
|
2018-02-02 08:06:01 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve promises in _user object
|
|
|
|
_user = await rap(_user);
|
|
|
|
|
|
|
|
resolve(_user);
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
function img(url) {
|
|
|
|
return {
|
|
|
|
thumbnail: {
|
|
|
|
large: `${url}`,
|
|
|
|
medium: '',
|
|
|
|
small: ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
*/
|