File: account_data.py

package info (click to toggle)
mautrix-python 0.20.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,812 kB
  • sloc: python: 19,103; makefile: 16
file content (66 lines) | stat: -rw-r--r-- 2,686 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright (c) 2022 Tulir Asokan
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations

from mautrix.api import Method, Path
from mautrix.types import JSON, AccountDataEventContent, EventType, RoomID, Serializable

from ..base import BaseClientAPI


class AccountDataMethods(BaseClientAPI):
    """
    Methods in section 13.9 Client Config of the spec. These methods are used for storing user-local
    data on the homeserver to synchronize client configuration across sessions.

    See also: `API reference <https://matrix.org/docs/spec/client_server/r0.6.1#id125>`__"""

    async def get_account_data(self, type: EventType | str, room_id: RoomID | None = None) -> JSON:
        """
        Get a specific account data event from the homeserver.

        See also: `API reference <https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-user-userid-account-data-type>`__

        Args:
            type: The type of the account data event to get.
            room_id: Optionally, the room ID to get per-room account data.

        Returns:
            The data in the event.
        """
        if isinstance(type, EventType) and not type.is_account_data:
            raise ValueError("Event type is not an account data event type")
        base_path = Path.v3.user[self.mxid]
        if room_id:
            base_path = base_path.rooms[room_id]
        return await self.api.request(Method.GET, base_path.account_data[type])

    async def set_account_data(
        self,
        type: EventType | str,
        data: AccountDataEventContent | dict[str, JSON],
        room_id: RoomID | None = None,
    ) -> None:
        """
        Store account data on the homeserver.

        See also: `API reference <https://matrix.org/docs/spec/client_server/r0.6.1#put-matrix-client-r0-user-userid-account-data-type>`__

        Args:
            type: The type of the account data event to set.
            data: The content to store in that account data event.
            room_id: Optionally, the room ID to set per-room account data.
        """
        if isinstance(type, EventType) and not type.is_account_data:
            raise ValueError("Event type is not an account data event type")
        base_path = Path.v3.user[self.mxid]
        if room_id:
            base_path = base_path.rooms[room_id]
        await self.api.request(
            Method.PUT,
            base_path.account_data[type],
            data.serialize() if isinstance(data, Serializable) else data,
        )