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,40 @@
|
|
|
1
|
+
"""In-memory store backends (mirrors ``src/store/memory/``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from zapy._store.memory.appstate import WaAppStateMemoryStore
|
|
6
|
+
from zapy._store.memory.auth import WaAuthMemoryStore
|
|
7
|
+
from zapy._store.memory.chat_metadata import WaChatMetadataMemoryStore
|
|
8
|
+
from zapy._store.memory.contact import WaContactMemoryStore
|
|
9
|
+
from zapy._store.memory.device_list import WaDeviceListMemoryStore
|
|
10
|
+
from zapy._store.memory.group_metadata import WaGroupMetadataMemoryStore
|
|
11
|
+
from zapy._store.memory.identity import WaIdentityMemoryStore
|
|
12
|
+
from zapy._store.memory.message import WaMessageMemoryStore
|
|
13
|
+
from zapy._store.memory.message_secret import WaMessageSecretMemoryStore
|
|
14
|
+
from zapy._store.memory.pre_key import WaPreKeyMemoryStore
|
|
15
|
+
from zapy._store.memory.privacy_token import WaPrivacyTokenMemoryStore
|
|
16
|
+
from zapy._store.memory.retry import WaRetryMemoryStore
|
|
17
|
+
from zapy._store.memory.sender_key import SenderKeyMemoryStore
|
|
18
|
+
from zapy._store.memory.session import WaSessionMemoryStore, signal_address_key
|
|
19
|
+
from zapy._store.memory.signal import WaSignalMemoryStore
|
|
20
|
+
from zapy._store.memory.thread import WaThreadMemoryStore
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"SenderKeyMemoryStore",
|
|
24
|
+
"WaAppStateMemoryStore",
|
|
25
|
+
"WaAuthMemoryStore",
|
|
26
|
+
"WaChatMetadataMemoryStore",
|
|
27
|
+
"WaContactMemoryStore",
|
|
28
|
+
"WaDeviceListMemoryStore",
|
|
29
|
+
"WaGroupMetadataMemoryStore",
|
|
30
|
+
"WaIdentityMemoryStore",
|
|
31
|
+
"WaMessageMemoryStore",
|
|
32
|
+
"WaMessageSecretMemoryStore",
|
|
33
|
+
"WaPreKeyMemoryStore",
|
|
34
|
+
"WaPrivacyTokenMemoryStore",
|
|
35
|
+
"WaRetryMemoryStore",
|
|
36
|
+
"WaSessionMemoryStore",
|
|
37
|
+
"WaSignalMemoryStore",
|
|
38
|
+
"WaThreadMemoryStore",
|
|
39
|
+
"signal_address_key",
|
|
40
|
+
]
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"""In-memory app-state store (mirrors ``src/store/memory/appstate.store.ts``).
|
|
2
|
+
|
|
3
|
+
Sync keys are deduplicated by key id and collections are replaced wholesale
|
|
4
|
+
per update, each bounded independently.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from collections.abc import Sequence
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
|
|
12
|
+
from zapy._store.contracts.appstate import (
|
|
13
|
+
AppStateCollectionName,
|
|
14
|
+
WaAppStateCollectionStateUpdate,
|
|
15
|
+
WaAppStateCollectionStoreState,
|
|
16
|
+
WaAppStateCollectionVersion,
|
|
17
|
+
WaAppStateStoreData,
|
|
18
|
+
WaAppStateSyncKey,
|
|
19
|
+
)
|
|
20
|
+
from zapy._util.bytes import bytes_to_hex, uint8_equal
|
|
21
|
+
from zapy._util.coercion import resolve_positive
|
|
22
|
+
from zapy._util.collections import set_bounded_map_entry
|
|
23
|
+
|
|
24
|
+
_DEFAULT_MAX_SYNC_KEYS = 4_096
|
|
25
|
+
_DEFAULT_MAX_COLLECTION_ENTRIES = 50_000
|
|
26
|
+
|
|
27
|
+
APP_STATE_EMPTY_LT_HASH = bytes(128)
|
|
28
|
+
"""Empty hash (canonical constant lives in the Layer 12 appstate types)."""
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True, slots=True)
|
|
32
|
+
class WaAppStateMemoryStoreOptions:
|
|
33
|
+
"""Bounds for :class:`WaAppStateMemoryStore`."""
|
|
34
|
+
|
|
35
|
+
max_sync_keys: int | None = None
|
|
36
|
+
max_collection_entries: int | None = None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _key_device_id(key_id: bytes) -> int | None:
|
|
40
|
+
return None if len(key_id) < 6 else (key_id[0] << 8) | key_id[1]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _key_epoch(key_id: bytes) -> int:
|
|
44
|
+
if len(key_id) < 6:
|
|
45
|
+
return -1
|
|
46
|
+
return key_id[2] * 16_777_216 + key_id[3] * 65_536 + key_id[4] * 256 + key_id[5]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _pick_active_sync_key(
|
|
50
|
+
keys: list[WaAppStateSyncKey],
|
|
51
|
+
) -> WaAppStateSyncKey | None:
|
|
52
|
+
active: WaAppStateSyncKey | None = None
|
|
53
|
+
for key in keys:
|
|
54
|
+
if active is None:
|
|
55
|
+
active = key
|
|
56
|
+
continue
|
|
57
|
+
current_epoch = _key_epoch(active.key_id)
|
|
58
|
+
next_epoch = _key_epoch(key.key_id)
|
|
59
|
+
if next_epoch > current_epoch:
|
|
60
|
+
active = key
|
|
61
|
+
continue
|
|
62
|
+
if next_epoch < current_epoch:
|
|
63
|
+
continue
|
|
64
|
+
next_device_id = _key_device_id(key.key_id)
|
|
65
|
+
current_device_id = _key_device_id(active.key_id)
|
|
66
|
+
if (
|
|
67
|
+
next_device_id is not None
|
|
68
|
+
and current_device_id is not None
|
|
69
|
+
and next_device_id < current_device_id
|
|
70
|
+
):
|
|
71
|
+
active = key
|
|
72
|
+
return active
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class WaAppStateMemoryStore:
|
|
76
|
+
"""In-memory app-state sync keys and collection states."""
|
|
77
|
+
|
|
78
|
+
def __init__(
|
|
79
|
+
self,
|
|
80
|
+
initial: WaAppStateStoreData | None = None,
|
|
81
|
+
options: WaAppStateMemoryStoreOptions | None = None,
|
|
82
|
+
) -> None:
|
|
83
|
+
resolved = options if options is not None else WaAppStateMemoryStoreOptions()
|
|
84
|
+
self._keys: dict[str, WaAppStateSyncKey] = {}
|
|
85
|
+
self._collections: dict[str, WaAppStateCollectionStoreState] = {}
|
|
86
|
+
self._max_sync_keys = resolve_positive(
|
|
87
|
+
resolved.max_sync_keys,
|
|
88
|
+
_DEFAULT_MAX_SYNC_KEYS,
|
|
89
|
+
"WaAppStateMemoryStoreOptions.maxSyncKeys",
|
|
90
|
+
)
|
|
91
|
+
self._max_collection_entries = resolve_positive(
|
|
92
|
+
resolved.max_collection_entries,
|
|
93
|
+
_DEFAULT_MAX_COLLECTION_ENTRIES,
|
|
94
|
+
"WaAppStateMemoryStoreOptions.maxCollectionEntries",
|
|
95
|
+
)
|
|
96
|
+
if initial is not None:
|
|
97
|
+
for key in initial.keys:
|
|
98
|
+
set_bounded_map_entry(
|
|
99
|
+
self._keys,
|
|
100
|
+
bytes_to_hex(key.key_id),
|
|
101
|
+
key,
|
|
102
|
+
self._max_sync_keys,
|
|
103
|
+
)
|
|
104
|
+
for name, version in initial.collections.items():
|
|
105
|
+
index_value_map: dict[str, bytes] = {}
|
|
106
|
+
for index, value in version.index_value_map.items():
|
|
107
|
+
set_bounded_map_entry(
|
|
108
|
+
index_value_map,
|
|
109
|
+
index,
|
|
110
|
+
value,
|
|
111
|
+
self._max_collection_entries,
|
|
112
|
+
)
|
|
113
|
+
self._collections[name] = WaAppStateCollectionStoreState(
|
|
114
|
+
initialized=True,
|
|
115
|
+
version=version.version,
|
|
116
|
+
hash=version.hash,
|
|
117
|
+
index_value_map=index_value_map,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
async def export_data(self) -> WaAppStateStoreData:
|
|
121
|
+
collections: dict[str, WaAppStateCollectionVersion] = {}
|
|
122
|
+
for name, state in self._collections.items():
|
|
123
|
+
collections[name] = WaAppStateCollectionVersion(
|
|
124
|
+
version=state.version,
|
|
125
|
+
hash=state.hash,
|
|
126
|
+
index_value_map=dict(state.index_value_map),
|
|
127
|
+
)
|
|
128
|
+
return WaAppStateStoreData(
|
|
129
|
+
keys=list(self._keys.values()), collections=collections
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
async def upsert_sync_keys(self, keys: Sequence[WaAppStateSyncKey]) -> int:
|
|
133
|
+
inserted = 0
|
|
134
|
+
for key in keys:
|
|
135
|
+
key_hex = bytes_to_hex(key.key_id)
|
|
136
|
+
existing = self._keys.get(key_hex)
|
|
137
|
+
if existing is not None and uint8_equal(existing.key_data, key.key_data):
|
|
138
|
+
continue
|
|
139
|
+
set_bounded_map_entry(self._keys, key_hex, key, self._max_sync_keys)
|
|
140
|
+
inserted += 1
|
|
141
|
+
return inserted
|
|
142
|
+
|
|
143
|
+
async def get_sync_keys_batch(
|
|
144
|
+
self, key_ids: Sequence[bytes]
|
|
145
|
+
) -> list[WaAppStateSyncKey | None]:
|
|
146
|
+
return [self._keys.get(bytes_to_hex(key_id)) for key_id in key_ids]
|
|
147
|
+
|
|
148
|
+
async def get_sync_key_data(self, key_id: bytes) -> bytes | None:
|
|
149
|
+
record = self._keys.get(bytes_to_hex(key_id))
|
|
150
|
+
return record.key_data if record is not None else None
|
|
151
|
+
|
|
152
|
+
async def get_sync_key_data_batch(
|
|
153
|
+
self, key_ids: Sequence[bytes]
|
|
154
|
+
) -> list[bytes | None]:
|
|
155
|
+
return [await self.get_sync_key_data(key_id) for key_id in key_ids]
|
|
156
|
+
|
|
157
|
+
async def get_active_sync_key(self) -> WaAppStateSyncKey | None:
|
|
158
|
+
return _pick_active_sync_key(list(self._keys.values()))
|
|
159
|
+
|
|
160
|
+
async def get_collection_state(
|
|
161
|
+
self, collection: AppStateCollectionName
|
|
162
|
+
) -> WaAppStateCollectionStoreState:
|
|
163
|
+
state = self._collections.get(collection)
|
|
164
|
+
if state is None:
|
|
165
|
+
state = WaAppStateCollectionStoreState(
|
|
166
|
+
initialized=False,
|
|
167
|
+
version=0,
|
|
168
|
+
hash=APP_STATE_EMPTY_LT_HASH,
|
|
169
|
+
index_value_map={},
|
|
170
|
+
)
|
|
171
|
+
self._collections[collection] = state
|
|
172
|
+
return state
|
|
173
|
+
|
|
174
|
+
async def get_collection_states(
|
|
175
|
+
self, collections: Sequence[AppStateCollectionName]
|
|
176
|
+
) -> list[WaAppStateCollectionStoreState]:
|
|
177
|
+
return [await self.get_collection_state(name) for name in collections]
|
|
178
|
+
|
|
179
|
+
async def set_collection_states(
|
|
180
|
+
self, updates: Sequence[WaAppStateCollectionStateUpdate]
|
|
181
|
+
) -> None:
|
|
182
|
+
for update in updates:
|
|
183
|
+
index_value_map: dict[str, bytes] = {}
|
|
184
|
+
for index, value in update.index_value_map.items():
|
|
185
|
+
set_bounded_map_entry(
|
|
186
|
+
index_value_map,
|
|
187
|
+
index,
|
|
188
|
+
value,
|
|
189
|
+
self._max_collection_entries,
|
|
190
|
+
)
|
|
191
|
+
self._collections[update.collection] = WaAppStateCollectionStoreState(
|
|
192
|
+
initialized=True,
|
|
193
|
+
version=update.version,
|
|
194
|
+
hash=update.hash,
|
|
195
|
+
index_value_map=index_value_map,
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
async def clear(self) -> None:
|
|
199
|
+
self._keys.clear()
|
|
200
|
+
self._collections.clear()
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""In-memory auth store backend (mirrors ``src/store/memory/auth.store.ts``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from zapy._auth.types import WaAuthCredentials
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class WaAuthMemoryStore:
|
|
12
|
+
"""Single-credential memory store. Nothing survives process exit."""
|
|
13
|
+
|
|
14
|
+
def __init__(self) -> None:
|
|
15
|
+
self._credentials: WaAuthCredentials | None = None
|
|
16
|
+
|
|
17
|
+
async def load(self) -> WaAuthCredentials | None:
|
|
18
|
+
return self._credentials
|
|
19
|
+
|
|
20
|
+
async def save(self, credentials: WaAuthCredentials) -> None:
|
|
21
|
+
self._credentials = credentials
|
|
22
|
+
|
|
23
|
+
async def clear(self) -> None:
|
|
24
|
+
self._credentials = None
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""In-memory chat-metadata store (mirrors ``src/store/memory/chat-metadata.store.ts``).
|
|
2
|
+
|
|
3
|
+
Snapshots expire lazily on read plus an explicit ``cleanup_expired`` sweep;
|
|
4
|
+
there is no background timer. ``destroy`` only drops cached state.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import math
|
|
10
|
+
import time
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
from zapy._store.contracts.chat_metadata import WaChatMetadataSnapshot
|
|
15
|
+
from zapy._util.coercion import resolve_positive
|
|
16
|
+
from zapy._util.collections import set_bounded_map_entry
|
|
17
|
+
|
|
18
|
+
_DEFAULT_TTL_MS = 30 * 60 * 1000
|
|
19
|
+
_DEFAULT_MAX_CHATS = 2_048
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True, slots=True)
|
|
23
|
+
class WaChatMetadataMemoryStoreRecord(WaChatMetadataSnapshot):
|
|
24
|
+
"""Stored snapshot plus the expiry assigned at write time."""
|
|
25
|
+
|
|
26
|
+
expires_at_ms: int | float = 0
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True, slots=True)
|
|
30
|
+
class WaChatMetadataMemoryStoreOptions:
|
|
31
|
+
"""Bounds and logger for :class:`WaChatMetadataMemoryStore`."""
|
|
32
|
+
|
|
33
|
+
max_chats: int | None = None
|
|
34
|
+
logger: Any = None
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _now_ms() -> int:
|
|
38
|
+
return int(time.time() * 1000)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class WaChatMetadataMemoryStore:
|
|
42
|
+
"""In-memory chat metadata with TTL expiry."""
|
|
43
|
+
|
|
44
|
+
def __init__(
|
|
45
|
+
self,
|
|
46
|
+
ttl_ms: int | float = _DEFAULT_TTL_MS,
|
|
47
|
+
options: WaChatMetadataMemoryStoreOptions | None = None,
|
|
48
|
+
) -> None:
|
|
49
|
+
if (
|
|
50
|
+
isinstance(ttl_ms, bool)
|
|
51
|
+
or not isinstance(ttl_ms, (int, float))
|
|
52
|
+
or not math.isfinite(ttl_ms)
|
|
53
|
+
or ttl_ms <= 0
|
|
54
|
+
):
|
|
55
|
+
raise ValueError("chatMetadata ttlMs must be a positive finite number")
|
|
56
|
+
resolved = (
|
|
57
|
+
options if options is not None else WaChatMetadataMemoryStoreOptions()
|
|
58
|
+
)
|
|
59
|
+
self._records: dict[str, WaChatMetadataMemoryStoreRecord] = {}
|
|
60
|
+
self._ttl_ms = ttl_ms
|
|
61
|
+
self._max_chats = resolve_positive(
|
|
62
|
+
resolved.max_chats,
|
|
63
|
+
_DEFAULT_MAX_CHATS,
|
|
64
|
+
"WaChatMetadataMemoryStoreOptions.maxChats",
|
|
65
|
+
)
|
|
66
|
+
self._logger = resolved.logger
|
|
67
|
+
self._capacity_warned = False
|
|
68
|
+
|
|
69
|
+
def _warn_capacity(self) -> None:
|
|
70
|
+
if self._capacity_warned or self._logger is None:
|
|
71
|
+
return
|
|
72
|
+
self._capacity_warned = True
|
|
73
|
+
self._logger.warn(
|
|
74
|
+
"chat metadata store at capacity, evicting oldest",
|
|
75
|
+
{"max": self._max_chats},
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
async def upsert_chat_metadata(self, snapshot: WaChatMetadataSnapshot) -> None:
|
|
79
|
+
set_bounded_map_entry(
|
|
80
|
+
self._records,
|
|
81
|
+
snapshot.chat_jid,
|
|
82
|
+
WaChatMetadataMemoryStoreRecord(
|
|
83
|
+
chat_jid=snapshot.chat_jid,
|
|
84
|
+
updated_at_ms=snapshot.updated_at_ms,
|
|
85
|
+
ephemeral_expiration=snapshot.ephemeral_expiration,
|
|
86
|
+
ephemeral_setting_timestamp=snapshot.ephemeral_setting_timestamp,
|
|
87
|
+
expires_at_ms=snapshot.updated_at_ms + self._ttl_ms,
|
|
88
|
+
),
|
|
89
|
+
self._max_chats,
|
|
90
|
+
lambda _key, _value: self._warn_capacity(),
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
async def get_chat_metadata(
|
|
94
|
+
self, chat_jid: str, now_ms: int | float | None = None
|
|
95
|
+
) -> WaChatMetadataSnapshot | None:
|
|
96
|
+
record = self._records.get(chat_jid)
|
|
97
|
+
if record is None:
|
|
98
|
+
return None
|
|
99
|
+
now = _now_ms() if now_ms is None else now_ms
|
|
100
|
+
if record.expires_at_ms <= now:
|
|
101
|
+
del self._records[chat_jid]
|
|
102
|
+
return None
|
|
103
|
+
return record
|
|
104
|
+
|
|
105
|
+
async def delete_chat_metadata(self, chat_jid: str) -> int:
|
|
106
|
+
if chat_jid in self._records:
|
|
107
|
+
del self._records[chat_jid]
|
|
108
|
+
return 1
|
|
109
|
+
return 0
|
|
110
|
+
|
|
111
|
+
async def cleanup_expired(self, now_ms: int | float) -> int:
|
|
112
|
+
removed = 0
|
|
113
|
+
for chat_jid, record in list(self._records.items()):
|
|
114
|
+
if record.expires_at_ms > now_ms:
|
|
115
|
+
continue
|
|
116
|
+
del self._records[chat_jid]
|
|
117
|
+
removed += 1
|
|
118
|
+
return removed
|
|
119
|
+
|
|
120
|
+
async def clear(self) -> None:
|
|
121
|
+
self._records.clear()
|
|
122
|
+
|
|
123
|
+
async def destroy(self) -> None:
|
|
124
|
+
await self.clear()
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""In-memory contact store (mirrors ``src/store/memory/contact.store.ts``).
|
|
2
|
+
|
|
3
|
+
Contacts are held in LID-canonical form with a secondary phone-number
|
|
4
|
+
index so a PN-form lookup resolves to the same row.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from collections.abc import Sequence
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
|
|
12
|
+
from zapy._protocol import defaults as _defaults
|
|
13
|
+
from zapy._store.contracts.contact import WaStoredContactRecord
|
|
14
|
+
from zapy._util.coercion import resolve_positive
|
|
15
|
+
from zapy._util.collections import set_bounded_map_entry
|
|
16
|
+
|
|
17
|
+
_DEFAULT_MAX_CONTACTS = 20_000
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True, slots=True)
|
|
21
|
+
class WaContactMemoryStoreOptions:
|
|
22
|
+
"""Bounds for :class:`WaContactMemoryStore`."""
|
|
23
|
+
|
|
24
|
+
max_contacts: int | None = None
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _is_user_jid(jid: str) -> bool:
|
|
28
|
+
"""Mirror ``isUserJid``: true for ``<user>@s.whatsapp.net`` (no multi-@)."""
|
|
29
|
+
server = _defaults.HOST_DOMAIN
|
|
30
|
+
at_index = len(jid) - len(server) - 1
|
|
31
|
+
if at_index < 1 or jid[at_index] != "@" or not jid.endswith(server):
|
|
32
|
+
return False
|
|
33
|
+
for index in range(at_index):
|
|
34
|
+
if jid[index] == "@":
|
|
35
|
+
return False
|
|
36
|
+
return True
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _merge_contact_records(
|
|
40
|
+
previous: WaStoredContactRecord | None, incoming: WaStoredContactRecord
|
|
41
|
+
) -> WaStoredContactRecord:
|
|
42
|
+
if previous is None:
|
|
43
|
+
return incoming
|
|
44
|
+
return WaStoredContactRecord(
|
|
45
|
+
jid=incoming.jid,
|
|
46
|
+
display_name=incoming.display_name
|
|
47
|
+
if incoming.display_name is not None
|
|
48
|
+
else previous.display_name,
|
|
49
|
+
push_name=incoming.push_name
|
|
50
|
+
if incoming.push_name is not None
|
|
51
|
+
else previous.push_name,
|
|
52
|
+
lid=incoming.lid if incoming.lid is not None else previous.lid,
|
|
53
|
+
phone_number=incoming.phone_number
|
|
54
|
+
if incoming.phone_number is not None
|
|
55
|
+
else previous.phone_number,
|
|
56
|
+
username=incoming.username
|
|
57
|
+
if incoming.username is not None
|
|
58
|
+
else previous.username,
|
|
59
|
+
last_updated_ms=max(previous.last_updated_ms, incoming.last_updated_ms),
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class WaContactMemoryStore:
|
|
64
|
+
"""In-memory contact cache with a phone-number cross-reference index."""
|
|
65
|
+
|
|
66
|
+
def __init__(self, options: WaContactMemoryStoreOptions | None = None) -> None:
|
|
67
|
+
resolved = options if options is not None else WaContactMemoryStoreOptions()
|
|
68
|
+
self._contacts: dict[str, WaStoredContactRecord] = {}
|
|
69
|
+
self._phone_index: dict[str, str] = {}
|
|
70
|
+
self._max_contacts = resolve_positive(
|
|
71
|
+
resolved.max_contacts,
|
|
72
|
+
_DEFAULT_MAX_CONTACTS,
|
|
73
|
+
"WaContactMemoryStoreOptions.maxContacts",
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
def _write_record(self, record: WaStoredContactRecord) -> None:
|
|
77
|
+
previous = self._contacts.get(record.jid)
|
|
78
|
+
merged = _merge_contact_records(previous, record)
|
|
79
|
+
set_bounded_map_entry(self._contacts, merged.jid, merged, self._max_contacts)
|
|
80
|
+
if (
|
|
81
|
+
previous is not None
|
|
82
|
+
and previous.phone_number
|
|
83
|
+
and previous.phone_number != merged.phone_number
|
|
84
|
+
and self._phone_index.get(previous.phone_number) == merged.jid
|
|
85
|
+
):
|
|
86
|
+
del self._phone_index[previous.phone_number]
|
|
87
|
+
if merged.phone_number:
|
|
88
|
+
self._phone_index[merged.phone_number] = merged.jid
|
|
89
|
+
|
|
90
|
+
async def upsert(self, record: WaStoredContactRecord) -> None:
|
|
91
|
+
self._write_record(record)
|
|
92
|
+
|
|
93
|
+
async def upsert_batch(self, records: Sequence[WaStoredContactRecord]) -> None:
|
|
94
|
+
for record in records:
|
|
95
|
+
self._write_record(record)
|
|
96
|
+
|
|
97
|
+
async def get_by_jid(self, jid: str) -> WaStoredContactRecord | None:
|
|
98
|
+
direct = self._contacts.get(jid)
|
|
99
|
+
if direct is not None:
|
|
100
|
+
return direct
|
|
101
|
+
if _is_user_jid(jid):
|
|
102
|
+
return await self.get_by_phone_number(jid)
|
|
103
|
+
return None
|
|
104
|
+
|
|
105
|
+
async def get_by_phone_number(self, pn: str) -> WaStoredContactRecord | None:
|
|
106
|
+
jid = self._phone_index.get(pn)
|
|
107
|
+
if jid is None:
|
|
108
|
+
return None
|
|
109
|
+
return self._contacts.get(jid)
|
|
110
|
+
|
|
111
|
+
async def delete_by_jid(self, jid: str) -> int:
|
|
112
|
+
existing = self._contacts.get(jid)
|
|
113
|
+
if existing is None:
|
|
114
|
+
return 0
|
|
115
|
+
del self._contacts[jid]
|
|
116
|
+
if (
|
|
117
|
+
existing.phone_number
|
|
118
|
+
and self._phone_index.get(existing.phone_number) == jid
|
|
119
|
+
):
|
|
120
|
+
del self._phone_index[existing.phone_number]
|
|
121
|
+
return 1
|
|
122
|
+
|
|
123
|
+
async def clear(self) -> None:
|
|
124
|
+
self._contacts.clear()
|
|
125
|
+
self._phone_index.clear()
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"""In-memory device-list store (mirrors ``src/store/memory/device-list.store.ts``).
|
|
2
|
+
|
|
3
|
+
Snapshots expire lazily on read plus an explicit ``cleanup_expired`` sweep;
|
|
4
|
+
there is no background timer. ``destroy`` only drops cached state.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import math
|
|
10
|
+
import time
|
|
11
|
+
from collections.abc import Sequence
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
from zapy._store.contracts.device_list import WaDeviceListSnapshot
|
|
16
|
+
from zapy._util.coercion import resolve_positive
|
|
17
|
+
from zapy._util.collections import set_bounded_map_entry
|
|
18
|
+
|
|
19
|
+
_DEFAULT_TTL_MS = 5 * 60 * 1000
|
|
20
|
+
_DEFAULT_MAX_USERS = 16_384
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True, slots=True)
|
|
24
|
+
class WaDeviceListMemoryStoreRecord(WaDeviceListSnapshot):
|
|
25
|
+
"""Stored snapshot plus the expiry assigned at write time."""
|
|
26
|
+
|
|
27
|
+
expires_at_ms: int | float = 0
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True, slots=True)
|
|
31
|
+
class WaDeviceListMemoryStoreOptions:
|
|
32
|
+
"""Bounds and logger for :class:`WaDeviceListMemoryStore`."""
|
|
33
|
+
|
|
34
|
+
max_users: int | None = None
|
|
35
|
+
logger: Any = None
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _now_ms() -> int:
|
|
39
|
+
return int(time.time() * 1000)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class WaDeviceListMemoryStore:
|
|
43
|
+
"""In-memory per-user device lists with TTL expiry and an alt-JID index."""
|
|
44
|
+
|
|
45
|
+
def __init__(
|
|
46
|
+
self,
|
|
47
|
+
ttl_ms: int | float = _DEFAULT_TTL_MS,
|
|
48
|
+
options: WaDeviceListMemoryStoreOptions | None = None,
|
|
49
|
+
) -> None:
|
|
50
|
+
if (
|
|
51
|
+
isinstance(ttl_ms, bool)
|
|
52
|
+
or not isinstance(ttl_ms, (int, float))
|
|
53
|
+
or not math.isfinite(ttl_ms)
|
|
54
|
+
or ttl_ms <= 0
|
|
55
|
+
):
|
|
56
|
+
raise ValueError("device-list ttlMs must be a positive finite number")
|
|
57
|
+
resolved = options if options is not None else WaDeviceListMemoryStoreOptions()
|
|
58
|
+
self._records: dict[str, WaDeviceListMemoryStoreRecord] = {}
|
|
59
|
+
self._alt_index: dict[str, str] = {}
|
|
60
|
+
self._ttl_ms = ttl_ms
|
|
61
|
+
self._max_users = resolve_positive(
|
|
62
|
+
resolved.max_users,
|
|
63
|
+
_DEFAULT_MAX_USERS,
|
|
64
|
+
"WaDeviceListMemoryStoreOptions.maxUsers",
|
|
65
|
+
)
|
|
66
|
+
self._logger = resolved.logger
|
|
67
|
+
self._capacity_warned = False
|
|
68
|
+
|
|
69
|
+
def _warn_capacity(self) -> None:
|
|
70
|
+
if self._capacity_warned or self._logger is None:
|
|
71
|
+
return
|
|
72
|
+
self._capacity_warned = True
|
|
73
|
+
self._logger.warn(
|
|
74
|
+
"device list store at capacity, evicting oldest",
|
|
75
|
+
{"max": self._max_users},
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
def _read_live(
|
|
79
|
+
self, user_jid: str, now_ms: int | float
|
|
80
|
+
) -> WaDeviceListSnapshot | None:
|
|
81
|
+
record = self._records.get(user_jid)
|
|
82
|
+
if record is None:
|
|
83
|
+
return None
|
|
84
|
+
if record.expires_at_ms <= now_ms:
|
|
85
|
+
if record.alt_user_jid:
|
|
86
|
+
self._alt_index.pop(record.alt_user_jid, None)
|
|
87
|
+
del self._records[user_jid]
|
|
88
|
+
return None
|
|
89
|
+
return record
|
|
90
|
+
|
|
91
|
+
async def upsert_user_devices_batch(
|
|
92
|
+
self, snapshots: Sequence[WaDeviceListSnapshot]
|
|
93
|
+
) -> None:
|
|
94
|
+
for snapshot in snapshots:
|
|
95
|
+
previous = self._records.get(snapshot.user_jid)
|
|
96
|
+
if previous is not None and (
|
|
97
|
+
previous.alt_user_jid and previous.alt_user_jid != snapshot.alt_user_jid
|
|
98
|
+
):
|
|
99
|
+
self._alt_index.pop(previous.alt_user_jid, None)
|
|
100
|
+
set_bounded_map_entry(
|
|
101
|
+
self._records,
|
|
102
|
+
snapshot.user_jid,
|
|
103
|
+
WaDeviceListMemoryStoreRecord(
|
|
104
|
+
user_jid=snapshot.user_jid,
|
|
105
|
+
device_jids=snapshot.device_jids,
|
|
106
|
+
updated_at_ms=snapshot.updated_at_ms,
|
|
107
|
+
alt_user_jid=snapshot.alt_user_jid,
|
|
108
|
+
expires_at_ms=snapshot.updated_at_ms + self._ttl_ms,
|
|
109
|
+
),
|
|
110
|
+
self._max_users,
|
|
111
|
+
lambda _key, _value: self._warn_capacity(),
|
|
112
|
+
)
|
|
113
|
+
if snapshot.alt_user_jid:
|
|
114
|
+
self._alt_index[snapshot.alt_user_jid] = snapshot.user_jid
|
|
115
|
+
|
|
116
|
+
async def get_user_devices_batch(
|
|
117
|
+
self,
|
|
118
|
+
user_jids: Sequence[str],
|
|
119
|
+
now_ms: int | float | None = None,
|
|
120
|
+
) -> list[WaDeviceListSnapshot | None]:
|
|
121
|
+
now = _now_ms() if now_ms is None else now_ms
|
|
122
|
+
return [self._read_live(user_jid, now) for user_jid in user_jids]
|
|
123
|
+
|
|
124
|
+
async def find_by_any_user_jid(
|
|
125
|
+
self, jid: str, now_ms: int | float | None = None
|
|
126
|
+
) -> WaDeviceListSnapshot | None:
|
|
127
|
+
now = _now_ms() if now_ms is None else now_ms
|
|
128
|
+
direct = self._read_live(jid, now)
|
|
129
|
+
if direct is not None:
|
|
130
|
+
return direct
|
|
131
|
+
primary = self._alt_index.get(jid)
|
|
132
|
+
if primary is None:
|
|
133
|
+
return None
|
|
134
|
+
return self._read_live(primary, now)
|
|
135
|
+
|
|
136
|
+
async def delete_user_devices(self, user_jid: str) -> int:
|
|
137
|
+
record = self._records.get(user_jid)
|
|
138
|
+
if record is None:
|
|
139
|
+
return 0
|
|
140
|
+
if record.alt_user_jid:
|
|
141
|
+
self._alt_index.pop(record.alt_user_jid, None)
|
|
142
|
+
del self._records[user_jid]
|
|
143
|
+
return 1
|
|
144
|
+
|
|
145
|
+
async def cleanup_expired(self, now_ms: int | float) -> int:
|
|
146
|
+
removed = 0
|
|
147
|
+
for user_jid, record in list(self._records.items()):
|
|
148
|
+
if record.expires_at_ms > now_ms:
|
|
149
|
+
continue
|
|
150
|
+
if record.alt_user_jid:
|
|
151
|
+
self._alt_index.pop(record.alt_user_jid, None)
|
|
152
|
+
del self._records[user_jid]
|
|
153
|
+
removed += 1
|
|
154
|
+
return removed
|
|
155
|
+
|
|
156
|
+
async def clear(self) -> None:
|
|
157
|
+
self._records.clear()
|
|
158
|
+
self._alt_index.clear()
|
|
159
|
+
|
|
160
|
+
async def destroy(self) -> None:
|
|
161
|
+
await self.clear()
|