From c2f228f7def6e59808e7936afad48b74c5ab4b19 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Sun, 27 Mar 2022 12:18:43 +0200 Subject: [PATCH] ctor for Input User/Channel with mandatory access_hash parameter --- .github/dev.yml | 2 +- .github/release.yml | 2 +- Examples/Program_CollectAccessHash.cs | 2 +- src/TL.Extensions.cs | 4 +-- src/TL.Helpers.cs | 49 +++++++++++++++++++++------ src/TL.Schema.cs | 2 +- 6 files changed, 44 insertions(+), 17 deletions(-) diff --git a/.github/dev.yml b/.github/dev.yml index c557db8..279d04a 100644 --- a/.github/dev.yml +++ b/.github/dev.yml @@ -2,7 +2,7 @@ pr: none trigger: - master -name: 2.1.4-dev.$(Rev:r) +name: 2.2.1-dev.$(Rev:r) pool: vmImage: ubuntu-latest diff --git a/.github/release.yml b/.github/release.yml index 7063826..0b36981 100644 --- a/.github/release.yml +++ b/.github/release.yml @@ -1,7 +1,7 @@ pr: none trigger: none -name: 2.1.$(Rev:r) +name: 2.2.$(Rev:r) pool: vmImage: ubuntu-latest diff --git a/Examples/Program_CollectAccessHash.cs b/Examples/Program_CollectAccessHash.cs index 6276814..9537987 100644 --- a/Examples/Program_CollectAccessHash.cs +++ b/Examples/Program_CollectAccessHash.cs @@ -61,7 +61,7 @@ namespace WTelegramClientTest } Console.WriteLine("With the access hash, we can now join the channel for example."); - await client.Channels_JoinChannel(new InputChannel { channel_id = DurovID, access_hash = durovAccessHash }); + await client.Channels_JoinChannel(new InputChannel(DurovID, durovAccessHash)); Console.WriteLine("Channel joined. Press any key to save and exit"); Console.ReadKey(true); diff --git a/src/TL.Extensions.cs b/src/TL.Extensions.cs index ff75277..bdc9434 100644 --- a/src/TL.Extensions.cs +++ b/src/TL.Extensions.cs @@ -112,7 +112,7 @@ namespace TL } textUrl.url = sb.ToString(offset + 2, offset2 - offset - 3); if (textUrl.url.StartsWith("tg://user?id=") && long.TryParse(textUrl.url[13..], out var user_id) && client.GetAccessHashFor(user_id) is long hash) - entities[lastIndex] = new InputMessageEntityMentionName { offset = textUrl.offset, length = textUrl.length, user_id = new InputUser { user_id = user_id, access_hash = hash } }; + entities[lastIndex] = new InputMessageEntityMentionName { offset = textUrl.offset, length = textUrl.length, user_id = new InputUser(user_id, hash) }; sb.Remove(offset, offset2 - offset); break; } @@ -287,7 +287,7 @@ namespace TL { tag = tag[8..^1]; if (tag.StartsWith("tg://user?id=") && long.TryParse(tag[13..], out var user_id) && client.GetAccessHashFor(user_id) is long hash) - entities.Add(new InputMessageEntityMentionName { offset = offset, length = -1, user_id = new InputUser { user_id = user_id, access_hash = hash } }); + entities.Add(new InputMessageEntityMentionName { offset = offset, length = -1, user_id = new InputUser(user_id, hash) }); else entities.Add(new MessageEntityTextUrl { offset = offset, length = -1, url = tag }); } diff --git a/src/TL.Helpers.cs b/src/TL.Helpers.cs index 6dcd110..a7e42fe 100644 --- a/src/TL.Helpers.cs +++ b/src/TL.Helpers.cs @@ -15,8 +15,26 @@ namespace TL } partial class InputPeer { public static InputPeerSelf Self => new(); } - partial class InputPeerChannel { public static implicit operator InputChannel(InputPeerChannel channel) => new() { channel_id = channel.channel_id, access_hash = channel.access_hash }; } - partial class InputPeerUser { public static implicit operator InputUser(InputPeerUser user) => new() { user_id = user.user_id, access_hash = user.access_hash }; } + partial class InputPeerChat + { + /// ⚠ Only for small private Chat. Chat groups of type Channel must use InputPeerChannel. See Terminology in README + /// Chat identifier + public InputPeerChat(long chat_id) => this.chat_id = chat_id; + } + partial class InputPeerUser + { + /// User identifier + /// REQUIRED FIELD. See FAQ for how to obtain it
access_hash value from the constructor + public InputPeerUser(long user_id, long access_hash) { this.user_id = user_id; this.access_hash = access_hash; } + public static implicit operator InputUser(InputPeerUser user) => new(user.user_id, user.access_hash); + } + partial class InputPeerChannel + { + /// Channel identifier + /// REQUIRED FIELD. See FAQ for how to obtain it
access_hash value from the constructor + public InputPeerChannel(long channel_id, long access_hash) { this.channel_id = channel_id; this.access_hash = access_hash; } + public static implicit operator InputChannel(InputPeerChannel channel) => new(channel.channel_id, channel.access_hash); + } partial class InputUserBase { public abstract long? UserId { get; } } partial class InputUserSelf { public override long? UserId => null; } @@ -25,7 +43,10 @@ namespace TL { public override long? UserId => user_id; public static InputUserSelf Self => new(); - public static implicit operator InputPeerUser(InputUser user) => new() { user_id = user.user_id, access_hash = user.access_hash }; + /// User identifier + /// REQUIRED FIELD. See FAQ for how to obtain it
access_hash value from the constructor + public InputUser(long user_id, long access_hash) { this.user_id = user_id; this.access_hash = access_hash; } + public static implicit operator InputPeerUser(InputUser user) => new(user.user_id, user.access_hash); } partial class InputFileBase @@ -95,8 +116,8 @@ namespace TL public override long ID => id; public override bool IsActive => (flags & Flags.deleted) == 0; public override string ToString() => username != null ? '@' + username : last_name == null ? first_name : $"{first_name} {last_name}"; - public override InputPeer ToInputPeer() => new InputPeerUser { user_id = id, access_hash = access_hash }; - protected override InputUser ToInputUser() => new() { user_id = id, access_hash = access_hash }; + public override InputPeer ToInputPeer() => new InputPeerUser(id, access_hash); + protected override InputUser ToInputUser() => new(id, access_hash); /// An estimation of the number of days ago the user was last seen (Online=0, Recently=1, LastWeek=5, LastMonth=20, LongTimeAgo=150) public TimeSpan LastSeenAgo => status?.LastSeenAgo ?? TimeSpan.FromDays(150); } @@ -137,7 +158,7 @@ namespace TL public override bool IsActive => (flags & (Flags.kicked | Flags.left | Flags.deactivated)) == 0; public override ChatPhoto Photo => photo; public override bool IsBanned(ChatBannedRights.Flags flags = 0) => ((default_banned_rights?.flags ?? 0) & flags) != 0; - public override InputPeer ToInputPeer() => new InputPeerChat { chat_id = id }; + public override InputPeer ToInputPeer() => new InputPeerChat(id); public override string ToString() => $"Chat \"{title}\"" + (flags.HasFlag(Flags.deactivated) ? " [deactivated]" : null); } partial class ChatForbidden @@ -145,7 +166,7 @@ namespace TL public override bool IsActive => false; public override ChatPhoto Photo => null; public override bool IsBanned(ChatBannedRights.Flags flags = 0) => true; - public override InputPeer ToInputPeer() => new InputPeerChat { chat_id = id }; + public override InputPeer ToInputPeer() => new InputPeerChat(id); public override string ToString() => $"ChatForbidden {id} \"{title}\""; } partial class Channel @@ -153,8 +174,8 @@ namespace TL public override bool IsActive => (flags & Flags.left) == 0; public override ChatPhoto Photo => photo; public override bool IsBanned(ChatBannedRights.Flags flags = 0) => ((banned_rights?.flags ?? 0) & flags) != 0 || ((default_banned_rights?.flags ?? 0) & flags) != 0; - public override InputPeer ToInputPeer() => new InputPeerChannel { channel_id = id, access_hash = access_hash }; - public static implicit operator InputChannel(Channel channel) => new() { channel_id = channel.id, access_hash = channel.access_hash }; + public override InputPeer ToInputPeer() => new InputPeerChannel(id, access_hash); + public static implicit operator InputChannel(Channel channel) => new(channel.id, channel.access_hash); public override string ToString() => (flags.HasFlag(Flags.broadcast) ? "Channel " : "Group ") + (username != null ? '@' + username : $"\"{title}\""); public bool IsChannel => (flags & Flags.broadcast) != 0; @@ -165,7 +186,7 @@ namespace TL public override bool IsActive => false; public override ChatPhoto Photo => null; public override bool IsBanned(ChatBannedRights.Flags flags = 0) => true; - public override InputPeer ToInputPeer() => new InputPeerChannel { channel_id = id, access_hash = access_hash }; + public override InputPeer ToInputPeer() => new InputPeerChannel(id, access_hash); public override string ToString() => $"ChannelForbidden {id} \"{title}\""; } @@ -395,7 +416,13 @@ namespace TL public static implicit operator InputStickerSetID(StickerSet stickerSet) => new() { id = stickerSet.id, access_hash = stickerSet.access_hash }; } - partial class InputChannel { public static implicit operator InputPeerChannel(InputChannel channel) => new() { channel_id = channel.channel_id, access_hash = channel.access_hash }; } + partial class InputChannel + { + /// Channel identifier + /// REQUIRED FIELD. See FAQ for how to obtain it
access_hash value from the constructor + public InputChannel(long channel_id, long access_hash) { this.channel_id = channel_id; this.access_hash = access_hash; } + public static implicit operator InputPeerChannel(InputChannel channel) => new(channel.channel_id, channel.access_hash); + } partial class Contacts_ResolvedPeer { diff --git a/src/TL.Schema.cs b/src/TL.Schema.cs index 6b14130..2c8296f 100644 --- a/src/TL.Schema.cs +++ b/src/TL.Schema.cs @@ -39,7 +39,7 @@ namespace TL public class InputPeerSelf : InputPeer { } /// Defines a chat for further interaction. See [TLDef(0x35A95CB9)] - public class InputPeerChat : InputPeer + public partial class InputPeerChat : InputPeer { /// Chat identifier public long chat_id;