Exemplo n.º 1
0
        /// <summary>
        /// Gets message history for a chat.
        /// </summary>
        /// <param name="contact">User</param>
        /// <param name="offset">Number of list elements to be skipped. As of Layer 15 this value is added to the one that was calculated from max_id. Negative values are also accepted.</param>
        /// <param name="maxId">If a positive value was transferred, the method will return only messages with IDs less than max_id</param>
        /// <returns>Returns message history for a chat..</returns>
        public async Task <Models.History> GetHistoryAsync(Models.Contact contact)
        {
            await ConnectAsync();

            int limit = 30;

            Models.History history = new Models.History
            {
                Messages = new List <Models.Message>(),
            };
            InputPeer peer = null;

            if (contact.IsForeign)
            {
                peer = new InputPeerForeignConstructor(contact.Id, contact.AccessHash);
            }
            else
            {
                peer = new InputPeerContactConstructor(contact.Id);
            }

            MessagesMessages result = await _client.GetHistory(peer, 0, limit);

            if (result is MessagesMessagesConstructor messagesMessagesConstructor)
            {
                history.Messages.AddRange(messagesMessagesConstructor.messages.Select(m => (Models.Message)m).Where(m => m != null));
                history.AddChats(messagesMessagesConstructor.chats.Select(c => (Models.Chat)c).Where(c => c != null));
                history.AddContacts(messagesMessagesConstructor.users.Select(u => (Models.Contact)u).Where(u => u != null));
            }
            else if (result is MessagesMessagesSliceConstructor messagesMessagesSliceConstructor)
            {
                int count = messagesMessagesSliceConstructor.count;
                int total = 0;
                do
                {
                    total += limit;
                    history.Messages.AddRange(messagesMessagesSliceConstructor.messages.Select(m => (Models.Message)m).Where(m => m != null));
                    history.AddChats(messagesMessagesSliceConstructor.chats.Select(c => (Models.Chat)c).Where(c => c != null));
                    history.AddContacts(messagesMessagesSliceConstructor.users.Select(u => (Models.Contact)u).Where(u => u != null));
                    if (total > count)
                    {
                        break;
                    }
                    result = await _client.GetHistory(peer, total, limit);

                    messagesMessagesSliceConstructor = (MessagesMessagesSliceConstructor)result;
                } while (true);
            }

            return(history);
        }
Exemplo n.º 2
0
        public async Task <bool> SetCancelAllActionAsync(Models.Contact contact)
        {
            await ConnectAsync();

            InputPeer peer = null;

            if (contact.IsForeign)
            {
                peer = new InputPeerForeignConstructor(contact.Id, contact.AccessHash);
            }
            else
            {
                peer = new InputPeerContactConstructor(contact.Id);
            }
            return(await _client.SetTyping(peer, SendMessageAction.CancelAction));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Marks message history as read.
        /// </summary>
        /// <param name="contact">Target user</param>
        /// <param name="offset">Value from (messages.affectedHistory) or 0</param>
        /// <param name="maxId">If a positive value is passed, only messages with identifiers less or equal than the given one will be read</param>
        /// <returns>Object contains info on affected part of communication history with the user or in a chat.</returns>
        public async Task <MessagesAffectedHistory> MarkedMessagesAsReadAsync(Models.Contact contact, int offset, int maxId = -1)
        {
            InputPeer peer = null;

            if (contact.IsForeign)
            {
                peer = new InputPeerForeignConstructor(contact.Id, contact.AccessHash);
            }
            else
            {
                peer = new InputPeerContactConstructor(contact.Id);
            }

            await ConnectAsync();

            return(await _client.ReadHistory(peer, offset, maxId));
        }