Example #1
0
        public async Task SyncLog(long chatId)
        {
            // Slow down syncing logs for 10 seconds

            _logger.LogInformation("Waiting to Reconcile Log for {chatId}", chatId);
            Thread.Sleep(10000);
            _logger.LogInformation("Reconciling Log for {chatId}", chatId);

            var messages = new Dictionary <long, TelegramMessage>();

            // Load messages already logged.
            foreach (var messageFromAudit in _messageAuditService.ReadLog(chatId))
            {
                // Use Try Add to avoid errors if we managed to record the same message.
                messages.TryAdd(messageFromAudit.MessageId, messageFromAudit);
            }

            // Build up a list of new messages to log as they should be logged in reverse time order.
            var toAudit = new List <TelegramMessage>();

            await foreach (var messageFromTelegram in GetMessageHistory(chatId))
            {
                // Check if we already audited this message
                if (!messages.ContainsKey(messageFromTelegram.MessageId))
                {
                    // The message is new so we add it to the output and plan to log it
                    messages.Add(messageFromTelegram.MessageId, messageFromTelegram);
                    toAudit.Add(messageFromTelegram);
                }
            }

            if (toAudit.Any())
            {
                _messageAuditService.LogMessage(toAudit.OrderBy(m => m.Sent).ToList());
            }
            _logger.LogInformation("Reconciled Log for {chatId}", chatId);
        }
Example #2
0
        public async Task <IActionResult> OnGetAsync()
        {
            var hexId = ((string)RouteData.Values["chatId"]).Trim();

            if (hexId.Length == 16)
            {
                ChatId = Convert.ToInt64(hexId, 16);
                var chatGroup = await _telegramService.GetGroup(ChatId);

                Title = chatGroup.Title;

                var messages = new Dictionary <long, TelegramMessage>();

                // Load messages already logged.
                foreach (var messageFromAudit in _messageAuditService.ReadLog(ChatId))
                {
                    // Use Try Add to avoid errors if we managed to record the same message.
                    messages.TryAdd(messageFromAudit.MessageId, messageFromAudit);
                }

                // Build up a list of new messages to log as they should be logged in reverse time order.
                var toAudit = new List <TelegramMessage>();
                await foreach (var messageFromTelegram in _telegramService.GetMessageHistory(ChatId))
                {
                    // Check if we already audited this message
                    if (!messages.ContainsKey(messageFromTelegram.MessageId))
                    {
                        // The message is new so we add it to the output and plan to log it
                        messages.Add(messageFromTelegram.MessageId, messageFromTelegram);
                        toAudit.Add(messageFromTelegram);
                    }
                }

                if (toAudit.Any())
                {
                    _messageAuditService.LogMessage(toAudit.OrderBy(m => m.Sent).ToList());
                }

                Messages = messages.Values.OrderByDescending(m => m.Sent);
            }

            return(Page());
        }