2023-01-06 00:39:21 +01:00
|
|
|
package primary
|
2022-11-19 23:16:54 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
scn "blackforestbytes.com/simplecloudnotifier"
|
2023-07-27 17:44:06 +02:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/db"
|
2022-11-19 23:16:54 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
2023-06-17 20:08:39 +02:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
2022-12-07 23:32:58 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/sq"
|
2022-11-19 23:16:54 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) CreateUser(ctx db.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
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) ClearProTokens(ctx db.TxContext, 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 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
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) GetUser(ctx db.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
|
|
|
|
}
|
|
|
|
|
2024-05-31 23:56:16 +02:00
|
|
|
user, err := models.DecodeUser(ctx, tx, rows)
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return models.User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) UpdateUserUsername(ctx db.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
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) UpdateUserProToken(ctx db.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
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) IncUserMessageCounter(ctx db.TxContext, user *models.User) error {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-17 20:08:39 +02:00
|
|
|
now := time.Now()
|
|
|
|
|
2022-11-19 23:16:54 +01:00
|
|
|
quota := user.QuotaUsedToday() + 1
|
|
|
|
|
2023-06-17 20:08:39 +02:00
|
|
|
user.QuotaUsed = quota
|
|
|
|
user.QuotaUsedDay = langext.Ptr(scn.QuotaDayString())
|
|
|
|
|
2023-06-10 03:41:54 +02:00
|
|
|
_, err = tx.Exec(ctx, "UPDATE users SET timestamp_lastsent = :ts, messages_sent = messages_sent+1, quota_used = :qu, quota_used_day = :qd WHERE user_id = :uid", sq.PP{
|
2023-06-17 20:08:39 +02:00
|
|
|
"ts": time2DB(now),
|
|
|
|
"qu": user.QuotaUsed,
|
|
|
|
"qd": user.QuotaUsedDay,
|
2022-12-07 22:11:44 +01:00
|
|
|
"uid": user.UserID,
|
|
|
|
})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-17 20:16:02 +02:00
|
|
|
user.TimestampLastSent = &now
|
|
|
|
user.MessagesSent = user.MessagesSent + 1
|
|
|
|
|
2022-11-19 23:16:54 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) UpdateUserLastRead(ctx db.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
|
|
|
|
}
|