sharp-chat/SharpChat/Flashii/FlashiiBump.cs

47 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace SharpChat.Flashii {
public class FlashiiBump {
[JsonPropertyName(@"id")]
public long UserId { get; set; }
[JsonPropertyName(@"ip")]
public string UserIP { get; set; }
public static async Task SubmitAsync(HttpClient httpClient, IEnumerable<ChatUser> users) {
FlashiiBump[] bups = users.Where(u => u.HasSessions).Select(x => new FlashiiBump {
UserId = x.UserId,
UserIP = x.RemoteAddresses.First().ToString()
}).ToArray();
if(bups.Any())
await SubmitAsync(httpClient, bups);
}
public static async Task SubmitAsync(HttpClient httpClient, IEnumerable<FlashiiBump> users) {
if(httpClient == null)
throw new ArgumentNullException(nameof(httpClient));
if(users == null)
throw new ArgumentNullException(nameof(users));
if(!users.Any())
return;
byte[] data = JsonSerializer.SerializeToUtf8Bytes(users);
HttpRequestMessage request = new(HttpMethod.Post, FlashiiUrls.BumpURL) {
Content = new ByteArrayContent(data),
Headers = {
{ @"X-SharpChat-Signature", data.GetSignedHash() },
}
};
await httpClient.SendAsync(request);
}
}
}