Add Django views for the Matrix appservice transactions

matrix-profile-room-messages
Jason Robinson 2020-12-23 21:59:35 +02:00
rodzic 449089a59e
commit 524e5efbfd
10 zmienionych plików z 61 dodań i 6 usunięć

Wyświetl plik

@ -12,6 +12,9 @@
* Added configuration for a Matrix appservice to be registered with a homeserver.
* Added a Django view to push incoming Matrix appservice transactions into the congigured
payload processing function.
## [0.21.0] - 2020-12-20
### Added

Wyświetl plik

@ -113,10 +113,7 @@ Appservice
..........
To generate the appservice registration file you must ensure you've added the relevant
configuration (see :ref:`usage-configuration`), at minimum providing the following:
* ``base_url``
* ``matrix_config_function``
configuration (see :ref:`usage-configuration`).
Then launch a Django shell inside your project and run the following:

Wyświetl plik

@ -1,7 +1,10 @@
# noinspection PyPackageRequirements
from django.conf.urls import url
# noinspection PyPackageRequirements
from django.urls import include
urlpatterns = [
url(r'', include("federation.hostmeta.django.urls")),
url(r'ap/', include("federation.entities.activitypub.django.urls")),
url(r'^matrix/', include("federation.entities.matrix.django.urls")),
]

Wyświetl plik

@ -0,0 +1,14 @@
# noinspection PyPackageRequirements
from django.conf.urls import url
# noinspection PyPackageRequirements
from django.views.decorators.csrf import csrf_exempt
from federation.entities.matrix.django.views import MatrixASTransactionsView
urlpatterns = [
url(
regex=r"transactions/(?P<txn_id>[\w-]+)$",
view=csrf_exempt(MatrixASTransactionsView.as_view()),
name="matrix-as-transactions",
),
]

Wyświetl plik

@ -0,0 +1,38 @@
import logging
# noinspection PyPackageRequirements
from django.http import JsonResponse
# noinspection PyPackageRequirements
from django.views import View
from federation.utils.django import get_function_from_config
from federation.utils.matrix import get_matrix_configuration
logger = logging.getLogger("federation")
class MatrixASBaseView(View):
def dispatch(self, request, *args, **kwargs):
token = request.GET.get("access_token")
if not token:
return JsonResponse({"error": "M_FORBIDDEN"}, content_type='application/json', status=403)
matrix_config = get_matrix_configuration()
if token != matrix_config["appservice"]["token"]:
return JsonResponse({"error": "M_FORBIDDEN"}, content_type='application/json', status=403)
return super().dispatch(request, *args, **kwargs)
class MatrixASTransactionsView(MatrixASBaseView):
# noinspection PyUnusedLocal,PyMethodMayBeStatic
def put(self, request, *args, **kwargs):
# Inject the transaction ID to the request as part of the meta items
request.META["matrix_transaction_id"] = kwargs.get("txn_id")
process_payload_function = get_function_from_config('process_payload_function')
result = process_payload_function(request)
if result:
return JsonResponse({}, content_type='application/json', status=200)
else:
return JsonResponse({"error": "M_UNKNOWN"}, content_type='application/json', status=400)

Wyświetl plik

@ -20,7 +20,7 @@ def get_registration_config() -> Dict:
return {
"id": matrix_config["appservice"]["id"],
"url": f"{config['base_url']}/matrix/appservice",
"url": f"{config['base_url']}/matrix",
"as_token": matrix_config["appservice"]["token"],
"hs_token": matrix_config["appservice"]["token"],
"sender_localpart": matrix_config["appservice"]["sender_localpart"],

Wyświetl plik

@ -5,7 +5,7 @@ def test_get_registration():
config = get_registration_config()
assert config == {
"id": "uniqueid",
"url": "https://example.com/matrix/appservice",
"url": "https://example.com/matrix",
"as_token": "secret_token",
"hs_token": "secret_token",
"sender_localpart": "_myawesomeapp",