예제 #1
0
 private static async Task TrySendPushNotification(PushNotification notification, PushSubscription recipient, WebPush.VapidDetails details, WebPush.WebPushClient client, JsonSerializerSettings serializerSettings, ILogger logger)
 {
     // This is run in a background thread. We should not access or update mutable state.
     try
     {
         var payload      = JsonConvert.SerializeObject(notification, serializerSettings);
         var subscription = new WebPush.PushSubscription(recipient.Endpoint, recipient.Keys["p256dh"], recipient.Keys["auth"]);
         await client.SendNotificationAsync(subscription, payload, details);
     }
     catch (Exception error)
     {
         using (logger.BeginKeyValueScope("recipient", recipient))
             using (logger.BeginKeyValueScope("publicKey", details.PublicKey))
                 using (logger.BeginKeyValueScope("publicKey", details.PrivateKey))
                 {
                     logger.LogError(error, "Error sending push notification");
                 }
     }
 }
예제 #2
0
        public async Task <ContentResult> Push([Required, FromBody] string name)
        {
            var user = await _userMgr.GetUserAsync(User);

            string ret;

            try
            {
                if (user.InGroupId == null)
                {
                    ret = "not_in_group";
                }
                else
                {
                    if (user.WebSubscription == null)
                    {
                        ret = "s_not_subscribed";
                    }
                    else
                    {
                        var rcvr = _groupsDb.Users.Where(c => c.InGroupId == user.InGroupId).SingleOrDefault(c => c.UserName == name);
                        if (rcvr == null)
                        {
                            ret = "not_found";
                        }
                        else
                        {
                            if (rcvr.InGroupId != user.InGroupId)
                            {
                                ret = "not_same_group";
                            }
                            else
                            {
                                var web_subscription = StaticData.GetPushSubscription(rcvr.WebSubscription);
                                if (web_subscription == null)
                                {
                                    ret = "r_not_subscribed";
                                }
                                else
                                {
                                    if (rcvr.LastNotified > DateTime.UtcNow.Subtract(TimeSpan.FromHours(1)).Ticks)
                                    {
                                        ret = "is_notified_hour";
                                    }
                                    else
                                    {
                                        if (rcvr.ConnectionId != null)
                                        {
                                            ret = "usr_active";
                                        }
                                        else
                                        {
                                            ValueTask <Chatterer> groupN = _groupsDb.Users.FindAsync(rcvr.InGroupId);
                                            WebPush.WebPushClient client = new WebPush.WebPushClient();
                                            WebPush.VapidDetails  det    = new WebPush.VapidDetails("mailto:[email protected]",
                                                                                                    _connections.vapidPublic, _connections.vapidPrivate);
                                            client.SetVapidDetails(det);
                                            var groupName = (await groupN).Group;
                                            var send_task = client.SendNotificationAsync(web_subscription, $"\"{groupName}\" by {user.UserName}");
                                            rcvr.LastNotified = DateTime.UtcNow.Ticks;
                                            var save_task = _groupsDb.SaveChangesAsync();
                                            await Task.WhenAll(send_task, save_task);

                                            ret = "OK";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ret = e.Message;
            }
            return(Content(ret, "text/plain"));
        }