Commit acc71476 authored by Lars Kruse's avatar Lars Kruse
Browse files

feat(matrix_chat): allow configuration of external Matrix user ID

Previously only the local Matrix user ID was usable.
parent 4141dc4a
from django import forms
from django.utils.translation import gettext as _
class MatrixChatGestaltSettingsForm(forms.Form):
matrix_id = forms.RegexField(
# see https://matrix.org/docs/spec/appendices#user-identifiers
r"^(@[\w.=\-/]+:[\w.-]+\.[a-zA-Z]{2,}|)$",
required=False,
strip=True,
label=_("Custom (external) matrix user ID"),
widget=forms.TextInput(),
help_text=_(
"Leave this field empty, if you want to use the local Matrix user ID. "
"A Matrix user ID looks like '@foo:example.org'."
),
)
from .settings import MATRIX_SETTINGS
import rules
@rules.predicate
def is_matrix_chat_enabled():
return MATRIX_SETTINGS.ENABLED
# Evaluating this rule returns False if the "matrix_chat" application is not installed
# (see INSTALLED_APPS). It returns True, if the application is installed and the corresponding
# setting is True.
rules.add_rule("is_matrix_chat_enabled", is_matrix_chat_enabled)
{% extends 'stadt/settings.html' %}
{% load i18n %}
{% block sidebar %}
{% with chapter='gestalt-matrix-chat' %}{{ block.super }}{% endwith %}
{% endblock %}
{% block content %}
<h2>{% trans "Chat Settings - Matrix" %}</h2>
<form method="post" class="form form-modern">
{% csrf_token %}
{% field form.matrix_id %}
<br/>
<button class="btn btn-sm btn-default" type="submit" name="action_send">
{% trans "Save settings" %}
</button>
</form>
{% endblock %}
from django.conf.urls import url
from .settings import MATRIX_SETTINGS
if MATRIX_SETTINGS.ENABLED:
from . import views
urlpatterns = [
url(
r"^stadt/settings/matrix-chat/$",
views.UpdateMatrixChatGestaltSettings.as_view(),
name="matrix-chat-settings",
),
]
else:
urlpatterns = []
from django.urls import reverse
from django.views.generic import FormView
from django.core.exceptions import ObjectDoesNotExist
from grouprise.core.views import PermissionMixin
from grouprise.features.groups.models import Group
from .forms import MatrixChatGestaltSettingsForm
from .models import MatrixChatGestaltSettings
class UpdateMatrixChatGestaltSettings(PermissionMixin, FormView):
permission_required = "gestalten.change"
form_class = MatrixChatGestaltSettingsForm
template_name = "matrix_chat/update_settings.html"
def get_group(self):
return Group.objects.filter(slug=self.request.GET.get("group")).first()
def get_permission_object(self):
return self.request.user.gestalt
def get_context_data(self, **kwargs):
group = self.get_group()
if group:
kwargs["group"] = group
return super().get_context_data(**kwargs)
def get_initial(self):
result = {}
try:
matrix_settings = self.request.user.gestalt.matrix_chat_settings
result["matrix_id"] = matrix_settings.matrix_id_override
except ObjectDoesNotExist:
pass
return result
@property
def success_url(self):
group = self.get_group()
slug = group.slug if group else ""
return "{}?group={}".format(reverse("matrix-chat-settings"), slug)
def form_valid(self, form):
gestalt = self.request.user.gestalt
matrix_settings = MatrixChatGestaltSettings.objects.get_or_create(
gestalt=gestalt
)[0]
matrix_settings.matrix_id_override = form.cleaned_data["matrix_id"]
matrix_settings.save()
gestalt.save()
return super().form_valid(form)
{% extends 'stadt/stadt.html' %}
{% load i18n %}
{% load rules %}
{% block title %}Einstellungen - {{ block.super }}{% endblock %}
{% block menu %}{% menu %}{% endblock %}
......@@ -26,6 +28,16 @@
<span class="nav-menu-item-label">E-Mail-Adressen</span>
</a>
</li>
{% test_rule "is_matrix_chat_enabled" as matrix_chat_enabled %}
{% if matrix_chat_enabled %}
<li>
<a href="{% url 'matrix-chat-settings' %}?group={{ group.slug }}"
class="nav-menu-item {% if chapter == 'gestalt-matrix-chat' %}nav-menu-item-active{% endif %}">
<i class="sg sg-fw sg-public-chat"></i>
<span class="nav-menu-item-label">{% trans 'Chat (Matrix)' %}</span>
</a>
</li>
{% endif %}
<li>
<a href="{% url 'account_change_password' %}?group={{ group.slug }}"
class="nav-menu-item {% if chapter == 'gestalt-password' %}nav-menu-item-active{% endif %}">
......@@ -36,7 +48,6 @@
{% endkwacro %}
{% kwacro group_links %}
{% load rules %}
{% has_perm 'groups.change' user group as can_edit_group_settings %}
{% if can_edit_group_settings %}
<li>
......
......@@ -392,6 +392,22 @@ msgstr "Gruppen suchen"
msgid "More groups"
msgstr "Weitere Gruppen"
msgid "Custom (external) matrix user ID"
msgstr "Eigene (externe) Matrix-Nutzer-ID"
msgid ""
"Leave this field empty, if you want to use the local Matrix user ID. A "
"Matrix user ID looks like '@foo:example.org'."
msgstr ""
"Lass das Feld leer, um die lokale Matrix-Nutzer-ID zu verwenden. Eine Matrix-"
"Nutzer-ID hat die Form '@foo:example.org'."
msgid "Chat Settings - Matrix"
msgstr "Chat-Einstellungen (Matrix)"
msgid "Save settings"
msgstr "Einstellungen speichern"
msgid "Membership"
msgstr "Mitgliedschaft"
......@@ -480,5 +496,8 @@ msgstr ""
msgid "Help about"
msgstr "Hilfe zu"
msgid "Chat (Matrix)"
msgstr "Chat (Matrix)"
msgid "all"
msgstr "alle"
......@@ -16,6 +16,7 @@ urlpatterns = [
path('', include('grouprise.features.gestalten.urls')),
path('', include('grouprise.features.gestalten.auth.urls')),
path('', include('grouprise.features.groups.urls')),
path('', include('grouprise.features.matrix_chat.urls')),
path('', include('grouprise.features.memberships.urls')),
path('', include('grouprise.features.polls.urls')),
path('', include('grouprise.features.rest_api.urls')),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment