2022-08-30 15:21:00 +00:00
|
|
|
|
using System;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2022-08-30 15:21:00 +00:00
|
|
|
|
using System.Net.Http;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
2022-08-30 15:21:00 +00:00
|
|
|
|
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; }
|
|
|
|
|
|
2022-08-30 15:21:00 +00:00
|
|
|
|
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();
|
|
|
|
|
|
2022-08-30 15:21:00 +00:00
|
|
|
|
if (bups.Any())
|
|
|
|
|
Submit(httpClient, bups);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 15:21:00 +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:21:00 +00:00
|
|
|
|
using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, FlashiiUrls.BUMP) {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|