* some groups stuff * further groups rework * fixes for pause changes * adjsut timeout interval * fixes and namespace change to file scoped * more fixes * further implement groups * fix change group ownership * add some more stuff for groups * more fixes and additions * some fixes based on analyzers, add shard info to ui * add discord command, cleanup * fix regex * add group migration and deletion on user deletion * add api method for client to check health of connection * adjust regex for vanity * fixes for server and bot * fixes some string comparison in linq queries * fixes group leave and sets alias to null * fix syntax in changeownership * add better logging, fixes for group leaving * fixes for group leave Co-authored-by: Stanley Dimant <root.darkarchon@outlook.com>
53 lines
1.7 KiB
C#
53 lines
1.7 KiB
C#
using MareSynchronosShared.Data;
|
|
using MareSynchronosShared.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MareSynchronosShared.Utils;
|
|
|
|
public static class SharedDbFunctions
|
|
{
|
|
public static async Task<(bool, string)> MigrateOrDeleteGroup(MareDbContext context, Group group, List<GroupPair> groupPairs, int maxGroupsByUser)
|
|
{
|
|
bool groupHasMigrated = false;
|
|
string newOwner = string.Empty;
|
|
foreach (var potentialNewOwner in groupPairs)
|
|
{
|
|
groupHasMigrated = await TryMigrateGroup(context, group, potentialNewOwner.GroupUserUID, maxGroupsByUser).ConfigureAwait(false);
|
|
|
|
if (groupHasMigrated)
|
|
{
|
|
newOwner = potentialNewOwner.GroupUserUID;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!groupHasMigrated)
|
|
{
|
|
context.GroupPairs.RemoveRange(groupPairs);
|
|
context.Groups.Remove(group);
|
|
|
|
await context.SaveChangesAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
return (groupHasMigrated, newOwner);
|
|
}
|
|
|
|
private static async Task<bool> TryMigrateGroup(MareDbContext context, Group group, string potentialNewOwnerUid, int maxGroupsByUser)
|
|
{
|
|
var newOwnerOwnedGroups = await context.Groups.CountAsync(g => g.OwnerUID == potentialNewOwnerUid).ConfigureAwait(false);
|
|
if (newOwnerOwnedGroups >= maxGroupsByUser)
|
|
{
|
|
return false;
|
|
}
|
|
group.OwnerUID = potentialNewOwnerUid;
|
|
group.Alias = null;
|
|
await context.SaveChangesAsync().ConfigureAwait(false);
|
|
return true;
|
|
}
|
|
}
|