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"
|
|
|
|
"database/sql"
|
2022-12-07 23:32:58 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/sq"
|
2022-11-19 23:16:54 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (db *Database) CreateUser(ctx TxContext, readKey string, sendKey string, adminKey string, protoken *string, username *string) (models.User, error) {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return models.User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
2023-01-14 00:48:51 +01:00
|
|
|
userid := models.NewUserID()
|
|
|
|
|
|
|
|
_, err = tx.Exec(ctx, "INSERT INTO users (user_id, username, read_key, send_key, admin_key, is_pro, pro_token, timestamp_created) VALUES (:uid, :un, :rk, :sk, :ak, :pro, :tok, :ts)", sq.PP{
|
|
|
|
"uid": userid,
|
2022-12-07 22:11:44 +01:00
|
|
|
"un": username,
|
|
|
|
"rk": readKey,
|
|
|
|
"sk": sendKey,
|
|
|
|
"ak": adminKey,
|
|
|
|
"pro": bool2DB(protoken != nil),
|
|
|
|
"tok": protoken,
|
|
|
|
"ts": time2DB(now),
|
|
|
|
})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return models.User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return models.User{
|
2023-01-14 00:48:51 +01:00
|
|
|
UserID: userid,
|
2022-11-19 23:16:54 +01:00
|
|
|
Username: username,
|
|
|
|
ReadKey: readKey,
|
|
|
|
SendKey: sendKey,
|
|
|
|
AdminKey: adminKey,
|
|
|
|
TimestampCreated: now,
|
|
|
|
TimestampLastRead: nil,
|
|
|
|
TimestampLastSent: nil,
|
|
|
|
MessagesSent: 0,
|
|
|
|
QuotaUsed: 0,
|
|
|
|
QuotaUsedDay: nil,
|
|
|
|
IsPro: protoken != nil,
|
|
|
|
ProToken: protoken,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetUserByKey(ctx TxContext, key string) (*models.User, error) {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
rows, err := tx.Query(ctx, "SELECT * FROM users WHERE admin_key = :key OR send_key = :key OR read_key = :key LIMIT 1", sq.PP{"key": key})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := models.DecodeUser(rows)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, 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
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) UpdateUserKeys(ctx TxContext, userid models.UserID, sendKey string, readKey string, adminKey string) error {
|
2022-11-20 01:28:32 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-21 18:14:13 +01:00
|
|
|
_, err = tx.Exec(ctx, "UPDATE users SET send_key = :sk, read_key = :rk, admin_key = :ak WHERE user_id = :uid", sq.PP{
|
2022-12-07 22:11:44 +01:00
|
|
|
"sk": sendKey,
|
|
|
|
"rk": readKey,
|
|
|
|
"ak": adminKey,
|
|
|
|
"uid": userid,
|
|
|
|
})
|
2022-11-20 01:28:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-20 21:35:08 +01:00
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) UpdateUserSendKey(ctx TxContext, userid models.UserID, newkey string) error {
|
2022-11-20 21:35:08 +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 send_key = :sk WHERE user_id = :uid", sq.PP{
|
|
|
|
"sk": newkey,
|
|
|
|
"uid": userid,
|
|
|
|
})
|
2022-11-20 21:35:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) UpdateUserReadKey(ctx TxContext, userid models.UserID, newkey string) error {
|
2022-11-20 21:35:08 +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 read_key = :rk WHERE user_id = :uid", sq.PP{
|
|
|
|
"rk": newkey,
|
|
|
|
"uid": userid,
|
|
|
|
})
|
2022-11-20 21:35:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) UpdateUserAdminKey(ctx TxContext, userid models.UserID, newkey string) error {
|
2022-11-20 21:35:08 +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 admin_key = :ak WHERE user_id = :uid", sq.PP{
|
|
|
|
"ak": newkey,
|
|
|
|
"uid": userid,
|
|
|
|
})
|
2022-11-20 21:35:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|