private List <Contact> GetGlobalPartyContacts(ContactsFound contactsFound)
        {
            var globalContacts = new List <Contact>();

            _dialogs.AddChats(contactsFound.Chats);
            Utils.DebugPrint("Chats found " + ObjectDumper.Dump(contactsFound.Chats));
            foreach (var chat in contactsFound.Chats)
            {
                var name   = TelegramUtils.GetChatTitle(chat);
                var kicked = TelegramUtils.GetChatKicked(chat);
                if (kicked)
                {
                    continue;
                }
                globalContacts.Add(new TelegramPartyContact
                {
                    FirstName = name,
                    Ids       = new List <Contact.ID>
                    {
                        new Contact.PartyID
                        {
                            Service       = this,
                            Id            = TelegramUtils.GetChatId(chat),
                            ExtendedParty = chat is Channel
                        }
                    },
                });
            }
            return(globalContacts);
        }
Exemple #2
0
 public Task GetPartyName(BubbleGroup group, Action <string> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         result(TelegramUtils.GetChatTitle(_dialogs.GetChat(uint.Parse(group.Address))));
     }));
 }
 private void SetResultAsChat(IChat chat, string id, Action<Tuple<Contact, Contact.ID>> result)
 {
     var contact = new TelegramContact
     {
         FirstName = TelegramUtils.GetChatTitle(chat)
     };
     var contactId = new Contact.PartyID
     {
         ExtendedParty = chat is Channel,
         Id = id,
         Service = this
     };
     result(new Tuple<Contact, Contact.ID>(contact, contactId));
 }
        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);
                        }
                    }
                }
            }));
        }
        private List <Contact> GetGlobalPartyContacts(ContactsFound contactsFound, bool forChannels)
        {
            var globalContacts = new List <Contact>();

            _dialogs.AddChats(contactsFound.Chats);
            foreach (var chat in contactsFound.Chats)
            {
                var name   = TelegramUtils.GetChatTitle(chat);
                var kicked = TelegramUtils.GetChatKicked(chat);
                if (kicked)
                {
                    continue;
                }
                var channel = chat as Channel;

                if (forChannels)
                {
                    if (channel != null)
                    {
                        if (channel.Broadcast != null)
                        {
                            // it is a channel
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    if (channel != null)
                    {
                        if (channel.Broadcast != null)
                        {
                            continue;
                        }
                        else
                        {
                            // it is not a channel
                        }
                    }
                    else
                    {
                        // it is not a channel
                    }
                }

                // Public channels require us knowing the AccessHash for them in order
                // to join them
                ulong channelAccessHash = 0;
                if (channel != null)
                {
                    channelAccessHash = channel.AccessHash;
                }

                globalContacts.Add(new TelegramPartyContact
                {
                    FirstName = name,
                    Ids       = new List <Contact.ID>
                    {
                        new Contact.PartyID
                        {
                            Service       = this,
                            Id            = TelegramUtils.GetChatId(chat),
                            ExtendedParty = channel != null,
                        }
                    },
                    AccessHash = channelAccessHash
                });
            }
            return(globalContacts);
        }
        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());
                        }
                    }
                }
            }));
        }
Exemple #7
0
 public Task GetContacts(string query, bool searchForParties, Action <List <Contact> > result)
 {
     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,
                 Ids = new List <Contact.ID>
                 {
                     new Contact.ID
                     {
                         Service = this,
                         Id = x.Id.ToString(CultureInfo.InvariantCulture)
                     }
                 },
             }).OfType <Contact>().OrderBy(x => x.FirstName).ToList();
             result(contacts);
         }
         else
         {
             var partyContacts = new List <Contact>();
             foreach (var iDialog in _dialogs.Dialogs)
             {
                 var dialog = iDialog as Dialog;
                 if (dialog == null)
                 {
                     continue;
                 }
                 var peerChat = dialog.Peer as PeerChat;
                 if (peerChat == null)
                 {
                     continue;
                 }
                 var peerChatId = peerChat.ChatId.ToString(CultureInfo.InvariantCulture);
                 string name = null;
                 foreach (var iChat in _dialogs.Chats)
                 {
                     var chatId = TelegramUtils.GetChatId(iChat);
                     if (chatId == peerChatId)
                     {
                         name = TelegramUtils.GetChatTitle(iChat);
                         break;
                     }
                 }
                 if (!string.IsNullOrWhiteSpace(name))
                 {
                     partyContacts.Add(new TelegramPartyContact
                     {
                         FirstName = name,
                         Ids = new List <Contact.ID>
                         {
                             new Contact.PartyID
                             {
                                 Service = this,
                                 Id = peerChatId,
                             }
                         },
                     });
                 }
             }
             result(partyContacts);
         }
     }));
 }
        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);
                    }
                }
            }));
        }