veza/veza-backend-api/internal/models/message.go
senke 4d4d07836c feat(chat): Sprint 1 -- migrations, models, repositories for chat rewrite
- Add migrations 109-112: read_receipts, delivered_status, message_reactions, messages extra columns
- Create ReadReceipt, DeliveredStatus, MessageReaction GORM models
- Update Message model with EditedAt, Status, IsPinned, Metadata fields
- Enrich ChatMessageRepository with cursor pagination, search, soft delete
- Create ReadReceiptRepository, DeliveredStatusRepository, ReactionRepository
- Create ChatPubSubService with Redis PubSub and in-memory fallback
2026-02-22 20:38:20 +01:00

46 lines
1.7 KiB
Go

package models
import (
"time"
"github.com/google/uuid"
"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"`
UserID uuid.UUID `gorm:"column:sender_id;type:uuid;not null" json:"user_id"`
Content string `gorm:"not null;type:text" json:"content"`
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"`
IsEdited bool `gorm:"default:false" json:"is_edited"`
EditedAt *time.Time `gorm:"" json:"edited_at,omitempty"`
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"`
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"
}