Skip to content

修改群修改topic后,本地缓存未更新的bug #438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/wechaty/user/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ def filter_func(room: Room) -> bool:
payload = room.payload
if not payload:
return False
if query == payload.id or (query.lower() in payload.topic.lower()): # type: ignore
if query == payload.id or (query.lower() in payload.topic.lower()): # type: ignore
return True
return False

func = filter_func
elif isinstance(query, RoomQueryFilter):
def filter_func(room: Room) -> bool:
Expand All @@ -179,9 +180,10 @@ def filter_func(room: Room) -> bool:
if not payload:
return False

if query.id == payload.id or (query.topic.lower() in payload.topic.lower()): # noqa
if query.id == payload.id or (query.topic.lower() in payload.topic.lower()): # noqa
return True
return False

func = filter_func
elif isinstance(query, types.FunctionType):
func = query
Expand Down Expand Up @@ -230,7 +232,7 @@ async def find_all(cls,
rooms: List[Room] = [cls.load(room_id) for room_id in room_ids]
tasks: List[Task] = [asyncio.create_task(room.ready()) for room in rooms]
await gather_with_concurrency(PARALLEL_TASK_NUM, tasks)

# 2. filter the rooms
if not query:
return rooms
Expand Down Expand Up @@ -290,6 +292,15 @@ def load(cls, room_id: str) -> Room:
cls._pool[room_id] = room
return room

@classmethod
def upload_cache(cls, room_id: str)->None:
"""
dynamic upload
clear the room_id of _pool and upload it use load
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a wording issue in the docstring: "clear the room_id of _pool and upload it use load". Consider rephrasing it to something like "clear the room_id from _pool and re-upload it using load", to improve clarity.

Suggested change
clear the room_id of _pool and upload it use load
clear the room_id from _pool and re-upload it using load

"""
cls._pool[room_id] = None
cls.load(room_id)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve the upload_cache method design and implementation.

The current implementation has several issues:

  1. Confusing method name: upload_cache suggests uploading to cache, but it's actually clearing/refreshing the cache.
  2. Should return the Room instance: For better usability and to fix the bug in wechaty.py.
  3. Use del instead of setting to None: Setting to None keeps the key in the dictionary.
  4. Unclear docstring: The purpose and behavior could be better explained.

Apply this diff to improve the method:

 @classmethod
-def upload_cache(cls, room_id: str)->None:
+def refresh_cache(cls, room_id: str) -> 'Room':
     """
-    dynamic upload
-    clear the room_id of _pool and upload it use load
+    Refresh the cached room instance by clearing and reloading it.
+    
+    Args:
+        room_id: The room ID to refresh
+        
+    Returns:
+        Room: The refreshed room instance
     """
-    cls._pool[room_id] = None
-    cls.load(room_id)
+    if room_id in cls._pool:
+        del cls._pool[room_id]
+    return cls.load(room_id)

Note: If you rename this method, also update the call in src/wechaty/wechaty.py line 716.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@classmethod
def upload_cache(cls, room_id: str)->None:
"""
dynamic upload
clear the room_id of _pool and upload it use load
"""
cls._pool[room_id] = None
cls.load(room_id)
@classmethod
def refresh_cache(cls, room_id: str) -> 'Room':
"""
Refresh the cached room instance by clearing and reloading it.
Args:
room_id: The room ID to refresh
Returns:
Room: The refreshed room instance
"""
if room_id in cls._pool:
del cls._pool[room_id]
return cls.load(room_id)
🤖 Prompt for AI Agents
In src/wechaty/user/room.py lines 295 to 302, rename the method upload_cache to
a clearer name like refresh_cache to reflect its purpose of clearing and
reloading the cache. Change the implementation to delete the room_id key from
the _pool dictionary using del instead of setting it to None, and then call
cls.load(room_id) to reload the cache. Modify the method to return the Room
instance obtained from cls.load(room_id) for better usability. Update the
docstring to clearly describe that the method refreshes the cache for the given
room_id by removing the cached entry and reloading it. Also, remember to update
the corresponding call in src/wechaty/wechaty.py at line 716 to use the new
method name.


def __str__(self) -> str:
"""
string format for room instance
Expand Down Expand Up @@ -349,7 +360,7 @@ async def ready(self, force_sync: bool = False, load_members: bool = False) -> N

async def say(self,
some_thing: Union[str, Contact,
FileBox, MiniProgram, UrlLink],
FileBox, MiniProgram, UrlLink],
mention_ids: Optional[List[str]] = None
) -> Union[None, Message]:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/wechaty/wechaty.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ async def room_leave_listener(payload: EventRoomLeavePayload) -> None:
async def room_topic_listener(payload: EventRoomTopicPayload) -> None:
log.info('receive <room-topic> event <%s>', payload)

room: Room = self.Room.load(payload.room_id)
room: Room = self.Room.upload_cache(payload.room_id)
await room.ready()

changer = self.Contact.load(payload.changer_id)
Expand Down
Loading