Skip to content

Commit

Permalink
Refactor & update member search v2 (#3046)
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha-133 authored Jan 7, 2025
1 parent b3c09b5 commit 36439b6
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 225 deletions.
171 changes: 122 additions & 49 deletions src/Discord.Net.Core/Entities/Guilds/MemberSearchPropertiesV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ public class MemberSearchPropertiesV2
/// <summary>
/// Gets or sets the after property for the search.
/// </summary>
public MemberSearchPropertiesV2After After { get; set; }
public MemberSearchPaginationFilter After { get; set; }

/// <summary>
/// Gets or sets the before property for the search.
/// </summary>
public MemberSearchPaginationFilter Before { get; set; }

/// <summary>
/// Gets or sets the sort type for the search.
Expand All @@ -23,19 +28,19 @@ public class MemberSearchPropertiesV2
/// <summary>
/// Gets or sets the and query for the search.
/// </summary>
public MemberSearchV2QueryParams? AndQuery { get; set; }
public MemberSearchFilter? AndQuery { get; set; }

/// <summary>
/// Gets or sets the or query for the search.
/// </summary>
public MemberSearchV2QueryParams? OrQuery { get; set; }
public MemberSearchFilter? OrQuery { get; set; }
}


/// <summary>
/// Represents the after property for searching members in a guild.
/// </summary>
public struct MemberSearchPropertiesV2After
public struct MemberSearchPaginationFilter
{
/// <summary>
/// Gets or sets the user ID to search after.
Expand All @@ -47,24 +52,26 @@ public struct MemberSearchPropertiesV2After
/// </summary>
public long GuildJoinedAt { get; set; }

public MemberSearchPropertiesV2After(ulong userId, long guildJoinedAt)
public MemberSearchPaginationFilter(ulong userId, long guildJoinedAt)
{
UserId = userId;
GuildJoinedAt = guildJoinedAt;
}

public MemberSearchPropertiesV2After(ulong userId, DateTimeOffset guildJoinedAt)
public MemberSearchPaginationFilter(ulong userId, DateTimeOffset guildJoinedAt)
{
UserId = userId;
GuildJoinedAt = guildJoinedAt.ToUnixTimeMilliseconds();
}

public MemberSearchPaginationFilter() { }
}


/// <summary>
/// Represents the query parameters for searching members in a guild.
/// </summary>
public struct MemberSearchV2QueryParams
public struct MemberSearchFilter
{
/// <summary>
/// Gets or sets the safety signal search properties.
Expand All @@ -74,27 +81,60 @@ public struct MemberSearchV2QueryParams
/// <summary>
/// Gets or sets the role IDs to search for.
/// </summary>
public MemberSearchV2QueryProperties? RoleIds { get; set; }
/// <remarks>
/// Only <see cref="IMemberSearchQuery.OrQuery"/> and <see cref="IMemberSearchQuery.AndQuery"/> are supported.
/// </remarks>
public MemberSearchSnowflakeQuery? RoleIds { get; set; }

/// <summary>
/// Gets or sets the range to search for the user ID.
/// </summary>
public MemberSearchV2RangeProperties? UserId { get; set; }
/// <remarks>
/// Only <see cref="IMemberSearchQuery.OrQuery"/> and <see cref="IMemberSearchQuery.Range"/> are supported.
/// </remarks>
public MemberSearchSnowflakeQuery? UserId { get; set; }

/// <summary>
/// Gets or sets the range to search for the user's guild joined at timestamp.
/// </summary>
public MemberSearchV2RangeProperties? GuildJoinedAt { get; set; }
/// <remarks>
/// Only <see cref="IMemberSearchQuery.Range"/> is supported.
/// </remarks>
public MemberSearchIntQuery? GuildJoinedAt { get; set; }

/// <summary>
/// Gets or sets the source invite code to search for.
/// </summary>
public MemberSearchV2QueryProperties? SourceInviteCode { get; set; }
/// <remarks>
/// Only <see cref="IMemberSearchQuery.OrQuery"/> is supported.
/// </remarks>
public MemberSearchStringQuery? SourceInviteCode { get; set; }

/// <summary>
/// Gets or sets the join source type to search for.
/// </summary>
public MemberSearchV2QueryProperties? JoinSourceType { get; set; }
/// <remarks>
/// Only <see cref="IMemberSearchQuery.OrQuery"/> is supported.
/// </remarks>
public MemberSearchIntQuery? JoinSourceType { get; set; }

/// <summary>
/// Gets or sets whether the member left and rejoined the guild.
/// </summary>
public bool? DidRejoin { get; set; }

/// <summary>
/// Gets or sets whether the member has not yet passed the guild's member verification requirements.
/// </summary>
public bool? IsPending { get; set; }

/// <summary>
/// Gets or sets the usernames to match against.
/// </summary>
/// <remarks>
/// Only <see cref="IMemberSearchQuery.OrQuery"/> is supported.
/// </remarks>
public MemberSearchStringQuery? Usernames { get; set; }
}


Expand All @@ -106,12 +146,12 @@ public struct MemberSearchV2SafetySignalsProperties
/// <summary>
/// Gets or sets the unusual DM activity until property for the search.
/// </summary>
public MemberSearchV2SafetySignalProperties? UnusualDmActivityUntil { get; set; }
public MemberSearchIntQuery? UnusualDmActivityUntil { get; set; }

/// <summary>
/// Gets or sets the communication disabled until property for the search.
/// </summary>
public MemberSearchV2SafetySignalProperties? CommunicationDisabledUntil { get; set; }
public MemberSearchIntQuery? CommunicationDisabledUntil { get; set; }

/// <summary>
/// Gets or sets the unusual account activity property for the search.
Expand All @@ -124,60 +164,93 @@ public struct MemberSearchV2SafetySignalsProperties
public bool? AutomodQuarantinedUsername { get; set; }
}


/// <summary>
/// Represents the query properties for searching members in a guild.
/// Represents the range properties for searching members in a guild.
/// </summary>
public readonly struct MemberSearchV2QueryProperties
public struct MemberSearchV2Range
{
/// <summary>
/// Gets the and query for the search.
/// Gets or sets the less than property for the search.
/// </summary>
public Dictionary<int, object> AndQuery { get; }
public long? LessThanOrEqual { get; set; }

/// <summary>
/// Gets the or query for the search.
/// Gets or sets the greater than property for the search.
/// </summary>
public Dictionary<int, object> OrQuery { get; }

public MemberSearchV2QueryProperties(Dictionary<int, string> andQuery, Dictionary<int, string> orQuery)
{
AndQuery = andQuery.Select(x => new KeyValuePair<int, object>(x.Key, x.Value)).ToDictionary(x => x.Key, y => y.Value);
OrQuery = orQuery.Select(x => new KeyValuePair<int, object>(x.Key, x.Value)).ToDictionary(x => x.Key, y => y.Value);
}

public MemberSearchV2QueryProperties(Dictionary<int, long> andQuery, Dictionary<int, long> orQuery)
{
AndQuery = andQuery.Select(x => new KeyValuePair<int, object>(x.Key, x.Value)).ToDictionary(x => x.Key, y => y.Value);
OrQuery = orQuery.Select(x => new KeyValuePair<int, object>(x.Key, x.Value)).ToDictionary(x => x.Key, y => y.Value);
}
public long? GreaterThanOrEqual { get; set; }
}


/// <summary>
/// Represents the safety signal properties for searching members in a guild.
/// </summary>
public struct MemberSearchV2SafetySignalProperties
public interface IMemberSearchQuery
{
/// <summary>
/// Gets or sets the range for the search.
/// </summary>
public MemberSearchV2RangeProperties Range { get; set; }
}

MemberSearchV2Range? Range { get; }

/// <summary>
/// Represents the range properties for searching members in a guild.
/// </summary>
public struct MemberSearchV2RangeProperties
{
/// <summary>
/// Gets or sets the less than property for the search.
/// Gets the AND query for the search.
/// </summary>
public long? LessThanOrEqual { get; set; }
IEnumerable<object> AndQuery { get; }

/// <summary>
/// Gets or sets the greater than property for the search.
/// Gets the OR query for the search.
/// </summary>
public long? GreaterThanOrEqual { get; set; }
IEnumerable<object> OrQuery { get; }
}

public struct MemberSearchStringQuery : IMemberSearchQuery
{
/// <inheritdoc />
public MemberSearchV2Range? Range { get; set; }

/// <inheritdoc cref="IMemberSearchQuery.AndQuery"/>
public IEnumerable<string> AndQuery { get; set; }

/// <inheritdoc cref="IMemberSearchQuery.OrQuery"/>
public IEnumerable<string> OrQuery { get; set; }

/// <inheritdoc />
IEnumerable<object> IMemberSearchQuery.AndQuery => AndQuery;

/// <inheritdoc />
IEnumerable<object> IMemberSearchQuery.OrQuery => OrQuery;
}


public struct MemberSearchIntQuery : IMemberSearchQuery
{
/// <inheritdoc />
public MemberSearchV2Range? Range { get; set; }

/// <inheritdoc cref="IMemberSearchQuery.AndQuery"/>
public IEnumerable<int> AndQuery { get; set; }

/// <inheritdoc cref="IMemberSearchQuery.OrQuery"/>
public IEnumerable<int> OrQuery { get; set; }

/// <inheritdoc />
IEnumerable<object> IMemberSearchQuery.AndQuery => AndQuery.Select(x => (object)x);

/// <inheritdoc />
IEnumerable<object> IMemberSearchQuery.OrQuery => OrQuery.Select(x => (object)x);
}


public struct MemberSearchSnowflakeQuery : IMemberSearchQuery
{
/// <inheritdoc />
public MemberSearchV2Range? Range { get; set; }

/// <inheritdoc cref="IMemberSearchQuery.AndQuery"/>
public IEnumerable<ulong> AndQuery { get; set; }

/// <inheritdoc cref="IMemberSearchQuery.OrQuery"/>
public IEnumerable<ulong> OrQuery { get; set; }

/// <inheritdoc />
IEnumerable<object> IMemberSearchQuery.AndQuery => AndQuery.Select(x => (object)x);

/// <inheritdoc />
IEnumerable<object> IMemberSearchQuery.OrQuery => OrQuery.Select(x => (object)x);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class GuildMemberSearchResponse
public ulong GuildId { get; set; }

[JsonProperty("members")]
public GuildSearchMemberData[] Members { get; set; }
public SupplementalGuildUser[] Members { get; set; }

[JsonProperty("page_result_count")]
public int PageResultCount { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Discord.API;

internal class GuildSearchMemberData
internal class SupplementalGuildUser
{
[JsonProperty("member")]
public GuildMember Member { get; set; }
Expand Down
43 changes: 26 additions & 17 deletions src/Discord.Net.Rest/API/Rest/SearchGuildMembersParamsV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ internal class SearchGuildMembersParamsV2
public Optional<int?> Limit { get; set; }

[JsonProperty("and_query")]
public Optional<SearchQueryParams> AndQuery { get; set; }
public Optional<MemberSearchFilter> AndQuery { get; set; }

[JsonProperty("or_query")]
public Optional<SearchQueryParams> OrQuery { get; set; }
public Optional<MemberSearchFilter> OrQuery { get; set; }

[JsonProperty("after")]
public Optional<SearchParamsAfter> After { get; set; }
public Optional<MemberSearchPaginationFilter> After { get; set; }

[JsonProperty("before")]
public Optional<MemberSearchPaginationFilter> Before { get; set; }

[JsonProperty("sort")]
public Optional<MemberSearchV2SortType> Sort { get; set; }
}

internal class SearchParamsAfter
internal class MemberSearchPaginationFilter
{
[JsonProperty("guild_joined_at")]
public long GuildJoinedAt { get; set; }
Expand All @@ -30,7 +33,7 @@ internal class SearchParamsAfter
public ulong UserId { get; set; }
}

internal class SearchQueryParams
internal class MemberSearchFilter
{
[JsonProperty("safety_signals")]
public Optional<SafetySignalsProperties> SafetySignals { get; set; }
Expand All @@ -39,34 +42,46 @@ internal class SearchQueryParams
public Optional<SearchQueryProperties> RoleIds { get; set; }

[JsonProperty("user_id")]
public Optional<SearchRangeProperties> UserId { get; set; }
public Optional<SearchQueryProperties> UserId { get; set; }

[JsonProperty("guild_joined_at")]
public Optional<SearchRangeProperties> GuildJoinedAt { get; set; }
public Optional<SearchQueryProperties> GuildJoinedAt { get; set; }

[JsonProperty("source_invite_code")]
public Optional<SearchQueryProperties> SourceInviteCode { get; set; }

[JsonProperty("join_source_type")]
public Optional<SearchQueryProperties> JoinSourceType { get; set; }

[JsonProperty("did_rejoin")]
public Optional<bool> DidRejoin { get; set; }

[JsonProperty("is_pending")]
public Optional<bool> IsPending { get; set; }

[JsonProperty("usernames")]
public Optional<SearchQueryProperties> Usernames { get; set; }
}

internal class SearchQueryProperties
{
[JsonProperty("and_query")]
public Optional<Dictionary<int, object>> AndQuery { get; set; }
public Optional<IEnumerable<object>> AndQuery { get; set; }

[JsonProperty("or_query")]
public Optional<Dictionary<int, object>> OrQuery { get; set; }
public Optional<IEnumerable<object>> OrQuery { get; set; }

[JsonProperty("range")]
public Optional<SearchRangeProperties> Range { get; set; }
}

internal class SafetySignalsProperties
{
[JsonProperty("unusual_dm_activity_until")]
public Optional<SafetySignalProperties> UnusualDMActivityUntil { get; set; }
public Optional<SearchQueryProperties> UnusualDMActivityUntil { get; set; }

[JsonProperty("communication_disabled_until")]
public Optional<SafetySignalProperties> CommunicationDisabledUntil { get; set; }
public Optional<SearchQueryProperties> CommunicationDisabledUntil { get; set; }

[JsonProperty("unusual_account_activity")]
public Optional<bool> UnusualAccountActivity { get; set; }
Expand All @@ -75,12 +90,6 @@ internal class SafetySignalsProperties
public Optional<bool> AutomodQuarantinedUsername { get; set; }
}

internal class SafetySignalProperties
{
[JsonProperty("range")]
public SearchRangeProperties Until { get; set; }
}

internal class SearchRangeProperties
{
[JsonProperty("gte")]
Expand Down
Loading

0 comments on commit 36439b6

Please sign in to comment.