From 73d0b189bb2224429e6f74f6c7b3ba895d0fd21a Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Thu, 10 Oct 2024 14:45:39 -0600 Subject: [PATCH] scoped store: implement new AccessHasher interface Signed-off-by: Sumner Evans --- go.mod | 2 +- go.sum | 4 +-- pkg/connector/client.go | 6 +++-- pkg/connector/store/scoped_store.go | 40 +++++++++++++++++++++++------ pkg/connector/telegram.go | 6 +++-- 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 66a873a3..ca08b6ae 100644 --- a/go.mod +++ b/go.mod @@ -59,4 +59,4 @@ require ( rsc.io/qr v0.2.0 // indirect ) -replace github.com/gotd/td => github.com/beeper/td v0.107.1-0.20241009171837-4aced54beeac +replace github.com/gotd/td => github.com/beeper/td v0.107.1-0.20241011144443-b584de8b2e91 diff --git a/go.sum b/go.sum index 72a37512..fd8a44e5 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU= github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= -github.com/beeper/td v0.107.1-0.20241009171837-4aced54beeac h1:mzdAAwOOC662v42tjFWtwdRdnVsZvPjEx8+PWlrZm3E= -github.com/beeper/td v0.107.1-0.20241009171837-4aced54beeac/go.mod h1:mwQQQrrAn3wizT37UjBAUB4lTy1j2RHnkRJ4z9ivqGs= +github.com/beeper/td v0.107.1-0.20241011144443-b584de8b2e91 h1:+5n60Lmug6Ogb1tkC9b7hDtnDQ+DDu694sLuHwVZgcY= +github.com/beeper/td v0.107.1-0.20241011144443-b584de8b2e91/go.mod h1:zzgUtTDJD4TVaCpKfCD0rxazQxPhSlPzx/CVBpqsx1g= github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs= diff --git a/pkg/connector/client.go b/pkg/connector/client.go index d9dbbca3..c6200feb 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -515,8 +515,10 @@ func (t *TelegramClient) getUserInfoFromTelegramUser(ctx context.Context, u tg.U return nil, fmt.Errorf("user is %T not *tg.User", user) } var identifiers []string - if err := t.ScopedStore.SetAccessHash(ctx, ids.PeerTypeUser, user.ID, user.AccessHash); err != nil { - return nil, err + if accessHash, ok := user.GetAccessHash(); ok { + if err := t.ScopedStore.SetAccessHash(ctx, ids.PeerTypeUser, user.ID, accessHash); err != nil { + return nil, err + } } if !user.Min { if err := t.ScopedStore.SetUsername(ctx, ids.PeerTypeUser, user.ID, user.Username); err != nil { diff --git a/pkg/connector/store/scoped_store.go b/pkg/connector/store/scoped_store.go index 11e5abe1..391ae7bd 100644 --- a/pkg/connector/store/scoped_store.go +++ b/pkg/connector/store/scoped_store.go @@ -167,22 +167,46 @@ func (s *ScopedStore) DeleteChannelStateForUser(ctx context.Context) (err error) return } -var _ updates.ChannelAccessHasher = (*ScopedStore)(nil) +var _ updates.AccessHasher = (*ScopedStore)(nil) -// Deprecated: only for interface, don't use directly. Use GetAccessHash instead -func (s *ScopedStore) GetChannelAccessHash(ctx context.Context, userID, channelID int64) (accessHash int64, found bool, err error) { - s.assertUserIDMatches(userID) +// Deprecated: only for interface, don't use directly. Use [GetAccessHash] +// instead. +func (s *ScopedStore) GetChannelAccessHash(ctx context.Context, forUserID, channelID int64) (accessHash int64, found bool, err error) { + s.assertUserIDMatches(forUserID) accessHash, err = s.GetAccessHash(ctx, ids.PeerTypeChannel, channelID) - found = accessHash != 0 + if errors.Is(err, ErrNoAccessHash) { + err = nil + found = false + } return } -// Deprecated: only for interface, don't use directly. Use SetAccessHash instead -func (s *ScopedStore) SetChannelAccessHash(ctx context.Context, userID, channelID, accessHash int64) (err error) { - s.assertUserIDMatches(userID) +// Deprecated: only for interface, don't use directly. Use [SetAccessHash] +// instead. +func (s *ScopedStore) SetChannelAccessHash(ctx context.Context, forUserID, channelID, accessHash int64) (err error) { + s.assertUserIDMatches(forUserID) return s.SetAccessHash(ctx, ids.PeerTypeChannel, channelID, accessHash) } +// Deprecated: only for interface, don't use directly. Use [GetAccessHash] +// instead. +func (s *ScopedStore) GetUserAccessHash(ctx context.Context, forUserID int64, userID int64) (accessHash int64, found bool, err error) { + s.assertUserIDMatches(forUserID) + accessHash, err = s.GetAccessHash(ctx, ids.PeerTypeUser, userID) + if errors.Is(err, ErrNoAccessHash) { + err = nil + found = false + } + return +} + +// Deprecated: only for interface, don't use directly. Use [SetAccessHash] +// instead. +func (s *ScopedStore) SetUserAccessHash(ctx context.Context, forUserID int64, userID int64, accessHash int64) error { + s.assertUserIDMatches(forUserID) + return s.SetAccessHash(ctx, ids.PeerTypeUser, userID, accessHash) +} + var ErrNoAccessHash = errors.New("access hash not found") func (s *ScopedStore) GetAccessHash(ctx context.Context, entityType ids.PeerType, entityID int64) (accessHash int64, err error) { diff --git a/pkg/connector/telegram.go b/pkg/connector/telegram.go index b3ee3ec4..0cb34986 100644 --- a/pkg/connector/telegram.go +++ b/pkg/connector/telegram.go @@ -441,8 +441,10 @@ func (t *TelegramClient) updateGhost(ctx context.Context, userID int64, user *tg } func (t *TelegramClient) updateChannel(ctx context.Context, channel *tg.Channel) (*bridgev2.UserInfo, error) { - if err := t.ScopedStore.SetAccessHash(ctx, ids.PeerTypeChannel, channel.ID, channel.AccessHash); err != nil { - return nil, err + if accessHash, ok := channel.GetAccessHash(); ok { + if err := t.ScopedStore.SetAccessHash(ctx, ids.PeerTypeChannel, channel.ID, accessHash); err != nil { + return nil, err + } } if !channel.Broadcast {