Exemple #1
0
        private async Task Consumer_Received(object sender, BasicDeliverEventArgs e)
        {
            try
            {
                var body = e.Body.ToArray();
                var text = Encoding.UTF8.GetString(body);
                OfferFinalizedMessage message = JsonConvert.DeserializeObject <OfferFinalizedMessage>(text);

                if (message == null)
                {
                    _channel.BasicAck(e.DeliveryTag, false);
                    return;
                }

                string[] holders = { message.Holder1, message.Holder2, message.Holder3 };

                await using (var connection = new MySqlConnection(OTHubSettings.Instance.MariaDB.ConnectionString))
                {
                    foreach (string holder in holders)
                    {
                        var users = (await connection.QueryAsync(
                                         @"SELECT DISTINCT mn.UserID, COALESCE(mn.DisplayName, i.NodeID) NodeName, U.TelegramUserID, 
ts.NotificationsEnabled, ts.JobWonEnabled, ts.HasReceivedMessageFromUser FROM mynodes mn
JOIN otidentity I ON I.NodeID = mn.NodeID
JOIN users U ON U.ID = mn.UserID
LEFT JOIN telegramsettings ts ON ts.UserID = U.ID
WHERE I.Version > 0 AND I.Identity = @identity", new
                        {
                            identity = holder
                        })).ToArray();

                        if (users.Any())
                        {
                            var jobData = await connection.QueryFirstOrDefaultAsync(
                                @"SELECT o.TokenAmountPerHolder, o.HoldingTimeInMinutes, b.DisplayName AS BlockchainName 
FROM OTOffer o
JOIN blockchains b ON b.id = o.BlockchainID
where o.BlockchainID = @blockchainID AND o.OfferID = @offerID",
                                new
                            {
                                blockchainID = message.BlockchainID,
                                offerID      = message.OfferID
                            });

                            if (jobData == null)
                            {
                                continue;
                            }

                            string  blockchain           = jobData.BlockchainName;
                            decimal tokenAmount          = jobData.TokenAmountPerHolder;
                            long    holdingTimeInMinutes = jobData.HoldingTimeInMinutes;

                            foreach (var user in users)
                            {
                                string userID                     = user.UserID;
                                string nodeName                   = user.NodeName;
                                long?  telegramUserID             = user.TelegramUserID;
                                bool?  notificationsEnabled       = user.NotificationsEnabled == 1;
                                bool?  jobWonEnabled              = user.JobWonEnabled == 1;
                                bool?  hasReceivedMessageFromUser = user.HasReceivedMessageFromUser == 1;

                                (string title, string description, string url)data = await NotificationsReaderWriter.InsertJobWonNotification(connection, message, userID,
                                                                                                                                              nodeName, tokenAmount, holdingTimeInMinutes, blockchain);

                                if (data.title != null)
                                {
                                    try
                                    {
                                        await _hubContext.Clients.User(userID).SendAsync("JobWon", data.title, data.description);
                                    }
                                    catch (Exception ex)
                                    {
                                    }

                                    if (telegramUserID.HasValue && notificationsEnabled == true && jobWonEnabled == true && hasReceivedMessageFromUser == true)
                                    {
                                        try
                                        {
                                            await _bot.JobWon(userID, telegramUserID.Value, data.title, data.description, $"https://othub.origin-trail.network/{data.url}");
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.WriteLine("Failed send telegram notification: " + ex.Message);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                _channel.BasicAck(e.DeliveryTag, false);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to process offer finalized: " + ex.ToString());
                _channel.BasicNack(e.DeliveryTag, false, true);
            }
        }