Exemplo n.º 1
0
        private async Task <(long UserMessageCount, long AllMessageCount, List <Message> messages)> GetMessages(string channelId, Models.ChannelType channelType, DateTime messagesOlderThan)
        {
            long           allMessageCount  = 0;
            long           userMessageCount = 0;
            bool           isMoreMessages   = true;
            List <Message> messages         = new List <Message>();

            while (isMoreMessages)
            {
                MessageHistory slackMessages = null;
                switch (channelType)
                {
                case Models.ChannelType.Public:
                    slackMessages = await SlackClient.GetConversationsHistoryAsync(new Channel()
                    {
                        id = channelId
                    }, messagesOlderThan, new DateTime(2000, 01, 01), 1000);

                    break;

                case Models.ChannelType.Private:
                    slackMessages = await SlackClient.GetConversationsHistoryAsync(new Channel()
                    {
                        id = channelId
                    }, messagesOlderThan, new DateTime(2000, 01, 01), 1000);

                    break;

                case Models.ChannelType.DirectMessage:
                    slackMessages = await SlackClient.GetConversationsHistoryAsync(new Channel()
                    {
                        id = channelId
                    }, messagesOlderThan, new DateTime(2000, 01, 01), 1000);

                    break;
                }

                if (slackMessages?.messages != null)
                {
                    allMessageCount  += slackMessages.messages.Count(x => string.IsNullOrEmpty(x.subtype) || !x.subtype.Equals(Config.SlackGroupJoinMessageType));
                    userMessageCount += slackMessages.messages
                                        .Count(x =>
                                               (string.IsNullOrEmpty(UserIdOfToken) || string.IsNullOrEmpty(x.user) || x.user.Equals(UserIdOfToken)) &&
                                               (string.IsNullOrEmpty(x.subtype) || !x.subtype.Equals(Config.SlackGroupJoinMessageType))
                                               );
                    messages.AddRange(slackMessages.messages);
                }

                if (slackMessages?.messages?.Count() >= 1000)
                {
                    messagesOlderThan = slackMessages.messages.Min(x => x.ts);
                }
                else
                {
                    isMoreMessages = false;
                }
            }

            return(userMessageCount, allMessageCount, messages);
        }