Skip to content

Commit

Permalink
simplify level update so we don't have to worry about linked account …
Browse files Browse the repository at this point in the history
…levels
  • Loading branch information
RaidMax committed Feb 28, 2022
1 parent c6866fd commit c22bfed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 41 deletions.
47 changes: 6 additions & 41 deletions SharedLibraryCore/Services/ClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -597,51 +597,16 @@ private async Task UpdateAliasNew(string originalName, int? ip, Data.Models.Clie
/// <returns></returns>
public virtual async Task UpdateLevel(Permission newPermission, EFClient temporalClient, EFClient origin)
{
await using var ctx = _contextFactory.CreateContext();
var entity = await ctx.Clients
.Where(_client => _client.ClientId == temporalClient.ClientId)
await using var context = _contextFactory.CreateContext();
var entity = await context.Clients
.Where(client => client.ClientId == temporalClient.ClientId)
.FirstAsync();

var oldPermission = entity.Level;
_logger.LogInformation("Updating {ClientId} from {OldPermission} to {NewPermission} ",
temporalClient.ClientId, entity.Level, newPermission);

entity.Level = newPermission;
await ctx.SaveChangesAsync();

using (LogContext.PushProperty("Server", temporalClient?.CurrentServer?.ToString()))
{
_logger.LogInformation("Updated {clientId} to {newPermission}", temporalClient.ClientId, newPermission);

var linkedPermissionSet = new[] { Permission.Banned, Permission.Flagged };
// if their permission level has been changed to level that needs to be updated on all accounts
if (linkedPermissionSet.Contains(newPermission) || linkedPermissionSet.Contains(oldPermission))
{
//get all clients that have the same linkId
var iqMatchingClients = ctx.Clients
.Where(_client => _client.AliasLinkId == entity.AliasLinkId);

var iqLinkClients = new List<Data.Models.Client.EFClient>().AsQueryable();
if (!_appConfig.EnableImplicitAccountLinking)
{
var linkIds = await ctx.Aliases.Where(alias =>
alias.IPAddress != null && alias.IPAddress == temporalClient.IPAddress)
.Select(alias => alias.LinkId)
.ToListAsync();
iqLinkClients = ctx.Clients.Where(client => linkIds.Contains(client.AliasLinkId));
}

// this updates the level for all the clients with the same LinkId
// only if their new level is flagged or banned
await iqMatchingClients.Union(iqLinkClients).ForEachAsync(_client =>
{
_client.Level = newPermission;
_logger.LogInformation("Updated linked {clientId} to {newPermission}", _client.ClientId,
newPermission);
});

await ctx.SaveChangesAsync();
}
}

await context.SaveChangesAsync();
temporalClient.Level = newPermission;
}

Expand Down
8 changes: 8 additions & 0 deletions WebfrontCore/Controllers/Client/ClientController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public async Task<IActionResult> ProfileAsync(int id, MetaType? metaFilterType)
client.SetAdditionalProperty(EFMeta.ClientTag, tag.LinkedMeta.Value);
}

// even though we haven't set their level to "banned" yet
// (ie they haven't reconnected with the infringing player identifier)
// we want to show them as banned as to not confuse people.
if (activePenalties.Any(penalty => penalty.Type == EFPenalty.PenaltyType.Ban))
{
client.Level = Data.Models.Client.EFClient.Permission.Banned;
}

var displayLevelInt = (int)client.Level;
var displayLevel = client.Level.ToLocalizedLevelName();

Expand Down

0 comments on commit c22bfed

Please sign in to comment.