updated stuff

This commit is contained in:
2026-01-23 19:07:05 +11:00
parent 1667423db6
commit af6bec983b
33 changed files with 1186 additions and 222 deletions

View File

@@ -0,0 +1,50 @@
package db
import (
"context"
"time"
"git.haelnorr.com/h/oslstats/internal/discord"
"github.com/bwmarrin/discordgo"
"github.com/pkg/errors"
"github.com/uptrace/bun"
)
type DiscordToken struct {
bun.BaseModel `bun:"table:discord_tokens,alias:dt"`
DiscordID string `bun:"discord_id,pk,notnull"`
AccessToken string `bun:"access_token,notnull"`
RefreshToken string `bun:"refresh_token,notnull"`
ExpiresAt int64 `bun:"expires_at,notnull"`
}
func UpdateDiscordToken(ctx context.Context, db *bun.DB, user *discordgo.User, token *discord.Token) error {
if db == nil {
return errors.New("db cannot be nil")
}
if user == nil {
return errors.New("user cannot be nil")
}
if token == nil {
return errors.New("token cannot be nil")
}
expiresAt := time.Now().Add(time.Duration(token.ExpiresIn) * time.Second).Unix()
discordToken := &DiscordToken{
DiscordID: user.ID,
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
ExpiresAt: expiresAt,
}
_, err := db.NewInsert().
Model(discordToken).
On("CONFLICT (discord_id) DO UPDATE").
Set("access_token = EXCLUDED.access_token").
Set("refresh_token = EXCLUDED.refresh_token").
Set("expires_at = EXCLUDED.expires_at").
Exec(ctx)
return err
}