Skip to content
Snippets Groups Projects
Verified Commit 29eb7740 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Implement mynit support

parent c758b8a8
No related branches found
No related tags found
No related merge requests found
# Copyright 2020, 2021 Dominik George <dominik.george@teckids.org> # Copyright 2020, 2021, 2022 Dominik George <dominik.george@teckids.org>
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
...@@ -16,13 +16,22 @@ import ntptime ...@@ -16,13 +16,22 @@ import ntptime
import re import re
import urequests import urequests
if "mynit" in locals:
_MYTRIX_MYNIT_SCHEMA = {
"homeserver": ("URL of Matrix homeserver", "str", None, "", "_mytrix_mynit_update"),
"matrix_id": ("Matrix ID", "str", None, "", "_mytrix_mynit_update"),
"access_token": ("Access token", "str", None, "", "_mytrix_mynit_update"),
}
mynit.register("matrix", "Matrix", _MYTRIX_MYNIT_SCHEMA)
del _MYTRIX_MYNIT_SCHEMA
class Matrix: class Matrix:
"""Implements Matrix client functionality and state keeping""" """Implements Matrix client functionality and state keeping"""
sync_limit = 1 sync_limit = 1
def __init__(self, homeserver, matrix_id, access_token, txn_id=None): def __init__(self, homeserver=None, matrix_id=None, access_token=None, txn_id=None):
"""Configure the Matrix client. """Configure the Matrix client.
The arguments homeserver, matrix_id and access_token configure access to the Matrix network. The arguments homeserver, matrix_id and access_token configure access to the Matrix network.
...@@ -32,9 +41,18 @@ class Matrix: ...@@ -32,9 +41,18 @@ class Matrix:
if defaults to the current UNIX timestamp, retrieved by an NTP request. For details on transaction if defaults to the current UNIX timestamp, retrieved by an NTP request. For details on transaction
IDs, see the Matrix documentation. IDs, see the Matrix documentation.
""" """
self.homeserver = homeserver
self.matrix_id = matrix_id if "mynit" in locals:
self.access_token = access_token if (homeserver, matrix_id, access_token) != (None, None, None):
raise TypeError("No arguments must be passed if mynit is used")
self._mynit_update()
else:
self.homeserver = homeserver
self.matrix_id = matrix_id
self.access_token = access_token
if not self.homeserver or not self.matrix_id or not self.access_token:
raise TypeError("homeserver, matrix_id and access_token must be set")
if txn_id is None: if txn_id is None:
self._txn_id = ntptime.time() self._txn_id = ntptime.time()
...@@ -43,6 +61,11 @@ class Matrix: ...@@ -43,6 +61,11 @@ class Matrix:
self._from_cache = {} self._from_cache = {}
def _mynit_update(self):
self.homeserver = mynit["matrix"]["homeserver"]
self.matrix_id = mynit["matrix"]["matrix_id"]
self.access_token = mynit["matrix"]["access_token"]
def _request(self, method, endpoint, query_data=None, json_data=None): def _request(self, method, endpoint, query_data=None, json_data=None):
"""Send an HTTP request using urequests.""" """Send an HTTP request using urequests."""
url = "%s/_matrix/client%s" % (self.homeserver, endpoint) url = "%s/_matrix/client%s" % (self.homeserver, endpoint)
...@@ -230,3 +253,11 @@ class Matrix: ...@@ -230,3 +253,11 @@ class Matrix:
return dm_rooms[mxid][0] return dm_rooms[mxid][0]
return self._create_dm_room(mxid, dm_rooms) return self._create_dm_room(mxid, dm_rooms)
_mytrix_instances = []
def _mytrix_mynit_update():
for i in _mytrix_instances:
i._mynit_update()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment