Sharkey/src/server/api/models/post.ts

222 lines
4.1 KiB
TypeScript
Raw Normal View History

2017-09-07 12:13:01 -07:00
import * as mongo from 'mongodb';
2018-02-01 15:06:01 -08:00
import deepcopy = require('deepcopy');
import rap from '@prezzemolo/rap';
2018-03-28 09:20:40 -07:00
import db from '../../../db/mongodb';
2018-02-01 15:06:01 -08:00
import { IUser, pack as packUser } from './user';
import { pack as packApp } from './app';
import { pack as packChannel } from './channel';
import Vote from './poll-vote';
import Reaction from './post-reaction';
import { pack as packFile } from './drive-file';
import parse from '../common/text';
const Post = db.get<IPost>('posts');
2017-01-16 16:12:33 -08:00
2018-02-01 15:06:01 -08:00
export default Post;
2017-03-01 10:16:39 -08:00
export function isValidText(text: string): boolean {
return text.length <= 1000 && text.trim() != '';
}
2017-09-07 12:13:01 -07:00
export type IPost = {
_id: mongo.ObjectID;
2018-03-28 22:48:47 -07:00
channelId: mongo.ObjectID;
createdAt: Date;
mediaIds: mongo.ObjectID[];
replyId: mongo.ObjectID;
repostId: mongo.ObjectID;
2018-02-03 21:52:33 -08:00
poll: any; // todo
2017-09-07 12:13:01 -07:00
text: string;
2018-03-28 22:48:47 -07:00
userId: mongo.ObjectID;
appId: mongo.ObjectID;
viaMobile: boolean;
repostCount: number;
repliesCount: number;
reactionCounts: any;
mentions: mongo.ObjectID[];
2018-03-04 15:44:37 -08:00
geo: {
latitude: number;
longitude: number;
altitude: number;
accuracy: number;
altitudeAccuracy: number;
heading: number;
speed: number;
};
2017-09-07 12:13:01 -07:00
};
2018-02-01 15:06:01 -08:00
/**
* Pack a post for API response
*
* @param post target
* @param me? serializee
* @param options? serialize options
* @return response
*/
export const pack = async (
post: string | mongo.ObjectID | IPost,
me?: string | mongo.ObjectID | IUser,
options?: {
detail: boolean
}
) => {
const opts = options || {
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
if (mongo.ObjectID.prototype.isPrototypeOf(post)) {
_post = await Post.findOne({
_id: post
});
} else if (typeof post === 'string') {
_post = await Post.findOne({
_id: new mongo.ObjectID(post)
});
} else {
_post = deepcopy(post);
}
if (!_post) throw 'invalid post arg.';
const id = _post._id;
// Rename _id to id
_post.id = _post._id;
delete _post._id;
delete _post.mentions;
// Parse text
if (_post.text) {
_post.ast = parse(_post.text);
}
// Populate user
2018-03-28 22:48:47 -07:00
_post.user = packUser(_post.userId, meId);
2018-02-01 15:06:01 -08:00
// Populate app
2018-03-28 22:48:47 -07:00
if (_post.appId) {
_post.app = packApp(_post.appId);
2018-02-01 15:06:01 -08:00
}
// Populate channel
2018-03-28 22:48:47 -07:00
if (_post.channelId) {
_post.channel = packChannel(_post.channelId);
2018-02-01 15:06:01 -08:00
}
// Populate media
2018-03-28 22:48:47 -07:00
if (_post.mediaIds) {
_post.media = Promise.all(_post.mediaIds.map(fileId =>
2018-02-01 15:06:01 -08:00
packFile(fileId)
));
}
// When requested a detailed post data
if (opts.detail) {
// Get previous post info
_post.prev = (async () => {
const prev = await Post.findOne({
2018-03-28 22:48:47 -07:00
userId: _post.userId,
2018-02-01 15:06:01 -08:00
_id: {
$lt: id
}
}, {
fields: {
_id: true
},
sort: {
_id: -1
}
});
return prev ? prev._id : null;
})();
// Get next post info
_post.next = (async () => {
const next = await Post.findOne({
2018-03-28 22:48:47 -07:00
userId: _post.userId,
2018-02-01 15:06:01 -08:00
_id: {
$gt: id
}
}, {
fields: {
_id: true
},
sort: {
_id: 1
}
});
return next ? next._id : null;
})();
2018-03-28 22:48:47 -07:00
if (_post.replyId) {
2018-02-01 15:06:01 -08:00
// Populate reply to post
2018-03-28 22:48:47 -07:00
_post.reply = pack(_post.replyId, meId, {
2018-02-01 15:06:01 -08:00
detail: false
});
}
2018-03-28 22:48:47 -07:00
if (_post.repostId) {
2018-02-01 15:06:01 -08:00
// Populate repost
2018-03-28 22:48:47 -07:00
_post.repost = pack(_post.repostId, meId, {
2018-02-01 15:06:01 -08:00
detail: _post.text == null
});
}
// Poll
if (meId && _post.poll) {
_post.poll = (async (poll) => {
const vote = await Vote
.findOne({
2018-03-28 22:48:47 -07:00
userId: meId,
postId: id
2018-02-01 15:06:01 -08:00
});
if (vote != null) {
const myChoice = poll.choices
.filter(c => c.id == vote.choice)[0];
2018-03-28 22:48:47 -07:00
myChoice.isVoted = true;
2018-02-01 15:06:01 -08:00
}
return poll;
})(_post.poll);
}
// Fetch my reaction
if (meId) {
2018-03-28 22:48:47 -07:00
_post.myReaction = (async () => {
2018-02-01 15:06:01 -08:00
const reaction = await Reaction
.findOne({
2018-03-28 22:48:47 -07:00
userId: meId,
postId: id,
deletedAt: { $exists: false }
2018-02-01 15:06:01 -08:00
});
if (reaction) {
return reaction.reaction;
}
return null;
})();
}
}
// resolve promises in _post object
_post = await rap(_post);
return _post;
};