move gotd fork into repo. (#111)

- update to latest telegram layer
- remove some references to fields in tg.Entities that don't exist in
the schema
- originally added here:
https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e
  - referenced here
-
https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3
-
https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
This commit is contained in:
Adam Van Ymeren
2025-06-27 20:03:37 -07:00
committed by GitHub
parent 0952df0244
commit 7a04f298d2
19264 changed files with 1539697 additions and 84 deletions
@@ -0,0 +1,2 @@
dcOption#18b7a10d flags:# ipv6:flags.0?true media_only:flags.1?true tcpo_only:flags.2?true cdn:flags.3?true static:flags.4?true id:int ip_address:string port:int secret:flags.10?bytes = DcOption;
config#330b4067 flags:# phonecalls_enabled:flags.1?true default_p2p_contacts:flags.3?true preload_featured_stickers:flags.4?true ignore_phone_entities:flags.5?true revoke_pm_inbox:flags.6?true blocked_mode:flags.8?true pfs_enabled:flags.13?true date:int expires:int test_mode:Bool this_dc:int dc_options:Vector<DcOption> dc_txt_domain_name:string chat_size_max:int megagroup_size_max:int forwarded_count_max:int online_update_period_ms:int offline_blur_timeout_ms:int offline_idle_timeout_ms:int online_cloud_timeout_ms:int notify_cloud_delay_ms:int notify_default_delay_ms:int push_chat_period_ms:int push_chat_limit:int saved_gifs_limit:int edit_time_limit:int revoke_time_limit:int revoke_pm_time_limit:int rating_e_decay:int stickers_recent_limit:int stickers_faved_limit:int channels_read_media_period:int tmp_sessions:flags.0?int pinned_dialogs_count_max:int pinned_infolder_count_max:int call_receive_timeout_ms:int call_ring_timeout_ms:int call_connect_timeout_ms:int call_packet_timeout_ms:int me_url_prefix:string autoupdate_url_prefix:flags.7?string gif_search_username:flags.9?string venue_search_username:flags.10?string img_search_username:flags.11?string static_maps_provider:flags.12?string caption_length_max:int message_length_max:int webfile_dc_id:int suggested_lang_code:flags.2?string lang_pack_version:flags.2?int base_lang_pack_version:flags.2?int = Config;
+4
View File
@@ -0,0 +1,4 @@
// Package internal wraps internal packages for tdp package.
package internal
//go:generate go run go.mau.fi/mautrix-telegram/pkg/gotd/cmd/gotdgen --format=true --clean --package schema --target schema --schema _testdata/schema.tl
@@ -0,0 +1,54 @@
// Code generated by gotdgen, DO NOT EDIT.
package schema
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"go.uber.org/multierr"
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)
// No-op definition for keeping imports.
var (
_ = bin.Buffer{}
_ = context.Background()
_ = fmt.Stringer(nil)
_ = strings.Builder{}
_ = errors.Is
_ = multierr.AppendInto
_ = sort.Ints
_ = tdp.Format
_ = tgerr.Error{}
_ = tdjson.Encoder{}
)
// Invoker can invoke raw MTProto rpc calls.
type Invoker interface {
Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error
}
// Client implement methods for calling functions from TL schema via Invoker.
type Client struct {
rpc Invoker
}
// Invoker returns Invoker used by this client.
func (c *Client) Invoker() Invoker {
return c.rpc
}
// NewClient creates new Client.
func NewClient(invoker Invoker) *Client {
return &Client{
rpc: invoker,
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,428 @@
// Code generated by gotdgen, DO NOT EDIT.
package schema
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"go.uber.org/multierr"
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)
// No-op definition for keeping imports.
var (
_ = bin.Buffer{}
_ = context.Background()
_ = fmt.Stringer(nil)
_ = strings.Builder{}
_ = errors.Is
_ = multierr.AppendInto
_ = sort.Ints
_ = tdp.Format
_ = tgerr.Error{}
_ = tdjson.Encoder{}
)
// DCOption represents TL type `dcOption#18b7a10d`.
type DCOption struct {
// Flags field of DCOption.
Flags bin.Fields
// Ipv6 field of DCOption.
Ipv6 bool
// MediaOnly field of DCOption.
MediaOnly bool
// TCPObfuscatedOnly field of DCOption.
TCPObfuscatedOnly bool
// CDN field of DCOption.
CDN bool
// Static field of DCOption.
Static bool
// ID field of DCOption.
ID int
// IPAddress field of DCOption.
IPAddress string
// Port field of DCOption.
Port int
// Secret field of DCOption.
//
// Use SetSecret and GetSecret helpers.
Secret []byte
}
// DCOptionTypeID is TL type id of DCOption.
const DCOptionTypeID = 0x18b7a10d
// Ensuring interfaces in compile-time for DCOption.
var (
_ bin.Encoder = &DCOption{}
_ bin.Decoder = &DCOption{}
_ bin.BareEncoder = &DCOption{}
_ bin.BareDecoder = &DCOption{}
)
func (d *DCOption) Zero() bool {
if d == nil {
return true
}
if !(d.Flags.Zero()) {
return false
}
if !(d.Ipv6 == false) {
return false
}
if !(d.MediaOnly == false) {
return false
}
if !(d.TCPObfuscatedOnly == false) {
return false
}
if !(d.CDN == false) {
return false
}
if !(d.Static == false) {
return false
}
if !(d.ID == 0) {
return false
}
if !(d.IPAddress == "") {
return false
}
if !(d.Port == 0) {
return false
}
if !(d.Secret == nil) {
return false
}
return true
}
// String implements fmt.Stringer.
func (d *DCOption) String() string {
if d == nil {
return "DCOption(nil)"
}
type Alias DCOption
return fmt.Sprintf("DCOption%+v", Alias(*d))
}
// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*DCOption) TypeID() uint32 {
return DCOptionTypeID
}
// TypeName returns name of type in TL schema.
func (*DCOption) TypeName() string {
return "dcOption"
}
// TypeInfo returns info about TL type.
func (d *DCOption) TypeInfo() tdp.Type {
typ := tdp.Type{
Name: "dcOption",
ID: DCOptionTypeID,
}
if d == nil {
typ.Null = true
return typ
}
typ.Fields = []tdp.Field{
{
Name: "Ipv6",
SchemaName: "ipv6",
Null: !d.Flags.Has(0),
},
{
Name: "MediaOnly",
SchemaName: "media_only",
Null: !d.Flags.Has(1),
},
{
Name: "TCPObfuscatedOnly",
SchemaName: "tcpo_only",
Null: !d.Flags.Has(2),
},
{
Name: "CDN",
SchemaName: "cdn",
Null: !d.Flags.Has(3),
},
{
Name: "Static",
SchemaName: "static",
Null: !d.Flags.Has(4),
},
{
Name: "ID",
SchemaName: "id",
},
{
Name: "IPAddress",
SchemaName: "ip_address",
},
{
Name: "Port",
SchemaName: "port",
},
{
Name: "Secret",
SchemaName: "secret",
Null: !d.Flags.Has(10),
},
}
return typ
}
// SetFlags sets flags for non-zero fields.
func (d *DCOption) SetFlags() {
if !(d.Ipv6 == false) {
d.Flags.Set(0)
}
if !(d.MediaOnly == false) {
d.Flags.Set(1)
}
if !(d.TCPObfuscatedOnly == false) {
d.Flags.Set(2)
}
if !(d.CDN == false) {
d.Flags.Set(3)
}
if !(d.Static == false) {
d.Flags.Set(4)
}
if !(d.Secret == nil) {
d.Flags.Set(10)
}
}
// Encode implements bin.Encoder.
func (d *DCOption) Encode(b *bin.Buffer) error {
if d == nil {
return fmt.Errorf("can't encode dcOption#18b7a10d as nil")
}
b.PutID(DCOptionTypeID)
return d.EncodeBare(b)
}
// EncodeBare implements bin.BareEncoder.
func (d *DCOption) EncodeBare(b *bin.Buffer) error {
if d == nil {
return fmt.Errorf("can't encode dcOption#18b7a10d as nil")
}
d.SetFlags()
if err := d.Flags.Encode(b); err != nil {
return fmt.Errorf("unable to encode dcOption#18b7a10d: field flags: %w", err)
}
b.PutInt(d.ID)
b.PutString(d.IPAddress)
b.PutInt(d.Port)
if d.Flags.Has(10) {
b.PutBytes(d.Secret)
}
return nil
}
// Decode implements bin.Decoder.
func (d *DCOption) Decode(b *bin.Buffer) error {
if d == nil {
return fmt.Errorf("can't decode dcOption#18b7a10d to nil")
}
if err := b.ConsumeID(DCOptionTypeID); err != nil {
return fmt.Errorf("unable to decode dcOption#18b7a10d: %w", err)
}
return d.DecodeBare(b)
}
// DecodeBare implements bin.BareDecoder.
func (d *DCOption) DecodeBare(b *bin.Buffer) error {
if d == nil {
return fmt.Errorf("can't decode dcOption#18b7a10d to nil")
}
{
if err := d.Flags.Decode(b); err != nil {
return fmt.Errorf("unable to decode dcOption#18b7a10d: field flags: %w", err)
}
}
d.Ipv6 = d.Flags.Has(0)
d.MediaOnly = d.Flags.Has(1)
d.TCPObfuscatedOnly = d.Flags.Has(2)
d.CDN = d.Flags.Has(3)
d.Static = d.Flags.Has(4)
{
value, err := b.Int()
if err != nil {
return fmt.Errorf("unable to decode dcOption#18b7a10d: field id: %w", err)
}
d.ID = value
}
{
value, err := b.String()
if err != nil {
return fmt.Errorf("unable to decode dcOption#18b7a10d: field ip_address: %w", err)
}
d.IPAddress = value
}
{
value, err := b.Int()
if err != nil {
return fmt.Errorf("unable to decode dcOption#18b7a10d: field port: %w", err)
}
d.Port = value
}
if d.Flags.Has(10) {
value, err := b.Bytes()
if err != nil {
return fmt.Errorf("unable to decode dcOption#18b7a10d: field secret: %w", err)
}
d.Secret = value
}
return nil
}
// SetIpv6 sets value of Ipv6 conditional field.
func (d *DCOption) SetIpv6(value bool) {
if value {
d.Flags.Set(0)
d.Ipv6 = true
} else {
d.Flags.Unset(0)
d.Ipv6 = false
}
}
// GetIpv6 returns value of Ipv6 conditional field.
func (d *DCOption) GetIpv6() (value bool) {
if d == nil {
return
}
return d.Flags.Has(0)
}
// SetMediaOnly sets value of MediaOnly conditional field.
func (d *DCOption) SetMediaOnly(value bool) {
if value {
d.Flags.Set(1)
d.MediaOnly = true
} else {
d.Flags.Unset(1)
d.MediaOnly = false
}
}
// GetMediaOnly returns value of MediaOnly conditional field.
func (d *DCOption) GetMediaOnly() (value bool) {
if d == nil {
return
}
return d.Flags.Has(1)
}
// SetTCPObfuscatedOnly sets value of TCPObfuscatedOnly conditional field.
func (d *DCOption) SetTCPObfuscatedOnly(value bool) {
if value {
d.Flags.Set(2)
d.TCPObfuscatedOnly = true
} else {
d.Flags.Unset(2)
d.TCPObfuscatedOnly = false
}
}
// GetTCPObfuscatedOnly returns value of TCPObfuscatedOnly conditional field.
func (d *DCOption) GetTCPObfuscatedOnly() (value bool) {
if d == nil {
return
}
return d.Flags.Has(2)
}
// SetCDN sets value of CDN conditional field.
func (d *DCOption) SetCDN(value bool) {
if value {
d.Flags.Set(3)
d.CDN = true
} else {
d.Flags.Unset(3)
d.CDN = false
}
}
// GetCDN returns value of CDN conditional field.
func (d *DCOption) GetCDN() (value bool) {
if d == nil {
return
}
return d.Flags.Has(3)
}
// SetStatic sets value of Static conditional field.
func (d *DCOption) SetStatic(value bool) {
if value {
d.Flags.Set(4)
d.Static = true
} else {
d.Flags.Unset(4)
d.Static = false
}
}
// GetStatic returns value of Static conditional field.
func (d *DCOption) GetStatic() (value bool) {
if d == nil {
return
}
return d.Flags.Has(4)
}
// GetID returns value of ID field.
func (d *DCOption) GetID() (value int) {
if d == nil {
return
}
return d.ID
}
// GetIPAddress returns value of IPAddress field.
func (d *DCOption) GetIPAddress() (value string) {
if d == nil {
return
}
return d.IPAddress
}
// GetPort returns value of Port field.
func (d *DCOption) GetPort() (value int) {
if d == nil {
return
}
return d.Port
}
// SetSecret sets value of Secret conditional field.
func (d *DCOption) SetSecret(value []byte) {
d.Flags.Set(10)
d.Secret = value
}
// GetSecret returns value of Secret conditional field and
// boolean which is true if field was set.
func (d *DCOption) GetSecret() (value []byte, ok bool) {
if d == nil {
return
}
if !d.Flags.Has(10) {
return value, false
}
return d.Secret, true
}
@@ -0,0 +1,61 @@
// Code generated by gotdgen, DO NOT EDIT.
package schema
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"go.uber.org/multierr"
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)
// No-op definition for keeping imports.
var (
_ = bin.Buffer{}
_ = context.Background()
_ = fmt.Stringer(nil)
_ = strings.Builder{}
_ = errors.Is
_ = multierr.AppendInto
_ = sort.Ints
_ = tdp.Format
_ = tgerr.Error{}
_ = tdjson.Encoder{}
)
// TypesMap returns mapping from type ids to TL type names.
func TypesMap() map[uint32]string {
return map[uint32]string{
DCOptionTypeID: "dcOption#18b7a10d",
ConfigTypeID: "config#330b4067",
}
}
// NamesMap returns mapping from type names to TL type ids.
func NamesMap() map[string]uint32 {
return map[string]uint32{
"dcOption": DCOptionTypeID,
"config": ConfigTypeID,
}
}
// TypesConstructorMap maps type ids to constructors.
func TypesConstructorMap() map[uint32]func() bin.Object {
return map[uint32]func() bin.Object{
DCOptionTypeID: func() bin.Object { return &DCOption{} },
ConfigTypeID: func() bin.Object { return &Config{} },
}
}
// ClassConstructorsMap maps class schema name to constructors type ids.
func ClassConstructorsMap() map[string][]uint32 {
return map[string][]uint32{}
}
+123
View File
@@ -0,0 +1,123 @@
// Package tdp is td pretty-printing and formatting facilities for types from
// MTProto.
package tdp
import (
"encoding/base64"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
// options for formatting.
type options struct {
writeTypeID bool
}
// Option of formatting.
type Option func(o *options)
// WithTypeID adds type id tp type name.
func WithTypeID(o *options) {
o.writeTypeID = true
}
const (
defaultIdent = " "
noIdent = ""
)
func formatValue(b *strings.Builder, prefix, fieldName string, opt options, v reflect.Value) {
switch v.Kind() {
case reflect.Struct, reflect.Ptr, reflect.Interface:
i, ok := v.Interface().(Object)
if ok {
format(b, prefix+defaultIdent, opt, i)
} else if v.CanAddr() {
formatValue(b, prefix, fieldName, opt, v.Addr())
}
case reflect.Slice:
if buf, ok := v.Interface().([]byte); ok {
b.WriteString(base64.RawURLEncoding.EncodeToString(buf))
return
}
b.WriteRune('\n')
for i := 0; i < v.Len(); i++ {
vi := v.Index(i)
b.WriteString(prefix)
b.WriteString(defaultIdent)
b.WriteString("- ")
formatValue(b, prefix+defaultIdent, fieldName, opt, vi)
b.WriteRune('\n')
}
case reflect.Int:
// Special case for date.
var (
now = time.Now()
max = now.AddDate(0, 0, 7).Unix()
min = now.AddDate(-2, 0, 0).Unix()
)
i := v.Int()
if i > min && i < max && strings.Contains(fieldName, "date") {
b.WriteString(time.Unix(i, 0).UTC().Format(time.RFC3339))
} else {
b.WriteString(strconv.FormatInt(i, 10))
}
default:
b.WriteString(fmt.Sprint(v.Interface()))
}
}
func format(b *strings.Builder, prefix string, opt options, obj Object) {
if obj == nil {
// No type information is available. it is like Format(nil).
b.WriteString("<nil>")
return
}
info := obj.TypeInfo()
b.WriteString(info.Name)
if opt.writeTypeID {
b.WriteRune('#')
b.WriteString(strconv.FormatInt(int64(info.ID), 16))
}
if info.Null {
b.WriteString("(nil)")
return
}
v := reflect.ValueOf(obj).Elem()
for i, f := range info.Fields {
if i == 0 && f.SchemaName == "flags" {
// Flag field, skipping.
continue
}
if f.Null {
// Optional field not set, skipping.
continue
}
b.WriteRune('\n')
b.WriteString(prefix)
b.WriteString(defaultIdent)
b.WriteString(f.SchemaName)
b.WriteString(": ")
formatValue(b, prefix, f.SchemaName, opt, v.FieldByName(f.Name))
}
}
// Format pretty-prints v into string.
func Format(object Object, opts ...Option) string {
var opt options
for _, o := range opts {
o(&opt)
}
var b strings.Builder
format(&b, noIdent, opt, object)
return b.String()
}
+49
View File
@@ -0,0 +1,49 @@
package tdp_test
import (
"testing"
"github.com/stretchr/testify/require"
"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
"go.mau.fi/mautrix-telegram/pkg/gotd/tdp/internal/schema"
)
func TestFormat(t *testing.T) {
for _, tt := range []struct {
Input tdp.Object
Output string
Options []tdp.Option
}{
{
Output: "<nil>",
},
{
Input: &schema.DCOption{
ID: 10,
IPAddress: "127.0.0.1",
Port: 1010,
},
Options: []tdp.Option{tdp.WithTypeID},
Output: "dcOption#18b7a10d",
},
{
Input: &schema.Config{
DCOptions: []schema.DCOption{
{
ID: 1,
IPAddress: "127.0.0.1",
Port: 1010,
},
},
},
Options: []tdp.Option{tdp.WithTypeID},
},
} {
t.Skip("TODO: Use golden files")
t.Run(tt.Output, func(t *testing.T) {
require.Equal(t, tt.Output, tdp.Format(tt.Input, tt.Options...))
})
}
}
+25
View File
@@ -0,0 +1,25 @@
package tdp
// Field of TL type, non-recursive.
type Field struct {
Name string
SchemaName string
Null bool
}
// Type info for TL type, non-recursive.
type Type struct {
// Name in TL schema.
Name string
// ID is type id.
ID uint32
// Fields of type.
Fields []Field
// Null denotes whether value is null.
Null bool
}
// Object of TL schema that can return type info.
type Object interface {
TypeInfo() Type
}