feat: finalize matrix platform audit and docs

This commit is contained in:
Mikhail Putilovskij 2026-04-21 15:35:03 +03:00
parent 6422c7db58
commit 4524a6abc8
30 changed files with 3093 additions and 176 deletions

View file

@ -13,7 +13,9 @@ PENDING_CONFIRM_PREFIX = "matrix_pending_confirm:"
LOAD_PENDING_PREFIX = "matrix_load_pending:"
RESET_PENDING_PREFIX = "matrix_reset_pending:"
STAGED_ATTACHMENTS_PREFIX = "matrix_staged_attachments:"
PLATFORM_CHAT_SEQ_KEY = "matrix_platform_chat_seq"
_STAGED_ATTACHMENTS_LOCKS: WeakValueDictionary[str, asyncio.Lock] = WeakValueDictionary()
_PLATFORM_CHAT_SEQ_LOCK = asyncio.Lock()
async def get_room_meta(store: StateStore, room_id: str) -> dict | None:
@ -29,9 +31,7 @@ async def get_platform_chat_id(store: StateStore, room_id: str) -> str | None:
return meta.get("platform_chat_id") if meta else None
async def set_platform_chat_id(
store: StateStore, room_id: str, platform_chat_id: str
) -> None:
async def set_platform_chat_id(store: StateStore, room_id: str, platform_chat_id: str) -> None:
meta = dict(await get_room_meta(store, room_id) or {})
meta["platform_chat_id"] = platform_chat_id
await set_room_meta(store, room_id, meta)
@ -71,16 +71,29 @@ async def next_chat_id(store: StateStore, matrix_user_id: str) -> str:
return f"C{index}"
async def next_platform_chat_id(store: StateStore) -> str:
async with _PLATFORM_CHAT_SEQ_LOCK:
data = await store.get(PLATFORM_CHAT_SEQ_KEY)
index = int((data or {}).get("next_platform_chat_index", 1))
await store.set(
PLATFORM_CHAT_SEQ_KEY,
{"next_platform_chat_index": index + 1},
)
return str(index)
def _pending_confirm_key(user_id: str, room_id: str | None = None) -> str:
if room_id is None:
return f"{PENDING_CONFIRM_PREFIX}{user_id}"
return f"{PENDING_CONFIRM_PREFIX}{user_id}:{room_id}"
async def get_pending_confirm(
store: StateStore, user_id: str, room_id: str | None = None
) -> dict | None:
return await store.get(_pending_confirm_key(user_id, room_id))
async def set_pending_confirm(
store: StateStore, user_id: str, room_id: str | dict, meta: dict | None = None
) -> None:
@ -146,9 +159,7 @@ def _staged_attachments_lock(room_id: str, user_id: str) -> asyncio.Lock:
return lock
async def get_staged_attachments(
store: StateStore, room_id: str, user_id: str
) -> list[dict]:
async def get_staged_attachments(store: StateStore, room_id: str, user_id: str) -> list[dict]:
data = await store.get(_staged_attachments_key(room_id, user_id))
if not isinstance(data, dict):
return []
@ -166,9 +177,7 @@ async def add_staged_attachment(
async with _staged_attachments_lock(room_id, user_id):
attachments = await get_staged_attachments(store, room_id, user_id)
attachments.append(attachment)
await store.set(
_staged_attachments_key(room_id, user_id), {"attachments": attachments}
)
await store.set(_staged_attachments_key(room_id, user_id), {"attachments": attachments})
async def remove_staged_attachment_at(
@ -181,16 +190,12 @@ async def remove_staged_attachment_at(
removed = attachments.pop(index)
if attachments:
await store.set(
_staged_attachments_key(room_id, user_id), {"attachments": attachments}
)
await store.set(_staged_attachments_key(room_id, user_id), {"attachments": attachments})
else:
await store.delete(_staged_attachments_key(room_id, user_id))
return removed
async def clear_staged_attachments(
store: StateStore, room_id: str, user_id: str
) -> None:
async def clear_staged_attachments(store: StateStore, room_id: str, user_id: str) -> None:
async with _staged_attachments_lock(room_id, user_id):
await store.delete(_staged_attachments_key(room_id, user_id))