Skip to content
Snippets Groups Projects
model_extensions.py 1.18 KiB
Newer Older
from typing import Optional, Union

from django.utils.translation import gettext_lazy as _

from celery.result import AsyncResult

from aleksis.apps.matrix.models import MatrixRoom
from aleksis.apps.matrix.tasks import use_group_in_matrix
from aleksis.core.models import Group


@Group.method
def use_in_matrix(self, sync=False) -> Union[MatrixRoom, AsyncResult]:
    """Create and sync a room for this group in Matrix."""
    if sync:
        return self._use_in_matrix()
    else:
        return use_group_in_matrix.delay(self.pk)


@Group.method
def _use_in_matrix(self):
    """Create and sync a room for this group in Matrix."""
    room = MatrixRoom.from_group(self)
    room.sync()
    return room


@Group.property_
def matrix_alias(self) -> Optional[str]:
    """Return the alias of the group's room in Matrix."""
    if hasattr(self, "matrix_room"):
        return self.matrix_room.alias
    return None


@Group.property_
def matrix_room_id(self) -> Optional[str]:
    """Return the ID of the group's room in Matrix."""
    if hasattr(self, "matrix_room"):
        return self.matrix_room.room_id
    return None


Group.add_permission("view_matrixroom", _("Can view matrix room of a group"))