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.Tasks; namespace Luski.net.JsonTypes; public class SocketTextChannel : SocketChannel, ITextChannel { public async Task GetMessage(long ID) { return await SocketMessage.GetMessage(ID); } public async Task GetPicture() { if (Type == ChannelType.DM) return Members.First().GetAvatar().Result; else { if (Server.Cache != null) { bool isc = System.IO.File.Exists($"{Server.Cache}/channels/{Id}"); if (!isc) await Server.GetFromServer($"SocketChannel/GetPicture/{Id}", $"{Server.Cache}/channels/{Id}"); } return System.IO.File.ReadAllBytes($"{Server.Cache}/channels/{Id}"); } } public async Task> GetMessages(long Message_Id, 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, 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 * Server.Percent) * 2.0)); if (num == 0) num = 1; string? key = Encryption.File.Channels.GetKey(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); }); key = null; return await Task.FromResult(data.Messages.ToList().AsReadOnly() as IReadOnlyList); } else { throw new Exception(data.ErrorMessage); } } } public async Task> GetMessages(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 { SocketBulkMessage data = await Server.GetFromServer("SocketBulkMessage", SocketBulkMessageContext.Default.SocketBulkMessage, new KeyValuePair("id", Id.ToString()), new KeyValuePair("messages", count.ToString())); if (data is not null && !data.Error.HasValue) { int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * Server.Percent) * 2.0)); if (num == 0) num = 1; string? key = Encryption.File.Channels.GetKey(Id); if (data.Messages is null) data.Messages = Array.Empty(); Parallel.ForEach(data.Messages, new ParallelOptions() { MaxDegreeOfParallelism = num }, i => { i.decrypt(key); }); key = null; 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, params File?[] Files) { string key = Encryption.File.Channels.GetKey(Id); if (Id == 0) key = Encryption.ServerPublicKey; HTTP.Message m = new() { Context = Convert.ToBase64String(Encryption.Encrypt(Message, key)), Channel = Id, }; 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)); Files[i] = null; } } m.Files = bb.ToArray(); } IncomingHTTP data = await Server.SendServer("socketmessage", m, HTTP.MessageContext.Default.Message, IncomingHTTPContext.Default.IncomingHTTP); if (data.Error is not null && data.ErrorMessage != "Server responded with empty data") throw new Exception(data.ErrorMessage); return Task.CompletedTask; } } [JsonSerializable(typeof(SocketTextChannel))] [JsonSourceGenerationOptions( GenerationMode = JsonSourceGenerationMode.Default, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, WriteIndented = false)] internal partial class SocketTextChannelContext : JsonSerializerContext { }