2022-01-09 15:01:16 +03:00
|
|
|
from .types import DataCenter, ChannelState, SessionState, EntityType, Entity
|
2021-09-19 14:45:19 +03:00
|
|
|
|
2018-03-02 00:34:32 +03:00
|
|
|
from abc import ABC, abstractmethod
|
2021-09-19 14:45:19 +03:00
|
|
|
from typing import List, Optional
|
2018-03-02 00:34:32 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Session(ABC):
|
|
|
|
@abstractmethod
|
2021-09-19 14:45:19 +03:00
|
|
|
async def insert_dc(self, dc: DataCenter):
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2021-09-19 14:45:19 +03:00
|
|
|
Store a new or update an existing `DataCenter` with matching ``id``.
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2018-03-02 00:34:32 +03:00
|
|
|
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
|
|
|
|
|
2018-03-02 00:34:32 +03:00
|
|
|
@abstractmethod
|
2021-09-19 14:45:19 +03:00
|
|
|
async def set_state(self, state: SessionState):
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2021-09-19 14:45:19 +03:00
|
|
|
Set the state about the current session.
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2018-03-02 00:34:32 +03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@abstractmethod
|
2021-09-19 14:45:19 +03:00
|
|
|
async def get_state(self) -> Optional[SessionState]:
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2021-09-19 14:45:19 +03:00
|
|
|
Get the state about the current session.
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2018-03-02 00:34:32 +03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@abstractmethod
|
2021-09-19 14:45:19 +03:00
|
|
|
async def insert_channel_state(self, state: ChannelState):
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2021-09-19 14:45:19 +03:00
|
|
|
Store a new or update an existing `ChannelState` with matching ``id``.
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2018-03-02 00:34:32 +03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@abstractmethod
|
2021-09-19 14:45:19 +03:00
|
|
|
async def get_all_channel_states(self) -> List[ChannelState]:
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2021-09-19 14:45:19 +03:00
|
|
|
Get a list of all currently-stored `ChannelState`. Should not contain duplicate ``id``.
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2018-03-02 00:34:32 +03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2019-02-10 13:10:41 +03:00
|
|
|
@abstractmethod
|
2021-09-19 14:45:19 +03:00
|
|
|
async def insert_entities(self, entities: List[Entity]):
|
2019-02-10 13:10:41 +03:00
|
|
|
"""
|
2021-09-19 14:45:19 +03:00
|
|
|
Store new or update existing `Entity` with matching ``id``.
|
2019-02-10 13:10:41 +03:00
|
|
|
|
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.
|
2019-02-10 13:10:41 +03:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-04-25 14:37:29 +03:00
|
|
|
@abstractmethod
|
2022-01-09 15:01:16 +03:00
|
|
|
async def get_entity(self, ty: Optional[EntityType], id: int) -> Optional[Entity]:
|
2018-04-25 14:37:29 +03:00
|
|
|
"""
|
2021-09-19 14:45:19 +03:00
|
|
|
Get the `Entity` with matching ``ty`` and ``id``.
|
2018-04-25 14:37:29 +03:00
|
|
|
|
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.
|
2018-04-25 14:37:29 +03:00
|
|
|
|
2022-01-09 15:01:16 +03:00
|
|
|
* `EntityType.USER` and `EntityType.BOT`.
|
|
|
|
* `EntityType.GROUP`.
|
|
|
|
* `EntityType.CHANNEL`, `EntityType.MEGAGROUP` and `EntityType.GIGAGROUP`.
|
2018-03-02 00:34:32 +03:00
|
|
|
|
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.
|
2018-03-02 00:34:32 +03:00
|
|
|
|
2022-01-09 15:01:16 +03:00
|
|
|
You may use ``EntityType.canonical`` to find out the canonical type.
|
2021-09-19 18:08:51 +03:00
|
|
|
|
|
|
|
A ``ty`` with the value of ``None`` should be treated as "any entity with matching ID".
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2018-03-02 00:34:32 +03:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@abstractmethod
|
2021-09-19 14:45:19 +03:00
|
|
|
async def save(self):
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2021-09-19 14:45:19 +03:00
|
|
|
Save the session.
|
2018-03-03 14:13:42 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
May do nothing if the other methods already saved when they were called.
|
2018-03-02 00:34:32 +03:00
|
|
|
|
2021-09-19 14:45:19 +03:00
|
|
|
May return custom data when manual saving is intended.
|
2018-03-03 14:13:42 +03:00
|
|
|
"""
|
2018-03-02 00:34:32 +03:00
|
|
|
raise NotImplementedError
|