xtablo-source/backend/templates/discussion_forms.go
2026-05-16 10:11:14 +02:00

83 lines
1.9 KiB
Go

package templates
import (
"strconv"
"time"
"backend/internal/db/sqlc"
"github.com/google/uuid"
)
const DiscussionMaxBodyLength = 10000
type DiscussionForm struct {
Body string
}
type DiscussionErrors struct {
Body string
General string
}
type DiscussionMessageView struct {
ID uuid.UUID
AuthorEmail string
Body string
CreatedAt time.Time
}
type DiscussionTabData struct {
Messages []DiscussionMessageView
}
func DiscussionPostURL(tabloID uuid.UUID) string {
return "/tablos/" + tabloID.String() + "/discussion/messages"
}
func DiscussionMaxBodyLengthString() string {
return strconv.Itoa(DiscussionMaxBodyLength)
}
func DiscussionURL(tabloID uuid.UUID) string {
return "/tablos/" + tabloID.String() + "/discussion"
}
func DiscussionMessagesFromRows(rows []sqlc.ListDiscussionMessagesByTabloRow) []DiscussionMessageView {
messages := make([]DiscussionMessageView, 0, len(rows))
for _, row := range rows {
messages = append(messages, DiscussionMessageView{
ID: row.ID,
AuthorEmail: row.AuthorEmail,
Body: row.Body,
CreatedAt: row.CreatedAt.Time,
})
}
return messages
}
func DiscussionMessageFromRow(row sqlc.GetDiscussionMessageWithAuthorRow) DiscussionMessageView {
return DiscussionMessageView{
ID: row.ID,
AuthorEmail: row.AuthorEmail,
Body: row.Body,
CreatedAt: row.CreatedAt.Time,
}
}
func DiscussionDateLabel(t time.Time) string {
return t.Local().Format("January 2, 2006")
}
func DiscussionTimestampLabel(t time.Time) string {
return t.Local().Format("January 2, 2006 15:04")
}
func DiscussionShowDaySeparator(messages []DiscussionMessageView, index int) bool {
if index == 0 {
return true
}
current := messages[index].CreatedAt.Local()
previous := messages[index-1].CreatedAt.Local()
return current.Year() != previous.Year() || current.YearDay() != previous.YearDay()
}