chat: add database types and auth middleware
This commit is contained in:
parent
656eec81d8
commit
6f6e19bbb5
24 changed files with 1165 additions and 15 deletions
6
Foxchat.Chat/Database/BaseModel.cs
Normal file
6
Foxchat.Chat/Database/BaseModel.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace Foxchat.Chat.Database;
|
||||
|
||||
public abstract class BaseModel
|
||||
{
|
||||
public Ulid Id { get; init; } = Ulid.NewUlid();
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
using Foxchat.Chat.Database.Models;
|
||||
using Foxchat.Core;
|
||||
using Foxchat.Core.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
@ -11,6 +12,11 @@ public class ChatContext : IDatabaseContext
|
|||
private readonly NpgsqlDataSource _dataSource;
|
||||
|
||||
public override DbSet<Instance> Instance { get; set; }
|
||||
public DbSet<IdentityInstance> IdentityInstances { get; set; }
|
||||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<Guild> Guilds { get; set; }
|
||||
public DbSet<Channel> Channels { get; set; }
|
||||
public DbSet<Message> Messages { get; set; }
|
||||
|
||||
public ChatContext(InstanceConfig config)
|
||||
{
|
||||
|
@ -26,9 +32,9 @@ public class ChatContext : IDatabaseContext
|
|||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> optionsBuilder
|
||||
.UseNpgsql(_dataSource, o => o.UseNodaTime())
|
||||
.UseSnakeCaseNamingConvention();
|
||||
=> optionsBuilder
|
||||
.UseNpgsql(_dataSource, o => o.UseNodaTime())
|
||||
.UseSnakeCaseNamingConvention();
|
||||
|
||||
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
|
||||
{
|
||||
|
@ -38,20 +44,35 @@ public class ChatContext : IDatabaseContext
|
|||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<IdentityInstance>().HasIndex(i => i.Domain).IsUnique();
|
||||
|
||||
modelBuilder.Entity<User>().HasIndex(u => new { u.RemoteUserId, u.InstanceId }).IsUnique();
|
||||
modelBuilder.Entity<User>().HasIndex(u => new { u.Username, u.InstanceId }).IsUnique();
|
||||
|
||||
modelBuilder.Entity<Guild>()
|
||||
.HasOne(e => e.Owner)
|
||||
.WithMany(e => e.OwnedGuilds)
|
||||
.HasForeignKey(e => e.OwnerId)
|
||||
.IsRequired();
|
||||
|
||||
modelBuilder.Entity<User>()
|
||||
.HasMany(e => e.Guilds)
|
||||
.WithMany(e => e.Users);
|
||||
}
|
||||
}
|
||||
|
||||
// ReSharper disable once UnusedType.Global
|
||||
public class DesignTimeIdentityContextFactory : IDesignTimeDbContextFactory<ChatContext>
|
||||
{
|
||||
public ChatContext CreateDbContext(string[] args)
|
||||
{
|
||||
// Read the configuration file
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddConfiguration("identity.ini")
|
||||
.AddConfiguration("chat.ini")
|
||||
.Build()
|
||||
// Get the configuration as our config class
|
||||
.Get<InstanceConfig>() ?? new();
|
||||
|
||||
return new ChatContext(config);
|
||||
}
|
||||
}
|
||||
}
|
9
Foxchat.Chat/Database/Models/Channel.cs
Normal file
9
Foxchat.Chat/Database/Models/Channel.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
namespace Foxchat.Chat.Database.Models;
|
||||
|
||||
public class Channel : BaseModel
|
||||
{
|
||||
public Ulid GuildId { get; init; }
|
||||
public Guild Guild { get; init; } = null!;
|
||||
public string Name { get; set; } = null!;
|
||||
public string? Topic { get; set; }
|
||||
}
|
11
Foxchat.Chat/Database/Models/Guild.cs
Normal file
11
Foxchat.Chat/Database/Models/Guild.cs
Normal file
|
@ -0,0 +1,11 @@
|
|||
namespace Foxchat.Chat.Database.Models;
|
||||
|
||||
public class Guild : BaseModel
|
||||
{
|
||||
public string Name { get; set; } = null!;
|
||||
public Ulid OwnerId { get; set; }
|
||||
public User Owner { get; set; } = null!;
|
||||
|
||||
public List<User> Users { get; } = [];
|
||||
public List<Channel> Channels { get; } = [];
|
||||
}
|
17
Foxchat.Chat/Database/Models/IdentityInstance.cs
Normal file
17
Foxchat.Chat/Database/Models/IdentityInstance.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
namespace Foxchat.Chat.Database.Models;
|
||||
|
||||
public class IdentityInstance : BaseModel
|
||||
{
|
||||
public string Domain { get; init; } = null!;
|
||||
public string BaseUrl { get; init; } = null!;
|
||||
public string PublicKey { get; init; } = null!;
|
||||
|
||||
public InstanceStatus Status { get; set; } = InstanceStatus.Active;
|
||||
public string? Reason { get; set; }
|
||||
|
||||
public enum InstanceStatus
|
||||
{
|
||||
Active,
|
||||
Suspended,
|
||||
}
|
||||
}
|
15
Foxchat.Chat/Database/Models/Message.cs
Normal file
15
Foxchat.Chat/Database/Models/Message.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
using NodaTime;
|
||||
|
||||
namespace Foxchat.Chat.Database.Models;
|
||||
|
||||
public class Message : BaseModel
|
||||
{
|
||||
public Ulid ChannelId { get; init; }
|
||||
public Channel Channel { get; init; } = null!;
|
||||
public Ulid AuthorId { get; init; }
|
||||
public User Author { get; init; } = null!;
|
||||
|
||||
public string? Content { get; set; }
|
||||
|
||||
public Instant? UpdatedAt { get; set; }
|
||||
}
|
14
Foxchat.Chat/Database/Models/User.cs
Normal file
14
Foxchat.Chat/Database/Models/User.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
namespace Foxchat.Chat.Database.Models;
|
||||
|
||||
public class User : BaseModel
|
||||
{
|
||||
public Ulid InstanceId { get; init; }
|
||||
public IdentityInstance Instance { get; init; } = null!;
|
||||
public string RemoteUserId { get; init; } = null!;
|
||||
public string Username { get; init; } = null!;
|
||||
|
||||
public string? Avatar { get; set; }
|
||||
|
||||
public List<Guild> Guilds { get; } = [];
|
||||
public List<Guild> OwnedGuilds { get; } = [];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue