setUserAvatar method Null safety

  1. @protected
Future<bool> setUserAvatar(
  1. {required User user,
  2. required MediaAsset asset}
)

Sets the referenced asset as avatar. The asset needs to be present in the CMS already. INTERNAL USE ONLY

Implementation

@protected
Future<bool> setUserAvatar(
    {required final User user, required final MediaAsset asset}) async {
  // we can't work with an empty Firebase uid or with a local asset
  if (user.firebaseUid == null ||
      asset.location == MediaAssetLocation.local) {
    return false;
  }

  final userId = await fetchUserId(user.firebaseUid!);

  if (userId == null) {
    return false;
  }

  final gqlQueryString = '''
    mutation SetUserAvatar {
      updateUser(
        input: {
          where: { id: "$userId" }
          data: {
            avatar: "${asset.id}",
          }
        }
      ) {
        user {
          ${UserGqlEntity.gqlQueryFragment}
        }
      }
    }
  ''';

  try {
    final queryResult = await graphQLClient
        .mutate<void>(MutationOptions(document: gql(gqlQueryString)));

    if (queryResult.hasException) {
      logger?.e(queryResult.exception.toString());
      return false;
    }
  } catch (e, stackTrace) {
    logger?.e('Could not set avatar on user instance', e, stackTrace);
    return false;
  }

  return true;
}