Skip to content

Commit

Permalink
Support for filter non-guild users in private admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha12 committed Feb 2, 2024
1 parent 0309f16 commit f3ba317
Show file tree
Hide file tree
Showing 9 changed files with 1,000 additions and 15 deletions.
32 changes: 29 additions & 3 deletions src/GrillBot.App/Actions/Api/V1/User/GetUserList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GrillBot.Common.Models;
using GrillBot.Common.Extensions;
using GrillBot.Common.Managers.Localization;
using GrillBot.Common.Models;
using GrillBot.Core.Extensions;
using GrillBot.Core.Infrastructure.Actions;
using GrillBot.Core.Models.Pagination;
Expand All @@ -10,17 +12,22 @@ public class GetUserList : ApiAction
{
private GrillBotDatabaseBuilder DatabaseBuilder { get; }
private IDiscordClient DiscordClient { get; }
private ITextsManager Texts { get; }

public GetUserList(ApiRequestContext apiContext, GrillBotDatabaseBuilder databaseBuilder, IDiscordClient discordClient) : base(apiContext)
public GetUserList(ApiRequestContext apiContext, GrillBotDatabaseBuilder databaseBuilder, IDiscordClient discordClient,
ITextsManager texts) : base(apiContext)
{
DatabaseBuilder = databaseBuilder;
DiscordClient = discordClient;
Texts = texts;
}

public override async Task<ApiResult> ProcessAsync()
{
var parameters = (GetUserListParams)Parameters[0]!;
parameters.FixStatus();

FixStatus(parameters);
ValidateParameters(parameters);

await using var repository = DatabaseBuilder.CreateRepository();

Expand Down Expand Up @@ -55,4 +62,23 @@ private async Task<UserListItem> MapItemAsync(Database.Entity.User entity)

return result;
}

public void FixStatus(GetUserListParams parameters)
{
parameters.Status = parameters.Status switch
{
UserStatus.Invisible => UserStatus.Offline,
UserStatus.AFK => UserStatus.Idle,
_ => parameters.Status
};
}

private void ValidateParameters(GetUserListParams parameters)
{
if (parameters.HideLeftUsers && string.IsNullOrEmpty(parameters.GuildId))
{
throw new ValidationException(Texts["User/Validation/CannotHideMissingGuildId", ApiContext.Language])
.ToBadRequestValidation(parameters.HideLeftUsers, nameof(parameters.HideLeftUsers), nameof(parameters.GuildId));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ private async Task ProcessMemberSynchronizationAsync()
foreach (var user in users.Where(o => o.GuildId == guild.Id.ToString()))
{
user.User!.Status = UserStatus.Offline;
user.IsInGuild = false;

if (!members.TryGetValue(user.UserId.ToUlong(), out var guildUser))
continue;

Expand Down
25 changes: 13 additions & 12 deletions src/GrillBot.Data/Models/API/Users/GetUserListParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class GetUserListParams : IQueryableModel<Database.Entity.User>, IDiction
[DiscordId]
public string? GuildId { get; set; }

/// <summary>
/// Hide non-guild users. Works only if <see cref="GuildId" /> is filled. Otherwise validation error will show.
/// </summary>
public bool HideLeftUsers { get; set; }

/// <summary>
/// Selected flags from UserFlags enum.
/// </summary>
Expand Down Expand Up @@ -62,9 +67,14 @@ public class GetUserListParams : IQueryableModel<Database.Entity.User>, IDiction
}

if (!string.IsNullOrEmpty(GuildId))
query = query.Where(o => o.Guilds.Any(x => x.GuildId == GuildId));
{
if (!HideLeftUsers)
query = query.Where(o => o.Guilds.Any(x => x.GuildId == GuildId));
else
query = query.Where(o => o.Guilds.Any(x => GuildId == x.GuildId && x.IsInGuild));
}

if (Flags != null)
if (Flags != null && Flags > 0)
query = query.Where(o => (o.Flags & Flags) == Flags);

if (HaveBirthday)
Expand All @@ -88,16 +98,6 @@ public class GetUserListParams : IQueryableModel<Database.Entity.User>, IDiction
};
}

public void FixStatus()
{
Status = Status switch
{
UserStatus.Invisible => UserStatus.Offline,
UserStatus.AFK => UserStatus.Idle,
_ => Status
};
}

public Dictionary<string, string?> ToDictionary()
{
var result = new Dictionary<string, string?>
Expand All @@ -107,6 +107,7 @@ public void FixStatus()
{ nameof(Flags), (Flags ?? 0).ToString() },
{ nameof(HaveBirthday), HaveBirthday.ToString() },
{ nameof(UsedInviteCode), UsedInviteCode },
{ nameof(HideLeftUsers), HideLeftUsers.ToString() }
};

if (Status != null)
Expand Down
3 changes: 3 additions & 0 deletions src/GrillBot.Data/Resources/Localization/messages.cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@
"Unverify": "Unverify",
"Timeout": "Dočasné umlčení",
"MemberWarning": "Varování"
},
"Validation": {
"CannotHideMissingGuildId": "Nelze skrýt uživatele, kteří nejsou na serveru. Nejprve vyplň filtr serveru."
}
},
"Emojization": {
Expand Down
3 changes: 3 additions & 0 deletions src/GrillBot.Data/Resources/Localization/messages.en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@
"Unverify": "Unverify",
"Timeout": "Timeout",
"MemberWarning": "Warning"
},
"Validation": {
"CannotHideMissingGuildId": "Cannot hide users who are not on the server. First, fill in the server filter."
}
},
"Emojization": {
Expand Down
3 changes: 3 additions & 0 deletions src/GrillBot.Database/Entity/GuildUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class GuildUser
[MinLength(2)]
public string? Nickname { get; set; }

public bool IsInGuild { get; set; }

public ISet<GuildUserChannel> Channels { get; set; }
public ISet<EmoteStatisticItem> EmoteStatistics { get; set; }
public ISet<Nickname> Nicknames { get; set; }
Expand Down Expand Up @@ -76,6 +78,7 @@ public static GuildUser FromDiscord(IGuild guild, IGuildUser user)
public void Update(IGuildUser user)
{
Nickname = user.IsUser() ? user.Nickname : user.Nickname?.Cut(32, true);
IsInGuild = true;
User?.Update(user);
}
}
Loading

0 comments on commit f3ba317

Please sign in to comment.