scoped store: implement new AccessHasher interface

Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
Sumner Evans
2024-10-10 14:45:39 -06:00
parent 48059a3a51
commit 73d0b189bb
5 changed files with 43 additions and 15 deletions
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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=
+4 -2
View File
@@ -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 {
+32 -8
View File
@@ -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) {
+4 -2
View File
@@ -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 {