2019-11-09 03:51:54 -06:00
|
|
|
import { Users, Followings } from '../../models';
|
2021-03-23 21:05:37 -05:00
|
|
|
import { ILocalUser, IRemoteUser, User } from '../../models/entities/user';
|
2019-11-09 03:51:54 -06:00
|
|
|
import { deliver } from '../../queue';
|
|
|
|
|
|
|
|
//#region types
|
|
|
|
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';
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
export default class DeliverManager {
|
2021-03-23 21:05:37 -05:00
|
|
|
private actor: { id: User['id']; host: null; };
|
2019-11-09 03:51:54 -06:00
|
|
|
private activity: any;
|
|
|
|
private recipes: IRecipe[] = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param actor Actor
|
|
|
|
* @param activity Activity to deliver
|
|
|
|
*/
|
2021-03-23 21:05:37 -05:00
|
|
|
constructor(actor: { id: User['id']; host: null; }, activity: any) {
|
2019-11-09 03:51:54 -06:00
|
|
|
this.actor = actor;
|
|
|
|
this.activity = activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add recipe for followers deliver
|
|
|
|
*/
|
|
|
|
public addFollowersRecipe() {
|
|
|
|
const deliver = {
|
|
|
|
type: 'Followers'
|
|
|
|
} as IFollowersRecipe;
|
|
|
|
|
|
|
|
this.addRecipe(deliver);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add recipe for direct deliver
|
|
|
|
* @param to To
|
|
|
|
*/
|
|
|
|
public addDirectRecipe(to: IRemoteUser) {
|
|
|
|
const recipe = {
|
|
|
|
type: 'Direct',
|
|
|
|
to
|
|
|
|
} as IDirectRecipe;
|
|
|
|
|
|
|
|
this.addRecipe(recipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add recipe
|
|
|
|
* @param recipe Recipe
|
|
|
|
*/
|
|
|
|
public addRecipe(recipe: IRecipe) {
|
|
|
|
this.recipes.push(recipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute delivers
|
|
|
|
*/
|
|
|
|
public async execute() {
|
|
|
|
if (!Users.isLocalUser(this.actor)) return;
|
|
|
|
|
2021-03-21 07:00:59 -05:00
|
|
|
const inboxes = new Set<string>();
|
2019-11-09 03:51:54 -06:00
|
|
|
|
|
|
|
// build inbox list
|
|
|
|
for (const recipe of this.recipes) {
|
|
|
|
if (isFollowers(recipe)) {
|
|
|
|
// followers deliver
|
|
|
|
const followers = await Followings.find({
|
|
|
|
followeeId: this.actor.id
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const following of followers) {
|
|
|
|
if (Followings.isRemoteFollower(following)) {
|
|
|
|
const inbox = following.followerSharedInbox || following.followerInbox;
|
2021-03-21 07:00:59 -05:00
|
|
|
inboxes.add(inbox);
|
2019-11-09 03:51:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (isDirect(recipe)) {
|
|
|
|
// direct deliver
|
|
|
|
const inbox = recipe.to.inbox;
|
2021-03-21 07:00:59 -05:00
|
|
|
if (inbox) inboxes.add(inbox);
|
2019-11-09 03:51:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// deliver
|
|
|
|
for (const inbox of inboxes) {
|
|
|
|
deliver(this.actor, this.activity, inbox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//#region Utilities
|
|
|
|
/**
|
|
|
|
* Deliver activity to followers
|
|
|
|
* @param activity Activity
|
|
|
|
* @param from Followee
|
|
|
|
*/
|
|
|
|
export async function deliverToFollowers(actor: ILocalUser, activity: any) {
|
|
|
|
const manager = new DeliverManager(actor, activity);
|
|
|
|
manager.addFollowersRecipe();
|
|
|
|
await manager.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deliver activity to user
|
|
|
|
* @param activity Activity
|
|
|
|
* @param to Target user
|
|
|
|
*/
|
|
|
|
export async function deliverToUser(actor: ILocalUser, activity: any, to: IRemoteUser) {
|
|
|
|
const manager = new DeliverManager(actor, activity);
|
|
|
|
manager.addDirectRecipe(to);
|
|
|
|
await manager.execute();
|
|
|
|
}
|
|
|
|
//#endregion
|