|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-2021 Dan <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +from typing import Union, List, AsyncGenerator, Optional |
| 20 | + |
| 21 | +from pyrogram import raw |
| 22 | +from pyrogram import types |
| 23 | +from pyrogram.scaffold import Scaffold |
| 24 | + |
| 25 | + |
| 26 | +class GetChatEventLog(Scaffold): |
| 27 | + async def get_chat_event_log( |
| 28 | + self, |
| 29 | + chat_id: Union[int, str], |
| 30 | + query: str = "", |
| 31 | + offset_id: int = 0, |
| 32 | + limit: int = 0, |
| 33 | + filters: "types.ChatEventFilter" = None, |
| 34 | + user_ids: List[Union[int, str]] = None |
| 35 | + ) -> Optional[AsyncGenerator["types.ChatEvent", None]]: |
| 36 | + """Get the actions taken by chat members and administrators in the last 48h. |
| 37 | +
|
| 38 | + Only available for supergroups and channels. Requires administrator rights. |
| 39 | + Results are returned in reverse chronological order (i.e., newest first). |
| 40 | +
|
| 41 | + Args: |
| 42 | + chat_id (``int`` | ``str``): |
| 43 | + Unique identifier (int) or username (str) of the target chat. |
| 44 | +
|
| 45 | + query (``str``, *optional*): |
| 46 | + Search query to filter events based on text. |
| 47 | + By default, an empty query is applied and all events will be returned. |
| 48 | +
|
| 49 | + offset_id (``int``, *optional*): |
| 50 | + Offset event identifier from which to start returning results. |
| 51 | + By default, no offset is applied and events will be returned starting from the latest. |
| 52 | +
|
| 53 | + limit (``int``, *optional*): |
| 54 | + Maximum amount of events to be returned. |
| 55 | + By default, all events will be returned. |
| 56 | +
|
| 57 | + filters (:obj:`~pyrogram.types.ChatEventFilter`, *optional*): |
| 58 | + The types of events to return. |
| 59 | + By default, all types will be returned. |
| 60 | +
|
| 61 | + user_ids (List of ``int`` | ``str``, *optional*): |
| 62 | + User identifiers (int) or usernames (str) by which to filter events. |
| 63 | + By default, events relating to all users will be returned. |
| 64 | +
|
| 65 | + Yields: |
| 66 | + :obj:`~pyrogram.types.ChatEvent` objects. |
| 67 | + """ |
| 68 | + current = 0 |
| 69 | + total = abs(limit) or (1 << 31) |
| 70 | + limit = min(100, total) |
| 71 | + |
| 72 | + while True: |
| 73 | + r: raw.base.channels.AdminLogResults = await self.send( |
| 74 | + raw.functions.channels.GetAdminLog( |
| 75 | + channel=await self.resolve_peer(chat_id), |
| 76 | + q=query, |
| 77 | + min_id=0, |
| 78 | + max_id=offset_id, |
| 79 | + limit=limit, |
| 80 | + events_filter=filters.write(), |
| 81 | + admins=( |
| 82 | + [await self.resolve_peer(i) for i in user_ids] |
| 83 | + if user_ids is not None |
| 84 | + else user_ids |
| 85 | + ) |
| 86 | + ) |
| 87 | + ) |
| 88 | + |
| 89 | + if not r.events: |
| 90 | + return |
| 91 | + |
| 92 | + last = r.events[-1] |
| 93 | + offset_id = last.id |
| 94 | + |
| 95 | + for event in r.events: |
| 96 | + yield await types.ChatEvent._parse(self, event, r.users, r.chats) |
| 97 | + |
| 98 | + current += 1 |
| 99 | + |
| 100 | + if current >= total: |
| 101 | + return |
0 commit comments