2018-06-09 23:09:02 +03:00
|
|
|
import itertools
|
|
|
|
from collections import UserList
|
|
|
|
|
2018-06-10 22:50:28 +03:00
|
|
|
from async_generator import async_generator, yield_
|
|
|
|
|
2018-06-09 23:09:02 +03:00
|
|
|
from .users import UserMethods
|
|
|
|
from .. import utils
|
2018-06-10 22:50:28 +03:00
|
|
|
from ..tl import types, functions, custom
|
2018-06-09 23:09:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
class DialogMethods(UserMethods):
|
2018-06-10 23:00:55 +03:00
|
|
|
|
2018-06-09 23:09:02 +03:00
|
|
|
# region Public methods
|
|
|
|
|
2018-06-10 22:50:28 +03:00
|
|
|
@async_generator
|
2018-06-09 23:09:02 +03:00
|
|
|
async def iter_dialogs(
|
2018-06-27 14:05:19 +03:00
|
|
|
self, limit=None, *, offset_date=None, offset_id=0,
|
2018-06-20 13:03:42 +03:00
|
|
|
offset_peer=types.InputPeerEmpty(), ignore_migrated=False,
|
|
|
|
_total=None):
|
2018-06-09 23:09:02 +03:00
|
|
|
"""
|
|
|
|
Returns an iterator over the dialogs, yielding 'limit' at most.
|
|
|
|
Dialogs are the open "chats" or conversations with other people,
|
|
|
|
groups you have joined, or channels you are subscribed to.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
limit (`int` | `None`):
|
|
|
|
How many dialogs to be retrieved as maximum. Can be set to
|
|
|
|
``None`` to retrieve all dialogs. Note that this may take
|
|
|
|
whole minutes if you have hundreds of dialogs, as Telegram
|
|
|
|
will tell the library to slow down through a
|
|
|
|
``FloodWaitError``.
|
|
|
|
|
|
|
|
offset_date (`datetime`, optional):
|
|
|
|
The offset date to be used.
|
|
|
|
|
|
|
|
offset_id (`int`, optional):
|
|
|
|
The message ID to be used as an offset.
|
|
|
|
|
|
|
|
offset_peer (:tl:`InputPeer`, optional):
|
|
|
|
The peer to be used as an offset.
|
|
|
|
|
2018-06-20 13:03:42 +03:00
|
|
|
ignore_migrated (`bool`, optional):
|
|
|
|
Whether :tl:`Chat` that have ``migrated_to`` a :tl:`Channel`
|
|
|
|
should be included or not. By default all the chats in your
|
|
|
|
dialogs are returned, but setting this to ``True`` will hide
|
|
|
|
them in the same way official applications do.
|
|
|
|
|
2018-06-09 23:09:02 +03:00
|
|
|
_total (`list`, optional):
|
|
|
|
A single-item list to pass the total parameter by reference.
|
|
|
|
|
|
|
|
Yields:
|
|
|
|
Instances of `telethon.tl.custom.dialog.Dialog`.
|
|
|
|
"""
|
|
|
|
limit = float('inf') if limit is None else int(limit)
|
|
|
|
if limit == 0:
|
|
|
|
if not _total:
|
|
|
|
return
|
|
|
|
# Special case, get a single dialog and determine count
|
|
|
|
dialogs = await self(functions.messages.GetDialogsRequest(
|
|
|
|
offset_date=offset_date,
|
|
|
|
offset_id=offset_id,
|
|
|
|
offset_peer=offset_peer,
|
2018-06-29 14:20:45 +03:00
|
|
|
limit=1,
|
|
|
|
hash=0
|
2018-06-09 23:09:02 +03:00
|
|
|
))
|
|
|
|
_total[0] = getattr(dialogs, 'count', len(dialogs.dialogs))
|
|
|
|
return
|
|
|
|
|
|
|
|
seen = set()
|
|
|
|
req = functions.messages.GetDialogsRequest(
|
|
|
|
offset_date=offset_date,
|
|
|
|
offset_id=offset_id,
|
|
|
|
offset_peer=offset_peer,
|
2018-06-29 14:20:45 +03:00
|
|
|
limit=0,
|
|
|
|
hash=0
|
2018-06-09 23:09:02 +03:00
|
|
|
)
|
|
|
|
while len(seen) < limit:
|
|
|
|
req.limit = min(limit - len(seen), 100)
|
|
|
|
r = await self(req)
|
|
|
|
|
|
|
|
if _total:
|
|
|
|
_total[0] = getattr(r, 'count', len(r.dialogs))
|
2018-07-22 20:20:55 +03:00
|
|
|
|
2018-06-09 23:09:02 +03:00
|
|
|
entities = {utils.get_peer_id(x): x
|
|
|
|
for x in itertools.chain(r.users, r.chats)}
|
2018-07-22 20:20:55 +03:00
|
|
|
|
|
|
|
messages = {}
|
|
|
|
for m in r.messages:
|
|
|
|
m._finish_init(self, entities, None)
|
|
|
|
messages[m.id] = m
|
2018-06-09 23:09:02 +03:00
|
|
|
|
|
|
|
# Happens when there are pinned dialogs
|
|
|
|
if len(r.dialogs) > limit:
|
|
|
|
r.dialogs = r.dialogs[:limit]
|
|
|
|
|
|
|
|
for d in r.dialogs:
|
|
|
|
peer_id = utils.get_peer_id(d.peer)
|
|
|
|
if peer_id not in seen:
|
|
|
|
seen.add(peer_id)
|
2018-06-20 13:03:42 +03:00
|
|
|
cd = custom.Dialog(self, d, entities, messages)
|
2018-06-20 20:48:00 +03:00
|
|
|
if cd.dialog.pts:
|
|
|
|
self._channel_pts[cd.id] = cd.dialog.pts
|
|
|
|
|
2018-06-20 13:03:42 +03:00
|
|
|
if not ignore_migrated or getattr(
|
|
|
|
cd.entity, 'migrated_to', None) is None:
|
|
|
|
await yield_(cd)
|
2018-06-09 23:09:02 +03:00
|
|
|
|
|
|
|
if len(r.dialogs) < req.limit\
|
|
|
|
or not isinstance(r, types.messages.DialogsSlice):
|
|
|
|
# Less than we requested means we reached the end, or
|
|
|
|
# we didn't get a DialogsSlice which means we got all.
|
|
|
|
break
|
|
|
|
|
|
|
|
req.offset_date = r.messages[-1].date
|
|
|
|
req.offset_peer = entities[utils.get_peer_id(r.dialogs[-1].peer)]
|
2018-06-20 22:12:47 +03:00
|
|
|
if req.offset_id == r.messages[-1].id:
|
|
|
|
# In some very rare cases this will get stuck in an infinite
|
|
|
|
# loop, where the offsets will get reused over and over. If
|
|
|
|
# the new offset is the same as the one before, break already.
|
|
|
|
break
|
|
|
|
|
2018-06-09 23:09:02 +03:00
|
|
|
req.offset_id = r.messages[-1].id
|
|
|
|
req.exclude_pinned = True
|
|
|
|
|
|
|
|
async def get_dialogs(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Same as :meth:`iter_dialogs`, but returns a list instead
|
|
|
|
with an additional ``.total`` attribute on the list.
|
|
|
|
"""
|
|
|
|
total = [0]
|
|
|
|
kwargs['_total'] = total
|
2018-06-10 23:00:55 +03:00
|
|
|
dialogs = UserList()
|
|
|
|
async for x in self.iter_dialogs(*args, **kwargs):
|
|
|
|
dialogs.append(x)
|
2018-06-09 23:09:02 +03:00
|
|
|
dialogs.total = total[0]
|
|
|
|
return dialogs
|
|
|
|
|
2018-06-10 23:00:55 +03:00
|
|
|
@async_generator
|
2018-06-18 19:41:06 +03:00
|
|
|
async def iter_drafts(self):
|
2018-06-09 23:09:02 +03:00
|
|
|
"""
|
|
|
|
Iterator over all open draft messages.
|
|
|
|
|
|
|
|
Instances of `telethon.tl.custom.draft.Draft` are yielded.
|
|
|
|
You can call `telethon.tl.custom.draft.Draft.set_message`
|
|
|
|
to change the message or `telethon.tl.custom.draft.Draft.delete`
|
|
|
|
among other things.
|
|
|
|
"""
|
|
|
|
r = await self(functions.messages.GetAllDraftsRequest())
|
|
|
|
for update in r.updates:
|
2018-06-10 22:50:28 +03:00
|
|
|
await yield_(custom.Draft._from_update(self, update))
|
2018-06-09 23:09:02 +03:00
|
|
|
|
|
|
|
async def get_drafts(self):
|
|
|
|
"""
|
|
|
|
Same as :meth:`iter_drafts`, but returns a list instead.
|
|
|
|
"""
|
2018-06-10 23:00:55 +03:00
|
|
|
result = []
|
|
|
|
async for x in self.iter_drafts():
|
|
|
|
result.append(x)
|
|
|
|
return result
|
2018-06-09 23:09:02 +03:00
|
|
|
|
|
|
|
# endregion
|