getUserProfile method

  1. @override
Future<CachedProfileInformation> getUserProfile(
  1. String userId, {
  2. Duration timeout = const Duration(seconds: 30),
  3. Duration maxCacheAge = const Duration(days: 1),
})
override

Get the combined profile information for this user. First checks for a non outdated cached profile before requesting from the server. Cached profiles are outdated if they have been cached in a time older than the maxCacheAge or they have been marked as outdated by an event in the sync loop. In case of an

userId The user whose profile information to get.

Implementation

@override
Future<CachedProfileInformation> getUserProfile(
  String userId, {
  Duration timeout = const Duration(seconds: 30),
  Duration maxCacheAge = const Duration(days: 1),
}) async {
  final cachedProfile = await database?.getUserProfile(userId);
  if (cachedProfile != null &&
      !cachedProfile.outdated &&
      DateTime.now().difference(cachedProfile.updated) < maxCacheAge) {
    return cachedProfile;
  }

  final ProfileInformation profile;
  try {
    profile = await (_userProfileRequests[userId] ??=
        super.getUserProfile(userId).timeout(timeout));
  } catch (e) {
    Logs().d('Unable to fetch profile from server', e);
    if (cachedProfile == null) rethrow;
    return cachedProfile;
  } finally {
    unawaited(_userProfileRequests.remove(userId));
  }

  final newCachedProfile = CachedProfileInformation.fromProfile(
    profile,
    outdated: false,
    updated: DateTime.now(),
  );

  await database?.storeUserProfile(userId, newCachedProfile);

  return newCachedProfile;
}