SimpleCloudNotifier/scnserver/models/channel.go

168 lines
5.1 KiB
Go
Raw Normal View History

2022-11-18 23:12:37 +01:00
package models
import (
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"
)
type Channel struct {
2022-11-20 22:18:24 +01:00
ChannelID ChannelID
OwnerUserID UserID
InternalName string
DisplayName string
DescriptionName *string
2022-11-18 23:12:37 +01:00
SubscribeKey string
TimestampCreated time.Time
TimestampLastSent *time.Time
MessagesSent int
}
2022-11-20 21:15:06 +01:00
func (c Channel) JSON(includeKey bool) ChannelJSON {
2022-11-18 23:12:37 +01:00
return ChannelJSON{
ChannelID: c.ChannelID,
OwnerUserID: c.OwnerUserID,
InternalName: c.InternalName,
DisplayName: c.DisplayName,
DescriptionName: c.DescriptionName,
2022-11-20 21:15:06 +01:00
SubscribeKey: langext.Conditional(includeKey, langext.Ptr(c.SubscribeKey), nil),
2022-11-18 23:12:37 +01:00
TimestampCreated: c.TimestampCreated.Format(time.RFC3339Nano),
TimestampLastSent: timeOptFmt(c.TimestampLastSent, time.RFC3339Nano),
MessagesSent: c.MessagesSent,
}
}
func (c Channel) WithSubscription(sub *Subscription) ChannelWithSubscription {
return ChannelWithSubscription{
Channel: c,
Subscription: sub,
}
}
2024-06-12 00:35:06 +02:00
func (c Channel) JSONPreview() ChannelPreviewJSON {
return ChannelPreviewJSON{
ChannelID: c.ChannelID,
OwnerUserID: c.OwnerUserID,
InternalName: c.InternalName,
DisplayName: c.DisplayName,
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,
}
}
2022-11-18 23:12:37 +01:00
type ChannelJSON struct {
2022-11-20 22:18:24 +01:00
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"`
2022-11-20 22:18:24 +01:00
SubscribeKey *string `json:"subscribe_key"` // can be nil, depending on endpoint
TimestampCreated string `json:"timestamp_created"`
TimestampLastSent *string `json:"timestamp_lastsent"`
2022-11-20 22:18:24 +01:00
MessagesSent int `json:"messages_sent"`
2022-11-18 23:12:37 +01:00
}
type ChannelWithSubscriptionJSON struct {
ChannelJSON
Subscription *SubscriptionJSON `json:"subscription"`
}
2024-06-12 00:35:06 +02:00
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"`
}
2022-11-18 23:12:37 +01:00
type ChannelDB struct {
2022-11-20 22:18:24 +01:00
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"`
2022-11-20 22:18:24 +01:00
SubscribeKey string `db:"subscribe_key"`
TimestampCreated int64 `db:"timestamp_created"`
TimestampLastSent *int64 `db:"timestamp_lastsent"`
2022-11-20 22:18:24 +01:00
MessagesSent int `db:"messages_sent"`
2022-11-18 23:12:37 +01:00
}
func (c ChannelDB) Model() Channel {
return Channel{
ChannelID: c.ChannelID,
OwnerUserID: c.OwnerUserID,
InternalName: c.InternalName,
DisplayName: c.DisplayName,
DescriptionName: c.DescriptionName,
2022-11-18 23:12:37 +01:00
SubscribeKey: c.SubscribeKey,
2023-04-21 21:45:16 +02:00
TimestampCreated: timeFromMilli(c.TimestampCreated),
2022-11-18 23:12:37 +01:00
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,
}
}
2024-05-31 23:56:16 +02:00
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)
2022-11-18 23:12:37 +01:00
if err != nil {
return Channel{}, err
}
2022-11-19 12:47:23 +01:00
return data.Model(), nil
}
2024-05-31 23:56:16 +02:00
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)
2022-11-19 12:47:23 +01:00
if err != nil {
return nil, err
}
return langext.ArrMap(data, func(v ChannelDB) Channel { return v.Model() }), nil
2022-11-18 23:12:37 +01:00
}
2024-05-31 23:56:16 +02:00
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
}
2024-05-31 23:56:16 +02:00
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
}