feat: port messenger from lab 5
This commit is contained in:
@@ -4,11 +4,28 @@ CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
|
||||
CLEANUP_ARGS ?= --dry-run
|
||||
SERVICES ?=
|
||||
JOB_ID ?=
|
||||
ATTACHMENT_ID ?=
|
||||
REASON ?=
|
||||
export JOB_ID ATTACHMENT_ID REASON
|
||||
|
||||
COMPOSE = docker compose --project-name highload-lab6 --env-file .env -f compose.yml
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover
|
||||
.PHONY: app-init app-build app-up app-start app-stop app-down app-restart app-logs app-status
|
||||
.PHONY: app-migrate app-s3-bootstrap worker-start worker-stop queue-status attachments-cleanup
|
||||
.PHONY: jobs-failed jobs-replay jobs-upgrade jobs-cleanup-staging
|
||||
.PHONY: test-backend test-pipeline frontend-build
|
||||
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
|
||||
@echo "app-init / app-build / app-up: configure, build, start the stack"
|
||||
@echo "app-status / app-logs / app-down: inspect or stop the stack"
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
|
||||
@@ -28,3 +45,72 @@ test-persistence:
|
||||
test-failover:
|
||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
app-init:
|
||||
$(PYTHON) scripts/init_env.py
|
||||
|
||||
app-build: app-init
|
||||
$(COMPOSE) build
|
||||
|
||||
app-up: app-init
|
||||
$(COMPOSE) up -d --build --wait
|
||||
|
||||
app-start: app-init
|
||||
$(COMPOSE) up -d --no-build --wait
|
||||
|
||||
app-stop:
|
||||
$(COMPOSE) stop
|
||||
|
||||
app-down:
|
||||
$(COMPOSE) down
|
||||
|
||||
app-restart: app-init
|
||||
./scripts/restart.sh
|
||||
|
||||
app-logs:
|
||||
$(COMPOSE) logs -f --tail=100 $(SERVICES)
|
||||
|
||||
app-status:
|
||||
$(COMPOSE) ps --all
|
||||
|
||||
app-migrate: app-init
|
||||
$(COMPOSE) run --rm migrate
|
||||
|
||||
app-s3-bootstrap: app-init
|
||||
$(COMPOSE) run --rm s3-bootstrap
|
||||
|
||||
worker-start:
|
||||
$(COMPOSE) start worker
|
||||
|
||||
worker-stop:
|
||||
$(COMPOSE) stop worker
|
||||
|
||||
queue-status:
|
||||
$(COMPOSE) exec -T broker rabbitmqctl -q list_queues name messages_ready messages_unacknowledged --formatter json
|
||||
|
||||
attachments-cleanup:
|
||||
$(COMPOSE) run --rm --no-deps upload-cleanup ./messenger attachments-cleanup $(CLEANUP_ARGS)
|
||||
|
||||
jobs-failed:
|
||||
$(COMPOSE) exec -T dispatcher ./messenger jobs list-failed
|
||||
|
||||
jobs-replay:
|
||||
@test -n "$$JOB_ID" && test -n "$$REASON" || { echo "Set JOB_ID=... REASON='...'"; exit 2; }
|
||||
$(COMPOSE) exec -T dispatcher ./messenger jobs replay "$$JOB_ID" "$$REASON"
|
||||
|
||||
jobs-upgrade:
|
||||
@test -n "$$ATTACHMENT_ID" && test -n "$$REASON" || { echo "Set ATTACHMENT_ID=... REASON='...'"; exit 2; }
|
||||
$(COMPOSE) exec -T dispatcher ./messenger jobs upgrade "$$ATTACHMENT_ID" 2 "$$REASON"
|
||||
|
||||
jobs-cleanup-staging:
|
||||
@test -n "$$JOB_ID" || { echo "Set JOB_ID=..."; exit 2; }
|
||||
$(COMPOSE) exec -T dispatcher ./messenger jobs cleanup-staging "$$JOB_ID"
|
||||
|
||||
test-backend: app-init
|
||||
$(COMPOSE) --profile test run --rm --build backend-tests
|
||||
|
||||
test-pipeline: app-init
|
||||
$(COMPOSE) --profile test run --rm --build backend-tests go test -race -count=1 -v ./backend/tests ./backend/workers -run 'TestPipeline|TestVersionedRendering|TestThumbnail|TestOptimized'
|
||||
|
||||
frontend-build:
|
||||
$(COMPOSE) exec -T frontend npm run build
|
||||
|
||||
+333
@@ -0,0 +1,333 @@
|
||||
name: highload-lab6
|
||||
|
||||
services:
|
||||
redis:
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
image: redis:7.4.11-bookworm
|
||||
command:
|
||||
[
|
||||
"redis-server",
|
||||
"--appendonly",
|
||||
"yes",
|
||||
"--appendfsync",
|
||||
"everysec",
|
||||
"--dir",
|
||||
"/data",
|
||||
]
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
db:
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
networks: [data]
|
||||
image: postgres:16.10-bookworm
|
||||
environment:
|
||||
POSTGRES_DB: ${POSTGRES_DB}
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
migrate:
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
build: ./src
|
||||
environment:
|
||||
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||
DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
|
||||
volumes:
|
||||
- ./secrets/database_url:${DATABASE_URL_FILE:-/run/secrets/database_url}:ro
|
||||
command: ["./messenger", "migrate", "up"]
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
s3:
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
networks: [edge, data]
|
||||
ports:
|
||||
- "127.0.0.1:9000:9000"
|
||||
build:
|
||||
context: ./infra
|
||||
dockerfile: minio.Dockerfile
|
||||
environment:
|
||||
MINIO_ROOT_USER_FILE: /run/secrets/s3_access_key_id
|
||||
MINIO_ROOT_PASSWORD_FILE: /run/secrets/s3_secret_access_key
|
||||
MINIO_BROWSER: "off"
|
||||
MINIO_API_CORS_ALLOW_ORIGIN: ${S3_CORS_ORIGINS:-http://127.0.0.1:5173,http://localhost:5173}
|
||||
volumes:
|
||||
- s3_data:/data
|
||||
- ./secrets/s3_access_key_id:/run/secrets/s3_access_key_id:ro
|
||||
- ./secrets/s3_secret_access_key:/run/secrets/s3_secret_access_key:ro
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://127.0.0.1:9000/minio/health/ready"]
|
||||
interval: 3s
|
||||
timeout: 2s
|
||||
retries: 30
|
||||
s3-bootstrap:
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
build: ./src
|
||||
environment: &s3-environment
|
||||
S3_ROOT_ACCESS_KEY_ID_FILE: /run/secrets/s3_root_access_key_id
|
||||
S3_ROOT_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_root_secret_access_key
|
||||
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:9000}
|
||||
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-http://127.0.0.1:9000}
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID_FILE: ${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}
|
||||
S3_SECRET_ACCESS_KEY_FILE: ${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}
|
||||
volumes:
|
||||
- ./secrets/s3_access_key_id:/run/secrets/s3_root_access_key_id:ro
|
||||
- ./secrets/s3_secret_access_key:/run/secrets/s3_root_secret_access_key:ro
|
||||
- ./secrets/s3_api_access_key_id:${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}:ro
|
||||
- ./secrets/s3_api_secret_access_key:${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}:ro
|
||||
command: ["./messenger", "s3-bootstrap"]
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
api:
|
||||
cpus: 0.5
|
||||
mem_limit: 512m
|
||||
networks: [proxy, data]
|
||||
build: ./src
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
stop_grace_period: 15s
|
||||
environment: &api-environment
|
||||
<<: *s3-environment
|
||||
S3_ROOT_ACCESS_KEY_ID_FILE: ""
|
||||
S3_ROOT_SECRET_ACCESS_KEY_FILE: ""
|
||||
BROKER_URL_FILE: /run/secrets/broker_url
|
||||
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
|
||||
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60}
|
||||
WORKER_MAX_PIXELS: ${WORKER_MAX_PIXELS:-8000000}
|
||||
UPLOAD_TMP_DIR: /tmp/uploads
|
||||
UPLOAD_CLEANUP_GRACE_SECONDS: ${UPLOAD_CLEANUP_GRACE_SECONDS:-600}
|
||||
# Go 1.22 sees host CPUs; keep parallelism suitable for this 0.5 CPU limit.
|
||||
GOMAXPROCS: ${API_GOMAXPROCS:-1}
|
||||
GOMEMLIMIT: 384MiB
|
||||
APP_HOST: ${APP_HOST:-0.0.0.0}
|
||||
APP_PORT: ${APP_PORT:-8080}
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||
CACHE_URL_FILE: ${CACHE_URL_FILE:-/run/secrets/cache_url}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
REDIS_ADDR: redis:6379
|
||||
DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
|
||||
tmpfs:
|
||||
- /tmp/uploads:rw,noexec,nosuid,size=16m,mode=1777
|
||||
volumes: &api-volumes
|
||||
- ./secrets/s3_api_access_key_id:${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}:ro
|
||||
- ./secrets/s3_api_secret_access_key:${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}:ro
|
||||
- ./secrets/broker_url:/run/secrets/broker_url:ro
|
||||
- ./secrets/database_url:${DATABASE_URL_FILE:-/run/secrets/database_url}:ro
|
||||
- ./secrets/cache_url:${CACHE_URL_FILE:-/run/secrets/cache_url}:ro
|
||||
depends_on:
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
db:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ["CMD", "./messenger", "healthcheck"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
broker:
|
||||
image: rabbitmq:4.2.0-management
|
||||
hostname: broker
|
||||
environment:
|
||||
RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS: "+S 2:2"
|
||||
RABBITMQ_CTL_ERL_ARGS: "+S 1:1"
|
||||
cpus: 0.5
|
||||
mem_limit: 512m
|
||||
networks: [data]
|
||||
command:
|
||||
- sh
|
||||
- -ec
|
||||
- |
|
||||
export RABBITMQ_DEFAULT_USER="$$(cat /run/secrets/broker_user)"
|
||||
export RABBITMQ_DEFAULT_PASS="$$(cat /run/secrets/broker_password)"
|
||||
exec docker-entrypoint.sh rabbitmq-server
|
||||
volumes:
|
||||
- rabbitmq_data:/var/lib/rabbitmq
|
||||
- ./secrets/broker_user:/run/secrets/broker_user:ro
|
||||
- ./secrets/broker_password:/run/secrets/broker_password:ro
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "rabbitmq-diagnostics -q check_running && rabbitmq-diagnostics -q check_port_connectivity"]
|
||||
interval: 5s
|
||||
timeout: 10s
|
||||
start_period: 20s
|
||||
retries: 30
|
||||
worker:
|
||||
tmpfs:
|
||||
- /tmp:rw,noexec,nosuid,size=64m,mode=1777
|
||||
build: ./src
|
||||
restart: unless-stopped
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
networks: [data]
|
||||
environment: *api-environment
|
||||
volumes: *api-volumes
|
||||
command: ["./messenger", "worker"]
|
||||
healthcheck:
|
||||
disable: true
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
broker:
|
||||
condition: service_healthy
|
||||
dispatcher:
|
||||
build: ./src
|
||||
restart: unless-stopped
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
environment: *api-environment
|
||||
volumes: *api-volumes
|
||||
command: ["./messenger", "dispatcher"]
|
||||
healthcheck:
|
||||
disable: true
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
broker:
|
||||
condition: service_healthy
|
||||
upload-cleanup:
|
||||
restart: unless-stopped
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
build: ./src
|
||||
command: ["./messenger", "attachments-cleanup", "--loop"]
|
||||
environment:
|
||||
<<: *s3-environment
|
||||
S3_ROOT_ACCESS_KEY_ID_FILE: ""
|
||||
S3_ROOT_SECRET_ACCESS_KEY_FILE: ""
|
||||
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||
volumes:
|
||||
- ./secrets/s3_api_access_key_id:${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}:ro
|
||||
- ./secrets/s3_api_secret_access_key:${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}:ro
|
||||
- ./secrets/database_url:${DATABASE_URL_FILE:-/run/secrets/database_url}:ro
|
||||
healthcheck:
|
||||
disable: true
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
haproxy:
|
||||
environment:
|
||||
APP_PORT: ${APP_PORT:-8080}
|
||||
API_REPLICAS: ${API_REPLICAS:-2}
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
networks: [edge, proxy]
|
||||
image: haproxy:3.2.25-alpine
|
||||
ports:
|
||||
- "127.0.0.1:${API_HTTP_PORT:-8080}:8080"
|
||||
- "127.0.0.1:8404:8404"
|
||||
volumes:
|
||||
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD",
|
||||
"wget",
|
||||
"-q",
|
||||
"-O",
|
||||
"/dev/null",
|
||||
"http://127.0.0.1:8080/health/ready",
|
||||
]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
backend-tests:
|
||||
cpus: 2.0
|
||||
mem_limit: 1g
|
||||
networks: [data]
|
||||
profiles: ["test"]
|
||||
build:
|
||||
context: ./src
|
||||
target: builder
|
||||
environment:
|
||||
<<: *s3-environment
|
||||
S3_PUBLIC_ENDPOINT: http://s3:9000
|
||||
TEST_DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
|
||||
TEST_REDIS_ADDR: redis:6379
|
||||
volumes:
|
||||
- ./secrets/s3_api_access_key_id:${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}:ro
|
||||
- ./secrets/s3_api_secret_access_key:${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}:ro
|
||||
- ./secrets/s3_access_key_id:/run/secrets/s3_root_access_key_id:ro
|
||||
- ./secrets/s3_secret_access_key:/run/secrets/s3_root_secret_access_key:ro
|
||||
depends_on:
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
db:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
command: ["go", "test", "-race", "-count=1", "-v", "./backend/..."]
|
||||
frontend:
|
||||
cpus: 0.5
|
||||
mem_limit: 512m
|
||||
networks: [edge]
|
||||
build:
|
||||
context: ./src/frontend
|
||||
environment:
|
||||
API_PROXY_TARGET: http://haproxy:8080
|
||||
ports:
|
||||
- "127.0.0.1:5173:5173"
|
||||
volumes:
|
||||
- ./src/frontend:/app
|
||||
- /app/node_modules
|
||||
depends_on:
|
||||
haproxy:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD",
|
||||
"node",
|
||||
"-e",
|
||||
"fetch('http://127.0.0.1:5173/').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))",
|
||||
]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
|
||||
volumes:
|
||||
rabbitmq_data:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
s3_data:
|
||||
|
||||
networks:
|
||||
edge:
|
||||
proxy:
|
||||
internal: true
|
||||
data:
|
||||
internal: true
|
||||
@@ -0,0 +1,49 @@
|
||||
global
|
||||
# Operator access only inside this container; no host port is published.
|
||||
stats socket ipv4@127.0.0.1:9999 level admin
|
||||
log stdout format raw local0
|
||||
maxconn 1024
|
||||
|
||||
defaults
|
||||
log global
|
||||
mode http
|
||||
option httplog
|
||||
timeout connect 1s
|
||||
timeout client 35s
|
||||
timeout server 35s
|
||||
timeout check 1s
|
||||
# Let the client decide which failed POST is safe to repeat.
|
||||
retries 0
|
||||
|
||||
# Docker returns the current addresses of all containers of the api service.
|
||||
resolvers docker
|
||||
nameserver docker_dns 127.0.0.11:53
|
||||
accepted_payload_size 8192
|
||||
resolve_retries 3
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 1s
|
||||
hold obsolete 1s
|
||||
hold nx 1s
|
||||
hold timeout 1s
|
||||
hold refused 1s
|
||||
hold other 1s
|
||||
|
||||
frontend api_entry
|
||||
bind :8080
|
||||
default_backend api_replicas
|
||||
|
||||
backend api_replicas
|
||||
balance roundrobin
|
||||
option httpchk GET /health/ready
|
||||
http-check expect status 200
|
||||
# Separate slots receive distinct Docker DNS addresses. No sticky sessions.
|
||||
server-template api "${API_REPLICAS}" "api:${APP_PORT}" check inter 1s fall 2 rise 2 resolvers docker resolve-prefer ipv4 init-addr libc,none
|
||||
|
||||
# Local teaching aid: backend addresses, UP/DOWN state and request counters.
|
||||
listen statistics
|
||||
bind :8404
|
||||
stats enable
|
||||
stats show-legends
|
||||
stats uri /stats
|
||||
stats refresh 2s
|
||||
@@ -0,0 +1,19 @@
|
||||
FROM golang:1.24.2-bookworm AS builder
|
||||
WORKDIR /source
|
||||
RUN git init . \
|
||||
&& git remote add origin https://github.com/minio/minio.git \
|
||||
&& git fetch --depth 1 origin 0d7408fc9969caf07de6a8c3a84f9fbb10a6739e \
|
||||
&& git checkout --detach FETCH_HEAD
|
||||
RUN CGO_ENABLED=0 go build -trimpath \
|
||||
-ldflags="-s -w -X github.com/minio/minio/cmd.Version=2025-04-22T22:12:26Z -X github.com/minio/minio/cmd.ReleaseTag=RELEASE.2025-04-22T22-12-26Z -X github.com/minio/minio/cmd.CommitID=0d7408fc9969caf07de6a8c3a84f9fbb10a6739e" \
|
||||
-o /out/minio .
|
||||
|
||||
FROM debian:12.15-slim
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& groupadd --system storage && useradd --system --gid storage storage \
|
||||
&& mkdir /data && chown storage:storage /data
|
||||
COPY --from=builder /out/minio /usr/local/bin/minio
|
||||
USER storage:storage
|
||||
ENTRYPOINT ["minio"]
|
||||
CMD ["server", "/data", "--console-address", ":9001"]
|
||||
@@ -0,0 +1,122 @@
|
||||
import os
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def read_values(path):
|
||||
values = {}
|
||||
if path.exists():
|
||||
for line in path.read_text(encoding="utf-8").splitlines():
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#") and "=" in line:
|
||||
key, value = line.split("=", 1)
|
||||
values[key.strip()] = value.strip().strip("\"'")
|
||||
return values
|
||||
|
||||
|
||||
def main():
|
||||
path = ROOT / ".env"
|
||||
existing = read_values(path)
|
||||
defaults = read_values(ROOT / ".env.example")
|
||||
defaults.update(
|
||||
POSTGRES_DB="messenger",
|
||||
POSTGRES_USER="messenger",
|
||||
POSTGRES_PASSWORD=secrets.token_hex(24),
|
||||
)
|
||||
defaults.update(API_HTTP_PORT="8080", UPLOAD_CLEANUP_GRACE_SECONDS="600", UPLOAD_TMP_DIR="/tmp/uploads",
|
||||
S3_PUBLIC_ENDPOINT="http://127.0.0.1:9000",
|
||||
S3_CORS_ORIGINS="http://127.0.0.1:5173,http://localhost:5173")
|
||||
values = defaults | existing
|
||||
for name, lower, upper in (
|
||||
("APP_PORT", 1, 65535),
|
||||
("API_REPLICAS", 2, 32),
|
||||
("SESSION_TTL_SECONDS", 1, 31536000),
|
||||
("IDEMPOTENCY_TTL_SECONDS", 1, 31536000),
|
||||
):
|
||||
try:
|
||||
valid = lower <= int(values[name]) <= upper
|
||||
except (ValueError, KeyError):
|
||||
valid = False
|
||||
if not valid:
|
||||
raise SystemExit(f"{name} must be between {lower} and {upper}")
|
||||
for name in (
|
||||
"POSTGRES_DB",
|
||||
"POSTGRES_USER",
|
||||
"POSTGRES_PASSWORD",
|
||||
"APP_HOST",
|
||||
"APP_ENV",
|
||||
):
|
||||
if not values[name]:
|
||||
raise SystemExit(f"{name} must not be empty")
|
||||
# Existing values, including credentials, are never replaced.
|
||||
missing = {key: value for key, value in values.items() if key not in existing}
|
||||
if missing:
|
||||
prefix = "\n" if path.exists() and path.stat().st_size else ""
|
||||
with path.open("a", encoding="utf-8") as file:
|
||||
file.write(
|
||||
prefix + "".join(f"{key}={value}\n" for key, value in missing.items())
|
||||
)
|
||||
os.chmod(path, 0o600)
|
||||
secret_dir = ROOT / "secrets"
|
||||
secret_dir.mkdir(mode=0o700, exist_ok=True)
|
||||
database_url = "postgres://{}:{}@db:5432/{}?sslmode=disable".format(
|
||||
quote(values["POSTGRES_USER"], safe=""),
|
||||
quote(values["POSTGRES_PASSWORD"], safe=""),
|
||||
quote(values["POSTGRES_DB"], safe=""),
|
||||
)
|
||||
for filename,content in (("broker_user","messenger"),("broker_password",secrets.token_hex(32))):
|
||||
target=secret_dir/filename
|
||||
if not target.exists():
|
||||
target.write_text(content+"\n",encoding="utf-8")
|
||||
os.chmod(target,0o644)
|
||||
broker_url="amqp://{}:{}@broker:5672/%2F".format(
|
||||
quote((secret_dir/"broker_user").read_text().strip(),safe=""),
|
||||
quote((secret_dir/"broker_password").read_text().strip(),safe=""))
|
||||
# Migrate only the generated legacy Redis URL; preserve custom AMQP settings.
|
||||
broker_file=secret_dir/"broker_url"
|
||||
if broker_file.exists() and broker_file.read_text().strip()=="redis://broker:6379/0":
|
||||
broker_file.write_text(broker_url+"\n",encoding="utf-8")
|
||||
for name, content in (
|
||||
("DATABASE_URL_FILE", database_url),
|
||||
("CACHE_URL_FILE", "redis://redis:6379/0"),
|
||||
("BROKER_URL_FILE", broker_url),
|
||||
("S3_ACCESS_KEY_ID_FILE", secrets.token_hex(12)),
|
||||
("S3_SECRET_ACCESS_KEY_FILE", secrets.token_hex(32)),
|
||||
):
|
||||
container_path = Path(values[name])
|
||||
if container_path.parent != Path("/run/secrets") or not container_path.name:
|
||||
raise SystemExit(f"{name} must name a file directly inside /run/secrets")
|
||||
target = (
|
||||
secret_dir
|
||||
/ {
|
||||
"DATABASE_URL_FILE": "database_url",
|
||||
"CACHE_URL_FILE": "cache_url",
|
||||
"BROKER_URL_FILE": "broker_url",
|
||||
"S3_ACCESS_KEY_ID_FILE": "s3_access_key_id",
|
||||
"S3_SECRET_ACCESS_KEY_FILE": "s3_secret_access_key",
|
||||
}[name]
|
||||
)
|
||||
if not target.exists():
|
||||
# Containers run as an unprivileged user; the private host directory
|
||||
# protects the files, while read permission lets the API consume them.
|
||||
with target.open("x", encoding="utf-8") as file:
|
||||
file.write(content + "\n")
|
||||
os.chmod(target, 0o644)
|
||||
# Existing S3 files remain the root credentials; API gets a new, separate identity.
|
||||
for filename, content in (
|
||||
("s3_api_access_key_id", secrets.token_hex(12)),
|
||||
("s3_api_secret_access_key", secrets.token_hex(32)),
|
||||
):
|
||||
target = secret_dir / filename
|
||||
if not target.exists():
|
||||
with target.open("x", encoding="utf-8") as file:
|
||||
file.write(content + "\n")
|
||||
os.chmod(target, 0o644)
|
||||
print("Local .env and connection files ready; existing values and files preserved.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose --project-name highload-lab6 --env-file .env -f compose.yml restart db redis s3 broker >/dev/null
|
||||
docker compose --project-name highload-lab6 --env-file .env -f compose.yml up -d --wait db redis s3 broker >/dev/null
|
||||
docker compose --project-name highload-lab6 --env-file .env -f compose.yml restart api upload-cleanup worker dispatcher >/dev/null
|
||||
docker compose --project-name highload-lab6 --env-file .env -f compose.yml up -d --wait >/dev/null
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?Set TARGET_INSTANCE_ID to the X-Instance-Id of an API replica}"
|
||||
for container in $(docker compose --project-name highload-lab6 --env-file .env -f compose.yml ps -q api); do
|
||||
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container")
|
||||
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
|
||||
# Only containers from this Compose project's api service are eligible.
|
||||
docker stop --timeout 8 "$container" >/dev/null
|
||||
printf 'Stopped API instance %s\n' "$hostname"
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
printf 'No running API replica matches TARGET_INSTANCE_ID\n' >&2
|
||||
exit 1
|
||||
@@ -0,0 +1,26 @@
|
||||
.git
|
||||
.gitignore
|
||||
.github
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
secrets
|
||||
certs
|
||||
volumes
|
||||
data
|
||||
backups
|
||||
*.key
|
||||
*.pem
|
||||
*.p12
|
||||
*.pfx
|
||||
*.jks
|
||||
.venv
|
||||
__pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
README.md
|
||||
REPORT.md
|
||||
COURSE.md
|
||||
CONTRIBUTING.md
|
||||
MAINTAINERS.md
|
||||
@@ -0,0 +1,42 @@
|
||||
FROM golang:1.22.2-bookworm AS builder
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends webp && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
RUN go mod download
|
||||
|
||||
COPY backend ./backend
|
||||
|
||||
RUN CGO_ENABLED=0 \
|
||||
GOOS=linux \
|
||||
go build \
|
||||
-trimpath \
|
||||
-ldflags="-s -w" \
|
||||
-o /out/messenger \
|
||||
./backend
|
||||
|
||||
|
||||
FROM debian:12.15-slim
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends webp && rm -rf /var/lib/apt/lists/* \
|
||||
&& groupadd --system app \
|
||||
&& useradd --system --gid app --no-create-home app
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=builder /out/messenger ./messenger
|
||||
|
||||
ENV APP_HOST=0.0.0.0
|
||||
ENV APP_PORT=8080
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD ["./messenger", "healthcheck"]
|
||||
|
||||
USER app:app
|
||||
|
||||
CMD ["./messenger"]
|
||||
@@ -0,0 +1,140 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
func serverAddress() string {
|
||||
host := os.Getenv("APP_HOST")
|
||||
if host == "" {
|
||||
host = "0.0.0.0"
|
||||
}
|
||||
|
||||
port := os.Getenv("APP_PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
return net.JoinHostPort(host, port)
|
||||
}
|
||||
|
||||
func RunHTTP() error {
|
||||
if _, err := repositories.UploadCleanupGrace(); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
db, err := repositories.OpenDatabase(ctx)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return fmt.Errorf("database initialization failed: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
// Opt-in experiment telemetry goes to container logs, never a public port.
|
||||
if os.Getenv("BENCH_POOL_STATS") == "1" {
|
||||
go func() {
|
||||
for range time.Tick(time.Second) {
|
||||
s := db.Stat()
|
||||
data, _ := json.Marshal(map[string]any{
|
||||
"time": time.Now().UTC(), "acquired": s.AcquiredConns(),
|
||||
"total": s.TotalConns(), "max": s.MaxConns(),
|
||||
"gomaxprocs": runtime.GOMAXPROCS(0), "goroutines": runtime.NumGoroutine(),
|
||||
"acquire_count": s.AcquireCount(), "acquire_duration_ns": s.AcquireDuration().Nanoseconds(),
|
||||
"empty_acquire_count": s.EmptyAcquireCount(), "canceled_acquire_count": s.CanceledAcquireCount(),
|
||||
})
|
||||
log.Printf("benchmark_pool %s", data)
|
||||
}
|
||||
}()
|
||||
}
|
||||
redisClient, err := repositories.RedisClientFromEnvironment()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer redisClient.Close()
|
||||
sessionStore := repositories.NewSessionStore(redisClient, "messenger:session:")
|
||||
userStore := repositories.NewUserStore(db)
|
||||
chatStore := repositories.NewChatStore(db)
|
||||
idempotencyTTL, err := repositories.IdempotencyTTL()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
messageStore := repositories.NewMessageStoreWithTTL(db, idempotencyTTL)
|
||||
ttl, err := services.SessionTTL()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userService := services.NewUserService(userStore)
|
||||
sessionService := services.NewSessionService(userService, sessionStore, ttl)
|
||||
chatService := services.NewChatService(chatStore)
|
||||
messageService := services.NewMessageService(messageStore)
|
||||
objects, err := repositories.NewObjectStoreFromEnvironment()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
attachmentService := services.NewAttachmentService(repositories.NewAttachmentStore(db), objects)
|
||||
mux := handlers.NewRouter(services.NewHealthService(repositories.NewHealthStore(db), sessionStore, objects), userService, sessionService, chatService, messageService, attachmentService)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: serverAddress(),
|
||||
Handler: mux,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
shutdownContext, stop := signal.NotifyContext(
|
||||
context.Background(),
|
||||
os.Interrupt,
|
||||
syscall.SIGTERM,
|
||||
)
|
||||
defer stop()
|
||||
|
||||
serverErrors := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
log.Printf("server is listening on %s", server.Addr)
|
||||
|
||||
if err := server.ListenAndServe(); err != nil &&
|
||||
!errors.Is(err, http.ErrServerClosed) {
|
||||
serverErrors <- err
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-serverErrors:
|
||||
log.Printf("server error: %v", err)
|
||||
|
||||
case <-shutdownContext.Done():
|
||||
log.Printf("shutdown signal received")
|
||||
handlers.BeginShutdown()
|
||||
|
||||
shutdownDeadline, cancel := context.WithTimeout(
|
||||
context.Background(),
|
||||
10*time.Second,
|
||||
)
|
||||
defer cancel()
|
||||
|
||||
if err := server.Shutdown(shutdownDeadline); err != nil {
|
||||
log.Printf("graceful shutdown failed: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Printf("server stopped gracefully")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func runUploadCleanup(args []string) error {
|
||||
flags := flag.NewFlagSet("attachments-cleanup", flag.ContinueOnError)
|
||||
loop := flags.Bool("loop", false, "run every 60 seconds")
|
||||
dryRun := flags.Bool("dry-run", false, "report without deleting anything")
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
init, cancel := context.WithTimeout(ctx, 20*time.Second)
|
||||
db, err := repositories.OpenDatabase(init)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
objects, err := repositories.NewObjectStoreFromEnvironment()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
store := repositories.NewAttachmentStore(db)
|
||||
for {
|
||||
pass, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
result, err := store.CleanupUploads(pass, *dryRun, objects.Delete)
|
||||
direct, directErr := store.CleanupDirectUploads(pass, *dryRun, objects.Delete)
|
||||
jobs, jobsErr := store.CleanupJobs(pass, 24*time.Hour, *dryRun, objects.Delete)
|
||||
cancel()
|
||||
if encodeErr := json.NewEncoder(os.Stdout).Encode(map[string]any{"originals": result, "direct": direct, "job_staging": jobs}); encodeErr != nil {
|
||||
return encodeErr
|
||||
}
|
||||
if !*loop {
|
||||
if err == nil {
|
||||
err = directErr
|
||||
if err == nil {
|
||||
err = jobsErr
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
if err != nil || directErr != nil || jobsErr != nil {
|
||||
log.Print("upload cleanup pass failed; journal retained for retry")
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case <-time.After(60 * time.Second):
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func runJobAdmin(args []string) error {
|
||||
if len(args) < 1 {
|
||||
return errors.New("usage: jobs list-failed | replay JOB_ID REASON | cleanup-staging JOB_ID | upgrade ATTACHMENT_ID 2 REASON")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
db, err := repositories.OpenDatabase(ctx)
|
||||
if err != nil {
|
||||
return errors.New("database unavailable")
|
||||
}
|
||||
defer db.Close()
|
||||
store := repositories.NewAttachmentStore(db)
|
||||
if args[0] == "list-failed" && len(args) == 1 {
|
||||
jobs, e := store.FailedJobs(ctx)
|
||||
if e != nil {
|
||||
return errors.New("cannot read failed registry")
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(jobs)
|
||||
}
|
||||
if len(args) < 2 {
|
||||
return errors.New("job id required")
|
||||
}
|
||||
if _, err = uuid.Parse(args[1]); err != nil {
|
||||
return errors.New("invalid job id")
|
||||
}
|
||||
objects, err := repositories.NewObjectStoreFromEnvironment()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var changed bool
|
||||
switch {
|
||||
case args[0] == "upgrade" && len(args) == 4:
|
||||
version, e := strconv.Atoi(args[2])
|
||||
if e != nil {
|
||||
return errors.New("invalid pipeline version")
|
||||
}
|
||||
changed, err = store.UpgradePipeline(ctx, args[1], version, args[3], func(ctx context.Context, key string) error {
|
||||
r, e := objects.Get(ctx, key)
|
||||
if e == nil {
|
||||
r.Close()
|
||||
}
|
||||
return e
|
||||
})
|
||||
case args[0] == "replay" && len(args) == 3:
|
||||
changed, err = store.ReplayJob(ctx, args[1], args[2], func(ctx context.Context, key string) error {
|
||||
r, e := objects.Get(ctx, key)
|
||||
if e == nil {
|
||||
r.Close()
|
||||
}
|
||||
return e
|
||||
})
|
||||
case args[0] == "cleanup-staging" && len(args) == 2:
|
||||
changed, err = store.CleanupJobStaging(ctx, args[1], 24*time.Hour, false, objects.Delete)
|
||||
default:
|
||||
return errors.New("usage: jobs list-failed | replay JOB_ID REASON | cleanup-staging JOB_ID | upgrade ATTACHMENT_ID 2 REASON")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(map[string]bool{"changed": changed})
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/app"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/workers"
|
||||
)
|
||||
|
||||
// Run dispatches the existing executable modes; the binary and CLI stay the same.
|
||||
func Run(args []string) error {
|
||||
if len(args) == 0 {
|
||||
if environment := os.Getenv("APP_ENV"); environment != "" {
|
||||
log.SetPrefix("[" + environment + "] ")
|
||||
}
|
||||
return app.RunHTTP()
|
||||
}
|
||||
switch args[0] {
|
||||
case "image-process":
|
||||
return workers.RunImageChild(args[1:])
|
||||
case "jobs":
|
||||
return runJobAdmin(args[1:])
|
||||
case "worker", "dispatcher", "broker-publish":
|
||||
return workers.RunPipeline(args[0], args[1:])
|
||||
}
|
||||
if environment := os.Getenv("APP_ENV"); environment != "" {
|
||||
log.SetPrefix("[" + environment + "] ")
|
||||
}
|
||||
switch {
|
||||
case args[0] == "s3-bootstrap" && len(args) == 1:
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
if err := repositories.BootstrapS3(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Print("private S3 bucket ready")
|
||||
return nil
|
||||
case args[0] == "attachments-cleanup":
|
||||
return runUploadCleanup(args[1:])
|
||||
case args[0] == "healthcheck" && len(args) == 1:
|
||||
return healthcheck()
|
||||
case args[0] == "migrate" && len(args) == 2 && (args[1] == "up" || args[1] == "down"):
|
||||
url, err := repositories.DatabaseURL()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = repositories.RunMigrations(url, args[1]); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("migration %s complete", args[1])
|
||||
return nil
|
||||
default:
|
||||
return errors.New("usage: messenger migrate up|down")
|
||||
}
|
||||
}
|
||||
|
||||
func healthcheck() error {
|
||||
client := http.Client{Timeout: 2 * time.Second}
|
||||
port := os.Getenv("APP_PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
response, err := client.Get("http://" + net.JoinHostPort("127.0.0.1", port) + "/health/ready")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return errors.New("API is not ready")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"io"
|
||||
"log"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func uploadAttachmentHandler(chats *services.ChatService, attachments *services.AttachmentService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
raw := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/api/v1/chats/"), "/attachments")
|
||||
chatID, err := uuid.Parse(raw)
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err = chats.Get(r.Context(), userID, chatID.String()); err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
query, err := urlQuery(r)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
filenames := query["filename"]
|
||||
if len(filenames) != 1 {
|
||||
writeError(w, 400, "invalid_request", "exactly one filename is required", requestID)
|
||||
return
|
||||
}
|
||||
if err = services.ValidateFilename(filenames[0]); err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
if r.ContentLength > services.MaxUploadBytes {
|
||||
writeServiceError(w, services.ErrPayloadTooLarge, requestID)
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, services.MaxUploadBytes)
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second)
|
||||
defer cancel()
|
||||
attachment, err := attachments.Upload(ctx, userID, chatID.String(), filenames[0], r.Header.Get("Content-Type"), r.Body)
|
||||
var tooLarge *http.MaxBytesError
|
||||
if errors.As(err, &tooLarge) {
|
||||
err = services.ErrPayloadTooLarge
|
||||
}
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 202, attachment)
|
||||
}
|
||||
}
|
||||
|
||||
func attachmentHandler(attachments *services.AttachmentService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/v1/attachments/")
|
||||
parts := strings.Split(path, "/")
|
||||
if len(parts) > 2 || (len(parts) == 2 && parts[1] != "content") {
|
||||
writeServiceError(w, services.ErrAttachmentNotFound, requestID)
|
||||
return
|
||||
}
|
||||
id, err := uuid.Parse(parts[0])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid attachment id", requestID)
|
||||
return
|
||||
}
|
||||
a, err := attachments.Get(r.Context(), id.String(), userID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
if len(parts) == 1 {
|
||||
writeJSON(w, 200, a)
|
||||
return
|
||||
}
|
||||
query, err := urlQuery(r)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
if a.Status != "ready" {
|
||||
writeServiceError(w, services.ErrConflict, requestID)
|
||||
return
|
||||
}
|
||||
if variants, exists := query["variant"]; exists {
|
||||
if len(variants) != 1 {
|
||||
writeError(w, 400, "invalid_request", "invalid variant", requestID)
|
||||
return
|
||||
}
|
||||
if variants[0] == "thumbnail" || variants[0] == "crop" || variants[0] == "optimized" {
|
||||
found := false
|
||||
for _, raw := range a.Variants {
|
||||
if v, ok := raw.(map[string]any); ok && v["name"] == variants[0] {
|
||||
a.ObjectKey = repositories.VariantObjectKey(a.ID, a.PipelineVersion, variants[0])
|
||||
a.ContentType = v["content_type"].(string)
|
||||
a.SizeBytes = int64(v["size_bytes"].(float64))
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
writeServiceError(w, services.ErrAttachmentNotFound, requestID)
|
||||
return
|
||||
}
|
||||
} else if variants[0] != "original" {
|
||||
writeServiceError(w, services.ErrAttachmentNotFound, requestID)
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second)
|
||||
defer cancel()
|
||||
content, err := attachments.Content(ctx, a)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
defer content.Close()
|
||||
w.Header().Set("Content-Type", a.ContentType)
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(a.SizeBytes, 10))
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.WriteHeader(200)
|
||||
if _, err = io.Copy(w, content); err != nil {
|
||||
log.Printf("attachment download interrupted, request %s", requestID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func urlQuery(r *http.Request) (url.Values, error) {
|
||||
q, err := url.ParseQuery(r.URL.RawQuery)
|
||||
if err != nil {
|
||||
return nil, services.InvalidInput{Message: "invalid query"}
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type createChatRequest struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
}
|
||||
|
||||
func createChatHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
|
||||
writeError(w, 415, "unsupported_media_type", "content type must be application/json", requestID)
|
||||
return
|
||||
}
|
||||
var request createChatRequest
|
||||
if err := decodeJSONBody(r, &request); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid JSON body", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Create(r.Context(), userID, request.Title, request.MemberIDs)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, chat)
|
||||
}
|
||||
}
|
||||
|
||||
func getChatHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
chatID := strings.TrimPrefix(r.URL.Path, "/api/v1/chats/")
|
||||
if chatID == "" || strings.Contains(chatID, "/") {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Get(r.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, chat)
|
||||
}
|
||||
}
|
||||
|
||||
func listChatsHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
limit, cursor, ok := pageParams(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, err := chats.List(r.Context(), userID, limit, cursor)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, page)
|
||||
}
|
||||
}
|
||||
|
||||
func chatCollectionHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
create := createChatHandler(chats)
|
||||
list := listChatsHandler(chats)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
list(w, r)
|
||||
} else {
|
||||
create(w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func chatSubresourceHandler(chats *services.ChatService, messages *services.MessageService, attachments *services.AttachmentService) http.HandlerFunc {
|
||||
get := getChatHandler(chats)
|
||||
createMessage := createMessageHandler(chats, messages)
|
||||
listMessages := listMessagesHandler(chats, messages)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if attachments != nil && strings.Contains(r.URL.Path, "/attachment-uploads") {
|
||||
directUploadHandler(chats, attachments)(w, r)
|
||||
return
|
||||
}
|
||||
if attachments != nil && strings.HasSuffix(r.URL.Path, "/attachments") {
|
||||
uploadAttachmentHandler(chats, attachments)(w, r)
|
||||
return
|
||||
}
|
||||
if strings.HasSuffix(r.URL.Path, "/messages") {
|
||||
if r.Method == http.MethodGet {
|
||||
listMessages(w, r)
|
||||
} else {
|
||||
createMessage(w, r)
|
||||
}
|
||||
return
|
||||
}
|
||||
get(w, r)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func directUploadHandler(chats *services.ChatService, attachments *services.AttachmentService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
user, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/chats/"), "/")
|
||||
if (len(parts) != 2 && (len(parts) != 4 || parts[3] != "complete")) || parts[1] != "attachment-uploads" {
|
||||
writeError(w, 404, "not_found", "upload endpoint not found", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := uuid.Parse(parts[0])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err = chats.Get(r.Context(), user, chat.String()); err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second)
|
||||
defer cancel()
|
||||
if len(parts) == 4 {
|
||||
id, err := uuid.Parse(parts[2])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid upload id", requestID)
|
||||
return
|
||||
}
|
||||
a, err := attachments.CompleteDirect(ctx, user, chat.String(), id.String())
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 202, a)
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 4096)
|
||||
var input services.DirectUploadInput
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
err = decoder.Decode(&input)
|
||||
if err == nil {
|
||||
var extra any
|
||||
if next := decoder.Decode(&extra); !errors.Is(next, io.EOF) {
|
||||
err = errors.New("expected one JSON object")
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid upload JSON (maximum 4 KiB)", requestID)
|
||||
return
|
||||
}
|
||||
result, err := attachments.InitDirect(ctx, user, chat.String(), input)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, result)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type healthResponse struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
type apiError struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
type errorResponse struct {
|
||||
Error apiError `json:"error"`
|
||||
}
|
||||
|
||||
func newRequestID() string {
|
||||
bytes := make([]byte, 16)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "request-id-generation-failed"
|
||||
}
|
||||
return hex.EncodeToString(bytes)
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, status int, code, message, requestID string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
response := errorResponse{Error: apiError{Code: code, Message: message, RequestID: requestID}}
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
log.Printf("failed to encode error response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeServiceError(w http.ResponseWriter, err error, requestID string) {
|
||||
var upload repositories.UploadError
|
||||
if errors.As(err, &upload) {
|
||||
writeError(w, upload.Status, upload.Code, upload.Message, requestID)
|
||||
return
|
||||
}
|
||||
var invalid services.InvalidInput
|
||||
switch {
|
||||
case errors.As(err, &invalid):
|
||||
writeError(w, 400, "invalid_request", invalid.Message, requestID)
|
||||
case errors.Is(err, services.ErrPayloadTooLarge):
|
||||
writeError(w, 413, "payload_too_large", "image exceeds upload limits", requestID)
|
||||
case errors.Is(err, services.ErrUnsupportedMedia):
|
||||
writeError(w, 415, "unsupported_media_type", "a valid PNG or JPEG matching Content-Type is required", requestID)
|
||||
case errors.Is(err, services.ErrAttachmentNotFound):
|
||||
writeError(w, 404, "not_found", "attachment not found", requestID)
|
||||
case errors.Is(err, services.ErrConflict):
|
||||
writeError(w, 409, "conflict", "request conflicts with an existing resource", requestID)
|
||||
case errors.Is(err, services.ErrUnknownUser):
|
||||
writeError(w, 401, "unauthorized", "unknown user", requestID)
|
||||
case errors.Is(err, services.ErrChatNotFound):
|
||||
writeError(w, 404, "not_found", "chat not found", requestID)
|
||||
default:
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
}
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, value any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
if err := json.NewEncoder(w).Encode(value); err != nil {
|
||||
log.Printf("failed to encode response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func requestUserID(w http.ResponseWriter, r *http.Request, requestID string) (string, bool) {
|
||||
id := currentUserID(r)
|
||||
if id == "" {
|
||||
writeError(w, 401, "unauthorized", "valid Bearer session is required", requestID)
|
||||
return "", false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
func pageParams(w http.ResponseWriter, r *http.Request, requestID string) (int, string, bool) {
|
||||
limit := 50
|
||||
if values, exists := r.URL.Query()["limit"]; exists {
|
||||
if len(values) != 1 || values[0] == "" {
|
||||
writeError(w, 400, "invalid_request", "invalid limit", requestID)
|
||||
return 0, "", false
|
||||
}
|
||||
parsed, err := strconv.Atoi(values[0])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "limit must be between 1 and 100", requestID)
|
||||
return 0, "", false
|
||||
}
|
||||
limit = parsed
|
||||
}
|
||||
var cursor string
|
||||
if values, exists := r.URL.Query()["cursor"]; exists {
|
||||
if len(values) != 1 || values[0] == "" {
|
||||
writeError(w, 400, "invalid_request", "invalid cursor", requestID)
|
||||
return 0, "", false
|
||||
}
|
||||
cursor = values[0]
|
||||
}
|
||||
return limit, cursor, true
|
||||
}
|
||||
|
||||
func healthHandler(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, healthResponse{"ok"})
|
||||
}
|
||||
|
||||
func readyHandler(health *services.HealthService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
// Readiness must finish before HAProxy's one-second check read timeout.
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 500*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := health.Ready(ctx); err != nil {
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, healthResponse{"ok"})
|
||||
}
|
||||
}
|
||||
|
||||
func NewRouter(health *services.HealthService, users *services.UserService, sessions *services.SessionService, chats *services.ChatService, messages *services.MessageService, attachmentServices ...*services.AttachmentService) http.Handler {
|
||||
var attachments *services.AttachmentService
|
||||
if len(attachmentServices) > 0 {
|
||||
attachments = attachmentServices[0]
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/health/live", healthHandler)
|
||||
mux.HandleFunc("/health/ready", readyHandler(health))
|
||||
mux.Handle("/api/v1/users", createUserHandler(users))
|
||||
mux.Handle("/api/v1/auth/sessions", sessionHandler(sessions))
|
||||
mux.Handle("/api/v1/auth/sessions/current", authRequired(sessions, logoutHandler(sessions)))
|
||||
mux.Handle("/api/v1/users/me", authRequired(sessions, currentUserHandler(users)))
|
||||
mux.Handle("/api/v1/chats", authRequired(sessions, chatCollectionHandler(chats)))
|
||||
mux.Handle("/api/v1/chats/", authRequired(sessions, chatSubresourceHandler(chats, messages, attachments)))
|
||||
if attachments != nil {
|
||||
mux.Handle("/api/v1/attachments/", authRequired(sessions, attachmentHandler(attachments)))
|
||||
}
|
||||
return withInstance(mux)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Docker's hostname lets the operator map an API response to its container.
|
||||
var instanceID = func() string {
|
||||
id, err := os.Hostname()
|
||||
if err != nil || id == "" {
|
||||
return uuid.NewString()
|
||||
}
|
||||
return id
|
||||
}()
|
||||
|
||||
var draining atomic.Bool
|
||||
|
||||
func BeginShutdown() { draining.Store(true) }
|
||||
|
||||
func withInstance(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Instance-Id", instanceID)
|
||||
w.Header().Set("X-Request-Id", newRequestID())
|
||||
if draining.Load() && r.URL.Path != "/health/live" {
|
||||
writeError(w, 503, "unavailable", "instance is shutting down", w.Header().Get("X-Request-Id"))
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type createMessageRequest struct {
|
||||
Text *string `json:"text"`
|
||||
AttachmentIDs json.RawMessage `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
func messageChatID(path string) (string, bool) {
|
||||
const prefix = "/api/v1/chats/"
|
||||
const suffix = "/messages"
|
||||
if !strings.HasPrefix(path, prefix) || !strings.HasSuffix(path, suffix) {
|
||||
return "", false
|
||||
}
|
||||
chatID := strings.TrimSuffix(strings.TrimPrefix(path, prefix), suffix)
|
||||
if chatID == "" || strings.Contains(chatID, "/") {
|
||||
return "", false
|
||||
}
|
||||
return chatID, true
|
||||
}
|
||||
|
||||
func createMessageHandler(chats *services.ChatService, messages *services.MessageService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
chatID, ok := messageChatID(r.URL.Path)
|
||||
if !ok {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Get(r.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
|
||||
writeError(w, 415, "unsupported_media_type", "content type must be application/json", requestID)
|
||||
return
|
||||
}
|
||||
var request createMessageRequest
|
||||
if err := decodeJSONBody(r, &request); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid JSON body", requestID)
|
||||
return
|
||||
}
|
||||
ids := []string{}
|
||||
if request.Text == nil {
|
||||
writeError(w, 400, "invalid_request", "text must be a string", requestID)
|
||||
return
|
||||
}
|
||||
if len(request.AttachmentIDs) > 0 {
|
||||
if err := json.Unmarshal(request.AttachmentIDs, &ids); err != nil || ids == nil {
|
||||
writeError(w, 400, "invalid_request", "attachment_ids must be an array of UUIDs", requestID)
|
||||
return
|
||||
}
|
||||
}
|
||||
keys := r.Header.Values("Idempotency-Key")
|
||||
if len(keys) != 1 {
|
||||
writeError(w, 400, "invalid_request", "exactly one Idempotency-Key is required", requestID)
|
||||
return
|
||||
}
|
||||
message, err := messages.CreateIdempotent(r.Context(), userID, chat, *request.Text, keys[0], ids)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, message)
|
||||
}
|
||||
}
|
||||
|
||||
func listMessagesHandler(chats *services.ChatService, messages *services.MessageService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
chatID, ok := messageChatID(r.URL.Path)
|
||||
if !ok {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Get(r.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
limit, cursor, ok := pageParams(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, err := messages.List(r.Context(), userID, chat, limit, cursor)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, page)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type authenticatedUserKey struct{}
|
||||
|
||||
func currentUserID(r *http.Request) string {
|
||||
id, _ := r.Context().Value(authenticatedUserKey{}).(string)
|
||||
return id
|
||||
}
|
||||
|
||||
func authRequired(sessions *services.SessionService, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
authorization := r.Header.Get("Authorization")
|
||||
if !strings.HasPrefix(authorization, "Bearer ") || strings.TrimSpace(strings.TrimPrefix(authorization, "Bearer ")) == "" {
|
||||
unauthorized(w, "Bearer", newRequestID())
|
||||
return
|
||||
}
|
||||
user, err := sessions.User(r.Context(), strings.TrimPrefix(authorization, "Bearer "))
|
||||
if errors.Is(err, services.ErrInvalidSession) {
|
||||
unauthorized(w, "Bearer", newRequestID())
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), authenticatedUserKey{}, user.ID)))
|
||||
})
|
||||
}
|
||||
|
||||
func unauthorized(w http.ResponseWriter, scheme, requestID string) {
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if scheme == "Basic" {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`)
|
||||
} else {
|
||||
w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`)
|
||||
}
|
||||
writeError(w, 401, "unauthorized", "invalid credentials", requestID)
|
||||
}
|
||||
|
||||
func sessionHandler(sessions *services.SessionService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok || r.ContentLength > 0 {
|
||||
unauthorized(w, "Basic", requestID)
|
||||
return
|
||||
}
|
||||
user, token, expires, valid, err := sessions.Login(r.Context(), username, password)
|
||||
if err != nil {
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
return
|
||||
}
|
||||
if !valid {
|
||||
unauthorized(w, "Basic", requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
User any `json:"user"`
|
||||
}{token, "Bearer", expires, user})
|
||||
}
|
||||
}
|
||||
|
||||
func logoutHandler(sessions *services.SessionService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodDelete {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
token := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
||||
if err := sessions.Revoke(r.Context(), token); err != nil {
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type createUserRequest struct {
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func decodeJSONBody(r *http.Request, destination any) error {
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
var body json.RawMessage
|
||||
if err := decoder.Decode(&body); err != nil {
|
||||
return err
|
||||
}
|
||||
var extra any
|
||||
if err := decoder.Decode(&extra); err != io.EOF {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("more than one JSON value")
|
||||
}
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(body, &fields); err != nil {
|
||||
return err
|
||||
}
|
||||
if fields == nil {
|
||||
return fmt.Errorf("JSON body must be an object")
|
||||
}
|
||||
// encoding/json matches struct fields case-insensitively, even with
|
||||
// DisallowUnknownFields. Check exact contract names before decoding values.
|
||||
typeOfRequest := reflect.TypeOf(destination).Elem()
|
||||
allowed := make(map[string]bool, typeOfRequest.NumField())
|
||||
for i := 0; i < typeOfRequest.NumField(); i++ {
|
||||
name := strings.Split(typeOfRequest.Field(i).Tag.Get("json"), ",")[0]
|
||||
if name != "" && name != "-" {
|
||||
allowed[name] = true
|
||||
}
|
||||
}
|
||||
for name := range fields {
|
||||
if !allowed[name] {
|
||||
return fmt.Errorf("unknown JSON field %q", name)
|
||||
}
|
||||
}
|
||||
return json.Unmarshal(body, destination)
|
||||
}
|
||||
|
||||
func createUserHandler(users *services.UserService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
|
||||
writeError(w, 415, "unsupported_media_type", "content type must be application/json", requestID)
|
||||
return
|
||||
}
|
||||
var request createUserRequest
|
||||
if err := decodeJSONBody(r, &request); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid JSON body", requestID)
|
||||
return
|
||||
}
|
||||
user, err := users.Create(r.Context(), request.Username, request.DisplayName, request.Password)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, user)
|
||||
}
|
||||
}
|
||||
|
||||
func currentUserHandler(users *services.UserService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
user, err := users.Get(r.Context(), userID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, user)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package faults
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Only an operator-mounted directory enables faults; clients cannot set them.
|
||||
// Rename makes each injected crash one-shot across automatic container restarts.
|
||||
func Take(point, id string) bool {
|
||||
directory := os.Getenv("PIPELINE_FAULT_DIR")
|
||||
if directory == "" {
|
||||
return false
|
||||
}
|
||||
if _, err := uuid.Parse(id); err != nil {
|
||||
return false
|
||||
}
|
||||
name := filepath.Join(directory, point+"."+id)
|
||||
return os.Rename(name, name+".hit") == nil
|
||||
}
|
||||
func Crash(point, id string) {
|
||||
if Take(point, id) {
|
||||
log.Printf("injected pipeline crash: %s, attachment %s", point, id)
|
||||
os.Exit(86)
|
||||
}
|
||||
}
|
||||
|
||||
// Observe records a test-only branch without exposing an API or a lease token.
|
||||
func Observe(point, id string) {
|
||||
directory := os.Getenv("PIPELINE_FAULT_DIR")
|
||||
if directory == "" {
|
||||
return
|
||||
}
|
||||
if _, err := uuid.Parse(id); err != nil {
|
||||
return
|
||||
}
|
||||
_ = os.WriteFile(filepath.Join(directory, point+"."+id+".observed"), []byte("observed\n"), 0644)
|
||||
}
|
||||
|
||||
// Pause is operator-only and ends on release or context expiry.
|
||||
func Pause(ctx context.Context, point, id string) {
|
||||
if !Take(point, id) {
|
||||
return
|
||||
}
|
||||
release := filepath.Join(os.Getenv("PIPELINE_FAULT_DIR"), point+"."+id+".release")
|
||||
ticker := time.NewTicker(50 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
if _, err := os.Stat(release); err == nil {
|
||||
_ = os.WriteFile(release+".observed", []byte("released\n"), 0644)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"messenger/src/backend/commands"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := commands.Run(os.Args[1:]); err != nil {
|
||||
if len(os.Args) > 1 && (os.Args[1] == "image-process" || os.Args[1] == "healthcheck") {
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"time"
|
||||
)
|
||||
|
||||
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 []any `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ObjectKey string `json:"-"`
|
||||
PipelineVersion int `json:"-"`
|
||||
}
|
||||
|
||||
type AttachmentStore struct{ db *pgxpool.Pool }
|
||||
|
||||
func NewAttachmentStore(db *pgxpool.Pool) *AttachmentStore { return &AttachmentStore{db: db} }
|
||||
func (s *AttachmentStore) Create(ctx context.Context, a Attachment) error {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
_, err := s.db.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ObjectKey, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt)
|
||||
return err
|
||||
}
|
||||
func (s *AttachmentStore) GetForMember(ctx context.Context, id, userID string) (Attachment, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
a := Attachment{Variants: []any{}}
|
||||
var variants []byte
|
||||
err := s.db.QueryRow(ctx, `SELECT a.id,a.chat_id,a.owner_id,a.filename,a.object_key,a.content_type,a.size_bytes,a.sha256,a.created_at,a.updated_at,a.status,a.variants,a.error_code,a.published_pipeline_version FROM attachments a JOIN chat_members cm ON cm.chat_id=a.chat_id WHERE a.id=$1 AND cm.user_id=$2`, id, userID).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ObjectKey, &a.ContentType, &a.SizeBytes, &a.SHA256, &a.CreatedAt, &a.UpdatedAt, &a.Status, &variants, &a.ErrorCode, &a.PipelineVersion)
|
||||
if err == nil {
|
||||
err = json.Unmarshal(variants, &a.Variants)
|
||||
}
|
||||
a.CreatedAt = a.CreatedAt.UTC()
|
||||
a.UpdatedAt = a.UpdatedAt.UTC()
|
||||
return a, err
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
)
|
||||
|
||||
const jobQueue = "attachments.v1"
|
||||
|
||||
type JobBroker struct {
|
||||
connection *amqp.Connection
|
||||
channel *amqp.Channel
|
||||
returned chan amqp.Return
|
||||
}
|
||||
|
||||
func NewJobBroker(ctx context.Context, address string) (*JobBroker, error) {
|
||||
connection, err := amqp.DialConfig(address, amqp.Config{
|
||||
Heartbeat: 10 * time.Second,
|
||||
Dial: func(network, address string) (net.Conn, error) {
|
||||
return (&net.Dialer{Timeout: 5 * time.Second}).DialContext(ctx, network, address)
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New("RabbitMQ connection unavailable")
|
||||
}
|
||||
channel, err := connection.Channel()
|
||||
if err != nil {
|
||||
connection.Close()
|
||||
return nil, errors.New("RabbitMQ channel unavailable")
|
||||
}
|
||||
b := &JobBroker{connection: connection, channel: channel, returned: make(chan amqp.Return, 1)}
|
||||
// Queue metadata and message bodies both survive a broker restart.
|
||||
_, err = channel.QueueDeclare(jobQueue, true, false, false, false, nil)
|
||||
if err == nil {
|
||||
err = channel.Confirm(false)
|
||||
}
|
||||
if err == nil {
|
||||
err = channel.Qos(1, 0, false)
|
||||
}
|
||||
if err != nil {
|
||||
b.Close()
|
||||
return nil, errors.New("RabbitMQ queue initialization failed")
|
||||
}
|
||||
channel.NotifyReturn(b.returned)
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (b *JobBroker) Close() { b.channel.Close(); b.connection.Close() }
|
||||
|
||||
func (b *JobBroker) Publish(ctx context.Context, body []byte) error {
|
||||
// mandatory catches an unroutable message; confirms alone do not prove routing.
|
||||
confirmation, err := b.channel.PublishWithDeferredConfirmWithContext(ctx, "", jobQueue, true, false, amqp.Publishing{
|
||||
DeliveryMode: amqp.Persistent, ContentType: "application/json", Body: body,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.New("RabbitMQ publication failed")
|
||||
}
|
||||
confirmed, err := confirmation.WaitContext(ctx)
|
||||
if err != nil || !confirmed {
|
||||
return errors.New("RabbitMQ publication not confirmed")
|
||||
}
|
||||
// basic.return precedes basic.ack on the channel; NotifyReturn is buffered.
|
||||
select {
|
||||
case <-b.returned:
|
||||
return errors.New("RabbitMQ message was not routed")
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *JobBroker) Consume() (<-chan amqp.Delivery, error) {
|
||||
return b.channel.Consume(jobQueue, "", false, false, false, false, nil)
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Chat struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type ChatStore struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewChatStore(db *pgxpool.Pool) *ChatStore {
|
||||
return &ChatStore{db: db}
|
||||
}
|
||||
|
||||
var ErrUnknownMember = errors.New("unknown member")
|
||||
|
||||
func (s *ChatStore) Create(ctx context.Context, title string, memberIDs []string) (Chat, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
membersCopy := append([]string(nil), memberIDs...)
|
||||
chat := Chat{
|
||||
ID: uuid.New().String(),
|
||||
Title: title,
|
||||
MemberIDs: membersCopy,
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
for _, id := range membersCopy {
|
||||
var exists bool
|
||||
if err = tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM users WHERE id=$1)", id).Scan(&exists); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
if !exists {
|
||||
return Chat{}, ErrUnknownMember
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO chats(id,title,created_at) VALUES($1,$2,$3)", chat.ID, title, chat.CreatedAt); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
for _, id := range membersCopy {
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO chat_members(chat_id,user_id) VALUES($1,$2)", chat.ID, id); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (s *ChatStore) GetByID(ctx context.Context, id string) (Chat, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
var chat Chat
|
||||
err := s.db.QueryRow(ctx, "SELECT id,title,created_at FROM chats WHERE id=$1", id).Scan(&chat.ID, &chat.Title, &chat.CreatedAt)
|
||||
if err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
rows, err := s.db.Query(ctx, "SELECT user_id FROM chat_members WHERE chat_id=$1 ORDER BY user_id", id)
|
||||
if err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
chat.MemberIDs = []string{}
|
||||
for rows.Next() {
|
||||
var member string
|
||||
if err = rows.Scan(&member); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
chat.MemberIDs = append(chat.MemberIDs, member)
|
||||
}
|
||||
return chat, rows.Err()
|
||||
}
|
||||
|
||||
func (s *ChatStore) ListPageForUser(ctx context.Context, userID string, cursor *ChatCursor, limit int) ([]Chat, bool, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
afterTime := time.Time{}
|
||||
afterID := "00000000-0000-0000-0000-000000000000"
|
||||
if cursor != nil {
|
||||
afterTime = cursor.CreatedAt
|
||||
afterID = cursor.ChatID
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `SELECT c.id,c.title,c.created_at FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE m.user_id=$1 AND (c.created_at,c.id)>($2,$3::uuid) ORDER BY c.created_at,c.id LIMIT $4`, userID, afterTime, afterID, limit+1)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
chats := []Chat{}
|
||||
for rows.Next() {
|
||||
var c Chat
|
||||
if err = rows.Scan(&c.ID, &c.Title, &c.CreatedAt); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
chats = append(chats, c)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
more := len(chats) > limit
|
||||
if more {
|
||||
chats = chats[:limit]
|
||||
}
|
||||
for i := range chats {
|
||||
chat, err := s.GetByID(ctx, chats[i].ID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
chats[i] = chat
|
||||
}
|
||||
return chats, more, nil
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func UploadCleanupGrace() (time.Duration, error) {
|
||||
value := os.Getenv("UPLOAD_CLEANUP_GRACE_SECONDS")
|
||||
if value == "" {
|
||||
value = "600"
|
||||
}
|
||||
seconds, err := strconv.Atoi(value)
|
||||
if err != nil || seconds < 120 || seconds > 86400 {
|
||||
return 0, errors.New("UPLOAD_CLEANUP_GRACE_SECONDS must be between 120 and 86400")
|
||||
}
|
||||
return time.Duration(seconds) * time.Second, nil
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func RedisClientFromEnvironment() (*redis.Client, error) {
|
||||
if path := os.Getenv("CACHE_URL_FILE"); path != "" {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
options, err := redis.ParseURL(strings.TrimSpace(string(data)))
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid Redis URL in CACHE_URL_FILE")
|
||||
}
|
||||
options.PoolSize = 10
|
||||
options.MaxRetries = -1
|
||||
options.DialTimeout = time.Second
|
||||
options.ReadTimeout = time.Second
|
||||
options.WriteTimeout = time.Second
|
||||
return redis.NewClient(options), nil
|
||||
}
|
||||
address := os.Getenv("REDIS_ADDR")
|
||||
if address == "" {
|
||||
address = "redis:6379"
|
||||
}
|
||||
return NewRedisClient(address), nil
|
||||
}
|
||||
|
||||
func IdempotencyTTL() (time.Duration, error) {
|
||||
value := os.Getenv("IDEMPOTENCY_TTL_SECONDS")
|
||||
if value == "" {
|
||||
value = "86400"
|
||||
}
|
||||
seconds, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil || seconds < 1 || seconds > 31536000 {
|
||||
return 0, errors.New("IDEMPOTENCY_TTL_SECONDS must be between 1 and 31536000")
|
||||
}
|
||||
return time.Duration(seconds) * time.Second, nil
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package repositories
|
||||
|
||||
import "time"
|
||||
|
||||
type ChatCursor struct {
|
||||
UserID string `json:"user_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ChatID string `json:"chat_id"`
|
||||
}
|
||||
|
||||
type MessageCursor struct {
|
||||
ChatID string `json:"chat_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
MessageID string `json:"message_id"`
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
pgxmigrate "github.com/golang-migrate/migrate/v4/database/pgx/v5"
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
func DatabaseURL() (string, error) {
|
||||
if path := os.Getenv("DATABASE_URL_FILE"); path != "" {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimSpace(string(data)), nil
|
||||
}
|
||||
if url := os.Getenv("DATABASE_URL"); url != "" {
|
||||
return url, nil
|
||||
}
|
||||
return "", errors.New("DATABASE_URL_FILE or DATABASE_URL is required")
|
||||
}
|
||||
|
||||
func OpenDatabase(ctx context.Context) (*pgxpool.Pool, error) {
|
||||
url, err := DatabaseURL()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.MaxConns = 10
|
||||
if value := os.Getenv("DATABASE_MAX_CONNS"); value != "" {
|
||||
maxConns, parseErr := strconv.Atoi(value)
|
||||
if parseErr != nil || maxConns < 1 || maxConns > 100 {
|
||||
return nil, errors.New("DATABASE_MAX_CONNS must be between 1 and 100")
|
||||
}
|
||||
cfg.MaxConns = int32(maxConns)
|
||||
}
|
||||
cfg.ConnConfig.ConnectTimeout = 5 * time.Second
|
||||
db, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = db.Ping(ctx); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// RunMigrations uses versioned up/down SQL embedded in the API binary.
|
||||
// "down" rolls back exactly one version and is an explicit operator command.
|
||||
func RunMigrations(url, direction string) error {
|
||||
source, err := iofs.New(migrationFiles, "migrations")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sqlDB, err := sql.Open("pgx", url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
driver, err := pgxmigrate.WithInstance(sqlDB, &pgxmigrate.Config{})
|
||||
if err != nil {
|
||||
sqlDB.Close()
|
||||
return err
|
||||
}
|
||||
m, err := migrate.NewWithInstance("iofs", source, "pgx5", driver)
|
||||
if err != nil {
|
||||
sqlDB.Close()
|
||||
return err
|
||||
}
|
||||
defer m.Close()
|
||||
if direction == "down" {
|
||||
err = m.Steps(-1)
|
||||
} else {
|
||||
err = m.Up()
|
||||
}
|
||||
if errors.Is(err, migrate.ErrNoChange) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (s *AttachmentStore) CleanupDirectUploads(ctx context.Context, dryRun bool, remove func(context.Context, string) error) (CleanupResult, error) {
|
||||
result := CleanupResult{DryRun: dryRun}
|
||||
rows, err := s.db.Query(ctx, `SELECT id::text FROM direct_uploads WHERE cleanup_after<=clock_timestamp() ORDER BY cleanup_after,id LIMIT 100`)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
ids := []string{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err = rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return result, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Candidates = len(ids)
|
||||
for _, id := range ids {
|
||||
outcome, err := s.cleanupDirectUpload(ctx, id, dryRun, remove)
|
||||
if err != nil {
|
||||
result.Failed++
|
||||
continue
|
||||
}
|
||||
switch outcome {
|
||||
case "deleted":
|
||||
result.Deleted++
|
||||
case "eligible":
|
||||
result.Eligible++
|
||||
default:
|
||||
result.Skipped++
|
||||
}
|
||||
}
|
||||
if result.Failed > 0 {
|
||||
return result, fmt.Errorf("%d direct upload cleanup operations failed", result.Failed)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) cleanupDirectUpload(ctx context.Context, id string, dryRun bool, remove func(context.Context, string) error) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var key, state string
|
||||
var barrier, complete, purge, now time.Time
|
||||
err = tx.QueryRow(ctx, `SELECT staging_key,state,complete_before+interval '600 seconds',complete_before,purge_after,clock_timestamp() FROM direct_uploads WHERE id=$1 AND cleanup_after<=clock_timestamp() FOR UPDATE SKIP LOCKED`, id).Scan(&key, &state, &barrier, &complete, &purge, &now)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "skipped", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
parsed, err := uuid.Parse(id)
|
||||
if err != nil || key != "staging/direct/"+parsed.String() {
|
||||
return "", errors.New("invalid direct staging key")
|
||||
}
|
||||
if state == "pending" && now.Before(barrier) {
|
||||
return "skipped", nil
|
||||
}
|
||||
if dryRun {
|
||||
return "eligible", nil
|
||||
}
|
||||
if err = remove(ctx, key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if now.After(purge) && !now.Before(barrier) {
|
||||
_, err = tx.Exec(ctx, `DELETE FROM direct_uploads WHERE id=$1`, id)
|
||||
} else {
|
||||
if state == "pending" && !now.Before(complete) {
|
||||
state = "expired"
|
||||
}
|
||||
// An early cleanup is not final: a still valid POST can recreate staging.
|
||||
// Keep a second cleanup after the safety barrier, then retain the receipt 24h.
|
||||
next := barrier
|
||||
if !now.Before(barrier) {
|
||||
next = purge
|
||||
}
|
||||
_, err = tx.Exec(ctx, `UPDATE direct_uploads SET state=$2,cleanup_after=$3 WHERE id=$1`, id, state, next)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "deleted", nil
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// UploadError is also persisted for terminal validation failures, so retries agree.
|
||||
type UploadError struct {
|
||||
Status int
|
||||
Code, Message string
|
||||
}
|
||||
|
||||
func (e UploadError) Error() string { return e.Message }
|
||||
|
||||
func DirectError(code string) UploadError {
|
||||
switch code {
|
||||
case "upload_not_uploaded":
|
||||
return UploadError{409, code, "file has not been uploaded"}
|
||||
case "upload_expired":
|
||||
return UploadError{409, code, "upload confirmation period has expired"}
|
||||
case "upload_size_mismatch":
|
||||
return UploadError{400, code, "uploaded size differs from declared size"}
|
||||
case "upload_checksum_mismatch":
|
||||
return UploadError{400, code, "uploaded SHA-256 differs from declared checksum"}
|
||||
case "payload_too_large":
|
||||
return UploadError{413, code, "image exceeds upload limits"}
|
||||
default:
|
||||
return UploadError{415, "unsupported_media_type", "a valid PNG or JPEG matching Content-Type is required"}
|
||||
}
|
||||
}
|
||||
|
||||
type DirectUpload struct {
|
||||
ID string `json:"upload_id"`
|
||||
AttachmentID string `json:"-"`
|
||||
OwnerID string `json:"-"`
|
||||
ChatID string `json:"-"`
|
||||
Filename string `json:"-"`
|
||||
ContentType string `json:"-"`
|
||||
SizeBytes int64 `json:"-"`
|
||||
SHA256 string `json:"-"`
|
||||
StagingKey string `json:"-"`
|
||||
FinalKey string `json:"-"`
|
||||
State string `json:"-"`
|
||||
ErrorCode *string `json:"-"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
CompleteBefore time.Time `json:"complete_before"`
|
||||
}
|
||||
|
||||
const directColumns = `d.id,d.attachment_id,d.owner_id,d.chat_id,d.filename,d.content_type,d.size_bytes,d.sha256,d.staging_key,d.final_key,d.state,d.error_code,d.expires_at,d.complete_before`
|
||||
|
||||
func scanDirect(row pgx.Row) (DirectUpload, error) {
|
||||
var u DirectUpload
|
||||
err := row.Scan(&u.ID, &u.AttachmentID, &u.OwnerID, &u.ChatID, &u.Filename, &u.ContentType, &u.SizeBytes, &u.SHA256, &u.StagingKey, &u.FinalKey, &u.State, &u.ErrorCode, &u.ExpiresAt, &u.CompleteBefore)
|
||||
return u, err
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) CreateDirect(ctx context.Context, u DirectUpload) (DirectUpload, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
return scanDirect(s.db.QueryRow(ctx, `INSERT INTO direct_uploads AS d(id,attachment_id,owner_id,chat_id,filename,content_type,size_bytes,sha256,staging_key,final_key) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING `+directColumns, u.ID, u.AttachmentID, u.OwnerID, u.ChatID, u.Filename, u.ContentType, u.SizeBytes, u.SHA256, u.StagingKey, u.FinalKey))
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) GetDirect(ctx context.Context, id, chat, user string) (DirectUpload, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
return scanDirect(s.db.QueryRow(ctx, `SELECT `+directColumns+` FROM direct_uploads d JOIN chat_members cm ON cm.chat_id=d.chat_id AND cm.user_id=$3 WHERE d.id=$1 AND d.chat_id=$2 AND d.owner_id=$3`, id, chat, user))
|
||||
}
|
||||
|
||||
// The session lock serializes complete across replicas. A separately committed
|
||||
// reservation survives rollback/crash, while its row lock protects the final key.
|
||||
func (s *AttachmentStore) CompleteDirect(ctx context.Context, id, chat, user string, validate func(context.Context, DirectUpload) error) (Attachment, error) {
|
||||
var a Attachment
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
u, err := scanDirect(tx.QueryRow(ctx, `SELECT `+directColumns+` FROM direct_uploads d JOIN chat_members cm ON cm.chat_id=d.chat_id AND cm.user_id=$3 WHERE d.id=$1 AND d.chat_id=$2 AND d.owner_id=$3 FOR UPDATE OF d`, id, chat, user))
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
if u.StagingKey != "staging/direct/"+u.ID || u.FinalKey != "originals/"+u.AttachmentID {
|
||||
return a, errors.New("invalid direct upload object keys")
|
||||
}
|
||||
if u.State == "ready" {
|
||||
return s.GetForMember(ctx, u.AttachmentID, user)
|
||||
}
|
||||
if u.State == "rejected" && u.ErrorCode != nil {
|
||||
return a, DirectError(*u.ErrorCode)
|
||||
}
|
||||
var expired bool
|
||||
if err = tx.QueryRow(ctx, `SELECT clock_timestamp()>=$1`, u.CompleteBefore).Scan(&expired); err != nil {
|
||||
return a, err
|
||||
}
|
||||
if expired || u.State == "expired" {
|
||||
return a, DirectError("upload_expired")
|
||||
}
|
||||
grace, err := UploadCleanupGrace()
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
// ON CONFLICT handles retry after an interrupted COPY without losing tracking.
|
||||
_, err = s.db.Exec(ctx, `INSERT INTO upload_attempts(id,object_key,cleanup_after) VALUES($1,$2,clock_timestamp()+$3*interval '1 second') ON CONFLICT(id) DO UPDATE SET cleanup_after=GREATEST(upload_attempts.cleanup_after,EXCLUDED.cleanup_after) WHERE upload_attempts.object_key=EXCLUDED.object_key`, u.AttachmentID, u.FinalKey, grace.Seconds())
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
var key string
|
||||
if err = tx.QueryRow(ctx, `SELECT object_key FROM upload_attempts WHERE id=$1 AND cleanup_after>clock_timestamp() FOR UPDATE`, u.AttachmentID).Scan(&key); err != nil {
|
||||
return a, err
|
||||
}
|
||||
if key != u.FinalKey {
|
||||
return a, errors.New("direct upload reservation mismatch")
|
||||
}
|
||||
if err = validate(ctx, u); err != nil {
|
||||
var invalid UploadError
|
||||
if errors.As(err, &invalid) && (invalid.Status == 400 || invalid.Status == 413 || invalid.Status == 415) {
|
||||
_, saveErr := tx.Exec(ctx, `UPDATE direct_uploads SET state='rejected',error_code=$2,cleanup_after=clock_timestamp(),purge_after=clock_timestamp()+interval '24 hours' WHERE id=$1`, id, invalid.Code)
|
||||
if saveErr != nil {
|
||||
return a, saveErr
|
||||
}
|
||||
if saveErr = tx.Commit(ctx); saveErr != nil {
|
||||
return a, saveErr
|
||||
}
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
now := time.Now().UTC().Truncate(time.Microsecond)
|
||||
a = Attachment{ID: u.AttachmentID, ChatID: u.ChatID, OwnerID: u.OwnerID, Filename: u.Filename, ContentType: u.ContentType, SizeBytes: u.SizeBytes, SHA256: u.SHA256, ObjectKey: u.FinalKey, Status: "queued", Variants: []any{}, CreatedAt: now, UpdatedAt: now}
|
||||
_, err = tx.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ObjectKey, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt)
|
||||
if err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if err = enqueueJob(ctx, tx, a, a.ObjectKey); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE direct_uploads SET state='ready',cleanup_after=clock_timestamp(),purge_after=clock_timestamp()+interval '24 hours' WHERE id=$1`, id); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM upload_attempts WHERE id=$1`, a.ID); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
func NotFound(err error) bool {
|
||||
return errors.Is(err, pgx.ErrNoRows)
|
||||
}
|
||||
|
||||
func CheckViolation(err error) bool {
|
||||
var pgerr *pgconn.PgError
|
||||
return errors.As(err, &pgerr) && pgerr.Code == "23514"
|
||||
}
|
||||
|
||||
func UniqueViolation(err error) bool {
|
||||
var pgerr *pgconn.PgError
|
||||
return errors.As(err, &pgerr) && pgerr.Code == "23505"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type HealthStore struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewHealthStore(db *pgxpool.Pool) *HealthStore {
|
||||
return &HealthStore{db: db}
|
||||
}
|
||||
|
||||
func (s *HealthStore) Ping(ctx context.Context) error {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
return s.db.Ping(ctx)
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
var ErrAttachmentNotFound = errors.New("attachment not found")
|
||||
var ErrAttachmentFailed = errors.New("attachment failed")
|
||||
|
||||
var ErrIdempotencyConflict = errors.New("idempotency key belongs to a different message")
|
||||
|
||||
// CreateIdempotent commits the message and its replay record together.
|
||||
func (s *MessageStore) CreateIdempotent(ctx context.Context, chatID, senderID, text, key string, attachments ...[]string) (Message, error) {
|
||||
ids := []string{}
|
||||
if len(attachments) > 0 && attachments[0] != nil {
|
||||
ids = attachments[0]
|
||||
}
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
|
||||
// The transaction-scoped lock is shared by all API replicas. Hash collisions
|
||||
// only serialize unrelated requests; the full scope is checked by the query.
|
||||
scope := senderID + ":POST:/api/v1/chats/" + chatID + "/messages:" + key
|
||||
if _, err = tx.Exec(ctx, "SELECT pg_advisory_xact_lock(hashtextextended($1, 0))", scope); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
// A shared row lock prevents failure publication racing with a new link.
|
||||
hasFailed := false
|
||||
for _, id := range ids {
|
||||
var status string
|
||||
err = tx.QueryRow(ctx, "SELECT status FROM attachments WHERE id=$1 AND chat_id=$2 FOR SHARE", id, chatID).Scan(&status)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return Message{}, ErrAttachmentNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
hasFailed = hasFailed || status == "failed"
|
||||
}
|
||||
|
||||
var message Message
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at, ARRAY(SELECT attachment_id::text FROM message_attachments ma WHERE ma.message_id=m.id ORDER BY position)
|
||||
FROM message_idempotency i JOIN messages m ON m.id = i.message_id
|
||||
WHERE i.sender_id=$1 AND i.chat_id=$2 AND i.key=$3
|
||||
AND i.expires_at > clock_timestamp()`, senderID, chatID, key).
|
||||
Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, &message.CreatedAt, &message.AttachmentIDs)
|
||||
if err == nil {
|
||||
message.CreatedAt = message.CreatedAt.UTC()
|
||||
if message.Text != text || !reflect.DeepEqual(message.AttachmentIDs, ids) {
|
||||
return Message{}, ErrIdempotencyConflict
|
||||
}
|
||||
return message, tx.Commit(ctx)
|
||||
}
|
||||
if !errors.Is(err, pgx.ErrNoRows) {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
if hasFailed {
|
||||
return Message{}, ErrAttachmentFailed
|
||||
}
|
||||
|
||||
// Expiry permits key reuse; it never deletes the original message.
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM message_idempotency WHERE sender_id=$1 AND chat_id=$2 AND key=$3", senderID, chatID, key); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
message = Message{
|
||||
AttachmentIDs: ids, ID: uuid.NewString(), ChatID: chatID, SenderID: senderID, Text: text,
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES($1,$2,$3,$4,$5)", message.ID, chatID, senderID, text, message.CreatedAt); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
for position, id := range ids {
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO message_attachments(message_id,attachment_id,position) VALUES($1,$2,$3)", message.ID, id, position); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO message_idempotency(sender_id,chat_id,key,message_id,expires_at)
|
||||
VALUES($1,$2,$3,$4,clock_timestamp() + $5 * interval '1 second')`, senderID, chatID, key, message.ID, s.idempotencyTTL.Seconds()); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
return message, tx.Commit(ctx)
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type FailedJob struct {
|
||||
ID string `json:"job_id"`
|
||||
AttachmentID string `json:"attachment_id"`
|
||||
Code string `json:"error_code"`
|
||||
Attempts int `json:"attempts"`
|
||||
FailedAt time.Time `json:"failed_at"`
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) FailedJobs(ctx context.Context) ([]FailedJob, error) {
|
||||
rows, err := s.db.Query(ctx, `SELECT j.id,j.attachment_id,f.error_code,f.attempts,f.failed_at FROM attachment_job_failures f JOIN attachment_jobs j ON j.id=f.job_id JOIN attachments a ON a.id=j.attachment_id WHERE a.status='failed' AND j.pipeline_version=a.pipeline_version AND j.superseded_at IS NULL ORDER BY f.failed_at DESC LIMIT 100`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
result := []FailedJob{}
|
||||
for rows.Next() {
|
||||
var j FailedJob
|
||||
if err = rows.Scan(&j.ID, &j.AttachmentID, &j.Code, &j.Attempts, &j.FailedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, j)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) ReplayJob(ctx context.Context, id, reason string, exists func(context.Context, string) error) (bool, error) {
|
||||
reason = strings.TrimSpace(reason)
|
||||
if reason == "" || utf8.RuneCountInString(reason) > 200 {
|
||||
return false, errors.New("replay reason must contain 1-200 characters")
|
||||
}
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var status, key, attachment string
|
||||
var deleted, obsolete bool
|
||||
err = tx.QueryRow(ctx, `SELECT a.status,j.staging_key,a.id,j.staging_deleted_at IS NOT NULL,j.pipeline_version<>a.pipeline_version OR j.superseded_at IS NOT NULL FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.id=$1 FOR UPDATE OF j,a`, id).Scan(&status, &key, &attachment, &deleted, &obsolete)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if obsolete || status != "failed" {
|
||||
return false, nil
|
||||
}
|
||||
if deleted {
|
||||
return false, errors.New("staging has expired; replay unavailable")
|
||||
}
|
||||
if key != "staging/jobs/"+attachment && key != "originals/"+attachment {
|
||||
return false, errors.New("invalid staging key")
|
||||
}
|
||||
if err = exists(ctx, key); err != nil {
|
||||
return false, errors.New("staging unavailable; replay not scheduled")
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO attachment_job_replays(job_id,reason) VALUES($1,$2)`, id, reason); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachment_jobs SET attempts=0,generation=generation+1,next_attempt_at=now(),lease_until=NULL,lease_token=NULL,published_at=NULL WHERE id=$1`, id); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachments SET status='queued',error_code=NULL,updated_at=now() WHERE id=$1`, attachment); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// Replay and cleanup lock the same rows. A ready source is removed only after
|
||||
// final publication, failed sources expire after retention; queued work is safe.
|
||||
func (s *AttachmentStore) CleanupJobStaging(ctx context.Context, id string, retention time.Duration, dryRun bool, remove func(context.Context, string) error) (bool, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var key, attachment, status string
|
||||
var eligible bool
|
||||
err = tx.QueryRow(ctx, `SELECT j.staging_key,a.id,a.status,j.staging_deleted_at IS NULL AND (a.status='ready' AND jsonb_array_length(a.variants)>=3 OR a.status='failed' AND j.pipeline_version=a.pipeline_version AND COALESCE(f.failed_at,a.updated_at)<now()-$2*interval '1 second') FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id LEFT JOIN attachment_job_failures f ON f.job_id=j.id WHERE j.id=$1 FOR UPDATE OF j,a SKIP LOCKED`, id, retention.Seconds()).Scan(&key, &attachment, &status, &eligible)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !eligible || key == "originals/"+attachment {
|
||||
return false, nil
|
||||
}
|
||||
if key != "staging/jobs/"+attachment {
|
||||
return false, errors.New("invalid staging key")
|
||||
}
|
||||
if dryRun {
|
||||
return true, nil
|
||||
}
|
||||
if err = remove(ctx, key); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachment_jobs SET staging_deleted_at=now() WHERE id=$1`, id); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) CleanupJobs(ctx context.Context, retention time.Duration, dryRun bool, remove func(context.Context, string) error) (int, error) {
|
||||
rows, err := s.db.Query(ctx, `SELECT j.id FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id LEFT JOIN attachment_job_failures f ON f.job_id=j.id WHERE j.staging_deleted_at IS NULL AND j.staging_key='staging/jobs/'||j.attachment_id::text AND (a.status='ready' AND jsonb_array_length(a.variants)>=3 OR a.status='failed' AND j.pipeline_version=a.pipeline_version AND COALESCE(f.failed_at,a.updated_at)<now()-$1*interval '1 second') ORDER BY j.created_at LIMIT 100`, retention.Seconds())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
ids := []string{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err = rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return 0, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
count := 0
|
||||
for _, id := range ids {
|
||||
done, e := s.CleanupJobStaging(ctx, id, retention, dryRun, remove)
|
||||
if e != nil {
|
||||
return count, e
|
||||
}
|
||||
if done {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"math/big"
|
||||
"time"
|
||||
)
|
||||
|
||||
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"`
|
||||
}
|
||||
type Job struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
ID string `json:"job_id"`
|
||||
Type string `json:"type"`
|
||||
AttachmentID string `json:"attachment_id"`
|
||||
PipelineVersion int `json:"pipeline_version"`
|
||||
Attempt int `json:"attempt"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// QueuedJobs is also the recovery source when publish/confirm is uncertain.
|
||||
// Re-publication is intentional: a durable intent is never removed by publish.
|
||||
func (s *AttachmentStore) QueuedJobs(ctx context.Context) ([]Job, error) {
|
||||
rows, err := s.db.Query(ctx, `SELECT j.id,j.attachment_id,j.pipeline_version,j.attempts+1,j.created_at FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.superseded_at IS NULL AND j.pipeline_version=a.pipeline_version AND a.status IN ('queued','processing') AND j.next_attempt_at<=now() AND (j.lease_until IS NULL OR j.lease_until<now()) AND (j.published_at IS NULL OR j.published_at<now()-interval '30 seconds') ORDER BY j.created_at LIMIT 100`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
jobs := []Job{}
|
||||
for rows.Next() {
|
||||
j := Job{SchemaVersion: 1, Type: "attachment.process.v1"}
|
||||
if err = rows.Scan(&j.ID, &j.AttachmentID, &j.PipelineVersion, &j.Attempt, &j.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
j.CreatedAt = j.CreatedAt.UTC()
|
||||
jobs = append(jobs, j)
|
||||
}
|
||||
return jobs, rows.Err()
|
||||
}
|
||||
func (s *AttachmentStore) MarkPublished(ctx context.Context, id string) error {
|
||||
_, err := s.db.Exec(ctx, `UPDATE attachment_jobs SET published_at=now() WHERE id=$1`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// StartAttempt commits the lease and attempt before decoding. A token fences a
|
||||
// consumer that was paused between claim and processing past its lease deadline.
|
||||
func (s *AttachmentStore) StartAttempt(ctx context.Context, j Job, timeout time.Duration, maxAttempts int) (string, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var status string
|
||||
var attempts, generation int
|
||||
var available bool
|
||||
err = tx.QueryRow(ctx, `SELECT a.status,j.attempts,j.generation,j.next_attempt_at<=now() AND (j.lease_until IS NULL OR j.lease_until<now()) FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.id=$1 AND j.attachment_id=$2 AND j.pipeline_version=$3 AND j.superseded_at IS NULL AND a.pipeline_version=j.pipeline_version FOR UPDATE OF j,a SKIP LOCKED`, j.ID, j.AttachmentID, j.PipelineVersion).Scan(&status, &attempts, &generation, &available)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if status == "ready" || status == "failed" || !available {
|
||||
return "", nil
|
||||
}
|
||||
// A crashed attempt remains visible in the audit trail.
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachment_job_attempts SET finished_at=clock_timestamp(),error_code='processing_timeout' WHERE job_id=$1 AND finished_at IS NULL`, j.ID); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if attempts >= maxAttempts {
|
||||
if err = failJob(ctx, tx, j, "retry_exhausted", attempts); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "", tx.Commit(ctx)
|
||||
}
|
||||
token := uuid.NewString()
|
||||
_, err = tx.Exec(ctx, `UPDATE attachment_jobs SET attempts=attempts+1,lease_token=$2,lease_until=now()+$3*interval '1 second' WHERE id=$1`, j.ID, token, timeout.Seconds()+5)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachments SET status='processing',updated_at=now() WHERE id=$1`, j.AttachmentID); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO attachment_job_attempts(job_id,generation,attempt) VALUES($1,$2,$3)`, j.ID, generation, attempts+1); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
var ErrStaleJob = errors.New("stale job lease or version")
|
||||
|
||||
var ErrProcessingTimeout = errors.New("processing timeout")
|
||||
|
||||
// ProcessJob holds the row lock across deterministic object writes. The lock and
|
||||
// token check prevent overlapping/stale attempts from publishing metadata.
|
||||
func (s *AttachmentStore) ProcessJob(ctx context.Context, j Job, token string, maxAttempts int, process func(Attachment, string) ([]Variant, string, error)) error {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var staging string
|
||||
var attempts, generation int
|
||||
var a Attachment
|
||||
err = tx.QueryRow(ctx, `SELECT a.id,a.object_key,a.content_type,a.size_bytes,a.sha256,a.status,j.staging_key,j.attempts,j.generation FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.id=$1 AND j.attachment_id=$2 AND j.pipeline_version=$3 AND j.lease_token=$4 AND j.lease_until>now() AND j.superseded_at IS NULL AND a.pipeline_version=j.pipeline_version FOR UPDATE OF j,a`, j.ID, j.AttachmentID, j.PipelineVersion, token).Scan(&a.ID, &a.ObjectKey, &a.ContentType, &a.SizeBytes, &a.SHA256, &a.Status, &staging, &attempts, &generation)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrStaleJob
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if a.Status != "processing" {
|
||||
return errors.New("job is not processing")
|
||||
}
|
||||
a.PipelineVersion = j.PipelineVersion
|
||||
variants, code, workErr := process(a, staging)
|
||||
var next *time.Time
|
||||
var auditCode *string
|
||||
if workErr == nil {
|
||||
data, e := json.Marshal(variants)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
_, err = tx.Exec(ctx, `UPDATE attachments SET status='ready',variants=$2,published_pipeline_version=$3,error_code=NULL,updated_at=now() WHERE id=$1 AND pipeline_version=$3`, a.ID, data, j.PipelineVersion)
|
||||
if err == nil {
|
||||
_, err = tx.Exec(ctx, `INSERT INTO attachment_publications(attachment_id,job_id,pipeline_version,variants) VALUES($1,$2,$3,$4)`, a.ID, j.ID, j.PipelineVersion, data)
|
||||
}
|
||||
} else {
|
||||
reason := "temporary_failure"
|
||||
if errors.Is(workErr, ErrProcessingTimeout) {
|
||||
reason = "processing_timeout"
|
||||
}
|
||||
if code != "" {
|
||||
reason = code
|
||||
}
|
||||
auditCode = &reason
|
||||
if code != "" || attempts >= maxAttempts {
|
||||
if code == "" {
|
||||
code = "retry_exhausted"
|
||||
}
|
||||
err = failJob(ctx, tx, j, code, attempts)
|
||||
} else {
|
||||
due := time.Now().UTC().Add(RetryDelay(attempts))
|
||||
next = &due
|
||||
_, err = tx.Exec(ctx, `UPDATE attachments SET status='queued',error_code=NULL,updated_at=now() WHERE id=$1`, a.ID)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(ctx, `UPDATE attachment_job_attempts SET finished_at=clock_timestamp(),error_code=$4,next_attempt_at=$5 WHERE job_id=$1 AND generation=$2 AND attempt=$3`, j.ID, generation, attempts, auditCode, next)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(ctx, `UPDATE attachment_jobs SET lease_until=NULL,lease_token=NULL,published_at=NULL,next_attempt_at=COALESCE($2,now()) WHERE id=$1`, j.ID, next)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func failJob(ctx context.Context, tx pgx.Tx, j Job, code string, attempts int) error {
|
||||
if _, err := tx.Exec(ctx, `UPDATE attachments SET status='failed',error_code=$2,updated_at=now() WHERE id=$1`, j.AttachmentID, code); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `UPDATE attachment_jobs SET lease_token=NULL,lease_until=NULL WHERE id=$1`, j.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := tx.Exec(ctx, `INSERT INTO attachment_job_failures(job_id,error_code,attempts) VALUES($1,$2,$3) ON CONFLICT(job_id) DO UPDATE SET error_code=EXCLUDED.error_code,attempts=EXCLUDED.attempts,failed_at=clock_timestamp()`, j.ID, code, attempts)
|
||||
return err
|
||||
}
|
||||
|
||||
// Equal jitter: exponential ceiling 2,4,8,...,30 seconds; delay in [ceiling/2,ceiling).
|
||||
func RetryDelay(attempt int) time.Duration {
|
||||
ceiling := 2 * time.Second
|
||||
for i := 1; i < attempt && ceiling < 30*time.Second; i++ {
|
||||
ceiling *= 2
|
||||
}
|
||||
if ceiling > 30*time.Second {
|
||||
ceiling = 30 * time.Second
|
||||
}
|
||||
n, err := rand.Int(rand.Reader, big.NewInt(int64(ceiling/2)))
|
||||
if err != nil {
|
||||
return ceiling
|
||||
}
|
||||
return ceiling/2 + time.Duration(n.Int64())
|
||||
}
|
||||
|
||||
func enqueueJob(ctx context.Context, tx pgx.Tx, a Attachment, staging string) error {
|
||||
_, err := tx.Exec(ctx, `UPDATE attachments SET status='queued' WHERE id=$1`, a.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id,staging_key) VALUES($1,$2,$3)`, uuid.NewString(), a.ID, staging)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) JobTerminal(ctx context.Context, id string) (bool, error) {
|
||||
var done bool
|
||||
err := s.db.QueryRow(ctx, `SELECT j.superseded_at IS NOT NULL OR j.pipeline_version<a.pipeline_version OR a.status IN ('ready','failed') FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.id=$1`, id).Scan(&done)
|
||||
return done, err
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
type MessageStore struct {
|
||||
db *pgxpool.Pool
|
||||
idempotencyTTL time.Duration
|
||||
}
|
||||
|
||||
func NewMessageStore(db *pgxpool.Pool) *MessageStore {
|
||||
return NewMessageStoreWithTTL(db, 24*time.Hour)
|
||||
}
|
||||
|
||||
func NewMessageStoreWithTTL(db *pgxpool.Pool, ttl time.Duration) *MessageStore {
|
||||
return &MessageStore{db: db, idempotencyTTL: ttl}
|
||||
}
|
||||
|
||||
func (s *MessageStore) Create(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
senderID string,
|
||||
text string,
|
||||
) (Message, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
message := Message{
|
||||
AttachmentIDs: []string{},
|
||||
ID: uuid.New().String(),
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
|
||||
_, err := s.db.Exec(ctx, "INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES($1,$2,$3,$4,$5)", message.ID, message.ChatID, message.SenderID, message.Text, message.CreatedAt)
|
||||
return message, err
|
||||
}
|
||||
|
||||
func (s *MessageStore) ListPageByChat(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
cursor *MessageCursor,
|
||||
limit int,
|
||||
) ([]Message, bool, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
afterTime := time.Time{}
|
||||
afterID := "00000000-0000-0000-0000-000000000000"
|
||||
if cursor != nil {
|
||||
afterTime = cursor.CreatedAt
|
||||
afterID = cursor.MessageID
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `SELECT id,chat_id,sender_id,text,created_at,ARRAY(SELECT attachment_id::text FROM message_attachments ma WHERE ma.message_id=messages.id ORDER BY position) FROM messages WHERE chat_id=$1 AND (created_at,id)>($2,$3::uuid) ORDER BY created_at,id LIMIT $4`, chatID, afterTime, afterID, limit+1)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Message{}
|
||||
for rows.Next() {
|
||||
var m Message
|
||||
if err = rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt, &m.AttachmentIDs); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
items = append(items, m)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
more := len(items) > limit
|
||||
if more {
|
||||
items = items[:limit]
|
||||
}
|
||||
return items, more, nil
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
DROP TABLE IF EXISTS sessions;
|
||||
DROP TABLE IF EXISTS messages;
|
||||
DROP TABLE IF EXISTS chat_members;
|
||||
DROP TABLE IF EXISTS chats;
|
||||
DROP TABLE IF EXISTS users;
|
||||
@@ -0,0 +1,32 @@
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id uuid PRIMARY KEY,
|
||||
username varchar(32) NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
|
||||
password_hash bytea NOT NULL,
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
PRIMARY KEY (chat_id,user_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS chat_members_user_idx ON chat_members(user_id,chat_id);
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
sender_id uuid NOT NULL REFERENCES users(id),
|
||||
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS messages_history_idx ON messages(chat_id,created_at,id);
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
token_hash bytea PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS sessions_expiry_idx ON sessions(expires_at);
|
||||
@@ -0,0 +1,6 @@
|
||||
CREATE TABLE sessions (
|
||||
token_hash bytea PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX sessions_expiry_idx ON sessions(expires_at);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE sessions;
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE message_idempotency;
|
||||
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE message_idempotency (
|
||||
sender_id uuid NOT NULL REFERENCES users(id),
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
key varchar(128) NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
message_id uuid NOT NULL REFERENCES messages(id),
|
||||
expires_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (sender_id, chat_id, key)
|
||||
);
|
||||
@@ -0,0 +1,5 @@
|
||||
-- An operator must first remove attachment-only messages before this rollback.
|
||||
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_check CHECK (char_length(text) BETWEEN 1 AND 4000);
|
||||
DROP TABLE message_attachments;
|
||||
DROP TABLE attachments;
|
||||
@@ -0,0 +1,22 @@
|
||||
CREATE TABLE attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
owner_id uuid NOT NULL REFERENCES users(id),
|
||||
filename text NOT NULL,
|
||||
object_key text NOT NULL UNIQUE,
|
||||
content_type text NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
|
||||
size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX attachments_chat_idx ON attachments(chat_id);
|
||||
CREATE TABLE message_attachments (
|
||||
message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||
attachment_id uuid NOT NULL REFERENCES attachments(id),
|
||||
position smallint NOT NULL CHECK (position BETWEEN 0 AND 9),
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
UNIQUE(message_id, position)
|
||||
);
|
||||
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_check CHECK (char_length(text) <= 4000);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE upload_attempts;
|
||||
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE upload_attempts (
|
||||
id uuid PRIMARY KEY,
|
||||
object_key text NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
cleanup_after timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX upload_attempts_cleanup_idx ON upload_attempts(cleanup_after,id);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE direct_uploads;
|
||||
@@ -0,0 +1,20 @@
|
||||
CREATE TABLE direct_uploads (
|
||||
id uuid PRIMARY KEY,
|
||||
attachment_id uuid NOT NULL UNIQUE,
|
||||
owner_id uuid NOT NULL REFERENCES users(id),
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
filename text NOT NULL,
|
||||
content_type text NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
|
||||
size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
staging_key text NOT NULL UNIQUE,
|
||||
final_key text NOT NULL UNIQUE,
|
||||
state text NOT NULL DEFAULT 'pending' CHECK (state IN ('pending','ready','rejected','expired')),
|
||||
error_code text,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
expires_at timestamptz NOT NULL DEFAULT clock_timestamp()+interval '120 seconds',
|
||||
complete_before timestamptz NOT NULL DEFAULT clock_timestamp()+interval '720 seconds',
|
||||
cleanup_after timestamptz NOT NULL DEFAULT clock_timestamp()+interval '1320 seconds',
|
||||
purge_after timestamptz NOT NULL DEFAULT clock_timestamp()+interval '24 hours'
|
||||
);
|
||||
CREATE INDEX direct_uploads_cleanup_idx ON direct_uploads(cleanup_after,id);
|
||||
@@ -0,0 +1,2 @@
|
||||
DROP TABLE attachment_jobs;
|
||||
ALTER TABLE attachments DROP COLUMN status, DROP COLUMN variants, DROP COLUMN error_code;
|
||||
@@ -0,0 +1,10 @@
|
||||
ALTER TABLE attachments ADD COLUMN status text NOT NULL DEFAULT 'ready' CHECK(status IN ('queued','processing','ready','failed'));
|
||||
ALTER TABLE attachments ADD COLUMN variants jsonb NOT NULL DEFAULT '[]';
|
||||
ALTER TABLE attachments ADD COLUMN error_code text;
|
||||
CREATE TABLE attachment_jobs (
|
||||
id uuid PRIMARY KEY, attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id),
|
||||
staging_key text NOT NULL, pipeline_version integer NOT NULL DEFAULT 1,
|
||||
attempts integer NOT NULL DEFAULT 0, next_attempt_at timestamptz NOT NULL DEFAULT now(),
|
||||
lease_until timestamptz, published_at timestamptz,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
@@ -0,0 +1,3 @@
|
||||
DROP INDEX job_dispatch_due_idx;
|
||||
DROP TABLE attachment_job_replays,attachment_job_failures,attachment_job_attempts;
|
||||
ALTER TABLE attachment_jobs DROP COLUMN lease_token,DROP COLUMN generation,DROP COLUMN staging_deleted_at;
|
||||
@@ -0,0 +1,22 @@
|
||||
ALTER TABLE attachment_jobs ADD COLUMN lease_token uuid;
|
||||
ALTER TABLE attachment_jobs ADD COLUMN generation integer NOT NULL DEFAULT 1;
|
||||
ALTER TABLE attachment_jobs ADD COLUMN staging_deleted_at timestamptz;
|
||||
CREATE TABLE attachment_job_attempts (
|
||||
job_id uuid NOT NULL REFERENCES attachment_jobs(id), generation integer NOT NULL,
|
||||
attempt integer NOT NULL, started_at timestamptz NOT NULL DEFAULT now(),
|
||||
finished_at timestamptz, error_code text, next_attempt_at timestamptz,
|
||||
PRIMARY KEY(job_id,generation,attempt)
|
||||
);
|
||||
CREATE TABLE attachment_job_failures (
|
||||
job_id uuid PRIMARY KEY REFERENCES attachment_jobs(id), error_code text NOT NULL,
|
||||
attempts integer NOT NULL, failed_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE TABLE attachment_job_replays (
|
||||
id bigserial PRIMARY KEY, job_id uuid NOT NULL REFERENCES attachment_jobs(id),
|
||||
reason text NOT NULL CHECK(char_length(reason) BETWEEN 1 AND 200),
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX job_dispatch_due_idx ON attachment_jobs(next_attempt_at,published_at);
|
||||
INSERT INTO attachment_job_failures(job_id,error_code,attempts,failed_at)
|
||||
SELECT j.id,COALESCE(a.error_code,'processing_failed'),j.attempts,a.updated_at
|
||||
FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE a.status='failed';
|
||||
@@ -0,0 +1,11 @@
|
||||
-- Downgrade is intentionally refused if multiple versions exist: never drop jobs.
|
||||
DO $$ BEGIN
|
||||
IF EXISTS(SELECT 1 FROM attachment_jobs WHERE pipeline_version<>1) THEN
|
||||
RAISE EXCEPTION 'cannot downgrade while versioned jobs exist';
|
||||
END IF;
|
||||
END $$;
|
||||
DROP TABLE attachment_publications,attachment_pipeline_upgrades;
|
||||
ALTER TABLE attachment_jobs DROP CONSTRAINT attachment_jobs_version_key;
|
||||
ALTER TABLE attachment_jobs ADD CONSTRAINT attachment_jobs_attachment_id_key UNIQUE(attachment_id);
|
||||
ALTER TABLE attachment_jobs DROP COLUMN superseded_at;
|
||||
ALTER TABLE attachments DROP COLUMN pipeline_version,DROP COLUMN published_pipeline_version;
|
||||
@@ -0,0 +1,19 @@
|
||||
ALTER TABLE attachments ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1 CHECK(pipeline_version BETWEEN 1 AND 2);
|
||||
ALTER TABLE attachments ADD COLUMN published_pipeline_version integer NOT NULL DEFAULT 0 CHECK(published_pipeline_version BETWEEN 0 AND 2);
|
||||
UPDATE attachments SET published_pipeline_version=1 WHERE status='ready';
|
||||
ALTER TABLE attachment_jobs DROP CONSTRAINT attachment_jobs_attachment_id_key;
|
||||
ALTER TABLE attachment_jobs ADD CONSTRAINT attachment_jobs_version_key UNIQUE(attachment_id,pipeline_version);
|
||||
ALTER TABLE attachment_jobs ADD COLUMN superseded_at timestamptz;
|
||||
CREATE TABLE attachment_pipeline_upgrades (
|
||||
id bigserial PRIMARY KEY, attachment_id uuid NOT NULL REFERENCES attachments(id),
|
||||
from_version integer NOT NULL,to_version integer NOT NULL,reason text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE TABLE attachment_publications (
|
||||
id bigserial PRIMARY KEY,attachment_id uuid NOT NULL REFERENCES attachments(id),
|
||||
job_id uuid NOT NULL REFERENCES attachment_jobs(id),pipeline_version integer NOT NULL,
|
||||
variants jsonb NOT NULL,published_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
INSERT INTO attachment_publications(attachment_id,job_id,pipeline_version,variants)
|
||||
SELECT a.id,j.id,1,a.variants FROM attachments a JOIN attachment_jobs j ON j.attachment_id=a.id
|
||||
WHERE a.status='ready';
|
||||
@@ -0,0 +1,74 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// VariantObjectKey separates effects of different pipelines, including retries.
|
||||
func VariantObjectKey(id string, version int, name string) string {
|
||||
if version < 1 {
|
||||
version = 1
|
||||
}
|
||||
extension := ".jpg"
|
||||
if name == "optimized" {
|
||||
extension = ".webp"
|
||||
}
|
||||
return fmt.Sprintf("variants/%s/v%d/%s%s", id, version, name, extension)
|
||||
}
|
||||
|
||||
// UpgradePipeline is an explicit operator action. It never downgrades a result.
|
||||
// The row lock serializes this change with publication, replay and cleanup.
|
||||
func (s *AttachmentStore) UpgradePipeline(ctx context.Context, id string, version int, reason string, exists func(context.Context, string) error) (bool, error) {
|
||||
reason = strings.TrimSpace(reason)
|
||||
if reason == "" || utf8.RuneCountInString(reason) > 200 {
|
||||
return false, errors.New("upgrade reason must contain 1-200 characters")
|
||||
}
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var current int
|
||||
var status, original, source string
|
||||
err = tx.QueryRow(ctx, `SELECT a.pipeline_version,a.status,a.object_key,j.staging_key FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE a.id=$1 AND j.pipeline_version=a.pipeline_version FOR UPDATE OF j,a`, id).Scan(¤t, &status, &original, &source)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if version <= current {
|
||||
return false, nil
|
||||
}
|
||||
if version != current+1 || version > 2 {
|
||||
return false, errors.New("only sequential upgrade to pipeline 2 is supported")
|
||||
}
|
||||
|
||||
if status == "ready" {
|
||||
source = original
|
||||
}
|
||||
if source != "staging/jobs/"+id && source != "originals/"+id {
|
||||
return false, errors.New("invalid pipeline source")
|
||||
}
|
||||
if err = exists(ctx, source); err != nil {
|
||||
return false, errors.New("pipeline source unavailable")
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachment_jobs SET superseded_at=clock_timestamp(),lease_token=NULL,lease_until=NULL WHERE attachment_id=$1 AND pipeline_version<$2`, id, version); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachment_job_attempts SET finished_at=clock_timestamp(),error_code='superseded' WHERE job_id IN (SELECT id FROM attachment_jobs WHERE attachment_id=$1 AND pipeline_version<$2) AND finished_at IS NULL`, id, version); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id,staging_key,pipeline_version) VALUES($1,$2,$3,$4)`, uuid.NewString(), id, source, version); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachments SET pipeline_version=$2,status='queued',error_code=NULL,updated_at=clock_timestamp() WHERE id=$1`, id, version); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO attachment_pipeline_upgrades(attachment_id,from_version,to_version,reason) VALUES($1,$2,$3,$4)`, id, current, version, reason); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, tx.Commit(ctx)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var ErrSessionNotFound = errors.New("session not found")
|
||||
|
||||
type SessionStore struct {
|
||||
client *redis.Client
|
||||
prefix string
|
||||
}
|
||||
|
||||
func NewRedisClient(address string) *redis.Client {
|
||||
return redis.NewClient(&redis.Options{
|
||||
Addr: address, PoolSize: 10, MaxRetries: -1,
|
||||
DialTimeout: time.Second, ReadTimeout: time.Second, WriteTimeout: time.Second,
|
||||
})
|
||||
}
|
||||
|
||||
func NewSessionStore(client *redis.Client, prefix string) *SessionStore {
|
||||
return &SessionStore{client: client, prefix: prefix}
|
||||
}
|
||||
|
||||
func (s *SessionStore) key(tokenHash []byte) string {
|
||||
return s.prefix + hex.EncodeToString(tokenHash)
|
||||
}
|
||||
|
||||
func (s *SessionStore) Create(ctx context.Context, tokenHash []byte, userID string, expires time.Time) error {
|
||||
ttl := time.Until(expires)
|
||||
if ttl <= 0 {
|
||||
return errors.New("session expiry must be in the future")
|
||||
}
|
||||
return s.client.Set(ctx, s.key(tokenHash), userID, ttl).Err()
|
||||
}
|
||||
|
||||
func (s *SessionStore) UserID(ctx context.Context, tokenHash []byte) (string, error) {
|
||||
id, err := s.client.Get(ctx, s.key(tokenHash)).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return "", ErrSessionNotFound
|
||||
}
|
||||
return id, err
|
||||
}
|
||||
|
||||
func (s *SessionStore) Revoke(ctx context.Context, tokenHash []byte) error {
|
||||
return s.client.Del(ctx, s.key(tokenHash)).Err()
|
||||
}
|
||||
|
||||
func (s *SessionStore) Ping(ctx context.Context) error {
|
||||
return s.client.Ping(ctx).Err()
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ObjectStore struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
func NewObjectStoreFromEnvironment() (*ObjectStore, error) {
|
||||
endpoint, err := url.Parse(os.Getenv("S3_ENDPOINT"))
|
||||
if err != nil || endpoint.Host == "" || (endpoint.Scheme != "http" && endpoint.Scheme != "https") || endpoint.Path != "" {
|
||||
return nil, errors.New("invalid S3_ENDPOINT")
|
||||
}
|
||||
read := func(name string) (string, error) {
|
||||
b, e := os.ReadFile(os.Getenv(name))
|
||||
if e != nil {
|
||||
return "", errors.New("cannot read " + name)
|
||||
}
|
||||
v := strings.TrimSpace(string(b))
|
||||
if v == "" {
|
||||
return "", errors.New("empty " + name)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
access, err := read("S3_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
secret, err := read("S3_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
transport.ResponseHeaderTimeout = 5 * time.Second
|
||||
transport.MaxIdleConnsPerHost = 8
|
||||
client, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(access, secret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION"), Transport: transport})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bucket := os.Getenv("S3_BUCKET")
|
||||
if bucket == "" {
|
||||
return nil, errors.New("S3_BUCKET is required")
|
||||
}
|
||||
return &ObjectStore{client: client, bucket: bucket}, nil
|
||||
}
|
||||
func (s *ObjectStore) Ping(ctx context.Context) error {
|
||||
exists, err := s.client.BucketExists(ctx, s.bucket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return errors.New("bucket missing")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (s *ObjectStore) Bootstrap(ctx context.Context) error {
|
||||
exists, err := s.client.BucketExists(ctx, s.bucket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
if err = s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{Region: os.Getenv("S3_REGION")}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// No anonymous policy: every object operation requires signed credentials.
|
||||
return s.client.SetBucketPolicy(ctx, s.bucket, "")
|
||||
}
|
||||
func (s *ObjectStore) Put(ctx context.Context, key string, body io.Reader, size int64, contentType string) error {
|
||||
_, err := s.client.PutObject(ctx, s.bucket, key, body, size, minio.PutObjectOptions{ContentType: contentType, DisableMultipart: true})
|
||||
return err
|
||||
}
|
||||
func (s *ObjectStore) Delete(ctx context.Context, key string) error {
|
||||
return s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{})
|
||||
}
|
||||
func (s *ObjectStore) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
object, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// GetObject is lazy: force the request before writing HTTP 200 headers.
|
||||
if _, err = object.Stat(); err != nil {
|
||||
object.Close()
|
||||
return nil, err
|
||||
}
|
||||
return object, nil
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
madmin "github.com/minio/madmin-go/v3"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"github.com/minio/minio-go/v7/pkg/lifecycle"
|
||||
)
|
||||
|
||||
func readS3Secret(name string) (string, error) {
|
||||
data, err := os.ReadFile(os.Getenv(name))
|
||||
if err != nil {
|
||||
return "", errors.New("cannot read " + name)
|
||||
}
|
||||
value := strings.TrimSpace(string(data))
|
||||
if value == "" {
|
||||
return "", errors.New("empty " + name)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// BootstrapS3 is the only application command that needs administrative credentials.
|
||||
func BootstrapS3(ctx context.Context) error {
|
||||
endpoint, err := url.Parse(os.Getenv("S3_ENDPOINT"))
|
||||
if err != nil || endpoint.Host == "" {
|
||||
return errors.New("invalid S3_ENDPOINT")
|
||||
}
|
||||
rootAccess, err := readS3Secret("S3_ROOT_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rootSecret, err := readS3Secret("S3_ROOT_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
access, err := readS3Secret("S3_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
secret, err := readS3Secret("S3_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if access == rootAccess {
|
||||
return errors.New("API and root credentials must differ")
|
||||
}
|
||||
client, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(rootAccess, rootSecret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION")})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
store := &ObjectStore{client: client, bucket: os.Getenv("S3_BUCKET")}
|
||||
if err = store.Bootstrap(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = bootstrapDirectLifecycle(ctx, client, store.bucket); err != nil {
|
||||
return err
|
||||
}
|
||||
admin, err := madmin.New(endpoint.Host, rootAccess, rootSecret, endpoint.Scheme == "https")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
policy := map[string]any{"Version": "2012-10-17", "Statement": []any{
|
||||
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetBucketLocation", "s3:ListBucket"}, "Resource": []string{"arn:aws:s3:::" + store.bucket}},
|
||||
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetObject", "s3:PutObject", "s3:DeleteObject"}, "Resource": []string{"arn:aws:s3:::" + store.bucket + "/originals/*", "arn:aws:s3:::" + store.bucket + "/staging/direct/*", "arn:aws:s3:::" + store.bucket + "/staging/jobs/*", "arn:aws:s3:::" + store.bucket + "/variants/*"}},
|
||||
}}
|
||||
data, err := json.Marshal(policy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
const name = "messenger-attachments-api"
|
||||
if err = admin.AddCannedPolicy(ctx, name, data); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = admin.SetUserReq(ctx, access, madmin.AddOrUpdateUserReq{SecretKey: secret, Status: madmin.AccountEnabled}); err != nil {
|
||||
return err
|
||||
}
|
||||
// Explicitly assign the managed policy; creating a user alone grants no S3 rights.
|
||||
// This idempotent API is supported by our pinned MinIO server release.
|
||||
return admin.SetPolicy(ctx, name, access, false)
|
||||
}
|
||||
|
||||
func bootstrapDirectLifecycle(ctx context.Context, client *minio.Client, bucket string) error {
|
||||
config, err := client.GetBucketLifecycle(ctx, bucket)
|
||||
if err != nil && minio.ToErrorResponse(err).Code != "NoSuchLifecycleConfiguration" {
|
||||
return err
|
||||
}
|
||||
if config == nil {
|
||||
config = lifecycle.NewConfiguration()
|
||||
}
|
||||
const id = "messenger-direct-staging-expiration"
|
||||
rules := make([]lifecycle.Rule, 0, len(config.Rules)+1)
|
||||
for _, rule := range config.Rules {
|
||||
if rule.ID != id {
|
||||
rules = append(rules, rule)
|
||||
}
|
||||
}
|
||||
rules = append(rules, lifecycle.Rule{ID: id, Status: "Enabled", RuleFilter: lifecycle.Filter{Prefix: "staging/direct/"}, Expiration: lifecycle.Expiration{Days: 1}})
|
||||
config.Rules = rules
|
||||
return client.SetBucketLifecycle(ctx, bucket, config)
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type PostUpload struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Fields map[string]string `json:"fields"`
|
||||
}
|
||||
|
||||
type StoredObject struct {
|
||||
Size int64
|
||||
ETag string
|
||||
}
|
||||
|
||||
func IsObjectMissing(err error) bool {
|
||||
code := minio.ToErrorResponse(err).Code
|
||||
return code == "NoSuchKey" || code == "NoSuchObject" || code == "NotFound"
|
||||
}
|
||||
|
||||
func (s *ObjectStore) PresignDirect(ctx context.Context, key, mime string, size int64, expires time.Time) (PostUpload, error) {
|
||||
endpoint, err := url.Parse(os.Getenv("S3_PUBLIC_ENDPOINT"))
|
||||
if err != nil || endpoint.Host == "" || (endpoint.Scheme != "http" && endpoint.Scheme != "https") || endpoint.Path != "" || endpoint.RawQuery != "" || endpoint.User != nil {
|
||||
return PostUpload{}, errors.New("invalid S3_PUBLIC_ENDPOINT")
|
||||
}
|
||||
access, err := readS3Secret("S3_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
secret, err := readS3Secret("S3_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
signer, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(access, secret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION"), BucketLookup: minio.BucketLookupPath})
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
policy := minio.NewPostPolicy()
|
||||
for _, err = range []error{policy.SetBucket(s.bucket), policy.SetKey(key), policy.SetExpires(expires), policy.SetContentType(mime), policy.SetContentLengthRange(size, size), policy.SetSuccessStatusAction("204")} {
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
}
|
||||
target, fields, err := signer.PresignedPostPolicy(ctx, policy)
|
||||
if err != nil {
|
||||
return PostUpload{}, errors.New("cannot sign upload policy")
|
||||
}
|
||||
return PostUpload{Method: "POST", URL: target.String(), Fields: fields}, nil
|
||||
}
|
||||
|
||||
func (s *ObjectStore) Stat(ctx context.Context, key string) (StoredObject, error) {
|
||||
object, err := s.client.StatObject(ctx, s.bucket, key, minio.StatObjectOptions{})
|
||||
return StoredObject{Size: object.Size, ETag: object.ETag}, err
|
||||
}
|
||||
|
||||
func (s *ObjectStore) Copy(ctx context.Context, source, destination, etag string) error {
|
||||
_, err := s.client.CopyObject(ctx, minio.CopyDestOptions{Bucket: s.bucket, Object: destination}, minio.CopySrcOptions{Bucket: s.bucket, Object: source, MatchETag: etag})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const queryTimeout = 5 * time.Second
|
||||
|
||||
func boundedQuery(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(ctx, queryTimeout)
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// ReserveUpload is committed before any S3 PUT, so an interrupted request is tracked.
|
||||
func (s *AttachmentStore) ReserveUpload(ctx context.Context, a Attachment, grace time.Duration) error {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
_, err := s.db.Exec(ctx, `INSERT INTO upload_attempts(id,object_key,cleanup_after) VALUES($1,$2,clock_timestamp()+$3*interval '1 second')`, a.ID, a.ObjectKey, grace.Seconds())
|
||||
return err
|
||||
}
|
||||
|
||||
// PublishUpload holds the journal row lock across PUT and metadata COMMIT.
|
||||
// Cleanup skips that row; a crash rolls back metadata but preserves the reservation.
|
||||
func (s *AttachmentStore) PublishUpload(ctx context.Context, a Attachment, put func(context.Context) error) error {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var key string
|
||||
err = tx.QueryRow(ctx, `SELECT object_key FROM upload_attempts WHERE id=$1 AND cleanup_after>clock_timestamp() FOR UPDATE`, a.ID).Scan(&key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if key != a.ObjectKey {
|
||||
return errors.New("upload reservation mismatch")
|
||||
}
|
||||
if err = put(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, "originals/"+a.ID, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = enqueueJob(ctx, tx, a, a.ObjectKey); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM upload_attempts WHERE id=$1", a.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
type CleanupResult struct {
|
||||
Candidates int `json:"candidates"`
|
||||
Deleted int `json:"deleted"`
|
||||
Eligible int `json:"eligible"`
|
||||
Protected int `json:"protected"`
|
||||
Skipped int `json:"skipped"`
|
||||
Failed int `json:"failed"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
}
|
||||
|
||||
// CleanupUploads only visits journal entries; it never scans or deletes legacy data.
|
||||
func (s *AttachmentStore) CleanupUploads(ctx context.Context, dryRun bool, remove func(context.Context, string) error) (CleanupResult, error) {
|
||||
result := CleanupResult{DryRun: dryRun}
|
||||
rows, err := s.db.Query(ctx, `SELECT id::text FROM upload_attempts WHERE cleanup_after<=clock_timestamp() ORDER BY cleanup_after,id LIMIT 100`)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
ids := []string{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err = rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return result, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Candidates = len(ids)
|
||||
for _, id := range ids {
|
||||
outcome, err := s.cleanupUpload(ctx, id, dryRun, remove)
|
||||
if err != nil {
|
||||
result.Failed++
|
||||
continue
|
||||
}
|
||||
switch outcome {
|
||||
case "eligible":
|
||||
result.Eligible++
|
||||
case "deleted":
|
||||
result.Deleted++
|
||||
case "protected":
|
||||
result.Protected++
|
||||
default:
|
||||
result.Skipped++
|
||||
}
|
||||
}
|
||||
if result.Failed > 0 {
|
||||
return result, fmt.Errorf("%d upload cleanup operations failed", result.Failed)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) cleanupUpload(ctx context.Context, id string, dryRun bool, remove func(context.Context, string) error) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var key string
|
||||
err = tx.QueryRow(ctx, `SELECT object_key FROM upload_attempts WHERE id=$1 AND cleanup_after<=clock_timestamp() FOR UPDATE SKIP LOCKED`, id).Scan(&key)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "skipped", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// Restrict destructive actions to server-generated keys even if a journal row is malformed.
|
||||
parsed, err := uuid.Parse(id)
|
||||
if err != nil || (key != "originals/"+parsed.String() && key != "staging/jobs/"+parsed.String()) {
|
||||
return "", errors.New("invalid upload journal key")
|
||||
}
|
||||
var ready bool
|
||||
if err = tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM attachments WHERE object_key=$1 UNION ALL SELECT 1 FROM attachment_jobs WHERE staging_key=$1)", key).Scan(&ready); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if dryRun {
|
||||
if ready {
|
||||
return "protected", nil
|
||||
}
|
||||
return "eligible", nil
|
||||
}
|
||||
if !ready {
|
||||
// On DELETE failure retain the journal entry for the next pass.
|
||||
if err = remove(ctx, key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM upload_attempts WHERE id=$1", id); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ready {
|
||||
return "protected", nil
|
||||
}
|
||||
return "deleted", nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type UserStore struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewUserStore(db *pgxpool.Pool) *UserStore {
|
||||
return &UserStore{db: db}
|
||||
}
|
||||
|
||||
func (s *UserStore) Create(ctx context.Context, username, displayName string, passwordHash []byte) (User, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
user := User{
|
||||
ID: uuid.New().String(),
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
|
||||
_, err := s.db.Exec(ctx, "INSERT INTO users(id,username,display_name,password_hash,created_at) VALUES($1,$2,$3,$4,$5)", user.ID, user.Username, user.DisplayName, passwordHash, user.CreatedAt)
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserStore) GetByID(ctx context.Context, id string) (User, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
var user User
|
||||
err := s.db.QueryRow(ctx, "SELECT id,username,display_name,created_at FROM users WHERE id=$1", id).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt)
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserStore) GetCredentials(ctx context.Context, username string) (User, []byte, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
var user User
|
||||
var packedHash []byte
|
||||
err := s.db.QueryRow(ctx, "SELECT id,username,display_name,created_at,password_hash FROM users WHERE username=$1", username).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &packedHash)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return User{}, nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return User{}, nil, err
|
||||
}
|
||||
return user, packedHash, nil
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"messenger/src/backend/repositories"
|
||||
"mime"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
const MaxUploadBytes int64 = 10485760
|
||||
const MaxImagePixels int64 = 40000000
|
||||
|
||||
var ErrPayloadTooLarge = errors.New("payload too large")
|
||||
var ErrUnsupportedMedia = errors.New("unsupported image")
|
||||
var ErrAttachmentNotFound = errors.New("attachment not found")
|
||||
|
||||
type AttachmentService struct {
|
||||
metadata *repositories.AttachmentStore
|
||||
objects AttachmentObjects
|
||||
uploadSlot chan struct{}
|
||||
}
|
||||
|
||||
type AttachmentObjects interface {
|
||||
Put(context.Context, string, io.Reader, int64, string) error
|
||||
Get(context.Context, string) (io.ReadCloser, error)
|
||||
}
|
||||
|
||||
func NewAttachmentService(metadata *repositories.AttachmentStore, objects AttachmentObjects) *AttachmentService {
|
||||
return &AttachmentService{metadata: metadata, objects: objects, uploadSlot: make(chan struct{}, 1)}
|
||||
}
|
||||
func ValidateFilename(name string) error {
|
||||
if !utf8.ValidString(name) || strings.TrimSpace(name) == "" || utf8.RuneCountInString(name) > 255 {
|
||||
return InvalidInput{"filename must contain 1-255 characters"}
|
||||
}
|
||||
for _, r := range name {
|
||||
if r == '/' || r == '\\' || unicode.IsControl(r) {
|
||||
return InvalidInput{"invalid filename"}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func ValidateImage(data []byte, declared string) (string, error) {
|
||||
return ValidateImageReader(bytes.NewReader(data), declared)
|
||||
}
|
||||
|
||||
func ValidateImageReader(reader io.ReadSeeker, declared string) (string, error) {
|
||||
contentType, _, err := mime.ParseMediaType(declared)
|
||||
if err != nil || (contentType != "image/png" && contentType != "image/jpeg") {
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
config, format, err := image.DecodeConfig(reader)
|
||||
if err != nil {
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
actual := "image/" + format
|
||||
if actual != contentType {
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
if config.Width <= 0 || config.Height <= 0 || int64(config.Width) > MaxImagePixels/int64(config.Height) {
|
||||
return "", ErrPayloadTooLarge
|
||||
}
|
||||
// DecodeConfig alone accepts truncated headers; decode only after bounding pixels.
|
||||
if _, err = reader.Seek(0, io.SeekStart); err != nil {
|
||||
return "", err
|
||||
}
|
||||
decoded, _, err := image.Decode(reader)
|
||||
if err != nil {
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
_ = decoded
|
||||
return actual, nil
|
||||
}
|
||||
func (s *AttachmentService) Upload(ctx context.Context, userID, chatID, filename, contentType string, body io.Reader) (repositories.Attachment, error) {
|
||||
select {
|
||||
case s.uploadSlot <- struct{}{}:
|
||||
defer func() { <-s.uploadSlot }()
|
||||
case <-ctx.Done():
|
||||
return repositories.Attachment{}, ctx.Err()
|
||||
}
|
||||
grace, err := repositories.UploadCleanupGrace()
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
directory := os.Getenv("UPLOAD_TMP_DIR")
|
||||
file, err := os.CreateTemp(directory, "upload-*")
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
defer file.Close()
|
||||
// Linux keeps the open descriptor usable; no abandoned pathname survives a crash.
|
||||
if err = os.Remove(file.Name()); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
hash := sha256.New()
|
||||
size, err := io.CopyBuffer(io.MultiWriter(file, hash), io.LimitReader(body, MaxUploadBytes+1), make([]byte, 32*1024))
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if size > MaxUploadBytes {
|
||||
return repositories.Attachment{}, ErrPayloadTooLarge
|
||||
}
|
||||
if err = ctx.Err(); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if _, err = file.Seek(0, io.SeekStart); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
actual, err := ValidateImageReader(file, contentType)
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if err = ctx.Err(); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if _, err = file.Seek(0, io.SeekStart); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
now := time.Now().UTC().Truncate(time.Microsecond)
|
||||
a := repositories.Attachment{ID: uuid.NewString(), ChatID: chatID, OwnerID: userID, Filename: filename, ContentType: actual, SizeBytes: size, SHA256: hex.EncodeToString(hash.Sum(nil)), Status: "queued", Variants: []any{}, CreatedAt: now, UpdatedAt: now}
|
||||
a.ObjectKey = "staging/jobs/" + a.ID
|
||||
if err = s.metadata.ReserveUpload(ctx, a, grace); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
err = s.metadata.PublishUpload(ctx, a, func(ctx context.Context) error {
|
||||
return s.objects.Put(ctx, a.ObjectKey, file, a.SizeBytes, a.ContentType)
|
||||
})
|
||||
// On any uncertainty leave the durable reservation to the cleanup worker.
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
func (s *AttachmentService) Get(ctx context.Context, id, userID string) (repositories.Attachment, error) {
|
||||
a, err := s.metadata.GetForMember(ctx, id, userID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return a, ErrAttachmentNotFound
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
func (s *AttachmentService) Content(ctx context.Context, a repositories.Attachment) (io.ReadCloser, error) {
|
||||
return s.objects.Get(ctx, a.ObjectKey)
|
||||
}
|
||||
|
||||
func NormalizeAttachmentIDs(ids []string) ([]string, error) {
|
||||
if len(ids) > 10 {
|
||||
return nil, InvalidInput{"at most 10 attachments are allowed"}
|
||||
}
|
||||
result := make([]string, 0, len(ids))
|
||||
seen := map[string]bool{}
|
||||
for _, id := range ids {
|
||||
parsed, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
return nil, InvalidInput{"invalid attachment id"}
|
||||
}
|
||||
id = strings.ToLower(parsed.String())
|
||||
if seen[id] {
|
||||
return nil, InvalidInput{"duplicate attachment id"}
|
||||
}
|
||||
seen[id] = true
|
||||
result = append(result, id)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ChatPage struct {
|
||||
Items []repositories.Chat `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
type ChatService struct {
|
||||
chats *repositories.ChatStore
|
||||
}
|
||||
|
||||
func NewChatService(chats *repositories.ChatStore) *ChatService {
|
||||
return &ChatService{chats: chats}
|
||||
}
|
||||
|
||||
func ChatHasMember(chat repositories.Chat, userID string) bool {
|
||||
for _, memberID := range chat.MemberIDs {
|
||||
if memberID == userID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *ChatService) Create(ctx context.Context, currentUserID, title string, memberIDs []string) (repositories.Chat, error) {
|
||||
if strings.TrimSpace(title) == "" {
|
||||
return repositories.Chat{}, InvalidInput{"title must not be blank"}
|
||||
}
|
||||
if utf8.RuneCountInString(title) > 100 {
|
||||
return repositories.Chat{}, InvalidInput{"title is too long"}
|
||||
}
|
||||
if len(memberIDs) == 0 || len(memberIDs) > 100 {
|
||||
return repositories.Chat{}, InvalidInput{"member_ids must contain from 1 to 100 users"}
|
||||
}
|
||||
seen := make(map[string]struct{}, len(memberIDs))
|
||||
normalizedMembers := make([]string, len(memberIDs))
|
||||
for i, memberID := range memberIDs {
|
||||
parsed, err := uuid.Parse(memberID)
|
||||
if err != nil {
|
||||
return repositories.Chat{}, InvalidInput{"member_ids must contain valid UUIDs"}
|
||||
}
|
||||
memberID = parsed.String()
|
||||
if _, exists := seen[memberID]; exists {
|
||||
return repositories.Chat{}, InvalidInput{"member_ids must not contain duplicates"}
|
||||
}
|
||||
seen[memberID] = struct{}{}
|
||||
normalizedMembers[i] = memberID
|
||||
}
|
||||
if _, exists := seen[currentUserID]; !exists {
|
||||
return repositories.Chat{}, InvalidInput{"current user must be a chat member"}
|
||||
}
|
||||
chat, err := s.chats.Create(ctx, title, normalizedMembers)
|
||||
if errors.Is(err, repositories.ErrUnknownMember) {
|
||||
return repositories.Chat{}, InvalidInput{"all members must be existing users"}
|
||||
}
|
||||
return chat, err
|
||||
}
|
||||
|
||||
func (s *ChatService) Get(ctx context.Context, currentUserID, chatID string) (repositories.Chat, error) {
|
||||
chat, err := s.chats.GetByID(ctx, chatID)
|
||||
if repositories.NotFound(err) {
|
||||
return repositories.Chat{}, ErrChatNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return repositories.Chat{}, err
|
||||
}
|
||||
if !ChatHasMember(chat, currentUserID) {
|
||||
return repositories.Chat{}, ErrChatNotFound
|
||||
}
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func EncodeChatCursor(cursor repositories.ChatCursor) (string, error) {
|
||||
data, err := json.Marshal(cursor)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(data), nil
|
||||
}
|
||||
|
||||
func DecodeChatCursor(value string) (repositories.ChatCursor, error) {
|
||||
data, err := base64.RawURLEncoding.DecodeString(value)
|
||||
if err != nil {
|
||||
return repositories.ChatCursor{}, err
|
||||
}
|
||||
var cursor repositories.ChatCursor
|
||||
if err := json.Unmarshal(data, &cursor); err != nil {
|
||||
return repositories.ChatCursor{}, err
|
||||
}
|
||||
if cursor.UserID == "" || cursor.ChatID == "" || cursor.CreatedAt.IsZero() {
|
||||
return repositories.ChatCursor{}, strconv.ErrSyntax
|
||||
}
|
||||
if _, err := uuid.Parse(cursor.UserID); err != nil {
|
||||
return repositories.ChatCursor{}, err
|
||||
}
|
||||
if _, err := uuid.Parse(cursor.ChatID); err != nil {
|
||||
return repositories.ChatCursor{}, err
|
||||
}
|
||||
return cursor, nil
|
||||
}
|
||||
|
||||
func (s *ChatService) List(ctx context.Context, userID string, limit int, cursorToken string) (ChatPage, error) {
|
||||
if limit < 1 || limit > 100 {
|
||||
return ChatPage{}, InvalidInput{"limit must be between 1 and 100"}
|
||||
}
|
||||
var cursor *repositories.ChatCursor
|
||||
if cursorToken != "" {
|
||||
decoded, err := DecodeChatCursor(cursorToken)
|
||||
if err != nil || decoded.UserID != userID {
|
||||
return ChatPage{}, InvalidInput{"invalid cursor"}
|
||||
}
|
||||
cursor = &decoded
|
||||
}
|
||||
items, more, err := s.chats.ListPageForUser(ctx, userID, cursor, limit)
|
||||
if err != nil {
|
||||
return ChatPage{}, err
|
||||
}
|
||||
page := ChatPage{Items: items}
|
||||
if page.Items == nil {
|
||||
page.Items = []repositories.Chat{}
|
||||
}
|
||||
if more {
|
||||
last := items[len(items)-1]
|
||||
next, err := EncodeChatCursor(repositories.ChatCursor{UserID: userID, CreatedAt: last.CreatedAt, ChatID: last.ID})
|
||||
if err != nil {
|
||||
return ChatPage{}, err
|
||||
}
|
||||
page.NextCursor = &next
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type DirectUploadInput struct {
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
type DirectUploadResponse struct {
|
||||
repositories.DirectUpload
|
||||
Upload repositories.PostUpload `json:"upload"`
|
||||
}
|
||||
|
||||
type DirectObjects interface {
|
||||
AttachmentObjects
|
||||
PresignDirect(context.Context, string, string, int64, time.Time) (repositories.PostUpload, error)
|
||||
Stat(context.Context, string) (repositories.StoredObject, error)
|
||||
Copy(context.Context, string, string, string) error
|
||||
}
|
||||
|
||||
var checksumPattern = regexp.MustCompile(`^[a-f0-9]{64}$`)
|
||||
|
||||
func (s *AttachmentService) InitDirect(ctx context.Context, user, chat string, input DirectUploadInput) (DirectUploadResponse, error) {
|
||||
if err := ValidateFilename(input.Filename); err != nil {
|
||||
return DirectUploadResponse{}, err
|
||||
}
|
||||
if input.ContentType != "image/png" && input.ContentType != "image/jpeg" {
|
||||
return DirectUploadResponse{}, ErrUnsupportedMedia
|
||||
}
|
||||
if input.SizeBytes > MaxUploadBytes {
|
||||
return DirectUploadResponse{}, ErrPayloadTooLarge
|
||||
}
|
||||
if input.SizeBytes < 1 || !checksumPattern.MatchString(input.SHA256) {
|
||||
return DirectUploadResponse{}, InvalidInput{"size must be positive and sha256 must contain 64 lowercase hex characters"}
|
||||
}
|
||||
objects, ok := s.objects.(DirectObjects)
|
||||
if !ok {
|
||||
return DirectUploadResponse{}, errors.New("direct uploads unavailable")
|
||||
}
|
||||
id, attachment := uuid.NewString(), uuid.NewString()
|
||||
u, err := s.metadata.CreateDirect(ctx, repositories.DirectUpload{ID: id, AttachmentID: attachment, OwnerID: user, ChatID: chat, Filename: input.Filename, ContentType: input.ContentType, SizeBytes: input.SizeBytes, SHA256: input.SHA256, StagingKey: "staging/direct/" + id, FinalKey: "originals/" + attachment})
|
||||
if err != nil {
|
||||
return DirectUploadResponse{}, err
|
||||
}
|
||||
post, err := objects.PresignDirect(ctx, u.StagingKey, u.ContentType, u.SizeBytes, u.ExpiresAt)
|
||||
return DirectUploadResponse{DirectUpload: u, Upload: post}, err
|
||||
}
|
||||
|
||||
func (s *AttachmentService) CompleteDirect(ctx context.Context, user, chat, id string) (repositories.Attachment, error) {
|
||||
u, err := s.metadata.GetDirect(ctx, id, chat, user)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return repositories.Attachment{}, ErrAttachmentNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if u.State == "ready" {
|
||||
return s.Get(ctx, u.AttachmentID, user)
|
||||
}
|
||||
objects, ok := s.objects.(DirectObjects)
|
||||
if !ok {
|
||||
return repositories.Attachment{}, errors.New("direct uploads unavailable")
|
||||
}
|
||||
select {
|
||||
case s.uploadSlot <- struct{}{}:
|
||||
defer func() { <-s.uploadSlot }()
|
||||
case <-ctx.Done():
|
||||
return repositories.Attachment{}, ctx.Err()
|
||||
}
|
||||
a, err := s.metadata.CompleteDirect(ctx, id, chat, user, func(ctx context.Context, u repositories.DirectUpload) error {
|
||||
info, err := objects.Stat(ctx, u.StagingKey)
|
||||
if repositories.IsObjectMissing(err) {
|
||||
return repositories.DirectError("upload_not_uploaded")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Size > MaxUploadBytes {
|
||||
return repositories.DirectError("payload_too_large")
|
||||
}
|
||||
if info.Size != u.SizeBytes {
|
||||
return repositories.DirectError("upload_size_mismatch")
|
||||
}
|
||||
// A signed staging key remains writable until expiry. Validate the isolated
|
||||
// final object, never a staging object that can change after validation.
|
||||
if err = objects.Copy(ctx, u.StagingKey, u.FinalKey, info.ETag); err != nil {
|
||||
return err
|
||||
}
|
||||
reader, err := objects.Get(ctx, u.FinalKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer reader.Close()
|
||||
file, err := os.CreateTemp(os.Getenv("UPLOAD_TMP_DIR"), "direct-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
if err = os.Remove(file.Name()); err != nil {
|
||||
return err
|
||||
}
|
||||
hash := sha256.New()
|
||||
size, err := io.CopyBuffer(io.MultiWriter(file, hash), io.LimitReader(reader, MaxUploadBytes+1), make([]byte, 32*1024))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if size > MaxUploadBytes {
|
||||
return repositories.DirectError("payload_too_large")
|
||||
}
|
||||
if size != u.SizeBytes {
|
||||
return repositories.DirectError("upload_size_mismatch")
|
||||
}
|
||||
if hex.EncodeToString(hash.Sum(nil)) != u.SHA256 {
|
||||
return repositories.DirectError("upload_checksum_mismatch")
|
||||
}
|
||||
if err = ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = file.Seek(0, io.SeekStart); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = ValidateImageReader(file, u.ContentType)
|
||||
if errors.Is(err, ErrPayloadTooLarge) {
|
||||
return repositories.DirectError("payload_too_large")
|
||||
}
|
||||
if errors.Is(err, ErrUnsupportedMedia) {
|
||||
return repositories.DirectError("unsupported_media_type")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ctx.Err()
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return a, ErrAttachmentNotFound
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package services
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrConflict = errors.New("conflict")
|
||||
ErrUnknownUser = errors.New("unknown user")
|
||||
ErrChatNotFound = errors.New("chat not found")
|
||||
ErrInvalidSession = errors.New("invalid session")
|
||||
)
|
||||
|
||||
type InvalidInput struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e InvalidInput) Error() string { return e.Message }
|
||||
@@ -0,0 +1,34 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
type HealthService struct {
|
||||
health *repositories.HealthStore
|
||||
sessions *repositories.SessionStore
|
||||
objects interface{ Ping(context.Context) error }
|
||||
}
|
||||
|
||||
func NewHealthService(health *repositories.HealthStore, sessions *repositories.SessionStore, objects ...interface{ Ping(context.Context) error }) *HealthService {
|
||||
service := &HealthService{health: health, sessions: sessions}
|
||||
if len(objects) > 0 {
|
||||
service.objects = objects[0]
|
||||
}
|
||||
return service
|
||||
}
|
||||
|
||||
func (s *HealthService) Ready(ctx context.Context) error {
|
||||
if err := s.health.Ping(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.sessions.Ping(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s.objects != nil {
|
||||
return s.objects.Ping(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
func (s *MessageService) CreateIdempotent(ctx context.Context, userID string, chat repositories.Chat, text, key string, attachments ...[]string) (repositories.Message, error) {
|
||||
if !ChatHasMember(chat, userID) {
|
||||
return repositories.Message{}, ErrChatNotFound
|
||||
}
|
||||
if !idempotencyKeyPattern.MatchString(key) {
|
||||
return repositories.Message{}, InvalidInput{"Idempotency-Key must contain 1-128 ASCII letters, digits or ._:-"}
|
||||
}
|
||||
var ids []string
|
||||
if len(attachments) > 0 {
|
||||
ids = attachments[0]
|
||||
}
|
||||
ids, err := NormalizeAttachmentIDs(ids)
|
||||
if err != nil {
|
||||
return repositories.Message{}, err
|
||||
}
|
||||
if (strings.TrimSpace(text) == "" && len(ids) == 0) || utf8.RuneCountInString(text) > 4000 {
|
||||
return repositories.Message{}, InvalidInput{"text must be at most 4000 characters; nonblank text or attachments required"}
|
||||
}
|
||||
message, err := s.messages.CreateIdempotent(ctx, chat.ID, userID, text, key, ids)
|
||||
if errors.Is(err, repositories.ErrAttachmentNotFound) {
|
||||
return repositories.Message{}, ErrAttachmentNotFound
|
||||
}
|
||||
if errors.Is(err, repositories.ErrIdempotencyConflict) || errors.Is(err, repositories.ErrAttachmentFailed) {
|
||||
return repositories.Message{}, ErrConflict
|
||||
}
|
||||
return message, err
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
type MessagePage struct {
|
||||
Items []repositories.Message `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
type MessageService struct {
|
||||
messages *repositories.MessageStore
|
||||
}
|
||||
|
||||
func NewMessageService(messages *repositories.MessageStore) *MessageService {
|
||||
return &MessageService{messages: messages}
|
||||
}
|
||||
|
||||
func EncodeMessageCursor(cursor repositories.MessageCursor) (string, error) {
|
||||
data, err := json.Marshal(cursor)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(data), nil
|
||||
}
|
||||
|
||||
func DecodeMessageCursor(value string) (repositories.MessageCursor, error) {
|
||||
data, err := base64.RawURLEncoding.DecodeString(value)
|
||||
if err != nil {
|
||||
return repositories.MessageCursor{}, err
|
||||
}
|
||||
var cursor repositories.MessageCursor
|
||||
if err := json.Unmarshal(data, &cursor); err != nil {
|
||||
return repositories.MessageCursor{}, err
|
||||
}
|
||||
if cursor.ChatID == "" || cursor.MessageID == "" || cursor.CreatedAt.IsZero() {
|
||||
return repositories.MessageCursor{}, strconv.ErrSyntax
|
||||
}
|
||||
if _, err := uuid.Parse(cursor.ChatID); err != nil {
|
||||
return repositories.MessageCursor{}, err
|
||||
}
|
||||
if _, err := uuid.Parse(cursor.MessageID); err != nil {
|
||||
return repositories.MessageCursor{}, err
|
||||
}
|
||||
return cursor, nil
|
||||
}
|
||||
|
||||
func (s *MessageService) List(ctx context.Context, userID string, chat repositories.Chat, limit int, cursorToken string) (MessagePage, error) {
|
||||
if !ChatHasMember(chat, userID) {
|
||||
return MessagePage{}, ErrChatNotFound
|
||||
}
|
||||
if limit < 1 || limit > 100 {
|
||||
return MessagePage{}, InvalidInput{"limit must be between 1 and 100"}
|
||||
}
|
||||
var cursor *repositories.MessageCursor
|
||||
if cursorToken != "" {
|
||||
decoded, err := DecodeMessageCursor(cursorToken)
|
||||
if err != nil || decoded.ChatID != chat.ID {
|
||||
return MessagePage{}, InvalidInput{"invalid cursor"}
|
||||
}
|
||||
cursor = &decoded
|
||||
}
|
||||
items, more, err := s.messages.ListPageByChat(ctx, chat.ID, cursor, limit)
|
||||
if err != nil {
|
||||
return MessagePage{}, err
|
||||
}
|
||||
page := MessagePage{Items: items}
|
||||
if page.Items == nil {
|
||||
page.Items = []repositories.Message{}
|
||||
}
|
||||
if more {
|
||||
last := items[len(items)-1]
|
||||
next, err := EncodeMessageCursor(repositories.MessageCursor{ChatID: chat.ID, CreatedAt: last.CreatedAt, MessageID: last.ID})
|
||||
if err != nil {
|
||||
return MessagePage{}, err
|
||||
}
|
||||
page.NextCursor = &next
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
const (
|
||||
argonMemory uint32 = 64 * 1024
|
||||
argonIterations uint32 = 3
|
||||
argonParallelism uint8 = 2
|
||||
passwordSaltSize = 16
|
||||
passwordHashSize = 32
|
||||
)
|
||||
|
||||
// HashPassword stores the salt and Argon2id parameters in one PHC-encoded value.
|
||||
func HashPassword(password string) ([]byte, error) {
|
||||
salt := make([]byte, passwordSaltSize)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hash := argon2.IDKey([]byte(password), salt, argonIterations, argonMemory, argonParallelism, passwordHashSize)
|
||||
encoded := fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
||||
argon2.Version, argonMemory, argonIterations, argonParallelism,
|
||||
base64.RawStdEncoding.EncodeToString(salt),
|
||||
base64.RawStdEncoding.EncodeToString(hash),
|
||||
)
|
||||
return []byte(encoded), nil
|
||||
}
|
||||
|
||||
func VerifyPassword(password string, encoded []byte) (bool, error) {
|
||||
parts := strings.Split(string(encoded), "$")
|
||||
if len(parts) != 6 || parts[0] != "" || parts[1] != "argon2id" || parts[2] != fmt.Sprintf("v=%d", argon2.Version) {
|
||||
return false, errors.New("invalid password hash format")
|
||||
}
|
||||
var memory, iterations uint32
|
||||
var parallelism uint8
|
||||
if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &iterations, ¶llelism); err != nil ||
|
||||
fmt.Sprintf("m=%d,t=%d,p=%d", memory, iterations, parallelism) != parts[3] ||
|
||||
memory < 8*1024 || memory > 128*1024 || iterations < 1 || iterations > 10 || parallelism < 1 || parallelism > 8 {
|
||||
return false, errors.New("invalid password hash parameters")
|
||||
}
|
||||
salt, err := base64.RawStdEncoding.Strict().DecodeString(parts[4])
|
||||
if err != nil || len(salt) != passwordSaltSize {
|
||||
return false, errors.New("invalid password salt")
|
||||
}
|
||||
expected, err := base64.RawStdEncoding.Strict().DecodeString(parts[5])
|
||||
if err != nil || len(expected) != passwordHashSize {
|
||||
return false, errors.New("invalid password hash")
|
||||
}
|
||||
actual := argon2.IDKey([]byte(password), salt, iterations, memory, parallelism, passwordHashSize)
|
||||
return subtle.ConstantTimeCompare(expected, actual) == 1, nil
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
type SessionService struct {
|
||||
users *UserService
|
||||
sessions *repositories.SessionStore
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
func SessionTTL() (time.Duration, error) {
|
||||
value := os.Getenv("SESSION_TTL_SECONDS")
|
||||
if value == "" {
|
||||
return time.Hour, nil
|
||||
}
|
||||
seconds, err := strconv.Atoi(value)
|
||||
if err != nil || seconds < 1 || seconds > 31536000 {
|
||||
return 0, errors.New("invalid SESSION_TTL_SECONDS")
|
||||
}
|
||||
return time.Duration(seconds) * time.Second, nil
|
||||
}
|
||||
|
||||
func NewSessionService(users *UserService, sessions *repositories.SessionStore, ttl time.Duration) *SessionService {
|
||||
return &SessionService{users: users, sessions: sessions, ttl: ttl}
|
||||
}
|
||||
|
||||
func (s *SessionService) Login(ctx context.Context, username, password string) (repositories.User, string, time.Time, bool, error) {
|
||||
user, valid, err := s.users.Authenticate(ctx, username, password)
|
||||
if err != nil || !valid {
|
||||
return repositories.User{}, "", time.Time{}, valid, err
|
||||
}
|
||||
token, expires, err := s.CreateForUser(ctx, user.ID)
|
||||
if err != nil {
|
||||
return repositories.User{}, "", time.Time{}, false, err
|
||||
}
|
||||
return user, token, expires, true, nil
|
||||
}
|
||||
|
||||
func (s *SessionService) CreateForUser(ctx context.Context, userID string) (string, time.Time, error) {
|
||||
raw := make([]byte, 32)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return "", time.Time{}, err
|
||||
}
|
||||
token := hex.EncodeToString(raw)
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
expires := time.Now().UTC().Add(s.ttl).Truncate(time.Microsecond)
|
||||
if err := s.sessions.Create(ctx, hash[:], userID, expires); err != nil {
|
||||
return "", time.Time{}, err
|
||||
}
|
||||
return token, expires, nil
|
||||
}
|
||||
|
||||
func (s *SessionService) User(ctx context.Context, token string) (repositories.User, error) {
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
id, err := s.sessions.UserID(ctx, hash[:])
|
||||
if errors.Is(err, repositories.ErrSessionNotFound) {
|
||||
return repositories.User{}, ErrInvalidSession
|
||||
}
|
||||
if err != nil {
|
||||
return repositories.User{}, err
|
||||
}
|
||||
user, err := s.users.Get(ctx, id)
|
||||
if errors.Is(err, ErrUnknownUser) {
|
||||
return repositories.User{}, ErrInvalidSession
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *SessionService) Revoke(ctx context.Context, token string) error {
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
return s.sessions.Revoke(ctx, hash[:])
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
type UserService struct {
|
||||
users *repositories.UserStore
|
||||
}
|
||||
|
||||
func NewUserService(users *repositories.UserStore) *UserService {
|
||||
return &UserService{users: users}
|
||||
}
|
||||
|
||||
func (s *UserService) Create(ctx context.Context, username, displayName, password string) (repositories.User, error) {
|
||||
if !usernamePattern.MatchString(username) {
|
||||
return repositories.User{}, InvalidInput{"username must contain 3-32 lowercase latin letters, digits or underscores"}
|
||||
}
|
||||
if strings.TrimSpace(displayName) == "" {
|
||||
return repositories.User{}, InvalidInput{"display_name must not be blank"}
|
||||
}
|
||||
if utf8.RuneCountInString(displayName) > 100 {
|
||||
return repositories.User{}, InvalidInput{"display_name is too long"}
|
||||
}
|
||||
if strings.TrimSpace(password) == "" || utf8.RuneCountInString(password) < 12 || utf8.RuneCountInString(password) > 128 {
|
||||
return repositories.User{}, InvalidInput{"password must contain 12-128 characters"}
|
||||
}
|
||||
hash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
return repositories.User{}, err
|
||||
}
|
||||
user, err := s.users.Create(ctx, username, displayName, hash)
|
||||
if repositories.UniqueViolation(err) {
|
||||
return repositories.User{}, ErrConflict
|
||||
}
|
||||
if repositories.CheckViolation(err) {
|
||||
return repositories.User{}, InvalidInput{"invalid user fields"}
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserService) Get(ctx context.Context, id string) (repositories.User, error) {
|
||||
user, err := s.users.GetByID(ctx, id)
|
||||
if repositories.NotFound(err) {
|
||||
return repositories.User{}, ErrUnknownUser
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserService) Authenticate(ctx context.Context, username, password string) (repositories.User, bool, error) {
|
||||
if !usernamePattern.MatchString(username) || utf8.RuneCountInString(password) < 12 || utf8.RuneCountInString(password) > 128 {
|
||||
return repositories.User{}, false, nil
|
||||
}
|
||||
user, packedHash, err := s.users.GetCredentials(ctx, username)
|
||||
if repositories.NotFound(err) {
|
||||
return repositories.User{}, false, nil
|
||||
}
|
||||
if err != nil || packedHash == nil {
|
||||
return repositories.User{}, false, err
|
||||
}
|
||||
valid, err := VerifyPassword(password, packedHash)
|
||||
if err != nil {
|
||||
return repositories.User{}, false, err
|
||||
}
|
||||
return user, valid, nil
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestAttachmentCleanupGraceLocksProtectionAndRetry(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
data := uploadPNG(t)
|
||||
makeAttempt := func(age bool, ready bool) repositories.Attachment {
|
||||
id := uuid.NewString()
|
||||
now := time.Now().UTC()
|
||||
a := repositories.Attachment{ID: id, ObjectKey: "originals/" + id, ChatID: chat, OwnerID: user, Filename: "cleanup.png", ContentType: "image/png", SizeBytes: int64(len(data)), SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", CreatedAt: now, UpdatedAt: now}
|
||||
if err := objects.Put(ctx, a.ObjectKey, bytes.NewReader(data), a.SizeBytes, a.ContentType); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { objects.Delete(context.Background(), a.ObjectKey) })
|
||||
if err := store.ReserveUpload(ctx, a, 600*time.Second); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if age {
|
||||
if _, err := f.db.Exec(ctx, "UPDATE upload_attempts SET cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1", id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if ready {
|
||||
if err := store.Create(ctx, a); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
fresh := makeAttempt(false, false)
|
||||
expired := makeAttempt(true, false)
|
||||
ready := makeAttempt(true, true)
|
||||
locked := makeAttempt(true, false)
|
||||
tx, err := f.db.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, "SELECT id FROM upload_attempts WHERE id=$1 FOR UPDATE", locked.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dry, err := store.CleanupUploads(ctx, true, objects.Delete)
|
||||
if err != nil || dry.Deleted != 0 || dry.Candidates != 3 || dry.Protected != 1 {
|
||||
t.Fatalf("dry run: %+v %v", dry, err)
|
||||
}
|
||||
result, err := store.CleanupUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Deleted != 1 || result.Protected != 1 || result.Skipped != 1 {
|
||||
t.Fatalf("cleanup: %+v %v", result, err)
|
||||
}
|
||||
if content, err := objects.Get(ctx, expired.ObjectKey); err == nil {
|
||||
content.Close()
|
||||
t.Fatal("expired object not removed")
|
||||
}
|
||||
for _, a := range []repositories.Attachment{fresh, ready, locked} {
|
||||
content, err := objects.Get(ctx, a.ObjectKey)
|
||||
if err != nil {
|
||||
t.Fatalf("protected object removed: %v", err)
|
||||
}
|
||||
content.Close()
|
||||
}
|
||||
tx.Rollback(ctx)
|
||||
failure, err := store.CleanupUploads(ctx, false, func(context.Context, string) error { return errors.New("injected S3 DELETE failure") })
|
||||
if err == nil || failure.Failed != 1 {
|
||||
t.Fatalf("DELETE failure: %+v %v", failure, err)
|
||||
}
|
||||
var count int
|
||||
f.db.QueryRow(ctx, "SELECT count(*) FROM upload_attempts WHERE id=$1", locked.ID).Scan(&count)
|
||||
if count != 1 {
|
||||
t.Fatal("DELETE failure lost journal")
|
||||
}
|
||||
result, err = store.CleanupUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Deleted != 1 {
|
||||
t.Fatalf("retry: %+v %v", result, err)
|
||||
}
|
||||
result, err = store.CleanupUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Candidates != 0 {
|
||||
t.Fatalf("repeat not idempotent: %+v %v", result, err)
|
||||
}
|
||||
cancelled, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
if _, err = store.CleanupUploads(cancelled, false, objects.Delete); err == nil {
|
||||
t.Fatal("cancelled DB operation succeeded")
|
||||
}
|
||||
f.db.QueryRow(ctx, "SELECT count(*) FROM upload_attempts WHERE id=$1", fresh.ID).Scan(&count)
|
||||
if count != 1 {
|
||||
t.Fatal("DB failure lost fresh reservation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentCleanupInvalidKeyIsNeverDeleted(t *testing.T) {
|
||||
f, store, _, _, _ := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
if _, err := f.db.Exec(ctx, "INSERT INTO upload_attempts(id,object_key,cleanup_after) VALUES($1,$2,clock_timestamp()-interval '1 second')", uuid.NewString(), "do-not-delete/anything"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
called := false
|
||||
result, err := store.CleanupUploads(ctx, false, func(context.Context, string) error { called = true; return nil })
|
||||
if err == nil || called || result.Failed != 1 {
|
||||
t.Fatalf("unsafe key: %+v %v delete=%v", result, err, called)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func attachmentRouter(f *testFixture, objects *repositories.ObjectStore) http.Handler {
|
||||
users := services.NewUserService(f.users)
|
||||
return handlers.NewRouter(services.NewHealthService(repositories.NewHealthStore(f.db), f.sessionStore), users, f.sessions, services.NewChatService(f.chats), services.NewMessageService(f.messages), services.NewAttachmentService(repositories.NewAttachmentStore(f.db), objects))
|
||||
}
|
||||
func attachmentCall(t *testing.T, f *testFixture, router http.Handler, method, path, user, mime string, body []byte, status int) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest(method, path, bytes.NewReader(body))
|
||||
if mime != "" {
|
||||
r.Header.Set("Content-Type", mime)
|
||||
}
|
||||
if method == "POST" && strings.HasSuffix(path, "/messages") {
|
||||
r.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
}
|
||||
if user != "" {
|
||||
authorizeTestRequest(t, f, r, user)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, r)
|
||||
if w.Code != status {
|
||||
t.Fatalf("%s %s: expected %d, got %d: %s", method, path, status, w.Code, w.Body.String())
|
||||
}
|
||||
if status >= 400 {
|
||||
var error struct {
|
||||
Error struct {
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &error); err != nil || error.Error.RequestID != w.Header().Get("X-Request-Id") {
|
||||
t.Fatal("invalid error envelope")
|
||||
}
|
||||
}
|
||||
return w
|
||||
}
|
||||
func TestAttachmentValidationHTTP(t *testing.T) {
|
||||
f, _, objects, user, chat := uploadFixture(t)
|
||||
router := attachmentRouter(f, objects)
|
||||
path := "/api/v1/chats/" + chat + "/attachments?filename=photo.png"
|
||||
png := uploadPNG(t)
|
||||
for _, sample := range []struct {
|
||||
body []byte
|
||||
mime string
|
||||
status int
|
||||
}{{bytes.Repeat([]byte("x"), 10485761), "image/png", 413}, {png[:40], "image/png", 415}, {png, "image/jpeg", 415}, {png, "text/plain", 415}} {
|
||||
attachmentCall(t, f, router, "POST", path, user, sample.mime, sample.body, sample.status)
|
||||
}
|
||||
huge := append([]byte(nil), png...)
|
||||
binary.BigEndian.PutUint32(huge[16:20], 10001)
|
||||
binary.BigEndian.PutUint32(huge[20:24], 4000)
|
||||
binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29]))
|
||||
attachmentCall(t, f, router, "POST", path, user, "image/png", huge, 413)
|
||||
for _, name := range []string{"", " ", "../file.png", `a\b.png`, "a\x00.png", "a\n.png", strings.Repeat("x", 256)} {
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/attachments?"+url.Values{"filename": {name}}.Encode(), user, "image/png", png, 400)
|
||||
}
|
||||
// Exercise the real HTTP chunked path without Content-Length.
|
||||
server := httptest.NewServer(router)
|
||||
defer server.Close()
|
||||
token, _, err := f.sessions.CreateForUser(context.Background(), user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reader, writer := io.Pipe()
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
defer writer.Close()
|
||||
io.Copy(writer, bytes.NewReader(bytes.Repeat([]byte("x"), 10485761)))
|
||||
}()
|
||||
request, err := http.NewRequest("POST", server.URL+path, reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
request.Header.Set("Content-Type", "image/png")
|
||||
response, err := server.Client().Do(request)
|
||||
if err != nil {
|
||||
reader.Close()
|
||||
<-done
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
reader.Close()
|
||||
<-done
|
||||
if response.StatusCode != 413 {
|
||||
t.Fatalf("chunked limit: %d", response.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentAccessHTTP(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
router := attachmentRouter(f, objects)
|
||||
eve, _ := createTestUser(t, f.users, "eve", "Eve")
|
||||
other := createTestChat(t, f.chats, "Other", []string{user})
|
||||
a, err := services.NewAttachmentService(store, objects).Upload(context.Background(), user, chat, "photo.png", "image/png", bytes.NewReader(uploadPNG(t)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { objects.Delete(context.Background(), a.ObjectKey) })
|
||||
for _, suffix := range []string{"", "/content"} {
|
||||
path := "/api/v1/attachments/" + a.ID + suffix
|
||||
attachmentCall(t, f, router, "GET", path, eve.ID, "", nil, 404)
|
||||
attachmentCall(t, f, router, "GET", path, "", "", nil, 401)
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/attachments?filename=photo.png", eve.ID, "image/png", uploadPNG(t), 404)
|
||||
for _, target := range []string{uuid.NewString(), a.ID} {
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+other.ID+"/messages", user, "application/json", []byte(`{"text":"","attachment_ids":["`+target+`"]}`), 404)
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/messages", user, "application/json", []byte(`{"text":"","attachment_ids":["`+a.ID+`","`+a.ID+`"]}`), 400)
|
||||
}
|
||||
|
||||
func TestUploadRecoveryHTTPDisconnect(t *testing.T) {
|
||||
f, _, objects, user, chat := uploadFixture(t)
|
||||
directory := t.TempDir()
|
||||
t.Setenv("UPLOAD_TMP_DIR", directory)
|
||||
completed := make(chan struct{})
|
||||
router := attachmentRouter(f, objects)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer close(completed); router.ServeHTTP(w, r) }))
|
||||
defer server.Close()
|
||||
token, _, err := f.sessions.CreateForUser(context.Background(), user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
address := strings.TrimPrefix(server.URL, "http://")
|
||||
connection, err := net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw := "POST /api/v1/chats/" + chat + "/attachments?filename=aborted.png HTTP/1.1\r\nHost: " + address + "\r\nAuthorization: Bearer " + token + "\r\nContent-Type: image/png\r\nContent-Length: 10485760\r\n\r\npartial"
|
||||
if _, err = io.WriteString(connection, raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
connection.Close()
|
||||
select {
|
||||
case <-completed:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("disconnected upload did not stop")
|
||||
}
|
||||
var count int
|
||||
f.db.QueryRow(context.Background(), "SELECT count(*) FROM upload_attempts").Scan(&count)
|
||||
if count != 0 {
|
||||
t.Fatal("incomplete HTTP body reserved an object")
|
||||
}
|
||||
files, err := os.ReadDir(directory)
|
||||
if err != nil || len(files) > 0 {
|
||||
t.Fatalf("temporary paths remain: %v %v", files, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAttachmentImageValidation(t *testing.T) {
|
||||
img := image.NewRGBA(image.Rect(0, 0, 3, 2))
|
||||
var pngData, jpegData bytes.Buffer
|
||||
if err := png.Encode(&pngData, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := jpeg.Encode(&jpegData, img, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime string
|
||||
}{{pngData.Bytes(), "image/png"}, {jpegData.Bytes(), "image/jpeg"}} {
|
||||
if actual, err := services.ValidateImage(sample.data, sample.mime); err != nil || actual != sample.mime {
|
||||
t.Fatalf("valid image: %s %v", actual, err)
|
||||
}
|
||||
}
|
||||
huge := append([]byte(nil), pngData.Bytes()...)
|
||||
binary.BigEndian.PutUint32(huge[16:20], 10001)
|
||||
binary.BigEndian.PutUint32(huge[20:24], 4000)
|
||||
binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29]))
|
||||
if _, err := services.ValidateImage(huge, "image/png"); !errors.Is(err, services.ErrPayloadTooLarge) {
|
||||
t.Fatalf("pixel limit: %v", err)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime string
|
||||
}{{pngData.Bytes(), "image/jpeg"}, {jpegData.Bytes(), "image/png"}, {pngData.Bytes()[:40], "image/png"}, {[]byte("not an image"), "image/png"}, {pngData.Bytes(), "text/plain"}} {
|
||||
if _, err := services.ValidateImage(sample.data, sample.mime); !errors.Is(err, services.ErrUnsupportedMedia) {
|
||||
t.Fatalf("invalid image accepted: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentFilenameValidation(t *testing.T) {
|
||||
for _, name := range []string{"", " ", "../photo.png", `a\b.png`, "photo\x00.png", "photo\n.png"} {
|
||||
if services.ValidateFilename(name) == nil {
|
||||
t.Fatalf("accepted %q", name)
|
||||
}
|
||||
}
|
||||
if err := services.ValidateFilename("Фотография 1.png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentMessageRequiresStringText(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Images", []string{user.ID})
|
||||
id := uuid.NewString()
|
||||
// Repository-level fixture: message validation does not fetch S3 bytes.
|
||||
err := repositories.NewAttachmentStore(f.db).Create(context.Background(), repositories.Attachment{
|
||||
ID: id, ChatID: chat.ID, OwnerID: user.ID, Filename: "photo.png", ObjectKey: "test/" + id,
|
||||
ContentType: "image/png", SizeBytes: 1, SHA256: strings.Repeat("a", 64), CreatedAt: time.Now().UTC(), UpdatedAt: time.Now().UTC(),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, body := range []string{`{"attachment_ids":["` + id + `"]}`, `{"text":null,"attachment_ids":["` + id + `"]}`} {
|
||||
r := httptest.NewRequest(http.MethodPost, "/api/v1/chats/"+chat.ID+"/messages", strings.NewReader(body))
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
r.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
authorizeTestRequest(t, f, r, user.ID)
|
||||
w := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
if len(listTestMessages(t, f.messages, chat.ID)) != 0 {
|
||||
t.Fatal("invalid request saved a message")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"messenger/src/backend/repositories"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConcurrentMessageCreation(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
userStore, chatStore := fixture.users, fixture.chats
|
||||
|
||||
const clientsCount = 10
|
||||
const messagesPerClient = 10
|
||||
const expectedMessages = clientsCount * messagesPerClient
|
||||
|
||||
memberIDs := make([]string, 0, clientsCount)
|
||||
users := make([]User, 0, clientsCount)
|
||||
|
||||
for i := range clientsCount {
|
||||
user, created := createTestUser(t, userStore,
|
||||
"user"+strconv.Itoa(i),
|
||||
"Пользователь "+strconv.Itoa(i),
|
||||
)
|
||||
|
||||
if !created {
|
||||
t.Fatalf("failed to create test user %d", i)
|
||||
}
|
||||
|
||||
users = append(users, user)
|
||||
memberIDs = append(memberIDs, user.ID)
|
||||
}
|
||||
|
||||
chat := createTestChat(t, chatStore,
|
||||
"Конкурентный чат",
|
||||
memberIDs,
|
||||
)
|
||||
|
||||
server := httptest.NewServer(fixture.router)
|
||||
defer server.Close()
|
||||
|
||||
client := server.Client()
|
||||
tokens := make([]string, clientsCount)
|
||||
for i, user := range users {
|
||||
request := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
authorizeTestRequest(t, fixture, request, user.ID)
|
||||
tokens[i] = request.Header.Get("Authorization")
|
||||
}
|
||||
|
||||
var waitGroup sync.WaitGroup
|
||||
messageIDs := make(chan string, expectedMessages)
|
||||
errors := make(chan error, expectedMessages)
|
||||
|
||||
for clientIndex := range clientsCount {
|
||||
waitGroup.Add(1)
|
||||
|
||||
go func(index int) {
|
||||
defer waitGroup.Done()
|
||||
|
||||
for messageIndex := range messagesPerClient {
|
||||
body := fmt.Appendf(nil,
|
||||
`{"text":"client-%d message-%d"}`,
|
||||
index,
|
||||
messageIndex,
|
||||
)
|
||||
|
||||
request, err := http.NewRequest(
|
||||
http.MethodPost,
|
||||
server.URL+"/api/v1/chats/"+chat.ID+"/messages",
|
||||
bytes.NewReader(body),
|
||||
)
|
||||
if err != nil {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: create request: %w",
|
||||
index,
|
||||
err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Authorization", tokens[index])
|
||||
request.Header.Set("Idempotency-Key", fmt.Sprintf("client-%d-message-%d", index, messageIndex))
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: send request: %w",
|
||||
index,
|
||||
err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
responseBody, readErr := io.ReadAll(response.Body)
|
||||
response.Body.Close()
|
||||
|
||||
if readErr != nil {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: read response: %w",
|
||||
index,
|
||||
readErr,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: expected status %d, got %d; body: %s",
|
||||
index,
|
||||
http.StatusCreated,
|
||||
response.StatusCode,
|
||||
string(responseBody),
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
var message repositories.Message
|
||||
|
||||
if err := json.Unmarshal(responseBody, &message); err != nil {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: decode response: %w",
|
||||
index,
|
||||
err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
if message.ID == "" {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: response contains empty message ID",
|
||||
index,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
messageIDs <- message.ID
|
||||
}
|
||||
}(clientIndex)
|
||||
}
|
||||
|
||||
waitGroup.Wait()
|
||||
|
||||
close(errors)
|
||||
close(messageIDs)
|
||||
|
||||
for err := range errors {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
createdIDs := make(map[string]bool)
|
||||
|
||||
for messageID := range messageIDs {
|
||||
if createdIDs[messageID] {
|
||||
t.Fatalf("duplicate message ID returned: %s", messageID)
|
||||
}
|
||||
|
||||
createdIDs[messageID] = true
|
||||
}
|
||||
|
||||
if len(createdIDs) != expectedMessages {
|
||||
t.Fatalf(
|
||||
"expected %d unique created messages, got %d",
|
||||
expectedMessages,
|
||||
len(createdIDs),
|
||||
)
|
||||
}
|
||||
|
||||
// Дополнительно проверяем историю через HTTP API.
|
||||
request, err := http.NewRequest(
|
||||
http.MethodGet,
|
||||
server.URL+"/api/v1/chats/"+chat.ID+"/messages?limit=100",
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("create history request: %v", err)
|
||||
}
|
||||
|
||||
request.Header.Set("Authorization", tokens[0])
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
t.Fatalf("request message history: %v", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(response.Body)
|
||||
|
||||
t.Fatalf(
|
||||
"expected history status %d, got %d; body: %s",
|
||||
http.StatusOK,
|
||||
response.StatusCode,
|
||||
string(body),
|
||||
)
|
||||
}
|
||||
|
||||
var page messagePage
|
||||
|
||||
if err := json.NewDecoder(response.Body).Decode(&page); err != nil {
|
||||
t.Fatalf("decode message history: %v", err)
|
||||
}
|
||||
|
||||
if len(page.Items) != expectedMessages {
|
||||
t.Fatalf(
|
||||
"expected %d messages in history, got %d",
|
||||
expectedMessages,
|
||||
len(page.Items),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func TestConnectionFilesTakePrecedence(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
database := filepath.Join(dir, "database")
|
||||
cache := filepath.Join(dir, "cache")
|
||||
if err := os.WriteFile(database, []byte("postgres://configured/db\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(cache, []byte("redis://:test-password@configured:6380/3\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("DATABASE_URL_FILE", database)
|
||||
t.Setenv("DATABASE_URL", "postgres://ignored/db")
|
||||
t.Setenv("CACHE_URL_FILE", cache)
|
||||
t.Setenv("REDIS_ADDR", "ignored:6379")
|
||||
url, err := repositories.DatabaseURL()
|
||||
if err != nil || url != "postgres://configured/db" {
|
||||
t.Fatalf("database file not used: %v", err)
|
||||
}
|
||||
client, err := repositories.RedisClientFromEnvironment()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
options := client.Options()
|
||||
if options.Addr != "configured:6380" || options.DB != 3 || options.Password != "test-password" {
|
||||
t.Fatal("Redis URL settings not applied")
|
||||
}
|
||||
t.Setenv("CACHE_URL_FILE", filepath.Join(dir, "missing"))
|
||||
if _, err := repositories.RedisClientFromEnvironment(); err == nil {
|
||||
t.Fatal("missing file accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfiguredIdempotencyTTL(t *testing.T) {
|
||||
t.Setenv("IDEMPOTENCY_TTL_SECONDS", "2")
|
||||
ttl, err := repositories.IdempotencyTTL()
|
||||
if err != nil || ttl != 2*time.Second {
|
||||
t.Fatalf("TTL not applied: %v", err)
|
||||
}
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Chat", []string{user.ID})
|
||||
store := repositories.NewMessageStoreWithTTL(f.db, ttl)
|
||||
ctx := context.Background()
|
||||
original, err := store.CreateIdempotent(ctx, chat.ID, user.ID, "hello", "ttl-test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var remaining float64
|
||||
if err := f.db.QueryRow(ctx, "SELECT extract(epoch FROM expires_at-clock_timestamp()) FROM message_idempotency WHERE key='ttl-test'").Scan(&remaining); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if remaining <= 0 || remaining > 2 {
|
||||
t.Fatalf("unexpected remaining TTL: %f", remaining)
|
||||
}
|
||||
replay, err := store.CreateIdempotent(ctx, chat.ID, user.ID, "hello", "ttl-test")
|
||||
if err != nil || replay.ID != original.ID {
|
||||
t.Fatal("replay before expiry changed the message")
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, "UPDATE message_idempotency SET expires_at=clock_timestamp()-interval '1 second' WHERE key='ttl-test'"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reused, err := store.CreateIdempotent(ctx, chat.ID, user.ID, "hello", "ttl-test")
|
||||
if err != nil || reused.ID == original.ID {
|
||||
t.Fatal("expired key was not reusable")
|
||||
}
|
||||
if len(listTestMessages(t, store, chat.ID)) != 2 {
|
||||
t.Fatal("expiry removed an original message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidIdempotencyTTL(t *testing.T) {
|
||||
for _, value := range []string{"0", "-1", "abc", "31536001"} {
|
||||
t.Setenv("IDEMPOTENCY_TTL_SECONDS", value)
|
||||
if _, err := repositories.IdempotencyTTL(); err == nil {
|
||||
t.Fatalf("accepted TTL %q", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
func TestCursorRejectsInvalidUUIDBeforeDatabaseAccess(t *testing.T) {
|
||||
const userID = "11111111-1111-4111-8111-111111111111"
|
||||
const chatID = "22222222-2222-4222-8222-222222222222"
|
||||
const messageID = "33333333-3333-4333-8333-333333333333"
|
||||
createdAt := time.Date(2026, 10, 10, 12, 0, 0, 0, time.UTC)
|
||||
for _, invalid := range []string{"not-a-uuid", "ffffffff-ffff-ffff-ffff-fffffffffffz"} {
|
||||
for _, field := range []string{"user_id", "chat_id"} {
|
||||
t.Run("chats/"+field+"/"+invalid, func(t *testing.T) {
|
||||
cursor := repositories.ChatCursor{UserID: userID, ChatID: chatID, CreatedAt: createdAt}
|
||||
if field == "user_id" {
|
||||
cursor.UserID = invalid
|
||||
} else {
|
||||
cursor.ChatID = invalid
|
||||
}
|
||||
token, err := services.EncodeChatCursor(cursor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := services.DecodeChatCursor(token); err == nil {
|
||||
t.Fatal("invalid UUID accepted")
|
||||
}
|
||||
// A nil store proves that validation finishes before any SQL call.
|
||||
_, err = services.NewChatService(nil).List(context.Background(), userID, 20, token)
|
||||
var invalidInput services.InvalidInput
|
||||
if !errors.As(err, &invalidInput) {
|
||||
t.Fatalf("want InvalidInput, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, field := range []string{"chat_id", "message_id"} {
|
||||
t.Run("messages/"+field+"/"+invalid, func(t *testing.T) {
|
||||
cursor := repositories.MessageCursor{ChatID: chatID, MessageID: messageID, CreatedAt: createdAt}
|
||||
if field == "chat_id" {
|
||||
cursor.ChatID = invalid
|
||||
} else {
|
||||
cursor.MessageID = invalid
|
||||
}
|
||||
token, err := services.EncodeMessageCursor(cursor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := services.DecodeMessageCursor(token); err == nil {
|
||||
t.Fatal("invalid UUID accepted")
|
||||
}
|
||||
chat := repositories.Chat{ID: chatID, MemberIDs: []string{userID}}
|
||||
_, err = services.NewMessageService(nil).List(context.Background(), userID, chat, 20, token)
|
||||
var invalidInput services.InvalidInput
|
||||
if !errors.As(err, &invalidInput) {
|
||||
t.Fatalf("want InvalidInput, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCursorPreservesPosition(t *testing.T) {
|
||||
original := repositories.ChatCursor{
|
||||
UserID: "11111111-1111-4111-8111-111111111111",
|
||||
ChatID: "22222222-2222-4222-8222-222222222222",
|
||||
CreatedAt: time.Date(2026, 10, 10, 12, 0, 0, 123000, time.UTC),
|
||||
}
|
||||
token, err := services.EncodeChatCursor(original)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decoded, err := services.DecodeChatCursor(token)
|
||||
if err != nil || decoded != original {
|
||||
t.Fatalf("cursor changed: %+v, error: %v", decoded, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func TestDirectUploadCleanupSafetyAndReplay(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
data := uploadPNG(t)
|
||||
fresh, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
expired, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
locked, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
ready, service := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
for _, u := range []string{fresh.ID, expired.ID, locked.ID, ready.ID} {
|
||||
record, err := store.GetDirect(ctx, u, chat, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = objects.Put(ctx, record.StagingKey, bytes.NewReader(data), int64(len(data)), "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
a, err := service.CompleteDirect(ctx, user, chat, ready.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '601 seconds',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=ANY($1::uuid[])`, []string{expired.ID, locked.ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tx, err := f.db.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, `SELECT id FROM direct_uploads WHERE id=$1 FOR UPDATE`, locked.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dry, err := store.CleanupDirectUploads(ctx, true, objects.Delete)
|
||||
if err != nil || dry.Eligible != 2 || dry.Deleted != 0 || dry.Skipped != 1 {
|
||||
t.Fatalf("dry: %+v %v", dry, err)
|
||||
}
|
||||
result, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Deleted != 2 || result.Skipped != 1 {
|
||||
t.Fatalf("cleanup: %+v %v", result, err)
|
||||
}
|
||||
for _, id := range []string{fresh.ID, locked.ID} {
|
||||
u, _ := store.GetDirect(ctx, id, chat, user)
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); err != nil {
|
||||
t.Fatal("fresh/locked staging removed", err)
|
||||
}
|
||||
}
|
||||
for _, id := range []string{ready.ID, expired.ID} {
|
||||
u, _ := store.GetDirect(ctx, id, chat, user)
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); !repositories.IsObjectMissing(err) {
|
||||
t.Fatal("eligible staging remains", err)
|
||||
}
|
||||
}
|
||||
if _, err = objects.Stat(ctx, a.ObjectKey); err != nil {
|
||||
t.Fatal("ready original removed", err)
|
||||
}
|
||||
// A valid form can recreate staging after early cleanup. The receipt persists.
|
||||
if postDirect(t, ready.Upload, data, nil) != 204 {
|
||||
t.Fatal("replay failed")
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '601 seconds',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1`, ready.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
again, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || again.Deleted != 1 {
|
||||
t.Fatalf("replay cleanup: %+v %v", again, err)
|
||||
}
|
||||
u, _ := store.GetDirect(ctx, ready.ID, chat, user)
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); !repositories.IsObjectMissing(err) {
|
||||
t.Fatal("replayed staging remains")
|
||||
}
|
||||
same, err := service.CompleteDirect(ctx, user, chat, ready.ID)
|
||||
if err != nil || same.ID != a.ID {
|
||||
t.Fatal("cleanup broke idempotent complete", err)
|
||||
}
|
||||
tx.Rollback(ctx)
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET purge_after=clock_timestamp()-interval '1 second',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1`, ready.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = store.CleanupDirectUploads(ctx, false, objects.Delete); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = objects.Stat(ctx, a.ObjectKey); err != nil {
|
||||
t.Fatal("receipt purge removed final", err)
|
||||
}
|
||||
if _, err = store.GetForMember(ctx, a.ID, user); err != nil {
|
||||
t.Fatal("receipt purge removed metadata", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadCleanupDependencyFailureAndMalformedKey(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
u, _ := store.GetDirect(ctx, result.ID, chat, user)
|
||||
if err := objects.Put(ctx, u.StagingKey, bytes.NewReader(data), int64(len(data)), "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '601 seconds',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1`, result.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
failed, err := store.CleanupDirectUploads(ctx, false, func(context.Context, string) error { return errors.New("S3 unavailable") })
|
||||
if err == nil || failed.Failed != 1 {
|
||||
t.Fatal("failed DELETE was lost")
|
||||
}
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); err != nil {
|
||||
t.Fatal("staging changed on failed DELETE", err)
|
||||
}
|
||||
cancelled, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
if _, err = store.CleanupDirectUploads(cancelled, false, objects.Delete); err == nil {
|
||||
t.Fatal("DB cancellation ignored")
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET staging_key=$2 WHERE id=$1`, result.ID, "originals/"+u.AttachmentID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
called := false
|
||||
failed, err = store.CleanupDirectUploads(ctx, false, func(context.Context, string) error { called = true; return nil })
|
||||
if err == nil || failed.Failed != 1 || called {
|
||||
t.Fatal("malformed key reached S3 DELETE")
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET staging_key=$2 WHERE id=$1`, result.ID, u.StagingKey); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
retried, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || retried.Deleted != 1 {
|
||||
t.Fatalf("retry: %+v %v", retried, err)
|
||||
}
|
||||
repeated, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || repeated.Deleted != 0 {
|
||||
t.Fatalf("repeat: %+v %v", repeated, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/lifecycle"
|
||||
)
|
||||
|
||||
func TestDirectUploadAccessPermissionsAndLifecycle(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
root := s3Client(t, true, false)
|
||||
app := s3Client(t, false, false)
|
||||
anon := s3Client(t, false, true)
|
||||
bucket := os.Getenv("S3_BUCKET")
|
||||
key := "staging/direct/" + uuid.NewString()
|
||||
t.Cleanup(func() { root.RemoveObject(ctx, bucket, key, minio.RemoveObjectOptions{}) })
|
||||
if _, err := app.PutObject(ctx, bucket, key, bytes.NewReader([]byte("x")), 1, minio.PutObjectOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
object, err := anon.GetObject(ctx, bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = object.Stat()
|
||||
object.Close()
|
||||
denied(t, err)
|
||||
_, err = app.PutObject(ctx, bucket, "staging/forbidden/"+uuid.NewString(), bytes.NewReader([]byte("x")), 1, minio.PutObjectOptions{})
|
||||
denied(t, err)
|
||||
config, err := root.GetBucketLifecycle(ctx, bucket)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Add only our own harmless prefix rule, verify bootstrap merges rather than replaces.
|
||||
ruleID := "direct-test-" + uuid.NewString()
|
||||
config.Rules = append(config.Rules, lifecycle.Rule{ID: ruleID, Status: "Enabled", RuleFilter: lifecycle.Filter{Prefix: "test-lifecycle/" + ruleID + "/"}, Expiration: lifecycle.Expiration{Days: 7}})
|
||||
if err = root.SetBucketLifecycle(ctx, bucket, config); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
c, e := root.GetBucketLifecycle(ctx, bucket)
|
||||
if e != nil {
|
||||
t.Error(e)
|
||||
return
|
||||
}
|
||||
rules := []lifecycle.Rule{}
|
||||
for _, r := range c.Rules {
|
||||
if r.ID != ruleID {
|
||||
rules = append(rules, r)
|
||||
}
|
||||
}
|
||||
c.Rules = rules
|
||||
if e = root.SetBucketLifecycle(ctx, bucket, c); e != nil {
|
||||
t.Error(e)
|
||||
}
|
||||
})
|
||||
for i := 0; i < 2; i++ {
|
||||
if err = repositories.BootstrapS3(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
config, err = root.GetBucketLifecycle(ctx, bucket)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
managed, preserved := 0, false
|
||||
for _, r := range config.Rules {
|
||||
if r.ID == ruleID {
|
||||
preserved = true
|
||||
}
|
||||
if r.ID == "messenger-direct-staging-expiration" {
|
||||
managed++
|
||||
if r.RuleFilter.Prefix != "staging/direct/" || r.Expiration.Days != 1 || r.Status != "Enabled" {
|
||||
t.Fatal("unsafe staging lifecycle rule")
|
||||
}
|
||||
}
|
||||
}
|
||||
if managed != 1 || !preserved {
|
||||
t.Fatal("bootstrap is not an idempotent lifecycle merge")
|
||||
}
|
||||
denied(t, app.SetBucketLifecycle(ctx, bucket, config))
|
||||
}
|
||||
|
||||
func TestDirectUploadAccessCORS(t *testing.T) {
|
||||
if os.Getenv("S3_ENDPOINT") == "" {
|
||||
t.Skip("S3 integration")
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
origin string
|
||||
allowed bool
|
||||
}{{"http://127.0.0.1:5173", true}, {"http://localhost:5173", true}, {"https://untrusted.example", false}} {
|
||||
request, err := http.NewRequest("OPTIONS", os.Getenv("S3_ENDPOINT")+"/"+os.Getenv("S3_BUCKET"), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request.Header.Set("Origin", sample.origin)
|
||||
request.Header.Set("Access-Control-Request-Method", "POST")
|
||||
request.Header.Set("Access-Control-Request-Headers", "content-type")
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
allowed := response.Header.Get("Access-Control-Allow-Origin") == sample.origin
|
||||
if allowed != sample.allowed || response.Header.Get("Access-Control-Allow-Origin") == "*" {
|
||||
t.Fatalf("unexpected CORS allowance for %s", sample.origin)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,478 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func directInput(data []byte, mime string) services.DirectUploadInput {
|
||||
sum := sha256.Sum256(data)
|
||||
return services.DirectUploadInput{Filename: "direct.png", ContentType: mime, SizeBytes: int64(len(data)), SHA256: hex.EncodeToString(sum[:])}
|
||||
}
|
||||
|
||||
func beginDirect(t *testing.T, f *testFixture, store *repositories.AttachmentStore, objects *repositories.ObjectStore, user, chat string, data []byte, mime string) (services.DirectUploadResponse, *services.AttachmentService) {
|
||||
t.Helper()
|
||||
t.Setenv("S3_PUBLIC_ENDPOINT", os.Getenv("S3_ENDPOINT"))
|
||||
service := services.NewAttachmentService(store, objects)
|
||||
result, err := service.InitDirect(context.Background(), user, chat, directInput(data, mime))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rememberDirect(t, store, objects, result.ID, chat, user)
|
||||
return result, service
|
||||
}
|
||||
|
||||
func rememberDirect(t *testing.T, store *repositories.AttachmentStore, objects *repositories.ObjectStore, id, chat, user string) {
|
||||
t.Helper()
|
||||
u, err := store.GetDirect(context.Background(), id, chat, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
objects.Delete(context.Background(), u.StagingKey)
|
||||
objects.Delete(context.Background(), u.FinalKey)
|
||||
})
|
||||
}
|
||||
|
||||
func postDirect(t *testing.T, p repositories.PostUpload, data []byte, change func(map[string]string)) int {
|
||||
t.Helper()
|
||||
fields := map[string]string{}
|
||||
for k, v := range p.Fields {
|
||||
fields[k] = v
|
||||
}
|
||||
if change != nil {
|
||||
change(fields)
|
||||
}
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
for k, v := range fields {
|
||||
if err := writer.WriteField(k, v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
file, err := writer.CreateFormFile("file", "image.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = file.Write(data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req, err := http.NewRequest("POST", p.URL, &body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
client := http.Client{Timeout: 10 * time.Second}
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal("S3 form request failed")
|
||||
}
|
||||
defer response.Body.Close()
|
||||
io.Copy(io.Discard, response.Body)
|
||||
return response.StatusCode
|
||||
}
|
||||
|
||||
func requireDirectError(t *testing.T, err error, status int, code string) {
|
||||
t.Helper()
|
||||
var e repositories.UploadError
|
||||
if !errors.As(err, &e) || e.Status != status || e.Code != code {
|
||||
t.Fatalf("expected %d %s, got %v", status, code, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadFlow(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
png := append(uploadPNG(t), []byte("aaaa")...)
|
||||
var jpg bytes.Buffer
|
||||
if err := jpeg.Encode(&jpg, image.NewRGBA(image.Rect(0, 0, 8, 8)), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime string
|
||||
}{{png, "image/png"}, {jpg.Bytes(), "image/jpeg"}} {
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, sample.data, sample.mime)
|
||||
if result.ExpiresAt.Sub(time.Now()) > 121*time.Second || result.CompleteBefore.Sub(result.ExpiresAt) < 599*time.Second {
|
||||
t.Fatal("wrong upload deadlines")
|
||||
}
|
||||
if status := postDirect(t, result.Upload, sample.data, nil); status != 204 {
|
||||
t.Fatalf("POST: %d", status)
|
||||
}
|
||||
a, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
router := attachmentRouter(f, objects)
|
||||
attachmentCall(t, f, router, "GET", "/api/v1/attachments/"+a.ID+"/content", user, "", nil, 409)
|
||||
if a.Status != "queued" {
|
||||
t.Fatal("direct complete must queue processing")
|
||||
}
|
||||
var jobs int
|
||||
if err = f.db.QueryRow(context.Background(), "SELECT count(*) FROM attachment_jobs WHERE attachment_id=$1", a.ID).Scan(&jobs); err != nil || jobs != 1 {
|
||||
t.Fatal("durable job missing", err)
|
||||
}
|
||||
message := []byte(`{"text":"","attachment_ids":["` + a.ID + `"]}`)
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/messages", user, "application/json", message, 201)
|
||||
// Lost success response is represented by ignoring a, then asking another service.
|
||||
again, err := services.NewAttachmentService(store, objects).CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
if err != nil || again.ID != a.ID || !again.CreatedAt.Equal(a.CreatedAt) {
|
||||
t.Fatal("complete is not idempotent", err)
|
||||
}
|
||||
replacement := append([]byte(nil), sample.data...)
|
||||
replacement[len(replacement)-1] ^= 1
|
||||
if status := postDirect(t, result.Upload, replacement, nil); status != 204 {
|
||||
t.Fatal("expected valid staging replay")
|
||||
}
|
||||
final, err := objects.Get(context.Background(), a.ObjectKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
original, err := io.ReadAll(final)
|
||||
final.Close()
|
||||
if err != nil || !bytes.Equal(original, sample.data) {
|
||||
t.Fatal("staging replay modified final")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadValidationHTTP(t *testing.T) {
|
||||
f, _, objects, user, chat := uploadFixture(t)
|
||||
t.Setenv("S3_PUBLIC_ENDPOINT", os.Getenv("S3_ENDPOINT"))
|
||||
router := attachmentRouter(f, objects)
|
||||
path := "/api/v1/chats/" + chat + "/attachment-uploads"
|
||||
input := directInput(uploadPNG(t), "image/png")
|
||||
valid, _ := json.Marshal(input)
|
||||
for _, sample := range []struct {
|
||||
body []byte
|
||||
status int
|
||||
}{
|
||||
{[]byte(`null`), 400}, {[]byte(`{}`), 400}, {append(append([]byte(nil), valid...), []byte(` {}`)...), 400},
|
||||
{[]byte(strings.Repeat("x", 4097)), 400}, {[]byte(`{"filename":"x","size_bytes":1.5}`), 400},
|
||||
} {
|
||||
attachmentCall(t, f, router, "POST", path, user, "application/json", sample.body, sample.status)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
mutate func(*services.DirectUploadInput)
|
||||
status int
|
||||
}{
|
||||
{func(i *services.DirectUploadInput) { i.Filename = "../image.png" }, 400},
|
||||
{func(i *services.DirectUploadInput) { i.SHA256 = "invalid" }, 400},
|
||||
{func(i *services.DirectUploadInput) { i.SizeBytes = 0 }, 400},
|
||||
{func(i *services.DirectUploadInput) { i.SizeBytes = 10485761 }, 413},
|
||||
{func(i *services.DirectUploadInput) { i.ContentType = "text/plain" }, 415},
|
||||
} {
|
||||
i := input
|
||||
sample.mutate(&i)
|
||||
body, _ := json.Marshal(i)
|
||||
attachmentCall(t, f, router, "POST", path, user, "application/json", body, sample.status)
|
||||
}
|
||||
store := repositories.NewAttachmentStore(f.db)
|
||||
w := attachmentCall(t, f, router, "POST", path, user, "application/json", valid, 201)
|
||||
var result services.DirectUploadResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rememberDirect(t, store, objects, result.ID, chat, user)
|
||||
if status := postDirect(t, result.Upload, uploadPNG(t), nil); status != 204 {
|
||||
t.Fatal("HTTP-created form failed")
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", path+"/"+result.ID+"/complete", user, "", nil, 202)
|
||||
}
|
||||
|
||||
func TestDirectUploadValidationContents(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
png := uploadPNG(t)
|
||||
huge := append([]byte(nil), png...)
|
||||
binary.BigEndian.PutUint32(huge[16:20], 10001)
|
||||
binary.BigEndian.PutUint32(huge[20:24], 4000)
|
||||
binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29]))
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime, code string
|
||||
status int
|
||||
}{
|
||||
{png[:40], "image/png", "unsupported_media_type", 415},
|
||||
{png, "image/jpeg", "unsupported_media_type", 415},
|
||||
{huge, "image/png", "payload_too_large", 413},
|
||||
} {
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, sample.data, sample.mime)
|
||||
if postDirect(t, result.Upload, sample.data, nil) != 204 {
|
||||
t.Fatal("S3 refused matching declared policy")
|
||||
}
|
||||
_, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, sample.status, sample.code)
|
||||
_, err = service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, sample.status, sample.code)
|
||||
}
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, png, "image/png")
|
||||
wrong := append([]byte(nil), png...)
|
||||
wrong[len(wrong)-1] ^= 1
|
||||
if postDirect(t, result.Upload, wrong, nil) != 204 {
|
||||
t.Fatal("form POST failed")
|
||||
}
|
||||
_, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 400, "upload_checksum_mismatch")
|
||||
result, service = beginDirect(t, f, store, objects, user, chat, png, "image/png")
|
||||
if postDirect(t, result.Upload, append(png, 1), nil) < 400 {
|
||||
t.Fatal("S3 did not enforce exact policy size")
|
||||
}
|
||||
u, _ := store.GetDirect(context.Background(), result.ID, chat, user)
|
||||
// Simulate a trusted-side fault that bypassed policy; complete must still check.
|
||||
if err = objects.Put(context.Background(), u.StagingKey, bytes.NewReader(png[:40]), 40, "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 400, "upload_size_mismatch")
|
||||
}
|
||||
|
||||
func TestDirectUploadAccess(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
png := uploadPNG(t)
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, png, "image/png")
|
||||
eve, _ := createTestUser(t, f.users, "eve", "Eve")
|
||||
bob, _ := createTestUser(t, f.users, "bob", "Bob")
|
||||
if _, err := f.db.Exec(context.Background(), `INSERT INTO chat_members(chat_id,user_id) VALUES($1,$2)`, chat, bob.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := createTestChat(t, f.chats, "other", []string{user})
|
||||
router := attachmentRouter(f, objects)
|
||||
path := "/api/v1/chats/" + chat + "/attachment-uploads"
|
||||
for _, actor := range []string{eve.ID, bob.ID} {
|
||||
attachmentCall(t, f, router, "POST", path+"/"+result.ID+"/complete", actor, "", nil, 404)
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", path+"/"+result.ID+"/complete", "", "", nil, 401)
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+other.ID+"/attachment-uploads/"+result.ID+"/complete", user, "", nil, 404)
|
||||
body, _ := json.Marshal(directInput(png, "image/png"))
|
||||
attachmentCall(t, f, router, "POST", path, eve.ID, "application/json", body, 404)
|
||||
u, _ := store.GetDirect(context.Background(), result.ID, chat, user)
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/messages", user, "application/json", []byte(`{"text":"","attachment_ids":["`+u.AttachmentID+`"]}`), 404)
|
||||
_, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 409, "upload_not_uploaded")
|
||||
for _, change := range []func(map[string]string){
|
||||
func(fields map[string]string) { fields["key"] = "originals/" + uuid.NewString() },
|
||||
func(fields map[string]string) { fields["Content-Type"] = "image/jpeg" },
|
||||
func(fields map[string]string) { fields["x-amz-signature"] = strings.Repeat("0", 64) },
|
||||
} {
|
||||
if postDirect(t, result.Upload, png, change) < 400 {
|
||||
t.Fatal("S3 accepted tampered form")
|
||||
}
|
||||
}
|
||||
expired, err := objects.PresignDirect(context.Background(), u.StagingKey, "image/png", int64(len(png)), time.Now().Add(-time.Minute))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if postDirect(t, expired, png, nil) < 400 {
|
||||
t.Fatal("S3 accepted expired form")
|
||||
}
|
||||
if _, err = f.db.Exec(context.Background(), `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '1 second' WHERE id=$1`, result.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 409, "upload_expired")
|
||||
}
|
||||
|
||||
type directFault struct {
|
||||
*repositories.ObjectStore
|
||||
afterCopy func() error
|
||||
failGet bool
|
||||
}
|
||||
|
||||
func (f directFault) Copy(ctx context.Context, source, dest, etag string) error {
|
||||
if err := f.ObjectStore.Copy(ctx, source, dest, etag); err != nil {
|
||||
return err
|
||||
}
|
||||
if f.afterCopy != nil {
|
||||
return f.afterCopy()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (f directFault) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
if f.failGet {
|
||||
return nil, errors.New("injected GET failure")
|
||||
}
|
||||
return f.ObjectStore.Get(ctx, key)
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoveryConcurrentComplete(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
ids := make(chan string, 2)
|
||||
errs := make(chan error, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
a, err := services.NewAttachmentService(store, objects).CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
ids <- a.ID
|
||||
errs <- err
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
close(ids)
|
||||
close(errs)
|
||||
for err := range errs {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
first := <-ids
|
||||
if second := <-ids; first == "" || first != second {
|
||||
t.Fatal("concurrent complete produced different attachments")
|
||||
}
|
||||
var n int
|
||||
if err := f.db.QueryRow(context.Background(), `SELECT count(*) FROM attachments`).Scan(&n); err != nil || n != 1 {
|
||||
t.Fatal("unexpected metadata count", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoveryFailures(t *testing.T) {
|
||||
for _, name := range []string{"copy-result-loss", "get-failure", "metadata-rejection", "database-disconnect"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, normal := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
fault := directFault{ObjectStore: objects}
|
||||
switch name {
|
||||
case "copy-result-loss":
|
||||
fault.afterCopy = func() error { return errors.New("COPY acknowledgement lost") }
|
||||
case "get-failure":
|
||||
fault.failGet = true
|
||||
case "metadata-rejection":
|
||||
if _, err := f.db.Exec(context.Background(), `ALTER TABLE attachments ADD CONSTRAINT reject_direct CHECK (filename <> 'direct.png')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "database-disconnect":
|
||||
fault.afterCopy = func() error {
|
||||
_, err := f.db.Exec(context.Background(), `SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE application_name=$1 AND state='idle in transaction' AND pid<>pg_backend_pid()`, f.db.Config().ConnConfig.RuntimeParams["application_name"])
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err := services.NewAttachmentService(store, fault).CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
if err == nil {
|
||||
t.Fatal("expected injected failure")
|
||||
}
|
||||
var count int
|
||||
if err = f.db.QueryRow(context.Background(), `SELECT count(*) FROM upload_attempts`).Scan(&count); err != nil || count != 1 {
|
||||
t.Fatal("durable journal missing", err)
|
||||
}
|
||||
if err = f.db.QueryRow(context.Background(), `SELECT count(*) FROM attachments`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("published incomplete attachment", err)
|
||||
}
|
||||
if name == "metadata-rejection" {
|
||||
if _, err = f.db.Exec(context.Background(), `ALTER TABLE attachments DROP CONSTRAINT reject_direct`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err = normal.CompleteDirect(context.Background(), user, chat, result.ID); err != nil {
|
||||
t.Fatal("retry failed", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoveryProcessExit(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
child := exec.Command(os.Args[0], "-test.run=^TestDirectUploadCrashHelper$")
|
||||
child.Env = append(os.Environ(), "DIRECT_CRASH_HELPER=1", "UPLOAD_TEST_DATABASE="+f.db.Config().ConnString(), "UPLOAD_TEST_SCHEMA="+f.db.Config().ConnConfig.RuntimeParams["search_path"], "UPLOAD_TEST_USER="+user, "UPLOAD_TEST_CHAT="+chat, "DIRECT_UPLOAD_ID="+result.ID)
|
||||
output, err := child.CombinedOutput()
|
||||
var exit *exec.ExitError
|
||||
if !errors.As(err, &exit) || exit.ExitCode() != 23 {
|
||||
t.Fatalf("helper did not crash after COPY: %v %s", err, output)
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
|
||||
func TestDirectUploadCrashHelper(t *testing.T) {
|
||||
if os.Getenv("DIRECT_CRASH_HELPER") != "1" {
|
||||
return
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(os.Getenv("UPLOAD_TEST_DATABASE"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.ConnConfig.RuntimeParams["search_path"] = os.Getenv("UPLOAD_TEST_SCHEMA")
|
||||
cfg.ConnConfig.RuntimeParams["application_name"] = os.Getenv("UPLOAD_TEST_SCHEMA")
|
||||
db, err := pgxpool.NewWithConfig(context.Background(), cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
objects := uploadObjects(t)
|
||||
fault := directFault{ObjectStore: objects, afterCopy: func() error { os.Exit(23); return nil }}
|
||||
_, err = services.NewAttachmentService(repositories.NewAttachmentStore(db), fault).CompleteDirect(context.Background(), os.Getenv("UPLOAD_TEST_USER"), os.Getenv("UPLOAD_TEST_CHAT"), os.Getenv("DIRECT_UPLOAD_ID"))
|
||||
t.Fatalf("helper failed before exit: %v", err)
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoverySharedSlotAndCancellation(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
entered, release := make(chan struct{}), make(chan struct{})
|
||||
fault := directFault{ObjectStore: objects, afterCopy: func() error {
|
||||
close(entered)
|
||||
<-release
|
||||
return errors.New("injected operation cancellation")
|
||||
}}
|
||||
service := services.NewAttachmentService(store, fault)
|
||||
done := make(chan error, 1)
|
||||
go func() { _, err := service.CompleteDirect(context.Background(), user, chat, result.ID); done <- err }()
|
||||
select {
|
||||
case <-entered:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("COPY not reached")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, proxyErr := service.Upload(ctx, user, chat, "waiting.png", "image/png", bytes.NewReader(data))
|
||||
_, directErr := service.CompleteDirect(ctx, user, chat, result.ID)
|
||||
close(release)
|
||||
if err := <-done; err == nil {
|
||||
t.Fatal("injected failure ignored")
|
||||
}
|
||||
if !errors.Is(proxyErr, context.Canceled) || !errors.Is(directErr, context.Canceled) {
|
||||
t.Fatalf("waiting requests ignored cancellation: proxy=%v direct=%v", proxyErr, directErr)
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func TestConcurrentRegistrationSameUsername(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
const body = `{"username":"alice","display_name":"Алиса","password":"lab2-test-password"}`
|
||||
var wg sync.WaitGroup
|
||||
start := make(chan struct{})
|
||||
results := make(chan int, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-start
|
||||
r := httptest.NewRequest(http.MethodPost, "/api/v1/users", strings.NewReader(body))
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
results <- w.Code
|
||||
}()
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
close(results)
|
||||
counts := map[int]int{}
|
||||
for status := range results {
|
||||
counts[status]++
|
||||
}
|
||||
if counts[201] != 1 || counts[409] != 1 {
|
||||
t.Fatalf("registration statuses: %v", counts)
|
||||
}
|
||||
var users int
|
||||
if err := f.db.QueryRow(context.Background(), "SELECT count(*) FROM users WHERE username='alice'").Scan(&users); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if users != 1 {
|
||||
t.Fatalf("stored %d users with the same username", users)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatTransactionRollsBackAfterMemberInsertFailure(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
alice, _ := createTestUser(t, f.users, "alice", "Алиса")
|
||||
bob, _ := createTestUser(t, f.users, "bob", "Боб")
|
||||
_, err := f.db.Exec(context.Background(), `CREATE FUNCTION reject_bob() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF NEW.user_id = '`+bob.ID+`'::uuid THEN RAISE EXCEPTION 'forced member failure'; END IF;
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
CREATE TRIGGER reject_bob_insert BEFORE INSERT ON chat_members FOR EACH ROW EXECUTE FUNCTION reject_bob();`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.chats.Create(context.Background(), "rollback-check", []string{alice.ID, bob.ID}); err == nil {
|
||||
t.Fatal("expected second member insertion to fail")
|
||||
}
|
||||
var chats, members int
|
||||
if err := f.db.QueryRow(context.Background(), "SELECT count(*) FROM chats WHERE title='rollback-check'").Scan(&chats); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.db.QueryRow(context.Background(), "SELECT count(*) FROM chat_members").Scan(&members); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if chats != 0 || members != 0 {
|
||||
t.Fatalf("transaction left chats=%d members=%d", chats, members)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShortSessionTTLAndLogout(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Алиса")
|
||||
t.Setenv("SESSION_TTL_SECONDS", "2")
|
||||
ttl, err := services.SessionTTL()
|
||||
if err != nil || ttl != 2*time.Second {
|
||||
t.Fatalf("short TTL: %v, %v", ttl, err)
|
||||
}
|
||||
sessions := services.NewSessionService(services.NewUserService(f.users), f.sessionStore, ttl)
|
||||
router := handlers.NewRouter(nil, services.NewUserService(f.users), sessions, nil, nil)
|
||||
assertHTTPUser := func(token string, want int) {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, r)
|
||||
if w.Code != want {
|
||||
t.Fatalf("short TTL HTTP status: got %d, want %d", w.Code, want)
|
||||
}
|
||||
t.Logf("GET /api/v1/users/me: HTTP %d", w.Code)
|
||||
}
|
||||
token, _, err := sessions.CreateForUser(context.Background(), user.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := sessions.User(context.Background(), token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertHTTPUser(token, 200)
|
||||
expiryStarted := time.Now()
|
||||
expiryDeadline := expiryStarted.Add(5 * time.Second)
|
||||
for {
|
||||
_, err := sessions.User(context.Background(), token)
|
||||
if errors.Is(err, services.ErrInvalidSession) {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("checking expiry: %v", err)
|
||||
}
|
||||
if time.Now().After(expiryDeadline) {
|
||||
t.Fatal("session with TTL=2 seconds did not expire within 5 seconds")
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
t.Logf("SESSION_TTL_SECONDS=2: expiry observed after %s", time.Since(expiryStarted))
|
||||
assertHTTPUser(token, 401)
|
||||
token, _, err = f.sessions.CreateForUser(context.Background(), user.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.sessions.Revoke(context.Background(), token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.sessions.User(context.Background(), token); err != services.ErrInvalidSession {
|
||||
t.Fatalf("revoked token: %v", err)
|
||||
}
|
||||
assertHTTPUser(token, 401)
|
||||
}
|
||||
|
||||
func TestWrongPasswordAndLogoutHTTP(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
createTestUser(t, f.users, "alice", "Алиса")
|
||||
login := func(password string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", nil)
|
||||
r.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("alice:"+password)))
|
||||
w := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
if w := login("wrong-password"); w.Code != 401 {
|
||||
t.Fatalf("wrong password returned %d", w.Code)
|
||||
}
|
||||
missingUser := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", nil)
|
||||
missingUser.SetBasicAuth("missing_user", "lab2-test-password")
|
||||
missingUserResponse := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(missingUserResponse, missingUser)
|
||||
if missingUserResponse.Code != 401 {
|
||||
t.Fatalf("unknown username returned %d", missingUserResponse.Code)
|
||||
}
|
||||
w := login("lab2-test-password")
|
||||
if w.Code != 201 {
|
||||
t.Fatalf("valid login returned %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var result struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodGet, "/api/v1/chats/not-a-uuid/messages", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+result.Token)
|
||||
w = httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("invalid chat UUID returned %d", w.Code)
|
||||
}
|
||||
r = httptest.NewRequest(http.MethodDelete, "/api/v1/auth/sessions/current", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+result.Token)
|
||||
w = httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
if w.Code != 204 {
|
||||
t.Fatalf("logout returned %d", w.Code)
|
||||
}
|
||||
r = httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+result.Token)
|
||||
w = httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
if w.Code != 401 {
|
||||
t.Fatalf("revoked token returned %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDependencyFailuresReturnUnavailable(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Алиса")
|
||||
failedRedis := repositories.NewRedisClient("127.0.0.1:1")
|
||||
defer failedRedis.Close()
|
||||
failedSessions := repositories.NewSessionStore(failedRedis, "unreachable:")
|
||||
redisRouter := handlers.NewRouter(services.NewHealthService(repositories.NewHealthStore(f.db), failedSessions), services.NewUserService(f.users), services.NewSessionService(services.NewUserService(f.users), failedSessions, time.Hour), services.NewChatService(f.chats), services.NewMessageService(f.messages))
|
||||
assertStatus := func(router http.Handler, method, path, auth string, want int) {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest(method, path, nil)
|
||||
if auth != "" {
|
||||
r.Header.Set("Authorization", auth)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, r)
|
||||
if w.Code != want {
|
||||
t.Fatalf("%s %s returned %d, want %d: %s", method, path, w.Code, want, w.Body.String())
|
||||
}
|
||||
}
|
||||
assertStatus(redisRouter, "GET", "/health/ready", "", 503)
|
||||
assertStatus(redisRouter, "GET", "/api/v1/users/me", "Bearer opaque-token", 503)
|
||||
assertStatus(redisRouter, "POST", "/api/v1/auth/sessions", "Basic "+base64.StdEncoding.EncodeToString([]byte("alice:lab2-test-password")), 503)
|
||||
|
||||
pool, err := pgxpool.New(context.Background(), os.Getenv("TEST_DATABASE_URL"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pool.Close()
|
||||
userStore := repositories.NewUserStore(pool)
|
||||
dbRouter := handlers.NewRouter(services.NewHealthService(repositories.NewHealthStore(pool), f.sessionStore), services.NewUserService(userStore), services.NewSessionService(services.NewUserService(userStore), f.sessionStore, time.Hour), services.NewChatService(repositories.NewChatStore(pool)), services.NewMessageService(repositories.NewMessageStore(pool)))
|
||||
token, _, err := f.sessions.CreateForUser(context.Background(), user.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertStatus(dbRouter, "GET", "/health/ready", "", 503)
|
||||
assertStatus(dbRouter, "GET", "/api/v1/users/me", "Bearer "+token, 503)
|
||||
assertStatus(dbRouter, "GET", "/api/v1/users/me", "", 401)
|
||||
}
|
||||
|
||||
func TestHistoryQueryPlanAtTenThousandMessages(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Алиса")
|
||||
chat := createTestChat(t, f.chats, "plan-check", []string{user.ID})
|
||||
ctx := context.Background()
|
||||
_, err := f.db.Exec(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,created_at)
|
||||
SELECT gen_random_uuid(),$1,$2,'plan message','2026-01-01'::timestamptz + n * interval '1 microsecond'
|
||||
FROM generate_series(1,10000) AS n`, chat.ID, user.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, "ANALYZE messages"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var lateTime time.Time
|
||||
var lateID string
|
||||
if err := f.db.QueryRow(ctx, "SELECT created_at,id FROM messages WHERE chat_id=$1 ORDER BY created_at,id OFFSET 9000 LIMIT 1", chat.ID).Scan(&lateTime, &lateID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
at time.Time
|
||||
id string
|
||||
}{
|
||||
{"first", time.Time{}, "00000000-0000-0000-0000-000000000000"},
|
||||
{"late", lateTime, lateID},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var plan []byte
|
||||
err := f.db.QueryRow(ctx, `EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
|
||||
SELECT id,chat_id,sender_id,text,created_at FROM messages
|
||||
WHERE chat_id=$1 AND (created_at,id)>($2,$3::uuid)
|
||||
ORDER BY created_at,id LIMIT $4`, chat.ID, tc.at, tc.id, 51).Scan(&plan)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Contains(plan, []byte("messages_history_idx")) || bytes.Contains(plan, []byte(`"Node Type": "Seq Scan"`)) {
|
||||
t.Fatalf("history page did not use the index: %s", plan)
|
||||
}
|
||||
t.Logf("%s page EXPLAIN (ANALYZE, BUFFERS): %s", tc.name, plan)
|
||||
})
|
||||
}
|
||||
var count int
|
||||
if err := f.db.QueryRow(ctx, "SELECT count(*) FROM messages WHERE chat_id=$1", chat.ID).Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count < 10000 {
|
||||
t.Fatal(fmt.Errorf("history contains only %d messages", count))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func TestIdempotencyConcurrentStoresAndScopes(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
alice, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
bob, _ := createTestUser(t, f.users, "bob", "Bob")
|
||||
chat := createTestChat(t, f.chats, "Chat", []string{alice.ID, bob.ID})
|
||||
otherChat := createTestChat(t, f.chats, "Other", []string{alice.ID})
|
||||
stores := []*repositories.MessageStore{f.messages, repositories.NewMessageStore(f.db)}
|
||||
const concurrency = 24
|
||||
start := make(chan struct{})
|
||||
results := make(chan repositories.Message, concurrency)
|
||||
errorsFound := make(chan error, concurrency)
|
||||
var group sync.WaitGroup
|
||||
for i := 0; i < concurrency; i++ {
|
||||
group.Add(1)
|
||||
go func(i int) {
|
||||
defer group.Done()
|
||||
<-start
|
||||
message, err := stores[i%len(stores)].CreateIdempotent(context.Background(), chat.ID, alice.ID, "hello", "same-key")
|
||||
results <- message
|
||||
errorsFound <- err
|
||||
}(i)
|
||||
}
|
||||
close(start)
|
||||
group.Wait()
|
||||
close(results)
|
||||
close(errorsFound)
|
||||
for err := range errorsFound {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var original repositories.Message
|
||||
for message := range results {
|
||||
if original.ID == "" {
|
||||
original = message
|
||||
} else if !reflect.DeepEqual(message, original) {
|
||||
t.Fatalf("replay changed the message: %v != %v", message, original)
|
||||
}
|
||||
}
|
||||
if len(listTestMessages(t, f.messages, chat.ID)) != 1 {
|
||||
t.Fatal("concurrent repeat created duplicate messages")
|
||||
}
|
||||
if _, err := f.messages.CreateIdempotent(context.Background(), chat.ID, alice.ID, "different", "same-key"); !errors.Is(err, repositories.ErrIdempotencyConflict) {
|
||||
t.Fatalf("different body: got %v", err)
|
||||
}
|
||||
for _, scope := range [][2]string{{chat.ID, bob.ID}, {otherChat.ID, alice.ID}} {
|
||||
message, err := f.messages.CreateIdempotent(context.Background(), scope[0], scope[1], "hello", "same-key")
|
||||
if err != nil || message.ID == original.ID {
|
||||
t.Fatalf("key leaked across scopes: %v", err)
|
||||
}
|
||||
}
|
||||
if _, err := f.db.Exec(context.Background(), "UPDATE message_idempotency SET expires_at=now()-interval '1 second' WHERE message_id=$1", original.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reused, err := f.messages.CreateIdempotent(context.Background(), chat.ID, alice.ID, "after expiry", "same-key")
|
||||
if err != nil || reused.ID == original.ID {
|
||||
t.Fatalf("expired key cannot be reused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdempotencyRecordFailureRollsBackMessage(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
alice, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Chat", []string{alice.ID})
|
||||
// Force a failure AFTER the message insert to verify transaction atomicity.
|
||||
_, err := f.db.Exec(context.Background(), `
|
||||
CREATE FUNCTION reject_replay() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN RAISE EXCEPTION 'synthetic replay failure'; END $$;
|
||||
CREATE TRIGGER reject_replay BEFORE INSERT ON message_idempotency
|
||||
FOR EACH ROW EXECUTE FUNCTION reject_replay()`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.messages.CreateIdempotent(context.Background(), chat.ID, alice.ID, "hello", "retry-key"); err == nil {
|
||||
t.Fatal("synthetic failure did not happen")
|
||||
}
|
||||
if len(listTestMessages(t, f.messages, chat.ID)) != 0 {
|
||||
t.Fatal("failed transaction left a message behind")
|
||||
}
|
||||
if _, err := f.db.Exec(context.Background(), "DROP TRIGGER reject_replay ON message_idempotency"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.messages.CreateIdempotent(context.Background(), chat.ID, alice.ID, "hello", "retry-key"); err != nil {
|
||||
t.Fatalf("failure was cached instead of permitting retry: %v", err)
|
||||
}
|
||||
if len(listTestMessages(t, f.messages, chat.ID)) != 1 {
|
||||
t.Fatal("retry did not produce exactly one message")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestMessengerWorkflow(t *testing.T) {
|
||||
router := newTestFixture(t).router
|
||||
|
||||
call := func(method, path, body, auth string, expected int) map[string]any {
|
||||
t.Helper()
|
||||
request := httptest.NewRequest(method, path, bytes.NewBufferString(body))
|
||||
if body != "" {
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if method == http.MethodPost && strings.HasSuffix(path, "/messages") {
|
||||
request.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
}
|
||||
if auth != "" {
|
||||
request.Header.Set("Authorization", auth)
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
router.ServeHTTP(response, request)
|
||||
if response.Code != expected {
|
||||
t.Fatalf("%s %s: status %d, want %d: %s", method, path, response.Code, expected, response.Body.String())
|
||||
}
|
||||
if response.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatalf("%s %s: missing request ID", method, path)
|
||||
}
|
||||
if response.Code == http.StatusNoContent {
|
||||
return nil
|
||||
}
|
||||
var result map[string]any
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
user := call("POST", "/api/v1/users", `{"username":"alice","display_name":"Алиса","password":"lab2-test-password"}`, "", 201)
|
||||
userID := user["id"].(string)
|
||||
basic := "Basic " + base64.StdEncoding.EncodeToString([]byte("alice:lab2-test-password"))
|
||||
session := call("POST", "/api/v1/auth/sessions", "", basic, 201)
|
||||
bearer := "Bearer " + session["token"].(string)
|
||||
call("GET", "/api/v1/users/me", "", bearer, 200)
|
||||
chat := call("POST", "/api/v1/chats", `{"title":"Тестовый чат","member_ids":["`+userID+`"]}`, bearer, 201)
|
||||
chatID := chat["id"].(string)
|
||||
path := "/api/v1/chats/" + chatID + "/messages"
|
||||
for i := 0; i < 3; i++ {
|
||||
call("POST", path, `{"text":"message"}`, bearer, 201)
|
||||
}
|
||||
page1 := call("GET", path+"?limit=2", "", bearer, 200)
|
||||
first := page1["items"].([]any)
|
||||
if len(first) != 2 || page1["next_cursor"] == nil {
|
||||
t.Fatalf("unexpected first page: %v", page1)
|
||||
}
|
||||
page2 := call("GET", path+"?limit=2&cursor="+url.QueryEscape(page1["next_cursor"].(string)), "", bearer, 200)
|
||||
second := page2["items"].([]any)
|
||||
if len(second) != 1 || page2["next_cursor"] != nil || first[0].(map[string]any)["id"] == second[0].(map[string]any)["id"] {
|
||||
t.Fatalf("unexpected second page: %v", page2)
|
||||
}
|
||||
call("DELETE", "/api/v1/auth/sessions/current", "", bearer, 204)
|
||||
call("GET", "/api/v1/users/me", "", bearer, 401)
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/repositories"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestJobExpiredLeaseCannotPublishOrExceedAttempts(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "lease_user", "Lease")
|
||||
chat := createTestChat(t, f.chats, "Lease", []string{user.ID})
|
||||
ctx := context.Background()
|
||||
id := uuid.NewString()
|
||||
store := repositories.NewAttachmentStore(f.db)
|
||||
if err := store.Create(ctx, repositories.Attachment{ID: id, ChatID: chat.ID, OwnerID: user.ID, Filename: "x.png", ObjectKey: "originals/" + id, ContentType: "image/png", SizeBytes: 1, SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", CreatedAt: time.Now(), UpdatedAt: time.Now()}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
job := repositories.Job{ID: uuid.NewString(), AttachmentID: id, PipelineVersion: 1}
|
||||
if _, err := f.db.Exec(ctx, `UPDATE attachments SET status='queued' WHERE id=$1;`, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id,staging_key) VALUES($1,$2,$3)`, job.ID, id, "staging/jobs/"+id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, err := store.StartAttempt(ctx, job, time.Second, 2)
|
||||
if err != nil || first == "" {
|
||||
t.Fatal("claim", err)
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE attachment_jobs SET lease_until=now()-interval '1 second' WHERE id=$1`, job.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := store.StartAttempt(ctx, job, time.Second, 2)
|
||||
if err != nil || second == "" || second == first {
|
||||
t.Fatal("reclaim", err)
|
||||
}
|
||||
effects := 0
|
||||
callback := func(repositories.Attachment, string) ([]repositories.Variant, string, error) {
|
||||
effects++
|
||||
return nil, "", errors.New("injected storage failure")
|
||||
}
|
||||
if err = store.ProcessJob(ctx, job, first, 2, callback); err == nil || effects != 0 {
|
||||
t.Fatal("stale worker performed side effect")
|
||||
}
|
||||
if err = store.ProcessJob(ctx, job, second, 2, callback); err != nil || effects != 1 {
|
||||
t.Fatal("new worker failed", err)
|
||||
}
|
||||
if token, err := store.StartAttempt(ctx, job, time.Second, 2); err != nil || token != "" {
|
||||
t.Fatal("failed job reclaimed", err)
|
||||
}
|
||||
var status string
|
||||
var attempts int
|
||||
if err = f.db.QueryRow(ctx, `SELECT a.status,j.attempts FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.id=$1`, job.ID).Scan(&status, &attempts); err != nil || status != "failed" || attempts != 2 {
|
||||
t.Fatalf("%s/%d %v", status, attempts, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetryBackoffStaysInExponentialJitterWindow(t *testing.T) {
|
||||
for attempt, bounds := range map[int][2]time.Duration{1: {time.Second, 2 * time.Second}, 2: {2 * time.Second, 4 * time.Second}, 3: {4 * time.Second, 8 * time.Second}, 8: {15 * time.Second, 30 * time.Second}} {
|
||||
seen := map[time.Duration]bool{}
|
||||
for i := 0; i < 20; i++ {
|
||||
delay := repositories.RetryDelay(attempt)
|
||||
if delay < bounds[0] || delay > bounds[1] {
|
||||
t.Fatalf("attempt %d: %v", attempt, delay)
|
||||
}
|
||||
seen[delay] = true
|
||||
}
|
||||
if len(seen) < 2 {
|
||||
t.Fatal("jitter produced no variation")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestDecodeJSONBodyRequiresExactFieldNames(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
user, _ := createTestUser(t, fixture.users, "json_tester", "JSON tester")
|
||||
chat := createTestChat(t, fixture.chats, "JSON body tests", []string{user.ID})
|
||||
path := "/api/v1/chats/" + chat.ID + "/messages"
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
body string
|
||||
valid bool
|
||||
}{
|
||||
{"canonical", `{"text":"first","attachment_ids":[]}`, true},
|
||||
{"escaped canonical name", `{"te\u0078t":"first"}`, true},
|
||||
{"uppercase extra", `{"text":"first","TEXT":"second"}`, false},
|
||||
{"uppercase extra first", `{"TEXT":"second","text":"first"}`, false},
|
||||
{"mixed case", `{"Text":"second"}`, false},
|
||||
{"uppercase attachments", `{"text":"first","ATTACHMENT_IDS":[]}`, false},
|
||||
{"escaped uppercase", `{"text":"first","\u0054EXT":"second"}`, false},
|
||||
{"unknown field", `{"text":"first","sender_id":"other"}`, false},
|
||||
{"wrong type", `{"text":123}`, false},
|
||||
{"null", `null`, false},
|
||||
{"array", `[]`, false},
|
||||
{"trailing object", `{"text":"first"}{}`, false},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodPost, path, strings.NewReader(test.body))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
authorizeTestRequest(t, fixture, request, user.ID)
|
||||
response := httptest.NewRecorder()
|
||||
fixture.router.ServeHTTP(response, request)
|
||||
want := http.StatusBadRequest
|
||||
if test.valid {
|
||||
want = http.StatusCreated
|
||||
}
|
||||
if response.Code != want {
|
||||
t.Fatalf("want %d, got %d: %s", want, response.Code, response.Body.String())
|
||||
}
|
||||
if test.valid {
|
||||
var value map[string]any
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if value["text"] != "first" {
|
||||
t.Fatal("text changed")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateMessageBlankText(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
userStore, chatStore, messageStore := fixture.users, fixture.chats, fixture.messages
|
||||
|
||||
user, created := createTestUser(t, userStore, "alice", "Алиса")
|
||||
if !created {
|
||||
t.Fatal("expected user to be created")
|
||||
}
|
||||
|
||||
chat := createTestChat(t, chatStore,
|
||||
"Тестовый чат",
|
||||
[]string{user.ID},
|
||||
)
|
||||
|
||||
handler := fixture.router
|
||||
|
||||
request := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/chats/"+chat.ID+"/messages",
|
||||
strings.NewReader(`{"text":" \t\n"}`),
|
||||
)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
authorizeTestRequest(t, fixture, request, user.ID)
|
||||
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf(
|
||||
"expected status %d, got %d; body: %s",
|
||||
http.StatusBadRequest,
|
||||
response.Code,
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if response.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("expected non-empty X-Request-Id header")
|
||||
}
|
||||
|
||||
if !strings.Contains(
|
||||
response.Body.String(),
|
||||
`"code":"invalid_request"`,
|
||||
) {
|
||||
t.Fatalf(
|
||||
"expected invalid_request error code, got body: %s",
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
messages := listTestMessages(t, messageStore, chat.ID)
|
||||
|
||||
if len(messages) != 0 {
|
||||
t.Fatalf(
|
||||
"expected no messages to be stored, got %d",
|
||||
len(messages),
|
||||
)
|
||||
}
|
||||
}
|
||||
func TestCreateMessageFromChatOutsider(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
userStore, chatStore, messageStore := fixture.users, fixture.chats, fixture.messages
|
||||
|
||||
alice, created := createTestUser(t, userStore, "alice", "Алиса")
|
||||
if !created {
|
||||
t.Fatal("expected Alice to be created")
|
||||
}
|
||||
|
||||
bob, created := createTestUser(t, userStore, "bob", "Боб")
|
||||
if !created {
|
||||
t.Fatal("expected Bob to be created")
|
||||
}
|
||||
|
||||
chat := createTestChat(t, chatStore,
|
||||
"Чат Алисы",
|
||||
[]string{alice.ID},
|
||||
)
|
||||
|
||||
handler := fixture.router
|
||||
|
||||
request := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/chats/"+chat.ID+"/messages",
|
||||
strings.NewReader(`{"text":"Сообщение чужому чату"}`),
|
||||
)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
authorizeTestRequest(t, fixture, request, bob.ID)
|
||||
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusNotFound {
|
||||
t.Fatalf(
|
||||
"expected status %d, got %d; body: %s",
|
||||
http.StatusNotFound,
|
||||
response.Code,
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if response.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("expected non-empty X-Request-Id header")
|
||||
}
|
||||
|
||||
if !strings.Contains(
|
||||
response.Body.String(),
|
||||
`"code":"not_found"`,
|
||||
) {
|
||||
t.Fatalf(
|
||||
"expected not_found error code, got body: %s",
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
messages := listTestMessages(t, messageStore, chat.ID)
|
||||
|
||||
if len(messages) != 0 {
|
||||
t.Fatalf(
|
||||
"expected no messages to be stored, got %d",
|
||||
len(messages),
|
||||
)
|
||||
}
|
||||
}
|
||||
func TestListMessagesPagination(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
userStore, chatStore, messageStore := fixture.users, fixture.chats, fixture.messages
|
||||
|
||||
user, created := createTestUser(t, userStore, "alice", "Алиса")
|
||||
if !created {
|
||||
t.Fatal("expected user to be created")
|
||||
}
|
||||
|
||||
chat := createTestChat(t, chatStore,
|
||||
"Тестовый чат",
|
||||
[]string{user.ID},
|
||||
)
|
||||
|
||||
first := createTestMessage(t, messageStore, chat.ID, user.ID, "Первое сообщение")
|
||||
second := createTestMessage(t, messageStore, chat.ID, user.ID, "Второе сообщение")
|
||||
third := createTestMessage(t, messageStore, chat.ID, user.ID, "Третье сообщение")
|
||||
|
||||
expectedIDs := map[string]bool{
|
||||
first.ID: true,
|
||||
second.ID: true,
|
||||
third.ID: true,
|
||||
}
|
||||
|
||||
handler := fixture.router
|
||||
|
||||
// Запрашиваем только два сообщения из трёх.
|
||||
firstRequest := httptest.NewRequest(
|
||||
http.MethodGet,
|
||||
"/api/v1/chats/"+chat.ID+"/messages?limit=2",
|
||||
nil,
|
||||
)
|
||||
authorizeTestRequest(t, fixture, firstRequest, user.ID)
|
||||
|
||||
firstResponse := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(firstResponse, firstRequest)
|
||||
|
||||
if firstResponse.Code != http.StatusOK {
|
||||
t.Fatalf(
|
||||
"expected first page status %d, got %d; body: %s",
|
||||
http.StatusOK,
|
||||
firstResponse.Code,
|
||||
firstResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
var firstPage messagePage
|
||||
|
||||
if err := json.NewDecoder(firstResponse.Body).Decode(&firstPage); err != nil {
|
||||
t.Fatalf("failed to decode first page: %v", err)
|
||||
}
|
||||
|
||||
if len(firstPage.Items) != 2 {
|
||||
t.Fatalf(
|
||||
"expected 2 messages on first page, got %d",
|
||||
len(firstPage.Items),
|
||||
)
|
||||
}
|
||||
|
||||
if firstPage.NextCursor == nil || *firstPage.NextCursor == "" {
|
||||
t.Fatal("expected non-empty next cursor on first page")
|
||||
}
|
||||
|
||||
// Проверяем, что сообщения на первой странице уникальны
|
||||
// и принадлежат исходному набору.
|
||||
seenIDs := make(map[string]bool)
|
||||
|
||||
for _, message := range firstPage.Items {
|
||||
if !expectedIDs[message.ID] {
|
||||
t.Fatalf("unexpected message ID on first page: %s", message.ID)
|
||||
}
|
||||
|
||||
if seenIDs[message.ID] {
|
||||
t.Fatalf("duplicate message on first page: %s", message.ID)
|
||||
}
|
||||
|
||||
seenIDs[message.ID] = true
|
||||
}
|
||||
|
||||
// Используем cursor первой страницы для получения второй.
|
||||
secondRequest := httptest.NewRequest(
|
||||
http.MethodGet,
|
||||
"/api/v1/chats/"+chat.ID+"/messages?limit=2&cursor="+
|
||||
url.QueryEscape(*firstPage.NextCursor),
|
||||
nil,
|
||||
)
|
||||
authorizeTestRequest(t, fixture, secondRequest, user.ID)
|
||||
|
||||
secondResponse := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(secondResponse, secondRequest)
|
||||
|
||||
if secondResponse.Code != http.StatusOK {
|
||||
t.Fatalf(
|
||||
"expected second page status %d, got %d; body: %s",
|
||||
http.StatusOK,
|
||||
secondResponse.Code,
|
||||
secondResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
var secondPage messagePage
|
||||
|
||||
if err := json.NewDecoder(secondResponse.Body).Decode(&secondPage); err != nil {
|
||||
t.Fatalf("failed to decode second page: %v", err)
|
||||
}
|
||||
|
||||
if len(secondPage.Items) != 1 {
|
||||
t.Fatalf(
|
||||
"expected 1 message on second page, got %d",
|
||||
len(secondPage.Items),
|
||||
)
|
||||
}
|
||||
|
||||
if secondPage.NextCursor != nil {
|
||||
t.Fatalf("expected second page to be final, got cursor %q", *secondPage.NextCursor)
|
||||
}
|
||||
|
||||
// Проверяем, что вторая страница не повторяет первую.
|
||||
for _, message := range secondPage.Items {
|
||||
if seenIDs[message.ID] {
|
||||
t.Fatalf("message repeated on second page: %s", message.ID)
|
||||
}
|
||||
|
||||
if !expectedIDs[message.ID] {
|
||||
t.Fatalf("unexpected message ID on second page: %s", message.ID)
|
||||
}
|
||||
|
||||
seenIDs[message.ID] = true
|
||||
}
|
||||
|
||||
if len(seenIDs) != len(expectedIDs) {
|
||||
t.Fatalf(
|
||||
"pagination lost messages: expected %d unique messages, got %d",
|
||||
len(expectedIDs),
|
||||
len(seenIDs),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestListPageByChatUsesCursorAndLimit(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Алиса")
|
||||
chat := createTestChat(t, f.chats, "Пагинация", []string{user.ID})
|
||||
other := createTestChat(t, f.chats, "Другой чат", []string{user.ID})
|
||||
createTestMessage(t, f.messages, other.ID, user.ID, "Не должно попасть в страницу")
|
||||
createdAt := time.Date(2026, 9, 18, 12, 0, 0, 0, time.UTC)
|
||||
ids := []string{
|
||||
"00000000-0000-4000-8000-000000000001", "00000000-0000-4000-8000-000000000002",
|
||||
"00000000-0000-4000-8000-000000000003", "00000000-0000-4000-8000-000000000004",
|
||||
}
|
||||
// Insert in reverse order to exercise SQL ordering, including equal timestamps.
|
||||
for i := len(ids) - 1; i >= 0; i-- {
|
||||
at := createdAt
|
||||
if i > 1 {
|
||||
at = at.Add(time.Duration(i-1) * time.Second)
|
||||
}
|
||||
_, err := f.db.Exec(context.Background(), "INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES($1,$2,$3,$4,$5)", ids[i], chat.ID, user.ID, "message", at)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
first, more, err := f.messages.ListPageByChat(context.Background(), chat.ID, nil, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !more || len(first) != 2 || first[0].ID != ids[0] || first[1].ID != ids[1] {
|
||||
t.Fatalf("unexpected first page: %v, more=%v", first, more)
|
||||
}
|
||||
cursor := &messageCursor{ChatID: chat.ID, CreatedAt: first[1].CreatedAt, MessageID: first[1].ID}
|
||||
second, more, err := f.messages.ListPageByChat(context.Background(), chat.ID, cursor, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if more || len(second) != 2 || second[0].ID != ids[2] || second[1].ID != ids[3] {
|
||||
t.Fatalf("unexpected second page: %v, more=%v", second, more)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListPageByChatReturnsNonNilEmptyPage(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
page, more, err := f.messages.ListPageByChat(context.Background(), uuid.NewString(), nil, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if page == nil || len(page) != 0 || more {
|
||||
t.Fatalf("unexpected empty page: %v, more=%v", page, more)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/repositories"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPipelineUpgradeFencesOldLeaseAndPublishesV2(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
ctx := context.Background()
|
||||
store := repositories.NewAttachmentStore(f.db)
|
||||
user, _ := createTestUser(t, f.users, "versions", "Versions")
|
||||
chat := createTestChat(t, f.chats, "Versions", []string{user.ID})
|
||||
id, oldID := uuid.NewString(), uuid.NewString()
|
||||
if err := store.Create(ctx, repositories.Attachment{ID: id, ChatID: chat.ID, OwnerID: user.ID, Filename: "v.png", ObjectKey: "originals/" + id, ContentType: "image/png", SizeBytes: 1, SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", CreatedAt: time.Now(), UpdatedAt: time.Now()}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, `UPDATE attachments SET status='queued' WHERE id=$1`, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id,staging_key) VALUES($1,$2,$3)`, oldID, id, "staging/jobs/"+id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := repositories.Job{ID: oldID, AttachmentID: id, PipelineVersion: 1}
|
||||
token, err := store.StartAttempt(ctx, old, time.Minute, 5)
|
||||
if err != nil || token == "" {
|
||||
t.Fatal(err)
|
||||
}
|
||||
changed, err := store.UpgradePipeline(ctx, id, 2, "unit test upgrade", func(context.Context, string) error { return nil })
|
||||
if err != nil || !changed {
|
||||
t.Fatal(err)
|
||||
}
|
||||
effects := 0
|
||||
callback := func(a repositories.Attachment, _ string) ([]repositories.Variant, string, error) {
|
||||
effects++
|
||||
if a.PipelineVersion != 2 {
|
||||
t.Fatal("old version reached effects")
|
||||
}
|
||||
return []repositories.Variant{{Name: "thumbnail"}, {Name: "crop"}, {Name: "optimized"}}, "", nil
|
||||
}
|
||||
if err = store.ProcessJob(ctx, old, token, 5, callback); !errors.Is(err, repositories.ErrStaleJob) || effects != 0 {
|
||||
t.Fatal("old lease not fenced", err)
|
||||
}
|
||||
if terminal, err := store.JobTerminal(ctx, oldID); err != nil || !terminal {
|
||||
t.Fatal("superseded delivery not terminal", err)
|
||||
}
|
||||
var newID string
|
||||
if err = f.db.QueryRow(ctx, `SELECT id FROM attachment_jobs WHERE attachment_id=$1 AND pipeline_version=2`, id).Scan(&newID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
next := repositories.Job{ID: newID, AttachmentID: id, PipelineVersion: 2}
|
||||
token, err = store.StartAttempt(ctx, next, time.Minute, 5)
|
||||
if err != nil || token == "" {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = store.ProcessJob(ctx, next, token, 5, callback); err != nil || effects != 1 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
published, err := store.GetForMember(ctx, id, user.ID)
|
||||
if err != nil || published.Status != "ready" || published.PipelineVersion != 2 {
|
||||
t.Fatal("wrong active publication", err)
|
||||
}
|
||||
if changed, err = store.UpgradePipeline(ctx, id, 1, "no downgrade", nil); err != nil || changed {
|
||||
t.Fatal("version rollback allowed", err)
|
||||
}
|
||||
if changed, err = store.ReplayJob(ctx, oldID, "obsolete replay", nil); err != nil || changed {
|
||||
t.Fatal("old job reopened", err)
|
||||
}
|
||||
var count int
|
||||
if err = f.db.QueryRow(ctx, `SELECT count(*) FROM attachment_publications WHERE attachment_id=$1 AND pipeline_version=2`, id).Scan(&count); err != nil || count != 1 {
|
||||
t.Fatal("publication count", count, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
madmin "github.com/minio/madmin-go/v3"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
func s3Client(t *testing.T, root bool, anonymous bool) *minio.Client {
|
||||
t.Helper()
|
||||
if os.Getenv("S3_ENDPOINT") == "" {
|
||||
t.Skip("S3 integration: run make test-backend")
|
||||
}
|
||||
endpoint, err := url.Parse(os.Getenv("S3_ENDPOINT"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
read := func(name string) string {
|
||||
data, err := os.ReadFile(os.Getenv(name))
|
||||
if err != nil {
|
||||
t.Fatal("S3 credential file not mounted")
|
||||
}
|
||||
return strings.TrimSpace(string(data))
|
||||
}
|
||||
access, secret := "", ""
|
||||
if !anonymous {
|
||||
if root {
|
||||
access = read("S3_ROOT_ACCESS_KEY_ID_FILE")
|
||||
secret = read("S3_ROOT_SECRET_ACCESS_KEY_FILE")
|
||||
} else {
|
||||
access = read("S3_ACCESS_KEY_ID_FILE")
|
||||
secret = read("S3_SECRET_ACCESS_KEY_FILE")
|
||||
}
|
||||
}
|
||||
client, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(access, secret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return client
|
||||
}
|
||||
func denied(t *testing.T, err error) {
|
||||
t.Helper()
|
||||
if err == nil || minio.ToErrorResponse(err).StatusCode != 403 {
|
||||
t.Fatalf("expected S3 HTTP 403, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestS3PermissionsAndBootstrap(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
app := s3Client(t, false, false)
|
||||
root := s3Client(t, true, false)
|
||||
anon := s3Client(t, false, true)
|
||||
bucket := os.Getenv("S3_BUCKET")
|
||||
key := "originals/" + uuid.NewString()
|
||||
data := []byte("permission fixture")
|
||||
t.Cleanup(func() { root.RemoveObject(context.Background(), bucket, key, minio.RemoveObjectOptions{}) })
|
||||
if _, err := app.PutObject(ctx, bucket, key, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := repositories.BootstrapS3(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
content, err := app.GetObject(ctx, bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual, err := io.ReadAll(content)
|
||||
content.Close()
|
||||
if err != nil || !bytes.Equal(actual, data) {
|
||||
t.Fatalf("bootstrap changed existing object: %v", err)
|
||||
}
|
||||
object, err := anon.GetObject(ctx, bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = object.Stat()
|
||||
object.Close()
|
||||
denied(t, err)
|
||||
listing, ok := <-anon.ListObjects(ctx, bucket, minio.ListObjectsOptions{})
|
||||
if !ok {
|
||||
t.Fatal("anonymous list did not return AccessDenied")
|
||||
}
|
||||
denied(t, listing.Err)
|
||||
forbidden := "forbidden/" + uuid.NewString()
|
||||
t.Cleanup(func() { root.RemoveObject(context.Background(), bucket, forbidden, minio.RemoveObjectOptions{}) })
|
||||
_, err = app.PutObject(ctx, bucket, forbidden, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{})
|
||||
denied(t, err)
|
||||
if _, err = root.PutObject(ctx, bucket, forbidden, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
outside, err := app.GetObject(ctx, bucket, forbidden, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = outside.Stat()
|
||||
outside.Close()
|
||||
denied(t, err)
|
||||
denied(t, app.RemoveObject(ctx, bucket, forbidden, minio.RemoveObjectOptions{}))
|
||||
other := "upload-permissions-" + uuid.NewString()
|
||||
if err = root.MakeBucket(ctx, other, minio.MakeBucketOptions{Region: os.Getenv("S3_REGION")}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
root.RemoveObject(context.Background(), other, key, minio.RemoveObjectOptions{})
|
||||
root.RemoveBucket(context.Background(), other)
|
||||
})
|
||||
_, err = app.PutObject(ctx, other, key, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{})
|
||||
denied(t, err)
|
||||
denied(t, app.MakeBucket(ctx, "forbidden-"+uuid.NewString(), minio.MakeBucketOptions{}))
|
||||
denied(t, app.SetBucketPolicy(ctx, bucket, ""))
|
||||
if err = app.RemoveObject(ctx, bucket, key, minio.RemoveObjectOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Admin APIs have a separate JSON error type; reject exactly AccessDenied.
|
||||
endpoint, _ := url.Parse(os.Getenv("S3_ENDPOINT"))
|
||||
access, _ := os.ReadFile(os.Getenv("S3_ACCESS_KEY_ID_FILE"))
|
||||
secret, _ := os.ReadFile(os.Getenv("S3_SECRET_ACCESS_KEY_FILE"))
|
||||
admin, err := madmin.New(endpoint.Host, strings.TrimSpace(string(access)), strings.TrimSpace(string(secret)), endpoint.Scheme == "https")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = admin.ListUsers(ctx)
|
||||
if err == nil || madmin.ToErrorResponse(err).Code != "AccessDenied" {
|
||||
t.Fatalf("API credentials permit admin access or wrong error: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPasswordKDFUsesSaltAndFullUTF8Password(t *testing.T) {
|
||||
password := "длинный-пароль-с-эмодзи-🔒-0123456789"
|
||||
first, err := services.HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := services.HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.HasPrefix(first, []byte("$argon2id$v=19$m=65536,t=3,p=2$")) || bytes.Equal(first, second) {
|
||||
t.Fatal("password hashes must use Argon2id with independent salts")
|
||||
}
|
||||
valid, err := services.VerifyPassword(password, first)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
invalid, err := services.VerifyPassword(password+"другое", first)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !valid || invalid {
|
||||
t.Fatal("Argon2id verification lost the full UTF-8 password")
|
||||
}
|
||||
if _, err := services.VerifyPassword(password, bytes.Repeat([]byte{1}, 64)); err == nil {
|
||||
t.Fatal("legacy scrypt hash must not be accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoredPasswordUsesArgon2id(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
user, _ := createTestUser(t, fixture.users, "alice", "Алиса")
|
||||
var stored []byte
|
||||
if err := fixture.db.QueryRow(context.Background(), "SELECT password_hash FROM users WHERE id=$1", user.ID).Scan(&stored); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.HasPrefix(stored, []byte("$argon2id$")) {
|
||||
t.Fatalf("unexpected stored password hash format: %q", stored)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBearerRequiredIgnoresXUserID(t *testing.T) {
|
||||
handler := handlers.NewRouter(nil, nil, nil, nil, nil)
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil)
|
||||
request.Header.Set("X-User-Id", "11111111-1111-4111-8111-111111111111")
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, request)
|
||||
if response.Code != 401 || response.Header().Get("WWW-Authenticate") != `Bearer realm="messenger"` {
|
||||
t.Fatalf("unexpected response: %d %v", response.Code, response.Header())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageCursorPreservesPosition(t *testing.T) {
|
||||
original := messageCursor{ChatID: "11111111-1111-4111-8111-111111111111", CreatedAt: time.Date(2026, 9, 23, 10, 0, 0, 123000, time.UTC), MessageID: "22222222-2222-4222-8222-222222222222"}
|
||||
token, err := services.EncodeMessageCursor(original)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decoded, err := services.DecodeMessageCursor(token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decoded != original {
|
||||
t.Fatalf("cursor changed: %+v", decoded)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type testFixture struct {
|
||||
db *pgxpool.Pool
|
||||
users *repositories.UserStore
|
||||
chats *repositories.ChatStore
|
||||
messages *repositories.MessageStore
|
||||
sessionStore *repositories.SessionStore
|
||||
sessions *services.SessionService
|
||||
router http.Handler
|
||||
}
|
||||
|
||||
// Each test owns a new schema; public application tables are never modified.
|
||||
func newTestFixture(t *testing.T) *testFixture {
|
||||
t.Helper()
|
||||
url := os.Getenv("TEST_DATABASE_URL")
|
||||
redisAddress := os.Getenv("TEST_REDIS_ADDR")
|
||||
if url == "" || redisAddress == "" {
|
||||
t.Skip("integration tests: run make test-backend, or set TEST_DATABASE_URL and TEST_REDIS_ADDR")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
admin, err := pgxpool.New(ctx, url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(admin.Close)
|
||||
schema := "test_" + strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
identifier := pgx.Identifier{schema}.Sanitize()
|
||||
if _, err = admin.Exec(ctx, "CREATE SCHEMA "+identifier); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if _, err := admin.Exec(ctx, "DROP SCHEMA "+identifier+" CASCADE"); err != nil {
|
||||
t.Errorf("cleanup test schema: %v", err)
|
||||
}
|
||||
})
|
||||
cfg, err := pgxpool.ParseConfig(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.ConnConfig.RuntimeParams["search_path"] = schema
|
||||
cfg.ConnConfig.RuntimeParams["application_name"] = schema
|
||||
db, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(db.Close)
|
||||
ddl, err := os.ReadFile("../repositories/migrations/000001_initial.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000002_redis_sessions.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
redisClient := repositories.NewRedisClient(redisAddress)
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000003_message_idempotency.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000004_attachments.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000005_upload_attempts.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000006_direct_uploads.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000007_jobs.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000008_job_recovery.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000009_pipeline_versions.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = redisClient.Close() })
|
||||
if err := redisClient.Ping(ctx).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users := repositories.NewUserStore(db)
|
||||
chats := repositories.NewChatStore(db)
|
||||
messages := repositories.NewMessageStore(db)
|
||||
userService := services.NewUserService(users)
|
||||
sessionStore := repositories.NewSessionStore(redisClient, "test:"+schema+":session:")
|
||||
sessions := services.NewSessionService(userService, sessionStore, time.Hour)
|
||||
router := handlers.NewRouter(services.NewHealthService(repositories.NewHealthStore(db), sessionStore), userService, sessions, services.NewChatService(chats), services.NewMessageService(messages))
|
||||
return &testFixture{db: db, users: users, chats: chats, messages: messages, sessionStore: sessionStore, sessions: sessions, router: router}
|
||||
}
|
||||
|
||||
func createTestUser(t *testing.T, store *repositories.UserStore, username, name string) (repositories.User, bool) {
|
||||
t.Helper()
|
||||
user, err := services.NewUserService(store).Create(context.Background(), username, name, "lab2-test-password")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return user, true
|
||||
}
|
||||
|
||||
func createTestChat(t *testing.T, store *repositories.ChatStore, title string, members []string) repositories.Chat {
|
||||
t.Helper()
|
||||
chat, err := store.Create(context.Background(), title, members)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return chat
|
||||
}
|
||||
|
||||
func createTestMessage(t *testing.T, store *repositories.MessageStore, chatID, userID, text string) repositories.Message {
|
||||
t.Helper()
|
||||
message, err := store.Create(context.Background(), chatID, userID, text)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
func listTestMessages(t *testing.T, store *repositories.MessageStore, chatID string) []repositories.Message {
|
||||
t.Helper()
|
||||
items, _, err := store.ListPageByChat(context.Background(), chatID, nil, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func authorizeTestRequest(t *testing.T, fixture *testFixture, request *http.Request, userID string) {
|
||||
t.Helper()
|
||||
token, _, err := fixture.sessions.CreateForUser(request.Context(), userID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type User = repositories.User
|
||||
type Chat = repositories.Chat
|
||||
type messagePage = services.MessagePage
|
||||
type messageCursor = repositories.MessageCursor
|
||||
@@ -0,0 +1,208 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"image"
|
||||
"image/png"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type uploadFault struct {
|
||||
*repositories.ObjectStore
|
||||
afterPut func() error
|
||||
}
|
||||
|
||||
func (s uploadFault) Put(ctx context.Context, key string, reader io.Reader, size int64, mime string) error {
|
||||
if err := s.ObjectStore.Put(ctx, key, reader, size, mime); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.afterPut()
|
||||
}
|
||||
|
||||
func uploadObjects(t *testing.T) *repositories.ObjectStore {
|
||||
t.Helper()
|
||||
if os.Getenv("S3_ENDPOINT") == "" {
|
||||
t.Skip("S3 integration: run make test-backend")
|
||||
}
|
||||
store, err := repositories.NewObjectStoreFromEnvironment()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return store
|
||||
}
|
||||
func uploadPNG(t *testing.T) []byte {
|
||||
t.Helper()
|
||||
var buffer bytes.Buffer
|
||||
if err := png.Encode(&buffer, image.NewRGBA(image.Rect(0, 0, 8, 8))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return buffer.Bytes()
|
||||
}
|
||||
func uploadFixture(t *testing.T) (*testFixture, *repositories.AttachmentStore, *repositories.ObjectStore, string, string) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Upload recovery", []string{user.ID})
|
||||
return f, repositories.NewAttachmentStore(f.db), uploadObjects(t), user.ID, chat.ID
|
||||
}
|
||||
func ageAndClean(t *testing.T, f *testFixture, store *repositories.AttachmentStore, objects *repositories.ObjectStore) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
var key string
|
||||
if err := f.db.QueryRow(ctx, "SELECT object_key FROM upload_attempts").Scan(&key); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { objects.Delete(context.Background(), key) })
|
||||
if _, err := f.db.Exec(ctx, "UPDATE upload_attempts SET cleanup_after=clock_timestamp()-interval '1 second'"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := store.CleanupUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Deleted != 1 {
|
||||
t.Fatalf("cleanup: %+v %v", result, err)
|
||||
}
|
||||
if content, err := objects.Get(ctx, key); err == nil {
|
||||
content.Close()
|
||||
t.Fatal("orphan still exists")
|
||||
}
|
||||
var count int
|
||||
if err := f.db.QueryRow(ctx, "SELECT count(*) FROM upload_attempts").Scan(&count); err != nil || count != 0 {
|
||||
t.Fatalf("journal: %d %v", count, err)
|
||||
}
|
||||
if err := f.db.QueryRow(ctx, "SELECT count(*) FROM attachments").Scan(&count); err != nil || count != 0 {
|
||||
t.Fatalf("unexpected metadata: %d %v", count, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadRecoveryAfterPutFailure(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
service := services.NewAttachmentService(store, uploadFault{objects, func() error { return errors.New("injected PUT result loss") }})
|
||||
if _, err := service.Upload(context.Background(), user, chat, "photo.png", "image/png", bytes.NewReader(uploadPNG(t))); err == nil {
|
||||
t.Fatal("expected failure")
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
func TestUploadRecoveryMetadataRejection(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
if _, err := f.db.Exec(context.Background(), `ALTER TABLE attachments ADD CONSTRAINT reject_fixture CHECK(filename <> 'reject.png')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service := services.NewAttachmentService(store, objects)
|
||||
if _, err := service.Upload(context.Background(), user, chat, "reject.png", "image/png", bytes.NewReader(uploadPNG(t))); err == nil {
|
||||
t.Fatal("expected metadata rejection")
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
func TestUploadRecoveryDatabaseConnectionLoss(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
// Kill only this fixture's idle upload transaction, after its S3 PUT.
|
||||
fault := func() error {
|
||||
_, err := f.db.Exec(context.Background(), `SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE application_name=$1 AND state='idle in transaction' AND pid<>pg_backend_pid()`, f.db.Config().ConnConfig.RuntimeParams["application_name"])
|
||||
return err
|
||||
}
|
||||
service := services.NewAttachmentService(store, uploadFault{objects, fault})
|
||||
if _, err := service.Upload(context.Background(), user, chat, "photo.png", "image/png", bytes.NewReader(uploadPNG(t))); err == nil {
|
||||
t.Fatal("expected connection loss")
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
|
||||
func TestUploadRecoveryProcessExit(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
directory := t.TempDir()
|
||||
t.Setenv("UPLOAD_TMP_DIR", directory)
|
||||
child := exec.Command(os.Args[0], "-test.run=^TestUploadCrashHelper$")
|
||||
child.Env = append(os.Environ(), "UPLOAD_CRASH_HELPER=1", "UPLOAD_TEST_DATABASE="+f.db.Config().ConnString(), "UPLOAD_TEST_SCHEMA="+f.db.Config().ConnConfig.RuntimeParams["search_path"], "UPLOAD_TEST_USER="+user, "UPLOAD_TEST_CHAT="+chat)
|
||||
output, err := child.CombinedOutput()
|
||||
var exit *exec.ExitError
|
||||
if !errors.As(err, &exit) || exit.ExitCode() != 23 {
|
||||
t.Fatalf("helper did not exit after PUT: %v %s", err, output)
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
files, err := os.ReadDir(directory)
|
||||
if err != nil || len(files) != 0 {
|
||||
t.Fatalf("temporary files left after exit: %v %v", files, err)
|
||||
}
|
||||
}
|
||||
func TestUploadCrashHelper(t *testing.T) {
|
||||
if os.Getenv("UPLOAD_CRASH_HELPER") != "1" {
|
||||
return
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(os.Getenv("UPLOAD_TEST_DATABASE"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.ConnConfig.RuntimeParams["search_path"] = os.Getenv("UPLOAD_TEST_SCHEMA")
|
||||
cfg.ConnConfig.RuntimeParams["application_name"] = os.Getenv("UPLOAD_TEST_SCHEMA")
|
||||
db, err := pgxpool.NewWithConfig(context.Background(), cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
objects := uploadObjects(t)
|
||||
service := services.NewAttachmentService(repositories.NewAttachmentStore(db), uploadFault{objects, func() error { os.Exit(23); return nil }})
|
||||
_, err = service.Upload(context.Background(), os.Getenv("UPLOAD_TEST_USER"), os.Getenv("UPLOAD_TEST_CHAT"), "crash.png", "image/png", bytes.NewReader(uploadPNG(t)))
|
||||
t.Fatalf("helper failed before exit: %v", err)
|
||||
}
|
||||
|
||||
type interruptedReader struct{ first bool }
|
||||
|
||||
func (r *interruptedReader) Read(p []byte) (int, error) {
|
||||
if !r.first {
|
||||
r.first = true
|
||||
return copy(p, []byte("partial PNG")), nil
|
||||
}
|
||||
return 0, io.ErrUnexpectedEOF
|
||||
}
|
||||
func TestUploadRecoveryInterruptedBody(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
directory := t.TempDir()
|
||||
t.Setenv("UPLOAD_TMP_DIR", directory)
|
||||
service := services.NewAttachmentService(store, objects)
|
||||
if _, err := service.Upload(context.Background(), user, chat, "photo.png", "image/png", &interruptedReader{}); !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
t.Fatalf("body interruption: %v", err)
|
||||
}
|
||||
var count int
|
||||
f.db.QueryRow(context.Background(), "SELECT count(*) FROM upload_attempts").Scan(&count)
|
||||
if count != 0 {
|
||||
t.Fatal("reservation before complete body")
|
||||
}
|
||||
files, err := os.ReadDir(directory)
|
||||
if err != nil || len(files) != 0 {
|
||||
t.Fatalf("temporary paths left: %v %v", files, err)
|
||||
}
|
||||
}
|
||||
func TestUploadRecoveryWaitingCancellation(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
entered := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
service := services.NewAttachmentService(store, uploadFault{objects, func() error { close(entered); <-release; return errors.New("test stop") }})
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
service.Upload(context.Background(), user, chat, "first.png", "image/png", bytes.NewReader(uploadPNG(t)))
|
||||
}()
|
||||
select {
|
||||
case <-entered:
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("first upload did not reach PUT")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, err := service.Upload(ctx, user, chat, "second.png", "image/png", strings.NewReader("unused"))
|
||||
close(release)
|
||||
<-done
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("waiting upload ignored cancellation: %v", err)
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateUserInvalidJSON(t *testing.T) {
|
||||
store := repositories.NewUserStore(nil)
|
||||
handler := handlers.NewRouter(nil, services.NewUserService(store), nil, nil, nil)
|
||||
|
||||
request := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/users",
|
||||
strings.NewReader(`{"username":`),
|
||||
)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf(
|
||||
"expected status %d, got %d; body: %s",
|
||||
http.StatusBadRequest,
|
||||
response.Code,
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if response.Header().Get("Content-Type") != "application/json" {
|
||||
t.Fatalf(
|
||||
"expected Content-Type application/json, got %q",
|
||||
response.Header().Get("Content-Type"),
|
||||
)
|
||||
}
|
||||
|
||||
if response.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("expected non-empty X-Request-Id header")
|
||||
}
|
||||
|
||||
body := response.Body.String()
|
||||
|
||||
if !strings.Contains(body, `"code":"invalid_request"`) {
|
||||
t.Fatalf(
|
||||
"expected invalid_request error code, got body: %s",
|
||||
body,
|
||||
)
|
||||
}
|
||||
|
||||
if !strings.Contains(body, "request_id") {
|
||||
t.Fatalf(
|
||||
"expected request_id in error body, got body: %s",
|
||||
body,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateUserDuplicateUsername(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
handler := fixture.router
|
||||
|
||||
firstRequest := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/users",
|
||||
strings.NewReader(`{
|
||||
"username": "alice",
|
||||
"display_name": "Алиса", "password": "lab2-test-password"
|
||||
}`),
|
||||
)
|
||||
firstRequest.Header.Set("Content-Type", "application/json")
|
||||
|
||||
firstResponse := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(firstResponse, firstRequest)
|
||||
|
||||
if firstResponse.Code != http.StatusCreated {
|
||||
t.Fatalf(
|
||||
"expected first request status %d, got %d; body: %s",
|
||||
http.StatusCreated,
|
||||
firstResponse.Code,
|
||||
firstResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
secondRequest := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/users",
|
||||
strings.NewReader(`{
|
||||
"username": "alice",
|
||||
"display_name": "Другая Алиса", "password": "lab2-test-password"
|
||||
}`),
|
||||
)
|
||||
secondRequest.Header.Set("Content-Type", "application/json")
|
||||
|
||||
secondResponse := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(secondResponse, secondRequest)
|
||||
|
||||
if secondResponse.Code != http.StatusConflict {
|
||||
t.Fatalf(
|
||||
"expected duplicate username status %d, got %d; body: %s",
|
||||
http.StatusConflict,
|
||||
secondResponse.Code,
|
||||
secondResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if secondResponse.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("expected non-empty X-Request-Id header")
|
||||
}
|
||||
|
||||
if !strings.Contains(
|
||||
secondResponse.Body.String(),
|
||||
`"code":"conflict"`,
|
||||
) {
|
||||
t.Fatalf(
|
||||
"expected conflict error code, got body: %s",
|
||||
secondResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
var count int
|
||||
if err := fixture.db.QueryRow(context.Background(), "SELECT count(*) FROM users").Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("expected one user, got %d", count)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
func TestCaseVariantExtraFieldsReturn400WithoutSaving(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Chat", []string{user.ID})
|
||||
path := "/api/v1/chats/" + chat.ID + "/messages"
|
||||
key := uuid.NewString()
|
||||
send := func(path, body, key string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
r.Header.Set("Idempotency-Key", key)
|
||||
authorizeTestRequest(t, f, r, user.ID)
|
||||
w := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
for _, test := range []struct{ path, body string }{
|
||||
{path, `{"text":"first","TEXT":"second"}`},
|
||||
{path, `{"TEXT":"second","text":"first"}`},
|
||||
{path, `{"Text":"second"}`},
|
||||
{path, `{"text":"first","Attachment_IDs":[]}`},
|
||||
{"/api/v1/chats", fmt.Sprintf(`{"title":"Chat","TITLE":"other","member_ids":[%q]}`, user.ID)},
|
||||
{"/api/v1/chats", fmt.Sprintf(`{"title":"Chat","MEMBER_IDS":[%q]}`, user.ID)},
|
||||
{"/api/v1/users", `{"username":"new","USERNAME":"other","display_name":"New","password":"test-password"}`},
|
||||
{"/api/v1/users", `{"username":"new","display_name":"New","Password":"test-password"}`},
|
||||
} {
|
||||
w := send(test.path, test.body, key)
|
||||
if w.Code != 400 || !strings.Contains(w.Body.String(), `"code":"invalid_request"`) {
|
||||
t.Fatalf("%s: want invalid_request 400, got %d %s", test.path, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
if len(listTestMessages(t, f.messages, chat.ID)) != 0 {
|
||||
t.Fatal("rejected request saved a message")
|
||||
}
|
||||
w := send(path, `{"text":"first"}`, key)
|
||||
if w.Code != 201 {
|
||||
t.Fatalf("corrected request with same key: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if w = send(path, `{"text":"first","attachment_ids":[]}`, key); w.Code != 201 {
|
||||
t.Fatalf("valid replay: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
messages := listTestMessages(t, f.messages, chat.ID)
|
||||
if len(messages) != 1 || messages[0].Text != "first" {
|
||||
t.Fatal("wrong message or duplicate saved")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatRejectsCaseVariantDuplicateMembersBeforeSQL(t *testing.T) {
|
||||
const id = "abcdef01-abcd-4abc-8abc-abcdef012345"
|
||||
for _, members := range [][]string{{id, strings.ToUpper(id)}, {strings.ToUpper(id), id}} {
|
||||
_, err := services.NewChatService(nil).Create(context.Background(), id, "Chat", members)
|
||||
var invalid services.InvalidInput
|
||||
if !errors.As(err, &invalid) {
|
||||
t.Fatalf("want InvalidInput before SQL, got %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatNormalizesMemberUUIDsHTTP(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
for _, duplicate := range []bool{true, false} {
|
||||
members := fmt.Sprintf(`[%q]`, strings.ToUpper(user.ID))
|
||||
want := 201
|
||||
if duplicate {
|
||||
members = fmt.Sprintf(`[%q,%q]`, user.ID, strings.ToUpper(user.ID))
|
||||
want = 400
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodPost, "/api/v1/chats", strings.NewReader(`{"title":"Chat","member_ids":`+members+`}`))
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
authorizeTestRequest(t, f, r, user.ID)
|
||||
w := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
if w.Code != want {
|
||||
t.Fatalf("duplicate=%v: want %d, got %d %s", duplicate, want, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
var chats, members int
|
||||
if err := f.db.QueryRow(context.Background(), "SELECT count(*) FROM chats").Scan(&chats); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.db.QueryRow(context.Background(), "SELECT count(*) FROM chat_members").Scan(&members); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if chats != 1 || members != 1 {
|
||||
t.Fatalf("unexpected persisted chats=%d members=%d", chats, members)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package workers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"messenger/src/backend/internal/faults"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
func processImage(ctx context.Context, objects services.AttachmentObjects, a repositories.Attachment, staging string) ([]repositories.Variant, string, error) {
|
||||
return publishImage(ctx, objects, a, staging, func(ctx context.Context, data []byte) ([]encodedVariant, string, error) {
|
||||
return renderInSubprocess(ctx, a.ID, a.PipelineVersion, data)
|
||||
})
|
||||
}
|
||||
|
||||
func publishImage(ctx context.Context, objects services.AttachmentObjects, a repositories.Attachment, staging string, render imageRenderer) ([]repositories.Variant, string, error) {
|
||||
reader, err := objects.Get(ctx, staging)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(reader, services.MaxUploadBytes+1))
|
||||
reader.Close()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
digest := sha256.Sum256(data)
|
||||
if len(data) > int(services.MaxUploadBytes) || int64(len(data)) != a.SizeBytes || hex.EncodeToString(digest[:]) != a.SHA256 {
|
||||
return nil, "invalid_image", errors.New("invalid staging checksum or size")
|
||||
}
|
||||
variants, code, err := render(ctx, data)
|
||||
if err != nil {
|
||||
return nil, code, err
|
||||
}
|
||||
if err = objects.Put(ctx, a.ObjectKey, bytes.NewReader(data), a.SizeBytes, a.ContentType); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
metadata := []repositories.Variant{}
|
||||
for _, variant := range variants {
|
||||
key := repositories.VariantObjectKey(a.ID, a.PipelineVersion, variant.Metadata.Name)
|
||||
if err = objects.Put(ctx, key, bytes.NewReader(variant.Data), int64(len(variant.Data)), variant.Metadata.ContentType); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
metadata = append(metadata, variant.Metadata)
|
||||
}
|
||||
faults.Crash("after_object_before_commit", a.ID)
|
||||
return metadata, "", nil
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
package workers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/image/webp"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
type imageObjects map[string][]byte
|
||||
|
||||
func (o imageObjects) Get(_ context.Context, key string) (io.ReadCloser, error) {
|
||||
return io.NopCloser(bytes.NewReader(o[key])), nil
|
||||
}
|
||||
func (o imageObjects) Put(_ context.Context, key string, r io.Reader, _ int64, _ string) error {
|
||||
b, e := io.ReadAll(r)
|
||||
o[key] = b
|
||||
return e
|
||||
}
|
||||
func TestThumbnailContract(t *testing.T) {
|
||||
if _, err := exec.LookPath("cwebp"); err != nil {
|
||||
t.Skip("cwebp: use Docker builder tests")
|
||||
}
|
||||
for _, size := range [][4]int{{320, 200, 256, 160}, {200, 320, 160, 256}, {17, 9, 17, 9}, {1000, 1, 256, 1}} {
|
||||
t.Run(fmt.Sprintf("%dx%d", size[0], size[1]), func(t *testing.T) {
|
||||
var source bytes.Buffer
|
||||
if err := png.Encode(&source, image.NewNRGBA(image.Rect(0, 0, size[0], size[1]))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
digest := sha256.Sum256(source.Bytes())
|
||||
o := imageObjects{"staging": source.Bytes()}
|
||||
a := repositories.Attachment{ID: "sample", ObjectKey: "originals/sample", SizeBytes: int64(source.Len()), SHA256: hex.EncodeToString(digest[:]), ContentType: "image/png"}
|
||||
variants, code, err := publishImage(context.Background(), o, a, "staging", renderImage)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", code, err)
|
||||
}
|
||||
thumb, err := jpeg.Decode(bytes.NewReader(o["variants/sample/v1/thumbnail.jpg"]))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(variants) != 3 {
|
||||
t.Fatal("three variants required")
|
||||
}
|
||||
crop, err := jpeg.Decode(bytes.NewReader(o["variants/sample/v1/crop.jpg"]))
|
||||
if err != nil || crop.Bounds().Dx() != 128 || crop.Bounds().Dy() != 128 {
|
||||
t.Fatal("invalid crop", err)
|
||||
}
|
||||
optimized, err := webp.Decode(bytes.NewReader(o["variants/sample/v1/optimized.webp"]))
|
||||
if err != nil || optimized.Bounds().Dx() != size[0] || optimized.Bounds().Dy() != size[1] {
|
||||
t.Fatal("invalid optimized", err)
|
||||
}
|
||||
wantW, wantH := size[2], size[3]
|
||||
if thumb.Bounds().Dx() != wantW || thumb.Bounds().Dy() != wantH || variants[0].Width != wantW || variants[0].Height != wantH {
|
||||
t.Fatalf("wrong dimensions: %v", thumb.Bounds())
|
||||
}
|
||||
pixel := color.RGBAModel.Convert(thumb.At(0, 0)).(color.RGBA)
|
||||
if pixel.R < 250 || pixel.G < 250 || pixel.B < 250 {
|
||||
t.Fatal("alpha not composited on white")
|
||||
}
|
||||
if !bytes.Equal(o[a.ObjectKey], source.Bytes()) {
|
||||
t.Fatal("original changed")
|
||||
}
|
||||
first := append([]byte{}, o["variants/sample/v1/thumbnail.jpg"]...)
|
||||
if _, _, err = publishImage(context.Background(), o, a, "staging", renderImage); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(o) != 5 || !bytes.Equal(first, o["variants/sample/v1/thumbnail.jpg"]) {
|
||||
t.Fatal("redelivery changed or multiplied effects")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestThumbnailAppliesEXIFOrientation(t *testing.T) {
|
||||
if _, err := exec.LookPath("cwebp"); err != nil {
|
||||
t.Skip("cwebp: use Docker builder tests")
|
||||
}
|
||||
var source bytes.Buffer
|
||||
if err := jpeg.Encode(&source, image.NewRGBA(image.Rect(0, 0, 320, 200)), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// APP1 Exif, little-endian TIFF, one SHORT Orientation entry with value 6.
|
||||
exif := []byte{'E', 'x', 'i', 'f', 0, 0, 'I', 'I', 42, 0, 8, 0, 0, 0, 1, 0, 0x12, 1, 3, 0, 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0}
|
||||
data := append([]byte{}, source.Bytes()[:2]...)
|
||||
data = append(data, 0xff, 0xe1, 0, byte(len(exif)+2))
|
||||
data = append(data, exif...)
|
||||
data = append(data, source.Bytes()[2:]...)
|
||||
digest := sha256.Sum256(data)
|
||||
o := imageObjects{"staging": data}
|
||||
a := repositories.Attachment{ID: "rotated", ObjectKey: "originals/rotated", SizeBytes: int64(len(data)), SHA256: hex.EncodeToString(digest[:]), ContentType: "image/jpeg"}
|
||||
variants, _, err := publishImage(context.Background(), o, a, "staging", renderImage)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if variants[0].Width != 160 || variants[0].Height != 256 {
|
||||
t.Fatalf("orientation ignored: %+v", variants[0])
|
||||
}
|
||||
if !bytes.Equal(o[a.ObjectKey], data) {
|
||||
t.Fatal("original EXIF modified")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOptimizedBoundsAndCenteredCrop(t *testing.T) {
|
||||
if _, err := exec.LookPath("cwebp"); err != nil {
|
||||
t.Skip("cwebp: use Docker builder tests")
|
||||
}
|
||||
var original bytes.Buffer
|
||||
if err := png.Encode(&original, image.NewNRGBA(image.Rect(0, 0, 1600, 1000))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
variants, _, err := renderImage(context.Background(), original.Bytes())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
optimized, err := webp.Decode(bytes.NewReader(variants[2].Data))
|
||||
if err != nil || optimized.Bounds().Dx() != 1280 || optimized.Bounds().Dy() != 800 {
|
||||
t.Fatal("optimized must fit 1280x1280", err)
|
||||
}
|
||||
stripe := image.NewNRGBA(image.Rect(0, 0, 4, 2))
|
||||
for y := 0; y < 2; y++ {
|
||||
for x := 0; x < 4; x++ {
|
||||
c := color.NRGBA{G: 255, A: 255}
|
||||
if x == 0 {
|
||||
c = color.NRGBA{R: 255, A: 255}
|
||||
}
|
||||
if x == 3 {
|
||||
c = color.NRGBA{B: 255, A: 255}
|
||||
}
|
||||
stripe.SetNRGBA(x, y, c)
|
||||
}
|
||||
}
|
||||
original = bytes.Buffer{}
|
||||
if err = png.Encode(&original, stripe); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
variants, _, err = renderImage(context.Background(), original.Bytes())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
crop, err := jpeg.Decode(bytes.NewReader(variants[1].Data))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pixel := color.NRGBAModel.Convert(crop.At(64, 64)).(color.NRGBA)
|
||||
if pixel.G < 240 || pixel.R > 15 || pixel.B > 15 {
|
||||
t.Fatal("crop did not preserve the green center", pixel)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionedRenderingHasDistinctImmutableResults(t *testing.T) {
|
||||
if _, err := exec.LookPath("cwebp"); err != nil {
|
||||
t.Skip("cwebp: use Docker builder tests")
|
||||
}
|
||||
imageData := image.NewNRGBA(image.Rect(0, 0, 48, 32))
|
||||
for y := 0; y < 32; y++ {
|
||||
for x := 0; x < 48; x++ {
|
||||
imageData.SetNRGBA(x, y, color.NRGBA{R: uint8(x*17 + y), G: uint8(y*31 + x), B: uint8(x * y * 3), A: 255})
|
||||
}
|
||||
}
|
||||
var input bytes.Buffer
|
||||
if err := png.Encode(&input, imageData); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
original := append([]byte{}, input.Bytes()...)
|
||||
first, _, err := renderVersion(context.Background(), input.Bytes(), 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, _, err := renderVersion(context.Background(), input.Bytes(), 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(first) != 3 || len(second) != 3 {
|
||||
t.Fatal("incomplete version")
|
||||
}
|
||||
changed := false
|
||||
for i := range first {
|
||||
if first[i].Metadata.Width != second[i].Metadata.Width || first[i].Metadata.Height != second[i].Metadata.Height {
|
||||
t.Fatal("version changed geometry")
|
||||
}
|
||||
changed = changed || first[i].Metadata.SHA256 != second[i].Metadata.SHA256
|
||||
if repositories.VariantObjectKey("sample", 1, first[i].Metadata.Name) == repositories.VariantObjectKey("sample", 2, second[i].Metadata.Name) {
|
||||
t.Fatal("versions share an object key")
|
||||
}
|
||||
}
|
||||
if !changed || !bytes.Equal(original, input.Bytes()) {
|
||||
t.Fatal("versions indistinguishable or source mutated")
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user