forked from mtusi2026/mtusi2026lab6
ЛР 6 — БПИ2302 — Нурлыгаянова — на 3
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
// Attachment states are persisted; ready requires both original and thumbnail.
|
||||
const StatusReady = "ready"
|
||||
const StatusQueued = "queued"
|
||||
|
||||
const (
|
||||
ContentTypePNG = "image/png"
|
||||
ContentTypeJPEG = "image/jpeg"
|
||||
)
|
||||
|
||||
type Attachment struct {
|
||||
ID string
|
||||
ChatID string
|
||||
OwnerID string
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
SHA256 string
|
||||
// ObjectKey — внутренний ключ в bucket, назначает сервер. В публичный API не попадает.
|
||||
ObjectKey string
|
||||
Variants []Variant
|
||||
ErrorCode *string
|
||||
Status string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// Variant describes one durable derivative; storage keys stay private.
|
||||
type Variant struct {
|
||||
Name string `json:"name"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type Chat struct {
|
||||
ID string
|
||||
Title string
|
||||
MemberIDs []string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
|
||||
ErrUnauthorized = errors.New("unauthorized")
|
||||
|
||||
ErrConflict = errors.New("conflict")
|
||||
|
||||
// ErrIdempotencyConflict — ключ уже использован с другим телом запроса.
|
||||
ErrIdempotencyConflict = fmt.Errorf("idempotency key reused with different request: %w", ErrConflict)
|
||||
|
||||
ErrInvalidArgument = errors.New("invalid argument")
|
||||
|
||||
ErrUnavailable = errors.New("unavailable")
|
||||
|
||||
ErrPayloadTooLarge = errors.New("payload too large")
|
||||
|
||||
ErrRateLimited = errors.New("rate limited")
|
||||
|
||||
ErrUnsupportedMedia = errors.New("unsupported media")
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type Message struct {
|
||||
ID string
|
||||
ChatID string
|
||||
AuthorID string
|
||||
Text string
|
||||
CreatedAt time.Time
|
||||
// AttachmentIDs — вложения этого чата в порядке из запроса; без вложений пусто.
|
||||
AttachmentIDs []string
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Page[T any] struct {
|
||||
Items []T
|
||||
NextCursor string
|
||||
}
|
||||
|
||||
type Cursor struct {
|
||||
CreatedAt time.Time `json:"t"`
|
||||
ID string `json:"i"`
|
||||
Scope string `json:"s,omitempty"`
|
||||
}
|
||||
|
||||
func (c Cursor) IsZero() bool {
|
||||
return c.ID == "" && c.CreatedAt.IsZero()
|
||||
}
|
||||
|
||||
func (c Cursor) Less(createdAt time.Time, id string) bool {
|
||||
if c.CreatedAt.Before(createdAt) {
|
||||
return true
|
||||
}
|
||||
if c.CreatedAt.After(createdAt) {
|
||||
return false
|
||||
}
|
||||
return c.ID < id
|
||||
}
|
||||
|
||||
func EncodeCursor(c Cursor) string {
|
||||
if c.IsZero() {
|
||||
return ""
|
||||
}
|
||||
b, _ := json.Marshal(c)
|
||||
return base64.RawURLEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
func DecodeCursor(s string) (Cursor, error) {
|
||||
if s == "" {
|
||||
return Cursor{}, nil
|
||||
}
|
||||
raw, err := base64.RawURLEncoding.DecodeString(s)
|
||||
if err != nil {
|
||||
return Cursor{}, ErrInvalidArgument
|
||||
}
|
||||
var c Cursor
|
||||
if err := json.Unmarshal(raw, &c); err != nil {
|
||||
return Cursor{}, ErrInvalidArgument
|
||||
}
|
||||
|
||||
if c.CreatedAt.IsZero() || c.Scope == "" {
|
||||
return Cursor{}, ErrInvalidArgument
|
||||
}
|
||||
if _, err := uuid.Parse(c.ID); err != nil || len(c.ID) != 36 {
|
||||
return Cursor{}, ErrInvalidArgument
|
||||
}
|
||||
if EncodeCursor(c) != s {
|
||||
return Cursor{}, ErrInvalidArgument
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type Session struct {
|
||||
Token string
|
||||
ExpiresAt time.Time
|
||||
User User
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type User struct {
|
||||
ID string
|
||||
Username string
|
||||
DisplayName string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
Reference in New Issue
Block a user