2022-11-19 15:13:47 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2024-06-01 15:37:59 +02:00
|
|
|
"fmt"
|
2022-11-19 15:13:47 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-11-19 23:16:54 +01:00
|
|
|
const (
|
|
|
|
ContentLengthTrim = 1900
|
|
|
|
ContentLengthShort = 200
|
|
|
|
)
|
|
|
|
|
2022-11-19 15:13:47 +01:00
|
|
|
type Message struct {
|
2024-09-15 21:07:46 +02:00
|
|
|
MessageID MessageID `db:"message_id" json:"message_id"`
|
|
|
|
SenderUserID UserID `db:"sender_user_id" json:"sender_user_id"` // user that sent the message (this is also the owner of the channel that contains it)
|
|
|
|
ChannelInternalName string `db:"channel_internal_name" json:"channel_internal_name"`
|
|
|
|
ChannelID ChannelID `db:"channel_id" json:"channel_id"`
|
|
|
|
SenderName *string `db:"sender_name" json:"sender_name"`
|
|
|
|
SenderIP string `db:"sender_ip" json:"sender_ip"`
|
|
|
|
TimestampReal SCNTime `db:"timestamp_real" json:"-"`
|
|
|
|
TimestampClient *SCNTime `db:"timestamp_client" json:"-"`
|
|
|
|
Title string `db:"title" json:"title"`
|
|
|
|
Content *string `db:"content" json:"content"`
|
|
|
|
Priority int `db:"priority" json:"priority"`
|
|
|
|
UserMessageID *string `db:"usr_message_id" json:"usr_message_id"`
|
|
|
|
UsedKeyID KeyTokenID `db:"used_key_id" json:"used_key_id"`
|
|
|
|
Deleted bool `db:"deleted" json:"-"`
|
|
|
|
|
|
|
|
MessageExtra `db:"-"` // fields that are not in DB and are set on PreMarshal
|
2022-11-19 15:13:47 +01:00
|
|
|
}
|
|
|
|
|
2024-09-15 21:07:46 +02:00
|
|
|
type MessageExtra struct {
|
|
|
|
Timestamp SCNTime `db:"-" json:"timestamp"`
|
|
|
|
Trimmed bool `db:"-" json:"trimmed"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Message) PreMarshal() Message {
|
|
|
|
u.MessageExtra.Timestamp = NewSCNTime(u.Timestamp())
|
|
|
|
return *u
|
2022-11-19 23:16:54 +01:00
|
|
|
}
|
|
|
|
|
2024-09-15 21:07:46 +02:00
|
|
|
func (m Message) Trim() Message {
|
|
|
|
r := m
|
|
|
|
if !r.Trimmed && r.NeedsTrim() {
|
|
|
|
r.Content = r.TrimmedContent()
|
|
|
|
r.MessageExtra.Trimmed = true
|
2022-11-19 15:13:47 +01:00
|
|
|
}
|
2024-09-15 21:07:46 +02:00
|
|
|
return r.PreMarshal()
|
2022-11-19 15:13:47 +01:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:07:30 +01:00
|
|
|
func (m Message) Timestamp() time.Time {
|
2024-09-15 21:07:46 +02:00
|
|
|
return langext.Coalesce(m.TimestampClient, m.TimestampReal).Time()
|
2022-11-19 17:07:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m Message) NeedsTrim() bool {
|
2022-11-19 23:16:54 +01:00
|
|
|
return m.Content != nil && len(*m.Content) > ContentLengthTrim
|
2022-11-19 17:07:30 +01:00
|
|
|
}
|
|
|
|
|
2022-11-19 23:16:54 +01:00
|
|
|
func (m Message) TrimmedContent() *string {
|
|
|
|
if m.Content == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-19 17:07:30 +01:00
|
|
|
if !m.NeedsTrim() {
|
2022-11-19 23:16:54 +01:00
|
|
|
return m.Content
|
2022-11-19 17:07:30 +01:00
|
|
|
}
|
2022-11-19 23:16:54 +01:00
|
|
|
return langext.Ptr(langext.Coalesce(m.Content, "")[0:ContentLengthTrim-3] + "...")
|
2022-11-19 17:07:30 +01:00
|
|
|
}
|
|
|
|
|
2022-11-19 23:16:54 +01:00
|
|
|
func (m Message) ShortContent() string {
|
2022-11-19 17:07:30 +01:00
|
|
|
if m.Content == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2022-11-19 23:16:54 +01:00
|
|
|
if len(*m.Content) < ContentLengthShort {
|
2022-11-19 17:07:30 +01:00
|
|
|
return *m.Content
|
|
|
|
}
|
2022-11-19 23:16:54 +01:00
|
|
|
return (*m.Content)[0:ContentLengthShort-3] + "..."
|
2022-11-19 17:07:30 +01:00
|
|
|
}
|
|
|
|
|
2024-06-01 15:37:59 +02:00
|
|
|
func (m Message) FormatNotificationTitle(user User, channel Channel) string {
|
|
|
|
if m.ChannelInternalName == user.DefaultChannel() {
|
|
|
|
return m.Title
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("[%s] %s", channel.DisplayName, m.Title)
|
|
|
|
}
|