// Package model holds the public JSON shapes of the lab3 contract. They are // deliberately separate from the database scan helpers so an accidental new // column cannot silently become an API field. package model import "time" // Time marshals an RFC 3339 UTC timestamp with exactly six fractional digits. // Go's default time.Time JSON encoder trims trailing zeros, which produces // 3-5 digit fractions that some Python 3.9 datetime parsers (used by the // public test client) reject. A fixed microsecond precision matches // PostgreSQL timestamptz and stays valid RFC 3339. type Time struct{ time.Time } const timestampLayout = "2006-01-02T15:04:05.000000Z" // MarshalJSON implements json.Marshaler. func (t Time) MarshalJSON() ([]byte, error) { return []byte(`"` + t.UTC().Format(timestampLayout) + `"`), nil } // User is the public user shape. It never contains a password hash or session // data. type User struct { ID string `json:"id"` Username string `json:"username"` DisplayName string `json:"display_name"` CreatedAt Time `json:"created_at"` } // Chat is the public chat shape. MemberIDs preserves the order supplied at // creation; membership is immutable in the mandatory lab3 API. type Chat struct { ID string `json:"id"` Title string `json:"title"` MemberIDs []string `json:"member_ids"` CreatedAt Time `json:"created_at"` } // Message is the public message shape. SenderID always comes from the server // session, never from the request body. AttachmentIDs is always a JSON array, // never null, and preserves the order supplied by the client. type Message struct { ID string `json:"id"` ChatID string `json:"chat_id"` SenderID string `json:"sender_id"` Text string `json:"text"` CreatedAt Time `json:"created_at"` AttachmentIDs []string `json:"attachment_ids"` } // Variant is the public derivative shape. Lab4 never creates variants, so // every Attachment carries an empty array; the schema is kept for lab5. 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"` } // Attachment is the public metadata shape. Object keys, bucket names and // storage credentials are deliberately absent: they are server-side details. 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"` SizeBytes int64 `json:"size_bytes"` SHA256 string `json:"sha256"` Status string `json:"status"` Variants []Variant `json:"variants"` ErrorCode *string `json:"error_code"` CreatedAt Time `json:"created_at"` UpdatedAt Time `json:"updated_at"` } // ChatPage is one keyset page of accessible chats. type ChatPage struct { Items []Chat `json:"items"` NextCursor *string `json:"next_cursor"` } // MessagePage is one keyset page of a chat history. type MessagePage struct { Items []Message `json:"items"` NextCursor *string `json:"next_cursor"` } // Session is the login response. Token is only ever written to the response // body, never to logs or PostgreSQL. type Session struct { Token string `json:"token"` TokenType string `json:"token_type"` ExpiresAt Time `json:"expires_at"` User User `json:"user"` }