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