Example #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);
        }
Example #2
0
 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));
 }
Example #3
0
        public Task GetContactFromAddress(string address, Action <Contact, Contact.ID> result)
        {
            return(Task.Factory.StartNew(async() =>
            {
                Func <IUser, Tuple <TelegramContact, TelegramContact.ID> > buildContact = user =>
                {
                    var id = new TelegramContact.ID
                    {
                        Service = this,
                        Id = address,
                    };
                    var contact = new TelegramContact
                    {
                        FirstName = TelegramUtils.GetUserName(user),
                        Ids = new List <Contact.ID>
                        {
                            id
                        },
                    };
                    return Tuple.Create(contact, id);
                };
                if (address == null)
                {
                    return;
                }

                var userTuple = _dialogs.GetUser(uint.Parse(address));
                if (userTuple != null)
                {
                    var tuple = buildContact(userTuple);
                    result(tuple.Item1, tuple.Item2);
                    return;
                }

                var userContacts = await FetchContacts();
                foreach (var userContact in userContacts)
                {
                    var userId = TelegramUtils.GetUserId(userContact);
                    if (userId == address)
                    {
                        var tuple = buildContact(userContact);
                        result(tuple.Item1, tuple.Item2);
                        return;
                    }
                }
                result(null, null);
            }));
        }
 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));
 }
 public Task FetchBubbleGroupAddressFromLink(string link, Action<Tuple<Contact, Contact.ID>> result)
 {
     return Task.Factory.StartNew(() =>
     {
         string linkUsefulPart;
         var linkType = GetLinkType(link, out linkUsefulPart);
         DebugPrint("Link useful part " + ObjectDumper.Dump(linkType));
         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);
                     DebugPrint("Updates " + ObjectDumper.Dump(updates));
                     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;
         }
     });
 }
Example #6
0
        public Task GetContactFromAddress(string address, Action<Contact, Contact.ID> result)
        {
            return Task.Factory.StartNew(async () =>
            {
                Func<IUser, Tuple<TelegramContact, TelegramContact.ID>> buildContact = user =>
                {
                    var id = new TelegramContact.ID
                    {
                        Service = this,
                        Id = address,
                    };
                    var contact = new TelegramContact
                    {
                        FirstName = TelegramUtils.GetUserName(user),
                        Ids = new List<Contact.ID>
                        {
                            id
                        },
                    };
                    return Tuple.Create(contact, id);
                };
                if (address == null)
                {
                    return;
                }

                var userTuple = _dialogs.GetUser(uint.Parse(address));
                if (userTuple != null)
                {
                    var tuple = buildContact(userTuple);
                    result(tuple.Item1, tuple.Item2);
                    return;
                }

                var userContacts = await FetchContacts();
                foreach (var userContact in userContacts)
                {
                    var userId = TelegramUtils.GetUserId(userContact);
                    if (userId == address)
                    {
                        var tuple = buildContact(userContact);
                        result(tuple.Item1, tuple.Item2);
                        return;
                    }
                }
                result(null, null);
            });
        }
Example #7
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;
                }
            }));
        }