added base logic for restore tg clients from db

This commit is contained in:
Pavel 2024-07-01 14:15:15 +03:00
parent 51aa0e4aee
commit 61ea9b7969
4 changed files with 107 additions and 9 deletions

@ -2,15 +2,22 @@ package telegram
import (
"context"
"database/sql"
"errors"
"fmt"
"path/filepath"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
"penahub.gitlab.yandexcloud.net/backend/tdlib/client"
"sync"
"time"
)
type TelegramClient struct {
repo *dal.DAL
TgClients []*client.Client
WaitingClients map[string]WaitingClient
mu sync.Mutex
}
type WaitingClient struct {
@ -21,11 +28,92 @@ type WaitingClient struct {
// todo come back saved tg accs to slice for check this status
func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, error) {
return &TelegramClient{
tgClient := &TelegramClient{
repo: repo,
TgClients: make([]*client.Client, 0),
WaitingClients: make(map[string]WaitingClient),
}, nil
}
allTgAccounts, err := repo.TgRepo.GetAllTgAccounts(ctx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return tgClient, nil
}
return nil, err
}
for _, account := range allTgAccounts {
authorizer := client.ClientAuthorizerr()
authorizer.TdlibParameters <- &client.SetTdlibParametersRequest{
UseTestDc: false,
DatabaseDirectory: filepath.Join(".tdlib", "database"),
FilesDirectory: filepath.Join(".tdlib", "files"),
UseFileDatabase: false,
UseChatInfoDatabase: false,
UseMessageDatabase: false,
UseSecretChats: false,
ApiId: account.ApiID,
ApiHash: account.ApiHash,
SystemLanguageCode: "en",
DeviceModel: "Server",
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 {
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:
// todo inactive
case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed:
// todo inactive
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 = append(tgClient.TgClients, tdlibClient)
tgClient.mu.Unlock()
break
case client.TypeAuthorizationStateWaitPassword:
authorizer.Password <- account.Password
}
}
}
return tgClient, nil
}
type AuthTgUserReq struct {
@ -37,19 +125,30 @@ type AuthTgUserReq struct {
func (tg *TelegramClient) AddedToMap(data WaitingClient, id string) {
fmt.Println("AddedToMap")
tg.mu.Lock()
defer tg.mu.Unlock()
tg.WaitingClients[id] = data
}
func (tg *TelegramClient) GetFromMap(id string) (WaitingClient, bool) {
fmt.Println("GetFromMap")
tg.mu.Lock()
defer tg.mu.Unlock()
if data, ok := tg.WaitingClients[id]; ok {
return data, true
}
return WaitingClient{}, false
}
func (tg *TelegramClient) SaveTgAccount() {
func (tg *TelegramClient) SaveTgAccount(ctx context.Context, tdLibClient *client.Client, account model.TgAccount) (int64, error) {
tg.mu.Lock()
defer tg.mu.Unlock()
tg.TgClients = append(tg.TgClients, tdLibClient)
id, err := tg.repo.TgRepo.CreateTgAccount(ctx, account)
if err != nil {
return 0, err
}
return id, nil
}
func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, error) {

2
go.mod

@ -19,7 +19,7 @@ require (
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.2
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240627194440-f997b952c31f
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701091250-bf14e745201b
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/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae

4
go.sum

@ -285,8 +285,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c h1:CWb4UcuNXhd1KTNOmy2U0TJO4+Qxgxrj5cwkyFqbgrk=
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-20240627194440-f997b952c31f h1:cvXLN5J6Np+lRbL1l4Kumu1uRB/D1OR2cbXc3qATzWc=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240627194440-f997b952c31f/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0=
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/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/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008=

@ -109,8 +109,7 @@ func (s *Service) SettingTgCode(ctx *fiber.Ctx) error {
fmt.Println("currnet state:", state)
switch state.AuthorizationStateType() {
case client.TypeAuthorizationStateReady:
s.telegramClient.TgClients = append(s.telegramClient.TgClients, data.TdLibClient)
id, err := s.dal.TgRepo.CreateTgAccount(ctx.Context(), model.TgAccount{
id, err := s.telegramClient.SaveTgAccount(ctx.Context(), data.TdLibClient, model.TgAccount{
ApiID: data.PreviousReq.ApiID,
ApiHash: data.PreviousReq.ApiHash,
PhoneNumber: data.PreviousReq.PhoneNumber,