Skip to content

Commit 7ee5606

Browse files
committed
Add terminate_session
and the corresponding bound methods
1 parent 3488a2f commit 7ee5606

File tree

6 files changed

+92
-2
lines changed

6 files changed

+92
-2
lines changed

compiler/docs/compiler.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def get_title_list(s: str) -> list:
164164
terminate
165165
get_me
166166
get_active_sessions
167+
terminate_session
167168
""",
168169
messages="""
169170
Messages
@@ -778,6 +779,10 @@ def get_title_list(s: str) -> list:
778779
Story.react
779780
Story.download
780781
""",
782+
active_session="""
783+
ActiveSession
784+
ActiveSession.terminate
785+
""",
781786
)
782787

783788
root = PYROGRAM_API_DEST + "/bound-methods"

compiler/docs/template/bound-methods.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,16 @@ Story
139139
:hidden:
140140

141141
{story_toctree}
142+
143+
ActiveSession
144+
-------------
145+
146+
.. hlist::
147+
:columns: 2
148+
149+
{active_session_hlist}
150+
151+
.. toctree::
152+
:hidden:
153+
154+
{active_session_toctree}

docs/source/releases/changes-in-this-fork.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ If you found any issue or have any suggestions, feel free to make `an issue <htt
2525
+------------------------+
2626

2727
- Updated :obj:`~pyrogram.filters.via_bot`, to optionally support filtering invalid bot ``user_id``.
28-
- Added the :meth:`~pyrogram.Client.get_active_sessions`.
28+
- Added the :meth:`~pyrogram.Client.get_active_sessions`, :meth:`~pyrogram.Client.terminate_session`, :meth:`~pyrogram.types.ActiveSession.terminate`.
2929
- Added the ``is_automatic_forward`` to the :obj:`~pyrogram.types.Message`.
3030
- Added the parameters ``offset_id`` to the :meth:`~pyrogram.Client.search_messages` and the parameters ``min_date``, ``max_date``, ``min_id``, ``max_id``, ``saved_messages_topic_id`` to the :meth:`~pyrogram.Client.search_messages_count`.
3131
- Dynamic session ReStart + restart optimizations (`#56 <https://github.com/TelegramPlayGround/pyrogram/pull/56>`__)

pyrogram/methods/auth/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from .sign_in_bot import SignInBot
3333
from .sign_up import SignUp
3434
from .terminate import Terminate
35+
from .terminate_session import TerminateSession
3536

3637

3738
class Auth(
@@ -50,6 +51,7 @@ class Auth(
5051
SignIn,
5152
SignInBot,
5253
SignUp,
53-
Terminate
54+
Terminate,
55+
TerminateSession,
5456
):
5557
pass
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present 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+
import pyrogram
20+
from pyrogram import raw
21+
22+
23+
class TerminateSession:
24+
async def terminate_session(
25+
self: "pyrogram.Client",
26+
session_id: int
27+
) -> bool:
28+
"""Terminates a session of the current user.
29+
30+
.. include:: /_includes/usable-by/users.rst
31+
32+
Parameters:
33+
session_id (``int``):
34+
Session identifier.
35+
36+
Returns:
37+
``bool``: On success, in case the session is destroyed, True is returned. Otherwise, False is returned.
38+
39+
Raises:
40+
RPCError: In case of a Telegram RPC error.
41+
42+
"""
43+
return await self.invoke(
44+
raw.functions.account.ResetAuthorization(hash=session_id)
45+
)

pyrogram/types/authorization/active_session.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,28 @@ def _parse(session: "raw.types.Authorization") -> "ActiveSession":
148148
can_accept_calls=not getattr(session, "call_requests_disabled", False),
149149
is_official_application=getattr(session, "official_app", None)
150150
)
151+
152+
async def terminate(self):
153+
"""Bound method *reset* of :obj:`~pyrogram.types.ActiveSession`.
154+
155+
Use as a shortcut for:
156+
157+
.. code-block:: python
158+
159+
await client.terminate_session(123456789)
160+
161+
Example:
162+
163+
.. code-block:: python
164+
165+
await session.reset()
166+
167+
Returns:
168+
True on success.
169+
170+
Raises:
171+
RPCError: In case of a Telegram RPC error.
172+
173+
"""
174+
175+
return await self._client.terminate_session(self.id)

0 commit comments

Comments
 (0)