Skip to content

Commit

Permalink
feat(*): migration to users_sql
Browse files Browse the repository at this point in the history
  • Loading branch information
oddyamill committed Aug 27, 2024
1 parent e0cbf6b commit f755be8
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 46 deletions.
6 changes: 3 additions & 3 deletions cmd/discord/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -45,7 +45,7 @@ func main() {
}

userID := GetUserID(interaction)
user := users.GetByDiscordID(userID)
user := users_sql.GetByDiscordID(userID)

responder := commands.DiscordResponder{
Interaction: *interaction.Interaction,
Expand All @@ -57,7 +57,7 @@ func main() {
return
}

if user.State != users.Ready {
if user.State != users_sql.Ready {
responder.Respond("Пошел нахуй.")
return
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/telegram/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -42,7 +42,7 @@ func main() {

func handleMessage(update tgbotapi.Update) {
var (
user *users.User
user *users_sql.User
command string
)

Expand All @@ -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("Пользователь не зарегистрирован")
Expand All @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions internal/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
20 changes: 10 additions & 10 deletions internal/commands/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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("Панель управления") +
Expand All @@ -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)),
},
},
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/commands/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions internal/commands/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -95,6 +94,6 @@ func getDiscord(context Context, responder Responder) {
return
}

users.UpdateDiscord(context.User, "")
context.User.UpdateDiscord("")
responder.Respond("Аккаунт Discord успешно отвязан!")
}
16 changes: 8 additions & 8 deletions internal/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}})
}
16 changes: 8 additions & 8 deletions internal/users_sql/users_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit f755be8

Please sign in to comment.