Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
0
        public static async Task <(string title, string description, string url)> InsertJobWonNotification(MySqlConnection connection, OfferFinalizedMessage message, string userID,
                                                                                                           string nodeName, decimal tokenAmount, long holdingTimeInMinutes, string blockchain)
        {
            string title = $"Job awarded for {nodeName} on {blockchain}";

            var exitsingCount = await connection.ExecuteScalarAsync <int>(@"SELECT COUNT(*) FROM notifications where UserID = @userID AND CreatedAt = @date AND Title = @title",
                                                                          new
            {
                userID = userID,
                date   = message.Timestamp,
                title  = title,
            });

            if (exitsingCount != 0)
            {
                return(null, null, null);
            }

            var timeInText = TimeSpan.FromMinutes(holdingTimeInMinutes)
                             .Humanize(5, maxUnit: TimeUnit.Year, minUnit: TimeUnit.Minute);

            tokenAmount = Math.Truncate(100 * tokenAmount) / 100;

            string url = $"offers/{message.OfferID}";

            string descripton = $"{timeInText} for {tokenAmount:N} TRAC";

            await connection.ExecuteAsync("INSERT INTO notifications(`UserID`, `Read`, `Dismissed`, `CreatedAt`, `Title`, `Description`, `RelativeUrl`) VALUES(@userID, 0, 0, @date, @title, @description, @url)", new
            {
                userID      = userID,
                date        = message.Timestamp,
                title       = title,
                description = descripton,
                url         = url
            });

            return(title, descripton, url);
        }