change logic initialization tg client

This commit is contained in:
Pavel 2024-07-01 16:02:26 +03:00
parent 61ea9b7969
commit ce8aa02c79
3 changed files with 74 additions and 66 deletions

@ -15,7 +15,7 @@ import (
type TelegramClient struct { type TelegramClient struct {
repo *dal.DAL repo *dal.DAL
TgClients []*client.Client TgClients map[int64]*client.Client
WaitingClients map[string]WaitingClient WaitingClients map[string]WaitingClient
mu sync.Mutex mu sync.Mutex
} }
@ -30,7 +30,7 @@ type WaitingClient struct {
func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, error) { func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, error) {
tgClient := &TelegramClient{ tgClient := &TelegramClient{
repo: repo, repo: repo,
TgClients: make([]*client.Client, 0), TgClients: make(map[int64]*client.Client),
WaitingClients: make(map[string]WaitingClient), WaitingClients: make(map[string]WaitingClient),
} }
@ -43,76 +43,82 @@ func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, err
} }
for _, account := range allTgAccounts { for _, account := range allTgAccounts {
authorizer := client.ClientAuthorizerr() if account.Status == model.ActiveTg {
authorizer.TdlibParameters <- &client.SetTdlibParametersRequest{ authorizer := client.ClientAuthorizerr()
UseTestDc: false, authorizer.TdlibParameters <- &client.SetTdlibParametersRequest{
DatabaseDirectory: filepath.Join(".tdlib", "database"), UseTestDc: false,
FilesDirectory: filepath.Join(".tdlib", "files"), DatabaseDirectory: filepath.Join(".tdlib", "database"),
UseFileDatabase: false, FilesDirectory: filepath.Join(".tdlib", "files"),
UseChatInfoDatabase: false, UseFileDatabase: false,
UseMessageDatabase: false, UseChatInfoDatabase: false,
UseSecretChats: false, UseMessageDatabase: false,
ApiId: account.ApiID, UseSecretChats: false,
ApiHash: account.ApiHash, ApiId: account.ApiID,
SystemLanguageCode: "en", ApiHash: account.ApiHash,
DeviceModel: "Server", SystemLanguageCode: "en",
SystemVersion: "1.0.0", DeviceModel: "Server",
ApplicationVersion: "1.0.0", SystemVersion: "1.0.0",
} ApplicationVersion: "1.0.0",
_, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
NewVerbosityLevel: 1,
})
if err != nil {
return nil, err
}
var tdlibClient *client.Client
var goErr error
go func() {
tdlibClient, goErr = client.NewClient(authorizer)
if goErr != nil {
fmt.Println("new client failed", err)
return
} }
fmt.Println("i am down")
}()
if goErr != nil {
return nil, goErr
}
for { _, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
state, ok := <-authorizer.State NewVerbosityLevel: 1,
if !ok { })
break if err != nil {
return nil, err
} }
fmt.Println("currnet state:", state)
switch state.AuthorizationStateType() {
case client.TypeAuthorizationStateWaitPhoneNumber:
authorizer.PhoneNumber <- account.PhoneNumber
case client.TypeAuthorizationStateWaitCode:
// todo inactive
case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed: var tdlibClient *client.Client
// todo inactive var goErr error
case client.TypeAuthorizationStateReady: go func() {
// костыль так как в либе тож костыль стоит пока там ьд обновиться будет ниловый всегда клиент tdlibClient, goErr = client.NewClient(authorizer)
time.Sleep(3 * time.Second) if goErr != nil {
me, err := tdlibClient.GetMe() fmt.Println("new client failed", err)
if err != nil { return
return nil, err }
fmt.Println("i am down")
}()
if goErr != nil {
return nil, goErr
}
for {
state, ok := <-authorizer.State
if !ok {
break
}
fmt.Println("currnet state:", state)
switch state.AuthorizationStateType() {
case client.TypeAuthorizationStateWaitPhoneNumber:
authorizer.PhoneNumber <- account.PhoneNumber
case client.TypeAuthorizationStateWaitCode:
err := tgClient.repo.TgRepo.UpdateStatusTg(ctx, account.ID, model.InactiveTg)
if err != nil {
return nil, err
}
case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed:
err := tgClient.repo.TgRepo.UpdateStatusTg(ctx, account.ID, model.InactiveTg)
if err != nil {
return nil, err
}
case client.TypeAuthorizationStateReady:
// костыль так как в либе тож костыль стоит пока там ьд обновиться будет ниловый всегда клиент
time.Sleep(3 * time.Second)
me, err := tdlibClient.GetMe()
if err != nil {
return nil, err
}
fmt.Printf("Me: %s %s [%v]", me.FirstName, me.LastName, me.Usernames)
tgClient.mu.Lock()
tgClient.TgClients[account.ID] = tdlibClient
tgClient.mu.Unlock()
break
case client.TypeAuthorizationStateWaitPassword:
authorizer.Password <- account.Password
} }
fmt.Printf("Me: %s %s [%v]", me.FirstName, me.LastName, me.Usernames)
tgClient.mu.Lock()
tgClient.TgClients = append(tgClient.TgClients, tdlibClient)
tgClient.mu.Unlock()
break
case client.TypeAuthorizationStateWaitPassword:
authorizer.Password <- account.Password
} }
} }
} }
return tgClient, nil return tgClient, nil
} }
@ -143,11 +149,11 @@ func (tg *TelegramClient) GetFromMap(id string) (WaitingClient, bool) {
func (tg *TelegramClient) SaveTgAccount(ctx context.Context, tdLibClient *client.Client, account model.TgAccount) (int64, error) { func (tg *TelegramClient) SaveTgAccount(ctx context.Context, tdLibClient *client.Client, account model.TgAccount) (int64, error) {
tg.mu.Lock() tg.mu.Lock()
defer tg.mu.Unlock() defer tg.mu.Unlock()
tg.TgClients = append(tg.TgClients, tdLibClient)
id, err := tg.repo.TgRepo.CreateTgAccount(ctx, account) id, err := tg.repo.TgRepo.CreateTgAccount(ctx, account)
if err != nil { if err != nil {
return 0, err return 0, err
} }
tg.TgClients[id] = tdLibClient
return id, nil return id, nil
} }

2
go.mod

@ -19,7 +19,7 @@ require (
google.golang.org/grpc v1.64.0 google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.2 google.golang.org/protobuf v1.34.2
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701123546-6dbbc07f9c24
penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990
penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f
penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae

2
go.sum

@ -287,6 +287,8 @@ penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE= penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b h1:1ALCrJxIatwnqpIUJIx31Cq+rnxFa3WGEA8scV1idVo= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b h1:1ALCrJxIatwnqpIUJIx31Cq+rnxFa3WGEA8scV1idVo=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701123546-6dbbc07f9c24 h1:hcMtuK4H3dyk2Wo911EXRYcG8GV8hZe+UwuRRGOxF+I=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701123546-6dbbc07f9c24/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0=
penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o=
penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE=
penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008=