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 ;
2023-07-03 23:24:35 -04:00
using System.Threading ;
2023-01-01 22:50:39 -05:00
using System.Threading.Tasks ;
2023-07-03 23:24:35 -04:00
using JacobTechEncryption ;
using JacobTechEncryption.Enums ;
using Luski.net.Enums.Main ;
2023-01-01 22:50:39 -05:00
2023-07-03 23:24:35 -04:00
namespace Luski.net.Structures.Main ;
2023-01-01 22:50:39 -05:00
2023-07-03 23:24:35 -04:00
public class MainSocketTextChannel : MainSocketChannel
2023-01-01 22:50:39 -05:00
{
2023-07-08 09:06:13 -04:00
public async Task < MainSocketMessage > GetMessage ( long ID , CancellationToken CancellationToken )
2023-01-01 22:50:39 -05:00
{
2023-07-08 09:06:13 -04:00
return await Server . GetMessage ( ID , CancellationToken ) ;
2023-01-01 22:50:39 -05:00
}
2023-08-21 10:58:17 -04:00
public async Task < Stream > GetPicture ( CancellationToken CancellationToken )
2023-01-01 22:50:39 -05:00
{
2023-07-03 23:24:35 -04:00
if ( Type = = ChannelType . DM ) return Members . First ( ) . GetAvatar ( CancellationToken ) . Result ;
2023-01-01 22:50:39 -05:00
else
{
2023-07-10 07:35:05 -04: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 ) ;
2023-08-21 10:58:17 -04:00
return Server . Storage . GetResourceStream ( StorageDirectory . ChannelIcons , Id . ToString ( ) ) ;
2023-01-01 22:50:39 -05:00
}
}
2023-07-08 09:06:13 -04: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 ,
2023-07-03 23:24:35 -04:00
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 )
{
2023-07-03 23:24:35 -04:00
int num = Convert . ToInt32 ( Math . Ceiling ( ( Environment . ProcessorCount * 5 ) * 2.0 ) ) ;
2023-01-01 22:50:39 -05:00
if ( num = = 0 ) num = 1 ;
2023-07-10 14:19:03 -04:00
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" ) ;
2023-07-08 09:06:13 -04:00
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 = >
{
2023-07-03 23:24:35 -04:00
i . decrypt ( key , CancellationToken ) ;
2023-01-01 22:50:39 -05:00
} ) ;
key = null ;
2023-07-08 09:06:13 -04:00
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 ) ;
}
}
}
2023-07-08 09:06:13 -04:00
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
{
2023-07-03 23:24:35 -04:00
DateTime start = DateTime . Now ;
2023-01-01 22:50:39 -05:00
SocketBulkMessage data = await Server . GetFromServer ( "SocketBulkMessage" ,
SocketBulkMessageContext . Default . SocketBulkMessage ,
2023-07-03 23:24:35 -04:00
CancellationToken ,
2023-01-01 22:50:39 -05:00
new KeyValuePair < string , string? > ( "id" , Id . ToString ( ) ) ,
new KeyValuePair < string , string? > ( "messages" , count . ToString ( ) ) ) ;
2023-07-03 23:24:35 -04:00
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 )
{
2023-07-03 23:24:35 -04:00
int num = Convert . ToInt32 ( Math . Ceiling ( ( Environment . ProcessorCount * 5 ) * 2.0 ) ) ;
2023-01-01 22:50:39 -05:00
if ( num = = 0 ) num = 1 ;
2023-07-10 14:19:03 -04:00
string key = Server . EncryptionHandler . GetChannelKey ( Id ) ;
2023-07-08 09:06:13 -04:00
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 = >
{
2023-07-03 23:24:35 -04:00
i . decrypt ( key , CancellationToken ) ;
2023-01-01 22:50:39 -05:00
} ) ;
key = null ;
2023-07-03 23:24:35 -04:00
Console . WriteLine ( $"Messages decrypted in {(DateTime.Now - start).TotalSeconds}" ) ;
2023-07-08 09:06:13 -04:00
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 ;
}
}
2023-07-03 23:24:35 -04:00
public async Task < Task > SendMessage ( string Message , CancellationToken CancellationToken , params File ? [ ] Files )
2023-01-01 22:50:39 -05:00
{
2023-07-10 14:19:03 -04:00
string key = Server . EncryptionHandler . GetChannelKey ( Id ) ;
2023-07-03 23:24:35 -04:00
JsonTypes . HTTP . Message m = new ( )
2023-01-01 22:50:39 -05:00
{
2023-07-03 23:24:35 -04:00
Context = Convert . ToBase64String ( Encryption . RSA . Encrypt ( Message , key , EncoderType . UTF8 ) ) ,
2023-01-01 22:50:39 -05:00
Channel = Id ,
} ;
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 )
{
2023-07-03 23:24:35 -04:00
bb . Add ( await ff . Upload ( key , CancellationToken ) ) ;
2023-01-01 22:50:39 -05:00
Files [ i ] = null ;
}
}
m . Files = bb . ToArray ( ) ;
}
2023-07-03 23:24:35 -04:00
IncomingHTTP data = await Server . SendServer ( "socketmessage" , m , net . JsonTypes . HTTP . MessageContext . Default . Message , IncomingHTTPContext . Default . IncomingHTTP , 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 ;
}
}
2023-07-03 23:24:35 -04:00
[JsonSerializable(typeof(MainSocketTextChannel))]
2023-01-01 22:50:39 -05:00
[ JsonSourceGenerationOptions (
GenerationMode = JsonSourceGenerationMode . Default ,
PropertyNamingPolicy = JsonKnownNamingPolicy . CamelCase ,
WriteIndented = false ) ]
2023-07-03 23:24:35 -04:00
internal partial class MainSocketTextChannelContext : JsonSerializerContext
2023-01-01 22:50:39 -05:00
{
}