public static async Task <Contact> AddOrUpdateContactAsync(DiscordRelationship relationship, ContactList list = null, ContactAnnotationList annotationList = null, StorageFolder folder = null) { if (list == null) { var store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite); // requests contact permissions var lists = await store.FindContactListsAsync(); list = lists.FirstOrDefault(l => l.DisplayName == CONTACT_LIST_NAME) ?? (await store.CreateContactListAsync(CONTACT_LIST_NAME)); } if (annotationList == null) { var annotationStore = await ContactManager.RequestAnnotationStoreAsync(ContactAnnotationStoreAccessType.AppAnnotationsReadWrite); annotationList = await Tools.GetAnnotationListAsync(annotationStore); } folder = folder ?? await ApplicationData.Current.LocalFolder.CreateFolderAsync("AvatarCache", CreationCollisionOption.OpenIfExists); Contact contact; if ((contact = await list.GetContactFromRemoteIdAsync(string.Format(REMOTE_ID_FORMAT, relationship.User.Id))) == null) { Logger.Log($"Creating new contact for user {relationship.User}"); contact = new Contact { RemoteId = string.Format(REMOTE_ID_FORMAT, relationship.User.Id) }; } if (contact.Name != relationship.User.Username) { Logger.Log($"Updating contact username for {relationship.User}"); contact.Name = relationship.User.Username; } var currentHash = App.LocalSettings.Read <string>("ContactAvatarHashes", relationship.User.Id.ToString(), null); if (currentHash == null || relationship.User.AvatarHash != currentHash) { Logger.Log($"Updating contact avatar for {relationship.User}"); contact.SourceDisplayPicture = await GetAvatarReferenceAsync(relationship.User, folder); } await list.SaveContactAsync(contact); var annotations = await annotationList.FindAnnotationsByRemoteIdAsync(contact.RemoteId); if (!annotations.Any()) { Logger.Log($"Creating new contact annotation for user {relationship.User}"); var annotation = new ContactAnnotation() { ContactId = contact.Id, RemoteId = string.Format(REMOTE_ID_FORMAT, relationship.User.Id), SupportedOperations = ContactAnnotationOperations.Share | ContactAnnotationOperations.AudioCall | ContactAnnotationOperations.Message | ContactAnnotationOperations.ContactProfile | ContactAnnotationOperations.SocialFeeds | ContactAnnotationOperations.VideoCall, ProviderProperties = { ["ContactPanelAppID"] = APP_ID, ["ContactShareAppID"] = APP_ID } }; await annotationList.TrySaveAnnotationAsync(annotation); } return(contact); }
private async Task ExportAsync(ContactList contactList, ContactAnnotationList annotationList, Telegram.Td.Api.Users result) { if (result == null) { return; } var remove = new List <long>(); var prev = _contacts; var next = result.UserIds.ToHashSet(); if (prev != null) { foreach (var id in prev) { if (!next.Contains(id)) { remove.Add(id); } } } _contacts = next; foreach (var item in remove) { var contact = await contactList.GetContactFromRemoteIdAsync("u" + item); if (contact != null) { await contactList.DeleteContactAsync(contact); } } foreach (var item in result.UserIds) { var user = _protoService.GetUser(item); var contact = await contactList.GetContactFromRemoteIdAsync("u" + user.Id); if (contact == null) { contact = new Contact(); } if (user.ProfilePhoto != null && Telegram.Td.Api.Extensions.IsFileExisting(user.ProfilePhoto.Small.Local)) { contact.SourceDisplayPicture = await StorageFile.GetFileFromPathAsync(user.ProfilePhoto.Small.Local.Path); } contact.FirstName = user.FirstName; contact.LastName = user.LastName; //contact.Nickname = item.Username ?? string.Empty; contact.RemoteId = "u" + user.Id; //contact.Id = item.Id.ToString(); if (user.PhoneNumber.Length > 0) { var phone = contact.Phones.FirstOrDefault(); if (phone == null) { phone = new ContactPhone(); phone.Kind = ContactPhoneKind.Mobile; phone.Number = string.Format("+{0}", user.PhoneNumber); contact.Phones.Add(phone); } else { phone.Kind = ContactPhoneKind.Mobile; phone.Number = string.Format("+{0}", user.PhoneNumber); } } await contactList.SaveContactAsync(contact); ContactAnnotation annotation; var annotations = await annotationList.FindAnnotationsByRemoteIdAsync(user.Id.ToString()); if (annotations.Count == 0) { annotation = new ContactAnnotation(); } else { annotation = annotations[0]; } annotation.ContactId = contact.Id; annotation.RemoteId = contact.RemoteId; annotation.SupportedOperations = ContactAnnotationOperations.ContactProfile | ContactAnnotationOperations.Message | ContactAnnotationOperations.AudioCall | ContactAnnotationOperations.VideoCall | ContactAnnotationOperations.Share; if (annotation.ProviderProperties.Count == 0) { annotation.ProviderProperties.Add("ContactPanelAppID", Package.Current.Id.FamilyName + "!App"); annotation.ProviderProperties.Add("ContactShareAppID", Package.Current.Id.FamilyName + "!App"); } await annotationList.TrySaveAnnotationAsync(annotation); } }
private async Task ExportAsync(ContactList contactList, ContactAnnotationList annotationList, TLContactsContactsBase result) { var contacts = result as TLContactsContacts; if (contacts != null) { foreach (var item in contacts.Users.OfType <TLUser>()) { var contact = await contactList.GetContactFromRemoteIdAsync("u" + item.Id); if (contact == null) { contact = new Contact(); } contact.FirstName = item.FirstName ?? string.Empty; contact.LastName = item.LastName ?? string.Empty; //contact.Nickname = item.Username ?? string.Empty; contact.RemoteId = "u" + item.Id; //contact.Id = item.Id.ToString(); var phone = contact.Phones.FirstOrDefault(); if (phone == null) { phone = new ContactPhone(); phone.Kind = ContactPhoneKind.Mobile; phone.Number = string.Format("+{0}", item.Phone); contact.Phones.Add(phone); } else { phone.Kind = ContactPhoneKind.Mobile; phone.Number = string.Format("+{0}", item.Phone); } await contactList.SaveContactAsync(contact); ContactAnnotation annotation; var annotations = await annotationList.FindAnnotationsByRemoteIdAsync(item.Id.ToString()); if (annotations.Count == 0) { annotation = new ContactAnnotation(); } else { annotation = annotations[0]; } annotation.ContactId = contact.Id; annotation.RemoteId = contact.RemoteId; annotation.SupportedOperations = ContactAnnotationOperations.ContactProfile | ContactAnnotationOperations.Message | ContactAnnotationOperations.AudioCall; if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 5)) { annotation.SupportedOperations |= ContactAnnotationOperations.Share; } if (annotation.ProviderProperties.Count == 0) { annotation.ProviderProperties.Add("ContactPanelAppID", Package.Current.Id.FamilyName + "!App"); annotation.ProviderProperties.Add("ContactShareAppID", Package.Current.Id.FamilyName + "!App"); } await annotationList.TrySaveAnnotationAsync(annotation); } } }
private async Task ExportAsync(ContactList contactList, ContactAnnotationList annotationList, Telegram.Td.Api.Users result) { if (result == null) { return; } foreach (var item in result.UserIds) { var user = _protoService.GetUser(item); var contact = await contactList.GetContactFromRemoteIdAsync("u" + user.Id); if (contact == null) { contact = new Contact(); } if (user.ProfilePhoto != null && user.ProfilePhoto.Small.Local.IsDownloadingCompleted) { contact.SourceDisplayPicture = await StorageFile.GetFileFromPathAsync(user.ProfilePhoto.Small.Local.Path); } contact.FirstName = user.FirstName ?? string.Empty; contact.LastName = user.LastName ?? string.Empty; //contact.Nickname = item.Username ?? string.Empty; contact.RemoteId = "u" + user.Id; //contact.Id = item.Id.ToString(); var phone = contact.Phones.FirstOrDefault(); if (phone == null) { phone = new ContactPhone(); phone.Kind = ContactPhoneKind.Mobile; phone.Number = string.Format("+{0}", user.PhoneNumber); contact.Phones.Add(phone); } else { phone.Kind = ContactPhoneKind.Mobile; phone.Number = string.Format("+{0}", user.PhoneNumber); } await contactList.SaveContactAsync(contact); ContactAnnotation annotation; var annotations = await annotationList.FindAnnotationsByRemoteIdAsync(user.Id.ToString()); if (annotations.Count == 0) { annotation = new ContactAnnotation(); } else { annotation = annotations[0]; } annotation.ContactId = contact.Id; annotation.RemoteId = contact.RemoteId; annotation.SupportedOperations = ContactAnnotationOperations.ContactProfile | ContactAnnotationOperations.Message | ContactAnnotationOperations.AudioCall; if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 5)) { annotation.SupportedOperations |= ContactAnnotationOperations.Share; } if (annotation.ProviderProperties.Count == 0) { annotation.ProviderProperties.Add("ContactPanelAppID", Package.Current.Id.FamilyName + "!App"); annotation.ProviderProperties.Add("ContactShareAppID", Package.Current.Id.FamilyName + "!App"); } var added = await annotationList.TrySaveAnnotationAsync(annotation); } }