♻️ Finish centerlizing the data models
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using DysonNetwork.Shared.Data;
|
||||
using DysonNetwork.Shared.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
|
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using NodaTime;
|
||||
|
||||
#nullable disable
|
||||
|
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
|
@@ -32,7 +32,7 @@ public class NotificationController(
|
||||
|
||||
[HttpGet]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<List<Notification>>> ListNotifications(
|
||||
public async Task<ActionResult<List<SnNotification>>> ListNotifications(
|
||||
[FromQuery] int offset = 0,
|
||||
// The page size set to 5 is to avoid the client pulled the notification
|
||||
// but didn't render it in the screen-viewable region.
|
||||
@@ -146,7 +146,7 @@ public class NotificationController(
|
||||
)
|
||||
{
|
||||
await nty.SendNotificationBatch(
|
||||
new Notification
|
||||
new SnNotification
|
||||
{
|
||||
CreatedAt = SystemClock.Instance.GetCurrentInstant(),
|
||||
UpdatedAt = SystemClock.Instance.GetCurrentInstant(),
|
||||
|
@@ -1,27 +0,0 @@
|
||||
using DysonNetwork.Shared.Cache;
|
||||
using EFCore.BulkExtensions;
|
||||
using NodaTime;
|
||||
using Quartz;
|
||||
|
||||
namespace DysonNetwork.Ring.Notification;
|
||||
|
||||
public class NotificationFlushHandler(AppDatabase db) : IFlushHandler<Notification>
|
||||
{
|
||||
public async Task FlushAsync(IReadOnlyList<Notification> items)
|
||||
{
|
||||
await db.BulkInsertAsync(items.Select(x =>
|
||||
{
|
||||
x.CreatedAt = SystemClock.Instance.GetCurrentInstant();
|
||||
x.UpdatedAt = x.CreatedAt;
|
||||
return x;
|
||||
}), config => config.ConflictOption = ConflictOption.Ignore);
|
||||
}
|
||||
}
|
||||
|
||||
public class NotificationFlushJob(FlushBufferService fbs, NotificationFlushHandler hdl) : IJob
|
||||
{
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
await fbs.FlushAsync(hdl);
|
||||
}
|
||||
}
|
@@ -124,7 +124,7 @@ public class PushService
|
||||
if (actionUri is not null) meta["action_uri"] = actionUri;
|
||||
|
||||
var accountId = account.Id;
|
||||
var notification = new Notification
|
||||
var notification = new SnNotification
|
||||
{
|
||||
Topic = topic,
|
||||
Title = title,
|
||||
@@ -144,7 +144,7 @@ public class PushService
|
||||
_ = _queueService.EnqueuePushNotification(notification, Guid.Parse(accountId), save);
|
||||
}
|
||||
|
||||
public async Task DeliverPushNotification(Notification notification, CancellationToken cancellationToken = default)
|
||||
public async Task DeliverPushNotification(SnNotification notification, CancellationToken cancellationToken = default)
|
||||
{
|
||||
WebSocketService.SendPacketToAccount(notification.AccountId, new WebSocketPacket()
|
||||
{
|
||||
@@ -194,7 +194,7 @@ public class PushService
|
||||
}
|
||||
}
|
||||
|
||||
public async Task MarkNotificationsViewed(ICollection<Notification> notifications)
|
||||
public async Task MarkNotificationsViewed(ICollection<SnNotification> notifications)
|
||||
{
|
||||
var now = SystemClock.Instance.GetCurrentInstant();
|
||||
var id = notifications.Where(n => n.ViewedAt == null).Select(n => n.Id).ToList();
|
||||
@@ -214,12 +214,12 @@ public class PushService
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(n => n.ViewedAt, now));
|
||||
}
|
||||
|
||||
public async Task SendNotificationBatch(Notification notification, List<Guid> accounts, bool save = false)
|
||||
public async Task SendNotificationBatch(SnNotification notification, List<Guid> accounts, bool save = false)
|
||||
{
|
||||
if (save)
|
||||
{
|
||||
var now = SystemClock.Instance.GetCurrentInstant();
|
||||
var notifications = accounts.Select(accountId => new Notification
|
||||
var notifications = accounts.Select(accountId => new SnNotification
|
||||
{
|
||||
Topic = notification.Topic,
|
||||
Title = notification.Title,
|
||||
@@ -260,7 +260,7 @@ public class PushService
|
||||
await DeliverPushNotification(notification);
|
||||
}
|
||||
|
||||
private async Task SendPushNotificationAsync(SnNotificationPushSubscription subscription, Notification notification)
|
||||
private async Task SendPushNotificationAsync(SnNotificationPushSubscription subscription, SnNotification notification)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -358,15 +358,15 @@ public class PushService
|
||||
$"Successfully pushed notification #{notification.Id} to device {subscription.DeviceId} provider {subscription.Provider}");
|
||||
}
|
||||
|
||||
public async Task SaveNotification(Notification notification)
|
||||
public async Task SaveNotification(SnNotification notification)
|
||||
{
|
||||
_db.Notifications.Add(notification);
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task SaveNotification(Notification notification, List<Guid> accounts)
|
||||
public async Task SaveNotification(SnNotification notification, List<Guid> accounts)
|
||||
{
|
||||
_db.Notifications.AddRange(accounts.Select(a => new Notification
|
||||
_db.Notifications.AddRange(accounts.Select(a => new SnNotification
|
||||
{
|
||||
AccountId = a,
|
||||
Topic = notification.Topic,
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using DysonNetwork.Ring.Connection;
|
||||
using DysonNetwork.Ring.Notification;
|
||||
using DysonNetwork.Shared.Models;
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using Grpc.Core;
|
||||
@@ -65,7 +66,7 @@ public override Task<Empty> PushWebSocketPacketToDevice(PushWebSocketPacketToDev
|
||||
public override async Task<Empty> SendPushNotificationToUser(SendPushNotificationToUserRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
var notification = new Notification.Notification
|
||||
var notification = new SnNotification
|
||||
{
|
||||
Topic = request.Notification.Topic,
|
||||
Title = request.Notification.Title,
|
||||
@@ -95,7 +96,7 @@ public override Task<Empty> PushWebSocketPacketToDevice(PushWebSocketPacketToDev
|
||||
public override async Task<Empty> SendPushNotificationToUsers(SendPushNotificationToUsersRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
var notification = new Notification.Notification
|
||||
var notification = new SnNotification
|
||||
{
|
||||
Topic = request.Notification.Topic,
|
||||
Title = request.Notification.Title,
|
||||
|
@@ -1,7 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using NATS.Client.Core;
|
||||
using NATS.Client.JetStream;
|
||||
using NATS.Net;
|
||||
|
||||
namespace DysonNetwork.Ring.Services;
|
||||
|
@@ -15,13 +15,6 @@ public static class ScheduledJobsConfiguration
|
||||
.ForJob(appDatabaseRecyclingJob)
|
||||
.WithIdentity("AppDatabaseRecyclingTrigger")
|
||||
.WithCronSchedule("0 0 0 * * ?"));
|
||||
|
||||
var notificationFlushJob = new JobKey("NotificationFlush");
|
||||
q.AddJob<NotificationFlushJob>(opts => opts.WithIdentity(notificationFlushJob));
|
||||
q.AddTrigger(opts => opts
|
||||
.ForJob(notificationFlushJob)
|
||||
.WithIdentity("NotificationFlushTrigger")
|
||||
.WithSimpleSchedule(a => a.WithIntervalInSeconds(60).RepeatForever()));
|
||||
});
|
||||
services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
|
||||
|
||||
|
@@ -1,8 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.RateLimiting;
|
||||
using CorePush.Apple;
|
||||
using CorePush.Firebase;
|
||||
using DysonNetwork.Ring.Connection;
|
||||
using DysonNetwork.Ring.Email;
|
||||
using DysonNetwork.Ring.Notification;
|
||||
@@ -11,7 +9,6 @@ using DysonNetwork.Shared.Cache;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using NodaTime;
|
||||
using NodaTime.Serialization.SystemTextJson;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace DysonNetwork.Ring.Startup;
|
||||
|
||||
@@ -75,7 +72,6 @@ public static class ServiceCollectionExtensions
|
||||
public static IServiceCollection AddAppFlushHandlers(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<FlushBufferService>();
|
||||
services.AddScoped<NotificationFlushHandler>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
Reference in New Issue
Block a user