diff --git a/DysonNetwork.Sphere/AppDatabase.cs b/DysonNetwork.Sphere/AppDatabase.cs index 9d977fb..6079f64 100644 --- a/DysonNetwork.Sphere/AppDatabase.cs +++ b/DysonNetwork.Sphere/AppDatabase.cs @@ -50,6 +50,9 @@ public class AppDatabase( public DbSet Realms { get; set; } public DbSet RealmMembers { get; set; } + + public DbSet ChatRooms { get; set; } + public DbSet ChatMembers { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { @@ -165,7 +168,20 @@ public class AppDatabase( .WithMany(p => p.Members) .HasForeignKey(pm => pm.RealmId) .OnDelete(DeleteBehavior.Cascade); - modelBuilder.Entity() + modelBuilder.Entity() + .HasOne(pm => pm.Account) + .WithMany() + .HasForeignKey(pm => pm.AccountId) + .OnDelete(DeleteBehavior.Cascade); + + modelBuilder.Entity() + .HasKey(pm => new { pm.ChatRoom, pm.AccountId }); + modelBuilder.Entity() + .HasOne(pm => pm.ChatRoom) + .WithMany(p => p.Members) + .HasForeignKey(pm => pm.ChatRoomId) + .OnDelete(DeleteBehavior.Cascade); + modelBuilder.Entity() .HasOne(pm => pm.Account) .WithMany() .HasForeignKey(pm => pm.AccountId) diff --git a/DysonNetwork.Sphere/Chat/ChatController.cs b/DysonNetwork.Sphere/Chat/ChatController.cs new file mode 100644 index 0000000..0be8635 --- /dev/null +++ b/DysonNetwork.Sphere/Chat/ChatController.cs @@ -0,0 +1,15 @@ +using DysonNetwork.Sphere.Account; +using DysonNetwork.Sphere.Realm; +using DysonNetwork.Sphere.Chat; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using System.Threading.Tasks; + +namespace DysonNetwork.Sphere.Chat; + +[ApiController] +[Route("/chat")] +public class ChatController(AppDatabase db) : ControllerBase +{ +} \ No newline at end of file diff --git a/DysonNetwork.Sphere/Chat/ChatRoom.cs b/DysonNetwork.Sphere/Chat/ChatRoom.cs new file mode 100644 index 0000000..35540bb --- /dev/null +++ b/DysonNetwork.Sphere/Chat/ChatRoom.cs @@ -0,0 +1,48 @@ +using System.ComponentModel.DataAnnotations; +using System.Text.Json.Serialization; +using DysonNetwork.Sphere.Storage; +using NodaTime; + +namespace DysonNetwork.Sphere.Chat; + +public enum ChatRoomType +{ + Group, + DirectMessage +} + +public class ChatRoom : ModelBase +{ + public long Id { get; set; } + [MaxLength(1024)] public string Name { get; set; } = string.Empty; + [MaxLength(4096)] public string Description { get; set; } = string.Empty; + public ChatRoomType Type { get; set; } + public bool IsPublic { get; set; } + + public CloudFile? Picture { get; set; } + public CloudFile? Background { get; set; } + + [JsonIgnore] public ICollection Members { get; set; } = new List(); + + public long? RealmId { get; set; } + public Realm.Realm? Realm { get; set; } +} + +public enum ChatMemberRole +{ + Owner = 100, + Moderator = 50, + Normal = 0 +} + +public class ChatMember : ModelBase +{ + public long ChatRoomId { get; set; } + [JsonIgnore] public ChatRoom ChatRoom { get; set; } = null!; + public long AccountId { get; set; } + [JsonIgnore] public Account.Account Account { get; set; } = null!; + + public ChatMemberRole Role { get; set; } + public Instant? JoinedAt { get; set; } + public bool IsBot { get; set; } = false; +} \ No newline at end of file diff --git a/DysonNetwork.Sphere/Realm/Realm.cs b/DysonNetwork.Sphere/Realm/Realm.cs index 7e3995d..602cc30 100644 --- a/DysonNetwork.Sphere/Realm/Realm.cs +++ b/DysonNetwork.Sphere/Realm/Realm.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; using System.Text.Json.Serialization; +using DysonNetwork.Sphere.Chat; using DysonNetwork.Sphere.Storage; using Microsoft.EntityFrameworkCore; using NodaTime; @@ -22,9 +23,10 @@ public class Realm : ModelBase public CloudFile? Background { get; set; } [JsonIgnore] public ICollection Members { get; set; } = new List(); + [JsonIgnore] public ICollection ChatRooms { get; set; } = new List(); public long AccountId { get; set; } - [JsonIgnore] public Account.Account Account { get; set; } + [JsonIgnore] public Account.Account Account { get; set; } = null!; } public enum RealmMemberRole