SimpleCloudNotifier/scnserver/models/delivery.go

107 lines
3.4 KiB
Go
Raw Normal View History

2022-11-19 15:13:47 +01:00
package models
import (
2024-05-31 23:56:16 +02:00
"context"
"github.com/jmoiron/sqlx"
2022-11-19 15:13:47 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/langext"
2022-12-11 03:14:42 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/sq"
2022-11-19 15:13:47 +01:00
"time"
)
2023-04-21 21:45:16 +02:00
type DeliveryStatus string //@enum:type
2022-11-19 15:13:47 +01:00
const (
DeliveryStatusRetry DeliveryStatus = "RETRY"
DeliveryStatusSuccess DeliveryStatus = "SUCCESS"
DeliveryStatusFailed DeliveryStatus = "FAILED"
)
type Delivery struct {
2022-11-20 22:18:24 +01:00
DeliveryID DeliveryID
MessageID MessageID
2022-11-20 22:18:24 +01:00
ReceiverUserID UserID
ReceiverClientID ClientID
2022-11-19 15:13:47 +01:00
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,
2022-11-19 15:13:47 +01:00
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,
}
}
2022-11-20 15:40:19 +01:00
func (d Delivery) MaxRetryCount() int {
return 5
}
2022-11-19 15:13:47 +01:00
type DeliveryJSON struct {
2022-11-20 22:18:24 +01:00
DeliveryID DeliveryID `json:"delivery_id"`
MessageID MessageID `json:"message_id"`
2022-11-20 22:18:24 +01:00
ReceiverUserID UserID `json:"receiver_user_id"`
ReceiverClientID ClientID `json:"receiver_client_id"`
2022-11-19 15:13:47 +01:00
TimestampCreated string `json:"timestamp_created"`
TimestampFinalized *string `json:"timestamp_finalized"`
2022-11-19 15:13:47 +01:00
Status DeliveryStatus `json:"status"`
RetryCount int `json:"retry_count"`
NextDelivery *string `json:"next_delivery"`
FCMMessageID *string `json:"fcm_message_id"`
}
type DeliveryDB struct {
2022-11-20 22:18:24 +01:00
DeliveryID DeliveryID `db:"delivery_id"`
MessageID MessageID `db:"message_id"`
2022-11-20 22:18:24 +01:00
ReceiverUserID UserID `db:"receiver_user_id"`
ReceiverClientID ClientID `db:"receiver_client_id"`
2022-11-19 15:13:47 +01:00
TimestampCreated int64 `db:"timestamp_created"`
TimestampFinalized *int64 `db:"timestamp_finalized"`
2022-11-19 15:13:47 +01:00
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,
2022-11-19 15:13:47 +01:00
ReceiverUserID: d.ReceiverUserID,
ReceiverClientID: d.ReceiverClientID,
2023-04-21 21:45:16 +02:00
TimestampCreated: timeFromMilli(d.TimestampCreated),
2022-11-19 15:13:47 +01:00
TimestampFinalized: timeOptFromMilli(d.TimestampFinalized),
Status: d.Status,
RetryCount: d.RetryCount,
NextDelivery: timeOptFromMilli(d.NextDelivery),
FCMMessageID: d.FCMMessageID,
}
}
2024-05-31 23:56:16 +02:00
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)
2022-11-19 15:13:47 +01:00
if err != nil {
return Delivery{}, err
}
return data.Model(), nil
}
2024-05-31 23:56:16 +02:00
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)
2022-11-19 15:13:47 +01:00
if err != nil {
return nil, err
}
return langext.ArrMap(data, func(v DeliveryDB) Delivery { return v.Model() }), nil
}