1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-05 10:02:47 -05:00

[PM-10560] Create notification database storage (#4688)

* Add new tables

* Add stored procedures

* Add core entities and models

* Setup EF

* Add repository interfaces

* Add dapper repos

* Add EF repos

* Add order by

* EF updates

* PM-10560: Notifications repository matching requirements.

* PM-10560: Notifications repository matching requirements.

* PM-10560: Migration scripts

* PM-10560: EF index optimizations

* PM-10560: Cleanup

* PM-10560: Priority in natural order, Repository, sql simplifications

* PM-10560: Title column update

* PM-10560: Incorrect EF migration removal

* PM-10560: EF migrations

* PM-10560: Added views, SP naming simplification

* PM-10560: Notification entity Title update, EF migrations

* PM-10560: Removing Notification_ReadByUserId

* PM-10560: Notification ReadByUserIdAndStatus fix

* PM-10560: Notification ReadByUserIdAndStatus fix to be in line with requirements and EF

---------

Co-authored-by: Maciej Zieniuk <mzieniuk@bitwarden.com>
Co-authored-by: Matt Bishop <mbishop@bitwarden.com>
This commit is contained in:
Thomas Avery
2024-09-09 14:52:12 -05:00
committed by GitHub
parent 55bf815050
commit 4c0f8d54f3
39 changed files with 9983 additions and 0 deletions

View File

@ -2,6 +2,7 @@
using Bit.Core.Auth.Repositories;
using Bit.Core.Billing.Repositories;
using Bit.Core.Enums;
using Bit.Core.NotificationCenter.Repositories;
using Bit.Core.Repositories;
using Bit.Core.SecretsManager.Repositories;
using Bit.Core.Tools.Repositories;
@ -9,6 +10,7 @@ using Bit.Core.Vault.Repositories;
using Bit.Infrastructure.EntityFramework.AdminConsole.Repositories;
using Bit.Infrastructure.EntityFramework.Auth.Repositories;
using Bit.Infrastructure.EntityFramework.Billing.Repositories;
using Bit.Infrastructure.EntityFramework.NotificationCenter.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Infrastructure.EntityFramework.SecretsManager.Repositories;
using Bit.Infrastructure.EntityFramework.Tools.Repositories;
@ -89,6 +91,8 @@ public static class EntityFrameworkServiceCollectionExtensions
services.AddSingleton<IWebAuthnCredentialRepository, WebAuthnCredentialRepository>();
services.AddSingleton<IProviderPlanRepository, ProviderPlanRepository>();
services.AddSingleton<IProviderInvoiceItemRepository, ProviderInvoiceItemRepository>();
services.AddSingleton<INotificationRepository, NotificationRepository>();
services.AddSingleton<INotificationStatusRepository, NotificationStatusRepository>();
if (selfHosted)
{

View File

@ -0,0 +1,35 @@
#nullable enable
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Configurations;
public class NotificationEntityTypeConfiguration : IEntityTypeConfiguration<Notification>
{
public void Configure(EntityTypeBuilder<Notification> builder)
{
builder
.Property(n => n.Id)
.ValueGeneratedNever();
builder
.HasKey(n => n.Id)
.IsClustered();
builder
.HasIndex(n => new { n.ClientType, n.Global, n.UserId, n.OrganizationId, n.Priority, n.CreationDate })
.IsDescending(false, false, false, false, true, true)
.IsClustered(false);
builder
.HasIndex(n => n.OrganizationId)
.IsClustered(false);
builder
.HasIndex(n => n.UserId)
.IsClustered(false);
builder.ToTable(nameof(Notification));
}
}

View File

@ -0,0 +1,18 @@
#nullable enable
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Configurations;
public class NotificationStatusEntityTypeConfiguration : IEntityTypeConfiguration<NotificationStatus>
{
public void Configure(EntityTypeBuilder<NotificationStatus> builder)
{
builder
.HasKey(ns => new { ns.UserId, ns.NotificationId })
.IsClustered();
builder.ToTable(nameof(NotificationStatus));
}
}

View File

@ -0,0 +1,21 @@
using AutoMapper;
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
using Bit.Infrastructure.EntityFramework.Models;
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
public class Notification : Core.NotificationCenter.Entities.Notification
{
public virtual User User { get; set; }
public virtual Organization Organization { get; set; }
}
public class NotificationMapperProfile : Profile
{
public NotificationMapperProfile()
{
CreateMap<Core.NotificationCenter.Entities.Notification, Notification>()
.PreserveReferences()
.ReverseMap();
}
}

View File

@ -0,0 +1,20 @@
using AutoMapper;
using Bit.Infrastructure.EntityFramework.Models;
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
public class NotificationStatus : Core.NotificationCenter.Entities.NotificationStatus
{
public virtual Notification Notification { get; set; }
public virtual User User { get; set; }
}
public class NotificationStatusMapperProfile : Profile
{
public NotificationStatusMapperProfile()
{
CreateMap<Core.NotificationCenter.Entities.NotificationStatus, NotificationStatus>()
.PreserveReferences()
.ReverseMap();
}
}

View File

@ -0,0 +1,92 @@
#nullable enable
using AutoMapper;
using Bit.Core.NotificationCenter.Enums;
using Bit.Core.NotificationCenter.Models.Filter;
using Bit.Core.NotificationCenter.Repositories;
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
using Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Repositories;
public class NotificationRepository : Repository<Core.NotificationCenter.Entities.Notification, Notification, Guid>,
INotificationRepository
{
public NotificationRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
: base(serviceScopeFactory, mapper, context => context.Notifications)
{
}
public async Task<IEnumerable<Core.NotificationCenter.Entities.Notification>> GetByUserIdAsync(Guid userId,
ClientType clientType)
{
return await GetByUserIdAndStatusAsync(userId, clientType, new NotificationStatusFilter());
}
public async Task<IEnumerable<Core.NotificationCenter.Entities.Notification>> GetByUserIdAndStatusAsync(Guid userId,
ClientType clientType, NotificationStatusFilter? statusFilter)
{
await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);
var notificationQuery = BuildNotificationQuery(dbContext, userId, clientType);
if (statusFilter != null && (statusFilter.Read != null || statusFilter.Deleted != null))
{
notificationQuery = from n in notificationQuery
join ns in dbContext.NotificationStatuses on n.Id equals ns.NotificationId
where
ns.UserId == userId &&
(
statusFilter.Read == null ||
(statusFilter.Read == true ? ns.ReadDate != null : ns.ReadDate == null) ||
statusFilter.Deleted == null ||
(statusFilter.Deleted == true ? ns.DeletedDate != null : ns.DeletedDate == null)
)
select n;
}
var notifications = await notificationQuery
.OrderByDescending(n => n.Priority)
.ThenByDescending(n => n.CreationDate)
.ToListAsync();
return Mapper.Map<List<Core.NotificationCenter.Entities.Notification>>(notifications);
}
private static IQueryable<Notification> BuildNotificationQuery(DatabaseContext dbContext, Guid userId,
ClientType clientType)
{
var clientTypes = new[] { ClientType.All };
if (clientType != ClientType.All)
{
clientTypes = [ClientType.All, clientType];
}
return from n in dbContext.Notifications
join ou in dbContext.OrganizationUsers.Where(ou => ou.UserId == userId)
on n.OrganizationId equals ou.OrganizationId into grouping
from ou in grouping.DefaultIfEmpty()
where
clientTypes.Contains(n.ClientType) &&
(
(
n.Global &&
n.UserId == null &&
n.OrganizationId == null
) ||
(
!n.Global &&
n.UserId == userId &&
(n.OrganizationId == null || ou != null)
) ||
(
!n.Global &&
n.UserId == null &&
ou != null
)
)
select n;
}
}

View File

@ -0,0 +1,60 @@
#nullable enable
using AutoMapper;
using Bit.Core.NotificationCenter.Repositories;
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
using Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Repositories;
public class NotificationStatusRepository : BaseEntityFrameworkRepository, INotificationStatusRepository
{
public NotificationStatusRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper) : base(
serviceScopeFactory,
mapper)
{
}
public async Task<Bit.Core.NotificationCenter.Entities.NotificationStatus?> GetByNotificationIdAndUserIdAsync(Guid notificationId, Guid userId)
{
await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);
var entity = await dbContext.NotificationStatuses
.Where(ns =>
ns.NotificationId == notificationId && ns.UserId == userId)
.FirstOrDefaultAsync();
return Mapper.Map<Bit.Core.NotificationCenter.Entities.NotificationStatus?>(entity);
}
public async Task<Bit.Core.NotificationCenter.Entities.NotificationStatus> CreateAsync(Bit.Core.NotificationCenter.Entities.NotificationStatus notificationStatus)
{
await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);
var entity = Mapper.Map<NotificationStatus>(notificationStatus);
await dbContext.AddAsync(entity);
await dbContext.SaveChangesAsync();
return notificationStatus;
}
public async Task UpdateAsync(Bit.Core.NotificationCenter.Entities.NotificationStatus notificationStatus)
{
await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);
var entity = await dbContext.NotificationStatuses
.Where(ns =>
ns.NotificationId == notificationStatus.NotificationId && ns.UserId == notificationStatus.UserId)
.FirstOrDefaultAsync();
if (entity != null)
{
entity.DeletedDate = notificationStatus.DeletedDate;
entity.ReadDate = notificationStatus.ReadDate;
await dbContext.SaveChangesAsync();
}
}
}

View File

@ -5,6 +5,7 @@ using Bit.Infrastructure.EntityFramework.Auth.Models;
using Bit.Infrastructure.EntityFramework.Billing.Models;
using Bit.Infrastructure.EntityFramework.Converters;
using Bit.Infrastructure.EntityFramework.Models;
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
using Bit.Infrastructure.EntityFramework.SecretsManager.Models;
using Bit.Infrastructure.EntityFramework.Vault.Models;
using Microsoft.EntityFrameworkCore;
@ -71,6 +72,8 @@ public class DatabaseContext : DbContext
public DbSet<WebAuthnCredential> WebAuthnCredentials { get; set; }
public DbSet<ProviderPlan> ProviderPlans { get; set; }
public DbSet<ProviderInvoiceItem> ProviderInvoiceItems { get; set; }
public DbSet<Notification> Notifications { get; set; }
public DbSet<NotificationStatus> NotificationStatuses { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{