Skip to content

Commit 577d4d8

Browse files
wulan17null-nick
authored andcommitted
BugFixes with TopicForum (Experimental Fixes)
1 parent 3cd6c39 commit 577d4d8

42 files changed

Lines changed: 974 additions & 131 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pyrogram/enums/message_service_type.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,6 @@ class MessageServiceType(AutoName):
4242
DELETE_CHAT_PHOTO = auto()
4343
"Deleted chat photo"
4444

45-
GROUP_CHAT_CREATED = auto()
46-
"Group chat created"
47-
48-
CHANNEL_CHAT_CREATED = auto()
49-
"Channel chat created"
50-
51-
MIGRATE_TO_CHAT_ID = auto()
52-
"Migrated to chat id"
53-
54-
MIGRATE_FROM_CHAT_ID = auto()
55-
"Migrated from chat id"
56-
57-
PINNED_MESSAGE = auto()
58-
"Pinned message"
59-
60-
GAME_HIGH_SCORE = auto()
61-
"Game high score"
62-
6345
FORUM_TOPIC_CREATED = auto()
6446
"a new forum topic created in the chat"
6547

@@ -78,6 +60,24 @@ class MessageServiceType(AutoName):
7860
GENERAL_TOPIC_UNHIDDEN = auto()
7961
"a forum general topic unhidden in the chat"
8062

63+
GROUP_CHAT_CREATED = auto()
64+
"Group chat created"
65+
66+
CHANNEL_CHAT_CREATED = auto()
67+
"Channel chat created"
68+
69+
MIGRATE_TO_CHAT_ID = auto()
70+
"Migrated to chat id"
71+
72+
MIGRATE_FROM_CHAT_ID = auto()
73+
"Migrated from chat id"
74+
75+
PINNED_MESSAGE = auto()
76+
"Pinned message"
77+
78+
GAME_HIGH_SCORE = auto()
79+
"Game high score"
80+
8181
VIDEO_CHAT_STARTED = auto()
8282
"Video chat started"
8383

pyrogram/methods/bots/send_game.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ async def send_game(
2828
chat_id: Union[int, str],
2929
game_short_name: str,
3030
disable_notification: bool = None,
31-
reply_to_message_id: int = None,
3231
message_thread_id: int = None,
32+
reply_to_message_id: int = None,
3333
protect_content: bool = None,
3434
reply_markup: Union[
3535
"types.InlineKeyboardMarkup",
@@ -55,6 +55,10 @@ async def send_game(
5555
Sends the message silently.
5656
Users will receive a notification with no sound.
5757
58+
message_thread_id (``int``, *optional*):
59+
Unique identifier of a message thread to which the message belongs.
60+
for supergroups only
61+
5862
reply_to_message_id (``int``, *optional*):
5963
If the message is a reply, ID of the original message.
6064

pyrogram/methods/chats/create_forum_topic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@ async def create_forum_topic(
6464
)
6565
)
6666

67-
return types.ForumTopicCreated._parse(r.updates[1].message.action)
67+
return types.ForumTopicCreated._parse(r.updates[1].message)

pyrogram/methods/chats/delete_forum_topic.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,11 @@ async def delete_forum_topic(
4646
4747
await app.delete_forum_topic(chat_id, topic_id)
4848
"""
49-
try:
50-
await self.invoke(
51-
raw.functions.channels.DeleteTopicHistory(
52-
channel=await self.resolve_peer(chat_id),
53-
top_msg_id=topic_id
54-
)
49+
await self.invoke(
50+
raw.functions.channels.DeleteTopicHistory(
51+
channel=await self.resolve_peer(chat_id),
52+
top_msg_id=topic_id
5553
)
56-
except Exception as e:
57-
print(e)
58-
return False
54+
)
55+
5956
return True

pyrogram/methods/chats/edit_forum_topic.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ async def edit_forum_topic(
2727
chat_id: Union[int, str],
2828
topic_id: int,
2929
title: str = None,
30-
icon_emoji_id: int = None
30+
icon_emoji_id: int = None,
31+
closed: bool = None,
32+
hidden: bool = None
3133
) -> bool:
3234
"""Edit a forum topic.
3335
@@ -44,7 +46,13 @@ async def edit_forum_topic(
4446
The forum topic title.
4547
4648
icon_emoji_id (``int``, *optional*):
47-
Unique identifier of the custom emoji shown as the topic icon
49+
Unique identifier of the custom emoji shown as the topic icon.
50+
51+
closed (``bool``, *optional*):
52+
Close forum topic.
53+
54+
hidden (``bool``, *optional*):
55+
Hide forum topic.
4856
4957
Returns:
5058
`bool`: On success, a Boolean is returned.
@@ -59,7 +67,10 @@ async def edit_forum_topic(
5967
channel=await self.resolve_peer(chat_id),
6068
topic_id=topic_id,
6169
title=title,
62-
icon_emoji_id=icon_emoji_id
70+
icon_emoji_id=icon_emoji_id,
71+
closed=closed,
72+
hidden=hidden
6373
)
6474
)
75+
6576
return True

pyrogram/methods/chats/get_forum_topics.py

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
import logging
20-
from typing import Union, Optional, AsyncGenerator
19+
from typing import Union, AsyncGenerator, Optional
2120

2221
import pyrogram
23-
from pyrogram import raw
24-
from pyrogram import types
25-
from pyrogram import utils
26-
27-
log = logging.getLogger(__name__)
22+
from pyrogram import types, raw, utils
2823

2924

3025
class GetForumTopics:
@@ -43,26 +38,66 @@ async def get_forum_topics(
4338
4439
limit (``int``, *optional*):
4540
Limits the number of topics to be retrieved.
41+
By default, no limit is applied and all topics are returned.
4642
4743
Returns:
48-
``Generator``: On success, a generator yielding :obj:`~pyrogram.types.ForumTopic` objects is returned.
44+
``Generator``: A generator yielding :obj:`~pyrogram.types.ForumTopic` objects.
4945
5046
Example:
5147
.. code-block:: python
5248
53-
# get all forum topics
49+
# Iterate through all topics
5450
async for topic in app.get_forum_topics(chat_id):
5551
print(topic)
56-
57-
Raises:
58-
ValueError: In case of invalid arguments.
5952
"""
53+
current = 0
54+
total = limit or (1 << 31) - 1
55+
limit = min(100, total)
56+
57+
offset_date = 0
58+
offset_id = 0
59+
offset_topic = 0
60+
61+
while True:
62+
r = await self.invoke(
63+
raw.functions.channels.GetForumTopics(
64+
channel=await self.resolve_peer(chat_id),
65+
offset_date=offset_date,
66+
offset_id=offset_id,
67+
offset_topic=offset_topic,
68+
limit=limit
69+
)
70+
)
71+
72+
users = {i.id: i for i in r.users}
73+
chats = {i.id: i for i in r.chats}
74+
75+
messages = {}
76+
77+
for message in r.messages:
78+
if isinstance(message, raw.types.MessageEmpty):
79+
continue
80+
81+
messages[message.id] = await types.Message._parse(self, message, users, chats)
82+
83+
topics = []
84+
85+
for topic in r.topics:
86+
topics.append(types.ForumTopic._parse(self, topic, messages, users, chats))
87+
88+
if not topics:
89+
return
90+
91+
last = topics[-1]
6092

61-
peer = await self.resolve_peer(chat_id)
93+
offset_id = last.top_message.id
94+
offset_date = utils.datetime_to_timestamp(last.top_message.date)
95+
offset_topic = last.id
6296

63-
rpc = raw.functions.channels.GetForumTopics(channel=peer, offset_date=0, offset_id=0, offset_topic=0, limit=limit)
97+
for topic in topics:
98+
yield topic
6499

65-
r = await self.invoke(rpc, sleep_threshold=-1)
100+
current += 1
66101

67-
for _topic in r.topics:
68-
yield types.ForumTopic._parse(_topic)
102+
if current >= total:
103+
return

pyrogram/methods/chats/get_forum_topics_by_id.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,30 @@ async def get_forum_topics_by_id(
6161
Raises:
6262
ValueError: In case of invalid arguments.
6363
"""
64-
ids, ids_type = (
65-
(topic_ids, int) if topic_ids
66-
else (None, None)
64+
is_iterable = not isinstance(topic_ids, int)
65+
ids = list(topic_ids) if is_iterable else [topic_ids]
66+
67+
r = await self.invoke(
68+
raw.functions.channels.GetForumTopicsByID(
69+
channel=await self.resolve_peer(chat_id),
70+
topics=ids
71+
)
6772
)
6873

69-
if ids is None:
70-
raise ValueError("No argument supplied. Either pass topic_ids")
74+
users = {i.id: i for i in r.users}
75+
chats = {i.id: i for i in r.chats}
7176

72-
peer = await self.resolve_peer(chat_id)
77+
messages = {}
7378

74-
is_iterable = not isinstance(ids, int)
75-
ids = list(ids) if is_iterable else [ids]
76-
ids = [i for i in ids]
79+
for message in r.messages:
80+
if isinstance(message, raw.types.MessageEmpty):
81+
continue
7782

78-
rpc = raw.functions.channels.GetForumTopicsByID(channel=peer, topics=ids)
83+
messages[message.id] = await types.Message._parse(self, message, users, chats)
7984

80-
r = await self.invoke(rpc, sleep_threshold=-1)
85+
topics = types.List()
8186

82-
if is_iterable:
83-
topic_list = []
84-
for topic in r.topics:
85-
topic_list.append(types.ForumTopic._parse(topic))
86-
topics = types.List(topic_list)
87-
else:
88-
topics = types.ForumTopic._parse(r.topics[0])
87+
for i in r.topics:
88+
topics.append(types.ForumTopic._parse(self, i, messages, users, chats))
8989

90-
return topics
90+
return topics if is_iterable else topics[0] if topics else None

pyrogram/methods/messages/copy_media_group.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ async def copy_media_group(
3131
message_id: int,
3232
captions: Union[List[str], str] = None,
3333
disable_notification: bool = None,
34-
reply_to_message_id: int = None,
3534
message_thread_id: int = None,
35+
reply_to_message_id: int = None,
3636
schedule_date: datetime = None,
3737
partial_reply: str = None,
3838
) -> List["types.Message"]:
@@ -67,6 +67,10 @@ async def copy_media_group(
6767
Sends the message silently.
6868
Users will receive a notification with no sound.
6969
70+
message_thread_id (``int``, *optional*):
71+
Unique identifier for the target message thread (topic) of the forum.
72+
for forum supergroups only.
73+
7074
reply_to_message_id (``int``, *optional*):
7175
If the message is a reply, ID of the original message.
7276
@@ -90,7 +94,7 @@ async def copy_media_group(
9094
await app.copy_media_group(to_chat, from_chat, 123)
9195
9296
await app.copy_media_group(to_chat, from_chat, 123, captions="single caption")
93-
97+
9498
await app.copy_media_group(to_chat, from_chat, 123,
9599
captions=["caption 1", None, ""])
96100
"""
@@ -118,8 +122,8 @@ async def copy_media_group(
118122
**await self.parser.parse(
119123
captions[i] if isinstance(captions, list) and i < len(captions) and captions[i] else
120124
captions if isinstance(captions, str) and i == 0 else
121-
message.caption if message.caption and message.caption != "None" and not type(
122-
captions) is str else "")
125+
message.caption if message.caption and message.caption != "None" and type(
126+
captions) is not str else "")
123127
)
124128
)
125129

pyrogram/methods/messages/copy_message.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from typing import Union, List, Optional
2222

2323
import pyrogram
24-
from pyrogram import types, enums
24+
from pyrogram import types, enums, utils
2525

2626
log = logging.getLogger(__name__)
2727

@@ -123,7 +123,8 @@ async def copy_message(
123123
parse_mode=parse_mode,
124124
caption_entities=caption_entities,
125125
disable_notification=disable_notification,
126-
reply_to_message_id=reply_to_message_id or message_thread_id,
126+
message_thread_id=message_thread_id,
127+
reply_to_message_id=reply_to_message_id,
127128
schedule_date=schedule_date,
128129
protect_content=protect_content,
129130
has_spoiler=has_spoiler,

pyrogram/methods/messages/send_animation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ async def send_animation(
4646
thumb: Union[str, BinaryIO] = None,
4747
file_name: str = None,
4848
disable_notification: bool = None,
49-
reply_to_message_id: int = None,
5049
message_thread_id: int = None,
50+
reply_to_message_id: int = None,
5151
partial_reply: str = None,
5252
schedule_date: datetime = None,
5353
protect_content: bool = None,
@@ -128,7 +128,8 @@ async def send_animation(
128128
If the message is a reply, ID of the original message.
129129
130130
message_thread_id (``int``, *optional*):
131-
If the message is in a thread, ID of the original message.
131+
Unique identifier for the target message thread (topic) of the forum.
132+
for forum supergroups only.
132133
133134
partial_reply (``str``, *optional*):
134135
Text to quote.

0 commit comments

Comments
 (0)