2018-09-01 13:17:30 +02:00
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import User, { isLocalUser, isRemoteUser } from '../../models/user';
|
|
|
|
import Following from '../../models/following';
|
|
|
|
import renderPerson from '../../remote/activitypub/renderer/person';
|
|
|
|
import renderUpdate from '../../remote/activitypub/renderer/update';
|
2019-01-30 18:29:36 +01:00
|
|
|
import { renderActivity } from '../../remote/activitypub/renderer';
|
2018-09-01 13:17:30 +02:00
|
|
|
import { deliver } from '../../queue';
|
|
|
|
|
|
|
|
export async function publishToFollowers(userId: mongo.ObjectID) {
|
|
|
|
const user = await User.findOne({
|
|
|
|
_id: userId
|
|
|
|
});
|
|
|
|
|
|
|
|
const followers = await Following.find({
|
|
|
|
followeeId: user._id
|
|
|
|
});
|
|
|
|
|
|
|
|
const queue: string[] = [];
|
|
|
|
|
|
|
|
// フォロワーがリモートユーザーかつ投稿者がローカルユーザーならUpdateを配信
|
|
|
|
if (isLocalUser(user)) {
|
2018-11-30 15:30:28 +01:00
|
|
|
for (const following of followers) {
|
2018-09-01 13:17:30 +02:00
|
|
|
const follower = following._follower;
|
|
|
|
|
|
|
|
if (isRemoteUser(follower)) {
|
|
|
|
const inbox = follower.sharedInbox || follower.inbox;
|
|
|
|
if (!queue.includes(inbox)) queue.push(inbox);
|
|
|
|
}
|
2018-11-30 15:30:28 +01:00
|
|
|
}
|
2018-09-01 13:17:30 +02:00
|
|
|
|
|
|
|
if (queue.length > 0) {
|
2019-01-30 18:29:36 +01:00
|
|
|
const content = renderActivity(renderUpdate(await renderPerson(user), user));
|
2018-12-11 12:36:55 +01:00
|
|
|
for (const inbox of queue) {
|
2018-09-01 13:17:30 +02:00
|
|
|
deliver(user, content, inbox);
|
2018-12-11 12:36:55 +01:00
|
|
|
}
|
2018-09-01 13:17:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|