-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Clean up redacted access checking Also 1984 out user facing distinction between user redacted, admin redacted and GDPR request redacted accounts * Implement better handling of search query parameters * Do the frontend handling of search * Fix the empty search * Fix feedback issues
- Loading branch information
1 parent
a771327
commit fb62642
Showing
6 changed files
with
272 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
namespace ReplayBrowser.Models; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace ReplayBrowser.Models; | ||
|
||
public enum SearchMode | ||
{ | ||
[Display(Name = "Map")] | ||
Map, | ||
[Display(Name = "Gamemode")] | ||
Gamemode, | ||
[Display(Name = "Server ID")] | ||
ServerId, | ||
[Display(Name = "Round End Text")] | ||
RoundEndText, | ||
[Display(Name = "Player IC Name")] | ||
PlayerIcName, | ||
[Display(Name = "Player OOC Name")] | ||
PlayerOocName, | ||
[Display(Name = "Player GUID")] | ||
Guid, | ||
[Display(Name = "Server Name")] | ||
ServerName, | ||
[Display(Name = "Round ID")] | ||
RoundId | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,85 @@ | ||
using System.Text.Json.Serialization; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace ReplayBrowser.Models; | ||
|
||
public class SearchQueryItem | ||
{ | ||
[JsonPropertyName("searchMode")] | ||
public required string SearchMode { get; set; } | ||
public string SearchMode | ||
{ | ||
set | ||
{ | ||
if (!ModeMapping.TryGetValue(value.ToLower(), out var mapped)) | ||
throw new ArgumentOutOfRangeException(); | ||
SearchModeEnum = mapped; | ||
} | ||
} | ||
[JsonPropertyName("searchValue")] | ||
public required string SearchValue { get; set; } | ||
[JsonIgnore] | ||
public SearchMode SearchModeEnum { get; set; } | ||
|
||
public SearchMode SearchModeEnum | ||
{ | ||
get | ||
public static List<SearchQueryItem> FromQuery(IQueryCollection query) { | ||
List<SearchQueryItem> result = []; | ||
// Yes this is fragile. No it won't really do anything but annoy people | ||
// Technically inefficient. In practice, meh | ||
// Too bad this collection isn't just a list of tuples | ||
var ordered = query.OrderBy(q => q.Key.Contains('[') ? int.Parse(q.Key[(q.Key.IndexOf('[') + 1)..q.Key.IndexOf(']')]) : int.MaxValue).ToList(); | ||
|
||
foreach (var item in ordered) | ||
{ | ||
return SearchMode switch | ||
{ | ||
"Map" => Models.SearchMode.Map, | ||
"Gamemode" => Models.SearchMode.Gamemode, | ||
"Server id" => Models.SearchMode.ServerId, | ||
"Round end text" => Models.SearchMode.RoundEndText, | ||
"Player ic name" => Models.SearchMode.PlayerIcName, | ||
"Player ooc name" => Models.SearchMode.PlayerOocName, | ||
"Guid" => Models.SearchMode.Guid, | ||
"Server name" => Models.SearchMode.ServerName, | ||
"Round id" => Models.SearchMode.RoundId, | ||
_ => throw new ArgumentOutOfRangeException() | ||
}; | ||
var index = item.Key.IndexOf('['); | ||
if (index != -1) | ||
result.AddRange(QueryValueParse(item.Key[..index], item.Value)); | ||
else | ||
result.AddRange(QueryValueParse(item.Key, item.Value)); | ||
} | ||
|
||
var legacyQuery = query["searches"]; | ||
if (legacyQuery.Count > 0 && legacyQuery[0]!.Length > 0) | ||
result.AddRange(FromQueryLegacy(legacyQuery[0]!)); | ||
|
||
return result; | ||
} | ||
|
||
public static List<SearchQueryItem> FromQueryLegacy(string searchesParam) { | ||
var decoded = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(searchesParam)); | ||
return System.Text.Json.JsonSerializer.Deserialize<List<SearchQueryItem>>(decoded)!; | ||
} | ||
|
||
public static List<SearchQueryItem> QueryValueParse(string key, StringValues values) { | ||
if (!ModeMapping.TryGetValue(key, out var type)) | ||
return []; | ||
|
||
return values | ||
.Where(v => v is not null && v.Length > 0) | ||
.Select(v => new SearchQueryItem { SearchModeEnum = type, SearchValue = v! }) | ||
.ToList(); | ||
} | ||
|
||
public static string QueryName(SearchMode mode) | ||
=> ModeMapping.First(v => v.Value == mode).Key; | ||
|
||
// String values must be lowercase! | ||
// Be careful with changing any of the values here, as it can cause old searched to be invalid | ||
// For this reason, it's better to only add new entries | ||
public static readonly Dictionary<string, SearchMode> ModeMapping = new() { | ||
{ "guid", Models.SearchMode.Guid }, | ||
{ "username", Models.SearchMode.PlayerOocName }, | ||
{ "character", Models.SearchMode.PlayerIcName }, | ||
{ "server_id", Models.SearchMode.ServerId }, | ||
{ "server", Models.SearchMode.ServerName }, | ||
{ "round", Models.SearchMode.RoundId }, | ||
{ "map", Models.SearchMode.Map }, | ||
{ "gamemode", Models.SearchMode.Gamemode }, | ||
{ "endtext", Models.SearchMode.RoundEndText }, | ||
// Legacy | ||
{ "player ooc name", Models.SearchMode.PlayerOocName }, | ||
{ "player ic name", Models.SearchMode.PlayerIcName }, | ||
{ "server id", Models.SearchMode.ServerId }, | ||
{ "server name", Models.SearchMode.ServerName }, | ||
{ "round id", Models.SearchMode.RoundId }, | ||
{ "round end text", Models.SearchMode.RoundEndText }, | ||
}; | ||
} |
Oops, something went wrong.