Improve logging and make get_dialogs use iterators more
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
from typing import Tuple, Optional, List, Union, Dict, TYPE_CHECKING
|
from typing import Tuple, Optional, AsyncIterable, Union, Dict, TYPE_CHECKING
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
@@ -189,14 +189,12 @@ class AbstractUser(ABC):
|
|||||||
if UPDATE_TIME:
|
if UPDATE_TIME:
|
||||||
UPDATE_TIME.labels(update_type=type(update).__name__).observe(time.time() - start_time)
|
UPDATE_TIME.labels(update_type=type(update).__name__).observe(time.time() - start_time)
|
||||||
|
|
||||||
async def get_dialogs(self, limit: int = None) -> List[Union[Chat, Channel]]:
|
def get_dialogs(self, limit: int = None) -> AsyncIterable[Union[User, Chat, Channel]]:
|
||||||
if self.is_bot:
|
return (dialog.entity async for dialog in
|
||||||
return []
|
self.client.iter_dialogs(limit=limit, ignore_migrated=True, archived=False)
|
||||||
dialogs = await self.client.get_dialogs(limit=limit)
|
if isinstance(dialog.entity, (ChatForbidden, ChannelForbidden))
|
||||||
return [dialog.entity for dialog in dialogs if (
|
or (isinstance(dialog.entity, Chat)
|
||||||
not isinstance(dialog.entity, (User, ChatForbidden, ChannelForbidden))
|
and (dialog.entity.deactivated or dialog.entity.left)))
|
||||||
and not (isinstance(dialog.entity, Chat)
|
|
||||||
and (dialog.entity.deactivated or dialog.entity.left)))]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
@@ -216,7 +214,7 @@ class AbstractUser(ABC):
|
|||||||
if not self.client:
|
if not self.client:
|
||||||
self._init_client()
|
self._init_client()
|
||||||
await self.client.connect()
|
await self.client.connect()
|
||||||
self.log.debug("%s connected: %s", self.mxid, self.connected)
|
self.log.debug(f"{self.mxid if not self.is_bot else 'Bot'} connected: {self.connected}")
|
||||||
return self
|
return self
|
||||||
|
|
||||||
async def ensure_started(self, even_if_no_session=False) -> 'AbstractUser':
|
async def ensure_started(self, even_if_no_session=False) -> 'AbstractUser':
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ ReplyFunc = Callable[[str], Awaitable[Message]]
|
|||||||
|
|
||||||
|
|
||||||
class Bot(AbstractUser):
|
class Bot(AbstractUser):
|
||||||
log: logging.Logger = logging.getLogger("mau.bot")
|
log: logging.Logger = logging.getLogger("mau.user.bot")
|
||||||
mxid_regex: Pattern = re.compile("@.+:.+")
|
mxid_regex: Pattern = re.compile("@.+:.+")
|
||||||
|
|
||||||
token: str
|
token: str
|
||||||
|
|||||||
@@ -108,6 +108,8 @@ class Puppet(CustomPuppetMixin):
|
|||||||
if self.custom_mxid:
|
if self.custom_mxid:
|
||||||
self.by_custom_mxid[self.custom_mxid] = self
|
self.by_custom_mxid[self.custom_mxid] = self
|
||||||
|
|
||||||
|
self.log = self.log.getChild(str(self.id))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def tgid(self) -> TelegramID:
|
def tgid(self) -> TelegramID:
|
||||||
return self.id
|
return self.id
|
||||||
|
|||||||
@@ -87,6 +87,8 @@ class User(AbstractUser):
|
|||||||
if tgid:
|
if tgid:
|
||||||
self.by_tgid[tgid] = self
|
self.by_tgid[tgid] = self
|
||||||
|
|
||||||
|
self.log = self.log.getChild(self.mxid)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
return self.mxid
|
return self.mxid
|
||||||
@@ -305,8 +307,10 @@ class User(AbstractUser):
|
|||||||
return await self._search_remote(query), True
|
return await self._search_remote(query), True
|
||||||
|
|
||||||
async def sync_dialogs(self, synchronous_create: bool = False) -> None:
|
async def sync_dialogs(self, synchronous_create: bool = False) -> None:
|
||||||
|
if self.is_bot:
|
||||||
|
return
|
||||||
creators = []
|
creators = []
|
||||||
for entity in await self.get_dialogs(limit=config["bridge.sync_dialog_limit"] or None):
|
async for entity in self.get_dialogs(limit=config["bridge.sync_dialog_limit"] or None):
|
||||||
portal = po.Portal.get_by_entity(entity)
|
portal = po.Portal.get_by_entity(entity)
|
||||||
self.portals[portal.tgid_full] = portal
|
self.portals[portal.tgid_full] = portal
|
||||||
creators.append(
|
creators.append(
|
||||||
|
|||||||
@@ -316,11 +316,10 @@ class ProvisioningAPI(AuthAPI):
|
|||||||
return err
|
return err
|
||||||
|
|
||||||
if not user.is_bot:
|
if not user.is_bot:
|
||||||
chats = await user.get_dialogs()
|
|
||||||
return web.json_response([{
|
return web.json_response([{
|
||||||
"id": get_peer_id(chat),
|
"id": get_peer_id(chat),
|
||||||
"title": chat.title,
|
"title": chat.title,
|
||||||
} for chat in chats])
|
} async for chat in user.get_dialogs()])
|
||||||
else:
|
else:
|
||||||
return web.json_response([{
|
return web.json_response([{
|
||||||
"id": get_peer_id(chat.peer),
|
"id": get_peer_id(chat.peer),
|
||||||
|
|||||||
Reference in New Issue
Block a user