Skip to content

Commit

Permalink
feat: use int64 for identifiers
Browse files Browse the repository at this point in the history
As per latest Telegram update, use int64 instead of int.
  • Loading branch information
ernado committed Sep 20, 2021
1 parent 8000507 commit dbc8429
Show file tree
Hide file tree
Showing 66 changed files with 3,237 additions and 242 deletions.
12 changes: 6 additions & 6 deletions internal/gen/_template/handlers.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ func NewUpdateDispatcher() UpdateDispatcher {

type Entities struct {
Short bool
Users map[int]*User
Chats map[int]*Chat
Channels map[int]*Channel
Users map[int64]*User
Chats map[int64]*Chat
Channels map[int64]*Channel
}

func (u *Entities) short() {
u.Short = true
u.Users = make(map[int]*User, 0)
u.Chats = make(map[int]*Chat, 0)
u.Channels = make(map[int]*Channel, 0)
u.Users = make(map[int64]*User, 0)
u.Chats = make(map[int64]*Chat, 0)
u.Channels = make(map[int64]*Channel, 0)
}

// Handle implements UpdateDispatcher.
Expand Down
2 changes: 2 additions & 0 deletions internal/gen/combinators.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ func collectOnlyFields(fields []fieldDef, matchers ...simpleField) []fieldDef {
func mapCollectableFields(fields []fieldDef) (r []fieldDef) {
return collectOnlyFields(fields,
simpleField{Name: "ID", Type: "int"},
simpleField{Name: "ID", Type: "int64"},
)
}

func sortableFields(fields []fieldDef) (r []fieldDef) {
return collectOnlyFields(fields,
simpleField{Name: "ID", Type: "int"},
simpleField{Name: "ID", Type: "int64"},
simpleField{Name: "Date", Type: "int"},
)
}
12 changes: 6 additions & 6 deletions telegram/internal/e2etest/echo_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func NewEchoBot(suite *Suite, auth chan<- *tg.User) EchoBot {
}

type users struct {
users map[int]*tg.User
users map[int64]*tg.User
lock sync.RWMutex
}

func newUsers() *users {
return &users{
users: map[int]*tg.User{},
users: map[int64]*tg.User{},
}
}

Expand All @@ -57,7 +57,7 @@ func (m *users) add(list ...tg.UserClass) {
tg.UserClassArray(list).FillNotEmptyMap(m.users)
}

func (m *users) get(id int) (r *tg.User) {
func (m *users) get(id int64) (r *tg.User) {
m.lock.RLock()
r = m.users[id]
m.lock.RUnlock()
Expand All @@ -78,7 +78,7 @@ func (b EchoBot) login(ctx context.Context, client *telegram.Client) (*tg.User,
return nil, err
}

expectedUsername := "echobot" + strconv.Itoa(me.ID)
expectedUsername := "echobot" + strconv.FormatInt(me.ID, 10)
raw := tg.NewClient(waitInvoker{prev: client})
_, err := raw.AccountUpdateUsername(ctx, expectedUsername)
if err != nil {
Expand Down Expand Up @@ -149,7 +149,7 @@ func (b EchoBot) handler(client *telegram.Client) tg.NewMessageHandler {

b.logger.Info("Got message",
zap.String("text", m.Message),
zap.Int("user_id", user.ID),
zap.Int64("user_id", user.ID),
zap.String("user_first_name", user.FirstName),
zap.String("username", user.Username),
)
Expand Down Expand Up @@ -181,7 +181,7 @@ func (b EchoBot) Run(ctx context.Context) error {

b.logger.Info("Logged in",
zap.String("user", me.Username),
zap.Int("id", me.ID),
zap.Int64("id", me.ID),
)

select {
Expand Down
2 changes: 1 addition & 1 deletion telegram/internal/upconv/upconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func convertOptional(msg *tg.Message, i tg.UpdatesClass) {
msg.SetFwdFrom(v)
}
}
if u, ok := i.(interface{ GetViaBotID() (int, bool) }); ok {
if u, ok := i.(interface{ GetViaBotID() (int64, bool) }); ok {
if v, ok := u.GetViaBotID(); ok {
msg.SetViaBotID(v)
}
Expand Down
8 changes: 4 additions & 4 deletions telegram/message/entity/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type htmlParser struct {
builder *Builder
stack []stackElem
attr map[string]string
userResolver func(id int) (tg.InputUserClass, error)
userResolver func(id int64) (tg.InputUserClass, error)
}

func (p *htmlParser) fillAttrs() {
Expand Down Expand Up @@ -75,7 +75,7 @@ func (p *htmlParser) startTag() error {
}

if u.Scheme == "tg" && u.Host == "user" {
id, err := strconv.Atoi(u.Query().Get("id"))
id, err := strconv.ParseInt(u.Query().Get("id"), 10, 64)
if err != nil {
return xerrors.Errorf("invalid user ID %q: %w", id, err)
}
Expand Down Expand Up @@ -159,9 +159,9 @@ func (p *htmlParser) parse() error {
// Notice that it's okay for bots, but not for users.
//
// See https://core.telegram.org/bots/api#html-style.
func HTML(r io.Reader, b *Builder, userResolver func(id int) (tg.InputUserClass, error)) error {
func HTML(r io.Reader, b *Builder, userResolver func(id int64) (tg.InputUserClass, error)) error {
if userResolver == nil {
userResolver = func(id int) (tg.InputUserClass, error) {
userResolver = func(id int64) (tg.InputUserClass, error) {
return &tg.InputUser{
UserID: id,
}, nil
Expand Down
8 changes: 4 additions & 4 deletions telegram/message/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ import (

// Bytes reads HTML from given byte slice and returns styling option
// to build styled text block.
func Bytes(resolver func(id int) (tg.InputUserClass, error), b []byte) styling.StyledTextOption {
func Bytes(resolver func(id int64) (tg.InputUserClass, error), b []byte) styling.StyledTextOption {
return Reader(resolver, bytes.NewReader(b))
}

// String reads HTML from given string and returns styling option
// to build styled text block.
func String(resolver func(id int) (tg.InputUserClass, error), s string) styling.StyledTextOption {
func String(resolver func(id int64) (tg.InputUserClass, error), s string) styling.StyledTextOption {
return Reader(resolver, strings.NewReader(s))
}

// Format formats string using fmt, parses HTML from formatted string and returns styling option
// to build styled text block.
func Format(resolver func(id int) (tg.InputUserClass, error), format string, args ...interface{}) styling.StyledTextOption {
func Format(resolver func(id int64) (tg.InputUserClass, error), format string, args ...interface{}) styling.StyledTextOption {
return styling.Custom(func(eb *entity.Builder) error {
var buf bytes.Buffer
_, err := fmt.Fprintf(&buf, format, args...)
Expand All @@ -39,7 +39,7 @@ func Format(resolver func(id int) (tg.InputUserClass, error), format string, arg

// Reader reads HTML from given reader and returns styling option
// to build styled text block.
func Reader(resolver func(id int) (tg.InputUserClass, error), r io.Reader) styling.StyledTextOption {
func Reader(resolver func(id int64) (tg.InputUserClass, error), r io.Reader) styling.StyledTextOption {
return styling.Custom(func(eb *entity.Builder) error {
return entity.HTML(r, eb, resolver)
})
Expand Down
2 changes: 1 addition & 1 deletion telegram/message/peer/dialog_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
// DialogKey is a generic peer key.
type DialogKey struct {
Kind Kind
ID int
ID int64
AccessHash int64
}

Expand Down
30 changes: 15 additions & 15 deletions telegram/message/peer/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (

// Entities is simple peer entities storage.
type Entities struct {
users map[int]*tg.User
chats map[int]*tg.Chat
channels map[int]*tg.Channel
users map[int64]*tg.User
chats map[int64]*tg.Chat
channels map[int64]*tg.Channel
}

// NewEntities creates new Entities struct.
func NewEntities(
users map[int]*tg.User,
chats map[int]*tg.Chat,
channels map[int]*tg.Channel,
users map[int64]*tg.User,
chats map[int64]*tg.Chat,
channels map[int64]*tg.Channel,
) Entities {
return Entities{users: users, chats: chats, channels: channels}
}
Expand Down Expand Up @@ -49,19 +49,19 @@ func EntitiesFromUpdate(uctx tg.Entities) Entities {

// Users returns map of users.
// Notice that returned map is not a copy.
func (ent Entities) Users() map[int]*tg.User {
func (ent Entities) Users() map[int64]*tg.User {
return ent.users
}

// Chats returns map of chats.
// Notice that returned map is not a copy.
func (ent Entities) Chats() map[int]*tg.Chat {
func (ent Entities) Chats() map[int64]*tg.Chat {
return ent.chats
}

// Channels returns map of channels.
// Notice that returned map is not a copy.
func (ent Entities) Channels() map[int]*tg.Channel {
func (ent Entities) Channels() map[int64]*tg.Channel {
return ent.channels
}

Expand All @@ -83,9 +83,9 @@ func (ent Entities) FillFromUpdate(uctx tg.Entities) {

// Fill adds and updates all entities from given maps.
func (ent Entities) Fill(
users map[int]*tg.User,
chats map[int]*tg.Chat,
channels map[int]*tg.Channel,
users map[int64]*tg.User,
chats map[int64]*tg.Chat,
channels map[int64]*tg.Channel,
) {
for k, v := range users {
ent.users[k] = v
Expand Down Expand Up @@ -141,19 +141,19 @@ func (ent Entities) ExtractPeer(peerID tg.PeerClass) (tg.InputPeerClass, error)
}

// User finds user by given ID.
func (ent Entities) User(id int) (*tg.User, bool) {
func (ent Entities) User(id int64) (*tg.User, bool) {
v, ok := ent.users[id]
return v, ok
}

// Chat finds chat by given ID.
func (ent Entities) Chat(id int) (*tg.Chat, bool) {
func (ent Entities) Chat(id int64) (*tg.Chat, bool) {
v, ok := ent.chats[id]
return v, ok
}

// Channel finds channel by given ID.
func (ent Entities) Channel(id int) (*tg.Channel, bool) {
func (ent Entities) Channel(id int64) (*tg.Channel, bool) {
v, ok := ent.channels[id]
return v, ok
}
18 changes: 9 additions & 9 deletions telegram/message/peer/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func (m mockResult) MapChats() (r tg.ChatClassArray) {
}

func TestEntities(t *testing.T) {
users := map[int]*tg.User{
users := map[int64]*tg.User{
10: {ID: 10, AccessHash: 10},
}
chats := map[int]*tg.Chat{
chats := map[int64]*tg.Chat{
10: {ID: 10},
}
channels := map[int]*tg.Channel{
channels := map[int64]*tg.Channel{
10: {ID: 10, AccessHash: 10},
}
ent := NewEntities(users, chats, channels)
Expand All @@ -66,9 +66,9 @@ func TestEntities(t *testing.T) {
}},
{"FillFromResult", func() Entities {
e := NewEntities(
map[int]*tg.User{},
map[int]*tg.Chat{},
map[int]*tg.Channel{},
map[int64]*tg.User{},
map[int64]*tg.Chat{},
map[int64]*tg.Channel{},
)
e.FillFromResult(result)
return e
Expand All @@ -78,9 +78,9 @@ func TestEntities(t *testing.T) {
}},
{"FillFromUpdate", func() Entities {
e := NewEntities(
map[int]*tg.User{},
map[int]*tg.Chat{},
map[int]*tg.Channel{},
map[int64]*tg.User{},
map[int64]*tg.Chat{},
map[int64]*tg.Channel{},
)
e.FillFromUpdate(ctx)
return e
Expand Down
2 changes: 1 addition & 1 deletion telegram/message/peer/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestLRU(t *testing.T) {
// State: [4 3 2 1]
for i := range [5]struct{}{} {
lru.put(strconv.Itoa(i), &tg.InputPeerChat{
ChatID: i,
ChatID: int64(i),
})
}

Expand Down
4 changes: 2 additions & 2 deletions telegram/message/peer/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Test_plainResolver_Resolve(t *testing.T) {

r, err := resolver.ResolveDomain(ctx, domain)
require.IsType(t, &tg.InputPeerUser{}, r)
require.Equal(t, 10, r.(*tg.InputPeerUser).UserID)
require.Equal(t, int64(10), r.(*tg.InputPeerUser).UserID)
require.NoError(t, err)

_, err = resolver.ResolveDomain(ctx, domain)
Expand Down Expand Up @@ -71,7 +71,7 @@ func Test_plainResolver_ResolvePhone(t *testing.T) {
r, err := resolver.ResolvePhone(ctx, phone)
require.NoError(t, err)
require.IsType(t, &tg.InputPeerUser{}, r)
require.Equal(t, 10, r.(*tg.InputPeerUser).UserID)
require.Equal(t, int64(10), r.(*tg.InputPeerUser).UserID)

_, err = resolver.ResolvePhone(ctx, phone)
require.Error(t, err)
Expand Down
4 changes: 2 additions & 2 deletions telegram/message/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func resolver(t *testing.T, expectedDomain string, expected tg.InputPeerClass) p

type answerable struct {
ID int
UserID int
UserID int64
}

func (a answerable) GetPeer() tg.PeerClass {
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestResolve(t *testing.T) {
check(s.ResolveDomain("@durov"), expected)
check(s.ResolveDeeplink("https://t.me/durov"), expected)

uctx := tg.Entities{Users: map[int]*tg.User{
uctx := tg.Entities{Users: map[int64]*tg.User{
expected.UserID: {ID: expected.UserID, AccessHash: expected.AccessHash, Username: "durov"},
}}
ans := answerable{ID: 10, UserID: expected.UserID}
Expand Down
2 changes: 1 addition & 1 deletion telegram/message/scheduled.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (m *ScheduledManager) Get(ctx context.Context, id int, ids ...int) (tg.Modi
}

// HistoryWithHash gets scheduled messages history.
func (m *ScheduledManager) HistoryWithHash(ctx context.Context, hash int) (tg.ModifiedMessagesMessages, error) {
func (m *ScheduledManager) HistoryWithHash(ctx context.Context, hash int64) (tg.ModifiedMessagesMessages, error) {
p, err := m.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions telegram/query/cached/compute_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/gotd/td/tg"
)

func (s *ContactsGetContacts) computeHash(v *tg.ContactsContacts) int {
func (s *ContactsGetContacts) computeHash(v *tg.ContactsContacts) int64 {
cts := v.Contacts

sort.SliceStable(cts, func(i, j int) bool {
Expand All @@ -18,5 +18,5 @@ func (s *ContactsGetContacts) computeHash(v *tg.ContactsContacts) int {
h.Update(uint32(contact.UserID))
}

return int(h.Sum())
return int64(h.Sum())
}
Loading

0 comments on commit dbc8429

Please sign in to comment.