1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-25 07:02:20 +00:00

Create Matrix room when a Team got created

This commit is contained in:
Yohann D'ANELLO 2020-10-28 19:03:25 +01:00
parent c9be07e45a
commit b33723efb3
5 changed files with 73 additions and 18 deletions

View File

@ -2,6 +2,7 @@ import os
import re import re
from django.template.loader import render_to_string from django.template.loader import render_to_string
from nio import RoomVisibility, RoomPreset
from corres2math.lists import get_sympa_client from corres2math.lists import get_sympa_client
from django.core.exceptions import ObjectDoesNotExist 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.text import format_lazy
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from corres2math.matrix import Matrix
class Team(models.Model): class Team(models.Model):
name = models.CharField( name = models.CharField(
@ -63,6 +66,13 @@ class Team(models.Model):
self.access_code = get_random_string(6) self.access_code = get_random_string(6)
self.create_mailing_list() 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) return super().save(*args, **kwargs)
def get_absolute_url(self): def get_absolute_url(self):

View File

@ -10,4 +10,5 @@ class CustomAuthUser(DjangoAuthUser):
d = super().attributs() d = super().attributs()
if self.user: if self.user:
d["matrix_username"] = f"corres2math_{self.user.pk}" d["matrix_username"] = f"corres2math_{self.user.pk}"
d["display_name"] = str(self.user.registration)
return d return d

View File

@ -1,5 +1,5 @@
[ [
{ {
"model": "cas_server.servicepattern", "model": "cas_server.servicepattern",
"pk": 1, "pk": 1,
"fields": { "fields": {
@ -13,5 +13,5 @@
"single_log_out": true, "single_log_out": true,
"single_log_out_callback": "" "single_log_out_callback": ""
} }
} }
] ]

43
corres2math/matrix.py Normal file
View File

@ -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))

View File

@ -3,14 +3,15 @@ django-bootstrap-datepicker-plus
django-cas-server django-cas-server
django-crispy-forms django-crispy-forms
django-extensions django-extensions
django-filter django-filter~=2.3.0
django-haystack django-haystack~=3.0
django-mailer django-mailer
django-polymorphic django-polymorphic
django-tables2 django-tables2
djangorestframework djangorestframework~=3.11.1
django-rest-polymorphic django-rest-polymorphic
matrix-nio
ptpython ptpython
python-magic python-magic~=0.4.18
gunicorn gunicorn
whoosh whoosh