Exemplo n.º 1
0
        private Contact CreateTelegramContact(User user)
        {
            Contact globalContact;

            if (user.Bot != null)
            {
                globalContact = new TelegramBotContact
                {
                    User = user
                };
            }
            else
            {
                globalContact = new TelegramContact
                {
                    User = user
                };
            }

            globalContact.Available = TelegramUtils.GetAvailable(user);
            globalContact.LastSeen  = TelegramUtils.GetLastSeenTime(user);
            globalContact.FirstName = user.FirstName;
            globalContact.LastName  = user.LastName;
            globalContact.Ids       = new List <Contact.ID>
            {
                new Contact.ID
                {
                    Service = this,
                    Id      = user.Id.ToString(CultureInfo.InvariantCulture)
                }
            };

            return(globalContact);
        }
Exemplo n.º 2
0
        private List <Contact> GetGlobalContacts(ContactsFound contactsFound)
        {
            var globalContacts = new List <Contact>();

            _dialogs.AddUsers(contactsFound.Users);
            foreach (var iUser in contactsFound.Users)
            {
                var user = iUser as User;
                if (user != null && user.Bot == null)
                {
                    globalContacts.Add(new TelegramContact
                    {
                        Available = TelegramUtils.GetAvailable(user),
                        LastSeen  = TelegramUtils.GetLastSeenTime(user),
                        FirstName = user.FirstName,
                        LastName  = user.LastName,
                        User      = user,
                        Ids       = new List <Contact.ID>
                        {
                            new Contact.ID
                            {
                                Service = this,
                                Id      = user.Id.ToString(CultureInfo.InvariantCulture)
                            }
                        },
                    });
                }
            }
            return(globalContacts);
        }
Exemplo n.º 3
0
 public Task GetContacts(string query, bool searchForParties, Action <List <Contact> > result)
 {
     return(Task.Factory.StartNew(async() =>
     {
         //TODO: search for parties
         var response = (ContactsContacts)await _fullClient.Methods.ContactsGetContactsAsync(
             new ContactsGetContactsArgs
         {
             Hash = string.Empty
         });
         var users = response.Users.OfType <UserContact>();
         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);
     }));
 }
Exemplo n.º 4
0
 public Task GetContactsFavorites(Action <List <Contact> > result)
 {
     return(Task.Factory.StartNew(async() =>
     {
         var count = 0;
         var inputUsers = new List <IInputUser>();
         foreach (var bubbleGroup in BubbleGroupManager.SortByMostPopular(this, true))
         {
             var address = bubbleGroup.Address;
             foreach (var user in _dialogs.Users)
             {
                 var userId = TelegramUtils.GetUserId(user);
                 if (userId == address)
                 {
                     var inputUser = TelegramUtils.CastUserToInputUser(user);
                     if (inputUser != null)
                     {
                         inputUsers.Add(inputUser);
                         break;
                     }
                 }
             }
             count++;
             if (count > 6)
             {
                 break;
             }
         }
         if (!inputUsers.Any())
         {
             result(null);
         }
         else
         {
             using (var client = new FullClientDisposable(this))
             {
                 var users = await GetUsers(inputUsers, client.Client);
                 var contacts = users.Select(x =>
                                             new TelegramContact
                 {
                     Available = TelegramUtils.GetAvailable(x),
                     LastSeen = TelegramUtils.GetLastSeenTime(x),
                     FirstName = TelegramUtils.GetUserName(x),
                     Ids = new List <Contact.ID>
                     {
                         new Contact.ID
                         {
                             Service = this,
                             Id = TelegramUtils.GetUserId(x).ToString(CultureInfo.InvariantCulture)
                         }
                     },
                 }).OfType <Contact>().OrderBy(x => x.FirstName).ToList();
                 result(contacts);
             }
         }
     }));
 }
Exemplo n.º 5
0
        public Task GetContactsFavorites(Action <List <Contact> > result)
        {
            return(Task.Factory.StartNew(() =>
            {
                var count = 0;
                var users = new List <IUser>();
                foreach (var bubbleGroup in BubbleGroupManager.SortByMostPopular(this, true))
                {
                    var address = bubbleGroup.Address;
                    var user = _dialogs.GetUser(uint.Parse(address));
                    if (user != null)
                    {
                        users.Add(user);
                    }
                    count++;
                    if (count > 6)
                    {
                        break;
                    }
                }

                if (!users.Any())
                {
                    result(null);
                    return;
                }

                var contacts = users.Select(x =>
                                            new TelegramContact
                {
                    Available = TelegramUtils.GetAvailable(x),
                    LastSeen = TelegramUtils.GetLastSeenTime(x),
                    FirstName = TelegramUtils.GetUserName(x),
                    Ids = new List <Contact.ID>
                    {
                        new Contact.ID
                        {
                            Service = this,
                            Id = TelegramUtils.GetUserId(x).ToString(CultureInfo.InvariantCulture)
                        }
                    },
                    User = x as User
                }).OfType <Contact>().OrderBy(x => x.FirstName).ToList();

                result(contacts);
            }));
        }
Exemplo n.º 6
0
        public Task GetUserInformation(string address, Action <Disa.Framework.UserInformation> result)
        {
            return(Task.Factory.StartNew(async() =>
            {
                foreach (var user in _dialogs.Users)
                {
                    var userId = TelegramUtils.GetUserId(user);
                    if (userId == address)
                    {
                        using (var client = new FullClientDisposable(this))
                        {
                            var inputUser = TelegramUtils.CastUserToInputUser(user);
                            var updatedUser = await GetUser(inputUser, client.Client);
                            var name = TelegramUtils.GetUserName(updatedUser);
                            var lastSeen = TelegramUtils.GetLastSeenTime(updatedUser);
                            var presence = TelegramUtils.GetAvailable(updatedUser);
                            var phoneNumber = TelegramUtils.GetUserPhoneNumber(updatedUser);

                            if (string.IsNullOrWhiteSpace(name))
                            {
                                result(null);  //TODO: ensure this doesn't crash Disa
                                return;
                            }
                            result(new UserInformation
                            {
                                Title = name,
                                SubtitleType = string.IsNullOrWhiteSpace(phoneNumber) ?
                                               UserInformation.TypeSubtitle.Other : UserInformation.TypeSubtitle.PhoneNumber,
                                Subtitle = string.IsNullOrWhiteSpace(phoneNumber) ?
                                           address : TelegramUtils.ConvertTelegramPhoneNumberIntoInternational(phoneNumber),
                                LastSeen = lastSeen,
                                Presence = presence,
                            });
                            return;
                        }
                    }
                }
                result(null);  //TODO: ensure this doesn't crash Disa
            }));
        }
Exemplo n.º 7
0
        public Task GetUserInformation(string address, Action <Disa.Framework.UserInformation> result)
        {
            return(Task.Factory.StartNew(() =>
            {
                if (address == null)
                {
                    return;
                }

                var user = _dialogs.GetUser(uint.Parse(address));
                using (var client = new FullClientDisposable(this))
                {
                    var name = TelegramUtils.GetUserName(user);
                    var lastSeen = TelegramUtils.GetLastSeenTime(user);
                    var presence = TelegramUtils.GetAvailable(user);
                    var phoneNumber = TelegramUtils.GetUserPhoneNumber(user);

                    if (string.IsNullOrWhiteSpace(name))
                    {
                        result(null); //TODO: ensure this doesn't crash Disa
                        return;
                    }
                    result(new UserInformation
                    {
                        Title = name,
                        SubtitleType = string.IsNullOrWhiteSpace(phoneNumber)
                            ? UserInformation.TypeSubtitle.Other
                            : UserInformation.TypeSubtitle.PhoneNumber,
                        Subtitle = string.IsNullOrWhiteSpace(phoneNumber)
                            ? TelegramUtils.GetUserHandle(user)
                            : TelegramUtils.ConvertTelegramPhoneNumberIntoInternational(phoneNumber),
                        LastSeen = lastSeen,
                        Presence = presence,
                        UserHandle = TelegramUtils.GetUserHandle(user),
                    });
                }
            }));
        }
Exemplo n.º 8
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);
                        }
                    }
                }
            }));
        }
Exemplo n.º 9
0
        public Task FetchBubbleGroupAddressFromLink(string link, Action <Tuple <Contact, Contact.ID> > result)
        {
            return(Task.Factory.StartNew(() =>
            {
                string linkUsefulPart;
                var linkType = GetLinkType(link, out linkUsefulPart);
                switch (linkType)
                {
                case LinkType.Invalid:
                    result(new Tuple <Contact, Contact.ID>(null, null));
                    break;

                case LinkType.PrivateGroup:
                    IChat alreadyJoinedChat;
                    bool linkCheck = CheckLink(linkUsefulPart, out alreadyJoinedChat);
                    if (linkCheck)
                    {
                        if (alreadyJoinedChat != null)
                        {
                            SetResultAsChat(alreadyJoinedChat, TelegramUtils.GetChatId(alreadyJoinedChat), result);
                            return;
                        }
                        var updates = JoinChat(linkUsefulPart);
                        var updatesObj = updates as Updates;
                        if (updatesObj != null)
                        {
                            var chatObj = updatesObj.Chats[0];
                            SetResultAsChat(chatObj, TelegramUtils.GetChatId(chatObj), result);
                        }
                    }
                    else
                    {
                        result(new Tuple <Contact, Contact.ID>(null, null));
                    }
                    break;

                case LinkType.PublicGroup:
                    JoinChannelIfLeft(linkUsefulPart);
                    var chat = _dialogs.GetChat(uint.Parse(linkUsefulPart));
                    SetResultAsChat(chat, linkUsefulPart, result);
                    break;

                case LinkType.PublicUser:
                    var user = _dialogs.GetUser(uint.Parse(linkUsefulPart));
                    var userObj = user as User;
                    var userContact = new TelegramContact
                    {
                        Available = TelegramUtils.GetAvailable(user),
                        LastSeen = TelegramUtils.GetLastSeenTime(user),
                        FirstName = userObj?.FirstName,
                        LastName = userObj?.LastName,
                        User = userObj,
                    };
                    var userContactId = new Contact.ID
                    {
                        Id = linkUsefulPart,
                        Service = this
                    };
                    result(new Tuple <Contact, Contact.ID>(userContact, userContactId));
                    break;

                default:
                    result(new Tuple <Contact, Contact.ID>(null, null));
                    break;
                }
            }));
        }
Exemplo n.º 10
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);
         }
     }));
 }