SendMessage()
This commit is contained in:
90
server/models/channel.go
Normal file
90
server/models/channel.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/blockloop/scan"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Channel struct {
|
||||
ChannelID int64
|
||||
OwnerUserID int64
|
||||
Name string
|
||||
SubscribeKey string
|
||||
SendKey string
|
||||
TimestampCreated time.Time
|
||||
TimestampLastRead *time.Time
|
||||
TimestampLastSent *time.Time
|
||||
MessagesSent int
|
||||
}
|
||||
|
||||
func (c Channel) JSON() ChannelJSON {
|
||||
return ChannelJSON{
|
||||
ChannelID: c.ChannelID,
|
||||
OwnerUserID: c.OwnerUserID,
|
||||
Name: c.Name,
|
||||
SubscribeKey: c.SubscribeKey,
|
||||
SendKey: c.SendKey,
|
||||
TimestampCreated: c.TimestampCreated.Format(time.RFC3339Nano),
|
||||
TimestampLastRead: timeOptFmt(c.TimestampLastRead, time.RFC3339Nano),
|
||||
TimestampLastSent: timeOptFmt(c.TimestampLastSent, time.RFC3339Nano),
|
||||
MessagesSent: c.MessagesSent,
|
||||
}
|
||||
}
|
||||
|
||||
type ChannelJSON struct {
|
||||
ChannelID int64 `json:"channel_id"`
|
||||
OwnerUserID int64 `json:"owner_user_id"`
|
||||
Name string `json:"name"`
|
||||
SubscribeKey string `json:"subscribe_key"`
|
||||
SendKey string `json:"send_key"`
|
||||
TimestampCreated string `json:"timestamp_created"`
|
||||
TimestampLastRead *string `json:"timestamp_last_read"`
|
||||
TimestampLastSent *string `json:"timestamp_last_sent"`
|
||||
MessagesSent int `json:"messages_sent"`
|
||||
}
|
||||
|
||||
type ChannelDB struct {
|
||||
ChannelID int64 `db:"channel_id"`
|
||||
OwnerUserID int64 `db:"owner_user_id"`
|
||||
Name string `db:"name"`
|
||||
SubscribeKey string `db:"subscribe_key"`
|
||||
SendKey string `db:"send_key"`
|
||||
TimestampCreated int64 `db:"timestamp_created"`
|
||||
TimestampLastRead *int64 `db:"timestamp_last_read"`
|
||||
TimestampLastSent *int64 `db:"timestamp_last_sent"`
|
||||
MessagesSent int `db:"messages_sent"`
|
||||
}
|
||||
|
||||
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),
|
||||
TimestampLastRead: timeOptFromMilli(c.TimestampLastRead),
|
||||
TimestampLastSent: timeOptFromMilli(c.TimestampLastSent),
|
||||
MessagesSent: c.MessagesSent,
|
||||
}
|
||||
}
|
||||
|
||||
func DecodeChannel(r *sql.Rows) (Channel, error) {
|
||||
var data ChannelDB
|
||||
err := scan.RowStrict(&data, r)
|
||||
if err != nil {
|
||||
return Channel{}, err
|
||||
}
|
||||
return data.Model(), nil
|
||||
}
|
||||
|
||||
func DecodeChannels(r *sql.Rows) ([]Channel, error) {
|
||||
var data []ChannelDB
|
||||
err := scan.RowsStrict(&data, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return langext.ArrMap(data, func(v ChannelDB) Channel { return v.Model() }), nil
|
||||
}
|
||||
87
server/models/client.go
Normal file
87
server/models/client.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/blockloop/scan"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ClientType string
|
||||
|
||||
const (
|
||||
ClientTypeAndroid ClientType = "ANDROID"
|
||||
ClientTypeIOS ClientType = "IOS"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
ClientID int64
|
||||
UserID int64
|
||||
Type ClientType
|
||||
FCMToken *string
|
||||
TimestampCreated time.Time
|
||||
AgentModel string
|
||||
AgentVersion 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,
|
||||
}
|
||||
}
|
||||
|
||||
type ClientJSON struct {
|
||||
ClientID int64 `json:"client_id"`
|
||||
UserID int64 `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"`
|
||||
}
|
||||
|
||||
type ClientDB struct {
|
||||
ClientID int64 `db:"client_id"`
|
||||
UserID int64 `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"`
|
||||
}
|
||||
|
||||
func (c ClientDB) Model() Client {
|
||||
return Client{
|
||||
ClientID: c.ClientID,
|
||||
UserID: c.UserID,
|
||||
Type: c.Type,
|
||||
FCMToken: c.FCMToken,
|
||||
TimestampCreated: time.UnixMilli(c.TimestampCreated),
|
||||
AgentModel: c.AgentModel,
|
||||
AgentVersion: c.AgentVersion,
|
||||
}
|
||||
}
|
||||
|
||||
func DecodeClient(r *sql.Rows) (Client, error) {
|
||||
var data ClientDB
|
||||
err := scan.RowStrict(&data, r)
|
||||
if err != nil {
|
||||
return Client{}, err
|
||||
}
|
||||
return data.Model(), nil
|
||||
}
|
||||
|
||||
func DecodeClients(r *sql.Rows) ([]Client, error) {
|
||||
var data []ClientDB
|
||||
err := scan.RowsStrict(&data, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return langext.ArrMap(data, func(v ClientDB) Client { return v.Model() }), nil
|
||||
}
|
||||
20
server/models/compat.go
Normal file
20
server/models/compat.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package models
|
||||
|
||||
type CompatMessage struct {
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Priority int `json:"priority"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
UserMessageID string `json:"usr_msg_id"`
|
||||
SCNMessageID string `json:"scn_msg_id"`
|
||||
}
|
||||
|
||||
type ShortCompatMessage struct {
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Trimmed bool `json:"trimmed"`
|
||||
Priority int `json:"priority"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
UserMessageID string `json:"usr_msg_id"`
|
||||
SCNMessageID string `json:"scn_msg_id"`
|
||||
}
|
||||
103
server/models/delivery.go
Normal file
103
server/models/delivery.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/blockloop/scan"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DeliveryStatus string
|
||||
|
||||
const (
|
||||
DeliveryStatusRetry DeliveryStatus = "RETRY"
|
||||
DeliveryStatusSuccess DeliveryStatus = "SUCCESS"
|
||||
DeliveryStatusFailed DeliveryStatus = "FAILED"
|
||||
)
|
||||
|
||||
type Delivery struct {
|
||||
DeliveryID int64
|
||||
SCNMessageID int64
|
||||
ReceiverUserID int64
|
||||
ReceiverClientID int64
|
||||
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,
|
||||
SCNMessageID: d.SCNMessageID,
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
type DeliveryJSON struct {
|
||||
DeliveryID int64 `json:"delivery_id"`
|
||||
SCNMessageID int64 `json:"scn_message_id"`
|
||||
ReceiverUserID int64 `json:"receiver_user_id"`
|
||||
ReceiverClientID int64 `json:"receiver_client_id"`
|
||||
TimestampCreated string `json:"timestamp_created"`
|
||||
TimestampFinalized *string `json:"tiestamp_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 int64 `db:"delivery_id"`
|
||||
SCNMessageID int64 `db:"scn_message_id"`
|
||||
ReceiverUserID int64 `db:"receiver_user_id"`
|
||||
ReceiverClientID int64 `db:"receiver_client_id"`
|
||||
TimestampCreated int64 `db:"timestamp_created"`
|
||||
TimestampFinalized *int64 `db:"tiestamp_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,
|
||||
SCNMessageID: d.SCNMessageID,
|
||||
ReceiverUserID: d.ReceiverUserID,
|
||||
ReceiverClientID: d.ReceiverClientID,
|
||||
TimestampCreated: time.UnixMilli(d.TimestampCreated),
|
||||
TimestampFinalized: timeOptFromMilli(d.TimestampFinalized),
|
||||
Status: d.Status,
|
||||
RetryCount: d.RetryCount,
|
||||
NextDelivery: timeOptFromMilli(d.NextDelivery),
|
||||
FCMMessageID: d.FCMMessageID,
|
||||
}
|
||||
}
|
||||
|
||||
func DecodeDelivery(r *sql.Rows) (Delivery, error) {
|
||||
var data DeliveryDB
|
||||
err := scan.RowStrict(&data, r)
|
||||
if err != nil {
|
||||
return Delivery{}, err
|
||||
}
|
||||
return data.Model(), nil
|
||||
}
|
||||
|
||||
func DecodeDeliveries(r *sql.Rows) ([]Delivery, error) {
|
||||
var data []DeliveryDB
|
||||
err := scan.RowsStrict(&data, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return langext.ArrMap(data, func(v DeliveryDB) Delivery { return v.Model() }), nil
|
||||
}
|
||||
100
server/models/message.go
Normal file
100
server/models/message.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/blockloop/scan"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
SCNMessageID int64
|
||||
SenderUserID int64
|
||||
OwnerUserID int64
|
||||
ChannelName string
|
||||
ChannelID int64
|
||||
TimestampReal time.Time
|
||||
TimestampClient *time.Time
|
||||
Title string
|
||||
Content *string
|
||||
Priority int
|
||||
UserMessageID *string
|
||||
}
|
||||
|
||||
func (m Message) JSON() MessageJSON {
|
||||
return MessageJSON{
|
||||
SCNMessageID: m.SCNMessageID,
|
||||
SenderUserID: m.SenderUserID,
|
||||
OwnerUserID: m.OwnerUserID,
|
||||
ChannelName: m.ChannelName,
|
||||
ChannelID: m.ChannelID,
|
||||
Timestamp: langext.Coalesce(m.TimestampClient, m.TimestampReal).Format(time.RFC3339Nano),
|
||||
Title: m.Title,
|
||||
Content: m.Content,
|
||||
Priority: m.Priority,
|
||||
UserMessageID: m.UserMessageID,
|
||||
Trimmed: false,
|
||||
}
|
||||
}
|
||||
|
||||
type MessageJSON struct {
|
||||
SCNMessageID int64 `json:"scn_message_id"`
|
||||
SenderUserID int64 `json:"sender_user_id"`
|
||||
OwnerUserID int64 `json:"owner_user_id"`
|
||||
ChannelName string `json:"channel_name"`
|
||||
ChannelID int64 `json:"channel_id"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
Title string `json:"title"`
|
||||
Content *string `json:"body"`
|
||||
Priority int `json:"priority"`
|
||||
UserMessageID *string `json:"usr_message_id"`
|
||||
Trimmed bool `json:"trimmed"`
|
||||
}
|
||||
|
||||
type MessageDB struct {
|
||||
SCNMessageID int64 `db:"scn_message_id"`
|
||||
SenderUserID int64 `db:"sender_user_id"`
|
||||
OwnerUserID int64 `db:"owner_user_id"`
|
||||
ChannelName string `db:"channel_name"`
|
||||
ChannelID int64 `db:"channel_id"`
|
||||
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"`
|
||||
}
|
||||
|
||||
func (m MessageDB) Model() Message {
|
||||
return Message{
|
||||
SCNMessageID: m.SCNMessageID,
|
||||
SenderUserID: m.SenderUserID,
|
||||
OwnerUserID: m.OwnerUserID,
|
||||
ChannelName: m.ChannelName,
|
||||
ChannelID: m.ChannelID,
|
||||
TimestampReal: time.UnixMilli(m.TimestampReal),
|
||||
TimestampClient: timeOptFromMilli(m.TimestampClient),
|
||||
Title: m.Title,
|
||||
Content: m.Content,
|
||||
Priority: m.Priority,
|
||||
UserMessageID: m.UserMessageID,
|
||||
}
|
||||
}
|
||||
|
||||
func DecodeMessage(r *sql.Rows) (Message, error) {
|
||||
var data MessageDB
|
||||
err := scan.RowStrict(&data, r)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
return data.Model(), nil
|
||||
}
|
||||
|
||||
func DecodeMessages(r *sql.Rows) ([]Message, error) {
|
||||
var data []MessageDB
|
||||
err := scan.RowsStrict(&data, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return langext.ArrMap(data, func(v MessageDB) Message { return v.Model() }), nil
|
||||
}
|
||||
80
server/models/subscription.go
Normal file
80
server/models/subscription.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/blockloop/scan"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Subscription struct {
|
||||
SubscriptionID int64
|
||||
SubscriberUserID int64
|
||||
ChannelOwnerUserID int64
|
||||
ChannelID int64
|
||||
ChannelName 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,
|
||||
ChannelName: s.ChannelName,
|
||||
TimestampCreated: s.TimestampCreated.Format(time.RFC3339Nano),
|
||||
Confirmed: s.Confirmed,
|
||||
}
|
||||
}
|
||||
|
||||
type SubscriptionJSON struct {
|
||||
SubscriptionID int64 `json:"subscription_id"`
|
||||
SubscriberUserID int64 `json:"subscriber_user_id"`
|
||||
ChannelOwnerUserID int64 `json:"channel_owner_user_id"`
|
||||
ChannelID int64 `json:"channel_id"`
|
||||
ChannelName string `json:"channel_name"`
|
||||
TimestampCreated string `json:"timestamp_created"`
|
||||
Confirmed bool `json:"confirmed"`
|
||||
}
|
||||
|
||||
type SubscriptionDB struct {
|
||||
SubscriptionID int64 `db:"subscription_id"`
|
||||
SubscriberUserID int64 `db:"subscriber_user_id"`
|
||||
ChannelOwnerUserID int64 `db:"channel_owner_user_id"`
|
||||
ChannelID int64 `db:"channel_id"`
|
||||
ChannelName string `db:"channel_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,
|
||||
ChannelName: s.ChannelName,
|
||||
TimestampCreated: time.UnixMilli(s.TimestampCreated),
|
||||
Confirmed: s.Confirmed != 0,
|
||||
}
|
||||
}
|
||||
|
||||
func DecodeSubscription(r *sql.Rows) (Subscription, error) {
|
||||
var data SubscriptionDB
|
||||
err := scan.RowStrict(&data, r)
|
||||
if err != nil {
|
||||
return Subscription{}, err
|
||||
}
|
||||
return data.Model(), nil
|
||||
}
|
||||
|
||||
func DecodeSubscriptions(r *sql.Rows) ([]Subscription, error) {
|
||||
var data []SubscriptionDB
|
||||
err := scan.RowsStrict(&data, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return langext.ArrMap(data, func(v SubscriptionDB) Subscription { return v.Model() }), nil
|
||||
}
|
||||
137
server/models/user.go
Normal file
137
server/models/user.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/blockloop/scan"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
|
||||
"time"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
UserID int64
|
||||
Username *string
|
||||
SendKey string
|
||||
ReadKey string
|
||||
AdminKey string
|
||||
TimestampCreated time.Time
|
||||
TimestampLastRead *time.Time
|
||||
TimestampLastSent *time.Time
|
||||
MessagesSent int
|
||||
QuotaUsed int
|
||||
QuotaUsedDay *string
|
||||
IsPro bool
|
||||
ProToken *string
|
||||
}
|
||||
|
||||
func (u User) JSON() UserJSON {
|
||||
return UserJSON{
|
||||
UserID: u.UserID,
|
||||
Username: u.Username,
|
||||
ReadKey: u.ReadKey,
|
||||
SendKey: u.SendKey,
|
||||
AdminKey: u.AdminKey,
|
||||
TimestampCreated: u.TimestampCreated.Format(time.RFC3339Nano),
|
||||
TimestampLastRead: timeOptFmt(u.TimestampLastRead, time.RFC3339Nano),
|
||||
TimestampLastSent: timeOptFmt(u.TimestampLastSent, time.RFC3339Nano),
|
||||
MessagesSent: u.MessagesSent,
|
||||
QuotaUsed: u.QuotaUsed,
|
||||
QuotaUsedDay: u.QuotaUsedDay,
|
||||
IsPro: u.IsPro,
|
||||
}
|
||||
}
|
||||
|
||||
func (u User) MaxContentLength() int {
|
||||
if u.IsPro {
|
||||
return 16384
|
||||
} else {
|
||||
return 2048
|
||||
}
|
||||
}
|
||||
|
||||
func (u User) QuotaPerDay() int {
|
||||
if u.IsPro {
|
||||
return 1000
|
||||
} else {
|
||||
return 50
|
||||
}
|
||||
}
|
||||
|
||||
func (u User) QuotaUsedToday() int {
|
||||
now := time.Now().In(timeext.TimezoneBerlin).Format("2006-01-02")
|
||||
if u.QuotaUsedDay != nil && *u.QuotaUsedDay == now {
|
||||
return u.QuotaUsed
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (u User) QuotaRemainingToday() int {
|
||||
return u.QuotaPerDay() - u.QuotaUsedToday()
|
||||
}
|
||||
|
||||
type UserJSON struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
Username *string `json:"username"`
|
||||
ReadKey string `json:"read_key"`
|
||||
SendKey string `json:"send_key"`
|
||||
AdminKey string `json:"admin_key"`
|
||||
TimestampCreated string `json:"timestamp_created"`
|
||||
TimestampLastRead *string `json:"timestamp_last_read"`
|
||||
TimestampLastSent *string `json:"timestamp_last_sent"`
|
||||
MessagesSent int `json:"messages_sent"`
|
||||
QuotaUsed int `json:"quota_used"`
|
||||
QuotaUsedDay *string `json:"quota_used_day"`
|
||||
IsPro bool `json:"is_pro"`
|
||||
}
|
||||
|
||||
type UserDB struct {
|
||||
UserID int64 `db:"user_id"`
|
||||
Username *string `db:"username"`
|
||||
SendKey string `db:"send_key"`
|
||||
ReadKey string `db:"read_key"`
|
||||
AdminKey string `db:"admin_key"`
|
||||
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,
|
||||
SendKey: u.SendKey,
|
||||
ReadKey: u.ReadKey,
|
||||
AdminKey: u.AdminKey,
|
||||
TimestampCreated: time.UnixMilli(u.TimestampCreated),
|
||||
TimestampLastRead: timeOptFromMilli(u.TimestampLastRead),
|
||||
TimestampLastSent: timeOptFromMilli(u.TimestampLastSent),
|
||||
MessagesSent: u.MessagesSent,
|
||||
QuotaUsed: u.QuotaUsed,
|
||||
QuotaUsedDay: u.QuotaUsedDay,
|
||||
IsPro: u.IsPro,
|
||||
}
|
||||
}
|
||||
|
||||
func DecodeUser(r *sql.Rows) (User, error) {
|
||||
var data UserDB
|
||||
err := scan.RowStrict(&data, r)
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
return data.Model(), nil
|
||||
}
|
||||
|
||||
func DecodeUsers(r *sql.Rows) ([]User, error) {
|
||||
var data []UserDB
|
||||
err := scan.RowsStrict(&data, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return langext.ArrMap(data, func(v UserDB) User { return v.Model() }), nil
|
||||
}
|
||||
21
server/models/utils.go
Normal file
21
server/models/utils.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"time"
|
||||
)
|
||||
|
||||
func timeOptFmt(t *time.Time, fmt string) *string {
|
||||
if t == nil {
|
||||
return nil
|
||||
} else {
|
||||
return langext.Ptr(t.Format(fmt))
|
||||
}
|
||||
}
|
||||
|
||||
func timeOptFromMilli(millis *int64) *time.Time {
|
||||
if millis == nil {
|
||||
return nil
|
||||
}
|
||||
return langext.Ptr(time.UnixMilli(*millis))
|
||||
}
|
||||
Reference in New Issue
Block a user