forked from mtusi2026/mtusi2026lab2
реализован уровень 3
This commit is contained in:
+46
-13
@@ -23,6 +23,13 @@ type User struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
User User `json:"user"`
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
@@ -71,6 +78,10 @@ func ValidateChatInput(title string, memberIDs []string, callerID string) error
|
||||
return fmt.Errorf("member_ids cannot be empty")
|
||||
}
|
||||
|
||||
if len(memberIDs) > 100 {
|
||||
return fmt.Errorf("member_ids must contain at most 100 users")
|
||||
}
|
||||
|
||||
hasCaller := false
|
||||
seen := make(map[string]bool)
|
||||
|
||||
@@ -78,6 +89,7 @@ func ValidateChatInput(title string, memberIDs []string, callerID string) error
|
||||
if _, err := uuid.Parse(id); err != nil {
|
||||
return fmt.Errorf("invalid member uuid: %s", id)
|
||||
}
|
||||
|
||||
if seen[id] {
|
||||
return fmt.Errorf("duplicate member_id: %s", id)
|
||||
}
|
||||
@@ -112,38 +124,59 @@ type CursorData struct {
|
||||
ChatID string
|
||||
}
|
||||
|
||||
// EncodeCursor превращает время, message_id и chat_id в непрозрачную base64 строку
|
||||
const messageCursorVersion = "v1"
|
||||
|
||||
func EncodeCursor(createdAt time.Time, messageID, chatID string) string {
|
||||
raw := fmt.Sprintf("%s|%s|%s", createdAt.Format(time.RFC3339Nano), messageID, chatID)
|
||||
return base64.StdEncoding.EncodeToString([]byte(raw))
|
||||
raw := fmt.Sprintf(
|
||||
"%s|%s|%s|%s",
|
||||
messageCursorVersion,
|
||||
createdAt.UTC().Format(time.RFC3339Nano),
|
||||
messageID,
|
||||
chatID,
|
||||
)
|
||||
|
||||
return base64.RawURLEncoding.EncodeToString([]byte(raw))
|
||||
}
|
||||
|
||||
// DecodeCursor декодирует строку курсора и проверяет, что он принадлежит именно этому чату
|
||||
func DecodeCursor(cursorStr string, expectedChatID string) (*CursorData, error) {
|
||||
bytes, err := base64.StdEncoding.DecodeString(cursorStr)
|
||||
func DecodeCursor(
|
||||
cursorStr string,
|
||||
expectedChatID string,
|
||||
) (*CursorData, error) {
|
||||
raw, err := base64.RawURLEncoding.DecodeString(cursorStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid base64 cursor")
|
||||
}
|
||||
|
||||
parts := strings.Split(string(bytes), "|")
|
||||
if len(parts) != 3 {
|
||||
parts := strings.Split(string(raw), "|")
|
||||
if len(parts) != 4 {
|
||||
return nil, fmt.Errorf("malformed cursor")
|
||||
}
|
||||
|
||||
t, err := time.Parse(time.RFC3339Nano, parts[0])
|
||||
if parts[0] != messageCursorVersion {
|
||||
return nil, fmt.Errorf("unsupported cursor version")
|
||||
}
|
||||
|
||||
createdAt, err := time.Parse(time.RFC3339Nano, parts[1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid cursor timestamp")
|
||||
}
|
||||
|
||||
chatID := parts[2]
|
||||
//защита из контракта: курсор от другого чата должен давать 400
|
||||
messageID := parts[2]
|
||||
chatID := parts[3]
|
||||
|
||||
if _, err := uuid.Parse(messageID); err != nil {
|
||||
return nil, fmt.Errorf("invalid cursor message_id")
|
||||
}
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
return nil, fmt.Errorf("invalid cursor chat_id")
|
||||
}
|
||||
if chatID != expectedChatID {
|
||||
return nil, fmt.Errorf("cursor belongs to another chat")
|
||||
}
|
||||
|
||||
return &CursorData{
|
||||
CreatedAt: t,
|
||||
MessageID: parts[1],
|
||||
CreatedAt: createdAt.UTC(),
|
||||
MessageID: messageID,
|
||||
ChatID: chatID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user