Post --> Note

Closes #1411
This commit is contained in:
syuilo 2018-04-08 02:30:37 +09:00
parent c7106d250c
commit a1b490afa7
167 changed files with 4440 additions and 1762 deletions

View file

@ -8,5 +8,5 @@ export type IFavorite = {
_id: mongo.ObjectID;
createdAt: Date;
userId: mongo.ObjectID;
postId: mongo.ObjectID;
noteId: mongo.ObjectID;
};

View file

@ -1,17 +1,17 @@
import * as mongo from 'mongodb';
import deepcopy = require('deepcopy');
import db from '../db/mongodb';
import Reaction from './post-reaction';
import Reaction from './note-reaction';
import { pack as packUser } from './user';
const PostReaction = db.get<IPostReaction>('postReactions');
PostReaction.createIndex(['userId', 'postId'], { unique: true });
export default PostReaction;
const NoteReaction = db.get<INoteReaction>('noteReactions');
NoteReaction.createIndex(['userId', 'noteId'], { unique: true });
export default NoteReaction;
export interface IPostReaction {
export interface INoteReaction {
_id: mongo.ObjectID;
createdAt: Date;
postId: mongo.ObjectID;
noteId: mongo.ObjectID;
userId: mongo.ObjectID;
reaction: string;
}

View file

@ -0,0 +1,13 @@
import * as mongo from 'mongodb';
import db from '../db/mongodb';
const NoteWatching = db.get<INoteWatching>('noteWatching');
NoteWatching.createIndex(['userId', 'noteId'], { unique: true });
export default NoteWatching;
export interface INoteWatching {
_id: mongo.ObjectID;
createdAt: Date;
userId: mongo.ObjectID;
noteId: mongo.ObjectID;
}

View file

@ -6,14 +6,14 @@ 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 Reaction from './note-reaction';
import { pack as packFile } from './drive-file';
const Post = db.get<IPost>('posts');
const Note = db.get<INote>('notes');
Post.createIndex('uri', { sparse: true, unique: true });
Note.createIndex('uri', { sparse: true, unique: true });
export default Post;
export default Note;
export function isValidText(text: string): boolean {
return text.length <= 1000 && text.trim() != '';
@ -23,14 +23,14 @@ export function isValidCw(text: string): boolean {
return text.length <= 100 && text.trim() != '';
}
export type IPost = {
export type INote = {
_id: mongo.ObjectID;
channelId: mongo.ObjectID;
createdAt: Date;
deletedAt: Date;
mediaIds: mongo.ObjectID[];
replyId: mongo.ObjectID;
repostId: mongo.ObjectID;
renoteId: mongo.ObjectID;
poll: any; // todo
text: string;
tags: string[];
@ -39,7 +39,7 @@ export type IPost = {
userId: mongo.ObjectID;
appId: mongo.ObjectID;
viaMobile: boolean;
repostCount: number;
renoteCount: number;
repliesCount: number;
reactionCounts: any;
mentions: mongo.ObjectID[];
@ -57,7 +57,7 @@ export type IPost = {
_reply?: {
userId: mongo.ObjectID;
};
_repost?: {
_renote?: {
userId: mongo.ObjectID;
};
_user: {
@ -70,15 +70,15 @@ export type IPost = {
};
/**
* Pack a post for API response
* Pack a note for API response
*
* @param post target
* @param note target
* @param me? serializee
* @param options? serialize options
* @return response
*/
export const pack = async (
post: string | mongo.ObjectID | IPost,
note: string | mongo.ObjectID | INote,
me?: string | mongo.ObjectID | IUser,
options?: {
detail: boolean
@ -97,58 +97,58 @@ export const pack = async (
: (me as IUser)._id
: null;
let _post: any;
let _note: any;
// Populate the post if 'post' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(post)) {
_post = await Post.findOne({
_id: post
// Populate the note if 'note' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(note)) {
_note = await Note.findOne({
_id: note
});
} else if (typeof post === 'string') {
_post = await Post.findOne({
_id: new mongo.ObjectID(post)
} else if (typeof note === 'string') {
_note = await Note.findOne({
_id: new mongo.ObjectID(note)
});
} else {
_post = deepcopy(post);
_note = deepcopy(note);
}
if (!_post) throw 'invalid post arg.';
if (!_note) throw 'invalid note arg.';
const id = _post._id;
const id = _note._id;
// Rename _id to id
_post.id = _post._id;
delete _post._id;
_note.id = _note._id;
delete _note._id;
delete _post.mentions;
if (_post.geo) delete _post.geo.type;
delete _note.mentions;
if (_note.geo) delete _note.geo.type;
// Populate user
_post.user = packUser(_post.userId, meId);
_note.user = packUser(_note.userId, meId);
// Populate app
if (_post.appId) {
_post.app = packApp(_post.appId);
if (_note.appId) {
_note.app = packApp(_note.appId);
}
// Populate channel
if (_post.channelId) {
_post.channel = packChannel(_post.channelId);
if (_note.channelId) {
_note.channel = packChannel(_note.channelId);
}
// Populate media
if (_post.mediaIds) {
_post.media = Promise.all(_post.mediaIds.map(fileId =>
if (_note.mediaIds) {
_note.media = Promise.all(_note.mediaIds.map(fileId =>
packFile(fileId)
));
}
// When requested a detailed post data
// When requested a detailed note data
if (opts.detail) {
// Get previous post info
_post.prev = (async () => {
const prev = await Post.findOne({
userId: _post.userId,
// Get previous note info
_note.prev = (async () => {
const prev = await Note.findOne({
userId: _note.userId,
_id: {
$lt: id
}
@ -163,10 +163,10 @@ export const pack = async (
return prev ? prev._id : null;
})();
// Get next post info
_post.next = (async () => {
const next = await Post.findOne({
userId: _post.userId,
// Get next note info
_note.next = (async () => {
const next = await Note.findOne({
userId: _note.userId,
_id: {
$gt: id
}
@ -181,27 +181,27 @@ export const pack = async (
return next ? next._id : null;
})();
if (_post.replyId) {
// Populate reply to post
_post.reply = pack(_post.replyId, meId, {
if (_note.replyId) {
// Populate reply to note
_note.reply = pack(_note.replyId, meId, {
detail: false
});
}
if (_post.repostId) {
// Populate repost
_post.repost = pack(_post.repostId, meId, {
detail: _post.text == null
if (_note.renoteId) {
// Populate renote
_note.renote = pack(_note.renoteId, meId, {
detail: _note.text == null
});
}
// Poll
if (meId && _post.poll) {
_post.poll = (async (poll) => {
if (meId && _note.poll) {
_note.poll = (async (poll) => {
const vote = await Vote
.findOne({
userId: meId,
postId: id
noteId: id
});
if (vote != null) {
@ -212,16 +212,16 @@ export const pack = async (
}
return poll;
})(_post.poll);
})(_note.poll);
}
// Fetch my reaction
if (meId) {
_post.myReaction = (async () => {
_note.myReaction = (async () => {
const reaction = await Reaction
.findOne({
userId: meId,
postId: id,
noteId: id,
deletedAt: { $exists: false }
});
@ -234,8 +234,8 @@ export const pack = async (
}
}
// resolve promises in _post object
_post = await rap(_post);
// resolve promises in _note object
_note = await rap(_note);
return _post;
return _note;
};

View file

@ -2,7 +2,7 @@ import * as mongo from 'mongodb';
import deepcopy = require('deepcopy');
import db from '../db/mongodb';
import { IUser, pack as packUser } from './user';
import { pack as packPost } from './post';
import { pack as packNote } from './note';
const Notification = db.get<INotification>('notifications');
export default Notification;
@ -36,12 +36,12 @@ export interface INotification {
* follow -
* mention - 稿
* reply - (Watchしている)稿
* repost - (Watchしている)稿Repostされた
* quote - (Watchしている)稿Repostされた
* renote - (Watchしている)稿Renoteされた
* quote - (Watchしている)稿Renoteされた
* reaction - (Watchしている)稿
* poll_vote - (Watchしている)稿
*/
type: 'follow' | 'mention' | 'reply' | 'repost' | 'quote' | 'reaction' | 'poll_vote';
type: 'follow' | 'mention' | 'reply' | 'renote' | 'quote' | 'reaction' | 'poll_vote';
/**
*
@ -91,12 +91,12 @@ export const pack = (notification: any) => new Promise<any>(async (resolve, reje
break;
case 'mention':
case 'reply':
case 'repost':
case 'renote':
case 'quote':
case 'reaction':
case 'poll_vote':
// Populate post
_notification.post = await packPost(_notification.postId, me);
// Populate note
_notification.note = await packNote(_notification.noteId, me);
break;
default:
console.error(`Unknown type: ${_notification.type}`);

View file

@ -8,6 +8,6 @@ export interface IPollVote {
_id: mongo.ObjectID;
createdAt: Date;
userId: mongo.ObjectID;
postId: mongo.ObjectID;
noteId: mongo.ObjectID;
choice: number;
}

View file

@ -1,13 +0,0 @@
import * as mongo from 'mongodb';
import db from '../db/mongodb';
const PostWatching = db.get<IPostWatching>('postWatching');
PostWatching.createIndex(['userId', 'postId'], { unique: true });
export default PostWatching;
export interface IPostWatching {
_id: mongo.ObjectID;
createdAt: Date;
userId: mongo.ObjectID;
postId: mongo.ObjectID;
}

View file

@ -2,7 +2,7 @@ import * as mongo from 'mongodb';
import deepcopy = require('deepcopy');
import rap from '@prezzemolo/rap';
import db from '../db/mongodb';
import { IPost, pack as packPost } from './post';
import { INote, pack as packNote } from './note';
import Following from './following';
import Mute from './mute';
import getFriends from '../server/api/common/get-friends';
@ -22,7 +22,7 @@ type IUserBase = {
followersCount: number;
followingCount: number;
name?: string;
postsCount: number;
notesCount: number;
driveCapacity: number;
username: string;
usernameLower: string;
@ -30,8 +30,8 @@ type IUserBase = {
bannerId: mongo.ObjectID;
data: any;
description: string;
latestPost: IPost;
pinnedPostId: mongo.ObjectID;
latestNote: INote;
pinnedNoteId: mongo.ObjectID;
isSuspended: boolean;
keywords: string[];
host: string;
@ -120,7 +120,7 @@ export function init(user): IUser {
user._id = new mongo.ObjectID(user._id);
user.avatarId = new mongo.ObjectID(user.avatarId);
user.bannerId = new mongo.ObjectID(user.bannerId);
user.pinnedPostId = new mongo.ObjectID(user.pinnedPostId);
user.pinnedNoteId = new mongo.ObjectID(user.pinnedNoteId);
return user;
}
@ -186,7 +186,7 @@ export const pack = (
delete _user._id;
// Remove needless properties
delete _user.latestPost;
delete _user.latestNote;
if (!_user.host) {
// Remove private properties
@ -260,9 +260,9 @@ export const pack = (
}
if (opts.detail) {
if (_user.pinnedPostId) {
// Populate pinned post
_user.pinnedPost = packPost(_user.pinnedPostId, meId, {
if (_user.pinnedNoteId) {
// Populate pinned note
_user.pinnedNote = packNote(_user.pinnedNoteId, meId, {
detail: true
});
}