summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/api')
-rw-r--r--src/api/endpoints/posts/create.ts4
-rw-r--r--src/api/models/post.ts14
-rw-r--r--src/api/models/user.ts40
-rw-r--r--src/api/serializers/post.ts38
-rw-r--r--src/api/serializers/user.ts42
5 files changed, 99 insertions, 39 deletions
diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts
index eb979402c4..805dba7f83 100644
--- a/src/api/endpoints/posts/create.ts
+++ b/src/api/endpoints/posts/create.ts
@@ -6,7 +6,7 @@ import deepEqual = require('deep-equal');
import parse from '../../common/text';
import Post from '../../models/post';
import { isValidText } from '../../models/post';
-import User from '../../models/user';
+import { default as User, IUser } from '../../models/user';
import Following from '../../models/following';
import DriveFile from '../../models/drive-file';
import Watching from '../../models/post-watching';
@@ -24,7 +24,7 @@ import config from '../../../conf';
* @param {any} app
* @return {Promise<any>}
*/
-module.exports = (params, user, app) => new Promise(async (res, rej) => {
+module.exports = (params, user: IUser, app) => new Promise(async (res, rej) => {
// Get 'text' parameter
const [text, textErr] = $(params.text).optional.string().pipe(isValidText).$;
if (textErr) return rej('invalid text');
diff --git a/src/api/models/post.ts b/src/api/models/post.ts
index baab63f991..8b9f7f5ef6 100644
--- a/src/api/models/post.ts
+++ b/src/api/models/post.ts
@@ -1,3 +1,5 @@
+import * as mongo from 'mongodb';
+
import db from '../../db/mongodb';
export default db.get('posts') as any; // fuck type definition
@@ -5,3 +7,15 @@ export default db.get('posts') as any; // fuck type definition
export function isValidText(text: string): boolean {
return text.length <= 1000 && text.trim() != '';
}
+
+export type IPost = {
+ _id: mongo.ObjectID;
+ created_at: Date;
+ media_ids: mongo.ObjectID[];
+ reply_to_id: mongo.ObjectID;
+ repost_id: mongo.ObjectID;
+ poll: {}; // todo
+ text: string;
+ user_id: mongo.ObjectID;
+ app_id: mongo.ObjectID;
+};
diff --git a/src/api/models/user.ts b/src/api/models/user.ts
index 9f8cf0161d..1591b339bc 100644
--- a/src/api/models/user.ts
+++ b/src/api/models/user.ts
@@ -1,4 +1,7 @@
+import * as mongo from 'mongodb';
+
import db from '../../db/mongodb';
+import { IPost } from './post';
const collection = db.get('users');
@@ -31,6 +34,39 @@ export function isValidBirthday(birthday: string): boolean {
return typeof birthday == 'string' && /^([0-9]{4})\-([0-9]{2})-([0-9]{2})$/.test(birthday);
}
-export interface IUser {
+export type IUser = {
+ _id: mongo.ObjectID;
+ created_at: Date;
+ email: string;
+ followers_count: number;
+ following_count: number;
+ links: string[];
name: string;
-}
+ 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;
+ };
+ 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[];
+};
diff --git a/src/api/serializers/post.ts b/src/api/serializers/post.ts
index 13773bda9e..86016d6696 100644
--- a/src/api/serializers/post.ts
+++ b/src/api/serializers/post.ts
@@ -3,8 +3,9 @@
*/
import * as mongo from 'mongodb';
import deepcopy = require('deepcopy');
-import Post from '../models/post';
+import { default as Post, IPost } from '../models/post';
import Reaction from '../models/post-reaction';
+import { IUser } from '../models/user';
import Vote from '../models/poll-vote';
import serializeApp from './app';
import serializeUser from './user';
@@ -14,14 +15,14 @@ import parse from '../common/text';
/**
* Serialize a post
*
- * @param {any} post
- * @param {any} me?
- * @param {any} options?
- * @return {Promise<any>}
+ * @param post target
+ * @param me? serializee
+ * @param options? serialize options
+ * @return response
*/
const self = (
- post: any,
- me?: any,
+ post: string | mongo.ObjectID | IPost,
+ me?: string | mongo.ObjectID | IUser,
options?: {
detail: boolean
}
@@ -30,6 +31,15 @@ const self = (
detail: true,
};
+ // 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;
+
let _post: any;
// Populate the post if 'post' is ID
@@ -59,7 +69,7 @@ const self = (
}
// Populate user
- _post.user = await serializeUser(_post.user_id, me);
+ _post.user = await serializeUser(_post.user_id, meId);
// Populate app
if (_post.app_id) {
@@ -109,23 +119,23 @@ const self = (
if (_post.reply_to_id) {
// Populate reply to post
- _post.reply_to = await self(_post.reply_to_id, me, {
+ _post.reply_to = await self(_post.reply_to_id, meId, {
detail: false
});
}
if (_post.repost_id) {
// Populate repost
- _post.repost = await self(_post.repost_id, me, {
+ _post.repost = await self(_post.repost_id, meId, {
detail: _post.text == null
});
}
// Poll
- if (me && _post.poll) {
+ if (meId && _post.poll) {
const vote = await Vote
.findOne({
- user_id: me._id,
+ user_id: meId,
post_id: id
});
@@ -135,10 +145,10 @@ const self = (
}
// Fetch my reaction
- if (me) {
+ if (meId) {
const reaction = await Reaction
.findOne({
- user_id: me._id,
+ user_id: meId,
post_id: id,
deleted_at: { $exists: false }
});
diff --git a/src/api/serializers/user.ts b/src/api/serializers/user.ts
index c9189d9034..57599fe85c 100644
--- a/src/api/serializers/user.ts
+++ b/src/api/serializers/user.ts
@@ -3,7 +3,7 @@
*/
import * as mongo from 'mongodb';
import deepcopy = require('deepcopy');
-import User from '../models/user';
+import { default as User, IUser } from '../models/user';
import serializePost from './post';
import Following from '../models/following';
import getFriends from '../common/get-friends';
@@ -12,14 +12,14 @@ import config from '../../conf';
/**
* Serialize a user
*
- * @param {any} user
- * @param {any} me?
- * @param {any} options?
- * @return {Promise<any>}
+ * @param user target
+ * @param me? serializee
+ * @param options? serialize options
+ * @return response
*/
export default (
- user: any,
- me?: any,
+ user: string | mongo.ObjectID | IUser,
+ me?: string | mongo.ObjectID | IUser,
options?: {
detail?: boolean,
includeSecrets?: boolean
@@ -54,13 +54,13 @@ export default (
}
// Me
- if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) {
- if (typeof me === 'string') {
- me = new mongo.ObjectID(me);
- } else {
- me = me._id;
- }
- }
+ 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;
@@ -92,17 +92,17 @@ export default (
? `${config.drive_url}/${_user.banner_id}`
: null;
- if (!me || !me.equals(_user.id) || !opts.detail) {
+ if (!meId || !meId.equals(_user.id) || !opts.detail) {
delete _user.avatar_id;
delete _user.banner_id;
delete _user.drive_capacity;
}
- if (me && !me.equals(_user.id)) {
+ if (meId && !meId.equals(_user.id)) {
// If the user is following
const follow = await Following.findOne({
- follower_id: me,
+ follower_id: meId,
followee_id: _user.id,
deleted_at: { $exists: false }
});
@@ -111,7 +111,7 @@ export default (
// If the user is followed
const follow2 = await Following.findOne({
follower_id: _user.id,
- followee_id: me,
+ followee_id: meId,
deleted_at: { $exists: false }
});
_user.is_followed = follow2 !== null;
@@ -119,13 +119,13 @@ export default (
if (opts.detail) {
if (_user.pinned_post_id) {
- _user.pinned_post = await serializePost(_user.pinned_post_id, me, {
+ _user.pinned_post = await serializePost(_user.pinned_post_id, meId, {
detail: true
});
}
- if (me && !me.equals(_user.id)) {
- const myFollowingIds = await getFriends(me);
+ if (meId && !meId.equals(_user.id)) {
+ const myFollowingIds = await getFriends(meId);
// Get following you know count
const followingYouKnowCount = await Following.count({