egirlskey/packages/backend/src/core/activitypub/ApDeliverManagerService.ts

208 lines
4.8 KiB
TypeScript
Raw Normal View History

2022-09-17 18:27:08 +00:00
import { Inject, Injectable } from '@nestjs/common';
import { IsNull, Not } from 'typeorm';
2022-09-17 18:27:08 +00:00
import { DI } from '@/di-symbols.js';
2022-09-20 20:33:11 +00:00
import type { FollowingsRepository, UsersRepository } from '@/models/index.js';
import type { Config } from '@/config.js';
2022-09-17 18:27:08 +00:00
import type { ILocalUser, IRemoteUser, User } from '@/models/entities/User.js';
import { QueueService } from '@/core/QueueService.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { bindThis } from '@/decorators.js';
interface IRecipe {
type: string;
}
interface IFollowersRecipe extends IRecipe {
type: 'Followers';
}
interface IDirectRecipe extends IRecipe {
type: 'Direct';
to: IRemoteUser;
}
const isFollowers = (recipe: any): recipe is IFollowersRecipe =>
recipe.type === 'Followers';
const isDirect = (recipe: any): recipe is IDirectRecipe =>
recipe.type === 'Direct';
2022-09-17 18:27:08 +00:00
@Injectable()
export class ApDeliverManagerService {
constructor(
@Inject(DI.config)
private config: Config,
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@Inject(DI.followingsRepository)
private followingsRepository: FollowingsRepository,
private userEntityService: UserEntityService,
private queueService: QueueService,
) {
}
/**
* Deliver activity to followers
* @param activity Activity
* @param from Followee
*/
@bindThis
2022-09-17 18:27:08 +00:00
public async deliverToFollowers(actor: { id: ILocalUser['id']; host: null; }, activity: any) {
const manager = new DeliverManager(
this.userEntityService,
this.followingsRepository,
this.queueService,
actor,
activity,
);
manager.addFollowersRecipe();
await manager.execute();
}
/**
* Deliver activity to user
* @param activity Activity
* @param to Target user
*/
@bindThis
2022-09-17 18:27:08 +00:00
public async deliverToUser(actor: { id: ILocalUser['id']; host: null; }, activity: any, to: IRemoteUser) {
const manager = new DeliverManager(
this.userEntityService,
this.followingsRepository,
this.queueService,
actor,
activity,
);
manager.addDirectRecipe(to);
await manager.execute();
}
@bindThis
2022-09-17 18:27:08 +00:00
public createDeliverManager(actor: { id: User['id']; host: null; }, activity: any) {
return new DeliverManager(
this.userEntityService,
this.followingsRepository,
this.queueService,
actor,
activity,
);
}
}
class DeliverManager {
private actor: { id: User['id']; host: null; };
private activity: any;
private recipes: IRecipe[] = [];
/**
* Constructor
* @param actor Actor
* @param activity Activity to deliver
*/
2022-09-17 18:27:08 +00:00
constructor(
private userEntityService: UserEntityService,
private followingsRepository: FollowingsRepository,
private queueService: QueueService,
actor: { id: User['id']; host: null; },
activity: any,
) {
this.actor = actor;
this.activity = activity;
}
/**
* Add recipe for followers deliver
*/
@bindThis
public addFollowersRecipe() {
const deliver = {
2021-12-09 14:58:30 +00:00
type: 'Followers',
} as IFollowersRecipe;
this.addRecipe(deliver);
}
/**
* Add recipe for direct deliver
* @param to To
*/
@bindThis
public addDirectRecipe(to: IRemoteUser) {
const recipe = {
type: 'Direct',
2021-12-09 14:58:30 +00:00
to,
} as IDirectRecipe;
this.addRecipe(recipe);
}
/**
* Add recipe
* @param recipe Recipe
*/
@bindThis
public addRecipe(recipe: IRecipe) {
this.recipes.push(recipe);
}
/**
* Execute delivers
*/
@bindThis
public async execute() {
2022-09-17 18:27:08 +00:00
if (!this.userEntityService.isLocalUser(this.actor)) return;
2021-03-21 12:00:59 +00:00
const inboxes = new Set<string>();
/*
build inbox list
Process follower recipes first to avoid duplication when processing
direct recipes later.
*/
2022-04-02 06:31:11 +00:00
if (this.recipes.some(r => isFollowers(r))) {
// followers deliver
// TODO: SELECT DISTINCT ON ("followerSharedInbox") "followerSharedInbox" みたいな問い合わせにすればよりパフォーマンス向上できそう
// ただ、sharedInboxがnullなリモートユーザーも稀におり、その対応ができなさそう
2022-09-17 18:27:08 +00:00
const followers = await this.followingsRepository.find({
where: {
followeeId: this.actor.id,
followerHost: Not(IsNull()),
},
select: {
followerSharedInbox: true,
followerInbox: true,
},
}) as {
followerSharedInbox: string | null;
followerInbox: string;
}[];
for (const following of followers) {
2022-09-17 18:27:08 +00:00
const inbox = following.followerSharedInbox ?? following.followerInbox;
inboxes.add(inbox);
}
}
2022-04-03 06:33:22 +00:00
this.recipes.filter((recipe): recipe is IDirectRecipe =>
// followers recipes have already been processed
isDirect(recipe)
// check that shared inbox has not been added yet
&& !(recipe.to.sharedInbox && inboxes.has(recipe.to.sharedInbox))
// check that they actually have an inbox
2022-04-03 06:33:22 +00:00
&& recipe.to.inbox != null,
)
2022-09-17 18:27:08 +00:00
.forEach(recipe => inboxes.add(recipe.to.inbox!));
// deliver
for (const inbox of inboxes) {
2022-09-17 18:27:08 +00:00
this.queueService.deliver(this.actor, this.activity, inbox);
}
}
}