♻️ Centralized data models (wip)

This commit is contained in:
2025-09-27 14:09:28 +08:00
parent 51b6f7309e
commit e70d8371f8
206 changed files with 1352 additions and 2128 deletions

View File

@@ -1,7 +1,7 @@
using System.Linq.Expressions;
using System.Reflection;
using DysonNetwork.Ring.Notification;
using DysonNetwork.Shared.Data;
using DysonNetwork.Shared.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Query;
@@ -15,8 +15,8 @@ public class AppDatabase(
IConfiguration configuration
) : DbContext(options)
{
public DbSet<Notification.Notification> Notifications { get; set; } = null!;
public DbSet<PushSubscription> PushSubscriptions { get; set; } = null!;
public DbSet<Shared.Models.SnNotification> Notifications { get; set; } = null!;
public DbSet<SnNotificationPushSubscription> PushSubscriptions { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

View File

@@ -1,6 +1,6 @@
using System.Net.WebSockets;
using DysonNetwork.Shared.Proto;
using WebSocketPacket = DysonNetwork.Shared.Data.WebSocketPacket;
using WebSocketPacket = DysonNetwork.Shared.Models.WebSocketPacket;
namespace DysonNetwork.Ring.Connection;

View File

@@ -3,7 +3,7 @@ using DysonNetwork.Shared.Proto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using WebSocketPacket = DysonNetwork.Shared.Data.WebSocketPacket;
using WebSocketPacket = DysonNetwork.Shared.Models.WebSocketPacket;
namespace DysonNetwork.Ring.Connection;

View File

@@ -1,10 +1,10 @@
using System.Collections.Concurrent;
using System.Net.WebSockets;
using DysonNetwork.Shared.Data;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Shared.Stream;
using NATS.Client.Core;
using WebSocketPacket = DysonNetwork.Shared.Data.WebSocketPacket;
using WebSocketPacket = DysonNetwork.Shared.Models.WebSocketPacket;
namespace DysonNetwork.Ring.Connection;

View File

@@ -1,21 +0,0 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using DysonNetwork.Shared.Data;
using NodaTime;
namespace DysonNetwork.Ring.Notification;
public class Notification : ModelBase
{
public Guid Id { get; set; } = Guid.NewGuid();
[MaxLength(1024)] public string Topic { get; set; } = null!;
[MaxLength(1024)] public string? Title { get; set; }
[MaxLength(2048)] public string? Subtitle { get; set; }
[MaxLength(4096)] public string? Content { get; set; }
[Column(TypeName = "jsonb")] public Dictionary<string, object?> Meta { get; set; } = new();
public int Priority { get; set; } = 10;
public Instant? ViewedAt { get; set; }
public Guid AccountId { get; set; }
}

View File

@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using DysonNetwork.Shared.Auth;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -78,7 +79,7 @@ public class NotificationController(
[HttpPut("subscription")]
[Authorize]
public async Task<ActionResult<PushSubscription>>
public async Task<ActionResult<SnNotificationPushSubscription>>
SubscribeToPushNotification(
[FromBody] PushNotificationSubscribeRequest request
)

View File

@@ -2,10 +2,11 @@ using CorePush.Apple;
using CorePush.Firebase;
using DysonNetwork.Ring.Connection;
using DysonNetwork.Ring.Services;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using Microsoft.EntityFrameworkCore;
using NodaTime;
using WebSocketPacket = DysonNetwork.Shared.Data.WebSocketPacket;
using WebSocketPacket = DysonNetwork.Shared.Models.WebSocketPacket;
namespace DysonNetwork.Ring.Notification;
@@ -62,7 +63,7 @@ public class PushService
.ExecuteDeleteAsync();
}
public async Task<PushSubscription> SubscribeDevice(
public async Task<SnNotificationPushSubscription> SubscribeDevice(
string deviceId,
string deviceToken,
PushProvider provider,
@@ -90,7 +91,7 @@ public class PushService
return existingSubscription;
}
var subscription = new PushSubscription
var subscription = new SnNotificationPushSubscription
{
DeviceId = deviceId,
DeviceToken = deviceToken,
@@ -259,7 +260,7 @@ public class PushService
await DeliverPushNotification(notification);
}
private async Task SendPushNotificationAsync(PushSubscription subscription, Notification notification)
private async Task SendPushNotificationAsync(SnNotificationPushSubscription subscription, Notification notification)
{
try
{

View File

@@ -1,25 +0,0 @@
using System.ComponentModel.DataAnnotations;
using DysonNetwork.Shared.Data;
using Microsoft.EntityFrameworkCore;
using NodaTime;
namespace DysonNetwork.Ring.Notification;
public enum PushProvider
{
Apple,
Google
}
[Index(nameof(AccountId), nameof(DeviceId), nameof(DeletedAt), IsUnique = true)]
public class PushSubscription : ModelBase
{
public Guid Id { get; set; } = Guid.NewGuid();
public Guid AccountId { get; set; }
[MaxLength(8192)] public string DeviceId { get; set; } = null!;
[MaxLength(8192)] public string DeviceToken { get; set; } = null!;
public PushProvider Provider { get; set; }
public int CountDelivered { get; set; }
public Instant? LastUsedAt { get; set; }
}

View File

@@ -25,7 +25,7 @@ public class RingServiceGrpc(
public override Task<Empty> PushWebSocketPacket(PushWebSocketPacketRequest request, ServerCallContext context)
{
var packet = Shared.Data.WebSocketPacket.FromProtoValue(request.Packet);
var packet = Shared.Models.WebSocketPacket.FromProtoValue(request.Packet);
WebSocketService.SendPacketToAccount(Guid.Parse(request.UserId), packet);
return Task.FromResult(new Empty());
@@ -34,7 +34,7 @@ public class RingServiceGrpc(
public override Task<Empty> PushWebSocketPacketToUsers(PushWebSocketPacketToUsersRequest request,
ServerCallContext context)
{
var packet = Shared.Data.WebSocketPacket.FromProtoValue(request.Packet);
var packet = Shared.Models.WebSocketPacket.FromProtoValue(request.Packet);
foreach (var accountId in request.UserIds)
WebSocketService.SendPacketToAccount(Guid.Parse(accountId), packet);
@@ -45,7 +45,7 @@ public class RingServiceGrpc(
public override Task<Empty> PushWebSocketPacketToDevice(PushWebSocketPacketToDeviceRequest request,
ServerCallContext context)
{
var packet = Shared.Data.WebSocketPacket.FromProtoValue(request.Packet);
var packet = Shared.Models.WebSocketPacket.FromProtoValue(request.Packet);
websocket.SendPacketToDevice(request.DeviceId, packet);
return Task.FromResult(new Empty());
@@ -54,7 +54,7 @@ public override Task<Empty> PushWebSocketPacketToDevice(PushWebSocketPacketToDev
public override Task<Empty> PushWebSocketPacketToDevices(PushWebSocketPacketToDevicesRequest request,
ServerCallContext context)
{
var packet = Shared.Data.WebSocketPacket.FromProtoValue(request.Packet);
var packet = Shared.Models.WebSocketPacket.FromProtoValue(request.Packet);
foreach (var deviceId in request.DeviceIds)
websocket.SendPacketToDevice(deviceId, packet);

View File

@@ -119,7 +119,7 @@ public class QueueBackgroundService(
var pushService = scope.ServiceProvider.GetRequiredService<PushService>();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<QueueBackgroundService>>();
var notification = JsonSerializer.Deserialize<Notification.Notification>(message.Data);
var notification = JsonSerializer.Deserialize<Shared.Models.SnNotification>(message.Data);
if (notification == null)
{
logger.LogError("Invalid push notification data format");

View File

@@ -26,7 +26,7 @@ public class QueueService(INatsConnection nats)
await js.PublishAsync(QueueBackgroundService.QueueName, rawMessage);
}
public async Task EnqueuePushNotification(Notification.Notification notification, Guid userId, bool isSavable = false)
public async Task EnqueuePushNotification(Shared.Models.SnNotification notification, Guid userId, bool isSavable = false)
{
// Update the account ID in case it wasn't set
notification.AccountId = userId;