sharp-chat/SharpChat/Flashii/FlashiiBump.cs

48 lines
1.6 KiB
C#
Raw Permalink 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; }
public static void Submit(HttpClient httpClient, IEnumerable<ChatUser> users) {
2022-08-30 15:00:58 +00:00
List<FlashiiBump> bups = users.Where(u => u.HasSessions).Select(x => new FlashiiBump { UserId = x.UserId, UserIP = x.RemoteAddresses.First().ToString() }).ToList();
if (bups.Any())
Submit(httpClient, bups);
2022-08-30 15:00:58 +00:00
}
public static void Submit(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);
2022-08-30 15:42:03 +00:00
using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, FlashiiUrls.BumpURL) {
Content = new ByteArrayContent(data),
Headers = {
{ @"X-SharpChat-Signature", data.GetSignedHash() },
}
};
httpClient.SendAsync(request).ContinueWith(x => {
if(x.IsFaulted)
Logger.Write($@"Flashii Bump Error: {x.Exception}");
});
2022-08-30 15:00:58 +00:00
}
}
}