✨ Notification APIs
This commit is contained in:
parent
cb7179aa27
commit
bd7e589681
@ -9,6 +9,7 @@ namespace DysonNetwork.Sphere.Account;
|
|||||||
public class Notification : ModelBase
|
public class Notification : ModelBase
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; } = Guid.NewGuid();
|
public Guid Id { get; set; } = Guid.NewGuid();
|
||||||
|
[MaxLength(1024)] public string Topic { get; set; } = null!;
|
||||||
[MaxLength(1024)] public string? Title { get; set; }
|
[MaxLength(1024)] public string? Title { get; set; }
|
||||||
[MaxLength(2048)] public string? Subtitle { get; set; }
|
[MaxLength(2048)] public string? Subtitle { get; set; }
|
||||||
[MaxLength(4096)] public string? Content { get; set; }
|
[MaxLength(4096)] public string? Content { get; set; }
|
||||||
|
59
DysonNetwork.Sphere/Account/NotificationController.cs
Normal file
59
DysonNetwork.Sphere/Account/NotificationController.cs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using DysonNetwork.Sphere.Post;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace DysonNetwork.Sphere.Account;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("/notifications")]
|
||||||
|
public class NotificationController(AppDatabase db, NotificationService nty) : ControllerBase
|
||||||
|
{
|
||||||
|
[HttpGet]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<List<Notification>>> ListNotifications([FromQuery] int offset = 0,
|
||||||
|
[FromQuery] int take = 20)
|
||||||
|
{
|
||||||
|
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
||||||
|
var currentUser = currentUserValue as Account;
|
||||||
|
if (currentUser == null) return Unauthorized();
|
||||||
|
|
||||||
|
var totalCount = await db.Notifications
|
||||||
|
.Where(s => s.AccountId == currentUser.Id)
|
||||||
|
.CountAsync();
|
||||||
|
var notifications = await db.Notifications
|
||||||
|
.Where(s => s.AccountId == currentUser.Id)
|
||||||
|
.OrderByDescending(e => e.CreatedAt)
|
||||||
|
.Skip(offset)
|
||||||
|
.Take(take)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
Response.Headers["X-Total"] = totalCount.ToString();
|
||||||
|
|
||||||
|
return Ok(notifications);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PushNotificationSubscribeRequest
|
||||||
|
{
|
||||||
|
[MaxLength(4096)] public string DeviceId { get; set; } = null!;
|
||||||
|
[MaxLength(4096)] public string DeviceToken { get; set; } = null!;
|
||||||
|
public NotificationPushProvider Provider { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("subscription")]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<NotificationPushSubscription>> SubscribeToPushNotification(
|
||||||
|
[FromBody] PushNotificationSubscribeRequest request
|
||||||
|
)
|
||||||
|
{
|
||||||
|
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
||||||
|
var currentUser = currentUserValue as Account;
|
||||||
|
if (currentUser == null) return Unauthorized();
|
||||||
|
|
||||||
|
var result =
|
||||||
|
await nty.SubscribePushNotification(currentUser, request.Provider, request.DeviceId, request.DeviceToken);
|
||||||
|
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
}
|
@ -43,6 +43,8 @@ public class NotificationService
|
|||||||
}, clientFactory.CreateClient());
|
}, clientFactory.CreateClient());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO remove all push notification with this device id when this device is logged out
|
||||||
|
|
||||||
public async Task<NotificationPushSubscription> SubscribePushNotification(
|
public async Task<NotificationPushSubscription> SubscribePushNotification(
|
||||||
Account account,
|
Account account,
|
||||||
NotificationPushProvider provider,
|
NotificationPushProvider provider,
|
||||||
|
@ -17,6 +17,7 @@ public class PostController(AppDatabase db, PostService ps, IEnforcer enforcer)
|
|||||||
var currentUser = currentUserValue as Account.Account;
|
var currentUser = currentUserValue as Account.Account;
|
||||||
|
|
||||||
var totalCount = await db.Posts
|
var totalCount = await db.Posts
|
||||||
|
.FilterWithVisibility(currentUser, isListing: true)
|
||||||
.CountAsync();
|
.CountAsync();
|
||||||
var posts = await db.Posts
|
var posts = await db.Posts
|
||||||
.Include(e => e.Publisher)
|
.Include(e => e.Publisher)
|
||||||
@ -77,6 +78,7 @@ public class PostController(AppDatabase db, PostService ps, IEnforcer enforcer)
|
|||||||
|
|
||||||
var totalCount = await db.Posts
|
var totalCount = await db.Posts
|
||||||
.Where(e => e.RepliedPostId == post.Id)
|
.Where(e => e.RepliedPostId == post.Id)
|
||||||
|
.FilterWithVisibility(currentUser, isListing: true)
|
||||||
.CountAsync();
|
.CountAsync();
|
||||||
var posts = await db.Posts
|
var posts = await db.Posts
|
||||||
.Where(e => e.RepliedPostId == id)
|
.Where(e => e.RepliedPostId == id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user