Esempio n. 1
0
        public async Task <IActionResult> PutDetail(int id, Detail detail)
        {
            if (id != detail.Id)
            {
                return(BadRequest());
            }

            _context.Entry(detail).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DetailExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Esempio n. 2
0
        public async Task <IActionResult> AddFeed(string url)
        {
            var feed = await _feed.GetFeed(url);

            db.Feeds.Add(feed);
            await db.SaveChangesAsync();

            return(Ok(feed));
        }
Esempio n. 3
0
        public async Task <IActionResult> Subscribe([FromBody] ClientSubscription sub)
        {
            //var user = await _userManager.GetUserAsync(User);
            var user = await db.Users.FirstOrDefaultAsync();

            var subscription = new Subscription
            {
                UserId   = user.UserId,
                p256dh   = sub.keys.p256dh,
                auth     = sub.keys.auth,
                endpoint = sub.endpoint
            };

            db.Subscriptions.Add(subscription);
            await db.SaveChangesAsync();

            return(Ok(subscription));
        }
Esempio n. 4
0
        private async Task SendNotification(Subscription s, Article article)
        {
            var subscription  = new PushSubscription(s.endpoint, s.p256dh, s.auth);
            var vapidDetails  = new VapidDetails(_conf["Push:subject"], _conf["Push:publicKey"], _conf["Push:privateKey"]);
            var webPushClient = new WebPushClient();
            var payload       = JsonConvert.SerializeObject(new { feedId = article.FeedId, articleId = article.ArticleId, title = article.Title });

            try
            {
                await webPushClient.SendNotificationAsync(subscription, payload, vapidDetails);
            }
            catch (WebPushException exception)
            {
                if (exception.StatusCode == System.Net.HttpStatusCode.Gone)
                {
                    db.Subscriptions.Remove(s);
                }
            }
            await db.SaveChangesAsync();
        }