SimpleCloudNotifier/scnserver/models/user.go

202 lines
5.7 KiB
Go
Raw Normal View History

2022-11-18 21:25:40 +01:00
package models
2022-11-18 23:12:37 +01:00
import (
scn "blackforestbytes.com/simplecloudnotifier"
2024-05-31 23:56:16 +02:00
"context"
"github.com/jmoiron/sqlx"
2022-11-19 12:47:23 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/langext"
2022-12-11 03:14:42 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/sq"
2022-11-18 23:12:37 +01:00
"time"
)
2022-11-18 21:25:40 +01:00
type User struct {
2022-11-20 22:18:24 +01:00
UserID UserID
2022-11-18 21:25:40 +01:00
Username *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{
2023-07-30 16:53:46 +02:00
UserID: u.UserID,
Username: u.Username,
TimestampCreated: u.TimestampCreated.Format(time.RFC3339Nano),
TimestampLastRead: timeOptFmt(u.TimestampLastRead, time.RFC3339Nano),
TimestampLastSent: timeOptFmt(u.TimestampLastSent, time.RFC3339Nano),
MessagesSent: u.MessagesSent,
QuotaUsed: u.QuotaUsedToday(),
QuotaPerDay: u.QuotaPerDay(),
QuotaRemaining: u.QuotaRemainingToday(),
IsPro: u.IsPro,
DefaultChannel: u.DefaultChannel(),
MaxBodySize: u.MaxContentLength(),
MaxTitleLength: u.MaxTitleLength(),
DefaultPriority: u.DefaultPriority(),
MaxChannelNameLength: u.MaxChannelNameLength(),
MaxChannelDescriptionLength: u.MaxChannelDescriptionLength(),
MaxSenderNameLength: u.MaxSenderNameLength(),
MaxUserMessageIDLength: u.MaxUserMessageIDLength(),
2022-11-18 21:25:40 +01:00
}
}
2023-04-21 21:45:16 +02:00
func (u User) JSONWithClients(clients []Client, ak string, sk string, rk string) UserJSONWithClientsAndKeys {
return UserJSONWithClientsAndKeys{
2022-11-24 12:53:27 +01:00
UserJSON: u.JSON(),
Clients: langext.ArrMap(clients, func(v Client) ClientJSON { return v.JSON() }),
2023-04-21 21:45:16 +02:00
SendKey: sk,
ReadKey: rk,
AdminKey: ak,
2022-11-24 12:53:27 +01:00
}
}
2022-11-19 15:13:47 +01:00
func (u User) MaxContentLength() int {
if u.IsPro {
2023-07-30 16:37:39 +02:00
return 2 * 1024 * 1024 // 2 MB
2022-11-19 15:13:47 +01:00
} else {
2023-07-30 16:37:39 +02:00
return 2 * 1024 // 2 KB
2022-11-19 15:13:47 +01:00
}
}
2022-11-20 20:34:18 +01:00
func (u User) MaxTitleLength() int {
return 120
}
2022-11-19 15:13:47 +01:00
func (u User) QuotaPerDay() int {
if u.IsPro {
2023-07-30 16:37:39 +02:00
return 5000
2022-11-19 15:13:47 +01:00
} else {
return 50
}
}
func (u User) QuotaUsedToday() int {
now := scn.QuotaDayString()
2022-11-19 15:13:47 +01:00
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-20 22:18:24 +01:00
func (u User) DefaultChannel() string {
return "main"
}
2022-11-30 21:39:14 +01:00
func (u User) DefaultPriority() int {
return 1
}
2022-11-21 22:52:44 +01:00
func (u User) MaxChannelNameLength() int {
return 120
}
2023-07-30 16:53:46 +02:00
func (u User) MaxChannelDescriptionLength() int {
return 300
}
2023-07-30 16:53:46 +02:00
func (u User) MaxSenderNameLength() int {
return 120
}
2023-07-30 16:53:46 +02:00
func (u User) MaxUserMessageIDLength() int {
return 64
}
func (u User) MaxTimestampDiffHours() int {
return 24
}
2024-06-12 00:35:06 +02:00
func (u User) JSONPreview() UserPreviewJSON {
return UserPreviewJSON{
UserID: u.UserID,
Username: u.Username,
}
}
2022-11-18 21:25:40 +01:00
type UserJSON struct {
2023-07-30 16:53:46 +02:00
UserID UserID `json:"user_id"`
Username *string `json:"username"`
TimestampCreated string `json:"timestamp_created"`
TimestampLastRead *string `json:"timestamp_lastread"`
TimestampLastSent *string `json:"timestamp_lastsent"`
MessagesSent int `json:"messages_sent"`
QuotaUsed int `json:"quota_used"`
QuotaRemaining int `json:"quota_remaining"`
QuotaPerDay int `json:"quota_max"`
IsPro bool `json:"is_pro"`
DefaultChannel string `json:"default_channel"`
MaxBodySize int `json:"max_body_size"`
MaxTitleLength int `json:"max_title_length"`
DefaultPriority int `json:"default_priority"`
MaxChannelNameLength int `json:"max_channel_name_length"`
MaxChannelDescriptionLength int `json:"max_channel_description_length"`
MaxSenderNameLength int `json:"max_sender_name_length"`
MaxUserMessageIDLength int `json:"max_user_message_id_length"`
2022-11-18 21:25:40 +01:00
}
2022-11-18 23:12:37 +01:00
2024-06-12 00:35:06 +02:00
type UserPreviewJSON struct {
UserID UserID `json:"user_id"`
Username *string `json:"username"`
}
2023-04-21 21:45:16 +02:00
type UserJSONWithClientsAndKeys struct {
2022-11-24 12:53:27 +01:00
UserJSON
2023-04-21 21:45:16 +02:00
Clients []ClientJSON `json:"clients"`
SendKey string `json:"send_key"`
ReadKey string `json:"read_key"`
AdminKey string `json:"admin_key"`
2022-11-24 12:53:27 +01:00
}
2022-11-18 23:12:37 +01:00
type UserDB struct {
2022-11-20 22:18:24 +01:00
UserID UserID `db:"user_id"`
2022-11-18 23:12:37 +01:00
Username *string `db:"username"`
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,
2023-04-21 21:45:16 +02:00
TimestampCreated: timeFromMilli(u.TimestampCreated),
2022-11-18 23:12:37 +01:00
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,
}
}
2024-05-31 23:56:16 +02:00
func DecodeUser(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (User, error) {
data, err := sq.ScanSingle[UserDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
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
}
2024-05-31 23:56:16 +02:00
func DecodeUsers(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]User, error) {
data, err := sq.ScanAll[UserDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
2022-11-19 12:47:23 +01:00
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
}