refactor nil client added to map after auth

This commit is contained in:
Pavel 2024-07-03 00:04:07 +03:00
parent 742e965289
commit 2d1456d40b
4 changed files with 53 additions and 49 deletions

@ -21,12 +21,10 @@ type TelegramClient struct {
} }
type WaitingClient struct { type WaitingClient struct {
TdLibClient *client.Client
PreviousReq AuthTgUserReq PreviousReq AuthTgUserReq
Authorizer *client.ClientAuthorizer Authorizer *client.ClientAuthorizer
} }
// todo come back saved tg accs to slice for check this status
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,
@ -49,10 +47,10 @@ func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, err
UseTestDc: false, UseTestDc: false,
DatabaseDirectory: filepath.Join(".tdlib", "database"), DatabaseDirectory: filepath.Join(".tdlib", "database"),
FilesDirectory: filepath.Join(".tdlib", "files"), FilesDirectory: filepath.Join(".tdlib", "files"),
UseFileDatabase: false, UseFileDatabase: true,
UseChatInfoDatabase: false, UseChatInfoDatabase: true,
UseMessageDatabase: false, UseMessageDatabase: true,
UseSecretChats: false, UseSecretChats: true,
ApiId: account.ApiID, ApiId: account.ApiID,
ApiHash: account.ApiHash, ApiHash: account.ApiHash,
SystemLanguageCode: "en", SystemLanguageCode: "en",
@ -147,15 +145,17 @@ func (tg *TelegramClient) GetFromMap(id string) (WaitingClient, bool) {
return WaitingClient{}, false return WaitingClient{}, false
} }
func (tg *TelegramClient) SaveTgAccount(ctx context.Context, tdLibClient *client.Client, account model.TgAccount) (int64, error) { func (tg *TelegramClient) SaveTgAccount(appID int32, appHash string, tdLibClient *client.Client) {
tg.mu.Lock() account, err := tg.repo.TgRepo.SearchIDByAppIDanAppHash(context.Background(), appID, appHash)
defer tg.mu.Unlock()
id, err := tg.repo.TgRepo.CreateTgAccount(ctx, account)
if err != nil { if err != nil {
return 0, err fmt.Println("err SaveTgAccount", err)
return
}
if account.Status == model.ActiveTg {
tg.mu.Lock()
defer tg.mu.Unlock()
tg.TgClients[account.ID] = tdLibClient
} }
tg.TgClients[id] = tdLibClient
return id, nil
} }
func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, error) { func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, error) {
@ -190,34 +190,25 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string
//} //}
// //
//fmt.Println("resp", resp) //fmt.Println("resp", resp)
//
//_, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
// ChatId: channel.Id, ChatId: channel.Id,
// MemberId: &client.MessageSenderUser{UserId: botID}, MemberId: &client.MessageSenderUser{UserId: botID},
// Status: &client.ChatMemberStatusAdministrator{ Status: &client.ChatMemberStatusAdministrator{
// CanBeEdited: true, CanBeEdited: true,
// Rights: &client.ChatAdministratorRights{ Rights: &client.ChatAdministratorRights{
// CanManageChat: false, CanManageChat: true,
// CanChangeInfo: true, CanChangeInfo: true,
// CanPostMessages: true, CanPostMessages: true,
// CanEditMessages: true, CanInviteUsers: true,
// CanDeleteMessages: true, CanRestrictMembers: true,
// CanInviteUsers: true, CanPromoteMembers: true,
// CanRestrictMembers: true, },
// CanPinMessages: true, },
// CanManageTopics: true, })
// CanPromoteMembers: true, if err != nil {
// CanManageVideoChats: true, return "", fmt.Errorf("failed to make bot admin: %s", err.Error())
// CanPostStories: true, }
// CanEditStories: true,
// CanDeleteStories: true,
// IsAnonymous: true,
// },
// },
//})
//if err != nil {
// return "", fmt.Errorf("failed to make bot admin: %s", err.Error())
//}
//_, err = activeClient.LeaveChat(&client.LeaveChatRequest{ //_, err = activeClient.LeaveChat(&client.LeaveChatRequest{
// ChatId: channel.Id, // ChatId: channel.Id,
@ -226,6 +217,8 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string
// return "", fmt.Errorf("failed to leave the channel: %s", err.Error()) // return "", fmt.Errorf("failed to leave the channel: %s", err.Error())
//} //}
fmt.Println("channel.Id", channel.Id)
inviteLink, err := activeClient.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{ inviteLink, err := activeClient.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{
ChatId: channel.Id, ChatId: channel.Id,
Name: channelName, Name: channelName,

5
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-20240701131253-42ace400b730 penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307
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
@ -34,6 +34,8 @@ require (
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible // indirect
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
@ -52,6 +54,7 @@ require (
github.com/richardlehane/msoleps v1.0.3 // indirect github.com/richardlehane/msoleps v1.0.3 // indirect
github.com/rivo/uniseg v0.4.7 // indirect github.com/rivo/uniseg v0.4.7 // indirect
github.com/tealeg/xlsx v1.0.5 // indirect github.com/tealeg/xlsx v1.0.5 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/twmb/franz-go/pkg/kmsg v1.8.0 // indirect github.com/twmb/franz-go/pkg/kmsg v1.8.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.53.0 // indirect github.com/valyala/fasthttp v1.53.0 // indirect

8
go.sum

@ -36,6 +36,10 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM= github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM=
@ -142,6 +146,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE=
github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33 h1:N9f/Q+2Ssa+yDcbfaoLTYvXmdeyUUxsJKdPUVsjSmiA= github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33 h1:N9f/Q+2Ssa+yDcbfaoLTYvXmdeyUUxsJKdPUVsjSmiA=
github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33/go.mod h1:rpcH99JknBh8seZmlOlUg51gasZH6QH34oXNsIwYT6E= github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33/go.mod h1:rpcH99JknBh8seZmlOlUg51gasZH6QH34oXNsIwYT6E=
github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf h1:TJJm6KcBssmbWzplF5lzixXl1RBAi/ViPs1GaSOkhwo= github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf h1:TJJm6KcBssmbWzplF5lzixXl1RBAi/ViPs1GaSOkhwo=
@ -287,6 +293,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-20240701131253-42ace400b730 h1:Macb4cgvNb666E5Ckz3M0QQpJNJ1aWY8lHdTSyt8ZcA= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730 h1:Macb4cgvNb666E5Ckz3M0QQpJNJ1aWY8lHdTSyt8ZcA=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307 h1:wCSZWRvg3q1AboSM8TC35429GMYna4U9sKbw9YEJHr0=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307/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=

@ -55,10 +55,10 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error {
UseTestDc: false, UseTestDc: false,
DatabaseDirectory: filepath.Join(".tdlib", "database"), DatabaseDirectory: filepath.Join(".tdlib", "database"),
FilesDirectory: filepath.Join(".tdlib", "files"), FilesDirectory: filepath.Join(".tdlib", "files"),
UseFileDatabase: false, UseFileDatabase: true,
UseChatInfoDatabase: false, UseChatInfoDatabase: true,
UseMessageDatabase: false, UseMessageDatabase: true,
UseSecretChats: false, UseSecretChats: true,
ApiId: req.ApiID, ApiId: req.ApiID,
ApiHash: req.ApiHash, ApiHash: req.ApiHash,
SystemLanguageCode: "en", SystemLanguageCode: "en",
@ -83,6 +83,7 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error {
fmt.Println("new client failed", err) fmt.Println("new client failed", err)
return return
} }
s.telegramClient.SaveTgAccount(req.ApiID, req.ApiHash, tdlibClient)
fmt.Println("i am down") fmt.Println("i am down")
}() }()
if goErr != nil { if goErr != nil {
@ -101,7 +102,6 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error {
case client.TypeAuthorizationStateWaitCode: case client.TypeAuthorizationStateWaitCode:
signature := xid.New() signature := xid.New()
s.telegramClient.AddedToMap(telegram.WaitingClient{ s.telegramClient.AddedToMap(telegram.WaitingClient{
TdLibClient: tdlibClient,
PreviousReq: req, PreviousReq: req,
Authorizer: authorizer, Authorizer: authorizer,
}, signature.String()) }, signature.String())
@ -139,7 +139,7 @@ func (s *Service) SettingTgCode(ctx *fiber.Ctx) error {
fmt.Println("currnet state:", state) fmt.Println("currnet state:", state)
switch state.AuthorizationStateType() { switch state.AuthorizationStateType() {
case client.TypeAuthorizationStateReady: case client.TypeAuthorizationStateReady:
id, err := s.telegramClient.SaveTgAccount(ctx.Context(), data.TdLibClient, model.TgAccount{ id, err := s.dal.TgRepo.CreateTgAccount(ctx.Context(), model.TgAccount{
ApiID: data.PreviousReq.ApiID, ApiID: data.PreviousReq.ApiID,
ApiHash: data.PreviousReq.ApiHash, ApiHash: data.PreviousReq.ApiHash,
PhoneNumber: data.PreviousReq.PhoneNumber, PhoneNumber: data.PreviousReq.PhoneNumber,
@ -171,7 +171,7 @@ func (s *Service) DeleteTgAccountByID(ctx *fiber.Ctx) error {
} }
//func (s *Service) TEST(ctx *fiber.Ctx) error { //func (s *Service) TEST(ctx *fiber.Ctx) error {
// botID := 6712573453 // botID := 542073142
// chanalName := "testAAAAAA" // chanalName := "testAAAAAA"
// link, err := s.telegramClient.CreateChannel(chanalName, int64(botID)) // link, err := s.telegramClient.CreateChannel(chanalName, int64(botID))
// if err != nil { // if err != nil {