Telethon/telethon/_sessions/abstract.py

93 lines
3.1 KiB
Python
Raw Normal View History

from .types import DataCenter, ChannelState, SessionState, EntityType, Entity
2021-09-19 14:45:19 +03:00
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[EntityType], 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.
* `EntityType.USER` and `EntityType.BOT`.
* `EntityType.GROUP`.
* `EntityType.CHANNEL`, `EntityType.MEGAGROUP` and `EntityType.GIGAGROUP`.
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.
You may use ``EntityType.canonical`` 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