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';
|
2017-01-17 09:12:33 +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';
|
|
|
|
import getFriends from '../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');
|
|
|
|
User.createIndex('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
|
|
|
|
2017-09-08 04:13:01 +09:00
|
|
|
export type IUser = {
|
|
|
|
_id: mongo.ObjectID;
|
|
|
|
created_at: Date;
|
2018-02-04 14:52:33 +09:00
|
|
|
deleted_at: Date;
|
2017-09-08 04:13:01 +09:00
|
|
|
email: string;
|
|
|
|
followers_count: number;
|
|
|
|
following_count: number;
|
|
|
|
links: string[];
|
2017-02-28 23:56:20 +09:00
|
|
|
name: string;
|
2017-09-08 04:13:01 +09:00
|
|
|
password: string;
|
|
|
|
posts_count: number;
|
|
|
|
drive_capacity: number;
|
|
|
|
username: string;
|
|
|
|
username_lower: string;
|
|
|
|
token: string;
|
|
|
|
avatar_id: mongo.ObjectID;
|
|
|
|
banner_id: mongo.ObjectID;
|
|
|
|
data: any;
|
|
|
|
twitter: {
|
|
|
|
access_token: string;
|
|
|
|
access_token_secret: string;
|
|
|
|
user_id: string;
|
|
|
|
screen_name: string;
|
|
|
|
};
|
2017-10-07 03:36:46 +09:00
|
|
|
line: {
|
|
|
|
user_id: string;
|
|
|
|
};
|
2017-09-08 04:13:01 +09:00
|
|
|
description: string;
|
|
|
|
profile: {
|
|
|
|
location: string;
|
|
|
|
birthday: string; // 'YYYY-MM-DD'
|
|
|
|
tags: string[];
|
|
|
|
};
|
|
|
|
last_used_at: Date;
|
|
|
|
latest_post: IPost;
|
|
|
|
pinned_post_id: mongo.ObjectID;
|
2018-03-02 06:26:31 +09:00
|
|
|
is_bot: boolean;
|
2017-09-08 04:13:01 +09:00
|
|
|
is_pro: boolean;
|
|
|
|
is_suspended: boolean;
|
|
|
|
keywords: string[];
|
2017-12-08 22:57:58 +09:00
|
|
|
two_factor_secret: string;
|
|
|
|
two_factor_enabled: boolean;
|
2018-03-05 08:07:09 +09:00
|
|
|
client_settings: any;
|
|
|
|
settings: any;
|
2017-09-08 04:13:01 +09:00
|
|
|
};
|
2017-10-07 07:23:00 +09:00
|
|
|
|
|
|
|
export function init(user): IUser {
|
|
|
|
user._id = new mongo.ObjectID(user._id);
|
|
|
|
user.avatar_id = new mongo.ObjectID(user.avatar_id);
|
|
|
|
user.banner_id = new mongo.ObjectID(user.banner_id);
|
|
|
|
user.pinned_post_id = new mongo.ObjectID(user.pinned_post_id);
|
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 ? {
|
|
|
|
} : {
|
|
|
|
settings: false,
|
|
|
|
client_settings: false,
|
|
|
|
profile: false,
|
|
|
|
keywords: false,
|
|
|
|
domains: false
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
|
delete _user.latest_post;
|
|
|
|
|
|
|
|
// Remove private properties
|
|
|
|
delete _user.password;
|
|
|
|
delete _user.token;
|
|
|
|
delete _user.two_factor_temp_secret;
|
|
|
|
delete _user.two_factor_secret;
|
|
|
|
delete _user.username_lower;
|
|
|
|
if (_user.twitter) {
|
|
|
|
delete _user.twitter.access_token;
|
|
|
|
delete _user.twitter.access_token_secret;
|
|
|
|
}
|
|
|
|
delete _user.line;
|
|
|
|
|
|
|
|
// Visible via only the official client
|
|
|
|
if (!opts.includeSecrets) {
|
|
|
|
delete _user.email;
|
2018-03-06 08:35:25 +09:00
|
|
|
delete _user.settings;
|
2018-02-02 08:06:01 +09:00
|
|
|
delete _user.client_settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.detail) {
|
|
|
|
delete _user.two_factor_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
_user.avatar_url = _user.avatar_id != null
|
|
|
|
? `${config.drive_url}/${_user.avatar_id}`
|
|
|
|
: `${config.drive_url}/default-avatar.jpg`;
|
|
|
|
|
|
|
|
_user.banner_url = _user.banner_id != null
|
|
|
|
? `${config.drive_url}/${_user.banner_id}`
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if (!meId || !meId.equals(_user.id) || !opts.detail) {
|
|
|
|
delete _user.avatar_id;
|
|
|
|
delete _user.banner_id;
|
|
|
|
|
|
|
|
delete _user.drive_capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (meId && !meId.equals(_user.id)) {
|
|
|
|
// Whether the user is following
|
|
|
|
_user.is_following = (async () => {
|
|
|
|
const follow = await Following.findOne({
|
|
|
|
follower_id: meId,
|
|
|
|
followee_id: _user.id,
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
});
|
|
|
|
return follow !== null;
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Whether the user is followed
|
|
|
|
_user.is_followed = (async () => {
|
|
|
|
const follow2 = await Following.findOne({
|
|
|
|
follower_id: _user.id,
|
|
|
|
followee_id: meId,
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
});
|
|
|
|
return follow2 !== null;
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Whether the user is muted
|
|
|
|
_user.is_muted = (async () => {
|
|
|
|
const mute = await Mute.findOne({
|
|
|
|
muter_id: meId,
|
|
|
|
mutee_id: _user.id,
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
});
|
|
|
|
return mute !== null;
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.detail) {
|
|
|
|
if (_user.pinned_post_id) {
|
|
|
|
// Populate pinned post
|
|
|
|
_user.pinned_post = packPost(_user.pinned_post_id, meId, {
|
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (meId && !meId.equals(_user.id)) {
|
|
|
|
const myFollowingIds = await getFriends(meId);
|
|
|
|
|
|
|
|
// Get following you know count
|
|
|
|
_user.following_you_know_count = Following.count({
|
|
|
|
followee_id: { $in: myFollowingIds },
|
|
|
|
follower_id: _user.id,
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get followers you know count
|
|
|
|
_user.followers_you_know_count = Following.count({
|
|
|
|
followee_id: _user.id,
|
|
|
|
follower_id: { $in: myFollowingIds },
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve promises in _user object
|
|
|
|
_user = await rap(_user);
|
|
|
|
|
|
|
|
resolve(_user);
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
function img(url) {
|
|
|
|
return {
|
|
|
|
thumbnail: {
|
|
|
|
large: `${url}`,
|
|
|
|
medium: '',
|
|
|
|
small: ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
*/
|