2022-11-18 21:25:40 +01:00
|
|
|
package models
|
|
|
|
|
2022-11-18 23:12:37 +01:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"github.com/blockloop/scan"
|
2022-11-19 12:47:23 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
2022-11-19 15:13:47 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
|
2022-11-18 23:12:37 +01:00
|
|
|
"time"
|
|
|
|
)
|
2022-11-18 21:25:40 +01:00
|
|
|
|
|
|
|
type User struct {
|
|
|
|
UserID int64
|
|
|
|
Username *string
|
|
|
|
SendKey string
|
2022-11-18 23:12:37 +01:00
|
|
|
ReadKey string
|
2022-11-18 21:25:40 +01:00
|
|
|
AdminKey string
|
|
|
|
TimestampCreated time.Time
|
|
|
|
TimestampLastRead *time.Time
|
|
|
|
TimestampLastSent *time.Time
|
|
|
|
MessagesSent int
|
2022-11-19 15:13:47 +01:00
|
|
|
QuotaUsed int
|
|
|
|
QuotaUsedDay *string
|
2022-11-18 21:25:40 +01:00
|
|
|
IsPro bool
|
|
|
|
ProToken *string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u User) JSON() UserJSON {
|
|
|
|
return UserJSON{
|
|
|
|
UserID: u.UserID,
|
|
|
|
Username: u.Username,
|
|
|
|
ReadKey: u.ReadKey,
|
|
|
|
SendKey: u.SendKey,
|
|
|
|
AdminKey: u.AdminKey,
|
|
|
|
TimestampCreated: u.TimestampCreated.Format(time.RFC3339Nano),
|
|
|
|
TimestampLastRead: timeOptFmt(u.TimestampLastRead, time.RFC3339Nano),
|
|
|
|
TimestampLastSent: timeOptFmt(u.TimestampLastSent, time.RFC3339Nano),
|
|
|
|
MessagesSent: u.MessagesSent,
|
2022-11-19 15:13:47 +01:00
|
|
|
QuotaUsed: u.QuotaUsed,
|
|
|
|
QuotaUsedDay: u.QuotaUsedDay,
|
2022-11-18 21:25:40 +01:00
|
|
|
IsPro: u.IsPro,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 15:13:47 +01:00
|
|
|
func (u User) MaxContentLength() int {
|
|
|
|
if u.IsPro {
|
|
|
|
return 16384
|
|
|
|
} else {
|
|
|
|
return 2048
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u User) QuotaPerDay() int {
|
|
|
|
if u.IsPro {
|
|
|
|
return 1000
|
|
|
|
} else {
|
|
|
|
return 50
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u User) QuotaUsedToday() int {
|
|
|
|
now := time.Now().In(timeext.TimezoneBerlin).Format("2006-01-02")
|
|
|
|
if u.QuotaUsedDay != nil && *u.QuotaUsedDay == now {
|
|
|
|
return u.QuotaUsed
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u User) QuotaRemainingToday() int {
|
|
|
|
return u.QuotaPerDay() - u.QuotaUsedToday()
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:25:40 +01:00
|
|
|
type UserJSON struct {
|
|
|
|
UserID int64 `json:"user_id"`
|
|
|
|
Username *string `json:"username"`
|
|
|
|
ReadKey string `json:"read_key"`
|
|
|
|
SendKey string `json:"send_key"`
|
|
|
|
AdminKey string `json:"admin_key"`
|
|
|
|
TimestampCreated string `json:"timestamp_created"`
|
|
|
|
TimestampLastRead *string `json:"timestamp_last_read"`
|
|
|
|
TimestampLastSent *string `json:"timestamp_last_sent"`
|
|
|
|
MessagesSent int `json:"messages_sent"`
|
2022-11-19 15:13:47 +01:00
|
|
|
QuotaUsed int `json:"quota_used"`
|
|
|
|
QuotaUsedDay *string `json:"quota_used_day"`
|
2022-11-18 21:25:40 +01:00
|
|
|
IsPro bool `json:"is_pro"`
|
|
|
|
}
|
2022-11-18 23:12:37 +01:00
|
|
|
|
|
|
|
type UserDB struct {
|
|
|
|
UserID int64 `db:"user_id"`
|
|
|
|
Username *string `db:"username"`
|
|
|
|
SendKey string `db:"send_key"`
|
|
|
|
ReadKey string `db:"read_key"`
|
|
|
|
AdminKey string `db:"admin_key"`
|
|
|
|
TimestampCreated int64 `db:"timestamp_created"`
|
|
|
|
TimestampLastRead *int64 `db:"timestamp_lastread"`
|
|
|
|
TimestampLastSent *int64 `db:"timestamp_lastsent"`
|
|
|
|
MessagesSent int `db:"messages_sent"`
|
2022-11-19 15:13:47 +01:00
|
|
|
QuotaUsed int `db:"quota_used"`
|
|
|
|
QuotaUsedDay *string `db:"quota_used_day"`
|
2022-11-18 23:12:37 +01:00
|
|
|
IsPro bool `db:"is_pro"`
|
|
|
|
ProToken *string `db:"pro_token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u UserDB) Model() User {
|
|
|
|
return User{
|
|
|
|
UserID: u.UserID,
|
|
|
|
Username: u.Username,
|
|
|
|
SendKey: u.SendKey,
|
|
|
|
ReadKey: u.ReadKey,
|
|
|
|
AdminKey: u.AdminKey,
|
|
|
|
TimestampCreated: time.UnixMilli(u.TimestampCreated),
|
|
|
|
TimestampLastRead: timeOptFromMilli(u.TimestampLastRead),
|
|
|
|
TimestampLastSent: timeOptFromMilli(u.TimestampLastSent),
|
|
|
|
MessagesSent: u.MessagesSent,
|
2022-11-19 15:13:47 +01:00
|
|
|
QuotaUsed: u.QuotaUsed,
|
|
|
|
QuotaUsedDay: u.QuotaUsedDay,
|
2022-11-18 23:12:37 +01:00
|
|
|
IsPro: u.IsPro,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeUser(r *sql.Rows) (User, error) {
|
2022-11-19 12:47:23 +01:00
|
|
|
var data UserDB
|
|
|
|
err := scan.RowStrict(&data, r)
|
2022-11-18 23:12:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return User{}, err
|
|
|
|
}
|
2022-11-19 12:47:23 +01:00
|
|
|
return data.Model(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeUsers(r *sql.Rows) ([]User, error) {
|
|
|
|
var data []UserDB
|
|
|
|
err := scan.RowsStrict(&data, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return langext.ArrMap(data, func(v UserDB) User { return v.Model() }), nil
|
2022-11-18 23:12:37 +01:00
|
|
|
}
|