Skip to content

Commit

Permalink
Pre-processing deliver body
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Jan 5, 2024
1 parent 6598d32 commit d148108
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
10 changes: 8 additions & 2 deletions packages/backend/src/core/QueueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { DbQueue, DeliverQueue, EndedPollNotificationQueue, InboxQueue, Obj
import type { DbJobData, DeliverJobData, RelationshipJobData, ThinUser } from '../queue/types.js';
import type httpSignature from '@peertube/http-signature';
import type * as Bull from 'bullmq';
import { ApRequestCreator } from '@/core/activitypub/ApRequestService.js';

@Injectable()
export class QueueService {
Expand Down Expand Up @@ -74,11 +75,14 @@ export class QueueService {
if (content == null) return null;
if (to == null) return null;

const contentBody = JSON.stringify(content);

const data: DeliverJobData = {
user: {
id: user.id,
},
content,
content: contentBody,
digest: ApRequestCreator.createDigest(contentBody),
to,
isSharedInbox,
};
Expand All @@ -103,6 +107,7 @@ export class QueueService {
@bindThis
public async deliverMany(user: ThinUser, content: IActivity | null, inboxes: Map<string, boolean>) {
if (content == null) return null;
const contentBody = JSON.stringify(content);

const opts = {
attempts: this.config.deliverJobMaxAttempts ?? 12,
Expand All @@ -117,7 +122,8 @@ export class QueueService {
name: d[0],
data: {
user,
content,
content: contentBody,
digest: ApRequestCreator.createDigest(contentBody),
to: d[0],
isSharedInbox: d[1],
} as DeliverJobData,
Expand Down
13 changes: 9 additions & 4 deletions packages/backend/src/core/activitypub/ApRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ type PrivateKey = {
};

export class ApRequestCreator {
static createSignedPost(args: { key: PrivateKey, url: string, body: string, additionalHeaders: Record<string, string> }): Signed {
static createSignedPost(args: { key: PrivateKey, url: string, body: string, digest?: string, additionalHeaders: Record<string, string> }): Signed {
const u = new URL(args.url);
const digestHeader = `SHA-256=${crypto.createHash('sha256').update(args.body).digest('base64')}`;
const digestHeader = args.digest ?? this.createDigest(args.body);

const request: Request = {
url: u.href,
Expand All @@ -59,6 +59,10 @@ export class ApRequestCreator {
};
}

static createDigest(body: string) {
return `SHA-256=${crypto.createHash('sha256').update(body).digest('base64')}`;
}

static createSignedGet(args: { key: PrivateKey, url: string, additionalHeaders: Record<string, string> }): Signed {
const u = new URL(args.url);

Expand Down Expand Up @@ -145,8 +149,8 @@ export class ApRequestService {
}

@bindThis
public async signedPost(user: { id: MiUser['id'] }, url: string, object: unknown): Promise<void> {
const body = JSON.stringify(object);
public async signedPost(user: { id: MiUser['id'] }, url: string, object: unknown, digest?: string): Promise<void> {
const body = typeof object === 'string' ? object : JSON.stringify(object);

const keypair = await this.userKeypairService.getUserKeypair(user.id);

Expand All @@ -157,6 +161,7 @@ export class ApRequestService {
},
url,
body,
digest,
additionalHeaders: {
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class DeliverProcessorService {
}

try {
await this.apRequestService.signedPost(job.data.user, job.data.to, job.data.content);
await this.apRequestService.signedPost(job.data.user, job.data.to, job.data.content, job.data.digest);

// Update stats
this.federatedInstanceService.fetch(host).then(i => {
Expand Down
4 changes: 3 additions & 1 deletion packages/backend/src/queue/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export type DeliverJobData = {
/** Actor */
user: ThinUser;
/** Activity */
content: unknown;
content: string;
/** Digest header */
digest: string;
/** inbox URL to deliver */
to: string;
/** whether it is sharedInbox */
Expand Down

0 comments on commit d148108

Please sign in to comment.