Пример #1
0
        Task INewMessage.GetContacts(string query, bool searchForParties, Action <List <Contact> > result)
        {
            Contract.Ensures(Contract.Result <Task>() != null);
            return(Task.Factory.StartNew(async() =>
            {
                if (!searchForParties)
                {
                    var users = await FetchContacts();
                    var contacts = users.Select(x => CreateTelegramContact(x))
                                   .Where(x => !string.IsNullOrWhiteSpace(x.FirstName)).OfType <Contact>().OrderBy(x => x.FirstName).ToList();

                    if (string.IsNullOrWhiteSpace(query))
                    {
                        result(contacts);
                    }
                    else
                    {
                        if (query.Length >= 5)
                        {
                            var localContacts = contacts.FindAll(x => Utils.Search(x.FullName, query));
                            using (var client = new FullClientDisposable(this))
                            {
                                var searchResult =
                                    TelegramUtils.RunSynchronously(
                                        client.Client.Methods.ContactsSearchAsync(new ContactsSearchArgs
                                {
                                    Q = query,
                                    Limit = 50         //like the official client
                                }));
                                var contactsFound = searchResult as ContactsFound;
                                var globalContacts = GetGlobalContacts(contactsFound);
                                localContacts.AddRange(globalContacts);
                            }
                            result(localContacts);
                        }
                        else
                        {
                            result(contacts.FindAll(x => Utils.Search(x.FullName, query)));
                        }
                    }
                }
                else
                {
                    var partyContacts = new List <Contact>();

                    // Only grab disa solo, party and super groups.
                    // Important: Don't get confused between disa channels and telegram channels.
                    //            Telegram channels include both super groups and channels, differentiated
                    //            by the telegram Channel.Broadcast and Channel.Megagroup fields.

                    foreach (var chat in _dialogs.GetAllChats())
                    {
                        var name = TelegramUtils.GetChatTitle(chat);
                        var upgraded = TelegramUtils.GetChatUpgraded(chat);
                        if (upgraded)
                        {
                            continue;
                        }
                        var left = TelegramUtils.GetChatLeft(chat);
                        if (left)
                        {
                            continue;
                        }
                        var kicked = TelegramUtils.GetChatKicked(chat);
                        if (kicked)
                        {
                            continue;
                        }
                        var isChannel = chat is Channel;
                        if (isChannel)
                        {
                            var channel = chat as Channel;
                            if (channel.Megagroup == null)
                            {
                                continue;
                            }
                        }
                        partyContacts.Add(new TelegramPartyContact
                        {
                            FirstName = name,
                            Ids = new List <Contact.ID>
                            {
                                new Contact.PartyID
                                {
                                    Service = this,
                                    Id = TelegramUtils.GetChatId(chat),
                                    ExtendedParty = isChannel
                                }
                            },
                        });
                    }

                    if (string.IsNullOrWhiteSpace(query))
                    {
                        result(partyContacts.OrderBy(c => c.FirstName).ToList());
                    }
                    else
                    {
                        if (query.Length >= 5)
                        {
                            var localContacts = partyContacts.FindAll(x => Utils.Search(x.FirstName, query));
                            using (var client = new FullClientDisposable(this))
                            {
                                var searchResult =
                                    TelegramUtils.RunSynchronously(
                                        client.Client.Methods.ContactsSearchAsync(new ContactsSearchArgs
                                {
                                    Q = query,
                                    Limit = 50         //like the official client
                                }));
                                var contactsFound = searchResult as ContactsFound;
                                var globalContacts = GetGlobalPartyContacts(contactsFound: contactsFound, forChannels: false);
                                localContacts.AddRange(globalContacts);
                            }
                            result(localContacts.OrderBy(c => c.FirstName).ToList());
                        }
                        else
                        {
                            var searchResult = partyContacts.FindAll(x => Utils.Search(x.FirstName, query));
                            result(searchResult.OrderBy(c => c.FirstName).ToList());
                        }
                    }
                }
            }));
        }
Пример #2
0
        Task INewMessage.GetContacts(string query, bool searchForParties, Action <List <Contact> > result)
        {
            Contract.Ensures(Contract.Result <Task>() != null);
            return(Task.Factory.StartNew(async() =>
            {
                if (!searchForParties)
                {
                    var users = await FetchContacts();
                    var contacts = users.Select(x =>
                                                new TelegramContact
                    {
                        Available = TelegramUtils.GetAvailable(x),
                        LastSeen = TelegramUtils.GetLastSeenTime(x),
                        FirstName = x.FirstName,
                        LastName = x.LastName,
                        User = x,
                        Ids = new List <Contact.ID>
                        {
                            new Contact.ID
                            {
                                Service = this,
                                Id = x.Id.ToString(CultureInfo.InvariantCulture)
                            }
                        },
                    }).OfType <Contact>().OrderBy(x => x.FirstName).ToList();
                    if (string.IsNullOrWhiteSpace(query))
                    {
                        result(contacts);
                    }
                    else
                    {
                        if (query.Length >= 5)
                        {
                            var localContacts = contacts.FindAll(x => Utils.Search(x.FullName, query));
                            using (var client = new FullClientDisposable(this))
                            {
                                var searchResult =
                                    TelegramUtils.RunSynchronously(
                                        client.Client.Methods.ContactsSearchAsync(new ContactsSearchArgs
                                {
                                    Q = query,
                                    Limit = 50         //like the official client
                                }));
                                var contactsFound = searchResult as ContactsFound;
                                var globalContacts = GetGlobalContacts(contactsFound);
                                localContacts.AddRange(globalContacts);
                            }
                            result(localContacts);
                        }
                        else
                        {
                            result(contacts.FindAll(x => Utils.Search(x.FullName, query)));
                        }
                    }
                }
                else
                {
                    var partyContacts = new List <Contact>();
                    foreach (var chat in _dialogs.GetAllChats())
                    {
                        var name = TelegramUtils.GetChatTitle(chat);
                        var upgraded = TelegramUtils.GetChatUpgraded(chat);
                        if (upgraded)
                        {
                            continue;
                        }
                        var left = TelegramUtils.GetChatLeft(chat);
                        if (left)
                        {
                            continue;
                        }
                        var kicked = TelegramUtils.GetChatKicked(chat);
                        if (kicked)
                        {
                            continue;
                        }
                        partyContacts.Add(new TelegramPartyContact
                        {
                            FirstName = name,
                            Ids = new List <Contact.ID>
                            {
                                new Contact.PartyID
                                {
                                    Service = this,
                                    Id = TelegramUtils.GetChatId(chat),
                                    ExtendedParty = chat is Channel
                                }
                            },
                        });
                    }

                    //partyContacts.Sort((x, y) => x.FullName.CompareTo(y.FullName));


                    if (string.IsNullOrWhiteSpace(query))
                    {
                        result(partyContacts);
                    }
                    else
                    {
                        if (query.Length >= 5)
                        {
                            var localContacts = partyContacts.FindAll(x => Utils.Search(x.FirstName, query));
                            using (var client = new FullClientDisposable(this))
                            {
                                var searchResult =
                                    TelegramUtils.RunSynchronously(
                                        client.Client.Methods.ContactsSearchAsync(new ContactsSearchArgs
                                {
                                    Q = query,
                                    Limit = 50         //like the official client
                                }));
                                var contactsFound = searchResult as ContactsFound;
                                var globalContacts = GetGlobalPartyContacts(contactsFound);
                                localContacts.AddRange(globalContacts);
                            }
                            result(localContacts);
                        }
                        else
                        {
                            var searchResult = partyContacts.FindAll(x => Utils.Search(x.FirstName, query));
                            result(searchResult);
                        }
                    }
                }
            }));
        }
Пример #3
0
        public Task FetchChannelBubbleGroup(Contact.ID[] contactIds, Action <BubbleGroup> result)
        {
            return(Task.Factory.StartNew(() =>
            {
                // Sanity check, we are currently only processing collections passed in with a cardinality of 1
                if (contactIds.Length != 1)
                {
                    result(null);
                    return;
                }

                foreach (var chat in _dialogs.GetAllChats())
                {
                    var name = TelegramUtils.GetChatTitle(chat);
                    var upgraded = TelegramUtils.GetChatUpgraded(chat);
                    if (upgraded)
                    {
                        continue;
                    }
                    var left = TelegramUtils.GetChatLeft(chat);
                    if (left)
                    {
                        continue;
                    }
                    var kicked = TelegramUtils.GetChatKicked(chat);
                    if (kicked)
                    {
                        continue;
                    }
                    var isChannel = chat is Channel;
                    if (isChannel)
                    {
                        var channel = chat as Channel;
                        if (channel.Broadcast == null)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }

                    var channelAddress = TelegramUtils.GetChatId(chat);
                    if (BubbleGroupComparer(contactIds[0].Id, channelAddress))
                    {
                        var newBubble = new NewBubble(
                            time: Time.GetNowUnixTimestamp(),
                            direction: Bubble.BubbleDirection.Outgoing,
                            address: channelAddress,
                            participantAddress: null,
                            party: true,
                            service: this);
                        newBubble.ExtendedParty = true;

                        var newGroup = BubbleGroupFactory.AddNewIfNotExist(newBubble);
                        if (newGroup == null)
                        {
                            newGroup = BubbleGroupManager.FindWithAddress(this, channelAddress);
                        }

                        result(newGroup);

                        return;
                    }
                }

                // OK, we didn't get a channel locally, we must be trying to join a public channel
                using (var client = new FullClientDisposable(this))
                {
                    // A NewChannel flow will stuff away a Contact in the ContactId.Tag field
                    var telegramPartyContact = contactIds[0].Tag as TelegramPartyContact;
                    if (telegramPartyContact == null)
                    {
                        result(null);
                        return;
                    }

                    // Go for the public channel join
                    var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsJoinChannelAsync(new ChannelsJoinChannelArgs
                    {
                        Channel = new InputChannel
                        {
                            ChannelId = uint.Parse(contactIds[0].Id),
                            AccessHash = telegramPartyContact.AccessHash
                        }
                    }));

                    // Process the result and add in the new public channel to our local set of groups
                    var updates = response as Updates;
                    if (updates != null)
                    {
                        SendToResponseDispatcher(updates, client.Client);
                        _dialogs.AddChats(updates.Chats);
                        var chat = TelegramUtils.GetChatFromUpdate(updates);

                        var channelAddress = TelegramUtils.GetChatId(chat);

                        var newBubble = new NewBubble(
                            time: Time.GetNowUnixTimestamp(),
                            direction: Bubble.BubbleDirection.Outgoing,
                            address: channelAddress,
                            participantAddress: null,
                            party: true,
                            service: this);
                        newBubble.ExtendedParty = true;

                        var newGroup = BubbleGroupFactory.AddNew(newBubble);

                        result(newGroup);
                    }
                    else
                    {
                        result(null);
                    }
                }
            }));
        }