2023-01-06 00:39:21 +01:00
|
|
|
package primary
|
2022-11-19 23:16:54 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
scn "blackforestbytes.com/simplecloudnotifier"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
2022-12-07 23:32:58 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/sq"
|
2022-11-19 23:16:54 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-04-21 21:45:16 +02:00
|
|
|
func (db *Database) CreateUser(ctx TxContext, protoken *string, username *string) (models.User, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return models.User{}, err
|
|
|
|
}
|
|
|
|
|
2023-05-28 22:27:38 +02:00
|
|
|
entity := models.UserDB{
|
|
|
|
UserID: models.NewUserID(),
|
2022-11-19 23:16:54 +01:00
|
|
|
Username: username,
|
2023-05-28 22:27:38 +02:00
|
|
|
TimestampCreated: time2DB(time.Now()),
|
2022-11-19 23:16:54 +01:00
|
|
|
TimestampLastRead: nil,
|
|
|
|
TimestampLastSent: nil,
|
|
|
|
MessagesSent: 0,
|
|
|
|
QuotaUsed: 0,
|
|
|
|
QuotaUsedDay: nil,
|
|
|
|
IsPro: protoken != nil,
|
|
|
|
ProToken: protoken,
|
2023-05-28 22:27:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = sq.InsertSingle(ctx, tx, "users", entity)
|
|
|
|
if err != nil {
|
|
|
|
return models.User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entity.Model(), nil
|
2022-11-19 23:16:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) ClearProTokens(ctx TxContext, protoken string) error {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
_, err = tx.Exec(ctx, "UPDATE users SET is_pro=0, pro_token=NULL WHERE pro_token = :tok", sq.PP{"tok": protoken})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) GetUser(ctx TxContext, userid models.UserID) (models.User, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return models.User{}, err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
rows, err := tx.Query(ctx, "SELECT * FROM users WHERE user_id = :uid LIMIT 1", sq.PP{"uid": userid})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return models.User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := models.DecodeUser(rows)
|
|
|
|
if err != nil {
|
|
|
|
return models.User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) UpdateUserUsername(ctx TxContext, userid models.UserID, username *string) error {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
_, err = tx.Exec(ctx, "UPDATE users SET username = :nam WHERE user_id = :uid", sq.PP{
|
|
|
|
"nam": username,
|
|
|
|
"uid": userid,
|
|
|
|
})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) UpdateUserProToken(ctx TxContext, userid models.UserID, protoken *string) error {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
_, err = tx.Exec(ctx, "UPDATE users SET pro_token = :tok, is_pro = :pro WHERE user_id = :uid", sq.PP{
|
|
|
|
"tok": protoken,
|
|
|
|
"pro": bool2DB(protoken != nil),
|
|
|
|
"uid": userid,
|
|
|
|
})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) IncUserMessageCounter(ctx TxContext, user models.User) error {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
quota := user.QuotaUsedToday() + 1
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
_, err = tx.Exec(ctx, "UPDATE users SET timestamp_lastsent = :ts, messages_sent = :ctr, quota_used = :qu, quota_used_day = :qd WHERE user_id = :uid", sq.PP{
|
|
|
|
"ts": time2DB(time.Now()),
|
|
|
|
"ctr": user.MessagesSent + 1,
|
|
|
|
"qu": quota,
|
|
|
|
"qd": scn.QuotaDayString(),
|
|
|
|
"uid": user.UserID,
|
|
|
|
})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) UpdateUserLastRead(ctx TxContext, userid models.UserID) error {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
_, err = tx.Exec(ctx, "UPDATE users SET timestamp_lastread = :ts WHERE user_id = :uid", sq.PP{
|
|
|
|
"ts": time2DB(time.Now()),
|
|
|
|
"uid": userid,
|
|
|
|
})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|