getContactDetails function

Future<Map<String, dynamic>> getContactDetails(
  1. dynamic atSign
)

returns atSign's AtContact details from server

Implementation

Future<Map<String, dynamic>> getContactDetails(atSign) async {
  final _logger = AtSignLogger('getContactDetails');

  var contactDetails = <String, dynamic>{};

  if (EventKeyStreamService().atClientManager == null || atSign == null) {
    return contactDetails;
  } else if (!atSign.contains('@')) {
    atSign = '@$atSign';
  }
  var metadata = Metadata();
  metadata.isPublic = true;
  metadata.namespaceAware = false;
  var key = AtKey();
  key.sharedBy = atSign;
  key.metadata = metadata;
  var contactFields = [
    'firstname.persona',
    'lastname.persona',
    'image.persona',
  ];

  try {
    // firstname
    key.key = contactFields[0];
    var result = await EventKeyStreamService()
        .atClientManager
        .atClient
        .get(key)
        .catchError(
            // ignore: return_of_invalid_type_from_catch_error
            (e) => _logger
                .severe('error in get ${e.errorCode} ${e.errorMessage}'));
    var firstname = result.value;

    // lastname
    key.key = contactFields[1];
    result = await EventKeyStreamService().atClientManager.atClient.get(key);
    var lastname = result.value;

    // construct name
    var name = ((firstname ?? '') + ' ' + (lastname ?? '')).trim();
    if (name.length == 0) {
      name = atSign.substring(1);
    }

    // profile picture
    key.metadata.isBinary = true;
    key.key = contactFields[2];
    result = await EventKeyStreamService().atClientManager.atClient.get(key);
    var image = result.value;
    contactDetails['name'] = name;
    contactDetails['image'] = image;
  } catch (e) {
    contactDetails['name'] = null;
    contactDetails['image'] = null;
  }
  return contactDetails;
}