Skip to content

Commit 9bcb0b7

Browse files
Gnuxiejesopo
andauthored
Replace acceptInvitesFromGroup with acceptInvitesFromSpace. (#338)
Replace acceptInvitesFromGroup with acceptInvitesFromSpace. #125 #99 acceptInvitesFromGroup was implemented with an experimental api that was a precursor to spaces which was refereed to as either communities or groups. Support for communities/groups ended in Synapse 1.61.0 https://github.com/matrix-org/synapse/releases/tag/v1.61.0. To test we just edit the config dynamically which changes how the join room listener functions though idk, shouldn't we have just made a new mjolnir instance for this test, or changed the config before the test started somehow? Co-authored-by: jesopo <[email protected]>
1 parent f5a1a39 commit 9bcb0b7

File tree

5 files changed

+67
-10
lines changed

5 files changed

+67
-10
lines changed

config/default.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ dataPath: "/data/storage"
3434
# If true (the default), Mjolnir will only accept invites from users present in managementRoom.
3535
autojoinOnlyIfManager: true
3636

37-
# If `autojoinOnlyIfManager` is false, only the members in this group can invite
37+
# If `autojoinOnlyIfManager` is false, only the members in this space can invite
3838
# the bot to new rooms.
39-
acceptInvitesFromGroup: "+example:example.org"
39+
acceptInvitesFromSpace: "!example:example.org"
4040

4141
# Whether Mjolnir should report ignored invites to the management room (if autojoinOnlyIfManager is true).
4242
recordIgnoredInvites: false

config/harness.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ dataPath: "./test/harness/mjolnir-data/"
3232
# to new rooms.
3333
autojoinOnlyIfManager: true
3434

35-
# If `autojoinOnlyIfManager` is false, only the members in this group can invite
35+
# If `autojoinOnlyIfManager` is false, only the members in this space can invite
3636
# the bot to new rooms.
37-
acceptInvitesFromGroup: '+example:example.org'
37+
acceptInvitesFromSpace: '!example:example.org'
3838

3939
# If the bot is invited to a room and it won't accept the invite (due to the
4040
# conditions above), report it to the management room. Defaults to disabled (no

src/Mjolnir.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class Mjolnir {
123123
* @param {string} options.managementRoom The room to report ignored invitations to if `recordIgnoredInvites` is true.
124124
* @param {boolean} options.recordIgnoredInvites Whether to report invites that will be ignored to the `managementRoom`.
125125
* @param {boolean} options.autojoinOnlyIfManager Whether to only accept an invitation by a user present in the `managementRoom`.
126-
* @param {string} options.acceptInvitesFromGroup A group of users to accept invites from, ignores invites form users not in this group.
126+
* @param {string} options.acceptInvitesFromSpace A space of users to accept invites from, ignores invites form users not in this space.
127127
*/
128128
private static addJoinOnInviteListener(mjolnir: Mjolnir, client: MatrixClient, options: { [key: string]: any }) {
129129
client.on("room.invite", async (roomId: string, inviteEvent: any) => {
@@ -147,9 +147,18 @@ export class Mjolnir {
147147
const managers = await client.getJoinedRoomMembers(mjolnir.managementRoomId);
148148
if (!managers.includes(membershipEvent.sender)) return reportInvite(); // ignore invite
149149
} else {
150-
const groupMembers = await client.unstableApis.getGroupUsers(options.acceptInvitesFromGroup);
151-
const userIds = groupMembers.map(m => m.user_id);
152-
if (!userIds.includes(membershipEvent.sender)) return reportInvite(); // ignore invite
150+
const spaceId = await client.resolveRoom(options.acceptInvitesFromSpace);
151+
const spaceUserIds = await client.getJoinedRoomMembers(spaceId)
152+
.catch(async e => {
153+
if (e.body?.errcode === "M_FORBIDDEN") {
154+
await mjolnir.logMessage(LogLevel.ERROR, 'Mjolnir', `Mjolnir is not in the space configured for acceptInvitesFromSpace, did you invite it?`);
155+
await client.joinRoom(spaceId);
156+
return await client.getJoinedRoomMembers(spaceId);
157+
} else {
158+
return Promise.reject(e);
159+
}
160+
});
161+
if (!spaceUserIds.includes(membershipEvent.sender)) return reportInvite(); // ignore invite
153162
}
154163

155164
return client.joinRoom(roomId);

src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface IConfig {
3636
password: string;
3737
};
3838
dataPath: string;
39-
acceptInvitesFromGroup: string;
39+
acceptInvitesFromSpace: string;
4040
autojoinOnlyIfManager: boolean;
4141
recordIgnoredInvites: boolean;
4242
managementRoom: string;
@@ -115,7 +115,7 @@ const defaultConfig: IConfig = {
115115
password: "",
116116
},
117117
dataPath: "/data/storage",
118-
acceptInvitesFromGroup: '+example:example.org',
118+
acceptInvitesFromSpace: '!noop:example.org',
119119
autojoinOnlyIfManager: false,
120120
recordIgnoredInvites: false,
121121
managementRoom: "!noop:example.org",
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { MatrixClient } from "matrix-bot-sdk";
2+
import { Mjolnir } from "../../src/Mjolnir"
3+
import { newTestUser } from "./clientHelper";
4+
5+
describe("Test: Accept Invites From Space", function() {
6+
let client: MatrixClient|undefined;
7+
this.beforeEach(async function () {
8+
client = await newTestUser(this.config.homeserverUrl, { name: { contains: "spacee" }});
9+
await client.start();
10+
})
11+
this.afterEach(async function () {
12+
await client.stop();
13+
})
14+
it("Mjolnir should accept an invite from a user in a nominated Space", async function() {
15+
this.timeout(20000);
16+
17+
const mjolnir: Mjolnir = this.mjolnir!;
18+
const mjolnirUserId = await mjolnir.client.getUserId();
19+
20+
const space = await client.createSpace({
21+
name: "mjolnir space invite test",
22+
invites: [mjolnirUserId],
23+
isPublic: false
24+
});
25+
26+
await this.mjolnir.client.joinRoom(space.roomId);
27+
28+
// we're mutating a static object, which may affect other tests :(
29+
mjolnir.config.autojoinOnlyIfManager = false;
30+
mjolnir.config.acceptInvitesFromSpace = space.roomId;
31+
32+
const promise = new Promise(async resolve => {
33+
const newRoomId = await client.createRoom({ invite: [mjolnirUserId] });
34+
client.on("room.event", (roomId, event) => {
35+
if (
36+
roomId === newRoomId
37+
&& event.type === "m.room.member"
38+
&& event.sender === mjolnirUserId
39+
&& event.content?.membership === "join"
40+
) {
41+
resolve(null);
42+
}
43+
});
44+
});
45+
await promise;
46+
});
47+
});
48+

0 commit comments

Comments
 (0)