guard for dont duplicate tg accounts
This commit is contained in:
parent
ce8aa02c79
commit
456fbd3d2f
@ -2,12 +2,12 @@ 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/quiz/common.git/pj_errors"
|
||||
"penahub.gitlab.yandexcloud.net/backend/tdlib/client"
|
||||
"sync"
|
||||
"time"
|
||||
@ -36,7 +36,7 @@ func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, err
|
||||
|
||||
allTgAccounts, err := repo.TgRepo.GetAllTgAccounts(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
if errors.Is(err, pj_errors.ErrNotFound) {
|
||||
return tgClient, nil
|
||||
}
|
||||
return nil, err
|
||||
@ -141,6 +141,7 @@ func (tg *TelegramClient) GetFromMap(id string) (WaitingClient, bool) {
|
||||
tg.mu.Lock()
|
||||
defer tg.mu.Unlock()
|
||||
if data, ok := tg.WaitingClients[id]; ok {
|
||||
delete(tg.WaitingClients, id)
|
||||
return data, true
|
||||
}
|
||||
return WaitingClient{}, false
|
||||
|
2
go.mod
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-20240701123546-6dbbc07f9c24
|
||||
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730
|
||||
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
|
||||
|
6
go.sum
6
go.sum
@ -285,10 +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-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-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/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/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=
|
||||
|
@ -1,11 +1,13 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/rs/xid"
|
||||
"path/filepath"
|
||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors"
|
||||
"penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/telegram"
|
||||
"penahub.gitlab.yandexcloud.net/backend/tdlib/client"
|
||||
)
|
||||
@ -24,6 +26,17 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error {
|
||||
if err := ctx.BodyParser(&req); err != nil {
|
||||
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
||||
}
|
||||
allAccounts, err := s.dal.TgRepo.GetAllTgAccounts(ctx.Context())
|
||||
if err != nil && !errors.Is(err, pj_errors.ErrNotFound) {
|
||||
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
||||
}
|
||||
if !errors.Is(err, pj_errors.ErrNotFound) {
|
||||
for _, account := range allAccounts {
|
||||
if account.ApiID == req.ApiID && account.ApiHash == req.ApiHash && account.Status == model.ActiveTg {
|
||||
return ctx.Status(fiber.StatusConflict).SendString("this account already exist and active")
|
||||
}
|
||||
}
|
||||
}
|
||||
authorizer := client.ClientAuthorizerr()
|
||||
authorizer.TdlibParameters <- &client.SetTdlibParametersRequest{
|
||||
UseTestDc: false,
|
||||
@ -41,7 +54,7 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error {
|
||||
ApplicationVersion: "1.0.0",
|
||||
}
|
||||
|
||||
_, err := client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
|
||||
_, err = client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
|
||||
NewVerbosityLevel: 1,
|
||||
})
|
||||
if err != nil {
|
||||
@ -73,13 +86,13 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error {
|
||||
case client.TypeAuthorizationStateWaitPhoneNumber:
|
||||
authorizer.PhoneNumber <- req.PhoneNumber
|
||||
case client.TypeAuthorizationStateWaitCode:
|
||||
id := xid.New()
|
||||
signature := xid.New()
|
||||
s.telegramClient.AddedToMap(telegram.WaitingClient{
|
||||
TdLibClient: tdlibClient,
|
||||
PreviousReq: req,
|
||||
Authorizer: authorizer,
|
||||
}, id.String())
|
||||
return ctx.Status(fiber.StatusOK).JSON(fiber.Map{"id": id.String()})
|
||||
}, signature.String())
|
||||
return ctx.Status(fiber.StatusOK).JSON(fiber.Map{"signature": signature.String()})
|
||||
|
||||
case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed:
|
||||
return ctx.Status(fiber.StatusForbidden).SendString(fmt.Sprintf("auth failed, last state is %s", state))
|
||||
@ -89,14 +102,14 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error {
|
||||
|
||||
func (s *Service) SettingTgCode(ctx *fiber.Ctx) error {
|
||||
var req struct {
|
||||
Code string `json:"code"`
|
||||
ID string `json:"id"`
|
||||
Code string `json:"code"`
|
||||
Signature string `json:"signature"`
|
||||
}
|
||||
if err := ctx.BodyParser(&req); err != nil {
|
||||
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
||||
}
|
||||
|
||||
data, ok := s.telegramClient.GetFromMap(req.ID)
|
||||
data, ok := s.telegramClient.GetFromMap(req.Signature)
|
||||
if !ok {
|
||||
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid id, don't have data")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user