2024-06-27 13:27:16 +00:00
|
|
|
package service
|
|
|
|
|
2024-06-30 18:02:23 +00:00
|
|
|
import (
|
2024-07-01 13:21:15 +00:00
|
|
|
"errors"
|
2024-06-30 18:02:23 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
"path/filepath"
|
2024-07-01 08:53:42 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
2024-07-01 13:21:15 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors"
|
2024-06-30 18:02:23 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/core/clients/telegram"
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/tdlib/client"
|
2024-07-01 13:41:00 +00:00
|
|
|
"strconv"
|
2024-06-30 18:02:23 +00:00
|
|
|
)
|
2024-06-27 13:27:16 +00:00
|
|
|
|
2024-06-30 18:02:23 +00:00
|
|
|
type Message struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Data string `json:"data"`
|
2024-06-27 13:27:16 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 18:02:23 +00:00
|
|
|
func (s *Service) GetPoolTgAccounts(ctx *fiber.Ctx) error {
|
2024-07-01 13:41:00 +00:00
|
|
|
allAccounts, err := s.dal.TgRepo.GetAllTgAccounts(ctx.Context())
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, pj_errors.ErrNotFound):
|
|
|
|
return ctx.Status(fiber.StatusNotFound).SendString("not found")
|
|
|
|
default:
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(allAccounts)
|
2024-06-27 13:27:16 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 18:02:23 +00:00
|
|
|
func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error {
|
|
|
|
var req telegram.AuthTgUserReq
|
|
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
|
|
|
}
|
2024-07-01 14:52:14 +00:00
|
|
|
if req.ApiID == 0 || req.ApiHash == "" || req.Password == "" || req.PhoneNumber == "" {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("empty required fields")
|
|
|
|
}
|
2024-07-01 13:21:15 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-30 18:02:23 +00:00
|
|
|
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: req.ApiID,
|
|
|
|
ApiHash: req.ApiHash,
|
|
|
|
SystemLanguageCode: "en",
|
|
|
|
DeviceModel: "Server",
|
|
|
|
SystemVersion: "1.0.0",
|
|
|
|
ApplicationVersion: "1.0.0",
|
|
|
|
}
|
|
|
|
|
2024-07-01 13:21:15 +00:00
|
|
|
_, err = client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
|
2024-06-30 18:02:23 +00:00
|
|
|
NewVerbosityLevel: 1,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var tdlibClient *client.Client
|
2024-07-01 08:16:47 +00:00
|
|
|
// завершается уже в другом контроллере
|
|
|
|
var goErr error
|
2024-06-30 18:02:23 +00:00
|
|
|
go func() {
|
2024-07-01 08:16:47 +00:00
|
|
|
tdlibClient, goErr = client.NewClient(authorizer)
|
|
|
|
if goErr != nil {
|
2024-06-30 18:02:23 +00:00
|
|
|
fmt.Println("new client failed", err)
|
|
|
|
return
|
|
|
|
}
|
2024-07-01 08:16:47 +00:00
|
|
|
fmt.Println("i am down")
|
2024-06-30 18:02:23 +00:00
|
|
|
}()
|
2024-07-01 08:16:47 +00:00
|
|
|
if goErr != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(goErr.Error())
|
|
|
|
}
|
2024-06-30 18:02:23 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
state, ok := <-authorizer.State
|
|
|
|
if !ok {
|
2024-07-01 08:16:47 +00:00
|
|
|
return ctx.Status(fiber.StatusOK).SendString("state chan is close auth maybe ok")
|
2024-06-30 18:02:23 +00:00
|
|
|
}
|
|
|
|
fmt.Println("currnet state:", state)
|
|
|
|
switch state.AuthorizationStateType() {
|
|
|
|
case client.TypeAuthorizationStateWaitPhoneNumber:
|
|
|
|
authorizer.PhoneNumber <- req.PhoneNumber
|
|
|
|
case client.TypeAuthorizationStateWaitCode:
|
2024-07-01 13:21:15 +00:00
|
|
|
signature := xid.New()
|
2024-06-30 18:02:23 +00:00
|
|
|
s.telegramClient.AddedToMap(telegram.WaitingClient{
|
|
|
|
TdLibClient: tdlibClient,
|
|
|
|
PreviousReq: req,
|
|
|
|
Authorizer: authorizer,
|
2024-07-01 13:21:15 +00:00
|
|
|
}, signature.String())
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(fiber.Map{"signature": signature.String()})
|
2024-06-30 18:02:23 +00:00
|
|
|
|
|
|
|
case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed:
|
2024-07-01 08:16:47 +00:00
|
|
|
return ctx.Status(fiber.StatusForbidden).SendString(fmt.Sprintf("auth failed, last state is %s", state))
|
2024-06-30 18:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) SettingTgCode(ctx *fiber.Ctx) error {
|
|
|
|
var req struct {
|
2024-07-01 13:21:15 +00:00
|
|
|
Code string `json:"code"`
|
|
|
|
Signature string `json:"signature"`
|
2024-06-30 18:02:23 +00:00
|
|
|
}
|
|
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
|
|
|
}
|
|
|
|
|
2024-07-01 14:52:14 +00:00
|
|
|
if req.Code == "" || req.Signature == "" {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("empty required fields")
|
|
|
|
}
|
|
|
|
|
2024-07-01 13:21:15 +00:00
|
|
|
data, ok := s.telegramClient.GetFromMap(req.Signature)
|
2024-06-30 18:02:23 +00:00
|
|
|
if !ok {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid id, don't have data")
|
|
|
|
}
|
|
|
|
data.Authorizer.Code <- req.Code
|
|
|
|
for {
|
|
|
|
state, ok := <-data.Authorizer.State
|
|
|
|
if !ok {
|
2024-07-01 14:52:14 +00:00
|
|
|
return ctx.Status(fiber.StatusNoContent).SendString("state chan is close auth maybe ok")
|
2024-06-30 18:02:23 +00:00
|
|
|
}
|
|
|
|
fmt.Println("currnet state:", state)
|
|
|
|
switch state.AuthorizationStateType() {
|
|
|
|
case client.TypeAuthorizationStateReady:
|
2024-07-01 11:15:15 +00:00
|
|
|
id, err := s.telegramClient.SaveTgAccount(ctx.Context(), data.TdLibClient, model.TgAccount{
|
2024-07-01 08:53:42 +00:00
|
|
|
ApiID: data.PreviousReq.ApiID,
|
|
|
|
ApiHash: data.PreviousReq.ApiHash,
|
|
|
|
PhoneNumber: data.PreviousReq.PhoneNumber,
|
|
|
|
Status: model.ActiveTg,
|
|
|
|
Password: data.PreviousReq.Password,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(fiber.Map{"id": id})
|
2024-06-30 18:02:23 +00:00
|
|
|
case client.TypeAuthorizationStateWaitPassword:
|
|
|
|
data.Authorizer.Password <- data.PreviousReq.Password
|
|
|
|
case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed:
|
2024-07-01 08:16:47 +00:00
|
|
|
return ctx.Status(fiber.StatusForbidden).SendString(fmt.Sprintf("auth failed, last state is %s", state))
|
2024-06-30 18:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-27 13:27:16 +00:00
|
|
|
}
|
|
|
|
|
2024-06-30 18:02:23 +00:00
|
|
|
func (s *Service) DeleteTgAccountByID(ctx *fiber.Ctx) error {
|
2024-07-01 13:41:00 +00:00
|
|
|
id, err := strconv.ParseInt(ctx.Params("id"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("invalid id format")
|
|
|
|
}
|
|
|
|
err = s.dal.TgRepo.SoftDeleteTgAccount(ctx.Context(), id)
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
}
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
2024-06-27 13:27:16 +00:00
|
|
|
}
|