109 lines
3.6 KiB
C#
109 lines
3.6 KiB
C#
using LightlessSyncShared.Data;
|
|
using LightlessSyncShared.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
public class PairService
|
|
{
|
|
private readonly IDbContextFactory<LightlessDbContext> _dbFactory;
|
|
private readonly ILogger<PairService> _logger;
|
|
|
|
public PairService(IDbContextFactory<LightlessDbContext> dbFactory, ILogger<PairService> logger)
|
|
{
|
|
_dbFactory = dbFactory;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<bool> TryAddPairAsync(string userUid, string otherUid)
|
|
{
|
|
if (userUid == otherUid || string.IsNullOrWhiteSpace(userUid) || string.IsNullOrWhiteSpace(otherUid))
|
|
return false;
|
|
|
|
await using var db = await _dbFactory.CreateDbContextAsync();
|
|
|
|
var user = await db.Users.SingleOrDefaultAsync(u => u.UID == userUid);
|
|
var other = await db.Users.SingleOrDefaultAsync(u => u.UID == otherUid);
|
|
|
|
if (user == null || other == null)
|
|
return false;
|
|
|
|
bool modified = false;
|
|
|
|
if (!await db.ClientPairs.AnyAsync(p => p.UserUID == userUid && p.OtherUserUID == otherUid))
|
|
{
|
|
db.ClientPairs.Add(new ClientPair
|
|
{
|
|
UserUID = userUid,
|
|
OtherUserUID = otherUid
|
|
});
|
|
modified = true;
|
|
}
|
|
|
|
if (!await db.ClientPairs.AnyAsync(p => p.UserUID == otherUid && p.OtherUserUID == userUid))
|
|
{
|
|
db.ClientPairs.Add(new ClientPair
|
|
{
|
|
UserUID = otherUid,
|
|
OtherUserUID = userUid
|
|
});
|
|
modified = true;
|
|
}
|
|
|
|
if (!await db.Permissions.AnyAsync(p => p.UserUID == userUid && p.OtherUserUID == otherUid))
|
|
{
|
|
var defaultPerms = await db.UserDefaultPreferredPermissions
|
|
.SingleOrDefaultAsync(p => p.UserUID == userUid);
|
|
|
|
if (defaultPerms != null)
|
|
{
|
|
db.Permissions.Add(new UserPermissionSet
|
|
{
|
|
UserUID = userUid,
|
|
OtherUserUID = otherUid,
|
|
DisableAnimations = defaultPerms.DisableIndividualAnimations,
|
|
DisableSounds = defaultPerms.DisableIndividualSounds,
|
|
DisableVFX = defaultPerms.DisableIndividualVFX,
|
|
IsPaused = false,
|
|
Sticky = true,
|
|
});
|
|
modified = true;
|
|
}
|
|
}
|
|
|
|
if (!await db.Permissions.AnyAsync(p => p.UserUID == otherUid && p.OtherUserUID == userUid))
|
|
{
|
|
var defaultPerms = await db.UserDefaultPreferredPermissions
|
|
.SingleOrDefaultAsync(p => p.UserUID == otherUid);
|
|
|
|
if (defaultPerms != null)
|
|
{
|
|
db.Permissions.Add(new UserPermissionSet
|
|
{
|
|
UserUID = otherUid,
|
|
OtherUserUID = userUid,
|
|
DisableAnimations = defaultPerms.DisableIndividualAnimations,
|
|
DisableSounds = defaultPerms.DisableIndividualSounds,
|
|
DisableVFX = defaultPerms.DisableIndividualVFX,
|
|
IsPaused = false,
|
|
Sticky = true,
|
|
});
|
|
modified = true;
|
|
}
|
|
}
|
|
|
|
if (modified)
|
|
{
|
|
await db.SaveChangesAsync();
|
|
_logger.LogInformation("Mutual pair established between {UserUID} and {OtherUID}", userUid, otherUid);
|
|
}
|
|
else
|
|
{
|
|
_logger.LogInformation("Pair already exists between {UserUID} and {OtherUID}", userUid, otherUid);
|
|
}
|
|
|
|
return modified;
|
|
}
|
|
}
|