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,250 @@
|
|
|
1
|
+
"""In-memory retry store (mirrors ``src/store/memory/retry.store.ts``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import copy
|
|
6
|
+
import math
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from zapy._util.coercion import resolve_positive
|
|
11
|
+
from zapy._util.collections import set_bounded_map_entry
|
|
12
|
+
|
|
13
|
+
# DEVIATION: no background expiry timer (the port is synchronous with no
|
|
14
|
+
# threads per repo conventions); entries expire lazily via cleanup_expired
|
|
15
|
+
# and destroy() only clears the maps.
|
|
16
|
+
_DEFAULT_TTL_MS = 60 * 1000
|
|
17
|
+
_DEFAULT_MAX_OUTBOUND_MESSAGES = 10_000
|
|
18
|
+
_DEFAULT_MAX_INBOUND_COUNTERS = 20_000
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class WaRetryMemoryStoreOptions:
|
|
23
|
+
"""Options for :class:`WaRetryMemoryStore`."""
|
|
24
|
+
|
|
25
|
+
max_outbound_messages: int | float | None = None
|
|
26
|
+
max_inbound_counters: int | float | None = None
|
|
27
|
+
logger: Any = None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass
|
|
31
|
+
class _RetryInboundCounterRecord:
|
|
32
|
+
count: int
|
|
33
|
+
expires_at_ms: int | float
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class WaRetryMemoryStore:
|
|
37
|
+
"""In-memory outbound retry messages plus inbound retry counters."""
|
|
38
|
+
|
|
39
|
+
def __init__(
|
|
40
|
+
self,
|
|
41
|
+
ttl_ms: int | float = _DEFAULT_TTL_MS,
|
|
42
|
+
options: WaRetryMemoryStoreOptions | None = None,
|
|
43
|
+
) -> None:
|
|
44
|
+
if (
|
|
45
|
+
isinstance(ttl_ms, bool)
|
|
46
|
+
or not isinstance(ttl_ms, (int, float))
|
|
47
|
+
or not math.isfinite(ttl_ms)
|
|
48
|
+
or ttl_ms <= 0
|
|
49
|
+
):
|
|
50
|
+
raise ValueError("retry ttlMs must be a positive finite number")
|
|
51
|
+
resolved = options if options is not None else WaRetryMemoryStoreOptions()
|
|
52
|
+
self._outbound_messages: dict[str, Any] = {}
|
|
53
|
+
self._eligible_sets: dict[str, set[str]] = {}
|
|
54
|
+
self._inbound_counters: dict[str, _RetryInboundCounterRecord] = {}
|
|
55
|
+
self._ttl_ms = ttl_ms
|
|
56
|
+
self._max_outbound_messages = resolve_positive(
|
|
57
|
+
resolved.max_outbound_messages,
|
|
58
|
+
_DEFAULT_MAX_OUTBOUND_MESSAGES,
|
|
59
|
+
"WaRetryMemoryStoreOptions.maxOutboundMessages",
|
|
60
|
+
)
|
|
61
|
+
self._max_inbound_counters = resolve_positive(
|
|
62
|
+
resolved.max_inbound_counters,
|
|
63
|
+
_DEFAULT_MAX_INBOUND_COUNTERS,
|
|
64
|
+
"WaRetryMemoryStoreOptions.maxInboundCounters",
|
|
65
|
+
)
|
|
66
|
+
self._logger = resolved.logger
|
|
67
|
+
self._outbound_capacity_warned = False
|
|
68
|
+
self._inbound_capacity_warned = False
|
|
69
|
+
|
|
70
|
+
def _warn_outbound_capacity(self) -> None:
|
|
71
|
+
if self._outbound_capacity_warned or self._logger is None:
|
|
72
|
+
return
|
|
73
|
+
self._outbound_capacity_warned = True
|
|
74
|
+
self._logger.warn(
|
|
75
|
+
"retry outbound store at capacity, evicting oldest",
|
|
76
|
+
{"max": self._max_outbound_messages},
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
def _warn_inbound_capacity(self) -> None:
|
|
80
|
+
if self._inbound_capacity_warned or self._logger is None:
|
|
81
|
+
return
|
|
82
|
+
self._inbound_capacity_warned = True
|
|
83
|
+
self._logger.warn(
|
|
84
|
+
"retry inbound counters at capacity, evicting oldest",
|
|
85
|
+
{"max": self._max_inbound_counters},
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
def _on_outbound_evict(self, message_id: str, _record: Any) -> None:
|
|
89
|
+
self._eligible_sets.pop(message_id, None)
|
|
90
|
+
self._warn_outbound_capacity()
|
|
91
|
+
|
|
92
|
+
def _on_inbound_evict(self, _key: str, _record: _RetryInboundCounterRecord) -> None:
|
|
93
|
+
self._warn_inbound_capacity()
|
|
94
|
+
|
|
95
|
+
def get_ttl_ms(self) -> int | float:
|
|
96
|
+
"""Return the TTL this store was constructed with."""
|
|
97
|
+
return self._ttl_ms
|
|
98
|
+
|
|
99
|
+
def supports_raw_replay_payload(self) -> bool:
|
|
100
|
+
"""Always true: this store keeps the replay payload as given."""
|
|
101
|
+
return True
|
|
102
|
+
|
|
103
|
+
async def get_outbound_requester_status(
|
|
104
|
+
self, message_id: str, requester_device_jid: str
|
|
105
|
+
) -> dict[str, bool] | None:
|
|
106
|
+
"""Eligibility/delivery of ``requester_device_jid`` for ``message_id``."""
|
|
107
|
+
current = self._outbound_messages.get(message_id)
|
|
108
|
+
if current is None:
|
|
109
|
+
return None
|
|
110
|
+
eligible = current.eligible_requester_device_jids
|
|
111
|
+
if not eligible:
|
|
112
|
+
return None
|
|
113
|
+
eligible_set = self._eligible_sets.get(current.message_id)
|
|
114
|
+
if eligible_set is not None:
|
|
115
|
+
is_eligible = requester_device_jid in eligible_set
|
|
116
|
+
else:
|
|
117
|
+
is_eligible = requester_device_jid in eligible
|
|
118
|
+
if not is_eligible:
|
|
119
|
+
return {"eligible": False, "delivered": False}
|
|
120
|
+
delivered = current.delivered_requester_device_jids
|
|
121
|
+
if delivered is not None and requester_device_jid in delivered:
|
|
122
|
+
return {"eligible": True, "delivered": True}
|
|
123
|
+
return {"eligible": True, "delivered": False}
|
|
124
|
+
|
|
125
|
+
async def upsert_outbound_message(self, record: Any) -> None:
|
|
126
|
+
"""Store a copy of ``record``, evicting the oldest message when full."""
|
|
127
|
+
stored = copy.copy(record)
|
|
128
|
+
eligible = record.eligible_requester_device_jids
|
|
129
|
+
delivered = record.delivered_requester_device_jids
|
|
130
|
+
stored.eligible_requester_device_jids = (
|
|
131
|
+
list(eligible) if eligible is not None else None
|
|
132
|
+
)
|
|
133
|
+
stored.delivered_requester_device_jids = (
|
|
134
|
+
list(delivered) if delivered is not None else None
|
|
135
|
+
)
|
|
136
|
+
set_bounded_map_entry(
|
|
137
|
+
self._outbound_messages,
|
|
138
|
+
record.message_id,
|
|
139
|
+
stored,
|
|
140
|
+
self._max_outbound_messages,
|
|
141
|
+
self._on_outbound_evict,
|
|
142
|
+
)
|
|
143
|
+
if eligible:
|
|
144
|
+
self._eligible_sets[record.message_id] = set(eligible)
|
|
145
|
+
else:
|
|
146
|
+
self._eligible_sets.pop(record.message_id, None)
|
|
147
|
+
|
|
148
|
+
async def delete_outbound_message(self, message_id: str) -> int:
|
|
149
|
+
"""Remove ``message_id``; return 1 when something was removed, else 0."""
|
|
150
|
+
existed = self._outbound_messages.pop(message_id, None) is not None
|
|
151
|
+
self._eligible_sets.pop(message_id, None)
|
|
152
|
+
return 1 if existed else 0
|
|
153
|
+
|
|
154
|
+
async def get_outbound_message(self, message_id: str) -> Any | None:
|
|
155
|
+
"""Return the stored record for ``message_id``, or ``None``."""
|
|
156
|
+
return self._outbound_messages.get(message_id)
|
|
157
|
+
|
|
158
|
+
async def update_outbound_message_state(
|
|
159
|
+
self,
|
|
160
|
+
message_id: str,
|
|
161
|
+
state: Any,
|
|
162
|
+
updated_at_ms: int | float,
|
|
163
|
+
expires_at_ms: int | float,
|
|
164
|
+
) -> None:
|
|
165
|
+
"""Refresh state and timestamps of ``message_id``; no-op when absent."""
|
|
166
|
+
current = self._outbound_messages.get(message_id)
|
|
167
|
+
if current is None:
|
|
168
|
+
return
|
|
169
|
+
stored = copy.copy(current)
|
|
170
|
+
stored.state = state
|
|
171
|
+
stored.updated_at_ms = updated_at_ms
|
|
172
|
+
stored.expires_at_ms = expires_at_ms
|
|
173
|
+
set_bounded_map_entry(
|
|
174
|
+
self._outbound_messages,
|
|
175
|
+
message_id,
|
|
176
|
+
stored,
|
|
177
|
+
self._max_outbound_messages,
|
|
178
|
+
self._on_outbound_evict,
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
async def mark_outbound_requester_delivered(
|
|
182
|
+
self,
|
|
183
|
+
message_id: str,
|
|
184
|
+
requester_device_jid: str,
|
|
185
|
+
updated_at_ms: int | float,
|
|
186
|
+
expires_at_ms: int | float,
|
|
187
|
+
) -> None:
|
|
188
|
+
"""Record a delivery receipt; no-op when ``message_id`` is absent."""
|
|
189
|
+
current = self._outbound_messages.get(message_id)
|
|
190
|
+
if current is None:
|
|
191
|
+
return
|
|
192
|
+
delivered = set(current.delivered_requester_device_jids or [])
|
|
193
|
+
delivered.add(requester_device_jid)
|
|
194
|
+
stored = copy.copy(current)
|
|
195
|
+
stored.delivered_requester_device_jids = list(delivered)
|
|
196
|
+
stored.updated_at_ms = updated_at_ms
|
|
197
|
+
stored.expires_at_ms = expires_at_ms
|
|
198
|
+
set_bounded_map_entry(
|
|
199
|
+
self._outbound_messages,
|
|
200
|
+
message_id,
|
|
201
|
+
stored,
|
|
202
|
+
self._max_outbound_messages,
|
|
203
|
+
self._on_outbound_evict,
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
async def increment_inbound_counter(
|
|
207
|
+
self,
|
|
208
|
+
message_id: str,
|
|
209
|
+
requester_jid: str,
|
|
210
|
+
_updated_at_ms: int | float,
|
|
211
|
+
expires_at_ms: int | float,
|
|
212
|
+
) -> int:
|
|
213
|
+
"""Bump the retry counter for a ``messageId|requesterJid`` pair."""
|
|
214
|
+
key = f"{message_id}|{requester_jid}"
|
|
215
|
+
current = self._inbound_counters.get(key)
|
|
216
|
+
count = current.count + 1 if current is not None else 1
|
|
217
|
+
set_bounded_map_entry(
|
|
218
|
+
self._inbound_counters,
|
|
219
|
+
key,
|
|
220
|
+
_RetryInboundCounterRecord(count=count, expires_at_ms=expires_at_ms),
|
|
221
|
+
self._max_inbound_counters,
|
|
222
|
+
self._on_inbound_evict,
|
|
223
|
+
)
|
|
224
|
+
return count
|
|
225
|
+
|
|
226
|
+
async def cleanup_expired(self, now_ms: int | float) -> int:
|
|
227
|
+
"""Drop outbound messages and counters with ``expiresAtMs <= nowMs``."""
|
|
228
|
+
removed = 0
|
|
229
|
+
for message_id, record in list(self._outbound_messages.items()):
|
|
230
|
+
if record.expires_at_ms > now_ms:
|
|
231
|
+
continue
|
|
232
|
+
del self._outbound_messages[message_id]
|
|
233
|
+
self._eligible_sets.pop(message_id, None)
|
|
234
|
+
removed += 1
|
|
235
|
+
for key, record in list(self._inbound_counters.items()):
|
|
236
|
+
if record.expires_at_ms > now_ms:
|
|
237
|
+
continue
|
|
238
|
+
del self._inbound_counters[key]
|
|
239
|
+
removed += 1
|
|
240
|
+
return removed
|
|
241
|
+
|
|
242
|
+
async def clear(self) -> None:
|
|
243
|
+
"""Drop every outbound message and inbound counter."""
|
|
244
|
+
self._outbound_messages.clear()
|
|
245
|
+
self._eligible_sets.clear()
|
|
246
|
+
self._inbound_counters.clear()
|
|
247
|
+
|
|
248
|
+
async def destroy(self) -> None:
|
|
249
|
+
"""Clear all state (no background timer exists in this port)."""
|
|
250
|
+
await self.clear()
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""In-memory sender-key backend (mirrors ``sender-key.store.ts``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from zapy._store.contracts.sender_key import WaGroupSenderKeyList, WaSenderKeyStore
|
|
9
|
+
from zapy._store.memory.session import signal_address_key
|
|
10
|
+
from zapy._util.coercion import resolve_positive
|
|
11
|
+
from zapy._util.collections import set_bounded_map_entry
|
|
12
|
+
|
|
13
|
+
_DEFAULT_MAX_SENDER_KEYS = 8_192
|
|
14
|
+
_DEFAULT_MAX_SENDER_DISTRIBUTIONS = 8_192
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _make_key(group_id: str, sender: Any) -> str:
|
|
18
|
+
return f"{group_id}|{signal_address_key(sender)}"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class SenderKeyMemoryStore(WaSenderKeyStore):
|
|
22
|
+
"""In-memory group sender keys and distributions with size bounds."""
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
max_sender_keys: int | float | None = None,
|
|
27
|
+
max_sender_distributions: int | float | None = None,
|
|
28
|
+
) -> None:
|
|
29
|
+
self._sender_keys: dict[str, Any] = {}
|
|
30
|
+
self._sender_distributions: dict[str, Any] = {}
|
|
31
|
+
self._max_sender_keys = resolve_positive(
|
|
32
|
+
max_sender_keys,
|
|
33
|
+
_DEFAULT_MAX_SENDER_KEYS,
|
|
34
|
+
"WaSenderKeyMemoryStoreOptions.maxSenderKeys",
|
|
35
|
+
)
|
|
36
|
+
self._max_sender_distributions = resolve_positive(
|
|
37
|
+
max_sender_distributions,
|
|
38
|
+
_DEFAULT_MAX_SENDER_DISTRIBUTIONS,
|
|
39
|
+
"WaSenderKeyMemoryStoreOptions.maxSenderDistributions",
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
async def upsert_sender_key(self, record: Any) -> None:
|
|
43
|
+
key = _make_key(record.group_id, record.sender)
|
|
44
|
+
set_bounded_map_entry(
|
|
45
|
+
self._sender_keys,
|
|
46
|
+
key,
|
|
47
|
+
record,
|
|
48
|
+
self._max_sender_keys,
|
|
49
|
+
self._on_sender_key_evict,
|
|
50
|
+
)
|
|
51
|
+
self.on_entry_access(self._sender_keys, key)
|
|
52
|
+
|
|
53
|
+
async def upsert_sender_key_distribution(self, record: Any) -> None:
|
|
54
|
+
key = _make_key(record.group_id, record.sender)
|
|
55
|
+
set_bounded_map_entry(
|
|
56
|
+
self._sender_distributions,
|
|
57
|
+
key,
|
|
58
|
+
record,
|
|
59
|
+
self._max_sender_distributions,
|
|
60
|
+
self._on_distribution_evict,
|
|
61
|
+
)
|
|
62
|
+
self.on_entry_access(self._sender_distributions, key)
|
|
63
|
+
|
|
64
|
+
async def upsert_sender_key_distributions(self, records: Sequence[Any]) -> None:
|
|
65
|
+
for record in records:
|
|
66
|
+
key = _make_key(record.group_id, record.sender)
|
|
67
|
+
set_bounded_map_entry(
|
|
68
|
+
self._sender_distributions,
|
|
69
|
+
key,
|
|
70
|
+
record,
|
|
71
|
+
self._max_sender_distributions,
|
|
72
|
+
self._on_distribution_evict,
|
|
73
|
+
)
|
|
74
|
+
self.on_entry_access(self._sender_distributions, key)
|
|
75
|
+
|
|
76
|
+
async def get_group_sender_key_list(self, group_id: str) -> WaGroupSenderKeyList:
|
|
77
|
+
sk_list = [
|
|
78
|
+
record
|
|
79
|
+
for record in self._sender_keys.values()
|
|
80
|
+
if record.group_id == group_id
|
|
81
|
+
]
|
|
82
|
+
sk_distrib_list = [
|
|
83
|
+
record
|
|
84
|
+
for record in self._sender_distributions.values()
|
|
85
|
+
if record.group_id == group_id
|
|
86
|
+
]
|
|
87
|
+
return WaGroupSenderKeyList(sk_list=sk_list, sk_distrib_list=sk_distrib_list)
|
|
88
|
+
|
|
89
|
+
async def get_device_sender_key(self, group_id: str, sender: Any) -> Any | None:
|
|
90
|
+
key = _make_key(group_id, sender)
|
|
91
|
+
record = self._sender_keys.get(key)
|
|
92
|
+
if record is None:
|
|
93
|
+
return None
|
|
94
|
+
self.on_entry_access(self._sender_keys, key)
|
|
95
|
+
return record
|
|
96
|
+
|
|
97
|
+
async def get_device_sender_key_distributions(
|
|
98
|
+
self, group_id: str, senders: Sequence[Any]
|
|
99
|
+
) -> list[Any | None]:
|
|
100
|
+
records: list[Any | None] = [None] * len(senders)
|
|
101
|
+
for index, sender in enumerate(senders):
|
|
102
|
+
key = _make_key(group_id, sender)
|
|
103
|
+
record = self._sender_distributions.get(key)
|
|
104
|
+
if record is None:
|
|
105
|
+
records[index] = None
|
|
106
|
+
else:
|
|
107
|
+
self.on_entry_access(self._sender_distributions, key)
|
|
108
|
+
records[index] = record
|
|
109
|
+
return records
|
|
110
|
+
|
|
111
|
+
async def delete_device_sender_key(
|
|
112
|
+
self, target: Any, group_id: str | None = None
|
|
113
|
+
) -> int:
|
|
114
|
+
deleted = 0
|
|
115
|
+
deleted += self._delete_matching(self._sender_keys, target, group_id)
|
|
116
|
+
deleted += self._delete_matching(self._sender_distributions, target, group_id)
|
|
117
|
+
return deleted
|
|
118
|
+
|
|
119
|
+
async def mark_forget_sender_key(
|
|
120
|
+
self, group_id: str, participants: Sequence[Any]
|
|
121
|
+
) -> int:
|
|
122
|
+
deleted = 0
|
|
123
|
+
for participant in participants:
|
|
124
|
+
deleted += self._delete_matching(self._sender_keys, participant, group_id)
|
|
125
|
+
deleted += self._delete_matching(
|
|
126
|
+
self._sender_distributions, participant, group_id
|
|
127
|
+
)
|
|
128
|
+
return deleted
|
|
129
|
+
|
|
130
|
+
async def clear(self) -> None:
|
|
131
|
+
self._sender_keys.clear()
|
|
132
|
+
self._sender_distributions.clear()
|
|
133
|
+
self.on_entries_clear()
|
|
134
|
+
|
|
135
|
+
def on_entry_access(self, entries: dict[str, Any], key: str) -> None:
|
|
136
|
+
"""Runs with the map/key of every read hit and write."""
|
|
137
|
+
|
|
138
|
+
def on_entry_remove(self, entries: dict[str, Any], key: str) -> None:
|
|
139
|
+
"""Runs with the map/key of every deleted/evicted entry."""
|
|
140
|
+
|
|
141
|
+
def on_entries_clear(self) -> None:
|
|
142
|
+
"""Runs on clear. No-op unless overridden."""
|
|
143
|
+
|
|
144
|
+
def _on_sender_key_evict(self, key: str, _value: Any) -> None:
|
|
145
|
+
self.on_entry_remove(self._sender_keys, key)
|
|
146
|
+
|
|
147
|
+
def _on_distribution_evict(self, key: str, _value: Any) -> None:
|
|
148
|
+
self.on_entry_remove(self._sender_distributions, key)
|
|
149
|
+
|
|
150
|
+
def _delete_matching(
|
|
151
|
+
self, entries: dict[str, Any], target: Any, group_id: str | None
|
|
152
|
+
) -> int:
|
|
153
|
+
deleted = 0
|
|
154
|
+
target_address_key = signal_address_key(target)
|
|
155
|
+
for key, record in list(entries.items()):
|
|
156
|
+
same_group = True if not group_id else record.group_id == group_id
|
|
157
|
+
same_address = signal_address_key(record.sender) == target_address_key
|
|
158
|
+
if same_group and same_address:
|
|
159
|
+
del entries[key]
|
|
160
|
+
self.on_entry_remove(entries, key)
|
|
161
|
+
deleted += 1
|
|
162
|
+
return deleted
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""In-memory session store backend (mirrors ``src/store/memory/session.store.ts``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from zapy._protocol.defaults import HOST_DOMAIN
|
|
9
|
+
from zapy._store.contracts.session import WaSessionStore
|
|
10
|
+
from zapy._util.coercion import resolve_positive
|
|
11
|
+
from zapy._util.collections import set_bounded_map_entry
|
|
12
|
+
|
|
13
|
+
_DEFAULT_MAX_SESSIONS = 8_192
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def signal_address_key(address: Any) -> str:
|
|
17
|
+
"""Key ``user|server|device`` for an address (mirrors ``signalAddressKey``)."""
|
|
18
|
+
server = getattr(address, "server", None)
|
|
19
|
+
if server is None:
|
|
20
|
+
server = HOST_DOMAIN
|
|
21
|
+
return f"{address.user}|{server}|{address.device}"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class WaSessionMemoryStore(WaSessionStore):
|
|
25
|
+
"""In-memory Signal sessions with a size bound and access/removal hooks."""
|
|
26
|
+
|
|
27
|
+
def __init__(self, max_sessions: int | float | None = None) -> None:
|
|
28
|
+
self._signal_sessions: dict[str, Any] = {}
|
|
29
|
+
self._max_sessions = resolve_positive(
|
|
30
|
+
max_sessions,
|
|
31
|
+
_DEFAULT_MAX_SESSIONS,
|
|
32
|
+
"WaSessionMemoryStoreOptions.maxSessions",
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
async def has_session(self, address: Any) -> bool:
|
|
36
|
+
key = signal_address_key(address)
|
|
37
|
+
if key not in self._signal_sessions:
|
|
38
|
+
return False
|
|
39
|
+
self.on_entry_access(key)
|
|
40
|
+
return True
|
|
41
|
+
|
|
42
|
+
async def has_sessions(self, addresses: Sequence[Any]) -> list[bool]:
|
|
43
|
+
result = [False] * len(addresses)
|
|
44
|
+
for index, address in enumerate(addresses):
|
|
45
|
+
key = signal_address_key(address)
|
|
46
|
+
found = key in self._signal_sessions
|
|
47
|
+
if found:
|
|
48
|
+
self.on_entry_access(key)
|
|
49
|
+
result[index] = found
|
|
50
|
+
return result
|
|
51
|
+
|
|
52
|
+
async def get_session(self, address: Any) -> Any | None:
|
|
53
|
+
key = signal_address_key(address)
|
|
54
|
+
session = self._signal_sessions.get(key)
|
|
55
|
+
if session is None:
|
|
56
|
+
return None
|
|
57
|
+
self.on_entry_access(key)
|
|
58
|
+
return session
|
|
59
|
+
|
|
60
|
+
async def get_sessions_batch(self, addresses: Sequence[Any]) -> list[Any | None]:
|
|
61
|
+
result: list[Any | None] = [None] * len(addresses)
|
|
62
|
+
for index, address in enumerate(addresses):
|
|
63
|
+
key = signal_address_key(address)
|
|
64
|
+
session = self._signal_sessions.get(key)
|
|
65
|
+
if session is None:
|
|
66
|
+
result[index] = None
|
|
67
|
+
else:
|
|
68
|
+
self.on_entry_access(key)
|
|
69
|
+
result[index] = session
|
|
70
|
+
return result
|
|
71
|
+
|
|
72
|
+
async def set_session(self, address: Any, session: Any) -> None:
|
|
73
|
+
key = signal_address_key(address)
|
|
74
|
+
set_bounded_map_entry(
|
|
75
|
+
self._signal_sessions, key, session, self._max_sessions, self._on_evict
|
|
76
|
+
)
|
|
77
|
+
self.on_entry_access(key)
|
|
78
|
+
|
|
79
|
+
async def set_sessions_batch(self, entries: Sequence[Any]) -> None:
|
|
80
|
+
for entry in entries:
|
|
81
|
+
key = signal_address_key(entry.address)
|
|
82
|
+
set_bounded_map_entry(
|
|
83
|
+
self._signal_sessions,
|
|
84
|
+
key,
|
|
85
|
+
entry.session,
|
|
86
|
+
self._max_sessions,
|
|
87
|
+
self._on_evict,
|
|
88
|
+
)
|
|
89
|
+
self.on_entry_access(key)
|
|
90
|
+
|
|
91
|
+
async def delete_session(self, address: Any) -> None:
|
|
92
|
+
key = signal_address_key(address)
|
|
93
|
+
if key in self._signal_sessions:
|
|
94
|
+
del self._signal_sessions[key]
|
|
95
|
+
self.on_entry_remove(key)
|
|
96
|
+
|
|
97
|
+
async def clear(self) -> None:
|
|
98
|
+
self._signal_sessions.clear()
|
|
99
|
+
self.on_entries_clear()
|
|
100
|
+
|
|
101
|
+
def on_entry_access(self, key: str) -> None:
|
|
102
|
+
"""Runs with the key of every read hit and write. No-op unless overridden."""
|
|
103
|
+
|
|
104
|
+
def on_entry_remove(self, key: str) -> None:
|
|
105
|
+
"""Runs with the key of every deleted/evicted entry. No-op unless overridden."""
|
|
106
|
+
|
|
107
|
+
def on_entries_clear(self) -> None:
|
|
108
|
+
"""Runs on clear. No-op unless overridden."""
|
|
109
|
+
|
|
110
|
+
def _on_evict(self, key: str, _value: Any) -> None:
|
|
111
|
+
self.on_entry_remove(key)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""In-memory signal store backend (mirrors ``src/store/memory/signal.store.ts``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from zapy._signal.types import RegistrationInfo, SignedPreKeyRecord
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class WaSignalMemoryStore:
|
|
12
|
+
"""In-memory registration info + signed pre-key."""
|
|
13
|
+
|
|
14
|
+
def __init__(self) -> None:
|
|
15
|
+
self._registration_info: RegistrationInfo | None = None
|
|
16
|
+
self._signed_pre_key: SignedPreKeyRecord | None = None
|
|
17
|
+
self._signed_pre_key_rotation_ts: int | None = None
|
|
18
|
+
|
|
19
|
+
async def get_registration_info(self) -> RegistrationInfo | None:
|
|
20
|
+
return self._registration_info
|
|
21
|
+
|
|
22
|
+
async def set_registration_info(self, info: RegistrationInfo) -> None:
|
|
23
|
+
self._registration_info = info
|
|
24
|
+
|
|
25
|
+
async def get_signed_pre_key(self) -> SignedPreKeyRecord | None:
|
|
26
|
+
return self._signed_pre_key
|
|
27
|
+
|
|
28
|
+
async def set_signed_pre_key(self, record: SignedPreKeyRecord) -> None:
|
|
29
|
+
self._signed_pre_key = record
|
|
30
|
+
|
|
31
|
+
async def get_signed_pre_key_by_id(self, key_id: int) -> SignedPreKeyRecord | None:
|
|
32
|
+
if self._signed_pre_key is None:
|
|
33
|
+
return None
|
|
34
|
+
return self._signed_pre_key if self._signed_pre_key.key_id == key_id else None
|
|
35
|
+
|
|
36
|
+
async def set_signed_pre_key_rotation_ts(self, value: int | None) -> None:
|
|
37
|
+
self._signed_pre_key_rotation_ts = value
|
|
38
|
+
|
|
39
|
+
async def get_signed_pre_key_rotation_ts(self) -> int | None:
|
|
40
|
+
return self._signed_pre_key_rotation_ts
|
|
41
|
+
|
|
42
|
+
async def clear(self) -> None:
|
|
43
|
+
self._registration_info = None
|
|
44
|
+
self._signed_pre_key = None
|
|
45
|
+
self._signed_pre_key_rotation_ts = None
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""In-memory thread store (mirrors ``src/store/memory/thread.store.ts``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from zapy._util.coercion import resolve_positive
|
|
10
|
+
from zapy._util.collections import normalize_query_limit, set_bounded_map_entry
|
|
11
|
+
|
|
12
|
+
_DEFAULT_MAX_THREADS = 10_000
|
|
13
|
+
_DEFAULT_LIST_LIMIT = 100
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class WaThreadMemoryStoreOptions:
|
|
18
|
+
"""Options for :class:`WaThreadMemoryStore`."""
|
|
19
|
+
|
|
20
|
+
max_threads: int | float | None = None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class WaThreadMemoryStore:
|
|
24
|
+
"""Bounded in-memory thread index keyed by thread JID."""
|
|
25
|
+
|
|
26
|
+
def __init__(self, options: WaThreadMemoryStoreOptions | None = None) -> None:
|
|
27
|
+
self._threads: dict[str, Any] = {}
|
|
28
|
+
self._max_threads = resolve_positive(
|
|
29
|
+
options.max_threads if options is not None else None,
|
|
30
|
+
_DEFAULT_MAX_THREADS,
|
|
31
|
+
"WaThreadMemoryStoreOptions.maxThreads",
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
async def upsert(self, record: Any) -> None:
|
|
35
|
+
"""Insert or replace ``record`` keyed by its JID, evicting oldest first."""
|
|
36
|
+
set_bounded_map_entry(self._threads, record.jid, record, self._max_threads)
|
|
37
|
+
|
|
38
|
+
async def upsert_batch(self, records: Sequence[Any]) -> None:
|
|
39
|
+
"""Insert or replace each record in order, evicting oldest first."""
|
|
40
|
+
for record in records:
|
|
41
|
+
set_bounded_map_entry(self._threads, record.jid, record, self._max_threads)
|
|
42
|
+
|
|
43
|
+
async def get_by_jid(self, jid: str) -> Any | None:
|
|
44
|
+
"""Return the record for ``jid``, or ``None`` when absent."""
|
|
45
|
+
return self._threads.get(jid)
|
|
46
|
+
|
|
47
|
+
async def list(self, limit: int | float | None = None) -> list[Any]:
|
|
48
|
+
"""Insertion-order records capped at ``limit`` (default 100)."""
|
|
49
|
+
normalized_limit = normalize_query_limit(limit, _DEFAULT_LIST_LIMIT)
|
|
50
|
+
out: list[Any] = []
|
|
51
|
+
for thread in self._threads.values():
|
|
52
|
+
out.append(thread)
|
|
53
|
+
if len(out) >= normalized_limit:
|
|
54
|
+
break
|
|
55
|
+
return out
|
|
56
|
+
|
|
57
|
+
async def delete_by_jid(self, jid: str) -> int:
|
|
58
|
+
"""Remove ``jid``; return 1 when something was removed, else 0."""
|
|
59
|
+
if jid in self._threads:
|
|
60
|
+
del self._threads[jid]
|
|
61
|
+
return 1
|
|
62
|
+
return 0
|
|
63
|
+
|
|
64
|
+
async def clear(self) -> None:
|
|
65
|
+
"""Drop every stored thread."""
|
|
66
|
+
self._threads.clear()
|