From d4715aebefb71567d7684f710f8653fdff1e18e4 Mon Sep 17 00:00:00 2001 From: YAN Wenkun Date: Fri, 25 Jul 2025 05:59:46 +0800 Subject: [PATCH] Migrate matrix-client to matrix-nio (#2025) --- glob/share_3rdparty.py | 78 ++++++++++++++++++++++++++++++++++-------- pyproject.toml | 2 +- requirements.txt | 2 +- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/glob/share_3rdparty.py b/glob/share_3rdparty.py index c6cfcb1e..3bab1682 100644 --- a/glob/share_3rdparty.py +++ b/glob/share_3rdparty.py @@ -335,8 +335,8 @@ async def share_art(request): content_type = assetFileType try: - from matrix_client.api import MatrixHttpApi - from matrix_client.client import MatrixClient + from nio import AsyncClient, LoginResponse, RoomSendResponse, UploadResponse, RoomMessageText, RoomMessageMedia + import asyncio homeserver = 'matrix.org' if matrix_auth: @@ -345,20 +345,35 @@ async def share_art(request): if not homeserver.startswith("https://"): homeserver = "https://" + homeserver - client = MatrixClient(homeserver) - try: - token = client.login(username=matrix_auth['username'], password=matrix_auth['password']) - if not token: - return web.json_response({"error": "Invalid Matrix credentials."}, content_type='application/json', status=400) - except: + client = AsyncClient(homeserver, matrix_auth['username']) + + # Login + login_resp = await client.login(matrix_auth['password']) + if not isinstance(login_resp, LoginResponse) or not login_resp.access_token: + await client.close() return web.json_response({"error": "Invalid Matrix credentials."}, content_type='application/json', status=400) - matrix = MatrixHttpApi(homeserver, token=token) + # Upload asset with open(asset_filepath, 'rb') as f: - mxc_url = matrix.media_upload(f.read(), content_type, filename=filename)['content_uri'] + upload_resp, _maybe_keys = await client.upload(f, content_type=content_type, filename=filename) + asset_data = f.seek(0) or f.read() # get size for info below + if not isinstance(upload_resp, UploadResponse) or not upload_resp.content_uri: + await client.close() + return web.json_response({"error": "Failed to upload asset to Matrix."}, content_type='application/json', status=500) + mxc_url = upload_resp.content_uri - workflow_json_mxc_url = matrix.media_upload(prompt['workflow'], 'application/json', filename='workflow.json')['content_uri'] + # Upload workflow JSON + import io + workflow_json_bytes = json.dumps(prompt['workflow']).encode('utf-8') + workflow_io = io.BytesIO(workflow_json_bytes) + upload_workflow_resp, _maybe_keys = await client.upload(workflow_io, content_type='application/json', filename='workflow.json') + workflow_io.seek(0) + if not isinstance(upload_workflow_resp, UploadResponse) or not upload_workflow_resp.content_uri: + await client.close() + return web.json_response({"error": "Failed to upload workflow to Matrix."}, content_type='application/json', status=500) + workflow_json_mxc_url = upload_workflow_resp.content_uri + # Send text message text_content = "" if title: text_content += f"{title}\n" @@ -366,9 +381,44 @@ async def share_art(request): text_content += f"{description}\n" if credits: text_content += f"\ncredits: {credits}\n" - matrix.send_message(comfyui_share_room_id, text_content) - matrix.send_content(comfyui_share_room_id, mxc_url, filename, 'm.image') - matrix.send_content(comfyui_share_room_id, workflow_json_mxc_url, 'workflow.json', 'm.file') + await client.room_send( + room_id=comfyui_share_room_id, + message_type="m.room.message", + content={"msgtype": "m.text", "body": text_content} + ) + + # Send image + await client.room_send( + room_id=comfyui_share_room_id, + message_type="m.room.message", + content={ + "msgtype": "m.image", + "body": filename, + "url": mxc_url, + "info": { + "mimetype": content_type, + "size": len(asset_data) + } + } + ) + + # Send workflow JSON file + await client.room_send( + room_id=comfyui_share_room_id, + message_type="m.room.message", + content={ + "msgtype": "m.file", + "body": "workflow.json", + "url": workflow_json_mxc_url, + "info": { + "mimetype": "application/json", + "size": len(workflow_json_bytes) + } + } + ) + + await client.close() + except: import traceback traceback.print_exc() diff --git a/pyproject.toml b/pyproject.toml index 1e7e6831..7ff9f75d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ name = "comfyui-manager" description = "ComfyUI-Manager provides features to install and manage custom nodes for ComfyUI, as well as various functionalities to assist with ComfyUI." version = "3.34.5" license = { file = "LICENSE.txt" } -dependencies = ["GitPython", "PyGithub", "matrix-client==0.4.0", "transformers", "huggingface-hub>0.20", "typer", "rich", "typing-extensions", "toml", "uv", "chardet"] +dependencies = ["GitPython", "PyGithub", "matrix-nio", "transformers", "huggingface-hub>0.20", "typer", "rich", "typing-extensions", "toml", "uv", "chardet"] [project.urls] Repository = "https://github.com/ltdrdata/ComfyUI-Manager" diff --git a/requirements.txt b/requirements.txt index 38724e16..08372455 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ GitPython PyGithub -matrix-client==0.4.0 +matrix-nio transformers huggingface-hub>0.20 typer