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

@@ -1,4 +1,4 @@
import Post, { pack, IPost } from '../../models/post';
import Note, { pack, INote } from '../../models/note';
import User, { isLocalUser, IUser, isRemoteUser } from '../../models/user';
import stream from '../../publishers/stream';
import Following from '../../models/following';
@@ -8,7 +8,7 @@ import renderCreate from '../../remote/activitypub/renderer/create';
import context from '../../remote/activitypub/renderer/context';
import { IDriveFile } from '../../models/drive-file';
import notify from '../../publishers/notify';
import PostWatching from '../../models/post-watching';
import NoteWatching from '../../models/note-watching';
import watch from './watch';
import Mute from '../../models/mute';
import pushSw from '../../publishers/push-sw';
@@ -20,8 +20,8 @@ import { IApp } from '../../models/app';
export default async (user: IUser, data: {
createdAt?: Date;
text?: string;
reply?: IPost;
repost?: IPost;
reply?: INote;
renote?: INote;
media?: IDriveFile[];
geo?: any;
poll?: any;
@@ -31,7 +31,7 @@ export default async (user: IUser, data: {
visibility?: string;
uri?: string;
app?: IApp;
}, silent = false) => new Promise<IPost>(async (res, rej) => {
}, silent = false) => new Promise<INote>(async (res, rej) => {
if (data.createdAt == null) data.createdAt = new Date();
if (data.visibility == null) data.visibility = 'public';
@@ -59,7 +59,7 @@ export default async (user: IUser, data: {
createdAt: data.createdAt,
mediaIds: data.media ? data.media.map(file => file._id) : [],
replyId: data.reply ? data.reply._id : null,
repostId: data.repost ? data.repost._id : null,
renoteId: data.renote ? data.renote._id : null,
text: data.text,
textHtml: tokens === null ? null : html(tokens),
poll: data.poll,
@@ -73,7 +73,7 @@ export default async (user: IUser, data: {
// 以下非正規化データ
_reply: data.reply ? { userId: data.reply.userId } : null,
_repost: data.repost ? { userId: data.repost.userId } : null,
_renote: data.renote ? { userId: data.renote.userId } : null,
_user: {
host: user.host,
hostLower: user.hostLower,
@@ -86,29 +86,29 @@ export default async (user: IUser, data: {
if (data.uri != null) insert.uri = data.uri;
// 投稿を作成
const post = await Post.insert(insert);
const note = await Note.insert(insert);
res(post);
res(note);
User.update({ _id: user._id }, {
// Increment posts count
// Increment notes count
$inc: {
postsCount: 1
notesCount: 1
},
// Update latest post
// Update latest note
$set: {
latestPost: post
latestNote: note
}
});
// Serialize
const postObj = await pack(post);
const noteObj = await pack(note);
// タイムラインへの投稿
if (post.channelId == null) {
if (note.channelId == null) {
// Publish event to myself's stream
if (isLocalUser(user)) {
stream(post.userId, 'post', postObj);
stream(note.userId, 'note', noteObj);
}
// Fetch all followers
@@ -121,15 +121,14 @@ export default async (user: IUser, data: {
}
}, {
$match: {
followeeId: post.userId
followeeId: note.userId
}
}], {
_id: false
});
if (!silent) {
const note = await renderNote(user, post);
const content = renderCreate(note);
const content = renderCreate(await renderNote(user, note));
content['@context'] = context;
// 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送
@@ -142,7 +141,7 @@ export default async (user: IUser, data: {
if (isLocalUser(follower)) {
// Publish event to followers stream
stream(follower._id, 'post', postObj);
stream(follower._id, 'note', noteObj);
} else {
// フォロワーがリモートユーザーかつ投稿者がローカルユーザーなら投稿を配信
if (isLocalUser(user)) {
@@ -155,33 +154,33 @@ export default async (user: IUser, data: {
// チャンネルへの投稿
/* TODO
if (post.channelId) {
if (note.channelId) {
promises.push(
// Increment channel index(posts count)
Channel.update({ _id: post.channelId }, {
// Increment channel index(notes count)
Channel.update({ _id: note.channelId }, {
$inc: {
index: 1
}
}),
// Publish event to channel
promisedPostObj.then(postObj => {
publishChannelStream(post.channelId, 'post', postObj);
promisedNoteObj.then(noteObj => {
publishChannelStream(note.channelId, 'note', noteObj);
}),
Promise.all([
promisedPostObj,
promisedNoteObj,
// Get channel watchers
ChannelWatching.find({
channelId: post.channelId,
channelId: note.channelId,
// 削除されたドキュメントは除く
deletedAt: { $exists: false }
})
]).then(([postObj, watches]) => {
]).then(([noteObj, watches]) => {
// チャンネルの視聴者(のタイムライン)に配信
watches.forEach(w => {
stream(w.userId, 'post', postObj);
stream(w.userId, 'note', noteObj);
});
})
);
@@ -204,16 +203,16 @@ export default async (user: IUser, data: {
});
const mentioneesMutedUserIds = mentioneeMutes.map(m => m.muteeId.toString());
if (mentioneesMutedUserIds.indexOf(user._id.toString()) == -1) {
event(mentionee, reason, postObj);
pushSw(mentionee, reason, postObj);
event(mentionee, reason, noteObj);
pushSw(mentionee, reason, noteObj);
}
}
}
// If has in reply to post
// If has in reply to note
if (data.reply) {
// Increment replies count
Post.update({ _id: data.reply._id }, {
Note.update({ _id: data.reply._id }, {
$inc: {
repliesCount: 1
}
@@ -221,12 +220,12 @@ export default async (user: IUser, data: {
// (自分自身へのリプライでない限りは)通知を作成
notify(data.reply.userId, user._id, 'reply', {
postId: post._id
noteId: note._id
});
// Fetch watchers
PostWatching.find({
postId: data.reply._id,
NoteWatching.find({
noteId: data.reply._id,
userId: { $ne: user._id },
// 削除されたドキュメントは除く
deletedAt: { $exists: false }
@@ -237,7 +236,7 @@ export default async (user: IUser, data: {
}).then(watchers => {
watchers.forEach(watcher => {
notify(watcher.userId, user._id, 'reply', {
postId: post._id
noteId: note._id
});
});
});
@@ -251,17 +250,17 @@ export default async (user: IUser, data: {
addMention(data.reply.userId, 'reply');
}
// If it is repost
if (data.repost) {
// If it is renote
if (data.renote) {
// Notify
const type = data.text ? 'quote' : 'repost';
notify(data.repost.userId, user._id, type, {
post_id: post._id
const type = data.text ? 'quote' : 'renote';
notify(data.renote.userId, user._id, type, {
note_id: note._id
});
// Fetch watchers
PostWatching.find({
postId: data.repost._id,
NoteWatching.find({
noteId: data.renote._id,
userId: { $ne: user._id },
// 削除されたドキュメントは除く
deletedAt: { $exists: false }
@@ -272,41 +271,41 @@ export default async (user: IUser, data: {
}).then(watchers => {
watchers.forEach(watcher => {
notify(watcher.userId, user._id, type, {
postId: post._id
noteId: note._id
});
});
});
// この投稿をWatchする
if (isLocalUser(user) && user.account.settings.autoWatch !== false) {
watch(user._id, data.repost);
watch(user._id, data.renote);
}
// If it is quote repost
// If it is quote renote
if (data.text) {
// Add mention
addMention(data.repost.userId, 'quote');
addMention(data.renote.userId, 'quote');
} else {
// Publish event
if (!user._id.equals(data.repost.userId)) {
event(data.repost.userId, 'repost', postObj);
if (!user._id.equals(data.renote.userId)) {
event(data.renote.userId, 'renote', noteObj);
}
}
// 今までで同じ投稿をRepostしているか
const existRepost = await Post.findOne({
// 今までで同じ投稿をRenoteしているか
const existRenote = await Note.findOne({
userId: user._id,
repostId: data.repost._id,
renoteId: data.renote._id,
_id: {
$ne: post._id
$ne: note._id
}
});
if (!existRepost) {
// Update repostee status
Post.update({ _id: data.repost._id }, {
if (!existRenote) {
// Update renoteee status
Note.update({ _id: data.renote._id }, {
$inc: {
repostCount: 1
renoteCount: 1
}
});
}
@@ -333,23 +332,23 @@ export default async (user: IUser, data: {
// When mentioned user not found
if (mentionee == null) return;
// 既に言及されたユーザーに対する返信や引用repostの場合も無視
// 既に言及されたユーザーに対する返信や引用renoteの場合も無視
if (data.reply && data.reply.userId.equals(mentionee._id)) return;
if (data.repost && data.repost.userId.equals(mentionee._id)) return;
if (data.renote && data.renote.userId.equals(mentionee._id)) return;
// Add mention
addMention(mentionee._id, 'mention');
// Create notification
notify(mentionee._id, user._id, 'mention', {
post_id: post._id
note_id: note._id
});
}));
}
// Append mentions data
if (mentions.length > 0) {
Post.update({ _id: post._id }, {
Note.update({ _id: note._id }, {
$set: {
mentions
}

View File

@@ -1,24 +1,24 @@
import { IUser, pack as packUser, isLocalUser, isRemoteUser } from '../../../models/user';
import Post, { IPost, pack as packPost } from '../../../models/post';
import PostReaction from '../../../models/post-reaction';
import { publishPostStream } from '../../../publishers/stream';
import Note, { INote, pack as packNote } from '../../../models/note';
import NoteReaction from '../../../models/note-reaction';
import { publishNoteStream } from '../../../publishers/stream';
import notify from '../../../publishers/notify';
import pushSw from '../../../publishers/push-sw';
import PostWatching from '../../../models/post-watching';
import NoteWatching from '../../../models/note-watching';
import watch from '../watch';
import renderLike from '../../../remote/activitypub/renderer/like';
import { deliver } from '../../../queue';
import context from '../../../remote/activitypub/renderer/context';
export default async (user: IUser, post: IPost, reaction: string) => new Promise(async (res, rej) => {
export default async (user: IUser, note: INote, reaction: string) => new Promise(async (res, rej) => {
// Myself
if (post.userId.equals(user._id)) {
return rej('cannot react to my post');
if (note.userId.equals(user._id)) {
return rej('cannot react to my note');
}
// if already reacted
const exist = await PostReaction.findOne({
postId: post._id,
const exist = await NoteReaction.findOne({
noteId: note._id,
userId: user._id
});
@@ -27,9 +27,9 @@ export default async (user: IUser, post: IPost, reaction: string) => new Promise
}
// Create reaction
await PostReaction.insert({
await NoteReaction.insert({
createdAt: new Date(),
postId: post._id,
noteId: note._id,
userId: user._id,
reaction
});
@@ -40,28 +40,28 @@ export default async (user: IUser, post: IPost, reaction: string) => new Promise
inc[`reactionCounts.${reaction}`] = 1;
// Increment reactions count
await Post.update({ _id: post._id }, {
await Note.update({ _id: note._id }, {
$inc: inc
});
publishPostStream(post._id, 'reacted');
publishNoteStream(note._id, 'reacted');
// Notify
notify(post.userId, user._id, 'reaction', {
postId: post._id,
notify(note.userId, user._id, 'reaction', {
noteId: note._id,
reaction: reaction
});
pushSw(post.userId, 'reaction', {
user: await packUser(user, post.userId),
post: await packPost(post, post.userId),
pushSw(note.userId, 'reaction', {
user: await packUser(user, note.userId),
note: await packNote(note, note.userId),
reaction: reaction
});
// Fetch watchers
PostWatching
NoteWatching
.find({
postId: post._id,
noteId: note._id,
userId: { $ne: user._id }
}, {
fields: {
@@ -71,7 +71,7 @@ export default async (user: IUser, post: IPost, reaction: string) => new Promise
.then(watchers => {
watchers.forEach(watcher => {
notify(watcher.userId, user._id, 'reaction', {
postId: post._id,
noteId: note._id,
reaction: reaction
});
});
@@ -79,16 +79,16 @@ export default async (user: IUser, post: IPost, reaction: string) => new Promise
// ユーザーがローカルユーザーかつ自動ウォッチ設定がオンならばこの投稿をWatchする
if (isLocalUser(user) && user.account.settings.autoWatch !== false) {
watch(user._id, post);
watch(user._id, note);
}
//#region 配信
const content = renderLike(user, post);
const content = renderLike(user, note);
content['@context'] = context;
// リアクターがローカルユーザーかつリアクション対象がリモートユーザーの投稿なら配送
if (isLocalUser(user) && isRemoteUser(post._user)) {
deliver(user, content, post._user.account.inbox).save();
if (isLocalUser(user) && isRemoteUser(note._user)) {
deliver(user, content, note._user.account.inbox).save();
}
//#endregion
});

View File

@@ -1,15 +1,15 @@
import * as mongodb from 'mongodb';
import Watching from '../../models/post-watching';
import Watching from '../../models/note-watching';
export default async (me: mongodb.ObjectID, post: object) => {
export default async (me: mongodb.ObjectID, note: object) => {
// 自分の投稿はwatchできない
if (me.equals((post as any).userId)) {
if (me.equals((note as any).userId)) {
return;
}
// if watching now
const exist = await Watching.findOne({
postId: (post as any)._id,
noteId: (note as any)._id,
userId: me,
deletedAt: { $exists: false }
});
@@ -20,7 +20,7 @@ export default async (me: mongodb.ObjectID, post: object) => {
await Watching.insert({
createdAt: new Date(),
postId: (post as any)._id,
noteId: (note as any)._id,
userId: me
});
};