mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2024-11-21 20:42:45 +01:00
38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
using System.Net;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using DHT.Server.Data;
|
|
using DHT.Server.Database;
|
|
using DHT.Utils.Http;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace DHT.Server.Endpoints;
|
|
|
|
sealed class TrackUsersEndpoint(IDatabaseFile db) : BaseEndpoint(db) {
|
|
protected override async Task Respond(HttpRequest request, HttpResponse response, CancellationToken cancellationToken) {
|
|
var root = await ReadJson(request);
|
|
|
|
if (root.ValueKind != JsonValueKind.Array) {
|
|
throw new HttpException(HttpStatusCode.BadRequest, "Expected root element to be an array.");
|
|
}
|
|
|
|
var users = new User[root.GetArrayLength()];
|
|
int i = 0;
|
|
|
|
foreach (JsonElement user in root.EnumerateArray()) {
|
|
users[i++] = ReadUser(user, "user");
|
|
}
|
|
|
|
await Db.Users.Add(users);
|
|
}
|
|
|
|
private static User ReadUser(JsonElement json, string path) => new () {
|
|
Id = json.RequireSnowflake("id", path),
|
|
Name = json.RequireString("name", path),
|
|
DisplayName = json.HasKey("displayName") ? json.RequireString("displayName", path) : null,
|
|
AvatarUrl = json.HasKey("avatar") ? json.RequireString("avatar", path) : null,
|
|
Discriminator = json.HasKey("discriminator") ? json.RequireString("discriminator", path) : null
|
|
};
|
|
}
|