mirror of
				https://gitlab.com/animath/si/plateforme-corres2math.git
				synced 2025-11-04 10:22:15 +01:00 
			
		
		
		
	Add script to fix channel permissions
This commit is contained in:
		
							
								
								
									
										0
									
								
								apps/participation/management/commands/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								apps/participation/management/commands/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
from asgiref.sync import async_to_sync
 | 
			
		||||
from nio import RoomPreset
 | 
			
		||||
 | 
			
		||||
from corres2math.matrix import Matrix, RoomVisibility
 | 
			
		||||
from django.core.management import BaseCommand
 | 
			
		||||
from registration.models import AdminRegistration
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Command(BaseCommand):
 | 
			
		||||
    def handle(self, *args, **options):
 | 
			
		||||
        Matrix.set_display_name("Bot des Correspondances")
 | 
			
		||||
 | 
			
		||||
        if not async_to_sync(Matrix.resolve_room_alias)("#faq:correspondances-maths.fr"):
 | 
			
		||||
            Matrix.create_room(
 | 
			
		||||
                visibility=RoomVisibility.public,
 | 
			
		||||
                alias="faq",
 | 
			
		||||
                name="FAQ",
 | 
			
		||||
                topic="Posez toutes vos questions ici !",
 | 
			
		||||
                federate=False,
 | 
			
		||||
                preset=RoomPreset.public_chat,
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        for admin in AdminRegistration.objects.all():
 | 
			
		||||
            Matrix.set_room_power_level("#faq:correspondances-maths.fr",
 | 
			
		||||
                                        f"@{admin.matrix_username}:correspondances-maths.fr", 95)
 | 
			
		||||
@@ -1,9 +1,7 @@
 | 
			
		||||
import os
 | 
			
		||||
from typing import Any, Dict, Optional, Union
 | 
			
		||||
 | 
			
		||||
from asgiref.sync import async_to_sync
 | 
			
		||||
from nio import AsyncClient, RoomCreateError, RoomCreateResponse, RoomKickResponse, RoomInviteError,\
 | 
			
		||||
    RoomInviteResponse, RoomPreset, RoomResolveAliasResponse, RoomVisibility
 | 
			
		||||
from nio import *
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Matrix:
 | 
			
		||||
@@ -23,13 +21,21 @@ class Matrix:
 | 
			
		||||
                client.access_token = cls._token
 | 
			
		||||
                return client
 | 
			
		||||
 | 
			
		||||
        await client.login(password="toto1234", device_name="Plateforme")
 | 
			
		||||
        await client.login(password=os.getenv("SYNAPSE_PASSWORD"), device_name="Plateforme")
 | 
			
		||||
        cls._token = client.access_token
 | 
			
		||||
        cls._device_id = client.device_id
 | 
			
		||||
        with open(".matrix_token", "w") as f:
 | 
			
		||||
            f.write(cls._token)
 | 
			
		||||
        with open(".matrix_device", "w") as f:
 | 
			
		||||
            f.write(cls._device_id)
 | 
			
		||||
        return client
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    @async_to_sync
 | 
			
		||||
    async def set_display_name(cls, name: str) -> Union[ProfileSetDisplayNameResponse, ProfileSetDisplayNameError]:
 | 
			
		||||
        client = await cls._get_client()
 | 
			
		||||
        return await client.set_displayname(name)
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    @async_to_sync
 | 
			
		||||
    async def create_room(
 | 
			
		||||
@@ -54,9 +60,11 @@ class Matrix:
 | 
			
		||||
            power_level_override)
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    async def resolve_room_alias(cls, room_alias: str) -> str:
 | 
			
		||||
    async def resolve_room_alias(cls, room_alias: str) -> Optional[str]:
 | 
			
		||||
        client = await cls._get_client()
 | 
			
		||||
        resp: RoomResolveAliasResponse = await client.room_resolve_alias(room_alias)
 | 
			
		||||
        if isinstance(resp, RoomResolveAliasError):
 | 
			
		||||
            return None
 | 
			
		||||
        return resp.room_id
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
@@ -74,3 +82,17 @@ class Matrix:
 | 
			
		||||
        if room_id.startswith("#"):
 | 
			
		||||
            room_id = await cls.resolve_room_alias(room_id)
 | 
			
		||||
        return await client.room_kick(room_id, user_id, reason)
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    @async_to_sync
 | 
			
		||||
    async def set_room_power_level(cls, room_id: str, user_id: str, power_level: int)\
 | 
			
		||||
            -> Union[RoomPutStateResponse, RoomPutStateError]:
 | 
			
		||||
        client = await cls._get_client()
 | 
			
		||||
        if room_id.startswith("#"):
 | 
			
		||||
            room_id = await cls.resolve_room_alias(room_id)
 | 
			
		||||
        resp = await client.room_get_state_event(room_id, "m.room.power_levels")
 | 
			
		||||
        content = resp.content
 | 
			
		||||
        content["users"][user_id] = power_level
 | 
			
		||||
        print(content)
 | 
			
		||||
        print(resp.state_key)
 | 
			
		||||
        return await client.room_put_state(room_id, "m.room.power_levels", content=content, state_key=resp.state_key)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user