From b33723efb336239eaef6d55c79b4f95983250e3e Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Wed, 28 Oct 2020 19:03:25 +0100 Subject: [PATCH] Create Matrix room when a Team got created --- apps/participation/models.py | 10 ++++++ apps/registration/auth.py | 1 + apps/registration/fixtures/initial.json | 28 ++++++++-------- corres2math/matrix.py | 43 +++++++++++++++++++++++++ requirements.txt | 9 +++--- 5 files changed, 73 insertions(+), 18 deletions(-) create mode 100644 corres2math/matrix.py diff --git a/apps/participation/models.py b/apps/participation/models.py index 7fc9b67..03bbd58 100644 --- a/apps/participation/models.py +++ b/apps/participation/models.py @@ -2,6 +2,7 @@ import os import re from django.template.loader import render_to_string +from nio import RoomVisibility, RoomPreset from corres2math.lists import get_sympa_client from django.core.exceptions import ObjectDoesNotExist @@ -14,6 +15,8 @@ from django.utils.crypto import get_random_string from django.utils.text import format_lazy from django.utils.translation import gettext_lazy as _ +from corres2math.matrix import Matrix + class Team(models.Model): name = models.CharField( @@ -63,6 +66,13 @@ class Team(models.Model): self.access_code = get_random_string(6) self.create_mailing_list() + Matrix.create_room( + visibility=RoomVisibility.private, + alias=f"team-{self.trigram.lower()}", + topic=f"Discussion de l'équipe {self.name}", + preset=RoomPreset.private_chat, + ) + return super().save(*args, **kwargs) def get_absolute_url(self): diff --git a/apps/registration/auth.py b/apps/registration/auth.py index 4014de5..dd993e1 100644 --- a/apps/registration/auth.py +++ b/apps/registration/auth.py @@ -10,4 +10,5 @@ class CustomAuthUser(DjangoAuthUser): d = super().attributs() if self.user: d["matrix_username"] = f"corres2math_{self.user.pk}" + d["display_name"] = str(self.user.registration) return d diff --git a/apps/registration/fixtures/initial.json b/apps/registration/fixtures/initial.json index 9cd99b0..40afb61 100644 --- a/apps/registration/fixtures/initial.json +++ b/apps/registration/fixtures/initial.json @@ -1,17 +1,17 @@ [ -{ - "model": "cas_server.servicepattern", - "pk": 1, - "fields": { - "pos": 100, - "name": "Plateforme des Correspondances", - "pattern": "^https://correspondances-maths.fr:8448/.*$", - "user_field": "matrix_username", - "restrict_users": false, - "proxy": true, - "proxy_callback": true, - "single_log_out": true, - "single_log_out_callback": "" + { + "model": "cas_server.servicepattern", + "pk": 1, + "fields": { + "pos": 100, + "name": "Plateforme des Correspondances", + "pattern": "^https://correspondances-maths.fr:8448/.*$", + "user_field": "matrix_username", + "restrict_users": false, + "proxy": true, + "proxy_callback": true, + "single_log_out": true, + "single_log_out_callback": "" + } } -} ] diff --git a/corres2math/matrix.py b/corres2math/matrix.py new file mode 100644 index 0000000..94c7b67 --- /dev/null +++ b/corres2math/matrix.py @@ -0,0 +1,43 @@ +import asyncio +from typing import Any, Dict, Optional, Union + +from nio import AsyncClient, RoomCreateError, RoomCreateResponse, RoomInviteError, RoomInviteResponse, RoomPreset, \ + RoomVisibility + + +class Matrix: + @classmethod + def _get_client(cls) -> AsyncClient: + if hasattr(cls, "_client"): + return cls._client + + async def login(): + cls._client = AsyncClient("https://correspondances-maths.fr", "@corres2mathbot:correspondances-maths.fr") + await cls._client.login("toto1234") + return cls._client + return asyncio.get_event_loop().run_until_complete(login()) + + @classmethod + def create_room( + cls, + visibility: RoomVisibility = RoomVisibility.private, + alias: Optional[str] = None, + name: Optional[str] = None, + topic: Optional[str] = None, + room_version: Optional[str] = None, + federate: bool = True, + is_direct: bool = False, + preset: Optional[RoomPreset] = None, + invite=(), + initial_state=(), + power_level_override: Optional[Dict[str, Any]] = None, + ) -> Union[RoomCreateResponse, RoomCreateError]: + resp: Union[RoomCreateResponse, RoomCreateError] + + return asyncio.get_event_loop().run_until_complete(cls._get_client().room_create( + visibility, alias, name, topic, room_version, federate, is_direct, preset, invite, initial_state, + power_level_override)) + + @classmethod + def invite(cls, room_id: str, user_id: str) -> Union[RoomInviteResponse, RoomInviteError]: + return asyncio.get_event_loop().run_until_complete(cls._get_client().room_invite(room_id, user_id)) diff --git a/requirements.txt b/requirements.txt index 086f289..fe4e57a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,14 +3,15 @@ django-bootstrap-datepicker-plus django-cas-server django-crispy-forms django-extensions -django-filter -django-haystack +django-filter~=2.3.0 +django-haystack~=3.0 django-mailer django-polymorphic django-tables2 -djangorestframework +djangorestframework~=3.11.1 django-rest-polymorphic +matrix-nio ptpython -python-magic +python-magic~=0.4.18 gunicorn whoosh \ No newline at end of file