From 08d476d11e939e5acdc3e04db5dd687d10d5e320 Mon Sep 17 00:00:00 2001 From: Tommy Chow Date: Fri, 5 Aug 2022 03:24:35 -0400 Subject: [PATCH] Fix large blocked users list preventing login (#175) * Remove await from get blocked users * Implement request limit to blocked users --- lib/api/twitch_api.dart | 6 ++++++ lib/core/user/user_store.dart | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/api/twitch_api.dart b/lib/api/twitch_api.dart index 3ec02566..e864107b 100644 --- a/lib/api/twitch_api.dart +++ b/lib/api/twitch_api.dart @@ -410,6 +410,12 @@ class TwitchApi { final result = blockedList.map((e) => UserBlockedTwitch.fromJson(e)).toList(); if (cursor != null) { + // Wait a bit (150 milliseconds) before recursively calling. + // This will prevent going over the rate limit to due a massive blocked users list. + // + // With the Twitch API, we can make up to 800 requests per minute. + // Waiting 150 milliseconds between requests will cap the rate here at 400 requests per minute. + await Future.delayed(const Duration(milliseconds: 150)); result.addAll(await getUserBlockedList(id: id, cursor: cursor, headers: headers)); } diff --git a/lib/core/user/user_store.dart b/lib/core/user/user_store.dart index a4a2994a..b4dcc375 100644 --- a/lib/core/user/user_store.dart +++ b/lib/core/user/user_store.dart @@ -27,7 +27,10 @@ abstract class UserStoreBase with Store { _details = await twitchApi.getUserInfo(headers: headers); // Get and update the current user's list of blocked users. - if (_details?.id != null) _blockedUsers = (await twitchApi.getUserBlockedList(id: _details!.id, headers: headers)).asObservable(); + // Don't use await because having a huge list of blocked users will block the UI. + if (_details?.id != null) { + twitchApi.getUserBlockedList(id: _details!.id, headers: headers).then((blockedUsers) => _blockedUsers = blockedUsers.asObservable()); + } _disposeReaction = autorun((_) => _blockedUsers.sort((a, b) => a.userLogin.compareTo(b.userLogin))); }