package model import ( "slices" "time" "github.com/google/uuid" ) const maxChatMembers = 100 // Chat неизменяем после создания: участники задаются один раз, удаления в API нет. type Chat struct { ID uuid.UUID Title string MemberIDs []uuid.UUID CreatedAt time.Time } func (c Chat) HasMember(userID uuid.UUID) bool { return slices.Contains(c.MemberIDs, userID) } // ChatDraft — проверенные данные нового чата. Существование участников // проверяет хранилище атомарно с созданием. type ChatDraft struct { Creator uuid.UUID Title string MemberIDs []uuid.UUID } func NewChatDraft(creator uuid.UUID, title string, memberIDs []uuid.UUID) (ChatDraft, error) { if err := requireText("title", title, 1, 100); err != nil { return ChatDraft{}, err } if len(memberIDs) == 0 || len(memberIDs) > maxChatMembers { return ChatDraft{}, invalidf("member_ids: от 1 до %d участников", maxChatMembers) } seen := make(map[uuid.UUID]struct{}, len(memberIDs)) for _, id := range memberIDs { if _, dup := seen[id]; dup { return ChatDraft{}, invalidf("member_ids: повторяющийся участник") } seen[id] = struct{}{} } if _, ok := seen[creator]; !ok { return ChatDraft{}, invalidf("member_ids: должен содержать создателя чата") } return ChatDraft{Creator: creator, Title: title, MemberIDs: slices.Clone(memberIDs)}, nil } func (c Chat) Position() Position { return Position{CreatedAt: c.CreatedAt, ID: c.ID} }