2024-10-25 15:26:03 +00:00
|
|
|
package telegram
|
2024-06-27 13:27:16 +00:00
|
|
|
|
2024-06-30 18:02:23 +00:00
|
|
|
import (
|
2024-07-01 13:21:15 +00:00
|
|
|
"errors"
|
2025-02-24 17:06:12 +00:00
|
|
|
"gitea.pena/SQuiz/common/dal"
|
|
|
|
"gitea.pena/SQuiz/common/pj_errors"
|
|
|
|
|
2024-09-20 21:43:35 +00:00
|
|
|
//"fmt"
|
2024-06-30 18:02:23 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2025-02-21 13:17:41 +00:00
|
|
|
// "github.com/rs/xid"
|
2024-09-20 21:43:35 +00:00
|
|
|
//"path/filepath"
|
2025-02-21 13:17:41 +00:00
|
|
|
// "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
2025-02-24 17:06:12 +00:00
|
|
|
// "gitea.pena/SQuiz/core/clients/telegram"
|
2025-02-21 13:17:41 +00:00
|
|
|
// "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-10-25 15:26:03 +00:00
|
|
|
type Deps struct {
|
2025-02-24 17:06:12 +00:00
|
|
|
DAL *dal.DAL
|
|
|
|
//TelegramClient *telegram.TelegramClient
|
2024-10-25 15:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Telegram struct {
|
2025-02-24 17:06:12 +00:00
|
|
|
dal *dal.DAL
|
|
|
|
//telegramClient *telegram.TelegramClient
|
2024-10-25 15:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTelegramController(deps Deps) *Telegram {
|
|
|
|
return &Telegram{
|
2025-02-24 17:06:12 +00:00
|
|
|
dal: deps.DAL,
|
|
|
|
//telegramClient: deps.TelegramClient,
|
2024-10-25 15:26:03 +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-10-25 15:26:03 +00:00
|
|
|
func (r *Telegram) GetPoolTgAccounts(ctx *fiber.Ctx) error {
|
|
|
|
allAccounts, err := r.dal.TgRepo.GetAllTgAccounts(ctx.Context())
|
2024-07-01 13:41:00 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2025-02-24 17:06:12 +00:00
|
|
|
func (r *Telegram) AddingTgAccount(ctx *fiber.Ctx) error {
|
2024-09-20 21:43:35 +00:00
|
|
|
// var req telegram.AuthTgUserReq
|
|
|
|
// if err := ctx.BodyParser(&req); err != nil {
|
|
|
|
// return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
|
|
|
// }
|
|
|
|
// if req.ApiID == 0 || req.ApiHash == "" || req.Password == "" || req.PhoneNumber == "" {
|
|
|
|
// return ctx.Status(fiber.StatusBadRequest).SendString("empty required fields")
|
|
|
|
// }
|
|
|
|
// 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,
|
|
|
|
// DatabaseDirectory: filepath.Join(".tdlib", "database"),
|
|
|
|
// FilesDirectory: filepath.Join(".tdlib", "files"),
|
|
|
|
// UseFileDatabase: true,
|
|
|
|
// UseChatInfoDatabase: true,
|
|
|
|
// UseMessageDatabase: true,
|
|
|
|
// UseSecretChats: true,
|
|
|
|
// ApiId: req.ApiID,
|
|
|
|
// ApiHash: req.ApiHash,
|
|
|
|
// SystemLanguageCode: "en",
|
|
|
|
// DeviceModel: "Server",
|
|
|
|
// SystemVersion: "1.0.0",
|
|
|
|
// ApplicationVersion: "1.0.0",
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// _, err = client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
|
|
|
|
// NewVerbosityLevel: 1,
|
|
|
|
// })
|
|
|
|
// if err != nil {
|
|
|
|
// return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// var tdlibClient *client.Client
|
|
|
|
// // завершается уже в другом контроллере
|
|
|
|
// var goErr error
|
|
|
|
// // todo ужно продумать завершение горутины если код вставлять не пошли
|
|
|
|
// go func() {
|
|
|
|
// tdlibClient, goErr = client.NewClient(authorizer)
|
|
|
|
// if goErr != nil {
|
|
|
|
// fmt.Println("new client failed", err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// s.telegramClient.SaveTgAccount(req.ApiID, req.ApiHash, tdlibClient)
|
|
|
|
// fmt.Println("i am down")
|
|
|
|
// }()
|
|
|
|
// if goErr != nil {
|
|
|
|
// return ctx.Status(fiber.StatusInternalServerError).SendString(goErr.Error())
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// for {
|
|
|
|
// state, ok := <-authorizer.State
|
|
|
|
// if !ok {
|
|
|
|
// return ctx.Status(fiber.StatusOK).SendString("state chan is close auth maybe ok")
|
|
|
|
// }
|
|
|
|
// fmt.Println("currnet state:", state)
|
|
|
|
// switch state.AuthorizationStateType() {
|
|
|
|
// case client.TypeAuthorizationStateWaitPhoneNumber:
|
|
|
|
// authorizer.PhoneNumber <- req.PhoneNumber
|
|
|
|
// case client.TypeAuthorizationStateWaitCode:
|
|
|
|
// signature := xid.New()
|
|
|
|
// s.telegramClient.AddedToMap(telegram.WaitingClient{
|
|
|
|
// PreviousReq: req,
|
|
|
|
// Authorizer: authorizer,
|
|
|
|
// }, 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))
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
return nil
|
2024-06-30 18:02:23 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
func (r *Telegram) SettingTgCode(ctx *fiber.Ctx) error {
|
2024-06-30 18:02:23 +00:00
|
|
|
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")
|
|
|
|
}
|
2025-04-19 23:01:02 +00:00
|
|
|
|
2024-09-20 21:43:35 +00:00
|
|
|
// data, ok := s.telegramClient.GetFromMap(req.Signature)
|
|
|
|
// 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 {
|
|
|
|
// return ctx.Status(fiber.StatusNoContent).SendString("state chan is close auth maybe ok")
|
|
|
|
// }
|
|
|
|
// fmt.Println("currnet state:", state)
|
|
|
|
// }
|
|
|
|
return nil
|
|
|
|
// switch state.AuthorizationStateType() {
|
|
|
|
// case client.TypeAuthorizationStateReady:
|
|
|
|
// id, err := s.dal.TgRepo.CreateTgAccount(ctx.Context(), model.TgAccount{
|
|
|
|
// 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})
|
|
|
|
// case client.TypeAuthorizationStateWaitPassword:
|
|
|
|
// data.Authorizer.Password <- data.PreviousReq.Password
|
|
|
|
// case client.TypeAuthorizationStateLoggingOut, client.TypeAuthorizationStateClosing, client.TypeAuthorizationStateClosed:
|
|
|
|
// return ctx.Status(fiber.StatusForbidden).SendString(fmt.Sprintf("auth failed, last state is %s", state))
|
|
|
|
// }
|
|
|
|
// }
|
2024-06-27 13:27:16 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
func (r *Telegram) 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")
|
|
|
|
}
|
2024-10-25 15:26:03 +00:00
|
|
|
err = r.dal.TgRepo.SoftDeleteTgAccount(ctx.Context(), id)
|
2024-07-01 13:41:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
}
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
2024-06-27 13:27:16 +00:00
|
|
|
}
|