Example #1
0
        /// <summary>
        /// Add contact by Discord friend object
        /// </summary>
        /// <param name="user">Discord User</param>
        public async void AddContact(Friend user)
        {
            // Create contact
            Contact contact = new Contact();

            contact.FirstName            = user.user.Username;
            contact.SourceDisplayPicture = RandomAccessStreamReference.CreateFromUri(Common.AvatarUri(user.user.Avatar, user.Id));

            // Save contact
            if (contactList == null)
            {
                return;
            }
            await contactList.SaveContactAsync(contact);

            ContactAnnotationList annotationList = await GetContactAnnotationList();

            if (annotationList == null)
            {
                return;
            }

            // Creeate annotations of contact
            ContactAnnotation annotation = new ContactAnnotation();

            annotation.ContactId           = contact.Id;
            annotation.RemoteId            = user.Id;
            annotation.SupportedOperations = ContactAnnotationOperations.ContactProfile | ContactAnnotationOperations.Message | ContactAnnotationOperations.Share;

            // Save annotations on contact
            if (!await annotationList.TrySaveAnnotationAsync(annotation))
            {
                return;
            }
        }
Example #2
0
        private async Task <ContactAnnotationList> GetAnnotationListAsync(UserDataAccount userDataAccount)
        {
            try
            {
                var store = await ContactManager.RequestAnnotationStoreAsync(ContactAnnotationStoreAccessType.AppAnnotationsReadWrite);

                if (store == null)
                {
                    return(null);
                }

                ContactAnnotationList contactList = null;
                if (_cacheService.Options.TryGetValue("x_annotation_list", out string id))
                {
                    contactList = await store.GetAnnotationListAsync(id);
                }

                if (contactList == null)
                {
                    contactList = await store.CreateAnnotationListAsync(userDataAccount.Id);

                    await _protoService.SendAsync(new Telegram.Td.Api.SetOption("x_annotation_list", new Telegram.Td.Api.OptionValueString(contactList.Id)));
                }

                return(contactList);
            }
            catch
            {
                return(null);
            }
        }
Example #3
0
        /// <summary>
        /// Load ContactAnnotation List
        /// </summary>
        /// <returns></returns>
        private async Task <ContactAnnotationList> GetContactAnnotationList()
        {
            // If annotation list is already determined, return it
            if (annotationList == null)
            {
                // Initialize annotations contact store
                annotationStore = await Windows.ApplicationModel.Contacts.ContactManager.RequestAnnotationStoreAsync(ContactAnnotationStoreAccessType.AppAnnotationsReadWrite);

                if (annotationStore == null)
                {
                    // Unavailable, call it quits
                    return(null);
                }

                // Get annotations list
                IReadOnlyList <ContactAnnotationList> annotationLists = await annotationStore.FindAnnotationListsAsync();

                if (annotationLists.Count == 0)
                {
                    annotationList = await annotationStore.CreateAnnotationListAsync();
                }
                else
                {
                    annotationList = annotationLists[0];
                }
            }

            return(annotationList);
        }
Example #4
0
        /// <summary>
        /// Handler for the user clicking the Login button
        /// </summary>
        public async void LogIn_Button_Click(object sender, RoutedEventArgs e)
        {
            // Authenticate the user with the service.
            // This app does not have a web service, so there is no authentication
            // that needs to happen.

            // Get the ContactList and ContactAnnotationList
            ContactList contactList = await _GetContactListAsync();

            ContactAnnotationList annotationList = await _GetContactAnnotationListAsync();

            // After the user was authenticated, create a Me contact in the contact
            // list for this app.
            await _CreateMeContactAsync(contactList, annotationList);

            // Provision the app for social feed integration
            await _SocialInfoProvisionAsync();

            // At this point, the user was logged in and the contact was created.
            // Mark the user as being logged in for future launches and for the
            // background agent.
            ApplicationSettings.SaveIsUserLoggedIn(true);

            // Update the state of the buttons
            LogInButton.IsEnabled  = false;
            LogOutButton.IsEnabled = !LogInButton.IsEnabled;

            // Sync the contacts
            await _SyncContactsAsync(contactList, annotationList);
        }
        private async Task AnnotateContactAsync(Contact contact)
        {
            // Annotate this contact with a remote ID, which you can then retrieve when the Contact Panel is activated.
            ContactAnnotation contactAnnotation = new ContactAnnotation
            {
                ContactId           = contact.Id,
                RemoteId            = Constants.ContactRemoteId,
                SupportedOperations = ContactAnnotationOperations.ContactProfile | ContactAnnotationOperations.Message
            };

            // Annotate that this contact can load this app's Contact Panel.
            var infos = await Windows.System.AppDiagnosticInfo.RequestInfoForAppAsync();

            contactAnnotation.ProviderProperties.Add("ContactPanelAppID", infos[0].AppInfo.AppUserModelId);

            var contactAnnotationStore = await ContactManager.RequestAnnotationStoreAsync(ContactAnnotationStoreAccessType.AppAnnotationsReadWrite);

            var contactAnnotationLists = await contactAnnotationStore.FindAnnotationListsAsync();

            ContactAnnotationList contactAnnotationList = contactAnnotationLists.Count > 0 ? contactAnnotationLists[0] : null;

            if (contactAnnotationList == null)
            {
                contactAnnotationList = await contactAnnotationStore.CreateAnnotationListAsync();
            }


            await contactAnnotationList.TrySaveAnnotationAsync(contactAnnotation);
        }
Example #6
0
        private async Task InitializeAnnotationListAsync()
        {
            if (ContactAnnotationStore == null)
            {
                throw new Exception("Unable to get contact annotation store");
            }

            var contactAnnotationLists = await ContactAnnotationStore.FindAnnotationListsAsync();

            _contactAnnotationList = (from c in contactAnnotationLists select c).SingleOrDefault();

            if (_contactAnnotationList == null)
            {
                _contactAnnotationList = await ContactAnnotationStore.CreateAnnotationListAsync();
            }
        }
Example #7
0
        /// <summary>
        /// Sync the contacts for the user
        /// </summary>
        /// <param name="contactList">The ContactList for this app</param>
        /// <param name="annotationList">The ContactAnnotationList</param>
        private async Task _SyncContactsAsync(
            ContactList contactList,
            ContactAnnotationList annotationList)
        {
            // This app will read the contacts from a file and do some at this time.
            // In the case of a web service, this should be done on a background task.

            // Get the XML file that contains that user information
            StorageFile file =
                await Package.Current.InstalledLocation.TryGetItemAsync("Assets\\Contacts.xml")
                as StorageFile;

            if (file == null)
            {
                return;
            }

            // Parse the XML file and create all of the contacts
            XDocument doc      = XDocument.Load(file.Path);
            var       contacts = doc.Descendants("Contact");

            foreach (XElement contact in contacts)
            {
                Contact currentContact = new Contact();
                currentContact.FirstName = contact.Attribute("FirstName").Value;
                currentContact.LastName  = contact.Attribute("LastName").Value;
                currentContact.Emails.Add(new ContactEmail()
                {
                    Address = contact.Attribute("Email").Value,
                    Kind    = ContactEmailKind.Personal
                });
                currentContact.RemoteId = contact.Attribute("Id").Value;

                await contactList.SaveContactAsync(currentContact);

                ContactAnnotation annotation = new ContactAnnotation();
                annotation.ContactId           = currentContact.Id;
                annotation.RemoteId            = currentContact.RemoteId;
                annotation.SupportedOperations =
                    ContactAnnotationOperations.SocialFeeds;

                await annotationList.TrySaveAnnotationAsync(annotation);
            }
        }
Example #8
0
        /// <summary>
        /// Creates the Me contact
        /// </summary>
        /// <param name="contactList">The ContactList for this app</param>
        /// <param name="annotationList">The ContactAnnotationList</param>
        private async Task _CreateMeContactAsync(
            ContactList contactList,
            ContactAnnotationList annotationList)
        {
            // All of the contact information will come from the web service.
            // This app will use some default values.
            Contact meContact = await contactList.GetMeContactAsync();

            meContact.FirstName = "Eleanor";
            meContact.LastName  = "Taylor";

            meContact.Emails.Add(new ContactEmail()
            {
                Address = "*****@*****.**",
                Kind    = ContactEmailKind.Personal
            });

            // The RemoteId is the ID used by the web service to identify the user.
            meContact.RemoteId = "Eleanor_Taylor";
            await contactList.SaveContactAsync(meContact);

            // Set the annotations for the me contact.
            // By setting the SocialFeeds annotation, the People app will be able to
            // show social feeds for that contact.
            ContactAnnotation annotation = new ContactAnnotation();

            annotation.ContactId           = meContact.Id;
            annotation.RemoteId            = meContact.RemoteId;
            annotation.SupportedOperations = ContactAnnotationOperations.SocialFeeds;

            bool saveSuccessful = await annotationList.TrySaveAnnotationAsync(annotation);

            if (!saveSuccessful)
            {
                throw new Exception("Could not save the annotations for the me contact.");
            }
        }
        private async void CreateTestContacts()
        {
            //
            // Creating two test contacts with email address and phone number.
            //

            Contact contact1 = new Contact();

            contact1.FirstName = "TestContact1";

            ContactEmail email1 = new ContactEmail();

            email1.Address = "*****@*****.**";
            contact1.Emails.Add(email1);

            ContactPhone phone1 = new ContactPhone();

            phone1.Number = "4255550100";
            contact1.Phones.Add(phone1);

            Contact contact2 = new Contact();

            contact2.FirstName = "TestContact2";

            ContactEmail email2 = new ContactEmail();

            email2.Address = "*****@*****.**";
            email2.Kind    = ContactEmailKind.Other;
            contact2.Emails.Add(email2);

            ContactPhone phone2 = new ContactPhone();

            phone2.Number = "4255550101";
            phone2.Kind   = ContactPhoneKind.Mobile;
            contact2.Phones.Add(phone2);

            // Save the contacts
            ContactList contactList = await _GetContactList();

            if (null == contactList)
            {
                return;
            }

            await contactList.SaveContactAsync(contact1);

            await contactList.SaveContactAsync(contact2);

            //
            // Create annotations for those test contacts.
            // Annotation is the contact meta data that allows People App to generate deep links
            // in the contact card that takes the user back into this app.
            //

            ContactAnnotationList annotationList = await _GetContactAnnotationList();

            if (null == annotationList)
            {
                return;
            }

            ContactAnnotation annotation = new ContactAnnotation();

            annotation.ContactId = contact1.Id;

            // Remote ID: The identifier of the user relevant for this app. When this app is
            // launched into from the People App, this id will be provided as context on which user
            // the operation (e.g. ContactProfile) is for.
            annotation.RemoteId = "user12";

            // The supported operations flags indicate that this app can fulfill these operations
            // for this contact. These flags are read by apps such as the People App to create deep
            // links back into this app. This app must also be registered for the relevant
            // protocols in the Package.appxmanifest (in this case, ms-contact-profile).
            annotation.SupportedOperations = ContactAnnotationOperations.ContactProfile;

            if (!await annotationList.TrySaveAnnotationAsync(annotation))
            {
                rootPage.NotifyUser("Failed to save annotation for TestContact1 to the store.", NotifyType.ErrorMessage);
                return;
            }

            annotation           = new ContactAnnotation();
            annotation.ContactId = contact2.Id;
            annotation.RemoteId  = "user22";

            // You can also specify multiple supported operations for a contact in a single
            // annotation. In this case, this annotation indicates that the user can be
            // communicated via VOIP call, Video Call, or IM via this application.
            annotation.SupportedOperations = ContactAnnotationOperations.Message |
                                             ContactAnnotationOperations.AudioCall |
                                             ContactAnnotationOperations.VideoCall;

            if (!await annotationList.TrySaveAnnotationAsync(annotation))
            {
                rootPage.NotifyUser("Failed to save annotation for TestContact2 to the store.", NotifyType.ErrorMessage);
                return;
            }

            rootPage.NotifyUser("Sample data created successfully.", NotifyType.StatusMessage);
        }
        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);
        }
Example #11
0
        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);
                }
            }
        }
Example #12
0
        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);
            }
        }
Example #13
0
        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);
            }
        }