diff --git a/cmd/discord/main.go b/cmd/discord/main.go index e282fdd..1fd4442 100644 --- a/cmd/discord/main.go +++ b/cmd/discord/main.go @@ -4,7 +4,7 @@ import ( "github.com/bwmarrin/discordgo" "github.com/kiasuo/bot/internal/commands" "github.com/kiasuo/bot/internal/helpers" - "github.com/kiasuo/bot/internal/users" + "github.com/kiasuo/bot/internal/users_sql" "log" "os" "os/signal" @@ -45,7 +45,7 @@ func main() { } userID := GetUserID(interaction) - user := users.GetByDiscordID(userID) + user := users_sql.GetByDiscordID(userID) responder := commands.DiscordResponder{ Interaction: *interaction.Interaction, @@ -57,7 +57,7 @@ func main() { return } - if user.State != users.Ready { + if user.State != users_sql.Ready { responder.Respond("Пошел нахуй.") return } diff --git a/cmd/telegram/main.go b/cmd/telegram/main.go index 24b25ea..4511f52 100644 --- a/cmd/telegram/main.go +++ b/cmd/telegram/main.go @@ -4,7 +4,7 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" "github.com/kiasuo/bot/internal/commands" "github.com/kiasuo/bot/internal/helpers" - "github.com/kiasuo/bot/internal/users" + "github.com/kiasuo/bot/internal/users_sql" "log" "strings" ) @@ -42,7 +42,7 @@ func main() { func handleMessage(update tgbotapi.Update) { var ( - user *users.User + user *users_sql.User command string ) @@ -56,7 +56,7 @@ func handleMessage(update tgbotapi.Update) { return } - user = users.GetByTelegramID(update.Message.ForwardFrom.ID) + user = users_sql.GetByTelegramID(update.Message.ForwardFrom.ID) if user == nil { responder.Respond("Пользователь не зарегистрирован") @@ -65,14 +65,14 @@ func handleMessage(update tgbotapi.Update) { command = commands.AdminCommandName } else if update.Message.IsCommand() { - user = users.GetByTelegramID(update.Message.From.ID) + user = users_sql.GetByTelegramID(update.Message.From.ID) if user == nil { responder.Respond("Ты кто такой? Уйди.") return } - if user.State != users.Ready { + if user.State != users_sql.Ready { responder.Respond("Пошел отсюда.") return } @@ -101,12 +101,12 @@ func handleCallbackQuery(update tgbotapi.Update) { return } - var user *users.User + var user *users_sql.User if data[0] == commands.AdminCommandName { - user = users.GetByID(data[2]) + user = users_sql.GetByID(data[2]) } else { - user = users.GetByTelegramID(update.CallbackQuery.From.ID) + user = users_sql.GetByTelegramID(update.CallbackQuery.From.ID) } context := commands.Context{ diff --git a/compose.yaml b/compose.yaml index a388e50..7eab6ee 100644 --- a/compose.yaml +++ b/compose.yaml @@ -32,7 +32,7 @@ services: expose: - 5432 healthcheck: - test: [ "CMD", "pg_isready" ] + test: [ "CMD", "pg_isready", "-U", "$POSTGRES_USER" ] interval: 10s timeout: 5s retries: 5 diff --git a/internal/client/client.go b/internal/client/client.go index c5a2f10..0862d8f 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -4,14 +4,14 @@ import ( "bytes" "encoding/json" "errors" - "github.com/kiasuo/bot/internal/users" + "github.com/kiasuo/bot/internal/users_sql" "net/http" ) const BaseUrl = "https://kiasuo-proxy.oddya.ru/diary" type Client struct { - User users.User + User users_sql.User } func httpRequest[T any](client Client, request *http.Request) (*http.Response, *T, error) { @@ -64,7 +64,7 @@ func RefreshToken(client Client) error { client.User.AccessToken = result.AccessToken client.User.RefreshToken = result.RefreshToken - users.UpdateToken(client.User, result.AccessToken, result.RefreshToken) + client.User.UpdateToken(result.AccessToken, result.RefreshToken) return nil } diff --git a/internal/client/types.go b/internal/client/types.go index 1e7b0f4..0768e43 100644 --- a/internal/client/types.go +++ b/internal/client/types.go @@ -5,6 +5,11 @@ type Token struct { RefreshToken string `json:"refreshToken"` } +// TODO decrypt token and check expiration date +type TokenPayload struct { + Expiration int64 `json:"exp"` +} + type User struct { ID int `json:"id"` Username string `json:"username"` diff --git a/internal/commands/admin.go b/internal/commands/admin.go index 2ea6cd9..e1a1abe 100644 --- a/internal/commands/admin.go +++ b/internal/commands/admin.go @@ -2,7 +2,7 @@ package commands import ( "github.com/kiasuo/bot/internal/helpers" - "github.com/kiasuo/bot/internal/users" + "github.com/kiasuo/bot/internal/users_sql" "strconv" ) @@ -11,11 +11,11 @@ const AdminCommandName string = "-internal-admin" func AdminCommand(context Context, responder Responder, formatter Formatter) { user := context.User - state := map[users.UserState]string{ - users.Unknown: "неизвестно", - users.Ready: "готов", - users.Pending: "ожидает", - users.Blacklisted: "заблокирован", + state := map[users_sql.UserState]string{ + users_sql.Unknown: "неизвестно", + users_sql.Ready: "готов", + users_sql.Pending: "ожидает", + users_sql.Blacklisted: "заблокирован", }[user.State] template := formatter.Title("Панель управления") + @@ -26,8 +26,8 @@ func AdminCommand(context Context, responder Responder, formatter Formatter) { keyboard := Keyboard{ KeyboardRow{ KeyboardButton{ - Text: helpers.If(user.State == users.Blacklisted, "Разблокировать", "Заблокировать"), - Callback: AdminCommandName + ":blacklist:" + user.ID.Hex(), + Text: helpers.If(user.State == users_sql.Blacklisted, "Разблокировать", "Заблокировать"), + Callback: AdminCommandName + ":blacklist:" + string(rune(user.ID)), }, }, } @@ -44,8 +44,8 @@ func AdminCommand(context Context, responder Responder, formatter Formatter) { func AdminCallback(context Context, responder Responder, formatter Formatter, data []string) { switch data[1] { case "blacklist": - isBlacklisted := context.User.State == users.Blacklisted - users.UpdateState(context.User, helpers.If(isBlacklisted, users.Ready, users.Blacklisted)) + isBlacklisted := context.User.State == users_sql.Blacklisted + context.User.UpdateState(helpers.If(isBlacklisted, users_sql.Ready, users_sql.Blacklisted)) default: responder.Respond("Неизвестная команда. Меню устарело?") return diff --git a/internal/commands/context.go b/internal/commands/context.go index c3aa8bc..5bfaf6d 100644 --- a/internal/commands/context.go +++ b/internal/commands/context.go @@ -2,12 +2,12 @@ package commands import ( "github.com/kiasuo/bot/internal/client" - "github.com/kiasuo/bot/internal/users" + "github.com/kiasuo/bot/internal/users_sql" ) type Context struct { Command string - User users.User + User users_sql.User } func (c Context) GetClient() client.Client { diff --git a/internal/commands/settings.go b/internal/commands/settings.go index b832813..ff7e13e 100644 --- a/internal/commands/settings.go +++ b/internal/commands/settings.go @@ -3,7 +3,6 @@ package commands import ( "github.com/kiasuo/bot/internal/client" "github.com/kiasuo/bot/internal/helpers" - "github.com/kiasuo/bot/internal/users" "strconv" ) @@ -84,7 +83,7 @@ func updateUserStudent(context Context, responder Responder, formatter Formatter return err } - users.UpdateStudent(context.User, studentID, studentNameAcronym) + context.User.UpdateStudent(studentID, studentNameAcronym) responder.Respond("Ученик %s успешно выбран!", formatter.Bold(studentNameAcronym)) return nil } @@ -95,6 +94,6 @@ func getDiscord(context Context, responder Responder) { return } - users.UpdateDiscord(context.User, "") + context.User.UpdateDiscord("") responder.Respond("Аккаунт Discord успешно отвязан!") } diff --git a/internal/users/users.go b/internal/users/users.go index fab9b33..a59d136 100644 --- a/internal/users/users.go +++ b/internal/users/users.go @@ -80,18 +80,18 @@ func update(user User, update bson.D) { } } -func UpdateToken(user User, accessToken string, refreshToken string) { - update(user, bson.D{{Key: "accessToken", Value: accessToken}, {Key: "refreshToken", Value: refreshToken}}) +func (u User) UpdateToken(accessToken string, refreshToken string) { + update(u, bson.D{{Key: "accessToken", Value: accessToken}, {Key: "refreshToken", Value: refreshToken}}) } -func UpdateState(user User, state UserState) { - update(user, bson.D{{Key: "state", Value: state}}) +func (u User) UpdateState(state UserState) { + update(u, bson.D{{Key: "state", Value: state}}) } -func UpdateStudent(user User, studentID int, studentNameAcronym string) { - update(user, bson.D{{Key: "studentID", Value: studentID}, {Key: "studentNameAcronym", Value: studentNameAcronym}}) +func (u User) UpdateStudent(studentID int, studentNameAcronym string) { + update(u, bson.D{{Key: "studentID", Value: studentID}, {Key: "studentNameAcronym", Value: studentNameAcronym}}) } -func UpdateDiscord(user User, discordID string) { - update(user, bson.D{{Key: "discordID", Value: discordID}}) +func (u User) UpdateDiscord(discordID string) { + update(u, bson.D{{Key: "discordID", Value: discordID}}) } diff --git a/internal/users_sql/users_sql.go b/internal/users_sql/users_sql.go index fe82ed8..5070429 100644 --- a/internal/users_sql/users_sql.go +++ b/internal/users_sql/users_sql.go @@ -98,18 +98,18 @@ func GetByDiscordID(discordID string) *User { return queryRow("SELECT * FROM users WHERE discord_id = $1", discordID) } -func UpdateToken(user User, accessToken string, refreshToken string) { - query("UPDATE users SET access_token = $1, refresh_token = $2 WHERE id = $3", accessToken, refreshToken, user.ID) +func (u User) UpdateToken(accessToken string, refreshToken string) { + query("UPDATE users SET access_token = $1, refresh_token = $2 WHERE id = $3", accessToken, refreshToken, u.ID) } -func UpdateState(user User, state UserState) { - query("UPDATE users SET state = $1 WHERE id = $2", state, user.ID) +func (u User) UpdateState(state UserState) { + query("UPDATE users SET state = $1 WHERE id = $2", state, u.ID) } -func UpdateStudent(user User, studentID int, studentNameAcronym string) { - query("UPDATE users SET student_id = $1, student_name_acronym = $2 WHERE id = $3", studentID, studentNameAcronym, user.ID) +func (u User) UpdateStudent(studentID int, studentNameAcronym string) { + query("UPDATE users SET student_id = $1, student_name_acronym = $2 WHERE id = $3", studentID, studentNameAcronym, u.ID) } -func UpdateDiscord(user User, discordID string) { - query("UPDATE users SET discord_id = $1 WHERE id = $2", discordID, user.ID) +func (u User) UpdateDiscord(discordID string) { + query("UPDATE users SET discord_id = $1 WHERE id = $2", discordID, u.ID) }