Use ID types

This commit is contained in:
2022-11-20 22:18:24 +01:00
parent ca58aa782d
commit 0cc6e27267
23 changed files with 334 additions and 256 deletions

View File

@@ -8,8 +8,8 @@ import (
)
type Channel struct {
ChannelID int64
OwnerUserID int64
ChannelID ChannelID
OwnerUserID UserID
Name string
SubscribeKey string
SendKey string
@@ -32,26 +32,26 @@ func (c Channel) JSON(includeKey bool) ChannelJSON {
}
type ChannelJSON struct {
ChannelID int64 `json:"channel_id"`
OwnerUserID int64 `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"`
TimestampLastSent *string `json:"timestamp_last_sent"`
MessagesSent int `json:"messages_sent"`
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"`
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"`
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"`
TimestampLastRead *int64 `db:"timestamp_last_read"`
TimestampLastSent *int64 `db:"timestamp_last_sent"`
MessagesSent int `db:"messages_sent"`
}
func (c ChannelDB) Model() Channel {

View File

@@ -15,8 +15,8 @@ const (
)
type Client struct {
ClientID int64
UserID int64
ClientID ClientID
UserID UserID
Type ClientType
FCMToken *string
TimestampCreated time.Time
@@ -37,8 +37,8 @@ func (c Client) JSON() ClientJSON {
}
type ClientJSON struct {
ClientID int64 `json:"client_id"`
UserID int64 `json:"user_id"`
ClientID ClientID `json:"client_id"`
UserID UserID `json:"user_id"`
Type ClientType `json:"type"`
FCMToken *string `json:"fcm_token"`
TimestampCreated string `json:"timestamp_created"`
@@ -47,8 +47,8 @@ type ClientJSON struct {
}
type ClientDB struct {
ClientID int64 `db:"client_id"`
UserID int64 `db:"user_id"`
ClientID ClientID `db:"client_id"`
UserID UserID `db:"user_id"`
Type ClientType `db:"type"`
FCMToken *string `db:"fcm_token"`
TimestampCreated int64 `db:"timestamp_created"`

View File

@@ -16,10 +16,10 @@ const (
)
type Delivery struct {
DeliveryID int64
SCNMessageID int64
ReceiverUserID int64
ReceiverClientID int64
DeliveryID DeliveryID
SCNMessageID SCNMessageID
ReceiverUserID UserID
ReceiverClientID ClientID
TimestampCreated time.Time
TimestampFinalized *time.Time
Status DeliveryStatus
@@ -48,10 +48,10 @@ func (d Delivery) MaxRetryCount() int {
}
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"`
DeliveryID DeliveryID `json:"delivery_id"`
SCNMessageID SCNMessageID `json:"scn_message_id"`
ReceiverUserID UserID `json:"receiver_user_id"`
ReceiverClientID ClientID `json:"receiver_client_id"`
TimestampCreated string `json:"timestamp_created"`
TimestampFinalized *string `json:"tiestamp_finalized"`
Status DeliveryStatus `json:"status"`
@@ -61,10 +61,10 @@ type DeliveryJSON struct {
}
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"`
DeliveryID DeliveryID `db:"delivery_id"`
SCNMessageID SCNMessageID `db:"scn_message_id"`
ReceiverUserID UserID `db:"receiver_user_id"`
ReceiverClientID ClientID `db:"receiver_client_id"`
TimestampCreated int64 `db:"timestamp_created"`
TimestampFinalized *int64 `db:"tiestamp_finalized"`
Status DeliveryStatus `db:"status"`

68
server/models/ids.go Normal file
View File

@@ -0,0 +1,68 @@
package models
import "strconv"
type EntityID interface {
IntID() int64
String() string
}
type UserID int64
func (id UserID) IntID() int64 {
return int64(id)
}
func (id UserID) String() string {
return strconv.FormatInt(int64(id), 10)
}
type ChannelID int64
func (id ChannelID) IntID() int64 {
return int64(id)
}
func (id ChannelID) String() string {
return strconv.FormatInt(int64(id), 10)
}
type DeliveryID int64
func (id DeliveryID) IntID() int64 {
return int64(id)
}
func (id DeliveryID) String() string {
return strconv.FormatInt(int64(id), 10)
}
type SCNMessageID int64
func (id SCNMessageID) IntID() int64 {
return int64(id)
}
func (id SCNMessageID) String() string {
return strconv.FormatInt(int64(id), 10)
}
type SubscriptionID int64
func (id SubscriptionID) IntID() int64 {
return int64(id)
}
func (id SubscriptionID) String() string {
return strconv.FormatInt(int64(id), 10)
}
type ClientID int64
func (id ClientID) IntID() int64 {
return int64(id)
}
func (id ClientID) String() string {
return strconv.FormatInt(int64(id), 10)
}

View File

@@ -13,11 +13,11 @@ const (
)
type Message struct {
SCNMessageID int64
SenderUserID int64
OwnerUserID int64
SCNMessageID SCNMessageID
SenderUserID UserID
OwnerUserID UserID
ChannelName string
ChannelID int64
ChannelID ChannelID
TimestampReal time.Time
TimestampClient *time.Time
Title string
@@ -87,31 +87,31 @@ func (m Message) ShortContent() string {
}
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"`
SCNMessageID SCNMessageID `json:"scn_message_id"`
SenderUserID UserID `json:"sender_user_id"`
OwnerUserID UserID `json:"owner_user_id"`
ChannelName string `json:"channel_name"`
ChannelID ChannelID `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"`
SCNMessageID SCNMessageID `db:"scn_message_id"`
SenderUserID UserID `db:"sender_user_id"`
OwnerUserID UserID `db:"owner_user_id"`
ChannelName string `db:"channel_name"`
ChannelID ChannelID `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 {

View File

@@ -8,10 +8,10 @@ import (
)
type Subscription struct {
SubscriptionID int64
SubscriberUserID int64
ChannelOwnerUserID int64
ChannelID int64
SubscriptionID SubscriptionID
SubscriberUserID UserID
ChannelOwnerUserID UserID
ChannelID ChannelID
ChannelName string
TimestampCreated time.Time
Confirmed bool
@@ -30,23 +30,23 @@ func (s Subscription) JSON() SubscriptionJSON {
}
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"`
SubscriptionID SubscriptionID `json:"subscription_id"`
SubscriberUserID UserID `json:"subscriber_user_id"`
ChannelOwnerUserID UserID `json:"channel_owner_user_id"`
ChannelID ChannelID `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"`
SubscriptionID SubscriptionID `db:"subscription_id"`
SubscriberUserID UserID `db:"subscriber_user_id"`
ChannelOwnerUserID UserID `db:"channel_owner_user_id"`
ChannelID ChannelID `db:"channel_id"`
ChannelName string `db:"channel_name"`
TimestampCreated int64 `db:"timestamp_created"`
Confirmed int `db:"confirmed"`
}
func (s SubscriptionDB) Model() Subscription {

View File

@@ -9,7 +9,7 @@ import (
)
type User struct {
UserID int64
UserID UserID
Username *string
SendKey string
ReadKey string
@@ -38,6 +38,7 @@ func (u User) JSON() UserJSON {
QuotaUsed: u.QuotaUsed,
QuotaUsedDay: u.QuotaUsedDay,
IsPro: u.IsPro,
DefaultChannel: u.DefaultChannel(),
}
}
@@ -74,8 +75,12 @@ func (u User) QuotaRemainingToday() int {
return u.QuotaPerDay() - u.QuotaUsedToday()
}
func (u User) DefaultChannel() string {
return "main"
}
type UserJSON struct {
UserID int64 `json:"user_id"`
UserID UserID `json:"user_id"`
Username *string `json:"username"`
ReadKey string `json:"read_key"`
SendKey string `json:"send_key"`
@@ -87,10 +92,11 @@ type UserJSON struct {
QuotaUsed int `json:"quota_used"`
QuotaUsedDay *string `json:"quota_used_day"`
IsPro bool `json:"is_pro"`
DefaultChannel string `json:"default_channel"`
}
type UserDB struct {
UserID int64 `db:"user_id"`
UserID UserID `db:"user_id"`
Username *string `db:"username"`
SendKey string `db:"send_key"`
ReadKey string `db:"read_key"`