diff --git a/Telega.Example/Program.cs b/Telega.Example/Program.cs index 56b0f51..5ad83cd 100644 --- a/Telega.Example/Program.cs +++ b/Telega.Example/Program.cs @@ -78,18 +78,14 @@ static async Task DownloadFirstChannelPictureExample(TelegramClient tg) .IfNone(() => throw new Exception("A channel is not found")); var photo = firstChannel.Photo .AsTag().IfNone(() => throw new Exception("The first channel does not have a photo")); - var bigPhotoFile = photo.PhotoBig - .AsTag().IfNone(() => throw new Exception("The first channel photo is unavailable")); - - InputFileLocation ToInput(FileLocation.Tag location) => - new InputFileLocation.Tag( - volumeId: location.VolumeId, - localId: location.LocalId, - secret: location.Secret, - fileReference: location.FileReference - ); - - var photoLoc = ToInput(bigPhotoFile); + var bigPhotoFile = photo.PhotoBig; + + var photoLoc = new InputFileLocation.PeerPhotoTag( + peer: new InputPeer.ChannelTag(firstChannel.Id, firstChannel.AccessHash.AssertSome()), + volumeId: bigPhotoFile.VolumeId, + localId: bigPhotoFile.LocalId, + big: true + ); var fileType = await tg.Upload.GetFileType(photoLoc); var fileTypeExt = fileType.Match( pngTag: _ => ".png", diff --git a/Telega.Rpc.Dto.Generator/Generation/Gen.cs b/Telega.Rpc.Dto.Generator/Generation/Gen.cs index 154b08a..372712c 100644 --- a/Telega.Rpc.Dto.Generator/Generation/Gen.cs +++ b/Telega.Rpc.Dto.Generator/Generation/Gen.cs @@ -22,7 +22,7 @@ static class Gen Line("using T = Telega.Rpc.Dto.Types;") ); - static NestedText GenTypeTagBody(string tagName, Signature tag) + static NestedText GenTypeTagBody(string typeName, string tagName, Signature tag) { var modifiedArgs = tag.Args .Map(x => new Arg( @@ -68,6 +68,11 @@ static NestedText GenTypeTagBody(string tagName, Signature tag) Line(""), WithGen.GenWith(argsWithoutFlags, tagName), Line(""), + typeName != tagName ? Scope( + Line($"public static implicit operator {typeName}({tagName} tag) => new {typeName}(tag);"), + Line($"public static implicit operator Some<{typeName}>({tagName} tag) => new {typeName}(tag);"), + Line("") + ) : EmptyScope(), RelationsGen.GenRelations(tagName, argsWithoutFlags), Line(""), Line(""), @@ -77,14 +82,14 @@ static NestedText GenTypeTagBody(string tagName, Signature tag) ); } - static NestedText GenTypeTag(Signature tag) + static NestedText GenTypeTag(string typeName, Signature tag) { var tagName = tag.Name; return Scope( Line($"public sealed class {tagName} : ITgTypeTag, IEquatable<{tagName}>, IComparable<{tagName}>, IComparable"), Line("{"), - Indent(1, GenTypeTagBody(tagName, tag)), + Indent(1, GenTypeTagBody(typeName, tagName, tag)), Line("}") ); } @@ -159,18 +164,13 @@ static NestedText GenFunc(Signature func, string funcName) static NestedText GenTypeWithManyTags(string typeName, Arr typeTags) { - var tagsDefs = typeTags.Map(GenTypeTag).Scope(Environment.NewLine + Environment.NewLine); + var tagsDefs = typeTags.Map(x => GenTypeTag(typeName, x)).Scope(Environment.NewLine + Environment.NewLine); var tagDef = Scope( Line("readonly ITgTypeTag _tag;"), Line($"{typeName}(ITgTypeTag tag) => _tag = tag ?? throw new ArgumentNullException(nameof(tag));") ); - var tagCreateDef = typeTags.Map(tag => - Line($"public static implicit operator {typeName}({tag.Name} tag) => new {typeName}(tag);") - ).Scope(); - - var serializeRef = Scope( Line("void ITgSerializable.Serialize(BinaryWriter bw)"), Line("{"), @@ -287,7 +287,6 @@ static NestedText GenTypeWithManyTags(string typeName, Arr typeTags) var bodyDef = Scope(Environment.NewLine + Environment.NewLine, tagsDefs, tagDef, - tagCreateDef, serializeRef, staticTryDeserializeDef, staticDeserializeDef, @@ -314,7 +313,7 @@ static NestedText GenTypeWithManyTags(string typeName, Arr typeTags) static NestedText GenTypeWithOneTag(string typeName, Signature tag) { - var tagBody = GenTypeTagBody(typeName, tag); + var tagBody = GenTypeTagBody(typeName, typeName, tag); var serializeRef = Scope( Line("void ITgSerializable.Serialize(BinaryWriter bw)"), diff --git a/Telega.Rpc.Dto.Generator/TextModel/NestedTextAbbreviations.cs b/Telega.Rpc.Dto.Generator/TextModel/NestedTextAbbreviations.cs index 9581769..2d2400f 100644 --- a/Telega.Rpc.Dto.Generator/TextModel/NestedTextAbbreviations.cs +++ b/Telega.Rpc.Dto.Generator/TextModel/NestedTextAbbreviations.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using LanguageExt; using static LanguageExt.Prelude; namespace Telega.Rpc.Dto.Generator.TextModel @@ -9,6 +10,7 @@ static class NestedTextAbbreviations public static NestedText Line(Text text) => NestedText.CreateLine(text); public static NestedText Line(string s) => Line((Text) s); public static NestedText Indent(int offset, NestedText text) => NestedText.CreateIndent(offset, text); + public static NestedText EmptyScope() => NestedText.CreateScope(Arr.Empty, (Text) Environment.NewLine); public static NestedText Scope(this IEnumerable xs, Text separator = null) => NestedText.CreateScope(xs.ToArr(), separator ?? Environment.NewLine); public static NestedText Scope(Text separator, params NestedText[] xs) => xs.Scope(separator); public static NestedText Scope(Text separator, params IEnumerable[] xs) => xs.Bind(identity).Scope(separator); diff --git a/Telega/TelegramClientMessages.cs b/Telega/TelegramClientMessages.cs index 54ac001..b80d510 100644 --- a/Telega/TelegramClientMessages.cs +++ b/Telega/TelegramClientMessages.cs @@ -26,7 +26,7 @@ await _tg.Call(new GetDialogs( )); public async Task GetHistory( - InputPeer peer, + Some peer, int offsetId = 0, int offsetDate = 0, int addOffset = 0, @@ -46,7 +46,7 @@ await _tg.Call(new GetHistory( hash )); - public async Task SendMessage(InputPeer peer, Some message) => + public async Task SendMessage(Some peer, Some message) => await _tg.Call(new SendMessage( peer: peer, message: message, @@ -61,8 +61,8 @@ await _tg.Call(new SendMessage( )); public async Task SendPhoto( - InputPeer peer, - InputFile file, + Some peer, + Some file, Some message ) => await _tg.Call(new SendMedia( @@ -79,8 +79,8 @@ await _tg.Call(new SendMedia( )); public async Task SendDocument( - InputPeer peer, - InputFile file, + Some peer, + Some file, Some mimeType, Arr attributes, Some message @@ -106,7 +106,7 @@ await _tg.Call(new SendMedia( message: message )); - public async Task SendTyping(InputPeer peer) => + public async Task SendTyping(Some peer) => await _tg.Call(new SetTyping( action: new SendMessageAction.TypingTag(), peer: peer