2021-09-25 02:13:06 +02:00
using System ;
2021-10-23 01:37:50 +02:00
using System.Collections.Generic ;
2021-09-25 02:13:06 +02:00
using System.Globalization ;
2022-10-25 20:07:43 +02:00
using System.IO ;
2021-09-23 09:29:19 +02:00
using System.Linq ;
2021-08-19 06:56:55 +02:00
using System.Text ;
using System.Web ;
namespace TL
2021-08-10 03:12:33 +02:00
{
2021-10-23 01:37:50 +02:00
public interface IPeerInfo
{
2021-10-22 15:26:46 +02:00
long ID { get ; }
bool IsActive { get ; }
2023-03-16 13:43:18 +01:00
string MainUsername { get ; }
2021-10-22 15:26:46 +02:00
InputPeer ToInputPeer ( ) ;
}
2022-11-20 17:15:57 +01:00
partial class InputPeer
{
public static readonly InputPeerSelf Self = new ( ) ;
public abstract long ID { get ; }
}
partial class InputPeerSelf
{
public override long ID = > 0 ;
}
2022-03-27 12:18:43 +02:00
partial class InputPeerChat
{
2023-12-05 01:13:19 +01:00
/// <summary>⚠ <b>This type is only for basic Chat</b>. See <see href="https://wiz0u.github.io/WTelegramClient/#terminology">Terminology</see> in the README to understand what this means<br/>Chat groups of type Channel must use <see cref="InputPeerChannel"/>.</summary>
2022-03-27 12:18:43 +02:00
/// <param name="chat_id">Chat identifier</param>
public InputPeerChat ( long chat_id ) = > this . chat_id = chat_id ;
2022-03-27 22:26:57 +02:00
internal InputPeerChat ( ) { }
2022-11-20 17:15:57 +01:00
public override long ID = > chat_id ;
2022-03-27 12:18:43 +02:00
}
partial class InputPeerUser
{
/// <param name="user_id">User identifier</param>
2022-11-20 17:15:57 +01:00
/// <param name="access_hash">⚠ <b>REQUIRED FIELD</b>. See FAQ for how to obtain it<br/><strong>access_hash</strong> value from the <see cref="User"/> structure</param>
2022-03-27 12:18:43 +02:00
public InputPeerUser ( long user_id , long access_hash ) { this . user_id = user_id ; this . access_hash = access_hash ; }
2022-03-27 22:26:57 +02:00
internal InputPeerUser ( ) { }
2022-03-27 12:18:43 +02:00
public static implicit operator InputUser ( InputPeerUser user ) = > new ( user . user_id , user . access_hash ) ;
2022-11-20 17:15:57 +01:00
public override long ID = > user_id ;
2022-03-27 12:18:43 +02:00
}
partial class InputPeerChannel
{
/// <param name="channel_id">Channel identifier</param>
2022-11-20 17:15:57 +01:00
/// <param name="access_hash">⚠ <b>REQUIRED FIELD</b>. See FAQ for how to obtain it<br/><strong>access_hash</strong> value from the <see cref="Channel"/> structure</param>
2022-03-27 12:18:43 +02:00
public InputPeerChannel ( long channel_id , long access_hash ) { this . channel_id = channel_id ; this . access_hash = access_hash ; }
2022-03-27 22:26:57 +02:00
internal InputPeerChannel ( ) { }
2022-03-27 12:18:43 +02:00
public static implicit operator InputChannel ( InputPeerChannel channel ) = > new ( channel . channel_id , channel . access_hash ) ;
2022-11-20 17:15:57 +01:00
public override long ID = > channel_id ;
}
partial class InputPeerUserFromMessage
{
public override long ID = > user_id ;
}
partial class InputPeerChannelFromMessage
{
public override long ID = > channel_id ;
2022-03-27 12:18:43 +02:00
}
2022-03-23 13:50:43 +01:00
partial class InputUserBase { public abstract long? UserId { get ; } }
partial class InputUserSelf { public override long? UserId = > null ; }
partial class InputUserFromMessage { public override long? UserId = > user_id ; }
partial class InputUser
{
public override long? UserId = > user_id ;
public static InputUserSelf Self = > new ( ) ;
2022-03-27 12:18:43 +02:00
/// <param name="user_id">User identifier</param>
2022-11-20 17:15:57 +01:00
/// <param name="access_hash">⚠ <b>REQUIRED FIELD</b>. See FAQ for how to obtain it<br/><strong>access_hash</strong> value from the <see cref="User"/> structure</param>
2022-03-27 12:18:43 +02:00
public InputUser ( long user_id , long access_hash ) { this . user_id = user_id ; this . access_hash = access_hash ; }
2022-03-27 22:26:57 +02:00
internal InputUser ( ) { }
2022-03-27 12:18:43 +02:00
public static implicit operator InputPeerUser ( InputUser user ) = > new ( user . user_id , user . access_hash ) ;
2022-03-23 13:50:43 +01:00
}
2021-10-23 01:37:50 +02:00
partial class InputFileBase
{
public abstract InputEncryptedFileBase ToInputEncryptedFile ( int key_fingerprint ) ;
public abstract InputSecureFileBase ToInputSecureFile ( byte [ ] file_hash , byte [ ] secret ) ;
2022-10-26 14:26:22 +02:00
/// <param name="isSquareVideo10s"><see langword="false"/> for a profile photo. <see langword="null"/> for auto-detection<br/><see langword="true"/> for a profile video. The video <u>MUST</u> be square, 10 seconds max, larger than 160x160</param>
2022-10-25 20:07:43 +02:00
public InputChatUploadedPhoto ToInputChatPhoto ( bool? isSquareVideo10s = null )
{
if ( isSquareVideo10s ? ? Path . GetExtension ( Name ) ? . ToLowerInvariant ( ) is ".mp4" )
return new InputChatUploadedPhoto { video = this , flags = InputChatUploadedPhoto . Flags . has_video } ;
else
return new InputChatUploadedPhoto { file = this , flags = InputChatUploadedPhoto . Flags . has_file } ;
}
2021-10-23 01:37:50 +02:00
}
partial class InputFile
{
public override InputEncryptedFileBase ToInputEncryptedFile ( int key_fingerprint ) = > new InputEncryptedFileUploaded { id = id , parts = parts , md5_checksum = md5_checksum , key_fingerprint = key_fingerprint } ;
public override InputSecureFileBase ToInputSecureFile ( byte [ ] file_hash , byte [ ] secret ) = > new InputSecureFileUploaded { id = id , parts = parts , md5_checksum = md5_checksum , file_hash = file_hash , secret = secret } ;
}
partial class InputFileBig
{
public override InputEncryptedFileBase ToInputEncryptedFile ( int key_fingerprint ) = > new InputEncryptedFileBigUploaded { id = id , parts = parts , key_fingerprint = key_fingerprint } ;
public override InputSecureFileBase ToInputSecureFile ( byte [ ] file_hash , byte [ ] secret ) = > new InputSecureFileUploaded { id = id , parts = parts , file_hash = file_hash , secret = secret } ;
}
2022-11-26 23:10:06 +01:00
partial class InputMediaUploadedDocument
{
public InputMediaUploadedDocument ( ) { }
public InputMediaUploadedDocument ( InputFileBase inputFile , string mimeType )
{
file = inputFile ;
mime_type = mimeType ;
if ( inputFile . Name is string filename ) attributes = new [ ] { new DocumentAttributeFilename { file_name = filename } } ;
}
2023-11-25 19:16:51 +01:00
public InputMediaUploadedDocument ( InputFileBase inputFile , string mimeType , params DocumentAttribute [ ] attribs )
{
file = inputFile ;
mime_type = mimeType ;
if ( inputFile . Name is string filename & & ! attribs . Any ( a = > a is DocumentAttributeFilename ) )
attributes = attribs . Append ( new DocumentAttributeFilename { file_name = filename } ) . ToArray ( ) ;
else
attributes = attribs ;
}
2022-11-26 23:10:06 +01:00
}
2022-01-20 02:44:43 +01:00
partial class InputPhoto
{
public static implicit operator InputMediaPhoto ( InputPhoto photo ) = > new ( ) { id = photo } ;
}
2022-12-12 10:07:07 +01:00
/// <remarks>Use the <c>UserOrChat(peer)</c> method from the root class you received, in order to convert this to a more useful <see cref="User"/> or <see cref="ChatBase"/></remarks>
2021-10-23 01:37:50 +02:00
partial class Peer
{
public abstract long ID { get ; }
2023-04-09 14:14:56 +02:00
protected internal abstract IPeerInfo UserOrChat ( Dictionary < long , User > users , Dictionary < long , ChatBase > chats ) ;
2024-01-16 14:55:51 +01:00
public static implicit operator long ( Peer peer ) = > peer . ID ;
2021-10-23 01:37:50 +02:00
}
partial class PeerUser
{
public override string ToString ( ) = > "user " + user_id ;
public override long ID = > user_id ;
2023-04-09 14:14:56 +02:00
protected internal override IPeerInfo UserOrChat ( Dictionary < long , User > users , Dictionary < long , ChatBase > chats ) = > users . TryGetValue ( user_id , out var user ) ? user : null ;
2021-10-23 01:37:50 +02:00
}
partial class PeerChat
{
public override string ToString ( ) = > "chat " + chat_id ;
public override long ID = > chat_id ;
2023-04-09 14:14:56 +02:00
protected internal override IPeerInfo UserOrChat ( Dictionary < long , User > users , Dictionary < long , ChatBase > chats ) = > chats . TryGetValue ( chat_id , out var chat ) ? chat : null ;
2021-10-23 01:37:50 +02:00
}
partial class PeerChannel
{
public override string ToString ( ) = > "channel " + channel_id ;
public override long ID = > channel_id ;
2023-04-09 14:14:56 +02:00
protected internal override IPeerInfo UserOrChat ( Dictionary < long , User > users , Dictionary < long , ChatBase > chats ) = > chats . TryGetValue ( channel_id , out var chat ) ? chat : null ;
2021-10-23 01:37:50 +02:00
}
partial class UserBase : IPeerInfo
{
public abstract long ID { get ; }
public abstract bool IsActive { get ; }
2023-03-16 13:43:18 +01:00
public abstract string MainUsername { get ; }
2021-10-23 01:37:50 +02:00
public abstract InputPeer ToInputPeer ( ) ;
2021-12-22 04:46:01 +01:00
protected abstract InputUser ToInputUser ( ) ;
2021-12-25 03:20:22 +01:00
public static implicit operator InputPeer ( UserBase user ) = > user ? . ToInputPeer ( ) ;
public static implicit operator InputUser ( UserBase user ) = > user ? . ToInputUser ( ) ;
2021-10-23 01:37:50 +02:00
}
partial class UserEmpty
{
public override long ID = > id ;
public override bool IsActive = > false ;
2023-03-16 13:43:18 +01:00
public override string MainUsername = > null ;
2021-10-23 01:37:50 +02:00
public override string ToString ( ) = > null ;
public override InputPeer ToInputPeer ( ) = > null ;
2021-12-22 04:46:01 +01:00
protected override InputUser ToInputUser ( ) = > null ;
2021-10-23 01:37:50 +02:00
}
partial class User
{
public override long ID = > id ;
public override bool IsActive = > ( flags & Flags . deleted ) = = 0 ;
2023-03-16 13:43:18 +01:00
public override string MainUsername = > username ? ? usernames ? . FirstOrDefault ( u = > u . flags . HasFlag ( Username . Flags . active ) ) ? . username ;
2023-02-15 18:42:52 +01:00
public override string ToString ( ) = > MainUsername is string uname ? '@' + uname : last_name = = null ? first_name : $"{first_name} {last_name}" ;
2022-03-27 12:18:43 +02:00
public override InputPeer ToInputPeer ( ) = > new InputPeerUser ( id , access_hash ) ;
protected override InputUser ToInputUser ( ) = > new ( id , access_hash ) ;
2021-12-10 17:21:39 +01:00
/// <summary>An estimation of the number of days ago the user was last seen (Online=0, Recently=1, LastWeek=5, LastMonth=20, LongTimeAgo=150)</summary>
public TimeSpan LastSeenAgo = > status ? . LastSeenAgo ? ? TimeSpan . FromDays ( 150 ) ;
2023-03-16 13:43:18 +01:00
public bool IsBot = > ( flags & Flags . bot ) ! = 0 ;
2023-05-17 11:45:06 +02:00
public IEnumerable < string > ActiveUsernames
{
get
{
if ( username ! = null )
yield return username ;
if ( usernames ! = null )
foreach ( var un in usernames )
if ( un . flags . HasFlag ( Username . Flags . active ) )
yield return un . username ;
}
}
2021-10-23 01:37:50 +02:00
}
2022-11-26 14:16:52 +01:00
/// <remarks>a <c>null</c> value means <a href="https://corefork.telegram.org/constructor/userStatusEmpty">userStatusEmpty</a> = last seen a long time ago, more than a month (or blocked/deleted users)</remarks>
partial class UserStatus { internal abstract TimeSpan LastSeenAgo { get ; } }
partial class UserStatusOnline { internal override TimeSpan LastSeenAgo = > TimeSpan . Zero ; }
partial class UserStatusOffline { internal override TimeSpan LastSeenAgo = > DateTime . UtcNow - new DateTime ( ( was_online + 62135596800L ) * 10000000 , DateTimeKind . Utc ) ; }
2021-12-10 17:21:39 +01:00
/// <remarks>covers anything between 1 second and 2-3 days</remarks>
2022-11-26 14:16:52 +01:00
partial class UserStatusRecently { internal override TimeSpan LastSeenAgo = > TimeSpan . FromDays ( 1 ) ; }
2021-12-10 17:21:39 +01:00
/// <remarks>between 2-3 and seven days</remarks>
2022-11-26 14:16:52 +01:00
partial class UserStatusLastWeek { internal override TimeSpan LastSeenAgo = > TimeSpan . FromDays ( 5 ) ; }
2021-12-10 17:21:39 +01:00
/// <remarks>between 6-7 days and a month</remarks>
2022-11-26 14:16:52 +01:00
partial class UserStatusLastMonth { internal override TimeSpan LastSeenAgo = > TimeSpan . FromDays ( 20 ) ; }
2021-12-10 17:21:39 +01:00
2021-10-22 15:26:46 +02:00
partial class ChatBase : IPeerInfo
2021-08-10 03:12:33 +02:00
{
2022-01-18 15:24:04 +01:00
/// <summary>Is this chat among current user active chats?</summary>
2021-10-22 15:26:46 +02:00
public abstract bool IsActive { get ; }
2023-12-05 01:13:19 +01:00
/// <summary>Is this chat a broadcast channel?</summary>
2023-04-25 20:47:57 +02:00
public virtual bool IsChannel = > false ;
public bool IsGroup = > ! IsChannel ;
2023-03-16 13:43:18 +01:00
public virtual string MainUsername = > null ;
2021-12-28 09:38:22 +01:00
public abstract ChatPhoto Photo { get ; }
2021-08-14 15:15:41 +02:00
/// <summary>returns true if you're banned of any of these rights</summary>
2021-08-14 08:55:30 +02:00
public abstract bool IsBanned ( ChatBannedRights . Flags flags = 0 ) ;
2021-10-22 15:26:46 +02:00
public abstract InputPeer ToInputPeer ( ) ;
2023-04-08 16:32:19 +02:00
public static implicit operator InputPeer ( ChatBase chat ) = > chat ? . ToInputPeer ( ) ;
2021-08-10 03:12:33 +02:00
}
partial class ChatEmpty
{
2021-10-22 15:26:46 +02:00
public override bool IsActive = > false ;
2021-12-28 09:38:22 +01:00
public override ChatPhoto Photo = > null ;
2021-08-14 08:55:30 +02:00
public override bool IsBanned ( ChatBannedRights . Flags flags = 0 ) = > true ;
2021-10-22 15:26:46 +02:00
public override InputPeer ToInputPeer ( ) = > null ;
2021-10-01 02:22:26 +02:00
public override string ToString ( ) = > $"ChatEmpty {id}" ;
2021-08-10 03:12:33 +02:00
}
partial class Chat
{
2022-04-13 16:02:02 +02:00
public override bool IsActive = > ( flags & ( Flags . left | Flags . deactivated ) ) = = 0 ;
2021-12-28 09:38:22 +01:00
public override ChatPhoto Photo = > photo ;
2021-08-14 08:55:30 +02:00
public override bool IsBanned ( ChatBannedRights . Flags flags = 0 ) = > ( ( default_banned_rights ? . flags ? ? 0 ) & flags ) ! = 0 ;
2022-03-27 12:18:43 +02:00
public override InputPeer ToInputPeer ( ) = > new InputPeerChat ( id ) ;
2021-11-05 04:37:21 +01:00
public override string ToString ( ) = > $"Chat \" { title } \ "" + ( flags . HasFlag ( Flags . deactivated ) ? " [deactivated]" : null ) ;
2021-08-10 03:12:33 +02:00
}
partial class ChatForbidden
{
2021-10-22 15:26:46 +02:00
public override bool IsActive = > false ;
2021-12-28 09:38:22 +01:00
public override ChatPhoto Photo = > null ;
2021-08-14 08:55:30 +02:00
public override bool IsBanned ( ChatBannedRights . Flags flags = 0 ) = > true ;
2022-03-27 12:18:43 +02:00
public override InputPeer ToInputPeer ( ) = > new InputPeerChat ( id ) ;
2021-10-01 02:22:26 +02:00
public override string ToString ( ) = > $"ChatForbidden {id} \" { title } \ "" ;
2021-08-10 03:12:33 +02:00
}
partial class Channel
{
2021-10-22 15:26:46 +02:00
public override bool IsActive = > ( flags & Flags . left ) = = 0 ;
2023-04-25 20:47:57 +02:00
public override bool IsChannel = > ( flags & Flags . broadcast ) ! = 0 ;
2023-05-04 17:07:47 +02:00
public override string MainUsername = > username ? ? usernames ? . FirstOrDefault ( un = > un . flags . HasFlag ( Username . Flags . active ) ) ? . username ;
2021-12-28 09:38:22 +01:00
public override ChatPhoto Photo = > photo ;
2021-08-14 08:55:30 +02:00
public override bool IsBanned ( ChatBannedRights . Flags flags = 0 ) = > ( ( banned_rights ? . flags ? ? 0 ) & flags ) ! = 0 | | ( ( default_banned_rights ? . flags ? ? 0 ) & flags ) ! = 0 ;
2022-03-27 12:18:43 +02:00
public override InputPeer ToInputPeer ( ) = > new InputPeerChannel ( id , access_hash ) ;
public static implicit operator InputChannel ( Channel channel ) = > new ( channel . id , channel . access_hash ) ;
2023-02-15 18:42:52 +01:00
public override string ToString ( ) = > ( flags . HasFlag ( Flags . broadcast ) ? "Channel " : "Group " ) + ( MainUsername is string uname ? '@' + uname : $"\" { title } \ "" ) ;
2023-05-04 17:07:47 +02:00
public IEnumerable < string > ActiveUsernames
{
get
{
if ( username ! = null )
yield return username ;
if ( usernames ! = null )
foreach ( var un in usernames )
if ( un . flags . HasFlag ( Username . Flags . active ) )
yield return un . username ;
}
}
2021-08-10 03:12:33 +02:00
}
partial class ChannelForbidden
{
2021-10-22 15:26:46 +02:00
public override bool IsActive = > false ;
2023-04-25 20:47:57 +02:00
public override bool IsChannel = > ( flags & Flags . broadcast ) ! = 0 ;
2021-12-28 09:38:22 +01:00
public override ChatPhoto Photo = > null ;
2021-08-14 08:55:30 +02:00
public override bool IsBanned ( ChatBannedRights . Flags flags = 0 ) = > true ;
2022-03-27 12:18:43 +02:00
public override InputPeer ToInputPeer ( ) = > new InputPeerChannel ( id , access_hash ) ;
2021-10-01 02:22:26 +02:00
public override string ToString ( ) = > $"ChannelForbidden {id} \" { title } \ "" ;
2021-08-10 03:12:33 +02:00
}
2021-08-16 05:56:39 +02:00
2022-01-18 15:24:04 +01:00
partial class ChatFullBase { public abstract int ParticipantsCount { get ; } }
partial class ChatFull { public override int ParticipantsCount = > participants . Participants . Length ; }
partial class ChannelFull { public override int ParticipantsCount = > participants_count ; }
2021-10-23 03:36:46 +02:00
partial class ChatParticipantBase { public abstract bool IsAdmin { get ; } }
partial class ChatParticipant { public override bool IsAdmin = > false ; }
partial class ChatParticipantCreator { public override bool IsAdmin = > true ; }
partial class ChatParticipantAdmin { public override bool IsAdmin = > true ; }
2021-10-23 01:37:50 +02:00
2022-01-18 15:24:04 +01:00
partial class ChatParticipantsBase { public abstract ChatParticipantBase [ ] Participants { get ; } }
partial class ChatParticipantsForbidden { public override ChatParticipantBase [ ] Participants = > Array . Empty < ChatParticipantBase > ( ) ; }
partial class ChatParticipants { public override ChatParticipantBase [ ] Participants = > participants ; }
2022-11-20 17:15:57 +01:00
partial class MessageEmpty { public override string ToString ( ) = > "(no message)" ; }
partial class Message { public override string ToString ( ) = > $"{(from_id ?? peer_id)?.ID}> {message} {media}" ; }
partial class MessageService { public override string ToString ( ) = > $"{(from_id ?? peer_id)?.ID} [{action.GetType().Name[13..]}]" ; }
2022-09-04 15:24:59 +02:00
partial class MessageMedia { ///<summary>Use this helper method to send a copy of the media without downloading it</summary>
2022-09-10 18:23:01 +02:00
///<remarks>Quiz poll may need to be voted before obtaining the correct answers. Dice will not replicate same value. TTL ignored<br/>May return <see langword="null"/> for Invoice and other unsupported media types</remarks>
2022-09-04 15:24:59 +02:00
public virtual InputMedia ToInputMedia ( ) = > null ; }
partial class MessageMediaPhoto { public override InputMedia ToInputMedia ( ) = > new InputMediaPhoto { id = photo } ; }
partial class MessageMediaGeo { public override InputMedia ToInputMedia ( ) = > new InputMediaGeoPoint { geo_point = geo } ; }
2022-09-10 18:23:01 +02:00
partial class MessageMediaContact { public override InputMedia ToInputMedia ( ) = > new InputMediaContact { phone_number = phone_number , first_name = first_name , last_name = last_name , vcard = vcard } ; }
2022-09-04 15:24:59 +02:00
partial class MessageMediaDocument { public override InputMedia ToInputMedia ( ) = > new InputMediaDocument { id = document } ; }
partial class MessageMediaVenue { public override InputMedia ToInputMedia ( ) = > new InputMediaVenue { geo_point = geo , title = title , address = address , provider = provider , venue_id = venue_id , venue_type = venue_type } ; }
partial class MessageMediaGame { public override InputMedia ToInputMedia ( ) = > new InputMediaGame { id = game } ; }
partial class MessageMediaGeoLive { public override InputMedia ToInputMedia ( ) = > new InputMediaGeoLive { geo_point = geo , heading = heading , period = period , proximity_notification_radius = proximity_notification_radius ,
flags = ( period ! = 0 ? InputMediaGeoLive . Flags . has_period : 0 ) | ( flags . HasFlag ( Flags . has_heading ) ? InputMediaGeoLive . Flags . has_heading : 0 ) | ( flags . HasFlag ( Flags . has_proximity_notification_radius ) ? InputMediaGeoLive . Flags . has_proximity_notification_radius : 0 ) } ; }
2022-09-10 18:23:01 +02:00
partial class MessageMediaPoll { public override InputMedia ToInputMedia ( ) = > new InputMediaPoll { poll = poll , solution = results . solution , solution_entities = results . solution_entities ,
correct_answers = results . results ? . Where ( pav = > pav . flags . HasFlag ( PollAnswerVoters . Flags . correct ) ) . Select ( pav = > pav . option ) . ToArray ( ) ,
2022-09-04 15:24:59 +02:00
flags = ( results . results ! = null ? InputMediaPoll . Flags . has_correct_answers : 0 ) | ( results . solution ! = null ? InputMediaPoll . Flags . has_solution : 0 ) } ; }
partial class MessageMediaDice { public override InputMedia ToInputMedia ( ) = > new InputMediaDice { emoticon = emoticon } ; }
2023-10-28 23:47:04 +02:00
partial class MessageMediaWebPage { public override InputMedia ToInputMedia ( ) = > new InputMediaWebPage { flags = ( InputMediaWebPage . Flags ) ( ( int ) flags & 3 ) , url = webpage . Url } ; }
2022-09-04 15:24:59 +02:00
2021-08-10 03:12:33 +02:00
partial class PhotoBase
{
public abstract long ID { get ; }
2021-09-30 03:40:08 +02:00
protected abstract InputPhoto ToInputPhoto ( ) ;
public static implicit operator InputPhoto ( PhotoBase photo ) = > photo . ToInputPhoto ( ) ;
2022-01-20 02:44:43 +01:00
public static implicit operator InputMediaPhoto ( PhotoBase photo ) = > photo . ToInputPhoto ( ) ;
2021-08-10 03:12:33 +02:00
}
partial class PhotoEmpty
{
public override long ID = > id ;
2021-09-30 03:40:08 +02:00
protected override InputPhoto ToInputPhoto ( ) = > null ;
2021-08-10 03:12:33 +02:00
}
partial class Photo
{
public override long ID = > id ;
2021-09-30 03:40:08 +02:00
protected override InputPhoto ToInputPhoto ( ) = > new ( ) { id = id , access_hash = access_hash , file_reference = file_reference } ;
2021-09-23 09:29:19 +02:00
public InputPhotoFileLocation ToFileLocation ( ) = > ToFileLocation ( LargestPhotoSize ) ;
2021-08-10 03:12:33 +02:00
public InputPhotoFileLocation ToFileLocation ( PhotoSizeBase photoSize ) = > new ( ) { id = id , access_hash = access_hash , file_reference = file_reference , thumb_size = photoSize . Type } ;
2021-09-23 09:29:19 +02:00
public PhotoSizeBase LargestPhotoSize = > sizes . Aggregate ( ( agg , next ) = > ( long ) next . Width * next . Height > ( long ) agg . Width * agg . Height ? next : agg ) ;
2021-08-10 03:12:33 +02:00
}
2021-09-23 09:29:19 +02:00
partial class PhotoSizeBase
{
public abstract int Width { get ; }
public abstract int Height { get ; }
public abstract int FileSize { get ; }
}
partial class PhotoSizeEmpty
{
public override int Width = > 0 ;
public override int Height = > 0 ;
public override int FileSize = > 0 ;
}
partial class PhotoSize
{
public override int Width = > w ;
public override int Height = > h ;
public override int FileSize = > size ;
}
partial class PhotoCachedSize
{
public override int Width = > w ;
public override int Height = > h ;
public override int FileSize = > bytes . Length ;
}
partial class PhotoStrippedSize
{
public override int Width = > bytes [ 2 ] ;
public override int Height = > bytes [ 1 ] ;
public override int FileSize = > bytes . Length ;
}
partial class PhotoSizeProgressive
{
public override int Width = > w ;
public override int Height = > h ;
public override int FileSize = > sizes . Last ( ) ;
}
partial class PhotoPathSize
{
public override int Width = > - 1 ;
public override int Height = > - 1 ;
public override int FileSize = > bytes . Length ;
}
2021-08-20 14:45:39 +02:00
namespace Layer23
{
2021-09-23 09:29:19 +02:00
partial class PhotoSize
{
public override int Width = > w ;
public override int Height = > h ;
public override int FileSize = > size ;
}
partial class PhotoCachedSize
{
public override int Width = > w ;
public override int Height = > h ;
public override int FileSize = > bytes . Length ;
}
2021-08-20 14:45:39 +02:00
}
2021-08-10 03:12:33 +02:00
2022-09-04 15:24:59 +02:00
partial class GeoPoint
{
public static implicit operator InputGeoPoint ( GeoPoint geo ) = > new ( ) { lat = geo . lat , lon = geo . lon , accuracy_radius = geo . accuracy_radius , flags = ( InputGeoPoint . Flags ) geo . flags } ;
}
2023-10-09 15:19:03 +02:00
partial class InputNotifyPeerBase
{
public static implicit operator InputNotifyPeerBase ( InputPeer peer ) = > new InputNotifyPeer { peer = peer } ;
public static implicit operator InputNotifyPeerBase ( ChatBase chat ) = > new InputNotifyPeer { peer = chat } ;
public static implicit operator InputNotifyPeerBase ( UserBase user ) = > new InputNotifyPeer { peer = user } ;
}
2022-12-02 01:42:39 +01:00
partial class WallPaperBase { public static implicit operator InputWallPaperBase ( WallPaperBase wp ) = > wp . ToInputWallPaper ( ) ;
protected abstract InputWallPaperBase ToInputWallPaper ( ) ; }
partial class WallPaper { protected override InputWallPaperBase ToInputWallPaper ( ) = > new InputWallPaper { id = id , access_hash = access_hash } ; }
partial class WallPaperNoFile { protected override InputWallPaperBase ToInputWallPaper ( ) = > new InputWallPaperNoFile { id = id } ; }
2022-06-25 17:05:27 +02:00
partial class Contacts_Blocked { public IPeerInfo UserOrChat ( PeerBlocked peer ) = > peer . peer_id ? . UserOrChat ( users , chats ) ; }
2021-12-12 20:38:13 +01:00
partial class Messages_DialogsBase { public IPeerInfo UserOrChat ( DialogBase dialog ) = > UserOrChat ( dialog . Peer ) ;
public abstract int TotalCount { get ; } }
partial class Messages_Dialogs { public override int TotalCount = > dialogs . Length ; }
partial class Messages_DialogsSlice { public override int TotalCount = > count ; }
partial class Messages_DialogsNotModified { public override int TotalCount = > count ; }
2021-10-23 01:37:50 +02:00
2022-02-13 02:50:10 +01:00
partial class Messages_MessagesBase { public abstract int Count { get ; } public abstract int Offset { get ; } }
partial class Messages_Messages { public override int Count = > messages . Length ; public override int Offset = > 0 ; }
partial class Messages_MessagesSlice { public override int Count = > count ; public override int Offset = > offset_id_offset ; }
partial class Messages_ChannelMessages { public override int Count = > count ; public override int Offset = > offset_id_offset ; }
partial class Messages_MessagesNotModified { public override int Count = > count ; public override int Offset = > 0 ; }
2021-10-23 01:37:50 +02:00
2021-10-23 03:36:46 +02:00
partial class Updates_DifferenceBase { public abstract Updates_State State { get ; } }
partial class Updates_DifferenceEmpty { public override Updates_State State = > null ; }
partial class Updates_Difference { public override Updates_State State = > state ; }
partial class Updates_DifferenceSlice { public override Updates_State State = > intermediate_state ; }
partial class Updates_DifferenceTooLong { public override Updates_State State = > null ; }
2021-10-23 01:37:50 +02:00
2022-01-07 00:24:47 +01:00
partial class UpdatesBase
{
public abstract Update [ ] UpdateList { get ; }
public virtual Dictionary < long , User > Users = > NoUsers ;
public virtual Dictionary < long , ChatBase > Chats = > NoChats ;
private static readonly Dictionary < long , User > NoUsers = new ( ) ;
private static readonly Dictionary < long , ChatBase > NoChats = new ( ) ;
2023-12-17 23:39:55 +01:00
public virtual ( long mbox_id , int pts , int pts_count ) GetMBox ( ) = > default ;
2022-01-07 00:24:47 +01:00
}
partial class UpdatesCombined
{
public override Update [ ] UpdateList = > updates ;
public override Dictionary < long , User > Users = > users ;
public override Dictionary < long , ChatBase > Chats = > chats ;
2023-12-17 23:39:55 +01:00
public override ( long mbox_id , int pts , int pts_count ) GetMBox ( ) = > ( - 2 , seq , seq - seq_start + 1 ) ;
2022-01-07 00:24:47 +01:00
}
partial class Updates
{
public override Update [ ] UpdateList = > updates ;
public override Dictionary < long , User > Users = > users ;
public override Dictionary < long , ChatBase > Chats = > chats ;
2023-12-17 23:39:55 +01:00
public override ( long mbox_id , int pts , int pts_count ) GetMBox ( ) = > ( - 2 , seq , 1 ) ;
2022-01-07 00:24:47 +01:00
}
partial class UpdatesTooLong { public override Update [ ] UpdateList = > Array . Empty < Update > ( ) ; }
partial class UpdateShort { public override Update [ ] UpdateList = > new [ ] { update } ; }
partial class UpdateShortSentMessage { public override Update [ ] UpdateList = > Array . Empty < Update > ( ) ; }
partial class UpdateShortMessage { public override Update [ ] UpdateList = > new [ ] { new UpdateNewMessage
{
message = new Message
{
2022-05-13 23:16:48 +02:00
flags = ( Message . Flags ) flags | ( flags . HasFlag ( Flags . out_ ) ? 0 : Message . Flags . has_from_id ) , id = id , date = date ,
2022-01-07 00:24:47 +01:00
message = message , entities = entities , reply_to = reply_to ,
2022-05-13 23:16:48 +02:00
from_id = flags . HasFlag ( Flags . out_ ) ? null : new PeerUser { user_id = user_id } ,
2022-01-07 00:24:47 +01:00
peer_id = new PeerUser { user_id = user_id } ,
fwd_from = fwd_from , via_bot_id = via_bot_id , ttl_period = ttl_period
} , pts = pts , pts_count = pts_count
} } ; }
partial class UpdateShortChatMessage { public override Update [ ] UpdateList = > new [ ] { new UpdateNewMessage
{
message = new Message
{
flags = ( Message . Flags ) flags | Message . Flags . has_from_id , id = id , date = date ,
message = message , entities = entities , reply_to = reply_to ,
from_id = new PeerUser { user_id = from_id } ,
peer_id = new PeerChat { chat_id = chat_id } ,
fwd_from = fwd_from , via_bot_id = via_bot_id , ttl_period = ttl_period
} , pts = pts , pts_count = pts_count
} } ; }
2022-12-02 01:42:39 +01:00
partial class InputEncryptedChat { public static implicit operator int ( InputEncryptedChat chat ) = > chat . chat_id ;
public static implicit operator InputEncryptedChat ( EncryptedChatBase chat ) = > new ( ) { chat_id = chat . ID , access_hash = chat . AccessHash } ; }
2022-10-05 18:36:43 +02:00
2021-10-23 01:37:50 +02:00
partial class EncryptedFile
{
public static implicit operator InputEncryptedFile ( EncryptedFile file ) = > file = = null ? null : new InputEncryptedFile { id = file . id , access_hash = file . access_hash } ;
2022-10-07 03:00:57 +02:00
public static implicit operator InputEncryptedFileLocation ( EncryptedFile file ) = > file = = null ? null : new InputEncryptedFileLocation { id = file . id , access_hash = file . access_hash } ;
2021-10-23 01:37:50 +02:00
public InputEncryptedFileLocation ToFileLocation ( ) = > new ( ) { id = id , access_hash = access_hash } ;
}
2022-01-20 02:44:43 +01:00
partial class InputDocument
{
public static implicit operator InputMediaDocument ( InputDocument document ) = > new ( ) { id = document } ;
}
2021-08-10 03:12:33 +02:00
partial class DocumentBase
{
public abstract long ID { get ; }
2021-09-30 03:40:08 +02:00
protected abstract InputDocument ToInputDocument ( ) ;
public static implicit operator InputDocument ( DocumentBase document ) = > document . ToInputDocument ( ) ;
2022-01-20 02:44:43 +01:00
public static implicit operator InputMediaDocument ( DocumentBase document ) = > document . ToInputDocument ( ) ;
2021-08-10 03:12:33 +02:00
}
partial class DocumentEmpty
{
public override long ID = > id ;
2021-09-30 03:40:08 +02:00
protected override InputDocument ToInputDocument ( ) = > null ;
2021-08-10 03:12:33 +02:00
}
partial class Document
{
public override long ID = > id ;
2022-02-24 17:12:52 +01:00
public override string ToString ( ) = > Filename is string filename ? base . ToString ( ) + ": " + filename : base . ToString ( ) ;
2023-04-23 14:35:08 +02:00
public string Filename = > GetAttribute < DocumentAttributeFilename > ( ) ? . file_name ;
2021-09-30 03:40:08 +02:00
protected override InputDocument ToInputDocument ( ) = > new ( ) { id = id , access_hash = access_hash , file_reference = file_reference } ;
2021-09-23 09:29:19 +02:00
public InputDocumentFileLocation ToFileLocation ( PhotoSizeBase thumbSize = null ) = > new ( ) { id = id , access_hash = access_hash , file_reference = file_reference , thumb_size = thumbSize ? . Type } ;
2022-01-26 11:08:33 +01:00
public PhotoSizeBase LargestThumbSize = > thumbs ? . Aggregate ( ( agg , next ) = > ( long ) next . Width * next . Height > ( long ) agg . Width * agg . Height ? next : agg ) ;
2023-04-23 14:35:08 +02:00
public T GetAttribute < T > ( ) where T : DocumentAttribute = > attributes . OfType < T > ( ) . FirstOrDefault ( ) ;
2021-08-10 03:12:33 +02:00
}
2021-10-23 01:37:50 +02:00
partial class SendMessageAction
2021-08-10 03:12:33 +02:00
{
2021-10-23 01:37:50 +02:00
public override string ToString ( )
{
var type = GetType ( ) . Name [ 11. . ^ 6 ] ;
for ( int i = 1 ; i < type . Length ; i + + )
if ( char . IsUpper ( type [ i ] ) )
2021-11-20 12:54:49 +01:00
return type . ToLowerInvariant ( ) . Insert ( i , "ing " ) . Remove ( i - 1 , type [ i - 1 ] = = 'e' ? 1 : 0 ) ;
2021-10-23 01:37:50 +02:00
return type . ToLowerInvariant ( ) ;
}
2021-08-10 03:12:33 +02:00
}
2021-10-23 03:36:46 +02:00
partial class SpeakingInGroupCallAction { public override string ToString ( ) = > "speaking in group call" ; }
partial class SendMessageTypingAction { public override string ToString ( ) = > "typing" ; }
partial class SendMessageCancelAction { public override string ToString ( ) = > "stopping" ; }
partial class SendMessageGeoLocationAction { public override string ToString ( ) = > "selecting a location" ; }
partial class SendMessageGamePlayAction { public override string ToString ( ) = > "playing a game" ; }
partial class SendMessageHistoryImportAction { public override string ToString ( ) = > "importing history" ; }
2021-11-27 02:57:50 +01:00
partial class SendMessageEmojiInteraction { public override string ToString ( ) = > "clicking on emoji" ; }
partial class SendMessageEmojiInteractionSeen { public override string ToString ( ) = > "watching emoji reaction" ; }
2021-08-10 03:12:33 +02:00
2022-06-17 15:12:50 +02:00
partial class InputStickerSet
{
public static implicit operator InputStickerSet ( string shortName ) = > new InputStickerSetShortName { short_name = shortName } ;
}
2021-10-23 01:37:50 +02:00
partial class StickerSet
2021-08-10 03:12:33 +02:00
{
2021-10-23 01:37:50 +02:00
public static implicit operator InputStickerSetID ( StickerSet stickerSet ) = > new ( ) { id = stickerSet . id , access_hash = stickerSet . access_hash } ;
2023-04-23 14:35:08 +02:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060")]
public InputStickerSetThumb ToFileLocation ( PhotoSizeBase thumbSize ) = > new ( ) { stickerset = this , thumb_version = thumb_version } ;
public PhotoSizeBase LargestThumbSize = > thumbs ? . Aggregate ( ( agg , next ) = > ( long ) next . Width * next . Height > ( long ) agg . Width * agg . Height ? next : agg ) ;
2021-08-10 03:12:33 +02:00
}
2023-04-21 16:31:57 +02:00
partial class MessageEntity
{
public string Type { get { var name = GetType ( ) . Name ; return name [ ( name . IndexOf ( "MessageEntity" ) + 13 ) . . ] ; } }
public int Offset { get = > offset ; set = > offset = value ; }
public int Length { get = > length ; set = > length = value ; }
}
2022-03-27 12:18:43 +02:00
partial class InputChannel
{
/// <param name="channel_id">Channel identifier</param>
2022-11-20 17:15:57 +01:00
/// <param name="access_hash">⚠ <b>REQUIRED FIELD</b>. See FAQ for how to obtain it<br/><strong>access_hash</strong> value from the <see cref="Channel"/> structure</param>
2022-03-27 12:18:43 +02:00
public InputChannel ( long channel_id , long access_hash ) { this . channel_id = channel_id ; this . access_hash = access_hash ; }
2022-03-27 22:26:57 +02:00
internal InputChannel ( ) { }
2022-03-27 12:18:43 +02:00
public static implicit operator InputPeerChannel ( InputChannel channel ) = > new ( channel . channel_id , channel . access_hash ) ;
}
2021-12-10 17:21:39 +01:00
2021-10-23 01:37:50 +02:00
partial class Contacts_ResolvedPeer
2021-08-10 03:12:33 +02:00
{
2022-06-25 17:05:27 +02:00
public static implicit operator InputPeer ( Contacts_ResolvedPeer resolved ) = > resolved ? . UserOrChat . ToInputPeer ( ) ;
2022-01-11 04:14:23 +01:00
/// <returns>A <see cref="TL.User"/>, or <see langword="null"/> if the username was for a channel</returns>
2021-12-25 03:20:22 +01:00
public User User = > peer is PeerUser pu ? users [ pu . user_id ] : null ;
2023-06-27 16:00:55 +02:00
/// <returns>A <see cref="TL.Channel"/> or <see cref="TL.ChannelForbidden"/>, or <see langword="null"/> if the username was for a user</returns>
2022-01-11 04:14:23 +01:00
public ChatBase Chat = > peer is PeerChannel or PeerChat ? chats [ peer . ID ] : null ;
2023-06-27 16:00:55 +02:00
/// <returns>A <see cref="TL.Channel"/>, or <see langword="null"/> if the username was for a user or for a forbidden channel</returns>
public Channel Channel = > peer is PeerChannel pc ? chats [ pc . channel_id ] as Channel : null ;
2021-08-10 03:12:33 +02:00
}
2021-10-23 01:37:50 +02:00
partial class Updates_ChannelDifferenceBase
2021-08-10 03:12:33 +02:00
{
2021-10-23 01:37:50 +02:00
public abstract MessageBase [ ] NewMessages { get ; }
public abstract Update [ ] OtherUpdates { get ; }
public abstract bool Final { get ; }
public abstract int Timeout { get ; }
2021-08-10 03:12:33 +02:00
}
2021-10-23 01:37:50 +02:00
partial class Updates_ChannelDifferenceEmpty
2021-08-10 03:12:33 +02:00
{
2021-10-23 01:37:50 +02:00
public override MessageBase [ ] NewMessages = > Array . Empty < MessageBase > ( ) ;
public override Update [ ] OtherUpdates = > Array . Empty < Update > ( ) ;
public override bool Final = > flags . HasFlag ( Flags . final ) ;
public override int Timeout = > timeout ;
}
partial class Updates_ChannelDifference
{
public override MessageBase [ ] NewMessages = > new_messages ;
public override Update [ ] OtherUpdates = > other_updates ;
public override bool Final = > flags . HasFlag ( Flags . final ) ;
public override int Timeout = > timeout ;
}
partial class Updates_ChannelDifferenceTooLong
{
public override MessageBase [ ] NewMessages = > messages ;
public override Update [ ] OtherUpdates = > null ;
public override bool Final = > flags . HasFlag ( Flags . final ) ;
public override int Timeout = > timeout ;
2021-08-10 03:12:33 +02:00
}
2021-11-27 02:57:50 +01:00
2021-12-07 00:34:57 +01:00
partial class ChannelParticipantBase
{
public virtual bool IsAdmin = > false ;
2023-04-08 16:32:19 +02:00
public abstract long UserId { get ; }
2021-12-07 00:34:57 +01:00
}
partial class ChannelParticipantCreator
{
public override bool IsAdmin = > true ;
2023-04-08 16:32:19 +02:00
public override long UserId = > user_id ;
2021-12-07 00:34:57 +01:00
}
partial class ChannelParticipantAdmin
{
public override bool IsAdmin = > true ;
2023-04-08 16:32:19 +02:00
public override long UserId = > user_id ;
2021-12-07 00:34:57 +01:00
}
2023-04-08 16:32:19 +02:00
partial class ChannelParticipant { public override long UserId = > user_id ; }
partial class ChannelParticipantSelf { public override long UserId = > user_id ; }
partial class ChannelParticipantBanned { public override long UserId = > peer is PeerUser pu ? pu . user_id : 0 ; }
partial class ChannelParticipantLeft { public override long UserId = > peer is PeerUser pu ? pu . user_id : 0 ; }
2021-11-27 02:57:50 +01:00
2022-06-25 17:05:27 +02:00
partial class Messages_PeerDialogs { public IPeerInfo UserOrChat ( DialogBase dialog ) = > dialog . Peer ? . UserOrChat ( users , chats ) ; }
2021-10-23 01:37:50 +02:00
2022-09-04 15:24:59 +02:00
partial class Game { public static implicit operator InputGameID ( Game game ) = > new ( ) { id = game . id , access_hash = game . access_hash } ; }
2023-04-23 14:35:08 +02:00
partial class WebDocumentBase { public T GetAttribute < T > ( ) where T : DocumentAttribute = > Attributes . OfType < T > ( ) . FirstOrDefault ( ) ; }
2022-09-04 15:24:59 +02:00
partial class WebDocument { public static implicit operator InputWebFileLocation ( WebDocument doc ) = > new ( ) { url = doc . url , access_hash = doc . access_hash } ; }
2022-01-07 00:24:47 +01:00
2022-12-02 01:42:39 +01:00
partial class PhoneCallBase { public static implicit operator InputPhoneCall ( PhoneCallBase call ) = > new ( ) { id = call . ID , access_hash = call . AccessHash } ; }
2022-11-26 23:10:06 +01:00
2022-12-19 14:14:17 +01:00
partial class ChannelAdminLogEventsFilter
{
2023-05-03 14:24:52 +02:00
public static implicit operator ChannelAdminLogEventsFilter ( Flags flags ) = > flags = = 0 ? null : new ( ) { flags = flags } ;
2022-12-19 14:14:17 +01:00
}
2022-05-20 14:54:07 +02:00
partial class InputMessage
{
2022-11-26 23:10:06 +01:00
public static implicit operator InputMessage ( int id ) = > new InputMessageID { id = id } ;
2022-05-20 14:54:07 +02:00
}
2022-09-24 15:30:43 +02:00
partial class InputDialogPeerBase
{
2022-11-26 23:10:06 +01:00
public static implicit operator InputDialogPeerBase ( InputPeer peer ) = > new InputDialogPeer { peer = peer } ;
2023-10-09 15:19:03 +02:00
public static implicit operator InputDialogPeerBase ( ChatBase chat ) = > new InputDialogPeer { peer = chat } ;
public static implicit operator InputDialogPeerBase ( UserBase user ) = > new InputDialogPeer { peer = user } ;
2022-09-24 15:30:43 +02:00
}
2021-10-23 01:37:50 +02:00
partial class SecureFile
{
public static implicit operator InputSecureFile ( SecureFile file ) = > new ( ) { id = file . id , access_hash = file . access_hash } ;
public InputSecureFileLocation ToFileLocation ( ) = > new ( ) { id = id , access_hash = access_hash } ;
2021-08-10 03:12:33 +02:00
}
2021-08-16 05:56:39 +02:00
2021-11-14 15:35:41 +01:00
partial class JsonObjectValue { public override string ToString ( ) = > $"{HttpUtility.JavaScriptStringEncode(key, true)}:{value}" ; }
2023-11-11 12:25:02 +01:00
partial class JSONValue { public abstract object ToNative ( ) ;
private static JsonObjectValue FromJsonProperty ( System . Text . Json . JsonProperty p ) = > new ( ) { key = p . Name , value = FromJsonElement ( p . Value ) } ;
public static JSONValue FromJsonElement ( System . Text . Json . JsonElement elem ) = > elem . ValueKind switch
{
System . Text . Json . JsonValueKind . True or
System . Text . Json . JsonValueKind . False = > new JsonBool { value = elem . GetBoolean ( ) } ,
System . Text . Json . JsonValueKind . Object = > new JsonObject { value = elem . EnumerateObject ( ) . Select ( FromJsonProperty ) . ToArray ( ) } ,
System . Text . Json . JsonValueKind . Array = > new JsonArray { value = elem . EnumerateArray ( ) . Select ( FromJsonElement ) . ToArray ( ) } ,
System . Text . Json . JsonValueKind . String = > new JsonString { value = elem . GetString ( ) } ,
System . Text . Json . JsonValueKind . Number = > new JsonNumber { value = elem . GetDouble ( ) } ,
_ = > new JsonNull ( ) ,
} ;
}
2022-12-02 01:42:39 +01:00
partial class JsonNull { public override object ToNative ( ) = > null ; public override string ToString ( ) = > "null" ; }
partial class JsonBool { public override object ToNative ( ) = > value ; public override string ToString ( ) = > value ? "true" : "false" ; }
partial class JsonNumber { public override object ToNative ( ) = > value ; public override string ToString ( ) = > value . ToString ( CultureInfo . InvariantCulture ) ; }
partial class JsonString { public override object ToNative ( ) = > value ; public override string ToString ( ) = > HttpUtility . JavaScriptStringEncode ( value , true ) ; }
2021-08-19 06:56:55 +02:00
partial class JsonArray
{
public override string ToString ( )
{
var sb = new StringBuilder ( ) . Append ( '[' ) ;
for ( int i = 0 ; i < value . Length ; i + + )
sb . Append ( i = = 0 ? "" : "," ) . Append ( value [ i ] ) ;
return sb . Append ( ']' ) . ToString ( ) ;
}
2021-11-13 15:09:28 +01:00
public object [ ] ToNativeArray ( ) = > value . Select ( v = > v . ToNative ( ) ) . ToArray ( ) ;
public override object ToNative ( )
{
if ( value . Length = = 0 ) return Array . Empty < object > ( ) ;
var first = value [ 0 ] . ToNative ( ) ;
2021-11-14 15:35:41 +01:00
var T = first . GetType ( ) ;
var array = Array . CreateInstance ( T , value . Length ) ; // create an array T[] of the native type
2021-11-13 15:09:28 +01:00
array . SetValue ( first , 0 ) ;
for ( int i = 1 ; i < value . Length ; i + + )
{
var elem = value [ i ] . ToNative ( ) ;
2021-11-14 15:35:41 +01:00
if ( elem . GetType ( ) ! = T ) return ToNativeArray ( ) ; // incompatible => return an object[] instead
2021-11-13 15:09:28 +01:00
array . SetValue ( elem , i ) ;
}
return array ;
}
2021-08-19 06:56:55 +02:00
}
partial class JsonObject
{
2021-11-14 15:35:41 +01:00
/// <summary>Returns a JSON serialization string for this object</summary>
2021-08-19 06:56:55 +02:00
public override string ToString ( )
{
var sb = new StringBuilder ( ) . Append ( '{' ) ;
for ( int i = 0 ; i < value . Length ; i + + )
sb . Append ( i = = 0 ? "" : "," ) . Append ( value [ i ] ) ;
return sb . Append ( '}' ) . ToString ( ) ;
}
2021-11-14 15:35:41 +01:00
/// <summary>Returns the given entry in native form (<see langword="bool"/>, <see langword="double"/>, <see langword="string"/>, <see cref="Dictionary{TKey, TValue}">Dictionary</see> or <see cref="Array"/>), or <see langword="null"/> if the key is not found</summary>
public object this [ string key ] = > value . FirstOrDefault ( v = > v . key = = key ) ? . value . ToNative ( ) ;
/// <summary>Converts the entries to a Dictionary with keys and values in native form (<see langword="bool"/>, <see langword="double"/>, <see langword="string"/>, <see cref="Dictionary{TKey, TValue}">Dictionary</see> or <see cref="Array"/>)</summary>
2021-11-13 15:09:28 +01:00
public Dictionary < string , object > ToDictionary ( ) = > value . ToDictionary ( v = > v . key , v = > v . value . ToNative ( ) ) ;
public override object ToNative ( )
{
if ( value . Length = = 0 ) return new Dictionary < string , object > ( ) ;
var first = value [ 0 ] . value . ToNative ( ) ;
2021-11-14 15:35:41 +01:00
var T = first . GetType ( ) ; // create a Dictionary<string, T> of the native type T:
var dic = Activator . CreateInstance ( typeof ( Dictionary < , > ) . MakeGenericType ( typeof ( string ) , T ) ) as System . Collections . IDictionary ;
2021-11-13 15:09:28 +01:00
dic . Add ( value [ 0 ] . key , first ) ;
for ( int i = 1 ; i < value . Length ; i + + )
{
var elem = value [ i ] . value . ToNative ( ) ;
2021-11-14 15:35:41 +01:00
if ( elem . GetType ( ) ! = T ) return ToDictionary ( ) ; // incompatible => return a Dictionary<string, object> instead
2021-11-13 15:09:28 +01:00
dic . Add ( value [ i ] . key , elem ) ;
}
return dic ;
}
2021-08-19 06:56:55 +02:00
}
2022-11-26 23:10:06 +01:00
2022-12-02 01:42:39 +01:00
partial class Theme { public static implicit operator InputTheme ( Theme theme ) = > new ( ) { id = theme . id , access_hash = theme . access_hash } ; }
partial class GroupCallBase { public static implicit operator InputGroupCall ( GroupCallBase call ) = > new ( ) { id = call . ID , access_hash = call . AccessHash } ; }
2021-08-10 03:12:33 +02:00
}