/// <summary> /// Check if contact exists /// </summary> /// <param name="user">User to check</param> /// <returns>True if the user does not exist</returns> private async Task <bool> CheckContact(SharedModels.User user) { // Intialize store if not done if (store == null) { store = await Windows.ApplicationModel.Contacts.ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite); } if (store == null) { // Unavailable, call it quits return(true); } ContactList contactList; // Get contact lists for Discord IReadOnlyList <ContactList> contactLists = await store.FindContactListsAsync(); if (contactLists.Count == 0) { contactList = await store.CreateContactListAsync("Discord"); } else { contactList = contactLists[0]; } // Get user var returnval = await contactList.GetContactFromRemoteIdAsync(user.Id); // If user is null, return true return(returnval != null); }
/// <summary> /// Add contact /// </summary> /// <param name="user">Discord User</param> public async Task AddContact(SharedModels.User user) { if (!await CheckContact(user)) { // Create contact Contact contact = new Contact(); contact.Name = user.Username + "#" + user.Discriminator; contact.RemoteId = user.Id; contact.SourceDisplayPicture = RandomAccessStreamReference.CreateFromUri(Common.AvatarUri(user.Avatar, user.Id)); // Save the contacts ContactList contactList = await GetContactList(); if (null == contactList) { return; } try { await contactList.SaveContactAsync(contact); } catch { // :shrug: } if (annotationList == null) { return; } // Create annotations for contact ContactAnnotation annotation = new ContactAnnotation(); annotation.RemoteId = user.Id; annotation.ContactId = contact.Id; annotation.SupportedOperations = ContactAnnotationOperations.ContactProfile | ContactAnnotationOperations.Message | ContactAnnotationOperations.Share; annotation.ProviderProperties.Add("ContactPanelAppID", Windows.ApplicationModel.Package.Current.Id.FamilyName + "!App"); // Save annotations on contact if (!await annotationList.TrySaveAnnotationAsync(annotation)) { Debug.WriteLine("Failed to save contact " + user.Username); } } }