Skip to content

Commit 4b94c09

Browse files
authored
Fixed on_comment event dispatching multiple times for a single comment (#619)
1 parent c61ac43 commit 4b94c09

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

docs/extensions/annotations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def visit_any(self, t: types.AnyType) -> Any:
8080
return Any
8181

8282
def visit_overloaded(self, t: types.Overloaded) -> GenericAlias:
83-
return Union[self.collect_types(t.items)]
83+
return Union[self.collect_types(t.items)] # noqa: UP007
8484

8585
def visit_callable_type(self, t: types.CallableType) -> GenericAlias:
8686
return Callable[list(self.collect_types(t.arg_types)), self.collect_types([t.ret_type])[0]]
@@ -116,7 +116,7 @@ def visit_union_type(self, t: types.UnionType) -> Any:
116116
return t.items[0]
117117
if all(isinstance(arg, LiteralGenericAlias) for arg in t.items):
118118
return Literal[tuple(t.__args__[0] for t in self.collect_types(t.items))]
119-
return Union[self.collect_types(t.items)]
119+
return Union[self.collect_types(t.items)] # noqa: UP007
120120

121121
def visit_type_alias_type(self, t: types.TypeAliasType) -> Any:
122122
return t.expand_all_if_possible().accept(self) if not t.is_recursive else self.get(t.alias.fullname)

steam/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class ClientKwargs(TypedDict, total=False):
101101
connector: aiohttp.BaseConnector | None
102102
intents: Intents
103103
max_messages: int | None
104+
max_comments: int | None
104105
app: App | None
105106
apps: list[App]
106107
state: PersonaState
@@ -133,6 +134,8 @@ class Client:
133134
The intents you wish to start the client with.
134135
max_messages
135136
The maximum number of messages to store in the internal cache, default is 1000.
137+
max_comments
138+
the maximum number of comments to store in the internal cache, default is 10000.
136139
app
137140
An app to set your status as on connect. This will take precedence over any apps set using ``apps`` as your
138141
current status.

steam/ext/csgo/protobufs/cstrike.py

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

steam/package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ async def fetch_manifest(
253253
else:
254254
err = e
255255
if sys.version_info >= (3, 11):
256-
raise ExceptionGroup("No manifest found with id and depot_id supplied", errs)
256+
raise ExceptionGroup("No manifest found with id and depot_id supplied", errs) # noqa: F821
257257
else:
258258
raise err
259259

steam/state.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ def __init__(self, client: Client, **kwargs: Unpack[ClientKwargs]):
196196
self.handled_wallet = asyncio.Event()
197197
self.intents: Final = kwargs.get("intents", Intents.Safe)
198198
self.max_messages: int | None = kwargs.get("max_messages", 1000)
199+
self.max_comments: int = kwargs.get("max_comments", 10000)
200+
self._process_comment_lock: asyncio.Lock = asyncio.Lock()
201+
self._processed_comment_ids: set[int] = set()
199202

200203
app = kwargs.get("app")
201204
apps = kwargs.get("apps")
@@ -2387,7 +2390,15 @@ async def handle_notifications(self, msg: notifications.GetSteamNotificationsRes
23872390
log.info("Unknown commentable type %d", type)
23882391
continue
23892392
try:
2390-
self.dispatch("comment", await commentable.fetch_comment(int(body["cgid"])))
2393+
comment = await commentable.fetch_comment(int(body["cgid"]))
2394+
async with self._process_comment_lock: # prevents multiple dispatch for a single comment
2395+
if comment.id in self._processed_comment_ids:
2396+
log.debug("Ignoring processed comment: %s", comment.id)
2397+
continue
2398+
if len(self._processed_comment_ids) > self.max_comments:
2399+
self._processed_comment_ids.pop()
2400+
self._processed_comment_ids.add(comment.id)
2401+
self.dispatch("comment", comment)
23912402
except (WSException, KeyError):
23922403
log.info("Failed to fetch comment %s", notification, exc_info=True)
23932404
case 9: # trade, this is only going to happen at startup

0 commit comments

Comments
 (0)