Skip to content

Commit

Permalink
Merge pull request #84 from Gnuxie/gnuxie/protect-all-joined-rooms
Browse files Browse the repository at this point in the history
change `handleExternalInvite` to become `handleExternalMembership`
  • Loading branch information
Gnuxie authored Jan 31, 2025
2 parents a2bbaf4 + 1b1fc29 commit caf1da3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/Protection/ProtectedRoomsSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export interface ProtectedRoomsSet {
readonly allProtectedRooms: MatrixRoomID[];
handleTimelineEvent(roomID: StringRoomID, event: RoomEvent): void;
handleEventReport(report: EventReport): void;
handleExternalInvite(roomID: StringRoomID, event: MembershipEvent): void;
handleExternalMembership(roomID: StringRoomID, event: MembershipEvent): void;
isProtectedRoom(roomID: StringRoomID): boolean;
unregisterListeners(): void;
}
Expand Down Expand Up @@ -168,15 +168,15 @@ export class StandardProtectedRoomsSet implements ProtectedRoomsSet {
void Task(protection.handleEventReport(report));
}
}
public handleExternalInvite(
public handleExternalMembership(
roomID: StringRoomID,
event: MembershipEvent
): void {
for (const protection of this.protections.allProtections) {
if (protection.handleExternalInvite === undefined) {
if (protection.handleExternalMembership === undefined) {
continue;
}
protection.handleExternalInvite(roomID, event);
protection.handleExternalMembership(roomID, event);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Protection/Protection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ export interface Protection<TProtectionDescription> {
handlePermissionRequirementsMet?(room: MatrixRoomID): void;

/**
* Handle an invitation to a room that is external to the protected rooms set.
* Handle an invitation, room join, kick, leave etc to a room that is external to the protected rooms set.
* @param roomID The room the invitation is for.
* @param event The invitation event itself.
*/
handleExternalInvite?(roomID: StringRoomID, event: MembershipEvent): void;
handleExternalMembership?(roomID: StringRoomID, event: MembershipEvent): void;

/**
* This can be used to determine who are new to the overall protected rooms
Expand Down
86 changes: 72 additions & 14 deletions src/StateTracking/EventBatch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Copyright 2022 - 2023 Gnuxie <Gnuxie@protonmail.com>
// Copyright 2022 - 2025 Gnuxie <Gnuxie@protonmail.com>
// Copyright 2019 - 2021 The Matrix.org Foundation C.I.C.
//
// SPDX-License-Identifier: AFL-3.0 AND Apache-2.0
// SPDX-License-Identifier: Apache-2.0
//
// SPDX-FileAttributionText: <text>
// This modified file incorporates work from mjolnir
// https://github.com/matrix-org/mjolnir
// </text>
// SPDX-FileAttributionText: <text>
// This modified file incorporates work from matrix-protection-suite
// https://github.com/Gnuxie/matrix-protection-suite
// </text>

import { StringEventID } from '@the-draupnir-project/matrix-basic-types';
import { Logger } from '../Logging/Logger';
Expand All @@ -17,23 +21,46 @@ function logBatchCompleteCallbackError(e: unknown): void {
log.error('Caught an exception from the callback for an event batch', e);
}

type EventWithID = { event_id: StringEventID };
export interface Batcher<Key extends string, Value> {
add(key: Key, value: Value): void;
}

export interface EventBatch<E extends EventWithID = EventWithID> {
addEvent({ event_id }: E): void;
export class StandardBatcher<Key extends string, Value>
implements Batcher<Key, Value>
{
private currentBatch: Batch<Key, Value>;
public constructor(
private readonly batchFactoryMethod: () => Batch<Key, Value>
) {
this.currentBatch = batchFactoryMethod();
}

public add(key: Key, value: Value): void {
if (this.currentBatch.isFinished()) {
this.currentBatch = this.batchFactoryMethod();
}
this.currentBatch.add(key, value);
}
}

export interface Batch<Key extends string, Value> {
add(key: Key, data: Value): void;
isFinished(): boolean;
batchCompleteCallback: (events: E[]) => Promise<void>;
batchCompleteCallback: (entries: [Key, Value][]) => Promise<void>;
}

export class ConstantPeriodEventBatch<E extends EventWithID = EventWithID>
implements EventBatch<E>
export class ConstantPeriodItemBatch<Key extends string, Value>
implements Batch<Key, Value>
{
private readonly waitPeriodMS: number;
private events = new Map<StringEventID, E>();
private items = new Map<Key, Value>();
private isBatchComplete = false;
private isWaiting = false;
constructor(
public readonly batchCompleteCallback: EventBatch['batchCompleteCallback'],
public readonly batchCompleteCallback: Batch<
Key,
Value
>['batchCompleteCallback'],
{ waitPeriodMS = 200 }
) {
this.waitPeriodMS = waitPeriodMS;
Expand All @@ -43,16 +70,16 @@ export class ConstantPeriodEventBatch<E extends EventWithID = EventWithID>
return this.isBatchComplete;
}

public addEvent(event: E): void {
public add(key: Key, item: Value): void {
if (this.isFinished()) {
throw new TypeError(
'Something tried adding an event to a completed EventBatch'
);
}
if (this.events.has(event.event_id)) {
if (this.items.has(key)) {
return;
}
this.events.set(event.event_id, event);
this.items.set(key, item);
if (!this.isWaiting) {
// spawn off the timer to call the callback.
this.startCallbackTimer();
Expand All @@ -69,8 +96,39 @@ export class ConstantPeriodEventBatch<E extends EventWithID = EventWithID>

private completeBatch(): void {
this.isBatchComplete = true;
this.batchCompleteCallback([...this.events.values()]).catch(
this.batchCompleteCallback([...this.items.entries()]).catch(
logBatchCompleteCallbackError
);
}
}

type EventWithID = { event_id: StringEventID };

export interface EventBatch<E extends EventWithID = EventWithID> {
addEvent({ event_id }: E): void;
isFinished(): boolean;
batchCompleteCallback: (events: E[]) => Promise<void>;
}

export class ConstantPeriodEventBatch<E extends EventWithID = EventWithID>
implements EventBatch<E>
{
private readonly batch: ConstantPeriodItemBatch<StringEventID, EventWithID>;
constructor(
public readonly batchCompleteCallback: EventBatch['batchCompleteCallback'],
{ waitPeriodMS = 200 }
) {
this.batch = new ConstantPeriodItemBatch<StringEventID, EventWithID>(
(entries) => this.batchCompleteCallback(entries.map((entry) => entry[1])),
{ waitPeriodMS }
);
}

isFinished(): boolean {
return this.batch.isFinished();
}

addEvent(event: E): void {
this.batch.add(event.event_id, event);
}
}

0 comments on commit caf1da3

Please sign in to comment.