CreateSubscription(), UpdateSubscription(), GetMessage(), DeleteMessage()

This commit is contained in:
2022-11-19 23:16:54 +01:00
parent 8278c059ad
commit 0d641b727f
19 changed files with 1155 additions and 669 deletions

View File

@@ -14,7 +14,6 @@ type Channel struct {
SubscribeKey string
SendKey string
TimestampCreated time.Time
TimestampLastRead *time.Time
TimestampLastSent *time.Time
MessagesSent int
}
@@ -27,7 +26,6 @@ func (c Channel) JSON() ChannelJSON {
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,
}
@@ -40,7 +38,6 @@ type ChannelJSON struct {
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"`
}
@@ -65,7 +62,6 @@ func (c ChannelDB) Model() Channel {
SubscribeKey: c.SubscribeKey,
SendKey: c.SendKey,
TimestampCreated: time.UnixMilli(c.TimestampCreated),
TimestampLastRead: timeOptFromMilli(c.TimestampLastRead),
TimestampLastSent: timeOptFromMilli(c.TimestampLastSent),
MessagesSent: c.MessagesSent,
}

View File

@@ -7,6 +7,11 @@ import (
"time"
)
const (
ContentLengthTrim = 1900
ContentLengthShort = 200
)
type Message struct {
SCNMessageID int64
SenderUserID int64
@@ -21,7 +26,7 @@ type Message struct {
UserMessageID *string
}
func (m Message) JSON() MessageJSON {
func (m Message) FullJSON() MessageJSON {
return MessageJSON{
SCNMessageID: m.SCNMessageID,
SenderUserID: m.SenderUserID,
@@ -33,6 +38,23 @@ func (m Message) JSON() MessageJSON {
Content: m.Content,
Priority: m.Priority,
UserMessageID: m.UserMessageID,
Trimmed: false,
}
}
func (m Message) TrimmedJSON() MessageJSON {
return MessageJSON{
SCNMessageID: m.SCNMessageID,
SenderUserID: m.SenderUserID,
OwnerUserID: m.OwnerUserID,
ChannelName: m.ChannelName,
ChannelID: m.ChannelID,
Timestamp: m.Timestamp().Format(time.RFC3339Nano),
Title: m.Title,
Content: m.TrimmedContent(),
Priority: m.Priority,
UserMessageID: m.UserMessageID,
Trimmed: m.NeedsTrim(),
}
}
@@ -41,24 +63,27 @@ func (m Message) Timestamp() time.Time {
}
func (m Message) NeedsTrim() bool {
return m.Content != nil && len(*m.Content) > 1900
return m.Content != nil && len(*m.Content) > ContentLengthTrim
}
func (m Message) TrimmedBody() string {
if !m.NeedsTrim() {
return langext.Coalesce(m.Content, "")
func (m Message) TrimmedContent() *string {
if m.Content == nil {
return nil
}
return langext.Coalesce(m.Content, "")[0:1900-3] + "..."
if !m.NeedsTrim() {
return m.Content
}
return langext.Ptr(langext.Coalesce(m.Content, "")[0:ContentLengthTrim-3] + "...")
}
func (m Message) ShortBody() string {
func (m Message) ShortContent() string {
if m.Content == nil {
return ""
}
if len(*m.Content) < 200 {
if len(*m.Content) < ContentLengthShort {
return *m.Content
}
return (*m.Content)[0:200-3] + "..."
return (*m.Content)[0:ContentLengthShort-3] + "..."
}
type MessageJSON struct {
@@ -72,6 +97,7 @@ type MessageJSON struct {
Content *string `json:"body"`
Priority int `json:"priority"`
UserMessageID *string `json:"usr_message_id"`
Trimmed bool `json:"trimmed"`
}
type MessageDB struct {

View File

@@ -1,10 +1,10 @@
package models
import (
scn "blackforestbytes.com/simplecloudnotifier"
"database/sql"
"github.com/blockloop/scan"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
"time"
)
@@ -58,7 +58,7 @@ func (u User) QuotaPerDay() int {
}
func (u User) QuotaUsedToday() int {
now := time.Now().In(timeext.TimezoneBerlin).Format("2006-01-02")
now := scn.QuotaDayString()
if u.QuotaUsedDay != nil && *u.QuotaUsedDay == now {
return u.QuotaUsed
} else {