Add primitive groups and required channel

Adds server-side group and channel management, private invitations, member moderation, avatar handling, and required-channel configuration. This change also introduces SignalR group profile/member notifications, updates registration to atomically join required channels, and adds EF migration and test coverage for group membership, permissions, and invitation expiry rules.
This commit is contained in:
Artemy
2026-10-04 10:35:07 +07:00
parent fed8f518ee
commit 2e9d7e625e
48 changed files with 3593 additions and 167 deletions
@@ -12,9 +12,11 @@ public class ChatGroupConfigurator : IEntityTypeConfiguration<ChatGroup>
builder.Property(e => e.Name).IsRequired().HasMaxLength(100);
builder.Property(e => e.Description).HasMaxLength(500);
builder.HasOne<Govor.Domain.Models.Users.User>().WithMany()
.HasForeignKey(e => e.OwnerUserId).OnDelete(DeleteBehavior.Restrict);
builder.HasMany(e => e.Members)
.WithOne()
.WithOne(e => e.ChatGroup)
.HasForeignKey(e => e.GroupId)
.OnDelete(DeleteBehavior.Cascade);
@@ -28,4 +30,4 @@ public class ChatGroupConfigurator : IEntityTypeConfiguration<ChatGroup>
.HasForeignKey(e => e.GroupId)
.OnDelete(DeleteBehavior.Cascade);
}
}
}
@@ -9,8 +9,11 @@ public class GroupAdminsConfiguration : IEntityTypeConfiguration<GroupAdmins>
public void Configure(EntityTypeBuilder<GroupAdmins> builder)
{
builder.HasKey(e => e.Id);
builder.HasIndex(e => new { e.GroupId, e.UserId }).IsUnique();
builder.HasOne<Govor.Domain.Models.Users.User>().WithMany()
.HasForeignKey(e => e.UserId).OnDelete(DeleteBehavior.Cascade);
builder.Property(e => e.UserId).IsRequired();
builder.Property(e => e.GroupId).IsRequired();
}
}
}
@@ -11,6 +11,7 @@ public class GroupInvitationConfiguration : IEntityTypeConfiguration<GroupInvita
builder.HasKey(e => e.Id);
builder.Property(e => e.InvitationCode).IsRequired().HasMaxLength(200);
builder.HasIndex(e => e.InvitationCode).IsUnique();
builder.Property(e => e.Description).HasMaxLength(500);
builder.Property(e => e.EndDate).IsRequired();
builder.Property(e => e.CreatedAt).IsRequired();
@@ -23,4 +24,4 @@ public class GroupInvitationConfiguration : IEntityTypeConfiguration<GroupInvita
builder.Ignore(e => e.GroupMemberships);
}
}
}
@@ -9,13 +9,16 @@ public class GroupMembershipConfiguration : IEntityTypeConfiguration<GroupMember
public void Configure(EntityTypeBuilder<GroupMembership> builder)
{
builder.HasKey(e => e.Id);
builder.HasIndex(e => new { e.GroupId, e.UserId }).IsUnique();
builder.HasOne<Govor.Domain.Models.Users.User>().WithMany()
.HasForeignKey(e => e.UserId).OnDelete(DeleteBehavior.Cascade);
builder.Property(e => e.UserId).IsRequired();
builder.Property(e => e.GroupId).IsRequired();
builder.Property(e => e.InvitationId).IsRequired(false);
builder.HasOne(e => e.ChatGroup)
.WithMany()
.WithMany(g => g.Members)
.HasForeignKey(e => e.GroupId)
.OnDelete(DeleteBehavior.Cascade);
@@ -26,4 +29,4 @@ public class GroupMembershipConfiguration : IEntityTypeConfiguration<GroupMember
.OnDelete(DeleteBehavior.SetNull);
}
}
}
@@ -0,0 +1,16 @@
using Govor.Domain.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Govor.Domain.Configurations;
public class ServerCommunitySettingsConfiguration : IEntityTypeConfiguration<ServerCommunitySettings>
{
public void Configure(EntityTypeBuilder<ServerCommunitySettings> builder)
{
builder.HasKey(s => s.Id);
builder.Property(s => s.Id).ValueGeneratedNever();
builder.HasOne<ChatGroup>().WithMany().HasForeignKey(s => s.RequiredChannelId).OnDelete(DeleteBehavior.Restrict);
builder.HasData(new ServerCommunitySettings { Id = 1, AllowLeave = true });
}
}
+2
View File
@@ -37,6 +37,7 @@ public class GovorDbContext(DbContextOptions<GovorDbContext> options) : DbContex
public virtual DbSet<GroupInvitation> GroupInvitations { get; set; }
public virtual DbSet<GroupMembership> GroupMemberships { get; set; }
public virtual DbSet<GroupAdmins> GroupAdmins { get; set; }
public virtual DbSet<ServerCommunitySettings> ServerCommunitySettings { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@@ -59,6 +60,7 @@ public class GovorDbContext(DbContextOptions<GovorDbContext> options) : DbContex
modelBuilder.ApplyConfiguration(new GroupInvitationConfiguration());
modelBuilder.ApplyConfiguration(new GroupMembershipConfiguration());
modelBuilder.ApplyConfiguration(new GroupAdminsConfiguration());
modelBuilder.ApplyConfiguration(new ServerCommunitySettingsConfiguration());
modelBuilder.ApplyConfiguration(new OneTimePreKeyConfiguration());
modelBuilder.ApplyConfiguration(new UserCryptoSessionConfiguration());
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,267 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Govor.Domain.Migrations
{
/// <inheritdoc />
public partial class PrimitiveGroupsAndRequiredChannel : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_GroupMemberships_ChatGroups_ChatGroupId",
table: "GroupMemberships");
migrationBuilder.DropIndex(
name: "IX_GroupMemberships_ChatGroupId",
table: "GroupMemberships");
migrationBuilder.DropIndex(
name: "IX_GroupMemberships_GroupId",
table: "GroupMemberships");
migrationBuilder.DropIndex(
name: "IX_GroupAdmins_GroupId",
table: "GroupAdmins");
migrationBuilder.DropColumn(
name: "ChatGroupId",
table: "GroupMemberships");
migrationBuilder.AddColumn<bool>(
name: "IsRevoked",
table: "GroupInvitations",
type: "boolean",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<int>(
name: "UsedCount",
table: "GroupInvitations",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<DateTime>(
name: "CreatedAt",
table: "ChatGroups",
type: "timestamp with time zone",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
migrationBuilder.AddColumn<Guid>(
name: "OwnerUserId",
table: "ChatGroups",
type: "uuid",
nullable: true);
migrationBuilder.CreateTable(
name: "ServerCommunitySettings",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false),
RequiredChannelId = table.Column<Guid>(type: "uuid", nullable: true),
AllowLeave = table.Column<bool>(type: "boolean", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ServerCommunitySettings", x => x.Id);
table.ForeignKey(
name: "FK_ServerCommunitySettings_ChatGroups_RequiredChannelId",
column: x => x.RequiredChannelId,
principalTable: "ChatGroups",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.InsertData(
table: "ServerCommunitySettings",
columns: new[] { "Id", "AllowLeave", "RequiredChannelId" },
values: new object[] { 1, true, null });
// Preserve bans when consolidating legacy duplicate memberships; remove orphan rows
// before adding user foreign keys. Invitation codes that were ambiguous are revoked.
migrationBuilder.Sql("""
DELETE FROM "GroupMemberships" WHERE "UserId" NOT IN (SELECT "Id" FROM "Users");
DELETE FROM "GroupAdmins" WHERE "UserId" NOT IN (SELECT "Id" FROM "Users");
DELETE FROM "GroupMemberships" WHERE "Id" IN (
SELECT "Id" FROM (SELECT "Id", ROW_NUMBER() OVER (
PARTITION BY "GroupId", "UserId" ORDER BY "IsBanned" DESC, "MemberSince", "Id") AS rn
FROM "GroupMemberships") AS duplicates WHERE rn > 1);
DELETE FROM "GroupAdmins" WHERE "Id" IN (
SELECT "Id" FROM (SELECT "Id", ROW_NUMBER() OVER (
PARTITION BY "GroupId", "UserId" ORDER BY "Id") AS rn
FROM "GroupAdmins") AS duplicates WHERE rn > 1);
DELETE FROM "GroupAdmins" AS a WHERE NOT EXISTS (
SELECT 1 FROM "GroupMemberships" AS m WHERE m."GroupId" = a."GroupId"
AND m."UserId" = a."UserId" AND NOT m."IsBanned");
UPDATE "GroupInvitations" SET "IsRevoked" = TRUE,
"InvitationCode" = REPLACE(CAST("Id" AS TEXT), '-', '')
WHERE "InvitationCode" IN (SELECT "InvitationCode" FROM "GroupInvitations"
GROUP BY "InvitationCode" HAVING COUNT(*) > 1);
UPDATE "GroupInvitations" AS i SET "UsedCount" = (
SELECT COUNT(*) FROM "GroupMemberships" AS m WHERE m."InvitationId" = i."Id");
UPDATE "ChatGroups" AS g SET "OwnerUserId" = (
SELECT m."UserId" FROM "GroupMemberships" AS m
WHERE m."GroupId" = g."Id" AND NOT m."IsBanned"
ORDER BY CASE WHEN EXISTS (SELECT 1 FROM "GroupAdmins" AS a
WHERE a."GroupId" = m."GroupId" AND a."UserId" = m."UserId") THEN 0 ELSE 1 END,
m."MemberSince", m."UserId" LIMIT 1);
UPDATE "ChatGroups" AS g SET "CreatedAt" = COALESCE((
SELECT MIN(m."MemberSince") FROM "GroupMemberships" AS m
WHERE m."GroupId" = g."Id"), CURRENT_TIMESTAMP);
""");
migrationBuilder.CreateIndex(
name: "IX_GroupMemberships_GroupId_UserId",
table: "GroupMemberships",
columns: new[] { "GroupId", "UserId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_GroupMemberships_UserId",
table: "GroupMemberships",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_GroupInvitations_InvitationCode",
table: "GroupInvitations",
column: "InvitationCode",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_GroupAdmins_GroupId_UserId",
table: "GroupAdmins",
columns: new[] { "GroupId", "UserId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_GroupAdmins_UserId",
table: "GroupAdmins",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_ChatGroups_OwnerUserId",
table: "ChatGroups",
column: "OwnerUserId");
migrationBuilder.CreateIndex(
name: "IX_ServerCommunitySettings_RequiredChannelId",
table: "ServerCommunitySettings",
column: "RequiredChannelId");
migrationBuilder.AddForeignKey(
name: "FK_ChatGroups_Users_OwnerUserId",
table: "ChatGroups",
column: "OwnerUserId",
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_GroupAdmins_Users_UserId",
table: "GroupAdmins",
column: "UserId",
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_GroupMemberships_Users_UserId",
table: "GroupMemberships",
column: "UserId",
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_ChatGroups_Users_OwnerUserId",
table: "ChatGroups");
migrationBuilder.DropForeignKey(
name: "FK_GroupAdmins_Users_UserId",
table: "GroupAdmins");
migrationBuilder.DropForeignKey(
name: "FK_GroupMemberships_Users_UserId",
table: "GroupMemberships");
migrationBuilder.DropTable(
name: "ServerCommunitySettings");
migrationBuilder.DropIndex(
name: "IX_GroupMemberships_GroupId_UserId",
table: "GroupMemberships");
migrationBuilder.DropIndex(
name: "IX_GroupMemberships_UserId",
table: "GroupMemberships");
migrationBuilder.DropIndex(
name: "IX_GroupInvitations_InvitationCode",
table: "GroupInvitations");
migrationBuilder.DropIndex(
name: "IX_GroupAdmins_GroupId_UserId",
table: "GroupAdmins");
migrationBuilder.DropIndex(
name: "IX_GroupAdmins_UserId",
table: "GroupAdmins");
migrationBuilder.DropIndex(
name: "IX_ChatGroups_OwnerUserId",
table: "ChatGroups");
migrationBuilder.DropColumn(
name: "IsRevoked",
table: "GroupInvitations");
migrationBuilder.DropColumn(
name: "UsedCount",
table: "GroupInvitations");
migrationBuilder.DropColumn(
name: "CreatedAt",
table: "ChatGroups");
migrationBuilder.DropColumn(
name: "OwnerUserId",
table: "ChatGroups");
migrationBuilder.AddColumn<Guid>(
name: "ChatGroupId",
table: "GroupMemberships",
type: "uuid",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_GroupMemberships_ChatGroupId",
table: "GroupMemberships",
column: "ChatGroupId");
migrationBuilder.CreateIndex(
name: "IX_GroupMemberships_GroupId",
table: "GroupMemberships",
column: "GroupId");
migrationBuilder.CreateIndex(
name: "IX_GroupAdmins_GroupId",
table: "GroupAdmins",
column: "GroupId");
migrationBuilder.AddForeignKey(
name: "FK_GroupMemberships_ChatGroups_ChatGroupId",
table: "GroupMemberships",
column: "ChatGroupId",
principalTable: "ChatGroups",
principalColumn: "Id");
}
}
}
@@ -28,6 +28,9 @@ namespace Govor.Domain.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("Description")
.IsRequired()
.HasMaxLength(500)
@@ -47,6 +50,9 @@ namespace Govor.Domain.Migrations
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.Property<Guid?>("OwnerUserId")
.HasColumnType("uuid");
b.Property<int>("ReactionMode")
.HasColumnType("integer");
@@ -55,6 +61,8 @@ namespace Govor.Domain.Migrations
b.HasKey("Id");
b.HasIndex("OwnerUserId");
b.ToTable("ChatGroups");
});
@@ -96,7 +104,10 @@ namespace Govor.Domain.Migrations
b.HasKey("Id");
b.HasIndex("GroupId");
b.HasIndex("UserId");
b.HasIndex("GroupId", "UserId")
.IsUnique();
b.ToTable("GroupAdmins");
});
@@ -126,9 +137,15 @@ namespace Govor.Domain.Migrations
.HasMaxLength(200)
.HasColumnType("character varying(200)");
b.Property<bool>("IsRevoked")
.HasColumnType("boolean");
b.Property<int>("MaxParticipants")
.HasColumnType("integer");
b.Property<int>("UsedCount")
.HasColumnType("integer");
b.Property<Guid>("UserMakerId")
.HasColumnType("uuid");
@@ -136,6 +153,9 @@ namespace Govor.Domain.Migrations
b.HasIndex("GroupId");
b.HasIndex("InvitationCode")
.IsUnique();
b.HasIndex("UserMakerId");
b.ToTable("GroupInvitations");
@@ -147,9 +167,6 @@ namespace Govor.Domain.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<Guid?>("ChatGroupId")
.HasColumnType("uuid");
b.Property<Guid>("GroupId")
.HasColumnType("uuid");
@@ -167,12 +184,13 @@ namespace Govor.Domain.Migrations
b.HasKey("Id");
b.HasIndex("ChatGroupId");
b.HasIndex("GroupId");
b.HasIndex("InvitationId");
b.HasIndex("UserId");
b.HasIndex("GroupId", "UserId")
.IsUnique();
b.ToTable("GroupMemberships");
});
@@ -744,6 +762,31 @@ namespace Govor.Domain.Migrations
b.ToTable("UserReactionPacks");
});
modelBuilder.Entity("Govor.Domain.Models.ServerCommunitySettings", b =>
{
b.Property<int>("Id")
.HasColumnType("integer");
b.Property<bool>("AllowLeave")
.HasColumnType("boolean");
b.Property<Guid?>("RequiredChannelId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("RequiredChannelId");
b.ToTable("ServerCommunitySettings");
b.HasData(
new
{
Id = 1,
AllowLeave = true
});
});
modelBuilder.Entity("Govor.Domain.Models.Users.Admin", b =>
{
b.Property<Guid>("UserId")
@@ -965,6 +1008,14 @@ namespace Govor.Domain.Migrations
b.ToTable("UserSessions");
});
modelBuilder.Entity("Govor.Domain.Models.ChatGroup", b =>
{
b.HasOne("Govor.Domain.Models.Users.User", null)
.WithMany()
.HasForeignKey("OwnerUserId")
.OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity("Govor.Domain.Models.Friendship", b =>
{
b.HasOne("Govor.Domain.Models.Users.User", "Addressee")
@@ -991,6 +1042,12 @@ namespace Govor.Domain.Migrations
.HasForeignKey("GroupId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Govor.Domain.Models.Users.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Govor.Domain.Models.GroupInvitation", b =>
@@ -1012,12 +1069,8 @@ namespace Govor.Domain.Migrations
modelBuilder.Entity("Govor.Domain.Models.GroupMembership", b =>
{
b.HasOne("Govor.Domain.Models.ChatGroup", null)
.WithMany("Members")
.HasForeignKey("ChatGroupId");
b.HasOne("Govor.Domain.Models.ChatGroup", "ChatGroup")
.WithMany()
.WithMany("Members")
.HasForeignKey("GroupId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
@@ -1027,6 +1080,12 @@ namespace Govor.Domain.Migrations
.HasForeignKey("InvitationId")
.OnDelete(DeleteBehavior.SetNull);
b.HasOne("Govor.Domain.Models.Users.User", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("ChatGroup");
});
@@ -1156,6 +1215,14 @@ namespace Govor.Domain.Migrations
.IsRequired();
});
modelBuilder.Entity("Govor.Domain.Models.ServerCommunitySettings", b =>
{
b.HasOne("Govor.Domain.Models.ChatGroup", null)
.WithMany()
.HasForeignKey("RequiredChannelId")
.OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity("Govor.Domain.Models.Users.Admin", b =>
{
b.HasOne("Govor.Domain.Models.Users.User", "User")
+2
View File
@@ -12,6 +12,8 @@ public class ChatGroup
public Govor.Domain.Models.Reactions.ChannelReactionMode ReactionMode { get; set; }
public long ReactionPolicyVersion { get; set; }
public bool IsPrivate { get; set; }
public Guid? OwnerUserId { get; set; }
public DateTime CreatedAt { get; set; }
public List<GroupAdmins> Admins { get; set; } = new();
public List<GroupMembership> Members { get; set; } = new();
public List<GroupInvitation> InviteCodes { get; set; } = new();
+3 -1
View File
@@ -12,6 +12,8 @@ public class GroupInvitation
public DateTime EndDate { get; set; }
public DateTime CreatedAt { get; set; }
public int MaxParticipants { get; set; }
public int UsedCount { get; set; }
public bool IsRevoked { get; set; }
public List<Guid> GroupMemberships { get; set; } = new();
public User? UserMaker { get; set; }
}
}
@@ -0,0 +1,10 @@
namespace Govor.Domain.Models;
public enum GroupRole { Member, Admin, Owner }
public class ServerCommunitySettings
{
public int Id { get; set; } = 1;
public Guid? RequiredChannelId { get; set; }
public bool AllowLeave { get; set; } = true;
}