sharp-chat/SharpChat/Flashii/FlashiiBump.cs

48 lines
1.5 KiB
C#
Raw Normal View History

using System;
2022-08-30 15:00:58 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
2022-08-30 15:00:58 +00:00
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
2022-08-30 15:00:58 +00:00
namespace SharpChat.Flashii {
public class FlashiiBump {
[JsonPropertyName(@"id")]
public long UserId { get; set; }
[JsonPropertyName(@"ip")]
public string UserIP { get; set; }
2023-02-06 20:14:50 +00:00
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);
2022-08-30 15:00:58 +00:00
}
2023-02-06 20:14:50 +00:00
public static async Task SubmitAsync(HttpClient httpClient, IEnumerable<FlashiiBump> users) {
if(httpClient == null)
throw new ArgumentNullException(nameof(httpClient));
2022-08-30 15:00:58 +00:00
if(users == null)
throw new ArgumentNullException(nameof(users));
if(!users.Any())
return;
byte[] data = JsonSerializer.SerializeToUtf8Bytes(users);
2023-02-06 20:14:50 +00:00
HttpRequestMessage request = new(HttpMethod.Post, FlashiiUrls.BumpURL) {
Content = new ByteArrayContent(data),
Headers = {
{ @"X-SharpChat-Signature", data.GetSignedHash() },
}
};
2023-02-06 20:14:50 +00:00
await httpClient.SendAsync(request);
2022-08-30 15:00:58 +00:00
}
}
}