From a35d962f8ca5f619d94a92933e50ed3dbde0a804 Mon Sep 17 00:00:00 2001 From: David Teller Date: Fri, 1 Jul 2022 16:19:01 +0200 Subject: [PATCH 01/14] Base support for MSC3840: Ignore invites This PR expects that most of the work will be done at higher level, where the user can decide to look at a list of ignored invites, un-ignore them individually, etc. Signed-off-by: David Teller --- src/client.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/client.ts b/src/client.ts index 3058bca8658..f5514f0f5d3 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3424,6 +3424,23 @@ export class MatrixClient extends TypedEventEmitter { + return this.setAccountData(IGNORED_INVITES_ACCOUNT_DATA_KEY, invites); + } + /** * Join a room. If you have already joined the room, this will no-op. * @param {string} roomIdOrAlias The room ID or room alias to join. @@ -9320,3 +9337,34 @@ export class MatrixClient extends TypedEventEmitter Date: Fri, 1 Jul 2022 17:09:43 +0200 Subject: [PATCH 02/14] WIP: Linting --- src/client.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/client.ts b/src/client.ts index f5514f0f5d3..85cef92765e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3435,7 +3435,7 @@ export class MatrixClient extends TypedEventEmitter { return this.setAccountData(IGNORED_INVITES_ACCOUNT_DATA_KEY, invites); @@ -9354,7 +9354,7 @@ export type IgnoredRoomInvites = { * as a Matrix timestamp. */ ts: number; -} +}; /** * Invites to ignore across all devices/sessions, as per MSC 3840. @@ -9367,4 +9367,4 @@ export type IgnoredInvites = { * A list of rooms the user does not wish to be invited to. */ ignored_rooms?: IgnoredRoomInvites[]; -}; \ No newline at end of file +}; From 78a731789d7ed648a50773d2117ae6c8797c58bf Mon Sep 17 00:00:00 2001 From: David Teller Date: Mon, 4 Jul 2022 07:28:33 +0200 Subject: [PATCH 03/14] WIP: Fixes --- src/client.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index 85cef92765e..78b0fa0867e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3429,8 +3429,8 @@ export class MatrixClient extends TypedEventEmitter Date: Wed, 6 Jul 2022 13:38:08 +0200 Subject: [PATCH 04/14] WIP: Moving event key to EventType --- src/@types/event.ts | 1 + src/client.ts | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/@types/event.ts b/src/@types/event.ts index dac2770ade3..3a68f5378d3 100644 --- a/src/@types/event.ts +++ b/src/@types/event.ts @@ -81,6 +81,7 @@ export enum EventType { PushRules = "m.push_rules", Direct = "m.direct", IgnoredUserList = "m.ignored_user_list", + IgnoredInvites = "org.matrix.msc3840.ignored_invites", // to_device events RoomKey = "m.room_key", diff --git a/src/client.ts b/src/client.ts index 78b0fa0867e..a1116b732d2 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3428,8 +3428,10 @@ export class MatrixClient extends TypedEventEmitter { - return this.setAccountData(IGNORED_INVITES_ACCOUNT_DATA_KEY, invites); + return this.setAccountData(EventType.IgnoredInvites, invites); } /** @@ -9338,8 +9340,6 @@ export class MatrixClient extends TypedEventEmitter Date: Wed, 6 Jul 2022 13:38:46 +0200 Subject: [PATCH 05/14] WIP: Adding some tests --- spec/unit/ignored-invites.spec.js | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 spec/unit/ignored-invites.spec.js diff --git a/spec/unit/ignored-invites.spec.js b/spec/unit/ignored-invites.spec.js new file mode 100644 index 00000000000..2d4b0f6348e --- /dev/null +++ b/spec/unit/ignored-invites.spec.js @@ -0,0 +1,48 @@ +import { MatrixEvent } from '../../src'; +import { TestClient } from '../TestClient'; + +describe('Store ignored invites', function() { + let client; + + beforeEach(function() { + client = new TestClient("foobar", "device"); + + // Mockup account data storage. + let dataStore = new Map(); + client.client.setAccountData = function (eventType, content) { + dataStore.set(eventType, content); + return Promise.resolve(); + } + client.client.getAccountData = function (eventType) { + let data = dataStore.get(eventType); + return new MatrixEvent({ + content: data + }); + }; + }); + + afterEach(function() { + client.stop(); + }); + + it('Stores ignored invites', async function() { + // Initially, the invite list should be empty but not `null`. + expect(await client.client.getIgnoredInvites()).toEqual({}); + + // Insert something, we should be able to recover it. + const SAMPLE = { + ignored_rooms: [ + { + room_id: "12345", + ts: Date.now(), + } + ] + }; + let promise = client.client.setIgnoredInvites(SAMPLE); + client.httpBackend.flush(); + await promise; + + // Check that it was (mock)stored on the client. + expect(await client.client.getIgnoredInvites()).toEqual(SAMPLE); + }); +}); From eee42b2afbe2583d8dee9f99d8206785817ba42e Mon Sep 17 00:00:00 2001 From: David Teller Date: Wed, 6 Jul 2022 13:44:59 +0200 Subject: [PATCH 06/14] WIP: Coverage? --- spec/unit/ignored-invites.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/ignored-invites.spec.js b/spec/unit/ignored-invites.spec.js index 2d4b0f6348e..4ee38b582ee 100644 --- a/spec/unit/ignored-invites.spec.js +++ b/spec/unit/ignored-invites.spec.js @@ -39,7 +39,7 @@ describe('Store ignored invites', function() { ] }; let promise = client.client.setIgnoredInvites(SAMPLE); - client.httpBackend.flush(); + await client.httpBackend.flush(); await promise; // Check that it was (mock)stored on the client. From 1564ad7c2e59a74da15b0ae42e6acee73de90040 Mon Sep 17 00:00:00 2001 From: David Teller Date: Wed, 6 Jul 2022 13:53:22 +0200 Subject: [PATCH 07/14] WIP: Linter --- spec/unit/ignored-invites.spec.js | 20 ++++++++++---------- src/client.ts | 4 +--- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/spec/unit/ignored-invites.spec.js b/spec/unit/ignored-invites.spec.js index 4ee38b582ee..2621d43a0d1 100644 --- a/spec/unit/ignored-invites.spec.js +++ b/spec/unit/ignored-invites.spec.js @@ -8,17 +8,17 @@ describe('Store ignored invites', function() { client = new TestClient("foobar", "device"); // Mockup account data storage. - let dataStore = new Map(); - client.client.setAccountData = function (eventType, content) { + const dataStore = new Map(); + client.client.setAccountData = function(eventType, content) { dataStore.set(eventType, content); return Promise.resolve(); - } - client.client.getAccountData = function (eventType) { - let data = dataStore.get(eventType); + }; + client.client.getAccountData = function(eventType) { + const data = dataStore.get(eventType); return new MatrixEvent({ - content: data + content: data, }); - }; + }; }); afterEach(function() { @@ -35,10 +35,10 @@ describe('Store ignored invites', function() { { room_id: "12345", ts: Date.now(), - } - ] + }, + ], }; - let promise = client.client.setIgnoredInvites(SAMPLE); + const promise = client.client.setIgnoredInvites(SAMPLE); await client.httpBackend.flush(); await promise; diff --git a/src/client.ts b/src/client.ts index a1116b732d2..fd680f684b1 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3429,9 +3429,7 @@ export class MatrixClient extends TypedEventEmitter Date: Thu, 7 Jul 2022 09:16:40 +0200 Subject: [PATCH 08/14] WIP: Applying feedback --- spec/unit/ignored-invites.spec.js | 48 ------------------------------- spec/unit/matrix-client.spec.ts | 35 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 48 deletions(-) delete mode 100644 spec/unit/ignored-invites.spec.js diff --git a/spec/unit/ignored-invites.spec.js b/spec/unit/ignored-invites.spec.js deleted file mode 100644 index 2621d43a0d1..00000000000 --- a/spec/unit/ignored-invites.spec.js +++ /dev/null @@ -1,48 +0,0 @@ -import { MatrixEvent } from '../../src'; -import { TestClient } from '../TestClient'; - -describe('Store ignored invites', function() { - let client; - - beforeEach(function() { - client = new TestClient("foobar", "device"); - - // Mockup account data storage. - const dataStore = new Map(); - client.client.setAccountData = function(eventType, content) { - dataStore.set(eventType, content); - return Promise.resolve(); - }; - client.client.getAccountData = function(eventType) { - const data = dataStore.get(eventType); - return new MatrixEvent({ - content: data, - }); - }; - }); - - afterEach(function() { - client.stop(); - }); - - it('Stores ignored invites', async function() { - // Initially, the invite list should be empty but not `null`. - expect(await client.client.getIgnoredInvites()).toEqual({}); - - // Insert something, we should be able to recover it. - const SAMPLE = { - ignored_rooms: [ - { - room_id: "12345", - ts: Date.now(), - }, - ], - }; - const promise = client.client.setIgnoredInvites(SAMPLE); - await client.httpBackend.flush(); - await promise; - - // Check that it was (mock)stored on the client. - expect(await client.client.getIgnoredInvites()).toEqual(SAMPLE); - }); -}); diff --git a/spec/unit/matrix-client.spec.ts b/spec/unit/matrix-client.spec.ts index fbe8c67d7e7..8563ec121a9 100644 --- a/spec/unit/matrix-client.spec.ts +++ b/spec/unit/matrix-client.spec.ts @@ -1297,4 +1297,39 @@ describe("MatrixClient", function() { expect(result!.aliases).toEqual(response.aliases); }); }); + + describe("ignoring invites", () => { + it('stores ignored invites', async function() { + // Mockup account data storage. + const dataStore = new Map(); + client.setAccountData = function(eventType, content) { + dataStore.set(eventType, content); + return Promise.resolve(); + }; + client.getAccountData = function(eventType) { + const data = dataStore.get(eventType); + return new MatrixEvent({ + content: data, + }); + }; + + // Initially, the invite list should be empty but not `null`. + expect(await client.getIgnoredInvites()).toEqual({}); + + // Insert something, we should be able to recover it. + const SAMPLE = { + ignored_rooms: [ + { + room_id: "12345", + ts: Date.now(), + }, + ], + }; + await client.setIgnoredInvites(SAMPLE); + + // Check that it was (mock)stored on the client. + expect(await client.getIgnoredInvites()).toEqual(SAMPLE); + }); + + }); }); From 96516bcd66010d45b7b017cc25a21f0190cffb3d Mon Sep 17 00:00:00 2001 From: David Teller Date: Thu, 7 Jul 2022 09:22:09 +0200 Subject: [PATCH 09/14] WIP: Lint --- spec/unit/matrix-client.spec.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/spec/unit/matrix-client.spec.ts b/spec/unit/matrix-client.spec.ts index 8563ec121a9..9df098504b2 100644 --- a/spec/unit/matrix-client.spec.ts +++ b/spec/unit/matrix-client.spec.ts @@ -1315,7 +1315,7 @@ describe("MatrixClient", function() { // Initially, the invite list should be empty but not `null`. expect(await client.getIgnoredInvites()).toEqual({}); - + // Insert something, we should be able to recover it. const SAMPLE = { ignored_rooms: [ @@ -1326,10 +1326,9 @@ describe("MatrixClient", function() { ], }; await client.setIgnoredInvites(SAMPLE); - + // Check that it was (mock)stored on the client. expect(await client.getIgnoredInvites()).toEqual(SAMPLE); }); - }); }); From 7143515f66d34e04662c51c9d0c326211dc2380f Mon Sep 17 00:00:00 2001 From: David Teller Date: Thu, 7 Jul 2022 18:21:12 +0100 Subject: [PATCH 10/14] WIP: Following feedback on MSC3840 --- spec/unit/matrix-client.spec.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/spec/unit/matrix-client.spec.ts b/spec/unit/matrix-client.spec.ts index 9df098504b2..3fc8cd5274f 100644 --- a/spec/unit/matrix-client.spec.ts +++ b/spec/unit/matrix-client.spec.ts @@ -1318,12 +1318,11 @@ describe("MatrixClient", function() { // Insert something, we should be able to recover it. const SAMPLE = { - ignored_rooms: [ - { - room_id: "12345", + ignored_rooms: { + "12345": { ts: Date.now(), }, - ], + }, }; await client.setIgnoredInvites(SAMPLE); From 7546df8dcb08c952fa77a7c9cce5fc3b5df9a555 Mon Sep 17 00:00:00 2001 From: David Teller Date: Thu, 7 Jul 2022 18:31:45 +0100 Subject: [PATCH 11/14] Update client.ts --- src/client.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/client.ts b/src/client.ts index fd680f684b1..19888296e37 100644 --- a/src/client.ts +++ b/src/client.ts @@ -9338,20 +9338,24 @@ export class MatrixClient extends TypedEventEmitter Date: Thu, 7 Jul 2022 19:08:19 +0100 Subject: [PATCH 12/14] WIP: Everything should now be in maps and properly readonly --- src/client.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/client.ts b/src/client.ts index 19888296e37..c56f73a1f9f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -9338,35 +9338,34 @@ export class MatrixClient extends TypedEventEmitter; }; From 188d123c6e87a8d9480fdd9c3e2fa724b63b82cd Mon Sep 17 00:00:00 2001 From: David Teller Date: Thu, 7 Jul 2022 19:15:53 +0100 Subject: [PATCH 13/14] WIP: Linter --- src/client.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index c56f73a1f9f..12000aff8ec 100644 --- a/src/client.ts +++ b/src/client.ts @@ -9355,7 +9355,7 @@ export interface IgnoreInviteMetadata { * A human-readable reason for ignoring this invite. */ readonly reason?: string; -}; +} /** * Invites to ignore across all devices/sessions, as per MSC 3840. @@ -9368,4 +9368,4 @@ export interface IgnoredInvites { * A list of rooms the user does not wish to be invited to. */ readonly ignored_rooms?: Record; -}; +} From 6b84eb0345764587faddda5ce8959a81994a88df Mon Sep 17 00:00:00 2001 From: David Teller Date: Thu, 7 Jul 2022 19:29:40 +0100 Subject: [PATCH 14/14] WIP: Linters --- src/client.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index 12000aff8ec..b35162b11e5 100644 --- a/src/client.ts +++ b/src/client.ts @@ -9365,7 +9365,7 @@ export interface IgnoreInviteMetadata { */ export interface IgnoredInvites { /** - * A list of rooms the user does not wish to be invited to. + * A map of room ids => metadata for rooms the user does not wish to be invited to. */ - readonly ignored_rooms?: Record; + readonly ignored_rooms?: Record; }