Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 53 additions & 16 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import 'dart:math';
import 'dart:typed_data';

import 'package:async/async.dart' as async;
import 'package:async/async.dart';
import 'package:collection/collection.dart' show IterableExtension;
import 'package:http/http.dart' as http;
Expand Down Expand Up @@ -2147,6 +2148,8 @@
'Successfully connected as ${userID.localpart} with ${homeserver.toString()}',
);

await ensureNotSoftLoggedOut();

/// Timeout of 0, so that we don't see a spinner for 30 seconds.
firstSyncReceived = _sync(timeout: Duration.zero);
if (waitForFirstSync) {
Expand Down Expand Up @@ -2275,30 +2278,68 @@

_handleSoftLogoutFuture ??= () async {
onLoginStateChanged.add(LoginState.softLoggedOut);
try {
await onSoftLogout(this);
onLoginStateChanged.add(LoginState.loggedIn);
} catch (e, s) {
Logs().w('Unable to refresh session after soft logout', e, s);
await logout();
rethrow;

async.Result? softLogoutResult;

while (softLogoutResult?.isValue != true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't #2033 with a debounce be better?

softLogoutResult = await async.Result.capture(onSoftLogout(this));
final error = softLogoutResult.asError?.error;

if (error is MatrixException) {
final retryAfterMs = error.retryAfterMs;

Check warning on line 2289 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2289

Added line #L2289 was not covered by tests
if (retryAfterMs != null) {
Logs().w(

Check warning on line 2291 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2291

Added line #L2291 was not covered by tests
'Rate limit while attempting to refresh access token. Waiting seconds...',
retryAfterMs / 1000,

Check warning on line 2293 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2293

Added line #L2293 was not covered by tests
);
await Future.delayed(Duration(milliseconds: retryAfterMs));

Check warning on line 2295 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2295

Added line #L2295 was not covered by tests
} else {
Logs().wtf(

Check warning on line 2297 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2297

Added line #L2297 was not covered by tests
'Unable to login after soft logout! Logging out.',
error,
softLogoutResult.asError?.stackTrace,

Check warning on line 2300 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2300

Added line #L2300 was not covered by tests
);
await logout();

Check warning on line 2302 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2302

Added line #L2302 was not covered by tests
return;
}
} else if (error != null) {
Logs().w(

Check warning on line 2306 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2306

Added line #L2306 was not covered by tests
'Unable to login after soft logout! Try again...',
error,
softLogoutResult.asError?.stackTrace,

Check warning on line 2309 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2309

Added line #L2309 was not covered by tests
);
await Future.delayed(const Duration(seconds: 1));

Check warning on line 2311 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2311

Added line #L2311 was not covered by tests
}
}
}();
await _handleSoftLogoutFuture;
_handleSoftLogoutFuture = null;
}

Timer? _softLogoutTimer;
Timer? get softLogoutTimer => _softLogoutTimer;

Check warning on line 2320 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2320

Added line #L2320 was not covered by tests

/// Checks if the token expires in under [expiresIn] time and calls the
/// given `onSoftLogout()` if so. You have to provide `onSoftLogout` in the
/// Client constructor. Otherwise this will do nothing.
Future<void> ensureNotSoftLoggedOut([
Duration expiresIn = const Duration(minutes: 1),
bool setTimerForNextSoftLogout = true,
]) async {
final tokenExpiresAt = accessTokenExpiresAt;
if (onSoftLogout != null &&
tokenExpiresAt != null &&
tokenExpiresAt.difference(DateTime.now()) <= expiresIn) {
await _handleSoftLogout();
var tokenExpiresAt = accessTokenExpiresAt;
if (isLogged() && onSoftLogout != null && tokenExpiresAt != null) {
_softLogoutTimer?.cancel();
if (tokenExpiresAt.difference(DateTime.now()) <= expiresIn) {
Logs().d('Handle soft logout...');
await _handleSoftLogout();

Check warning on line 2334 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2331-L2334

Added lines #L2331 - L2334 were not covered by tests
}
tokenExpiresAt = accessTokenExpiresAt;

Check warning on line 2336 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2336

Added line #L2336 was not covered by tests
if (setTimerForNextSoftLogout && tokenExpiresAt != null) {
final doNextSoftLogoutIn = tokenExpiresAt.difference(DateTime.now()) -

Check warning on line 2338 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2338

Added line #L2338 was not covered by tests
const Duration(minutes: 1);
Logs().v('Next token refresh will be triggered in $doNextSoftLogoutIn');
_softLogoutTimer = Timer(doNextSoftLogoutIn, ensureNotSoftLoggedOut);

Check warning on line 2341 in lib/src/client.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/client.dart#L2340-L2341

Added lines #L2340 - L2341 were not covered by tests
}
}
}

Expand All @@ -2320,10 +2361,6 @@
// amount of time if nothing happens.
if (prevBatch != null) timeout ??= const Duration(seconds: 30);

await ensureNotSoftLoggedOut(
timeout == null ? const Duration(minutes: 1) : (timeout * 2),
);

await _checkSyncFilter();

final syncRequest = sync(
Expand Down