2022-11-18 23:12:37 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2022-12-07 22:11:44 +01:00
|
|
|
"github.com/jmoiron/sqlx"
|
2022-11-19 12:47:23 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
2022-11-18 23:12:37 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Channel struct {
|
2022-11-20 22:18:24 +01:00
|
|
|
ChannelID ChannelID
|
|
|
|
OwnerUserID UserID
|
2022-11-18 23:12:37 +01:00
|
|
|
Name string
|
|
|
|
SubscribeKey string
|
|
|
|
SendKey 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,
|
|
|
|
Name: c.Name,
|
2022-11-20 21:15:06 +01:00
|
|
|
SubscribeKey: langext.Conditional(includeKey, langext.Ptr(c.SubscribeKey), nil),
|
|
|
|
SendKey: langext.Conditional(includeKey, langext.Ptr(c.SendKey), nil),
|
2022-11-18 23:12:37 +01:00
|
|
|
TimestampCreated: c.TimestampCreated.Format(time.RFC3339Nano),
|
|
|
|
TimestampLastSent: timeOptFmt(c.TimestampLastSent, time.RFC3339Nano),
|
|
|
|
MessagesSent: c.MessagesSent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChannelJSON struct {
|
2022-11-20 22:18:24 +01:00
|
|
|
ChannelID ChannelID `json:"channel_id"`
|
|
|
|
OwnerUserID UserID `json:"owner_user_id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
SubscribeKey *string `json:"subscribe_key"` // can be nil, depending on endpoint
|
|
|
|
SendKey *string `json:"send_key"` // can be nil, depending on endpoint
|
|
|
|
TimestampCreated string `json:"timestamp_created"`
|
2022-12-07 22:11:44 +01:00
|
|
|
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 ChannelDB struct {
|
2022-11-20 22:18:24 +01:00
|
|
|
ChannelID ChannelID `db:"channel_id"`
|
|
|
|
OwnerUserID UserID `db:"owner_user_id"`
|
|
|
|
Name string `db:"name"`
|
|
|
|
SubscribeKey string `db:"subscribe_key"`
|
|
|
|
SendKey string `db:"send_key"`
|
|
|
|
TimestampCreated int64 `db:"timestamp_created"`
|
2022-12-07 22:11:44 +01:00
|
|
|
TimestampLastRead *int64 `db:"timestamp_lastread"`
|
|
|
|
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,
|
|
|
|
Name: c.Name,
|
|
|
|
SubscribeKey: c.SubscribeKey,
|
|
|
|
SendKey: c.SendKey,
|
|
|
|
TimestampCreated: time.UnixMilli(c.TimestampCreated),
|
|
|
|
TimestampLastSent: timeOptFromMilli(c.TimestampLastSent),
|
|
|
|
MessagesSent: c.MessagesSent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
func DecodeChannel(r *sqlx.Rows) (Channel, error) {
|
|
|
|
data, err := scanSingle[ChannelDB](r)
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
func DecodeChannels(r *sqlx.Rows) ([]Channel, error) {
|
|
|
|
data, err := scanAll[ChannelDB](r)
|
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
|
|
|
}
|