Files
backend/internal/model/notification.go
lan 4d8f2ec997 Initial backend repository commit.
Set up project files and add .gitignore to exclude local build/runtime artifacts.

Made-with: Cursor
2026-03-09 21:28:58 +08:00

54 lines
1.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package model
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
// NotificationType 通知类型
type NotificationType string
const (
NotificationTypeLikePost NotificationType = "like_post"
NotificationTypeLikeComment NotificationType = "like_comment"
NotificationTypeComment NotificationType = "comment"
NotificationTypeReply NotificationType = "reply"
NotificationTypeFollow NotificationType = "follow"
NotificationTypeMention NotificationType = "mention"
NotificationTypeSystem NotificationType = "system"
)
// Notification 通知实体
type Notification struct {
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
UserID string `json:"user_id" gorm:"type:varchar(36);not null;index:idx_notifications_user_read_created,priority:1"` // 接收者
Type NotificationType `json:"type" gorm:"type:varchar(30);not null"`
Title string `json:"title" gorm:"type:varchar(200);not null"`
Content string `json:"content" gorm:"type:text"`
Data string `json:"data" gorm:"type:jsonb"` // 相关数据JSON
// 已读状态
IsRead bool `json:"is_read" gorm:"default:false;index:idx_notifications_user_read_created,priority:2"`
ReadAt *time.Time `json:"read_at" gorm:"type:timestamp"`
// 软删除
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
// 时间戳
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;index:idx_notifications_user_read_created,priority:3,sort:desc"`
}
// BeforeCreate 创建前生成UUID
func (n *Notification) BeforeCreate(tx *gorm.DB) error {
if n.ID == "" {
n.ID = uuid.New().String()
}
return nil
}
func (Notification) TableName() string {
return "notifications"
}