shellzapy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- shellzapy-0.1.0.dist-info/METADATA +112 -0
- shellzapy-0.1.0.dist-info/RECORD +345 -0
- shellzapy-0.1.0.dist-info/WHEEL +4 -0
- shellzapy-0.1.0.dist-info/licenses/LICENSE +21 -0
- shellzapy-0.1.0.dist-info/licenses/NOTICE +9 -0
- zapy/__init__.py +58 -0
- zapy/_appstate/__init__.py +59 -0
- zapy/_appstate/constants.py +82 -0
- zapy/_appstate/crypto/__init__.py +17 -0
- zapy/_appstate/crypto/app_state_crypto.py +401 -0
- zapy/_appstate/parsers/__init__.py +23 -0
- zapy/_appstate/parsers/encoding.py +157 -0
- zapy/_appstate/parsers/response_parser.py +135 -0
- zapy/_appstate/sync/__init__.py +11 -0
- zapy/_appstate/sync/sync_client.py +1559 -0
- zapy/_appstate/types.py +137 -0
- zapy/_appstate/utils.py +100 -0
- zapy/_auth/__init__.py +3 -0
- zapy/_auth/auth_client.py +618 -0
- zapy/_auth/credentials_flow.py +290 -0
- zapy/_auth/pairing/__init__.py +3 -0
- zapy/_auth/pairing/companion_host.py +239 -0
- zapy/_auth/pairing/flow.py +808 -0
- zapy/_auth/pairing/pairing_code_crypto.py +143 -0
- zapy/_auth/pairing/qr_flow.py +123 -0
- zapy/_auth/pairing/shortcake_crypto.py +122 -0
- zapy/_auth/pairing/shortcake_flow.py +393 -0
- zapy/_auth/types.py +110 -0
- zapy/_client/__init__.py +26 -0
- zapy/_client/client.py +972 -0
- zapy/_client/connection/__init__.py +6 -0
- zapy/_client/connection/manager.py +525 -0
- zapy/_client/connection/receipt_queue.py +74 -0
- zapy/_client/coordinators/__init__.py +81 -0
- zapy/_client/coordinators/ab_props.py +260 -0
- zapy/_client/coordinators/app_state_mutation.py +2538 -0
- zapy/_client/coordinators/bot.py +830 -0
- zapy/_client/coordinators/broadcast_list.py +101 -0
- zapy/_client/coordinators/business.py +247 -0
- zapy/_client/coordinators/email.py +158 -0
- zapy/_client/coordinators/group.py +1478 -0
- zapy/_client/coordinators/incoming_node.py +758 -0
- zapy/_client/coordinators/low_level.py +119 -0
- zapy/_client/coordinators/message.py +1153 -0
- zapy/_client/coordinators/message_dispatch.py +1637 -0
- zapy/_client/coordinators/newsletter.py +110 -0
- zapy/_client/coordinators/offline_resume.py +271 -0
- zapy/_client/coordinators/passive_tasks.py +447 -0
- zapy/_client/coordinators/presence.py +103 -0
- zapy/_client/coordinators/privacy.py +623 -0
- zapy/_client/coordinators/profile.py +1024 -0
- zapy/_client/coordinators/retry.py +1878 -0
- zapy/_client/coordinators/status.py +132 -0
- zapy/_client/coordinators/stream_control.py +260 -0
- zapy/_client/coordinators/trusted_contact_token.py +417 -0
- zapy/_client/events/__init__.py +266 -0
- zapy/_client/events/abprops.py +58 -0
- zapy/_client/events/appstate_mutation.py +248 -0
- zapy/_client/events/business.py +565 -0
- zapy/_client/events/call.py +144 -0
- zapy/_client/events/chatstate.py +41 -0
- zapy/_client/events/devices.py +122 -0
- zapy/_client/events/dirty.py +384 -0
- zapy/_client/events/group.py +655 -0
- zapy/_client/events/identity.py +40 -0
- zapy/_client/events/incoming.py +1079 -0
- zapy/_client/events/mex_notification.py +317 -0
- zapy/_client/events/offline.py +123 -0
- zapy/_client/events/picture.py +67 -0
- zapy/_client/events/presence.py +74 -0
- zapy/_client/events/privacy.py +187 -0
- zapy/_client/events/privacy_token.py +72 -0
- zapy/_client/events/receipt.py +109 -0
- zapy/_client/events/registration.py +70 -0
- zapy/_client/events/username.py +76 -0
- zapy/_client/factory.py +2020 -0
- zapy/_client/media.py +829 -0
- zapy/_client/newsletter/__init__.py +90 -0
- zapy/_client/newsletter/admin.py +323 -0
- zapy/_client/newsletter/content.py +499 -0
- zapy/_client/newsletter/discovery.py +233 -0
- zapy/_client/newsletter/messaging.py +396 -0
- zapy/_client/newsletter/mex.py +99 -0
- zapy/_client/newsletter/parse.py +406 -0
- zapy/_client/newsletter/types.py +393 -0
- zapy/_client/persistence/__init__.py +89 -0
- zapy/_client/persistence/companion_host.py +133 -0
- zapy/_client/persistence/ephemeral_setting.py +245 -0
- zapy/_client/persistence/group_history.py +361 -0
- zapy/_client/persistence/history_blob.py +169 -0
- zapy/_client/persistence/history_sync.py +646 -0
- zapy/_client/persistence/mailbox.py +232 -0
- zapy/_client/persistence/write_behind_persistence.py +368 -0
- zapy/_client/plugins/__init__.py +23 -0
- zapy/_client/plugins/define.py +44 -0
- zapy/_client/plugins/install.py +254 -0
- zapy/_client/plugins/types.py +64 -0
- zapy/_client/tokens/__init__.py +21 -0
- zapy/_client/tokens/cs_token.py +47 -0
- zapy/_client/tokens/tc_token.py +43 -0
- zapy/_client/types.py +988 -0
- zapy/_crypto/__init__.py +65 -0
- zapy/_crypto/core/__init__.py +62 -0
- zapy/_crypto/core/hkdf.py +41 -0
- zapy/_crypto/core/keys.py +51 -0
- zapy/_crypto/core/nonce.py +25 -0
- zapy/_crypto/core/primitives.py +179 -0
- zapy/_crypto/core/random.py +32 -0
- zapy/_crypto/core/xeddsa.py +72 -0
- zapy/_crypto/curves/__init__.py +3 -0
- zapy/_crypto/curves/constants.py +10 -0
- zapy/_crypto/curves/ed25519.py +61 -0
- zapy/_crypto/curves/types.py +18 -0
- zapy/_crypto/curves/x25519.py +117 -0
- zapy/_crypto/math/__init__.py +3 -0
- zapy/_crypto/math/constants.py +35 -0
- zapy/_crypto/math/edwards.py +174 -0
- zapy/_crypto/math/fe.py +120 -0
- zapy/_crypto/math/le.py +13 -0
- zapy/_crypto/math/mod.py +15 -0
- zapy/_crypto/math/prime.py +11 -0
- zapy/_crypto/math/types.py +25 -0
- zapy/_crypto/native_backend.py +40 -0
- zapy/_infra/__init__.py +3 -0
- zapy/_infra/log.py +142 -0
- zapy/_infra/perf.py +77 -0
- zapy/_media/__init__.py +64 -0
- zapy/_media/constants.py +133 -0
- zapy/_media/container.py +110 -0
- zapy/_media/crypto/__init__.py +19 -0
- zapy/_media/crypto/crypto.py +633 -0
- zapy/_media/crypto/media_retry.py +107 -0
- zapy/_media/processor.py +100 -0
- zapy/_media/sticker/__init__.py +8 -0
- zapy/_media/sticker/pack.py +188 -0
- zapy/_media/transfer/__init__.py +15 -0
- zapy/_media/transfer/client.py +635 -0
- zapy/_media/transfer/conn.py +48 -0
- zapy/_media/types.py +169 -0
- zapy/_message/__init__.py +121 -0
- zapy/_message/addons/__init__.py +5 -0
- zapy/_message/addons/link_preview/__init__.py +54 -0
- zapy/_message/addons/link_preview/builder.py +56 -0
- zapy/_message/addons/link_preview/detect.py +72 -0
- zapy/_message/addons/link_preview/fetcher.py +553 -0
- zapy/_message/addons/link_preview/types.py +100 -0
- zapy/_message/client.py +486 -0
- zapy/_message/context_info.py +424 -0
- zapy/_message/crypto/__init__.py +71 -0
- zapy/_message/crypto/addon_crypto.py +656 -0
- zapy/_message/crypto/icdc.py +138 -0
- zapy/_message/crypto/phash.py +87 -0
- zapy/_message/crypto/reporting_token.py +498 -0
- zapy/_message/crypto/use_case_secret.py +113 -0
- zapy/_message/encode/__init__.py +84 -0
- zapy/_message/encode/content.py +636 -0
- zapy/_message/encode/device_sent.py +88 -0
- zapy/_message/encode/media_payload.py +122 -0
- zapy/_message/encode/padding.py +27 -0
- zapy/_message/kinds/__init__.py +1 -0
- zapy/_message/kinds/bot.py +179 -0
- zapy/_message/kinds/group_history.py +206 -0
- zapy/_message/kinds/newsletter.py +388 -0
- zapy/_message/kinds/sticker_pack.py +74 -0
- zapy/_message/messaging/__init__.py +336 -0
- zapy/_message/messaging/companion_host.py +117 -0
- zapy/_message/messaging/fanout.py +311 -0
- zapy/_message/messaging/group_metadata.py +444 -0
- zapy/_message/messaging/ignore_key.py +264 -0
- zapy/_message/messaging/key_protocol.py +217 -0
- zapy/_message/messaging/link_preview.py +448 -0
- zapy/_message/messaging/messages.py +1429 -0
- zapy/_message/primitives/__init__.py +1 -0
- zapy/_message/primitives/ack.py +232 -0
- zapy/_message/primitives/incoming.py +1141 -0
- zapy/_message/primitives/media_retry.py +451 -0
- zapy/_message/primitives/peer_data_operation.py +271 -0
- zapy/_message/types.py +434 -0
- zapy/_proto/WAProto_pb2.py +2584 -0
- zapy/_proto/__init__.py +1 -0
- zapy/_protocol/__init__.py +3 -0
- zapy/_protocol/abprops_spec.py +6216 -0
- zapy/_protocol/auth.py +17 -0
- zapy/_protocol/browser.py +57 -0
- zapy/_protocol/defaults.py +38 -0
- zapy/_protocol/device_identity.py +54 -0
- zapy/_protocol/jid.py +241 -0
- zapy/_protocol/message.py +127 -0
- zapy/_protocol/nodes.py +144 -0
- zapy/_protocol/notification.py +18 -0
- zapy/_retry/__init__.py +77 -0
- zapy/_retry/codec.py +209 -0
- zapy/_retry/constants.py +28 -0
- zapy/_retry/parse.py +132 -0
- zapy/_retry/reason.py +65 -0
- zapy/_retry/replay.py +483 -0
- zapy/_retry/tracker.py +161 -0
- zapy/_retry/types.py +122 -0
- zapy/_signal/__init__.py +171 -0
- zapy/_signal/api/__init__.py +133 -0
- zapy/_signal/api/codec.py +164 -0
- zapy/_signal/api/constants.py +14 -0
- zapy/_signal/api/device_sync.py +915 -0
- zapy/_signal/api/digest_sync.py +363 -0
- zapy/_signal/api/identity_sync.py +250 -0
- zapy/_signal/api/missing_pre_keys_sync.py +267 -0
- zapy/_signal/api/prekeys.py +191 -0
- zapy/_signal/api/result_map.py +40 -0
- zapy/_signal/api/rotate_key.py +111 -0
- zapy/_signal/api/session_sync.py +297 -0
- zapy/_signal/attestation.py +145 -0
- zapy/_signal/constants.py +22 -0
- zapy/_signal/encoding.py +78 -0
- zapy/_signal/group/__init__.py +35 -0
- zapy/_signal/group/chain.py +139 -0
- zapy/_signal/group/codec.py +78 -0
- zapy/_signal/group/encoding.py +166 -0
- zapy/_signal/group/manager.py +361 -0
- zapy/_signal/registration/__init__.py +44 -0
- zapy/_signal/registration/keygen.py +46 -0
- zapy/_signal/registration/reg_encoding.py +66 -0
- zapy/_signal/registration/reg_utils.py +52 -0
- zapy/_signal/session/__init__.py +114 -0
- zapy/_signal/session/encoding.py +385 -0
- zapy/_signal/session/protocol.py +590 -0
- zapy/_signal/session/ratchet.py +381 -0
- zapy/_signal/session/resolver.py +630 -0
- zapy/_signal/session/serializer.py +96 -0
- zapy/_signal/session/session.py +272 -0
- zapy/_signal/types.py +214 -0
- zapy/_store/__init__.py +118 -0
- zapy/_store/cache/__init__.py +166 -0
- zapy/_store/cache/identity.py +130 -0
- zapy/_store/cache/privacy_token.py +116 -0
- zapy/_store/cache/sender_key.py +168 -0
- zapy/_store/cache/session.py +142 -0
- zapy/_store/contracts/__init__.py +84 -0
- zapy/_store/contracts/appstate.py +82 -0
- zapy/_store/contracts/auth.py +16 -0
- zapy/_store/contracts/chat_metadata.py +29 -0
- zapy/_store/contracts/contact.py +31 -0
- zapy/_store/contracts/device_list.py +35 -0
- zapy/_store/contracts/group_metadata.py +33 -0
- zapy/_store/contracts/identity.py +18 -0
- zapy/_store/contracts/message.py +36 -0
- zapy/_store/contracts/message_secret.py +41 -0
- zapy/_store/contracts/pre_key.py +32 -0
- zapy/_store/contracts/privacy_token.py +32 -0
- zapy/_store/contracts/retry.py +47 -0
- zapy/_store/contracts/sender_key.py +37 -0
- zapy/_store/contracts/session.py +21 -0
- zapy/_store/contracts/signal.py +23 -0
- zapy/_store/contracts/thread.py +35 -0
- zapy/_store/create_store.py +890 -0
- zapy/_store/locks.py +1154 -0
- zapy/_store/memory/__init__.py +40 -0
- zapy/_store/memory/appstate.py +200 -0
- zapy/_store/memory/auth.py +24 -0
- zapy/_store/memory/chat_metadata.py +124 -0
- zapy/_store/memory/contact.py +125 -0
- zapy/_store/memory/device_list.py +161 -0
- zapy/_store/memory/group_metadata.py +125 -0
- zapy/_store/memory/identity.py +86 -0
- zapy/_store/memory/message.py +85 -0
- zapy/_store/memory/message_secret.py +145 -0
- zapy/_store/memory/pre_key.py +112 -0
- zapy/_store/memory/privacy_token.py +103 -0
- zapy/_store/memory/retry.py +250 -0
- zapy/_store/memory/sender_key.py +162 -0
- zapy/_store/memory/session.py +111 -0
- zapy/_store/memory/signal.py +45 -0
- zapy/_store/memory/thread.py +66 -0
- zapy/_store/noop.py +279 -0
- zapy/_transport/__init__.py +3 -0
- zapy/_transport/binary/__init__.py +77 -0
- zapy/_transport/binary/constants.py +80 -0
- zapy/_transport/binary/decoder.py +260 -0
- zapy/_transport/binary/encoder.py +279 -0
- zapy/_transport/binary/tokens.py +1289 -0
- zapy/_transport/comms.py +759 -0
- zapy/_transport/keepalive/__init__.py +3 -0
- zapy/_transport/keepalive/keep_alive.py +252 -0
- zapy/_transport/node/__init__.py +3 -0
- zapy/_transport/node/builders/__init__.py +9 -0
- zapy/_transport/node/builders/abprops.py +33 -0
- zapy/_transport/node/builders/account_sync.py +120 -0
- zapy/_transport/node/builders/bot.py +56 -0
- zapy/_transport/node/builders/business.py +261 -0
- zapy/_transport/node/builders/chatstate.py +74 -0
- zapy/_transport/node/builders/community.py +116 -0
- zapy/_transport/node/builders/device.py +30 -0
- zapy/_transport/node/builders/email.py +181 -0
- zapy/_transport/node/builders/global_.py +74 -0
- zapy/_transport/node/builders/group.py +279 -0
- zapy/_transport/node/builders/media.py +21 -0
- zapy/_transport/node/builders/message.py +436 -0
- zapy/_transport/node/builders/mobile.py +227 -0
- zapy/_transport/node/builders/newsletter.py +293 -0
- zapy/_transport/node/builders/offline.py +20 -0
- zapy/_transport/node/builders/pairing.py +130 -0
- zapy/_transport/node/builders/passive.py +28 -0
- zapy/_transport/node/builders/prekeys.py +215 -0
- zapy/_transport/node/builders/presence.py +100 -0
- zapy/_transport/node/builders/privacy.py +243 -0
- zapy/_transport/node/builders/privacy_token.py +94 -0
- zapy/_transport/node/builders/profile.py +176 -0
- zapy/_transport/node/builders/retry.py +156 -0
- zapy/_transport/node/builders/shortcake.py +87 -0
- zapy/_transport/node/builders/tos.py +92 -0
- zapy/_transport/node/builders/usync.py +185 -0
- zapy/_transport/node/helpers.py +201 -0
- zapy/_transport/node/orchestrator.py +210 -0
- zapy/_transport/node/query.py +82 -0
- zapy/_transport/node/transport.py +105 -0
- zapy/_transport/node/usync.py +33 -0
- zapy/_transport/node/xml.py +38 -0
- zapy/_transport/noise/__init__.py +7 -0
- zapy/_transport/noise/cert.py +85 -0
- zapy/_transport/noise/client_payload.py +344 -0
- zapy/_transport/noise/constants.py +12 -0
- zapy/_transport/noise/frame_codec.py +120 -0
- zapy/_transport/noise/handshake.py +92 -0
- zapy/_transport/noise/session.py +493 -0
- zapy/_transport/noise/socket.py +45 -0
- zapy/_transport/noise/types.py +66 -0
- zapy/_transport/noise/version.json +3 -0
- zapy/_transport/proxy.py +35 -0
- zapy/_transport/types.py +19 -0
- zapy/_transport/wa_version_fetcher.py +159 -0
- zapy/_transport/web_socket.py +602 -0
- zapy/_util/__init__.py +3 -0
- zapy/_util/bytes.py +131 -0
- zapy/_util/coercion.py +29 -0
- zapy/_util/collections.py +45 -0
- zapy/_util/primitives.py +49 -0
- zapy/client.py +840 -0
- zapy/errors.py +148 -0
- zapy/events.py +564 -0
- zapy/jid.py +140 -0
- zapy/py.typed +0 -0
- zapy/stores/__init__.py +50 -0
- zapy/stores/base.py +1627 -0
- zapy/stores/json_file.py +359 -0
- zapy/stores/memory.py +322 -0
- zapy/stores/sqlite.py +343 -0
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
"""Newsletter content building (mirrors ``src/client/newsletter/content.ts``).
|
|
2
|
+
|
|
3
|
+
The oracle builds ``Proto.Message`` protobufs through the message layer. That
|
|
4
|
+
layer's send-content union belongs to another worker, so this module accepts
|
|
5
|
+
the seam types below and encodes through an injected ``encode_message``
|
|
6
|
+
callable (tests inject a JSON encoder; production wiring passes the real
|
|
7
|
+
dict-to-proto encoder when the message layer exposes one):
|
|
8
|
+
|
|
9
|
+
- ``str`` — plain conversation text (link-preview hook optional);
|
|
10
|
+
- :class:`WaNewsletterTextInput` — text with an explicit shape;
|
|
11
|
+
- :class:`WaNewsletterMediaInput` — one media upload + proto envelope;
|
|
12
|
+
- ``bytes`` — pre-encoded plaintext, classified by the explicit ``kind`` /
|
|
13
|
+
``media_type`` arguments.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import base64
|
|
19
|
+
from collections.abc import Awaitable, Callable
|
|
20
|
+
from dataclasses import dataclass
|
|
21
|
+
from typing import Any, BinaryIO, Literal, Protocol
|
|
22
|
+
|
|
23
|
+
from zapy._crypto.core.primitives import sha256
|
|
24
|
+
from zapy._infra.log import Logger
|
|
25
|
+
from zapy._util.primitives import to_error
|
|
26
|
+
|
|
27
|
+
WaNewsletterUploadMedia = bytes | str | BinaryIO
|
|
28
|
+
|
|
29
|
+
WaNewsletterMediaKind = Literal[
|
|
30
|
+
"image",
|
|
31
|
+
"video",
|
|
32
|
+
"audio",
|
|
33
|
+
"document",
|
|
34
|
+
"sticker",
|
|
35
|
+
"sticker-pack",
|
|
36
|
+
"gif",
|
|
37
|
+
"ptt",
|
|
38
|
+
"ptv",
|
|
39
|
+
"thumbnail-link",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
# Mirrors ``NEWSLETTER_MEDIA_UPLOAD_PATHS`` (``src/media/constants.ts``).
|
|
43
|
+
NEWSLETTER_MEDIA_UPLOAD_PATHS: dict[str, str] = {
|
|
44
|
+
"image": "/newsletter/newsletter-image",
|
|
45
|
+
"video": "/newsletter/newsletter-video",
|
|
46
|
+
"audio": "/newsletter/newsletter-audio",
|
|
47
|
+
"document": "/newsletter/newsletter-document",
|
|
48
|
+
"sticker": "/newsletter/newsletter-sticker",
|
|
49
|
+
"sticker-pack": "/newsletter/newsletter-sticker-pack",
|
|
50
|
+
"gif": "/newsletter/newsletter-gif",
|
|
51
|
+
"ptt": "/newsletter/newsletter-ptt",
|
|
52
|
+
"ptv": "/newsletter/newsletter-ptv",
|
|
53
|
+
"thumbnail-link": "/newsletter/newsletter-thumbnail-link",
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
# Encoded media types a channel publish accepts; anything else ships as text.
|
|
57
|
+
NEWSLETTER_MEDIA_TYPES: frozenset[str] = frozenset(
|
|
58
|
+
{
|
|
59
|
+
"audio",
|
|
60
|
+
"document",
|
|
61
|
+
"gif",
|
|
62
|
+
"image",
|
|
63
|
+
"ptt",
|
|
64
|
+
"ptv",
|
|
65
|
+
"sticker",
|
|
66
|
+
"sticker-pack",
|
|
67
|
+
"url",
|
|
68
|
+
"vcard",
|
|
69
|
+
"video",
|
|
70
|
+
}
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@dataclass(frozen=True, slots=True)
|
|
75
|
+
class WaMediaHost:
|
|
76
|
+
"""One media-conn host entry."""
|
|
77
|
+
|
|
78
|
+
hostname: str
|
|
79
|
+
is_fallback: bool = False
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass(frozen=True, slots=True)
|
|
83
|
+
class WaMediaConn:
|
|
84
|
+
"""Minimal media-connector credentials (mirrors ``WaMediaConn``)."""
|
|
85
|
+
|
|
86
|
+
auth: str
|
|
87
|
+
hosts: tuple[WaMediaHost, ...]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class WaMediaTransfer(Protocol):
|
|
91
|
+
"""Plaintext upload leg (mirrors the used part of ``WaMediaTransferClient``)."""
|
|
92
|
+
|
|
93
|
+
async def upload_stream(
|
|
94
|
+
self,
|
|
95
|
+
*,
|
|
96
|
+
url: str,
|
|
97
|
+
body: bytes | BinaryIO,
|
|
98
|
+
content_length: int,
|
|
99
|
+
content_type: str | None = None,
|
|
100
|
+
) -> Any: ...
|
|
101
|
+
|
|
102
|
+
async def read_response_bytes(self, response: Any) -> bytes: ...
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass(frozen=True, slots=True)
|
|
106
|
+
class WaNewsletterUploadInput:
|
|
107
|
+
"""Newsletter media upload input."""
|
|
108
|
+
|
|
109
|
+
media_kind: str
|
|
110
|
+
media: WaNewsletterUploadMedia
|
|
111
|
+
mimetype: str
|
|
112
|
+
media_conn: WaMediaConn
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@dataclass(frozen=True, slots=True)
|
|
116
|
+
class WaNewsletterUploadResult:
|
|
117
|
+
"""Newsletter media upload result."""
|
|
118
|
+
|
|
119
|
+
url: str
|
|
120
|
+
direct_path: str
|
|
121
|
+
file_sha256: bytes
|
|
122
|
+
file_length: int
|
|
123
|
+
media_id: str
|
|
124
|
+
handle: str | None = None
|
|
125
|
+
metadata_url: str | None = None
|
|
126
|
+
thumbnail_direct_path: str | None = None
|
|
127
|
+
thumbnail_sha256: bytes | None = None
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@dataclass(frozen=True, slots=True)
|
|
131
|
+
class WaNewsletterTextInput:
|
|
132
|
+
"""Explicit text content."""
|
|
133
|
+
|
|
134
|
+
text: str
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
@dataclass(frozen=True, slots=True)
|
|
138
|
+
class WaNewsletterMediaInput:
|
|
139
|
+
"""Media content: uploaded first, then wrapped in a proto envelope."""
|
|
140
|
+
|
|
141
|
+
media_kind: str
|
|
142
|
+
media: WaNewsletterUploadMedia
|
|
143
|
+
mimetype: str | None = None
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@dataclass(frozen=True, slots=True)
|
|
147
|
+
class WaNewsletterBuiltContent:
|
|
148
|
+
"""Built plaintext ready for dispatch."""
|
|
149
|
+
|
|
150
|
+
kind: str
|
|
151
|
+
plaintext: bytes
|
|
152
|
+
media_type: str | None
|
|
153
|
+
upload: WaNewsletterUploadResult | None
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@dataclass(frozen=True, slots=True)
|
|
157
|
+
class BuildNewsletterContentOptions:
|
|
158
|
+
"""Content-builder dependencies."""
|
|
159
|
+
|
|
160
|
+
logger: Logger
|
|
161
|
+
encode_message: Callable[[dict[str, Any]], bytes]
|
|
162
|
+
media_transfer: WaMediaTransfer | None = None
|
|
163
|
+
get_media_conn: Callable[[], Awaitable[WaMediaConn]] | None = None
|
|
164
|
+
link_preview_resolver: (
|
|
165
|
+
Callable[[str], Awaitable[tuple[bytes, str | None] | None]] | None
|
|
166
|
+
) = None
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def select_media_upload_host(media_conn: WaMediaConn) -> str:
|
|
170
|
+
"""Pick the first non-fallback upload host (mirrors ``selectMediaUploadHost``)."""
|
|
171
|
+
for host in media_conn.hosts:
|
|
172
|
+
if not host.is_fallback:
|
|
173
|
+
return host.hostname
|
|
174
|
+
if not media_conn.hosts:
|
|
175
|
+
raise ValueError("media conn has no upload hosts")
|
|
176
|
+
return media_conn.hosts[0].hostname
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def build_media_upload_url(
|
|
180
|
+
host: str, path: str, auth: str, file_sha256: bytes, media_id: str | None = None
|
|
181
|
+
) -> str:
|
|
182
|
+
"""Build the plaintext upload URL (mirrors ``buildMediaUploadUrl``)."""
|
|
183
|
+
import urllib.parse
|
|
184
|
+
|
|
185
|
+
hash_token = base64.urlsafe_b64encode(bytes(file_sha256)).decode("ascii")
|
|
186
|
+
base = (
|
|
187
|
+
f"https://{host}{path}/{hash_token}"
|
|
188
|
+
f"?auth={urllib.parse.quote(auth, safe='')}"
|
|
189
|
+
f"&token={urllib.parse.quote(hash_token, safe='')}"
|
|
190
|
+
)
|
|
191
|
+
return base if media_id is None else f"{base}&media_id={media_id}"
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def assert_media_upload_status(status: int, label: str) -> None:
|
|
195
|
+
"""Throw unless the upload status is 2xx (mirrors ``assertMediaUploadStatus``)."""
|
|
196
|
+
if status < 200 or status >= 300:
|
|
197
|
+
raise ValueError(f"{label} failed with status {status}")
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def parse_media_upload_json_body(body: bytes, label: str) -> Any:
|
|
201
|
+
"""Parse an upload JSON body (mirrors ``parseMediaUploadJsonBody``)."""
|
|
202
|
+
import json
|
|
203
|
+
|
|
204
|
+
try:
|
|
205
|
+
return json.loads(bytes(body).decode("utf-8"))
|
|
206
|
+
except (ValueError, UnicodeDecodeError) as error:
|
|
207
|
+
raise ValueError(f"{label} returned invalid json: {to_error(error)}") from error
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
async def _read_upload_source(media: WaNewsletterUploadMedia) -> tuple[bytes, int]:
|
|
211
|
+
if isinstance(media, (bytes, bytearray)):
|
|
212
|
+
raw = bytes(media)
|
|
213
|
+
return raw, len(raw)
|
|
214
|
+
if isinstance(media, str):
|
|
215
|
+
with open(media, "rb") as handle:
|
|
216
|
+
raw = handle.read()
|
|
217
|
+
return raw, len(raw)
|
|
218
|
+
chunks: list[bytes] = []
|
|
219
|
+
while True:
|
|
220
|
+
chunk = media.read(65536)
|
|
221
|
+
if not chunk:
|
|
222
|
+
break
|
|
223
|
+
chunks.append(bytes(chunk))
|
|
224
|
+
raw = b"".join(chunks)
|
|
225
|
+
return raw, len(raw)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
async def upload_newsletter_media(
|
|
229
|
+
options: BuildNewsletterContentOptions,
|
|
230
|
+
input: WaNewsletterUploadInput,
|
|
231
|
+
) -> WaNewsletterUploadResult:
|
|
232
|
+
"""Upload newsletter media as plaintext (mirrors ``uploadNewsletterMedia``)."""
|
|
233
|
+
if options.media_transfer is None:
|
|
234
|
+
raise ValueError("newsletter media upload requires media_transfer")
|
|
235
|
+
body, byte_length = await _read_upload_source(input.media)
|
|
236
|
+
file_digest = sha256(body)
|
|
237
|
+
host = select_media_upload_host(input.media_conn)
|
|
238
|
+
import random
|
|
239
|
+
|
|
240
|
+
media_id = str(random.randint(0, 281_474_976_710_655))
|
|
241
|
+
url = build_media_upload_url(
|
|
242
|
+
host,
|
|
243
|
+
NEWSLETTER_MEDIA_UPLOAD_PATHS.get(
|
|
244
|
+
input.media_kind, "/newsletter/newsletter-image"
|
|
245
|
+
),
|
|
246
|
+
input.media_conn.auth,
|
|
247
|
+
file_digest,
|
|
248
|
+
media_id,
|
|
249
|
+
)
|
|
250
|
+
options.logger.debug(
|
|
251
|
+
"sending newsletter media upload",
|
|
252
|
+
{"host": host, "size": byte_length},
|
|
253
|
+
)
|
|
254
|
+
response = await options.media_transfer.upload_stream(
|
|
255
|
+
url=url,
|
|
256
|
+
body=body,
|
|
257
|
+
content_length=byte_length,
|
|
258
|
+
content_type=input.mimetype,
|
|
259
|
+
)
|
|
260
|
+
status = getattr(response, "status", 200)
|
|
261
|
+
assert_media_upload_status(int(status), "newsletter media upload")
|
|
262
|
+
parsed = parse_media_upload_json_body(
|
|
263
|
+
await options.media_transfer.read_response_bytes(response),
|
|
264
|
+
"newsletter media upload",
|
|
265
|
+
)
|
|
266
|
+
if not isinstance(parsed, dict):
|
|
267
|
+
raise ValueError("newsletter media upload response missing url/direct_path")
|
|
268
|
+
url_raw: Any = parsed.get("url")
|
|
269
|
+
direct_path_raw: Any = parsed.get("direct_path")
|
|
270
|
+
if (
|
|
271
|
+
not isinstance(url_raw, str)
|
|
272
|
+
or not url_raw
|
|
273
|
+
or not isinstance(direct_path_raw, str)
|
|
274
|
+
or not direct_path_raw
|
|
275
|
+
):
|
|
276
|
+
raise ValueError("newsletter media upload response missing url/direct_path")
|
|
277
|
+
url = url_raw
|
|
278
|
+
direct_path = direct_path_raw
|
|
279
|
+
thumbnail_info: dict[str, Any] = parsed.get("thumbnail_info") or {}
|
|
280
|
+
thumbnail_sha256_raw = thumbnail_info.get("thumbnail_sha256")
|
|
281
|
+
handle = parsed.get("handle")
|
|
282
|
+
metadata_url = parsed.get("metadata_url")
|
|
283
|
+
thumbnail_direct_path = thumbnail_info.get("thumbnail_direct_path")
|
|
284
|
+
return WaNewsletterUploadResult(
|
|
285
|
+
url=url,
|
|
286
|
+
direct_path=direct_path,
|
|
287
|
+
handle=handle if isinstance(handle, str) else None,
|
|
288
|
+
metadata_url=metadata_url if isinstance(metadata_url, str) else None,
|
|
289
|
+
thumbnail_direct_path=thumbnail_direct_path
|
|
290
|
+
if isinstance(thumbnail_direct_path, str)
|
|
291
|
+
else None,
|
|
292
|
+
thumbnail_sha256=base64.b64decode(thumbnail_sha256_raw)
|
|
293
|
+
if isinstance(thumbnail_sha256_raw, str) and thumbnail_sha256_raw
|
|
294
|
+
else None,
|
|
295
|
+
file_sha256=file_digest,
|
|
296
|
+
file_length=byte_length,
|
|
297
|
+
media_id=media_id,
|
|
298
|
+
)
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
async def _resolve_text_link_preview(
|
|
302
|
+
options: BuildNewsletterContentOptions, text: str
|
|
303
|
+
) -> tuple[bytes, str | None] | None:
|
|
304
|
+
if options.link_preview_resolver is None:
|
|
305
|
+
return None
|
|
306
|
+
try:
|
|
307
|
+
return await options.link_preview_resolver(text)
|
|
308
|
+
except Exception as error:
|
|
309
|
+
options.logger.warn(
|
|
310
|
+
"link preview resolver failed, sending plain text",
|
|
311
|
+
{"message": str(to_error(error))},
|
|
312
|
+
)
|
|
313
|
+
return None
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def _encode_text(options: BuildNewsletterContentOptions, text: str) -> bytes:
|
|
317
|
+
return options.encode_message({"conversation": text})
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
async def build_newsletter_message_content(
|
|
321
|
+
options: BuildNewsletterContentOptions,
|
|
322
|
+
content: str
|
|
323
|
+
| bytes
|
|
324
|
+
| WaNewsletterTextInput
|
|
325
|
+
| WaNewsletterMediaInput
|
|
326
|
+
| dict[str, Any],
|
|
327
|
+
*,
|
|
328
|
+
kind: str = "text",
|
|
329
|
+
media_type: str | None = None,
|
|
330
|
+
) -> WaNewsletterBuiltContent:
|
|
331
|
+
"""Build newsletter plaintext (mirrors ``buildNewsletterMessageContent``).
|
|
332
|
+
|
|
333
|
+
``dict`` content holds the raw proto-shaped envelope and is encoded as-is;
|
|
334
|
+
addon/update envelopes are rejected exactly like the oracle rejects typed
|
|
335
|
+
addon content.
|
|
336
|
+
"""
|
|
337
|
+
if isinstance(content, str):
|
|
338
|
+
preview = await _resolve_text_link_preview(options, content)
|
|
339
|
+
if preview is not None:
|
|
340
|
+
plaintext, resolved_media_type = preview
|
|
341
|
+
return WaNewsletterBuiltContent(
|
|
342
|
+
kind="media" if resolved_media_type else "text",
|
|
343
|
+
plaintext=plaintext,
|
|
344
|
+
media_type=resolved_media_type,
|
|
345
|
+
upload=None,
|
|
346
|
+
)
|
|
347
|
+
return WaNewsletterBuiltContent(
|
|
348
|
+
kind="text",
|
|
349
|
+
plaintext=_encode_text(options, content),
|
|
350
|
+
media_type=None,
|
|
351
|
+
upload=None,
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
if isinstance(content, WaNewsletterTextInput):
|
|
355
|
+
preview = await _resolve_text_link_preview(options, content.text)
|
|
356
|
+
if preview is not None:
|
|
357
|
+
plaintext, resolved_media_type = preview
|
|
358
|
+
return WaNewsletterBuiltContent(
|
|
359
|
+
kind="media" if resolved_media_type else "text",
|
|
360
|
+
plaintext=plaintext,
|
|
361
|
+
media_type=resolved_media_type,
|
|
362
|
+
upload=None,
|
|
363
|
+
)
|
|
364
|
+
return WaNewsletterBuiltContent(
|
|
365
|
+
kind="text",
|
|
366
|
+
plaintext=_encode_text(options, content.text),
|
|
367
|
+
media_type=None,
|
|
368
|
+
upload=None,
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
if isinstance(content, WaNewsletterMediaInput):
|
|
372
|
+
if options.media_transfer is None or options.get_media_conn is None:
|
|
373
|
+
raise ValueError(
|
|
374
|
+
"newsletter media send requires mediaTransfer "
|
|
375
|
+
"and getMediaConn dependencies"
|
|
376
|
+
)
|
|
377
|
+
media_conn = await options.get_media_conn()
|
|
378
|
+
mimetype = content.mimetype or (
|
|
379
|
+
"image/webp" if content.media_kind == "sticker" else None
|
|
380
|
+
)
|
|
381
|
+
if not mimetype:
|
|
382
|
+
raise ValueError(
|
|
383
|
+
"newsletter media send requires explicit mimetype "
|
|
384
|
+
f"for {content.media_kind} content"
|
|
385
|
+
)
|
|
386
|
+
upload = await upload_newsletter_media(
|
|
387
|
+
options,
|
|
388
|
+
WaNewsletterUploadInput(
|
|
389
|
+
media_kind=content.media_kind,
|
|
390
|
+
media=content.media,
|
|
391
|
+
mimetype=mimetype,
|
|
392
|
+
media_conn=media_conn,
|
|
393
|
+
),
|
|
394
|
+
)
|
|
395
|
+
envelope = _media_envelope(content.media_kind, upload, mimetype)
|
|
396
|
+
return WaNewsletterBuiltContent(
|
|
397
|
+
kind="media",
|
|
398
|
+
plaintext=options.encode_message(envelope),
|
|
399
|
+
media_type=content.media_kind,
|
|
400
|
+
upload=upload,
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
if isinstance(content, (bytes, bytearray)):
|
|
404
|
+
raw = bytes(content)
|
|
405
|
+
picked = media_type if media_type in NEWSLETTER_MEDIA_TYPES else None
|
|
406
|
+
return WaNewsletterBuiltContent(
|
|
407
|
+
kind="media" if picked else kind,
|
|
408
|
+
plaintext=raw,
|
|
409
|
+
media_type=picked,
|
|
410
|
+
upload=None,
|
|
411
|
+
)
|
|
412
|
+
|
|
413
|
+
if isinstance(content, dict):
|
|
414
|
+
_reject_addon_envelope(content)
|
|
415
|
+
return WaNewsletterBuiltContent(
|
|
416
|
+
kind="text",
|
|
417
|
+
plaintext=options.encode_message(content),
|
|
418
|
+
media_type=None,
|
|
419
|
+
upload=None,
|
|
420
|
+
)
|
|
421
|
+
|
|
422
|
+
raise ValueError(f"newsletter sends do not accept {type(content).__name__} content")
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
def _media_envelope(
|
|
426
|
+
media_kind: str, upload: WaNewsletterUploadResult, mimetype: str
|
|
427
|
+
) -> dict[str, Any]:
|
|
428
|
+
common: dict[str, Any] = {
|
|
429
|
+
"url": upload.url,
|
|
430
|
+
"directPath": upload.direct_path,
|
|
431
|
+
"mimetype": mimetype,
|
|
432
|
+
"fileSha256": upload.file_sha256,
|
|
433
|
+
"fileLength": upload.file_length,
|
|
434
|
+
}
|
|
435
|
+
if media_kind == "image":
|
|
436
|
+
return {"imageMessage": common}
|
|
437
|
+
if media_kind == "video":
|
|
438
|
+
return {"videoMessage": common}
|
|
439
|
+
if media_kind == "ptv":
|
|
440
|
+
return {"ptvMessage": common}
|
|
441
|
+
if media_kind == "audio":
|
|
442
|
+
return {"audioMessage": common}
|
|
443
|
+
if media_kind == "document":
|
|
444
|
+
return {"documentMessage": common}
|
|
445
|
+
if media_kind == "sticker":
|
|
446
|
+
return {"stickerMessage": common}
|
|
447
|
+
return {"documentMessage": common}
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
def _reject_addon_envelope(envelope: dict[str, Any]) -> None:
|
|
451
|
+
inner = envelope
|
|
452
|
+
for wrapper in (
|
|
453
|
+
"ephemeralMessage",
|
|
454
|
+
"viewOnceMessage",
|
|
455
|
+
"documentWithCaptionMessage",
|
|
456
|
+
):
|
|
457
|
+
nested = inner.get(wrapper)
|
|
458
|
+
if isinstance(nested, dict) and isinstance(nested.get("message"), dict):
|
|
459
|
+
inner = nested["message"]
|
|
460
|
+
break
|
|
461
|
+
if (
|
|
462
|
+
inner.get("reactionMessage") is not None
|
|
463
|
+
or inner.get("pollUpdateMessage") is not None
|
|
464
|
+
or inner.get("pinInChatMessage") is not None
|
|
465
|
+
or inner.get("keepInChatMessage") is not None
|
|
466
|
+
):
|
|
467
|
+
raise ValueError(
|
|
468
|
+
"newsletter sends do not accept addon/update protobuf payloads; "
|
|
469
|
+
"use the newsletter helpers instead"
|
|
470
|
+
)
|
|
471
|
+
protocol = inner.get("protocolMessage")
|
|
472
|
+
if isinstance(protocol, dict) and protocol.get("type") == "REVOKE":
|
|
473
|
+
raise ValueError(
|
|
474
|
+
"newsletter sends do not accept addon/update protobuf payloads; "
|
|
475
|
+
"use the newsletter helpers instead"
|
|
476
|
+
)
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
__all__: list[str] = [
|
|
480
|
+
"NEWSLETTER_MEDIA_TYPES",
|
|
481
|
+
"NEWSLETTER_MEDIA_UPLOAD_PATHS",
|
|
482
|
+
"BuildNewsletterContentOptions",
|
|
483
|
+
"WaMediaConn",
|
|
484
|
+
"WaMediaHost",
|
|
485
|
+
"WaMediaTransfer",
|
|
486
|
+
"WaNewsletterBuiltContent",
|
|
487
|
+
"WaNewsletterMediaInput",
|
|
488
|
+
"WaNewsletterMediaKind",
|
|
489
|
+
"WaNewsletterTextInput",
|
|
490
|
+
"WaNewsletterUploadInput",
|
|
491
|
+
"WaNewsletterUploadMedia",
|
|
492
|
+
"WaNewsletterUploadResult",
|
|
493
|
+
"assert_media_upload_status",
|
|
494
|
+
"build_media_upload_url",
|
|
495
|
+
"build_newsletter_message_content",
|
|
496
|
+
"parse_media_upload_json_body",
|
|
497
|
+
"select_media_upload_host",
|
|
498
|
+
"upload_newsletter_media",
|
|
499
|
+
]
|