From 70ce0bb5da0ce93e96635a5609598c3a3a272e45 Mon Sep 17 00:00:00 2001 From: Jonathan Weth <git@jonathanweth.de> Date: Tue, 8 Mar 2022 18:49:23 +0100 Subject: [PATCH] Handle invitations by extra requests when rate limit doesn't allow so many invites --- aleksis/apps/matrix/models.py | 10 +++++++++- aleksis/apps/matrix/tests/test_matrix.py | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/aleksis/apps/matrix/models.py b/aleksis/apps/matrix/models.py index 4ab8df1..e7b529b 100644 --- a/aleksis/apps/matrix/models.py +++ b/aleksis/apps/matrix/models.py @@ -103,6 +103,7 @@ class MatrixRoom(ExtensiblePolymorphicModel): not get_site_preferences()["matrix__disambiguate_room_aliases"] or e.args[0].get("errcode") != "M_ROOM_IN_USE" ): + raise match = re.match(r"^(.*)-(\d+)$", alias) @@ -137,7 +138,14 @@ class MatrixRoom(ExtensiblePolymorphicModel): if creation_content: body["creation_content"] = creation_content - r = do_matrix_request("POST", "createRoom", body=body) + try: + r = do_matrix_request("POST", "createRoom", body=body) + except MatrixException as e: + if e.args[0].get("error") == "Cannot invite so many users at once": + del body["invite"] + r = do_matrix_request("POST", "createRoom", body=body) + else: + raise return r diff --git a/aleksis/apps/matrix/tests/test_matrix.py b/aleksis/apps/matrix/tests/test_matrix.py index 52f9a88..14fc9f7 100644 --- a/aleksis/apps/matrix/tests/test_matrix.py +++ b/aleksis/apps/matrix/tests/test_matrix.py @@ -478,3 +478,22 @@ def test_matrix_profile(): profile = MatrixProfile.from_person(p1) assert profile == MatrixProfile.from_person(p1) + + +def test_too_much_invites(matrix_bot_user): + get_site_preferences()["matrix__homeserver_ids"] = "matrix.aleksis.example.org" + + g = Group.objects.create(name="Test Room") + + persons = [] + for i in range(100): + + u = User.objects.create_user(f"test{i}", f"test{i}@example.org", f"test{i}") + p = Person.objects.create(first_name=f"Test {i}", last_name="Person", user=u) + persons.append(p) + + g.members.set(persons) + + room = MatrixRoom.from_group(g) + + room.sync_profiles() -- GitLab