kumify/msgio/gateways/matrix.py
Kumi ff70221553 refactor: clean up unused imports and improve code quality
Removed unnecessary imports across various modules to streamline the application's dependencies and improve loading times. Specific changes include the removal of unused Django model and admin imports in several apps, simplifying view imports by eliminating unutilized components, and cleaning up static CSS for better maintainability. Corrections were made to conditional expressions for clearer logic. The removal of the django.test.TestCase import in test files reflects a shift towards a different testing strategy or the current lack of tests. Exception handling has been made more explicit to avoid catching unintended exceptions, paving the way for more robust error handling and logging in the future. Additionally, a new CSS file was added for frontend enhancements, indicating ongoing UI/UX improvements.

These changes collectively aim to make the codebase more maintainable and efficient, reducing clutter and focusing on used functionalities. It's a step towards optimizing the application's performance and ensuring a clean, manageable codebase for future development.
2024-06-02 20:27:02 +02:00

43 lines
1.3 KiB
Python

from django.dispatch import receiver
import asyncio
from nio import AsyncClient
from dbsettings.functions import dbsettings
from ..signals import send_message
from ..models import GatewayUser
from ..helpers import run_filters
class MatrixDispatcher:
def __init__(self, username=None, password=None, homeserver=None):
self.username = username or dbsettings.MATRIX_USERNAME
self.password = password or dbsettings.MATRIX_PASSWORD
self.homeserver = homeserver or dbsettings.MATRIX_HOMESERVER
async def send(self, message, room_id):
client = AsyncClient(self.homeserver, self.username)
await client.login(self.password)
await client.join(room_id)
await client.room_send(
room_id=room_id,
message_type="m.room.message",
content={
"msgtype": "m.text",
"body": message
}
)
@receiver(send_message)
def matrix_sender(sender, **kwargs):
if kwargs["dispatcher"] == "matrix":
notification = kwargs["notification"]
settings = GatewayUser.objects.get(user=notification.recipient, gateway="matrix")
room_id = settings.gatewayusersetting_set.get(key="room_id").value
text = run_filters(notification)
asyncio.get_event_loop().run_until_complete(MatrixDispatcher().send(text, room_id))