public Task FetchChannelBubbleGroupAddress(string name, string description, Action <bool, string> result)
        {
            return(Task.Factory.StartNew(async() =>
            {
                using (var client = new FullClientDisposable(this))
                {
                    try
                    {
                        var response = await client.Client.Methods.ChannelsCreateChannelAsync(new ChannelsCreateChannelArgs
                        {
                            Flags = 0,
                            Broadcast = null,
                            Megagroup = null,
                            Title = name,
                            About = description
                        });

                        var updates = response as Updates;
                        if (updates != null)
                        {
                            SendToResponseDispatcher(updates, client.Client);
                            _dialogs.AddChats(updates.Chats);
                            var chat = TelegramUtils.GetChatFromUpdate(updates);
                            result(true, TelegramUtils.GetChatId(chat));
                        }
                        else
                        {
                            result(false, null);
                        }
                    }
                    catch (Exception e)
                    {
                        //we get an exception if the user is not allowed to create groups
                        var rpcError = e as RpcErrorException;
                        if (rpcError != null)
                        {
                            result(false, null);
                        }
                    }
                }
            }));
        }
 public Task FetchBubbleGroupAddress(Tuple <Contact, Contact.ID>[] contacts, Action <bool, string> result)
 {
     return(Task.Factory.StartNew(async() =>
     {
         if (contacts.Length > 1)
         {
             var inputUsers = new List <IInputUser>();
             var names = new List <string>();
             foreach (var contact in contacts)
             {
                 names.Add(contact.Item1.FullName);
                 var telegramContact = contact.Item1 as TelegramContact;
                 var inputUser = TelegramUtils.CastUserToInputUser(telegramContact.User);
                 if (inputUser != null)
                 {
                     inputUsers.Add(inputUser);
                 }
             }
             if (inputUsers.Any())
             {
                 var subject = BubbleGroupUtils.GeneratePartyTitle(names.ToArray());
                 if (subject.Length > 25)
                 {
                     subject = subject.Substring(0, 22);
                     subject += "...";
                 }
                 using (var client = new FullClientDisposable(this))
                 {
                     try
                     {
                         var response = await client.Client.Methods.MessagesCreateChatAsync(
                             new MessagesCreateChatArgs
                         {
                             Users = inputUsers,
                             Title = subject,
                         });
                         var updates = response as Updates;
                         if (updates != null)
                         {
                             SendToResponseDispatcher(updates, client.Client);
                             _dialogs.AddUsers(updates.Users);
                             _dialogs.AddChats(updates.Chats);
                             var chat = TelegramUtils.GetChatFromUpdate(updates);
                             result(true, TelegramUtils.GetChatId(chat));
                         }
                         else
                         {
                             result(false, null);
                         }
                     }
                     catch (Exception e)
                     {
                         //we get an exception if the user is not allowed to create groups
                         var rpcError = e as RpcErrorException;
                         if (rpcError != null)
                         {
                             result(false, null);
                         }
                     }
                 }
             }
             else
             {
                 result(false, null);
             }
         }
         else
         {
             if (contacts[0].Item2 is Contact.PartyID)
             {
                 JoinChannelIfLeft(contacts[0].Item2.Id);
                 result(true, contacts[0].Item2.Id);
             }
             else
             {
                 var telegramContact = contacts[0].Item1 as TelegramContact;
                 _dialogs.AddUser(telegramContact.User);
                 result(true, contacts[0].Item2.Id);
             }
         }
     }));
 }
        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);
                    }
                }
            }));
        }