Example #1
0
        public IAsyncOperation <LoadMoreItemsResult> LoadMoreItemsAsync(uint phase)
        {
            return(AsyncInfo.Run(async token =>
            {
                // If the query string is empty we want to load recent chats only
                var empty = string.IsNullOrEmpty(_query);
                if (empty && phase > 0 && (_internal == null || _internal.IsEmpty()))
                {
                    _filters.ReplaceWith(new[]
                    {
                        new SearchChatsFilterContent(new SearchMessagesFilterPhotoAndVideo()),
                        new SearchChatsFilterContent(new SearchMessagesFilterDocument()),
                        new SearchChatsFilterContent(new SearchMessagesFilterUrl()),
                        new SearchChatsFilterContent(new SearchMessagesFilterAudio()),
                        new SearchChatsFilterContent(new SearchMessagesFilterVoiceNote())
                    });

                    return new LoadMoreItemsResult();
                }

                bool IsFiltered(Chat chat)
                {
                    if (_type == SearchChatsType.Post && !_protoService.CanPostMessages(chat))
                    {
                        return true;
                    }
                    else if (_type == SearchChatsType.BasicAndSupergroups && chat.Type is not ChatTypeBasicGroup && chat.Type is not ChatTypeSupergroup)
                    {
                        return !_protoService.CanPostMessages(chat);
                    }
Example #2
0
        public IAsyncOperation <LoadMoreItemsResult> LoadMoreItemsAsync(uint phase)
        {
            return(AsyncInfo.Run(async token =>
            {
                // If the query string is empty we want to load recent chats only
                var empty = string.IsNullOrEmpty(_query);
                if (empty && phase > 0)
                {
                    return new LoadMoreItemsResult();
                }

                bool IsFiltered(Chat chat)
                {
                    if (_type == SearchChatsType.Post && !_protoService.CanPostMessages(chat))
                    {
                        return true;
                    }
                    else if (_type == SearchChatsType.BasicAndSupergroups && !(chat.Type is ChatTypeBasicGroup) && !(chat.Type is ChatTypeSupergroup))
                    {
                        return !_protoService.CanPostMessages(chat);
                    }
                    else if (_type == SearchChatsType.PrivateAndGroups)
                    {
                        return !(chat.Type is ChatTypePrivate || chat.Type is ChatTypeBasicGroup || chat.Type is ChatTypeSupergroup supergroup && !supergroup.IsChannel);
                    }
                    else if (_type == SearchChatsType.Private)
                    {
                        return !(chat.Type is ChatTypePrivate);
                    }

                    return false;
                }

                if (phase == 0)
                {
                    if (_type != SearchChatsType.BasicAndSupergroups && _query.Length > 0 && Strings.Resources.SavedMessages.StartsWith(_query, StringComparison.OrdinalIgnoreCase))
                    {
                        var savedMessages = await _protoService.SendAsync(new CreatePrivateChat(_protoService.Options.MyId, false));
                        if (savedMessages is Chat chat)
                        {
                            _chats.Add(chat.Id);
                            _local.Add(new SearchResult(chat, _query, false));
                        }
                    }

                    var response = await _protoService.SendAsync(new SearchChats(_query, 100));
                    if (response is Chats chats)
                    {
                        foreach (var id in chats.ChatIds)
                        {
                            var chat = _protoService.GetChat(id);
                            if (chat != null)
                            {
                                if (IsFiltered(chat))
                                {
                                    continue;
                                }

                                if (chat.Type is ChatTypePrivate privata)
                                {
                                    if (_users.Contains(privata.UserId))
                                    {
                                        continue;
                                    }

                                    _users.Add(privata.UserId);
                                }

                                _chats.Add(id);
                                _local.Add(new SearchResult(chat, _query, false));
                            }
                        }
                    }
                }
                else if (phase == 1 && _type != SearchChatsType.BasicAndSupergroups)
                {
                    var response = await _protoService.SendAsync(new SearchContacts(_query, 100));
                    if (response is Users users)
                    {
                        foreach (var id in users.UserIds)
                        {
                            if (_users.Contains(id))
                            {
                                continue;
                            }

                            var user = _protoService.GetUser(id);
                            if (user != null)
                            {
                                _users.Add(id);
                                _local.Add(new SearchResult(user, _query, false));
                            }
                        }
                    }
                }
                else if (phase == 2)
                {
                    var response = await _protoService.SendAsync(new SearchChatsOnServer(_query, 100));
                    if (response is Chats chats && _local != null)
                    {
                        foreach (var id in chats.ChatIds)
                        {
                            if (_chats.Contains(id))
                            {
                                continue;
                            }

                            var chat = _protoService.GetChat(id);
                            if (chat != null)
                            {
                                if (IsFiltered(chat))
                                {
                                    continue;
                                }

                                if (chat.Type is ChatTypePrivate privata && _users.Contains(privata.UserId))
                                {
                                    continue;
                                }

                                _local.Add(new SearchResult(chat, _query, false));
                            }
                        }
                    }
                }
                else if (phase == 3)
                {
                    var response = await _protoService.SendAsync(new SearchPublicChats(_query));
                    if (response is Chats chats)
                    {
                        foreach (var id in chats.ChatIds)
                        {
                            var chat = _protoService.GetChat(id);
                            if (chat != null)
                            {
                                if (IsFiltered(chat))
                                {
                                    continue;
                                }

                                _remote.Add(new SearchResult(chat, _query, true));
                            }
                        }
                    }
                }
                else if (phase == 4)
                {
                    var response = await _protoService.SendAsync(new SearchMessages(_chatList, _query, int.MaxValue, 0, 0, 100));
                    if (response is Messages messages)
                    {
                        foreach (var message in messages.MessagesValue)
                        {
                            _messages.Add(message);
                        }
                    }
                }

                return new LoadMoreItemsResult();
            }));
        }
Example #3
0
        public IAsyncOperation <LoadMoreItemsResult> LoadMoreItemsAsync(uint phase)
        {
            return(AsyncInfo.Run(async token =>
            {
                // If the query string is empty we want to load recent chats only
                var empty = string.IsNullOrEmpty(_query);
                if (empty && phase > 0 && (_internal == null || _internal.IsEmpty()))
                {
                    _filters.ReplaceWith(new[]
                    {
                        new SearchChatsFilterContent(new SearchMessagesFilterPhotoAndVideo()),
                        new SearchChatsFilterContent(new SearchMessagesFilterDocument()),
                        new SearchChatsFilterContent(new SearchMessagesFilterUrl()),
                        new SearchChatsFilterContent(new SearchMessagesFilterAudio()),
                        new SearchChatsFilterContent(new SearchMessagesFilterVoiceNote())
                    });

                    return new LoadMoreItemsResult();
                }

                bool IsFiltered(Chat chat)
                {
                    if (_type == SearchChatsType.Post && !_protoService.CanPostMessages(chat))
                    {
                        return true;
                    }
                    else if (_type == SearchChatsType.BasicAndSupergroups && !(chat.Type is ChatTypeBasicGroup) && !(chat.Type is ChatTypeSupergroup))
                    {
                        return !_protoService.CanPostMessages(chat);
                    }
                    else if (_type == SearchChatsType.PrivateAndGroups)
                    {
                        return !(chat.Type is ChatTypePrivate || chat.Type is ChatTypeBasicGroup || chat.Type is ChatTypeSupergroup supergroup && !supergroup.IsChannel);
                    }
                    else if (_type == SearchChatsType.Private)
                    {
                        return !(chat.Type is ChatTypePrivate);
                    }

                    return false;
                }

                var hasRanges = _internal?.Any(x => x is SearchChatsFilterDateRange) ?? false;
                var hasContent = _internal?.Any(x => x is SearchChatsFilterContent) ?? false;
                var hasChat = _internal?.Any(x => x is SearchChatsFilterChat) ?? false;

                if (phase == 0 && (!hasChat || (hasContent && !hasChat && !empty)))
                {
                    if (_type != SearchChatsType.BasicAndSupergroups && _query.Length > 0 && Strings.Resources.SavedMessages.StartsWith(_query, StringComparison.OrdinalIgnoreCase))
                    {
                        var savedMessages = await _protoService.SendAsync(new CreatePrivateChat(_protoService.Options.MyId, false));
                        if (savedMessages is Chat chat)
                        {
                            _chats.Add(chat.Id);

                            if (!hasContent)
                            {
                                _local.Add(new SearchResult(chat, _query, false));
                            }

                            _filters.Add(new SearchChatsFilterChat(_protoService, chat));
                        }
                    }

                    var response = await _protoService.SendAsync(new SearchChats(_query, 100));
                    if (response is Chats chats)
                    {
                        foreach (var id in chats.ChatIds)
                        {
                            var chat = _protoService.GetChat(id);
                            if (chat != null)
                            {
                                if (IsFiltered(chat))
                                {
                                    continue;
                                }

                                if (chat.Type is ChatTypePrivate privata)
                                {
                                    if (_users.Contains(privata.UserId))
                                    {
                                        continue;
                                    }

                                    _users.Add(privata.UserId);
                                }

                                _chats.Add(id);

                                if (!hasContent)
                                {
                                    _local.Add(new SearchResult(chat, _query, false));
                                }

                                _filters.Add(new SearchChatsFilterChat(_protoService, chat));
                            }
                        }
                    }
                }
                else if (phase == 1 && !hasChat && !hasContent && _type != SearchChatsType.BasicAndSupergroups)
                {
                    var response = await _protoService.SendAsync(new SearchContacts(_query, 100));
                    if (response is Users users)
                    {
                        foreach (var id in users.UserIds)
                        {
                            if (_users.Contains(id))
                            {
                                continue;
                            }

                            var user = _protoService.GetUser(id);
                            if (user != null)
                            {
                                _users.Add(id);
                                _local.Add(new SearchResult(user, _query, false));
                            }
                        }
                    }
                }
                else if (phase == 2 && (!hasChat || (hasContent && !hasChat && !empty)))
                {
                    var response = await _protoService.SendAsync(new SearchChatsOnServer(_query, 100));
                    if (response is Chats chats && _local != null)
                    {
                        foreach (var id in chats.ChatIds)
                        {
                            if (_chats.Contains(id))
                            {
                                continue;
                            }

                            var chat = _protoService.GetChat(id);
                            if (chat != null)
                            {
                                if (IsFiltered(chat))
                                {
                                    continue;
                                }

                                if (chat.Type is ChatTypePrivate privata && _users.Contains(privata.UserId))
                                {
                                    continue;
                                }


                                if (!hasContent)
                                {
                                    _local.Add(new SearchResult(chat, _query, false));
                                }

                                _filters.Add(new SearchChatsFilterChat(_protoService, chat));
                            }
                        }
                    }
                }
                else if (phase == 3 && !hasChat && !hasContent)
                {
                    var response = await _protoService.SendAsync(new SearchPublicChats(_query));
                    if (response is Chats chats)
                    {
                        foreach (var id in chats.ChatIds)
                        {
                            var chat = _protoService.GetChat(id);
                            if (chat != null)
                            {
                                if (IsFiltered(chat))
                                {
                                    continue;
                                }

                                _remote.Add(new SearchResult(chat, _query, true));
                            }
                        }
                    }
                }
                else if (phase == 4)
                {
                    hasRanges = LoadDateRanges(_query);

                    if (!hasContent && !hasRanges && _filters.IsEmpty())
                    {
                        _filters.ReplaceWith(new[]
                        {
                            new SearchChatsFilterContent(new SearchMessagesFilterPhotoAndVideo()),
                            new SearchChatsFilterContent(new SearchMessagesFilterDocument()),
                            new SearchChatsFilterContent(new SearchMessagesFilterUrl()),
                            new SearchChatsFilterContent(new SearchMessagesFilterAudio()),
                            new SearchChatsFilterContent(new SearchMessagesFilterVoiceNote())
                        });
                    }

                    var content = _internal.FirstOrDefault(x => x is SearchChatsFilterContent) as SearchChatsFilterContent;
                    var range = _internal.FirstOrDefault(x => x is SearchChatsFilterDateRange) as SearchChatsFilterDateRange;
                    var chat = _internal.FirstOrDefault(x => x is SearchChatsFilterChat) as SearchChatsFilterChat;

                    var minDate = range?.StartDate ?? 0;
                    var maxDate = range?.EndDate ?? 0;

                    Function function;
                    if (chat != null)
                    {
                        function = new SearchChatMessages(chat.Id, _query, null, 0, 0, 100, content?.Filter, 0);
                    }
                    else
                    {
                        function = new SearchMessages(_chatList, _query, int.MaxValue, 0, 0, 100, content?.Filter, minDate, maxDate);
                    }

                    var response = await _protoService.SendAsync(function);
                    if (response is Messages messages)
                    {
                        foreach (var message in messages.MessagesValue)
                        {
                            _messages.Add(message);
                        }
                    }
                }

                return new LoadMoreItemsResult();
            }));
        }