using Luski.net.Enums; using Luski.net.Interfaces; using Luski.net.JsonTypes.BaseTypes; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text.Json.Serialization; using System.Threading; using System.Threading.Tasks; using JacobTechEncryption; using JacobTechEncryption.Enums; using Luski.Shared.PublicServers.V1.ClientToServer.HTTP; using Luski.Shared.PublicServers.V1.Enums; using Luski.Shared.PublicServers.V1.ServerToClient.HTTP; using ChannelType = Luski.net.Enums.Main.ChannelType; namespace Luski.net.Structures.Main; public class MainSocketTextChannel : MainSocketChannel { public async Task GetMessage(long ID, CancellationToken CancellationToken) { return await Server.GetMessage(ID, CancellationToken); } public async Task GetPicture(CancellationToken CancellationToken) { throw new NotImplementedException(); //if (Type == ChannelType.DM) return Members.First().GetAvatar(CancellationToken).Result; // else { bool isc = System.IO.File.Exists(Server.Storage.GetStorageDirectory(StorageDirectory.ChannelIcons) + Id.ToString()); if (!isc) await Server.GetFromServer($"SocketChannel/GetPicture/{Id}", Server.Storage.GetStorageDirectory(StorageDirectory.ChannelIcons) + Id.ToString(), CancellationToken); return Server.Storage.GetResourceStream(StorageDirectory.ChannelIcons, Id.ToString()); } } public async Task> GetMessages(long Message_Id, CancellationToken CancellationToken, int count = 50) { if (count > 200) { throw new Exception("You can not request more than 200 messages at a time"); } else if (count < 1) { throw new Exception("You must request at least 1 message"); } else { SocketBulkMessage data = await Server.GetFromServer("SocketBulkMessage", SocketBulkMessageContext.Default.SocketBulkMessage, CancellationToken, new KeyValuePair("channel_id", Id.ToString()), new KeyValuePair("messages", count.ToString()), new KeyValuePair("mostrecentid", Message_Id.ToString())); if (data.Error is null) { int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 5) * 2.0)); if (num == 0) num = 1; string key = Server.EncryptionHandler.GetChannelKey(Id); if (data is null) throw new Exception("Invalid data from server"); if (data.Messages is null) data.Messages = Array.Empty(); Parallel.ForEach(data.Messages, new ParallelOptions() { MaxDegreeOfParallelism = num }, i => { i.decrypt(key, CancellationToken); }); key = null; return await Task.FromResult(data.Messages.ToList().AsReadOnly() as IReadOnlyList); } else { throw new Exception(data.ErrorMessage); } } } public async Task> GetMessages(CancellationToken CancellationToken, int count = 50) { try { if (count > 200) { throw new Exception("You can not request more than 200 messages at a time"); } else if (count < 1) { throw new Exception("You must request at least 1 message"); } else { DateTime start = DateTime.Now; SocketBulkMessage data = await Server.GetFromServer("SocketBulkMessage", SocketBulkMessageContext.Default.SocketBulkMessage, CancellationToken, new KeyValuePair("id", Id.ToString()), new KeyValuePair("messages", count.ToString())); Console.WriteLine($"Messages downloaded in {(DateTime.Now - start).TotalSeconds}"); start = DateTime.Now; if (data is not null && !data.Error.HasValue) { int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 5) * 2.0)); if (num == 0) num = 1; string key = Server.EncryptionHandler.GetChannelKey(Id); if (data.Messages is null) data.Messages = Array.Empty(); Parallel.ForEach(data.Messages, new ParallelOptions() { MaxDegreeOfParallelism = num }, i => { i.decrypt(key, CancellationToken); }); key = null; Console.WriteLine($"Messages decrypted in {(DateTime.Now - start).TotalSeconds}"); return await Task.FromResult(data.Messages.ToList().AsReadOnly() as IReadOnlyList); } else { throw data?.Error switch { ErrorCode.InvalidToken => new Exception("Your current token is no longer valid"), ErrorCode.ServerError => new Exception($"Error from server: {data.ErrorMessage}"), ErrorCode.InvalidHeader => new Exception(data.ErrorMessage), ErrorCode.MissingHeader => new Exception("The header sent to the server was not found. This may be because you app is couropt or you are using the wron API version"), ErrorCode.Forbidden => new Exception("You are not allowed to do this request"), _ => new Exception(data?.Error.ToString()), }; } } } catch (Exception) { throw; } } public async Task SendMessage(string Message, CancellationToken CancellationToken, params File?[] Files) { string key = Server.EncryptionHandler.GetChannelKey(Id); MessageCTS m = new() { Base64Context = Convert.ToBase64String(Encryption.RSA.Encrypt(Message, key, EncoderType.UTF8)), ChannelID = Id, Profile = null, EncryptionKey = 0, Encoding = EncoderType.UTF8 }; if (Files is not null && Files.Length > 0) { List bb = new(); for (int i = 0; i < Files.Length; i++) { File? ff = Files[i]; if (ff is not null) { bb.Add(await ff.Upload(key, CancellationToken)); Files[i] = null; } } m.Files = bb.ToArray(); } STC data = await Server.SendServer("socketmessage", m, MessageCTSContext.Default.MessageCTS, STCContext.Default.STC, CancellationToken); if (data.Error is not null && data.ErrorMessage != "Server responded with empty data") throw new Exception(data.ErrorMessage); return Task.CompletedTask; } } [JsonSerializable(typeof(MainSocketTextChannel))] [JsonSourceGenerationOptions( GenerationMode = JsonSourceGenerationMode.Default, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, WriteIndented = false)] internal partial class MainSocketTextChannelContext : JsonSerializerContext { }