Luski.Net/Luski.net/Structures/Main/MainSocketTextChannel.cs

182 lines
7.6 KiB
C#
Raw Normal View History

2023-01-01 22:50:39 -05:00
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;
2023-01-01 22:50:39 -05:00
using System.Threading.Tasks;
using JacobTechEncryption;
using JacobTechEncryption.Enums;
2024-03-20 23:18:34 -04:00
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;
2023-01-01 22:50:39 -05:00
namespace Luski.net.Structures.Main;
2023-01-01 22:50:39 -05:00
public class MainSocketTextChannel : MainSocketChannel
2023-01-01 22:50:39 -05:00
{
public async Task<MainSocketMessage> GetMessage(long ID, CancellationToken CancellationToken)
2023-01-01 22:50:39 -05:00
{
return await Server.GetMessage(ID, CancellationToken);
2023-01-01 22:50:39 -05:00
}
public async Task<Stream> GetPicture(CancellationToken CancellationToken)
2023-01-01 22:50:39 -05:00
{
2024-08-27 10:57:22 -04:00
throw new NotImplementedException();
//if (Type == ChannelType.DM) return Members.First().GetAvatar(CancellationToken).Result;
// else
2023-01-01 22:50:39 -05:00
{
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());
2023-01-01 22:50:39 -05:00
}
}
public async Task<IReadOnlyList<MainSocketMessage>> GetMessages(long Message_Id, CancellationToken CancellationToken, int count = 50)
2023-01-01 22:50:39 -05:00
{
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,
2023-01-01 22:50:39 -05:00
new KeyValuePair<string, string?>("channel_id", Id.ToString()),
new KeyValuePair<string, string?>("messages", count.ToString()),
new KeyValuePair<string, string?>("mostrecentid", Message_Id.ToString()));
if (data.Error is null)
{
int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 5) * 2.0));
2023-01-01 22:50:39 -05:00
if (num == 0) num = 1;
string key = Server.EncryptionHandler.GetChannelKey(Id);
2023-01-01 22:50:39 -05:00
if (data is null) throw new Exception("Invalid data from server");
if (data.Messages is null) data.Messages = Array.Empty<MainSocketMessage>();
2023-01-01 22:50:39 -05:00
Parallel.ForEach(data.Messages, new ParallelOptions()
{
MaxDegreeOfParallelism = num
}, i =>
{
i.decrypt(key, CancellationToken);
2023-01-01 22:50:39 -05:00
});
key = null;
return await Task.FromResult(data.Messages.ToList().AsReadOnly() as IReadOnlyList<MainSocketMessage>);
2023-01-01 22:50:39 -05:00
}
else
{
throw new Exception(data.ErrorMessage);
}
}
}
public async Task<IReadOnlyList<MainSocketMessage>> GetMessages(CancellationToken CancellationToken, int count = 50)
2023-01-01 22:50:39 -05:00
{
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;
2023-01-01 22:50:39 -05:00
SocketBulkMessage data = await Server.GetFromServer("SocketBulkMessage",
SocketBulkMessageContext.Default.SocketBulkMessage,
CancellationToken,
2023-01-01 22:50:39 -05:00
new KeyValuePair<string, string?>("id", Id.ToString()),
new KeyValuePair<string, string?>("messages", count.ToString()));
Console.WriteLine($"Messages downloaded in {(DateTime.Now - start).TotalSeconds}");
start = DateTime.Now;
2023-01-01 22:50:39 -05:00
if (data is not null && !data.Error.HasValue)
{
int num = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 5) * 2.0));
2023-01-01 22:50:39 -05:00
if (num == 0) num = 1;
string key = Server.EncryptionHandler.GetChannelKey(Id);
if (data.Messages is null) data.Messages = Array.Empty<MainSocketMessage>();
2023-01-01 22:50:39 -05:00
Parallel.ForEach(data.Messages, new ParallelOptions()
{
MaxDegreeOfParallelism = num
}, i =>
{
i.decrypt(key, CancellationToken);
2023-01-01 22:50:39 -05:00
});
key = null;
Console.WriteLine($"Messages decrypted in {(DateTime.Now - start).TotalSeconds}");
return await Task.FromResult(data.Messages.ToList().AsReadOnly() as IReadOnlyList<MainSocketMessage>);
2023-01-01 22:50:39 -05:00
}
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<Task> SendMessage(string Message, CancellationToken CancellationToken, params File?[] Files)
2023-01-01 22:50:39 -05:00
{
string key = Server.EncryptionHandler.GetChannelKey(Id);
2024-03-20 23:18:34 -04:00
MessageCTS m = new()
2023-01-01 22:50:39 -05:00
{
2024-03-20 23:18:34 -04:00
Base64Context = Convert.ToBase64String(Encryption.RSA.Encrypt(Message, key, EncoderType.UTF8)),
ChannelID = Id,
Profile = null,
EncryptionKey = 0,
Encoding = EncoderType.UTF8
2023-01-01 22:50:39 -05:00
};
if (Files is not null && Files.Length > 0)
{
List<long> 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));
2023-01-01 22:50:39 -05:00
Files[i] = null;
}
}
m.Files = bb.ToArray();
}
2024-03-20 23:18:34 -04:00
STC data = await Server.SendServer("socketmessage", m, MessageCTSContext.Default.MessageCTS, STCContext.Default.STC, CancellationToken);
2023-01-01 22:50:39 -05:00
if (data.Error is not null && data.ErrorMessage != "Server responded with empty data") throw new Exception(data.ErrorMessage);
return Task.CompletedTask;
}
}
[JsonSerializable(typeof(MainSocketTextChannel))]
2023-01-01 22:50:39 -05:00
[JsonSourceGenerationOptions(
GenerationMode = JsonSourceGenerationMode.Default,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
WriteIndented = false)]
internal partial class MainSocketTextChannelContext : JsonSerializerContext
2023-01-01 22:50:39 -05:00
{
}