Esempio n. 1
0
        private void RemoveRelationship(DiscordRelationship rel, bool skipAll = false)
        {
            if (!skipAll)
            {
                if (_allList.Remove(rel))
                {
                    _syncContext.Post(a => All.Remove(rel), null);
                }
            }

            if (_onlineList.Remove(rel))
            {
                _syncContext.Post(a => Online.Remove(rel), null);
            }

            if (Pending.Contains(rel)) // is this actually faster?
            {
                _syncContext.Post(a => Pending.Remove(rel), null);
            }

            if (Blocked.Contains(rel)) // ^^
            {
                _syncContext.Post(a => Blocked.Remove(rel), null);
            }
        }
Esempio n. 2
0
        private void SortRelationship(DiscordRelationship rel, bool skipAll = false)
        {
            RemoveRelationship(rel, skipAll);

            switch (rel.RelationshipType)
            {
            case DiscordRelationshipType.Friend:
                int i;

                if (!skipAll)
                {
                    i = _allList.BinarySearch(rel);
                    if (i < 0)
                    {
                        i = ~i;
                    }

                    _allList.Insert(i, rel);
                    _syncContext.Post(a => All.Insert(i, rel), null);
                }

                if (rel.User.Presence != null && rel.User.Presence.Status != UserStatus.Offline)
                {
                    i = _onlineList.BinarySearch(rel);
                    if (i < 0)
                    {
                        i = ~i;
                    }

                    _onlineList.Insert(i, rel);
                    _syncContext.Post(a => Online.Insert(i, rel), null);
                }
                break;

            case DiscordRelationshipType.Blocked:
                _syncContext.Post(a => Blocked.Add(rel), null);
                break;

            case DiscordRelationshipType.IncomingRequest:
            case DiscordRelationshipType.OutgoingRequest:
                _syncContext.Post(a => Pending.Add(rel), null);
                break;

            default:
                break;
            }
        }
        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);
        }
Esempio n. 4
0
 public RelationshipEventArgs(DiscordRelationship relationship)
 {
     Relationship = relationship;
 }
Esempio n. 5
0
 public FriendInfo(DiscordRelationship relationship)
 {
     At  = relationship.User.ToString();
     _id = relationship.User.Id.ToString();
 }
Esempio n. 6
0
        public static async Task <Contact> AddOrUpdateContactForRelationship(ContactList list, DiscordRelationship relationship, StorageFolder folder = null)
        {
            Contact contact = null;

            if (folder == null)
            {
                folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("AvatarCache", CreationCollisionOption.OpenIfExists);
            }

            if ((contact = await list.GetContactFromRemoteIdAsync($"{REMOTE_ID_PREFIX}{relationship.User.Id}")) == null)
            {
                var reference = await GetAvatarReferenceAsync(relationship.User, folder);

                contact = new Contact
                {
                    DisplayNameOverride  = relationship.User.Username,
                    SourceDisplayPicture = reference,
                    RemoteId             = REMOTE_ID_PREFIX + relationship.User.Id.ToString()
                };

                contact.ProviderProperties["AvatarHash"] = relationship.User.AvatarHash;

                await list.SaveContactAsync(contact);
            }
            else
            {
                if (contact.ProviderProperties.TryGetValue("AvatarHash", out var obj) && obj is string str)
                {
                    if (relationship.User.AvatarHash != str)
                    {
                        var reference = await GetAvatarReferenceAsync(relationship.User, folder);

                        contact.SourceDisplayPicture             = reference;
                        contact.ProviderProperties["AvatarHash"] = relationship.User.AvatarHash;

                        await list.SaveContactAsync(contact);
                    }
                }
            }

            return(contact);
        }