Skip to content

Commit 9ceab2a

Browse files
authored
Merge pull request #988 from the-draupnir-project/gnuxie/tests-being-weird
Fix test failures on main innit Closes #985. We're solving this problem structurally by introducing a `Lifetime` primitive for resource management. the-draupnir-project/planning#79.
2 parents cb86d6d + 3945cf8 commit 9ceab2a

27 files changed

+191
-65
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"jsdom": "^24.0.0",
6565
"matrix-appservice-bridge": "^10.3.1",
6666
"matrix-bot-sdk": "npm:@vector-im/matrix-bot-sdk@^0.7.1-element.6",
67-
"matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@4.1.0",
67+
"matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@5.0.0",
6868
"matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/[email protected]",
6969
"pg": "^8.8.0",
7070
"yaml": "^2.3.2"

src/protections/BanPropagation.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import {
3030
UserConsequences,
3131
Membership,
3232
UnknownConfig,
33+
allocateProtection,
34+
OwnLifetime,
35+
Protection,
3336
} from "matrix-protection-suite";
3437
import { Draupnir } from "../Draupnir";
3538
import { resolveRoomReferenceSafe } from "matrix-protection-suite-for-matrix-bot-sdk";
@@ -212,11 +215,14 @@ export class BanPropagationProtection
212215
this.banReactionListener.bind(this);
213216
constructor(
214217
description: BanPropagationProtectionCapabilitiesDescription,
218+
lifetime: OwnLifetime<
219+
Protection<BanPropagationProtectionCapabilitiesDescription>
220+
>,
215221
capabilities: BanPropagationProtectionCapabilities,
216222
protectedRoomsSet: ProtectedRoomsSet,
217223
private readonly draupnir: Draupnir
218224
) {
219-
super(description, capabilities, protectedRoomsSet, {});
225+
super(description, lifetime, capabilities, protectedRoomsSet, {});
220226
this.draupnir.reactionHandler.on(
221227
BAN_PROPAGATION_PROMPT_LISTENER,
222228
this.banPropagationPromptListener
@@ -338,14 +344,17 @@ describeProtection<BanPropagationProtectionCapabilities, Draupnir>({
338344
},
339345
factory: async (
340346
decription,
347+
lifetime,
341348
protectedRoomsSet,
342349
draupnir,
343350
capabilities,
344351
_settings
345352
) =>
346-
Ok(
353+
allocateProtection(
354+
lifetime,
347355
new BanPropagationProtection(
348356
decription,
357+
lifetime,
349358
capabilities,
350359
protectedRoomsSet,
351360
draupnir

src/protections/BasicFlooding.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import {
2424
EventConsequences,
2525
Logger,
2626
Ok,
27+
OwnLifetime,
2728
ProtectedRoomsSet,
29+
Protection,
2830
ProtectionDescription,
2931
RoomEvent,
3032
UserConsequences,
@@ -80,6 +82,7 @@ describeProtection<
8082
configSchema: BasicFloodingProtectionSettings,
8183
factory: async (
8284
description,
85+
lifetime,
8386
protectedRoomsSet,
8487
draupnir,
8588
capabilities,
@@ -93,6 +96,7 @@ describeProtection<
9396
return Ok(
9497
new BasicFloodingProtection(
9598
description,
99+
lifetime,
96100
capabilities,
97101
protectedRoomsSet,
98102
draupnir,
@@ -154,12 +158,13 @@ export class BasicFloodingProtection
154158
private readonly eventConsequences: EventConsequences;
155159
public constructor(
156160
description: BasicFloodingProtectionDescription,
161+
lifetime: OwnLifetime<Protection<BasicFloodingProtectionDescription>>,
157162
capabilities: BasicFloodingProtectionCapabilities,
158163
protectedRoomsSet: ProtectedRoomsSet,
159164
private readonly draupnir: Draupnir,
160165
private readonly maxPerMinute: number
161166
) {
162-
super(description, capabilities, protectedRoomsSet, {});
167+
super(description, lifetime, capabilities, protectedRoomsSet, {});
163168
this.userConsequences = capabilities.userConsequences;
164169
this.eventConsequences = capabilities.eventConsequences;
165170
}

src/protections/BlockInvitationsOnServerProtection.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
import {
66
AbstractProtection,
7+
allocateProtection,
78
describeProtection,
89
Logger,
10+
OwnLifetime,
911
PolicyRuleType,
1012
ProtectedRoomsSet,
1113
Protection,
@@ -23,7 +25,7 @@ import {
2325
} from "@the-draupnir-project/matrix-basic-types";
2426
import { SynapseHttpAntispam } from "../webapis/SynapseHTTPAntispam/SynapseHttpAntispam";
2527
import { Draupnir } from "../Draupnir";
26-
import { Ok, ResultError } from "@gnuxie/typescript-result";
28+
import { ResultError } from "@gnuxie/typescript-result";
2729

2830
const log = new Logger("SynapseHTTPUserMayInvite");
2931

@@ -131,12 +133,15 @@ export class BlockInvitationsOnServerProtection
131133
private readonly userMayInvite: SynapseHTTPUserMayInvite;
132134
public constructor(
133135
description: BlockInvitationsOnServerProtectionDescription,
136+
lifetime: OwnLifetime<
137+
Protection<BlockInvitationsOnServerProtectionDescription>
138+
>,
134139
capabilities: BlockInvitationsOnServerProtectionCapabilities,
135140
protectedRoomsSet: ProtectedRoomsSet,
136141
automaticallyRedactForReasons: string[],
137142
synapseHTTPAntispam: SynapseHttpAntispam
138143
) {
139-
super(description, capabilities, protectedRoomsSet, {});
144+
super(description, lifetime, capabilities, protectedRoomsSet, {});
140145
this.userMayInvite = new SynapseHTTPUserMayInvite(
141146
protectedRoomsSet.watchedPolicyRooms,
142147
automaticallyRedactForReasons,
@@ -157,6 +162,7 @@ describeProtection<BlockInvitationsOnServerProtectionCapabilities, Draupnir>({
157162
defaultCapabilities: {},
158163
async factory(
159164
description,
165+
lifetime,
160166
protectedRoomsSet,
161167
draupnir,
162168
capabilities,
@@ -167,9 +173,11 @@ describeProtection<BlockInvitationsOnServerProtectionCapabilities, Draupnir>({
167173
"This protection requires synapse-http-antispam to be enabled"
168174
);
169175
}
170-
return Ok(
176+
return allocateProtection(
177+
lifetime,
171178
new BlockInvitationsOnServerProtection(
172179
description,
180+
lifetime,
173181
capabilities,
174182
protectedRoomsSet,
175183
draupnir.config.automaticallyRedactForReasons,

src/protections/DraupnirNews/DraupnirNews.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import {
99
AbstractProtection,
1010
ActionException,
1111
ActionExceptionKind,
12+
allocateProtection,
1213
ConstantPeriodBatch,
1314
describeProtection,
1415
EDStatic,
1516
isError,
1617
Logger,
1718
MessageContent,
19+
OwnLifetime,
1820
ProtectedRoomsSet,
21+
Protection,
1922
ProtectionDescription,
2023
StandardTimedGate,
2124
Value,
@@ -234,12 +237,13 @@ export class DraupnirNews
234237
);
235238
public constructor(
236239
description: DraupnirNewsDescription,
240+
lifetime: OwnLifetime<Protection<DraupnirNewsDescription>>,
237241
capabilities: DraupnirNewsProtectionCapabilities,
238242
protectedRoomsSet: ProtectedRoomsSet,
239243
private readonly settings: DraupnirNewsProtectionSettings,
240244
private readonly draupnir: Draupnir
241245
) {
242-
super(description, capabilities, protectedRoomsSet, {});
246+
super(description, lifetime, capabilities, protectedRoomsSet, {});
243247
}
244248

245249
private async updateNews(allNews: DraupnirNewsItem[]): Promise<Result<void>> {
@@ -281,14 +285,17 @@ describeProtection<
281285
configSchema: DraupnirNewsProtectionSettings,
282286
async factory(
283287
description,
288+
lifetime,
284289
protectedRoomsSet,
285290
draupnir,
286291
capabilities,
287292
settings
288293
) {
289-
return Ok(
294+
return allocateProtection(
295+
lifetime,
290296
new DraupnirNews(
291297
description,
298+
lifetime,
292299
capabilities,
293300
protectedRoomsSet,
294301
settings,

src/protections/FirstMessageIsImage.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
MembershipChange,
1717
MembershipChangeType,
1818
Ok,
19+
OwnLifetime,
1920
ProtectedRoomsSet,
2021
Protection,
2122
ProtectionDescription,
@@ -60,6 +61,7 @@ describeProtection<FirstMessageIsImageProtectionCapabilities, Draupnir>({
6061
},
6162
factory: async function (
6263
description,
64+
lifetime,
6365
protectedRoomsSet,
6466
draupnir,
6567
capabilities,
@@ -68,6 +70,7 @@ describeProtection<FirstMessageIsImageProtectionCapabilities, Draupnir>({
6870
return Ok(
6971
new FirstMessageIsImageProtection(
7072
description,
73+
lifetime,
7174
capabilities,
7275
protectedRoomsSet,
7376
draupnir
@@ -87,11 +90,12 @@ export class FirstMessageIsImageProtection
8790
private readonly eventConsequences: EventConsequences;
8891
constructor(
8992
description: FirstMessageIsImageProtectionDescription,
93+
lifetime: OwnLifetime<FirstMessageIsImageProtectionDescription>,
9094
capabilities: FirstMessageIsImageProtectionCapabilities,
9195
protectedRoomsSet: ProtectedRoomsSet,
9296
private readonly draupnir: Draupnir
9397
) {
94-
super(description, capabilities, protectedRoomsSet, {});
98+
super(description, lifetime, capabilities, protectedRoomsSet, {});
9599
this.userConsequences = capabilities.userConsequences;
96100
this.eventConsequences = capabilities.eventConsequences;
97101
}

src/protections/HomeserverUserPolicyApplication/HomeserverUserPolicyProtection.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
AbstractProtection,
88
describeProtection,
99
EDStatic,
10+
OwnLifetime,
1011
PolicyListRevision,
1112
PolicyRuleChange,
1213
ProtectedRoomsSet,
@@ -55,14 +56,15 @@ export class HomeserverUserPolicyProtection
5556
private readonly policyApplication: HomeserverUserPolicyApplication;
5657
constructor(
5758
description: HomeserverUserProtectionDescription,
59+
lifetime: OwnLifetime<HomeserverUserProtectionDescription>,
5860
capabilities: HomeserverUserPolicyProtectionCapabilities,
5961
protectedRoomsSet: ProtectedRoomsSet,
6062
auditLog: UserRestrictionAuditLog,
6163
automaticallyRedactForReasons: MatrixGlob[],
6264
managementRoomID: StringRoomID,
6365
confirmationPromptSender: ConfirmationPromptSender
6466
) {
65-
super(description, capabilities, protectedRoomsSet, {});
67+
super(description, lifetime, capabilities, protectedRoomsSet, {});
6668
this.policyApplication = new HomeserverUserPolicyApplication(
6769
managementRoomID,
6870
capabilities.userRestrictionCapability,
@@ -99,6 +101,7 @@ describeProtection<
99101
configSchema: HomeserverUserPolicyProtectionSettings,
100102
async factory(
101103
description,
104+
lifetime,
102105
protectedRoomsSet,
103106
draupnir,
104107
capabilitySet,
@@ -112,6 +115,7 @@ describeProtection<
112115
return Ok(
113116
new HomeserverUserPolicyProtection(
114117
description,
118+
lifetime,
115119
capabilitySet,
116120
protectedRoomsSet,
117121
draupnir.stores.userRestrictionAuditLog,

src/protections/InvalidEventProtection.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import { Type } from "@sinclair/typebox";
66
import {
77
AbstractProtection,
8+
allocateProtection,
89
describeProtection,
910
EDStatic,
1011
EventConsequences,
1112
EventWithMixins,
1213
isError,
1314
Logger,
14-
Ok,
15+
OwnLifetime,
1516
ProtectedRoomsSet,
1617
Protection,
1718
ProtectionDescription,
@@ -75,13 +76,14 @@ export class InvalidEventProtection
7576
);
7677
public constructor(
7778
description: InvalidEventProtectionDescription,
79+
lifetime: OwnLifetime<InvalidEventProtectionDescription>,
7880
capabilities: InvalidEventProtectionCapabilities,
7981
protectedRoomsSet: ProtectedRoomsSet,
8082
private readonly warningText: string,
8183
private readonly roomMessageSender: RoomMessageSender,
8284
private readonly managentRoomID: StringRoomID
8385
) {
84-
super(description, capabilities, protectedRoomsSet, {});
86+
super(description, lifetime, capabilities, protectedRoomsSet, {});
8587
this.eventConsequences = capabilities.eventConsequences;
8688
this.userConsequences = capabilities.userConsequences;
8789
}
@@ -196,14 +198,17 @@ describeProtection<
196198
configSchema: InvalidEventProtectionSettings,
197199
factory: async (
198200
decription,
201+
lifetime,
199202
protectedRoomsSet,
200203
draupnir,
201204
capabilitySet,
202205
settings
203206
) =>
204-
Ok(
207+
allocateProtection(
208+
lifetime,
205209
new InvalidEventProtection(
206210
decription,
211+
lifetime,
207212
capabilitySet,
208213
protectedRoomsSet,
209214
settings.warningText,

src/protections/JoinWaveShortCircuit.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ import {
1818
MembershipChange,
1919
MembershipChangeType,
2020
Ok,
21+
OwnLifetime,
2122
ProtectedRoomsSet,
23+
Protection,
2224
ProtectionDescription,
2325
RoomMembershipRevision,
26+
allocateProtection,
2427
describeProtection,
2528
isError,
2629
} from "matrix-protection-suite";
@@ -95,6 +98,7 @@ describeProtection<
9598
configSchema: JoinWaveShortCircuitProtectionSettings,
9699
factory: async function (
97100
description,
101+
lifetime,
98102
protectedRoomsSet,
99103
draupnir,
100104
capabilities,
@@ -104,9 +108,11 @@ describeProtection<
104108
if (isError(parsedSettings)) {
105109
return parsedSettings;
106110
}
107-
return Ok(
111+
return allocateProtection(
112+
lifetime,
108113
new JoinWaveShortCircuitProtection(
109114
description,
115+
lifetime,
110116
capabilities,
111117
protectedRoomsSet,
112118
draupnir,
@@ -124,12 +130,15 @@ export class JoinWaveShortCircuitProtection
124130

125131
constructor(
126132
description: JoinWaveShortCircuitProtectionDescription,
133+
lifetime: OwnLifetime<
134+
Protection<JoinWaveShortCircuitProtectionDescription>
135+
>,
127136
capabilities: CapabilitySet,
128137
protectedRoomsSet: ProtectedRoomsSet,
129138
private readonly draupnir: Draupnir,
130139
public readonly settings: JoinWaveShortCircuitProtectionSettings
131140
) {
132-
super(description, capabilities, protectedRoomsSet, {
141+
super(description, lifetime, capabilities, protectedRoomsSet, {
133142
requiredStatePermissions: ["m.room.join_rules"],
134143
});
135144
this.joinBuckets = new LazyLeakyBucket(

0 commit comments

Comments
 (0)