barkey/src/api/models/user.ts

86 lines
2.2 KiB
TypeScript
Raw Normal View History

2017-09-07 19:13:01 +00:00
import * as mongo from 'mongodb';
2017-01-17 00:12:33 +00:00
import db from '../../db/mongodb';
2017-09-07 19:13:01 +00:00
import { IPost } from './post';
2017-01-17 00:12:33 +00:00
2017-01-17 01:39:21 +00:00
const collection = db.get('users');
2016-12-28 22:49:51 +00:00
(collection as any).createIndex('username'); // fuck type definition
(collection as any).createIndex('token'); // fuck type definition
2016-12-28 22:49:51 +00:00
2017-01-18 07:30:42 +00:00
export default collection as any; // fuck type definition
2016-12-28 22:49:51 +00:00
export function validateUsername(username: string): boolean {
2017-02-22 10:39:34 +00: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-28 22:49:51 +00:00
}
2017-01-06 06:35:07 +00:00
2017-03-01 11:35:54 +00:00
export function isValidName(name: string): boolean {
return typeof name == 'string' && name.length < 30 && name.trim() != '';
2017-03-01 11:35:54 +00:00
}
2017-03-03 00:24:17 +00: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 06:35:07 +00:00
export function isValidBirthday(birthday: string): boolean {
2017-02-22 10:39:34 +00:00
return typeof birthday == 'string' && /^([0-9]{4})\-([0-9]{2})-([0-9]{2})$/.test(birthday);
}
2017-02-28 14:56:20 +00:00
2017-09-07 19:13:01 +00:00
export type IUser = {
_id: mongo.ObjectID;
created_at: Date;
email: string;
followers_count: number;
following_count: number;
links: string[];
2017-02-28 14:56:20 +00:00
name: string;
2017-09-07 19:13:01 +00: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-06 18:36:46 +00:00
line: {
user_id: string;
};
2017-09-07 19:13:01 +00: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;
is_pro: boolean;
is_suspended: boolean;
keywords: string[];
2017-12-08 13:57:58 +00:00
two_factor_secret: string;
two_factor_enabled: boolean;
2017-09-07 19:13:01 +00:00
};
2017-10-06 22:23:00 +00: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-06 22:34:35 +00:00
return user;
2017-10-06 22:23:00 +00:00
}