forked from FoundKeyGang/FoundKey
make delivery manager author agnostic
This commit is contained in:
parent
9b8438cdfc
commit
cec77dafa8
6 changed files with 38 additions and 37 deletions
|
@ -14,6 +14,7 @@ interface IEveryoneRecipe extends IRecipe {
|
||||||
|
|
||||||
interface IFollowersRecipe extends IRecipe {
|
interface IFollowersRecipe extends IRecipe {
|
||||||
type: 'Followers';
|
type: 'Followers';
|
||||||
|
followee: ILocalUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IDirectRecipe extends IRecipe {
|
interface IDirectRecipe extends IRecipe {
|
||||||
|
@ -32,26 +33,24 @@ const isDirect = (recipe: any): recipe is IDirectRecipe =>
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
export class DeliverManager {
|
export class DeliverManager {
|
||||||
private actor: { id: User['id']; host: null; };
|
|
||||||
private activity: any;
|
private activity: any;
|
||||||
private recipes: IRecipe[] = [];
|
private recipes: IRecipe[] = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param actor Actor
|
|
||||||
* @param activity Activity to deliver
|
* @param activity Activity to deliver
|
||||||
*/
|
*/
|
||||||
constructor(actor: { id: User['id']; host: null; }, activity: any) {
|
constructor(activity: any) {
|
||||||
this.actor = actor;
|
|
||||||
this.activity = activity;
|
this.activity = activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add recipe for followers deliver
|
* Add recipe for followers deliver
|
||||||
*/
|
*/
|
||||||
public addFollowersRecipe() {
|
public addFollowersRecipe(followee: ILocalUser) {
|
||||||
const deliver = {
|
const deliver = {
|
||||||
type: 'Followers',
|
type: 'Followers',
|
||||||
|
followee,
|
||||||
} as IFollowersRecipe;
|
} as IFollowersRecipe;
|
||||||
|
|
||||||
this.addRecipe(deliver);
|
this.addRecipe(deliver);
|
||||||
|
@ -89,8 +88,6 @@ export class DeliverManager {
|
||||||
* Execute delivers
|
* Execute delivers
|
||||||
*/
|
*/
|
||||||
public async execute() {
|
public async execute() {
|
||||||
if (!Users.isLocalUser(this.actor)) return;
|
|
||||||
|
|
||||||
const inboxes = new Set<string>();
|
const inboxes = new Set<string>();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -116,21 +113,25 @@ export class DeliverManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.recipes.some(r => isFollowers(r))) {
|
await Promise.all(
|
||||||
|
this.recipes.filter(isFollowers)
|
||||||
|
.map(recipe => {
|
||||||
// followers deliver
|
// followers deliver
|
||||||
const followers = await Followings.createQueryBuilder('followings')
|
return Followings.createQueryBuilder('followings')
|
||||||
// return either the shared inbox (if available) or the individual inbox
|
// return either the shared inbox (if available) or the individual inbox
|
||||||
.select('COALESCE(followings.followerSharedInbox, followings.followerInbox)', 'inbox')
|
.select('COALESCE(followings.followerSharedInbox, followings.followerInbox)', 'inbox')
|
||||||
// so we don't have to make our inboxes Set work as hard
|
// so we don't have to make our inboxes Set work as hard
|
||||||
.distinct(true)
|
.distinct(true)
|
||||||
// ...for the specific actors followers
|
// ...for the specific actors followers
|
||||||
.where('followings.followeeId = :actorId', { actorId: this.actor.id })
|
.where('followings.followeeId = :actorId', { actorId: recipe.followee.id })
|
||||||
// don't deliver to ourselves
|
// don't deliver to ourselves
|
||||||
.andWhere('followings.followerHost IS NOT NULL')
|
.andWhere('followings.followerHost IS NOT NULL')
|
||||||
.getRawMany();
|
.getRawMany()
|
||||||
|
.then(followers =>
|
||||||
followers.forEach(({ inbox }) => inboxes.add(inbox));
|
followers.forEach(({ inbox }) => inboxes.add(inbox))
|
||||||
}
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
this.recipes.filter((recipe): recipe is IDirectRecipe =>
|
this.recipes.filter((recipe): recipe is IDirectRecipe =>
|
||||||
// followers recipes have already been processed
|
// followers recipes have already been processed
|
||||||
|
@ -155,7 +156,7 @@ export class DeliverManager {
|
||||||
// skip instances as indicated
|
// skip instances as indicated
|
||||||
if (instancesToSkip.includes(new URL(inbox).host)) continue;
|
if (instancesToSkip.includes(new URL(inbox).host)) continue;
|
||||||
|
|
||||||
deliver(this.actor, this.activity, inbox);
|
deliver(null, this.activity, inbox);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -166,9 +167,9 @@ export class DeliverManager {
|
||||||
* @param activity Activity
|
* @param activity Activity
|
||||||
* @param from Followee
|
* @param from Followee
|
||||||
*/
|
*/
|
||||||
export async function deliverToFollowers(actor: { id: ILocalUser['id']; host: null; }, activity: any) {
|
export async function deliverToFollowers(actor: ILocalUser, activity: any) {
|
||||||
const manager = new DeliverManager(actor, activity);
|
const manager = new DeliverManager(activity);
|
||||||
manager.addFollowersRecipe();
|
manager.addFollowersRecipe(actor);
|
||||||
await manager.execute();
|
await manager.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,8 +178,8 @@ export async function deliverToFollowers(actor: { id: ILocalUser['id']; host: nu
|
||||||
* @param activity Activity
|
* @param activity Activity
|
||||||
* @param to Target user
|
* @param to Target user
|
||||||
*/
|
*/
|
||||||
export async function deliverToUser(actor: { id: ILocalUser['id']; host: null; }, activity: any, to: IRemoteUser) {
|
export async function deliverToUser(activity: any, to: IRemoteUser) {
|
||||||
const manager = new DeliverManager(actor, activity);
|
const manager = new DeliverManager(activity);
|
||||||
manager.addDirectRecipe(to);
|
manager.addDirectRecipe(to);
|
||||||
await manager.execute();
|
await manager.execute();
|
||||||
}
|
}
|
||||||
|
|
|
@ -432,7 +432,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
|
||||||
if (Users.isLocalUser(user) && !data.localOnly) {
|
if (Users.isLocalUser(user) && !data.localOnly) {
|
||||||
(async () => {
|
(async () => {
|
||||||
const noteActivity = renderActivity(await renderNoteOrRenoteActivity(note));
|
const noteActivity = renderActivity(await renderNoteOrRenoteActivity(note));
|
||||||
const dm = new DeliverManager(user, noteActivity);
|
const dm = new DeliverManager(noteActivity);
|
||||||
|
|
||||||
// Delivered to remote users who have been mentioned
|
// Delivered to remote users who have been mentioned
|
||||||
for (const u of mentionedUsers.filter(u => Users.isRemoteUser(u))) {
|
for (const u of mentionedUsers.filter(u => Users.isRemoteUser(u))) {
|
||||||
|
@ -453,7 +453,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
|
||||||
|
|
||||||
// Deliver to followers
|
// Deliver to followers
|
||||||
if (['public', 'home', 'followers'].includes(note.visibility)) {
|
if (['public', 'home', 'followers'].includes(note.visibility)) {
|
||||||
dm.addFollowersRecipe();
|
dm.addFollowersRecipe(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (['public'].includes(note.visibility)) {
|
if (['public'].includes(note.visibility)) {
|
||||||
|
|
|
@ -60,8 +60,8 @@ export async function deleteNotes(notes: Note[], user?: User): Promise<void> {
|
||||||
|
|
||||||
// Compute addressing information.
|
// Compute addressing information.
|
||||||
// Since we do not send any actual content, we send all note deletions to everyone.
|
// Since we do not send any actual content, we send all note deletions to everyone.
|
||||||
const manager = new DeliverManager(fetchedUser, content);
|
const manager = new DeliverManager(content);
|
||||||
manager.addFollowersRecipe();
|
manager.addFollowersRecipe(fetchedUser);
|
||||||
manager.addEveryone();
|
manager.addEveryone();
|
||||||
// Check mentioned users, since not all may have a shared inbox.
|
// Check mentioned users, since not all may have a shared inbox.
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
|
|
|
@ -130,14 +130,14 @@ export async function createReaction(user: { id: User['id']; host: User['host'];
|
||||||
//#region 配信
|
//#region 配信
|
||||||
if (Users.isLocalUser(user) && !note.localOnly) {
|
if (Users.isLocalUser(user) && !note.localOnly) {
|
||||||
const content = renderActivity(await renderLike(record, note));
|
const content = renderActivity(await renderLike(record, note));
|
||||||
const dm = new DeliverManager(user, content);
|
const dm = new DeliverManager(content);
|
||||||
if (note.userHost !== null) {
|
if (note.userHost !== null) {
|
||||||
const reactee = await Users.findOneBy({ id: note.userId });
|
const reactee = await Users.findOneBy({ id: note.userId });
|
||||||
dm.addDirectRecipe(reactee as IRemoteUser);
|
dm.addDirectRecipe(reactee as IRemoteUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (['public', 'home', 'followers'].includes(note.visibility)) {
|
if (['public', 'home', 'followers'].includes(note.visibility)) {
|
||||||
dm.addFollowersRecipe();
|
dm.addFollowersRecipe(user);
|
||||||
} else if (note.visibility === 'specified') {
|
} else if (note.visibility === 'specified') {
|
||||||
const visibleUsers = await Promise.all(note.visibleUserIds.map(id => Users.findOneBy({ id })));
|
const visibleUsers = await Promise.all(note.visibleUserIds.map(id => Users.findOneBy({ id })));
|
||||||
for (const u of visibleUsers.filter(u => u && Users.isRemoteUser(u))) {
|
for (const u of visibleUsers.filter(u => u && Users.isRemoteUser(u))) {
|
||||||
|
|
|
@ -46,12 +46,12 @@ export async function deleteReaction(user: { id: User['id']; host: User['host'];
|
||||||
//#region 配信
|
//#region 配信
|
||||||
if (Users.isLocalUser(user) && !note.localOnly) {
|
if (Users.isLocalUser(user) && !note.localOnly) {
|
||||||
const content = renderActivity(renderUndo(await renderLike(exist, note), user));
|
const content = renderActivity(renderUndo(await renderLike(exist, note), user));
|
||||||
const dm = new DeliverManager(user, content);
|
const dm = new DeliverManager(content);
|
||||||
if (note.userHost !== null) {
|
if (note.userHost !== null) {
|
||||||
const reactee = await Users.findOneBy({ id: note.userId });
|
const reactee = await Users.findOneBy({ id: note.userId });
|
||||||
dm.addDirectRecipe(reactee as IRemoteUser);
|
dm.addDirectRecipe(reactee as IRemoteUser);
|
||||||
}
|
}
|
||||||
dm.addFollowersRecipe();
|
dm.addFollowersRecipe(user);
|
||||||
dm.execute();
|
dm.execute();
|
||||||
}
|
}
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
|
@ -13,7 +13,7 @@ export async function doPostSuspend(user: { id: User['id']; host: User['host'] }
|
||||||
const content = renderActivity(renderDelete(`${config.url}/users/${user.id}`, user));
|
const content = renderActivity(renderDelete(`${config.url}/users/${user.id}`, user));
|
||||||
|
|
||||||
// deliver to all of known network
|
// deliver to all of known network
|
||||||
const dm = new DeliverManager(user, content);
|
const dm = new DeliverManager(content);
|
||||||
dm.addEveryone();
|
dm.addEveryone();
|
||||||
await dm.execute();
|
await dm.execute();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue