Реализовать lab3 на оценку 4: балансировка и отказ API
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model"
|
||||
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PersistentMessenger struct{ db *store.PostgresStore }
|
||||
|
||||
func NewPersistentMessenger(db *store.PostgresStore) *PersistentMessenger {
|
||||
return &PersistentMessenger{db: db}
|
||||
}
|
||||
|
||||
func (m *PersistentMessenger) CreateChat(ctx context.Context, actor, title string, members []string) (model.Chat, error) {
|
||||
if !validText(title, 100) || len(members) < 1 || len(members) > 100 {
|
||||
return model.Chat{}, ErrInvalidRequest
|
||||
}
|
||||
normalized := make([]string, len(members))
|
||||
seen := make(map[string]bool)
|
||||
for i, id := range members {
|
||||
if !uuidPattern.MatchString(id) {
|
||||
return model.Chat{}, ErrInvalidRequest
|
||||
}
|
||||
id = strings.ToLower(id)
|
||||
if seen[id] {
|
||||
return model.Chat{}, store.ErrInvalidMembers
|
||||
}
|
||||
seen[id] = true
|
||||
normalized[i] = id
|
||||
}
|
||||
if !seen[actor] {
|
||||
return model.Chat{}, store.ErrInvalidMembers
|
||||
}
|
||||
return m.db.CreateChat(ctx, title, normalized)
|
||||
}
|
||||
func (m *PersistentMessenger) GetChat(ctx context.Context, actor, id string) (model.Chat, error) {
|
||||
if !uuidPattern.MatchString(id) {
|
||||
return model.Chat{}, ErrInvalidRequest
|
||||
}
|
||||
return m.db.GetChat(ctx, actor, strings.ToLower(id))
|
||||
}
|
||||
func decodePosition(cursor, scope string, limit int) (*store.Position, error) {
|
||||
if limit < 1 || limit > 100 {
|
||||
return nil, ErrInvalidRequest
|
||||
}
|
||||
if cursor == "" {
|
||||
return nil, nil
|
||||
}
|
||||
if len(cursor) > 2048 {
|
||||
return nil, ErrInvalidRequest
|
||||
}
|
||||
raw, err := base64.RawURLEncoding.DecodeString(cursor)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidRequest
|
||||
}
|
||||
var data cursorData
|
||||
if json.Unmarshal(raw, &data) != nil || data.Scope != scope || data.After.CreatedAt.IsZero() || !uuidPattern.MatchString(data.After.ID) {
|
||||
return nil, ErrInvalidRequest
|
||||
}
|
||||
if data.After.CreatedAt.Year() < 1 || data.After.CreatedAt.Year() > 9999 {
|
||||
return nil, ErrInvalidRequest
|
||||
}
|
||||
return &store.Position{CreatedAt: data.After.CreatedAt, ID: strings.ToLower(data.After.ID)}, nil
|
||||
}
|
||||
func databasePage[T any](items []T, limit int, scope string, key func(T) position) (Page[T], error) {
|
||||
page := Page[T]{Items: items}
|
||||
if len(items) <= limit {
|
||||
return page, nil
|
||||
}
|
||||
page.Items = items[:limit]
|
||||
raw, err := json.Marshal(cursorData{Scope: scope, After: key(page.Items[limit-1])})
|
||||
if err != nil {
|
||||
return Page[T]{}, err
|
||||
}
|
||||
cursor := base64.RawURLEncoding.EncodeToString(raw)
|
||||
page.NextCursor = &cursor
|
||||
return page, nil
|
||||
}
|
||||
func (m *PersistentMessenger) ListChats(ctx context.Context, actor string, limit int, cursor string) (Page[model.Chat], error) {
|
||||
scope := "chats:" + actor
|
||||
after, err := decodePosition(cursor, scope, limit)
|
||||
if err != nil {
|
||||
return Page[model.Chat]{}, err
|
||||
}
|
||||
items, err := m.db.ChatPage(ctx, actor, limit+1, after)
|
||||
if err != nil {
|
||||
return Page[model.Chat]{}, err
|
||||
}
|
||||
return databasePage(items, limit, scope, func(c model.Chat) position { return position{CreatedAt: c.CreatedAt, ID: c.ID} })
|
||||
}
|
||||
|
||||
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
func (m *PersistentMessenger) CreateMessage(ctx context.Context, actor, chatID, text, key string) (model.Message, error) {
|
||||
if !uuidPattern.MatchString(chatID) || !validText(text, 4000) || !idempotencyKeyPattern.MatchString(key) {
|
||||
return model.Message{}, ErrInvalidRequest
|
||||
}
|
||||
return m.db.CreateMessage(ctx, actor, strings.ToLower(chatID), text, key)
|
||||
}
|
||||
func (m *PersistentMessenger) ListMessages(ctx context.Context, actor, chatID string, limit int, cursor string) (Page[model.Message], error) {
|
||||
chat, err := m.GetChat(ctx, actor, chatID)
|
||||
if err != nil {
|
||||
return Page[model.Message]{}, err
|
||||
}
|
||||
scope := "messages:" + chat.ID
|
||||
after, err := decodePosition(cursor, scope, limit)
|
||||
if err != nil {
|
||||
return Page[model.Message]{}, err
|
||||
}
|
||||
items, err := m.db.MessagePage(ctx, chat.ID, limit+1, after)
|
||||
if err != nil {
|
||||
return Page[model.Message]{}, err
|
||||
}
|
||||
return databasePage(items, limit, scope, func(message model.Message) position { return position{CreatedAt: message.CreatedAt, ID: message.ID} })
|
||||
}
|
||||
Reference in New Issue
Block a user