Trakt was the one service outside the tracker abstraction. TraktScrobbleService re-implemented the whole playback lifecycle beside TrackerCoordinator, and TraktSyncService pushed watched state from its own WatchStateNotifier subscription, so the player called two objects at every lifecycle point and one watch could be written twice. TraktTracker now implements RealtimeScrobbleTracker like Simkl; the duplicated player call sites collapse to one each, and Trakt shares the coordinator's ID resolver instead of re-fetching show ids every episode. Capabilities are split so a tracker declares what it is rather than being special-cased: ScrobblePolicy carries each service's own resend/seek rules, EpisodeHistoryTracker names the remote row a per-item history write targets, and SeriesProgressTracker covers one-counter-per-series services. Writes from all four trackers go through a shared TrackerWriteQueue, generalised from the Trakt-only queue, with the legacy Trakt payload migrated on load. Trakt becomes the fourth TrackersProvider slot and TraktAccountProvider is deleted, so one object owns the active session per profile. Two failure paths found while consolidating are fixed here too. The queue's retries only ran on profile bind, connect and app foreground, so a network blip mid-session left queued watches waiting for the next foreground. OfflineModeProvider now notifies on connectivity changes, not just offline-state or WiFi-flag changes, and main.dart flushes the queue when the network returns. The queue also counted every failure toward the five attempts that permanently drop an item, so a rate limit or a service having a bad hour could discard a pending watch - the loss the queue exists to prevent. Only an answer about the write itself now spends an attempt: 4xx counts, while rate limits, 5xx, recoverable token-refresh failures and requests that never arrived do not. A back-off answer also defers that service for the rest of the flush, so a queue holding many rows does not fire all of them at a service that just asked for quiet.
98 lines
3.3 KiB
Dart
98 lines
3.3 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../../../models/trackers/device_code.dart';
|
|
import '../../../utils/abortable_http_request.dart';
|
|
import '../../../utils/app_logger.dart';
|
|
import '../device_code_auth_service.dart';
|
|
import '../tracker_constants.dart';
|
|
import '../tracker_session.dart';
|
|
import 'trakt_constants.dart';
|
|
|
|
/// Trakt OAuth Device Authorization Grant flow (RFC 8628).
|
|
///
|
|
/// The user enters a short code at `trakt.tv/activate` (in any browser); the
|
|
/// app polls `/oauth/device/token` until the user completes the flow.
|
|
class TraktAuthService extends DeviceCodeAuthServiceBase {
|
|
TraktAuthService({super.httpClient});
|
|
|
|
@override
|
|
Future<DeviceCode> createDeviceCode() async {
|
|
final uri = Uri.parse(TraktConstants.deviceCodeUrl);
|
|
final sw = Stopwatch()..start();
|
|
final res = await sendAbortableHttpRequest(
|
|
httpClient,
|
|
'POST',
|
|
uri,
|
|
headers: TraktConstants.headers(),
|
|
body: json.encode({'client_id': TraktConstants.clientId}),
|
|
timeout: TrackerConstants.authRequestTimeout,
|
|
operation: 'Trakt device code request',
|
|
);
|
|
sw.stop();
|
|
appLogger.d('Trakt POST ${uri.path} → ${res.statusCode} (${sw.elapsedMilliseconds}ms)');
|
|
|
|
if (res.statusCode != 200) {
|
|
throw DeviceCodeAuthFlowException('Trakt device code request failed: HTTP ${res.statusCode}');
|
|
}
|
|
|
|
final body = json.decode(res.body) as Map<String, dynamic>;
|
|
final verificationUrl = body['verification_url'] as String;
|
|
final userCode = body['user_code'] as String;
|
|
return DeviceCode(
|
|
deviceCode: body['device_code'] as String,
|
|
userCode: userCode,
|
|
verificationUrl: verificationUrl,
|
|
verificationUrlComplete: '$verificationUrl/$userCode',
|
|
expiresIn: (body['expires_in'] as num).toInt(),
|
|
interval: (body['interval'] as num).toInt(),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Future<DevicePollEvent> probe(DeviceCode code) async {
|
|
final tokenUri = Uri.parse(TraktConstants.deviceTokenUrl);
|
|
final http.Response res;
|
|
try {
|
|
res = await sendAbortableHttpRequest(
|
|
httpClient,
|
|
'POST',
|
|
tokenUri,
|
|
headers: TraktConstants.headers(),
|
|
body: json.encode({
|
|
'code': code.deviceCode,
|
|
'client_id': TraktConstants.clientId,
|
|
'client_secret': TraktConstants.clientSecret,
|
|
}),
|
|
timeout: TrackerConstants.authRequestTimeout,
|
|
operation: 'Trakt device token poll',
|
|
);
|
|
appLogger.d('Trakt POST ${tokenUri.path} → ${res.statusCode}');
|
|
} catch (e) {
|
|
appLogger.d('Trakt device-code poll error (transient)', error: e);
|
|
return const DevicePollPending();
|
|
}
|
|
|
|
switch (res.statusCode) {
|
|
case 200:
|
|
return DevicePollSuccess(json.decode(res.body) as Map<String, dynamic>);
|
|
case 400:
|
|
return const DevicePollPending();
|
|
case 404 || 410:
|
|
return const DevicePollExpired();
|
|
case 409 || 418:
|
|
return const DevicePollDenied();
|
|
case 429:
|
|
return const DevicePollSlowDown();
|
|
default:
|
|
appLogger.w('Trakt device-code unexpected HTTP ${res.statusCode}');
|
|
return const DevicePollPending();
|
|
}
|
|
}
|
|
|
|
@override
|
|
TrackerSession buildSession(Map<String, dynamic> tokenResponse) =>
|
|
TrackerSession.fromTokenResponse(TrackerService.trakt, tokenResponse);
|
|
}
|