Fix [TestChannelMessageCounter]

This commit is contained in:
2023-06-17 20:08:39 +02:00
parent fb826919a6
commit d9a4c4ffd6
5 changed files with 51 additions and 18 deletions

View File

@@ -200,20 +200,25 @@ func (db *Database) GetChannel(ctx TxContext, userid models.UserID, channelid mo
return channel, nil
}
func (db *Database) IncChannelMessageCounter(ctx TxContext, channel models.Channel) error {
func (db *Database) IncChannelMessageCounter(ctx TxContext, channel *models.Channel) error {
tx, err := ctx.GetOrCreateTransaction(db)
if err != nil {
return err
}
now := time.Now()
_, err = tx.Exec(ctx, "UPDATE channels SET messages_sent = messages_sent+1, timestamp_lastsent = :ts WHERE channel_id = :cid", sq.PP{
"cid": time2DB(time.Now()),
"ts": channel.ChannelID,
"ts": time2DB(now),
"cid": channel.ChannelID,
})
if err != nil {
return err
}
channel.MessagesSent += 1
channel.TimestampLastSent = &now
return nil
}

View File

@@ -181,20 +181,25 @@ func (db *Database) UpdateKeyTokenChannels(ctx TxContext, keyTokenid models.KeyT
return nil
}
func (db *Database) IncKeyTokenMessageCounter(ctx TxContext, keyTokenid models.KeyTokenID) error {
func (db *Database) IncKeyTokenMessageCounter(ctx TxContext, keyToken *models.KeyToken) error {
tx, err := ctx.GetOrCreateTransaction(db)
if err != nil {
return err
}
now := time.Now()
_, err = tx.Exec(ctx, "UPDATE keytokens SET messages_sent = messages_sent+1, timestamp_lastused = :ts WHERE keytoken_id = :tid", sq.PP{
"ts": time2DB(time.Now()),
"tid": keyTokenid,
"ts": time2DB(now),
"tid": keyToken.KeyTokenID,
})
if err != nil {
return err
}
keyToken.TimestampLastUsed = &now
keyToken.MessagesSent += 1
return nil
}

View File

@@ -3,6 +3,7 @@ package primary
import (
scn "blackforestbytes.com/simplecloudnotifier"
"blackforestbytes.com/simplecloudnotifier/models"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
@@ -102,18 +103,24 @@ func (db *Database) UpdateUserProToken(ctx TxContext, userid models.UserID, prot
return nil
}
func (db *Database) IncUserMessageCounter(ctx TxContext, user models.User) error {
func (db *Database) IncUserMessageCounter(ctx TxContext, user *models.User) error {
tx, err := ctx.GetOrCreateTransaction(db)
if err != nil {
return err
}
now := time.Now()
quota := user.QuotaUsedToday() + 1
user.QuotaUsed = quota
user.QuotaUsedDay = langext.Ptr(scn.QuotaDayString())
user.TimestampLastSent = &now
_, 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{
"ts": time2DB(time.Now()),
"qu": quota,
"qd": scn.QuotaDayString(),
"ts": time2DB(now),
"qu": user.QuotaUsed,
"qd": user.QuotaUsedDay,
"uid": user.UserID,
})
if err != nil {