Telethon/telethon/_sessions/abstract.py

93 lines
3.1 KiB
Python
Raw Normal View History

2021-09-19 14:45:19 +03:00
from .types import DataCenter, ChannelState, SessionState, Entity
from abc import ABC, abstractmethod
2021-09-19 14:45:19 +03:00
from typing import List, Optional
class Session(ABC):
@abstractmethod
2021-09-19 14:45:19 +03:00
async def insert_dc(self, dc: DataCenter):
"""
2021-09-19 14:45:19 +03:00
Store a new or update an existing `DataCenter` with matching ``id``.
"""
raise NotImplementedError
2019-05-06 09:55:24 +03:00
@abstractmethod
2021-09-19 14:45:19 +03:00
async def get_all_dc(self) -> List[DataCenter]:
2019-05-06 09:55:24 +03:00
"""
2021-09-19 14:45:19 +03:00
Get a list of all currently-stored `DataCenter`. Should not contain duplicate ``id``.
2019-05-06 09:55:24 +03:00
"""
raise NotImplementedError
@abstractmethod
2021-09-19 14:45:19 +03:00
async def set_state(self, state: SessionState):
"""
2021-09-19 14:45:19 +03:00
Set the state about the current session.
"""
raise NotImplementedError
@abstractmethod
2021-09-19 14:45:19 +03:00
async def get_state(self) -> Optional[SessionState]:
"""
2021-09-19 14:45:19 +03:00
Get the state about the current session.
"""
raise NotImplementedError
@abstractmethod
2021-09-19 14:45:19 +03:00
async def insert_channel_state(self, state: ChannelState):
"""
2021-09-19 14:45:19 +03:00
Store a new or update an existing `ChannelState` with matching ``id``.
"""
raise NotImplementedError
@abstractmethod
2021-09-19 14:45:19 +03:00
async def get_all_channel_states(self) -> List[ChannelState]:
"""
2021-09-19 14:45:19 +03:00
Get a list of all currently-stored `ChannelState`. Should not contain duplicate ``id``.
"""
raise NotImplementedError
@abstractmethod
2021-09-19 14:45:19 +03:00
async def insert_entities(self, entities: List[Entity]):
"""
2021-09-19 14:45:19 +03:00
Store new or update existing `Entity` with matching ``id``.
2021-09-19 14:45:19 +03:00
Entities should be saved on a best-effort. It is okay to not save them, although the
library may need to do extra work if a previously-saved entity is missing, or even be
unable to continue without the entity.
"""
raise NotImplementedError
@abstractmethod
async def get_entity(self, ty: Optional[int], id: int) -> Optional[Entity]:
"""
2021-09-19 14:45:19 +03:00
Get the `Entity` with matching ``ty`` and ``id``.
2021-09-19 14:45:19 +03:00
The following groups of ``ty`` should be treated to be equivalent, that is, for a given
``ty`` and ``id``, if the ``ty`` is in a given group, a matching ``access_hash`` with
that ``id`` from within any ``ty`` in that group should be returned.
2021-09-19 14:45:19 +03:00
* ``'U'`` and ``'B'`` (user and bot).
* ``'G'`` (small group chat).
* ``'C'``, ``'M'`` and ``'E'`` (broadcast channel, megagroup channel, and gigagroup channel).
2021-09-19 14:45:19 +03:00
For example, if a ``ty`` representing a bot is stored but the asking ``ty`` is a user,
the corresponding ``access_hash`` should still be returned.
2021-09-19 14:45:19 +03:00
You may use `types.canonical_entity_type` to find out the canonical type.
A ``ty`` with the value of ``None`` should be treated as "any entity with matching ID".
"""
raise NotImplementedError
@abstractmethod
2021-09-19 14:45:19 +03:00
async def save(self):
"""
2021-09-19 14:45:19 +03:00
Save the session.
2021-09-19 14:45:19 +03:00
May do nothing if the other methods already saved when they were called.
2021-09-19 14:45:19 +03:00
May return custom data when manual saving is intended.
"""
raise NotImplementedError