chore: add csharpier to husky, format backend with csharpier
This commit is contained in:
parent
5fab66444f
commit
7f971e8549
73 changed files with 2098 additions and 1048 deletions
|
@ -8,89 +8,128 @@ namespace Foxnouns.Backend.Database;
|
|||
|
||||
public static class DatabaseQueryExtensions
|
||||
{
|
||||
public static async Task<User> ResolveUserAsync(this DatabaseContext context, string userRef, Token? token,
|
||||
CancellationToken ct = default)
|
||||
public static async Task<User> ResolveUserAsync(
|
||||
this DatabaseContext context,
|
||||
string userRef,
|
||||
Token? token,
|
||||
CancellationToken ct = default
|
||||
)
|
||||
{
|
||||
if (userRef == "@me")
|
||||
{
|
||||
return token != null
|
||||
? await context.Users.FirstAsync(u => u.Id == token.UserId, ct)
|
||||
: throw new ApiError.Unauthorized("This endpoint requires an authenticated user.",
|
||||
ErrorCode.AuthenticationRequired);
|
||||
: throw new ApiError.Unauthorized(
|
||||
"This endpoint requires an authenticated user.",
|
||||
ErrorCode.AuthenticationRequired
|
||||
);
|
||||
}
|
||||
|
||||
User? user;
|
||||
if (Snowflake.TryParse(userRef, out var snowflake))
|
||||
{
|
||||
user = await context.Users
|
||||
.Where(u => !u.Deleted)
|
||||
user = await context
|
||||
.Users.Where(u => !u.Deleted)
|
||||
.FirstOrDefaultAsync(u => u.Id == snowflake, ct);
|
||||
if (user != null) return user;
|
||||
if (user != null)
|
||||
return user;
|
||||
}
|
||||
|
||||
user = await context.Users
|
||||
.Where(u => !u.Deleted)
|
||||
user = await context
|
||||
.Users.Where(u => !u.Deleted)
|
||||
.FirstOrDefaultAsync(u => u.Username == userRef, ct);
|
||||
if (user != null) return user;
|
||||
throw new ApiError.NotFound("No user with that ID or username found.", code: ErrorCode.UserNotFound);
|
||||
if (user != null)
|
||||
return user;
|
||||
throw new ApiError.NotFound(
|
||||
"No user with that ID or username found.",
|
||||
code: ErrorCode.UserNotFound
|
||||
);
|
||||
}
|
||||
|
||||
public static async Task<User> ResolveUserAsync(this DatabaseContext context, Snowflake id,
|
||||
CancellationToken ct = default)
|
||||
public static async Task<User> ResolveUserAsync(
|
||||
this DatabaseContext context,
|
||||
Snowflake id,
|
||||
CancellationToken ct = default
|
||||
)
|
||||
{
|
||||
var user = await context.Users
|
||||
.Where(u => !u.Deleted)
|
||||
var user = await context
|
||||
.Users.Where(u => !u.Deleted)
|
||||
.FirstOrDefaultAsync(u => u.Id == id, ct);
|
||||
if (user != null) return user;
|
||||
if (user != null)
|
||||
return user;
|
||||
throw new ApiError.NotFound("No user with that ID found.", code: ErrorCode.UserNotFound);
|
||||
}
|
||||
|
||||
public static async Task<Member> ResolveMemberAsync(this DatabaseContext context, Snowflake id,
|
||||
CancellationToken ct = default)
|
||||
public static async Task<Member> ResolveMemberAsync(
|
||||
this DatabaseContext context,
|
||||
Snowflake id,
|
||||
CancellationToken ct = default
|
||||
)
|
||||
{
|
||||
var member = await context.Members
|
||||
.Include(m => m.User)
|
||||
var member = await context
|
||||
.Members.Include(m => m.User)
|
||||
.Where(m => !m.User.Deleted)
|
||||
.FirstOrDefaultAsync(m => m.Id == id, ct);
|
||||
if (member != null) return member;
|
||||
throw new ApiError.NotFound("No member with that ID found.", code: ErrorCode.MemberNotFound);
|
||||
if (member != null)
|
||||
return member;
|
||||
throw new ApiError.NotFound(
|
||||
"No member with that ID found.",
|
||||
code: ErrorCode.MemberNotFound
|
||||
);
|
||||
}
|
||||
|
||||
public static async Task<Member> ResolveMemberAsync(this DatabaseContext context, string userRef, string memberRef,
|
||||
Token? token, CancellationToken ct = default)
|
||||
public static async Task<Member> ResolveMemberAsync(
|
||||
this DatabaseContext context,
|
||||
string userRef,
|
||||
string memberRef,
|
||||
Token? token,
|
||||
CancellationToken ct = default
|
||||
)
|
||||
{
|
||||
var user = await context.ResolveUserAsync(userRef, token, ct);
|
||||
return await context.ResolveMemberAsync(user.Id, memberRef, ct);
|
||||
}
|
||||
|
||||
public static async Task<Member> ResolveMemberAsync(this DatabaseContext context, Snowflake userId,
|
||||
string memberRef, CancellationToken ct = default)
|
||||
public static async Task<Member> ResolveMemberAsync(
|
||||
this DatabaseContext context,
|
||||
Snowflake userId,
|
||||
string memberRef,
|
||||
CancellationToken ct = default
|
||||
)
|
||||
{
|
||||
Member? member;
|
||||
if (Snowflake.TryParse(memberRef, out var snowflake))
|
||||
{
|
||||
member = await context.Members
|
||||
.Include(m => m.User)
|
||||
member = await context
|
||||
.Members.Include(m => m.User)
|
||||
.Include(m => m.ProfileFlags)
|
||||
.Where(m => !m.User.Deleted)
|
||||
.FirstOrDefaultAsync(m => m.Id == snowflake && m.UserId == userId, ct);
|
||||
if (member != null) return member;
|
||||
if (member != null)
|
||||
return member;
|
||||
}
|
||||
|
||||
member = await context.Members
|
||||
.Include(m => m.User)
|
||||
member = await context
|
||||
.Members.Include(m => m.User)
|
||||
.Include(m => m.ProfileFlags)
|
||||
.Where(m => !m.User.Deleted)
|
||||
.FirstOrDefaultAsync(m => m.Name == memberRef && m.UserId == userId, ct);
|
||||
if (member != null) return member;
|
||||
throw new ApiError.NotFound("No member with that ID or name found.", code: ErrorCode.MemberNotFound);
|
||||
if (member != null)
|
||||
return member;
|
||||
throw new ApiError.NotFound(
|
||||
"No member with that ID or name found.",
|
||||
code: ErrorCode.MemberNotFound
|
||||
);
|
||||
}
|
||||
|
||||
public static async Task<Application> GetFrontendApplicationAsync(this DatabaseContext context,
|
||||
CancellationToken ct = default)
|
||||
public static async Task<Application> GetFrontendApplicationAsync(
|
||||
this DatabaseContext context,
|
||||
CancellationToken ct = default
|
||||
)
|
||||
{
|
||||
var app = await context.Applications.FirstOrDefaultAsync(a => a.Id == new Snowflake(0), ct);
|
||||
if (app != null) return app;
|
||||
if (app != null)
|
||||
return app;
|
||||
|
||||
app = new Application
|
||||
{
|
||||
|
@ -107,27 +146,42 @@ public static class DatabaseQueryExtensions
|
|||
return app;
|
||||
}
|
||||
|
||||
public static async Task<Token?> GetToken(this DatabaseContext context, byte[] rawToken,
|
||||
CancellationToken ct = default)
|
||||
public static async Task<Token?> GetToken(
|
||||
this DatabaseContext context,
|
||||
byte[] rawToken,
|
||||
CancellationToken ct = default
|
||||
)
|
||||
{
|
||||
var hash = SHA512.HashData(rawToken);
|
||||
|
||||
var oauthToken = await context.Tokens
|
||||
.Include(t => t.Application)
|
||||
var oauthToken = await context
|
||||
.Tokens.Include(t => t.Application)
|
||||
.Include(t => t.User)
|
||||
.FirstOrDefaultAsync(
|
||||
t => t.Hash == hash && t.ExpiresAt > SystemClock.Instance.GetCurrentInstant() && !t.ManuallyExpired,
|
||||
ct);
|
||||
t =>
|
||||
t.Hash == hash
|
||||
&& t.ExpiresAt > SystemClock.Instance.GetCurrentInstant()
|
||||
&& !t.ManuallyExpired,
|
||||
ct
|
||||
);
|
||||
|
||||
return oauthToken;
|
||||
}
|
||||
|
||||
public static async Task<Snowflake?> GetTokenUserId(this DatabaseContext context, byte[] rawToken,
|
||||
CancellationToken ct = default)
|
||||
public static async Task<Snowflake?> GetTokenUserId(
|
||||
this DatabaseContext context,
|
||||
byte[] rawToken,
|
||||
CancellationToken ct = default
|
||||
)
|
||||
{
|
||||
var hash = SHA512.HashData(rawToken);
|
||||
return await context.Tokens
|
||||
.Where(t => t.Hash == hash && t.ExpiresAt > SystemClock.Instance.GetCurrentInstant() && !t.ManuallyExpired)
|
||||
.Select(t => t.UserId).FirstOrDefaultAsync(ct);
|
||||
return await context
|
||||
.Tokens.Where(t =>
|
||||
t.Hash == hash
|
||||
&& t.ExpiresAt > SystemClock.Instance.GetCurrentInstant()
|
||||
&& !t.ManuallyExpired
|
||||
)
|
||||
.Select(t => t.UserId)
|
||||
.FirstOrDefaultAsync(ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue