# FreeLand Community chat server

The Community frontend is directly in this folder (`index.html`, `css/`, and `js/`). Its local Python API and SQLite storage live in `backend/`.

## Run locally

1. Open the website project root in VS Code.
2. Run the **Run FreeLand local server** task.
3. Open `http://127.0.0.1:8001/community/`.
4. Check `http://127.0.0.1:8001/api/health` for server status.

The first run creates `backend/data/community.sqlite3` and seeds three topic rooms with starter subrooms. Copy `backend/server.env.example` to `backend/server.env` for local overrides. Keep this prototype bound to `127.0.0.1`; it is not ready for public hosting.

## Chat and rooms

The sidebar shows topic rooms and their subrooms, ordered by recent activity. Search filters room names, topics, and descriptions. Signed-in members can create top-level rooms or one level of subrooms and send messages. Messages are stored in SQLite and the open room polls for new messages every few seconds.

| Method | Route | Purpose |
|---|---|---|
| `GET` | `/api/chat/rooms` | List rooms and recent activity |
| `POST` | `/api/chat/rooms` | Create a topic room or a subroom (sign-in required) |
| `GET` | `/api/chat/rooms/:id/messages?limit=100` | Read the latest room messages |
| `GET` | `/api/chat/rooms/:id/messages?after=:messageId` | Poll for newer messages |
| `POST` | `/api/chat/rooms/:id/messages` | Send a room message (sign-in required) |

Room creation JSON uses `name`, `topic`, `description`, and optional `parentId`. Message JSON uses `body`.

## Accounts and profile images

The board supports registration and sign-in with email or username, persistent session cookies, profile display name and bio, and profile images. Passwords are scrypt-hashed with per-account salts. Session tokens are random; only their SHA-256 hashes are saved. Cookies are `HttpOnly`, `SameSite=Lax`, and expire after 14 days by default.

Profile images are resized in the browser and saved in SQLite. PNG, JPEG, and WebP uploads are accepted up to 256 KB after resizing. The public image route is `GET /api/users/:id/avatar`.

Email verification and password recovery are not available because no mail provider is configured. Account sessions and login rate limiting are suitable for this local prototype only. Before public hosting, add production-grade deployment, HTTPS, persistent rate limits, email verification and recovery, moderation/report tools, backups, and monitoring.

## Database tables

- `users`: account details, password salt/hash, bio, avatar bytes, and avatar MIME type.
- `sessions`: hashed token and expiry.
- `rooms`: topic hierarchy, room descriptions, and last activity time.
- `chat_messages`: room, author, message body, and creation time.
- `categories`, `posts`, and `comments`: legacy discussion-board data retained in SQLite for migration/reference; the current chat UI uses rooms and messages.

All SQL statements use parameters. User text is rendered with `textContent`. The API only allows credentialed browser requests from same-host and localhost origins by default. For deployment, configure exact origins using `ALLOWED_ORIGINS`; do not use a wildcard with cookies. Keep secrets out of `js/api-config.js`.
