131 lines
4.8 KiB
Go
131 lines
4.8 KiB
Go
package model
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// Timestamp — детерминированная сериализация времени (ровно 3 знака миллисекунд).
|
|
// Из lab1, без изменений.
|
|
type Timestamp struct{ time.Time }
|
|
|
|
func (t Timestamp) MarshalJSON() ([]byte, error) {
|
|
s := t.UTC().Format("2006-01-02T15:04:05.000Z")
|
|
return strconv.AppendQuote(nil, s), nil
|
|
}
|
|
|
|
func NowTS() Timestamp { return Timestamp{Time: time.Now().UTC().Truncate(time.Millisecond)} }
|
|
|
|
// User — ответ API: пароль и его hash НЕ включены (контракт).
|
|
type User struct {
|
|
ID string `json:"id"`
|
|
Username string `json:"username"`
|
|
DisplayName string `json:"display_name"`
|
|
CreatedAt Timestamp `json:"created_at"`
|
|
}
|
|
|
|
// CreateUserReq — вход регистрации: username + display_name + password.
|
|
// Password только на входе, в User не попадает.
|
|
type CreateUserReq struct {
|
|
Username string `json:"username"`
|
|
DisplayName string `json:"display_name"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
// LoginRequest — вход в систему через Basic Auth (парсится из заголовка Authorization).
|
|
type LoginRequest struct {
|
|
Username string
|
|
Password string
|
|
}
|
|
|
|
// Session — внутренняя модель сессии (не отдаётся клиенту).
|
|
type Session struct {
|
|
TokenHash []byte // SHA-256 от opaque-токена
|
|
UserID string
|
|
ExpiresAt time.Time
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
// SessionResponse — ответ POST /auth/sessions (то, что отдаём клиенту).
|
|
type SessionResponse struct {
|
|
Token string `json:"token"`
|
|
TokenType string `json:"token_type"` // всегда "Bearer"
|
|
ExpiresAt Timestamp `json:"expires_at"`
|
|
User User `json:"user"`
|
|
}
|
|
|
|
type Chat struct {
|
|
ID string `json:"id"`
|
|
Title string `json:"title"`
|
|
MemberIDs []string `json:"member_ids"`
|
|
CreatedAt Timestamp `json:"created_at"`
|
|
}
|
|
|
|
type CreateChatReq struct {
|
|
Title string `json:"title"`
|
|
MemberIDs []string `json:"member_ids"`
|
|
}
|
|
|
|
// Message — ответ API. Поле AttachmentIDs обязательно по контракту lab4,
|
|
// для старых текстовых сообщений возвращаем [].
|
|
type Message struct {
|
|
ID string `json:"id"`
|
|
ChatID string `json:"chat_id"`
|
|
SenderID string `json:"sender_id"`
|
|
Text string `json:"text"`
|
|
CreatedAt Timestamp `json:"created_at"`
|
|
AttachmentIDs []string `json:"attachment_ids"`
|
|
}
|
|
|
|
// CreateMessageReq — вход для отправки сообщения.
|
|
// AttachmentIDs опционален (default: []); по контракту anyOf:
|
|
// либо text minLength:1, либо attachment_ids minItems:1.
|
|
type CreateMessageReq struct {
|
|
Text string `json:"text"`
|
|
AttachmentIDs []string `json:"attachment_ids"`
|
|
}
|
|
|
|
// Page — дженерик-страница (из lab1).
|
|
type Page[T any] struct {
|
|
Items []T `json:"items"`
|
|
NextCursor *string `json:"next_cursor"`
|
|
}
|
|
|
|
type Health struct {
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
// ─────────────────────── LAB 4: Вложения ───────────────────────
|
|
|
|
// Attachment — метаданные вложения (ответ API).
|
|
// В лабе 4 всегда: status="ready", variants=[], error_code=null.
|
|
// S3Key хранится в БД, но НЕ отдаётся клиенту (json:"-").
|
|
type Attachment struct {
|
|
ID string `json:"id"`
|
|
ChatID string `json:"chat_id"`
|
|
OwnerID string `json:"owner_id"`
|
|
Filename string `json:"filename"`
|
|
ContentType string `json:"content_type"` // "image/png" | "image/jpeg"
|
|
SizeBytes int64 `json:"size_bytes"`
|
|
SHA256 string `json:"sha256"`
|
|
Status string `json:"status"` // "ready" в лабе 4
|
|
Variants []Variant `json:"variants"` // [] в лабе 4
|
|
ErrorCode *string `json:"error_code"` // null в лабе 4
|
|
CreatedAt Timestamp `json:"created_at"`
|
|
UpdatedAt Timestamp `json:"updated_at"`
|
|
|
|
// Внутренние поля, не сериализуются в JSON
|
|
S3Key string `json:"-"`
|
|
}
|
|
|
|
// Variant — производная версия оригинала (thumbnail, crop, optimized).
|
|
// В лабе 4 не используется (variants всегда []), структура нужна для схемы контракта.
|
|
type Variant struct {
|
|
Name string `json:"name"` // "thumbnail" | "crop" | "optimized"
|
|
ContentType string `json:"content_type"` // image/jpeg | image/png | image/webp
|
|
SizeBytes int64 `json:"size_bytes"`
|
|
Width int `json:"width"`
|
|
Height int `json:"height"`
|
|
SHA256 string `json:"sha256"`
|
|
}
|