Skip to content

Commit

Permalink
update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
anonyindian committed Mar 14, 2023
1 parent 4979cfd commit fb70c6f
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 129 deletions.
124 changes: 74 additions & 50 deletions authHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"fmt"

"github.com/gotd/td/telegram/auth"
"github.com/gotd/td/tg"
"github.com/pkg/errors"
)

func IfAuthNecessary(c *auth.Client, ctx context.Context, flow auth.Flow) error {
func IfAuthNecessary(c *auth.Client, ctx context.Context, flow Flow) error {
auth, err := c.Status(ctx)
if err != nil {
return errors.Wrap(err, "get auth status")
Expand All @@ -23,7 +24,28 @@ func IfAuthNecessary(c *auth.Client, ctx context.Context, flow auth.Flow) error

}

func authFlow(f auth.Flow, ctx context.Context, client *auth.Client) error {
type Flow auth.Flow

func (f Flow) handleSignUp(ctx context.Context, client auth.FlowClient, phone, hash string, s *auth.SignUpRequired) error {
if err := f.Auth.AcceptTermsOfService(ctx, s.TermsOfService); err != nil {
return errors.Wrap(err, "confirm TOS")
}
info, err := f.Auth.SignUp(ctx)
if err != nil {
return errors.Wrap(err, "sign up info not provided")
}
if _, err := client.SignUp(ctx, auth.SignUp{
PhoneNumber: phone,
PhoneCodeHash: hash,
FirstName: info.FirstName,
LastName: info.LastName,
}); err != nil {
return errors.Wrap(err, "sign up")
}
return nil
}

func authFlow(f Flow, ctx context.Context, client *auth.Client) error {
if f.Auth == nil {
return errors.New("no UserAuthenticator provided")
}
Expand All @@ -37,61 +59,63 @@ func authFlow(f auth.Flow, ctx context.Context, client *auth.Client) error {
if err != nil {
return errors.Wrap(err, "send code")
}
hash := sentCode.PhoneCodeHash

code, err := f.Auth.Code(ctx, sentCode)
if err != nil {
return errors.Wrap(err, "get code")
}

_, signInErr := client.SignIn(ctx, phone, code, hash)

if errors.Is(signInErr, auth.ErrPasswordAuthNeeded) {
err = signInErr
for i := 0; err != nil && i < 3; i++ {
if i != 0 {
fmt.Println("The 2FA Code you just entered seems to be incorrect,")
fmt.Println("Attempts Left:", 3-i)
fmt.Println("Please try again.... ")
switch s := sentCode.(type) {
case *tg.AuthSentCode:
hash := s.PhoneCodeHash
code, err := f.Auth.Code(ctx, s)
if err != nil {
return errors.Wrap(err, "get code")
}
_, signInErr := client.SignIn(ctx, phone, code, hash)
if errors.Is(signInErr, auth.ErrPasswordAuthNeeded) {
err = signInErr
for i := 0; err != nil && i < 3; i++ {
if i != 0 {
fmt.Println("The 2FA Code you just entered seems to be incorrect,")
fmt.Println("Attempts Left:", 3-i)
fmt.Println("Please try again.... ")
}
password, err1 := f.Auth.Password(ctx)
if err1 != nil {
return errors.Wrap(err1, "get password")
}
_, err = client.Password(ctx, password)
}
password, err1 := f.Auth.Password(ctx)
if err1 != nil {
return errors.Wrap(err1, "get password")
if err != nil {
return errors.Wrap(err, "sign in with password")
}
_, err = client.Password(ctx, password)
// if _, err := client.Password(ctx, password); err != nil {
// return errors.Wrap(err, "sign in with password")
// }
return nil
}
if err != nil {
return errors.Wrap(err, "sign in with password")
var signUpRequired *auth.SignUpRequired
if errors.As(signInErr, &signUpRequired) {
return f.handleSignUp(ctx, client, phone, hash, signUpRequired)
}
// if _, err := client.Password(ctx, password); err != nil {
// return errors.Wrap(err, "sign in with password")
// }
return nil
}

var signUpRequired *auth.SignUpRequired
if errors.As(signInErr, &signUpRequired) {
if err := f.Auth.AcceptTermsOfService(ctx, signUpRequired.TermsOfService); err != nil {
return errors.Wrap(err, "confirm TOS")
if signInErr != nil {
// fmt.Println("\n\n", signInErr.Error(), "\n\n ")
return errors.Wrap(signInErr, "sign in")
}
info, err := f.Auth.SignUp(ctx)
if err != nil {
return errors.Wrap(err, "sign up info not provided")
}
if _, err := client.SignUp(ctx, auth.SignUp{
PhoneNumber: phone,
PhoneCodeHash: hash,
FirstName: info.FirstName,
LastName: info.LastName,
}); err != nil {
return errors.Wrap(err, "sign up")
case *tg.AuthSentCodeSuccess:
switch a := s.Authorization.(type) {
case *tg.AuthAuthorization:
// Looks that we are already authorized.
return nil
case *tg.AuthAuthorizationSignUpRequired:
if err := f.handleSignUp(ctx, client, phone, "", &auth.SignUpRequired{
TermsOfService: a.TermsOfService,
}); err != nil {
// TODO: not sure that blank hash will work here
return errors.Wrap(err, "sign up after auth sent code success")
}
return nil
default:
return errors.Errorf("unexpected authorization type: %T", a)
}
return nil
}

if signInErr != nil {
// fmt.Println("\n\n", signInErr.Error(), "\n\n ")
return errors.Wrap(signInErr, "sign in")
default:
return errors.Errorf("unexpected sent code type: %T", sentCode)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions clientHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
Sender *message.Sender
)

const VERSION = "v1.0.0-beta09"
const VERSION = "v1.0.0-beta9"

type ClientHelper struct {
// Unique Telegram Application ID, get it from https://my.telegram.org/apps.
Expand Down Expand Up @@ -105,7 +105,7 @@ func (ch *ClientHelper) CreateClient(ctx context.Context, opts *telegram.Options
client := telegram.NewClient(ch.AppID, ch.ApiHash, *opts)
if !ch.DisableCopyright {
fmt.Printf(`
GoTGProto %s, Copyright (C) 2022 Anony <github.com/anonyindian>
GoTGProto %s, Copyright (C) 2023 Anony <github.com/anonyindian>
Licensed under the terms of GNU General Public License v3
`, VERSION)
Expand All @@ -122,7 +122,7 @@ Licensed under the terms of GNU General Public License v3
client: authClient,
},
auth.SendCodeOptions{})
if err := IfAuthNecessary(authClient, ctx, authFlow); err != nil {
if err := IfAuthNecessary(authClient, ctx, Flow(authFlow)); err != nil {
return err
}
// if err := authClient.IfNecessary(ctx, authFlow); err != nil {
Expand Down
22 changes: 22 additions & 0 deletions ext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,28 @@ func (ctx *Context) ResolveUsername(username string) (types.EffectiveChat, error
return functions.ExtractContactResolvedPeer(ctx.Client.ContactsResolveUsername(ctx, strings.TrimPrefix(username, "@")))
}

// GetUserProfilePhotos invokes method photos.getUserPhotos#91cd32a8 returning error if any. Returns the list of user photos.
func (ctx *Context) GetUserProfilePhotos(userId int64, opts *tg.PhotosGetUserPhotosRequest) ([]tg.PhotoClass, error) {
peerUser := storage.GetPeerById(userId)
if peerUser.ID == 0 {
return nil, ErrPeerNotFound
}
if opts == nil {
opts = &tg.PhotosGetUserPhotosRequest{}
}
opts.UserID = &tg.InputUser{
UserID: userId,
AccessHash: peerUser.AccessHash,
}
p, err := ctx.Client.PhotosGetUserPhotos(ctx, &tg.PhotosGetUserPhotosRequest{
UserID: nil,
})
if err != nil {
return nil, err
}
return p.GetPhotos(), nil
}

// ExportSessionString returns session of authorized account in the form of string.
// Note: This session string can be used to log back in with the help of gotgproto.
// Check sessionMaker.SessionType for more information about it.
Expand Down
11 changes: 11 additions & 0 deletions generic/gen_cu.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,14 @@ func DemoteChatMember[chatUnion ChatUnion](ctx *ext.Context, chat, user chatUnio

return ctx.DemoteChatMember(chatId, userId, opts)
}

// GetUserProfilePhotos is a generic helper for ext.Context.GetUserProfilePhotos method.
func GetUserProfilePhotos[chatUnion ChatUnion](ctx *ext.Context, user chatUnion, opts *tg.PhotosGetUserPhotosRequest) ([]tg.PhotoClass, error) {

userId, err := getIdByUnion(ctx, user)
if err != nil {
return nil, err
}

return ctx.GetUserProfilePhotos(userId, opts)
}
32 changes: 16 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ go 1.18
require (
github.com/allegro/bigcache v1.2.1
github.com/go-faster/errors v0.6.1
github.com/gotd/td v0.71.0
github.com/gotd/td v0.79.0
github.com/pkg/errors v0.9.1
go.uber.org/multierr v1.8.0
go.uber.org/zap v1.23.0
gorm.io/driver/sqlite v1.2.6
gorm.io/gorm v1.22.4
go.uber.org/multierr v1.10.0
go.uber.org/zap v1.24.0
gorm.io/driver/sqlite v1.4.4
gorm.io/gorm v1.24.6
)

require (
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/go-faster/jx v0.40.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/go-faster/jx v1.0.0 // indirect
github.com/go-faster/xor v0.3.0 // indirect
github.com/gotd/ige v0.2.2 // indirect
github.com/gotd/neo v0.1.5 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.3 // indirect
github.com/klauspost/compress v1.15.12 // indirect
github.com/mattn/go-sqlite3 v1.14.9 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/segmentio/asm v1.2.0 // indirect
go.opentelemetry.io/otel v1.11.1 // indirect
go.opentelemetry.io/otel/trace v1.11.1 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
nhooyr.io/websocket v1.8.7 // indirect
rsc.io/qr v0.2.0 // indirect
)
Loading

0 comments on commit fb70c6f

Please sign in to comment.