無駄なAP deliverをしないように (#5589)

* DeliverManager, note/create

* recipe

* followers delivers

* comment

* rename

* fix

* cleanup
This commit is contained in:
MeiMei 2019-11-09 18:51:54 +09:00 committed by syuilo
parent b34b728fbb
commit 44ab428803
6 changed files with 182 additions and 153 deletions

View file

@ -1,6 +1,6 @@
import es from '../../db/elasticsearch';
import { publishMainStream, publishNotesStream } from '../stream';
import { deliver } from '../../queue';
import DeliverManager from '../../remote/activitypub/deliver-manager';
import renderNote from '../../remote/activitypub/renderer/note';
import renderCreate from '../../remote/activitypub/renderer/create';
import renderAnnounce from '../../remote/activitypub/renderer/announce';
@ -17,7 +17,7 @@ import extractMentions from '../../misc/extract-mentions';
import extractEmojis from '../../misc/extract-emojis';
import extractHashtags from '../../misc/extract-hashtags';
import { Note, IMentionedRemoteUsers } from '../../models/entities/note';
import { Mutings, Users, NoteWatchings, Followings, Notes, Instances, UserProfiles } from '../../models';
import { Mutings, Users, NoteWatchings, Notes, Instances, UserProfiles } from '../../models';
import { DriveFile } from '../../models/entities/drive-file';
import { App } from '../../models/entities/app';
import { Not, getConnection, In } from 'typeorm';
@ -246,12 +246,6 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
createMentionedEvents(mentionedUsers, note, nm);
const noteActivity = await renderNoteOrRenoteActivity(data, note);
if (Users.isLocalUser(user)) {
deliverNoteToMentionedRemoteUsers(mentionedUsers, user, noteActivity);
}
const profile = await UserProfiles.findOne(user.id).then(ensure);
// If has in reply to note
@ -294,11 +288,42 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
}
}
publish(user, note, data.reply, data.renote, noteActivity);
Promise.all(nmRelatedPromises).then(() => {
nm.deliver();
});
//#region AP deliver
if (Users.isLocalUser(user)) {
(async () => {
const noteActivity = await renderNoteOrRenoteActivity(data, note);
const dm = new DeliverManager(user, noteActivity);
// メンションされたリモートユーザーに配送
for (const u of mentionedUsers.filter(u => Users.isRemoteUser(u))) {
dm.addDirectRecipe(u as IRemoteUser);
}
// 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送
if (data.reply && data.reply.userHost !== null) {
const u = await Users.findOne(data.reply.userId);
if (u && Users.isRemoteUser(u)) dm.addDirectRecipe(u);
}
// 投稿がRenoteかつ投稿者がローカルユーザーかつRenote元の投稿の投稿者がリモートユーザーなら配送
if (data.renote && data.renote.userHost !== null) {
const u = await Users.findOne(data.renote.userId);
if (u && Users.isRemoteUser(u)) dm.addDirectRecipe(u);
}
// フォロワーに配送
if (['public', 'home', 'followers'].includes(note.visibility)) {
dm.addFollowersRecipe();
}
dm.execute();
})();
}
//#endregion
}
// Register to search database
@ -320,29 +345,6 @@ function incRenoteCount(renote: Note) {
Notes.increment({ id: renote.id }, 'score', 1);
}
async function publish(user: User, note: Note, reply: Note | null | undefined, renote: Note | null | undefined, noteActivity: any) {
if (Users.isLocalUser(user)) {
// 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送
if (reply && reply.userHost !== null) {
Users.findOne(reply.userId).then(ensure).then(u => {
deliver(user, noteActivity, u.inbox);
});
}
// 投稿がRenoteかつ投稿者がローカルユーザーかつRenote元の投稿の投稿者がリモートユーザーなら配送
if (renote && renote.userHost !== null) {
Users.findOne(renote.userId).then(ensure).then(u => {
deliver(user, noteActivity, u.inbox);
});
}
}
if (['public', 'home', 'followers'].includes(note.visibility)) {
// フォロワーに配信
publishToFollowers(note, user, noteActivity);
}
}
async function insertNote(user: User, data: Option, tags: string[], emojis: string[], mentionedUsers: User[]) {
const insert = new Note({
id: genId(data.createdAt!),
@ -472,34 +474,6 @@ async function notifyToWatchersOfReplyee(reply: Note, user: User, nm: Notificati
}
}
async function publishToFollowers(note: Note, user: User, noteActivity: any) {
const followers = await Followings.find({
followeeId: note.userId
});
const queue: string[] = [];
for (const following of followers) {
if (Followings.isRemoteFollower(following)) {
// フォロワーがリモートユーザーかつ投稿者がローカルユーザーなら投稿を配信
if (Users.isLocalUser(user)) {
const inbox = following.followerSharedInbox || following.followerInbox;
if (!queue.includes(inbox)) queue.push(inbox);
}
}
}
for (const inbox of queue) {
deliver(user as any, noteActivity, inbox);
}
}
function deliverNoteToMentionedRemoteUsers(mentionedUsers: User[], user: ILocalUser, noteActivity: any) {
for (const u of mentionedUsers.filter(u => Users.isRemoteUser(u))) {
deliver(user, noteActivity, (u as IRemoteUser).inbox);
}
}
async function createMentionedEvents(mentionedUsers: User[], note: Note, nm: NotificationManager) {
for (const u of mentionedUsers.filter(u => Users.isLocalUser(u))) {
const detailPackedNote = await Notes.pack(note, u, {