SimpleCloudNotifier/scnserver/models/user.go

141 lines
3.7 KiB
Go
Raw Permalink 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"
2022-11-18 23:12:37 +01:00
)
2022-11-18 21:25:40 +01:00
type User struct {
UserID UserID `db:"user_id" json:"user_id"`
Username *string `db:"username" json:"username"`
TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"`
TimestampLastRead *SCNTime `db:"timestamp_lastread" json:"timestamp_lastread"`
TimestampLastSent *SCNTime `db:"timestamp_lastsent" json:"timestamp_lastsent"`
MessagesSent int `db:"messages_sent" json:"messages_sent"`
QuotaUsed int `db:"quota_used" json:"quota_used"`
QuotaUsedDay *string `db:"quota_used_day" json:"-"`
IsPro bool `db:"is_pro" json:"is_pro"`
ProToken *string `db:"pro_token" json:"-"`
UserExtra `db:"-"` // fields that are not in DB and are set on PreMarshal
}
type UserExtra struct {
QuotaRemaining int `json:"quota_remaining"`
QuotaPerDay int `json:"quota_max"`
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"`
}
type UserPreview struct {
UserID UserID `json:"user_id"`
Username *string `json:"username"`
}
type UserWithClientsAndKeys struct {
User
Clients []Client `json:"clients"`
SendKey string `json:"send_key"`
ReadKey string `json:"read_key"`
AdminKey string `json:"admin_key"`
}
func (u User) WithClients(clients []Client, ak string, sk string, rk string) UserWithClientsAndKeys {
return UserWithClientsAndKeys{
User: u.PreMarshal(),
Clients: clients,
SendKey: sk,
ReadKey: rk,
AdminKey: ak,
}
}
func (u *User) PreMarshal() User {
u.UserExtra = UserExtra{
2023-07-30 16:53:46 +02:00
QuotaPerDay: u.QuotaPerDay(),
QuotaRemaining: u.QuotaRemainingToday(),
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
}
return *u
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
}
func (u User) JSONPreview() UserPreview {
return UserPreview{
2024-06-12 00:35:06 +02:00
UserID: u.UserID,
Username: u.Username,
}
}