[backend/masto-client] Allow searching by user fqn (ISH-196)

This commit is contained in:
Laura Hausmann 2024-03-16 17:54:25 +01:00
parent 7564c67371
commit a0e83949ff
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 27 additions and 7 deletions

View file

@ -130,8 +130,8 @@ public class SearchController(
return await db.Users return await db.Users
.IncludeCommonProperties() .IncludeCommonProperties()
.Where(p => p.DisplayNameContainsCaseInsensitive(search.Query!) || .Where(p => p.DisplayNameOrUsernameOrFqnContainsCaseInsensitive(search.Query!,
p.UsernameContainsCaseInsensitive(search.Query!)) config.Value.AccountDomain))
.Where(p => !search.Following || p.IsFollowedBy(user)) .Where(p => !search.Following || p.IsFollowedBy(user))
.Paginate(pagination, ControllerContext) //TODO: this will mess up our sorting .Paginate(pagination, ControllerContext) //TODO: this will mess up our sorting
.OrderByDescending(p => p.NotesCount) .OrderByDescending(p => p.NotesCount)

View file

@ -499,12 +499,32 @@ public class User : IEntity
[StringLength(32)] [StringLength(32)]
public string Id { get; set; } = null!; public string Id { get; set; } = null!;
[Projectable]
public string GetFqnLower(string accountDomain) => UsernameLower + "@" + (Host ?? accountDomain);
[Projectable]
public string GetFqn(string accountDomain) => Username + "@" + (Host ?? accountDomain);
[Projectable] [Projectable]
public bool DisplayNameContainsCaseInsensitive(string str) => public bool DisplayNameContainsCaseInsensitive(string str) =>
DisplayName != null && EF.Functions.ILike(DisplayName, "%" + EfHelpers.EscapeLikeQuery(str) + "%", @"\"); DisplayName != null && EF.Functions.ILike(DisplayName, "%" + EfHelpers.EscapeLikeQuery(str) + "%", @"\");
[Projectable] [Projectable]
public bool UsernameContainsCaseInsensitive(string str) => UsernameLower.Contains(str); public bool UsernameContainsCaseInsensitive(string str) => UsernameLower.Contains(str.ToLowerInvariant());
[Projectable]
public bool FqnContainsCaseInsensitive(string str, string accountDomain) =>
GetFqnLower(accountDomain).Contains(str.ToLowerInvariant());
[Projectable]
public bool UsernameOrFqnContainsCaseInsensitive(string str, string accountDomain) =>
str.Contains('@') ? FqnContainsCaseInsensitive(str, accountDomain) : UsernameContainsCaseInsensitive(str);
[Projectable]
public bool DisplayNameOrUsernameOrFqnContainsCaseInsensitive(string str, string accountDomain) =>
str.Contains('@') && !str.Contains(' ')
? FqnContainsCaseInsensitive(str, accountDomain)
: UsernameContainsCaseInsensitive(str) || DisplayNameContainsCaseInsensitive(str);
[Projectable] [Projectable]
public bool IsBlockedBy(User user) => BlockedBy.Contains(user); public bool IsBlockedBy(User user) => BlockedBy.Contains(user);