veza/veza-backend-api/internal/models/message.go

47 lines
1.7 KiB
Go
Raw Normal View History

2025-12-03 19:29:37 +00:00
package models
import (
"time"
2025-12-16 16:23:49 +00:00
"github.com/google/uuid"
2025-12-03 19:29:37 +00:00
"gorm.io/gorm"
)
// Message représente un message dans une room de chat
type Message struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
RoomID uuid.UUID `gorm:"type:uuid;not null" json:"room_id"`
2025-12-16 16:23:49 +00:00
UserID uuid.UUID `gorm:"column:sender_id;type:uuid;not null" json:"user_id"`
2025-12-03 19:29:37 +00:00
Content string `gorm:"not null;type:text" json:"content"`
2025-12-16 16:23:49 +00:00
Type string `gorm:"column:message_type;not null;default:'text'" json:"type"`
ParentID *uuid.UUID `gorm:"column:reply_to_id;type:uuid" json:"parent_id,omitempty"`
2025-12-03 19:29:37 +00:00
IsEdited bool `gorm:"default:false" json:"is_edited"`
EditedAt *time.Time `gorm:"" json:"edited_at,omitempty"`
2025-12-03 19:29:37 +00:00
IsDeleted bool `gorm:"default:false" json:"is_deleted"`
IsPinned bool `gorm:"default:false" json:"is_pinned"`
Status string `gorm:"size:20;default:'sent'" json:"status"`
Metadata *string `gorm:"type:jsonb" json:"metadata,omitempty"`
2025-12-03 19:29:37 +00:00
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
// Relations
Room Room `gorm:"foreignKey:RoomID;constraint:OnDelete:CASCADE" json:"-"`
User User `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"-"`
Parent *Message `gorm:"foreignKey:ParentID;constraint:OnDelete:SET NULL" json:"-"`
}
// BeforeCreate hook GORM pour générer UUID si non défini
func (m *Message) BeforeCreate(tx *gorm.DB) error {
if m.ID == uuid.Nil {
m.ID = uuid.New()
}
return nil
}
// TableName définit le nom de la table pour GORM
func (Message) TableName() string {
return "messages"
}