From 7a15a9379cc4229bd8d60b44ade5c15d521c45cd Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 22 Oct 2023 16:06:03 +0200 Subject: [PATCH] delete user on telegram bot on delete --- modules/notification/notification.go | 13 +++++------- modules/requestclient/requestclient.go | 28 ++++++++++++++++++++++++++ socketclients/socketclients.go | 25 +++-------------------- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/modules/notification/notification.go b/modules/notification/notification.go index be5b8c7..3d5b895 100644 --- a/modules/notification/notification.go +++ b/modules/notification/notification.go @@ -1,7 +1,6 @@ package notification import ( - "jannex/admin-dashboard-backend/modules/config" "jannex/admin-dashboard-backend/modules/database" "jannex/admin-dashboard-backend/modules/requestclient" "jannex/admin-dashboard-backend/modules/structs" @@ -104,13 +103,11 @@ func AddNotification(c *fiber.Ctx, body structs.AddNotificationRequest) error { }) } - if config.Cfg.TelegramBotManagerEnabled { - requestclient.TelegramBotManagerRequestClient(structs.TelegramBotManagerRequestBody{ - UserIds: userIds, - Title: body.Title, - Type: body.Type, - }) - } + requestclient.TelegramBotManagerRequestClient(structs.TelegramBotManagerRequestBody{ + UserIds: userIds, + Title: body.Title, + Type: body.Type, + }) return c.SendStatus(fiber.StatusOK) } diff --git a/modules/requestclient/requestclient.go b/modules/requestclient/requestclient.go index 5d549aa..ad7ad57 100644 --- a/modules/requestclient/requestclient.go +++ b/modules/requestclient/requestclient.go @@ -54,6 +54,10 @@ func InvexApiRequestClient(requestMethod string, url string) (statusCode int, bo } func TelegramBotManagerRequestClient(telegramBotManagerRequestBody structs.TelegramBotManagerRequestBody) { + if !config.Cfg.TelegramBotManagerEnabled { + return + } + a := fiber.AcquireAgent() req := a.Request() @@ -83,3 +87,27 @@ func TelegramBotManagerRequestClient(telegramBotManagerRequestBody structs.Teleg logger.AddSystemLog(rslogger.LogTypeError, "TelegramBotManagerRequestClient err: %s statusCode: %s", "failed to send notification", strconv.Itoa(code)) } } + +func TelegramBotManagerDeleteUserRequestClient(userId string) { + if !config.Cfg.TelegramBotManagerEnabled { + return + } + + a := fiber.AcquireAgent() + + req := a.Request() + req.Header.SetMethod(fiber.MethodDelete) + req.SetRequestURI(config.Cfg.TelegramBotManagerServerUrl + "/v1/verifycode/" + userId) + + if err := a.Parse(); err != nil { + log.Error().Msgf("Failed to parse request, err: %s", err) + logger.AddSystemLog(rslogger.LogTypeError, "TelegramBotManagerRequestClient failed to parse request, err: %s", err.Error()) + return + } + + code, _, _ := a.Bytes() + + if code != 200 { + logger.AddSystemLog(rslogger.LogTypeError, "TelegramBotManagerRequestClient err: %s statusCode: %s", "failed to send notification", strconv.Itoa(code)) + } +} diff --git a/socketclients/socketclients.go b/socketclients/socketclients.go index 2d812ec..2ec8c7b 100644 --- a/socketclients/socketclients.go +++ b/socketclients/socketclients.go @@ -5,6 +5,7 @@ import ( "jannex/admin-dashboard-backend/modules/cache" "jannex/admin-dashboard-backend/modules/database" "jannex/admin-dashboard-backend/modules/logger" + "jannex/admin-dashboard-backend/modules/requestclient" "jannex/admin-dashboard-backend/modules/structs" "jannex/admin-dashboard-backend/modules/systempermissions" "jannex/admin-dashboard-backend/modules/utils" @@ -918,6 +919,8 @@ func AllUsersDeleteUser(conn *websocket.Conn, userId string) { logger.AddSystemLog(rslogger.LogTypeInfo, "User %s has deleted the user %s", conn.Locals("userId").(string), userId) + + requestclient.TelegramBotManagerDeleteUserRequestClient(userId) } func AllUsersUserDeactivation(conn *websocket.Conn, userId string, deactivate bool) { @@ -944,28 +947,6 @@ func AllUsersUserDeactivation(conn *websocket.Conn, userId string, deactivate bo conn.Locals("userId").(string), userId, strconv.FormatBool(deactivate)) } -/* -func ScannersUpdateScannerUsedByUserId(userId string, scannerId string) { - database.DB.Model(&structs.Scanner{}).Where("id = ?", scannerId).Updates(structs.Scanner{UsedByUserId: userId}) - - BroadcastMessage(structs.SendSocketMessage{ - Cmd: utils.SentCmdUpdateScannerUsedBy, - Body: struct { - ScannerId string - UsedByUserId string - }{ - ScannerId: scannerId, - UsedByUserId: userId, - }, - }) - - if userId != "" { - logger.AddSystemLog(rslogger.LogTypeInfo, "Scanner %s is now used by user %s", scannerId, userId) - } else { - logger.AddSystemLog(rslogger.LogTypeInfo, "Scanner %s is not longer used", scannerId) - } -}*/ - func GetUserApiKeys(userId string) []structs.UserApiKey { var apiKeys []structs.UserApiKey