Refactor models to use single struct per entity

This commit is contained in:
2024-09-15 21:07:46 +02:00
parent 6d432b9de4
commit 527a659a1b
41 changed files with 778 additions and 1576 deletions

View File

@@ -1,37 +1,28 @@
package models
import (
"context"
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
type Channel struct {
ChannelID ChannelID
OwnerUserID UserID
InternalName string
DisplayName string
DescriptionName *string
SubscribeKey string
TimestampCreated time.Time
TimestampLastSent *time.Time
MessagesSent int
ChannelID ChannelID `db:"channel_id" json:"channel_id"`
OwnerUserID UserID `db:"owner_user_id" json:"owner_user_id"`
InternalName string `db:"internal_name" json:"internal_name"`
DisplayName string `db:"display_name" json:"display_name"`
DescriptionName *string `db:"description_name" json:"description_name"`
SubscribeKey string `db:"subscribe_key" json:"subscribe_key" jsonfilter:"INCLUDE_KEY"` // can be nil, depending on endpoint
TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"`
TimestampLastSent *SCNTime `db:"timestamp_lastsent" json:"timestamp_lastsent"`
MessagesSent int `db:"messages_sent" json:"messages_sent"`
}
func (c Channel) JSON(includeKey bool) ChannelJSON {
return ChannelJSON{
ChannelID: c.ChannelID,
OwnerUserID: c.OwnerUserID,
InternalName: c.InternalName,
DisplayName: c.DisplayName,
DescriptionName: c.DescriptionName,
SubscribeKey: langext.Conditional(includeKey, langext.Ptr(c.SubscribeKey), nil),
TimestampCreated: c.TimestampCreated.Format(time.RFC3339Nano),
TimestampLastSent: timeOptFmt(c.TimestampLastSent, time.RFC3339Nano),
MessagesSent: c.MessagesSent,
}
type ChannelWithSubscription struct {
Channel
Subscription *Subscription `db:"sub" json:"subscription"`
}
type ChannelPreview struct {
ChannelID ChannelID `json:"channel_id"`
OwnerUserID UserID `json:"owner_user_id"`
InternalName string `json:"internal_name"`
DisplayName string `json:"display_name"`
DescriptionName *string `json:"description_name"`
}
func (c Channel) WithSubscription(sub *Subscription) ChannelWithSubscription {
@@ -41,8 +32,8 @@ func (c Channel) WithSubscription(sub *Subscription) ChannelWithSubscription {
}
}
func (c Channel) JSONPreview() ChannelPreviewJSON {
return ChannelPreviewJSON{
func (c Channel) Preview() ChannelPreview {
return ChannelPreview{
ChannelID: c.ChannelID,
OwnerUserID: c.OwnerUserID,
InternalName: c.InternalName,
@@ -50,118 +41,3 @@ func (c Channel) JSONPreview() ChannelPreviewJSON {
DescriptionName: c.DescriptionName,
}
}
type ChannelWithSubscription struct {
Channel
Subscription *Subscription
}
func (c ChannelWithSubscription) JSON(includeChannelKey bool) ChannelWithSubscriptionJSON {
var sub *SubscriptionJSON = nil
if c.Subscription != nil {
sub = langext.Ptr(c.Subscription.JSON())
}
return ChannelWithSubscriptionJSON{
ChannelJSON: c.Channel.JSON(includeChannelKey),
Subscription: sub,
}
}
type ChannelJSON struct {
ChannelID ChannelID `json:"channel_id"`
OwnerUserID UserID `json:"owner_user_id"`
InternalName string `json:"internal_name"`
DisplayName string `json:"display_name"`
DescriptionName *string `json:"description_name"`
SubscribeKey *string `json:"subscribe_key"` // can be nil, depending on endpoint
TimestampCreated string `json:"timestamp_created"`
TimestampLastSent *string `json:"timestamp_lastsent"`
MessagesSent int `json:"messages_sent"`
}
type ChannelWithSubscriptionJSON struct {
ChannelJSON
Subscription *SubscriptionJSON `json:"subscription"`
}
type ChannelPreviewJSON struct {
ChannelID ChannelID `json:"channel_id"`
OwnerUserID UserID `json:"owner_user_id"`
InternalName string `json:"internal_name"`
DisplayName string `json:"display_name"`
DescriptionName *string `json:"description_name"`
}
type ChannelDB struct {
ChannelID ChannelID `db:"channel_id"`
OwnerUserID UserID `db:"owner_user_id"`
InternalName string `db:"internal_name"`
DisplayName string `db:"display_name"`
DescriptionName *string `db:"description_name"`
SubscribeKey string `db:"subscribe_key"`
TimestampCreated int64 `db:"timestamp_created"`
TimestampLastSent *int64 `db:"timestamp_lastsent"`
MessagesSent int `db:"messages_sent"`
}
func (c ChannelDB) Model() Channel {
return Channel{
ChannelID: c.ChannelID,
OwnerUserID: c.OwnerUserID,
InternalName: c.InternalName,
DisplayName: c.DisplayName,
DescriptionName: c.DescriptionName,
SubscribeKey: c.SubscribeKey,
TimestampCreated: timeFromMilli(c.TimestampCreated),
TimestampLastSent: timeOptFromMilli(c.TimestampLastSent),
MessagesSent: c.MessagesSent,
}
}
type ChannelWithSubscriptionDB struct {
ChannelDB
Subscription *SubscriptionDB `db:"sub"`
}
func (c ChannelWithSubscriptionDB) Model() ChannelWithSubscription {
var sub *Subscription = nil
if c.Subscription != nil {
sub = langext.Ptr(c.Subscription.Model())
}
return ChannelWithSubscription{
Channel: c.ChannelDB.Model(),
Subscription: sub,
}
}
func DecodeChannel(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Channel, error) {
data, err := sq.ScanSingle[ChannelDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return Channel{}, err
}
return data.Model(), nil
}
func DecodeChannels(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Channel, error) {
data, err := sq.ScanAll[ChannelDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return nil, err
}
return langext.ArrMap(data, func(v ChannelDB) Channel { return v.Model() }), nil
}
func DecodeChannelWithSubscription(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (ChannelWithSubscription, error) {
data, err := sq.ScanSingle[ChannelWithSubscriptionDB](ctx, q, r, sq.SModeExtended, sq.Safe, true)
if err != nil {
return ChannelWithSubscription{}, err
}
return data.Model(), nil
}
func DecodeChannelsWithSubscription(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]ChannelWithSubscription, error) {
data, err := sq.ScanAll[ChannelWithSubscriptionDB](ctx, q, r, sq.SModeExtended, sq.Safe, true)
if err != nil {
return nil, err
}
return langext.ArrMap(data, func(v ChannelWithSubscriptionDB) ChannelWithSubscription { return v.Model() }), nil
}

View File

@@ -1,13 +1,5 @@
package models
import (
"context"
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
type ClientType string //@enum:type
const (
@@ -19,76 +11,12 @@ const (
)
type Client struct {
ClientID ClientID
UserID UserID
Type ClientType
FCMToken string
TimestampCreated time.Time
AgentModel string
AgentVersion string
Name *string
}
func (c Client) JSON() ClientJSON {
return ClientJSON{
ClientID: c.ClientID,
UserID: c.UserID,
Type: c.Type,
FCMToken: c.FCMToken,
TimestampCreated: c.TimestampCreated.Format(time.RFC3339Nano),
AgentModel: c.AgentModel,
AgentVersion: c.AgentVersion,
Name: c.Name,
}
}
type ClientJSON struct {
ClientID ClientID `json:"client_id"`
UserID UserID `json:"user_id"`
Type ClientType `json:"type"`
FCMToken string `json:"fcm_token"`
TimestampCreated string `json:"timestamp_created"`
AgentModel string `json:"agent_model"`
AgentVersion string `json:"agent_version"`
Name *string `json:"name"`
}
type ClientDB struct {
ClientID ClientID `db:"client_id"`
UserID UserID `db:"user_id"`
Type ClientType `db:"type"`
FCMToken string `db:"fcm_token"`
TimestampCreated int64 `db:"timestamp_created"`
AgentModel string `db:"agent_model"`
AgentVersion string `db:"agent_version"`
Name *string `db:"name"`
}
func (c ClientDB) Model() Client {
return Client{
ClientID: c.ClientID,
UserID: c.UserID,
Type: c.Type,
FCMToken: c.FCMToken,
TimestampCreated: timeFromMilli(c.TimestampCreated),
AgentModel: c.AgentModel,
AgentVersion: c.AgentVersion,
Name: c.Name,
}
}
func DecodeClient(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Client, error) {
data, err := sq.ScanSingle[ClientDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return Client{}, err
}
return data.Model(), nil
}
func DecodeClients(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Client, error) {
data, err := sq.ScanAll[ClientDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return nil, err
}
return langext.ArrMap(data, func(v ClientDB) Client { return v.Model() }), nil
ClientID ClientID `db:"client_id" json:"client_id"`
UserID UserID `db:"user_id" json:"user_id"`
Type ClientType `db:"type" json:"type"`
FCMToken string `db:"fcm_token" json:"fcm_token"`
TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"`
AgentModel string `db:"agent_model" json:"agent_model"`
AgentVersion string `db:"agent_version" json:"agent_version"`
Name *string `db:"name" json:"name"`
}

View File

@@ -1,13 +1,5 @@
package models
import (
"context"
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
type DeliveryStatus string //@enum:type
const (
@@ -17,90 +9,18 @@ const (
)
type Delivery struct {
DeliveryID DeliveryID
MessageID MessageID
ReceiverUserID UserID
ReceiverClientID ClientID
TimestampCreated time.Time
TimestampFinalized *time.Time
Status DeliveryStatus
RetryCount int
NextDelivery *time.Time
FCMMessageID *string
}
func (d Delivery) JSON() DeliveryJSON {
return DeliveryJSON{
DeliveryID: d.DeliveryID,
MessageID: d.MessageID,
ReceiverUserID: d.ReceiverUserID,
ReceiverClientID: d.ReceiverClientID,
TimestampCreated: d.TimestampCreated.Format(time.RFC3339Nano),
TimestampFinalized: timeOptFmt(d.TimestampFinalized, time.RFC3339Nano),
Status: d.Status,
RetryCount: d.RetryCount,
NextDelivery: timeOptFmt(d.NextDelivery, time.RFC3339Nano),
FCMMessageID: d.FCMMessageID,
}
DeliveryID DeliveryID `db:"delivery_id" json:"delivery_id"`
MessageID MessageID `db:"message_id" json:"message_id"`
ReceiverUserID UserID `db:"receiver_user_id" json:"receiver_user_id"`
ReceiverClientID ClientID `db:"receiver_client_id" json:"receiver_client_id"`
TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"`
TimestampFinalized *SCNTime `db:"timestamp_finalized" json:"timestamp_finalized"`
Status DeliveryStatus `db:"status" json:"status"`
RetryCount int `db:"retry_count" json:"retry_count"`
NextDelivery *SCNTime `db:"next_delivery" json:"next_delivery"`
FCMMessageID *string `db:"fcm_message_id" json:"fcm_message_id"`
}
func (d Delivery) MaxRetryCount() int {
return 5
}
type DeliveryJSON struct {
DeliveryID DeliveryID `json:"delivery_id"`
MessageID MessageID `json:"message_id"`
ReceiverUserID UserID `json:"receiver_user_id"`
ReceiverClientID ClientID `json:"receiver_client_id"`
TimestampCreated string `json:"timestamp_created"`
TimestampFinalized *string `json:"timestamp_finalized"`
Status DeliveryStatus `json:"status"`
RetryCount int `json:"retry_count"`
NextDelivery *string `json:"next_delivery"`
FCMMessageID *string `json:"fcm_message_id"`
}
type DeliveryDB struct {
DeliveryID DeliveryID `db:"delivery_id"`
MessageID MessageID `db:"message_id"`
ReceiverUserID UserID `db:"receiver_user_id"`
ReceiverClientID ClientID `db:"receiver_client_id"`
TimestampCreated int64 `db:"timestamp_created"`
TimestampFinalized *int64 `db:"timestamp_finalized"`
Status DeliveryStatus `db:"status"`
RetryCount int `db:"retry_count"`
NextDelivery *int64 `db:"next_delivery"`
FCMMessageID *string `db:"fcm_message_id"`
}
func (d DeliveryDB) Model() Delivery {
return Delivery{
DeliveryID: d.DeliveryID,
MessageID: d.MessageID,
ReceiverUserID: d.ReceiverUserID,
ReceiverClientID: d.ReceiverClientID,
TimestampCreated: timeFromMilli(d.TimestampCreated),
TimestampFinalized: timeOptFromMilli(d.TimestampFinalized),
Status: d.Status,
RetryCount: d.RetryCount,
NextDelivery: timeOptFromMilli(d.NextDelivery),
FCMMessageID: d.FCMMessageID,
}
}
func DecodeDelivery(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Delivery, error) {
data, err := sq.ScanSingle[DeliveryDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return Delivery{}, err
}
return data.Model(), nil
}
func DecodeDeliveries(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Delivery, error) {
data, err := sq.ScanAll[DeliveryDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return nil, err
}
return langext.ArrMap(data, func(v DeliveryDB) Delivery { return v.Model() }), nil
}

View File

@@ -0,0 +1,35 @@
package models
import (
"encoding/json"
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
"time"
)
type SCNDuration time.Duration
func (t SCNDuration) MarshalToDB(v SCNDuration) (int64, error) {
return v.Duration().Milliseconds(), nil
}
func (t SCNDuration) UnmarshalToModel(v int64) (SCNDuration, error) {
return SCNDuration(timeext.FromMilliseconds(v)), nil
}
func (t SCNDuration) Duration() time.Duration {
return time.Duration(t)
}
func (t *SCNDuration) UnmarshalJSON(data []byte) error {
flt := float64(0)
if err := json.Unmarshal(data, &flt); err != nil {
return err
}
d0 := timeext.FromSeconds(flt)
*t = SCNDuration(d0)
return nil
}
func (t SCNDuration) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Duration().Seconds())
}

View File

@@ -5,7 +5,7 @@ package models
import "gogs.mikescher.com/BlackForestBytes/goext/langext"
import "gogs.mikescher.com/BlackForestBytes/goext/enums"
const ChecksumEnumGenerator = "ba14f2f5d0b0357f248dcbd12933de102c80f1e61be697a37ebb723609fc0c59" // GoExtVersion: 0.0.485
const ChecksumEnumGenerator = "8ffad0d7406eb7f17cbbfeff6fee6e6fa7156470203934ebd220c824e6e15e09" // GoExtVersion: 0.0.511
// ================================ ClientType ================================
//

View File

@@ -15,7 +15,7 @@ import "reflect"
import "regexp"
import "strings"
const ChecksumCharsetIDGenerator = "ba14f2f5d0b0357f248dcbd12933de102c80f1e61be697a37ebb723609fc0c59" // GoExtVersion: 0.0.485
const ChecksumCharsetIDGenerator = "8ffad0d7406eb7f17cbbfeff6fee6e6fa7156470203934ebd220c824e6e15e09" // GoExtVersion: 0.0.511
const idlen = 24

View File

@@ -1,12 +1,9 @@
package models
import (
"context"
"github.com/jmoiron/sqlx"
"encoding/json"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"strings"
"time"
)
type TokenPerm string //@enum:type
@@ -45,17 +42,53 @@ func ParseTokenPermissionList(input string) TokenPermissionList {
return r
}
func (e TokenPermissionList) MarshalToDB(v TokenPermissionList) (string, error) {
return v.String(), nil
}
func (e TokenPermissionList) UnmarshalToModel(v string) (TokenPermissionList, error) {
return ParseTokenPermissionList(v), nil
}
func (t TokenPermissionList) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
type ChannelIDArr []ChannelID
func (t ChannelIDArr) MarshalToDB(v ChannelIDArr) (string, error) {
return strings.Join(langext.ArrMap(v, func(v ChannelID) string { return v.String() }), ";"), nil
}
func (t ChannelIDArr) UnmarshalToModel(v string) (ChannelIDArr, error) {
channels := make([]ChannelID, 0)
if strings.TrimSpace(v) != "" {
channels = langext.ArrMap(strings.Split(v, ";"), func(v string) ChannelID { return ChannelID(v) })
}
return channels, nil
}
type KeyToken struct {
KeyTokenID KeyTokenID
Name string
TimestampCreated time.Time
TimestampLastUsed *time.Time
OwnerUserID UserID
AllChannels bool
Channels []ChannelID // can also be owned by other user (needs active subscription)
Token string
Permissions TokenPermissionList
MessagesSent int
KeyTokenID KeyTokenID `db:"keytoken_id" json:"keytoken_id"`
Name string `db:"name" json:"name"`
TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"`
TimestampLastUsed *SCNTime `db:"timestamp_lastused" json:"timestamp_lastused"`
OwnerUserID UserID `db:"owner_user_id" json:"owner_user_id"`
AllChannels bool `db:"all_channels" json:"all_channels"`
Channels ChannelIDArr `db:"channels" json:"channels"`
Token string `db:"token" json:"token" jsonfilter:"INCLUDE_TOKEN"`
Permissions TokenPermissionList `db:"permissions" json:"permissions"`
MessagesSent int `db:"messages_sent" json:"messages_sent"`
}
type KeyTokenPreview struct {
KeyTokenID KeyTokenID `json:"keytoken_id"`
Name string `json:"name"`
OwnerUserID UserID `json:"owner_user_id"`
AllChannels bool `json:"all_channels"`
Channels []ChannelID `json:"channels"`
Permissions string `json:"permissions"`
}
func (k KeyToken) IsUserRead(uid UserID) bool {
@@ -78,22 +111,8 @@ func (k KeyToken) IsChannelMessagesSend(c Channel) bool {
return (k.AllChannels == true || langext.InArray(c.ChannelID, k.Channels)) && k.OwnerUserID == c.OwnerUserID && k.Permissions.Any(PermAdmin, PermChannelSend)
}
func (k KeyToken) JSON() KeyTokenJSON {
return KeyTokenJSON{
KeyTokenID: k.KeyTokenID,
Name: k.Name,
TimestampCreated: k.TimestampCreated,
TimestampLastUsed: k.TimestampLastUsed,
OwnerUserID: k.OwnerUserID,
AllChannels: k.AllChannels,
Channels: k.Channels,
Permissions: k.Permissions.String(),
MessagesSent: k.MessagesSent,
}
}
func (k KeyToken) JSONPreview() KeyTokenPreviewJSON {
return KeyTokenPreviewJSON{
func (k KeyToken) Preview() KeyTokenPreview {
return KeyTokenPreview{
KeyTokenID: k.KeyTokenID,
Name: k.Name,
OwnerUserID: k.OwnerUserID,
@@ -102,86 +121,3 @@ func (k KeyToken) JSONPreview() KeyTokenPreviewJSON {
Permissions: k.Permissions.String(),
}
}
type KeyTokenJSON struct {
KeyTokenID KeyTokenID `json:"keytoken_id"`
Name string `json:"name"`
TimestampCreated time.Time `json:"timestamp_created"`
TimestampLastUsed *time.Time `json:"timestamp_lastused"`
OwnerUserID UserID `json:"owner_user_id"`
AllChannels bool `json:"all_channels"`
Channels []ChannelID `json:"channels"`
Permissions string `json:"permissions"`
MessagesSent int `json:"messages_sent"`
}
type KeyTokenWithTokenJSON struct {
KeyTokenJSON
Token string `json:"token"`
}
type KeyTokenPreviewJSON struct {
KeyTokenID KeyTokenID `json:"keytoken_id"`
Name string `json:"name"`
OwnerUserID UserID `json:"owner_user_id"`
AllChannels bool `json:"all_channels"`
Channels []ChannelID `json:"channels"`
Permissions string `json:"permissions"`
}
func (j KeyTokenJSON) WithToken(tok string) KeyTokenWithTokenJSON {
return KeyTokenWithTokenJSON{
KeyTokenJSON: j,
Token: tok,
}
}
type KeyTokenDB struct {
KeyTokenID KeyTokenID `db:"keytoken_id"`
Name string `db:"name"`
TimestampCreated int64 `db:"timestamp_created"`
TimestampLastUsed *int64 `db:"timestamp_lastused"`
OwnerUserID UserID `db:"owner_user_id"`
AllChannels bool `db:"all_channels"`
Channels string `db:"channels"`
Token string `db:"token"`
Permissions string `db:"permissions"`
MessagesSent int `db:"messages_sent"`
}
func (k KeyTokenDB) Model() KeyToken {
channels := make([]ChannelID, 0)
if strings.TrimSpace(k.Channels) != "" {
channels = langext.ArrMap(strings.Split(k.Channels, ";"), func(v string) ChannelID { return ChannelID(v) })
}
return KeyToken{
KeyTokenID: k.KeyTokenID,
Name: k.Name,
TimestampCreated: timeFromMilli(k.TimestampCreated),
TimestampLastUsed: timeOptFromMilli(k.TimestampLastUsed),
OwnerUserID: k.OwnerUserID,
AllChannels: k.AllChannels,
Channels: channels,
Token: k.Token,
Permissions: ParseTokenPermissionList(k.Permissions),
MessagesSent: k.MessagesSent,
}
}
func DecodeKeyToken(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (KeyToken, error) {
data, err := sq.ScanSingle[KeyTokenDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return KeyToken{}, err
}
return data.Model(), nil
}
func DecodeKeyTokens(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]KeyToken, error) {
data, err := sq.ScanAll[KeyTokenDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return nil, err
}
return langext.ArrMap(data, func(v KeyTokenDB) KeyToken { return v.Model() }), nil
}

View File

@@ -1,11 +1,8 @@
package models
import (
"context"
"fmt"
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
@@ -15,60 +12,45 @@ const (
)
type Message struct {
MessageID MessageID
SenderUserID UserID // user that sent the message (this is also the owner of the channel that contains it)
ChannelInternalName string
ChannelID ChannelID
SenderName *string
SenderIP string
TimestampReal time.Time
TimestampClient *time.Time
Title string
Content *string
Priority int
UserMessageID *string
UsedKeyID KeyTokenID
Deleted bool
MessageID MessageID `db:"message_id" json:"message_id"`
SenderUserID UserID `db:"sender_user_id" json:"sender_user_id"` // user that sent the message (this is also the owner of the channel that contains it)
ChannelInternalName string `db:"channel_internal_name" json:"channel_internal_name"`
ChannelID ChannelID `db:"channel_id" json:"channel_id"`
SenderName *string `db:"sender_name" json:"sender_name"`
SenderIP string `db:"sender_ip" json:"sender_ip"`
TimestampReal SCNTime `db:"timestamp_real" json:"-"`
TimestampClient *SCNTime `db:"timestamp_client" json:"-"`
Title string `db:"title" json:"title"`
Content *string `db:"content" json:"content"`
Priority int `db:"priority" json:"priority"`
UserMessageID *string `db:"usr_message_id" json:"usr_message_id"`
UsedKeyID KeyTokenID `db:"used_key_id" json:"used_key_id"`
Deleted bool `db:"deleted" json:"-"`
MessageExtra `db:"-"` // fields that are not in DB and are set on PreMarshal
}
func (m Message) FullJSON() MessageJSON {
return MessageJSON{
MessageID: m.MessageID,
SenderUserID: m.SenderUserID,
ChannelInternalName: m.ChannelInternalName,
ChannelID: m.ChannelID,
SenderName: m.SenderName,
SenderIP: m.SenderIP,
Timestamp: m.Timestamp().Format(time.RFC3339Nano),
Title: m.Title,
Content: m.Content,
Priority: m.Priority,
UserMessageID: m.UserMessageID,
UsedKeyID: m.UsedKeyID,
Trimmed: false,
}
type MessageExtra struct {
Timestamp SCNTime `db:"-" json:"timestamp"`
Trimmed bool `db:"-" json:"trimmed"`
}
func (m Message) TrimmedJSON() MessageJSON {
return MessageJSON{
MessageID: m.MessageID,
SenderUserID: m.SenderUserID,
ChannelInternalName: m.ChannelInternalName,
ChannelID: m.ChannelID,
SenderName: m.SenderName,
SenderIP: m.SenderIP,
Timestamp: m.Timestamp().Format(time.RFC3339Nano),
Title: m.Title,
Content: m.TrimmedContent(),
Priority: m.Priority,
UserMessageID: m.UserMessageID,
UsedKeyID: m.UsedKeyID,
Trimmed: m.NeedsTrim(),
func (u *Message) PreMarshal() Message {
u.MessageExtra.Timestamp = NewSCNTime(u.Timestamp())
return *u
}
func (m Message) Trim() Message {
r := m
if !r.Trimmed && r.NeedsTrim() {
r.Content = r.TrimmedContent()
r.MessageExtra.Trimmed = true
}
return r.PreMarshal()
}
func (m Message) Timestamp() time.Time {
return langext.Coalesce(m.TimestampClient, m.TimestampReal)
return langext.Coalesce(m.TimestampClient, m.TimestampReal).Time()
}
func (m Message) NeedsTrim() bool {
@@ -102,71 +84,3 @@ func (m Message) FormatNotificationTitle(user User, channel Channel) string {
return fmt.Sprintf("[%s] %s", channel.DisplayName, m.Title)
}
type MessageJSON struct {
MessageID MessageID `json:"message_id"`
SenderUserID UserID `json:"sender_user_id"`
ChannelInternalName string `json:"channel_internal_name"`
ChannelID ChannelID `json:"channel_id"`
SenderName *string `json:"sender_name"`
SenderIP string `json:"sender_ip"`
Timestamp string `json:"timestamp"`
Title string `json:"title"`
Content *string `json:"content"`
Priority int `json:"priority"`
UserMessageID *string `json:"usr_message_id"`
UsedKeyID KeyTokenID `json:"used_key_id"`
Trimmed bool `json:"trimmed"`
}
type MessageDB struct {
MessageID MessageID `db:"message_id"`
SenderUserID UserID `db:"sender_user_id"`
ChannelInternalName string `db:"channel_internal_name"`
ChannelID ChannelID `db:"channel_id"`
SenderName *string `db:"sender_name"`
SenderIP string `db:"sender_ip"`
TimestampReal int64 `db:"timestamp_real"`
TimestampClient *int64 `db:"timestamp_client"`
Title string `db:"title"`
Content *string `db:"content"`
Priority int `db:"priority"`
UserMessageID *string `db:"usr_message_id"`
UsedKeyID KeyTokenID `db:"used_key_id"`
Deleted int `db:"deleted"`
}
func (m MessageDB) Model() Message {
return Message{
MessageID: m.MessageID,
SenderUserID: m.SenderUserID,
ChannelInternalName: m.ChannelInternalName,
ChannelID: m.ChannelID,
SenderName: m.SenderName,
SenderIP: m.SenderIP,
TimestampReal: timeFromMilli(m.TimestampReal),
TimestampClient: timeOptFromMilli(m.TimestampClient),
Title: m.Title,
Content: m.Content,
Priority: m.Priority,
UserMessageID: m.UserMessageID,
UsedKeyID: m.UsedKeyID,
Deleted: m.Deleted != 0,
}
}
func DecodeMessage(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Message, error) {
data, err := sq.ScanSingle[MessageDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return Message{}, err
}
return data.Model(), nil
}
func DecodeMessages(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Message, error) {
data, err := sq.ScanAll[MessageDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return nil, err
}
return langext.ArrMap(data, func(v MessageDB) Message { return v.Model() }), nil
}

View File

@@ -1,188 +1,27 @@
package models
import (
"context"
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
"time"
)
type RequestLog struct {
RequestID RequestID
Method string
URI string
UserAgent *string
Authentication *string
RequestBody *string
RequestBodySize int64
RequestContentType string
RemoteIP string
KeyID *KeyTokenID
UserID *UserID
Permissions *string
ResponseStatuscode *int64
ResponseBodySize *int64
ResponseBody *string
ResponseContentType string
RetryCount int64
Panicked bool
PanicStr *string
ProcessingTime time.Duration
TimestampCreated time.Time
TimestampStart time.Time
TimestampFinish time.Time
}
func (c RequestLog) JSON() RequestLogJSON {
return RequestLogJSON{
RequestID: c.RequestID,
Method: c.Method,
URI: c.URI,
UserAgent: c.UserAgent,
Authentication: c.Authentication,
RequestBody: c.RequestBody,
RequestBodySize: c.RequestBodySize,
RequestContentType: c.RequestContentType,
RemoteIP: c.RemoteIP,
KeyID: c.KeyID,
UserID: c.UserID,
Permissions: c.Permissions,
ResponseStatuscode: c.ResponseStatuscode,
ResponseBodySize: c.ResponseBodySize,
ResponseBody: c.ResponseBody,
ResponseContentType: c.ResponseContentType,
RetryCount: c.RetryCount,
Panicked: c.Panicked,
PanicStr: c.PanicStr,
ProcessingTime: c.ProcessingTime.Seconds(),
TimestampCreated: c.TimestampCreated.Format(time.RFC3339Nano),
TimestampStart: c.TimestampStart.Format(time.RFC3339Nano),
TimestampFinish: c.TimestampFinish.Format(time.RFC3339Nano),
}
}
func (c RequestLog) DB() RequestLogDB {
return RequestLogDB{
RequestID: c.RequestID,
Method: c.Method,
URI: c.URI,
UserAgent: c.UserAgent,
Authentication: c.Authentication,
RequestBody: c.RequestBody,
RequestBodySize: c.RequestBodySize,
RequestContentType: c.RequestContentType,
RemoteIP: c.RemoteIP,
KeyID: c.KeyID,
UserID: c.UserID,
Permissions: c.Permissions,
ResponseStatuscode: c.ResponseStatuscode,
ResponseBodySize: c.ResponseBodySize,
ResponseBody: c.ResponseBody,
ResponseContentType: c.ResponseContentType,
RetryCount: c.RetryCount,
Panicked: langext.Conditional[int64](c.Panicked, 1, 0),
PanicStr: c.PanicStr,
ProcessingTime: c.ProcessingTime.Milliseconds(),
TimestampCreated: c.TimestampCreated.UnixMilli(),
TimestampStart: c.TimestampStart.UnixMilli(),
TimestampFinish: c.TimestampFinish.UnixMilli(),
}
}
type RequestLogJSON struct {
RequestID RequestID `json:"requestLog_id"`
Method string `json:"method"`
URI string `json:"uri"`
UserAgent *string `json:"user_agent"`
Authentication *string `json:"authentication"`
RequestBody *string `json:"request_body"`
RequestBodySize int64 `json:"request_body_size"`
RequestContentType string `json:"request_content_type"`
RemoteIP string `json:"remote_ip"`
KeyID *KeyTokenID `json:"key_id"`
UserID *UserID `json:"userid"`
Permissions *string `json:"permissions"`
ResponseStatuscode *int64 `json:"response_statuscode"`
ResponseBodySize *int64 `json:"response_body_size"`
ResponseBody *string `json:"response_body"`
ResponseContentType string `json:"response_content_type"`
RetryCount int64 `json:"retry_count"`
Panicked bool `json:"panicked"`
PanicStr *string `json:"panic_str"`
ProcessingTime float64 `json:"processing_time"`
TimestampCreated string `json:"timestamp_created"`
TimestampStart string `json:"timestamp_start"`
TimestampFinish string `json:"timestamp_finish"`
}
type RequestLogDB struct {
RequestID RequestID `db:"request_id"`
Method string `db:"method"`
URI string `db:"uri"`
UserAgent *string `db:"user_agent"`
Authentication *string `db:"authentication"`
RequestBody *string `db:"request_body"`
RequestBodySize int64 `db:"request_body_size"`
RequestContentType string `db:"request_content_type"`
RemoteIP string `db:"remote_ip"`
KeyID *KeyTokenID `db:"key_id"`
UserID *UserID `db:"userid"`
Permissions *string `db:"permissions"`
ResponseStatuscode *int64 `db:"response_statuscode"`
ResponseBodySize *int64 `db:"response_body_size"`
ResponseBody *string `db:"response_body"`
ResponseContentType string `db:"response_content_type"`
RetryCount int64 `db:"retry_count"`
Panicked int64 `db:"panicked"`
PanicStr *string `db:"panic_str"`
ProcessingTime int64 `db:"processing_time"`
TimestampCreated int64 `db:"timestamp_created"`
TimestampStart int64 `db:"timestamp_start"`
TimestampFinish int64 `db:"timestamp_finish"`
}
func (c RequestLogDB) Model() RequestLog {
return RequestLog{
RequestID: c.RequestID,
Method: c.Method,
URI: c.URI,
UserAgent: c.UserAgent,
Authentication: c.Authentication,
RequestBody: c.RequestBody,
RequestBodySize: c.RequestBodySize,
RequestContentType: c.RequestContentType,
RemoteIP: c.RemoteIP,
KeyID: c.KeyID,
UserID: c.UserID,
Permissions: c.Permissions,
ResponseStatuscode: c.ResponseStatuscode,
ResponseBodySize: c.ResponseBodySize,
ResponseBody: c.ResponseBody,
ResponseContentType: c.ResponseContentType,
RetryCount: c.RetryCount,
Panicked: c.Panicked != 0,
PanicStr: c.PanicStr,
ProcessingTime: timeext.FromMilliseconds(c.ProcessingTime),
TimestampCreated: timeFromMilli(c.TimestampCreated),
TimestampStart: timeFromMilli(c.TimestampStart),
TimestampFinish: timeFromMilli(c.TimestampFinish),
}
}
func DecodeRequestLog(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (RequestLog, error) {
data, err := sq.ScanSingle[RequestLogDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return RequestLog{}, err
}
return data.Model(), nil
}
func DecodeRequestLogs(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]RequestLog, error) {
data, err := sq.ScanAll[RequestLogDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return nil, err
}
return langext.ArrMap(data, func(v RequestLogDB) RequestLog { return v.Model() }), nil
RequestID RequestID `db:"request_id" json:"requestLog_id"`
Method string `db:"method" json:"method"`
URI string `db:"uri" json:"uri"`
UserAgent *string `db:"user_agent" json:"user_agent"`
Authentication *string `db:"authentication" json:"authentication"`
RequestBody *string `db:"request_body" json:"request_body"`
RequestBodySize int64 `db:"request_body_size" json:"request_body_size"`
RequestContentType string `db:"request_content_type" json:"request_content_type"`
RemoteIP string `db:"remote_ip" json:"remote_ip"`
KeyID *KeyTokenID `db:"key_id" json:"key_id"`
UserID *UserID `db:"userid" json:"userid"`
Permissions *string `db:"permissions" json:"permissions"`
ResponseStatuscode *int64 `db:"response_statuscode" json:"response_statuscode"`
ResponseBodySize *int64 `db:"response_body_size" json:"response_body_size"`
ResponseBody *string `db:"response_body" json:"response_body"`
ResponseContentType string `db:"response_content_type" json:"response_content_type"`
RetryCount int64 `db:"retry_count" json:"retry_count"`
Panicked bool `db:"panicked" json:"panicked"`
PanicStr *string `db:"panic_str" json:"panic_str"`
ProcessingTime SCNDuration `db:"processing_time" json:"processing_time"`
TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"`
TimestampStart SCNTime `db:"timestamp_start" json:"timestamp_start"`
TimestampFinish SCNTime `db:"timestamp_finish" json:"timestamp_finish"`
}

View File

@@ -1,13 +1,5 @@
package models
import (
"context"
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
// [!] subscriptions are read-access to channels,
//
// The set of subscriptions specifies which messages the ListMessages() API call returns
@@ -16,71 +8,11 @@ import (
// (use keytokens for write-access)
type Subscription struct {
SubscriptionID SubscriptionID
SubscriberUserID UserID
ChannelOwnerUserID UserID
ChannelID ChannelID
ChannelInternalName string
TimestampCreated time.Time
Confirmed bool
}
func (s Subscription) JSON() SubscriptionJSON {
return SubscriptionJSON{
SubscriptionID: s.SubscriptionID,
SubscriberUserID: s.SubscriberUserID,
ChannelOwnerUserID: s.ChannelOwnerUserID,
ChannelID: s.ChannelID,
ChannelInternalName: s.ChannelInternalName,
TimestampCreated: s.TimestampCreated.Format(time.RFC3339Nano),
Confirmed: s.Confirmed,
}
}
type SubscriptionJSON struct {
SubscriptionID SubscriptionID `json:"subscription_id"`
SubscriberUserID UserID `json:"subscriber_user_id"`
ChannelOwnerUserID UserID `json:"channel_owner_user_id"`
ChannelID ChannelID `json:"channel_id"`
ChannelInternalName string `json:"channel_internal_name"`
TimestampCreated string `json:"timestamp_created"`
Confirmed bool `json:"confirmed"`
}
type SubscriptionDB struct {
SubscriptionID SubscriptionID `db:"subscription_id"`
SubscriberUserID UserID `db:"subscriber_user_id"`
ChannelOwnerUserID UserID `db:"channel_owner_user_id"`
ChannelID ChannelID `db:"channel_id"`
ChannelInternalName string `db:"channel_internal_name"`
TimestampCreated int64 `db:"timestamp_created"`
Confirmed int `db:"confirmed"`
}
func (s SubscriptionDB) Model() Subscription {
return Subscription{
SubscriptionID: s.SubscriptionID,
SubscriberUserID: s.SubscriberUserID,
ChannelOwnerUserID: s.ChannelOwnerUserID,
ChannelID: s.ChannelID,
ChannelInternalName: s.ChannelInternalName,
TimestampCreated: timeFromMilli(s.TimestampCreated),
Confirmed: s.Confirmed != 0,
}
}
func DecodeSubscription(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Subscription, error) {
data, err := sq.ScanSingle[SubscriptionDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return Subscription{}, err
}
return data.Model(), nil
}
func DecodeSubscriptions(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Subscription, error) {
data, err := sq.ScanAll[SubscriptionDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
if err != nil {
return nil, err
}
return langext.ArrMap(data, func(v SubscriptionDB) Subscription { return v.Model() }), nil
SubscriptionID SubscriptionID `db:"subscription_id" json:"subscription_id"`
SubscriberUserID UserID `db:"subscriber_user_id" json:"subscriber_user_id"`
ChannelOwnerUserID UserID `db:"channel_owner_user_id" json:"channel_owner_user_id"`
ChannelID ChannelID `db:"channel_id" json:"channel_id"`
ChannelInternalName string `db:"channel_internal_name" json:"channel_internal_name"`
TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"`
Confirmed bool `db:"confirmed" json:"confirmed"`
}

65
scnserver/models/time.go Normal file
View File

@@ -0,0 +1,65 @@
package models
import (
"encoding/json"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/rfctime"
"time"
)
type SCNTime time.Time
func (t SCNTime) MarshalToDB(v SCNTime) (int64, error) {
return v.Time().UnixMilli(), nil
}
func (t SCNTime) UnmarshalToModel(v int64) (SCNTime, error) {
return NewSCNTime(time.UnixMilli(v)), nil
}
func (t SCNTime) Time() time.Time {
return time.Time(t)
}
func (t *SCNTime) UnmarshalJSON(data []byte) error {
str := ""
if err := json.Unmarshal(data, &str); err != nil {
return err
}
t0, err := time.Parse(time.RFC3339Nano, str)
if err != nil {
return err
}
*t = SCNTime(t0)
return nil
}
func (t SCNTime) MarshalJSON() ([]byte, error) {
str := t.Time().Format(time.RFC3339Nano)
return json.Marshal(str)
}
func NewSCNTime(t time.Time) SCNTime {
return SCNTime(t)
}
func NewSCNTimePtr(t *time.Time) *SCNTime {
if t == nil {
return nil
}
return langext.Ptr(SCNTime(*t))
}
func NowSCNTime() SCNTime {
return SCNTime(time.Now())
}
func tt(v rfctime.AnyTime) time.Time {
if r, ok := v.(time.Time); ok {
return r
}
if r, ok := v.(rfctime.RFCTime); ok {
return r.Time()
}
return time.Unix(0, v.UnixNano()).In(v.Location())
}

View File

@@ -2,38 +2,63 @@ package models
import (
scn "blackforestbytes.com/simplecloudnotifier"
"context"
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
type User struct {
UserID UserID
Username *string
TimestampCreated time.Time
TimestampLastRead *time.Time
TimestampLastSent *time.Time
MessagesSent int
QuotaUsed int
QuotaUsedDay *string
IsPro bool
ProToken *string
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
}
func (u User) JSON() UserJSON {
return UserJSON{
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(),
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{
QuotaPerDay: u.QuotaPerDay(),
QuotaRemaining: u.QuotaRemainingToday(),
IsPro: u.IsPro,
DefaultChannel: u.DefaultChannel(),
MaxBodySize: u.MaxContentLength(),
MaxTitleLength: u.MaxTitleLength(),
@@ -43,16 +68,7 @@ func (u User) JSON() UserJSON {
MaxSenderNameLength: u.MaxSenderNameLength(),
MaxUserMessageIDLength: u.MaxUserMessageIDLength(),
}
}
func (u User) JSONWithClients(clients []Client, ak string, sk string, rk string) UserJSONWithClientsAndKeys {
return UserJSONWithClientsAndKeys{
UserJSON: u.JSON(),
Clients: langext.ArrMap(clients, func(v Client) ClientJSON { return v.JSON() }),
SendKey: sk,
ReadKey: rk,
AdminKey: ak,
}
return *u
}
func (u User) MaxContentLength() int {
@@ -116,86 +132,9 @@ func (u User) MaxTimestampDiffHours() int {
return 24
}
func (u User) JSONPreview() UserPreviewJSON {
return UserPreviewJSON{
func (u User) JSONPreview() UserPreview {
return UserPreview{
UserID: u.UserID,
Username: u.Username,
}
}
type UserJSON struct {
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"`
}
type UserPreviewJSON struct {
UserID UserID `json:"user_id"`
Username *string `json:"username"`
}
type UserJSONWithClientsAndKeys struct {
UserJSON
Clients []ClientJSON `json:"clients"`
SendKey string `json:"send_key"`
ReadKey string `json:"read_key"`
AdminKey string `json:"admin_key"`
}
type UserDB struct {
UserID UserID `db:"user_id"`
Username *string `db:"username"`
TimestampCreated int64 `db:"timestamp_created"`
TimestampLastRead *int64 `db:"timestamp_lastread"`
TimestampLastSent *int64 `db:"timestamp_lastsent"`
MessagesSent int `db:"messages_sent"`
QuotaUsed int `db:"quota_used"`
QuotaUsedDay *string `db:"quota_used_day"`
IsPro bool `db:"is_pro"`
ProToken *string `db:"pro_token"`
}
func (u UserDB) Model() User {
return User{
UserID: u.UserID,
Username: u.Username,
TimestampCreated: timeFromMilli(u.TimestampCreated),
TimestampLastRead: timeOptFromMilli(u.TimestampLastRead),
TimestampLastSent: timeOptFromMilli(u.TimestampLastSent),
MessagesSent: u.MessagesSent,
QuotaUsed: u.QuotaUsed,
QuotaUsedDay: u.QuotaUsedDay,
IsPro: u.IsPro,
}
}
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)
if err != nil {
return User{}, err
}
return data.Model(), nil
}
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)
if err != nil {
return nil, err
}
return langext.ArrMap(data, func(v UserDB) User { return v.Model() }), nil
}

View File

@@ -2,6 +2,7 @@ package models
import (
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/sq"
"time"
)
@@ -23,3 +24,10 @@ func timeOptFromMilli(millis *int64) *time.Time {
func timeFromMilli(millis int64) time.Time {
return time.UnixMilli(millis)
}
func RegisterConverter(db sq.DB) {
db.RegisterConverter(sq.NewAutoDBTypeConverter(SCNTime{}))
db.RegisterConverter(sq.NewAutoDBTypeConverter(SCNDuration(0)))
db.RegisterConverter(sq.NewAutoDBTypeConverter(TokenPermissionList{}))
db.RegisterConverter(sq.NewAutoDBTypeConverter(ChannelIDArr{}))
}