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,125 @@
|
|
|
1
|
+
"""In-memory group-metadata store (mirrors ``group-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.group_metadata import WaGroupMetadataSnapshot
|
|
15
|
+
from zapy._util.coercion import resolve_positive
|
|
16
|
+
from zapy._util.collections import set_bounded_map_entry
|
|
17
|
+
|
|
18
|
+
_DEFAULT_TTL_MS = 5 * 60 * 1000
|
|
19
|
+
_DEFAULT_MAX_GROUPS = 4_096
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True, slots=True)
|
|
23
|
+
class WaGroupMetadataMemoryStoreRecord(WaGroupMetadataSnapshot):
|
|
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 WaGroupMetadataMemoryStoreOptions:
|
|
31
|
+
"""Bounds and logger for :class:`WaGroupMetadataMemoryStore`."""
|
|
32
|
+
|
|
33
|
+
max_groups: int | None = None
|
|
34
|
+
logger: Any = None
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _now_ms() -> int:
|
|
38
|
+
return int(time.time() * 1000)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class WaGroupMetadataMemoryStore:
|
|
42
|
+
"""In-memory group metadata with TTL expiry."""
|
|
43
|
+
|
|
44
|
+
def __init__(
|
|
45
|
+
self,
|
|
46
|
+
ttl_ms: int | float = _DEFAULT_TTL_MS,
|
|
47
|
+
options: WaGroupMetadataMemoryStoreOptions | 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("groupMetadata ttlMs must be a positive finite number")
|
|
56
|
+
resolved = (
|
|
57
|
+
options if options is not None else WaGroupMetadataMemoryStoreOptions()
|
|
58
|
+
)
|
|
59
|
+
self._records: dict[str, WaGroupMetadataMemoryStoreRecord] = {}
|
|
60
|
+
self._ttl_ms = ttl_ms
|
|
61
|
+
self._max_groups = resolve_positive(
|
|
62
|
+
resolved.max_groups,
|
|
63
|
+
_DEFAULT_MAX_GROUPS,
|
|
64
|
+
"WaGroupMetadataMemoryStoreOptions.maxGroups",
|
|
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
|
+
"group metadata store at capacity, evicting oldest",
|
|
75
|
+
{"max": self._max_groups},
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
async def upsert_group_metadata(self, snapshot: WaGroupMetadataSnapshot) -> None:
|
|
79
|
+
set_bounded_map_entry(
|
|
80
|
+
self._records,
|
|
81
|
+
snapshot.group_jid,
|
|
82
|
+
WaGroupMetadataMemoryStoreRecord(
|
|
83
|
+
group_jid=snapshot.group_jid,
|
|
84
|
+
participants=snapshot.participants,
|
|
85
|
+
updated_at_ms=snapshot.updated_at_ms,
|
|
86
|
+
ephemeral=snapshot.ephemeral,
|
|
87
|
+
ephemeral_trigger=snapshot.ephemeral_trigger,
|
|
88
|
+
expires_at_ms=snapshot.updated_at_ms + self._ttl_ms,
|
|
89
|
+
),
|
|
90
|
+
self._max_groups,
|
|
91
|
+
lambda _key, _value: self._warn_capacity(),
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
async def get_group_metadata(
|
|
95
|
+
self, group_jid: str, now_ms: int | float | None = None
|
|
96
|
+
) -> WaGroupMetadataSnapshot | None:
|
|
97
|
+
record = self._records.get(group_jid)
|
|
98
|
+
if record is None:
|
|
99
|
+
return None
|
|
100
|
+
now = _now_ms() if now_ms is None else now_ms
|
|
101
|
+
if record.expires_at_ms <= now:
|
|
102
|
+
del self._records[group_jid]
|
|
103
|
+
return None
|
|
104
|
+
return record
|
|
105
|
+
|
|
106
|
+
async def delete_group_metadata(self, group_jid: str) -> int:
|
|
107
|
+
if group_jid in self._records:
|
|
108
|
+
del self._records[group_jid]
|
|
109
|
+
return 1
|
|
110
|
+
return 0
|
|
111
|
+
|
|
112
|
+
async def cleanup_expired(self, now_ms: int | float) -> int:
|
|
113
|
+
removed = 0
|
|
114
|
+
for group_jid, record in list(self._records.items()):
|
|
115
|
+
if record.expires_at_ms > now_ms:
|
|
116
|
+
continue
|
|
117
|
+
del self._records[group_jid]
|
|
118
|
+
removed += 1
|
|
119
|
+
return removed
|
|
120
|
+
|
|
121
|
+
async def clear(self) -> None:
|
|
122
|
+
self._records.clear()
|
|
123
|
+
|
|
124
|
+
async def destroy(self) -> None:
|
|
125
|
+
await self.clear()
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""In-memory identity store backend (mirrors ``src/store/memory/identity.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.identity import WaIdentityStore
|
|
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_REMOTE_IDENTITIES = 8_192
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class WaIdentityMemoryStore(WaIdentityStore):
|
|
17
|
+
"""In-memory remote Signal identities with a size bound."""
|
|
18
|
+
|
|
19
|
+
def __init__(self, max_remote_identities: int | float | None = None) -> None:
|
|
20
|
+
self._remote_identities: dict[str, bytes] = {}
|
|
21
|
+
self._max_remote_identities = resolve_positive(
|
|
22
|
+
max_remote_identities,
|
|
23
|
+
_DEFAULT_MAX_REMOTE_IDENTITIES,
|
|
24
|
+
"WaIdentityMemoryStoreOptions.maxRemoteIdentities",
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
async def get_remote_identity(self, address: Any) -> bytes | None:
|
|
28
|
+
key = signal_address_key(address)
|
|
29
|
+
identity_key = self._remote_identities.get(key)
|
|
30
|
+
if identity_key is None:
|
|
31
|
+
return None
|
|
32
|
+
self.on_entry_access(key)
|
|
33
|
+
return identity_key
|
|
34
|
+
|
|
35
|
+
async def get_remote_identities(
|
|
36
|
+
self, addresses: Sequence[Any]
|
|
37
|
+
) -> list[bytes | None]:
|
|
38
|
+
result: list[bytes | None] = [None] * len(addresses)
|
|
39
|
+
for index, address in enumerate(addresses):
|
|
40
|
+
key = signal_address_key(address)
|
|
41
|
+
identity_key = self._remote_identities.get(key)
|
|
42
|
+
if identity_key is None:
|
|
43
|
+
result[index] = None
|
|
44
|
+
else:
|
|
45
|
+
self.on_entry_access(key)
|
|
46
|
+
result[index] = identity_key
|
|
47
|
+
return result
|
|
48
|
+
|
|
49
|
+
async def set_remote_identity(self, address: Any, identity_key: bytes) -> None:
|
|
50
|
+
key = signal_address_key(address)
|
|
51
|
+
set_bounded_map_entry(
|
|
52
|
+
self._remote_identities,
|
|
53
|
+
key,
|
|
54
|
+
identity_key,
|
|
55
|
+
self._max_remote_identities,
|
|
56
|
+
self._on_evict,
|
|
57
|
+
)
|
|
58
|
+
self.on_entry_access(key)
|
|
59
|
+
|
|
60
|
+
async def set_remote_identities(self, entries: Sequence[Any]) -> None:
|
|
61
|
+
for entry in entries:
|
|
62
|
+
key = signal_address_key(entry.address)
|
|
63
|
+
set_bounded_map_entry(
|
|
64
|
+
self._remote_identities,
|
|
65
|
+
key,
|
|
66
|
+
entry.identity_key,
|
|
67
|
+
self._max_remote_identities,
|
|
68
|
+
self._on_evict,
|
|
69
|
+
)
|
|
70
|
+
self.on_entry_access(key)
|
|
71
|
+
|
|
72
|
+
async def clear(self) -> None:
|
|
73
|
+
self._remote_identities.clear()
|
|
74
|
+
self.on_entries_clear()
|
|
75
|
+
|
|
76
|
+
def on_entry_access(self, key: str) -> None:
|
|
77
|
+
"""Runs with the key of every read hit and write. No-op unless overridden."""
|
|
78
|
+
|
|
79
|
+
def on_entry_remove(self, key: str) -> None:
|
|
80
|
+
"""Runs with the key of every cap-evicted entry. No-op unless overridden."""
|
|
81
|
+
|
|
82
|
+
def on_entries_clear(self) -> None:
|
|
83
|
+
"""Runs on clear. No-op unless overridden."""
|
|
84
|
+
|
|
85
|
+
def _on_evict(self, key: str, _value: bytes) -> None:
|
|
86
|
+
self.on_entry_remove(key)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""In-memory message store (mirrors ``src/store/memory/message.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_MESSAGES = 50_000
|
|
13
|
+
_DEFAULT_LIST_LIMIT = 50
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class WaMessageMemoryStoreOptions:
|
|
18
|
+
"""Options for :class:`WaMessageMemoryStore`."""
|
|
19
|
+
|
|
20
|
+
max_messages: int | float | None = None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class WaMessageMemoryStore:
|
|
24
|
+
"""Bounded in-memory message index keyed by message id."""
|
|
25
|
+
|
|
26
|
+
def __init__(self, options: WaMessageMemoryStoreOptions | None = None) -> None:
|
|
27
|
+
self._messages: dict[str, Any] = {}
|
|
28
|
+
self._max_messages = resolve_positive(
|
|
29
|
+
options.max_messages if options is not None else None,
|
|
30
|
+
_DEFAULT_MAX_MESSAGES,
|
|
31
|
+
"WaMessageMemoryStoreOptions.maxMessages",
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
async def upsert(self, record: Any) -> None:
|
|
35
|
+
"""Insert or replace ``record`` keyed by its id, evicting oldest first."""
|
|
36
|
+
set_bounded_map_entry(self._messages, record.id, record, self._max_messages)
|
|
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._messages, record.id, record, self._max_messages)
|
|
42
|
+
|
|
43
|
+
async def get_by_id(self, id: str) -> Any | None:
|
|
44
|
+
"""Return the record for ``id``, or ``None`` when absent."""
|
|
45
|
+
return self._messages.get(id)
|
|
46
|
+
|
|
47
|
+
async def list_by_thread(
|
|
48
|
+
self,
|
|
49
|
+
thread_jid: str,
|
|
50
|
+
limit: int | float | None = None,
|
|
51
|
+
before_timestamp_ms: int | float | None = None,
|
|
52
|
+
) -> list[Any]:
|
|
53
|
+
"""Newest-first records for ``thread_jid`` capped at ``limit`` (default 50)."""
|
|
54
|
+
normalized_limit = normalize_query_limit(limit, _DEFAULT_LIST_LIMIT)
|
|
55
|
+
records = [
|
|
56
|
+
record
|
|
57
|
+
for record in self._messages.values()
|
|
58
|
+
if record.thread_jid == thread_jid
|
|
59
|
+
and not (
|
|
60
|
+
before_timestamp_ms is not None
|
|
61
|
+
and (
|
|
62
|
+
record.timestamp_ms is None
|
|
63
|
+
or record.timestamp_ms >= before_timestamp_ms
|
|
64
|
+
)
|
|
65
|
+
)
|
|
66
|
+
]
|
|
67
|
+
records.sort(
|
|
68
|
+
key=lambda record: (
|
|
69
|
+
record.timestamp_ms if record.timestamp_ms is not None else 0
|
|
70
|
+
),
|
|
71
|
+
reverse=True,
|
|
72
|
+
)
|
|
73
|
+
del records[normalized_limit:]
|
|
74
|
+
return records
|
|
75
|
+
|
|
76
|
+
async def delete_by_id(self, id: str) -> int:
|
|
77
|
+
"""Remove ``id``; return 1 when something was removed, else 0."""
|
|
78
|
+
if id in self._messages:
|
|
79
|
+
del self._messages[id]
|
|
80
|
+
return 1
|
|
81
|
+
return 0
|
|
82
|
+
|
|
83
|
+
async def clear(self) -> None:
|
|
84
|
+
"""Drop every stored message."""
|
|
85
|
+
self._messages.clear()
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""In-memory message-secret backend (mirrors ``message-secret.store.ts``).
|
|
2
|
+
|
|
3
|
+
There is deliberately no background expiry sweep: ``asyncio``-only core code
|
|
4
|
+
never spawns threads, so entries expire lazily on read and via
|
|
5
|
+
:meth:`WaMessageSecretMemoryStore.cleanup_expired`.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import logging
|
|
11
|
+
import math
|
|
12
|
+
import time
|
|
13
|
+
from collections.abc import Sequence
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
|
|
16
|
+
from zapy._store.contracts.message_secret import (
|
|
17
|
+
WaMessageSecretEntry,
|
|
18
|
+
WaMessageSecretEntryWithId,
|
|
19
|
+
WaMessageSecretStore,
|
|
20
|
+
)
|
|
21
|
+
from zapy._util.coercion import resolve_positive
|
|
22
|
+
from zapy._util.collections import set_bounded_map_entry
|
|
23
|
+
|
|
24
|
+
_DEFAULT_TTL_MS = 30 * 60 * 1000
|
|
25
|
+
_DEFAULT_MAX_SECRETS = 10_000
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True, slots=True)
|
|
29
|
+
class _CachedEntry:
|
|
30
|
+
secret: bytes
|
|
31
|
+
sender_jid: str
|
|
32
|
+
expires_at_ms: float
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class WaMessageSecretMemoryStore(WaMessageSecretStore):
|
|
36
|
+
"""In-memory per-message secrets with TTL expiry and a size bound."""
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
ttl_ms: int | float = _DEFAULT_TTL_MS,
|
|
41
|
+
max_secrets: int | float | None = None,
|
|
42
|
+
logger: logging.Logger | 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("message-secret ttlMs must be a positive finite number")
|
|
51
|
+
self._secrets: dict[str, _CachedEntry] = {}
|
|
52
|
+
self._ttl_ms = float(ttl_ms)
|
|
53
|
+
self._max_secrets = resolve_positive(
|
|
54
|
+
max_secrets,
|
|
55
|
+
_DEFAULT_MAX_SECRETS,
|
|
56
|
+
"WaMessageSecretMemoryStoreOptions.maxSecrets",
|
|
57
|
+
)
|
|
58
|
+
self._logger = logger
|
|
59
|
+
self._capacity_warned = False
|
|
60
|
+
|
|
61
|
+
async def get(
|
|
62
|
+
self, message_id: str, now_ms: float | None = None
|
|
63
|
+
) -> WaMessageSecretEntry | None:
|
|
64
|
+
now = time.time() * 1000.0 if now_ms is None else now_ms
|
|
65
|
+
cached = self._secrets.get(message_id)
|
|
66
|
+
if cached is None:
|
|
67
|
+
return None
|
|
68
|
+
if cached.expires_at_ms <= now:
|
|
69
|
+
del self._secrets[message_id]
|
|
70
|
+
return None
|
|
71
|
+
return WaMessageSecretEntry(secret=cached.secret, sender_jid=cached.sender_jid)
|
|
72
|
+
|
|
73
|
+
async def get_batch(
|
|
74
|
+
self, message_ids: Sequence[str], now_ms: float | None = None
|
|
75
|
+
) -> list[WaMessageSecretEntry | None]:
|
|
76
|
+
now = time.time() * 1000.0 if now_ms is None else now_ms
|
|
77
|
+
result: list[WaMessageSecretEntry | None] = [None] * len(message_ids)
|
|
78
|
+
for index, message_id in enumerate(message_ids):
|
|
79
|
+
cached = self._secrets.get(message_id)
|
|
80
|
+
if cached is None:
|
|
81
|
+
result[index] = None
|
|
82
|
+
continue
|
|
83
|
+
if cached.expires_at_ms <= now:
|
|
84
|
+
del self._secrets[message_id]
|
|
85
|
+
result[index] = None
|
|
86
|
+
continue
|
|
87
|
+
result[index] = WaMessageSecretEntry(
|
|
88
|
+
secret=cached.secret, sender_jid=cached.sender_jid
|
|
89
|
+
)
|
|
90
|
+
return result
|
|
91
|
+
|
|
92
|
+
async def set(self, message_id: str, entry: WaMessageSecretEntry) -> None:
|
|
93
|
+
set_bounded_map_entry(
|
|
94
|
+
self._secrets,
|
|
95
|
+
message_id,
|
|
96
|
+
_CachedEntry(
|
|
97
|
+
secret=entry.secret,
|
|
98
|
+
sender_jid=entry.sender_jid,
|
|
99
|
+
expires_at_ms=time.time() * 1000.0 + self._ttl_ms,
|
|
100
|
+
),
|
|
101
|
+
self._max_secrets,
|
|
102
|
+
self._on_evict,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
async def set_batch(self, entries: Sequence[WaMessageSecretEntryWithId]) -> None:
|
|
106
|
+
now_ms = time.time() * 1000.0
|
|
107
|
+
for item in entries:
|
|
108
|
+
set_bounded_map_entry(
|
|
109
|
+
self._secrets,
|
|
110
|
+
item.message_id,
|
|
111
|
+
_CachedEntry(
|
|
112
|
+
secret=item.entry.secret,
|
|
113
|
+
sender_jid=item.entry.sender_jid,
|
|
114
|
+
expires_at_ms=now_ms + self._ttl_ms,
|
|
115
|
+
),
|
|
116
|
+
self._max_secrets,
|
|
117
|
+
self._on_evict,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
async def cleanup_expired(self, now_ms: float) -> int:
|
|
121
|
+
removed = 0
|
|
122
|
+
for message_id, entry in list(self._secrets.items()):
|
|
123
|
+
if entry.expires_at_ms > now_ms:
|
|
124
|
+
continue
|
|
125
|
+
del self._secrets[message_id]
|
|
126
|
+
removed += 1
|
|
127
|
+
return removed
|
|
128
|
+
|
|
129
|
+
async def clear(self) -> None:
|
|
130
|
+
self._secrets.clear()
|
|
131
|
+
|
|
132
|
+
async def destroy(self) -> None:
|
|
133
|
+
self._secrets.clear()
|
|
134
|
+
|
|
135
|
+
def _on_evict(self, _key: str, _value: _CachedEntry) -> None:
|
|
136
|
+
self._warn_capacity()
|
|
137
|
+
|
|
138
|
+
def _warn_capacity(self) -> None:
|
|
139
|
+
if self._capacity_warned or self._logger is None:
|
|
140
|
+
return
|
|
141
|
+
self._capacity_warned = True
|
|
142
|
+
self._logger.warning(
|
|
143
|
+
"message secret store at capacity, evicting oldest (max=%d)",
|
|
144
|
+
self._max_secrets,
|
|
145
|
+
)
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""In-memory pre-key store backend (mirrors ``src/store/memory/pre-key.store.ts``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Awaitable, Callable
|
|
6
|
+
|
|
7
|
+
from typing import TYPE_CHECKING
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from zapy._signal.types import PreKeyRecord
|
|
11
|
+
from zapy._util.coercion import resolve_positive
|
|
12
|
+
from zapy._util.collections import set_bounded_map_entry
|
|
13
|
+
|
|
14
|
+
_DEFAULT_MAX_PRE_KEYS = 4096
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class WaPreKeyMemoryStore:
|
|
18
|
+
"""In-memory one-time pre-keys with upload tracking and a size bound."""
|
|
19
|
+
|
|
20
|
+
def __init__(self, max_pre_keys: int | None = None) -> None:
|
|
21
|
+
self._pre_keys: dict[int, PreKeyRecord] = {}
|
|
22
|
+
self._uploaded_pre_keys: set[int] = set()
|
|
23
|
+
self._server_has_pre_keys = False
|
|
24
|
+
self._next_pre_key_id = 1
|
|
25
|
+
self._max_pre_keys = resolve_positive(
|
|
26
|
+
max_pre_keys, _DEFAULT_MAX_PRE_KEYS, "WaPreKeyMemoryStoreOptions.maxPreKeys"
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
async def put_pre_key(self, record: PreKeyRecord) -> None:
|
|
30
|
+
set_bounded_map_entry(
|
|
31
|
+
self._pre_keys,
|
|
32
|
+
record.key_id,
|
|
33
|
+
record,
|
|
34
|
+
self._max_pre_keys,
|
|
35
|
+
lambda key_id, _record: self._uploaded_pre_keys.discard(key_id),
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
async def get_or_gen_pre_keys(
|
|
39
|
+
self,
|
|
40
|
+
count: int | float,
|
|
41
|
+
generator: Callable[[int], PreKeyRecord | Awaitable[PreKeyRecord]],
|
|
42
|
+
) -> list[PreKeyRecord]:
|
|
43
|
+
if (
|
|
44
|
+
isinstance(count, bool)
|
|
45
|
+
or not isinstance(count, (int, float))
|
|
46
|
+
or int(count) != count
|
|
47
|
+
or abs(count) > 2**53 - 1
|
|
48
|
+
or count <= 0
|
|
49
|
+
):
|
|
50
|
+
raise ValueError(f"invalid prekey count: {count}")
|
|
51
|
+
count = int(count)
|
|
52
|
+
available: list[PreKeyRecord] = []
|
|
53
|
+
for key_id in sorted(self._pre_keys):
|
|
54
|
+
if key_id not in self._uploaded_pre_keys:
|
|
55
|
+
record = self._pre_keys.get(key_id)
|
|
56
|
+
if record is None:
|
|
57
|
+
continue
|
|
58
|
+
available.append(record)
|
|
59
|
+
if len(available) >= count:
|
|
60
|
+
return available
|
|
61
|
+
while len(available) < count:
|
|
62
|
+
generated = generator(self._next_pre_key_id)
|
|
63
|
+
self._next_pre_key_id += 1
|
|
64
|
+
record = await generated if isinstance(generated, Awaitable) else generated
|
|
65
|
+
set_bounded_map_entry(
|
|
66
|
+
self._pre_keys,
|
|
67
|
+
record.key_id,
|
|
68
|
+
record,
|
|
69
|
+
self._max_pre_keys,
|
|
70
|
+
lambda key_id, _record: self._uploaded_pre_keys.discard(key_id),
|
|
71
|
+
)
|
|
72
|
+
available.append(record)
|
|
73
|
+
return available
|
|
74
|
+
|
|
75
|
+
async def get_pre_key_by_id(self, key_id: int) -> PreKeyRecord | None:
|
|
76
|
+
return self._pre_keys.get(key_id)
|
|
77
|
+
|
|
78
|
+
async def get_pre_keys_by_id(self, key_ids: list[int]) -> list[PreKeyRecord | None]:
|
|
79
|
+
return [self._pre_keys.get(key_id) for key_id in key_ids]
|
|
80
|
+
|
|
81
|
+
async def consume_pre_key_by_id(self, key_id: int) -> PreKeyRecord | None:
|
|
82
|
+
record = self._pre_keys.get(key_id)
|
|
83
|
+
if record is None:
|
|
84
|
+
return None
|
|
85
|
+
del self._pre_keys[key_id]
|
|
86
|
+
self._uploaded_pre_keys.discard(key_id)
|
|
87
|
+
return record
|
|
88
|
+
|
|
89
|
+
async def get_or_gen_single_pre_key(
|
|
90
|
+
self, generator: Callable[[int], PreKeyRecord | Awaitable[PreKeyRecord]]
|
|
91
|
+
) -> PreKeyRecord:
|
|
92
|
+
records = await self.get_or_gen_pre_keys(1, generator)
|
|
93
|
+
return records[0]
|
|
94
|
+
|
|
95
|
+
async def mark_key_as_uploaded(self, key_id: int) -> None:
|
|
96
|
+
if key_id < 0 or key_id >= self._next_pre_key_id:
|
|
97
|
+
raise ValueError(f"prekey {key_id} is out of boundary")
|
|
98
|
+
for candidate in self._pre_keys:
|
|
99
|
+
if candidate <= key_id:
|
|
100
|
+
self._uploaded_pre_keys.add(candidate)
|
|
101
|
+
|
|
102
|
+
async def set_server_has_pre_keys(self, value: bool) -> None:
|
|
103
|
+
self._server_has_pre_keys = value
|
|
104
|
+
|
|
105
|
+
async def get_server_has_pre_keys(self) -> bool:
|
|
106
|
+
return self._server_has_pre_keys
|
|
107
|
+
|
|
108
|
+
async def clear(self) -> None:
|
|
109
|
+
self._pre_keys.clear()
|
|
110
|
+
self._uploaded_pre_keys.clear()
|
|
111
|
+
self._server_has_pre_keys = False
|
|
112
|
+
self._next_pre_key_id = 1
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""In-memory privacy-token backend (mirrors ``privacy-token.store.ts``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
|
|
7
|
+
from zapy._store.contracts.privacy_token import (
|
|
8
|
+
WaPrivacyTokenStore,
|
|
9
|
+
WaStoredPrivacyTokenRecord,
|
|
10
|
+
)
|
|
11
|
+
from zapy._util.coercion import resolve_positive
|
|
12
|
+
from zapy._util.collections import set_bounded_map_entry
|
|
13
|
+
|
|
14
|
+
_DEFAULT_MAX_ENTRIES = 10_000
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class WaPrivacyTokenMemoryStore(WaPrivacyTokenStore):
|
|
18
|
+
"""In-memory per-JID privacy tokens with merge-on-upsert and a size bound."""
|
|
19
|
+
|
|
20
|
+
def __init__(self, max_entries: int | float | None = None) -> None:
|
|
21
|
+
self._records: dict[str, WaStoredPrivacyTokenRecord] = {}
|
|
22
|
+
self._max_entries = resolve_positive(
|
|
23
|
+
max_entries, _DEFAULT_MAX_ENTRIES, "WaPrivacyTokenMemoryStore.maxEntries"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
async def upsert(self, record: WaStoredPrivacyTokenRecord) -> None:
|
|
27
|
+
existing = self._records.get(record.jid)
|
|
28
|
+
merged = (
|
|
29
|
+
self._merge_record(existing, record) if existing is not None else record
|
|
30
|
+
)
|
|
31
|
+
set_bounded_map_entry(
|
|
32
|
+
self._records, record.jid, merged, self._max_entries, self._on_evict
|
|
33
|
+
)
|
|
34
|
+
self.on_entry_access(record.jid)
|
|
35
|
+
|
|
36
|
+
async def upsert_batch(self, records: Sequence[WaStoredPrivacyTokenRecord]) -> None:
|
|
37
|
+
for record in records:
|
|
38
|
+
existing = self._records.get(record.jid)
|
|
39
|
+
merged = (
|
|
40
|
+
self._merge_record(existing, record) if existing is not None else record
|
|
41
|
+
)
|
|
42
|
+
set_bounded_map_entry(
|
|
43
|
+
self._records, record.jid, merged, self._max_entries, self._on_evict
|
|
44
|
+
)
|
|
45
|
+
self.on_entry_access(record.jid)
|
|
46
|
+
|
|
47
|
+
async def get_by_jid(self, jid: str) -> WaStoredPrivacyTokenRecord | None:
|
|
48
|
+
record = self._records.get(jid)
|
|
49
|
+
if record is None:
|
|
50
|
+
return None
|
|
51
|
+
self.on_entry_access(jid)
|
|
52
|
+
return record
|
|
53
|
+
|
|
54
|
+
async def delete_by_jid(self, jid: str) -> int:
|
|
55
|
+
if jid not in self._records:
|
|
56
|
+
return 0
|
|
57
|
+
del self._records[jid]
|
|
58
|
+
self.on_entry_remove(jid)
|
|
59
|
+
return 1
|
|
60
|
+
|
|
61
|
+
async def clear(self) -> None:
|
|
62
|
+
self._records.clear()
|
|
63
|
+
self.on_entries_clear()
|
|
64
|
+
|
|
65
|
+
async def destroy(self) -> None:
|
|
66
|
+
await self.clear()
|
|
67
|
+
|
|
68
|
+
def on_entry_access(self, jid: str) -> None:
|
|
69
|
+
"""Runs with the jid of every read hit and write. No-op unless overridden."""
|
|
70
|
+
|
|
71
|
+
def on_entry_remove(self, jid: str) -> None:
|
|
72
|
+
"""Runs with the jid of every deleted/evicted record."""
|
|
73
|
+
|
|
74
|
+
def on_entries_clear(self) -> None:
|
|
75
|
+
"""Runs on clear. No-op unless overridden."""
|
|
76
|
+
|
|
77
|
+
def _on_evict(self, jid: str, _value: WaStoredPrivacyTokenRecord) -> None:
|
|
78
|
+
self.on_entry_remove(jid)
|
|
79
|
+
|
|
80
|
+
@staticmethod
|
|
81
|
+
def _merge_record(
|
|
82
|
+
existing: WaStoredPrivacyTokenRecord, incoming: WaStoredPrivacyTokenRecord
|
|
83
|
+
) -> WaStoredPrivacyTokenRecord:
|
|
84
|
+
return WaStoredPrivacyTokenRecord(
|
|
85
|
+
jid=incoming.jid,
|
|
86
|
+
tc_token=incoming.tc_token
|
|
87
|
+
if incoming.tc_token is not None
|
|
88
|
+
else existing.tc_token,
|
|
89
|
+
tc_token_timestamp=(
|
|
90
|
+
incoming.tc_token_timestamp
|
|
91
|
+
if incoming.tc_token_timestamp is not None
|
|
92
|
+
else existing.tc_token_timestamp
|
|
93
|
+
),
|
|
94
|
+
tc_token_sender_timestamp=(
|
|
95
|
+
incoming.tc_token_sender_timestamp
|
|
96
|
+
if incoming.tc_token_sender_timestamp is not None
|
|
97
|
+
else existing.tc_token_sender_timestamp
|
|
98
|
+
),
|
|
99
|
+
nct_salt=incoming.nct_salt
|
|
100
|
+
if incoming.nct_salt is not None
|
|
101
|
+
else existing.nct_salt,
|
|
102
|
+
updated_at_ms=incoming.updated_at_ms,
|
|
103
|
+
)
|