Skip to content

Commit afec8bb

Browse files
committed
feat: add non-managed user filtering to yield_all_for_chat method
1 parent 976839d commit afec8bb

File tree

1 file changed

+10
-6
lines changed
  • backend/core/src/core/services/chat

1 file changed

+10
-6
lines changed

backend/core/src/core/services/chat/user.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,24 @@ def get_all(
171171
return query.all()
172172

173173
def yield_all_for_chat(
174-
self, chat_id: int, batch_size: int = 100
174+
self, chat_id: int, non_managed_only: bool = False, batch_size: int = 100
175175
) -> Iterable[list[TelegramChatUser]]:
176176
"""
177177
Yields all users for a given chat in batches, using keyset pagination.
178178
This is useful for processing large chats without loading all users into memory.
179179
"""
180180
last_seen_user_id = 0
181181
while True:
182+
filters = [
183+
TelegramChatUser.chat_id == chat_id,
184+
TelegramChatUser.user_id > last_seen_user_id,
185+
]
186+
if non_managed_only:
187+
filters.append(TelegramChatUser.is_managed.is_(False))
188+
182189
stmt = (
183190
select(TelegramChatUser)
184-
.where(
185-
TelegramChatUser.chat_id == chat_id,
186-
TelegramChatUser.user_id > last_seen_user_id,
187-
)
191+
.where(*filters)
188192
.order_by(TelegramChatUser.user_id.asc())
189193
.limit(batch_size)
190194
.options(
@@ -193,7 +197,7 @@ def yield_all_for_chat(
193197
)
194198
)
195199
)
196-
users = self.db_session.execute(stmt).scalars().unique().all()
200+
users = list(self.db_session.execute(stmt).scalars().unique().all())
197201

198202
if not users:
199203
break

0 commit comments

Comments
 (0)