|
| 1 | +"""Message utilities for deduplication and component handling.""" |
| 2 | + |
| 3 | +import hashlib |
| 4 | +from collections.abc import Iterable |
| 5 | + |
| 6 | +from astrbot.core.message.components import BaseMessageComponent, File, Image |
| 7 | + |
| 8 | +_MAX_RAW_TEXT_FINGERPRINT_LEN = 256 |
| 9 | + |
| 10 | + |
| 11 | +def build_component_dedup_signature( |
| 12 | + components: Iterable[BaseMessageComponent], |
| 13 | +) -> str: |
| 14 | + """Build a deduplication signature from message components. |
| 15 | +
|
| 16 | + This function extracts unique identifiers from Image and File components |
| 17 | + and creates a hash-based signature for deduplication purposes. |
| 18 | +
|
| 19 | + Args: |
| 20 | + components: An iterable of message components to analyze. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + A SHA1 hash (16 hex characters) representing the component signatures, |
| 24 | + or an empty string if no valid components are found. |
| 25 | + """ |
| 26 | + parts: list[str] = [] |
| 27 | + for component in components: |
| 28 | + if isinstance(component, Image): |
| 29 | + # Image can have url, file, or file_unique |
| 30 | + ref = component.url or component.file or component.file_unique or "" |
| 31 | + if ref: |
| 32 | + parts.append(f"img:{ref}") |
| 33 | + elif isinstance(component, File): |
| 34 | + # File can have url, file (via property), or name |
| 35 | + ref = component.url or component.file or component.name or "" |
| 36 | + if ref: |
| 37 | + parts.append(f"file:{ref}") |
| 38 | + # Future component types can be added here |
| 39 | + |
| 40 | + if not parts: |
| 41 | + return "" |
| 42 | + |
| 43 | + payload = "|".join(parts) |
| 44 | + return hashlib.sha1(payload.encode("utf-8")).hexdigest()[:16] |
| 45 | + |
| 46 | + |
| 47 | +def build_sender_content_dedup_key(content: str, sender_id: str) -> str | None: |
| 48 | + """Build a sender+content hash key for short-window deduplication.""" |
| 49 | + if not (content and sender_id): |
| 50 | + return None |
| 51 | + content_hash = hashlib.sha1(content.encode("utf-8")).hexdigest()[:16] |
| 52 | + return f"{sender_id}:{content_hash}" |
| 53 | + |
| 54 | + |
| 55 | +def build_content_dedup_key( |
| 56 | + *, |
| 57 | + platform_id: str, |
| 58 | + unified_msg_origin: str, |
| 59 | + sender_id: str, |
| 60 | + text: str, |
| 61 | + components: Iterable[BaseMessageComponent], |
| 62 | +) -> str: |
| 63 | + """Build a content fingerprint key for event deduplication.""" |
| 64 | + msg_text = str(text or "").strip() |
| 65 | + if len(msg_text) <= _MAX_RAW_TEXT_FINGERPRINT_LEN: |
| 66 | + msg_sig = msg_text |
| 67 | + else: |
| 68 | + msg_hash = hashlib.sha1(msg_text.encode("utf-8")).hexdigest()[:16] |
| 69 | + msg_sig = f"h:{len(msg_text)}:{msg_hash}" |
| 70 | + |
| 71 | + attach_sig = build_component_dedup_signature(components) |
| 72 | + return "|".join( |
| 73 | + [ |
| 74 | + "content", |
| 75 | + str(platform_id or ""), |
| 76 | + str(unified_msg_origin or ""), |
| 77 | + str(sender_id or ""), |
| 78 | + msg_sig, |
| 79 | + attach_sig, |
| 80 | + ] |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def build_message_id_dedup_key( |
| 85 | + *, |
| 86 | + platform_id: str, |
| 87 | + unified_msg_origin: str, |
| 88 | + message_id: str, |
| 89 | +) -> str | None: |
| 90 | + """Build a message_id fingerprint key for event deduplication.""" |
| 91 | + normalized_message_id = str(message_id or "") |
| 92 | + if not normalized_message_id: |
| 93 | + return None |
| 94 | + return "|".join( |
| 95 | + [ |
| 96 | + "message_id", |
| 97 | + str(platform_id or ""), |
| 98 | + str(unified_msg_origin or ""), |
| 99 | + normalized_message_id, |
| 100 | + ] |
| 101 | + ) |
0 commit comments