mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-10 10:53:09 +02:00
Refactoring
This commit is contained in:
parent
3feeaccf59
commit
69ab594ac7
2 changed files with 45 additions and 35 deletions
|
@ -3,8 +3,9 @@
|
||||||
*/
|
*/
|
||||||
import * as mongo from 'mongodb';
|
import * as mongo from 'mongodb';
|
||||||
import deepcopy = require('deepcopy');
|
import deepcopy = require('deepcopy');
|
||||||
import Post from '../models/post';
|
import { default as Post, IPost } from '../models/post';
|
||||||
import Reaction from '../models/post-reaction';
|
import Reaction from '../models/post-reaction';
|
||||||
|
import { IUser } from '../models/user';
|
||||||
import Vote from '../models/poll-vote';
|
import Vote from '../models/poll-vote';
|
||||||
import serializeApp from './app';
|
import serializeApp from './app';
|
||||||
import serializeUser from './user';
|
import serializeUser from './user';
|
||||||
|
@ -14,14 +15,14 @@ import parse from '../common/text';
|
||||||
/**
|
/**
|
||||||
* Serialize a post
|
* Serialize a post
|
||||||
*
|
*
|
||||||
* @param {any} post
|
* @param post target
|
||||||
* @param {any} me?
|
* @param me? serializee
|
||||||
* @param {any} options?
|
* @param options? serialize options
|
||||||
* @return {Promise<any>}
|
* @return response
|
||||||
*/
|
*/
|
||||||
const self = (
|
const self = (
|
||||||
post: any,
|
post: string | mongo.ObjectID | IPost,
|
||||||
me?: any,
|
me?: string | mongo.ObjectID | IUser,
|
||||||
options?: {
|
options?: {
|
||||||
detail: boolean
|
detail: boolean
|
||||||
}
|
}
|
||||||
|
@ -30,6 +31,15 @@ const self = (
|
||||||
detail: true,
|
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;
|
let _post: any;
|
||||||
|
|
||||||
// Populate the post if 'post' is ID
|
// Populate the post if 'post' is ID
|
||||||
|
@ -59,7 +69,7 @@ const self = (
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate user
|
// Populate user
|
||||||
_post.user = await serializeUser(_post.user_id, me);
|
_post.user = await serializeUser(_post.user_id, meId);
|
||||||
|
|
||||||
// Populate app
|
// Populate app
|
||||||
if (_post.app_id) {
|
if (_post.app_id) {
|
||||||
|
@ -109,23 +119,23 @@ const self = (
|
||||||
|
|
||||||
if (_post.reply_to_id) {
|
if (_post.reply_to_id) {
|
||||||
// Populate reply to post
|
// 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
|
detail: false
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_post.repost_id) {
|
if (_post.repost_id) {
|
||||||
// Populate repost
|
// Populate repost
|
||||||
_post.repost = await self(_post.repost_id, me, {
|
_post.repost = await self(_post.repost_id, meId, {
|
||||||
detail: _post.text == null
|
detail: _post.text == null
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Poll
|
// Poll
|
||||||
if (me && _post.poll) {
|
if (meId && _post.poll) {
|
||||||
const vote = await Vote
|
const vote = await Vote
|
||||||
.findOne({
|
.findOne({
|
||||||
user_id: me._id,
|
user_id: meId,
|
||||||
post_id: id
|
post_id: id
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -135,10 +145,10 @@ const self = (
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch my reaction
|
// Fetch my reaction
|
||||||
if (me) {
|
if (meId) {
|
||||||
const reaction = await Reaction
|
const reaction = await Reaction
|
||||||
.findOne({
|
.findOne({
|
||||||
user_id: me._id,
|
user_id: meId,
|
||||||
post_id: id,
|
post_id: id,
|
||||||
deleted_at: { $exists: false }
|
deleted_at: { $exists: false }
|
||||||
});
|
});
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
*/
|
*/
|
||||||
import * as mongo from 'mongodb';
|
import * as mongo from 'mongodb';
|
||||||
import deepcopy = require('deepcopy');
|
import deepcopy = require('deepcopy');
|
||||||
import User from '../models/user';
|
import { default as User, IUser } from '../models/user';
|
||||||
import serializePost from './post';
|
import serializePost from './post';
|
||||||
import Following from '../models/following';
|
import Following from '../models/following';
|
||||||
import getFriends from '../common/get-friends';
|
import getFriends from '../common/get-friends';
|
||||||
|
@ -12,14 +12,14 @@ import config from '../../conf';
|
||||||
/**
|
/**
|
||||||
* Serialize a user
|
* Serialize a user
|
||||||
*
|
*
|
||||||
* @param {any} user
|
* @param user target
|
||||||
* @param {any} me?
|
* @param me? serializee
|
||||||
* @param {any} options?
|
* @param options? serialize options
|
||||||
* @return {Promise<any>}
|
* @return response
|
||||||
*/
|
*/
|
||||||
export default (
|
export default (
|
||||||
user: any,
|
user: string | mongo.ObjectID | IUser,
|
||||||
me?: any,
|
me?: string | mongo.ObjectID | IUser,
|
||||||
options?: {
|
options?: {
|
||||||
detail?: boolean,
|
detail?: boolean,
|
||||||
includeSecrets?: boolean
|
includeSecrets?: boolean
|
||||||
|
@ -54,13 +54,13 @@ export default (
|
||||||
}
|
}
|
||||||
|
|
||||||
// Me
|
// Me
|
||||||
if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) {
|
const meId: mongo.ObjectID = me
|
||||||
if (typeof me === 'string') {
|
? mongo.ObjectID.prototype.isPrototypeOf(me)
|
||||||
me = new mongo.ObjectID(me);
|
? me as mongo.ObjectID
|
||||||
} else {
|
: typeof me === 'string'
|
||||||
me = me._id;
|
? new mongo.ObjectID(me)
|
||||||
}
|
: (me as IUser)._id
|
||||||
}
|
: null;
|
||||||
|
|
||||||
// Rename _id to id
|
// Rename _id to id
|
||||||
_user.id = _user._id;
|
_user.id = _user._id;
|
||||||
|
@ -92,17 +92,17 @@ export default (
|
||||||
? `${config.drive_url}/${_user.banner_id}`
|
? `${config.drive_url}/${_user.banner_id}`
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
if (!me || !me.equals(_user.id) || !opts.detail) {
|
if (!meId || !meId.equals(_user.id) || !opts.detail) {
|
||||||
delete _user.avatar_id;
|
delete _user.avatar_id;
|
||||||
delete _user.banner_id;
|
delete _user.banner_id;
|
||||||
|
|
||||||
delete _user.drive_capacity;
|
delete _user.drive_capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (me && !me.equals(_user.id)) {
|
if (meId && !meId.equals(_user.id)) {
|
||||||
// If the user is following
|
// If the user is following
|
||||||
const follow = await Following.findOne({
|
const follow = await Following.findOne({
|
||||||
follower_id: me,
|
follower_id: meId,
|
||||||
followee_id: _user.id,
|
followee_id: _user.id,
|
||||||
deleted_at: { $exists: false }
|
deleted_at: { $exists: false }
|
||||||
});
|
});
|
||||||
|
@ -111,7 +111,7 @@ export default (
|
||||||
// If the user is followed
|
// If the user is followed
|
||||||
const follow2 = await Following.findOne({
|
const follow2 = await Following.findOne({
|
||||||
follower_id: _user.id,
|
follower_id: _user.id,
|
||||||
followee_id: me,
|
followee_id: meId,
|
||||||
deleted_at: { $exists: false }
|
deleted_at: { $exists: false }
|
||||||
});
|
});
|
||||||
_user.is_followed = follow2 !== null;
|
_user.is_followed = follow2 !== null;
|
||||||
|
@ -119,13 +119,13 @@ export default (
|
||||||
|
|
||||||
if (opts.detail) {
|
if (opts.detail) {
|
||||||
if (_user.pinned_post_id) {
|
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
|
detail: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (me && !me.equals(_user.id)) {
|
if (meId && !meId.equals(_user.id)) {
|
||||||
const myFollowingIds = await getFriends(me);
|
const myFollowingIds = await getFriends(meId);
|
||||||
|
|
||||||
// Get following you know count
|
// Get following you know count
|
||||||
const followingYouKnowCount = await Following.count({
|
const followingYouKnowCount = await Following.count({
|
||||||
|
|
Loading…
Reference in a new issue