From e54d79d7c2c59c57277d829532d1f8100dd28ae3 Mon Sep 17 00:00:00 2001 From: agessaman Date: Mon, 2 Mar 2026 07:52:36 -0800 Subject: [PATCH] Refactor message handling in CompanionFrameServer to use a dedicated frame building method - Removed redundant code for building message frames based on message type. - Introduced a new method, _build_message_frame, to streamline the process of constructing message frames, improving code readability and maintainability. --- repeater/companion/frame_server.py | 62 +----------------------------- 1 file changed, 2 insertions(+), 60 deletions(-) diff --git a/repeater/companion/frame_server.py b/repeater/companion/frame_server.py index 63cea6e..4f2da3a 100644 --- a/repeater/companion/frame_server.py +++ b/repeater/companion/frame_server.py @@ -10,16 +10,9 @@ from __future__ import annotations import asyncio import logging -import struct from typing import Optional -from pymc_core.companion.constants import ( - RESP_CODE_CHANNEL_MSG_RECV, - RESP_CODE_CHANNEL_MSG_RECV_V3, - RESP_CODE_CONTACT_MSG_RECV, - RESP_CODE_CONTACT_MSG_RECV_V3, - RESP_CODE_NO_MORE_MESSAGES, -) +from pymc_core.companion.constants import RESP_CODE_NO_MORE_MESSAGES from pymc_core.companion.frame_server import CompanionFrameServer as _BaseFrameServer from pymc_core.companion.models import QueuedMessage @@ -103,58 +96,7 @@ class CompanionFrameServer(_BaseFrameServer): if msg is None: self._write_frame(bytes([RESP_CODE_NO_MORE_MESSAGES])) return - if msg.is_channel: - path_len_byte = msg.path_len if msg.path_len < 256 else 0xFF - txt_type = 0 - text_bytes = (msg.text or "").rstrip("\x00").encode("utf-8", errors="replace") - if self._app_target_ver >= 3: - frame = ( - bytes( - [ - RESP_CODE_CHANNEL_MSG_RECV_V3, - 0, - 0, - 0, - msg.channel_idx, - path_len_byte, - txt_type, - ] - ) - + struct.pack("= 6 else msg.sender_key.ljust(6, b"\x00") - ) - path_len_byte = msg.path_len if msg.path_len < 256 else 0xFF - text_bytes = msg.text.encode("utf-8", errors="replace") - if self._app_target_ver >= 3: - frame = ( - bytes([RESP_CODE_CONTACT_MSG_RECV_V3, 0, 0, 0]) - + prefix - + bytes([path_len_byte, msg.txt_type]) - + struct.pack(" dict: