diff --git a/lib/friendica_client/friendica_client.dart b/lib/friendica_client/friendica_client.dart index 475b9c2..3b1443c 100644 --- a/lib/friendica_client/friendica_client.dart +++ b/lib/friendica_client/friendica_client.dart @@ -41,6 +41,9 @@ import '../services/network_status_service.dart'; import '../utils/network_utils.dart'; import 'paging_data.dart'; +const _maxProcessingMillis = 3; +const _processingSleep = Duration(milliseconds: 1); + class DirectMessagingClient extends FriendicaClient { static final _logger = Logger('$DirectMessagingClient'); @@ -378,13 +381,22 @@ class NotificationsClient extends FriendicaClient { final url = 'https://$serverName/api/v1/notifications?include_all=true'; final request = Uri.parse('$url&${page.toQueryParameters()}'); _logger.finest(() => 'Getting new notifications'); - final result = await _getApiListRequest(request); + final result = + await _getApiListRequest(request).transformAsync((response) async { + final notifications = []; + + final st = Stopwatch()..start(); + for (final json in response.data) { + if (st.elapsedMilliseconds > _maxProcessingMillis) { + await Future.delayed(_processingSleep, () => st.reset()); + } + notifications.add(NotificationMastodonExtension.fromJson(json)); + } + return PagedResponse(notifications, + id: response.id, previous: response.previous, next: response.next); + }); _networkStatusService.finishNotificationUpdate(); - return result - .andThenSuccess((response) => response.map((jsonArray) => jsonArray - .map((json) => NotificationMastodonExtension.fromJson(json)) - .toList())) - .execErrorCast(); + return result.execErrorCast(); } FutureResult clearNotifications() async { @@ -894,13 +906,12 @@ class TimelineClient extends FriendicaClient { ? '$baseUrl$pagingData' : '${baseUrl}screen_name=$userId$pagingData'; final request = Uri.parse(url); - final result = (await _getApiListRequest(request).andThenSuccessAsync( - (postsJson) async => postsJson.data - .map((json) => TimelineEntryMastodonExtensions.fromJson(json)) - .toList())) - .execErrorCast(); + final result = await _getApiListRequest(request).transformAsync( + (postsJson) async => await _timelineEntriesFromJson(postsJson.data), + ); + _networkStatusService.finishTimelineLoading(); - return result; + return result.execErrorCast(); } FutureResult, ExecError> getTimeline( @@ -913,13 +924,26 @@ class TimelineClient extends FriendicaClient { '$baseUrl?exclude_replies=true&${page.toQueryParameters()}&$timelineQPs'; final request = Uri.parse(url); _logger.finest(() => 'Getting ${type.toHumanKey()} with paging data $page'); - final result = (await _getApiListRequest(request).andThenSuccessAsync( - (postsJson) async => postsJson.data - .map((json) => TimelineEntryMastodonExtensions.fromJson(json)) - .toList())) - .execErrorCast(); + final result = await _getApiListRequest(request).transformAsync( + (postsJson) async => await _timelineEntriesFromJson(postsJson.data), + ); _networkStatusService.finishTimelineLoading(); - return result; + return result.execErrorCast(); + } + + Future> _timelineEntriesFromJson( + List postsJson, + ) async { + final finalResult = []; + final st = Stopwatch()..start(); + for (final json in postsJson) { + if (st.elapsedMilliseconds > _maxProcessingMillis) { + await Future.delayed(_processingSleep, () => st.reset()); + } + finalResult.add(TimelineEntryMastodonExtensions.fromJson(json)); + } + + return finalResult; } String _typeToTimelinePath(TimelineIdentifiers type) {