sharp-chat/SharpChat/Flashii/FlashiiBump.cs

38 lines
1.2 KiB
C#
Raw Normal View History

2022-08-30 15:00:58 +00:00
using Hamakaze;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace SharpChat.Flashii {
public class FlashiiBump {
[JsonPropertyName(@"id")]
public long UserId { get; set; }
[JsonPropertyName(@"ip")]
public string UserIP { get; set; }
public static void Submit(IEnumerable<ChatUser> users) {
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(bups);
}
public static void Submit(IEnumerable<FlashiiBump> users) {
if(users == null)
throw new ArgumentNullException(nameof(users));
if(!users.Any())
return;
byte[] data = JsonSerializer.SerializeToUtf8Bytes(users);
HttpRequestMessage hrm = new HttpRequestMessage(@"POST", FlashiiUrls.BUMP);
hrm.AddHeader(@"X-SharpChat-Signature", data.GetSignedHash());
hrm.SetBody(data);
HttpClient.Send(hrm, onError: (t, e) => Logger.Write($@"Flashii Bump Error: {e}"));
}
}
}