Compare commits
22
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59c72a84ce | ||
|
|
43469ef08d | ||
|
|
35db442cd3 | ||
|
|
e37a46a027 | ||
|
|
50b1649965 | ||
|
|
8907bb49fe | ||
|
|
dc5d12348c | ||
|
|
eb9bfabed5 | ||
|
|
d752a61e52 | ||
|
|
81e299f09a | ||
|
|
421cfafec3 | ||
|
|
2303b12377 | ||
|
|
1a55a39425 | ||
|
|
e82d0ce254 | ||
|
|
161ddc0f7d | ||
|
|
c1e73bda81 | ||
|
|
508a8ab77e | ||
|
|
1e5f324579 | ||
|
|
1d4f4bd2cc | ||
|
|
d8f171b8cc | ||
|
|
8ad3941f6d | ||
|
|
d275ba3c14 |
No files matched your search
@@ -0,0 +1,17 @@
|
||||
.git
|
||||
.gitignore
|
||||
.dockerignore
|
||||
.env
|
||||
docker.env
|
||||
tmp/
|
||||
bin/
|
||||
data/
|
||||
node_modules
|
||||
web/node_modules
|
||||
internal/infrastructure/assets/dist
|
||||
.task/
|
||||
.claude/
|
||||
.idea
|
||||
.vscode
|
||||
*.db
|
||||
*.exe
|
||||
@@ -15,3 +15,25 @@ AUTH_RATE_LIMIT_MAX_ATTEMPTS=5
|
||||
AUTH_RATE_LIMIT_WINDOW_SECONDS=60
|
||||
TRUSTED_PROXIES=
|
||||
UPDATE_FEED_URL=https://git.floriansylvain.fr
|
||||
RENEWCMS_CONTAINER=false
|
||||
DB_DRIVER=sqlite
|
||||
DATA_DIR=
|
||||
DB_HOST=
|
||||
DB_PORT=
|
||||
DB_USER=
|
||||
DB_PASSWORD=
|
||||
DB_NAME=
|
||||
DB_TLS=false
|
||||
DB_SSLMODE=disable
|
||||
DB_MAX_OPEN_CONNS=10
|
||||
DB_MAX_IDLE_CONNS=5
|
||||
DB_CONN_MAX_LIFETIME_SECONDS=300
|
||||
STORAGE_DRIVER=local
|
||||
S3_BUCKET=
|
||||
S3_REGION=us-east-1
|
||||
S3_ENDPOINT=
|
||||
S3_ACCESS_KEY_ID=
|
||||
S3_SECRET_ACCESS_KEY=
|
||||
S3_KEY_PREFIX=
|
||||
S3_PATH_STYLE=false
|
||||
S3_TLS=true
|
||||
@@ -12,6 +12,7 @@ dist/
|
||||
|
||||
# Environment
|
||||
.env
|
||||
docker.env
|
||||
|
||||
# Generated files
|
||||
tmp/
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
FROM oven/bun:1-alpine AS web
|
||||
WORKDIR /src/web
|
||||
COPY web/package.json web/bun.lock ./
|
||||
RUN bun install --frozen-lockfile
|
||||
COPY web/ ./
|
||||
RUN bun run build-only
|
||||
|
||||
FROM golang:1.27.1-alpine AS server
|
||||
ARG VERSION=dev
|
||||
ENV CGO_ENABLED=0
|
||||
WORKDIR /src
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
COPY . .
|
||||
COPY --from=web /src/internal/infrastructure/assets/dist ./internal/infrastructure/assets/dist
|
||||
RUN go build -trimpath -ldflags "-s -w -X main.version=${VERSION}" -o /out/renewcms ./cmd/server
|
||||
|
||||
FROM alpine:3.22
|
||||
RUN addgroup -S -g 10001 renewcms \
|
||||
&& adduser -S -u 10001 -G renewcms -H -s /sbin/nologin renewcms \
|
||||
&& mkdir /data \
|
||||
&& chown renewcms:renewcms /data
|
||||
COPY --from=server /out/renewcms /usr/local/bin/renewcms
|
||||
ENV RENEWCMS_CONTAINER=true
|
||||
USER renewcms
|
||||
WORKDIR /data
|
||||
VOLUME /data
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 CMD ["wget", "-qO-", "http://127.0.0.1:8080/healthz"]
|
||||
ENTRYPOINT ["/usr/local/bin/renewcms"]
|
||||
@@ -15,6 +15,16 @@ cp .env.example .env # fill in PUBLIC_URL, JWT_SECRET and SMTP_*
|
||||
|
||||
Data is stored in `./data/` by default. For a real server (systemd, HTTPS, backups), see **[docs/deployment.md](docs/deployment.md)**.
|
||||
|
||||
### Run with Docker
|
||||
|
||||
```sh
|
||||
cp docker.env.example docker.env # fill in PUBLIC_URL, JWT_SECRET and SMTP_*
|
||||
docker compose pull
|
||||
docker compose up -d # http://localhost:8080
|
||||
```
|
||||
|
||||
Upgrade with `docker compose pull && docker compose up -d`. The image `git.floriansylvain.fr/fsylvain/renewcms` is published by the maintainer with `task docker:publish` (no CI). Tags: `vX.Y.Z`, `X.Y.Z`, `X.Y` and `latest` for a stable release; `vX.Y.Z-<pre>`, `X.Y.Z-<pre>` and the moving `beta` or `rc` for a pre-release (`latest` never moves for those, so the default is `beta` for now). To build from source use the override described in **[docs/docker.md](docs/docker.md)**.
|
||||
|
||||
### Updates from the browser
|
||||
|
||||
On Linux with systemd, install v2.6.0-beta or newer manually, then follow [the browser-update setup guide](docs/deployment.md#browser-updates). It covers finding the actual database directory, installing the helper, testing a restart and troubleshooting. Other platforms use manual upgrades.
|
||||
@@ -35,6 +45,7 @@ task dev # API with hot reload + Vite HMR → http://localhost:51
|
||||
|---|---|
|
||||
| [docs/development.md](docs/development.md) | Dev setup, how Vite and the API fit together, tests, lint, `task` commands |
|
||||
| [docs/deployment.md](docs/deployment.md) | Building release binaries, running as a service, HTTPS, backups, upgrades |
|
||||
| [docs/docker.md](docs/docker.md) | Running RenewCMS in Docker: image, Compose, data volume, backups, reverse proxy, tags, upgrades, publishing |
|
||||
| [docs/configuration.md](docs/configuration.md) | Every environment variable |
|
||||
| [docs/architecture.md](docs/architecture.md) | Backend layering and how the SPA is embedded |
|
||||
|
||||
|
||||
+249
-2
@@ -10,6 +10,7 @@ vars:
|
||||
|
||||
env:
|
||||
CGO_ENABLED: '0'
|
||||
__COMPAT_LAYER: RunAsInvoker
|
||||
|
||||
tasks:
|
||||
default:
|
||||
@@ -46,6 +47,127 @@ tasks:
|
||||
cmds:
|
||||
- go test -tags nofrontend ./test/... -count=1 {{.CLI_ARGS}}
|
||||
|
||||
test:mysql:
|
||||
desc: Run the MySQL tests against real servers (mysql:8.4 then mariadb:11 in Docker, removed afterwards)
|
||||
cmds:
|
||||
- cmd: |
|
||||
PASSWORD=renewcms-test-root
|
||||
STATUS=0
|
||||
run_engine() {
|
||||
NAME="renewcms-test-$1"
|
||||
IMAGE="$2"
|
||||
docker rm -f "$NAME" >/dev/null 2>&1
|
||||
docker run -d --name "$NAME" -e MYSQL_ROOT_PASSWORD="$PASSWORD" -p 127.0.0.1::3306 "$IMAGE" >/dev/null || return 1
|
||||
MAPPING="$(docker port "$NAME" 3306/tcp)"
|
||||
PORT="${MAPPING##*:}"
|
||||
READY=no
|
||||
TRIES=0
|
||||
while [ "$TRIES" -lt 90 ]; do
|
||||
if docker exec "$NAME" sh -c "\$(command -v mariadb || command -v mysql) -h127.0.0.1 -uroot -p$PASSWORD -e 'SELECT 1'" >/dev/null 2>&1; then
|
||||
READY=yes
|
||||
break
|
||||
fi
|
||||
TRIES=$((TRIES + 1))
|
||||
sleep 2
|
||||
done
|
||||
if [ "$READY" != yes ]; then
|
||||
echo "$IMAGE did not become ready" >&2
|
||||
docker logs "$NAME" 2>&1 | tail -n 20 >&2
|
||||
docker rm -f "$NAME" >/dev/null 2>&1
|
||||
return 1
|
||||
fi
|
||||
echo "== $IMAGE on 127.0.0.1:$PORT"
|
||||
RENEWCMS_TEST_MYSQL_DSN="root:$PASSWORD@tcp(127.0.0.1:$PORT)/mysql" go test -tags nofrontend ./test/... -run MySQL -count=1 {{.CLI_ARGS}}
|
||||
RESULT=$?
|
||||
docker rm -f "$NAME" >/dev/null 2>&1
|
||||
return $RESULT
|
||||
}
|
||||
run_engine mysql mysql:8.4 || STATUS=1
|
||||
run_engine mariadb mariadb:11 || STATUS=1
|
||||
exit $STATUS
|
||||
silent: true
|
||||
|
||||
test:postgres:
|
||||
desc: Run the PostgreSQL tests against real servers (postgres:16 then postgres:17 in Docker, removed afterwards)
|
||||
cmds:
|
||||
- cmd: |
|
||||
PASSWORD=renewcms-test-postgres
|
||||
STATUS=0
|
||||
run_engine() {
|
||||
NAME="renewcms-test-postgres-$1"
|
||||
IMAGE="postgres:$1"
|
||||
docker rm -f "$NAME" >/dev/null 2>&1
|
||||
docker run -d --name "$NAME" -e POSTGRES_PASSWORD="$PASSWORD" -p 127.0.0.1::5432 "$IMAGE" >/dev/null || return 1
|
||||
MAPPING="$(docker port "$NAME" 5432/tcp | head -n 1)"
|
||||
PORT="${MAPPING##*:}"
|
||||
READY=no
|
||||
TRIES=0
|
||||
while [ "$TRIES" -lt 90 ]; do
|
||||
if docker exec "$NAME" pg_isready -h 127.0.0.1 -U postgres >/dev/null 2>&1 && docker exec "$NAME" psql -h 127.0.0.1 -U postgres -c 'SELECT 1' >/dev/null 2>&1; then
|
||||
READY=yes
|
||||
break
|
||||
fi
|
||||
TRIES=$((TRIES + 1))
|
||||
sleep 2
|
||||
done
|
||||
if [ "$READY" != yes ]; then
|
||||
echo "$IMAGE did not become ready" >&2
|
||||
docker logs "$NAME" 2>&1 | tail -n 20 >&2
|
||||
docker rm -f "$NAME" >/dev/null 2>&1
|
||||
return 1
|
||||
fi
|
||||
echo "== $IMAGE on 127.0.0.1:$PORT"
|
||||
RENEWCMS_TEST_POSTGRES_DSN="postgres://postgres:$PASSWORD@127.0.0.1:$PORT/postgres?sslmode=disable" go test -tags nofrontend ./test/... -run Postgres -count=1 {{.CLI_ARGS}}
|
||||
RESULT=$?
|
||||
docker rm -f "$NAME" >/dev/null 2>&1
|
||||
return $RESULT
|
||||
}
|
||||
run_engine 16 || STATUS=1
|
||||
run_engine 17 || STATUS=1
|
||||
exit $STATUS
|
||||
silent: true
|
||||
|
||||
test:s3:
|
||||
desc: Run the S3 storage integration test against a throwaway SeaweedFS in Docker (skipped when Docker is unavailable)
|
||||
cmds:
|
||||
- cmd: |
|
||||
if ! docker info >/dev/null 2>&1; then
|
||||
echo "Docker is not available, skipping the S3 integration test"
|
||||
exit 0
|
||||
fi
|
||||
export MSYS_NO_PATHCONV=1
|
||||
NAME=renewcms-test-seaweedfs
|
||||
IMAGE=chrislusf/seaweedfs:4.48
|
||||
BUCKET=renewcms-test
|
||||
ACCESS_KEY=renewcms-test
|
||||
SECRET_KEY=renewcms-test-secret
|
||||
docker rm -f "$NAME" >/dev/null 2>&1
|
||||
docker run -d --name "$NAME" -e AWS_ACCESS_KEY_ID="$ACCESS_KEY" -e AWS_SECRET_ACCESS_KEY="$SECRET_KEY" -p 127.0.0.1::8333 "$IMAGE" server -dir=/data -s3 -s3.port=8333 >/dev/null || exit 1
|
||||
MAPPING="$(docker port "$NAME" 8333/tcp | head -n 1)"
|
||||
PORT="${MAPPING##*:}"
|
||||
READY=no
|
||||
TRIES=0
|
||||
while [ "$TRIES" -lt 60 ]; do
|
||||
if docker run --rm --network "container:$NAME" --entrypoint sh "$IMAGE" -c "echo 's3.bucket.create -name $BUCKET' | timeout 30 weed shell -master=127.0.0.1:9333 && echo s3.bucket.list | timeout 30 weed shell -master=127.0.0.1:9333 | grep -q $BUCKET" >/dev/null 2>&1; then
|
||||
READY=yes
|
||||
break
|
||||
fi
|
||||
TRIES=$((TRIES + 1))
|
||||
sleep 2
|
||||
done
|
||||
if [ "$READY" != yes ]; then
|
||||
echo "SeaweedFS did not become ready" >&2
|
||||
docker logs "$NAME" 2>&1 | tail -n 20 >&2
|
||||
docker rm -f "$NAME" >/dev/null 2>&1
|
||||
exit 1
|
||||
fi
|
||||
echo "== SeaweedFS on 127.0.0.1:$PORT"
|
||||
RENEWCMS_TEST_S3_ENDPOINT="http://127.0.0.1:$PORT" RENEWCMS_TEST_S3_BUCKET="$BUCKET" RENEWCMS_TEST_S3_ACCESS_KEY="$ACCESS_KEY" RENEWCMS_TEST_S3_SECRET_KEY="$SECRET_KEY" go test -tags nofrontend ./test/... -run S3StorageIntegration -count=1 -v {{.CLI_ARGS}}
|
||||
RESULT=$?
|
||||
docker rm -f "$NAME" >/dev/null 2>&1
|
||||
exit $RESULT
|
||||
silent: true
|
||||
|
||||
cover:
|
||||
desc: Go test coverage of the use cases → tmp/coverage.out
|
||||
cmds:
|
||||
@@ -56,8 +178,7 @@ tasks:
|
||||
mutate:
|
||||
desc: Gremlins mutation testing of the use cases → tmp/gremlins.json
|
||||
cmds:
|
||||
- mkdir -p tmp
|
||||
- gremlins unleash --integration --tags nofrontend --coverpkg=./internal/infrastructure/useCases/...,./api/middleware/...,./api/pkg/claims/... --timeout-coefficient 10 --output tmp/gremlins.json ./internal/infrastructure/useCases ./api/middleware ./api/pkg/claims
|
||||
- bash scripts/mutate.sh
|
||||
|
||||
test:web:
|
||||
desc: Run the SPA test suite (Vitest) in run mode
|
||||
@@ -170,6 +291,132 @@ tasks:
|
||||
cmds:
|
||||
- ./bin/{{.BINARY}}{{exeExt}}
|
||||
|
||||
docker:build:
|
||||
desc: Build the Docker image tagged with the git describe version and latest
|
||||
cmds:
|
||||
- docker build --build-arg VERSION={{.VERSION}} -t renewcms:{{.VERSION}} -t renewcms:latest .
|
||||
|
||||
docker:run:
|
||||
desc: Run the image once on a named volume, reading docker.env, on http://localhost:8080
|
||||
cmds:
|
||||
- docker run --rm --name renewcms -p 8080:8080 --env-file docker.env -e DB_FILE=/data/renewcms.db -e UPLOAD_DIR=/data/uploads -v renewcms-data:/data --stop-timeout 15 renewcms:latest
|
||||
|
||||
docker:up:
|
||||
desc: Build from the local sources and start the Compose service in the background
|
||||
env:
|
||||
VERSION: '{{.VERSION}}'
|
||||
cmds:
|
||||
- docker compose -f docker-compose.yml -f docker-compose.build.yml up -d --build
|
||||
|
||||
docker:pull:
|
||||
desc: Pull the published image (RENEWCMS_IMAGE, default the beta tag)
|
||||
cmds:
|
||||
- docker compose pull
|
||||
|
||||
docker:upgrade:
|
||||
desc: Pull the published image and recreate the Compose service on it
|
||||
cmds:
|
||||
- docker compose pull
|
||||
- docker compose up -d
|
||||
|
||||
docker:down:
|
||||
desc: Stop and remove the Compose service (the data volume is kept)
|
||||
cmds:
|
||||
- docker compose down
|
||||
|
||||
docker:logs:
|
||||
desc: Follow the Compose service logs
|
||||
cmds:
|
||||
- docker compose logs -f renewcms
|
||||
|
||||
docker:publish:
|
||||
desc: 'Build and push the multi-arch image to the registry. Usage, task docker:publish [-- --dry-run]'
|
||||
vars:
|
||||
REGISTRY_IMAGE:
|
||||
sh: echo "${REGISTRY_IMAGE-git.floriansylvain.fr/fsylvain/renewcms}"
|
||||
cmds:
|
||||
- cmd: |
|
||||
IMAGE='{{.REGISTRY_IMAGE}}'
|
||||
V='{{.VERSION}}'
|
||||
ARGS='{{.CLI_ARGS}}'
|
||||
fail() { echo "docker:publish refused: $1" >&2; exit 1; }
|
||||
DRY=no
|
||||
case "$ARGS" in
|
||||
"") ;;
|
||||
"--dry-run") DRY=yes ;;
|
||||
*) fail "unknown argument '$ARGS' (only --dry-run is accepted)" ;;
|
||||
esac
|
||||
[ -n "$IMAGE" ] || fail "REGISTRY_IMAGE is empty"
|
||||
case "$V" in
|
||||
v*) ;;
|
||||
*) fail "VERSION '$V' must be an exact tag vX.Y.Z or vX.Y.Z-<pre>" ;;
|
||||
esac
|
||||
R="${V#v}"
|
||||
CORE="${R%%-*}"
|
||||
PRE=""
|
||||
[ "$CORE" = "$R" ] || PRE="${R#*-}"
|
||||
MAJOR="${CORE%%.*}"
|
||||
REST="${CORE#*.}"
|
||||
MINOR="${REST%%.*}"
|
||||
PATCH="${REST#*.}"
|
||||
for N in "$MAJOR" "$MINOR" "$PATCH"; do
|
||||
case "$N" in
|
||||
""|*[!0-9]*) fail "VERSION '$V' must be an exact tag vX.Y.Z or vX.Y.Z-<pre>" ;;
|
||||
esac
|
||||
done
|
||||
[ "$CORE" = "$MAJOR.$MINOR.$PATCH" ] || fail "VERSION '$V' must be an exact tag vX.Y.Z or vX.Y.Z-<pre>"
|
||||
if [ "$R" != "$CORE" ]; then
|
||||
case "$PRE" in
|
||||
""|*[!0-9A-Za-z.-]*) fail "VERSION '$V' has an invalid pre-release part" ;;
|
||||
esac
|
||||
PRENAME="${PRE%%[!A-Za-z]*}"
|
||||
[ -n "$PRENAME" ] || fail "VERSION '$V' pre-release must start with letters, for example -beta or -rc.1"
|
||||
fi
|
||||
if [ "$DRY" = no ]; then
|
||||
[ -z "$(git status --porcelain)" ] || fail "the work tree has uncommitted changes"
|
||||
[ "$(git describe --tags --exact-match 2>/dev/null)" = "$V" ] || fail "VERSION '$V' is not an exact git tag on HEAD (an override is accepted only with --dry-run)"
|
||||
fi
|
||||
TAGS="$V $R"
|
||||
if [ -n "$PRE" ]; then
|
||||
TAGS="$TAGS $PRENAME"
|
||||
else
|
||||
TAGS="$TAGS $MAJOR.$MINOR latest"
|
||||
fi
|
||||
ARGS_TAGS=""
|
||||
LIST=""
|
||||
for T in $TAGS; do
|
||||
ARGS_TAGS="$ARGS_TAGS -t $IMAGE:$T"
|
||||
LIST="$LIST, $T"
|
||||
done
|
||||
echo "image: $IMAGE"
|
||||
echo "tags: ${LIST#, }"
|
||||
if [ "$DRY" = yes ]; then
|
||||
echo "dry run: building for the host platform only, loading locally, pushing nothing"
|
||||
docker buildx build --load --build-arg VERSION="$V" $ARGS_TAGS .
|
||||
exit 0
|
||||
fi
|
||||
BUILDER="$(docker buildx inspect 2>&1)" || fail "docker buildx is not available or no builder is selected (see docs/docker.md, Maintainer: publishing)"
|
||||
STORE="$(docker info 2>&1)"
|
||||
case "$BUILDER" in
|
||||
*linux/amd64*) ;;
|
||||
*) fail "the selected buildx builder does not support linux/amd64 (see docs/docker.md, Maintainer: publishing)" ;;
|
||||
esac
|
||||
case "$BUILDER" in
|
||||
*linux/arm64*) ;;
|
||||
*) fail "the selected buildx builder does not support linux/arm64: enable QEMU/binfmt (see docs/docker.md, Maintainer: publishing)" ;;
|
||||
esac
|
||||
case "$BUILDER" in
|
||||
*"Driver:"*"docker-container"*|*"Driver:"*"kubernetes"*|*"Driver:"*"remote"*) ;;
|
||||
*)
|
||||
case "$STORE" in
|
||||
*io.containerd.snapshotter*) ;;
|
||||
*) fail "the selected builder uses the plain docker driver without the containerd image store, which cannot build multi-platform images: run 'docker buildx create --use' (see docs/docker.md, Maintainer: publishing)" ;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
docker buildx build --platform linux/amd64,linux/arm64 --build-arg VERSION="$V" $ARGS_TAGS --push .
|
||||
silent: true
|
||||
|
||||
clean:
|
||||
desc: Remove build output, the built SPA and tmp/
|
||||
cmds:
|
||||
|
||||
+241
-120
@@ -3,21 +3,26 @@ package api
|
||||
import (
|
||||
domainAPIKey "RenewCMS/internal/domain/apikey"
|
||||
domainArticle "RenewCMS/internal/domain/article"
|
||||
domainAudit "RenewCMS/internal/domain/audit"
|
||||
domainImage "RenewCMS/internal/domain/image"
|
||||
domainInvite "RenewCMS/internal/domain/invite"
|
||||
domainMail "RenewCMS/internal/domain/mail"
|
||||
domainPurge "RenewCMS/internal/domain/purge"
|
||||
domainUpdate "RenewCMS/internal/domain/update"
|
||||
domainUser "RenewCMS/internal/domain/user"
|
||||
"RenewCMS/internal/infrastructure/auditing"
|
||||
"RenewCMS/internal/infrastructure/config"
|
||||
"RenewCMS/internal/infrastructure/hashing"
|
||||
"RenewCMS/internal/infrastructure/mailer"
|
||||
"RenewCMS/internal/infrastructure/markdown"
|
||||
"RenewCMS/internal/infrastructure/persistence/models"
|
||||
"RenewCMS/internal/infrastructure/persistence/database"
|
||||
persistence "RenewCMS/internal/infrastructure/persistence/repositories"
|
||||
"RenewCMS/internal/infrastructure/scheduler"
|
||||
"RenewCMS/internal/infrastructure/storage"
|
||||
"RenewCMS/internal/infrastructure/update"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"context"
|
||||
"crypto/ed25519"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -26,52 +31,77 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UseCases struct {
|
||||
apiKeyAuthOnce sync.Once
|
||||
apiKeyAuthMiddleware func(http.Handler) http.Handler
|
||||
Token *jwtauth.JWTAuth
|
||||
CreateArticleUseCase *useCases.CreateArticleUseCase
|
||||
GetArticleUseCase *useCases.GetArticleUseCase
|
||||
ListArticlesUseCase *useCases.ListArticlesUseCase
|
||||
UpdateArticleUseCase *useCases.UpdateArticleUseCase
|
||||
DeleteArticleUseCase *useCases.DeleteArticleUseCase
|
||||
GetUserUseCase *useCases.GetUserUseCase
|
||||
CreateUserUseCase *useCases.CreateUserUseCase
|
||||
AuthenticateUserUseCase *useCases.AuthenticateUserUseCase
|
||||
VerifyUserUseCase *useCases.VerifyUserUseCase
|
||||
CancelRegistrationUseCase *useCases.CancelRegistrationUseCase
|
||||
RequestPasswordResetUseCase *useCases.RequestPasswordResetUseCase
|
||||
ResetPasswordUseCase *useCases.ResetPasswordUseCase
|
||||
UpdateUserProfileUseCase *useCases.UpdateUserProfileUseCase
|
||||
ListUsersUseCase *useCases.ListUsersUseCase
|
||||
CreateInviteUseCase *useCases.CreateInviteUseCase
|
||||
CancelInviteUseCase *useCases.CancelInviteUseCase
|
||||
RenewInviteUseCase *useCases.RenewInviteUseCase
|
||||
ListUsersAndInvitesUseCase *useCases.ListUsersAndInvitesUseCase
|
||||
GetInviteUseCase *useCases.GetInviteUseCase
|
||||
AcceptInviteUseCase *useCases.AcceptInviteUseCase
|
||||
SendMailUseCase *useCases.SendMailUseCase
|
||||
CreateImageUseCase *useCases.CreateImageUseCase
|
||||
ListMediaUseCase *useCases.ListMediaUseCase
|
||||
RenameMediaUseCase *useCases.RenameMediaUseCase
|
||||
DeleteMediaUseCase *useCases.DeleteMediaUseCase
|
||||
MergeMediaUseCase *useCases.MergeMediaUseCase
|
||||
GetMediaUsageUseCase *useCases.GetMediaUsageUseCase
|
||||
GetSettingsUseCase *useCases.GetSettingsUseCase
|
||||
GetUpdateStatusUseCase *useCases.GetUpdateStatusUseCase
|
||||
CheckForUpdateUseCase *useCases.CheckForUpdateUseCase
|
||||
StartUpdateDownloadUseCase *useCases.StartUpdateDownloadUseCase
|
||||
RequestRestartUseCase *useCases.RequestRestartUseCase
|
||||
RecordRunningInfoUseCase *useCases.RecordRunningInfoUseCase
|
||||
CreateAPIKeyUseCase *useCases.CreateAPIKeyUseCase
|
||||
ListAPIKeysUseCase *useCases.ListAPIKeysUseCase
|
||||
RevokeAPIKeyUseCase *useCases.RevokeAPIKeyUseCase
|
||||
AuthenticateAPIKeyUseCase *useCases.AuthenticateAPIKeyUseCase
|
||||
apiKeyAuthOnce sync.Once
|
||||
apiKeyAuthMiddleware func(http.Handler) http.Handler
|
||||
Token *jwtauth.JWTAuth
|
||||
PurgeScheduler *scheduler.Runner
|
||||
ActivityCleanupScheduler *scheduler.Runner
|
||||
CreateArticleUseCase *useCases.CreateArticleUseCase
|
||||
GetArticleUseCase *useCases.GetArticleUseCase
|
||||
ListArticlesUseCase *useCases.ListArticlesUseCase
|
||||
UpdateArticleUseCase *useCases.UpdateArticleUseCase
|
||||
DeleteArticleUseCase *useCases.DeleteArticleUseCase
|
||||
GetUserUseCase *useCases.GetUserUseCase
|
||||
CreateUserUseCase *useCases.CreateUserUseCase
|
||||
AuthenticateUserUseCase *useCases.AuthenticateUserUseCase
|
||||
VerifyUserUseCase *useCases.VerifyUserUseCase
|
||||
CancelRegistrationUseCase *useCases.CancelRegistrationUseCase
|
||||
RequestPasswordResetUseCase *useCases.RequestPasswordResetUseCase
|
||||
ResetPasswordUseCase *useCases.ResetPasswordUseCase
|
||||
UpdateUserProfileUseCase *useCases.UpdateUserProfileUseCase
|
||||
ListUsersUseCase *useCases.ListUsersUseCase
|
||||
CreateInviteUseCase *useCases.CreateInviteUseCase
|
||||
CancelInviteUseCase *useCases.CancelInviteUseCase
|
||||
RenewInviteUseCase *useCases.RenewInviteUseCase
|
||||
ListUsersAndInvitesUseCase *useCases.ListUsersAndInvitesUseCase
|
||||
GetInviteUseCase *useCases.GetInviteUseCase
|
||||
AcceptInviteUseCase *useCases.AcceptInviteUseCase
|
||||
SendMailUseCase *useCases.SendMailUseCase
|
||||
CreateImageUseCase *useCases.CreateImageUseCase
|
||||
OpenMediaUseCase *useCases.OpenMediaUseCase
|
||||
ListMediaUseCase *useCases.ListMediaUseCase
|
||||
RenameMediaUseCase *useCases.RenameMediaUseCase
|
||||
DeleteMediaUseCase *useCases.DeleteMediaUseCase
|
||||
MergeMediaUseCase *useCases.MergeMediaUseCase
|
||||
GetMediaUsageUseCase *useCases.GetMediaUsageUseCase
|
||||
GetPurgeSettingsUseCase *useCases.GetPurgeSettingsUseCase
|
||||
UpdatePurgeSettingsUseCase *useCases.UpdatePurgeSettingsUseCase
|
||||
PreviewPurgeUseCase *useCases.PreviewPurgeUseCase
|
||||
RunPurgeUseCase *useCases.RunPurgeUseCase
|
||||
GetSettingsUseCase *useCases.GetSettingsUseCase
|
||||
GetUpdateStatusUseCase *useCases.GetUpdateStatusUseCase
|
||||
CheckForUpdateUseCase *useCases.CheckForUpdateUseCase
|
||||
StartUpdateDownloadUseCase *useCases.StartUpdateDownloadUseCase
|
||||
RequestRestartUseCase *useCases.RequestRestartUseCase
|
||||
RecordRunningInfoUseCase *useCases.RecordRunningInfoUseCase
|
||||
CreateAPIKeyUseCase *useCases.CreateAPIKeyUseCase
|
||||
ListAPIKeysUseCase *useCases.ListAPIKeysUseCase
|
||||
RevokeAPIKeyUseCase *useCases.RevokeAPIKeyUseCase
|
||||
AuthenticateAPIKeyUseCase *useCases.AuthenticateAPIKeyUseCase
|
||||
ListActivityUseCase *useCases.ListActivityUseCase
|
||||
GetActivitySummaryUseCase *useCases.GetActivitySummaryUseCase
|
||||
ExportActivityUseCase *useCases.ExportActivityUseCase
|
||||
GetActivityRetentionUseCase *useCases.GetActivityRetentionUseCase
|
||||
UpdateActivityRetentionUseCase *useCases.UpdateActivityRetentionUseCase
|
||||
}
|
||||
|
||||
func (c *UseCases) RunSchedulers(ctx context.Context) {
|
||||
var running sync.WaitGroup
|
||||
for _, runner := range []*scheduler.Runner{c.PurgeScheduler, c.ActivityCleanupScheduler} {
|
||||
if runner == nil {
|
||||
continue
|
||||
}
|
||||
running.Add(1)
|
||||
go func() {
|
||||
defer running.Done()
|
||||
runner.Run(ctx)
|
||||
}()
|
||||
}
|
||||
running.Wait()
|
||||
}
|
||||
|
||||
type Repositories struct {
|
||||
@@ -80,6 +110,9 @@ type Repositories struct {
|
||||
ImageRepo domainImage.Repository
|
||||
InviteRepo domainInvite.Repository
|
||||
APIKeyRepo domainAPIKey.Repository
|
||||
PurgeSettingsRepo domainPurge.Repository
|
||||
AuditRepo domainAudit.Repository
|
||||
AuditSettingsRepo domainAudit.SettingsRepository
|
||||
MailRepo domainMail.Repository
|
||||
ReleaseFeed domainUpdate.ReleaseFeed
|
||||
UpdateStore domainUpdate.StatusStore
|
||||
@@ -95,7 +128,6 @@ type Repositories struct {
|
||||
}
|
||||
|
||||
const (
|
||||
sqliteBusyTimeoutMs = 5000
|
||||
updateStatusFileName = "update-status.json"
|
||||
updateJobFileName = "update-job.json"
|
||||
updateHelperFileName = "update-helper.json"
|
||||
@@ -103,32 +135,94 @@ const (
|
||||
backupsDirName = "backups"
|
||||
)
|
||||
|
||||
func SQLiteDSN(dbName string) string {
|
||||
return fmt.Sprintf("%s?_pragma=busy_timeout(%d)", dbName, sqliteBusyTimeoutMs)
|
||||
func imageStorageConfig(settings config.Settings) storage.Config {
|
||||
if settings.Storage.Driver == config.StorageDriverS3 {
|
||||
s3 := settings.Storage.S3
|
||||
return storage.Config{
|
||||
Driver: storage.DriverS3,
|
||||
S3: storage.S3Config{
|
||||
Bucket: s3.Bucket,
|
||||
Region: s3.Region,
|
||||
Endpoint: s3.Endpoint,
|
||||
AccessKeyID: s3.AccessKeyID,
|
||||
SecretAccessKey: s3.SecretAccessKey,
|
||||
KeyPrefix: s3.KeyPrefix,
|
||||
PathStyle: s3.PathStyle,
|
||||
TLS: s3.TLS,
|
||||
},
|
||||
}
|
||||
}
|
||||
return storage.Config{Driver: storage.DriverLocal, Location: settings.UploadDir}
|
||||
}
|
||||
|
||||
func getDb(dbName string, uploadDir string) *gorm.DB {
|
||||
if err := os.MkdirAll(filepath.Dir(dbName), os.ModePerm); err != nil {
|
||||
panic("Unable to create necessary subdirectories: " + err.Error())
|
||||
func openImageStorage(settings config.Settings) domainImage.Storage {
|
||||
imageStorage, err := storage.Open(imageStorageConfig(settings))
|
||||
if err != nil {
|
||||
panic("Unable to open the image storage: " + err.Error())
|
||||
}
|
||||
db, err := gorm.Open(sqlite.Open(SQLiteDSN(dbName)), &gorm.Config{})
|
||||
return imageStorage
|
||||
}
|
||||
|
||||
func databaseConfig(settings config.Settings) database.Config {
|
||||
switch settings.Database.Driver {
|
||||
case config.DatabaseDriverMySQL:
|
||||
return mysqlDatabaseConfig(settings.Database)
|
||||
case config.DatabaseDriverPostgres:
|
||||
return postgresDatabaseConfig(settings.Database)
|
||||
case "", config.DatabaseDriverSQLite:
|
||||
return database.Config{Driver: database.DriverSQLite, Source: settings.DBFile}
|
||||
default:
|
||||
return database.Config{Driver: settings.Database.Driver}
|
||||
}
|
||||
}
|
||||
|
||||
func mysqlDatabaseConfig(settings config.DatabaseSettings) database.Config {
|
||||
return database.Config{
|
||||
Driver: database.DriverMySQL,
|
||||
Source: database.MySQLDSN(database.MySQLConfig{
|
||||
Host: settings.Host,
|
||||
Port: settings.Port,
|
||||
User: settings.User,
|
||||
Password: settings.Password,
|
||||
Name: settings.Name,
|
||||
TLS: settings.TLS,
|
||||
}),
|
||||
Pool: &database.PoolSettings{
|
||||
MaxOpenConns: settings.MaxOpenConns,
|
||||
MaxIdleConns: settings.MaxIdleConns,
|
||||
ConnMaxLifetime: settings.ConnMaxLifetime,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func postgresDatabaseConfig(settings config.DatabaseSettings) database.Config {
|
||||
return database.Config{
|
||||
Driver: database.DriverPostgres,
|
||||
Source: database.PostgresDSN(database.PostgresConfig{
|
||||
Host: settings.Host,
|
||||
Port: settings.Port,
|
||||
User: settings.User,
|
||||
Password: settings.Password,
|
||||
Name: settings.Name,
|
||||
SSLMode: settings.SSLMode,
|
||||
}),
|
||||
Pool: &database.PoolSettings{
|
||||
MaxOpenConns: settings.MaxOpenConns,
|
||||
MaxIdleConns: settings.MaxIdleConns,
|
||||
ConnMaxLifetime: settings.ConnMaxLifetime,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func openDatabase(settings config.Settings) *database.Connection {
|
||||
connection, err := database.Connect(databaseConfig(settings))
|
||||
if err != nil {
|
||||
panic("Unable to open the database: " + err.Error())
|
||||
}
|
||||
|
||||
if err := db.AutoMigrate(&models.Article{}, &models.Image{}, &models.User{}, &models.Invite{}, &models.APIKey{}); err != nil {
|
||||
if err := database.Migrate(connection.DB); err != nil {
|
||||
panic("Failed to migrate database: " + err.Error())
|
||||
}
|
||||
|
||||
if err := persistence.BackfillUserRoles(db); err != nil {
|
||||
panic("Failed to backfill user roles: " + err.Error())
|
||||
}
|
||||
|
||||
if err := persistence.BackfillImageMetadata(db, uploadDir); err != nil {
|
||||
panic("Failed to backfill image metadata: " + err.Error())
|
||||
}
|
||||
|
||||
return db
|
||||
return connection
|
||||
}
|
||||
|
||||
func releasePublicKey() ed25519.PublicKey {
|
||||
@@ -139,77 +233,99 @@ func releasePublicKey() ed25519.PublicKey {
|
||||
return key
|
||||
}
|
||||
|
||||
func getRepositories(db *gorm.DB, settings config.Settings) *Repositories {
|
||||
dataDir := filepath.Dir(settings.DBFile)
|
||||
func getRepositories(connection *database.Connection, settings config.Settings) *Repositories {
|
||||
db := connection.DB
|
||||
dataDir := settings.DataDir()
|
||||
return &Repositories{
|
||||
ArticleRepo: persistence.NewArticleRepository(db),
|
||||
UserRepo: persistence.NewUserRepository(db),
|
||||
ImageRepo: persistence.NewImageRepository(db, settings.UploadDir),
|
||||
ImageRepo: persistence.NewImageRepository(db),
|
||||
InviteRepo: persistence.NewInviteRepository(db),
|
||||
APIKeyRepo: persistence.NewAPIKeyRepository(db),
|
||||
PurgeSettingsRepo: persistence.NewPurgeSettingsRepository(db),
|
||||
AuditRepo: persistence.NewAuditEventRepository(db),
|
||||
AuditSettingsRepo: persistence.NewAuditSettingsRepository(db),
|
||||
MailRepo: mailer.NewMailRepository(settings.Smtp),
|
||||
ReleaseFeed: update.NewGiteaReleaseFeed(settings.UpdateFeedURL),
|
||||
UpdateStore: update.NewJSONStatusStore(filepath.Join(dataDir, updateStatusFileName)),
|
||||
UpdateJobs: update.NewJSONJobStore(filepath.Join(dataDir, updateJobFileName)),
|
||||
UpdateHelper: update.NewFileHelperDetector(filepath.Join(dataDir, updateHelperFileName)),
|
||||
UpdateHelper: update.NewContainerHelperDetector(update.NewFileHelperDetector(filepath.Join(dataDir, updateHelperFileName)), settings.Container),
|
||||
AssetFetcher: update.NewHTTPAssetFetcher(settings.UpdateFeedURL),
|
||||
SignatureVerifier: update.NewEd25519SignatureVerifier(releasePublicKey()),
|
||||
StagedRunner: update.NewExecStagedBinaryRunner(),
|
||||
DatabaseBackup: update.NewSQLiteDatabaseBackup(db, filepath.Join(dataDir, backupsDirName), time.Now),
|
||||
DatabaseBackup: connection.NewBackup(filepath.Join(dataDir, backupsDirName), time.Now),
|
||||
UpdateResults: update.NewJSONResultStore(filepath.Join(dataDir, domainUpdate.ResultFileName)),
|
||||
RestartRequests: update.NewJSONRestartRequestStore(filepath.Join(dataDir, domainUpdate.RestartRequestFileName)),
|
||||
RunningInfo: update.NewJSONRunningInfoStore(filepath.Join(dataDir, domainUpdate.RunningInfoFileName)),
|
||||
}
|
||||
}
|
||||
|
||||
func getUseCases(repos *Repositories, settings config.Settings) *UseCases {
|
||||
func getUseCases(repos *Repositories, imageStorage domainImage.Storage, settings config.Settings) *UseCases {
|
||||
hasher := hashing.NewBcryptHasher()
|
||||
renderer := markdown.NewConverter()
|
||||
keyGenerator := hashing.NewSHA256KeyGenerator()
|
||||
recorder := auditing.NewRecorder(repos.AuditRepo, time.Now, log.Default())
|
||||
mergeMedia := useCases.NewMergeMediaUseCase(repos.ImageRepo, repos.ArticleRepo, imageStorage, recorder)
|
||||
|
||||
runPurge := useCases.NewRunPurgeUseCase(repos.ImageRepo, repos.ArticleRepo, mergeMedia, repos.PurgeSettingsRepo, time.Now, log.Default(), recorder)
|
||||
purgeScheduler := useCases.NewPurgeSchedulerUseCase(repos.PurgeSettingsRepo, runPurge, recorder)
|
||||
activityCleanup := useCases.NewCleanupActivityUseCase(repos.AuditRepo, repos.AuditSettingsRepo, recorder)
|
||||
|
||||
return &UseCases{
|
||||
Token: jwtauth.New("HS256", []byte(settings.JWTSecret), nil),
|
||||
CreateArticleUseCase: useCases.NewCreateArticleUseCase(repos.ArticleRepo, renderer),
|
||||
GetArticleUseCase: useCases.NewGetArticleUseCase(repos.ArticleRepo, renderer),
|
||||
ListArticlesUseCase: useCases.NewListArticlesUseCase(repos.ArticleRepo, renderer),
|
||||
UpdateArticleUseCase: useCases.NewUpdateArticleUseCase(repos.ArticleRepo, renderer),
|
||||
DeleteArticleUseCase: useCases.NewDeleteArticleUseCase(repos.ArticleRepo),
|
||||
GetUserUseCase: useCases.NewGetUserUseCase(repos.UserRepo),
|
||||
CreateUserUseCase: useCases.NewCreateUserUseCase(repos.UserRepo, hasher, repos.MailRepo, settings.PublicURL),
|
||||
AuthenticateUserUseCase: useCases.NewAuthenticateUserUseCase(repos.UserRepo, hasher),
|
||||
VerifyUserUseCase: useCases.NewVerifyUserUseCase(repos.UserRepo, hasher),
|
||||
CancelRegistrationUseCase: useCases.NewCancelRegistrationUseCase(repos.UserRepo),
|
||||
RequestPasswordResetUseCase: useCases.NewRequestPasswordResetUseCase(repos.UserRepo, hasher, repos.MailRepo, settings.PublicURL),
|
||||
ResetPasswordUseCase: useCases.NewResetPasswordUseCase(repos.UserRepo, hasher, repos.APIKeyRepo, time.Now),
|
||||
UpdateUserProfileUseCase: useCases.NewUpdateUserProfileUseCase(repos.UserRepo),
|
||||
ListUsersUseCase: useCases.NewListUsersUseCase(repos.UserRepo),
|
||||
CreateInviteUseCase: useCases.NewCreateInviteUseCase(repos.UserRepo, repos.InviteRepo, hasher, repos.MailRepo, settings.PublicURL),
|
||||
CancelInviteUseCase: useCases.NewCancelInviteUseCase(repos.InviteRepo),
|
||||
RenewInviteUseCase: useCases.NewRenewInviteUseCase(repos.UserRepo, repos.InviteRepo, hasher, repos.MailRepo, settings.PublicURL),
|
||||
ListUsersAndInvitesUseCase: useCases.NewListUsersAndInvitesUseCase(repos.UserRepo, repos.InviteRepo),
|
||||
GetInviteUseCase: useCases.NewGetInviteUseCase(repos.InviteRepo, hasher),
|
||||
AcceptInviteUseCase: useCases.NewAcceptInviteUseCase(repos.UserRepo, repos.InviteRepo, hasher),
|
||||
SendMailUseCase: useCases.NewSendMailUseCase(repos.MailRepo),
|
||||
CreateImageUseCase: useCases.NewCreateImageUseCase(repos.ImageRepo),
|
||||
ListMediaUseCase: useCases.NewListMediaUseCase(repos.ImageRepo, repos.ArticleRepo, repos.UserRepo),
|
||||
RenameMediaUseCase: useCases.NewRenameMediaUseCase(repos.ImageRepo),
|
||||
DeleteMediaUseCase: useCases.NewDeleteMediaUseCase(repos.ImageRepo),
|
||||
MergeMediaUseCase: useCases.NewMergeMediaUseCase(repos.ImageRepo, repos.ArticleRepo),
|
||||
GetMediaUsageUseCase: useCases.NewGetMediaUsageUseCase(repos.ImageRepo, repos.ArticleRepo),
|
||||
GetSettingsUseCase: useCases.NewGetSettingsUseCase(settings.Version),
|
||||
GetUpdateStatusUseCase: useCases.NewGetUpdateStatusUseCase(repos.UpdateStore, repos.UpdateJobs, repos.UpdateHelper, repos.UpdateResults, repos.RestartRequests, settings.Version, time.Now),
|
||||
CheckForUpdateUseCase: useCases.NewCheckForUpdateUseCase(repos.ReleaseFeed, repos.UpdateStore, settings.Version, time.Now),
|
||||
StartUpdateDownloadUseCase: startUpdateDownloadUseCase(repos, settings),
|
||||
RequestRestartUseCase: useCases.NewRequestRestartUseCase(repos.UpdateStore, repos.UpdateJobs, repos.UpdateHelper, repos.UpdateResults, repos.RestartRequests, settings.Version, time.Now),
|
||||
RecordRunningInfoUseCase: useCases.NewRecordRunningInfoUseCase(repos.RunningInfo, settings.Version, os.Getpid(), time.Now),
|
||||
CreateAPIKeyUseCase: useCases.NewCreateAPIKeyUseCase(repos.APIKeyRepo, keyGenerator, time.Now),
|
||||
ListAPIKeysUseCase: useCases.NewListAPIKeysUseCase(repos.APIKeyRepo, repos.UserRepo, time.Now),
|
||||
RevokeAPIKeyUseCase: useCases.NewRevokeAPIKeyUseCase(repos.APIKeyRepo, repos.UserRepo, time.Now),
|
||||
AuthenticateAPIKeyUseCase: useCases.NewAuthenticateAPIKeyUseCase(repos.APIKeyRepo, keyGenerator, repos.UserRepo, time.Now),
|
||||
PurgeScheduler: scheduler.NewSystemRunner(purgeScheduler, log.Default()),
|
||||
ActivityCleanupScheduler: scheduler.NewSystemCleanupRunner(activityCleanup, log.Default()),
|
||||
Token: jwtauth.New("HS256", []byte(settings.JWTSecret), nil),
|
||||
CreateArticleUseCase: useCases.NewCreateArticleUseCase(repos.ArticleRepo, renderer, recorder),
|
||||
GetArticleUseCase: useCases.NewGetArticleUseCase(repos.ArticleRepo, renderer),
|
||||
ListArticlesUseCase: useCases.NewListArticlesUseCase(repos.ArticleRepo, renderer),
|
||||
UpdateArticleUseCase: useCases.NewUpdateArticleUseCase(repos.ArticleRepo, renderer, recorder),
|
||||
DeleteArticleUseCase: useCases.NewDeleteArticleUseCase(repos.ArticleRepo, recorder),
|
||||
GetUserUseCase: useCases.NewGetUserUseCase(repos.UserRepo),
|
||||
CreateUserUseCase: useCases.NewCreateUserUseCase(repos.UserRepo, hasher, repos.MailRepo, settings.PublicURL, recorder),
|
||||
AuthenticateUserUseCase: useCases.NewAuthenticateUserUseCase(repos.UserRepo, hasher, recorder),
|
||||
VerifyUserUseCase: useCases.NewVerifyUserUseCase(repos.UserRepo, hasher, recorder),
|
||||
CancelRegistrationUseCase: useCases.NewCancelRegistrationUseCase(repos.UserRepo),
|
||||
RequestPasswordResetUseCase: useCases.NewRequestPasswordResetUseCase(repos.UserRepo, hasher, repos.MailRepo, settings.PublicURL, recorder),
|
||||
ResetPasswordUseCase: useCases.NewResetPasswordUseCase(repos.UserRepo, hasher, repos.APIKeyRepo, time.Now, recorder),
|
||||
UpdateUserProfileUseCase: useCases.NewUpdateUserProfileUseCase(repos.UserRepo, recorder),
|
||||
ListUsersUseCase: useCases.NewListUsersUseCase(repos.UserRepo),
|
||||
CreateInviteUseCase: useCases.NewCreateInviteUseCase(repos.UserRepo, repos.InviteRepo, hasher, repos.MailRepo, settings.PublicURL, recorder),
|
||||
CancelInviteUseCase: useCases.NewCancelInviteUseCase(repos.InviteRepo, recorder),
|
||||
RenewInviteUseCase: useCases.NewRenewInviteUseCase(repos.UserRepo, repos.InviteRepo, hasher, repos.MailRepo, settings.PublicURL, recorder),
|
||||
ListUsersAndInvitesUseCase: useCases.NewListUsersAndInvitesUseCase(repos.UserRepo, repos.InviteRepo),
|
||||
GetInviteUseCase: useCases.NewGetInviteUseCase(repos.InviteRepo, hasher),
|
||||
AcceptInviteUseCase: useCases.NewAcceptInviteUseCase(repos.UserRepo, repos.InviteRepo, hasher, recorder),
|
||||
SendMailUseCase: useCases.NewSendMailUseCase(repos.MailRepo),
|
||||
CreateImageUseCase: useCases.NewCreateImageUseCase(repos.ImageRepo, imageStorage, recorder),
|
||||
OpenMediaUseCase: useCases.NewOpenMediaUseCase(imageStorage),
|
||||
ListMediaUseCase: useCases.NewListMediaUseCase(repos.ImageRepo, repos.ArticleRepo, repos.UserRepo),
|
||||
RenameMediaUseCase: useCases.NewRenameMediaUseCase(repos.ImageRepo, recorder),
|
||||
DeleteMediaUseCase: useCases.NewDeleteMediaUseCase(repos.ImageRepo, imageStorage, recorder),
|
||||
MergeMediaUseCase: mergeMedia,
|
||||
GetMediaUsageUseCase: useCases.NewGetMediaUsageUseCase(repos.ImageRepo, repos.ArticleRepo),
|
||||
GetPurgeSettingsUseCase: useCases.NewGetPurgeSettingsUseCase(repos.PurgeSettingsRepo),
|
||||
UpdatePurgeSettingsUseCase: useCases.NewUpdatePurgeSettingsUseCase(repos.PurgeSettingsRepo, time.Now),
|
||||
PreviewPurgeUseCase: useCases.NewPreviewPurgeUseCase(repos.ImageRepo, repos.ArticleRepo),
|
||||
RunPurgeUseCase: runPurge,
|
||||
GetSettingsUseCase: useCases.NewGetSettingsUseCase(settings.Version),
|
||||
GetUpdateStatusUseCase: useCases.NewGetUpdateStatusUseCase(repos.UpdateStore, repos.UpdateJobs, repos.UpdateHelper, repos.UpdateResults, repos.RestartRequests, settings.Version, settings.Container, time.Now),
|
||||
CheckForUpdateUseCase: useCases.NewCheckForUpdateUseCase(repos.ReleaseFeed, repos.UpdateStore, settings.Version, time.Now, recorder),
|
||||
StartUpdateDownloadUseCase: startUpdateDownloadUseCase(repos, settings, recorder),
|
||||
RequestRestartUseCase: useCases.NewRequestRestartUseCase(repos.UpdateStore, repos.UpdateJobs, repos.UpdateHelper, repos.UpdateResults, repos.RestartRequests, settings.Version, time.Now, recorder),
|
||||
RecordRunningInfoUseCase: useCases.NewRecordRunningInfoUseCase(repos.RunningInfo, settings.Version, os.Getpid(), time.Now),
|
||||
CreateAPIKeyUseCase: useCases.NewCreateAPIKeyUseCase(repos.APIKeyRepo, keyGenerator, time.Now, recorder),
|
||||
ListAPIKeysUseCase: useCases.NewListAPIKeysUseCase(repos.APIKeyRepo, repos.UserRepo, time.Now),
|
||||
RevokeAPIKeyUseCase: useCases.NewRevokeAPIKeyUseCase(repos.APIKeyRepo, repos.UserRepo, time.Now, recorder),
|
||||
AuthenticateAPIKeyUseCase: useCases.NewAuthenticateAPIKeyUseCase(repos.APIKeyRepo, keyGenerator, repos.UserRepo, time.Now),
|
||||
ListActivityUseCase: useCases.NewListActivityUseCase(repos.AuditRepo, repos.UserRepo, repos.ArticleRepo, repos.ImageRepo, repos.APIKeyRepo, time.Now),
|
||||
GetActivitySummaryUseCase: useCases.NewGetActivitySummaryUseCase(repos.AuditRepo, time.Now),
|
||||
ExportActivityUseCase: useCases.NewExportActivityUseCase(repos.AuditRepo, time.Now),
|
||||
GetActivityRetentionUseCase: useCases.NewGetActivityRetentionUseCase(repos.AuditSettingsRepo, time.Now),
|
||||
UpdateActivityRetentionUseCase: useCases.NewUpdateActivityRetentionUseCase(repos.AuditSettingsRepo, recorder, time.Now),
|
||||
}
|
||||
}
|
||||
|
||||
func startUpdateDownloadUseCase(repos *Repositories, settings config.Settings) *useCases.StartUpdateDownloadUseCase {
|
||||
func startUpdateDownloadUseCase(repos *Repositories, settings config.Settings, recorder domainAudit.Recorder) *useCases.StartUpdateDownloadUseCase {
|
||||
return useCases.NewStartUpdateDownloadUseCase(useCases.StartUpdateDownloadDependencies{
|
||||
Store: repos.UpdateStore,
|
||||
Jobs: repos.UpdateJobs,
|
||||
@@ -220,29 +336,34 @@ func startUpdateDownloadUseCase(repos *Repositories, settings config.Settings) *
|
||||
Verifier: repos.SignatureVerifier,
|
||||
Runner: repos.StagedRunner,
|
||||
Backup: repos.DatabaseBackup,
|
||||
Staging: update.NewOSStagingArea(filepath.Join(filepath.Dir(settings.DBFile), updatesDirName)),
|
||||
Staging: update.NewOSStagingArea(filepath.Join(settings.DataDir(), updatesDirName)),
|
||||
Logger: log.Default(),
|
||||
CurrentVersion: settings.Version,
|
||||
Platform: domainUpdate.Platform{OS: runtime.GOOS, Arch: runtime.GOARCH},
|
||||
FeedURL: settings.UpdateFeedURL,
|
||||
Now: time.Now,
|
||||
Recorder: recorder,
|
||||
})
|
||||
}
|
||||
|
||||
func InitContainer(settings config.Settings) (*UseCases, func() error) {
|
||||
db := getDb(settings.DBFile, settings.UploadDir)
|
||||
repos := getRepositories(db, settings)
|
||||
return getUseCases(repos, settings), closeDb(db)
|
||||
func backfillImageMetadata(imageRepo domainImage.Repository, imageStorage domainImage.Storage, settings config.Settings) error {
|
||||
run := func(readable domainImage.Storage) error {
|
||||
return useCases.NewBackfillImageMetadataUseCase(imageRepo, readable).Run()
|
||||
}
|
||||
if settings.Storage.Driver == config.StorageDriverS3 {
|
||||
return storage.RunBackfillReportingReads(imageStorage, log.Default(), run)
|
||||
}
|
||||
return run(imageStorage)
|
||||
}
|
||||
|
||||
func closeDb(db *gorm.DB) func() error {
|
||||
return func() error {
|
||||
sqlDb, err := db.DB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return sqlDb.Close()
|
||||
func InitContainer(settings config.Settings) (*UseCases, func() error) {
|
||||
imageStorage := openImageStorage(settings)
|
||||
connection := openDatabase(settings)
|
||||
repos := getRepositories(connection, settings)
|
||||
if err := backfillImageMetadata(repos.ImageRepo, imageStorage, settings); err != nil {
|
||||
panic("Failed to backfill image metadata: " + err.Error())
|
||||
}
|
||||
return getUseCases(repos, imageStorage, settings), connection.Close
|
||||
}
|
||||
|
||||
type BrowserUpdateCommands struct {
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
package activity
|
||||
|
||||
import (
|
||||
"RenewCMS/api/pkg/apperr"
|
||||
"RenewCMS/api/pkg/claims"
|
||||
validator "RenewCMS/api/pkg/validator"
|
||||
"RenewCMS/internal/domain/audit"
|
||||
"RenewCMS/internal/domain/user"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
csvContentType = "text/csv; charset=utf-8"
|
||||
csvContentDisposition = `attachment; filename="activity.csv"`
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
ListUseCase *useCases.ListActivityUseCase
|
||||
SummaryUseCase *useCases.GetActivitySummaryUseCase
|
||||
ExportUseCase *useCases.ExportActivityUseCase
|
||||
}
|
||||
|
||||
type filterQuery struct {
|
||||
Q string `validate:"max=200"`
|
||||
Actor string `validate:"max=40"`
|
||||
Category string `validate:"omitempty,oneof=articles media users api_keys auth system"`
|
||||
Outcome string `validate:"omitempty,oneof=all success failed"`
|
||||
Security string `validate:"omitempty,oneof=true"`
|
||||
Range string `validate:"omitempty,oneof=today 7d 30d custom"`
|
||||
From string `validate:"max=10"`
|
||||
To string `validate:"max=10"`
|
||||
}
|
||||
|
||||
type listQuery struct {
|
||||
filterQuery
|
||||
Page int `validate:"gte=1"`
|
||||
Size int `validate:"oneof=5 25 50 100"`
|
||||
}
|
||||
|
||||
type actorResponse struct {
|
||||
Type audit.ActorType `json:"type"`
|
||||
ID *uint32 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type targetResponse struct {
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Exists bool `json:"exists"`
|
||||
}
|
||||
|
||||
type itemResponse struct {
|
||||
ID uint64 `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Action string `json:"action"`
|
||||
Category audit.Category `json:"category"`
|
||||
Actor actorResponse `json:"actor"`
|
||||
Target *targetResponse `json:"target"`
|
||||
Outcome audit.Outcome `json:"outcome"`
|
||||
FailureReason *string `json:"failure_reason"`
|
||||
Security bool `json:"security"`
|
||||
Source audit.Source `json:"source"`
|
||||
IP *string `json:"ip"`
|
||||
UserAgent *string `json:"user_agent"`
|
||||
Params map[string]any `json:"params"`
|
||||
Changes []audit.Change `json:"changes"`
|
||||
}
|
||||
|
||||
type listResponse struct {
|
||||
Items []itemResponse `json:"items"`
|
||||
Total int `json:"total"`
|
||||
Page int `json:"page"`
|
||||
Size int `json:"size"`
|
||||
}
|
||||
|
||||
func NewHandler(list *useCases.ListActivityUseCase, summary *useCases.GetActivitySummaryUseCase, export *useCases.ExportActivityUseCase) *Handler {
|
||||
return &Handler{ListUseCase: list, SummaryUseCase: summary, ExportUseCase: export}
|
||||
}
|
||||
|
||||
func readFilterQuery(values url.Values) filterQuery {
|
||||
return filterQuery{
|
||||
Q: values.Get("q"),
|
||||
Actor: values.Get("actor"),
|
||||
Category: values.Get("category"),
|
||||
Outcome: values.Get("outcome"),
|
||||
Security: values.Get("security"),
|
||||
Range: values.Get("range"),
|
||||
From: values.Get("from"),
|
||||
To: values.Get("to"),
|
||||
}
|
||||
}
|
||||
|
||||
func readListQuery(values url.Values) (listQuery, error) {
|
||||
page, err := intParam(values, "page", 1)
|
||||
if err != nil {
|
||||
return listQuery{}, err
|
||||
}
|
||||
size, err := intParam(values, "size", 25)
|
||||
if err != nil {
|
||||
return listQuery{}, err
|
||||
}
|
||||
return listQuery{filterQuery: readFilterQuery(values), Page: page, Size: size}, nil
|
||||
}
|
||||
|
||||
func intParam(values url.Values, name string, fallback int) (int, error) {
|
||||
if !values.Has(name) {
|
||||
return fallback, nil
|
||||
}
|
||||
return strconv.Atoi(values.Get(name))
|
||||
}
|
||||
|
||||
func toActivityQuery(query filterQuery) useCases.ActivityQuery {
|
||||
return useCases.ActivityQuery{
|
||||
Q: query.Q,
|
||||
Actor: query.Actor,
|
||||
Category: query.Category,
|
||||
Outcome: query.Outcome,
|
||||
Security: query.Security,
|
||||
Range: query.Range,
|
||||
From: query.From,
|
||||
To: query.To,
|
||||
}
|
||||
}
|
||||
|
||||
func writeActivityError(w http.ResponseWriter, err error) {
|
||||
switch {
|
||||
case errors.Is(err, audit.ErrInvalidFilter):
|
||||
http.Error(w, apperr.BodyErrorMessage, http.StatusBadRequest)
|
||||
case errors.Is(err, audit.ErrActorNotAllowed):
|
||||
http.Error(w, apperr.OwnerOrAdminRequiredErrorMessage, http.StatusForbidden)
|
||||
case errors.Is(err, user.ErrUserNotFound):
|
||||
http.Error(w, apperr.UnauthorizedErrorMessage, http.StatusUnauthorized)
|
||||
default:
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) List(w http.ResponseWriter, r *http.Request) {
|
||||
query, err := readListQuery(r.URL.Query())
|
||||
if err != nil || validator.Validate.Struct(query) != nil {
|
||||
http.Error(w, apperr.BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
viewerId, ok := claims.UserId(r)
|
||||
if !ok {
|
||||
http.Error(w, apperr.UnauthorizedErrorMessage, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
activityQuery := toActivityQuery(query.filterQuery)
|
||||
activityQuery.Page, activityQuery.Size = query.Page, query.Size
|
||||
page, err := h.ListUseCase.ListActivity(useCases.ListActivityCommand{ViewerID: viewerId, Query: activityQuery})
|
||||
if err != nil {
|
||||
writeActivityError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
items := make([]itemResponse, 0, len(page.Items))
|
||||
for _, entry := range page.Items {
|
||||
items = append(items, toItemResponse(entry))
|
||||
}
|
||||
writeJSON(w, listResponse{Items: items, Total: page.Total, Page: page.Page, Size: page.Size})
|
||||
}
|
||||
|
||||
func toItemResponse(entry useCases.ActivityEntry) itemResponse {
|
||||
event := entry.Event
|
||||
item := itemResponse{
|
||||
ID: event.ID,
|
||||
CreatedAt: event.CreatedAt,
|
||||
Action: event.Action,
|
||||
Category: event.Category,
|
||||
Actor: actorResponse{Type: event.Actor.Type, ID: event.Actor.ID, Name: event.Actor.Name},
|
||||
Outcome: event.Outcome,
|
||||
FailureReason: event.FailureReason,
|
||||
Security: event.Security,
|
||||
Source: event.Source,
|
||||
IP: event.IP,
|
||||
UserAgent: event.UserAgent,
|
||||
Params: event.Params,
|
||||
Changes: event.Changes,
|
||||
}
|
||||
if item.Params == nil {
|
||||
item.Params = map[string]any{}
|
||||
}
|
||||
if item.Changes == nil {
|
||||
item.Changes = []audit.Change{}
|
||||
}
|
||||
if event.Target.Type != "" {
|
||||
item.Target = &targetResponse{Type: event.Target.Type, ID: event.Target.ID, Name: event.Target.Name, Exists: entry.TargetExists}
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
func (h *Handler) Summary(w http.ResponseWriter, r *http.Request) {
|
||||
query := readFilterQuery(r.URL.Query())
|
||||
if validator.Validate.Struct(query) != nil {
|
||||
http.Error(w, apperr.BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
summary, err := h.SummaryUseCase.GetActivitySummary(toActivityQuery(query))
|
||||
if err != nil {
|
||||
writeActivityError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, summary)
|
||||
}
|
||||
|
||||
func (h *Handler) Export(w http.ResponseWriter, r *http.Request) {
|
||||
query := readFilterQuery(r.URL.Query())
|
||||
if validator.Validate.Struct(query) != nil {
|
||||
http.Error(w, apperr.BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
prepared, err := h.ExportUseCase.Prepare(toActivityQuery(query))
|
||||
if err != nil {
|
||||
writeActivityError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", csvContentType)
|
||||
w.Header().Set("Content-Disposition", csvContentDisposition)
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
_ = prepared.WriteCSV(w)
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, payload any) {
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
_, _ = w.Write(body)
|
||||
}
|
||||
@@ -71,7 +71,7 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
created, err := h.CreateUseCase.CreateAPIKey(useCases.CreateAPIKeyCommand{
|
||||
created, err := h.CreateUseCase.CreateAPIKey(r.Context(), useCases.CreateAPIKeyCommand{
|
||||
UserID: userId,
|
||||
Name: request.Name,
|
||||
ExpiresAt: request.ExpiresAt,
|
||||
@@ -161,7 +161,7 @@ func (h *Handler) Revoke(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.RevokeUseCase.RevokeAPIKey(userId, uint32(id)); err != nil {
|
||||
if err := h.RevokeUseCase.RevokeAPIKey(r.Context(), userId, uint32(id)); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrAPIKeyNotFound):
|
||||
http.Error(w, apperr.APIKeyNotFoundErrorMessage, http.StatusNotFound)
|
||||
|
||||
@@ -149,7 +149,7 @@ func (h *Handler) PostArticle(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
createdArticle, err := h.CreateUseCase.CreateArticle(useCases.CreateArticleCommand{
|
||||
createdArticle, err := h.CreateUseCase.CreateArticle(r.Context(), useCases.CreateArticleCommand{
|
||||
Title: localArticle.Title,
|
||||
Body: localArticle.Body,
|
||||
Format: format,
|
||||
@@ -201,7 +201,7 @@ func (h *Handler) setIsOnline(w http.ResponseWriter, r *http.Request, isOnline b
|
||||
return
|
||||
}
|
||||
|
||||
updatedArticle, err := h.UpdateUseCase.UpdateIsOnline(id, isOnline, format)
|
||||
updatedArticle, err := h.UpdateUseCase.UpdateIsOnline(r.Context(), id, isOnline, format)
|
||||
if err != nil {
|
||||
writeArticleErrorResponse(w, err)
|
||||
return
|
||||
@@ -237,7 +237,7 @@ func (h *Handler) UpdateArticleBody(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
updatedArticle, err := h.UpdateUseCase.UpdateBody(id, localArticle.Body, format)
|
||||
updatedArticle, err := h.UpdateUseCase.UpdateBody(r.Context(), id, localArticle.Body, format)
|
||||
if err != nil {
|
||||
writeArticleErrorResponse(w, err)
|
||||
return
|
||||
@@ -314,7 +314,7 @@ func (h *Handler) DeleteArticle(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.DeleteUseCase.DeleteArticle(id); err != nil {
|
||||
if err := h.DeleteUseCase.DeleteArticle(r.Context(), id); err != nil {
|
||||
if errors.Is(err, domain.ErrArticleNotFound) {
|
||||
http.Error(w, apperr.ArticleNotFoundErrorMessage, http.StatusNotFound)
|
||||
return
|
||||
|
||||
@@ -67,7 +67,7 @@ func (h *AcceptInviteHandler) AcceptInvite(w http.ResponseWriter, r *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
createdUser, err := h.AcceptInviteUseCase.AcceptInvite(useCases.AcceptInviteCommand{
|
||||
createdUser, err := h.AcceptInviteUseCase.AcceptInvite(r.Context(), useCases.AcceptInviteCommand{
|
||||
Code: request.Code,
|
||||
Username: request.Username,
|
||||
Password: request.Password,
|
||||
|
||||
@@ -87,7 +87,7 @@ func (h *LoginHandler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
dbUser, err := h.AuthenticateUseCase.Authenticate(credentials.Username, credentials.Password)
|
||||
dbUser, err := h.AuthenticateUseCase.Authenticate(r.Context(), credentials.Username, credentials.Password)
|
||||
if err != nil {
|
||||
http.Error(w, apperr.LogsErrorMessage, http.StatusForbidden)
|
||||
return
|
||||
@@ -113,7 +113,7 @@ func (h *LoginHandler) Register(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
createdUser, err := h.CreateUseCase.CreateUser(useCases.CreateUserCommand{
|
||||
createdUser, err := h.CreateUseCase.CreateUser(r.Context(), useCases.CreateUserCommand{
|
||||
Username: credentials.Username,
|
||||
Password: credentials.Password,
|
||||
Email: credentials.Email,
|
||||
@@ -139,7 +139,8 @@ func (h *LoginHandler) RegistrationStatus(w http.ResponseWriter, _ *http.Request
|
||||
_, _ = w.Write(body)
|
||||
}
|
||||
|
||||
func (h *LoginHandler) Logout(w http.ResponseWriter, _ *http.Request) {
|
||||
func (h *LoginHandler) Logout(w http.ResponseWriter, r *http.Request) {
|
||||
h.AuthenticateUseCase.Logout(r.Context())
|
||||
removeJwtCookie(w, h.IsProduction)
|
||||
message, _ := json.Marshal(map[string]any{"message": "User logged out! HTTPonly jwt cookie deleted"})
|
||||
_, _ = w.Write(message)
|
||||
|
||||
@@ -46,7 +46,7 @@ func (h *PasswordResetHandler) RequestPasswordReset(w http.ResponseWriter, r *ht
|
||||
return
|
||||
}
|
||||
|
||||
_ = h.RequestPasswordResetUseCase.RequestPasswordReset(credentials.Email)
|
||||
_ = h.RequestPasswordResetUseCase.RequestPasswordReset(r.Context(), credentials.Email)
|
||||
|
||||
message, _ := json.Marshal(map[string]any{"message": apperr.PasswordResetRequestedMessage})
|
||||
_, _ = w.Write(message)
|
||||
@@ -66,7 +66,7 @@ func (h *PasswordResetHandler) ResetPassword(w http.ResponseWriter, r *http.Requ
|
||||
return
|
||||
}
|
||||
|
||||
err = h.ResetPasswordUseCase.ResetPassword(credentials.Email, credentials.Code, credentials.Password)
|
||||
err = h.ResetPasswordUseCase.ResetPassword(r.Context(), credentials.Email, credentials.Code, credentials.Password)
|
||||
if err != nil {
|
||||
if errors.Is(err, user.ErrInvalidPasswordResetCode) || errors.Is(err, user.ErrPasswordResetCodeExpired) {
|
||||
http.Error(w, apperr.PasswordResetFailedErrorMessage, http.StatusForbidden)
|
||||
|
||||
@@ -50,7 +50,7 @@ func (h *VerificationHandler) Verify(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
verifiedUser, err := h.VerifyUseCase.Verify(userId, credentials.Code)
|
||||
verifiedUser, err := h.VerifyUseCase.Verify(r.Context(), userId, credentials.Code)
|
||||
if err != nil {
|
||||
if errors.Is(err, user.ErrUserNotFound) {
|
||||
http.Error(w, apperr.UnauthorizedErrorMessage, http.StatusUnauthorized)
|
||||
|
||||
@@ -2,6 +2,7 @@ package frontend
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/infrastructure/assets"
|
||||
"bytes"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
@@ -93,12 +94,15 @@ func (h *spaHandler) serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
content, ok := f.(io.ReadSeeker)
|
||||
if !ok {
|
||||
http.Error(w, "File not seekable", http.StatusInternalServerError)
|
||||
html, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
http.Error(w, "Index not readable", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Cache-Control", indexCacheControl)
|
||||
http.ServeContent(w, r, "index.html", h.modTime, content)
|
||||
if policy := w.Header().Get("Content-Security-Policy"); policy != "" {
|
||||
w.Header().Set("Content-Security-Policy", AllowInlineScripts(policy, html))
|
||||
}
|
||||
http.ServeContent(w, r, "index.html", h.modTime, bytes.NewReader(html))
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const scriptSrcSelf = "script-src 'self'"
|
||||
|
||||
var inlineScriptPattern = regexp.MustCompile(`(?is)<script(\s[^>]*)?>(.*?)</script>`)
|
||||
var srcAttributePattern = regexp.MustCompile(`(?i)\ssrc\s*=`)
|
||||
|
||||
func inlineScriptSources(html []byte) []string {
|
||||
var sources []string
|
||||
for _, match := range inlineScriptPattern.FindAllSubmatch(html, -1) {
|
||||
if srcAttributePattern.Match(match[1]) || strings.TrimSpace(string(match[2])) == "" {
|
||||
continue
|
||||
}
|
||||
digest := sha256.Sum256(match[2])
|
||||
sources = append(sources, "'sha256-"+base64.StdEncoding.EncodeToString(digest[:])+"'")
|
||||
}
|
||||
return sources
|
||||
}
|
||||
|
||||
func AllowInlineScripts(policy string, html []byte) string {
|
||||
sources := inlineScriptSources(html)
|
||||
if len(sources) == 0 {
|
||||
return policy
|
||||
}
|
||||
return strings.Replace(policy, scriptSrcSelf, scriptSrcSelf+" "+strings.Join(sources, " "), 1)
|
||||
}
|
||||
@@ -19,12 +19,16 @@ import (
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
CreateUseCase *useCases.CreateImageUseCase
|
||||
ListUseCase *useCases.ListMediaUseCase
|
||||
RenameUseCase *useCases.RenameMediaUseCase
|
||||
DeleteUseCase *useCases.DeleteMediaUseCase
|
||||
MergeUseCase *useCases.MergeMediaUseCase
|
||||
UsageUseCase *useCases.GetMediaUsageUseCase
|
||||
CreateUseCase *useCases.CreateImageUseCase
|
||||
ListUseCase *useCases.ListMediaUseCase
|
||||
RenameUseCase *useCases.RenameMediaUseCase
|
||||
DeleteUseCase *useCases.DeleteMediaUseCase
|
||||
MergeUseCase *useCases.MergeMediaUseCase
|
||||
UsageUseCase *useCases.GetMediaUsageUseCase
|
||||
PurgeSettingsUseCase *useCases.GetPurgeSettingsUseCase
|
||||
UpdatePurgeSettingsUseCase *useCases.UpdatePurgeSettingsUseCase
|
||||
PreviewPurgeUseCase *useCases.PreviewPurgeUseCase
|
||||
RunPurgeUseCase *useCases.RunPurgeUseCase
|
||||
}
|
||||
|
||||
type RenameRequest struct {
|
||||
@@ -74,14 +78,18 @@ type mergeResponse struct {
|
||||
Merged int `json:"merged"`
|
||||
}
|
||||
|
||||
func NewHandler(create *useCases.CreateImageUseCase, list *useCases.ListMediaUseCase, rename *useCases.RenameMediaUseCase, deleteUseCase *useCases.DeleteMediaUseCase, merge *useCases.MergeMediaUseCase, usage *useCases.GetMediaUsageUseCase) *Handler {
|
||||
func NewHandler(create *useCases.CreateImageUseCase, list *useCases.ListMediaUseCase, rename *useCases.RenameMediaUseCase, deleteUseCase *useCases.DeleteMediaUseCase, merge *useCases.MergeMediaUseCase, usage *useCases.GetMediaUsageUseCase, purgeSettings *useCases.GetPurgeSettingsUseCase, updatePurgeSettings *useCases.UpdatePurgeSettingsUseCase, previewPurge *useCases.PreviewPurgeUseCase, runPurge *useCases.RunPurgeUseCase) *Handler {
|
||||
return &Handler{
|
||||
CreateUseCase: create,
|
||||
ListUseCase: list,
|
||||
RenameUseCase: rename,
|
||||
DeleteUseCase: deleteUseCase,
|
||||
MergeUseCase: merge,
|
||||
UsageUseCase: usage,
|
||||
CreateUseCase: create,
|
||||
ListUseCase: list,
|
||||
RenameUseCase: rename,
|
||||
DeleteUseCase: deleteUseCase,
|
||||
MergeUseCase: merge,
|
||||
UsageUseCase: usage,
|
||||
PurgeSettingsUseCase: purgeSettings,
|
||||
UpdatePurgeSettingsUseCase: updatePurgeSettings,
|
||||
PreviewPurgeUseCase: previewPurge,
|
||||
RunPurgeUseCase: runPurge,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,7 +213,7 @@ func (h *Handler) Upload(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
defer closeFile()
|
||||
|
||||
created, err := h.CreateUseCase.CreateImage(input)
|
||||
created, err := h.CreateUseCase.CreateImage(r.Context(), input)
|
||||
if err != nil {
|
||||
upload.WriteCreateError(w, err)
|
||||
return
|
||||
@@ -239,7 +247,7 @@ func (h *Handler) Rename(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
renamed, err := h.RenameUseCase.Rename(id, request.Filename)
|
||||
renamed, err := h.RenameUseCase.Rename(r.Context(), id, request.Filename)
|
||||
if err != nil {
|
||||
if errors.Is(err, domainImage.ErrInvalidFilename) {
|
||||
http.Error(w, apperr.MediaFilenameErrorMessage, http.StatusBadRequest)
|
||||
@@ -258,7 +266,7 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.DeleteUseCase.Delete(id); err != nil {
|
||||
if err := h.DeleteUseCase.Delete(r.Context(), id); err != nil {
|
||||
writeImageError(w, err)
|
||||
return
|
||||
}
|
||||
@@ -296,7 +304,7 @@ func (h *Handler) Merge(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.MergeUseCase.Merge(request.KeepID, request.MergeIDs)
|
||||
result, err := h.MergeUseCase.Merge(r.Context(), request.KeepID, request.MergeIDs)
|
||||
if err != nil {
|
||||
writeMergeError(w, err)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"RenewCMS/api/pkg/apperr"
|
||||
validator "RenewCMS/api/pkg/validator"
|
||||
"RenewCMS/internal/domain/purge"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PurgeSettingsRequest struct {
|
||||
AutoPurgeEnabled *bool `json:"auto_purge_enabled"`
|
||||
AutoPurgeIntervalDays *int `json:"auto_purge_interval_days" validate:"omitempty,gte=1,lte=365"`
|
||||
}
|
||||
|
||||
type PurgeRunRequest struct {
|
||||
Fingerprint *string `json:"fingerprint"`
|
||||
}
|
||||
|
||||
type purgeSettingsResponse struct {
|
||||
AutoPurgeEnabled bool `json:"auto_purge_enabled"`
|
||||
AutoPurgeIntervalDays int `json:"auto_purge_interval_days"`
|
||||
LastPurgeAt *time.Time `json:"last_purge_at"`
|
||||
LastPurgeMerged int `json:"last_purge_merged"`
|
||||
LastPurgeArticlesUpdated int `json:"last_purge_articles_updated"`
|
||||
EnabledAt *time.Time `json:"enabled_at"`
|
||||
NextPurgeAt *time.Time `json:"next_purge_at"`
|
||||
}
|
||||
|
||||
type purgePreviewGroupResponse struct {
|
||||
KeepID uint32 `json:"keep_id"`
|
||||
Filename string `json:"filename"`
|
||||
URL string `json:"url"`
|
||||
UsedIn int `json:"used_in"`
|
||||
Merged int `json:"merged"`
|
||||
}
|
||||
|
||||
type purgePreviewResponse struct {
|
||||
Groups int `json:"groups"`
|
||||
Copies int `json:"copies"`
|
||||
Articles int `json:"articles"`
|
||||
List []purgePreviewGroupResponse `json:"list"`
|
||||
More int `json:"more"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
}
|
||||
|
||||
type purgeRunResponse struct {
|
||||
GroupsTotal int `json:"groups_total"`
|
||||
GroupsPurged int `json:"groups_purged"`
|
||||
Merged int `json:"merged"`
|
||||
ArticlesUpdated int `json:"articles_updated"`
|
||||
FailedGroups int `json:"failed_groups"`
|
||||
}
|
||||
|
||||
func toPurgeSettingsResponse(settings purge.Settings) purgeSettingsResponse {
|
||||
return purgeSettingsResponse{
|
||||
AutoPurgeEnabled: settings.AutoPurgeEnabled,
|
||||
AutoPurgeIntervalDays: settings.AutoPurgeIntervalDays,
|
||||
LastPurgeAt: settings.LastPurgeAt,
|
||||
LastPurgeMerged: settings.LastPurgeMerged,
|
||||
LastPurgeArticlesUpdated: settings.LastPurgeArticlesUpdated,
|
||||
EnabledAt: settings.EnabledAt,
|
||||
NextPurgeAt: settings.NextPurgeAt(),
|
||||
}
|
||||
}
|
||||
|
||||
func writePurgeError(w http.ResponseWriter, err error) {
|
||||
switch {
|
||||
case errors.Is(err, purge.ErrInvalidPurgeInterval):
|
||||
http.Error(w, apperr.PurgeIntervalErrorMessage, http.StatusBadRequest)
|
||||
case errors.Is(err, purge.ErrPurgeConflict):
|
||||
http.Error(w, apperr.PurgeConflictErrorMessage, http.StatusConflict)
|
||||
case errors.Is(err, purge.ErrPurgeBusy):
|
||||
http.Error(w, apperr.PurgeBusyErrorMessage, http.StatusConflict)
|
||||
default:
|
||||
writeInternalServerError(w)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) GetPurgeSettings(w http.ResponseWriter, _ *http.Request) {
|
||||
settings, err := h.PurgeSettingsUseCase.Get()
|
||||
if err != nil {
|
||||
writePurgeError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, toPurgeSettingsResponse(settings))
|
||||
}
|
||||
|
||||
func (h *Handler) UpdatePurgeSettings(w http.ResponseWriter, r *http.Request) {
|
||||
var request PurgeSettingsRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, apperr.BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate.Struct(request); err != nil {
|
||||
http.Error(w, apperr.PurgeIntervalErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
settings, err := h.UpdatePurgeSettingsUseCase.Update(useCases.UpdatePurgeSettingsCommand{
|
||||
AutoPurgeEnabled: request.AutoPurgeEnabled,
|
||||
AutoPurgeIntervalDays: request.AutoPurgeIntervalDays,
|
||||
})
|
||||
if err != nil {
|
||||
writePurgeError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, toPurgeSettingsResponse(settings))
|
||||
}
|
||||
|
||||
func (h *Handler) PreviewPurge(w http.ResponseWriter, _ *http.Request) {
|
||||
preview, err := h.PreviewPurgeUseCase.Preview()
|
||||
if err != nil {
|
||||
writePurgeError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
list := make([]purgePreviewGroupResponse, 0, len(preview.List))
|
||||
for _, group := range preview.List {
|
||||
list = append(list, purgePreviewGroupResponse{
|
||||
KeepID: group.KeepID,
|
||||
Filename: group.Filename,
|
||||
URL: group.URL,
|
||||
UsedIn: group.UsedIn,
|
||||
Merged: group.Merged,
|
||||
})
|
||||
}
|
||||
writeJSON(w, http.StatusOK, purgePreviewResponse{
|
||||
Groups: preview.Groups,
|
||||
Copies: preview.Copies,
|
||||
Articles: preview.Articles,
|
||||
List: list,
|
||||
More: preview.More,
|
||||
Fingerprint: preview.Fingerprint,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) RunPurge(w http.ResponseWriter, r *http.Request) {
|
||||
raw, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, apperr.BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var request PurgeRunRequest
|
||||
if len(raw) > 0 {
|
||||
if err := json.Unmarshal(raw, &request); err != nil {
|
||||
http.Error(w, apperr.BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
result, err := h.RunPurgeUseCase.Run(r.Context(), request.Fingerprint)
|
||||
if err != nil {
|
||||
writePurgeError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, purgeRunResponse{
|
||||
GroupsTotal: result.GroupsTotal,
|
||||
GroupsPurged: result.GroupsPurged,
|
||||
Merged: result.Merged,
|
||||
ArticlesUpdated: result.ArticlesUpdated,
|
||||
FailedGroups: result.FailedGroups,
|
||||
})
|
||||
}
|
||||
@@ -1,21 +1,78 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"RenewCMS/api/pkg/apperr"
|
||||
validator "RenewCMS/api/pkg/validator"
|
||||
"RenewCMS/internal/domain/audit"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
GetSettingsUseCase *useCases.GetSettingsUseCase
|
||||
GetSettingsUseCase *useCases.GetSettingsUseCase
|
||||
GetActivityRetentionUseCase *useCases.GetActivityRetentionUseCase
|
||||
UpdateActivityRetentionUseCase *useCases.UpdateActivityRetentionUseCase
|
||||
}
|
||||
|
||||
func NewHandler(getSettings *useCases.GetSettingsUseCase) *Handler {
|
||||
return &Handler{GetSettingsUseCase: getSettings}
|
||||
type UpdateSettingsRequest struct {
|
||||
ActivityRetentionDays *int `json:"activity_retention_days" validate:"required,oneof=0 30 90 365"`
|
||||
}
|
||||
|
||||
type settingsResponse struct {
|
||||
Version string `json:"version"`
|
||||
ActivityRetentionDays int `json:"activity_retention_days"`
|
||||
ActivityLastCleanupAt *time.Time `json:"activity_last_cleanup_at"`
|
||||
ActivityLastCleanupRemoved int `json:"activity_last_cleanup_removed"`
|
||||
ActivityNextCleanupAt *time.Time `json:"activity_next_cleanup_at"`
|
||||
}
|
||||
|
||||
func NewHandler(getSettings *useCases.GetSettingsUseCase, getRetention *useCases.GetActivityRetentionUseCase, updateRetention *useCases.UpdateActivityRetentionUseCase) *Handler {
|
||||
return &Handler{GetSettingsUseCase: getSettings, GetActivityRetentionUseCase: getRetention, UpdateActivityRetentionUseCase: updateRetention}
|
||||
}
|
||||
|
||||
func (h *Handler) GetSettings(w http.ResponseWriter, _ *http.Request) {
|
||||
body, err := json.Marshal(h.GetSettingsUseCase.GetSettings())
|
||||
retention, err := h.GetActivityRetentionUseCase.GetActivityRetention()
|
||||
if err != nil {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
h.write(w, retention)
|
||||
}
|
||||
|
||||
func (h *Handler) UpdateSettings(w http.ResponseWriter, r *http.Request) {
|
||||
var request UpdateSettingsRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
http.Error(w, apperr.BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := validator.Validate.Struct(request); err != nil {
|
||||
http.Error(w, apperr.BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
retention, err := h.UpdateActivityRetentionUseCase.UpdateActivityRetention(r.Context(), *request.ActivityRetentionDays)
|
||||
if err != nil {
|
||||
if errors.Is(err, audit.ErrInvalidRetention) {
|
||||
http.Error(w, apperr.BodyErrorMessage, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
h.write(w, retention)
|
||||
}
|
||||
|
||||
func (h *Handler) write(w http.ResponseWriter, retention audit.Settings) {
|
||||
body, err := json.Marshal(settingsResponse{
|
||||
Version: h.GetSettingsUseCase.GetSettings().Version,
|
||||
ActivityRetentionDays: retention.RetentionDays,
|
||||
ActivityLastCleanupAt: retention.LastCleanupAt,
|
||||
ActivityLastCleanupRemoved: retention.LastCleanupRemoved,
|
||||
ActivityNextCleanupAt: retention.NextCleanupAt,
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package staticimages
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/image"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -24,13 +26,13 @@ var contentTypeExtensions = map[string]string{
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
uploadDir fs.FS
|
||||
openMedia *useCases.OpenMediaUseCase
|
||||
modTime time.Time
|
||||
}
|
||||
|
||||
func NewHandler(uploadDir string) *Handler {
|
||||
func NewHandler(openMedia *useCases.OpenMediaUseCase) *Handler {
|
||||
return &Handler{
|
||||
uploadDir: os.DirFS(uploadDir),
|
||||
openMedia: openMedia,
|
||||
modTime: time.Now(),
|
||||
}
|
||||
}
|
||||
@@ -39,18 +41,16 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
|
||||
cleaned := cleanPath(requestedPath(r))
|
||||
file, stat, ok := h.openFile(cleaned)
|
||||
if !ok {
|
||||
blob, err := h.openMedia.Open(cleaned)
|
||||
if errors.Is(err, image.ErrBlobNotFound) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
content, ok := file.(io.ReadSeeker)
|
||||
if !ok {
|
||||
http.Error(w, "File not seekable", http.StatusInternalServerError)
|
||||
if err != nil {
|
||||
http.Error(w, "Unable to read the file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer blob.Content.Close()
|
||||
|
||||
contentType := contentTypeFor(cleaned)
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
@@ -59,7 +59,18 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Security-Policy", svgContentSecurityPolicy)
|
||||
}
|
||||
|
||||
http.ServeContent(w, r, cleaned, stat.ModTime(), content)
|
||||
if content, ok := blob.Content.(io.ReadSeeker); ok {
|
||||
http.ServeContent(w, r, cleaned, blob.ModTime, content)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(blob.Size, 10))
|
||||
if !blob.ModTime.IsZero() {
|
||||
w.Header().Set("Last-Modified", blob.ModTime.UTC().Format(http.TimeFormat))
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if r.Method != http.MethodHead {
|
||||
_, _ = io.Copy(w, blob.Content)
|
||||
}
|
||||
}
|
||||
|
||||
func requestedPath(r *http.Request) string {
|
||||
@@ -73,22 +84,6 @@ func cleanPath(p string) string {
|
||||
return strings.TrimPrefix(path.Clean("/"+p), "/")
|
||||
}
|
||||
|
||||
func (h *Handler) openFile(p string) (fs.File, fs.FileInfo, bool) {
|
||||
if p == "" || p == "." {
|
||||
return nil, nil, false
|
||||
}
|
||||
file, err := h.uploadDir.Open(p)
|
||||
if err != nil {
|
||||
return nil, nil, false
|
||||
}
|
||||
stat, err := file.Stat()
|
||||
if err != nil || stat.IsDir() {
|
||||
file.Close()
|
||||
return nil, nil, false
|
||||
}
|
||||
return file, stat, true
|
||||
}
|
||||
|
||||
func contentTypeFor(p string) string {
|
||||
ext := path.Ext(p)
|
||||
if contentType, ok := contentTypeExtensions[ext]; ok {
|
||||
|
||||
@@ -47,7 +47,7 @@ func (h *Handler) GetStatus(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (h *Handler) Check(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := h.CheckForUpdateUseCase.Check(); err != nil {
|
||||
if _, err := h.CheckForUpdateUseCase.Check(r.Context()); err != nil {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -58,8 +58,8 @@ func (h *Handler) Check(w http.ResponseWriter, r *http.Request) {
|
||||
writeStatus(w, http.StatusOK, h.GetUpdateStatusUseCase.GetStatus(isOwner))
|
||||
}
|
||||
|
||||
func (h *Handler) Download(w http.ResponseWriter, _ *http.Request) {
|
||||
status, err := h.StartUpdateDownloadUseCase.Start()
|
||||
func (h *Handler) Download(w http.ResponseWriter, r *http.Request) {
|
||||
status, err := h.StartUpdateDownloadUseCase.Start(r.Context())
|
||||
if err != nil {
|
||||
writeDownloadError(w, err)
|
||||
return
|
||||
@@ -77,7 +77,7 @@ func (h *Handler) Restart(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, apperr.UnauthorizedErrorMessage, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
status, err := h.RequestRestartUseCase.Request(userId, kind)
|
||||
status, err := h.RequestRestartUseCase.Request(r.Context(), userId, kind)
|
||||
if err != nil {
|
||||
writeRestartError(w, err)
|
||||
return
|
||||
|
||||
@@ -82,7 +82,7 @@ func (h *InviteHandler) CreateInvite(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
created, err := h.CreateInviteUseCase.CreateInvite(useCases.CreateInviteCommand{
|
||||
created, err := h.CreateInviteUseCase.CreateInvite(r.Context(), useCases.CreateInviteCommand{
|
||||
Email: request.Email,
|
||||
InvitedBy: userId,
|
||||
})
|
||||
@@ -124,7 +124,7 @@ func (h *InviteHandler) CancelInvite(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.CancelInviteUseCase.CancelInvite(id); err != nil {
|
||||
if err := h.CancelInviteUseCase.CancelInvite(r.Context(), id); err != nil {
|
||||
writeManageInviteError(w, err)
|
||||
return
|
||||
}
|
||||
@@ -144,7 +144,7 @@ func (h *InviteHandler) RenewInvite(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
renewed, err := h.RenewInviteUseCase.RenewInvite(useCases.RenewInviteCommand{ID: id, RenewedBy: userId})
|
||||
renewed, err := h.RenewInviteUseCase.RenewInvite(r.Context(), useCases.RenewInviteCommand{ID: id, RenewedBy: userId})
|
||||
if err != nil {
|
||||
writeManageInviteError(w, err)
|
||||
return
|
||||
|
||||
@@ -46,7 +46,7 @@ func (h *Handler) UpdateMe(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
updatedUser, err := h.UpdateProfileUseCase.UpdateDisplayName(userId, *request.DisplayName)
|
||||
updatedUser, err := h.UpdateProfileUseCase.UpdateDisplayName(r.Context(), userId, *request.DisplayName)
|
||||
if err != nil {
|
||||
if errors.Is(err, user.ErrInvalidDisplayName) {
|
||||
http.Error(w, apperr.DisplayNameErrorMessage, http.StatusBadRequest)
|
||||
|
||||
@@ -19,6 +19,8 @@ const bearerScheme = "bearer "
|
||||
|
||||
type apiKeyIdContextKey struct{}
|
||||
|
||||
type apiKeyNameContextKey struct{}
|
||||
|
||||
func bearerAPIKey(r *http.Request) (string, bool) {
|
||||
header := r.Header.Get("Authorization")
|
||||
if len(header) < len(bearerScheme) || !strings.EqualFold(header[:len(bearerScheme)], bearerScheme) {
|
||||
@@ -34,6 +36,11 @@ func apiKeyIdFrom(r *http.Request) (uint32, bool) {
|
||||
return id, ok
|
||||
}
|
||||
|
||||
func apiKeyNameFrom(r *http.Request) string {
|
||||
name, _ := r.Context().Value(apiKeyNameContextKey{}).(string)
|
||||
return name
|
||||
}
|
||||
|
||||
func RequireSession(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, isKey := bearerAPIKey(r); isKey {
|
||||
@@ -70,6 +77,7 @@ func APIKeyAuth(authenticate *useCases.AuthenticateAPIKeyUseCase, maxRequests in
|
||||
|
||||
ctx := jwtauth.NewContext(r.Context(), token, nil)
|
||||
ctx = context.WithValue(ctx, apiKeyIdContextKey{}, authenticated.Key.ID)
|
||||
ctx = context.WithValue(ctx, apiKeyNameContextKey{}, authenticated.Key.Name)
|
||||
limited.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"RenewCMS/api/pkg/claims"
|
||||
"RenewCMS/internal/domain/audit"
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func AuditContext(getUser *useCases.GetUserUseCase, trustedProxies []string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
origin := audit.Origin{
|
||||
Actor: auditActor(getUser, r),
|
||||
Source: auditSource(r),
|
||||
IP: clientIP(r, trustedProxies),
|
||||
UserAgent: truncatedUserAgent(r),
|
||||
}
|
||||
next.ServeHTTP(w, r.WithContext(audit.WithOrigin(r.Context(), origin)))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func auditActor(getUser *useCases.GetUserUseCase, r *http.Request) audit.Actor {
|
||||
if id, ok := apiKeyIdFrom(r); ok {
|
||||
return audit.APIKeyActor(id, apiKeyNameFrom(r))
|
||||
}
|
||||
userId, ok := claims.UserId(r)
|
||||
if !ok {
|
||||
return audit.AnonymousActor("")
|
||||
}
|
||||
current, err := getUser.GetUser(userId)
|
||||
if err != nil {
|
||||
return audit.AnonymousActor("")
|
||||
}
|
||||
tokenVersion, ok := claims.TokenVersion(r)
|
||||
if !ok || tokenVersion != current.TokenVersion {
|
||||
return audit.AnonymousActor("")
|
||||
}
|
||||
return audit.UserActor(current)
|
||||
}
|
||||
|
||||
func auditSource(r *http.Request) audit.Source {
|
||||
if _, ok := apiKeyIdFrom(r); ok {
|
||||
return audit.SourceAPIKey
|
||||
}
|
||||
return audit.SourceWeb
|
||||
}
|
||||
|
||||
func clientIP(r *http.Request, trustedProxies []string) string {
|
||||
realIP := r.Header.Get("X-Real-IP")
|
||||
if realIP != "" && isTrustedProxy(r.RemoteAddr, trustedProxies) {
|
||||
return realIP
|
||||
}
|
||||
return hostOf(r.RemoteAddr)
|
||||
}
|
||||
|
||||
func truncatedUserAgent(r *http.Request) string {
|
||||
runes := []rune(r.Header.Get("User-Agent"))
|
||||
if len(runes) > audit.MaxUserAgentLength {
|
||||
runes = runes[:audit.MaxUserAgentLength]
|
||||
}
|
||||
return string(runes)
|
||||
}
|
||||
@@ -95,3 +95,21 @@ func RequireOwner(getUser *useCases.GetUserUseCase) func(http.Handler) http.Hand
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func RequireOwnerOrAdmin(getUser *useCases.GetUserUseCase) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
currentUser, ok := resolveCurrentUser(getUser, w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if currentUser.Role != user.RoleOwner && currentUser.Role != user.RoleAdmin {
|
||||
http.Error(w, apperr.OwnerOrAdminRequiredErrorMessage, http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"RenewCMS/api/pkg/apperr"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
"github.com/lestrrat-go/jwx/v2/jwt"
|
||||
)
|
||||
|
||||
func RequireSignedIn(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
token, _, err := jwtauth.FromContext(r.Context())
|
||||
if err != nil || token == nil || jwt.Validate(token) != nil {
|
||||
http.Error(w, apperr.UnauthorizedErrorMessage, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
const contentSecurityPolicy = "default-src 'self'; " +
|
||||
"script-src 'self' 'wasm-unsafe-eval'; " +
|
||||
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; " +
|
||||
"font-src 'self' https://fonts.gstatic.com; " +
|
||||
"img-src 'self' data: blob: https:; " +
|
||||
"connect-src 'self'; " +
|
||||
"object-src 'none'; " +
|
||||
"base-uri 'self'; " +
|
||||
"form-action 'self'; " +
|
||||
"frame-ancestors 'none'"
|
||||
|
||||
const strictTransportSecurity = "max-age=31536000"
|
||||
|
||||
func SecurityHeaders(hsts bool) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
h := w.Header()
|
||||
h.Set("Content-Security-Policy", contentSecurityPolicy)
|
||||
h.Set("X-Content-Type-Options", "nosniff")
|
||||
h.Set("X-Frame-Options", "DENY")
|
||||
h.Set("Referrer-Policy", "strict-origin-when-cross-origin")
|
||||
h.Set("Cross-Origin-Opener-Policy", "same-origin")
|
||||
if hsts {
|
||||
h.Set("Strict-Transport-Security", strictTransportSecurity)
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -38,3 +38,7 @@ const ArticleBodyFormatErrorMessage = "The format must be one of: markdown, html
|
||||
const APIKeyNotFoundErrorMessage = "API key not found"
|
||||
const APIKeyExpiryErrorMessage = "The expiry must be a date in the future."
|
||||
const SessionRequiredErrorMessage = "This action needs a real session."
|
||||
const OwnerOrAdminRequiredErrorMessage = "Only owners and admins can do this."
|
||||
const PurgeConflictErrorMessage = "The library changed, so nothing was purged. Check the new summary and try again."
|
||||
const PurgeBusyErrorMessage = "A purge is already running."
|
||||
const PurgeIntervalErrorMessage = "Enter a whole number of days between 1 and 365."
|
||||
+66
-15
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"RenewCMS/api/handlers/activity"
|
||||
"RenewCMS/api/handlers/apikey"
|
||||
"RenewCMS/api/handlers/article"
|
||||
"RenewCMS/api/handlers/auth"
|
||||
@@ -46,9 +47,10 @@ func (c *UseCases) apiKeyAuth() func(http.Handler) http.Handler {
|
||||
return c.apiKeyAuthMiddleware
|
||||
}
|
||||
|
||||
func NewArticleRouter(container *UseCases) http.Handler {
|
||||
func NewArticleRouter(container *UseCases, trustedProxies []string) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Use(container.apiKeyAuth())
|
||||
r.Use(middleware.AuditContext(container.GetUserUseCase, trustedProxies))
|
||||
|
||||
articleHandler := article.NewHandler(container.CreateArticleUseCase, container.GetArticleUseCase, container.ListArticlesUseCase, container.DeleteArticleUseCase, container.UpdateArticleUseCase, container.GetUserUseCase)
|
||||
|
||||
@@ -71,6 +73,8 @@ func NewArticleRouter(container *UseCases) http.Handler {
|
||||
|
||||
func NewAuthRouter(container *UseCases, isProduction bool, rateLimit config.AuthRateLimitSettings, trustedProxies []string) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Use(jwtauth.Verifier(container.Token))
|
||||
r.Use(middleware.AuditContext(container.GetUserUseCase, trustedProxies))
|
||||
|
||||
loginHandler := auth.NewLoginHandler(container.Token, container.CreateUserUseCase, container.AuthenticateUserUseCase, isProduction)
|
||||
verificationHandler := auth.NewVerificationHandler(container.VerifyUserUseCase, container.CancelRegistrationUseCase, isProduction)
|
||||
@@ -124,6 +128,7 @@ func NewUserRouter(container *UseCases, rateLimit config.AuthRateLimitSettings,
|
||||
|
||||
r.Use(jwtauth.Verifier(container.Token))
|
||||
r.Use(container.apiKeyAuth())
|
||||
r.Use(middleware.AuditContext(container.GetUserUseCase, trustedProxies))
|
||||
r.Use(jwtauth.Authenticator(container.Token))
|
||||
r.Use(middleware.RequireCurrentToken(container.GetUserUseCase))
|
||||
r.Use(middleware.RequireVerifiedUser(container.GetUserUseCase))
|
||||
@@ -140,17 +145,51 @@ func NewUserRouter(container *UseCases, rateLimit config.AuthRateLimitSettings,
|
||||
return r
|
||||
}
|
||||
|
||||
func NewSettingsRouter(container *UseCases) http.Handler {
|
||||
func NewSettingsRouter(container *UseCases, trustedProxies []string) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
settingsHandler := settings.NewHandler(container.GetSettingsUseCase)
|
||||
settingsHandler := settings.NewHandler(container.GetSettingsUseCase, container.GetActivityRetentionUseCase, container.UpdateActivityRetentionUseCase)
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(jwtauth.Verifier(container.Token))
|
||||
r.Use(container.apiKeyAuth())
|
||||
r.Use(jwtauth.Authenticator(container.Token))
|
||||
r.Use(middleware.RequireCurrentToken(container.GetUserUseCase))
|
||||
r.Use(middleware.RequireVerifiedUser(container.GetUserUseCase))
|
||||
r.Get("/", settingsHandler.GetSettings)
|
||||
})
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(middleware.RequireSession)
|
||||
r.Use(jwtauth.Verifier(container.Token))
|
||||
r.Use(middleware.AuditContext(container.GetUserUseCase, trustedProxies))
|
||||
r.Use(middleware.RequireSignedIn)
|
||||
r.Use(jwtauth.Authenticator(container.Token))
|
||||
r.Use(middleware.RequireCurrentToken(container.GetUserUseCase))
|
||||
r.Use(middleware.RequireVerifiedUser(container.GetUserUseCase))
|
||||
r.Use(middleware.RequireOwner(container.GetUserUseCase))
|
||||
r.Patch("/", settingsHandler.UpdateSettings)
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
func NewActivityRouter(container *UseCases, trustedProxies []string) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
activityHandler := activity.NewHandler(container.ListActivityUseCase, container.GetActivitySummaryUseCase, container.ExportActivityUseCase)
|
||||
|
||||
r.Use(middleware.RequireSession)
|
||||
r.Use(jwtauth.Verifier(container.Token))
|
||||
r.Use(container.apiKeyAuth())
|
||||
r.Use(middleware.AuditContext(container.GetUserUseCase, trustedProxies))
|
||||
r.Use(middleware.RequireSignedIn)
|
||||
r.Use(jwtauth.Authenticator(container.Token))
|
||||
r.Use(middleware.RequireCurrentToken(container.GetUserUseCase))
|
||||
r.Use(middleware.RequireVerifiedUser(container.GetUserUseCase))
|
||||
r.Get("/", settingsHandler.GetSettings)
|
||||
r.Get("/", activityHandler.List)
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(middleware.RequireOwnerOrAdmin(container.GetUserUseCase))
|
||||
r.Get("/summary", activityHandler.Summary)
|
||||
r.Get("/export", activityHandler.Export)
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -161,13 +200,14 @@ const (
|
||||
updateDownloadWindow = time.Minute
|
||||
)
|
||||
|
||||
func NewSystemRouter(container *UseCases) http.Handler {
|
||||
func NewSystemRouter(container *UseCases, trustedProxies []string) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
updateHandler := update.NewHandler(container.GetUpdateStatusUseCase, container.CheckForUpdateUseCase, container.StartUpdateDownloadUseCase, container.RequestRestartUseCase, container.GetUserUseCase)
|
||||
|
||||
r.Use(middleware.RequireSession)
|
||||
r.Use(jwtauth.Verifier(container.Token))
|
||||
r.Use(middleware.AuditContext(container.GetUserUseCase, trustedProxies))
|
||||
r.Use(jwtauth.Authenticator(container.Token))
|
||||
r.Use(middleware.RequireCurrentToken(container.GetUserUseCase))
|
||||
r.Use(middleware.RequireVerifiedUser(container.GetUserUseCase))
|
||||
@@ -189,13 +229,14 @@ func NewSystemRouter(container *UseCases) http.Handler {
|
||||
return r
|
||||
}
|
||||
|
||||
func NewAPIKeyRouter(container *UseCases) http.Handler {
|
||||
func NewAPIKeyRouter(container *UseCases, trustedProxies []string) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
apiKeyHandler := apikey.NewHandler(container.CreateAPIKeyUseCase, container.ListAPIKeysUseCase, container.RevokeAPIKeyUseCase)
|
||||
|
||||
r.Use(middleware.RequireSession)
|
||||
r.Use(jwtauth.Verifier(container.Token))
|
||||
r.Use(middleware.AuditContext(container.GetUserUseCase, trustedProxies))
|
||||
r.Use(jwtauth.Authenticator(container.Token))
|
||||
r.Use(middleware.RequireCurrentToken(container.GetUserUseCase))
|
||||
r.Use(middleware.RequireVerifiedUser(container.GetUserUseCase))
|
||||
@@ -205,16 +246,24 @@ func NewAPIKeyRouter(container *UseCases) http.Handler {
|
||||
return r
|
||||
}
|
||||
|
||||
func NewMediaRouter(container *UseCases) http.Handler {
|
||||
func NewMediaRouter(container *UseCases, trustedProxies []string) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
mediaHandler := media.NewHandler(container.CreateImageUseCase, container.ListMediaUseCase, container.RenameMediaUseCase, container.DeleteMediaUseCase, container.MergeMediaUseCase, container.GetMediaUsageUseCase)
|
||||
mediaHandler := media.NewHandler(container.CreateImageUseCase, container.ListMediaUseCase, container.RenameMediaUseCase, container.DeleteMediaUseCase, container.MergeMediaUseCase, container.GetMediaUsageUseCase, container.GetPurgeSettingsUseCase, container.UpdatePurgeSettingsUseCase, container.PreviewPurgeUseCase, container.RunPurgeUseCase)
|
||||
|
||||
r.Use(jwtauth.Verifier(container.Token))
|
||||
r.Use(container.apiKeyAuth())
|
||||
r.Use(middleware.AuditContext(container.GetUserUseCase, trustedProxies))
|
||||
r.Use(jwtauth.Authenticator(container.Token))
|
||||
r.Use(middleware.RequireCurrentToken(container.GetUserUseCase))
|
||||
r.Use(middleware.RequireVerifiedUser(container.GetUserUseCase))
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(middleware.RequireOwnerOrAdmin(container.GetUserUseCase))
|
||||
r.Get("/purge-settings", mediaHandler.GetPurgeSettings)
|
||||
r.Patch("/purge-settings", mediaHandler.UpdatePurgeSettings)
|
||||
r.Get("/purge-preview", mediaHandler.PreviewPurge)
|
||||
r.Post("/purge", mediaHandler.RunPurge)
|
||||
})
|
||||
r.Get("/", mediaHandler.List)
|
||||
r.Post("/", mediaHandler.Upload)
|
||||
r.Post("/merge", mediaHandler.Merge)
|
||||
@@ -234,13 +283,14 @@ func InitBackendRoutes(container *UseCases, settings config.Settings) *chi.Mux {
|
||||
r.Get("/", GetHelloWorld)
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(jwtauth.Verifier(container.Token))
|
||||
r.Mount("/article", NewArticleRouter(container))
|
||||
r.Mount("/article", NewArticleRouter(container, settings.TrustedProxies))
|
||||
})
|
||||
r.Mount("/user", NewUserRouter(container, settings.AuthRateLimit, settings.TrustedProxies))
|
||||
r.Mount("/settings", NewSettingsRouter(container))
|
||||
r.Mount("/media", NewMediaRouter(container))
|
||||
r.Mount("/api-key", NewAPIKeyRouter(container))
|
||||
r.Mount("/system", NewSystemRouter(container))
|
||||
r.Mount("/settings", NewSettingsRouter(container, settings.TrustedProxies))
|
||||
r.Mount("/activity", NewActivityRouter(container, settings.TrustedProxies))
|
||||
r.Mount("/media", NewMediaRouter(container, settings.TrustedProxies))
|
||||
r.Mount("/api-key", NewAPIKeyRouter(container, settings.TrustedProxies))
|
||||
r.Mount("/system", NewSystemRouter(container, settings.TrustedProxies))
|
||||
r.Mount("/auth", NewAuthRouter(container, settings.IsProduction(), settings.AuthRateLimit, settings.TrustedProxies))
|
||||
|
||||
return r
|
||||
@@ -280,11 +330,12 @@ func InitRoutes(container *UseCases, settings config.Settings) *chi.Mux {
|
||||
frontend := InitFrontendRoutes(settings.Environment)
|
||||
|
||||
apiRouter := chi.NewRouter()
|
||||
apiRouter.Use(middleware.SecurityHeaders(settings.IsProduction()))
|
||||
enableCorsIfConfigured(apiRouter, settings.CorsAllowedOrigins)
|
||||
apiRouter.Use(chimiddleware.GetHead)
|
||||
apiRouter.Get("/healthz", GetHealth)
|
||||
apiRouter.Mount("/v1", backend)
|
||||
apiRouter.Mount("/static/uploadedImages", staticimages.NewHandler(settings.UploadDir))
|
||||
apiRouter.Mount("/static/uploadedImages", staticimages.NewHandler(container.OpenMediaUseCase))
|
||||
apiRouter.Mount("/", frontend)
|
||||
|
||||
return apiRouter
|
||||
|
||||
+31
-7
@@ -12,6 +12,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"slices"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -44,11 +45,22 @@ var envDefaults = map[string]string{
|
||||
"ENVIRONMENT": "production",
|
||||
"PORT": "8080",
|
||||
"DB_FILE": "./data/renewcms.db",
|
||||
config.EnvDatabaseDriver: config.DefaultDatabaseDriver,
|
||||
"DB_TLS": config.DefaultDatabaseTLS,
|
||||
"DB_SSLMODE": config.DefaultDatabaseSSLMode,
|
||||
"DB_MAX_OPEN_CONNS": config.DefaultMaxOpenConns,
|
||||
"DB_MAX_IDLE_CONNS": config.DefaultMaxIdleConns,
|
||||
"DB_CONN_MAX_LIFETIME_SECONDS": config.DefaultConnMaxLifetimeSeconds,
|
||||
"UPLOAD_DIR": "./data/uploads",
|
||||
config.EnvStorageDriver: config.DefaultStorageDriver,
|
||||
"S3_REGION": config.DefaultS3Region,
|
||||
"S3_PATH_STYLE": config.DefaultS3PathStyle,
|
||||
"S3_TLS": config.DefaultS3TLS,
|
||||
"AUTH_RATE_LIMIT_MAX_ATTEMPTS": "5",
|
||||
"AUTH_RATE_LIMIT_WINDOW_SECONDS": "60",
|
||||
"TRUSTED_PROXIES": "",
|
||||
"UPDATE_FEED_URL": "https://git.floriansylvain.fr",
|
||||
"RENEWCMS_CONTAINER": "false",
|
||||
}
|
||||
|
||||
const versionFlag = "--version"
|
||||
@@ -80,7 +92,7 @@ func run() error {
|
||||
validator.InitValidator()
|
||||
|
||||
server := newServer(api.InitRoutes(container, settings), settings)
|
||||
return serveUntilStopped(server, settings, container.RecordRunningInfoUseCase.Record)
|
||||
return serveUntilStopped(server, settings, container.RecordRunningInfoUseCase.Record, container.RunSchedulers)
|
||||
}
|
||||
|
||||
func initEnvVariables() {
|
||||
@@ -107,10 +119,13 @@ func applyEnvDefaults() {
|
||||
}
|
||||
|
||||
func ensureRequiredEnvVars() {
|
||||
for _, envVar := range requiredEnvVars {
|
||||
if _, isSet := os.LookupEnv(envVar); !isSet {
|
||||
panic(fmt.Sprintf("Environment variable %s is not set", envVar))
|
||||
}
|
||||
required := slices.Concat(
|
||||
requiredEnvVars,
|
||||
config.DatabaseRequiredEnvVars(os.Getenv(config.EnvDatabaseDriver)),
|
||||
config.StorageRequiredEnvVars(os.Getenv(config.EnvStorageDriver)),
|
||||
)
|
||||
if err := config.MissingEnvVarError(required, os.LookupEnv); err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,9 +146,18 @@ func newServer(handler http.Handler, settings config.Settings) *http.Server {
|
||||
}
|
||||
}
|
||||
|
||||
func serveUntilStopped(server *http.Server, settings config.Settings, recordRunning func() error) error {
|
||||
func serveUntilStopped(server *http.Server, settings config.Settings, recordRunning func() error, runScheduler func(context.Context)) error {
|
||||
stopSignal, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
schedulerDone := make(chan struct{})
|
||||
go func() {
|
||||
defer close(schedulerDone)
|
||||
runScheduler(stopSignal)
|
||||
}()
|
||||
defer func() {
|
||||
stop()
|
||||
<-schedulerDone
|
||||
}()
|
||||
|
||||
serverErr := make(chan error, 1)
|
||||
go listen(server, serverErr, settings, recordRunning)
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
services:
|
||||
renewcms:
|
||||
build:
|
||||
context: .
|
||||
args:
|
||||
VERSION: ${VERSION:-dev}
|
||||
image: renewcms:${VERSION:-dev}
|
||||
@@ -0,0 +1,51 @@
|
||||
services:
|
||||
mysql:
|
||||
image: mysql:8.4
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:?set MYSQL_ROOT_PASSWORD}
|
||||
MYSQL_DATABASE: renewcms
|
||||
MYSQL_USER: renewcms
|
||||
MYSQL_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}
|
||||
volumes:
|
||||
- mysql-data:/var/lib/mysql
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -u root -p\"$$MYSQL_ROOT_PASSWORD\" --silent"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 10
|
||||
start_period: 30s
|
||||
|
||||
renewcms:
|
||||
image: ${RENEWCMS_IMAGE:-git.floriansylvain.fr/fsylvain/renewcms:beta}
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
mysql:
|
||||
condition: service_healthy
|
||||
env_file:
|
||||
- docker.env
|
||||
environment:
|
||||
DB_DRIVER: mysql
|
||||
DB_HOST: mysql
|
||||
DB_PORT: "3306"
|
||||
DB_NAME: renewcms
|
||||
DB_USER: renewcms
|
||||
DB_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}
|
||||
DATA_DIR: /data
|
||||
UPLOAD_DIR: /data/uploads
|
||||
RENEWCMS_CONTAINER: "true"
|
||||
ports:
|
||||
- "8080:8080"
|
||||
volumes:
|
||||
- renewcms-data:/data
|
||||
stop_grace_period: 15s
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8080/healthz"]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 10s
|
||||
|
||||
volumes:
|
||||
renewcms-data:
|
||||
mysql-data:
|
||||
@@ -0,0 +1,51 @@
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_DB: renewcms
|
||||
POSTGRES_USER: renewcms
|
||||
POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}
|
||||
volumes:
|
||||
- postgres-data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U renewcms -d renewcms"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 10
|
||||
start_period: 30s
|
||||
|
||||
renewcms:
|
||||
image: ${RENEWCMS_IMAGE:-git.floriansylvain.fr/fsylvain/renewcms:beta}
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
env_file:
|
||||
- docker.env
|
||||
environment:
|
||||
DB_DRIVER: postgres
|
||||
DB_HOST: postgres
|
||||
DB_PORT: "5432"
|
||||
DB_NAME: renewcms
|
||||
DB_USER: renewcms
|
||||
DB_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD}
|
||||
DB_SSLMODE: disable
|
||||
DATA_DIR: /data
|
||||
UPLOAD_DIR: /data/uploads
|
||||
RENEWCMS_CONTAINER: "true"
|
||||
ports:
|
||||
- "8080:8080"
|
||||
volumes:
|
||||
- renewcms-data:/data
|
||||
stop_grace_period: 15s
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8080/healthz"]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 10s
|
||||
|
||||
volumes:
|
||||
renewcms-data:
|
||||
postgres-data:
|
||||
@@ -0,0 +1,56 @@
|
||||
services:
|
||||
seaweedfs:
|
||||
image: ${SEAWEEDFS_IMAGE:-chrislusf/seaweedfs:4.48}
|
||||
restart: unless-stopped
|
||||
command: ["server", "-dir=/data", "-s3", "-s3.port=8333"]
|
||||
environment:
|
||||
AWS_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-renewcms}
|
||||
AWS_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?set S3_SECRET_ACCESS_KEY}
|
||||
volumes:
|
||||
- seaweedfs-data:/data
|
||||
|
||||
seaweedfs-init:
|
||||
image: ${SEAWEEDFS_IMAGE:-chrislusf/seaweedfs:4.48}
|
||||
restart: "no"
|
||||
depends_on:
|
||||
- seaweedfs
|
||||
entrypoint: ["sh", "-c"]
|
||||
command:
|
||||
- |
|
||||
until echo "s3.bucket.create -name renewcms-media" | timeout 30 weed shell -master=seaweedfs:9333 && echo "s3.bucket.list" | timeout 30 weed shell -master=seaweedfs:9333 | grep -q renewcms-media; do
|
||||
sleep 2
|
||||
done
|
||||
|
||||
renewcms:
|
||||
image: ${RENEWCMS_IMAGE:-git.floriansylvain.fr/fsylvain/renewcms:beta}
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
seaweedfs-init:
|
||||
condition: service_completed_successfully
|
||||
env_file:
|
||||
- docker.env
|
||||
environment:
|
||||
STORAGE_DRIVER: s3
|
||||
S3_ENDPOINT: http://seaweedfs:8333
|
||||
S3_BUCKET: renewcms-media
|
||||
S3_REGION: us-east-1
|
||||
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-renewcms}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?set S3_SECRET_ACCESS_KEY}
|
||||
S3_PATH_STYLE: "true"
|
||||
DB_FILE: /data/renewcms.db
|
||||
RENEWCMS_CONTAINER: "true"
|
||||
ports:
|
||||
- "8080:8080"
|
||||
volumes:
|
||||
- renewcms-data:/data
|
||||
stop_grace_period: 15s
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8080/healthz"]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 10s
|
||||
|
||||
volumes:
|
||||
renewcms-data:
|
||||
seaweedfs-data:
|
||||
@@ -0,0 +1,24 @@
|
||||
services:
|
||||
renewcms:
|
||||
image: ${RENEWCMS_IMAGE:-git.floriansylvain.fr/fsylvain/renewcms:beta}
|
||||
restart: unless-stopped
|
||||
env_file:
|
||||
- docker.env
|
||||
environment:
|
||||
DB_FILE: /data/renewcms.db
|
||||
UPLOAD_DIR: /data/uploads
|
||||
RENEWCMS_CONTAINER: "true"
|
||||
ports:
|
||||
- "8080:8080"
|
||||
volumes:
|
||||
- renewcms-data:/data
|
||||
stop_grace_period: 15s
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-qO-", "http://127.0.0.1:8080/healthz"]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 10s
|
||||
|
||||
volumes:
|
||||
renewcms-data:
|
||||
@@ -0,0 +1,34 @@
|
||||
PUBLIC_URL=https://cms.example.com
|
||||
JWT_SECRET=replace-with-output-of-openssl-rand-hex-32
|
||||
SMTP_HOST=smtp.example.com
|
||||
SMTP_PORT=465
|
||||
SMTP_LOGIN=noreply@example.com
|
||||
SMTP_PASSWORD=replace-me
|
||||
SMTP_SENDER=noreply@example.com
|
||||
|
||||
ENVIRONMENT=production
|
||||
CORS_ALLOWED_ORIGINS=
|
||||
AUTH_RATE_LIMIT_MAX_ATTEMPTS=5
|
||||
AUTH_RATE_LIMIT_WINDOW_SECONDS=60
|
||||
TRUSTED_PROXIES=
|
||||
DB_DRIVER=sqlite
|
||||
DATA_DIR=
|
||||
DB_HOST=
|
||||
DB_PORT=
|
||||
DB_USER=
|
||||
DB_PASSWORD=
|
||||
DB_NAME=
|
||||
DB_TLS=false
|
||||
DB_SSLMODE=disable
|
||||
DB_MAX_OPEN_CONNS=10
|
||||
DB_MAX_IDLE_CONNS=5
|
||||
DB_CONN_MAX_LIFETIME_SECONDS=300
|
||||
STORAGE_DRIVER=local
|
||||
S3_BUCKET=
|
||||
S3_REGION=us-east-1
|
||||
S3_ENDPOINT=
|
||||
S3_ACCESS_KEY_ID=
|
||||
S3_SECRET_ACCESS_KEY=
|
||||
S3_KEY_PREFIX=
|
||||
S3_PATH_STYLE=false
|
||||
S3_TLS=true
|
||||
+56
@@ -27,6 +27,62 @@ for example `https://cms.example/v1/article/`.
|
||||
| Users and invites | GET /v1/user/, PATCH /v1/user/me, POST /v1/user/invite (owner only), DELETE /v1/user/invite/{id} (owner only), POST /v1/user/invite/{id}/renew (owner only), GET /v1/auth/me |
|
||||
| Settings | GET /v1/settings |
|
||||
|
||||
## Activity log
|
||||
The activity routes are session only: a key answers 403 "This action needs a real session." and a
|
||||
key never reads the audit trail. Events are append-only; there is no `POST`, `PUT`, `PATCH` or
|
||||
`DELETE` under `/v1/activity`.
|
||||
|
||||
| Route | Who | Purpose |
|
||||
| --- | --- | --- |
|
||||
| `GET /v1/activity` | Any verified user | Filtered, paged events. Owner and admin see every event; any other role is scoped to its own events, and `actor` set to someone else answers 403 "Only owners and admins can do this." |
|
||||
| `GET /v1/activity/summary` | Owner, admin | Counts for the range, the previous period, active people and seven-bucket series. |
|
||||
| `GET /v1/activity/export` | Owner, admin | The same filters as the list without `page` or `size`, as a `text/csv; charset=utf-8` attachment (`when,actor,action,target,outcome,source,ip`). Cells starting with `=`, `+`, `-` or `@` get a leading quote. |
|
||||
|
||||
Query: `q`, `actor` (`user:ID`, `key:ID`, `system`), `category` (`articles`, `media`, `users`,
|
||||
`api_keys`, `auth`, `system`), `outcome` (`all`, `success`, `failed`), `security` (`true`), `range`
|
||||
(`today`, `7d`, `30d`, `custom`; default `7d`), `from` and `to` (dates, with `custom`), `page` (from
|
||||
1) and `size` (25, 50 or 100). Any invalid value answers 400 with the body-mismatch message. Dates
|
||||
are UTC. The list answers `{ items, total, page, size }`, newest first (`created_at`, then `id`).
|
||||
|
||||
`GET /v1/settings` also returns `activity_retention_days` (30, 90, 365 or 0 for forever, default
|
||||
90), `activity_last_cleanup_at` and `activity_last_cleanup_removed`. `PATCH /v1/settings` with
|
||||
`{"activity_retention_days": 30}` is session only and owner only (403 "Only the owner can do this.");
|
||||
any other value answers 400.
|
||||
|
||||
### What is recorded
|
||||
Every real action is recorded by its use case, once, after the change was made (outcome `success`)
|
||||
or after the use case refused it (outcome `failed`, with a plain `failure_reason`). Nothing is
|
||||
recorded for a read (no `GET`, no list, no public article read, no status poll), for a request that
|
||||
fails validation, or for a target that does not exist (404). A failed audit write is logged and
|
||||
never changes the response or the change itself.
|
||||
|
||||
| Category | Actions |
|
||||
| --- | --- |
|
||||
| `articles` | `article.create`, `article.edit`, `article.publish`, `article.unpublish`, `article.delete` |
|
||||
| `media` | `media.upload`, `media.rename`, `media.delete`, `media.merge`, `media.purge` |
|
||||
| `users` | `user.register`, `user.verify`, `user.update_profile`, `invite.create`, `invite.cancel`, `invite.renew`, `invite.accept` |
|
||||
| `system` | `system.media_purge` (scheduled), `system.update_checked`, `system.update_downloaded`, `system.restart_requested`, `system.activity_cleanup`, `system.activity_retention_changed` |
|
||||
|
||||
Target types: `article`, `image`, `user`, `invite`, `api_key` and `release` (id and name are the tag).
|
||||
Only the fields `title`, `online`, `body`, `filename` and `display_name` appear in `changes`, with the
|
||||
kinds `text` and `body`; a body edit carries a signed character count delta and never the text. A
|
||||
change that leaves a value as it was (same body, same publish state, same file name, same display
|
||||
name) records nothing. Params: `media.merge` and `media.purge` and `system.media_purge` carry
|
||||
`merged` and `articles_updated`, plus `failed_groups` when a group failed (still outcome `success`);
|
||||
`system.update_checked` carries `update_available`; `system.restart_requested` carries `kind` and,
|
||||
for an update, `tag` (it has no target). `system.update_downloaded` is recorded when the job reaches
|
||||
`ready` or `failed`, with the origin captured at request time; a refusal at the start is recorded
|
||||
immediately. A failed registration or invite acceptance has an `anonymous` actor named by the
|
||||
attempted username.
|
||||
|
||||
`failure_reason` is one fixed sentence per domain error, matched with `errors.Is`, at most 255
|
||||
characters, for example "The title must be between 3 and 200 characters.", "That file name is not
|
||||
valid.", "The merge request is not valid.", "A purge is already running.", "Registration is closed
|
||||
on this site.", "That invitation has expired.", "The release feed could not be reached." or "The
|
||||
update could not be downloaded.". Any other error becomes "Something went wrong on our side. Please
|
||||
try again."; the text of the error never reaches the event, the API or the CSV. Passwords, hashes,
|
||||
codes, article bodies, file bytes, storage paths and asset urls are never recorded.
|
||||
|
||||
Article routes accept `?format=markdown|html|json` (default `markdown`). `GET /v1/media/` accepts
|
||||
`q`, `hash`, `cursor`, `filter`, `sort` and `limit`.
|
||||
|
||||
|
||||
+74
-2
@@ -15,8 +15,10 @@ api/ ──▶ internal/infrastructure/useCases ──▶ internal/domain
|
||||
| Domain | `internal/domain/<name>/` | Entities, `Err…` sentinel errors, `Repository` interfaces. Imports only the stdlib and other domain packages, with no GORM tags. |
|
||||
| Use cases | `internal/infrastructure/useCases/` | Application logic. Depends only on domain interfaces, never on persistence, mailer, GORM or `net/http`. |
|
||||
| Adapters | `internal/infrastructure/persistence/{models,mappers,repositories}`, `internal/infrastructure/mailer` | Implement a domain `Repository` (assert with `var _ domain.Repository = &X{}`). GORM structs stay in `models` and are converted by `mappers`. |
|
||||
| Database drivers | `internal/infrastructure/persistence/database` | `database.Connect(Config{Driver, Source})` looks the driver up in a registry and returns a `Connection` (GORM handle, `NewBackup`, `Close`). A `Driver` implements `Open` and `NewBackup`, so a new database is one new file plus one registry entry. `database.Migrate` owns the `AutoMigrate` model list and the user-role backfill. Only `sqlite` exists today. |
|
||||
| Blob storage | `domain/image.Storage`, `internal/infrastructure/storage` | `Storage` (`Put`, `Open`, `Delete`) is the port for uploaded image bytes. `storage.Open(Config{Driver, Location})` picks the adapter; `local` is the only one. Repositories never touch files: use cases coordinate `Storage` and `image.Repository`. |
|
||||
| Delivery | `api/handlers/<name>/`, `api/router.go` | HTTP handlers receive use cases through `NewHandler(...)`. They never import persistence, mailer or GORM. |
|
||||
| Composition root | `api/dependecyInjection.go` | The only place that opens the DB, builds repositories and use cases, and lists models for `AutoMigrate`. |
|
||||
| Composition root | `api/dependecyInjection.go` | The only place that picks the database and storage drivers, builds repositories and use cases, and runs `database.Migrate` and the image metadata backfill at boot. |
|
||||
| Entry point | `cmd/server/main.go` | Env/config, HTTP server, graceful shutdown. |
|
||||
|
||||
Nothing under `internal/` imports `api/`.
|
||||
@@ -30,9 +32,46 @@ Nothing under `internal/` imports `api/`.
|
||||
| `api/pkg/upload` | Multipart `file` reading and the mapping of upload errors (415, 413, 400) shared by both upload routes. |
|
||||
| `api/pkg/publicuser` | `PublicUser`, the JSON shape of a user with no password, codes or token version. `publicuser.New` converts a domain `user.User`, so handlers never serialise the domain struct directly. |
|
||||
|
||||
### Media purge
|
||||
|
||||
`internal/domain/purge` holds `Settings` (automatic purge on or off, interval of 1 to 365 days, `last_purge_*` history, `enabled_at`), the `Repository` interface (`Get` returns the defaults when nothing was saved, `Save`, and `SaveLastRun`, which writes only the `last_purge_*` columns so a settings change made during a run is never overwritten) and the sentinels `ErrInvalidPurgeInterval`, `ErrPurgeConflict` (stale fingerprint) and `ErrPurgeBusy` (a run is already in progress). `Settings.NextPurgeAt` is `max(last_purge_at, enabled_at)` plus the interval when automatic purge is on, and null otherwise. The settings live in a single-row `purge_settings` table (`models.PurgeSettings`, `mappers.PurgeSettings*`, `persistence.PurgeSettingsRepository`), created by `AutoMigrate`.
|
||||
|
||||
The use cases `GetPurgeSettings`, `UpdatePurgeSettings`, `PreviewPurge` and `RunPurge` depend only on domain interfaces, an injected clock and an injected `*log.Logger`, so a scheduler can call them. `PreviewPurge` and `RunPurge` share one plan (`purgePlan.go`): a duplicate group (same non-empty sha256) is planned only when a copy is used in an article, the most used copy is kept (ties: oldest `created_at`, then lowest id) and every other copy is merged into it. The fingerprint is a SHA-256 over the planned groups. `RunPurge` takes a non-blocking in-process lock (`ErrPurgeBusy` for a concurrent call), refuses a non-matching fingerprint with `ErrPurgeConflict`, merges group by group with `MergeMedia` (a failing group is counted and skipped), unions the `UpdatedArticleIDs` of the fully merged groups, and records `last_purge_*` after every run.
|
||||
|
||||
#### Automatic purge scheduler
|
||||
|
||||
`PurgeSchedulerUseCase.Tick(now)` returns the `purge.RunResult` of the run, whether it ran and an error. It reads the purge settings on every call and runs `RunPurge.Run(nil)` (no fingerprint) when `Settings.NextPurgeAt()` is set and `now` is not before it. It never writes settings: only `RunPurge` records `last_purge_*`, through `SaveLastRun`, which moves the anchor forward even when nothing was purged. The runner in `internal/infrastructure/scheduler` calls `Tick` and logs one line with `groups_purged`, `merged`, `articles_updated` and `failed_groups` when a run happened. It calls `Tick` once after a startup delay of 1 minute, which catches up a run missed during downtime in a single purge, then once every hour. `api/dependecyInjection.go` builds it and `cmd/server/main.go` starts it with the signal context of the server. Changes to the settings apply on the next tick, with no restart.
|
||||
|
||||
- Overlap guard: a manual run holds the `RunPurge` lock, so `Tick` returns `ErrPurgeBusy` at once. The runner treats it as a skip, logs nothing and re-evaluates at the next tick.
|
||||
- Failures: any other error is logged and the loop keeps going. There is no retry inside a tick; the next hourly tick evaluates the schedule again.
|
||||
- Shutdown: on SIGINT or SIGTERM the runner stops taking ticks and waits, with no time limit, for a `Tick` in progress. `main` waits for the runner to return before the database is closed.
|
||||
|
||||
`middleware.RequireOwnerOrAdmin` lets only the roles owner and admin through (403 otherwise, including an empty role). The media router mounts these routes after the verified chain and before the `/{id}` routes:
|
||||
|
||||
| Route | Purpose |
|
||||
|---|---|
|
||||
| `GET /v1/media/purge-settings` | Settings with `next_purge_at`. |
|
||||
| `PATCH /v1/media/purge-settings` | Partial update of `auto_purge_enabled` and `auto_purge_interval_days`. |
|
||||
| `GET /v1/media/purge-preview` | Groups, copies, distinct articles, the first 5 groups, `more` and the fingerprint. |
|
||||
| `POST /v1/media/purge` | Runs the purge. Optional `fingerprint`; 409 when stale or when a run is in progress. |
|
||||
|
||||
### Activity log
|
||||
|
||||
`internal/domain/audit` holds the append-only `Event` (actor, target and failure-reason snapshots, outcome, security flag, source, ip, user agent, `params` and `changes`), the closed vocabularies (actor type, category, outcome, source, change kind), the `Filter`, `Summary` and `Settings` types, the `Repository` interface (`Append`, `List`, `Summary`, `DeleteOlderThan`: no way to edit or delete one event), the `SettingsRepository` (`Get`, `SaveRetention`, `SaveLastCleanup`; no row means 90 days) and the sentinels `ErrInvalidFilter`, `ErrInvalidRetention`, `ErrInvalidEvent` and `ErrActorNotAllowed`. The `Recorder` port is `Record(ctx, event)`; it returns nothing, so a caller can never fail on it.
|
||||
|
||||
`internal/infrastructure/auditing.Recorder` is the adapter. It fills a missing actor, source, ip and user agent from the `audit.Origin` carried by the request context, stamps `created_at` from the injected clock in UTC, bounds long names, drops an event outside the vocabulary, and recovers from any error or panic of the repository by writing one line to the process log (the action and the error only). `middleware.AuditContext` builds the origin after the credential middleware: a session gives a `user` actor and source `web`, an API key an `api_key` actor and source `api_key`, no valid credential an `anonymous` actor; the ip is the peer address, or `X-Real-IP` when the peer is a trusted proxy; the user agent is cut to 255 characters. Use cases that produce events (authenticate and logout, password reset request and completion, API key create and revoke, retention change, cleanup) take the context and an `audit.Recorder`; secrets (passwords, raw keys, reset codes, tokens and their hashes) are never put in an event or in its log line.
|
||||
|
||||
Persistence is `models.AuditEvent` (`audit_events`, indexes on `created_at`, `(actor_type, actor_id, created_at)` and `(category, created_at)`; `params` and `changes` are JSON text) and `models.AuditSettings` (one row), both in `AutoMigrate`. The queries use only portable SQL (`LOWER(...) LIKE ... ESCAPE '!'`, `COUNT(DISTINCT ...)`, `CASE` sums), so they run on SQLite, MySQL/MariaDB and PostgreSQL. The summary splits the range into seven equal buckets, oldest first.
|
||||
|
||||
The use cases `ListActivity`, `GetActivitySummary` and `ExportActivity` turn the raw query into a domain filter (`range` defaults to 7 days; `custom` needs two dates, `to` included), scope a caller who is not owner or admin to its own events, and resolve `target.exists` at read time. The export reads the log through `List` in pages of 500 and writes the CSV with `encoding/csv`. `/v1/activity` and the settings `PATCH` sit behind `RequireSession`, the verifier, the current-token and verified-user checks; summary and export also behind `RequireOwnerOrAdmin`, the settings `PATCH` behind `RequireOwner`.
|
||||
|
||||
`CleanupActivityUseCase.Tick(now)` follows the `PurgeScheduler` model. With a retention of 0 it never runs. Otherwise it is due once a day, at or after 03:00 in the location of `now` and when the last cleanup is before today's 03:00. It deletes events older than `now` minus the retention days (never newer than 24 hours), records the run (`activity_last_cleanup_at`, `activity_last_cleanup_removed`) even when nothing was removed, and only then records a `system.activity_cleanup` event when something was removed. A failing delete returns the error and leaves the last-cleanup fields as they were, so the next tick retries. `scheduler.Runner` now wraps a task with a label: `NewRunner` keeps the purge behaviour and log line, and `NewCleanupRunner` ticks the cleanup. `UseCases.RunSchedulers` runs both runners and `cmd/server/main.go` starts it with the server's signal context.
|
||||
|
||||
Producers: `ArticleChanges`, `TextChange` and `BodyChange` in `internal/domain/audit` build the change lists (the body change holds only a signed rune count delta), and `NewTarget` and `ReleaseTarget` build the target snapshots (types `article`, `image`, `user`, `invite`, `api_key`, `release`). The use cases create, edit, publish, unpublish and delete article, upload, rename, delete, merge and purge media, register, verify, update profile, create, cancel, renew and accept invite, check, download and restart take the request context first and an `audit.Recorder` last, and record exactly one event after the change or the refusal (the action catalogue is in `docs/api.md`). Events are never written by handlers, middleware or GORM hooks, and nothing is recorded for reads or for a target that does not exist. `useCases/auditFailures.go` holds the fixed table of failure sentences keyed by domain `Err...` sentinels (matched with `errors.Is`, each at most 255 characters; the restart action overrides `ErrJobInProgress`) with the generic fallback "Something went wrong on our side. Please try again."; raw error text is never stored. The manual purge runs the groups through the non-recording `MergeMediaUseCase.merge`, so only one `media.purge` event is written; `RunPurgeUseCase.Execute` is the non-recording run that `PurgeSchedulerUseCase.Tick(now)` uses to record one `system.media_purge` event with the system actor and `context.Background()`. `StartUpdateDownloadUseCase` hands `context.WithoutCancel(ctx)` to its job goroutine so the `system.update_downloaded` event, written when the job becomes `ready` or `failed`, keeps the origin of the request. `middleware.AuditContext` is mounted on the article, user, media and system routers (after the API key middleware where keys are accepted), so API key calls are attributed to the key.
|
||||
|
||||
### Image metadata backfill
|
||||
|
||||
At boot, `BackfillImageMetadata` fills `filename`, `size`, `content_type`, `width`, `height` and `sha256` for image rows that lack them, reading the files in `UPLOAD_DIR`. It is idempotent, and a row whose file is missing or unreadable is left as it is without stopping the boot. The old `article_id` column and the `article_images` table are kept and never written.
|
||||
At boot, `BackfillImageMetadataUseCase` (through `image.Repository.ListMissingMetadata` and `UpdateMetadata`) fills `filename`, `size`, `content_type`, `width`, `height` and `sha256` for image rows that lack them, reading the bytes through `image.Storage` (the files in `UPLOAD_DIR` with the local adapter, the bucket objects with the S3 adapter). It is idempotent, and a row whose file is missing or unreadable is left as it is without stopping the boot. The old `article_id` column and the `article_images` table are kept and never written.
|
||||
|
||||
## Frontend ↔ backend
|
||||
|
||||
@@ -50,3 +89,36 @@ bin/renewcms ── /v1/* API
|
||||
|
||||
- **Production:** a single origin. The SPA calls the API with relative URLs, so the JWT cookie works without CORS. Files under `assets/` are content-hashed by Vite and cached for a year. `index.html` is never cached.
|
||||
- **Development:** built with `-tags nofrontend` (`assets_stub.go`, `assets.Dist == nil`). Vite serves the SPA on :5173 and proxies `/v1` to the API on :8080. See [development.md](development.md).
|
||||
|
||||
## Storage and database seams
|
||||
|
||||
Uploaded images are addressed by a stored name (`<uuid>.<ext>`). `CreateImageUseCase` calls `Storage.Put` (which generates the name), then `Repository.Create` with `Path = /static/uploadedImages/<name>`, and deletes the blob again if the row cannot be created. `DeleteMediaUseCase` and `MergeMediaUseCase` remove the blob first and the row second (`mediaRemover`), so a failed blob delete keeps the row and the delete can be retried. `GET /static/uploadedImages/<name>` is served by `OpenMediaUseCase` through `api/handlers/staticimages`, which uses `http.ServeContent` when the blob is seekable and streams it otherwise; `ErrBlobNotFound` maps to 404 and any other error to 500.
|
||||
|
||||
SQL in repositories must run on SQLite, MySQL/MariaDB and PostgreSQL: substring searches use `LIKE ... ESCAPE '!'` through `containsPattern` instead of `instr`. `Settings.DataDir()` is where update state, staging and backups live; it is `DATA_DIR` when set and the directory of `DB_FILE` otherwise, so it does not depend on where the database is.
|
||||
|
||||
Three drivers are registered in `database.drivers`: `sqlite` (default), `mysql` (MySQL 8 and MariaDB 11, through `gorm.io/driver/mysql`) and `postgres` (PostgreSQL 16 and 17, alias `postgresql`, through `gorm.io/driver/postgres` on pgx v5); all are pure Go so `CGO_ENABLED=0` and the six release targets still build. `DB_DRIVER` picks one in `databaseConfig` (composition root); an unknown name fails in `database.Connect` with `unsupported database driver "<name>"`. `database.MySQLDSN` and `database.PostgresDSN` build the DSN from separate variables (the PostgreSQL one as an escaped URL with `sslmode`, `timezone=UTC` and a connect timeout), and the driver redacts the password from every error and names only host and port. `Connect` applies the optional `PoolSettings` to any driver. What keeps the repositories identical on both engines, all without touching the domain, use cases or handlers:
|
||||
|
||||
- Indexed and unique strings carry `size:` tags in the models (a MySQL index needs a bounded `varchar`). SQLite maps them to the same `text` column, so an existing SQLite file migrates with no schema or row change (`test/sqlite_schema_unchanged_test.go` builds a file from the old model definitions and compares `sqlite_master` and every row).
|
||||
- `Migrate` creates MySQL tables with `utf8mb4_bin` (`gorm:table_options`), so `Bob` and `bob` are distinct like in SQLite. The DSN also sets `collation=utf8mb4_bin`.
|
||||
- The DSN sets `clientFoundRows=true`: an update that matches a row but changes nothing still reports one affected row, as in SQLite. `InviteRepository.Renew`, `deleteOrNotFound` and `PurgeSettingsRepository.SaveLastRun` depend on that.
|
||||
- The Go driver sends a zero `time.Time` as `0000-00-00`, which MySQL's default `NO_ZERO_DATE` rejects, so each session sets `sql_mode` to the server default minus `NO_ZERO_DATE`. Reading `0000-00-00` gives the zero time back.
|
||||
- `UserRepository.Create` returns the filled model instead of re-reading it, because MySQL has no `RETURNING`.
|
||||
|
||||
On PostgreSQL the MySQL-only table options are not applied, pgx returns `timestamptz` values in the local zone so a query callback in the driver converts them to UTC, and only the `public` schema is used.
|
||||
|
||||
Backups are a `domain/update.DatabaseBackup` per driver. SQLite uses `VACUUM INTO`. MySQL uses `DumpBackup`, a pure Go logical dump (`SHOW CREATE TABLE` plus every row, strings escaped and binary values as hex, NULL and zero dates kept) written inside one repeatable-read transaction to `DATA_DIR/backups/renewcms-<time>.sql.partial` and renamed once complete. Any failure removes the partial file and returns an error, so the update download fails instead of claiming a backup. PostgreSQL reuses `DumpBackup` with its own `dumpDialect` (double-quoted identifiers, doubled single quotes, `\x` bytea literals): `CREATE TABLE` is rebuilt from `pg_catalog` (serial columns, defaults, primary key, unique and check constraints), followed by the indexes, the rows and a `setval` per `id` sequence, all inside one repeatable-read read-only transaction. All drivers keep the newest 3 files (`pruneBackups`).
|
||||
|
||||
`task test` runs everything that needs no server. The repository contract tests (`test/repository_contract_test.go`) run on SQLite always, and on MySQL when `RENEWCMS_TEST_MYSQL_DSN` points at a server (admin DSN; each test creates and drops its own database). `task test:mysql` starts `mysql:8.4` and then `mariadb:11` in Docker and runs the `MySQL` tests against each. The same contract tests run on PostgreSQL when `RENEWCMS_TEST_POSTGRES_DSN` points at a server (admin DSN, for example `postgres://postgres:pw@127.0.0.1:5432/postgres?sslmode=disable`), and `task test:postgres` starts `postgres:16` then `postgres:17` in Docker and runs the `Postgres` tests against each.
|
||||
|
||||
Two storage adapters implement `image.Storage`: `local` (default, files in `UPLOAD_DIR`) and `s3` (`storage.S3Storage`, `aws-sdk-go-v2` over plain HTTPS, pure Go so `CGO_ENABLED=0` still holds). `STORAGE_DRIVER` picks one in `imageStorageConfig` (composition root, from `config.StorageSettings`); `storage.Open` fails with `unsupported storage driver "<name>"` for anything else, and `openImageStorage` panics with the error. The S3 adapter works as follows, with no change to the domain, use cases, repositories or the `staticimages` handler:
|
||||
|
||||
- `Open` calls `GetObject` and returns a `Blob` whose `Content` is an `io.ReadSeeker` (`s3Reader`): `Size` and `ModTime` come from `ContentLength` and `LastModified`, the first bytes are read from that response and every `Seek` to another offset makes the next `Read` issue a ranged `GetObject`. `http.ServeContent` therefore answers `Range` (206, 416) and `If-Modified-Since` (304) with no `ETag`. A 404 maps to `image.ErrBlobNotFound`; any other failure is a plain error (500 in the handler).
|
||||
- `Put` does one `PutObject` of `<S3_KEY_PREFIX><uuid>.<ext>` with the upload's `Content-Type`. `Delete` treats 404 as success. Names that are not plain file names are never requested, as in the local adapter.
|
||||
- Each call has a 30 second timeout to get a response (the body then streams without a deadline) and the SDK default retries. Request checksums are only sent when required, so older S3-compatible servers work.
|
||||
- `storage.Open` checks the bucket with `HeadBucket`. The failure text names the bucket and the reason (404, 403, other status or transport error) and never the credentials.
|
||||
- At boot, `RunBackfillReportingReads` wraps the storage in a counter for the `BackfillImageMetadataUseCase` run and logs how many images it read; the use case itself is unchanged.
|
||||
- Update backups (`DATA_DIR`) never include uploads, whatever the driver.
|
||||
|
||||
`task test:s3` starts SeaweedFS in Docker and runs `TestS3StorageIntegration` against it (skipped without Docker, and not part of `task test`). Everything else uses an in-process fake S3 server listening on `127.0.0.1`.
|
||||
|
||||
Adding a database driver: implement `database.Driver` (`Open`, and `NewBackup` returning a `domain/update.DatabaseBackup`), register it in `drivers`, then select it in `databaseConfig` in the composition root (and add the env vars to `main.go`, `.env.example` and `configuration.md`). Adding a storage backend: implement `image.Storage`, add a case to `storage.Open`, and select it in `openImageStorage`. No use case, handler or repository changes.
|
||||
+75
-4
@@ -24,22 +24,93 @@ Copy [`.env.example`](../.env.example) to `.env` to get started. When a required
|
||||
|------------------------|----------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `ENVIRONMENT` | `production` | `production` or `development`. In `production` the auth cookie is `Secure`, so it is only sent over HTTPS. `development` turns on HTTP request logging. |
|
||||
| `PORT` | `8080` | Port the HTTP server listens on (all interfaces). |
|
||||
| `DB_FILE` | `./data/renewcms.db` | Path to the SQLite database file. Missing parent directories are created. The schema is migrated automatically at startup. |
|
||||
| `UPLOAD_DIR` | `./data/uploads` | Directory where uploaded images are stored. |
|
||||
| `DB_DRIVER` | `sqlite` | `sqlite`, `mysql` (MySQL and MariaDB) or `postgres` (alias `postgresql`). Any other value stops the server with `unsupported database driver "<value>"`. See [Database](#database). |
|
||||
| `DB_FILE` | `./data/renewcms.db` | Path to the SQLite database file. Missing parent directories are created. The schema is migrated automatically at startup. Ignored when `DB_DRIVER` is `mysql` or `postgres`. |
|
||||
| `DATA_DIR` | directory of `DB_FILE` | Directory for update state, staged updates and database backups (`backups/`). Set it with `DB_DRIVER=mysql` or `postgres`, where there is no database file to sit beside (the default still follows `DB_FILE`). With SQLite, leave it unset to keep everything beside the database. |
|
||||
| `STORAGE_DRIVER` | `local` | `local` (files in `UPLOAD_DIR`) or `s3` (an S3-compatible bucket). Any other value stops the server with `unsupported storage driver "<value>"`. See [Image storage](#image-storage). |
|
||||
| `UPLOAD_DIR` | `./data/uploads` | Directory where uploaded images are stored. Ignored, without error, when `STORAGE_DRIVER=s3`. |
|
||||
| `CORS_ALLOWED_ORIGINS` | *(empty)* | Semicolon-separated list of origins allowed to call the API. When empty, no CORS headers are sent. That is the right setting whenever the SPA is served by RenewCMS itself (production) or through the Vite proxy (development). |
|
||||
| `AUTH_RATE_LIMIT_MAX_ATTEMPTS` | `5` | Maximum number of requests a single client IP may send to `POST /v1/auth/login`, `POST /v1/auth/password-reset` or the invite-accept pair (`GET /v1/auth/invite` and `POST /v1/auth/invite/accept`, sharing one counter) within the window, tracked independently per route. |
|
||||
| `AUTH_RATE_LIMIT_WINDOW_SECONDS` | `60` | Length, in seconds, of the fixed rate-limit window for the routes above. |
|
||||
| `TRUSTED_PROXIES` | *(empty)* | Semicolon-separated list of exact IP addresses (no CIDR) of reverse proxies allowed to supply the client IP via `X-Real-IP` for the rate limiting of the routes above. When a request's connection address is not in this list, any `X-Real-IP` or `X-Forwarded-For` header it sends is ignored and the connection address is used instead. See [deployment.md](deployment.md). |
|
||||
| `UPDATE_FEED_URL` | `https://git.floriansylvain.fr` | Base URL of the Gitea server that publishes RenewCMS releases. The Settings page asks `{UPDATE_FEED_URL}/api/v1/repos/fsylvain/RenewCMS/releases` when the owner checks for updates. The last result is kept in `update-status.json`, next to `DB_FILE`. |
|
||||
| `RENEWCMS_CONTAINER` | `false` | Set to `true` (case-insensitive; any other value means `false`) when the app runs in a container. Settings then tells the owner to update by pulling a new image instead of offering browser updates, and `can_apply` is always `false`. The Dockerfile and `docker-compose.yml` set it to `true`. Update checks still work. |
|
||||
| `UPDATE_FEED_URL` | `https://git.floriansylvain.fr` | Base URL of the Gitea server that publishes RenewCMS releases. The Settings page asks `{UPDATE_FEED_URL}/api/v1/repos/fsylvain/RenewCMS/releases` when the owner checks for updates. The last result is kept in `update-status.json`, in the data directory. |
|
||||
|
||||
Automatic media purge has no environment variable. Its schedule (on or off, interval, `enabled_at`, `last_purge_at`) lives in the purge settings row of the database, is changed from the Settings page or `PATCH /v1/media/purge-settings`, and is read again at every hourly tick.
|
||||
|
||||
## Image storage
|
||||
|
||||
By default uploaded images are files in `UPLOAD_DIR`. To keep them in an S3-compatible bucket (AWS S3, MinIO, Ceph, Cloudflare R2, Backblaze B2 and similar), set `STORAGE_DRIVER=s3` and the variables below. They are only read when `STORAGE_DRIVER=s3`. Image URLs do not change: the app still serves `/static/uploadedImages/<name>`, reading the object from the bucket (the bucket stays private, and `Range` and `If-Modified-Since` requests keep working; there is no `ETag`).
|
||||
|
||||
| Name | Default | Description |
|
||||
|------------------------|-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `S3_BUCKET` | required | Name of an existing bucket. A missing value stops the server with `Environment variable S3_BUCKET is not set`. |
|
||||
| `S3_REGION` | `us-east-1` | Bucket region. Most S3-compatible servers accept the default. |
|
||||
| `S3_ENDPOINT` | *(empty)* | Host (with optional port) or full URL of an S3-compatible server. Leave empty for AWS S3. |
|
||||
| `S3_ACCESS_KEY_ID` | *(empty)* | Access key. Set it together with `S3_SECRET_ACCESS_KEY`, or set neither. Setting only one stops the server with an error naming the missing one. |
|
||||
| `S3_SECRET_ACCESS_KEY` | *(empty)* | Secret key. Never logged and never in an error message. When both keys are empty, the default AWS credential chain is used (`AWS_*` variables, shared credentials file, instance or task role). |
|
||||
| `S3_KEY_PREFIX` | *(empty)* | Prefix put before every object key, for example `uploads/`. Include the trailing slash. |
|
||||
| `S3_PATH_STYLE` | `false` | `true` addresses the bucket as `endpoint/bucket` instead of `bucket.endpoint`. SeaweedFS, MinIO and most self-hosted servers need `true`. |
|
||||
| `S3_TLS` | `true` | Only used when `S3_ENDPOINT` has no `http://` or `https://` prefix: `true` means `https`, `false` means `http`. |
|
||||
|
||||
At startup the server checks the bucket (`HeadBucket`). A missing bucket, refused credentials or an unreachable server stop startup with `Unable to open the image storage: S3 bucket "<name>" is not usable: <reason>`; the error never contains the secret. Each S3 call has a 30 second timeout and the SDK default retries. At boot, images whose metadata is missing are read back from the bucket to fill it; when there were some, one log line says how many.
|
||||
|
||||
### Moving existing uploads to a bucket
|
||||
|
||||
There is no automatic migration and the database does not change: image rows only hold the stored name, so the same names must exist in the bucket. With the site stopped, copy the files under the same names, under `S3_KEY_PREFIX` if you set one, then set `STORAGE_DRIVER=s3` and restart:
|
||||
|
||||
```sh
|
||||
aws s3 sync ./data/uploads s3://my-bucket/uploads/
|
||||
rclone sync ./data/uploads remote:my-bucket/uploads
|
||||
```
|
||||
|
||||
Keep `UPLOAD_DIR` until you have checked the site. Moving back is the same copy in the other direction.
|
||||
|
||||
### Backups
|
||||
|
||||
The pre-update backup covers the database only (and `DATA_DIR` state). Neither the update backup nor the data directory includes uploads, so back the bucket up with your provider (versioning, replication) or with `aws s3 sync` / `mc mirror`.
|
||||
|
||||
## Database
|
||||
|
||||
SQLite is the default and needs nothing but `DB_FILE`. To run against MySQL 8 or MariaDB 11 instead, set `DB_DRIVER=mysql` and the connection variables below. They are only read (and the four required ones only checked) when `DB_DRIVER` is `mysql` or `postgres`. A required variable that is not set stops the server with `Environment variable DB_HOST is not set` (or `DB_USER`, `DB_PASSWORD`, `DB_NAME`).
|
||||
|
||||
| Name | Default | Description |
|
||||
|--------------------------------|----------|----------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `DB_HOST` | required | Server host name or IP address. |
|
||||
| `DB_PORT` | `3306` (`5432` for PostgreSQL) | Server port. |
|
||||
| `DB_USER` | required | Database user. It needs full rights on `DB_NAME`, because the schema is created and migrated at startup. |
|
||||
| `DB_PASSWORD` | required | Database password. It is never logged and never appears in an error message. |
|
||||
| `DB_NAME` | required | Name of an existing database. The server does not create it. |
|
||||
| `DB_TLS` | `false` | `false`, `true`, `skip-verify` or `preferred`. Any other value stops the server with an error naming `DB_TLS` and these values. `true` verifies the server certificate; `skip-verify` encrypts without verifying it. |
|
||||
| `DB_SSLMODE` | `disable` | PostgreSQL only: `disable`, `require`, `verify-ca` or `verify-full`. Any other value stops the server with an error naming `DB_SSLMODE` and these values. Ignored for SQLite and MySQL (use `DB_TLS` there). |
|
||||
| `DB_MAX_OPEN_CONNS` | `10` | Maximum open connections in the pool. `0` means unlimited. |
|
||||
| `DB_MAX_IDLE_CONNS` | `5` | Maximum idle connections kept in the pool. |
|
||||
| `DB_CONN_MAX_LIFETIME_SECONDS` | `300` | Seconds after which a connection is closed and replaced. `0` keeps connections forever. Keep it below the server's `wait_timeout`. |
|
||||
|
||||
A non numeric (or negative) pool value stops the server with an error naming the variable. There is no DSN variable: the connection string is built from the variables above, with `parseTime=true`, `loc=UTC`, `charset=utf8mb4`, `collation=utf8mb4_bin` and `clientFoundRows=true`. Connecting uses a 5 second timeout, so a wrong host, port, password or database name stops startup quickly with `Unable to open the database`, naming the host and port but never the password.
|
||||
|
||||
### PostgreSQL
|
||||
|
||||
Set `DB_DRIVER=postgres` (or `postgresql`) and the same connection variables, with `DB_PORT` defaulting to `5432` and `DB_SSLMODE` instead of `DB_TLS`. PostgreSQL 16 and 17 are tested. The database must already exist and `DB_USER` needs full rights on the `public` schema, which is the only schema used. The connection string is built with escaping from the variables (a password may contain spaces, quotes, `@` or `/`), sets the session time zone to UTC and a 5 second connect timeout, and the pool variables above apply. A failure to connect stops startup with `Unable to open the database`, naming the host and port but never the password. Usernames and emails compare case-sensitively, as on SQLite. Before an update download RenewCMS writes a logical dump to `DATA_DIR/backups/renewcms-<time>.sql` (the newest 3 are kept); replay it into an empty database with `psql -f`. For routine backups prefer `pg_dump`.
|
||||
|
||||
### MySQL settings
|
||||
|
||||
Why those settings: tables are created with the `utf8mb4_bin` collation so that usernames and emails compare case-sensitively exactly like in SQLite (`Bob` and `bob` are different users). The collation of `DB_NAME` itself does not matter, but tables that already exist keep the collation they have. `clientFoundRows` makes an update that does not change a row still count as matched, which the invite and purge-settings code relies on. Each session also drops the server's `NO_ZERO_DATE` SQL mode, because the Go driver sends a zero time as `0000-00-00`. The other strict modes stay on.
|
||||
|
||||
With MySQL the data directory (`DATA_DIR`) holds update state and backups only. The pre-update backup is a `renewcms-<time>.sql` logical dump (schema and rows, written by RenewCMS itself, no `mysqldump` needed) that keeps the newest 3. If the dump fails, the update download fails with it. The dump holds your data, such as password hashes, but not the connection password.
|
||||
|
||||
Relative paths are resolved against the process's working directory. In production, use absolute paths (see [deployment.md](deployment.md)).
|
||||
|
||||
For example, `WorkingDirectory=/var/lib/renewcms` with `DB_FILE=./data/renewcms.db` puts the database in `/var/lib/renewcms/data`. Browser-update `--data-dir` must name that directory, because the app reads helper status and update records beside the database. It is not necessarily `WorkingDirectory` or `ReadWritePaths`. Do not change an existing `DB_FILE` merely to match the helper: that could open a different, empty database. See [browser-update setup](deployment.md#browser-updates).
|
||||
|
||||
## Activity log retention
|
||||
|
||||
The activity log needs no environment variable. Its retention period (30, 90, 365 days or forever, 90 by default) is a setting the owner changes with `PATCH /v1/settings`, and the daily cleanup runs at 03:00 in the server's time zone (`TZ`, or the host's zone).
|
||||
|
||||
## Adding a variable
|
||||
|
||||
1. Read it in the code where it is needed (infrastructure or `api/`, never `internal/domain`).
|
||||
2. Register it in [`cmd/server/main.go`](../cmd/server/main.go): add it to `requiredEnvVars` if it is required, or to `envDefaults` if it has a default.
|
||||
2. Register it in [`cmd/server/main.go`](../cmd/server/main.go): add it to `requiredEnvVars` if it is required (or to `DatabaseRequiredEnvVars` in `internal/infrastructure/config` if only one database driver needs it), or to `envDefaults` if it has a default.
|
||||
3. Add it to [`.env.example`](../.env.example) and to the tables above.
|
||||
|
||||
Never hardcode a secret, and never commit `.env`.
|
||||
+55
-2
@@ -8,7 +8,9 @@ RenewCMS ships as **one self-contained executable**. It serves:
|
||||
| `/healthz` | Liveness check, returns `200 ok` |
|
||||
| `/*` | The Vue SPA (embedded), with fallback to `index.html` for client-side routes |
|
||||
|
||||
Runtime state includes the SQLite database (`DB_FILE`), uploads (`UPLOAD_DIR`), and update records, staged binaries and backups beside the database. Running a release binary needs no Go, Bun or Node installation. Browser updates additionally require Linux with systemd and the helper setup below.
|
||||
Runtime state includes the SQLite database (`DB_FILE`, or a MySQL/MariaDB or PostgreSQL server, see [Using MySQL or MariaDB](#using-mysql-or-mariadb) and [Using PostgreSQL](#using-postgresql)), uploads (`UPLOAD_DIR`, or an S3-compatible bucket with `STORAGE_DRIVER=s3`, see [configuration.md](configuration.md#image-storage)), and update records, staged binaries and backups beside the database. Running a release binary needs no Go, Bun or Node installation. Browser updates additionally require Linux with systemd and the helper setup below.
|
||||
|
||||
To run it in a container instead, see [docker.md](docker.md). The published image `git.floriansylvain.fr/fsylvain/renewcms` is upgraded with `docker compose pull && docker compose up -d`. Tags are `vX.Y.Z`, `X.Y.Z`, `X.Y` and `latest` for a stable release, and `vX.Y.Z-<pre>`, `X.Y.Z-<pre>` and the moving `<pre-name>` (`beta`, `rc`) for a pre-release, which never moves `latest`. Maintainers publish with `task docker:publish` after `docker login git.floriansylvain.fr`; there is no CI. See [Maintainer: publishing](docker.md#maintainer-publishing).
|
||||
|
||||
## 1. Build
|
||||
|
||||
@@ -33,7 +35,7 @@ To check a download by hand: `go run ./cmd/releasesign verify checksums.txt`, wi
|
||||
|
||||
### Update checks
|
||||
|
||||
The owner's Settings page asks the release feed for the newest release (see `UPDATE_FEED_URL` in [configuration.md](configuration.md)) and keeps the last result in `update-status.json`, next to `DB_FILE`. Back it up with the rest of the data directory if you like; it is rebuilt by the next check when missing. The Settings page reports a newer version. To install it from the browser, set up [Browser updates](#browser-updates) once on the server.
|
||||
The owner's Settings page asks the release feed for the newest release (see `UPDATE_FEED_URL` in [configuration.md](configuration.md)) and keeps the last result in `update-status.json`, next to `DB_FILE`. Back it up with the rest of the data directory if you like; it is rebuilt by the next check when missing. The Settings page reports a newer version. To install it from the browser, set up [Browser updates](#browser-updates) once on the server. In Docker (`RENEWCMS_CONTAINER=true`) browser updates are not offered; Settings tells the owner to pull a new image instead, see [docker.md](docker.md).
|
||||
|
||||
### Update downloads
|
||||
|
||||
@@ -268,8 +270,59 @@ If RenewCMS runs on the same host as the proxy, consider firewalling port 8080.
|
||||
|
||||
Either way, set `TRUSTED_PROXIES` (see [configuration.md](configuration.md)) to the proxy's own address as seen by RenewCMS, typically `127.0.0.1` when the proxy and RenewCMS share a host, so the login/password-reset rate limiter honors the forwarded client IP instead of the proxy's own address.
|
||||
|
||||
### Security headers
|
||||
|
||||
RenewCMS sends `Content-Security-Policy` (including `frame-ancestors 'none'`), `X-Content-Type-Options`, `X-Frame-Options`, `Referrer-Policy` and `Cross-Origin-Opener-Policy` itself, so the proxy doesn't need to add them. With `ENVIRONMENT=production` it also sends `Strict-Transport-Security: max-age=31536000` (browsers ignore it over plain HTTP, so serve the site over HTTPS only). Don't add the same headers in the proxy, or they will be duplicated.
|
||||
|
||||
The CSP allows Google Fonts (`fonts.googleapis.com`, `fonts.gstatic.com`) and `https:` images, because the SPA loads its fonts from Google and articles may embed remote images. `style-src` keeps `'unsafe-inline'` for the article editor; `script-src` is `'self'` only.
|
||||
|
||||
## Using MySQL or MariaDB
|
||||
|
||||
SQLite is the default. To use an existing MySQL 8 or MariaDB 11 server instead, create an empty database and a user with full rights on it, then set in `/etc/renewcms/renewcms.env`:
|
||||
|
||||
```sh
|
||||
DB_DRIVER=mysql
|
||||
DB_HOST=db.example.com
|
||||
DB_USER=renewcms
|
||||
DB_PASSWORD=<generated password>
|
||||
DB_NAME=renewcms
|
||||
DB_TLS=true
|
||||
DATA_DIR=/var/lib/renewcms
|
||||
```
|
||||
|
||||
`DB_FILE` is then ignored. `DATA_DIR` is where update state, staged updates and backups live, so give it the same value as the helper's `--data-dir` (see [browser updates](#browser-updates)). Use `DB_TLS=true` (or `preferred`) whenever the server is not on the same host. Every variable is described in [configuration.md](configuration.md#database). The schema is created and migrated at startup, so the first start on an empty database needs nothing else.
|
||||
|
||||
## Using PostgreSQL
|
||||
|
||||
To use an existing PostgreSQL 16 or 17 server, create an empty database and a user with full rights on its `public` schema, then set in `/etc/renewcms/renewcms.env`:
|
||||
|
||||
```sh
|
||||
DB_DRIVER=postgres
|
||||
DB_HOST=db.example.com
|
||||
DB_USER=renewcms
|
||||
DB_PASSWORD=<generated password>
|
||||
DB_NAME=renewcms
|
||||
DB_SSLMODE=require
|
||||
DATA_DIR=/var/lib/renewcms
|
||||
```
|
||||
|
||||
`DB_FILE` is then ignored, and `DB_PORT` defaults to `5432`. `DATA_DIR` is where update state, staged updates and backups live, so give it the same value as the helper's `--data-dir`. Use `DB_SSLMODE=require` (or `verify-full`) whenever the server is not on the same host. Every variable is described in [configuration.md](configuration.md#database). The schema is created and migrated at startup. Moving an existing SQLite site is a manual procedure like the MySQL one below, and RenewCMS writes a logical `.sql` dump to `DATA_DIR/backups` before each update download.
|
||||
|
||||
### Moving an existing SQLite site to MySQL
|
||||
|
||||
There is no migration tool. The manual procedure, with the site stopped:
|
||||
|
||||
1. Take a backup of the data directory (database file and uploads).
|
||||
2. Start RenewCMS once on the empty MySQL database so it creates the schema, then stop it.
|
||||
3. Export the rows from the SQLite file with `sqlite3 renewcms.db .dump`, keep only the `INSERT INTO` statements for `articles`, `images`, `users`, `invites`, `api_keys` and `purge_settings`, and import them into MySQL. Quoting and date formats differ between the two engines, so check a sample first: SQLite writes times such as `2026-01-02 03:04:05.123456789+00:00`, and the `+00:00` suffix must be removed for MariaDB and older MySQL versions. An unset time (`0001-01-01 00:00:00`) is valid as it is in MySQL and is read back as unset.
|
||||
4. Copy `uploads/` to the new `UPLOAD_DIR`, set `DB_DRIVER=mysql` and the connection variables, and start the service.
|
||||
|
||||
Keep the SQLite file until you have checked the site.
|
||||
|
||||
## Backups
|
||||
|
||||
With MySQL, MariaDB or PostgreSQL, back the database up with your server's own tools (for example `mysqldump --single-transaction` or `pg_dump`), plus the uploads directory. RenewCMS also writes a logical dump to `DATA_DIR/backups` before each update download. The rest of this section is about SQLite.
|
||||
|
||||
Everything worth backing up is under the data directory. Take a consistent SQLite copy while the service is running:
|
||||
|
||||
```sh
|
||||
|
||||
@@ -33,15 +33,18 @@ The visual spec of record for the back-office SPA and the transactional emails,
|
||||
| [Media picker](screens/media-picker.md) | `/article/:id/edit` (dialog) | 13 | `source/RenewCMS Screens Back-office.dc.html` |
|
||||
| [Integration](screens/integration.md) | `/integration` | 8 | `source/RenewCMS Screens Back-office.dc.html` |
|
||||
| [API keys](screens/integration-api-keys.md) | `/integration/api-keys` | 12 | `source/RenewCMS Screens API keys.dc.html` |
|
||||
| [Activity](screens/activity.md) | `/activity` | 9 (plus 6 details, 3 My activity) | `source/RenewCMS Screens Activity.dc.html` |
|
||||
| [Users](screens/users.md) | `/users` | 17 | `source/RenewCMS Screens Back-office.dc.html` |
|
||||
| [Settings](screens/settings.md) | `/settings` | 25 | `source/RenewCMS Screens Back-office.dc.html` |
|
||||
| [Settings](screens/settings.md) | `/settings` | 25 | `source/RenewCMS Screens Back-office.dc.html`, `source/RenewCMS Screens Media cleanup.dc.html` |
|
||||
| [Profile](screens/profile.md) | `/profile` | 13 | `source/RenewCMS Screens Back-office.dc.html` |
|
||||
| [Accept invite](screens/accept-invite.md) | `/invite/accept?c=CODE` | 9 | `source/RenewCMS Screens Auth.dc.html` |
|
||||
| [Global](screens/global.md) | `any route` | 4 | `source/RenewCMS Screens Back-office.dc.html` |
|
||||
|
||||
## Other boards
|
||||
|
||||
- `source/ActivityFrame.dc.html`: the frame that draws every Activity state, the Home card and the Settings retention card (the Activity board imports it). Its copy, component and API tables are in `source/activity-spec.js`. It holds the exact values. Home and Settings are drawn around dashed placeholders.
|
||||
- `source/MediaFrame.dc.html`: the frame that draws the Media and Media picker states (the Back-office board imports it). Like `AppFrame.dc.html`, it holds the exact values.
|
||||
- `source/SettingsFrame.dc.html`: the Media cleanup card and its purge dialog (the Media cleanup board imports it, with `MediaFrame.dc.html` for the Duplicates toolbar button). They hold the exact values.
|
||||
- `source/IntegrationPage.dc.html` and `source/ApiKeysPage.dc.html`: the page components for the Integration and API keys screens (imported by `AppFrame.dc.html`). They hold the exact values.
|
||||
- `source/RenewCMS Prototype.dc.html`: the clickable flows (first run, edit and publish, password reset, delete, session expiry mid-edit).
|
||||
- `source/RenewCMS Handoff.dc.html`: the handoff page the first markdown here was generated from. It predates the Users and Profile screens and the invitation email, so it does not list them. Those are written by hand from the Back-office board, `AppFrame.dc.html`, the Prototype and `emails/invitation.html`.
|
||||
|
||||
@@ -814,3 +814,38 @@ Exact values live in `source/AppFrame.dc.html` (the Settings and Home states). C
|
||||
| RestartOverlay | custom full-page `role="status"` | title, text, startedAt, slow | reload | App shell |
|
||||
| UpdateBanner | UAlert | state: 'available'\|'ready' | go-to-settings, dismiss | Home (owner) |
|
||||
| UpdatePill | UBadge | none | none | DashboardShell Settings item (dot on mobile) |
|
||||
|
||||
## Added with Media cleanup
|
||||
### PurgeDuplicatesDialog
|
||||
Used by Settings ("Purge now") and Media, Duplicates tab ("Purge duplicates"). Built on `UModal`, `sm:max-w-md`, bottom sheet on mobile, cancel first, confirm is `color="error"`.
|
||||
- Props: `open`, `preview` (`groups`, `copies`, `articles`, `list[]`, `more`), `status` (`loading | ready | purging | nothing | error | partial | conflict`).
|
||||
- While `purging`: confirm shows `loader-circle` and reads "Purging…"; `dismissible` false, Cancel disabled.
|
||||
- Group row: 40×30 thumbnail, filename, "Kept · used in N articles", right-aligned "N copies merged". Max 5 rows, then "and N more groups".
|
||||
- Notices sit above the summary in a `UAlert` (soft): error for failure, warning for partial and conflict.
|
||||
- Emits `purged` (with counts). The parent shows the toast.
|
||||
|
||||
### MediaCleanupCard
|
||||
Settings card. `USwitch` (square, `rounded-none`), `UInputNumber` with "days" suffix, read-only lines, divider, outline button. Saves on change and on blur or Enter; shows "Saving…" then "Saved" in the header. Read-only mode disables controls, hides the button and shows an info `UAlert`.
|
||||
|
||||
### Media toolbar button
|
||||
Secondary `UButton` icon `merge`, label "Purge duplicates", after the sort menu on desktop; full width in the stacked toolbar on mobile (44 px).
|
||||
|
||||
## Added with Activity
|
||||
Source: `source/activity-spec.js` (component inventory) and `source/ActivityFrame.dc.html`. All built on existing Nuxt UI v4 components and tokens; no new token.
|
||||
|
||||
| Component | Nuxt UI base | Props | Emits | Slots | Used on |
|
||||
|---|---|---|---|---|---|
|
||||
| ActivityPage | custom page | none (reads the session role) | none | none | Activity |
|
||||
| ActivitySummaryStrip | UCard (as button) | summary, loading, activeFilter | apply-filter | none | Activity (owner, admin) |
|
||||
| ActivityFilterBar | UInput, USelectMenu, UButton, UBadge | filters, people, canFilterPeople | update:filters, clear | none | Activity |
|
||||
| ActivityTable | UTable (day separator rows), UAvatar, UBadge, UPagination, USelectMenu | items, total, page, size, loading | open, update:page, update:size | empty | Activity (md and up) |
|
||||
| ActivityCardList | UCard | same as ActivityTable | open | empty | Activity (below md) |
|
||||
| ActivityDetailsPanel | USlideover (right on desktop, bottom on mobile), UCard, UButton | item, index, total, open | previous, next, close | none | Activity |
|
||||
| ActivityDiff | custom list | changes | none | none | ActivityDetailsPanel |
|
||||
| ActivityFeedCard | UCard, UAvatar, UButton | items, loading, error, own | retry | none | Home |
|
||||
| RetentionCard | UCard, USelectMenu, UAlert, UButton | settings, canEdit | saved | none | Settings (owner) |
|
||||
|
||||
- Sparkline: seven `div` bars inside the strip card, no chart library. The strip cards are real `button`s with the card as their label.
|
||||
- Day separators are full-width rows of the table. The "When" cell carries the exact time as its `title`.
|
||||
- The sentence is composed from the `action` code and params with the i18n messages (English and French); the target name is user content and is not translated.
|
||||
- Failed and security rows use `--rc-error-soft` / `--ui-error` and `--rc-warning-soft` / `--ui-warning` for emphasis, always paired with an icon or a word, never colour alone.
|
||||
@@ -11,15 +11,16 @@ A global `router.beforeEach` guard reads `meta.auth`. On the first navigation it
|
||||
| `/register/validate` | AuthShell | public | VerifyPage | POST /v1/auth/verify (+ POST /login inline on 401) |
|
||||
| `/register/reset/request` | AuthShell | public | ResetRequestPage | POST /v1/auth/password-reset |
|
||||
| `/register/reset/validate` | AuthShell | public | ResetConfirmPage | POST /v1/auth/password-reset/confirm |
|
||||
| `/home` | DashboardShell | verified | HomePage | GET /v1/article/ · GET /v1/system/update (owner, for the banner) |
|
||||
| `/home` | DashboardShell | verified | HomePage | GET /v1/article/ · GET /v1/activity?size=5 · GET /v1/system/update (owner, for the banner) |
|
||||
| `/article` | DashboardShell | verified | ArticlesPage | GET /v1/article/ · PATCH /{id}/publish\|unpublish · DELETE /{id} |
|
||||
| `/article/new` | DashboardShell | verified | ArticlesPage + NewArticleModal | POST /v1/article/ |
|
||||
| `/article/:id/edit` | DashboardShell | verified | EditorPage | GET /{id} · PATCH /{id} · PATCH /{id}/publish\|unpublish · GET /v1/media/ · POST /v1/media/ · DELETE /{id} |
|
||||
| `/media` | DashboardShell | verified | MediaPage | GET /v1/media/ · POST /v1/media/ · PATCH /v1/media/{id} · DELETE /v1/media/{id} · POST /v1/media/merge · GET /v1/media/{id}/usage |
|
||||
| `/media` | DashboardShell | verified | MediaPage | GET /v1/media/purge-preview · POST /v1/media/purge · GET /v1/media/ · POST /v1/media/ · PATCH /v1/media/{id} · DELETE /v1/media/{id} · POST /v1/media/merge · GET /v1/media/{id}/usage |
|
||||
| `/activity` | DashboardShell | verified | ActivityPage + ActivityDetailsPanel | GET /v1/activity · GET /v1/activity/summary (owner, admin) · GET /v1/activity/export (owner, admin) |
|
||||
| `/integration` | DashboardShell | verified | IntegrationPage | none |
|
||||
| `/integration/api-keys` | DashboardShell | verified | ApiKeysPage + CreateApiKeyModal | GET /v1/api-key · POST /v1/api-key · DELETE /v1/api-key/{id} |
|
||||
| `/users` | DashboardShell | verified | UsersPage + InviteUserModal | GET /v1/user/ · POST /v1/user/invite · DELETE /v1/user/invite/:id · POST /v1/user/invite/:id/renew |
|
||||
| `/settings` | DashboardShell | verified | SettingsPage | GET /v1/settings · GET /v1/system/update · POST /v1/system/update/check · POST /v1/system/update/download · POST /v1/system/update/restart |
|
||||
| `/settings` | DashboardShell | verified | SettingsPage | GET /v1/settings · PATCH /v1/settings (owner, retention) · GET /v1/system/update · POST /v1/system/update/check · POST /v1/system/update/download · POST /v1/system/update/restart · GET /v1/media/purge-settings · PATCH /v1/media/purge-settings · GET /v1/media/purge-preview · POST /v1/media/purge |
|
||||
| `/profile` | DashboardShell | verified | ProfilePage | GET /v1/auth/me · PATCH /v1/user/me · POST /v1/auth/password-reset |
|
||||
| `/invite/accept` | AuthShell | public | AcceptInvitePage | GET /v1/auth/invite?c=* · POST /v1/auth/invite/accept* |
|
||||
| `/:pathMatch(.*)*` | either | any | NotFoundPage | none |
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
# Activity
|
||||
|
||||
- **Route:** `/activity`
|
||||
- **API:** GET /v1/activity · GET /v1/activity/summary · GET /v1/activity/export (see "API contract" below)
|
||||
- **Visual reference:** `source/RenewCMS Screens Activity.dc.html`, group "Activity, owner and admin" (open it in a browser; each state is drawn at desktop and mobile, with a light/dark toggle). The frame is `source/ActivityFrame.dc.html`; its copy, component mapping and API tables are `source/activity-spec.js`.
|
||||
|
||||
The audit log of the back-office: who did what, and when, across articles, media, users and invites, API keys, authentication and system tasks. It is for non-technical owners, so every row reads as a sentence, not a log line. Events are append-only: nothing in the app edits or deletes them. Only the retention period removes them (Settings, "Retention").
|
||||
|
||||
Other groups of the same board:
|
||||
|
||||
| Group | What it draws | Written up in |
|
||||
|---|---|---|
|
||||
| Details slide-over | The slide-over and its bottom-sheet variant, six events | this file, "Details slide-over" |
|
||||
| My activity | The same screen for a user who is not owner or admin | this file, "My activity" |
|
||||
| Home card | The Recent activity card on Home | `screens/home.md`, "Recent activity card" |
|
||||
| Settings retention | The Retention card on Settings | `screens/settings.md`, "Activity retention" |
|
||||
|
||||
## Roles
|
||||
|
||||
Owner and admin see every event, the summary strip, the Person filter and Export. Any other role sees only its own events: the title becomes "My activity", the strip, the Person filter and Export are hidden, and the server scopes the list to that user (asking for another actor answers 403).
|
||||
|
||||
Today the product has only `owner` and `admin` (see `screens/users.md`, "Roles"), so "My activity" and the scoped answer cannot be reached by a real account yet. They are specified and tested now so a future role needs no rework. The board's sample "Editor" role and the nav it draws for a regular user (Home, Articles, Media, Activity) are illustrative.
|
||||
|
||||
## Sidebar
|
||||
|
||||
"Activity" (icon `history`) sits between Users and Integration for owner and admin: Home, Articles, Media, Users, Activity, Integration, Settings. The board draws the order that way. It is visible to every verified user. The mobile drawer gets the same item.
|
||||
|
||||
## The page
|
||||
|
||||
Title "Activity" (regular users: "My activity"). Subtitle "Who did what, and when, across your site." (regular users: "Everything you've done on this site."). Page padding 24 px desktop, 16 px mobile; the sidebar and top bar are the existing DashboardShell.
|
||||
|
||||
1. **Header action** (owner and admin): outline `UButton` with icon `download`, "Export CSV". While the file is prepared: "Preparing file…" and disabled. Success toast "Export started. Your file will download shortly." Error toast "Couldn't export activity. Try again." Disabled in "Empty, no activity yet".
|
||||
2. **Summary strip** (owner and admin): four `UCard`, each a `button`. Labels: "Total actions", "Active people", "Failed actions", "Security events". Each shows the number, a sub-line and a seven-bar sparkline (seven `div` bars, no chart library, oldest left).
|
||||
- Total: "12% vs previous 7 days" with `trending-up` / `trending-down` (the comparison is against the equal-length period just before the selected range). Click clears the filters.
|
||||
- Active people: "7 people, 2 API keys". Click does nothing (no equivalent filter).
|
||||
- Failed: "0.5% of all actions". Emphasis with `--rc-error-soft` and `--ui-error`, icon `circle-x`. Click applies Outcome = Failed.
|
||||
- Security: "5 failed logins". Emphasis with `--rc-warning-soft` and `--ui-warning`, icon `shield-alert`. Click applies the security filter (auth, role changes, API key create and revoke).
|
||||
- Zero: Failed shows "0" with "Nothing failed" and `circle-check`; Security shows "0" with "Nothing to flag" and `circle-check`; both drop the emphasis fill. No data at all: every card reads "0" with the sub-line "No data yet" (Failed and Security keep their calm lines).
|
||||
- The strip follows the date range filter. Mobile: a 2×2 grid.
|
||||
3. **Filter bar**: `UInput` (icon `search`, clearable, "Search activity"), Person `USelectMenu` (grouped: users, API keys, "System"; "All people"), Category `USelectMenu` ("All categories": Articles, Media, Users and invites, API keys, Authentication, System), Outcome button group (All / Succeeded / Failed; `soft` is the selected one), date presets (Today / 7 days / 30 days / Custom; Custom opens a date-range popover). Mobile: the bar collapses to a search field and a "Filters" button with a count badge; the controls move to a bottom sheet.
|
||||
4. **Active filter chips**: `UBadge` (subtle) with a trailing ghost `x` button: "Search: spring", "Person: Marta Alves", "Category: Authentication", "Outcome: Failed", "Dates: 1 – 30 Sep 2026", then a `link` button "Clear all". The default date range (7 days) is not a chip.
|
||||
5. **Timeline table**: newest first, grouped by day with full-width separator rows ("Today", "Yesterday", "3 Oct 2026"). Columns: When (relative within the day, clock time before; the full time is the cell's `title`), Actor (`UAvatar` with initials, a key icon for API keys or a cog for System, plus the name; badges "API key" / "System"; a signed-out actor reads "Not signed in"), Action (category icon and the sentence with the target name in semibold; a `shield` marker on security rows), Target (a link when it still exists; struck through with a "Deleted" badge when it does not), Outcome (`UBadge` "Succeeded" / "Failed"). Row click and Enter open the details.
|
||||
6. **Pagination**: "Showing 1–25 of 1,284", `UPagination`, "Rows per page" select (25, 50, 100). Mobile: "Page 1 of 52" with previous and next.
|
||||
7. **Mobile list**: below `md` the table is a stacked list of `UCard` rows (actor, sentence, time, outcome, "Target deleted" when relevant), 44 px touch targets.
|
||||
|
||||
Sentences are composed in the SPA from a stable `action` code and the event params, in the user's language (English and French), never stored as text. The target name and the typed values in a diff stay as the user wrote them.
|
||||
|
||||
## States
|
||||
|
||||
### Default, large dataset
|
||||
**Trigger:** Both requests resolved with 1,284 events over 52 pages. Newest first, day separators, a security row. Hover on the first row shows its exact time.
|
||||
|
||||
### Loading
|
||||
**Trigger:** Both requests in flight. Skeleton strip and table rows after 150 ms.
|
||||
|
||||
### Zero-value summary strip
|
||||
**Trigger:** A quiet period. Failed and Security show a calm 0.
|
||||
|
||||
### Filtered, card applied
|
||||
**Trigger:** The Failed actions card was clicked. Chips plus "Clear all"; mobile shows the count on Filters. Six results.
|
||||
|
||||
### Mobile filters sheet
|
||||
**Trigger:** Filters open as a bottom sheet on mobile. The desktop frame keeps the inline bar. Sheet: title "Filters", Person (owner and admin), Category, Outcome, Dates (two date fields when Custom), footer "Clear all" and "Show 23 results".
|
||||
|
||||
### Empty, no activity yet
|
||||
**Trigger:** A new site. `UEmpty` with icon `history`, title "No activity yet", description "Actions on articles, media, users and API keys will appear here." Export disabled.
|
||||
|
||||
### Empty, filters match nothing
|
||||
**Trigger:** Filters with zero results. Title "No activity matches these filters", description "Try a different search or a wider date range.", action "Clear filters".
|
||||
|
||||
### Error with retry
|
||||
**Trigger:** The list request failed (500 or network). `UEmpty` in the table area: "Couldn't load activity", "Check your connection and try again.", action "Try again". The strip still shows if its own call succeeded.
|
||||
|
||||
### Forbidden (403)
|
||||
**Trigger:** The API answered 403 for this role or filter. "You can't view this activity.", "Your role doesn't include this page. Ask an owner or admin if you need access.", action "Back to home". The 401 is global.
|
||||
|
||||
## Details slide-over
|
||||
|
||||
`USlideover`, right side on desktop (about 440 px wide), bottom sheet on mobile. Opens from a row (click or Enter), closes with Escape, the close button or the scrim; focus returns to the row. Title "Activity details".
|
||||
|
||||
Body, in `UCard` sections:
|
||||
|
||||
- The full sentence, with the target name in semibold.
|
||||
- When: the user's local time, then a muted "UTC" line.
|
||||
- Actor (with the same badges), Source ("Web app", "API key", "System task"), IP address, Browser. For a system event, IP address and Browser read "Not applicable".
|
||||
- Target: name (struck through with a "Deleted" badge, plus "This item was deleted. Its name is kept for reference." when it no longer exists), and a link when it exists.
|
||||
- Why it failed (failed events only): the plain-language reason from the API, never a stack trace.
|
||||
- Changes (edits only): rows of field, Before, After. Values longer than two lines clamp with "Show more" / "Show less". A body edit is a single row "Body edited (+312 characters)" and never shows text.
|
||||
- Footer: "Previous event" and "Next event" buttons and "Event 2 of 1,284". They move through the current filtered list, across page boundaries.
|
||||
|
||||
### Edit with a diff
|
||||
**Trigger:** A title change and a body edit. Title Before and After, then "Body edited (+312 characters)".
|
||||
|
||||
### Settings diff, long text
|
||||
**Trigger:** Long values clamp to two lines with "Show more".
|
||||
|
||||
### Failed action, very long target title
|
||||
**Trigger:** A failed publish. The reason is shown and the long target title wraps.
|
||||
|
||||
### Deleted target, no diff
|
||||
**Trigger:** A deleted image. Struck-through target, "Deleted" badge, no Changes section.
|
||||
|
||||
### System-initiated, no human actor
|
||||
**Trigger:** A scheduled task. "System" badge, source "System task", IP and Browser "Not applicable".
|
||||
|
||||
### Security event, failed login
|
||||
**Trigger:** The actor was not signed in. "Not signed in", a restrained `shield` marker, the reason, the IP and browser.
|
||||
|
||||
## My activity
|
||||
|
||||
The same screen for a role that is not owner or admin. No summary strip, no Person filter, no Export. The list is the user's own events (server scoped).
|
||||
|
||||
### Default
|
||||
**Trigger:** The user has events. Title "My activity".
|
||||
|
||||
### Empty
|
||||
**Trigger:** No events yet: "No activity yet", "Your actions will appear here once you start working on the site."
|
||||
|
||||
### Details
|
||||
**Trigger:** The slide-over on one of the user's own events.
|
||||
|
||||
## API contract
|
||||
|
||||
401 and 403 on every route follow `states.md`, "HTTP status to copy". The board's table in `source/activity-spec.js` is the source; this section fixes the parts it leaves open.
|
||||
|
||||
**`GET /v1/activity`**
|
||||
|
||||
- Query: `q`, `actor` (`user:ID`, `key:ID`, `system`), `category` (`articles`, `media`, `users`, `api_keys`, `auth`, `system`), `outcome` (`all`, `success`, `failed`), `security` (`true`), `range` (`today`, `7d`, `30d`, `custom`), `from`, `to` (ISO dates, with `custom`), `page` (from 1), `size` (25, 50 or 100; default 25). Sorted by `created_at` descending, then `id` descending. Invalid values answer 400 "The request cannot be processed due to a mismatch in the format of the body."
|
||||
- `security=true` is what the Security card applies; it is a flag on the event, not a category.
|
||||
- Response: `{ items, total, page, size }`. An item:
|
||||
|
||||
```json
|
||||
{
|
||||
"id": 4821,
|
||||
"created_at": "2026-10-06T10:42:18Z",
|
||||
"action": "article.publish",
|
||||
"category": "articles",
|
||||
"actor": { "type": "user", "id": 3, "name": "Marta Alves" },
|
||||
"target": { "type": "article", "id": "8", "name": "Spring release", "exists": true },
|
||||
"outcome": "success",
|
||||
"failure_reason": null,
|
||||
"security": false,
|
||||
"source": "web",
|
||||
"ip": "203.0.113.12",
|
||||
"user_agent": "Chrome 129 on Windows",
|
||||
"changes": [{ "field": "online", "kind": "text", "before": "false", "after": "true" }]
|
||||
}
|
||||
```
|
||||
|
||||
- `actor.type` is `user`, `api_key`, `system` or `anonymous` (a failed login). `actor.name` is a snapshot taken when the event was recorded, so a renamed or deleted user still reads correctly. `target.name` is a snapshot too; `target.exists` is resolved when the list is read.
|
||||
- `action` is a stable dotted code. The SPA owns the wording, in English and French. `params` holds extra values a sentence needs (for example `{ "merged": 3, "articles_updated": 5 }` on a media purge); they are never secrets.
|
||||
- `changes[].kind` is `text` or `body`. A `body` change has `delta` (a signed number of characters) and no `before` or `after`. Only allowlisted fields ever appear in `changes`, and never a password, hash, API key, token or article body.
|
||||
- `source` is `web`, `api_key` or `system`. `ip` and `user_agent` are `null` for `system`.
|
||||
- A caller who is not owner or admin is scoped to its own events; `actor` other than itself answers 403.
|
||||
|
||||
**`GET /v1/activity/summary`** (owner and admin; 403 otherwise)
|
||||
|
||||
Same `range`, `from`, `to`. Response: `total`, `previous_total`, `active_people: { users, api_keys }`, `failed`, `security`, and `series` with seven daily counts for `total`, `active`, `failed` and `security`. The previous period is the equal-length period immediately before the range.
|
||||
|
||||
**`GET /v1/activity/export`** (owner and admin; 403 otherwise)
|
||||
|
||||
The same filters without `page` or `size`. Streams `text/csv; charset=utf-8` with `Content-Disposition: attachment`. Columns: `when` (RFC3339 UTC), `actor`, `action` (the code), `target`, `outcome`, `source`, `ip`. No changes, no user agent, no secrets. Cells that start with `=`, `+`, `-` or `@` are prefixed with a quote so a spreadsheet does not evaluate them.
|
||||
|
||||
**`GET /v1/settings` and `PATCH /v1/settings`**: see `screens/settings.md`, "Activity retention".
|
||||
|
||||
These routes are session only: with an API key they answer 403 "This action needs a real session." (A key never reads the audit trail.)
|
||||
|
||||
## What is recorded
|
||||
|
||||
Every row of the board is a sample. The real catalogue is the set of actions that exist in the product, in these categories:
|
||||
|
||||
| Category | Events |
|
||||
|---|---|
|
||||
| `articles` | create, edit (title, publish state, body size delta), publish, unpublish, delete |
|
||||
| `media` | upload, rename, delete, merge, purge (manual; the scheduled run is a `system` event) |
|
||||
| `users` | register, verify account, update profile (display name), invite create, cancel, renew, accept |
|
||||
| `api_keys` | create, revoke |
|
||||
| `auth` | login succeeded, login failed (security), logout, password reset requested (security), password reset completed (security) |
|
||||
| `system` | update checked, update downloaded, restart requested, scheduled media purge, activity retention changed, activity cleanup |
|
||||
|
||||
Not drawn as real events: the "Editor" role and role changes, a "Site description" or "Comments" setting, "Ran the nightly backup" (backups are a step of the update job, not a schedule), "Removed 3 expired invites" and an administrator resetting another user's password. They stay in the board as sample data and are only recorded if the matching feature exists.
|
||||
|
||||
Read requests are not recorded (no "article viewed"), and neither are public API reads.
|
||||
@@ -54,3 +54,13 @@ The greeting reads "Good morning, {name}", where the name is the display name wh
|
||||
**Trigger:** The download is checked. The same banner changes its copy, and a dismiss comes back on the next visit because the update is waiting.
|
||||
|
||||
- Banner: success tone, `circle-check` icon, title "Version 2.3.0 is ready to install", text "Restart from Settings to finish. The site is unavailable for about 10 seconds.", button "Go to Settings"
|
||||
|
||||
## Recent activity card
|
||||
|
||||
**API:** GET /v1/activity?size=5. Visual reference: `source/RenewCMS Screens Activity.dc.html`, group "Home card". The board draws the rest of Home as a dashed "Existing Home cards" placeholder, so this card is not pixel-checked against it.
|
||||
|
||||
A card with the last five events. Header "Recent activity" (regular users: "Your recent activity") and a "View all" link with `arrow-right` to `/activity`. Each row: a 24 px avatar, the sentence clamped to two lines with the target in semibold, then "Marta Alves · 2 min ago" (own events: only the time). A failed event gets a "Failed" badge; a successful security event a small `shield`.
|
||||
|
||||
- Placement as drawn: a 420 px column to the right of the existing cards on desktop; stacked on mobile, drawn above the existing cards. Confirm against the real Home layout before coding.
|
||||
- States: Owner or admin, Regular user, Loading (five skeleton rows after 150 ms), Error ("Couldn't load recent activity" with "Try again"), Empty ("No activity yet", "Actions on your site will appear here." / own: "Your actions will appear here.").
|
||||
- The card never blocks the page: an error or empty card leaves the stats and the article list as they are.
|
||||
@@ -1,7 +1,7 @@
|
||||
# Media
|
||||
|
||||
- **Route:** `/media` (verified users)
|
||||
- **API:** GET /v1/media/ · POST /v1/media/ · PATCH /v1/media/{id} · DELETE /v1/media/{id} · POST /v1/media/merge · GET /v1/media/{id}/usage
|
||||
- **API:** GET /v1/media/purge-preview · POST /v1/media/purge · GET /v1/media/ · POST /v1/media/ · PATCH /v1/media/{id} · DELETE /v1/media/{id} · POST /v1/media/merge · GET /v1/media/{id}/usage
|
||||
- **Visual reference:** `source/RenewCMS Screens Back-office.dc.html`, group "Media", drawn by `source/MediaFrame.dc.html` (open it in a browser; each state is drawn at desktop and mobile, with a light/dark toggle).
|
||||
|
||||
The library of every uploaded image. It replaces "images belong to one article": an image belongs to the library and can be used in any number of articles. Deleting an article never deletes images. The sidebar gains **Media** (`images` icon) between Articles and Users, breadcrumb "Media".
|
||||
@@ -12,7 +12,7 @@ New route for verified users, in the sidebar between Articles and Users with the
|
||||
|
||||
Header: title "Media", subtitle "Every image you've uploaded, ready to use in any article.", primary "Upload" button (`upload` icon) on the right. On mobile the button is a full-width 48 px button pinned to the bottom (it turns into "Try again in 0:42" while rate limited, and is replaced by the bulk bar while items are selected).
|
||||
|
||||
Toolbar: tabs **All**, **Unused** and **Duplicates** (count badge of duplicate groups, only while groups exist), then search ("Search by filename"), sort and the grid/list toggle. Sort options: Newest (default), Oldest, Name, Largest. Below md the toolbar stacks: search, tabs, then sort and toggle.
|
||||
Toolbar: tabs **All**, **Unused** and **Duplicates** (count badge of duplicate groups, only while groups exist), then search ("Search by filename"), sort and the grid/list toggle. Sort options: Newest (default), Oldest, Name, Largest. On the Duplicates tab, owners and admins also get a secondary "Purge duplicates" button (`merge` icon) after the sort menu (full width, 44 px, in the stacked toolbar on mobile), hidden while there are no groups; it opens the PurgeDuplicatesDialog described in [settings](settings.md#media-cleanup-second-card). Below md the toolbar stacks: search, tabs, then sort and toggle.
|
||||
|
||||
Grid (default): 5 columns on desktop, 2 on mobile, 16 px gap (10 px on mobile). Each card is one button: 4:3 thumbnail (checkerboard behind transparent PNG and SVG), filename (medium, ellipsis), `size · dimensions` (SVG dimensions read "Vector"), then badges. The accessible name reads "north-wall.jpg, 3.9 MB, used in 2 articles" or "…, unused". A "Select all" row with the image count sits above the grid.
|
||||
|
||||
@@ -149,6 +149,7 @@ Grid items are buttons, one tab stop each. Enter opens the detail panel, Space t
|
||||
| Upload: rejected files (413, 400) | Type and size are checked in the browser first, so nothing is sent. A server 413 or 400 for a file that got through shows the same row with its own message. |
|
||||
| Upload: too many requests (429) | Rate limited. Retry-After is counted down in the button. It re-enables at zero. Already-added files are kept. |
|
||||
| Duplicates: groups | The Duplicates tab. Groups of identical images side by side, each with its uploader, upload date and "Used in N articles". The suggested copy to keep is the most used, then the oldest. |
|
||||
| Duplicates: purge all | Owner or admin, groups exist. "Purge duplicates" opens the purge dialog (preview, confirm, progress, results), the same one as Settings "Purge now". "Merge into this one" is unchanged. |
|
||||
| Duplicates: empty | No groups. The Duplicates tab loses its count badge. |
|
||||
| Merge: confirm | "Merge into this one" on floorboards.jpg. The dialog says articles using the other copies will switch to this image and the other copies will be deleted, with the article count. |
|
||||
| Merge: in progress | Request in flight. The dialog can't be dismissed. |
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Settings
|
||||
|
||||
- **Route:** `/settings`
|
||||
- **API:** GET /v1/settings (version) · GET /v1/system/update · POST /v1/system/update/check · POST /v1/system/update/download · POST /v1/system/update/restart
|
||||
- **API:** GET /v1/media/purge-settings · PATCH /v1/media/purge-settings · GET /v1/media/purge-preview · POST /v1/media/purge (see "Media cleanup (second card)" at the end) · GET /v1/settings (version) · GET /v1/system/update · POST /v1/system/update/check · POST /v1/system/update/download · POST /v1/system/update/restart
|
||||
- **Visual reference:** `source/RenewCMS Screens Back-office.dc.html`, group "Settings", drawn by `source/AppFrame.dc.html` (open it in a browser; each state is drawn at desktop and mobile, with a light/dark toggle).
|
||||
|
||||
Opened from the sidebar (Settings, settings icon, after Integration) and from the mobile drawer. Every verified user sees it. It holds two sections: Version (everyone) and Updates (the owner has the buttons, admins read the status).
|
||||
@@ -125,3 +125,70 @@ For the owner only: an **Update** pill on the Settings sidebar item, and a dot o
|
||||
| Admin view | Admins see the version and the note, with no buttons. Up to date: "You're up to date" and "Only the owner can update or restart the site." No Home banner or Settings pill. |
|
||||
| Role changed (403) | An owner was changed to admin while the page was open and pressed a button. The card reloads in the admin view. |
|
||||
| Update already running (409) | Another owner started it. The card switches to that run's progress. |
|
||||
|
||||
## Media cleanup (second card)
|
||||
|
||||
Route: `/settings`. Visual reference: `RenewCMS Screens Media cleanup.dc.html` (frames: `SettingsFrame.dc.html`, `MediaFrame.dc.html`).
|
||||
API: `GET/PATCH /v1/media/purge-settings` (owner, admin), `GET /v1/media/purge-preview`, `POST /v1/media/purge`. 401 and 403 are global: no local copy.
|
||||
|
||||
Rule: a group is purged only if at least one copy is used in an article. All copies merge into the one used in the most articles (ties: oldest, then lowest id). Groups with no used copy are left alone. Automatic purge is off by default.
|
||||
|
||||
### Card
|
||||
Second card below Version, 20 px gap, same card styling. Page max width 720 px.
|
||||
- Title: "Media cleanup". Description: "Merge identical images so your library stays tidy. Articles that use a copy are switched to the most used one."
|
||||
- "Automatic purge" / helper "Runs in the background. Off by default." / USwitch.
|
||||
- When on: "Every" / helper "Between 1 and 365 days." / number input, suffix "days", default 30.
|
||||
- Read-only lines: "Last purge: 14 Jun 2026 · 3 copies merged · 5 articles updated" or "Never run". When on, also "Next purge: 14 Jul 2026".
|
||||
- Divider, then "Purge now" / "Merge every group of duplicates that has an image in use, right away." / outline button "Purge duplicates" (merge).
|
||||
- No Save button. Saves on toggle and on blur or Enter of the days field. "Saved" (check, success) sits in the card header and fades after 2 s; "Saving…" (loader-circle) while pending.
|
||||
- Non-owner/admin: switch and input disabled, button and divider hidden, info alert "Only owners and admins can change this."
|
||||
|
||||
### States (names matched exactly)
|
||||
- Default: off, never run
|
||||
- On: 30 days, last run
|
||||
- Saving
|
||||
- Saved
|
||||
- Save failed: "Couldn't save. Try again in a moment." under the field; the control reverts.
|
||||
- Validation error: "Enter a whole number of days between 1 and 365." Nothing is sent.
|
||||
- Read-only (non-admin)
|
||||
- Loading skeleton (after 150 ms)
|
||||
- Offline: banner "You're offline. Changing automatic purge and purging duplicates need a connection." Switch, input and button disabled.
|
||||
|
||||
### Dialog
|
||||
UModal, `sm:max-w-md`, bottom sheet on mobile, cancel first.
|
||||
- Title "Purge duplicates?". Description "Articles that use a duplicate will be switched to the most used copy of that image, and the other copies will be deleted. This can't be undone."
|
||||
- Summary: "4 groups · 9 copies will be deleted · 12 articles will be updated". Singular: "1 group · 1 copy will be deleted · 2 articles will be updated".
|
||||
- Bordered list of up to 5 groups: thumbnail, filename, "Kept · used in 4 articles", and "2 copies merged". More: "and 3 more groups".
|
||||
- Link "Review in Duplicates" to `/media?filter=duplicates`.
|
||||
- Buttons: "Cancel", danger "Purge 4 groups". Pending: "Purging…" with loader-circle, dialog can't be dismissed.
|
||||
- Loading preview: skeleton rows, confirm disabled.
|
||||
- Nothing to purge: icon copy, "Nothing to purge", "No duplicate is used in an article, so there is nothing to merge.", button "Close". No confirm.
|
||||
- Error (stays open): "Couldn't purge the duplicates. Nothing was changed. Try again in a moment."
|
||||
- Partial: "Some groups couldn't be merged. 3 of 4 groups were purged. Try again to finish." Buttons "Close", "Retry". Summary refreshes to the remaining groups.
|
||||
- Conflict (409): "The library changed, so nothing was purged. Check the new summary and try again." Summary refreshes.
|
||||
|
||||
### Results
|
||||
- Toast (5 s): circle-check, "Duplicates purged" / "9 copies merged. 12 articles updated."
|
||||
- "Last purge" updates in place; "Next purge" moves forward by the interval.
|
||||
- An unattended run shows no toast. The next visit shows the updated line.
|
||||
|
||||
### Media entry point
|
||||
Duplicates tab: secondary "Purge duplicates" button (merge) next to the sort menu; stacked toolbar on mobile. Opens the same dialog. Hidden for non-owner/admin and when there are no groups. "Merge into this one" is unchanged.
|
||||
|
||||
### Components (Nuxt UI v4)
|
||||
`UCard`, `USwitch`, `UInputNumber` (min 1, max 365, suffix), `UFormField`, `UAlert`, `UButton`, `UModal`, `USkeleton`, `useToast`.
|
||||
|
||||
## Activity retention
|
||||
|
||||
**API:** GET /v1/settings · PATCH /v1/settings (owner only). Visual reference: `source/RenewCMS Screens Activity.dc.html`, group "Settings retention". The board draws the other cards as a dashed "Existing Settings cards" placeholder and gives the page a different subtitle ("Site-wide options. Only owners can change them."), which is not a decision to change the existing one.
|
||||
|
||||
A third card below Media cleanup, owner only (hidden for admins), same card styling, 640 px at most as drawn.
|
||||
|
||||
- Header: "Retention" / "How long activity is kept." "Saved" (check, success) at the right after a save, fading after 2 s.
|
||||
- "Keep activity for" `USelectMenu`: 30 days, 90 days, 365 days, Forever. Default 90 days. Helper: "Older events are removed automatically." For Forever: "Nothing is removed automatically. The log grows over time."
|
||||
- Info alert (`lock`): "Activity logs are append-only", "Events can't be edited or deleted from the app. Only the retention period removes them."
|
||||
- Divider, then read-only lines: "Last cleanup: 6 Oct 2026, 03:00 · 142 events removed" and "Next cleanup: 7 Oct 2026, 03:00". Forever: "Never" and "Not scheduled".
|
||||
- Saves on change. Choosing a shorter period than the current one first shows a warning alert, "Events older than 30 days will be removed", "This happens at the next cleanup on 7 Oct 2026, 03:00. It can't be undone.", buttons "Apply 30 days" and "Cancel"; nothing is sent until Apply. Lengthening or choosing Forever saves at once.
|
||||
- Save error alert: "Couldn't save the retention period", "Your previous setting is still in place.", "Try again". The control reverts.
|
||||
- States: Default (90 days, saved), Shortening the period, Forever, Save error. Loading and Offline follow the Media cleanup card.
|
||||
- `GET /v1/settings` adds `activity_retention_days` (30, 90, 365 or 0 for forever), `activity_last_cleanup_at` (null before the first cleanup) and `activity_last_cleanup_removed`. `PATCH /v1/settings` takes `{ "activity_retention_days": 30 }`; any other value answers 400 "The request cannot be processed due to a mismatch in the format of the body."; a non-owner gets 403 "Only the owner can do this.". The cleanup runs once a day at 03:00 server time and never touches the last 24 hours.
|
||||
@@ -0,0 +1,252 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<script src="./support.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<x-dc>
|
||||
<div style="display:contents">{{ view }}</div>
|
||||
</x-dc>
|
||||
<script type="text/x-dc" data-dc-script data-props="{"$preview":{"width":1440,"height":900},"s":{"editor":null,"tsType":"object"},"mobile":{"editor":null,"tsType":"boolean"}}">
|
||||
class Component extends DCLogic {
|
||||
renderVals() {
|
||||
const s = this.props.s || {}, m = !!this.props.mobile, h = React.createElement
|
||||
const P = t => Object.fromEntries(t.split(';').filter(x => x.trim()).map(x => { const i = x.indexOf(':'); const k = x.slice(0, i).trim(); return [k.startsWith('--') ? k : k.replace(/-([a-z])/g, (_, c) => c.toUpperCase()), x.slice(i + 1).trim()] }))
|
||||
const D = (t, ...c) => h('div', { style: P(t) }, ...c)
|
||||
const S = (t, ...c) => h('span', { style: P(t) }, ...c)
|
||||
const I = (n, z = 16, t = '') => h('span', { 'aria-hidden': 'true', style: { display: 'inline-block', flexShrink: 0, width: z, height: z, backgroundColor: 'currentColor', WebkitMask: 'url(https://api.iconify.design/lucide/' + n + '.svg) center / contain no-repeat', mask: 'url(https://api.iconify.design/lucide/' + n + '.svg) center / contain no-repeat', ...(t ? P(t) : {}) } })
|
||||
const bh = m ? 44 : 32
|
||||
const MONO = "font-family:'IBM Plex Mono',monospace"
|
||||
const B = (label, o = {}) => {
|
||||
const k = o.k || 'outline'
|
||||
const bg = k === 'primary' ? 'var(--ui-primary)' : 'var(--ui-bg)'
|
||||
const fg = k === 'primary' ? 'var(--ui-bg)' : 'var(--ui-text-highlighted)'
|
||||
const bd = k === 'primary' ? bg : 'var(--ui-border-accented)'
|
||||
return S('height:' + (o.h || bh) + 'px;' + (o.w ? 'width:' + o.w + 'px;' : '') + 'padding:0 ' + (o.px ?? 12) + 'px;display:flex;align-items:center;justify-content:center;gap:6px;background:' + bg + ';color:' + fg + ';border:1px solid ' + bd + ';font-weight:500;font-size:' + (o.fs || 13) + 'px;white-space:nowrap;flex-shrink:0;opacity:' + (o.dis ? 0.4 : 1) + ';' + (o.full ? 'flex:1;' : ''), o.icon && I(o.icon, o.z || 16), label)
|
||||
}
|
||||
const BG = { ok: ['border:1px solid var(--ui-border-accented);color:var(--ui-text-toned)', 'check'], fail: ['background:var(--rc-error-soft);color:var(--ui-error)', 'circle-x'], sec: ['background:var(--rc-warning-soft);color:var(--ui-warning)', 'shield-alert'], del: ['border:1px solid var(--ui-border-accented);color:var(--ui-text-toned)', 'trash-2'], neutral: ['border:1px solid var(--ui-border-accented);color:var(--ui-text-toned)'], info: ['background:var(--rc-info-soft);color:var(--ui-info)'] }
|
||||
const Bd = (t, k, sz = 20) => S('height:' + sz + 'px;padding:0 6px;display:inline-flex;align-items:center;gap:4px;font-size:12px;font-weight:500;white-space:nowrap;flex-shrink:0;' + BG[k][0], BG[k][1] && I(BG[k][1], 12), t)
|
||||
const Out = e => e.ok ? Bd('Succeeded', 'ok') : Bd('Failed', 'fail')
|
||||
const Alert = (tone, icon, title, text, action) => D('display:flex;gap:10px;padding:12px 14px;background:var(--rc-' + tone + '-soft);color:var(--ui-' + tone + ')', S('display:flex;padding-top:2px', I(icon)), D('flex:1;display:flex;flex-direction:column;gap:8px', D('display:flex;flex-direction:column;gap:2px', S('font-weight:600', title), text && S('color:var(--ui-text-toned)', text)), action && D('display:flex;gap:8px', ...[].concat(action).map(a => B(a, { h: m ? 44 : 28, fs: 12 })))))
|
||||
const Empty = (icon, title, text, action, primary, pad = 56) => D('background:var(--ui-bg);border:1px solid var(--ui-border);padding:' + pad + 'px 24px;display:flex;flex-direction:column;align-items:center;gap:12px;text-align:center', S('width:44px;height:44px;display:flex;align-items:center;justify-content:center;background:var(--ui-bg-elevated);color:var(--ui-text-toned)', I(icon, 20)), S('font-weight:600;font-size:15px;color:var(--ui-text-highlighted)', title), S('color:var(--ui-text-muted);max-width:340px;text-wrap:pretty', text), action && B(action, { k: primary ? 'primary' : 'outline', px: 16 }))
|
||||
const Note = t => S('align-self:flex-start;height:20px;padding:0 6px;display:inline-flex;align-items:center;border:1px dashed var(--ui-border-accented);color:var(--ui-text-toned);font:500 11px \'IBM Plex Mono\',monospace', t)
|
||||
const sk = (w, hh = 12, extra = '') => S('display:block;width:' + w + ';height:' + hh + 'px;background:var(--ui-bg-accented);flex-shrink:0;' + extra)
|
||||
|
||||
// ---------- data
|
||||
const U = n => ({ n, k: 'user' }), KEY = n => ({ n, k: 'key' }), SYS = { n: 'Scheduler', k: 'system' }, ANON = { n: 'Not signed in', k: 'anon' }
|
||||
const p2 = n => String(n).padStart(2, '0')
|
||||
const T = (d, hh, mm, ss) => ({ abs: d + ' 2026, ' + p2(hh) + ':' + p2(mm) + ':' + p2(ss), utc: d + ' 2026, ' + p2(hh - 1) + ':' + p2(mm) + ':' + p2(ss) + ' UTC' })
|
||||
const WEB = { src: 'Web app', ip: '203.0.113.24', ua: 'Firefox 131 on macOS' }
|
||||
const LONG = 'The complete guide to restoring a nineteenth-century timber-framed farmhouse: structural surveys, lime mortar, breathable insulation and what it all costs'
|
||||
const E = [
|
||||
{ id: 1, day: 'Today', rel: '2 min ago', ...T('6 Oct', 10, 42, 18), a: U('Marta Alves'), cat: 'articles', s: 'Published the article ', em: 'Spring release', tk: 'Article', ex: 1, ok: 1, tid: 'art_8f3k2', ...WEB, diff: [{ f: 'Publish state', b: 'Draft', a: 'Published' }] },
|
||||
{ id: 2, day: 'Today', rel: '29 min ago', ...T('6 Oct', 10, 15, 4), a: U('Marta Alves'), cat: 'articles', s: 'Edited the article ', em: 'Why we reuse original floorboards', tk: 'Article', ex: 1, ok: 1, tid: 'art_2m9xq', ...WEB, diff: [{ f: 'Title', b: 'Why we reuse floorboards', a: 'Why we reuse original floorboards' }, { f: 'Body', note: 'Body edited (+312 characters)' }] },
|
||||
{ id: 3, day: 'Today', rel: '1 h ago', ...T('6 Oct', 9, 31, 47), a: ANON, cat: 'auth', s: 'Failed login attempt for ', em: 'alex@example.com', ok: 0, sec: 1, why: 'The password did not match. 3 attempts in 10 minutes.', src: 'Web app', ip: '198.51.100.77', ua: 'Chrome 129 on Windows' },
|
||||
{ id: 4, day: 'Today', rel: '2 h ago', ...T('6 Oct', 8, 50, 9), a: U('Jonas Weber'), cat: 'keys', s: 'Revoked API key ', em: 'Mobile app', tk: 'API key', ex: 1, ok: 1, sec: 1, tid: 'key_71ab0', src: 'Web app', ip: '203.0.113.90', ua: 'Safari 18 on iOS' },
|
||||
{ id: 5, day: 'Today', rel: '7 h ago', ...T('6 Oct', 3, 0, 2), a: SYS, cat: 'system', s: 'Ran the nightly backup', em: '', ok: 1, src: 'System task', ip: null, ua: null },
|
||||
{ id: 6, day: 'Yesterday', rel: '18:20', ...T('5 Oct', 18, 20, 36), a: U('Inès Duarte'), cat: 'users', s: 'Changed the role of ', em: 'Jonas Weber', tk: 'User', ex: 1, ok: 1, sec: 1, tid: 'usr_c40d1', tail: ' from Editor to Admin', ...WEB, ip: '203.0.113.12', diff: [{ f: 'Role', b: 'Editor', a: 'Admin' }] },
|
||||
{ id: 7, day: 'Yesterday', rel: '16:05', ...T('5 Oct', 16, 5, 51), a: KEY('Website sync'), cat: 'media', s: 'Uploaded the image ', em: 'north-wall.jpg', tk: 'Image', ex: 1, ok: 1, tid: 'img_0pk4w', src: 'API key', ip: '192.0.2.18', ua: 'node-fetch 3.3' },
|
||||
{ id: 8, day: 'Yesterday', rel: '14:40', ...T('5 Oct', 14, 40, 13), a: U('Marta Alves'), cat: 'media', s: 'Deleted the image ', em: 'old-banner.png', tk: 'Image', ex: 0, ok: 1, tid: 'img_9zq1c', ...WEB },
|
||||
{ id: 9, day: 'Yesterday', rel: '11:12', ...T('5 Oct', 11, 12, 27), a: U('Inès Duarte'), cat: 'articles', s: 'Failed to publish the article ', em: LONG, tk: 'Article', ex: 1, ok: 0, tid: 'art_5t7yh', why: 'The article has no cover image. Add one, then publish again.', ...WEB, ip: '203.0.113.12' },
|
||||
{ id: 10, day: 'Yesterday', rel: '03:00', ...T('5 Oct', 3, 0, 1), a: SYS, cat: 'users', s: 'Removed 3 expired invites', em: '', ok: 1, src: 'System task', ip: null, ua: null },
|
||||
{ id: 11, day: '3 Oct 2026', rel: '16:48', ...T('3 Oct', 16, 48, 22), a: U('Inès Duarte'), cat: 'system', s: 'Changed the setting ', em: 'Site description', tk: 'Setting', ex: 1, ok: 1, tid: 'set_site_desc', ...WEB, ip: '203.0.113.12', diff: [{ f: 'Site description', b: 'Renovation notes, project write-ups and practical guides from the Renew studio team in Lisbon, covering timber, lime plaster and stone restoration for older homes.', a: 'Renovation notes, project write-ups and practical guides from the Renew studio team in Lisbon, covering timber, lime plaster and stone restoration for older homes, plus a seasonal maintenance checklist.', long: 1 }, { f: 'Comments', b: 'Off', a: 'On' }] },
|
||||
{ id: 12, day: '3 Oct 2026', rel: '15:02', ...T('3 Oct', 15, 2, 40), a: U('Jonas Weber'), cat: 'keys', s: 'Created API key ', em: 'Mobile app', tk: 'API key', ex: 0, ok: 1, sec: 1, tid: 'key_71ab0', ...WEB, ip: '203.0.113.90' },
|
||||
{ id: 13, day: '3 Oct 2026', rel: '14:20', ...T('3 Oct', 14, 20, 5), a: U('Inès Duarte'), cat: 'users', s: 'Updated the user ', em: 'Sam Rivera', tk: 'User', ex: 1, ok: 1, tid: 'usr_77ea2', ...WEB, ip: '203.0.113.12', diff: [{ f: 'Name', b: 'Sam Rivera', a: 'Samuel Rivera' }, { f: 'Email', b: 'sam@example.com', a: 'samuel@example.com' }] },
|
||||
{ id: 14, day: '3 Oct 2026', rel: '11:36', ...T('3 Oct', 11, 36, 58), a: U('Marta Alves'), cat: 'media', s: 'Uploaded the image ', em: 'floorboards-copy.jpg', tk: 'Image', ex: 1, ok: 1, tid: 'img_4ww8e', ...WEB },
|
||||
{ id: 15, day: '3 Oct 2026', rel: '09:05', ...T('3 Oct', 9, 5, 12), a: U('Inès Duarte'), cat: 'auth', s: 'Reset the password for ', em: 'Sam Rivera', tk: 'User', ex: 1, ok: 1, sec: 1, tid: 'usr_77ea2', ...WEB, ip: '203.0.113.12' }
|
||||
]
|
||||
const CI = { articles: 'file-text', media: 'image', users: 'users', keys: 'key-round', auth: 'shield-check', system: 'server-cog' }
|
||||
const CL = { articles: 'Articles', media: 'Media', users: 'Users & invites', keys: 'API keys', auth: 'Authentication', system: 'System' }
|
||||
const user = s.role === 'user'
|
||||
const ME = user ? 'Marta Alves' : 'Inès Duarte'
|
||||
let list = s.ids ? s.ids.map(i => E.find(e => e.id === i)) : (user ? E.filter(e => e.a.n === 'Marta Alves') : E)
|
||||
const total = s.total || (user ? '41' : '1,284')
|
||||
const sentence = e => e.s + e.em + (e.tail || '')
|
||||
const Av = (a, z = 24) => {
|
||||
const base = 'width:' + z + 'px;height:' + z + 'px;flex-shrink:0;display:flex;align-items:center;justify-content:center;'
|
||||
if (a.k === 'user') return S(base + 'background:var(--rc-sand);color:var(--rc-on-sand);font:600 ' + Math.round(z * 0.42) + "px 'IBM Plex Sans',sans-serif", a.n.split(' ').map(x => x[0]).join('').slice(0, 2).toUpperCase())
|
||||
return S(base + 'border:1px solid var(--ui-border-accented);background:var(--ui-bg-elevated);color:var(--ui-text-toned)', I({ key: 'key-round', system: 'cog', anon: 'user-round-x' }[a.k], Math.round(z * 0.6)))
|
||||
}
|
||||
const ActorBd = a => a.k === 'key' ? Bd('API key', 'neutral') : a.k === 'system' ? Bd('System', 'neutral') : null
|
||||
const ell = 'overflow:hidden;text-overflow:ellipsis;white-space:nowrap;min-width:0'
|
||||
const link = 'text-decoration:underline;text-underline-offset:3px;color:var(--ui-text-highlighted)'
|
||||
const SecIcon = z => S('display:flex;color:var(--ui-warning)', I('shield-alert', z))
|
||||
|
||||
// ---------- controls
|
||||
const Search = () => D('height:' + bh + 'px;' + (m ? 'flex:1;' : 'width:240px;') + 'padding:0 4px 0 10px;display:flex;align-items:center;gap:8px;border:1px solid var(--ui-border-accented);background:var(--ui-bg);color:' + (F.q ? 'var(--ui-text-highlighted)' : 'var(--ui-text-muted)') + (m ? ';font-size:16px' : ''), I('search', 14), S('flex:1;' + ell, F.q || 'Search activity'), F.q && S('width:24px;height:24px;display:flex;align-items:center;justify-content:center;color:var(--ui-text-muted)', I('x', 14)))
|
||||
const Sel = (val, ph, w, icon) => D('height:' + bh + 'px;padding:0 10px;display:flex;align-items:center;gap:8px;border:1px solid var(--ui-border-accented);background:var(--ui-bg);color:' + (val ? 'var(--ui-text-highlighted)' : 'var(--ui-text-muted)') + ';' + (w ? 'width:' + w + 'px;flex-shrink:0' : 'width:100%'), icon && I(icon, 14, 'color:var(--ui-text-muted)'), S('flex:1;' + ell, val || ph), I('chevron-down', 14, 'color:var(--ui-text-muted)'))
|
||||
const Seg = (opts, on, fw) => D('display:flex;border:1px solid var(--ui-border-accented);flex-shrink:0;' + (fw ? 'width:100%' : ''), ...opts.map(o => S('height:' + (bh - 2) + 'px;padding:0 ' + (m ? 12 : 10) + 'px;' + (fw ? 'flex:1;justify-content:center;' : '') + 'display:flex;align-items:center;gap:6px;white-space:nowrap;font-weight:' + (o === on ? 500 : 400) + ';background:' + (o === on ? 'var(--rc-sand)' : 'var(--ui-bg)') + ';color:' + (o === on ? 'var(--rc-on-sand)' : 'var(--ui-text-toned)'), o === 'Custom' && I('calendar', 14), o)))
|
||||
const F = { q: '', actor: '', cat: '', out: 'All', range: '7 days', ...(s.f || {}) }
|
||||
const RANGES = ['Today', '7 days', '30 days', 'Custom']
|
||||
const chips = []
|
||||
if (F.q) chips.push('Search: ' + F.q)
|
||||
if (F.actor && !user) chips.push('Person: ' + F.actor)
|
||||
if (F.cat) chips.push('Category: ' + F.cat)
|
||||
if (F.out !== 'All') chips.push('Outcome: ' + F.out)
|
||||
if (F.range !== '7 days') chips.push('Dates: ' + (F.range === 'Custom' ? (F.custom || '1 – 30 Sep 2026') : F.range))
|
||||
const nF = chips.length
|
||||
const Chips = () => nF > 0 && D('display:flex;flex-wrap:wrap;align-items:center;gap:8px', ...chips.map(c => S('height:' + (m ? 32 : 24) + 'px;padding:0 4px 0 8px;display:inline-flex;align-items:center;gap:4px;background:var(--ui-bg-elevated);border:1px solid var(--ui-border);color:var(--ui-text-highlighted);font-size:12px;font-weight:500', c, S('width:20px;height:20px;display:flex;align-items:center;justify-content:center;color:var(--ui-text-muted)', I('x', 12)))), S('height:' + (m ? 44 : 24) + 'px;display:flex;align-items:center;padding:0 6px;font-weight:500;font-size:12px;' + link, 'Clear all'))
|
||||
const FilterBar = () => m
|
||||
? D('display:flex;gap:8px', Search(), h('span', { style: P('height:44px;padding:0 12px;display:flex;align-items:center;gap:6px;border:1px solid var(--ui-border-accented);background:var(--ui-bg);color:var(--ui-text-highlighted);font-weight:500;font-size:13px;flex-shrink:0') }, I('sliders-horizontal', 16), 'Filters', nF > 0 && S('min-width:18px;height:18px;padding:0 5px;display:inline-flex;align-items:center;justify-content:center;background:var(--ui-primary);color:var(--ui-bg);font:500 11px \'IBM Plex Mono\',monospace', String(nF))))
|
||||
: D('display:flex;align-items:center;gap:8px;flex-wrap:wrap', Search(), !user && Sel(F.actor, 'All people', 168, 'user-round'), Sel(F.cat, 'All categories', 176, 'layout-list'), Seg(['All', 'Succeeded', 'Failed'], F.out), D('flex:1;min-width:0'), Seg(RANGES, F.range))
|
||||
|
||||
// ---------- summary
|
||||
const mk = (l, v, sub, ic, tone, ser) => ({ l, v, sub, ic, tone, ser })
|
||||
const STAT = s.sum === 'zero' ? [mk('Total actions', '214', '4% vs previous 7 days', 'trending-down', 'n', [3, 5, 2, 6, 4, 1, 2]), mk('Active people', '5', '4 people, 1 API key', '', 'n', [2, 3, 2, 3, 2, 1, 2]), mk('Failed actions', '0', 'Nothing failed', 'circle-check', 'ok0', [0, 0, 0, 0, 0, 0, 0]), mk('Security events', '0', 'Nothing to flag', 'circle-check', 'ok0', [0, 0, 0, 0, 0, 0, 0])]
|
||||
: s.sum === 'none' ? [mk('Total actions', '0', 'No data yet', '', 'n', [0, 0, 0, 0, 0, 0, 0]), mk('Active people', '0', 'No data yet', '', 'n', [0, 0, 0, 0, 0, 0, 0]), mk('Failed actions', '0', 'Nothing failed', 'circle-check', 'ok0', [0, 0, 0, 0, 0, 0, 0]), mk('Security events', '0', 'Nothing to flag', 'circle-check', 'ok0', [0, 0, 0, 0, 0, 0, 0])]
|
||||
: [mk('Total actions', '1,284', '12% vs previous 7 days', 'trending-up', 'n', [4, 6, 5, 8, 7, 9, 6]), mk('Active people', '9', '7 people, 2 API keys', '', 'n', [5, 6, 6, 7, 7, 8, 9]), mk('Failed actions', '6', '0.5% of all actions', 'circle-x', 'error', [0, 1, 0, 2, 1, 0, 2]), mk('Security events', '11', '5 failed logins', 'shield-alert', 'warning', [1, 0, 2, 1, 3, 2, 2])]
|
||||
const Bars = (ser, tone, z) => {
|
||||
const mx = Math.max(...ser, 1), col = tone === 'error' ? 'var(--ui-error)' : tone === 'warning' ? 'var(--ui-warning)' : tone === 'ok0' ? 'var(--ui-border-accented)' : 'var(--ui-text-muted)'
|
||||
return D('display:flex;align-items:flex-end;gap:2px;height:' + z + 'px;flex-shrink:0', ...ser.map(v => S('display:block;width:4px;height:' + Math.max(2, Math.round(v / mx * z)) + 'px;background:' + col)))
|
||||
}
|
||||
const StatCard = (c, i) => {
|
||||
const act = s.card === i, tone = c.tone
|
||||
const bg = tone === 'error' ? 'var(--rc-error-soft)' : tone === 'warning' ? 'var(--rc-warning-soft)' : 'var(--ui-bg)'
|
||||
const vc = tone === 'error' ? 'var(--ui-error)' : tone === 'warning' ? 'var(--ui-warning)' : 'var(--ui-text-highlighted)'
|
||||
const sc = tone === 'ok0' ? 'var(--ui-success)' : 'var(--ui-text-muted)'
|
||||
return D('display:flex;flex-direction:column;gap:2px;padding:' + (m ? '10px 12px' : '12px 14px') + ';background:' + bg + ';border:1px solid ' + (act ? 'var(--ui-primary)' : 'var(--ui-border)') + ';' + (act ? 'box-shadow:inset 0 0 0 1px var(--ui-primary);' : '') + (m ? 'flex:0 0 156px;' : 'min-width:0;'),
|
||||
D('display:flex;align-items:center;justify-content:space-between;gap:8px', S('color:var(--ui-text-toned);font-weight:500;' + ell, c.l), Bars(c.ser, tone, m ? 16 : 20)),
|
||||
S('font:600 ' + (m ? 22 : 24) + "px/32px 'IBM Plex Sans',sans-serif;color:" + vc, c.v),
|
||||
S('display:flex;align-items:center;gap:4px;font-size:12px;color:' + sc + ';' + ell, c.ic && I(c.ic, 12), c.sub))
|
||||
}
|
||||
const SkelCard = () => D('display:flex;flex-direction:column;gap:8px;padding:' + (m ? '10px 12px' : '14px') + ';background:var(--ui-bg);border:1px solid var(--ui-border);' + (m ? 'flex:0 0 156px' : 'min-width:0'), sk('55%', 12), sk('40%', 22), sk('70%', 10))
|
||||
const Strip = () => {
|
||||
if (user) return null
|
||||
const kids = s.state === 'loading' ? [0, 1, 2, 3].map(SkelCard) : STAT.map(StatCard)
|
||||
return m ? D('display:flex;gap:8px;overflow:hidden;margin:0 -16px;padding:0 16px', ...kids) : D('display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:12px', ...kids)
|
||||
}
|
||||
|
||||
// ---------- table
|
||||
const cols = user ? '96px minmax(0,1fr) 230px 120px' : '96px 210px minmax(0,1fr) 230px 120px'
|
||||
const TableHead = () => D('display:grid;grid-template-columns:' + cols + ';height:36px;align-items:center;padding-right:16px;background:var(--ui-bg-elevated);color:var(--ui-text-muted);font-weight:500;flex-shrink:0', S('padding-left:16px;display:flex;align-items:center;gap:4px;color:var(--ui-text-highlighted)', 'When', I('arrow-down', 12)), !user && S('', 'Actor'), S('', 'Action'), S('', 'Target'), S('', 'Outcome'))
|
||||
const Day = d => D('display:flex;align-items:center;height:32px;padding:0 ' + (m ? 16 : 16) + 'px;background:var(--ui-bg-muted);border-top:1px solid var(--ui-border);font-weight:600;font-size:12px;color:var(--ui-text-highlighted);flex-shrink:0', d)
|
||||
const Tip = e => S('position:absolute;left:12px;top:38px;z-index:5;padding:6px 8px;background:var(--ui-bg-inverted);color:var(--ui-text-inverted);font-size:12px;white-space:nowrap;box-shadow:var(--rc-shadow-pop)', e.abs)
|
||||
const TargetCell = e => !e.tk ? S('color:var(--ui-text-muted)', '—') : D('display:flex;flex-direction:column;min-width:0;padding-right:12px', e.ex ? S('display:flex;align-items:center;gap:4px;' + link, S(ell, e.em), I('arrow-up-right', 12, 'color:var(--ui-text-muted)')) : D('display:flex;align-items:center;gap:6px;min-width:0', S('flex:1;text-decoration:line-through;color:var(--ui-text-muted);' + ell, e.em), Bd('Deleted', 'del')), S('font-size:12px;color:var(--ui-text-muted)', e.tk))
|
||||
const Row = e => {
|
||||
const hov = s.tip === e.id, sel = s.panel === e.id
|
||||
return D('position:relative;z-index:' + (hov ? 3 : 1) + ';display:grid;grid-template-columns:' + cols + ';align-items:center;min-height:52px;padding-right:16px;border-top:1px solid var(--ui-border);background:' + (sel || hov ? 'var(--ui-bg-elevated)' : 'var(--ui-bg)') + ';' + (e.sec ? 'box-shadow:inset 2px 0 0 var(--ui-warning);' : ''),
|
||||
S('padding-left:16px;position:relative;color:var(--ui-text-toned);white-space:nowrap', e.rel, hov && Tip(e)),
|
||||
!user && D('display:flex;align-items:center;gap:8px;min-width:0;padding-right:12px', Av(e.a), S('font-weight:500;color:var(--ui-text-highlighted);' + ell, e.a.n), ActorBd(e.a)),
|
||||
D('display:flex;align-items:center;gap:10px;min-width:0;padding-right:16px', S('display:flex;color:var(--ui-text-muted)', I(CI[e.cat], 16)), S('color:var(--ui-text);' + ell, e.s, S('font-weight:600;color:var(--ui-text-highlighted)', e.em), e.tail || ''), e.sec && SecIcon(14)),
|
||||
TargetCell(e), D('display:flex', Out(e)))
|
||||
}
|
||||
const Card = e => {
|
||||
const sel = s.panel === e.id
|
||||
return D('display:flex;flex-direction:column;gap:8px;padding:12px 16px;border-top:1px solid var(--ui-border);background:' + (sel ? 'var(--ui-bg-elevated)' : 'var(--ui-bg)') + ';' + (e.sec ? 'box-shadow:inset 2px 0 0 var(--ui-warning)' : ''),
|
||||
D('display:flex;align-items:center;gap:8px;min-width:0', !user && Av(e.a, 24), !user && S('font-weight:500;color:var(--ui-text-highlighted);' + ell, e.a.n), !user && ActorBd(e.a), D('flex:1'), S('color:var(--ui-text-muted);font-size:12px;white-space:nowrap', e.rel)),
|
||||
D('display:flex;gap:8px;align-items:flex-start', S('display:flex;padding-top:2px;color:var(--ui-text-muted)', I(CI[e.cat], 16)), S('flex:1;min-width:0;color:var(--ui-text);font-size:15px;line-height:22px;overflow-wrap:anywhere;display:-webkit-box;-webkit-line-clamp:3;-webkit-box-orient:vertical;overflow:hidden', e.s, S('font-weight:600;color:var(--ui-text-highlighted)', e.em), e.tail || '')),
|
||||
D('display:flex;align-items:center;gap:6px;flex-wrap:wrap', Out(e), e.sec && Bd('Security', 'sec'), e.tk && !e.ex && Bd('Target deleted', 'del')))
|
||||
}
|
||||
const rowsOf = fn => { const out = []; let day = null; list.forEach(e => { if (e.day !== day) { out.push(Day(e.day)); day = e.day } out.push(fn(e)) }); return out }
|
||||
const SkelRows = () => Array.from({ length: m ? 5 : 9 }, (_, i) => m
|
||||
? D('display:flex;flex-direction:column;gap:10px;padding:14px 16px;border-top:1px solid var(--ui-border)', D('display:flex;gap:8px;align-items:center', sk('24px', 24), sk('90px', 12), D('flex:1'), sk('40px', 10)), sk(['90%', '75%', '85%'][i % 3], 14), sk('70px', 20))
|
||||
: D('display:grid;grid-template-columns:' + cols + ';align-items:center;height:52px;padding-right:16px;border-top:1px solid var(--ui-border)', S('padding-left:16px', sk('56px')), !user && D('display:flex;gap:8px;align-items:center', sk('24px', 24), sk('90px')), sk(['70%', '55%', '82%'][i % 3], 12), sk('60%'), sk('72px', 20)))
|
||||
const Table = () => D('flex:1;min-height:0;overflow:hidden;display:flex;flex-direction:column;background:var(--ui-bg);border:1px solid var(--ui-border)', !m && TableHead(), ...(s.state === 'loading' ? (m ? [] : []).concat(SkelRows()) : rowsOf(m ? Card : Row)))
|
||||
const PgBtn = (c, on, dis) => S('width:' + bh + 'px;height:' + bh + 'px;display:flex;align-items:center;justify-content:center;border:1px solid ' + (on ? 'var(--ui-primary)' : 'var(--ui-border-accented)') + ';background:' + (on ? 'var(--ui-primary)' : 'var(--ui-bg)') + ';color:' + (on ? 'var(--ui-bg)' : 'var(--ui-text-highlighted)') + ';font-weight:500;flex-shrink:0;opacity:' + (dis ? 0.4 : 1), c)
|
||||
const Pager = () => s.state === 'loading' ? null : m
|
||||
? D('display:flex;align-items:center;gap:8px;flex-shrink:0', PgBtn(I('chevron-left'), 0, 1), D('flex:1;display:flex;flex-direction:column;align-items:center;font-size:12px;color:var(--ui-text-muted)', S('color:var(--ui-text-highlighted);font-weight:500', 'Page 1 of ' + (user ? '2' : '52')), S('', '1–25 of ' + total)), D('width:96px;flex-shrink:0', Sel('25', '', 0)), PgBtn(I('chevron-right')))
|
||||
: D('display:flex;align-items:center;gap:16px;flex-shrink:0', S('color:var(--ui-text-muted)', 'Showing 1–' + (user ? '25' : '25') + ' of ' + total), D('flex:1'), D('display:flex;align-items:center;gap:8px;color:var(--ui-text-muted)', 'Rows per page', D('width:80px', Sel('25', '', 0))), D('display:flex;gap:4px', PgBtn(I('chevron-left'), 0, 1), PgBtn('1', 1), PgBtn('2'), PgBtn('3'), PgBtn('…'), PgBtn(user ? '2' : '52'), PgBtn(I('chevron-right'))))
|
||||
|
||||
// ---------- header + page
|
||||
const title = user ? 'My activity' : 'Activity'
|
||||
const sub = user ? 'Everything you\'ve done on this site.' : 'Who did what, and when, across your site.'
|
||||
const noData = s.state === 'none'
|
||||
const Header = () => D('display:flex;align-items:flex-end;gap:16px;flex-wrap:wrap;flex-shrink:0', D('flex:1;min-width:180px;display:flex;flex-direction:column;gap:2px', S('font:600 20px/28px \'IBM Plex Sans\',sans-serif;color:var(--ui-text-highlighted)', title), S('color:var(--ui-text-muted)', sub)), !user && s.state !== 'forbidden' && B('Export CSV', { icon: 'download', dis: noData || s.state === 'error' }))
|
||||
const Page = () => {
|
||||
const k = [Header()]
|
||||
if (s.state === 'forbidden') { k.push(Empty('lock', 'You can\'t view this activity', 'Your role doesn\'t include this page. Ask an owner or admin if you need access.', 'Back to home', false)); return k }
|
||||
const st = Strip(); if (st) k.push(st)
|
||||
k.push(FilterBar()); const c = Chips(); if (c) k.push(c)
|
||||
if (s.state === 'error') k.push(Alert('error', 'circle-x', 'Couldn\'t load activity', 'Check your connection and try again.', 'Try again'))
|
||||
else if (s.state === 'none') k.push(Empty('history', 'No activity yet', user ? 'Your actions will appear here once you start working on the site.' : 'Actions on articles, media, users and API keys will appear here.'))
|
||||
else if (s.state === 'nomatch') k.push(Empty('search-x', 'No activity matches these filters', 'Try a different search or a wider date range.', 'Clear filters', false))
|
||||
else { k.push(Table()); const pg = Pager(); if (pg) k.push(pg) }
|
||||
return k
|
||||
}
|
||||
|
||||
// ---------- slide-over
|
||||
const Av2 = Av
|
||||
const Panel = () => {
|
||||
const e = E.find(x => x.id === s.panel), i = list.indexOf(e)
|
||||
const lab = t => S('color:var(--ui-text-muted)', t)
|
||||
const val = (...c) => D('display:flex;align-items:center;gap:8px;flex-wrap:wrap;min-width:0;color:var(--ui-text-highlighted)', ...c)
|
||||
const Diff = d => {
|
||||
if (d.note) return D('display:flex;flex-direction:column;gap:6px;padding:12px;border-top:1px solid var(--ui-border-muted)', S('font-weight:500;color:var(--ui-text-highlighted)', d.f), S('display:flex;align-items:center;gap:6px;color:var(--ui-text-toned)', I('file-pen-line', 14), d.note))
|
||||
const clamp = d.long && !s.expand
|
||||
const box = (t, bg, bd, c) => S('display:block;padding:8px 10px;background:' + bg + ';border:1px solid ' + bd + ';color:' + c + ';overflow-wrap:anywhere;' + (clamp ? 'display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden' : ''), t)
|
||||
return D('display:flex;flex-direction:column;gap:8px;padding:12px;border-top:1px solid var(--ui-border-muted)', S('font-weight:500;color:var(--ui-text-highlighted)', d.f),
|
||||
D('display:grid;grid-template-columns:' + (m ? 'minmax(0,1fr)' : 'minmax(0,1fr) 16px minmax(0,1fr)') + ';gap:' + (m ? 6 : 8) + 'px;align-items:' + (m ? 'stretch' : 'start'), D('display:flex;flex-direction:column;gap:2px;min-width:0', S('font-size:11px;color:var(--ui-text-muted)', 'Before'), box(d.b, 'var(--ui-bg-elevated)', 'var(--ui-border)', 'var(--ui-text-toned)')), m ? S('display:flex;justify-content:center;color:var(--ui-text-muted)', I('arrow-down', 14)) : S('display:flex;padding-top:30px;color:var(--ui-text-muted)', I('arrow-right', 14)), D('display:flex;flex-direction:column;gap:2px;min-width:0', S('font-size:11px;color:var(--ui-text-muted)', 'After'), box(d.a, 'var(--ui-bg)', 'var(--ui-border-accented)', 'var(--ui-text-highlighted)'))),
|
||||
d.long && S('align-self:flex-start;font-weight:500;font-size:12px;min-height:' + (m ? 32 : 16) + 'px;' + link, clamp ? 'Show more' : 'Show less'))
|
||||
}
|
||||
const body = D('flex:1;min-height:0;overflow:hidden;padding:' + (m ? 16 : 20) + 'px;display:flex;flex-direction:column;gap:20px',
|
||||
D('display:flex;flex-direction:column;gap:10px', S('font:600 18px/26px \'IBM Plex Sans\',sans-serif;color:var(--ui-text-highlighted);overflow-wrap:anywhere', sentence(e)), D('display:flex;gap:6px;flex-wrap:wrap;align-items:center', Out(e), e.sec && Bd('Security', 'sec'), S('display:inline-flex;align-items:center;gap:4px;font-size:12px;color:var(--ui-text-muted)', I(CI[e.cat], 14), CL[e.cat]))),
|
||||
!e.ok && Alert('error', 'circle-x', 'Why it failed', e.why),
|
||||
D('display:grid;grid-template-columns:96px minmax(0,1fr);gap:10px 12px;align-items:start', lab('When'), D('display:flex;flex-direction:column;gap:2px', S('color:var(--ui-text-highlighted)', e.abs + ' (your time)'), S('color:var(--ui-text-muted);font-size:12px', e.utc)), lab('Actor'), val(Av(e.a, 24), S('font-weight:500', e.a.n), ActorBd(e.a)), lab('Source'), val(e.src), lab('IP address'), e.ip ? S(MONO + ';font-size:12px;color:var(--ui-text-highlighted)', e.ip) : S('color:var(--ui-text-muted)', 'Not applicable'), lab('Browser'), e.ua ? S('color:var(--ui-text-highlighted)', e.ua) : S('color:var(--ui-text-muted)', 'Not applicable')),
|
||||
e.tk && D('display:flex;flex-direction:column;gap:8px', S('font:600 14px/22px \'IBM Plex Sans\',sans-serif;color:var(--ui-text-highlighted)', 'Target'), D('display:flex;flex-direction:column;gap:6px;border:1px solid var(--ui-border);padding:10px 12px', D('display:flex;align-items:center;gap:8px;min-width:0', e.ex ? S('flex:1;display:flex;align-items:center;gap:4px;overflow-wrap:anywhere;' + link, e.em, I('arrow-up-right', 14, 'color:var(--ui-text-muted)')) : S('flex:1;text-decoration:line-through;color:var(--ui-text-muted);overflow-wrap:anywhere', e.em), !e.ex && Bd('Deleted', 'del')), S('font-size:12px;color:var(--ui-text-muted)', e.tk + ' · ' + e.tid), !e.ex && S('font-size:12px;color:var(--ui-text-toned)', 'This item was deleted. Its name is kept for reference.'))),
|
||||
e.diff && D('display:flex;flex-direction:column;gap:8px', S('font:600 14px/22px \'IBM Plex Sans\',sans-serif;color:var(--ui-text-highlighted)', 'Changes'), D('border:1px solid var(--ui-border)', ...e.diff.map((d, j) => { const r = Diff(d); return r }))))
|
||||
const foot = D('flex-shrink:0;display:flex;align-items:center;gap:8px;padding:12px ' + (m ? 16 : 20) + 'px;border-top:1px solid var(--ui-border)', B('Previous event', { icon: 'chevron-up', dis: i === 0, full: m }), B('Next event', { icon: 'chevron-down', dis: i === list.length - 1, full: m }), !m && D('flex:1'), !m && S('color:var(--ui-text-muted);font-size:12px', 'Event ' + (i + 1) + ' of ' + total))
|
||||
const head = D('height:56px;flex-shrink:0;display:flex;align-items:center;gap:12px;padding:0 ' + (m ? 8 : 20) + 'px 0 ' + (m ? 16 : 20) + 'px;border-bottom:1px solid var(--ui-border)', S('flex:1;font:600 16px/24px \'IBM Plex Sans\',sans-serif;color:var(--ui-text-highlighted)', 'Activity details'), m && S('color:var(--ui-text-muted);font-size:12px', (i + 1) + ' of ' + total), S('width:' + (m ? 44 : 28) + 'px;height:' + (m ? 44 : 28) + 'px;display:flex;align-items:center;justify-content:center;color:var(--ui-text-muted)', I('x')))
|
||||
return D('position:absolute;inset:0;z-index:40;display:flex;justify-content:' + (m ? 'stretch' : 'flex-end') + ';align-items:' + (m ? 'flex-end' : 'stretch') + ';background:var(--rc-overlay)',
|
||||
D((m ? 'width:100%;height:88%;' : 'width:480px;height:100%;') + 'background:var(--ui-bg);display:flex;flex-direction:column;box-shadow:var(--rc-shadow-modal)', m && D('flex-shrink:0;display:flex;justify-content:center;padding-top:8px', S('display:block;width:36px;height:4px;background:var(--ui-border-accented)')), head, body, foot))
|
||||
}
|
||||
const Drawer = () => {
|
||||
const sec = (l, c) => D('display:flex;flex-direction:column;gap:6px', S('font-weight:500;color:var(--ui-text-highlighted)', l), c)
|
||||
return D('position:absolute;inset:0;z-index:40;display:flex;align-items:flex-end;background:var(--rc-overlay)', D('width:100%;background:var(--ui-bg);display:flex;flex-direction:column;box-shadow:var(--rc-shadow-modal)',
|
||||
D('display:flex;justify-content:center;padding-top:8px', S('display:block;width:36px;height:4px;background:var(--ui-border-accented)')),
|
||||
D('height:56px;display:flex;align-items:center;padding:0 8px 0 16px;border-bottom:1px solid var(--ui-border)', S('flex:1;font:600 16px/24px \'IBM Plex Sans\',sans-serif;color:var(--ui-text-highlighted)', 'Filters'), S('width:44px;height:44px;display:flex;align-items:center;justify-content:center;color:var(--ui-text-muted)', I('x'))),
|
||||
D('padding:16px;display:flex;flex-direction:column;gap:16px', !user && sec('Person', Sel(F.actor, 'All people', 0, 'user-round')), sec('Category', Sel(F.cat, 'All categories', 0, 'layout-list')), sec('Outcome', Seg(['All', 'Succeeded', 'Failed'], F.out, 1)), sec('Dates', D('display:flex;flex-direction:column;gap:8px', Seg(['Today', '7 days', '30 days', 'Custom'], F.range, 1), F.range === 'Custom' && D('display:flex;gap:8px;align-items:center', D('flex:1', Sel('1 Sep 2026', '', 0, 'calendar')), D('flex:1', Sel('30 Sep 2026', '', 0, 'calendar')))))),
|
||||
D('display:flex;gap:8px;padding:12px 16px 16px;border-top:1px solid var(--ui-border)', B('Clear all', { full: true, dis: nF === 0 }), B('Show ' + (s.count || '23') + ' results', { k: 'primary', full: true }))))
|
||||
}
|
||||
|
||||
// ---------- home + settings
|
||||
const Rows5 = (ls, own) => ls.map((e, i) => D('display:flex;gap:10px;align-items:flex-start;padding:10px 0;border-top:' + (i ? '1px solid var(--ui-border-muted)' : '0'), Av(e.a, 24), D('flex:1;min-width:0;display:flex;flex-direction:column;gap:2px', S('color:var(--ui-text);overflow-wrap:anywhere;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden', e.s, S('font-weight:600;color:var(--ui-text-highlighted)', e.em), e.tail || ''), S('font-size:12px;color:var(--ui-text-muted)', own ? e.rel : e.a.n + ' · ' + e.rel)), !e.ok && Bd('Failed', 'fail'), e.sec && e.ok && SecIcon(14)))
|
||||
const ActCard = () => {
|
||||
const own = user, ls = own ? E.filter(e => e.a.n === 'Marta Alves') : E.slice(0, 5)
|
||||
const st = s.card || 'ok'
|
||||
const inner = st === 'loading' ? Array.from({ length: 5 }, (_, i) => D('display:flex;gap:10px;padding:10px 0;border-top:' + (i ? '1px solid var(--ui-border-muted)' : '0'), sk('24px', 24), D('flex:1;display:flex;flex-direction:column;gap:6px', sk(['85%', '70%', '90%', '60%', '78%'][i], 12), sk('35%', 10))))
|
||||
: st === 'error' ? [Alert('error', 'circle-x', 'Couldn\'t load recent activity', null, 'Try again')]
|
||||
: st === 'empty' ? [D('padding:24px 0;display:flex;flex-direction:column;align-items:center;gap:6px;text-align:center', S('color:var(--ui-text-muted)', I('history', 20)), S('font-weight:600;color:var(--ui-text-highlighted)', 'No activity yet'), S('color:var(--ui-text-muted);max-width:240px', own ? 'Your actions will appear here.' : 'Actions on your site will appear here.'))]
|
||||
: Rows5(ls, own)
|
||||
return D('background:var(--ui-bg);border:1px solid var(--ui-border);display:flex;flex-direction:column;min-width:0', D('display:flex;align-items:center;gap:8px;padding:12px 16px;border-bottom:1px solid var(--ui-border)', S('flex:1;font:600 14px/22px \'IBM Plex Sans\',sans-serif;color:var(--ui-text-highlighted)', own ? 'Your recent activity' : 'Recent activity'), S('display:flex;align-items:center;gap:4px;font-weight:500;min-height:' + (m ? 44 : 20) + 'px;' + link, 'View all', I('arrow-right', 14))), D('padding:4px 16px 6px;display:flex;flex-direction:column', ...inner))
|
||||
}
|
||||
const Ph = (t, hh) => D('height:' + hh + 'px;border:1px dashed var(--ui-border-accented);display:flex;align-items:center;justify-content:center;color:var(--ui-text-muted);text-align:center;padding:16px', t)
|
||||
const HomeBody = () => [D('display:flex;flex-direction:column;gap:2px', S('font:600 20px/28px \'IBM Plex Sans\',sans-serif;color:var(--ui-text-highlighted)', 'Home'), S('color:var(--ui-text-muted)', 'Welcome back, ' + ME.split(' ')[0] + '.')), m ? D('display:flex;flex-direction:column;gap:16px', ActCard(), Ph('Existing Home cards', 160)) : D('display:grid;grid-template-columns:minmax(0,1fr) 420px;gap:16px;align-items:start', Ph('Existing Home cards', 420), ActCard())]
|
||||
const RET = { '30': '30 days', '90': '90 days', '365': '365 days', '0': 'Forever' }
|
||||
const SettingsBody = () => {
|
||||
const r = s.ret || '90', fv = r === '0'
|
||||
const dl = (k, v) => D('display:flex;flex-direction:' + (m ? 'column' : 'row') + ';gap:' + (m ? 0 : 12) + 'px', S('width:' + (m ? 'auto' : '120px') + ';color:var(--ui-text-muted);flex-shrink:0', k), S('color:var(--ui-text-highlighted)', v))
|
||||
const card = D('background:var(--ui-bg);border:1px solid var(--ui-border);max-width:640px;display:flex;flex-direction:column',
|
||||
D('display:flex;align-items:center;gap:8px;padding:12px 16px;border-bottom:1px solid var(--ui-border)', D('flex:1;display:flex;flex-direction:column', S('font:600 14px/22px \'IBM Plex Sans\',sans-serif;color:var(--ui-text-highlighted)', 'Retention'), S('color:var(--ui-text-muted);font-size:12px', 'How long activity is kept.')), s.saved && S('display:flex;align-items:center;gap:4px;color:var(--ui-success);font-size:12px', I('check', 14), 'Saved')),
|
||||
D('padding:16px;display:flex;flex-direction:column;gap:16px',
|
||||
s.err && Alert('error', 'circle-x', 'Couldn\'t save the retention period', 'Your previous setting is still in place.', 'Try again'),
|
||||
D('display:flex;flex-direction:column;gap:6px;max-width:260px', S('font-weight:500;color:var(--ui-text-highlighted)', 'Keep activity for'), Sel(RET[s.shorten ? '30' : r], '', 0), S('color:var(--ui-text-muted);font-size:12px', fv ? 'Nothing is removed automatically. The log grows over time.' : 'Older events are removed automatically.')),
|
||||
s.shorten && Alert('warning', 'triangle-alert', 'Events older than 30 days will be removed', 'This happens at the next cleanup on 7 Oct 2026, 03:00. It can\'t be undone.', ['Apply 30 days', 'Cancel']),
|
||||
Alert('info', 'lock', 'Activity logs are append-only', 'Events can\'t be edited or deleted from the app. Only the retention period removes them.'),
|
||||
D('display:flex;flex-direction:column;gap:6px;padding-top:12px;border-top:1px solid var(--ui-border)', dl('Last cleanup', fv ? 'Never' : '6 Oct 2026, 03:00 · 142 events removed'), dl('Next cleanup', fv ? 'Not scheduled' : '7 Oct 2026, 03:00'))))
|
||||
return [D('display:flex;flex-direction:column;gap:2px', S('font:600 20px/28px \'IBM Plex Sans\',sans-serif;color:var(--ui-text-highlighted)', 'Settings'), S('color:var(--ui-text-muted)', 'Site-wide options. Only owners can change them.')), Ph('Existing Settings cards', m ? 100 : 120), card]
|
||||
}
|
||||
|
||||
// ---------- shell
|
||||
const logo = h('svg', { width: 26, height: 26, viewBox: '0 0 16 16', shapeRendering: 'crispEdges', style: { color: 'var(--ui-text-highlighted)', flexShrink: 0 } }, h('rect', { width: 16, height: 16, fill: 'currentColor' }), h('path', { fill: 'var(--ui-bg)', fillRule: 'evenodd', d: 'M4 3h6v1h1v1h1v3h-1v1h-1v1h1v1h1v2h-2v-2h-1v-1H6v3H4zm2 2v3h4V5z' }))
|
||||
const kind = s.kind || 'page'
|
||||
const navKey = kind === 'home' ? 'home' : kind === 'settings' ? 'settings' : 'activity'
|
||||
const nav = (user ? [['home', 'house', 'Home'], ['articles', 'file-text', 'Articles'], ['media', 'images', 'Media'], ['activity', 'history', 'Activity']] : [['home', 'house', 'Home'], ['articles', 'file-text', 'Articles'], ['media', 'images', 'Media'], ['users', 'users', 'Users'], ['activity', 'history', 'Activity'], ['integration', 'plug', 'Integration'], ['settings', 'settings', 'Settings']])
|
||||
const side = D('width:224px;flex-shrink:0;background:var(--ui-bg);border-right:1px solid var(--ui-border);display:flex;flex-direction:column;padding:12px;gap:2px', D('display:flex;align-items:center;gap:10px;height:40px;padding:0 6px;margin-bottom:10px', logo, S('font-weight:600;font-size:14px;color:var(--ui-text-highlighted)', 'RenewCMS')),
|
||||
...nav.map(([k, ic, t]) => D('display:flex;align-items:center;gap:10px;height:32px;padding:0 10px;background:' + (k === navKey ? 'var(--rc-sand)' : 'transparent') + ';color:' + (k === navKey ? 'var(--rc-on-sand)' : 'var(--ui-text-toned)') + ';font-weight:' + (k === navKey ? 500 : 400), I(ic), t)),
|
||||
D('flex:1'), D('display:flex;align-items:center;gap:10px;height:40px;padding:0 6px;color:var(--ui-text-highlighted)', Av(U(ME), 26), S('flex:1;font-weight:500', ME), I('chevrons-up-down', 14)))
|
||||
const crumb = { page: title, home: 'Home', settings: 'Settings' }[kind]
|
||||
const topbar = m
|
||||
? D('height:56px;flex-shrink:0;border-bottom:1px solid var(--ui-border);background:var(--ui-bg);display:flex;align-items:center;padding:0 6px;gap:4px;color:var(--ui-text-highlighted)', S('width:44px;height:44px;display:flex;align-items:center;justify-content:center', I('menu', 20)), S('flex:1;font-weight:600;font-size:16px', crumb))
|
||||
: D('height:48px;flex-shrink:0;border-bottom:1px solid var(--ui-border);background:var(--ui-bg);display:flex;align-items:center;padding:0 24px;gap:8px;color:var(--ui-text-muted)', I('panel-left-close'), S('width:1px;height:16px;background:var(--ui-border);margin:0 6px'), S('color:var(--ui-text-highlighted);font-weight:500', crumb))
|
||||
const kids = kind === 'home' ? HomeBody() : kind === 'settings' ? SettingsBody() : Page()
|
||||
const content = D('flex:1;min-height:0;overflow:hidden;padding:' + (m ? 16 : 24) + 'px;display:flex;flex-direction:column;gap:16px', ...kids)
|
||||
const root = D('width:' + (m ? 390 : 1440) + 'px;height:' + (m ? 844 : 900) + 'px;position:relative;display:flex;flex-direction:' + (m ? 'column' : 'row') + ';background:var(--ui-bg-muted);color:var(--ui-text);font-family:\'IBM Plex Sans\',sans-serif;font-size:13px;line-height:20px;overflow:hidden',
|
||||
!m && side, D('flex:1;min-width:0;min-height:0;display:flex;flex-direction:column', topbar, content), s.panel && Panel(), m && s.drawer && Drawer())
|
||||
return { view: root }
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -144,9 +144,10 @@ class Component extends DCLogic {
|
||||
if (s.empty === 'first') { parts.push(Empty('images', 'No images yet', 'Upload your first image. You can use it in any article.', 'Upload an image', true), S('align-self:center;color:var(--ui-text-muted);font-size:12px', 'PNG, JPEG, WEBP, SVG · max 10 MB each')); return parts }
|
||||
if (s.limited) parts.push(Alert('info', 'timer', 'Too many uploads', 'For your security, wait a moment before uploading again.'))
|
||||
if (s.error) parts.push(Alert('error', 'circle-x', 'Couldn\'t load your images', 'Check your connection and try again.', 'Try again'))
|
||||
const pb = tab === 'dups' && !s.member && !s.dupEmpty && B('Purge duplicates', { icon: 'merge', dis: s.offline, full: m })
|
||||
const tb = m
|
||||
? D('display:flex;flex-direction:column;gap:12px', Search(), TabRow(), D('display:flex;gap:8px', Sort(), Toggle()))
|
||||
: D('display:flex;align-items:flex-end;gap:12px;border-bottom:1px solid var(--ui-border)', TabRow(), S('display:flex;padding-bottom:6px;gap:8px', Search(), Sort(), Toggle()))
|
||||
? D('display:flex;flex-direction:column;gap:12px', Search(), TabRow(), D('display:flex;gap:8px', Sort(), Toggle()), pb && D('display:flex', pb))
|
||||
: D('display:flex;align-items:flex-end;gap:12px;border-bottom:1px solid var(--ui-border)', TabRow(), S('display:flex;padding-bottom:6px;gap:8px', Search(), Sort(), pb, Toggle()))
|
||||
parts.push(tb)
|
||||
if (UP) parts.push(UpPanel())
|
||||
if (s.loading) parts.push(Skel())
|
||||
@@ -189,6 +190,7 @@ class Component extends DCLogic {
|
||||
if (k === 'delUsed') return { title: 'Delete an image that\'s in use?', desc: '"north-wall.jpg" is used in 2 articles. If you delete it, they will show a broken image. This can\'t be undone.', body: [ArtBox(usedList(2))], cancel: 'Cancel', confirm: md.loading ? 'Deleting…' : 'Delete image', tone: 'danger' }
|
||||
if (k === 'bulk') return { title: 'Delete 3 images?', desc: '2 of them are used in 5 articles, which will show a broken image. The other one isn\'t used anywhere. This can\'t be undone.', body: [D('display:flex;flex-direction:column;border:1px solid var(--ui-border)', ...[['north-wall.jpg', 'Used in 2'], ['kitchen-after.webp', 'Used in 3'], ['window-frame.jpg', 'Unused']].map(([n, u], i) => D('display:flex;align-items:center;gap:8px;min-height:36px;padding:0 12px;border-top:' + (i ? '1px solid var(--ui-border-muted)' : '0'), S('flex:1;color:var(--ui-text-highlighted);overflow:hidden;text-overflow:ellipsis;white-space:nowrap', n), Bd(u === 'Unused' ? 'Unused' : u, u === 'Unused' ? 'unused' : 'used'))))], cancel: 'Cancel', confirm: md.loading ? 'Deleting…' : 'Delete 3 images', tone: 'danger' }
|
||||
if (k === 'merge') return { title: 'Merge into floorboards.jpg?', desc: 'Articles that use the other 2 copies will be switched to floorboards.jpg, and the other 2 copies will be deleted. 5 articles will be updated. This can\'t be undone.', body: [D('display:flex;gap:12px;align-items:center;border:1px solid var(--ui-border);padding:10px 12px', Th(byId(12), 'width:56px;height:42px;flex-shrink:0;border:1px solid var(--ui-border)', 16), D('display:flex;flex-direction:column;min-width:0', S('font-weight:500;color:var(--ui-text-highlighted)', 'floorboards.jpg'), S('font-size:12px;color:var(--ui-text-muted)', 'Kept · used in 4 articles now')))], cancel: 'Cancel', confirm: md.loading ? 'Merging…' : 'Merge 2 copies', tone: 'danger' }
|
||||
if (k === 'purge') return { title: 'Purge duplicates?', desc: 'Articles that use a duplicate will be switched to the most used copy of that image, and the other copies will be deleted. This can\'t be undone.', body: [S('font-weight:500;color:var(--ui-text-highlighted)', '4 groups · 9 copies will be deleted · 12 articles will be updated'), D('border:1px solid var(--ui-border)', ...[[12, 'floorboards.jpg', 4, '2 copies'], [7, 'IMG_4471.jpg', 2, '1 copy'], [2, 'kitchen-after.webp', 3, '3 copies'], [4, 'logo-renew.svg', 5, '3 copies']].map(([id, n, u, c], i) => D('display:flex;align-items:center;gap:12px;padding:8px 12px;border-top:' + (i ? '1px solid var(--ui-border-muted)' : '0'), Th(byId(id), 'width:40px;height:30px;flex-shrink:0;border:1px solid var(--ui-border)', 16), D('flex:1;min-width:0;display:flex;flex-direction:column', S('font-weight:500;color:var(--ui-text-highlighted);overflow:hidden;text-overflow:ellipsis;white-space:nowrap', n), S('font-size:12px;color:var(--ui-text-muted)', 'Kept · used in ' + pl(u))), S('font-size:12px;color:var(--ui-text-toned);white-space:nowrap', c + ' merged')))), S('display:inline-flex;align-items:center;gap:4px;align-self:flex-start;font-weight:500;color:var(--ui-text-highlighted);text-decoration:underline;text-underline-offset:3px;min-height:' + (m ? 44 : 20) + 'px', 'Review in Duplicates', I('arrow-right', 14))], cancel: 'Cancel', confirm: 'Purge 4 groups', tone: 'danger' }
|
||||
if (k === 'remove') return { title: 'Remove this image from the article?', desc: 'It\'s removed from this article only. The file stays in your library and in any other article that uses it.', body: [D('display:flex;gap:12px;align-items:center;border:1px solid var(--ui-border);padding:10px 12px', Th(byId(1), 'width:56px;height:42px;flex-shrink:0;border:1px solid var(--ui-border)', 16), D('display:flex;flex-direction:column;min-width:0', S('font-weight:500;color:var(--ui-text-highlighted)', 'north-wall.jpg'), S('font-size:12px;color:var(--ui-text-muted)', 'Stays in the library · 3.9 MB')))], cancel: 'Cancel', confirm: 'Remove image' }
|
||||
}
|
||||
const Shell = (o, body) => D('position:absolute;inset:0;z-index:50;background:var(--rc-overlay);display:flex;align-items:' + (m ? 'flex-end' : 'flex-start') + ';justify-content:center;padding:' + (m ? 16 : o.top || 140) + 'px 16px 16px',
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
|
||||
</div>
|
||||
</x-dc>
|
||||
<script type="text/x-dc" data-dc-script data-props="{"theme":{"editor":"enum","options":["system","light","dark"],"default":"light","tsType":"'system' | 'light' | 'dark'"}}">
|
||||
<script type="text/x-dc" data-dc-script data-props="{"theme":{"editor":"enum","options":["system","light","dark"],"default":"system","tsType":"'system' | 'light' | 'dark'"}}">
|
||||
class Component extends DCLogic {
|
||||
state = { mode: this.props.theme ?? 'light' }
|
||||
rootRef = React.createRef()
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<script src="./support.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<x-dc>
|
||||
<helmet>
|
||||
<meta name="design_doc_mode" content="canvas">
|
||||
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;600&family=IBM+Plex+Mono:wght@400;500&display=swap" rel="stylesheet">
|
||||
<style>body{margin:0;font-family:'IBM Plex Sans',sans-serif;font-size:13px;line-height:20px}*{box-sizing:border-box}a{color:inherit;text-underline-offset:3px}a:hover{opacity:.8}</style>
|
||||
</helmet>
|
||||
<div ref="{{ rootRef }}" data-screen-label="Activity screens" style="background:var(--rc-canvas);color:var(--ui-text);padding:64px;display:flex;flex-direction:column;gap:72px;width:max-content;min-width:100%">
|
||||
<div style="display:flex;align-items:flex-end;gap:32px;max-width:1400px">
|
||||
<div style="flex:1;display:flex;flex-direction:column;gap:10px">
|
||||
<h1 style="margin:0;font:600 36px/44px 'IBM Plex Sans',sans-serif;letter-spacing:-0.02em;color:var(--ui-text-highlighted)">Screen: Activity log</h1>
|
||||
<p style="margin:0;max-width:880px;font-size:16px;line-height:26px;color:var(--ui-text-toned);text-wrap:pretty">New /activity screen, a Recent activity card for Home, and a Retention card for Settings. 1440 px at 50%, 390 px at 75%. Owner and admin see everything; regular users get "My activity". The theme switch shows every state in dark mode. Copy, component mapping and API calls are at the bottom.</p>
|
||||
</div>
|
||||
<div role="radiogroup" aria-label="Theme" style="display:flex;border:1px solid var(--ui-border-accented);background:var(--ui-bg)">
|
||||
<sc-for list="{{ modes }}" as="m" hint-placeholder-count="3">
|
||||
<button role="radio" aria-checked="{{ m.on }}" onClick="{{ m.pick }}" style="all:unset;cursor:pointer;height:36px;padding:0 14px;display:flex;align-items:center;font-weight:500;font-size:14px;background:{{ m.bg }};color:{{ m.fg }}" style-focus-visible="outline:2px solid var(--ui-primary);outline-offset:2px">{{ m.label }}</button>
|
||||
</sc-for>
|
||||
</div>
|
||||
</div>
|
||||
<sc-for list="{{ groups }}" as="g" hint-placeholder-count="5">
|
||||
<section data-screen-label="{{ g.label }}" style="display:flex;flex-direction:column;gap:28px">
|
||||
<div style="display:flex;flex-direction:column;gap:6px;max-width:1100px">
|
||||
<h2 style="margin:0;font:600 28px/36px 'IBM Plex Sans',sans-serif;color:var(--ui-text-highlighted)">{{ g.title }}</h2>
|
||||
<div style="display:flex;gap:16px;flex-wrap:wrap;font:400 14px/22px 'IBM Plex Mono',monospace;color:var(--ui-text-toned)"><span>{{ g.route }}</span><span style="color:var(--ui-text-muted)">{{ g.api }}</span></div>
|
||||
</div>
|
||||
<div style="display:flex;flex-wrap:wrap;gap:40px;align-items:flex-start">
|
||||
<sc-for list="{{ g.states }}" as="st" hint-placeholder-count="4">
|
||||
<div style="display:flex;flex-direction:column;gap:12px">
|
||||
<div style="display:flex;flex-direction:column;gap:2px;max-width:1040px"><span style="font:600 16px/24px 'IBM Plex Sans',sans-serif;color:var(--ui-text-highlighted)">{{ st.name }}</span><span style="font-size:13px;color:var(--ui-text-muted)">{{ st.trigger }}</span></div>
|
||||
<div style="display:flex;gap:24px;align-items:flex-start">
|
||||
<div style="width:720px;height:450px;overflow:hidden;border:1px solid var(--ui-border);flex-shrink:0"><div style="width:1440px;height:900px;zoom:0.5"><dc-import name="ActivityFrame" s="{{ st.s }}" mobile="{{ false }}" hint-size="1440px,900px"></dc-import></div></div>
|
||||
<div style="width:293px;height:633px;overflow:hidden;border:1px solid var(--ui-border);flex-shrink:0"><div style="width:390px;height:844px;zoom:0.75"><dc-import name="ActivityFrame" s="{{ st.m }}" mobile="{{ true }}" hint-size="390px,844px"></dc-import></div></div>
|
||||
</div>
|
||||
</div>
|
||||
</sc-for>
|
||||
</div>
|
||||
</section>
|
||||
</sc-for>
|
||||
|
||||
<section style="display:flex;flex-direction:column;gap:20px;max-width:1240px">
|
||||
<h2 style="margin:0;font:600 28px/36px 'IBM Plex Sans',sans-serif;color:var(--ui-text-highlighted)">Copy per state</h2>
|
||||
<div style="background:var(--ui-bg);border:1px solid var(--ui-border)">
|
||||
<sc-for list="{{ copy }}" as="r" hint-placeholder-count="5"><div style="display:grid;grid-template-columns:220px minmax(0,1fr);padding:10px 12px;border-top:1px solid var(--ui-border);line-height:20px"><span style="font:500 13px 'IBM Plex Mono',monospace;color:var(--ui-text-highlighted)">{{ r.a }}</span><span style="color:var(--ui-text-highlighted);text-wrap:pretty">{{ r.b }}</span></div></sc-for>
|
||||
</div>
|
||||
</section>
|
||||
<section style="display:flex;flex-direction:column;gap:20px;max-width:1240px">
|
||||
<h2 style="margin:0;font:600 28px/36px 'IBM Plex Sans',sans-serif;color:var(--ui-text-highlighted)">Component inventory (Nuxt UI v4)</h2>
|
||||
<div style="background:var(--ui-bg);border:1px solid var(--ui-border)">
|
||||
<sc-for list="{{ comps }}" as="r" hint-placeholder-count="5"><div style="display:grid;grid-template-columns:220px minmax(0,1fr);padding:10px 12px;border-top:1px solid var(--ui-border);line-height:20px"><span style="font:500 13px 'IBM Plex Mono',monospace;color:var(--ui-text-highlighted)">{{ r.a }}</span><span style="color:var(--ui-text-highlighted);text-wrap:pretty">{{ r.b }}</span></div></sc-for>
|
||||
</div>
|
||||
</section>
|
||||
<section style="display:flex;flex-direction:column;gap:20px;max-width:1240px">
|
||||
<h2 style="margin:0;font:600 28px/36px 'IBM Plex Sans',sans-serif;color:var(--ui-text-highlighted)">API calls</h2>
|
||||
<div style="background:var(--ui-bg);border:1px solid var(--ui-border)">
|
||||
<sc-for list="{{ api }}" as="r" hint-placeholder-count="5"><div style="display:grid;grid-template-columns:220px minmax(0,1fr);padding:10px 12px;border-top:1px solid var(--ui-border);line-height:20px"><span style="font:500 13px 'IBM Plex Mono',monospace;color:var(--ui-text-highlighted)">{{ r.a }}</span><span style="color:var(--ui-text-highlighted);text-wrap:pretty">{{ r.b }}</span></div></sc-for>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</x-dc>
|
||||
<script type="text/x-dc" data-dc-script data-props="{"theme":{"editor":"enum","options":["system","light","dark"],"default":"light","tsType":"'system' | 'light' | 'dark'"}}">
|
||||
class Component extends DCLogic {
|
||||
state = { mode: this.props.theme ?? 'light', spec: { copy: [], comps: [], api: [] } }
|
||||
rootRef = React.createRef()
|
||||
light = { '--ui-bg': '#ffffff', '--ui-bg-muted': '#fafafa', '--ui-bg-elevated': '#f3f2f0', '--ui-bg-accented': '#e6e4e0', '--ui-bg-inverted': '#111111', '--ui-text-dimmed': '#999999', '--ui-text-muted': '#666666', '--ui-text-toned': '#444444', '--ui-text': '#222222', '--ui-text-highlighted': '#111111', '--ui-text-inverted': '#ffffff', '--ui-border': '#e6e4e0', '--ui-border-muted': '#f3f2f0', '--ui-border-accented': '#8a8783', '--ui-primary': '#111111', '--ui-secondary': '#635643', '--ui-success': '#315b33', '--ui-info': '#34536d', '--ui-warning': '#6c4700', '--ui-error': '#882c21', '--rc-sand': '#e8dfd1', '--rc-on-sand': '#111111', '--rc-success-soft': '#eaefea', '--rc-info-soft': '#ebeef0', '--rc-warning-soft': '#f0ede6', '--rc-error-soft': '#f3eae9', '--rc-overlay': 'rgb(10 10 10 / 0.4)', '--rc-shadow-pop': '0 4px 16px rgb(17 17 17 / 0.08), 0 1px 2px rgb(17 17 17 / 0.06)', '--rc-shadow-modal': '0 16px 48px rgb(17 17 17 / 0.18)', '--rc-canvas': '#ecebe8' }
|
||||
dark = { '--ui-bg': '#111111', '--ui-bg-muted': '#0a0a0a', '--ui-bg-elevated': '#1a1a1a', '--ui-bg-accented': '#222222', '--ui-bg-inverted': '#fafafa', '--ui-text-dimmed': '#666666', '--ui-text-muted': '#999999', '--ui-text-toned': '#cfccc7', '--ui-text': '#e6e4e0', '--ui-text-highlighted': '#fafafa', '--ui-text-inverted': '#111111', '--ui-border': '#333333', '--ui-border-muted': '#222222', '--ui-border-accented': '#666666', '--ui-primary': '#fafafa', '--ui-secondary': '#d4c5b0', '--ui-success': '#a3caa3', '--ui-info': '#a5c2db', '--ui-warning': '#dfb67d', '--ui-error': '#fba394', '--rc-sand': '#d4c5b0', '--rc-on-sand': '#111111', '--rc-success-soft': '#1f241f', '--rc-info-soft': '#202428', '--rc-warning-soft': '#2a251e', '--rc-error-soft': '#2c201f', '--rc-overlay': 'rgb(0 0 0 / 0.6)', '--rc-shadow-pop': '0 4px 16px rgb(0 0 0 / 0.5)', '--rc-shadow-modal': '0 16px 48px rgb(0 0 0 / 0.6)', '--rc-canvas': '#1c1c1c' }
|
||||
resolved() {
|
||||
if (this.state.mode === 'system') return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
||||
return this.state.mode
|
||||
}
|
||||
apply() {
|
||||
const el = this.rootRef.current
|
||||
if (!el) return
|
||||
const vars = this.resolved() === 'dark' ? this.dark : this.light
|
||||
for (const k in vars) el.style.setProperty(k, vars[k])
|
||||
document.body.style.background = vars['--rc-canvas']
|
||||
}
|
||||
componentDidMount() {
|
||||
this.apply()
|
||||
import('./activity-spec.js').then(x => this.setState({ spec: { copy: x.copy, comps: x.comps, api: x.api } })).catch(() => {})
|
||||
}
|
||||
componentDidUpdate(p) {
|
||||
if (p.theme !== this.props.theme && this.props.theme) this.setState({ mode: this.props.theme })
|
||||
this.apply()
|
||||
}
|
||||
groups() {
|
||||
const W = (name, trigger, o = {}, mo = {}) => ({ name, trigger, s: o, m: { ...o, ...mo } })
|
||||
const U = { role: 'user' }
|
||||
const f1 = { cat: 'Authentication', out: 'Failed', range: '30 days' }
|
||||
return [
|
||||
{ label: 'Activity, owner and admin', title: 'Activity, owner and admin', route: '/activity', api: 'GET /v1/activity · GET /v1/activity/summary', states: [
|
||||
W('Default, large dataset', 'Newest first, day separators, a security row, 1,284 events across 52 pages. Hover on the first row shows the exact time.', { tip: 1 }),
|
||||
W('Loading', 'Skeleton strip and table while both requests are in flight.', { state: 'loading' }),
|
||||
W('Zero-value summary strip', 'Quiet period: Failed and Security show a calm 0.', { sum: 'zero' }),
|
||||
W('Filtered, card applied', 'Failed actions card clicked, chips and Clear all. Mobile shows the count on Filters.', { f: f1, card: 2, count: 6, total: '6', ids: [3], sum: 'default' }, {}),
|
||||
W('Mobile filters sheet', 'Filters open as a bottom sheet (desktop uses the inline bar above).', { f: { ...f1, range: 'Custom' }, drawer: 1, count: 6, total: '6', ids: [3] }),
|
||||
W('Empty, no activity yet', 'New site. Export is disabled.', { state: 'none', sum: 'none' }),
|
||||
W('Empty, filters match nothing', 'Offers Clear filters.', { state: 'nomatch', f: { q: 'sitemap', cat: 'Media', out: 'Failed' }, total: '0' }),
|
||||
W('Error with retry', 'Request failed. The summary strip still shows if its call succeeded.', { state: 'error' }),
|
||||
W('Forbidden (403)', 'API returned 403 for this role or filter.', { state: 'forbidden', role: 'user' })
|
||||
] },
|
||||
{ label: 'Details slide-over', title: 'Details slide-over and bottom sheet', route: 'Row click', api: 'Uses the item from GET /v1/activity', states: [
|
||||
W('Edit with a diff', 'Title change and "Body edited (+312 characters)". No body text.', { panel: 2 }),
|
||||
W('Settings diff, long text', 'Long values clamp to two lines with Show more.', { panel: 11 }),
|
||||
W('Failed action, very long target title', 'Failure reason shown, target title wraps.', { panel: 9 }),
|
||||
W('Deleted target, no diff', 'Struck-through target with a Deleted badge.', { panel: 8 }),
|
||||
W('System-initiated, no human actor', 'Scheduler with a System badge. IP and browser not applicable.', { panel: 5 }),
|
||||
W('Security event, failed login', 'Actor not signed in. Restrained shield marker.', { panel: 3 })
|
||||
] },
|
||||
{ label: 'My activity', title: 'My activity, regular user', route: '/activity', api: 'GET /v1/activity (server scoped to the user)', states: [
|
||||
W('Default', 'No summary strip, no actor filter, no Export. Title becomes My activity.', U),
|
||||
W('Empty', 'The user has not done anything yet.', { ...U, state: 'none' }),
|
||||
W('Details', 'Same slide-over, own event.', { ...U, panel: 1 })
|
||||
] },
|
||||
{ label: 'Home card', title: 'Home, Recent activity card', route: '/', api: 'GET /v1/activity?size=5', states: [
|
||||
W('Owner or admin', 'Last 5 events with the actor name.', { kind: 'home' }),
|
||||
W('Regular user', 'Own events only.', { kind: 'home', ...U }),
|
||||
W('Loading', '', { kind: 'home', card: 'loading' }),
|
||||
W('Error', '', { kind: 'home', card: 'error' }),
|
||||
W('Empty', '', { kind: 'home', card: 'empty', ...U })
|
||||
] },
|
||||
{ label: 'Settings retention', title: 'Settings, Retention (owner only)', route: '/settings', api: 'GET /v1/settings · PATCH /v1/settings', states: [
|
||||
W('Default, 90 days', 'Saves on change.', { kind: 'settings', saved: true }),
|
||||
W('Shortening the period', 'Confirms before older events are removed.', { kind: 'settings', ret: '90', shorten: true }),
|
||||
W('Forever', 'No cleanup scheduled.', { kind: 'settings', ret: '0' }),
|
||||
W('Save error', '', { kind: 'settings', err: true })
|
||||
] }
|
||||
]
|
||||
}
|
||||
renderVals() {
|
||||
const cur = this.state.mode
|
||||
const spec = this.state.spec
|
||||
const rows = a => a.map(r => ({ a: r[0], b: r[1] }))
|
||||
return {
|
||||
rootRef: this.rootRef,
|
||||
modes: ['light', 'dark', 'system'].map(k => ({ label: k[0].toUpperCase() + k.slice(1), on: cur === k, bg: cur === k ? 'var(--ui-primary)' : 'var(--ui-bg)', fg: cur === k ? 'var(--ui-bg)' : 'var(--ui-text-highlighted)', pick: () => this.setState({ mode: k }) })),
|
||||
groups: this.groups(),
|
||||
copy: rows(spec.copy), comps: rows(spec.comps), api: rows(spec.api)
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -223,6 +223,8 @@ class Component extends DCLogic {
|
||||
Mp({ upload: upT([rowRej, { n: 'notes.pdf', sz: '210 KB', st: 'rej', title: 'notes.pdf isn\'t an image', msg: 'Images can be up to 10 MB. Use a PNG, JPEG, WEBP or SVG image.' }, { n: 'broken.png', sz: '3.0 MB', st: 'rej', title: 'broken.png couldn\'t be read', msg: 'The server couldn\'t read this image. Save it again as a PNG or JPEG, then try again.' }], { title: 'Nothing was uploaded', sub: '3 files were rejected', done: true }), name: 'Upload: rejected files (413, 400)', trigger: 'Type and size are checked in the browser first, so nothing is sent. A server 413 or 400 for a file that got through shows the same row with its own message.' }),
|
||||
Mp({ limited: true, name: 'Upload: too many requests (429)', trigger: 'Rate limited. Retry-After is counted down in the button. It re-enables at zero. Already-added files are kept.' }),
|
||||
Mp({ tab: 'dups', name: 'Duplicates: groups', trigger: 'The Duplicates tab. Groups of identical images side by side, each with its uploader, upload date and "Used in N articles". The suggested copy to keep is the most used, then the oldest.' }),
|
||||
Mp({ tab: 'dups', modal: { k: 'purge' }, name: 'Purge duplicates: confirm', trigger: '"Purge duplicates" in the toolbar, next to the sort menu (in the stacked toolbar on mobile). Opens the same dialog as Settings; every other state is on the Media cleanup board.' }),
|
||||
Mp({ tab: 'dups', member: true, name: 'Duplicates: no purge button (member)', trigger: 'Verified users who are not owner or admin. The "Purge duplicates" button is hidden. "Merge into this one" is unchanged.' }),
|
||||
Mp({ tab: 'dups', dupEmpty: true, name: 'Duplicates: empty', trigger: 'No groups. The Duplicates tab loses its count badge.' }),
|
||||
Mp({ tab: 'dups', modal: { k: 'merge' }, name: 'Merge: confirm', trigger: '"Merge into this one" on floorboards.jpg. The dialog says articles using the other copies will switch to this image and the other copies will be deleted, with the article count.' }),
|
||||
Mp({ tab: 'dups', modal: { k: 'merge', loading: true }, name: 'Merge: in progress', trigger: 'Request in flight. The dialog can\'t be dismissed.' }),
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<script src="./support.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<x-dc>
|
||||
<helmet>
|
||||
<meta name="design_doc_mode" content="canvas">
|
||||
<link rel="icon" href="brand/favicon.svg">
|
||||
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;600&family=IBM+Plex+Mono:wght@400;500&family=Source+Serif+4:ital,opsz,wght@0,8..60,400;0,8..60,600;1,8..60,400&display=swap" rel="stylesheet">
|
||||
<style>body{margin:0;font-family:'IBM Plex Sans',sans-serif;font-size:13px;line-height:20px}*{box-sizing:border-box}a{color:inherit;text-underline-offset:3px}a:hover{opacity:.8}code{font-family:'IBM Plex Mono',monospace;font-size:12px}</style>
|
||||
</helmet>
|
||||
<div ref="{{ rootRef }}" data-screen-label="Media cleanup screens" style="--ui-bg:#ffffff;--ui-bg-muted:#fafafa;--ui-bg-elevated:#f3f2f0;--ui-bg-accented:#e6e4e0;--ui-bg-inverted:#111111;--ui-text-dimmed:#999999;--ui-text-muted:#666666;--ui-text-toned:#444444;--ui-text:#222222;--ui-text-highlighted:#111111;--ui-text-inverted:#ffffff;--ui-border:#e6e4e0;--ui-border-muted:#f3f2f0;--ui-border-accented:#8a8783;--ui-primary:#111111;--ui-secondary:#635643;--ui-success:#315b33;--ui-info:#34536d;--ui-warning:#6c4700;--ui-error:#882c21;--rc-sand:#e8dfd1;--rc-success-soft:#eaefea;--rc-info-soft:#ebeef0;--rc-warning-soft:#f0ede6;--rc-error-soft:#f3eae9;--rc-overlay:rgb(10 10 10 / 0.4);--rc-shadow-pop:0 4px 16px rgb(17 17 17 / 0.08), 0 1px 2px rgb(17 17 17 / 0.06);--rc-shadow-modal:0 16px 48px rgb(17 17 17 / 0.18);--rc-canvas:#ecebe8;background:var(--rc-canvas);color:var(--ui-text);padding:64px;display:flex;flex-direction:column;gap:72px;width:max-content;min-width:100%">
|
||||
|
||||
<div style="display:flex;align-items:flex-end;gap:32px;max-width:1400px">
|
||||
<div style="flex:1;display:flex;flex-direction:column;gap:10px">
|
||||
<h1 style="margin:0;font:600 36px/44px 'IBM Plex Sans',sans-serif;letter-spacing:-0.02em;color:var(--ui-text-highlighted)">Screen: Media cleanup</h1>
|
||||
<p style="margin:0;max-width:880px;font-size:16px;line-height:26px;color:var(--ui-text-toned);text-wrap:pretty">A Media cleanup card on Settings, a purge dialog, and an entry point on the Media Duplicates tab (also in <a href="RenewCMS Screens Back-office.dc.html">the Back-office board</a>). 1440 px at 50%, 390 px at 75%. The theme switch shows every state in dark mode.</p>
|
||||
</div>
|
||||
<div role="radiogroup" aria-label="Theme" style="display:flex;border:1px solid var(--ui-border-accented);background:var(--ui-bg)">
|
||||
<sc-for list="{{ modes }}" as="m" hint-placeholder-count="3">
|
||||
<button role="radio" aria-checked="{{ m.on }}" onClick="{{ m.pick }}" style="all:unset;cursor:pointer;height:36px;padding:0 14px;display:flex;align-items:center;font-weight:500;font-size:14px;background:{{ m.bg }};color:{{ m.fg }}" style-focus-visible="outline:2px solid var(--ui-primary);outline-offset:2px">{{ m.label }}</button>
|
||||
</sc-for>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<sc-for list="{{ groups }}" as="g" hint-placeholder-count="7">
|
||||
<section data-screen-label="{{ g.label }}" style="display:flex;flex-direction:column;gap:28px">
|
||||
<div style="display:flex;flex-direction:column;gap:6px;max-width:1100px">
|
||||
<h2 style="margin:0;font:600 28px/36px 'IBM Plex Sans',sans-serif;color:var(--ui-text-highlighted)">{{ g.title }}</h2>
|
||||
<div style="display:flex;gap:16px;flex-wrap:wrap;font:400 14px/22px 'IBM Plex Mono',monospace;color:var(--ui-text-toned)"><span>{{ g.route }}</span><span style="color:var(--ui-text-muted)">{{ g.api }}</span></div>
|
||||
<sc-if value="{{ g.note }}"><p style="margin:4px 0 0;font-size:15px;line-height:24px;color:var(--ui-text-toned);text-wrap:pretty">{{ g.note }}</p></sc-if>
|
||||
</div>
|
||||
<div style="display:flex;flex-wrap:wrap;gap:40px;align-items:flex-start">
|
||||
<sc-for list="{{ g.states }}" as="st" hint-placeholder-count="4">
|
||||
<div style="display:flex;flex-direction:column;gap:12px">
|
||||
<div style="display:flex;flex-direction:column;gap:2px;max-width:1040px"><span style="font:600 16px/24px 'IBM Plex Sans',sans-serif;color:var(--ui-text-highlighted)">{{ st.name }}</span><span style="font-size:13px;color:var(--ui-text-muted)">{{ st.trigger }}</span></div>
|
||||
<div style="display:flex;gap:24px;align-items:flex-start">
|
||||
<div style="width:720px;height:450px;overflow:hidden;border:1px solid var(--ui-border);flex-shrink:0"><div style="width:1440px;height:900px;zoom:0.5"><sc-if value="{{ st.notMedia }}"><dc-import name="SettingsFrame" s="{{ st }}" mobile="{{ false }}" hint-size="1440px,900px"></dc-import></sc-if><sc-if value="{{ st.isMedia }}"><dc-import name="MediaFrame" s="{{ st }}" mobile="{{ false }}" hint-size="1440px,900px"></dc-import></sc-if></div></div>
|
||||
<div style="width:293px;height:633px;overflow:hidden;border:1px solid var(--ui-border);flex-shrink:0"><div style="width:390px;height:844px;zoom:0.75"><sc-if value="{{ st.notMedia }}"><dc-import name="SettingsFrame" s="{{ st.m }}" mobile="{{ true }}" hint-size="390px,844px"></dc-import></sc-if><sc-if value="{{ st.isMedia }}"><dc-import name="MediaFrame" s="{{ st.m }}" mobile="{{ true }}" hint-size="390px,844px"></dc-import></sc-if></div></div>
|
||||
</div>
|
||||
</div>
|
||||
</sc-for>
|
||||
</div>
|
||||
<sc-if value="{{ g.table }}"><div style="display:flex;flex-direction:column;gap:8px;max-width:1240px"><span style="font:600 16px/24px 'IBM Plex Sans',sans-serif;color:var(--ui-text-highlighted)">{{ g.tableTitle }}</span><div style="background:var(--ui-bg);border:1px solid var(--ui-border)"><div style="display:grid;grid-template-columns:90px 240px minmax(0,1fr);padding:8px 12px;background:var(--ui-bg-elevated);color:var(--ui-text-muted);font-weight:500"><span>Status</span><span>Where</span><span>Copy</span></div><sc-for list="{{ g.table }}" as="r" hint-placeholder-count="3"><div style="display:grid;grid-template-columns:90px 240px minmax(0,1fr);gap:0;padding:10px 12px;border-top:1px solid var(--ui-border);line-height:20px"><span style="font:500 13px 'IBM Plex Mono',monospace;color:var(--ui-text-highlighted)">{{ r.a }}</span><span style="color:var(--ui-text-toned);padding-right:16px">{{ r.b }}</span><span style="color:var(--ui-text-highlighted);text-wrap:pretty">{{ r.c }}</span></div></sc-for></div></div></sc-if>
|
||||
</section>
|
||||
</sc-for>
|
||||
|
||||
</div>
|
||||
</x-dc>
|
||||
<script type="text/x-dc" data-dc-script data-props="{"theme":{"editor":"enum","options":["system","light","dark"],"default":"system","tsType":"'system' | 'light' | 'dark'"}}">
|
||||
class Component extends DCLogic {
|
||||
state = { mode: this.props.theme ?? 'light' }
|
||||
rootRef = React.createRef()
|
||||
light = { '--ui-bg': '#ffffff', '--ui-bg-muted': '#fafafa', '--ui-bg-elevated': '#f3f2f0', '--ui-bg-accented': '#e6e4e0', '--ui-bg-inverted': '#111111', '--ui-text-dimmed': '#999999', '--ui-text-muted': '#666666', '--ui-text-toned': '#444444', '--ui-text': '#222222', '--ui-text-highlighted': '#111111', '--ui-text-inverted': '#ffffff', '--ui-border': '#e6e4e0', '--ui-border-muted': '#f3f2f0', '--ui-border-accented': '#8a8783', '--ui-primary': '#111111', '--ui-secondary': '#635643', '--ui-success': '#315b33', '--ui-info': '#34536d', '--ui-warning': '#6c4700', '--ui-error': '#882c21', '--rc-sand': '#e8dfd1', '--rc-success-soft': '#eaefea', '--rc-info-soft': '#ebeef0', '--rc-warning-soft': '#f0ede6', '--rc-error-soft': '#f3eae9', '--rc-overlay': 'rgb(10 10 10 / 0.4)', '--rc-shadow-pop': '0 4px 16px rgb(17 17 17 / 0.08), 0 1px 2px rgb(17 17 17 / 0.06)', '--rc-shadow-modal': '0 16px 48px rgb(17 17 17 / 0.18)', '--rc-canvas': '#ecebe8' }
|
||||
dark = { '--ui-bg': '#111111', '--ui-bg-muted': '#0a0a0a', '--ui-bg-elevated': '#1a1a1a', '--ui-bg-accented': '#222222', '--ui-bg-inverted': '#fafafa', '--ui-text-dimmed': '#666666', '--ui-text-muted': '#999999', '--ui-text-toned': '#cfccc7', '--ui-text': '#e6e4e0', '--ui-text-highlighted': '#fafafa', '--ui-text-inverted': '#111111', '--ui-border': '#333333', '--ui-border-muted': '#222222', '--ui-border-accented': '#666666', '--ui-primary': '#fafafa', '--ui-secondary': '#d4c5b0', '--ui-success': '#a3caa3', '--ui-info': '#a5c2db', '--ui-warning': '#dfb67d', '--ui-error': '#fba394', '--rc-sand': '#d4c5b0', '--rc-success-soft': '#1f241f', '--rc-info-soft': '#202428', '--rc-warning-soft': '#2a251e', '--rc-error-soft': '#2c201f', '--rc-overlay': 'rgb(0 0 0 / 0.6)', '--rc-shadow-pop': '0 4px 16px rgb(0 0 0 / 0.5)', '--rc-shadow-modal': '0 16px 48px rgb(0 0 0 / 0.6)', '--rc-canvas': '#1c1c1c' }
|
||||
resolved() {
|
||||
if (this.state.mode === 'system') return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
||||
return this.state.mode
|
||||
}
|
||||
apply() {
|
||||
const el = this.rootRef.current
|
||||
if (!el) return
|
||||
const vars = this.resolved() === 'dark' ? this.dark : this.light
|
||||
for (const k in vars) el.style.setProperty(k, vars[k])
|
||||
document.body.style.background = vars['--rc-canvas']
|
||||
}
|
||||
componentDidMount() { this.apply() }
|
||||
componentDidUpdate(p) {
|
||||
if (p.theme !== this.props.theme && this.props.theme) this.setState({ mode: this.props.theme })
|
||||
this.apply()
|
||||
}
|
||||
groups() {
|
||||
const W = (name, trigger, o = {}, mo = {}) => ({ name, trigger, ...o, m: { ...o, ...mo } })
|
||||
const L = { d: '14 Jun 2026', c: 3, a: 5 }
|
||||
const on = { on: true, days: '30', last: L, next: '14 Jul 2026' }
|
||||
const D = (k, name, trigger, o = {}) => W(name, trigger, { ...on, last: null, on: false, dialog: { k, ...(o.dg || {}) }, ...o.s })
|
||||
const pvMore = { groups: 8, copies: 15, arts: 24, list: [{ im: 12, n: 'floorboards.jpg', u: 4, c: 2 }, { im: 7, n: 'IMG_4471.jpg', u: 2, c: 1 }, { im: 2, n: 'kitchen-after.webp', u: 3, c: 3 }, { im: 4, n: 'logo-renew.svg', u: 5, c: 3 }, { im: 11, n: 'lime-plaster.jpg', u: 2, c: 1 }], more: 3 }
|
||||
const pvOne = { groups: 1, copies: 1, arts: 2, list: [{ im: 7, n: 'IMG_4471.jpg', u: 2, c: 1 }], more: 0 }
|
||||
const pv3 = { groups: 3, copies: 7, arts: 10, list: [{ im: 12, n: 'floorboards.jpg', u: 4, c: 2 }, { im: 2, n: 'kitchen-after.webp', u: 3, c: 3 }, { im: 4, n: 'logo-renew.svg', u: 5, c: 3 }], more: 0 }
|
||||
const base = { last: null }
|
||||
const ST = (o) => ({ ...base, ...o })
|
||||
const Mp = (o) => ({ isMedia: true, kind: 'page', ...o, m: { isMedia: true, kind: 'page', ...o } })
|
||||
return this.finish([
|
||||
{ label: 'Settings', title: 'Settings: Media cleanup', route: '/settings', api: 'GET /v1/media/purge-settings · PATCH /v1/media/purge-settings (owner, admin)', note: 'A second card below Version, 20 px gap, same card styling. Automatic purge is off by default. Settings save immediately on toggle and on blur or Enter of the days field; there is no Save button. 401 and 403 are global (login redirect, /register/pending) and have no local copy.', states: [
|
||||
W('Default: off, never run', 'Owner or admin. Automatic purge is off, so the "Every" row and "Next purge" are hidden.', ST({})),
|
||||
W('On: 30 days, last run', 'Switch on. "Every" appears with the default of 30 days. The last-run line and the next run are shown.', on),
|
||||
W('Saving', 'Brief state after toggling or leaving the field. "Saving…" with loader-circle sits in the card header; controls stay usable.', { ...on, saving: true, focusDays: true }),
|
||||
W('Saved', 'The save succeeded. A small "Saved" confirmation shows in the card header and fades after 2 s. It is announced politely.', { ...on, saved: true }),
|
||||
W('Save failed', 'A 500 or network failure. The error sits under the field and the control reverts to its previous value (30).', { ...on, saveErr: true, days: '30', focusDays: true }),
|
||||
W('Validation error', 'Empty, 0, above 365 or not a whole number, checked on blur or Enter. Nothing is sent. The field keeps what was typed until corrected.', { ...on, days: '400', invalid: true, focusDays: true }),
|
||||
W('Read-only (non-admin)', 'Other verified users. Switch and input are disabled, "Purge now" is hidden, and an info alert explains why.', { ...on, ro: true }),
|
||||
W('Loading skeleton', 'The settings request is in flight. Skeleton shows only after 150 ms.', { loading: true }),
|
||||
W('Offline', 'navigator.onLine is false. The banner matches Media. Switch, input and button are disabled.', { ...on, offline: true })
|
||||
] },
|
||||
{ label: 'Dialog', title: 'Purge duplicates: dialog', route: 'opens from "Purge now" (Settings) and "Purge duplicates" (Media, Duplicates tab)', api: 'GET /v1/media/purge-preview · POST /v1/media/purge', note: 'UModal, sm:max-w-md, bottom sheet on mobile. Cancel first, then the danger confirm. While the request runs, the confirm shows loader-circle and the dialog can\'t be dismissed. The preview is computed before confirming. A group is purged only if one of its copies is used in an article; the kept copy is the one used in the most articles (ties: oldest, then lowest id).', states: [
|
||||
D('loading', 'Loading preview', 'The preview request is in flight. Skeleton rows show after 150 ms and the confirm is disabled.'),
|
||||
D('confirm', 'Confirm', '"Purge now" pressed. Summary, up to 5 groups, a link to the Duplicates tab.'),
|
||||
D('confirm', 'Confirm: many groups', 'More than 5 groups. The list stops at 5 and says how many more.', { dg: { pv: pvMore } }),
|
||||
D('progress', 'In progress', 'POST in flight. Confirm reads "Purging…" with loader-circle; Cancel is disabled and Esc, the overlay and swipe do nothing.'),
|
||||
D('nothing', 'Nothing to purge', 'The preview has no group with an image in use. No confirm; one Close button.'),
|
||||
D('error', 'Error', '500 or network failure. The dialog stays open, nothing changed. Confirm retries.'),
|
||||
D('partial', 'Partial failure', 'Some groups failed. The summary refreshes to what is left. Buttons: Close, Retry.', { dg: { pv: pvOne } }),
|
||||
D('conflict', 'Conflict (409)', 'The library changed since the preview. Nothing was purged, the summary refreshes and the dialog stays open.', { dg: { pv: pv3 } })
|
||||
] },
|
||||
{ label: 'Results', title: 'Results and feedback', route: '/settings', api: 'after POST /v1/media/purge', note: 'An unattended run does not toast. The next visit to Settings shows the updated "Last purge" line.', states: [
|
||||
W('Success', 'The dialog closes, a toast shows for 5 s, and the "Last purge" line updates in place.', { on: false, last: { d: '5 Oct 2026', c: 9, a: 12 }, toast: true }),
|
||||
W('Success, automatic purge on', 'Same, with the next run moved forward by the interval.', { on: true, days: '30', last: { d: '5 Oct 2026', c: 9, a: 12 }, next: '4 Nov 2026', toast: true })
|
||||
] },
|
||||
{ label: 'Media', title: 'Media: Duplicates tab entry point', route: '/media?filter=duplicates', api: 'owner and admin only', note: 'A secondary "Purge duplicates" button (icon merge) in the toolbar next to the sort menu, and in the stacked toolbar on mobile. Per-group "Merge into this one" is unchanged.', states: [
|
||||
Mp({ tab: 'dups', name: 'Duplicates tab with the button', trigger: 'Owner or admin.' }),
|
||||
Mp({ tab: 'dups', modal: { k: 'purge' }, name: 'Dialog from Media', trigger: 'The same dialog as on Settings.' }),
|
||||
Mp({ tab: 'dups', member: true, name: 'Member: button hidden', trigger: 'Other verified users.' }),
|
||||
Mp({ tab: 'dups', offline: true, name: 'Offline', trigger: 'The button is disabled with the existing banner.' })
|
||||
], tableTitle: 'Status to copy', table: [
|
||||
{ a: '200', b: 'Purge done', c: 'Toast "Duplicates purged" / "9 copies merged. 12 articles updated."' },
|
||||
{ a: '409', b: 'Library changed', c: '"The library changed, so nothing was purged. Check the new summary and try again."' },
|
||||
{ a: '207', b: 'Partial', c: '"Some groups couldn\'t be merged. 3 of 4 groups were purged. Try again to finish."' },
|
||||
{ a: '500', b: 'Purge failed', c: '"Couldn\'t purge the duplicates. Nothing was changed. Try again in a moment."' },
|
||||
{ a: '500', b: 'Setting not saved', c: '"Couldn\'t save. Try again in a moment."' },
|
||||
{ a: '400', b: 'Days invalid', c: '"Enter a whole number of days between 1 and 365."' }
|
||||
] }
|
||||
])
|
||||
}
|
||||
finish(gs) { return gs.map(g => ({ ...g, states: g.states.map(st => ({ notMedia: !st.isMedia, ...st, m: { ...st.m } })) })) }
|
||||
renderVals() {
|
||||
const modes = [['system', 'System'], ['light', 'Light'], ['dark', 'Dark']].map(([k, label]) => {
|
||||
const on = this.state.mode === k
|
||||
return { label, on, bg: on ? 'var(--ui-primary)' : 'transparent', fg: on ? 'var(--ui-bg)' : 'var(--ui-text-toned)', pick: () => this.setState({ mode: k }) }
|
||||
})
|
||||
return { rootRef: this.rootRef, modes, groups: this.groups() }
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,121 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<script src="./support.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<x-dc>
|
||||
<div style="display:contents">{{ view }}</div>
|
||||
</x-dc>
|
||||
<script type="text/x-dc" data-dc-script data-props="{"$preview":{"width":1440,"height":900},"s":{"editor":null,"tsType":"object"},"mobile":{"editor":null,"tsType":"boolean"}}">
|
||||
class Component extends DCLogic {
|
||||
renderVals() {
|
||||
const s = this.props.s || {}, m = !!this.props.mobile, h = React.createElement
|
||||
const P = t => Object.fromEntries(t.split(';').filter(x => x.trim()).map(x => { const i = x.indexOf(':'); const k = x.slice(0, i).trim(); return [k.startsWith('--') ? k : k.replace(/-([a-z])/g, (_, c) => c.toUpperCase()), x.slice(i + 1).trim()] }))
|
||||
const D = (t, ...c) => h('div', { style: P(t) }, ...c)
|
||||
const S = (t, ...c) => h('span', { style: P(t) }, ...c)
|
||||
const I = (n, z = 16, t = '') => h('span', { 'aria-hidden': 'true', style: { display: 'inline-block', flexShrink: 0, width: z, height: z, backgroundColor: 'currentColor', WebkitMask: 'url(https://api.iconify.design/lucide/' + n + '.svg) center / contain no-repeat', mask: 'url(https://api.iconify.design/lucide/' + n + '.svg) center / contain no-repeat', ...(t ? P(t) : {}) } })
|
||||
const bh = m ? 44 : 32
|
||||
const B = (label, o = {}) => {
|
||||
const k = o.k || 'outline'
|
||||
const bg = k === 'primary' ? 'var(--ui-primary)' : k === 'danger' ? 'var(--ui-error)' : 'var(--ui-bg)'
|
||||
const fg = k === 'primary' || k === 'danger' ? 'var(--ui-bg)' : 'var(--ui-text-highlighted)'
|
||||
const bd = k === 'primary' || k === 'danger' ? bg : 'var(--ui-border-accented)'
|
||||
return S('height:' + (o.h || bh) + 'px;padding:0 ' + (o.px || 12) + 'px;display:flex;align-items:center;justify-content:center;gap:6px;background:' + bg + ';color:' + fg + ';border:1px solid ' + bd + ';font-weight:500;font-size:13px;white-space:nowrap;opacity:' + (o.dis ? 0.4 : 1) + ';' + (o.full ? 'flex:1;' : ''), o.icon && I(o.icon, 16), label)
|
||||
}
|
||||
const CHECK = 'conic-gradient(var(--ui-bg-accented) 25%, var(--ui-bg) 0 50%, var(--ui-bg-accented) 0 75%, var(--ui-bg) 0) 0 0/16px 16px'
|
||||
const TONES = ['var(--rc-sand)', 'var(--ui-bg-accented)', 'color-mix(in srgb, var(--ui-border-accented) 35%, var(--ui-bg))', 'var(--ui-bg-elevated)']
|
||||
const mark = h('svg', { width: 20, height: 20, viewBox: '0 0 16 16', shapeRendering: 'crispEdges' }, h('rect', { width: 16, height: 16, fill: '#111' }), h('path', { fill: '#fff', fillRule: 'evenodd', d: 'M4 3h6v1h1v1h1v3h-1v1h-1v1h1v1h1v2h-2v-2h-1v-1H6v3H4zm2 2v3h4V5z' }))
|
||||
const Th = (im, t = '') => D('position:relative;overflow:hidden;display:flex;align-items:center;justify-content:center;color:var(--ui-text-muted);background:' + (im.alpha ? CHECK : 'repeating-linear-gradient(135deg, rgb(128 128 128 / 0.12) 0 1px, transparent 1px 12px), ' + TONES[im.tone % 4]) + ';' + t, im.svg ? mark : I('image', 16, 'opacity:0.55'))
|
||||
const IMX = { 12: { tone: 0 }, 7: { tone: 2 }, 2: { tone: 1 }, 4: { tone: 3, alpha: true, svg: true }, 11: { tone: 1 } }
|
||||
const Alert = (tone, icon, text, title) => D('display:flex;gap:10px;padding:12px 14px;background:var(--rc-' + tone + '-soft);color:var(--ui-' + tone + ')', S('display:flex;padding-top:2px', I(icon)), D('flex:1;display:flex;flex-direction:column;gap:2px', title && S('font-weight:600', title), S('color:var(--ui-text-toned)', text)))
|
||||
const hl = 'color:var(--ui-text-highlighted)'
|
||||
const mono = "font-family:'IBM Plex Mono',monospace"
|
||||
|
||||
// ---------- card
|
||||
const dis = !!(s.ro || s.offline || s.loading)
|
||||
const on = !!s.on
|
||||
const Sw = () => D('display:flex;align-items:center;justify-content:center;width:' + (m ? 44 : 36) + 'px;height:' + (m ? 44 : 24) + 'px;flex-shrink:0;opacity:' + (dis ? 0.4 : 1), D('position:relative;width:36px;height:20px;border:1px solid ' + (on ? 'var(--ui-primary)' : 'var(--ui-border-accented)') + ';background:' + (on ? 'var(--ui-primary)' : 'var(--ui-bg-accented)') + ';' + (s.focusSw ? 'outline:2px solid var(--ui-primary);outline-offset:2px;' : ''), D('position:absolute;top:1px;width:16px;height:16px;left:' + (on ? 17 : 1) + 'px;background:' + (on ? 'var(--ui-bg)' : 'var(--ui-bg)') + ';border:1px solid ' + (on ? 'transparent' : 'var(--ui-border-accented)') + ';box-sizing:border-box')))
|
||||
const Row = (label, help, ctl, stack) => D('display:flex;' + (m || stack ? 'flex-direction:column;align-items:stretch;' : 'align-items:flex-start;justify-content:space-between;') + 'gap:' + (m ? 12 : 16) + 'px', D('display:flex;flex-direction:column;gap:2px;min-width:0', S('font-weight:500;' + hl, label), help && S('font-size:12px;color:var(--ui-text-muted)', help)), ctl)
|
||||
const hasErr = s.invalid || s.saveErr
|
||||
const errTxt = s.invalid ? 'Enter a whole number of days between 1 and 365.' : 'Couldn\'t save. Try again in a moment.'
|
||||
const Days = () => D('display:flex;flex-direction:column;gap:6px;' + (m ? '' : 'align-items:flex-end;'),
|
||||
D('display:flex;align-items:center;width:' + (m ? '100%' : '140px') + ';min-height:' + (m ? 44 : 40) + 'px;border:1px solid ' + (hasErr ? 'var(--ui-error)' : s.focusDays ? 'var(--ui-primary)' : 'var(--ui-border-accented)') + ';' + (s.focusDays && !hasErr ? 'box-shadow:0 0 0 1px var(--ui-primary);' : '') + 'background:var(--ui-bg);font-size:' + (m ? 16 : 13) + 'px;opacity:' + (dis ? 0.4 : 1), S('flex:1;min-width:0;padding:0 12px;' + mono + ';' + hl, s.days ?? '30'), S('padding:0 12px;color:var(--ui-text-muted);' + mono + ';font-size:13px', 'days')),
|
||||
hasErr && S('display:flex;gap:6px;align-items:flex-start;color:var(--ui-error);font-size:12px;' + (m ? '' : 'max-width:300px;text-align:right;'), I('circle-alert', 14, 'margin-top:2px'), errTxt))
|
||||
const stat = s.saving ? S('display:flex;align-items:center;gap:6px;font-size:12px;color:var(--ui-text-muted)', I('loader-circle', 14), 'Saving…') : s.saved ? S('display:flex;align-items:center;gap:6px;font-size:12px;color:var(--ui-success)', I('check', 14), 'Saved') : null
|
||||
const last = s.last === undefined ? null : s.last
|
||||
const lastLine = last ? 'Last purge: ' + last.d + ' · ' + last.c + ' copies merged · ' + last.a + ' articles updated' : 'Never run'
|
||||
const Cleanup = () => {
|
||||
const kids = [
|
||||
D('display:flex;align-items:flex-start;gap:12px', D('flex:1;display:flex;flex-direction:column;gap:2px', S('font:600 15px/24px \'IBM Plex Sans\',sans-serif;' + hl, 'Media cleanup'), S('color:var(--ui-text-muted);text-wrap:pretty', 'Merge identical images so your library stays tidy. Articles that use a copy are switched to the most used one.')), D('height:24px;display:flex;align-items:center;flex-shrink:0', stat))
|
||||
]
|
||||
if (s.ro) kids.push(Alert('info', 'info', 'Only owners and admins can change this.'))
|
||||
kids.push(Row('Automatic purge', 'Runs in the background. Off by default.', Sw()))
|
||||
if (on) kids.push(Row('Every', 'Between 1 and 365 days.', Days()))
|
||||
kids.push(D('display:flex;flex-direction:column;gap:4px;color:var(--ui-text-toned)', S('', lastLine), on && S('', 'Next purge: ' + (s.next || '14 Jul 2026'))))
|
||||
if (!s.ro) {
|
||||
kids.push(D('height:1px;background:var(--ui-border)'))
|
||||
kids.push(Row('Purge now', 'Merge every group of duplicates that has an image in use, right away.', D('display:flex;' + (m ? '' : 'flex-shrink:0'), B('Purge duplicates', { icon: 'merge', dis: !!(s.offline || s.loading), full: m, ring: s.focusBtn }))))
|
||||
}
|
||||
return D('background:var(--ui-bg);border:1px solid var(--ui-border);padding:' + (m ? 16 : 20) + 'px;display:flex;flex-direction:column;gap:16px', ...kids)
|
||||
}
|
||||
const SkelBar = (w, hh = 13) => S('display:block;width:' + w + ';height:' + hh + 'px;background:var(--ui-bg-accented)')
|
||||
const CleanupSkel = () => D('background:var(--ui-bg);border:1px solid var(--ui-border);padding:' + (m ? 16 : 20) + 'px;display:flex;flex-direction:column;gap:16px', SkelBar('140px', 16), SkelBar('90%'), SkelBar('55%'), D('display:flex;justify-content:space-between;align-items:center', D('display:flex;flex-direction:column;gap:6px', SkelBar('120px'), SkelBar('180px', 11)), SkelBar('36px', 20)), SkelBar('60%'), D('height:1px;background:var(--ui-border)'), D('display:flex;justify-content:space-between;align-items:center', D('display:flex;flex-direction:column;gap:6px', SkelBar('100px'), SkelBar('220px', 11)), SkelBar('140px', bh)))
|
||||
const pad = m ? 16 : 24
|
||||
const page = D('flex:1;min-height:0;overflow:hidden;padding:' + pad + 'px;position:relative', D('max-width:720px;display:flex;flex-direction:column;gap:20px',
|
||||
D('display:flex;flex-direction:column;gap:2px', S('font:600 20px/28px \'IBM Plex Sans\',sans-serif;' + hl, 'Settings'), S('color:var(--ui-text-muted)', 'Workspace information.')),
|
||||
D('background:var(--ui-bg);border:1px solid var(--ui-border);padding:' + (m ? 16 : 20) + 'px;display:flex;align-items:center;justify-content:space-between;gap:16px', S('font:600 15px/24px \'IBM Plex Sans\',sans-serif;' + hl, 'Version'), S('font:400 14px/22px \'IBM Plex Mono\',monospace;color:var(--ui-text-toned)', 'v2.2.0-beta')),
|
||||
s.loading ? CleanupSkel() : Cleanup()))
|
||||
|
||||
// ---------- dialog
|
||||
const G = [{ im: 12, n: 'floorboards.jpg', u: 4, c: 2 }, { im: 7, n: 'IMG_4471.jpg', u: 2, c: 1 }, { im: 2, n: 'kitchen-after.webp', u: 3, c: 3 }, { im: 4, n: 'logo-renew.svg', u: 5, c: 3 }, { im: 11, n: 'lime-plaster.jpg', u: 2, c: 1 }]
|
||||
const pl = (n, w) => n + ' ' + w + (n === 1 ? '' : 's')
|
||||
const PV = { groups: 4, copies: 9, arts: 12, list: G.slice(0, 4), more: 0, ...(s.dialog && s.dialog.pv) }
|
||||
const summary = pv => pl(pv.groups, 'group') + ' · ' + pv.copies + (pv.copies === 1 ? ' copy' : ' copies') + ' will be deleted · ' + pl(pv.arts, 'article') + ' will be updated'
|
||||
const GList = pv => D('border:1px solid var(--ui-border)', ...pv.list.map((g, i) => D('display:flex;align-items:center;gap:12px;padding:8px 12px;border-top:' + (i ? '1px solid var(--ui-border-muted)' : '0'), Th(IMX[g.im], 'width:40px;height:30px;flex-shrink:0;border:1px solid var(--ui-border)'), D('flex:1;min-width:0;display:flex;flex-direction:column', S('font-weight:500;' + hl + ';overflow:hidden;text-overflow:ellipsis;white-space:nowrap', g.n), S('font-size:12px;color:var(--ui-text-muted)', 'Kept · used in ' + pl(g.u, 'article'))), S('font-size:12px;color:var(--ui-text-toned);white-space:nowrap;text-align:right', pl(g.c, 'copy').replace('copys', 'copies') + ' merged'))), pv.more > 0 && D('padding:8px 12px;border-top:1px solid var(--ui-border-muted);color:var(--ui-text-muted)', 'and ' + pv.more + ' more groups'))
|
||||
const Link = () => S('display:inline-flex;align-items:center;gap:4px;align-self:flex-start;font-weight:500;' + hl + ';text-decoration:underline;text-underline-offset:3px;min-height:' + (m ? 44 : 20) + 'px', 'Review in Duplicates', I('arrow-right', 14))
|
||||
const dg = s.dialog
|
||||
const Shell = (o, body) => D('position:absolute;inset:0;z-index:50;background:var(--rc-overlay);display:flex;align-items:' + (m ? 'flex-end' : 'flex-start') + ';justify-content:center;padding:' + (m ? 16 : 140) + 'px 16px 16px',
|
||||
D('width:' + (m ? '100%' : '448px') + ';max-height:100%;background:var(--ui-bg);box-shadow:var(--rc-shadow-modal);display:flex;flex-direction:column',
|
||||
o.center ? D('padding:28px 24px 0;display:flex;flex-direction:column;align-items:center;gap:12px;text-align:center', S('width:44px;height:44px;display:flex;align-items:center;justify-content:center;background:var(--rc-info-soft);color:var(--ui-info)', I(o.icon, 20)), S('font:600 16px/24px \'IBM Plex Sans\',sans-serif;' + hl, o.title), S('color:var(--ui-text-muted);text-wrap:pretty;max-width:340px', o.desc))
|
||||
: D('padding:20px 24px 0;display:flex;flex-direction:column;gap:4px', S('font:600 16px/24px \'IBM Plex Sans\',sans-serif;' + hl, o.title), o.desc && S('color:var(--ui-text-muted);text-wrap:pretty', o.desc)),
|
||||
body && D('padding:20px 24px 0;display:flex;flex-direction:column;gap:16px;min-height:0;overflow:hidden', ...body),
|
||||
D('padding:20px 24px;display:flex;flex-direction:' + (m ? 'column-reverse' : 'row') + ';justify-content:flex-end;gap:8px;flex-shrink:0', o.cancel && B(o.cancel, { dis: o.loading, full: false }), o.confirm && S('height:' + bh + 'px;padding:0 12px;display:flex;align-items:center;justify-content:center;gap:6px;background:var(--ui-error);color:var(--ui-bg);font-weight:500;opacity:' + (o.cdis ? 0.4 : 1), o.loading ? I('loader-circle') : o.cicon && I(o.cicon), o.confirm))))
|
||||
const DESC = 'Articles that use a duplicate will be switched to the most used copy of that image, and the other copies will be deleted. This can\'t be undone.'
|
||||
const DSkel = () => [SkelBar('80%', 16), D('border:1px solid var(--ui-border)', ...[0, 1, 2].map(i => D('display:flex;align-items:center;gap:12px;padding:8px 12px;border-top:' + (i ? '1px solid var(--ui-border-muted)' : '0'), SkelBar('40px', 30), D('flex:1;display:flex;flex-direction:column;gap:6px', SkelBar('45%'), SkelBar('60%', 11)))))]
|
||||
const Dialog = () => {
|
||||
const k = dg.k, pv = PV
|
||||
const gp = 'Purge ' + pl(pv.groups, 'group')
|
||||
const note = (tone, icon, t) => Alert(tone, icon, t)
|
||||
if (k === 'loading') return Shell({ title: 'Purge duplicates?', desc: DESC, cancel: 'Cancel', confirm: 'Purge duplicates', cdis: true }, DSkel())
|
||||
if (k === 'nothing') return Shell({ center: true, icon: 'copy', title: 'Nothing to purge', desc: 'No duplicate is used in an article, so there is nothing to merge.', cancel: 'Close' }, null)
|
||||
const body = [S('font-weight:500;' + hl, summary(pv)), GList(pv), Link()]
|
||||
if (k === 'confirm') return Shell({ title: 'Purge duplicates?', desc: DESC, cancel: 'Cancel', confirm: gp }, body)
|
||||
if (k === 'progress') return Shell({ title: 'Purge duplicates?', desc: DESC, cancel: 'Cancel', confirm: 'Purging…', loading: true }, body)
|
||||
if (k === 'error') return Shell({ title: 'Purge duplicates?', desc: DESC, cancel: 'Cancel', confirm: gp }, [note('error', 'circle-x', 'Couldn\'t purge the duplicates. Nothing was changed. Try again in a moment.'), ...body])
|
||||
if (k === 'partial') return Shell({ title: 'Purge duplicates?', desc: DESC, cancel: 'Close', confirm: 'Retry', cicon: 'rotate-cw' }, [note('warning', 'triangle-alert', 'Some groups couldn\'t be merged. 3 of 4 groups were purged. Try again to finish.'), ...body])
|
||||
if (k === 'conflict') return Shell({ title: 'Purge duplicates?', desc: DESC, cancel: 'Cancel', confirm: gp }, [note('warning', 'triangle-alert', 'The library changed, so nothing was purged. Check the new summary and try again.'), ...body])
|
||||
}
|
||||
const Toast = () => D('position:absolute;z-index:60;' + (m ? 'left:16px;right:16px;bottom:16px' : 'right:24px;bottom:24px;width:356px') + ';display:flex;gap:10px;padding:12px 14px;background:var(--ui-bg);border:1px solid var(--ui-border);box-shadow:var(--rc-shadow-pop)', S('margin-top:2px;display:flex;color:var(--ui-success)', I('circle-check')), D('flex:1;display:flex;flex-direction:column;gap:2px', S('font-weight:600;' + hl, 'Duplicates purged'), S('color:var(--ui-text-muted)', '9 copies merged. 12 articles updated.')), I('x'))
|
||||
|
||||
// ---------- shell
|
||||
const nav = [['home', 'house', 'Home'], ['articles', 'file-text', 'Articles'], ['media', 'images', 'Media'], ['users', 'users', 'Users'], ['integration', 'plug', 'Integration'], ['settings', 'settings', 'Settings']]
|
||||
const mk = (z) => h('svg', { width: z, height: z, viewBox: '0 0 16 16', shapeRendering: 'crispEdges' }, h('rect', { width: 16, height: 16, fill: '#111' }), h('path', { fill: '#fff', fillRule: 'evenodd', d: 'M4 3h6v1h1v1h1v3h-1v1h-1v1h1v1h1v2h-2v-2h-1v-1H6v3H4zm2 2v3h4V5z' }))
|
||||
const side = D('width:224px;flex-shrink:0;background:var(--ui-bg);border-right:1px solid var(--ui-border);display:flex;flex-direction:column;padding:12px;gap:2px',
|
||||
D('display:flex;align-items:center;gap:10px;height:40px;padding:0 6px;margin-bottom:10px', mk(26), S('font-weight:600;font-size:14px;' + hl, 'RenewCMS')),
|
||||
...nav.map(([k, ic, t]) => D('display:flex;align-items:center;gap:10px;height:32px;padding:0 10px;background:' + (k === 'settings' ? 'var(--rc-sand)' : 'transparent') + ';color:' + (k === 'settings' ? '#111111' : 'var(--ui-text-toned)') + ';font-weight:' + (k === 'settings' ? 500 : 400), I(ic), t)),
|
||||
D('flex:1'), D('display:flex;align-items:center;gap:10px;height:40px;padding:0 6px;' + hl, S('width:26px;height:26px;flex-shrink:0;background:#d4c5b0;color:#111;display:flex;align-items:center;justify-content:center;font:600 11px \'IBM Plex Sans\',sans-serif', 'MA'), S('flex:1;font-weight:500', 'Marta Alves'), I('chevrons-up-down', 14)))
|
||||
const topbar = m
|
||||
? D('height:56px;flex-shrink:0;border-bottom:1px solid var(--ui-border);background:var(--ui-bg);display:flex;align-items:center;padding:0 6px;gap:4px;' + hl, S('width:44px;height:44px;display:flex;align-items:center;justify-content:center', I('menu', 20)), S('flex:1;font-weight:600;font-size:16px', 'Settings'))
|
||||
: D('height:48px;flex-shrink:0;border-bottom:1px solid var(--ui-border);background:var(--ui-bg);display:flex;align-items:center;padding:0 24px;gap:8px;color:var(--ui-text-muted)', I('panel-left-close'), S('width:1px;height:16px;background:var(--ui-border);margin:0 6px'), S('font-weight:500;' + hl, 'Settings'))
|
||||
const offline = s.offline && D('flex-shrink:0;display:flex;align-items:center;gap:10px;padding:10px 16px;background:var(--ui-bg-inverted);color:var(--ui-text-inverted)', I('wifi-off'), S('flex:1', 'You\'re offline. Changing automatic purge and purging duplicates need a connection.'))
|
||||
const root = D('width:' + (m ? 390 : 1440) + 'px;height:' + (m ? 844 : 900) + 'px;position:relative;display:flex;flex-direction:' + (m ? 'column' : 'row') + ';background:var(--ui-bg-muted);color:var(--ui-text);font-family:\'IBM Plex Sans\',sans-serif;font-size:13px;line-height:20px;overflow:hidden',
|
||||
!m && side, D('flex:1;min-width:0;min-height:0;display:flex;flex-direction:column', topbar, offline, page), dg && Dialog(), s.toast && Toast())
|
||||
return { view: root }
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,55 @@
|
||||
export const copy = [
|
||||
['Page title', 'Activity. Regular users: My activity.'],
|
||||
['Subtitle', 'Who did what, and when, across your site. Regular users: Everything you\'ve done on this site.'],
|
||||
['Header action', 'Export CSV (owner and admin only). Preparing: Preparing file… Success toast: Export started. Your file will download shortly. Error toast: Couldn\'t export activity. Try again.'],
|
||||
['Summary labels', 'Total actions · Active people · Failed actions · Security events'],
|
||||
['Summary sub-lines', '12% vs previous 7 days · 7 people, 2 API keys · 0.5% of all actions · 5 failed logins'],
|
||||
['Summary zero', 'Failed: Nothing failed. Security: Nothing to flag. No data at all: No data yet.'],
|
||||
['Filter bar', 'Search activity · All people · All categories · All / Succeeded / Failed · Today / 7 days / 30 days / Custom'],
|
||||
['Chips', 'Search: spring · Person: Marta Alves · Category: Authentication · Outcome: Failed · Dates: 1 – 30 Sep 2026 · Clear all'],
|
||||
['Mobile filters', 'Filters (count badge) · sheet title Filters · Clear all · Show 23 results'],
|
||||
['Day separators', 'Today · Yesterday · 3 Oct 2026'],
|
||||
['Outcome badges', 'Succeeded · Failed. Row markers: Security (shield icon). Target badge: Deleted. Mobile card: Target deleted.'],
|
||||
['Actor badges', 'API key · System. Not signed in: Not signed in.'],
|
||||
['Empty, no activity', 'No activity yet. Actions on articles, media, users and API keys will appear here. Regular users: Your actions will appear here once you start working on the site.'],
|
||||
['Empty, no match', 'No activity matches these filters. Try a different search or a wider date range. Action: Clear filters'],
|
||||
['Error', 'Couldn\'t load activity. Check your connection and try again. Action: Try again'],
|
||||
['Forbidden (403)', 'You can\'t view this activity. Your role doesn\'t include this page. Ask an owner or admin if you need access. Action: Back to home'],
|
||||
['Pagination', 'Showing 1–25 of 1,284 · Rows per page 25 · mobile: Page 1 of 52'],
|
||||
['Slide-over', 'Activity details · When (your time) + UTC line · Actor · Source (Web app, API key, System task) · IP address · Browser · Target · Changes · Before / After · Show more / Show less · Previous event · Next event · Event 2 of 1,284'],
|
||||
['Slide-over, failed', 'Why it failed. Reason text comes from the API (plain language, no stack traces).'],
|
||||
['Slide-over, deleted target', 'This item was deleted. Its name is kept for reference.'],
|
||||
['Slide-over, system', 'Source: System task. IP address and Browser: Not applicable.'],
|
||||
['Body edits', 'Body edited (+312 characters). No body text is shown.'],
|
||||
['Home card', 'Recent activity (regular users: Your recent activity) · View all · Empty: No activity yet · Error: Couldn\'t load recent activity. Try again'],
|
||||
['Retention', 'Retention · How long activity is kept. · Keep activity for: 30 days / 90 days / 365 days / Forever · Older events are removed automatically. · Forever: Nothing is removed automatically. The log grows over time.'],
|
||||
['Retention notice', 'Activity logs are append-only. Events can\'t be edited or deleted from the app. Only the retention period removes them.'],
|
||||
['Retention shorten', 'Events older than 30 days will be removed. This happens at the next cleanup on 7 Oct 2026, 03:00. It can\'t be undone. Actions: Apply 30 days · Cancel'],
|
||||
['Retention status', 'Last cleanup 6 Oct 2026, 03:00 · 142 events removed · Next cleanup 7 Oct 2026, 03:00 · Never / Not scheduled for Forever. Save: Saved · Error: Couldn\'t save the retention period. Your previous setting is still in place.']
|
||||
]
|
||||
export const comps = [
|
||||
['Page shell, sidebar entry', 'Existing dashboard shell. "Activity" (icon history) between Users and Settings, all verified users.'],
|
||||
['Page header', 'Plain heading + UButton (outline, icon download) for Export CSV.'],
|
||||
['Summary strip', 'Four UCard, each with a UButton-style click target (as="button"). Sparkline: seven div bars in the card (no chart library). Semantic emphasis via error and warning soft tokens.'],
|
||||
['Search', 'UInput, icon search, clearable.'],
|
||||
['Person and Category', 'USelectMenu. Person lists users, API keys and "System" in groups.'],
|
||||
['Outcome and date presets', 'Groups of UButton (soft = selected, outline = other). Custom opens a date-range popover.'],
|
||||
['Active filter chips', 'UBadge (variant subtle) with a trailing UButton (ghost, icon x). "Clear all" is UButton variant link.'],
|
||||
['Timeline table', 'UTable with custom row slots for day separators (full-width row). Cells: UAvatar (initials, key or system icon), UBadge (API key, System, Succeeded, Failed, Deleted, Security).'],
|
||||
['Row hover time', 'Native title attribute on the time cell (keeps to the allowed component list).'],
|
||||
['Pagination', 'UPagination + USelectMenu for page size.'],
|
||||
['Details panel', 'USlideover side="right" on desktop, side="bottom" on mobile. Body uses UCard sections. Prev and next are UButton.'],
|
||||
['Mobile filters', 'USlideover side="bottom" with the same USelectMenu and UButton controls.'],
|
||||
['Mobile list', 'UTable replaced below the md breakpoint by a stacked list of UCard rows.'],
|
||||
['Empty, error, forbidden', 'UEmpty with icon, title, description and one UButton action. Error uses UEmpty in the table area.'],
|
||||
['Loading', 'Skeleton blocks (bg-accented divs with animate-pulse) inside the same UCard and UTable layout.'],
|
||||
['Home card', 'UCard with header, a list of UAvatar rows, and a link UButton.'],
|
||||
['Retention', 'UCard in Settings with USelectMenu and read-only text lines.']
|
||||
]
|
||||
export const api = [
|
||||
['GET /v1/activity', 'Query: q, actor (user:ID, key:ID, system), category (articles, media, users, api_keys, auth, system), outcome (all, success, failed), range (today, 7d, 30d, custom), from, to (ISO dates), page, size (25, 50, 100). Sorted newest first. Returns items (id, created_at, actor {type, id, name}, category, sentence parts, target {type, id, name, exists}, outcome, failure_reason, security, source, ip, user_agent, changes [{field, before, after, kind}]), total, page, size. Users who are not owner or admin are scoped to their own events by the server; requesting another actor returns 403. Body edits return a single change with kind "body" and a character delta, never the text. The same call powers the Home card (size=5) and the slide-over (items carry the detail fields).'],
|
||||
['GET /v1/activity/summary', 'Query: the same range, from, to. Owner and admin only (403 otherwise). Returns total, previous_total, active_people {users, api_keys}, failed, security, and a 7-day series for each. Clicking a card applies outcome=failed or category in (auth, users role changes, api_keys) to the table, or clears filters for Total.'],
|
||||
['GET /v1/activity/export', 'Query: the same filters, no page or size. Owner and admin only. Streams text/csv with columns when, actor, action, target, outcome, source, ip. No changes or secrets.'],
|
||||
['GET /v1/settings', 'Existing endpoint. Adds activity_retention_days (30, 90, 365, 0 for forever), activity_last_cleanup_at, activity_last_cleanup_removed.'],
|
||||
['PATCH /v1/settings', 'Existing endpoint. Body { activity_retention_days }. Owner only (403 otherwise). Shortening needs the confirm step in the UI first.']
|
||||
]
|
||||
@@ -281,3 +281,47 @@ Added by the Media screens, none of which exist yet:
|
||||
- `POST /v1/media/merge`: `{ keep_id, merge_ids }`. Rewrites article bodies from the merged copies' URLs to the kept image's URL, then deletes the copies. 200 with `{ articles_updated, merged }`, 404, 409 when a listed copy is gone or the group changed.
|
||||
- `GET /v1/media/{id}/usage`: `[{ id, title, published }]`, read from article bodies.
|
||||
- Images no longer belong to an article, and deleting an article no longer deletes images. `POST /v1/article/{id}/image` and `DELETE /v1/article/{id}/image/{imageId}` are replaced by the endpoints above.
|
||||
|
||||
## Added with Media cleanup
|
||||
### Media cleanup
|
||||
| State | Surface | Behaviour |
|
||||
|---|---|---|
|
||||
| Default | Settings card | Off, "Never run", no "Every" row |
|
||||
| On | Settings card | "Every" 30 days, last and next run |
|
||||
| Saving / Saved | Settings card header | "Saving…" then "Saved" for 2 s, polite live region |
|
||||
| Save failed | Under the field | "Couldn't save. Try again in a moment." Control reverts |
|
||||
| Validation | Under the field | "Enter a whole number of days between 1 and 365." Nothing sent |
|
||||
| Read-only | Settings card | Controls disabled, button hidden, info alert |
|
||||
| Loading | Settings card | Skeleton after 150 ms |
|
||||
| Offline | Settings and Media | Banner as on Media. Controls and buttons disabled |
|
||||
| Preview loading | Dialog | Skeleton rows, confirm disabled |
|
||||
| Confirm | Dialog | Summary, up to 5 groups, link to Duplicates |
|
||||
| In progress | Dialog | "Purging…", loader-circle, not dismissible |
|
||||
| Nothing to purge | Dialog | Info state, "Close" only |
|
||||
| Error | Dialog | Notice, stays open, confirm retries |
|
||||
| Partial | Dialog | Warning, summary refreshes, "Close" and "Retry" |
|
||||
| Conflict (409) | Dialog | Warning, summary refreshes |
|
||||
| Success | Toast, 5 s | "Duplicates purged" / "9 copies merged. 12 articles updated." "Last purge" updates in place |
|
||||
| Unattended run | Settings | No toast. Updated on next visit |
|
||||
|
||||
401 and 403 are global and have no local copy.
|
||||
|
||||
## Added with Activity
|
||||
### Activity
|
||||
| State | Surface | Behaviour |
|
||||
|---|---|---|
|
||||
| Default | `/activity` | Strip, filters, day-grouped table, pagination |
|
||||
| Loading | `/activity` | Skeleton strip and rows after 150 ms |
|
||||
| Zero-value strip | `/activity` | Failed and Security show a calm 0 |
|
||||
| Filtered | `/activity` | Chips and "Clear all"; a strip card applies its filter |
|
||||
| Empty, no activity | `/activity` | "No activity yet"; Export disabled |
|
||||
| Empty, no match | `/activity` | "No activity matches these filters" with "Clear filters" |
|
||||
| Error | `/activity` | "Couldn't load activity" with "Try again"; the strip may still show |
|
||||
| Forbidden (403) | `/activity` | "You can't view this activity." with "Back to home" |
|
||||
| Details | Slide-over / bottom sheet | Sentence, time, actor, source, target, reason, changes, previous and next |
|
||||
| My activity | `/activity` | No strip, Person filter or Export; own events only |
|
||||
| Export | Header button, toasts | "Preparing file…", then "Export started. Your file will download shortly." or "Couldn't export activity. Try again." |
|
||||
| Home card | `/home` | Owner or admin, regular user, loading, error, empty |
|
||||
| Retention | `/settings` | Default, shortening (confirm), Forever, save error |
|
||||
|
||||
401 is global. The 403 on `/activity` has local copy because the page itself is refused; the 403 on the summary or export of a non-admin is never shown, since the controls are hidden.
|
||||
@@ -55,6 +55,8 @@ If your editor reports `pattern all:dist: no matching files found`, either run `
|
||||
```sh
|
||||
task --list # everything available
|
||||
task test # Go tests; extra args pass through: task test -- -run TestCreateArticle -v
|
||||
task test:mysql # MySQL tests against real servers in Docker (mysql:8.4, then mariadb:11)
|
||||
task test:postgres # PostgreSQL tests against real servers in Docker (postgres:16, then postgres:17)
|
||||
task cover # coverage of the use cases → tmp/coverage.out
|
||||
task mutate # gremlins mutation testing → tmp/gremlins.json
|
||||
task test:web # SPA component tests (Vitest)
|
||||
|
||||
+242
@@ -0,0 +1,242 @@
|
||||
# Docker
|
||||
|
||||
RenewCMS runs as a container. The image is published to the registry at `git.floriansylvain.fr/fsylvain/renewcms`, so site operators pull it and never build from source; it can also be built from this repository. The image holds only the single binary (SPA embedded) on Alpine, runs as a non-root user (uid 10001) and keeps all state on one volume mounted at `/data`. It reuses the configuration contract described in [configuration.md](configuration.md); there are no Docker-specific variables.
|
||||
|
||||
Requirements: Docker with Compose v2. [task](https://taskfile.dev) is optional for operators and needed to build from source or to publish. Building needs no local Go or Bun. The image build bundles the SPA with `vite build` only; the `vue-tsc` type-check is not run inside the image (it does not work under the Bun-only build image), so keep running `bun run type-check` locally.
|
||||
|
||||
## Quick start
|
||||
|
||||
1. Copy the example env file and edit it:
|
||||
|
||||
```sh
|
||||
cp docker.env.example docker.env
|
||||
```
|
||||
|
||||
Set `PUBLIC_URL`, the `SMTP_*` values and a generated secret (`openssl rand -hex 32`) for `JWT_SECRET`. `docker.env` is git-ignored and excluded from the build context, so it never ends up in an image. Never commit it.
|
||||
|
||||
2. Pull and start the published image:
|
||||
|
||||
```sh
|
||||
docker compose pull
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
`docker-compose.yml` uses `image: ${RENEWCMS_IMAGE:-git.floriansylvain.fr/fsylvain/renewcms:beta}`. The default tag is `beta` because `latest` does not exist until the first stable release (the default changes to `latest` then). Set `RENEWCMS_IMAGE` in your shell or in a `.env` file next to the compose file to pin another tag, for example `RENEWCMS_IMAGE=git.floriansylvain.fr/fsylvain/renewcms:v2.9.0-beta`.
|
||||
|
||||
To build from the local sources instead, see [Building from source](#building-from-source).
|
||||
|
||||
3. Check it: `curl http://localhost:8080/healthz` returns `ok`, and `http://localhost:8080/` serves the SPA.
|
||||
|
||||
Other tasks: `task docker:pull`, `task docker:upgrade` (pull, then `docker compose up -d`), `task docker:up` (local build, see below), `task docker:build` (image only, tagged with the `git describe` version and `latest`), `task docker:run` (one-off foreground run on the volume `renewcms-data`, without Compose), `task docker:logs` and `task docker:down`.
|
||||
|
||||
If a required variable is missing the container exits non-zero and the log names it, for example `Environment variable JWT_SECRET is not set`.
|
||||
|
||||
### Local trial over plain HTTP
|
||||
|
||||
`ENVIRONMENT` defaults to `production`, which makes the auth cookie `Secure`: browsers do not store it over `http://`, so login appears to fail. For a local trial on `http://localhost:8080`, set `ENVIRONMENT=development` and `PUBLIC_URL=http://localhost:8080` in `docker.env`. Use `production` only behind HTTPS.
|
||||
|
||||
## Building from source
|
||||
|
||||
`docker-compose.build.yml` adds the `build:` section and tags the result `renewcms:${VERSION:-dev}`. Use it as an override:
|
||||
|
||||
```sh
|
||||
docker compose -f docker-compose.yml -f docker-compose.build.yml up -d --build
|
||||
```
|
||||
|
||||
`task docker:up` does the same and passes the `git describe` version as `VERSION`. To check what Compose resolves, run `docker compose config`: without the override it shows the registry image and no `build` section, with it the `build` section is present.
|
||||
|
||||
## Image tags
|
||||
|
||||
| Release | Tags pushed |
|
||||
|---|---|
|
||||
| `vX.Y.Z` (stable) | `vX.Y.Z`, `X.Y.Z`, `X.Y`, `latest` |
|
||||
| `vX.Y.Z-<pre>` (for example `v2.9.0-beta`, `v2.9.0-rc.1`) | `vX.Y.Z-<pre>`, `X.Y.Z-<pre>`, and the moving tag `<pre-name>` (`beta`, `rc`) |
|
||||
|
||||
A pre-release never moves `latest`. Images are published for `linux/amd64` and `linux/arm64`. `renewcms --version` in the image prints the exact release tag. Images are not signed; the Ed25519 key signs the release binaries only.
|
||||
|
||||
## Environment
|
||||
|
||||
Compose reads `docker.env` through `env_file` and sets `DB_FILE=/data/renewcms.db` and `UPLOAD_DIR=/data/uploads` itself. Everything in [configuration.md](configuration.md) can go in `docker.env`. To publish another host port, change the left side of `ports` in `docker-compose.yml`; keep `PORT` at 8080 unless you also change the healthcheck.
|
||||
|
||||
The container receives SIGTERM on stop, and RenewCMS shuts down gracefully within 10 seconds. Compose uses `stop_grace_period: 15s`. The healthcheck calls `/healthz` inside the container.
|
||||
|
||||
## Data volume
|
||||
|
||||
State lives on the named volume `renewcms-data` (Compose prefixes it with the project name, usually `renewcms_renewcms-data`; check with `docker volume ls`). It holds the SQLite database (unless you use MySQL), uploads, and update records.
|
||||
|
||||
- `docker compose down` removes the container and keeps the volume. The data survives and `docker compose up -d` brings it back.
|
||||
- **`docker compose down -v` deletes the volume and all data.** The next start is an empty instance.
|
||||
|
||||
### Backup
|
||||
|
||||
Replace `VOLUME` with the real volume name. The image has no `sqlite3`, so stop the service for a consistent copy:
|
||||
|
||||
```sh
|
||||
docker compose stop
|
||||
docker run --rm -v VOLUME:/data -v "$PWD":/backup alpine tar czf /backup/renewcms-backup.tar.gz -C /data .
|
||||
docker compose start
|
||||
```
|
||||
|
||||
### Restore into a fresh volume
|
||||
|
||||
```sh
|
||||
docker compose down -v
|
||||
docker volume create VOLUME
|
||||
docker run --rm -v VOLUME:/data -v "$PWD":/backup alpine sh -c "tar xzf /backup/renewcms-backup.tar.gz -C /data && chown -R 10001:10001 /data"
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
## MySQL or MariaDB
|
||||
|
||||
SQLite on the `/data` volume is the default. [`docker-compose.mysql.yml`](../docker-compose.mysql.yml) runs RenewCMS next to a MySQL 8.4 container instead:
|
||||
|
||||
```sh
|
||||
export MYSQL_ROOT_PASSWORD=<generated>
|
||||
export DB_PASSWORD=<generated>
|
||||
docker compose -f docker-compose.mysql.yml up -d
|
||||
```
|
||||
|
||||
It starts the `mysql` service with a named volume `mysql-data` and a healthcheck, and starts `renewcms` only once the database is healthy (`depends_on` with `service_healthy`). RenewCMS gets `DB_DRIVER=mysql`, `DB_HOST=mysql`, `DB_NAME`, `DB_USER` and `DB_PASSWORD` from the compose file and the rest from `docker.env`. `DATA_DIR=/data` keeps update state and the `.sql` pre-update backups on the `renewcms-data` volume, which still holds the uploads. Replace the placeholders; never commit real passwords.
|
||||
|
||||
To use MariaDB, change the `mysql` service image to `mariadb:11`, its `MYSQL_*` variables to `MARIADB_*` and its healthcheck to `healthcheck.sh --connect --innodb_initialized`. To use a server outside Compose, drop the `mysql` service and point `DB_HOST` at it. For a server on another machine set `DB_TLS=true`.
|
||||
|
||||
Back the database up with `docker compose -f docker-compose.mysql.yml exec mysql mysqldump ...` (a `tar` copy of the volume is not consistent while it runs), and the `renewcms-data` volume as described above. Moving an existing SQLite volume is a manual procedure, see [deployment.md](deployment.md#moving-an-existing-sqlite-site-to-mysql).
|
||||
|
||||
## PostgreSQL
|
||||
|
||||
[`docker-compose.postgres.yml`](../docker-compose.postgres.yml) runs RenewCMS next to a PostgreSQL 17 container:
|
||||
|
||||
```sh
|
||||
export DB_PASSWORD=<generated>
|
||||
docker compose -f docker-compose.postgres.yml up -d
|
||||
```
|
||||
|
||||
It starts the `postgres` service with a named volume `postgres-data` and a `pg_isready` healthcheck, and starts `renewcms` only once the database is healthy (`depends_on` with `service_healthy`). RenewCMS gets `DB_DRIVER=postgres`, `DB_HOST=postgres`, `DB_PORT`, `DB_NAME`, `DB_USER`, `DB_PASSWORD` and `DB_SSLMODE=disable` (the traffic stays on the Compose network) from the compose file and the rest from `docker.env`. `DATA_DIR=/data` keeps update state and the `.sql` pre-update backups on the `renewcms-data` volume. For a server on another machine drop the `postgres` service, point `DB_HOST` at it and set `DB_SSLMODE=require`. Back the database up with `docker compose -f docker-compose.postgres.yml exec postgres pg_dump -U renewcms renewcms`. Replace the placeholders; never commit real passwords.
|
||||
|
||||
## S3-compatible storage (SeaweedFS)
|
||||
|
||||
[`docker-compose.s3.yml`](../docker-compose.s3.yml) runs RenewCMS with `STORAGE_DRIVER=s3` next to a [SeaweedFS](https://github.com/seaweedfs/seaweedfs) server (Apache 2.0, image `chrislusf/seaweedfs`, override with `SEAWEEDFS_IMAGE`) with its S3 gateway enabled, and a one-shot `seaweedfs-init` step that creates the bucket with `weed shell`:
|
||||
|
||||
```sh
|
||||
export S3_SECRET_ACCESS_KEY=<generated>
|
||||
docker compose -f docker-compose.s3.yml up -d
|
||||
```
|
||||
|
||||
RenewCMS gets `S3_ENDPOINT=http://seaweedfs:8333`, `S3_PATH_STYLE=true`, the bucket name and the access key (`S3_ACCESS_KEY_ID`, default `renewcms`, and `S3_SECRET_ACCESS_KEY`) from the compose file, and the rest from `docker.env`. SeaweedFS reads the same key pair as its admin identity. The `renewcms-data` volume then holds the SQLite database and update state but no uploads; the images live in the `seaweedfs-data` volume. The bucket is not published and the app serves the images. SeaweedFS is used because MinIO stopped publishing community images. To use AWS S3 or another provider, drop the `seaweedfs` services and set the `S3_*` variables in `docker.env` (see [configuration.md](configuration.md#image-storage)). The volume backup above covers the database only, so back the bucket up separately. To move existing uploads see [configuration.md](configuration.md#moving-existing-uploads-to-a-bucket).
|
||||
|
||||
`task test:s3` starts a throwaway SeaweedFS and runs the storage integration test against it; it skips with a message when Docker is not available.
|
||||
|
||||
## Reverse proxy and TLS
|
||||
|
||||
With `ENVIRONMENT=production` the site must be served over HTTPS, so put a reverse proxy in front and set `PUBLIC_URL` to the public `https://` address. Bind the published port to the host only when the proxy runs on the same host, by changing `ports` to `"127.0.0.1:8080:8080"`.
|
||||
|
||||
Caddy on the host:
|
||||
|
||||
```caddyfile
|
||||
cms.example.com {
|
||||
reverse_proxy 127.0.0.1:8080 {
|
||||
header_up X-Real-IP {remote_host}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
nginx on the host:
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 443 ssl;
|
||||
server_name cms.example.com;
|
||||
client_max_body_size 20m;
|
||||
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:8080;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Set `TRUSTED_PROXIES` to the proxy address as the container sees it, otherwise the login rate limiter ignores `X-Real-IP` and counts every client as the proxy. With a proxy on the host and the default bridge network, that is the bridge gateway (check with `docker network inspect`, often `172.17.0.1` or the Compose network gateway). With a proxy container on the same Compose network, it is that container's IP. The value is a semicolon-separated list of exact IPs, no CIDR. See [deployment.md](deployment.md#4-put-https-in-front).
|
||||
|
||||
## SMTP
|
||||
|
||||
RenewCMS sends registration and password-reset mail through the `SMTP_*` variables. Use your provider's host and port in `docker.env`.
|
||||
|
||||
To use a mail server running on the Docker host, set `SMTP_HOST=host.docker.internal`. Docker Desktop resolves that name. On Linux add the mapping to the service in `docker-compose.yml`:
|
||||
|
||||
```yaml
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
```
|
||||
|
||||
The host MTA must listen on an address reachable from the container, not only on `127.0.0.1`.
|
||||
|
||||
If SMTP is unreachable, the server still starts. Only the actions that send mail (registration, password reset, invitations) fail, and the error appears in `docker compose logs renewcms`.
|
||||
|
||||
## Upgrading
|
||||
|
||||
1. Back up the volume (see above).
|
||||
2. Pull the new image and recreate the container on the same volume:
|
||||
|
||||
```sh
|
||||
docker compose pull
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
`task docker:upgrade` runs both. The schema is migrated at startup, and there is no down-migration.
|
||||
3. Check `docker compose logs renewcms` for the new version line, or `docker run --rm git.floriansylvain.fr/fsylvain/renewcms:beta --version`. The owner's Settings page also shows the running version.
|
||||
|
||||
To roll back, set `RENEWCMS_IMAGE` to the previous tag, run `docker compose up -d`, and restore the backup if the schema changed.
|
||||
|
||||
If you built from source, upgrade with `git pull` and `task docker:up` instead.
|
||||
|
||||
## Maintainer: publishing
|
||||
|
||||
Publishing is a manual, maintainer-run task, like `task release`. There is no CI pipeline.
|
||||
|
||||
1. Tag the release and keep the work tree clean:
|
||||
|
||||
```sh
|
||||
git tag v2.9.0-beta
|
||||
```
|
||||
|
||||
2. Log in once to the registry. The task never reads or stores credentials:
|
||||
|
||||
```sh
|
||||
docker login git.floriansylvain.fr
|
||||
```
|
||||
|
||||
3. Make sure a buildx builder can build both platforms. Check with `docker buildx ls`. With the containerd image store (the Docker Desktop default) the default builder already works. Otherwise create one, and on a Linux host install the QEMU emulators:
|
||||
|
||||
```sh
|
||||
docker buildx create --use
|
||||
docker run --privileged --rm tonistiigi/binfmt --install arm64
|
||||
```
|
||||
|
||||
4. Publish:
|
||||
|
||||
```sh
|
||||
task docker:publish
|
||||
```
|
||||
|
||||
`REGISTRY_IMAGE` defaults to `git.floriansylvain.fr/fsylvain/renewcms` and `VERSION` to `git describe --tags`. The task refuses to run, before building anything, when `REGISTRY_IMAGE` is empty, when `VERSION` is not an exact tag `vX.Y.Z` or `vX.Y.Z-<pre>` on HEAD, when the work tree is dirty, or when the builder cannot build `linux/amd64` and `linux/arm64`. It prints the tags it pushes.
|
||||
|
||||
### Dry run
|
||||
|
||||
```sh
|
||||
task docker:publish -- --dry-run
|
||||
task docker:publish VERSION=v2.9.0-rc.1 -- --dry-run
|
||||
```
|
||||
|
||||
The dry run builds for the host platform only, loads the image into the local Docker, pushes nothing and needs no login. It prints the tags a real run would push. A `VERSION` override is accepted only with `--dry-run`, and a dirty tree is allowed there. Remove the loaded images afterwards with `docker rmi`.
|
||||
|
||||
### After pushing
|
||||
|
||||
- `docker buildx imagetools inspect git.floriansylvain.fr/fsylvain/renewcms:<tag>` lists `linux/amd64` and `linux/arm64`.
|
||||
- The moving tag (`beta`) points at the new digest, and `latest` did not move for a pre-release.
|
||||
- On a clean machine, `docker compose pull && docker compose up -d` starts the new image and Settings shows the new version.
|
||||
|
||||
## Limitations
|
||||
|
||||
Browser updates are unsupported in Docker (they need Linux with systemd, and a binary replaced inside a container is lost when it is recreated). The image and `docker-compose.yml` set `RENEWCMS_CONTAINER=true`, so the owner's Settings page shows when a newer version exists and tells them to pull a new image: `docker compose pull && docker compose up -d`. There is no Update or Restart button. Upgrade by pulling the image as above, or by rebuilding it if you build from source.
|
||||
@@ -31,3 +31,6 @@ Each body lives in `docs/releases/<tag>.md`. Publish with `tea releases create -
|
||||
| `v2.6.2-beta` | v2.6.2-beta - English and French interface |
|
||||
| `v2.7.0-beta` | v2.7.0-beta - Markdown and block article bodies |
|
||||
| `v2.8.0-beta` | v2.8.0-beta - API keys and API reference |
|
||||
| `v2.8.1-beta` | v2.8.1-beta - Docker deployment |
|
||||
| `v2.8.2-beta` | v2.8.2-beta - Security headers and clearer log in errors |
|
||||
| `v2.8.3-beta` | v2.8.3-beta - Security policy fixes for the web app |
|
||||
@@ -0,0 +1,36 @@
|
||||
This release adds an official Docker deployment, so RenewCMS can run in a container instead of as a binary under systemd, and teaches Settings to guide container installs through updates.
|
||||
|
||||
## New
|
||||
|
||||
**Docker deployment**
|
||||
|
||||
- A container image holds the single RenewCMS binary on Alpine, runs as a non-root user and keeps the database and uploads on one volume. A Compose file starts it with an env file and a health check.
|
||||
- Images are published for amd64 and arm64 to the project registry at `git.floriansylvain.fr/fsylvain/renewcms`. Prereleases carry the version tag and a moving `beta` tag, and `latest` is only used for stable releases.
|
||||
- Upgrading is `docker compose pull && docker compose up -d`. Your data stays on the volume.
|
||||
- A new Docker guide covers the quick start, backups of the data volume, the reverse proxy and TLS, reaching an SMTP server from the container, and building the image from source.
|
||||
|
||||
**Update guidance in containers**
|
||||
|
||||
- In a container, Settings no longer shows the browser update flow, which cannot work there. It shows the new version, the command to run on the server with a Copy button, and a link to the Docker guide.
|
||||
- Download and restart are not offered in a container, and the API refuses them.
|
||||
|
||||
## API
|
||||
|
||||
- `GET /v1/system/update` has a new `container` boolean. It is `true` when the server runs in a container, and `can_apply` is then always `false`.
|
||||
- `POST /v1/system/update/download` and `POST /v1/system/update/restart` answer `409` in a container, as they already do when browser updates are not set up.
|
||||
|
||||
## Under the hood
|
||||
|
||||
- Add a multi-stage Dockerfile, a Compose file with a separate build override, and tasks to build, run, pull, upgrade and publish the image. Publishing is a maintainer task that refuses to run unless the version is an exact tag on a clean tree.
|
||||
- Add the container update guidance and the image publishing specifications and QA procedures to `features/`.
|
||||
|
||||
## Upgrading
|
||||
|
||||
- Replace the binary. No schema change and no required environment variable.
|
||||
- There is one new optional variable, `RENEWCMS_CONTAINER`, which defaults to `false`. The Docker image and the Compose file set it to `true`. Leave it unset on a binary install.
|
||||
- Docker users start from the Docker guide. To move an existing binary install to Docker, copy the database and uploads into the volume and keep the same environment variables.
|
||||
|
||||
## Known limitations
|
||||
|
||||
- Browser updates are not available in a container. Update by pulling a new image.
|
||||
- The new container wording in Settings has no design frame yet.
|
||||
@@ -0,0 +1,18 @@
|
||||
This release hardens the server with browser security headers on every response and makes the log in form clearer when the username is wrong.
|
||||
|
||||
## Improvements and fixes
|
||||
|
||||
- RenewCMS now sends security headers on every response, including the web app and uploaded images: a Content Security Policy that forbids framing, `X-Content-Type-Options`, `X-Frame-Options`, `Referrer-Policy` and `Cross-Origin-Opener-Policy`. With `ENVIRONMENT=production` it also sends `Strict-Transport-Security`.
|
||||
- The log in form now tells you when you typed an email address instead of your username, and when the username is longer than 20 characters, instead of only asking for at least 3 characters. The messages are available in English and French.
|
||||
|
||||
## Under the hood
|
||||
|
||||
- Add a security headers middleware, mounted on the whole router, with tests.
|
||||
- Document the headers, the Google Fonts and `https:` image allowances in the policy, and the advice not to duplicate them in the reverse proxy in the deployment guide.
|
||||
|
||||
## Upgrading
|
||||
|
||||
- Replace the binary, or pull the new image. No schema change and no new environment variable.
|
||||
- If your reverse proxy already adds the same security headers, remove them there, or browsers will receive them twice.
|
||||
- The policy allows scripts from the same origin only. A custom script injected by your proxy will be blocked.
|
||||
- Serve the site over HTTPS only when `ENVIRONMENT=production`, because the server then asks browsers to remember HTTPS for a year.
|
||||
@@ -0,0 +1,18 @@
|
||||
This release fixes the security headers added in v2.8.2-beta, which blocked parts of the web app in the browser.
|
||||
|
||||
## Improvements and fixes
|
||||
|
||||
- The web app's icons now display again. They are bundled into the app instead of being fetched from an external icon service, which the security policy blocked.
|
||||
- The light and dark mode script in the page is now allowed by the security policy, so the color mode no longer fails to apply on load.
|
||||
- The security policy now allows WebAssembly, which parts of the web app need.
|
||||
|
||||
## Under the hood
|
||||
|
||||
- Add a Vite plugin that bundles only the Lucide icons the app uses.
|
||||
- Compute a hash for each inline script in the served page and add it to the `script-src` directive, instead of allowing inline scripts in general.
|
||||
- Update the security headers tests.
|
||||
|
||||
## Upgrading
|
||||
|
||||
- Replace the binary, or pull the new image. No schema change and no new environment variable.
|
||||
- If you are on v2.8.2-beta, upgrade: that version's policy broke icons and color mode.
|
||||
@@ -0,0 +1,34 @@
|
||||
This release adds a duplicate purge to the media library: owners and admins can merge identical images in one action, or let RenewCMS do it on a schedule.
|
||||
|
||||
## New
|
||||
|
||||
**Media duplicate purge**
|
||||
|
||||
- Purge duplicates from the Duplicates tab of the media library with the new toolbar button, or from the new Media cleanup card in Settings. A summary shows how many copies would be merged and how many articles would be updated before you confirm.
|
||||
- For each group of identical images used in at least one article, the most used copy is kept, with ties going to the oldest. Every other copy is merged into it and the articles that used them are rewritten. Groups where no copy is used are left alone.
|
||||
- If the library changes between the summary and the confirmation, nothing is purged and you are asked to check the new summary.
|
||||
- Turn on automatic purge in Settings and choose an interval from 1 to 365 days, 30 by default. The first run happens one interval after you turn it on, then once per interval after each run.
|
||||
- The Media cleanup card shows the last run, with the number of copies merged and articles updated, and the date of the next one.
|
||||
- The purge is reserved for owners and admins. Editors get a clear message instead of the controls.
|
||||
- The new screens are available in English and French.
|
||||
|
||||
## API
|
||||
|
||||
- `GET /v1/media/purge-settings` returns the automatic purge settings: `auto_purge_enabled`, `auto_purge_interval_days`, `last_purge_at`, `last_purge_merged`, `last_purge_articles_updated` and `next_purge_at`, which is `null` while automatic purge is off.
|
||||
- `PATCH /v1/media/purge-settings` updates `auto_purge_enabled` and `auto_purge_interval_days`. An interval outside 1 to 365 answers `400`.
|
||||
- `GET /v1/media/purge-preview` returns what a purge would do, with a `fingerprint` of the plan. It changes nothing.
|
||||
- `POST /v1/media/purge` runs the purge. Send the preview's `fingerprint` to refuse a run if the library changed: a stale fingerprint answers `409`, and so does a purge that is already running.
|
||||
- All four routes answer `403` with "Only owners and admins can do this." for any other role, and accept a session only, like the rest of the media routes.
|
||||
|
||||
## Under the hood
|
||||
|
||||
- Add the purge domain, a SQLite repository for the single-row purge settings, and the use cases that build the plan, preview it, run it and decide when a scheduled run is due.
|
||||
- Add an hourly scheduler, started with the server after a one-minute delay and stopped on shutdown. It reads the settings on every tick, so changes apply without a restart, and a failed run is retried at the next tick.
|
||||
- Add the `RequireOwnerOrAdmin` middleware.
|
||||
- Add the media purge, scheduler and Settings card specifications and QA procedures to `features/`, the new screens to `docs/design/`, and Go and Vitest suites for each part.
|
||||
|
||||
## Upgrading
|
||||
|
||||
- Replace the binary, or pull the new image. The purge settings table is created automatically at startup. No new environment variable is required.
|
||||
- Automatic purge is off after the upgrade. Nothing is merged until an owner or admin runs a purge or turns it on.
|
||||
- A purge cannot be undone: merged copies are removed. Check the summary before confirming.
|
||||
@@ -0,0 +1,47 @@
|
||||
This release adds an activity log that records who did what on your site, and lets RenewCMS run on MySQL, MariaDB or PostgreSQL and keep uploaded images in an S3-compatible bucket.
|
||||
|
||||
## New
|
||||
|
||||
**Activity log**
|
||||
|
||||
- The new Activity page lists every important action as a plain sentence, newest first, grouped by day: articles created, edited, published or deleted, images uploaded, renamed, merged or purged, accounts and invitations, and the system tasks (scheduled purge, update checks and downloads, restarts).
|
||||
- Filter by search text, person, category, outcome and date range, with Today, 7 days, 30 days and a custom range. Click a summary card to apply its filter. Open any row to see its details, including what changed.
|
||||
- Failed actions are recorded with a plain reason, and failed logins and similar events are flagged as security events.
|
||||
- Owners and admins see everyone's activity, a summary strip (total, active people, failed actions, security events) and can export the filtered list as a CSV file. Any other role would only see its own activity.
|
||||
- Article bodies are never recorded, only the change in size. Passwords, codes, file contents and storage paths are never recorded either.
|
||||
- The Home page has a new Recent activity card with the last five events.
|
||||
- Owners choose how long events are kept in the new Retention card in Settings: 30 days, 90 days (the default), 365 days or forever. Shortening the period asks for confirmation first. Older events are removed automatically once a day, and Settings shows the last cleanup and the next one.
|
||||
- The new screens are available in English and French.
|
||||
|
||||
**More ways to run RenewCMS**
|
||||
|
||||
- Use MySQL 8, MariaDB 11 or PostgreSQL 16 and 17 instead of the local SQLite file by setting `DB_DRIVER` to `mysql` or `postgres` and the `DB_*` connection variables. SQLite stays the default.
|
||||
- Keep uploaded images in an S3-compatible bucket (AWS S3, MinIO, Ceph, Cloudflare R2, Backblaze B2 and similar) by setting `STORAGE_DRIVER=s3`. Image URLs do not change and the bucket stays private.
|
||||
- The update backup taken before a download now works with every database: SQLite as before, and a logical dump for MySQL, MariaDB and PostgreSQL. The newest 3 backups are kept.
|
||||
- Docker Compose files for each setup are included: `docker-compose.mysql.yml`, `docker-compose.postgres.yml` and `docker-compose.s3.yml`.
|
||||
|
||||
## API
|
||||
|
||||
- `GET /v1/activity` returns the filtered, paged events as `{ items, total, page, size }`, newest first. It accepts `q`, `actor`, `category`, `outcome`, `security`, `range`, `from`, `to`, `page` and `size`, and any invalid value answers `400`. Owners and admins see every event. Any other role only sees its own, and asking for another actor answers `403`.
|
||||
- `GET /v1/activity/summary` returns the counts for the range, the previous period, the active people and a seven-bucket series. Owners and admins only.
|
||||
- `GET /v1/activity/export` returns the same filters as a CSV attachment (`when,actor,action,target,outcome,source,ip`). Owners and admins only. Cells that start with `=`, `+`, `-` or `@` get a leading quote.
|
||||
- The three routes accept a session only: an API key answers `403` with "This action needs a real session."
|
||||
- There is no `POST`, `PUT`, `PATCH` or `DELETE` under `/v1/activity`. Events can only be removed by the retention period.
|
||||
- `GET /v1/settings` now also returns `activity_retention_days`, `activity_last_cleanup_at`, `activity_last_cleanup_removed` and `activity_next_cleanup_at`.
|
||||
- `PATCH /v1/settings` accepts `activity_retention_days` with `0` (forever), `30`, `90` or `365`. Session and owner only, with `403` "Only the owner can do this." for anyone else. Any other value answers `400`.
|
||||
|
||||
## Under the hood
|
||||
|
||||
- Decouple persistence and image storage behind driver registries: `persistence/database` for the SQL drivers and `internal/infrastructure/storage` for the image adapters, with the domain `image.Storage` port. Repositories no longer touch files.
|
||||
- Add the audit domain, an append-only repository, a recorder that never fails or blocks the caller, and the use cases that record events from inside each action.
|
||||
- Add a scheduler for the activity cleanup, which runs once a day at or after 03:00 server time, alongside the media purge scheduler.
|
||||
- Add the activity, Home card and Retention card specifications and QA procedures to `features/`, the new screens to `docs/design/`, and Go and Vitest suites for each part.
|
||||
- Document the new variables in `docs/configuration.md`, and the new setups in `docs/deployment.md` and `docs/docker.md`.
|
||||
|
||||
## Upgrading
|
||||
|
||||
- Replace the binary, or pull the new image. The activity tables are created automatically at startup.
|
||||
- New environment variables are all optional and default to the previous behaviour: `DB_DRIVER` (`sqlite`), `STORAGE_DRIVER` (`local`), `DATA_DIR` (the directory of `DB_FILE`), and the `DB_*` and `S3_*` variables listed in `docs/configuration.md`. An existing install needs no change.
|
||||
- Activity starts recording after the upgrade, and nothing earlier is added. The first cleanup applies the default retention of 90 days, so owners who want a longer history should change it in Settings.
|
||||
- There is no automatic migration between databases or from the upload directory to a bucket. To move, follow the steps in `docs/configuration.md` and `docs/deployment.md`.
|
||||
- Backups taken before an update cover the database only. With S3, back the bucket up with your provider.
|
||||
@@ -0,0 +1,561 @@
|
||||
Feature: The back-office keeps an append-only activity log that owners can read and retention can prune
|
||||
|
||||
As the owner of a RenewCMS site
|
||||
I want every important action recorded with who, what, when and from where
|
||||
So that I can review activity, spot failed logins and keep the log from growing forever
|
||||
|
||||
Background:
|
||||
Given the spec of record is docs/design/screens/activity.md ("Roles", "API contract", "What is recorded") and docs/design/screens/settings.md ("Activity retention")
|
||||
And this slice is backend only: no SPA, no new env var, no change to the design
|
||||
And error responses are plain text from http.Error and success responses are JSON with snake_case fields
|
||||
And the layering rule holds: internal/domain/audit imports only the stdlib and other domain packages, use cases see only domain interfaces, handlers see only use cases, persistence owns GORM, and no comment is added anywhere
|
||||
And validation of query and body goes through validate tags and validator.Validate.Struct where a struct exists
|
||||
And the error text for a malformed query or body is "The request cannot be processed due to a mismatch in the format of the body."
|
||||
|
||||
Rule: The domain event is a complete snapshot and the repository is append-only
|
||||
|
||||
Scenario: An event carries everything the log needs
|
||||
Given an event is built for the action "article.publish" in category "articles"
|
||||
Then it holds an actor snapshot (type, id, name), the action code and its params, the category, a target snapshot (type, id, name), an outcome, a failure_reason, a security flag, a source, an ip, a user_agent, changes and a created_at
|
||||
|
||||
Scenario Outline: The vocabulary is closed
|
||||
Then the allowed <field> values are exactly <values>
|
||||
|
||||
Examples:
|
||||
| field | values |
|
||||
| actor.type | user, api_key, system, anonymous |
|
||||
| category | articles, media, users, api_keys, auth, system |
|
||||
| outcome | success, failed |
|
||||
| source | web, api_key, system |
|
||||
| change.kind | text, body |
|
||||
|
||||
Scenario: The repository exposes no way to edit or delete one event
|
||||
Then the audit Repository interface has exactly the methods Append, List, Summary and DeleteOlderThan
|
||||
And the persistence implementation exposes no other exported method than those and its constructor
|
||||
And the HTTP surface has no POST, PUT, PATCH or DELETE under /v1/activity
|
||||
|
||||
Scenario: The sentinel errors are matchable
|
||||
Then the audit package declares Err sentinels for an invalid filter and an invalid retention period
|
||||
And each is asserted with errors.Is
|
||||
|
||||
Rule: The recorder never fails or blocks the caller
|
||||
|
||||
Scenario: A failing repository does not change the caller's result
|
||||
Given the audit repository fails on Append with the error "disk full"
|
||||
When a user logs in with valid credentials through POST /v1/auth/login
|
||||
Then the response is 200 with a jwt cookie and the usual JSON message
|
||||
And the process log contains a line naming the failed audit write and "disk full"
|
||||
|
||||
Scenario: A panicking repository does not crash the request
|
||||
Given the audit repository panics on Append
|
||||
When a user logs out through POST /v1/auth/logout
|
||||
Then the response is 200 and the process continues serving
|
||||
|
||||
Scenario: The recorder fills the event from the audit context
|
||||
Given a context holding actor user 3 "marta", source "web", ip "203.0.113.12" and user agent "Mozilla/5.0 test"
|
||||
When the recorder records an event that has no actor, source, ip or user agent
|
||||
Then the stored event has those values
|
||||
And created_at is the injected clock's time in UTC
|
||||
|
||||
Scenario: Secrets never reach the log or the stored event
|
||||
Given actions that handle a password, a raw API key, a reset code and a jwt
|
||||
When they are recorded
|
||||
Then no stored field, no params and no process log line contains any of those values or their hashes
|
||||
|
||||
Rule: The audit context is set by middleware from the credentials and the trusted-proxy IP
|
||||
|
||||
Scenario: A session sets a user actor with source web
|
||||
Given a request with a valid jwt cookie for user 3 "marta"
|
||||
When it passes the audit context middleware
|
||||
Then the context holds actor type "user", id 3, name "marta" and source "web"
|
||||
|
||||
Scenario: An API key sets an api_key actor with source api_key
|
||||
Given a request with "Authorization: Bearer <valid key>" for the key 7 named "Deploy bot"
|
||||
When it passes the audit context middleware
|
||||
Then the context holds actor type "api_key", id 7, name "Deploy bot" and source "api_key"
|
||||
|
||||
Scenario: No credentials sets an anonymous actor
|
||||
Given a request without a cookie or key
|
||||
Then the context holds actor type "anonymous" and source "web"
|
||||
|
||||
Scenario Outline: The ip comes from the trusted-proxy real IP only
|
||||
Given the trusted proxies are "10.0.0.5"
|
||||
And the request peer is "<peer>" and X-Real-IP is "<header>"
|
||||
Then the context ip is "<ip>"
|
||||
|
||||
Examples:
|
||||
| peer | header | ip |
|
||||
| 10.0.0.5:443 | 203.0.113.12 | 203.0.113.12 |
|
||||
| 198.51.100.9:443 | 203.0.113.12 | 198.51.100.9 |
|
||||
| 198.51.100.9:443 | | 198.51.100.9 |
|
||||
|
||||
Scenario: The user agent is stored as sent and bounded
|
||||
Given the User-Agent header is 400 characters long
|
||||
Then the context user agent is its first 255 characters
|
||||
|
||||
Rule: Persistence stores events portably and indexed
|
||||
|
||||
Background:
|
||||
Given the persistence tests run against a real in-memory SQLite database migrated with the model list of migrate.go
|
||||
And the SQL stays portable across SQLite, MySQL/MariaDB and PostgreSQL
|
||||
|
||||
Scenario: The model is migrated with its indexes
|
||||
Then the audit events table exists
|
||||
And it has an index on created_at, an index on (actor_type, actor_id, created_at) and an index on (category, created_at)
|
||||
|
||||
Scenario: An appended event round-trips
|
||||
When an event with params {"merged": 3}, changes and a null failure_reason is appended
|
||||
Then listing returns it with every field equal, params and changes included
|
||||
|
||||
Scenario: List sorts by created_at descending then id descending
|
||||
Given three events with the same created_at and one older
|
||||
Then List returns the three by id descending, then the older one
|
||||
|
||||
Scenario Outline: Filters narrow the list and the total
|
||||
Given events across actors, categories, outcomes and days
|
||||
When List runs with <filter>
|
||||
Then items and total contain exactly <expected>
|
||||
|
||||
Examples:
|
||||
| filter | expected |
|
||||
| actor user:3 | events of user 3 only |
|
||||
| actor key:7 | events of api key 7 only |
|
||||
| actor system | events with actor type system only |
|
||||
| category auth | auth events only |
|
||||
| outcome failed | failed events only |
|
||||
| outcome success | succeeded events only |
|
||||
| security true | events whose security flag is true |
|
||||
| q "SPRING" | events whose actor or target name contains "spring", case-insensitive |
|
||||
| q "50%_" | only names containing the literal text 50%_ |
|
||||
| from 2026-10-01 to 2026-10-03 | events from 2026-10-01T00:00:00Z to the end of 2026-10-03 |
|
||||
|
||||
Scenario: Paging slices the sorted list and total is the unpaged count
|
||||
Given 60 events
|
||||
When List runs with page 2 and size 25
|
||||
Then 25 items are returned, the 26th to 50th of the sorted list, and total is 60
|
||||
|
||||
Scenario: A page past the end is empty
|
||||
When List runs with page 9 and size 25 over 60 events
|
||||
Then items is empty and total is 60
|
||||
|
||||
Scenario: The summary counts the range and the previous period
|
||||
Given events in the last 7 days and in the 7 days before
|
||||
When Summary runs for range 7d
|
||||
Then total, previous_total, failed and security are the counts of those periods
|
||||
And active_people.users counts distinct user actors and active_people.api_keys counts distinct key actors, with anonymous and system excluded
|
||||
And series holds seven daily counts for total, active, failed and security, oldest first
|
||||
|
||||
Scenario: DeleteOlderThan removes only events before the cutoff
|
||||
Given events at 100, 40, 31, 29 and 1 days ago
|
||||
When DeleteOlderThan runs with the cutoff 30 days ago
|
||||
Then it returns 3 and only the 29-day and 1-day events remain
|
||||
|
||||
Rule: Reading the log is session only and scoped by role
|
||||
|
||||
Background:
|
||||
Given the handler is mounted at /v1/activity behind jwtauth.Verifier, jwtauth.Authenticator, the current-token check and the verified-user check
|
||||
And the routes sit behind RequireSession, so no API key reaches them
|
||||
|
||||
Scenario Outline: The credentials gate answers before any data
|
||||
When GET <path> is sent <credentials>
|
||||
Then the response is <status> with body "<body>"
|
||||
And the audit repository was not read
|
||||
|
||||
Examples:
|
||||
| path | credentials | status | body |
|
||||
| /v1/activity | without a jwt cookie | 401 | You must be logged in to access this resource. |
|
||||
| /v1/activity/summary | without a jwt cookie | 401 | You must be logged in to access this resource. |
|
||||
| /v1/activity/export | without a jwt cookie | 401 | You must be logged in to access this resource. |
|
||||
| /v1/activity | with a valid API key | 403 | This action needs a real session. |
|
||||
| /v1/activity/summary | with a valid API key | 403 | This action needs a real session. |
|
||||
| /v1/activity/export | with a valid API key | 403 | This action needs a real session. |
|
||||
| /v1/activity | with an unverified user's cookie | 403 | You must verify your account to access this resource. |
|
||||
|
||||
Scenario Outline: Owner and admin see every event
|
||||
Given events by users 1, 2 and 3, an api key and the system
|
||||
When GET /v1/activity is sent with the cookie of the <role>
|
||||
Then the response is 200 and total counts the events of every actor
|
||||
|
||||
Examples:
|
||||
| role |
|
||||
| owner |
|
||||
| admin |
|
||||
|
||||
Scenario: Another role is scoped to its own events
|
||||
Given a verified user 3 whose role is neither owner nor admin
|
||||
And events by users 1, 2 and 3
|
||||
When GET /v1/activity is sent with user 3's cookie
|
||||
Then every item has actor type "user" and id 3
|
||||
And total counts only user 3's events
|
||||
|
||||
Scenario Outline: A scoped caller cannot ask for another actor
|
||||
Given a verified user 3 whose role is neither owner nor admin
|
||||
When GET /v1/activity?actor=<actor> is sent with user 3's cookie
|
||||
Then the response is <status>
|
||||
|
||||
Examples:
|
||||
| actor | status |
|
||||
| user:3 | 200 |
|
||||
| user:2 | 403 |
|
||||
| key:7 | 403 |
|
||||
| system | 403 |
|
||||
|
||||
Scenario Outline: A scoped caller cannot read the summary or the export
|
||||
Given a verified user 3 whose role is neither owner nor admin
|
||||
When GET <path> is sent with user 3's cookie
|
||||
Then the response is 403 with body "Only owners and admins can do this."
|
||||
|
||||
Examples:
|
||||
| path |
|
||||
| /v1/activity/summary |
|
||||
| /v1/activity/export |
|
||||
|
||||
Rule: GET /v1/activity lists filtered, paged events
|
||||
|
||||
Scenario: The default request
|
||||
Given 30 events over the last 3 days
|
||||
When the owner sends GET /v1/activity
|
||||
Then the response is 200 with {"items": [...], "total": 30, "page": 1, "size": 25}
|
||||
And items has 25 entries, newest first
|
||||
|
||||
Scenario: An item has the documented shape
|
||||
Given user 3 "Marta Alves" published the article 8 "Spring release" at 2026-10-06T10:42:18Z from 203.0.113.12 with "Chrome 129 on Windows"
|
||||
When the owner sends GET /v1/activity
|
||||
Then the first item is
|
||||
"""
|
||||
{"id": 4821, "created_at": "2026-10-06T10:42:18Z", "action": "article.publish", "category": "articles", "actor": {"type": "user", "id": 3, "name": "Marta Alves"}, "target": {"type": "article", "id": "8", "name": "Spring release", "exists": true}, "outcome": "success", "failure_reason": null, "security": false, "source": "web", "ip": "203.0.113.12", "user_agent": "Chrome 129 on Windows", "params": {}, "changes": [{"field": "online", "kind": "text", "before": "false", "after": "true"}]}
|
||||
"""
|
||||
|
||||
Scenario: A system event has no ip or user agent
|
||||
Given a system event "system.activity_cleanup"
|
||||
When the owner sends GET /v1/activity
|
||||
Then its item has actor {"type": "system", "id": null, "name": "System"}, source "system", ip null and user_agent null
|
||||
|
||||
Scenario: A renamed or deleted user still reads correctly
|
||||
Given an event recorded when user 3 was named "Marta Alves"
|
||||
And user 3 has since been renamed "M. Alves" and then deleted
|
||||
When the owner sends GET /v1/activity
|
||||
Then the item's actor.name is still "Marta Alves"
|
||||
|
||||
Scenario Outline: target.exists is resolved when the list is read
|
||||
Given an event whose target is <type> "<id>" named "<name>"
|
||||
And <state>
|
||||
When the owner sends GET /v1/activity
|
||||
Then the item's target.name is "<name>" and target.exists is <exists>
|
||||
|
||||
Examples:
|
||||
| type | id | name | state | exists |
|
||||
| article | 8 | Spring release | the article still exists | true |
|
||||
| article | 8 | Spring release | the article was deleted | false |
|
||||
| user | 3 | Marta Alves | the user was deleted | false |
|
||||
|
||||
Scenario: A body change carries a delta and never text
|
||||
Given an event with a body change of +312 characters
|
||||
Then its change is {"field": "body", "kind": "body", "delta": 312} with no before and no after
|
||||
|
||||
Scenario Outline: The filters are applied through the query
|
||||
When the owner sends GET /v1/activity?<query>
|
||||
Then the response is 200 and items match <expected>
|
||||
|
||||
Examples:
|
||||
| query | expected |
|
||||
| q=spring | the actor or target name contains spring |
|
||||
| actor=user:3 | events of user 3 |
|
||||
| actor=key:7 | events of api key 7 |
|
||||
| actor=system | system events |
|
||||
| category=auth | auth events |
|
||||
| outcome=failed | failed events |
|
||||
| outcome=success | succeeded events |
|
||||
| outcome=all | both outcomes |
|
||||
| security=true | events with the security flag |
|
||||
| range=today | events since 00:00 UTC today |
|
||||
| range=7d | events of the last 7 days |
|
||||
| range=30d | events of the last 30 days |
|
||||
| range=custom&from=2026-09-01&to=2026-09-30 | events from 1 to 30 September 2026 UTC |
|
||||
| page=2&size=50 | the second page of 50 |
|
||||
| size=5 | the first page of 5 |
|
||||
|
||||
Scenario: Omitting range means 7 days
|
||||
Given events 2 days old and 10 days old
|
||||
When the owner sends GET /v1/activity
|
||||
Then only the 2-day-old event is listed
|
||||
|
||||
Scenario Outline: Invalid values answer 400
|
||||
When the owner sends GET /v1/activity?<query>
|
||||
Then the response is 400 with body "The request cannot be processed due to a mismatch in the format of the body."
|
||||
And the audit repository was not read
|
||||
|
||||
Examples:
|
||||
| query |
|
||||
| actor=user:abc |
|
||||
| actor=user:-1 |
|
||||
| actor=group:3 |
|
||||
| category=billing |
|
||||
| outcome=maybe |
|
||||
| security=false |
|
||||
| range=year |
|
||||
| range=custom |
|
||||
| range=custom&from=2026-09-30&to=2026-09-01 |
|
||||
| range=custom&from=yesterday&to=today |
|
||||
| page=0 |
|
||||
| page=abc |
|
||||
| size=10 |
|
||||
| size=200 |
|
||||
|
||||
Rule: GET /v1/activity/summary answers the strip
|
||||
|
||||
Scenario: The summary of the last seven days
|
||||
Given 12 events in the last 7 days, 3 failed, 2 with the security flag, by 2 users and 1 api key
|
||||
And 10 events in the 7 days before
|
||||
When the owner sends GET /v1/activity/summary?range=7d
|
||||
Then the response is 200 and the body is
|
||||
"""
|
||||
{"total": 12, "previous_total": 10, "active_people": {"users": 2, "api_keys": 1}, "failed": 3, "security": 2, "series": {"total": [...7 numbers], "active": [...7], "failed": [...7], "security": [...7]}}
|
||||
"""
|
||||
And each series array has 7 entries, oldest first, and the total series sums to 12
|
||||
|
||||
Scenario: An empty log answers zeros
|
||||
Given no events
|
||||
When the owner sends GET /v1/activity/summary
|
||||
Then every number is 0 and every series array is seven zeros
|
||||
|
||||
Scenario: The summary uses the same filters as the range
|
||||
When the owner sends GET /v1/activity/summary?range=custom&from=2026-09-01&to=2026-09-07
|
||||
Then the counts cover exactly that range and previous_total covers the 7 days immediately before 2026-09-01
|
||||
|
||||
Scenario: An invalid range answers 400
|
||||
When the owner sends GET /v1/activity/summary?range=year
|
||||
Then the response is 400 with the body-mismatch message
|
||||
|
||||
Rule: GET /v1/activity/export streams a safe CSV
|
||||
|
||||
Scenario: The response is a CSV attachment
|
||||
Given 3 events
|
||||
When the owner sends GET /v1/activity/export
|
||||
Then the response is 200 with Content-Type "text/csv; charset=utf-8" and Content-Disposition starting with "attachment"
|
||||
And the first row is "when,actor,action,target,outcome,source,ip"
|
||||
And there are 3 data rows, newest first
|
||||
|
||||
Scenario: A row holds the documented columns only
|
||||
Given user 3 "Marta Alves" published "Spring release" at 2026-10-06T10:42:18Z from 203.0.113.12 with changes and a user agent
|
||||
Then the row is "2026-10-06T10:42:18Z,Marta Alves,article.publish,Spring release,success,web,203.0.113.12"
|
||||
And the file contains no changes and no user agent
|
||||
|
||||
Scenario Outline: Cells that a spreadsheet would evaluate are neutralised
|
||||
Given an event whose <column> is "<value>"
|
||||
Then the cell is "<cell>"
|
||||
|
||||
Examples:
|
||||
| column | value | cell |
|
||||
| actor | =cmd | '=cmd |
|
||||
| target | +1+1 | '+1+1 |
|
||||
| target | -2 | '-2 |
|
||||
| actor | @SUM(A1) | '@SUM(A1) |
|
||||
| target | Spring release | Spring release |
|
||||
| target | a=b | a=b |
|
||||
|
||||
Scenario: Commas, quotes and newlines are quoted
|
||||
Given a target whose name contains a comma, double quotes and a newline
|
||||
Then the cell is quoted with doubled inner quotes and the file still has one row for the event
|
||||
|
||||
Scenario: The export uses the list filters without paging
|
||||
Given 60 events of which 8 failed
|
||||
When the owner sends GET /v1/activity/export?outcome=failed&page=2&size=25
|
||||
Then the file has the 8 failed rows and page and size are ignored
|
||||
|
||||
Scenario: A system event has an empty ip cell
|
||||
Given a system event
|
||||
Then its ip cell is empty
|
||||
|
||||
Scenario: An invalid filter answers 400 before any byte is streamed
|
||||
When the owner sends GET /v1/activity/export?outcome=maybe
|
||||
Then the response is 400 with the body-mismatch message and no CSV header
|
||||
|
||||
Scenario: A large export is read in batches
|
||||
Given 1200 events
|
||||
When the owner exports them
|
||||
Then the file has 1200 rows and the repository was read through List in pages, not all at once
|
||||
|
||||
Rule: The retention period is a setting only the owner can change
|
||||
|
||||
Scenario: The default is 90 days
|
||||
Given no retention row exists
|
||||
When a verified user sends GET /v1/settings
|
||||
Then the body has "activity_retention_days": 90, "activity_last_cleanup_at": null and "activity_last_cleanup_removed": 0
|
||||
And the existing fields of the response are unchanged
|
||||
|
||||
Scenario: The last cleanup is reported
|
||||
Given a cleanup ran at 2026-10-06T03:00:00Z and removed 142 events
|
||||
When a verified user sends GET /v1/settings
|
||||
Then "activity_last_cleanup_at" is "2026-10-06T03:00:00Z" and "activity_last_cleanup_removed" is 142
|
||||
|
||||
Scenario Outline: The owner sets a valid period
|
||||
When the owner sends PATCH /v1/settings with body {"activity_retention_days": <days>}
|
||||
Then the response is 200 and the body has "activity_retention_days": <days>
|
||||
And a following GET /v1/settings returns <days>
|
||||
|
||||
Examples:
|
||||
| days |
|
||||
| 30 |
|
||||
| 90 |
|
||||
| 365 |
|
||||
| 0 |
|
||||
|
||||
Scenario Outline: Any other value answers 400 and changes nothing
|
||||
Given the stored retention is 90
|
||||
When the owner sends PATCH /v1/settings with body <body>
|
||||
Then the response is 400 with body "The request cannot be processed due to a mismatch in the format of the body."
|
||||
And the stored retention is still 90
|
||||
|
||||
Examples:
|
||||
| body |
|
||||
| {"activity_retention_days": 7} |
|
||||
| {"activity_retention_days": -1} |
|
||||
| {"activity_retention_days": 366} |
|
||||
| {"activity_retention_days": 30.5} |
|
||||
| {"activity_retention_days": "30"} |
|
||||
| {"activity_retention_days": null} |
|
||||
| {} |
|
||||
| not json |
|
||||
|
||||
Scenario: An admin cannot change it
|
||||
When an admin sends PATCH /v1/settings with body {"activity_retention_days": 30}
|
||||
Then the response is 403 with body "Only the owner can do this."
|
||||
And the stored retention is unchanged
|
||||
|
||||
Scenario Outline: The gate answers before the body is read
|
||||
When PATCH /v1/settings is sent <credentials> with body {"activity_retention_days": 30}
|
||||
Then the response is <status> with body "<body>"
|
||||
|
||||
Examples:
|
||||
| credentials | status | body |
|
||||
| without a jwt cookie | 401 | You must be logged in to access this resource. |
|
||||
| with an owner's API key | 403 | This action needs a real session. |
|
||||
| with an unverified owner | 403 | You must verify your account to access this resource. |
|
||||
|
||||
Scenario: A change is recorded as a system event by the owner
|
||||
Given the stored retention is 90
|
||||
When the owner sends PATCH /v1/settings with body {"activity_retention_days": 30}
|
||||
Then one event exists with action "system.activity_retention_changed", category "system", actor the owner, source "web", outcome "success", security false and params {"from": 90, "to": 30}
|
||||
|
||||
Scenario: Setting the same value records nothing
|
||||
Given the stored retention is 90
|
||||
When the owner sends PATCH /v1/settings with body {"activity_retention_days": 90}
|
||||
Then the response is 200 and no event is recorded
|
||||
|
||||
Rule: A daily cleanup removes events past the retention period
|
||||
|
||||
Background:
|
||||
Given the cleanup is a use case with a Tick(now) like PurgeScheduler, driven by the scheduler runner with hand-fed channels and an injected clock
|
||||
And the server time zone is the injected clock's location
|
||||
|
||||
Scenario Outline: The cleanup is due once a day at or after 03:00
|
||||
Given the retention is 90 and the last cleanup was at <last>
|
||||
When Tick runs at <now>
|
||||
Then ran is <ran>
|
||||
|
||||
Examples:
|
||||
| last | now | ran |
|
||||
| never | 2026-10-06T02:59:00 | false |
|
||||
| never | 2026-10-06T03:00:00 | true |
|
||||
| never | 2026-10-06T14:00:00 | true |
|
||||
| 2026-10-05T03:00:00 | 2026-10-06T02:59:00 | false |
|
||||
| 2026-10-05T03:00:00 | 2026-10-06T03:00:00 | true |
|
||||
| 2026-10-06T03:00:00 | 2026-10-06T04:00:00 | false |
|
||||
| 2026-10-06T03:00:00 | 2026-10-07T03:00:00 | true |
|
||||
|
||||
Scenario: Forever never runs
|
||||
Given the retention is 0
|
||||
When Tick runs at 2026-10-06T03:00:00
|
||||
Then ran is false and DeleteOlderThan is not called
|
||||
|
||||
Scenario: The cutoff is now minus the retention days
|
||||
Given the retention is 30 and events at 40, 31, 29 and 1 days ago
|
||||
When the cleanup runs at 2026-10-06T03:00:00
|
||||
Then the events 31 and 40 days old are removed and the others remain
|
||||
And the last cleanup time is the clock's time and the removed count is 2
|
||||
|
||||
Scenario: The last 24 hours are never removed
|
||||
Given the cleanup is asked for a cutoff newer than 24 hours ago
|
||||
Then the effective cutoff is 24 hours before now
|
||||
|
||||
Scenario: A cleanup that removes nothing still records the run
|
||||
Given the retention is 90 and no event is older than 90 days
|
||||
When the cleanup runs
|
||||
Then the last cleanup time is the clock's time, the removed count is 0 and no system.activity_cleanup event is recorded
|
||||
|
||||
Scenario: A cleanup that removes events records a system event after deleting
|
||||
Given 142 events older than the retention
|
||||
When the cleanup runs
|
||||
Then one event exists with action "system.activity_cleanup", category "system", actor type "system", source "system", ip null, user_agent null, outcome "success" and params {"removed": 142}
|
||||
And that event is not among the removed ones
|
||||
|
||||
Scenario: A failing delete is retried at the next tick
|
||||
Given DeleteOlderThan fails
|
||||
When Tick runs
|
||||
Then the error is returned and logged, the last cleanup fields are unchanged and the next tick runs the cleanup again
|
||||
|
||||
Scenario: A shortened period applies at the next cleanup
|
||||
Given the retention was 90 and the owner sets 30
|
||||
Then no event is removed by the change itself
|
||||
And the next cleanup removes events older than 30 days
|
||||
|
||||
Scenario: The scheduler stops with the process
|
||||
When the context is cancelled
|
||||
Then the runner returns and no goroutine outlives it
|
||||
|
||||
Rule: The first producers record the right events
|
||||
|
||||
Scenario Outline: Authentication events
|
||||
When <trigger>
|
||||
Then one event exists with action "<action>", category "auth", <actor>, source "web", outcome "<outcome>", security <security>, failure_reason <reason> and the request ip and user agent
|
||||
|
||||
Examples:
|
||||
| trigger | action | actor | outcome | security | reason |
|
||||
| POST /v1/auth/login with the right username and password | auth.login | actor user with the account's name | success | false | null |
|
||||
| POST /v1/auth/login with the right username and a wrong password | auth.login_failed | actor anonymous named by the attempted username | failed | true | "Wrong username or password." |
|
||||
| POST /v1/auth/login with an unknown username | auth.login_failed | actor anonymous named by the attempted username | failed | true | "Wrong username or password." |
|
||||
| POST /v1/auth/logout with a valid jwt cookie | auth.logout | actor user | success | false | null |
|
||||
| POST /v1/auth/password-reset for an existing account | auth.password_reset_requested | actor user | success | true | null |
|
||||
| POST /v1/auth/password-reset/confirm with a valid code | auth.password_reset_completed | actor user | success | true | null |
|
||||
|
||||
Scenario: A failed login reveals no more than the response does
|
||||
Given a wrong password for an existing user and an unknown username
|
||||
Then both events have the same failure_reason, actor type anonymous and no target
|
||||
And neither event holds the password
|
||||
|
||||
Scenario Outline: Nothing is recorded when nothing happened
|
||||
When <trigger>
|
||||
Then no event is recorded and the response is unchanged
|
||||
|
||||
Examples:
|
||||
| trigger |
|
||||
| POST /v1/auth/logout without a jwt cookie |
|
||||
| POST /v1/auth/password-reset for an unknown email |
|
||||
| POST /v1/auth/password-reset/confirm with an invalid code |
|
||||
| POST /v1/auth/login with a body that fails validation |
|
||||
| POST /v1/auth/login answered 429 by the rate limiter |
|
||||
|
||||
Scenario: A reset event never holds the code
|
||||
When a password reset is requested and completed
|
||||
Then neither event nor the process log contains the raw code, its hash or the new password
|
||||
|
||||
Scenario: Creating an API key is recorded
|
||||
When the owner sends POST /v1/api-key with a name "Deploy bot"
|
||||
Then one event exists with action "api_key.create", category "api_keys", actor the owner, source "web", target {"type": "api_key", "id": "<new id>", "name": "Deploy bot"}, outcome "success" and security false
|
||||
And no stored field and no response of the activity API contains the raw key or its hash
|
||||
|
||||
Scenario: Revoking an API key is recorded
|
||||
Given the API key 7 named "Deploy bot"
|
||||
When the owner sends DELETE /v1/api-key/7
|
||||
Then one event exists with action "api_key.revoke", category "api_keys", actor the owner, target {"type": "api_key", "id": "7", "name": "Deploy bot"} and outcome "success"
|
||||
|
||||
Scenario: A revoke of an unknown key records nothing
|
||||
When the owner sends DELETE /v1/api-key/99
|
||||
Then the response is 404 and no event is recorded
|
||||
|
||||
Scenario: The recorded events are readable through the API
|
||||
Given a failed login, an API key creation and a retention change were recorded
|
||||
When the owner sends GET /v1/activity
|
||||
Then the three events are listed newest first with their documented items
|
||||
And GET /v1/activity?security=true lists only the failed login
|
||||
@@ -0,0 +1,72 @@
|
||||
# QA procedure: activity-log-backend
|
||||
|
||||
Go only, no SPA. Package `test` (black-box), new files `test/activity_log_domain_test.go`, `test/activity_log_persistence_test.go`, `test/activity_log_http_test.go` and `test/activity_cleanup_test.go`. Fake and helper type names must be unique across the package. No live server, no real SMTP, no real SQLite file, no real sleeping, no `time.Ticker`. No env var is added. Never put a secret value in a test name or log assertion helper beyond the fixed test strings.
|
||||
|
||||
## Setup shared by every case
|
||||
1. `validator.InitValidator()` once. `jwtauth.New("HS256", []byte("test-secret"), nil)` standing in for `JWT_SECRET`.
|
||||
2. Fakes: `fakeAuditClock` (mutable, `Now()` and `Set(t)`, UTC), `mockAuditRepo` (in-memory slice, `Append`, `List`, `Summary`, `DeleteOlderThan`, failure and panic switches, call counters per method), existing `mockUserRepo` extended with users: owner 1, admin 2, a verified user 3 with role `"editor"` (domain has only owner and admin, so the role is a plain string in the fake), an unverified user 4. Fake API key repo with key 7 "Deploy bot" and a raw key built with `apikey.RawKeyPrefix`.
|
||||
3. Router: a fresh `chi.NewRouter()` mirroring `api/router.go`. Mount `/activity` (RequireSession, `jwtauth.Verifier`, `jwtauth.Authenticator`, `RequireCurrentToken`, `RequireVerifiedUser`, then the audit context middleware), `/settings` with the new PATCH, `/api-key` and `/auth` over the real use cases and the fakes. `httptest.NewRecorder` per request. Cookies are real `jwt` cookies signed with the test token carrying `user_id` and `token_version`.
|
||||
4. Capture the standard log with `log.SetOutput(&buf)` and restore with `t.Cleanup`. No parallel tests except where `-race` is stated.
|
||||
5. Seed helper `seedEvents(repo, n, spec)` building events with explicit `created_at` from the clock.
|
||||
|
||||
## A. Domain (no HTTP)
|
||||
6. Vocabulary table: for each of actor type, category, outcome, source and change kind, the exported constants equal the allowed set from the feature; an invalid value is rejected by the domain constructor or validator with the matching `Err` sentinel (`errors.Is`).
|
||||
7. Append-only guard: `reflect.TypeOf((*audit.Repository)(nil)).Elem()` has exactly `Append`, `List`, `Summary`, `DeleteOlderThan`. The persistence implementation's exported method set (via `reflect`) equals the same four. Router walk (`chi.Walk`) finds only GET under `/v1/activity`.
|
||||
8. Recorder: with `mockAuditRepo` failing (`errors.New("disk full")`), `Record` returns without error, and `buf` names the failed write and `disk full`. A panicking repo is recovered. A context holding actor, source, ip and user agent fills an event that lacks them; an event that already has an actor keeps it. `created_at` equals `fakeAuditClock.Now()` in UTC.
|
||||
|
||||
## B. Persistence (real in-memory SQLite, same model list as `api/dependecyInjection.go` and `migrate.go`)
|
||||
9. Migrate, then query `sqlite_master` and assert the table and three indexes: `created_at`; `(actor_type, actor_id, created_at)`; `(category, created_at)`. Assert the audit model is in the `migrate.go` list (migrate the full list, not just the audit model).
|
||||
10. Round-trip: append an event with params, changes (text and body kinds) and a null failure_reason; `List` returns equal fields. A system event round-trips with null ip and user agent.
|
||||
11. Order: three events with identical `created_at` come back by id descending, older last.
|
||||
12. Filter table from the feature (actor user, key, system, category, outcome, security, `q` case-insensitive, `q` containing `%` and `_` matched literally, date range inclusive of the whole `to` day UTC). Assert items and total for each.
|
||||
13. Paging: 60 events, page 2 size 25 gives items 26-50 and total 60; page 9 gives empty items and total 60.
|
||||
14. Summary: events in the last 7 days and the 7 days before; assert total, previous_total, failed, security, distinct user and key counts (anonymous and system excluded) and the four seven-element series, oldest first.
|
||||
15. `DeleteOlderThan`: events at 100, 40, 31, 29, 1 days; cutoff 30 days; returns 3, remaining ids as expected.
|
||||
16. Portability: assert no raw dialect-specific SQL (`ILIKE`, `strftime`, `date_trunc`, backticks) appears in the repository source. Cross-database run is manual (see below).
|
||||
|
||||
## C. Audit context middleware (probe handler echoing the context as JSON)
|
||||
17. Table: valid cookie for user 3 gives `{type user, id 3, name, source web}`; valid `Authorization: Bearer <key>` gives `{api_key, 7, "Deploy bot", source api_key}`; none gives anonymous and web.
|
||||
18. IP table with `TrustedProxyRealIP([]string{"10.0.0.5"})` in front: trusted peer plus `X-Real-IP` gives the header; untrusted peer ignores it; no header gives the peer. User agent of 400 characters is cut to 255.
|
||||
|
||||
## D. Read endpoints (HTTP)
|
||||
19. Gate table: no cookie 401 `You must be logged in to access this resource.`; API key 403 `This action needs a real session.`; unverified cookie 403 `You must verify your account to access this resource.`, each for list, summary and export, with `mockAuditRepo` List/Summary counters at 0.
|
||||
20. Roles: owner and admin see all actors; user 3 sees only its own with `total` of its own count; `actor=user:3` is 200, `actor=user:2`, `actor=key:7`, `actor=system` are 403 (body `Only owners and admins can do this.`); summary and export for user 3 are 403 with the same body.
|
||||
21. Shape: decode the first item into a map and compare with the JSON in the feature, including `"failure_reason": null`, `"params": {}`, integer `actor.id`, string `target.id`, `exists`. System event has `actor.id` null, `ip` null, `user_agent` null. Renamed and deleted user keeps the snapshot name. `exists` table with fake article repo (present, deleted) and user repo (deleted). A `body` change has `delta` and no `before` or `after`.
|
||||
22. Query table: every row of the filter outline, asserted against the seeded set; default range 7d (2-day event in, 10-day event out); default `page` 1, `size` 25.
|
||||
23. 400 table: every invalid query row gives exactly `The request cannot be processed due to a mismatch in the format of the body.` with the repo read counter at 0.
|
||||
24. Summary: the 7d example (12 / 10 / 2 users + 1 key / 3 / 2 and seven-length series that sum to the total); empty log all zeros; custom range previous period is the 7 days immediately before; invalid range 400.
|
||||
25. Export: status 200, `Content-Type: text/csv; charset=utf-8`, `Content-Disposition` begins `attachment`. Parse with `encoding/csv`: header `when,actor,action,target,outcome,source,ip`, row values as in the feature, no changes and no user agent column, system ip cell empty. Formula table (`=cmd`, `+1+1`, `-2`, `@SUM(A1)` get a leading `'`; `Spring release` and `a=b` untouched). A name with comma, quotes and a newline is one parseable record. `outcome=failed&page=2&size=25` returns only failed rows. 1200 events yield 1200 rows and `mockAuditRepo` saw more than one `List` call with a bounded size. Invalid filter gives 400 with no `Content-Type: text/csv`.
|
||||
|
||||
## E. Retention settings (HTTP)
|
||||
26. `GET /v1/settings` with no stored row: `activity_retention_days` 90, `activity_last_cleanup_at` null, `activity_last_cleanup_removed` 0, existing fields (`version`) unchanged. With a stored cleanup: values echoed (RFC3339 UTC).
|
||||
27. `PATCH` accepts 30, 90, 365, 0 (200, body echoes, a following GET agrees). 400 table: 7, -1, 366, 30.5, `"30"`, null, `{}`, non-JSON, each with the body-mismatch message and the stored value unchanged.
|
||||
28. Gate: admin 403 `Only the owner can do this.`; no cookie 401; owner API key 403 `This action needs a real session.`; unverified owner 403 verification text.
|
||||
29. Event: PATCH 90 to 30 records one `system.activity_retention_changed` event with actor owner, source web and params `{"from":90,"to":30}`; PATCH to the same value records none.
|
||||
|
||||
## F. Cleanup scheduler (no HTTP, hand-fed channels, `fakeAuditClock`)
|
||||
30. Tick table from the feature (never/last cleanup versus now, boundary at exactly 03:00, same-day second tick not due, next-day tick due). Assert `ran` and the `DeleteOlderThan` call count. Retention 0 never calls it.
|
||||
31. Cutoff: retention 30, events at 40, 31, 29, 1 days; run at 03:00; 31 and 40 removed; `last_cleanup_at` equals the clock; removed count 2. A request for a cutoff inside 24 hours is clamped to now minus 24 hours.
|
||||
32. Zero removed: fields updated, no `system.activity_cleanup` event. 142 removed: one event with action `system.activity_cleanup`, actor type system, source system, ip and user agent null, params `{"removed":142}`, and it is not deleted by the run that created it.
|
||||
33. `DeleteOlderThan` failure: Tick returns the error, last-cleanup fields unchanged, the log buffer holds the error, the next Tick runs again after healing.
|
||||
34. Runner: one tick runs one Tick; cancelling the context returns `Run` within a timeout; a done channel proves no goroutine outlives it (`-race`).
|
||||
35. Shortening retention from 90 to 30 deletes nothing at PATCH time (repo `DeleteOlderThan` counter 0); the next due Tick deletes older than 30.
|
||||
|
||||
## G. Producers (HTTP)
|
||||
36. Login: right credentials gives `auth.login` (actor user, source web, security false, failure_reason null, ip and user agent from the request); wrong password and unknown username give `auth.login_failed` with actor anonymous named by the attempted username, outcome failed, security true, failure_reason `Wrong username or password.`, identical for both; validation failure and a rate-limited 429 record nothing. No stored field or log line contains the password.
|
||||
37. Logout with a valid cookie gives `auth.logout`; without a cookie records nothing.
|
||||
38. Password reset: request for an existing account gives `auth.password_reset_requested` (security true); unknown email records nothing and the response is byte-identical to the existing account's response except `Date`; confirm with a valid code gives `auth.password_reset_completed`; invalid code records nothing. Neither the raw code, its hash nor the new password appears in any stored field or in `buf`.
|
||||
39. API key: `POST /v1/api-key` records `api_key.create` with the target `{api_key, <id>, name}`; `DELETE /v1/api-key/7` records `api_key.revoke`; `DELETE /v1/api-key/99` is 404 and records nothing. Marshal every stored event and every `/v1/activity` response and assert the raw key and its hash are absent.
|
||||
40. Recorder failure on each producer: the HTTP status and body equal those of the same request with a healthy repository.
|
||||
41. End to end: record a failed login, a key creation and a retention change, then `GET /v1/activity` lists them newest first, and `?security=true` returns only the failed login.
|
||||
|
||||
## H. Layering and hygiene guards
|
||||
42. Grep-style test: `internal/domain/audit` imports only the stdlib and other `internal/domain/*`; the audit use cases import no `gorm`, `persistence`, `mailer`, `net/http`, chi or `api/...`; handlers import no `persistence`, `models` or `gorm`; the persistence repository asserts `var _ audit.Repository = &X{}`; the GORM model lives in `persistence/models` with a mapper.
|
||||
43. Grep the new and edited files, Go and docs, for comments: none; only tool directives.
|
||||
44. Docs read: `docs/api.md` lists the three activity routes, the query parameters, response shapes, the 400/401/403 answers and the PATCH `/v1/settings` contract with the three new GET fields; `docs/architecture.md` describes `domain/audit`, the recorder and context, the persistence model and indexes, and the retention scheduler; `docs/configuration.md` states that retention is stored in the database (default 90) and that no env var was added.
|
||||
|
||||
## Manual (not automated)
|
||||
45. Real boot with `task dev:api`: log in with a wrong password, log in correctly, create and revoke an API key, PATCH retention as owner (with the session cookie), then read `/v1/activity` and `/v1/activity/export` and open the CSV in a spreadsheet to confirm injected cells show as text.
|
||||
46. Behind a reverse proxy with `TRUSTED_PROXIES` containing the proxy: the stored ip is the client's, not the proxy's. Without it: the proxy's.
|
||||
47. Backdate events in the SQLite file, set retention to 30 and either wait for 03:00 or move the system clock; confirm removed rows and the new Last cleanup values in `GET /v1/settings`.
|
||||
48. Run the persistence suite against a MySQL/MariaDB and a PostgreSQL instance through the documented database settings of `docs/configuration.md`: index creation, case-insensitive `q`, literal `%` and `_`, date-range boundaries, and `DeleteOlderThan` counts.
|
||||
49. SIGINT during a cleanup: the process exits after the run ends, with no database error on close.
|
||||
@@ -0,0 +1,506 @@
|
||||
Feature: Every real action of the back-office is recorded in the activity log from inside its use case
|
||||
|
||||
As the owner of a RenewCMS site
|
||||
I want articles, media, users and invites, and system tasks to leave an activity event
|
||||
So that the Activity screen shows who did what, and a failed attempt says why in plain words
|
||||
|
||||
Background:
|
||||
Given the spec of record is docs/design/screens/activity.md ("What is recorded", "API contract") and the foundation slice features/activity-log-backend.feature
|
||||
And this slice is backend only: no SPA, no new env var, no new table, no change to docs/design
|
||||
And events are recorded by the use cases through audit.Recorder, never by handlers, middleware or GORM hooks
|
||||
And every mutating use case method takes the request context as its first parameter and hands it to Recorder.Record, which fills actor, source, ip and user agent from the audit context
|
||||
And system events set the system actor and source "system" themselves, with ip and user_agent null
|
||||
And an event is recorded once, after the change was made (success) or after the use case decided to refuse or abort it (failed), never before
|
||||
And no read is recorded: no GET, no list, no public article read, no status poll
|
||||
And error responses are plain text from http.Error and success responses are JSON with snake_case fields, unchanged by this slice
|
||||
And the layering rule holds: use cases see only domain interfaces, no comment is added anywhere
|
||||
|
||||
Rule: The audit context reaches every producing route
|
||||
|
||||
Scenario Outline: A producing route carries the origin of the request
|
||||
When <request> is sent with <credentials>
|
||||
Then the stored event has actor <actor>, source "<source>" and the request ip and user agent
|
||||
|
||||
Examples:
|
||||
| request | credentials | actor | source |
|
||||
| POST /v1/article | the owner's jwt cookie | the owner, named by display name | web |
|
||||
| POST /v1/article | an API key "Deploy bot"| api_key id 7 named "Deploy bot" | api_key |
|
||||
| POST /v1/media | an API key "Deploy bot"| api_key id 7 named "Deploy bot" | api_key |
|
||||
| POST /v1/system/update/check | the owner's jwt cookie | the owner | web |
|
||||
|
||||
Rule: A failed audit write never changes the user's action
|
||||
|
||||
Scenario Outline: A failing or panicking repository leaves the response untouched
|
||||
Given the audit repository <fault> on Append
|
||||
When <request> succeeds
|
||||
Then the response status and body equal those of the same request with a healthy repository
|
||||
And the change itself was applied
|
||||
And the process log names the failed audit write
|
||||
|
||||
Examples:
|
||||
| fault | request |
|
||||
| fails | POST /v1/article |
|
||||
| fails | DELETE /v1/article/8 |
|
||||
| panics | PATCH /v1/article/8/publish |
|
||||
| fails | POST /v1/media |
|
||||
| fails | POST /v1/media/merge |
|
||||
| fails | POST /v1/user/invite |
|
||||
| fails | POST /v1/system/update/restart |
|
||||
|
||||
Rule: Articles
|
||||
|
||||
Scenario: Creating an article is recorded
|
||||
When the owner sends POST /v1/article with {"title": "Spring release", "body": "Hello"}
|
||||
Then one event exists with action "article.create", category "articles", actor the owner, source "web", target {"type": "article", "id": "<new id>", "name": "Spring release"}, outcome "success", failure_reason null and no changes
|
||||
And the stored event holds no article body
|
||||
|
||||
Scenario: Editing the body records a size delta and never the text
|
||||
Given the article 8 "Spring release" has a body of 100 characters
|
||||
When the owner sends PATCH /v1/article/8 with a body of 412 characters
|
||||
Then one event exists with action "article.edit", target {"type": "article", "id": "8", "name": "Spring release"} and changes [{"field": "body", "kind": "body", "delta": 312}]
|
||||
And no stored field and no /v1/activity response contains any text of either body
|
||||
|
||||
Scenario Outline: The delta is signed and counts characters
|
||||
Given the article 8 has a body of <before> characters
|
||||
When its body is replaced by one of <after> characters
|
||||
Then the change delta is <delta>
|
||||
|
||||
Examples:
|
||||
| before | after | delta |
|
||||
| 100 | 412 | 312 |
|
||||
| 412 | 100 | -312 |
|
||||
| 5 | 5 | 0 |
|
||||
|
||||
Scenario: A body with multi-byte characters is counted in characters
|
||||
Given the article 8 has the body "héllo"
|
||||
When its body is replaced by "héllo wörld"
|
||||
Then the change delta is 6
|
||||
|
||||
Scenario: Saving an identical body records nothing
|
||||
Given the article 8 has the body "Same text"
|
||||
When the owner sends PATCH /v1/article/8 with the body "Same text"
|
||||
Then the response is 200 and no event is recorded
|
||||
|
||||
Scenario: A title change is recorded as a text diff when a title edit exists
|
||||
Given a use case that changes an article's title from "Spring release" to "Spring release 2"
|
||||
Then its event has action "article.edit" and changes [{"field": "title", "kind": "text", "before": "Spring release", "after": "Spring release 2"}]
|
||||
And only the fields title, online and body can ever appear in an article event's changes
|
||||
|
||||
Scenario: Publishing is recorded with the publish state
|
||||
Given the article 8 "Spring release" is offline
|
||||
When the owner sends PATCH /v1/article/8/publish
|
||||
Then one event exists with action "article.publish", category "articles" and changes [{"field": "online", "kind": "text", "before": "false", "after": "true"}]
|
||||
|
||||
Scenario: Unpublishing is recorded with the publish state
|
||||
Given the article 8 is online
|
||||
When the owner sends PATCH /v1/article/8/unpublish
|
||||
Then one event exists with action "article.unpublish" and changes [{"field": "online", "kind": "text", "before": "true", "after": "false"}]
|
||||
|
||||
Scenario Outline: A publish state that does not change records nothing
|
||||
Given the article 8 is <state>
|
||||
When the owner sends PATCH /v1/article/8/<path>
|
||||
Then the response is 200 and no event is recorded
|
||||
|
||||
Examples:
|
||||
| state | path |
|
||||
| online | publish |
|
||||
| offline | unpublish |
|
||||
|
||||
Scenario: Deleting an article is recorded with its name snapshot
|
||||
Given the article 8 "Spring release" exists
|
||||
When the owner sends DELETE /v1/article/8
|
||||
Then one event exists with action "article.delete", target {"type": "article", "id": "8", "name": "Spring release"} and outcome "success"
|
||||
And GET /v1/activity shows that target with exists false
|
||||
|
||||
Scenario Outline: A refused article action is recorded as failed with a plain reason
|
||||
Given <state>
|
||||
When <request>
|
||||
Then one event exists with action "<action>", outcome "failed" and failure_reason "<reason>"
|
||||
And the response is unchanged
|
||||
|
||||
Examples:
|
||||
| state | request | action | reason |
|
||||
| no article has this title | POST /v1/article with a 2 character title | article.create | The title must be between 3 and 200 characters. |
|
||||
| the repository fails with "db is locked" | POST /v1/article with a valid title | article.create | Something went wrong on our side. Please try again. |
|
||||
| the repository fails with "db is locked" | PATCH /v1/article/8/publish | article.publish | Something went wrong on our side. Please try again. |
|
||||
|
||||
Scenario: An article that does not exist records nothing
|
||||
When the owner sends DELETE /v1/article/99, PATCH /v1/article/99, PATCH /v1/article/99/publish and PATCH /v1/article/99/unpublish
|
||||
Then every response is 404 and no event is recorded
|
||||
|
||||
Scenario: A request that fails validation records nothing
|
||||
When the owner sends POST /v1/article with an empty body or a malformed id such as /v1/article/abc
|
||||
Then the response is 400 and no event is recorded
|
||||
|
||||
Scenario Outline: Reads are never recorded
|
||||
When <request> is sent
|
||||
Then no event is recorded
|
||||
|
||||
Examples:
|
||||
| request |
|
||||
| GET /v1/article |
|
||||
| GET /v1/article/8 |
|
||||
| GET /v1/article/8 as the public |
|
||||
|
||||
Rule: Media
|
||||
|
||||
Scenario: Uploading an image is recorded
|
||||
When the owner sends POST /v1/media with the PNG "spring-banner.png"
|
||||
Then one event exists with action "media.upload", category "media", actor the owner, target {"type": "image", "id": "<new id>", "name": "spring-banner.png"} and outcome "success"
|
||||
And the stored event holds no file content and no storage path
|
||||
|
||||
Scenario Outline: A refused upload is recorded as failed
|
||||
When the owner uploads <file>
|
||||
Then one event exists with action "media.upload", outcome "failed" and failure_reason "<reason>"
|
||||
And the target name is the file name as sent, normalised the way a stored upload would be named, or empty when it is unusable
|
||||
And the response is unchanged
|
||||
|
||||
Examples:
|
||||
| file | reason |
|
||||
| a GIF | The file must be a PNG, JPEG, WEBP or SVG image. |
|
||||
| a PNG larger than the limit | The file is too large to upload. |
|
||||
| a corrupt PNG | The image could not be read. |
|
||||
|
||||
Scenario: Renaming an image records the filename diff
|
||||
Given the image 5 "banner.png"
|
||||
When the owner sends PATCH /v1/media/5 with {"filename": "spring-banner"}
|
||||
Then one event exists with action "media.rename", target {"type": "image", "id": "5", "name": "spring-banner.png"} and changes [{"field": "filename", "kind": "text", "before": "banner.png", "after": "spring-banner.png"}]
|
||||
|
||||
Scenario: Renaming to the same name records nothing
|
||||
Given the image 5 "banner.png"
|
||||
When the owner renames it to "banner"
|
||||
Then no event is recorded
|
||||
|
||||
Scenario: A refused rename is recorded as failed
|
||||
Given the image 5 "banner.png"
|
||||
When the owner sends PATCH /v1/media/5 with an invalid filename
|
||||
Then one event exists with action "media.rename", outcome "failed", failure_reason "That file name is not valid." and target {"type": "image", "id": "5", "name": "banner.png"}
|
||||
|
||||
Scenario: Deleting an image is recorded with its name snapshot
|
||||
Given the image 5 "banner.png"
|
||||
When the owner sends DELETE /v1/media/5
|
||||
Then one event exists with action "media.delete", target {"type": "image", "id": "5", "name": "banner.png"} and outcome "success"
|
||||
And GET /v1/activity shows that target with exists false
|
||||
|
||||
Scenario: Merging images is recorded once with its counts
|
||||
Given the images 5 "banner.png", 6 "banner-copy.png" and 7 "banner-copy-2.png" share one content hash
|
||||
And two articles reference the copies
|
||||
When the owner sends POST /v1/media/merge keeping 5 and merging [6, 7]
|
||||
Then one event exists with action "media.merge", target {"type": "image", "id": "5", "name": "banner.png"} and params {"merged": 2, "articles_updated": 2}
|
||||
And no event names a removed copy as a separate action
|
||||
|
||||
Scenario Outline: A refused merge is recorded as failed
|
||||
When the owner sends POST /v1/media/merge that fails with <error>
|
||||
Then one event exists with action "media.merge", outcome "failed" and failure_reason "<reason>"
|
||||
|
||||
Examples:
|
||||
| error | reason |
|
||||
| ErrInvalidMergeRequest | The merge request is not valid. |
|
||||
| ErrMergeCopyMissing | One of the copies no longer exists. |
|
||||
| ErrMergeHashMismatch | The images are not identical copies.|
|
||||
|
||||
Scenario: A manual purge is recorded once and not as separate merges
|
||||
Given a purge plan with 2 groups, 3 copies and 5 articles to update
|
||||
When the owner sends POST /v1/media/purge
|
||||
Then one event exists with action "media.purge", category "media", actor the owner, source "web", outcome "success" and params {"merged": 3, "articles_updated": 5}
|
||||
And no "media.merge" event is recorded for its groups
|
||||
|
||||
Scenario: A partly failed manual purge is a success with the failed group count
|
||||
Given a purge plan with 3 groups where 1 group fails
|
||||
When the owner sends POST /v1/media/purge
|
||||
Then one event exists with action "media.purge", outcome "success" and params that include "failed_groups": 1
|
||||
|
||||
Scenario Outline: A refused manual purge is recorded as failed
|
||||
When the owner sends POST /v1/media/purge and the use case returns <error>
|
||||
Then one event exists with action "media.purge", outcome "failed" and failure_reason "<reason>"
|
||||
|
||||
Examples:
|
||||
| error | reason |
|
||||
| ErrPurgeConflict | The library changed since the preview. Review it again. |
|
||||
| ErrPurgeBusy | A purge is already running. |
|
||||
|
||||
Scenario: The scheduled purge is a system event
|
||||
Given the purge is due and merges 3 copies and updates 5 articles
|
||||
When PurgeSchedulerUseCase.Tick runs
|
||||
Then one event exists with action "system.media_purge", category "system", actor type system named "System", source "system", ip null, user_agent null, outcome "success" and params {"merged": 3, "articles_updated": 5}
|
||||
|
||||
Scenario: A scheduled purge with a failed group records the failed group count
|
||||
Given the purge is due and 1 group fails while others merge
|
||||
When Tick runs
|
||||
Then one event exists with action "system.media_purge", outcome "success" and params that include "failed_groups": 1
|
||||
|
||||
Scenario: A scheduled purge that is not due or merges nothing records nothing
|
||||
Given the purge is not due, or is due and finds no duplicate
|
||||
When Tick runs
|
||||
Then no event is recorded
|
||||
|
||||
Scenario: A scheduled purge that cannot record its run is a failed system event
|
||||
Given the purge is due and the run result cannot be saved
|
||||
When Tick runs
|
||||
Then one event exists with action "system.media_purge", outcome "failed" and failure_reason "Something went wrong on our side. Please try again."
|
||||
|
||||
Scenario: Browsing and the media usage lookup are never recorded
|
||||
When the owner sends GET /v1/media, GET /v1/media/5/usage, GET /v1/media/purge-preview and GET /v1/media/purge-settings
|
||||
Then no event is recorded
|
||||
|
||||
Rule: Users and invites
|
||||
|
||||
Scenario: Registering is recorded with the new account as actor
|
||||
Given registration is open
|
||||
When POST /v1/auth/register is sent with the username "marta"
|
||||
Then one event exists with action "user.register", category "users", actor user "marta" with the new id, source "web", target {"type": "user", "id": "<new id>", "name": "marta"} and outcome "success"
|
||||
And the stored event holds no password, no hash and no verification code
|
||||
|
||||
Scenario Outline: A refused registration is recorded as failed
|
||||
When POST /v1/auth/register is sent and <cause>
|
||||
Then one event exists with action "user.register", outcome "failed", failure_reason "<reason>", actor anonymous named by the attempted username and no target
|
||||
And the response is unchanged
|
||||
|
||||
Examples:
|
||||
| cause | reason |
|
||||
| registration is closed | Registration is closed on this site. |
|
||||
| the verification email cannot be sent | The verification email could not be sent. |
|
||||
|
||||
Scenario: Verifying an account is recorded
|
||||
Given the unverified user 4 "lea" with a valid code
|
||||
When user 4 sends POST /v1/auth/verify with that code
|
||||
Then one event exists with action "user.verify", category "users", actor user 4, target {"type": "user", "id": "4", "name": "lea"} and outcome "success"
|
||||
And the stored event does not hold the code or its hash
|
||||
|
||||
Scenario Outline: A refused verification is recorded as failed
|
||||
Given the unverified user 4 "lea"
|
||||
When user 4 sends POST /v1/auth/verify and the use case returns <error>
|
||||
Then one event exists with action "user.verify", outcome "failed" and failure_reason "<reason>"
|
||||
|
||||
Examples:
|
||||
| error | reason |
|
||||
| ErrInvalidVerificationCode | The verification code is not valid. |
|
||||
| ErrVerificationCodeExpired | The verification code has expired. |
|
||||
|
||||
Scenario: Changing the display name records a before and after
|
||||
Given user 3 has the display name "Marta"
|
||||
When user 3 sends PATCH /v1/user/me with {"display_name": "Marta Alves"}
|
||||
Then one event exists with action "user.update_profile", category "users", target {"type": "user", "id": "3", "name": "Marta Alves"} and changes [{"field": "display_name", "kind": "text", "before": "Marta", "after": "Marta Alves"}]
|
||||
And the actor snapshot name is the name held when the request began
|
||||
|
||||
Scenario Outline: Setting a display name that does not change records nothing
|
||||
Given user 3 has the display name "<current>"
|
||||
When user 3 sends PATCH /v1/user/me with {"display_name": "<sent>"}
|
||||
Then no event is recorded
|
||||
|
||||
Examples:
|
||||
| current | sent |
|
||||
| Marta | Marta |
|
||||
| Marta | Marta |
|
||||
| | |
|
||||
|
||||
Scenario: A display name that is too long is recorded as failed
|
||||
When user 3 sends PATCH /v1/user/me with a 61 character name
|
||||
Then one event exists with action "user.update_profile", outcome "failed" and failure_reason "That display name is too long."
|
||||
And the stored event holds no changes
|
||||
|
||||
Scenario: Creating an invite is recorded
|
||||
When the owner sends POST /v1/user/invite with {"email": "new@example.com"}
|
||||
Then one event exists with action "invite.create", category "users", actor the owner, target {"type": "invite", "id": "<invite id>", "name": "new@example.com"} and outcome "success"
|
||||
And the stored event holds no invite code and no hash
|
||||
|
||||
Scenario Outline: A refused invite action is recorded as failed
|
||||
When the owner <request> and the use case returns <error>
|
||||
Then one event exists with action "<action>", outcome "failed" and failure_reason "<reason>"
|
||||
|
||||
Examples:
|
||||
| request | error | action | reason |
|
||||
| sends POST /v1/user/invite | ErrEmailHasAccount | invite.create | That email already has an account. |
|
||||
| sends POST /v1/user/invite | ErrPendingInvite | invite.create | An invitation is already pending for that email. |
|
||||
| sends POST /v1/user/invite | ErrInviteEmailNotSent | invite.create | The invitation email could not be sent. |
|
||||
| sends POST /v1/user/invite/4/renew | ErrInviteEmailNotSent | invite.renew | The invitation email could not be sent. |
|
||||
| sends POST /v1/user/invite/4/renew | ErrInviteAlreadyAccepted | invite.renew | That invitation was already accepted. |
|
||||
| sends DELETE /v1/user/invite/4 | ErrInviteAlreadyAccepted | invite.cancel | That invitation was already accepted. |
|
||||
|
||||
Scenario: Cancelling an invite is recorded
|
||||
Given the pending invite 4 for "new@example.com"
|
||||
When the owner sends DELETE /v1/user/invite/4
|
||||
Then one event exists with action "invite.cancel", target {"type": "invite", "id": "4", "name": "new@example.com"} and outcome "success"
|
||||
|
||||
Scenario: Renewing an invite is recorded
|
||||
Given the pending invite 4 for "new@example.com"
|
||||
When the owner sends POST /v1/user/invite/4/renew
|
||||
Then one event exists with action "invite.renew", target {"type": "invite", "id": "4", "name": "new@example.com"} and outcome "success"
|
||||
And a renewal whose email fails is rolled back and recorded only as failed
|
||||
|
||||
Scenario: Accepting an invite is recorded with the new account as actor
|
||||
Given the pending invite 4 for "new@example.com"
|
||||
When POST /v1/auth/invite/accept is sent with a valid code and the username "sam"
|
||||
Then one event exists with action "invite.accept", category "users", actor user "sam" with the new id, source "web", target {"type": "invite", "id": "4", "name": "new@example.com"} and outcome "success"
|
||||
And the stored event holds no code, no hash and no password
|
||||
|
||||
Scenario Outline: A refused acceptance is recorded as failed
|
||||
When POST /v1/auth/invite/accept is sent and the use case returns <error>
|
||||
Then one event exists with action "invite.accept", outcome "failed", failure_reason "<reason>" and actor anonymous named by the attempted username
|
||||
And the stored event has no target when the invite could not be identified
|
||||
|
||||
Examples:
|
||||
| error | reason |
|
||||
| ErrInviteInvalid | That invitation is not valid. |
|
||||
| ErrInviteExpired | That invitation has expired. |
|
||||
| ErrInviteAlreadyAccepted | That invitation was already accepted. |
|
||||
| ErrUsernameTaken | That username is already taken. |
|
||||
| ErrEmailHasAccount | That email already has an account. |
|
||||
|
||||
Scenario: An invite or user that does not exist records nothing
|
||||
When the owner sends DELETE /v1/user/invite/99 or POST /v1/user/invite/99/renew
|
||||
Then every response is 404 and no event is recorded
|
||||
|
||||
Scenario Outline: Reads and checks are never recorded
|
||||
When <request> is sent
|
||||
Then no event is recorded
|
||||
|
||||
Examples:
|
||||
| request |
|
||||
| GET /v1/user |
|
||||
| GET /v1/auth/me |
|
||||
| GET /v1/auth/invite?code=<code> |
|
||||
| GET /v1/auth/registration |
|
||||
|
||||
Rule: System tasks
|
||||
|
||||
Scenario: Checking for an update is recorded
|
||||
Given the release feed answers v1.4.0 while the running version is v1.3.2
|
||||
When the owner sends POST /v1/system/update/check
|
||||
Then one event exists with action "system.update_checked", category "system", actor the owner, source "web", target {"type": "release", "id": "v1.4.0", "name": "v1.4.0"}, outcome "success" and params {"update_available": true}
|
||||
|
||||
Scenario Outline: A failed update check is recorded with a plain reason
|
||||
When the owner sends POST /v1/system/update/check and the use case returns <error>
|
||||
Then one event exists with action "system.update_checked", outcome "failed", failure_reason "<reason>" and no target
|
||||
|
||||
Examples:
|
||||
| error | reason |
|
||||
| ErrFeedUnavailable | The release feed could not be reached. |
|
||||
| ErrNoRelease | No release was found. |
|
||||
|
||||
Scenario: The status poll is never recorded
|
||||
When the owner sends GET /v1/system/update
|
||||
Then no event is recorded
|
||||
|
||||
Scenario: A download that completes is recorded when the job is ready
|
||||
Given a newer release v1.4.0 is available and the update helper is installed
|
||||
When the owner sends POST /v1/system/update/download
|
||||
Then the response is returned before the job ends and no event exists yet
|
||||
And when the job reaches the ready state one event exists with action "system.update_downloaded", actor the owner, source "web", target {"type": "release", "id": "v1.4.0", "name": "v1.4.0"} and outcome "success"
|
||||
And the origin (actor, ip, user agent) is the one captured at request time
|
||||
|
||||
Scenario Outline: A download that fails is recorded with a reason that hides the cause
|
||||
Given a newer release v1.4.0 is available and the job fails with the error kind "<kind>"
|
||||
Then one event exists with action "system.update_downloaded", outcome "failed" and failure_reason "<reason>"
|
||||
And the stored event holds no url, no path and no raw error text
|
||||
|
||||
Examples:
|
||||
| kind | reason |
|
||||
| download | The update could not be downloaded. |
|
||||
| verification | The downloaded update failed its integrity check. |
|
||||
|
||||
Scenario Outline: A download refused at the start is recorded as failed
|
||||
When the owner sends POST /v1/system/update/download and the use case returns <error>
|
||||
Then one event exists with action "system.update_downloaded", outcome "failed" and failure_reason "<reason>"
|
||||
And the response is unchanged
|
||||
|
||||
Examples:
|
||||
| error | reason |
|
||||
| ErrNotPrepared | This server cannot apply updates. |
|
||||
| ErrJobInProgress | An update is already being downloaded. |
|
||||
| ErrRestartPending | A restart is already pending. |
|
||||
| ErrNothingToUpdate | There is nothing to update. |
|
||||
| ErrPreflightRefused | The update cannot be downloaded. |
|
||||
|
||||
Scenario: A restart request is recorded
|
||||
Given the update helper is installed and no job is running
|
||||
When the owner sends POST /v1/system/update/restart
|
||||
Then one event exists with action "system.restart_requested", category "system", actor the owner, source "web", outcome "success" and params {"kind": "restart"}
|
||||
|
||||
Scenario: A restart that applies a downloaded update records the kind and version
|
||||
Given a downloaded update v1.4.0 is ready
|
||||
When the owner sends POST /v1/system/update/restart
|
||||
Then the event has params {"kind": "update", "tag": "v1.4.0"}
|
||||
|
||||
Scenario Outline: A refused restart is recorded as failed
|
||||
When the owner sends POST /v1/system/update/restart and the use case returns <error>
|
||||
Then one event exists with action "system.restart_requested", outcome "failed" and failure_reason "<reason>"
|
||||
|
||||
Examples:
|
||||
| error | reason |
|
||||
| ErrNotPrepared | This server cannot apply updates. |
|
||||
| ErrJobInProgress | An update is still being downloaded. |
|
||||
| ErrRestartPending | A restart is already pending. |
|
||||
| ErrRestartKindMismatch | The requested restart does not match the server state. |
|
||||
|
||||
Rule: A failed outcome never exposes more than the plain-language reason
|
||||
|
||||
Scenario: Failure reasons are mapped from domain sentinels
|
||||
Given a table that maps each domain Err sentinel above to a fixed sentence
|
||||
When a use case fails with an error that wraps one of them
|
||||
Then failure_reason is that sentence, matched with errors.Is
|
||||
|
||||
Scenario Outline: Unmapped errors become the generic sentence
|
||||
When a use case fails with <error>
|
||||
Then failure_reason is "Something went wrong on our side. Please try again."
|
||||
And neither the event, the activity API nor the CSV export holds the text of the error
|
||||
|
||||
Examples:
|
||||
| error |
|
||||
| errors.New("db is locked") |
|
||||
| an error carrying an SMTP host and password |
|
||||
| an error carrying a file system path |
|
||||
| an error wrapping a sentinel and a raw cause |
|
||||
|
||||
Scenario: Every failure reason fits the stored limit
|
||||
Then every mapped sentence is at most 255 characters
|
||||
|
||||
Scenario: A failed event keeps its target when the target is known
|
||||
Given the image 5 "banner.png" exists
|
||||
When a rename of image 5 is refused
|
||||
Then the failed event keeps the target snapshot of image 5
|
||||
|
||||
Rule: Secrets and bodies never reach the log
|
||||
|
||||
Scenario Outline: Sensitive values are absent everywhere
|
||||
Given the action <action> handled <secret>
|
||||
When it is recorded, listed through GET /v1/activity and exported through GET /v1/activity/export
|
||||
Then no stored field, no params, no change, no response, no CSV cell and no process log line contains <secret>
|
||||
|
||||
Examples:
|
||||
| action | secret |
|
||||
| user.register | the password, its hash and the verification code |
|
||||
| user.verify | the verification code and its hash |
|
||||
| invite.create | the invite code and its hash |
|
||||
| invite.accept | the invite code, its hash and the password |
|
||||
| article.edit | the old and the new article body |
|
||||
| media.upload | the file bytes and the storage path |
|
||||
| system.update_downloaded | the asset url and the staging path |
|
||||
|
||||
Scenario: Only allowlisted fields appear in changes
|
||||
Then the only change fields any producer emits are title, online, body, filename and display_name
|
||||
And the only change kinds are text and body
|
||||
|
||||
Rule: The recorded events read back through the existing API
|
||||
|
||||
Scenario: Producer events are listed with their documented shape
|
||||
Given an article was created, edited, published and deleted by the owner
|
||||
When the owner sends GET /v1/activity?category=articles
|
||||
Then the four events are listed newest first, with category "articles", the documented item shape and the article target exists false after the delete
|
||||
|
||||
Scenario Outline: Categories filter the producers
|
||||
Given one event of each producer was recorded
|
||||
When the owner sends GET /v1/activity?category=<category>
|
||||
Then the items are exactly the events listed in <actions>
|
||||
|
||||
Examples:
|
||||
| category | actions |
|
||||
| articles | article.create, article.edit, article.publish, article.unpublish, article.delete |
|
||||
| media | media.upload, media.rename, media.delete, media.merge, media.purge |
|
||||
| users | user.register, user.verify, user.update_profile, invite.create, invite.cancel, invite.renew, invite.accept |
|
||||
| system | system.media_purge, system.update_checked, system.update_downloaded, system.restart_requested |
|
||||
|
||||
Scenario: A failed producer event is filterable
|
||||
Given a refused merge and a successful rename were recorded
|
||||
When the owner sends GET /v1/activity?outcome=failed
|
||||
Then only the refused merge is listed with its failure_reason
|
||||
@@ -0,0 +1,67 @@
|
||||
# QA procedure: activity-log-producers
|
||||
|
||||
Go only, no SPA. Package `test` (black-box). New files: `test/activity_producers_articles_test.go`, `test/activity_producers_media_test.go`, `test/activity_producers_users_test.go`, `test/activity_producers_system_test.go`, `test/activity_producers_reasons_test.go`. Reuse the foundation helpers in `test/activity_support_test.go` (`mockAuditRepo`, `fakeAuditClock`, `seedEvents`). Extend the existing hand-written fakes (`mockArticleRepo`, the image, user, invite, purge, update and status fakes) with failure switches and call counters. New fake type names must be unique across the package. No live server, no real SMTP, no real SQLite file, no real sleeping, no `time.Ticker`. Secrets in tests are fixed test strings only.
|
||||
|
||||
## Setup shared by every case
|
||||
1. `validator.InitValidator()` once. `jwtauth.New("HS256", []byte("test-secret"), nil)` stands in for `JWT_SECRET`.
|
||||
2. One `auditing.NewRecorder(mockAuditRepo, fakeAuditClock.Now, logger)` shared by every use case. `logger` writes to a `bytes.Buffer` (restore with `t.Cleanup`).
|
||||
3. Users: owner 1, admin 2, verified user 3 "Marta", unverified user 4 "lea" (with a hashed code). API key 7 "Deploy bot" in the fake key repo.
|
||||
4. A fresh `chi.NewRouter()` per test that mirrors `api/router.go` for the article, user, media, system and auth routers, with `middleware.AuditContext(getUser, trustedProxies)` placed exactly where the router wires it (after `apiKeyAuth()` on the article, user and media routers). Cookies are real signed `jwt` cookies with `user_id` and `token_version`. `httptest.NewRecorder` per request.
|
||||
5. Helpers: `eventsOf(repo)`, `onlyEvent(t, repo)` (fails unless exactly one event), `assertNoEvent(t, repo)`.
|
||||
6. Each mutating use case method takes `ctx context.Context` first. A method called with a context holding no origin still works.
|
||||
|
||||
## A. Origin and routing
|
||||
7. Table (feature rule 1): each request with the cookie or the API key yields the expected actor type, id, name and source, plus the request ip and user agent. Proves `AuditContext` is mounted on the article, user, media and system routers.
|
||||
8. A route without credentials, or answered 401/403/429, records nothing.
|
||||
|
||||
## B. Articles
|
||||
9. Create: one `article.create` event, target id string, name as typed, no changes, no body anywhere (marshal the event and assert absence).
|
||||
10. Edit: table `(before, after, delta)`: 100 to 412 (312), 412 to 100 (-312), 5 to 5 with different content (0, still recorded), `héllo` to `héllo wörld` (6, characters not bytes). Identical body records nothing. Change kind `body`, `Delta` set, `Before` and `After` nil.
|
||||
11. Publish and unpublish: `online` text change (`"false"`/`"true"`). Repeating the same state records nothing and returns 200.
|
||||
12. Delete: event keeps the name snapshot. After it, `GET /v1/activity` shows `target.exists` false.
|
||||
13. Title diff: unit-test the change builder with title, online and body only; any other field name is rejected.
|
||||
14. Failure table: 2 character title, repository failing with `errors.New("db is locked")` (generic reason). 404 on delete, edit, publish, unpublish of id 99 and 400 on malformed id or body record nothing.
|
||||
15. Reads (list, get, public get) record nothing.
|
||||
|
||||
## C. Media
|
||||
16. Upload: in-memory PNG, target name equals the normalised filename, no bytes and no path. Failed upload table: GIF, oversize, corrupt PNG, each with its mapped reason.
|
||||
17. Rename: `filename` text change `banner.png` to `spring-banner.png`. Same name records nothing. Invalid stem records a failed event keeping the target. Missing image records nothing (404).
|
||||
18. Delete: event with name snapshot. Missing image records nothing.
|
||||
19. Merge: three images with one hash, two articles referencing the copies. One `media.merge` event with `{"merged":2,"articles_updated":2}`, target the kept image. Failure table: `ErrInvalidMergeRequest`, `ErrMergeCopyMissing`, `ErrMergeHashMismatch`.
|
||||
20. Manual purge: plan with 2 groups, 3 copies, 5 articles. Exactly one `media.purge` event with `{"merged":3,"articles_updated":5}` and zero `media.merge` events. A plan with one failing group: outcome success with `failed_groups` 1. `ErrPurgeConflict` and `ErrPurgeBusy` give failed events. Preview, usage, list and settings GETs record nothing.
|
||||
21. Scheduled purge: drive `PurgeSchedulerUseCase.Tick(now)` with the fake purge repo and clock. Due and merging 3: one `system.media_purge` event with system actor, source system, ip nil, user agent nil. Due with a failed group: success with `failed_groups`. Not due, or no duplicates: none. A fake `SaveLastRun` failure: failed event with the generic reason.
|
||||
22. Manual purge after the scheduler in the same test yields two events with sources `web` and `system`.
|
||||
|
||||
## D. Users and invites
|
||||
23. Register: open registration records `user.register` with the new account as actor and target. Closed registration and a failing mailer (error carrying a fake SMTP password) record failed events with the mapped reason, anonymous actor named by the attempted username, no raw text. Marshal the event and assert password, hash and code are absent.
|
||||
24. Verify: success, invalid code, expired code.
|
||||
25. Display name: before/after diff, trimmed no-op records nothing, empty to empty records nothing, 61 characters records a failed event with no changes. Actor snapshot name is the pre-change name.
|
||||
26. Invites: create, cancel, renew, accept each record the documented event with the invited email as target name. Failure table covers every error in the feature. A renew whose mail fails is rolled back and records one failed event only. Unknown ids (404) record nothing.
|
||||
27. Accept: actor is the new account, source `web`; failed accept actor is anonymous with the attempted username.
|
||||
28. Reads (`GET /v1/user`, `/v1/auth/me`, `/v1/auth/invite`, `/v1/auth/registration`) record nothing.
|
||||
|
||||
## E. System tasks
|
||||
29. Check: success event with `{"update_available":true}` and a release target. `ErrFeedUnavailable` and `ErrNoRelease` give failed events without target. Status `GET` records nothing.
|
||||
30. Download: fakes complete the job through a hand-fed channel (no `time.Sleep`). No event before the job ends, one after `ready`; actor, ip and user agent are those captured at request time even though the request context was cancelled. Failure kinds `download` and `verification` give the two reasons, no URL or path. Start-time refusals give failed events. Run with `-race`.
|
||||
31. Restart: `{"kind":"restart"}` and `{"kind":"update","tag":"v1.4.0"}`. Refusals for `ErrNotPrepared`, `ErrJobInProgress`, `ErrRestartPending`, `ErrRestartKindMismatch`.
|
||||
|
||||
## F. Reasons, secrets and recorder faults
|
||||
32. Reason table: wrap each sentinel with `fmt.Errorf("%w: %w", sentinel, errors.New("secret detail"))`, assert the sentence equals the table and the extra text is absent. Unmapped errors give the generic sentence. Every sentence is at most 255 characters.
|
||||
33. Recorder fault: for each producer row of the fault table, run with failing, panicking and healthy `mockAuditRepo`. Identical status and body, change applied, log line naming the failed write.
|
||||
34. Secret sweep: after exercising every producer, marshal every stored event, the `/v1/activity` response and the `/v1/activity/export` file; assert none contains the password, hash, verification code, invite code, raw API key, article body, file bytes, asset URL or staging path. The log buffer is free of them too.
|
||||
35. Allowlist: every `Change.Field` is in `{title, online, body, filename, display_name}` and every kind is `text` or `body`.
|
||||
36. Read-back: list by `category=articles|media|users|system` and `outcome=failed`; compare action sets with the feature tables.
|
||||
|
||||
## G. Mutation guard (gremlins stays clean)
|
||||
37. `task mutate` scope is `./internal/infrastructure/useCases`. Each new branch needs a killing assertion: every no-op guard (body, online, filename, display name) with equal and differing values; delta sign and rune-versus-byte count; success versus failed outcome and recorded once after the change (event count and order against the repository call); every `errors.Is` row of the reason map plus the generic fallback; every `ctx` forwarding; the system-actor defaults. Bare-bool conditions produce no mutant, so cover both branches with tests regardless.
|
||||
|
||||
## H. Layering and hygiene
|
||||
38. Touched use cases import no `gorm`, `persistence`, `mailer`, `net/http`, chi or `api/...`; handlers only pass `r.Context()`; `internal/domain/audit` imports only stdlib and domain packages.
|
||||
39. No comments in new or edited Go files, test files and docs, except tool directives.
|
||||
40. Docs (`docs/api.md`, `docs/architecture.md`) list the action catalogue, target types, reason sentences and the rule that no read is recorded.
|
||||
|
||||
## Manual (not automated)
|
||||
41. Real boot with `task dev:api` and SMTP: register, verify, invite, renew, accept and cancel an invite, change display name, create/edit/publish/unpublish/delete an article, upload/rename/merge/delete images, manual purge; read `/v1/activity` and the CSV export and read every sentence.
|
||||
42. Real update helper on Linux systemd (or the Docker QA flow): check, download until `ready`, restart; one event each, real asset names absent. Skip in a container.
|
||||
43. Leave the scheduled purge due and wait for the hourly tick: a single `system.media_purge` event, source `system`, no ip or user agent.
|
||||
44. Remove the database file's write access during one article edit: edit result unchanged, process log names the audit write failure.
|
||||
@@ -0,0 +1,349 @@
|
||||
Feature: Recent activity card on Home and Retention card on Settings
|
||||
|
||||
As a RenewCMS user
|
||||
I want Home to show my last five activity events and the owner to choose how long activity is kept on Settings
|
||||
So that I see what happened without opening the Activity page, and the log does not grow without bound
|
||||
|
||||
Background:
|
||||
Given the activity API (features/activity-log-backend.feature, activity-log-producers.feature, activity_screen.feature) and the settings retention fields already exist
|
||||
And this slice adds one field to the settings API, activity_next_cleanup_at, and wires the SPA to the existing endpoints; there is no other Go change
|
||||
And the Home card and the Settings "Retention" card are the two items activity_screen.feature left out of scope; the spec of record is docs/design/screens/home.md ("Recent activity card") and docs/design/screens/settings.md ("Activity retention"), groups "Home card" and "Settings retention" of RenewCMS Screens Activity.dc.html
|
||||
And both boards draw the existing cards as dashed placeholders, so neither card is pixel-compared and Vitest is the SPA coverage
|
||||
And "owner" and "admin" are the full Home view; any other or absent session role is the regular-user view
|
||||
And every new English string has a French entry in messages.ts
|
||||
And GET /v1/settings (every verified user) and PATCH /v1/settings (owner only, behind RequireOwner) both answer with the JSON object {"version", "activity_retention_days", "activity_last_cleanup_at", "activity_last_cleanup_removed", "activity_next_cleanup_at"}, snake_case, nothing else
|
||||
And this supersedes the statement of features/settings_page.feature that the body is exactly {"version": "<value>"}; the version field and the verified-user access of that feature are unchanged
|
||||
And activity_next_cleanup_at is an RFC 3339 timestamp with the server's offset, or null when activity_retention_days is 0 (Forever)
|
||||
And otherwise it is the next 03:00 (audit.CleanupHour) in the server's time zone strictly after the server's current time
|
||||
And the value is computed in the domain (audit.Settings) from the clock the use cases are built with, never in the handler and never in the SPA
|
||||
And the cleanup it announces is the existing daily job, which never touches the last 24 hours (audit.KeepRecentHours)
|
||||
And GetActivityRetentionUseCase and UpdateActivityRetentionUseCase both return the value, so a PATCH response carries the next cleanup of the new period (null after switching to Forever, a timestamp after leaving Forever)
|
||||
And the existing validation, 400 with the text of apperr.BodyErrorMessage for any value outside 0, 30, 90, 365 or a malformed body, and 403 for a non-owner are unchanged
|
||||
And the domain package stays free of GORM, HTTP and the clock source (the clock is passed in)
|
||||
And on /home a card "Recent activity" (RecentActivityCard) sits in a 420 px column to the right of the existing cards from the lg breakpoint up, with a 16 px gap, in a grid (minmax(0,1fr) 420px, items aligned to the start) that spans the full content width, with no max-width, as the board frame draws it
|
||||
And the greeting header and the update banner stay above the grid at full width
|
||||
And below lg the card is stacked above the stat tiles and the recent article list, below the banner, with a 16 px gap
|
||||
And the card is --ui-bg with a 1 px --ui-border and a header row (12px 16px padding, 1 px bottom border) holding the title (14 px semibold, --ui-text-highlighted) and, at the right, the link "View all" with an arrow-right icon (14 px; 44 px touch height below lg) to /activity
|
||||
And the title is "Recent activity", and "Your recent activity" in the regular-user view
|
||||
And on mount the card sends exactly one GET /v1/activity?size=5 with no other parameter and never an actor parameter; it never calls /summary or /export; the server returns own events only for a regular user
|
||||
And no range is sent, so the server's default of 7 days applies and a site quiet for more than a week reads "No activity yet"
|
||||
And the card loads independently of GET /v1/article/: neither waits for or blocks the other, and an error or empty card leaves the stat tiles, the recent article list, the articles' own empty and error states and "Connect your website" as they are
|
||||
And each row (up to 5, in API order) has a 24 px avatar, the sentence clamped to two lines with the target name in semibold (composed by ActivitySentence exactly as on /activity, never stored text), then a muted 12 px line: "{actor name} · {time}" in the full view and only "{time}" in the regular-user view
|
||||
And the avatar is the same as the Actor cell of /activity (initials for a user, key-round for an API key, cog for the system, user-round-x for an anonymous actor)
|
||||
And the time of an event of today is "{n} min ago" or "{n} h ago" (lib/activityTime), an event of yesterday reads "Yesterday, 14:20" and an older one "3 Oct 2026, 14:20" (French "Hier, 14:20", "3 oct. 2026, 14:20"); the full local time with seconds is its title attribute
|
||||
And a failed event shows a "Failed" badge at the right of its row; a successful event with security true shows a small shield icon (14 px, --ui-warning) and no badge; a failed security event shows the badge only
|
||||
And rows are not interactive; only "View all" navigates
|
||||
And while the request is in flight for longer than 150 ms the card body shows five skeleton rows (24 px circle, two bars) and aria-busy="true"; nothing is drawn in the body for the first 150 ms
|
||||
And on a 500, any other non-401 status or a network failure the body is an inline role="alert" error with icon circle-x, title "Couldn't load recent activity", no description, and a "Try again" button that sends the same GET /v1/activity?size=5 again; a 401 is handled globally by useApi
|
||||
And when total is 0 the body is the empty state: icon history, "No activity yet" and "Actions on your site will appear here." (regular user: "Your actions will appear here.")
|
||||
And on /settings, for the owner only, a third card (RetentionCard) follows UpdatesCard and MediaCleanupCard and has the same width as its sibling cards; for an admin, a regular user or an absent role it is not rendered at all
|
||||
And the card reads GET /v1/settings, the single call SettingsView already makes, whose typed response is widened with the four activity_* fields; the page still sends exactly one GET /v1/settings per navigation
|
||||
And the header is "Retention" (14 px semibold) over "How long activity is kept." (12 px muted) with a status area at the right, aria-live="polite": "Saving…" (loader-circle) while a PATCH is in flight, "Saved" (check, --ui-success) for 2 s after it succeeds
|
||||
And the body has the label "Keep activity for" over a USelectMenu (at most 260 px wide) with the items "30 days", "90 days", "365 days", "Forever" (values 30, 90, 365, 0) and a 12 px muted helper: "Older events are removed automatically." or, for Forever, "Nothing is removed automatically. The log grows over time."
|
||||
And an info UAlert with icon lock is always shown: "Activity logs are append-only" / "Events can't be edited or deleted from the app. Only the retention period removes them."
|
||||
And after a divider two read-only lines (120 px label then value; stacked on mobile) are shown in the user's local time: "Last cleanup" reads "6 Oct 2026, 03:00 · 142 events removed" ("1 event removed" for 1), or "Never" when activity_last_cleanup_at is null; "Next cleanup" reads "7 Oct 2026, 03:00" from activity_next_cleanup_at, or "Not scheduled" when it is null
|
||||
And for Forever the last-cleanup line still shows the real last cleanup when one exists, and "Never" otherwise
|
||||
And the periods are ordered 30 < 90 < 365 < Forever, and "shorter" means a lower rank than the saved period
|
||||
And choosing a longer period, including Forever, sends PATCH /v1/settings with {"activity_retention_days": <value>} at once
|
||||
And choosing a shorter period sends nothing and shows a warning UAlert with icon triangle-alert, title "Events older than {n} days will be removed", text "This happens at the next cleanup on {date}. It can't be undone." (the date is the saved settings' activity_next_cleanup_at, formatted like the line above) and the buttons "Apply {n} days" and "Cancel"
|
||||
And a PATCH 200 returns the full settings, which refresh the select and the last and next lines
|
||||
And any non-401 PATCH failure (400, 403, 500, network) shows an error UAlert with icon circle-x, title "Couldn't save the retention period", text "Your previous setting is still in place." and a "Try again" button, reverts the select to the saved value and leaves the lines unchanged; a 403 additionally refreshes the session, after which the card is gone
|
||||
And "Try again" re-sends the failed value without asking for the shortening confirmation again
|
||||
And while GET /v1/settings is loading for longer than 150 ms the card shows skeleton lines with aria-busy="true"
|
||||
And when GET /v1/settings fails the card shows the Media cleanup card's alert, "Couldn't reach RenewCMS. Check your connection and try again." with "Try again", which re-runs the shared load
|
||||
And while the browser is offline the select, "Apply" and "Cancel" are disabled; the page's offline banner copy is unchanged
|
||||
|
||||
Scenario Outline: Settings API, GET returns the next cleanup of the saved period
|
||||
Given the saved retention is <days> days and the server time is <now> in a server zone of +02:00
|
||||
When a verified user sends GET /v1/settings
|
||||
Then the status is 200
|
||||
And the body has "activity_next_cleanup_at" <next> and the other four fields
|
||||
|
||||
Examples:
|
||||
| days | now | next |
|
||||
| 90 | 2026-10-06T10:00:00+02:00 | "2026-10-07T03:00:00+02:00" |
|
||||
| 90 | 2026-10-06T02:59:59+02:00 | "2026-10-06T03:00:00+02:00" |
|
||||
| 90 | 2026-10-06T03:00:00+02:00 | "2026-10-07T03:00:00+02:00" |
|
||||
| 30 | 2026-12-31T23:30:00+02:00 | "2027-01-01T03:00:00+02:00" |
|
||||
| 365 | 2026-10-06T10:00:00+02:00 | "2026-10-07T03:00:00+02:00" |
|
||||
| 0 | 2026-10-06T10:00:00+02:00 | null |
|
||||
|
||||
Scenario: Settings API, GET keeps the existing fields and adds exactly one
|
||||
Given the saved retention is 90 days, the last cleanup is 2026-10-06T01:00:00Z with 142 events removed
|
||||
When a verified user sends GET /v1/settings
|
||||
Then the body is a JSON object with exactly the keys version, activity_retention_days, activity_last_cleanup_at, activity_last_cleanup_removed and activity_next_cleanup_at
|
||||
|
||||
Scenario: Settings API, GET is open to every verified user
|
||||
Given a verified admin or editor sends GET /v1/settings
|
||||
Then the status is 200 and the body includes activity_next_cleanup_at
|
||||
|
||||
Scenario Outline: Settings API, PATCH returns the next cleanup of the new period
|
||||
Given the saved retention is <from> days and the user is the owner
|
||||
When the owner sends PATCH /v1/settings with {"activity_retention_days": <to>}
|
||||
Then the status is 200 and activity_retention_days is <to>
|
||||
And activity_next_cleanup_at is <next>
|
||||
|
||||
Examples:
|
||||
| from | to | next |
|
||||
| 90 | 30 | the next 03:00 server time |
|
||||
| 90 | 0 | null |
|
||||
| 0 | 365 | the next 03:00 server time |
|
||||
| 90 | 90 | the next 03:00 server time |
|
||||
|
||||
Scenario: Settings API, PATCH with the saved value changes nothing
|
||||
Given the saved retention is 90 days
|
||||
When the owner sends PATCH /v1/settings with {"activity_retention_days": 90}
|
||||
Then the status is 200, nothing is saved and no audit event is recorded
|
||||
|
||||
Scenario: Settings API, PATCH by a non-owner is refused and changes nothing
|
||||
Given a verified admin
|
||||
When the admin sends PATCH /v1/settings with {"activity_retention_days": 30}
|
||||
Then the status is 403 with the text "Only the owner can do this." and the saved retention is unchanged
|
||||
|
||||
Scenario Outline: Settings API, PATCH rejects invalid input
|
||||
When the owner sends PATCH /v1/settings with <body>
|
||||
Then the status is 400 with the text of apperr.BodyErrorMessage and nothing is saved
|
||||
|
||||
Examples:
|
||||
| body |
|
||||
| {"activity_retention_days": 7} |
|
||||
| {"activity_retention_days": "30"} |
|
||||
| {} |
|
||||
| not json |
|
||||
|
||||
Scenario: Settings use cases compute the next cleanup from their clock
|
||||
Given GetActivityRetentionUseCase and UpdateActivityRetentionUseCase are built with a fake settings repository and a fixed clock
|
||||
Then GetActivityRetention returns Settings whose NextCleanupAt is the next 03:00 after the clock, or nil for Forever
|
||||
And UpdateActivityRetention returns the new period with its own NextCleanupAt
|
||||
And UpdateActivityRetention(7) returns an error satisfying errors.Is(err, audit.ErrInvalidRetention) and saves nothing
|
||||
|
||||
Scenario Outline: Home, owner or admin, the last five events with the actor name
|
||||
Given the session role is "<role>" and GET /v1/activity?size=5 resolves 200 with 5 events, the first being Marta Alves publishing the article "Spring release" 2 minutes ago
|
||||
When the user navigates to /home
|
||||
Then the card title is "Recent activity" and "View all" links to /activity
|
||||
And row 1 reads "Published the article Spring release" with "Spring release" in semibold, over "Marta Alves · 2 min ago", with a 24 px avatar "MA"
|
||||
And exactly 5 rows are shown and GET /v1/activity was requested once with size=5 and no other parameter
|
||||
|
||||
Examples:
|
||||
| role |
|
||||
| owner |
|
||||
| admin |
|
||||
|
||||
Scenario Outline: Home, regular user, own events only and no actor name
|
||||
Given the session role is "<role>" and GET /v1/activity?size=5 resolves 200 with 3 own events
|
||||
When the user navigates to /home
|
||||
Then the card title is "Your recent activity"
|
||||
And each row's meta line is only the time, for example "2 min ago"
|
||||
And no actor parameter was sent
|
||||
|
||||
Examples:
|
||||
| role |
|
||||
| editor |
|
||||
| absent |
|
||||
|
||||
Scenario Outline: Home rows, failure and security markers
|
||||
Given an event with outcome "<outcome>" and security <security>
|
||||
Then its row shows <marker>
|
||||
|
||||
Examples:
|
||||
| outcome | security | marker |
|
||||
| success | false | no badge and no shield |
|
||||
| failed | false | a "Failed" badge |
|
||||
| success | true | a shield icon and no badge |
|
||||
| failed | true | a "Failed" badge and no shield |
|
||||
|
||||
Scenario Outline: Home rows, the time label follows the day
|
||||
Given the current time is 2026-10-06 15:00 local and an event created <when>
|
||||
Then the meta time reads "<label>"
|
||||
|
||||
Examples:
|
||||
| when | label |
|
||||
| 2026-10-06 14:58 | 2 min ago |
|
||||
| 2026-10-06 12:00 | 3 h ago |
|
||||
| 2026-10-05 14:20 | Yesterday, 14:20 |
|
||||
| 2026-10-03 14:20 | 3 Oct 2026, 14:20 |
|
||||
|
||||
Scenario: Home rows, a long sentence is clamped to two lines
|
||||
Given an event on an article whose title has 150 characters
|
||||
Then the sentence is clamped to two lines with the title in semibold and the meta line stays on one line
|
||||
|
||||
Scenario: Home rows, an unknown action and a failed login
|
||||
Given an event with action "article.archive" and an "auth.login_failed" event for "alex@example.com"
|
||||
Then the first row shows "article.archive", the second "Failed login attempt for alex@example.com" with a "Failed" badge and the user-round-x avatar, and nothing throws
|
||||
|
||||
Scenario: Home rows are not interactive
|
||||
Then a click on a row navigates nowhere and opens nothing
|
||||
|
||||
Scenario: Home card, skeleton rows only after 150 ms
|
||||
Given GET /v1/activity?size=5 has not resolved
|
||||
When the user navigates to /home
|
||||
Then nothing is drawn in the card body for the first 150 ms
|
||||
And after 150 ms five skeleton rows are shown and the card is aria-busy="true"
|
||||
When the request resolves 200 with 5 events
|
||||
Then the skeletons are replaced by 5 rows
|
||||
And a response in under 150 ms never shows a skeleton
|
||||
|
||||
Scenario Outline: Home card, error shows a retryable alert that leaves the page alone
|
||||
Given GET /v1/activity?size=5 <failure> and GET /v1/article/ resolves 200 with 6 articles
|
||||
When the user navigates to /home
|
||||
Then the card shows an alert "Couldn't load recent activity" with a "Try again" button
|
||||
And the stat tiles "6" "articles", "4" "online", "2" "drafts", the recent article list and "Connect your website" are shown as in home.feature's Default state
|
||||
When the user activates "Try again"
|
||||
Then GET /v1/activity?size=5 is sent again and, on 200 with events, the rows replace the alert
|
||||
|
||||
Examples:
|
||||
| failure |
|
||||
| resolves with a 500 response |
|
||||
| resolves with a 403 response |
|
||||
| never reaches the server |
|
||||
|
||||
Scenario Outline: Home card, empty when there are no events yet
|
||||
Given GET /v1/activity?size=5 resolves 200 with items [] and total 0 and the role is "<role>"
|
||||
When the user navigates to /home
|
||||
Then the card shows the history icon, "No activity yet" and "<text>"
|
||||
And "View all" is still shown
|
||||
|
||||
Examples:
|
||||
| role | text |
|
||||
| owner | Actions on your site will appear here. |
|
||||
| editor | Your actions will appear here. |
|
||||
|
||||
Scenario: Home card and article list are independent
|
||||
Given GET /v1/article/ fails with a 500 and GET /v1/activity?size=5 resolves 200 with 5 events
|
||||
Then the articles alert "Couldn't load your articles" and the 5 activity rows are both shown
|
||||
And with GET /v1/article/ resolving [] and GET /v1/activity?size=5 failing, the articles' Empty state and the card's error are both shown
|
||||
|
||||
Scenario: Home layout is two columns from lg and stacked below
|
||||
Given the viewport is lg or wider
|
||||
Then the header and the update banner span the full content width and the grid below them spans the same full width with the article cards on the left and the activity card 420 px wide in the far-right column, 16 px apart, its top aligned with the top of the article cards
|
||||
Given the viewport is below lg
|
||||
Then the card sits above the stat tiles, below the banner, at full width
|
||||
|
||||
Scenario: Home card, "View all" opens the activity page
|
||||
When the user activates "View all"
|
||||
Then the router navigates to /activity
|
||||
|
||||
Scenario: Settings retention is hidden for everyone but the owner
|
||||
Given GET /v1/settings resolves 200 with activity_retention_days 90
|
||||
When an "admin" (or an "editor", or an absent role) navigates to /settings
|
||||
Then no "Retention" card and no "Keep activity for" select is in the page
|
||||
And the Version, Updates and Media cleanup cards are unchanged
|
||||
And GET /v1/settings was requested once
|
||||
|
||||
Scenario: Settings retention, Default with 90 days
|
||||
Given the role is "owner" and GET /v1/settings resolves 200 with activity_retention_days 90, activity_last_cleanup_at 6 Oct 2026 03:00 local, activity_last_cleanup_removed 142 and activity_next_cleanup_at 7 Oct 2026 03:00 local
|
||||
When the owner navigates to /settings
|
||||
Then the third card reads "Retention" and "How long activity is kept."
|
||||
And the select shows "90 days" with the helper "Older events are removed automatically."
|
||||
And the lock alert "Activity logs are append-only" is shown
|
||||
And the lines read "Last cleanup" "6 Oct 2026, 03:00 · 142 events removed" and "Next cleanup" "7 Oct 2026, 03:00"
|
||||
And no "Saved" is shown, no PATCH was sent and GET /v1/settings was requested once
|
||||
|
||||
Scenario: Settings retention, before the first cleanup
|
||||
Given activity_last_cleanup_at is null and activity_next_cleanup_at is set
|
||||
Then "Last cleanup" reads "Never" and "Next cleanup" shows the date
|
||||
|
||||
Scenario: Settings retention, skeleton lines after 150 ms
|
||||
Given GET /v1/settings has not resolved and the role is "owner"
|
||||
When the owner navigates to /settings
|
||||
Then nothing is drawn in the card body for the first 150 ms, then skeleton lines with aria-busy="true"
|
||||
|
||||
Scenario: Settings retention, the settings request fails
|
||||
Given GET /v1/settings fails with a 500
|
||||
Then the card shows "Couldn't reach RenewCMS. Check your connection and try again." with a "Try again" button that re-runs the shared GET /v1/settings
|
||||
|
||||
Scenario Outline: Settings retention, a longer period saves at once
|
||||
Given the saved period is <from> and the owner chooses "<to>"
|
||||
When PATCH /v1/settings resolves 200 with activity_retention_days <value> and the matching activity_next_cleanup_at
|
||||
Then PATCH was sent once with {"activity_retention_days": <value>} and no warning alert is shown
|
||||
And the status reads "Saving…" while in flight and "Saved" with a check afterwards, fading after 2 s
|
||||
And the select shows "<to>" and the "Next cleanup" line shows the returned value
|
||||
|
||||
Examples:
|
||||
| from | to | value |
|
||||
| 90 days | 365 days | 365 |
|
||||
| 30 days | 90 days | 90 |
|
||||
| 90 days | Forever | 0 |
|
||||
|
||||
Scenario: Settings retention, Forever changes the helper and the lines
|
||||
Given the saved period is 90 days and the owner chooses "Forever" and PATCH resolves 200 with activity_retention_days 0, activity_next_cleanup_at null and a last cleanup of 6 Oct 2026 03:00 with 142 removed
|
||||
Then the helper reads "Nothing is removed automatically. The log grows over time."
|
||||
And "Next cleanup" reads "Not scheduled" and "Last cleanup" still reads "6 Oct 2026, 03:00 · 142 events removed"
|
||||
|
||||
Scenario: Settings retention, Forever and never cleaned
|
||||
Given GET /v1/settings resolves 200 with activity_retention_days 0, activity_last_cleanup_at null and activity_next_cleanup_at null
|
||||
Then "Last cleanup" reads "Never" and "Next cleanup" reads "Not scheduled"
|
||||
|
||||
Scenario: Settings retention, shortening asks first
|
||||
Given the saved period is 90 days and activity_next_cleanup_at is 7 Oct 2026 03:00 local
|
||||
When the owner chooses "30 days"
|
||||
Then no PATCH is sent and the select shows "30 days"
|
||||
And a warning alert shows "Events older than 30 days will be removed" and "This happens at the next cleanup on 7 Oct 2026, 03:00. It can't be undone." with "Apply 30 days" and "Cancel"
|
||||
When the owner activates "Apply 30 days" and PATCH resolves 200 with activity_retention_days 30
|
||||
Then PATCH was sent once with {"activity_retention_days": 30}, the alert is gone and "Saved" shows
|
||||
|
||||
Scenario: Settings retention, shortening cancelled
|
||||
Given the warning for 30 days is shown on a saved 90
|
||||
When the owner activates "Cancel"
|
||||
Then the alert is gone, the select reads "90 days" again and no PATCH was ever sent
|
||||
|
||||
Scenario Outline: Settings retention, shortening is relative to the saved period
|
||||
Given the saved period is <saved>
|
||||
When the owner chooses "<chosen>"
|
||||
Then <outcome>
|
||||
|
||||
Examples:
|
||||
| saved | chosen | outcome |
|
||||
| Forever | 365 days | the warning "Events older than 365 days will be removed" shows |
|
||||
| 365 days | 90 days | the warning "Events older than 90 days will be removed" shows |
|
||||
| 90 days | 90 days | nothing happens and no request is sent |
|
||||
| 30 days | Forever | PATCH is sent at once |
|
||||
|
||||
Scenario: Settings retention, choosing a longer period while the warning is open drops it
|
||||
Given the warning for 30 days is shown on a saved 90
|
||||
When the owner chooses "365 days"
|
||||
Then the warning disappears and PATCH is sent once with 365
|
||||
When the owner then chooses "30 days" and again "365 days" before the first request settles or after it
|
||||
Then no stale warning is ever left on screen
|
||||
|
||||
Scenario Outline: Settings retention, save error reverts the control and Try again retries
|
||||
Given the saved period is 90 days and the owner chooses "365 days"
|
||||
When PATCH /v1/settings <failure>
|
||||
Then an error alert shows "Couldn't save the retention period" and "Your previous setting is still in place." with "Try again"
|
||||
And the select shows "90 days" again, the lines are unchanged and no "Saved" appears
|
||||
When the owner activates "Try again" and PATCH resolves 200 with 365
|
||||
Then PATCH was sent a second time with 365 and "Saved" replaces the alert
|
||||
|
||||
Examples:
|
||||
| failure |
|
||||
| resolves with a 500 response |
|
||||
| resolves with a 400 response |
|
||||
| resolves with a 403 response |
|
||||
| never reaches the server |
|
||||
|
||||
Scenario: Settings retention, Try again after Apply does not ask again
|
||||
Given the warning for 30 days is shown and the owner activates "Apply 30 days"
|
||||
When PATCH resolves 500
|
||||
Then the warning is gone, the error alert is shown and the select reads the saved period
|
||||
When the owner activates "Try again" and PATCH resolves 200 with 30
|
||||
Then PATCH was sent a second time with 30 and no warning reappears
|
||||
|
||||
Scenario: Settings retention, an owner demoted while the page is open
|
||||
Given PATCH /v1/settings resolves 403 and GET /v1/auth/me then resolves with role "admin"
|
||||
Then the error alert is shown and, after the session refresh, the Retention card is no longer rendered
|
||||
|
||||
Scenario: Settings retention, offline disables the controls
|
||||
Given the owner is on /settings and the browser goes offline
|
||||
Then the select, and "Apply" and "Cancel" when shown, are disabled and nothing is sent
|
||||
And the page's offline banner keeps its existing copy
|
||||
|
||||
Scenario: Settings retention, a 401 is global
|
||||
Given GET or PATCH /v1/settings resolves 401
|
||||
Then useApi redirects to /login?redirect=/settings&reason=expired and no card alert is shown
|
||||
|
||||
Scenario: The French interface
|
||||
Given the language is French
|
||||
Then every string of both cards is French and the Home rows use the French sentence templates of activity_screen.feature
|
||||
|
||||
Scenario: Existing behaviour is unchanged
|
||||
Then every scenario of home.feature, settings_page.feature (except its exact-body statement, superseded above) and the Media cleanup scenarios still holds with GET /v1/activity?size=5 mocked and the settings mock widened with the four activity_* fields
|
||||
@@ -0,0 +1,74 @@
|
||||
# QA: Recent activity card on Home and Retention card on Settings
|
||||
|
||||
## Implementation note for coder (informational, not a QA assertion)
|
||||
|
||||
- Backend: add NextCleanupAt (nullable time) to the audit settings entity with a domain method computing the next 03:00 (audit.CleanupHour) strictly after a given time, in that time's location; nil when RetentionDays is audit.ForeverRetentionDays. GetActivityRetentionUseCase and UpdateActivityRetentionUseCase get an injected `now func() time.Time` (wired in api/dependecyInjection.go) and populate it. settingsResponse in api/handlers/settings gains `activity_next_cleanup_at` (`*time.Time`, RFC 3339, null when nil). No logic in the handler, none in the SPA.
|
||||
- Home: HomeView mounts a RecentActivityCard calling GET /v1/activity?size=5 once, reusing ActivityActor, ActivitySentence and lib/activityTime.
|
||||
- Settings: widen WorkspaceSettings with the four activity_* fields, add a PATCH helper to useSettings, render RetentionCard after MediaCleanupCard for session.role === 'owner' only. No second GET /v1/settings.
|
||||
- features/settings_page.feature and its qa.md say the body is exactly {"version"}; this feature supersedes that statement, so update those tests to the new five-key body.
|
||||
|
||||
## Automated, Go (httptest, in-memory fakes, `task test`)
|
||||
|
||||
Build `settings.NewHandler(GetSettingsUseCase, GetActivityRetentionUseCase, UpdateActivityRetentionUseCase)` over a fake audit.SettingsRepository (records saved retention and last cleanup) and a fake audit.Recorder, with a fixed fake clock. Mount on a fresh chi.NewRouter mirroring NewSettingsRouter: GET "/" behind jwtauth.Verifier + Authenticator (test jwtauth.New("HS256", ...)); PATCH "/" additionally behind the owner check using a fake user repository holding an owner, an admin and an editor. Call validator.InitValidator() first. No live server, no SQLite file. Fake type names must be unique across test files.
|
||||
|
||||
- G1. GET table (clock in a +02:00 zone): retention 90 at 10:00 gives next = next day 03:00+02:00; at 02:59:59 gives same day 03:00; at exactly 03:00:00 gives next day 03:00; 30 at 2026-12-31T23:30 gives 2027-01-01T03:00; 365 at 10:00 gives next day; retention 0 gives JSON null (assert the key is present with value null, not absent).
|
||||
- G2. GET body has exactly the five keys (decode into map[string]any, assert the key set); last cleanup 2026-10-06T01:00:00Z and 142 preserved.
|
||||
- G3. GET is 200 for owner, admin and editor tokens; 401 without a cookie.
|
||||
- G4. PATCH owner 90->30: 200, activity_retention_days 30, next cleanup non-null at the next 03:00; the fake repository holds 30; one audit event with params from 90, to 30.
|
||||
- G5. PATCH owner 90->0: 200, next null. PATCH 0->365: next non-null.
|
||||
- G6. PATCH owner 90->90: 200, next non-null, no repository save, no audit event.
|
||||
- G7. Ownership: PATCH as admin and as editor: 403 with "Only the owner can do this."; repository unchanged; no audit event. PATCH without a cookie: 401.
|
||||
- G8. Invalid: {"activity_retention_days": 7}, "30" (string), {}, malformed JSON: each 400 with the apperr.BodyErrorMessage text (plain text via http.Error), repository unchanged.
|
||||
- G9. Use-case level (no HTTP): GetActivityRetention returns NextCleanupAt per G1 rows; UpdateActivityRetention(7) returns an error satisfying errors.Is(err, audit.ErrInvalidRetention) and saves nothing; a repository Get error propagates and the handler answers 500 "Internal server error".
|
||||
- G10. Domain: NextCleanupAt table including a DST-change day in a named zone (for example Europe/Paris, 2026-10-25), asserting it is still 03:00 local on the right date.
|
||||
|
||||
## Automated, SPA (Vitest + Vue Test Utils, `task test:web`)
|
||||
|
||||
Suggested files: web/src/views/__tests__/HomeActivityCard.spec.ts and SettingsRetention.spec.ts. Mock useApi per case; no real backend. Freeze the clock with vi.setSystemTime only where a time label is asserted (these specs are not visual).
|
||||
|
||||
Home (mount HomeView with a resolved session; GET /v1/article/ mocked with the 6-article fixture):
|
||||
|
||||
1. Owner and admin: 5 events, title "Recent activity", "View all" links to /activity; row 1 text with semibold target, "Marta Alves · 2 min ago", avatar "MA"; the activity mock was called once with exactly size=5 and no other parameter; /summary and /export never called.
|
||||
2. Regular user (editor, absent role): "Your recent activity"; meta lines are time only; no actor parameter.
|
||||
3. Marker table over outcome x security (no badge and no shield, Failed badge, shield only, badge only).
|
||||
4. Time label table (2 min ago, 3 h ago, "Yesterday, 14:20", "3 Oct 2026, 14:20") with fixed time; the title attribute holds the full local time.
|
||||
5. Clamp class on a 150-character title; unknown action "article.archive" renders the code; auth.login_failed sentence with Failed badge and user-round-x avatar.
|
||||
6. Rows are inert: clicking one changes no route and opens nothing.
|
||||
7. Skeleton only after 150 ms (fake timers): none before, 5 skeleton rows and aria-busy after, replaced on resolve; a fast response never shows one.
|
||||
8. Error table (500, 403, network): role="alert", title "Couldn't load recent activity", no description, "Try again"; stat tiles, article list and "Connect your website" still rendered; retry sends a second GET and recovers.
|
||||
9. Empty: both copies by role; "View all" still present.
|
||||
10. Independence in both directions (articles 500 + activity 200; articles [] + activity 500).
|
||||
11. "View all" navigates to /activity (test router).
|
||||
12. Layout classes: grid with the lg two-column template and the 420 px column, and no max-width class on the Home wrapper or the grid (the grid spans the full content width); DOM order puts the card first below lg (real pixels are manual).
|
||||
13. Existing home specs (HomeView, HomeUpdateBanner, HomeUpdateReady) keep passing with the activity mock added to the shared mount helper.
|
||||
|
||||
Settings (mount SettingsView, GET /v1/settings mocked with the five-key body):
|
||||
|
||||
14. Hidden for admin, editor and absent role; GET /v1/settings called once.
|
||||
15. Owner Default: header, "90 days", helper, lock alert, "Last cleanup 6 Oct 2026, 03:00 · 142 events removed", "Next cleanup 7 Oct 2026, 03:00" (from activity_next_cleanup_at; the SPA computes nothing); no PATCH; "1 event removed" singular.
|
||||
16. last_cleanup_at null: "Never". Forever with null/null: "Never" and "Not scheduled". Forever with a past cleanup: the real last line and "Not scheduled".
|
||||
17. Skeleton after 150 ms with aria-busy; load error alert and "Try again" re-runs the shared GET.
|
||||
18. Longer periods (90->365, 30->90, 90->Forever): one PATCH with {"activity_retention_days": N}, "Saving…" then "Saved", gone after 2 s (fake timers), no warning; the returned next cleanup replaces the line; a Forever response with next null shows "Not scheduled".
|
||||
19. Shortening: no PATCH, warning copy with the date taken from the loaded activity_next_cleanup_at; Apply sends once; Cancel reverts the select with no PATCH; table Forever->365, 365->90, 90->90 (no-op), 30->Forever (immediate); a longer choice while the warning is open drops it and leaves no stale warning after 30 days then 365 days again.
|
||||
20. Save error x (500, 400, 403, network): alert copy, select reverts, lines unchanged; Try again re-sends the same value without re-confirming; success shows "Saved"; error after Apply also clears the warning; a 403 triggers a session refresh and the card disappears when the role is no longer owner.
|
||||
21. Offline (mock useOnline false): select, Apply and Cancel disabled; nothing sent; page offline banner text unchanged.
|
||||
22. 401 handled globally: no local alert.
|
||||
23. French key parity: every new string has a messages.ts entry.
|
||||
24. Keyboard: the select opens with Enter or Space, arrows and Enter choose, Escape closes; Apply then Cancel reachable in order after the select.
|
||||
25. Existing SettingsView, SettingsMediaCleanup and Settings update specs keep passing with the widened mock body.
|
||||
|
||||
## Visual (task visual:check)
|
||||
|
||||
- Neither card can be pixel-compared (dashed placeholders); no new visual state is added. The card sub-states are not `###` headings in home.md or settings.md, so the tool should not iterate them; confirm on the first run.
|
||||
- Existing Home states and owner-session Settings states must still pass at 10 % with the card present. Every Home setup in web/visual/states.ts needs GET /v1/activity?size=5 mocked (an unmocked /v1 call fails the gate), and every owner Settings setup needs the widened GET /v1/settings body. Layout changes may exceed the gate: measure first, then ask the user for a per-state `exception` (needs approvedBy). Do not add any exception beforehand.
|
||||
- Time-dependent states stay `skip: 'clock-sensitive'`; no Vitest spec freezes the date for a visual claim.
|
||||
|
||||
## Manual verification
|
||||
|
||||
- [ ] Real GET /v1/settings and PATCH /v1/settings against `task dev` with a live SQLite file: five keys, next cleanup at the next 03:00 server time, null after switching to Forever, a timestamp after leaving Forever; as an admin PATCH is 403, as the owner a bad value is 400.
|
||||
- [ ] Browser in a time zone different from the server's: the "Next cleanup" and "Last cleanup" lines both show the user's local time of the same instants; the warning date matches the "Next cleanup" line.
|
||||
- [ ] Real daily cleanup (or a forced run) updates "Last cleanup" and moves "Next cleanup" a day forward.
|
||||
- [ ] Real GET /v1/activity?size=5 as owner, admin and editor (server-side own-event scoping), including a site with only events older than 7 days (reads "No activity yet").
|
||||
- [ ] Home at 1440 and 390 px, light and dark: 420 px column in the far-right of the full content width, 16 px gap, row clamping, "View all" 44 px touch target; the article column fills the remaining width and still reads well.
|
||||
- [ ] Keyboard and focus: Home tab order "New article", article rows, "View all"; Settings select, Apply, Cancel in order and focus back on the select after Cancel.
|
||||
- [ ] Hover, focus rings, the 2 s "Saved" fade and reduced motion.
|
||||
@@ -0,0 +1,693 @@
|
||||
Feature: Activity screen, details slide-over and My activity
|
||||
|
||||
As a verified RenewCMS user
|
||||
I want /activity to read as a plain-language timeline of who did what and when, with filters,
|
||||
a summary strip, CSV export and a details panel for each event
|
||||
So that an owner can audit the site without reading a log, and any other role sees its own history
|
||||
|
||||
Background:
|
||||
Given /activity is a verified route (meta.auth "verified", meta.title "Activity") rendered as
|
||||
ActivityView (views/ActivityView.vue, the "ActivityPage" of docs/design/components.md) inside
|
||||
DashboardShell, per docs/design/routes.md and docs/design/screens/activity.md
|
||||
And the activity API already exists (features/activity-log-backend.feature and
|
||||
features/activity-log-producers.feature) and this slice wires the SPA to it unchanged, with
|
||||
no Go change
|
||||
And the Home card and the Settings "Retention" card of the same board are not in this slice
|
||||
And where docs/design prose and the board frame ActivityFrame.dc.html differ, the frame is
|
||||
followed
|
||||
And the views read the session role from GET /v1/auth/me: "owner" and "admin" are the full
|
||||
view; any other or absent value is the regular-user view ("My activity"), which no real
|
||||
account can reach today and is covered by Vitest and one visual state only
|
||||
And a full view loads, in parallel, GET /v1/activity?range=7d&page=1&size=25 and GET
|
||||
/v1/activity/summary?range=7d; a regular-user view loads only GET /v1/activity with the same
|
||||
query and never calls /summary or /export, and never sends an actor parameter
|
||||
And the list query always sends range, page and size explicitly, and adds q, actor, category,
|
||||
outcome, security, from and to only when set (outcome "all" is not sent, from and to only
|
||||
with range=custom); the summary query sends only range, from and to; the export query sends
|
||||
the list query without page and size
|
||||
And filters and the page live in component state, not in the URL, and reset when the user
|
||||
leaves /activity
|
||||
And useApi's global handling covers 401 and the unverified 403
|
||||
(features/api_client_global_error_handling.feature); this screen maps every other failure
|
||||
And DashboardShell's navigation gains "Activity" (icon i-lucide-history, linking to /activity)
|
||||
between "Users" and "Integration", in the sidebar and in the mobile drawer, so it lists Home,
|
||||
Articles, Media, Users, Activity, Integration, Settings in that order, for every verified
|
||||
user; "Activity" has aria-current="page" on /activity; the specs that list the destinations
|
||||
are amended accordingly (named in the QA file)
|
||||
And the PageHeader breadcrumb and the mobile title read the page title: "Activity", or
|
||||
"My activity" for the regular-user view
|
||||
And the page padding is 24 px on desktop and 16 px on mobile with a 16 px gap between the
|
||||
header, the strip, the filter bar, the chips, the table and the pager
|
||||
And the header is the title "Activity" (regular user: "My activity") with the subtitle "Who
|
||||
did what, and when, across your site." (regular user: "Everything you've done on this
|
||||
site."), and, in the full view except Forbidden, an outline button with icon download and
|
||||
the label "Export CSV" at the right, aligned to the bottom of the title block
|
||||
And sentences are composed in the SPA from the event's action code, its outcome, its params
|
||||
and its target name through the existing i18n (English keys, French in messages.ts), never
|
||||
stored as text; the target name is user content, is inserted untranslated as a semibold
|
||||
segment, and is the last slot of every template, so a missing target name drops the slot
|
||||
with its leading space and colon
|
||||
And the success sentences are, in English and French (the actor sits in its own column, so
|
||||
sentences start with the verb; {0} is the target name):
|
||||
| action | English | French |
|
||||
| article.create | Created the article {0} | Article créé : {0} |
|
||||
| article.edit | Edited the article {0} | Article modifié : {0} |
|
||||
| article.publish | Published the article {0} | Article publié : {0} |
|
||||
| article.unpublish | Unpublished the article {0} | Article dépublié : {0} |
|
||||
| article.delete | Deleted the article {0} | Article supprimé : {0} |
|
||||
| media.upload | Uploaded the image {0} | Image téléversée : {0} |
|
||||
| media.rename | Renamed the image {0} | Image renommée : {0} |
|
||||
| media.delete | Deleted the image {0} | Image supprimée : {0} |
|
||||
| media.merge | Merged duplicates into the image {0} | Doublons fusionnés dans l'image {0} |
|
||||
| media.purge | Purged duplicate images: {0} merged, {1} articles updated | Doublons purgés : {0} fusionnés, {1} articles mis à jour |
|
||||
| system.media_purge | Ran the scheduled duplicate purge: {0} merged, {1} articles updated | Purge automatique des doublons : {0} fusionnés, {1} articles mis à jour |
|
||||
| user.register | Registered the account {0} | Compte créé : {0} |
|
||||
| user.verify | Verified the account {0} | Compte vérifié : {0} |
|
||||
| user.update_profile | Updated the profile of {0} | Profil mis à jour : {0} |
|
||||
| invite.create | Invited {0} | Invitation envoyée à {0} |
|
||||
| invite.cancel | Cancelled the invite for {0} | Invitation annulée : {0} |
|
||||
| invite.renew | Renewed the invite for {0} | Invitation renouvelée : {0} |
|
||||
| invite.accept | Accepted the invite for {0} | Invitation acceptée : {0} |
|
||||
| api_key.create | Created API key {0} | Clé d'API créée : {0} |
|
||||
| api_key.revoke | Revoked API key {0} | Clé d'API révoquée : {0} |
|
||||
| auth.login | Logged in | Connexion |
|
||||
| auth.logout | Logged out | Déconnexion |
|
||||
| auth.password_reset_requested | Requested a password reset | Réinitialisation du mot de passe demandée |
|
||||
| auth.password_reset_completed | Reset the password | Mot de passe réinitialisé |
|
||||
| system.update_checked | Checked for updates | Recherche de mises à jour |
|
||||
| system.update_downloaded | Downloaded the update {0} | Mise à jour téléchargée : {0} |
|
||||
| system.restart_requested (kind restart) | Requested a restart | Redémarrage demandé |
|
||||
| system.restart_requested (kind update) | Restarted to apply the update {0} | Redémarrage pour appliquer la mise à jour {0} |
|
||||
| system.activity_retention_changed | Changed the activity retention from {0} to {1} | Durée de conservation de l'activité modifiée : {0} → {1} |
|
||||
| system.activity_cleanup | Removed {0} old events from the activity log | {0} anciens événements supprimés du journal d'activité |
|
||||
And the failed sentences, used when outcome is "failed", are:
|
||||
| action | English | French |
|
||||
| article.create | Failed to create the article {0} | Échec de la création de l'article {0} |
|
||||
| article.edit | Failed to edit the article {0} | Échec de la modification de l'article {0} |
|
||||
| article.publish | Failed to publish the article {0} | Échec de la publication de l'article {0} |
|
||||
| article.unpublish | Failed to unpublish the article {0} | Échec de la dépublication de l'article {0} |
|
||||
| article.delete | Failed to delete the article {0} | Échec de la suppression de l'article {0} |
|
||||
| media.upload | Failed to upload the image {0} | Échec du téléversement de l'image {0} |
|
||||
| media.rename | Failed to rename the image {0} | Échec du renommage de l'image {0} |
|
||||
| media.delete | Failed to delete the image {0} | Échec de la suppression de l'image {0} |
|
||||
| media.merge | Failed to merge duplicates into the image {0} | Échec de la fusion des doublons dans l'image {0} |
|
||||
| media.purge | Failed to purge duplicate images | Échec de la purge des doublons |
|
||||
| system.media_purge | Failed to run the scheduled duplicate purge | Échec de la purge automatique des doublons |
|
||||
| user.register | Failed to register the account {0} | Échec de la création du compte {0} |
|
||||
| user.verify | Failed to verify the account {0} | Échec de la vérification du compte {0} |
|
||||
| user.update_profile | Failed to update the profile of {0} | Échec de la mise à jour du profil de {0} |
|
||||
| invite.create | Failed to invite {0} | Échec de l'invitation de {0} |
|
||||
| invite.cancel | Failed to cancel the invite for {0} | Échec de l'annulation de l'invitation de {0} |
|
||||
| invite.renew | Failed to renew the invite for {0} | Échec du renouvellement de l'invitation de {0} |
|
||||
| invite.accept | Failed to accept the invite for {0} | Échec de l'acceptation de l'invitation de {0} |
|
||||
| api_key.create | Failed to create API key {0} | Échec de la création de la clé d'API {0} |
|
||||
| api_key.revoke | Failed to revoke API key {0} | Échec de la révocation de la clé d'API {0} |
|
||||
| auth.login_failed | Failed login attempt for {0} | Échec de connexion pour {0} |
|
||||
| system.update_checked | Failed to check for updates | Échec de la recherche de mises à jour |
|
||||
| system.update_downloaded | Failed to download the update {0} | Échec du téléchargement de la mise à jour {0} |
|
||||
| system.restart_requested | Failed to restart | Échec du redémarrage |
|
||||
And an action whose failed sentence is not listed (for example auth.login) renders its success
|
||||
sentence with the Failed badge; an action code that is not in the catalogue renders the raw
|
||||
code (for example "article.archive") and never throws
|
||||
And for auth.login_failed the slot {0} is the actor's name (the attempted username, since the
|
||||
event has no target), while the Actor column reads "Not signed in"
|
||||
And counts in params are shown with the locale's number format; "{0} old events" and "{1}
|
||||
articles updated" use the singular form for 1 ("1 old event", "1 article updated"); a
|
||||
media.purge or system.media_purge event with params.failed_groups appends ", {2} groups
|
||||
failed" ("1 group failed") in English and ", {2} groupes en échec" in French;
|
||||
system.activity_retention_changed shows days as "{n} days" and 0 as "Forever" ("{n} jours",
|
||||
"Toujours")
|
||||
And the category icons are: articles file-text, media image, users users, api_keys key-round,
|
||||
auth shield-check, system server-cog; the category labels are Articles, Media, Users and
|
||||
invites, API keys, Authentication, System
|
||||
And the Actor cell is the actor's name with: a UAvatar of initials (first letters of the first
|
||||
two words, upper case) for type "user"; a key-round icon avatar and an "API key" badge for
|
||||
"api_key"; a cog icon avatar and a "System" badge for "system"; a user-round-x icon avatar
|
||||
and the text "Not signed in" for "anonymous"
|
||||
And the Target cell is "—" (muted) when target is null; when target.exists it is the name as an
|
||||
underlined link with an arrow-up-right icon, plus the target type as a muted second line
|
||||
(Article, Image, User, Invite, API key, Release); when it does not exist the name is struck
|
||||
through, muted, with a "Deleted" badge (icon trash-2) and the type line; the link goes to
|
||||
/article/{id}/edit for article, /media for image, /users for user and invite,
|
||||
/integration/api-keys for api_key, and a target type with no page (release) is plain text
|
||||
And the Outcome cell is a badge with icon and word, never colour alone: "Succeeded" (check,
|
||||
bordered) or "Failed" (circle-x, --rc-error-soft background, --ui-error text)
|
||||
And a row whose event has security true shows a shield-alert icon in --ui-warning after the
|
||||
sentence and a 2 px inset --ui-warning edge on its left side; on mobile it also shows a
|
||||
"Security" badge (shield-alert, --rc-warning-soft, --ui-warning)
|
||||
And the table (md and up) has the columns When, Actor, Action, Target, Outcome in a grid
|
||||
96px 210px minmax(0,1fr) 230px 120px (regular user: When, Action, Target, Outcome in 96px
|
||||
minmax(0,1fr) 230px 120px), a 36 px header on --ui-bg-elevated with an arrow-down icon on
|
||||
When, day separator rows of 32 px on --ui-bg-muted with the label in 12 px semibold, and
|
||||
rows at least 52 px high with a top border; a selected row (details open) and a hovered row
|
||||
use --ui-bg-elevated
|
||||
And rows are grouped by the user's local calendar day under a separator labelled "Today",
|
||||
"Yesterday" or the date as "3 Oct 2026" (French "3 oct. 2026"), in the order the API
|
||||
returns them (newest first); the When cell shows the time as drawn, relative within today
|
||||
("2 min ago", "29 min ago", "1 h ago") and the local clock time "HH:mm" on earlier days,
|
||||
and carries the full local time "3 Oct 2026, 14:20:05" as its title attribute
|
||||
And below md the table is replaced by a stacked list of cards (ActivityCardList) with the same
|
||||
separators: a first line with the actor (avatar, name, badge; hidden for the regular user)
|
||||
and the time at the right, the category icon and the sentence (15 px, up to 3 lines), then a
|
||||
line of badges: the Outcome badge, "Security" when flagged, and "Target deleted" when the
|
||||
target is gone; each card is at least 44 px high as a touch target
|
||||
And rows and cards are focusable, open the details on click and on Enter, and a click on the
|
||||
target link navigates to the target without opening the details
|
||||
And the summary strip (ActivitySummaryStrip, full view only) has four cards that are real
|
||||
buttons: "Total actions", "Active people", "Failed actions", "Security events", each with
|
||||
the number (locale-formatted), a sub-line, and a seven-bar sparkline of seven div bars,
|
||||
4 px wide, 2 px apart, oldest at the left, bar height round(value / max(series, 1) * H)
|
||||
with a 2 px minimum and H of 20 px on desktop and 16 px on mobile, drawn from
|
||||
series.total, series.active, series.failed and series.security
|
||||
And on desktop the strip is a 4-column grid with a 12 px gap; below md it is, as drawn in
|
||||
ActivityFrame.dc.html, a single row of 156 px cards with an 8 px gap that is clipped at the
|
||||
page edge
|
||||
And the Total sub-line is "{pct}% vs previous 7 days" with icon trending-up when total is at
|
||||
least previous_total and trending-down otherwise, pct being the rounded absolute change
|
||||
against previous_total; for the ranges Today, 30 days and Custom the words read "vs previous
|
||||
day", "vs previous 30 days" and "vs previous period"; when previous_total is 0 the sub-line
|
||||
is "No earlier data" and when the two totals are equal it is "No change", both without an
|
||||
icon (French "Aucune donnée antérieure", "Aucun changement")
|
||||
And the Active people number is active_people.users plus active_people.api_keys with the
|
||||
sub-line "{users} people, {api_keys} API keys" ("1 person", "1 API key")
|
||||
And the Failed sub-line is "{pct}% of all actions" (one decimal, "0.5%") with icon circle-x
|
||||
and the --rc-error-soft background and --ui-error number
|
||||
And the Security sub-line is "{n} flagged" (French "{n} signalés", "1 signalé"), n being
|
||||
summary.security, with icon shield-alert and the --rc-warning-soft background and
|
||||
--ui-warning number
|
||||
And a Failed or Security count of 0 reads "0" with "Nothing failed" or "Nothing to flag" and
|
||||
icon circle-check in --ui-success, drops the emphasis fill and draws its bars in
|
||||
--ui-border-accented; with total 0 Total and Active people read "0" with "No data yet"
|
||||
And the strip follows only the date range; q, person, category, outcome and security never
|
||||
change it, and it is not requested again when they change
|
||||
And when the summary request fails with any status or never reaches the server, the strip is
|
||||
not rendered and nothing is shown in its place
|
||||
And the filter bar (ActivityFilterBar) has: a UInput (icon search, clearable, placeholder
|
||||
"Search activity", 240 px) that applies 300 ms after the last keystroke or at once on Enter;
|
||||
a Person USelectMenu (icon user-round, "All people", 168 px, full view only) with the groups
|
||||
users, API keys and "System"; a Category USelectMenu (icon layout-list, "All categories",
|
||||
176 px); an Outcome button group "All / Succeeded / Failed" (soft is the selected one,
|
||||
outline the others); and the date presets "Today / 7 days / 30 days / Custom" (Custom has
|
||||
icon calendar and opens a date-range popover) at the right; controls are 32 px high
|
||||
And the Person options come from GET /v1/user/ (accepted users, shown by display name or
|
||||
username) and GET /v1/api-key/, requested once, the first time the Person menu opens, so a
|
||||
page load makes no other call than the two above
|
||||
And choosing a Person sends actor=user:{id}, actor=key:{id} or actor=system; a Category sends
|
||||
category=articles|media|users|api_keys|auth|system; Outcome Succeeded sends outcome=success
|
||||
and Failed outcome=failed; Today, 7 days and 30 days send range=today|7d|30d; Custom sends
|
||||
range=custom&from=YYYY-MM-DD&to=YYYY-MM-DD once both dates are chosen; any filter change
|
||||
resets the page to 1 and sends a new list request, and the page size is kept
|
||||
And the active filter chips (UBadge subtle with a trailing ghost x button) read "Search:
|
||||
spring", "Person: Marta Alves", "Category: Authentication", "Outcome: Failed", "Security
|
||||
events", "Dates: 30 days" or "Dates: 1 – 30 Sep 2026" (same month "1 – 30 Sep 2026", across
|
||||
months "28 Sep – 3 Oct 2026"), followed by a link button "Clear all"; the default range 7
|
||||
days is never a chip; removing a chip resets that filter; "Clear all" resets every filter to
|
||||
the default (range 7d)
|
||||
And below md the bar is a search field and a "Filters" button (icon sliders-horizontal) with a
|
||||
count badge (the number of chips, hidden at 0); it opens a bottom sheet titled "Filters"
|
||||
with Person (full view only), Category, Outcome, Dates (two date fields when Custom is
|
||||
chosen) and a footer "Clear all" (disabled at 0 filters) and "Show {total} results" ("Show 1
|
||||
result"); the sheet applies each change at once, so the footer count is the current total,
|
||||
and "Show … results" closes the sheet
|
||||
And the pager (full width under the table) reads "Showing {from}–{to} of {total}" with
|
||||
locale-formatted numbers, a UPagination (previous, 1, 2, 3, …, last, next; previous
|
||||
disabled on page 1), and "Rows per page" with a select of 25, 50 and 100; changing the size
|
||||
resets the page to 1; below md it reads "Page {page} of {pages}" above "{from}–{to} of
|
||||
{total}", with previous and next buttons and the size select
|
||||
And while a list request is in flight for longer than 150 ms, 9 skeleton rows (desktop) or 5
|
||||
skeleton cards (below md) replace the rows and the pager is hidden; the strip shows 4
|
||||
skeleton cards while the summary request is in flight for longer than 150 ms; nothing is
|
||||
drawn in their place for the first 150 ms
|
||||
And the list is "empty" when total is 0: with no chip it is "No activity yet" (icon history,
|
||||
"Actions on articles, media, users and API keys will appear here.", regular user: "Your
|
||||
actions will appear here once you start working on the site."), with at least one chip it is
|
||||
"No activity matches these filters" (icon search-x, action "Clear filters")
|
||||
And the error, empty and forbidden panels are drawn as in ActivityFrame.dc.html: "Couldn't
|
||||
load activity" is an error alert with icon circle-x, the text and an outline "Try again"
|
||||
button, and "You can't view this activity" (no trailing period) uses icon lock and the
|
||||
action "Back to home"
|
||||
And "Export CSV" is disabled in "No activity yet" and while the list is in the Error state,
|
||||
and enabled otherwise
|
||||
And ActivityDetailsPanel is a USlideover: 480 px wide from the right on desktop, an 88 %-high
|
||||
bottom sheet with a 36×4 px handle below md; its header reads "Activity details" with a
|
||||
close button (and "{position} of {total}" below md); it opens from a row or card, closes
|
||||
with Escape, the close button or the scrim, and focus returns to the row that opened it
|
||||
And the panel body has, in this order: the full sentence (18 px semibold, target in semibold)
|
||||
with the Outcome badge, a "Security" badge when flagged and the category icon and label; an
|
||||
error alert "Why it failed" with failure_reason, only when outcome is "failed" and the
|
||||
reason is present; the grid When, Actor, Source, IP address, Browser; a Target section;
|
||||
a Changes section
|
||||
And When shows the local time as "6 Oct 2026, 10:42:18 (your time)" and under it, muted, the
|
||||
UTC time as "6 Oct 2026, 10:42:18 UTC"; Actor is the avatar, the name and the same badges
|
||||
as the table; Source is "Web app" for web, "API key" for api_key and "System task" for
|
||||
system; IP address (IBM Plex Mono 12 px) and Browser read "Not applicable" (muted) when ip
|
||||
or user_agent is null
|
||||
And the Target section shows the name (a link with arrow-up-right when it exists, otherwise
|
||||
struck through with a "Deleted" badge and the line "This item was deleted. Its name is kept
|
||||
for reference."), and the line "{type} · {id}"; it is absent when target is null
|
||||
And the Changes section (ActivityDiff) is absent when changes is empty; each change is a row
|
||||
headed by its field label: a "text" change shows Before and After boxes (side by side with
|
||||
an arrow-right on desktop, stacked with an arrow-down below md), a "body" change shows only
|
||||
the line "Body edited (+312 characters)" with icon file-pen-line ("-48", "+1 character")
|
||||
and never any text
|
||||
And the field labels are title "Title", online "Publish state", body "Body", filename "File
|
||||
name", display_name "Display name" (French "Titre", "État de publication", "Corps", "Nom du
|
||||
fichier", "Nom d'affichage"), an unknown field shows its raw code, and for online the
|
||||
values "false" and "true" read "Draft" and "Published" ("Brouillon", "Publié")
|
||||
And a Before or After value taller than two lines is clamped to two lines and a "Show more"
|
||||
link button appears under that row; it toggles to "Show less"; shorter values show no toggle
|
||||
And the panel footer has "Previous event" (icon chevron-up) and "Next event" (icon
|
||||
chevron-down) buttons that move through the current filtered list, and on desktop the
|
||||
muted text "Event {position} of {total}" with position = (page − 1) × size + index + 1;
|
||||
below md the two buttons are full width and there is no footer text
|
||||
And when Previous or Next crosses a page boundary the list page follows, and a failed request
|
||||
for the neighbour page keeps the panel on the current event with no message
|
||||
|
||||
Scenario: Navigation — "Activity" sits between Users and Integration
|
||||
Given the user is on /activity
|
||||
When DashboardShell mounts
|
||||
Then the sidebar and the mobile drawer list Home, Articles, Media, Users, Activity,
|
||||
Integration, Settings in that order
|
||||
And "Activity" has the history icon, links to /activity and has aria-current="page"
|
||||
And the breadcrumb reads "Activity" (mobile title "Activity")
|
||||
|
||||
Scenario: Default — an owner sees the strip, the filter bar and a day-grouped table
|
||||
Given GET /v1/activity resolves 200 with 25 items and total 1284, and GET /v1/activity/summary
|
||||
resolves 200 with total 1284, previous_total 1146, active_people {users 7, api_keys 2},
|
||||
failed 6, security 11 and seven-day series, and the session role is "owner"
|
||||
When the user navigates to /activity
|
||||
Then the header shows "Activity", "Who did what, and when, across your site." and an enabled
|
||||
"Export CSV"
|
||||
And the strip reads "1,284" "12% vs previous 7 days" with trending-up; "9" "7 people, 2 API
|
||||
keys"; "6" "0.5% of all actions"; "11" "11 flagged"
|
||||
And the filter bar shows "Search activity", "All people", "All categories", All selected in
|
||||
the Outcome group, and 7 days selected, with no chip
|
||||
And the table rows follow the API order under day separators
|
||||
And the pager reads "Showing 1–25 of 1,284" and "Rows per page" 25
|
||||
And GET /v1/activity was requested once with range=7d, page=1, size=25 and no other
|
||||
parameter, and GET /v1/activity/summary once with range=7d
|
||||
|
||||
Scenario Outline: Rows — the Actor cell and the sentence follow the event
|
||||
Given a row for an event with actor type "<type>" named "<name>", action "<action>", outcome
|
||||
"<outcome>" and security <security>
|
||||
Then the Actor cell shows <actor>
|
||||
And the Action cell shows "<sentence>" with the target name in semibold, and <marker>
|
||||
And the Outcome cell shows "<badge>"
|
||||
|
||||
Examples:
|
||||
| type | name | action | outcome | security | actor | sentence | marker | badge |
|
||||
| user | Marta Alves | article.publish | success | false | an avatar "MA" and "Marta Alves" | Published the article Spring release | no shield | Succeeded |
|
||||
| api_key | Website sync | media.upload | success | false | a key icon, "Website sync", "API key" | Uploaded the image north-wall.jpg | no shield | Succeeded |
|
||||
| system | System | system.media_purge | success | false | a cog icon, "System", "System" | Ran the scheduled duplicate purge: 3 merged, 5 articles updated | no shield | Succeeded |
|
||||
| anonymous | alex@example.com | auth.login_failed | failed | true | a user-round-x icon, "Not signed in" | Failed login attempt for alex@example.com | a shield-alert marker | Failed |
|
||||
| user | Inès Duarte | article.publish | failed | false | an avatar "ID" and "Inès Duarte" | Failed to publish the article Spring release | no shield | Failed |
|
||||
| user | Jonas Weber | api_key.revoke | success | true | an avatar "JW" and "Jonas Weber" | Revoked API key Mobile app | a shield-alert marker | Succeeded |
|
||||
|
||||
Scenario: Rows — a deleted target is struck through, a living one is a link
|
||||
Given an event of target {"type": "image", "id": "9", "name": "old-banner.png", "exists": false}
|
||||
and an event of target {"type": "article", "id": "8", "name": "Spring release", "exists": true}
|
||||
Then the first Target cell shows "old-banner.png" struck through with a "Deleted" badge and
|
||||
"Image"
|
||||
And the second Target cell shows "Spring release" as a link to /article/8/edit and "Article"
|
||||
And clicking that link navigates to /article/8/edit and does not open the details
|
||||
|
||||
Scenario: Rows — an event with no target shows a dash
|
||||
Given an event whose target is null
|
||||
Then its Target cell shows a muted "—"
|
||||
|
||||
Scenario: Rows — an unknown action code renders the code
|
||||
Given an event with action "article.archive"
|
||||
Then its Action cell shows "article.archive" and the row still opens the details
|
||||
|
||||
Scenario Outline: Sentences — English and French come from the same code and params
|
||||
Given an event with action "<action>", outcome "<outcome>", params <params> and target
|
||||
"<target>"
|
||||
When the language is <language>
|
||||
Then the sentence reads "<sentence>" with "<target>" untranslated
|
||||
|
||||
Examples:
|
||||
| action | outcome | params | target | language | sentence |
|
||||
| article.publish | success | {} | Spring release | English | Published the article Spring release |
|
||||
| article.publish | success | {} | Spring release | French | Article publié : Spring release |
|
||||
| article.publish | failed | {} | Spring release | French | Échec de la publication de l'article Spring release |
|
||||
| media.purge | success | {"merged": 3, "articles_updated": 5} | | English | Purged duplicate images: 3 merged, 5 articles updated |
|
||||
| media.purge | success | {"merged": 3, "articles_updated": 5, "failed_groups": 1} | | English | Purged duplicate images: 3 merged, 5 articles updated, 1 group failed |
|
||||
| system.activity_cleanup | success | {"removed": 1} | | English | Removed 1 old event from the activity log |
|
||||
| system.activity_retention_changed | success | {"from": 90, "to": 0} | | English | Changed the activity retention from 90 days to Forever |
|
||||
| system.restart_requested | success | {"kind": "update", "tag": "v1.4.0"} | v1.4.0 | English | Restarted to apply the update v1.4.0 |
|
||||
| media.upload | failed | {} | | English | Failed to upload the image |
|
||||
|
||||
Scenario: Loading — skeletons after 150 ms while both requests are in flight
|
||||
Given GET /v1/activity and GET /v1/activity/summary have not resolved
|
||||
When the user navigates to /activity
|
||||
Then the header, the filter bar and an enabled "Export CSV" are drawn at once
|
||||
And nothing is drawn in place of the strip and the table for the first 150 ms
|
||||
And after 150 ms 4 skeleton cards replace the strip and 9 skeleton rows (5 cards below md)
|
||||
replace the table, the pager is hidden and the table container is aria-busy="true"
|
||||
When both requests resolve 200 with the Default data
|
||||
Then the skeletons are replaced by the Default state's strip, rows and pager
|
||||
|
||||
Scenario: Zero-value summary strip — a quiet period shows a calm 0
|
||||
Given GET /v1/activity/summary resolves 200 with total 214, previous_total 223, active_people
|
||||
{users 4, api_keys 1}, failed 0, security 0 and series of zeros for failed and security
|
||||
And GET /v1/activity resolves 200 with 25 items
|
||||
When the user navigates to /activity
|
||||
Then the Total card reads "214" "4% vs previous 7 days" with trending-down
|
||||
And the Active people card reads "5" "4 people, 1 API key"
|
||||
And the Failed card reads "0" "Nothing failed" with circle-check and no error fill
|
||||
And the Security card reads "0" "Nothing to flag" with circle-check and no warning fill
|
||||
And their bars are 2 px high
|
||||
|
||||
Scenario Outline: The Total sub-line when there is nothing to compare
|
||||
Given GET /v1/activity/summary resolves 200 with total <total> and previous_total <previous>
|
||||
When the user navigates to /activity
|
||||
Then the Total card reads "<total>" and "<subline>" without a trend icon
|
||||
|
||||
Examples:
|
||||
| total | previous | subline |
|
||||
| 40 | 0 | No earlier data |
|
||||
| 40 | 40 | No change |
|
||||
|
||||
Scenario: Empty, no activity yet — a new site
|
||||
Given GET /v1/activity resolves 200 with items [] and total 0, and GET /v1/activity/summary
|
||||
resolves 200 with every number 0
|
||||
And no filter chip is shown
|
||||
When the user navigates to /activity
|
||||
Then the strip reads "0" "No data yet" for Total and Active people, "0" "Nothing failed" and
|
||||
"0" "Nothing to flag"
|
||||
And an empty panel with icon history shows "No activity yet" and "Actions on articles,
|
||||
media, users and API keys will appear here."
|
||||
And "Export CSV" is disabled and no table header or pager is shown
|
||||
|
||||
Scenario: Empty, filters match nothing — the panel offers Clear filters
|
||||
Given the Default state has loaded
|
||||
When the user types "sitemap" in Search, chooses Category "Media" and Outcome "Failed", and
|
||||
GET /v1/activity resolves 200 with items [] and total 0
|
||||
Then the chips read "Search: sitemap", "Category: Media", "Outcome: Failed" and "Clear all"
|
||||
And an empty panel with icon search-x shows "No activity matches these filters" and "Try a
|
||||
different search or a wider date range." with the action "Clear filters"
|
||||
And "Export CSV" stays enabled and the strip is unchanged
|
||||
When the user activates "Clear filters"
|
||||
Then every filter is reset, GET /v1/activity is sent with range=7d, page=1, size=25 only and
|
||||
the rows come back
|
||||
|
||||
Scenario Outline: Error with retry — a 500, a 400 or a network failure replaces the table
|
||||
Given GET /v1/activity <failure> and GET /v1/activity/summary resolves 200
|
||||
When the user navigates to /activity
|
||||
Then the strip and the filter bar are shown
|
||||
And an error alert shows "Couldn't load activity" and "Check your connection and try again."
|
||||
with the action "Try again"
|
||||
And "Export CSV" is disabled and no table or pager is shown
|
||||
When the user activates "Try again"
|
||||
Then the Loading state shows for the list and GET /v1/activity is sent again with the same
|
||||
query
|
||||
When it then resolves 200 with the Default data
|
||||
Then the Default rows replace the alert
|
||||
|
||||
Examples:
|
||||
| failure |
|
||||
| resolves with a 500 response |
|
||||
| resolves with a 400 response |
|
||||
| never reaches the server |
|
||||
|
||||
Scenario Outline: The summary alone fails — the strip is not drawn and the list still works
|
||||
Given GET /v1/activity resolves 200 with the Default items and GET /v1/activity/summary
|
||||
<failure>
|
||||
When the user navigates to /activity
|
||||
Then the strip is not rendered, with no error shown for it
|
||||
And the filter bar, the rows and the pager are shown as in Default
|
||||
|
||||
Examples:
|
||||
| failure |
|
||||
| resolves with a 500 response |
|
||||
| resolves with a 403 response |
|
||||
| never reaches the server |
|
||||
|
||||
Scenario: Forbidden (403) — the page itself is refused
|
||||
Given GET /v1/activity resolves 403 and GET /v1/auth/me resolves 200 with a verified user
|
||||
When the user navigates to /activity
|
||||
Then the header shows "Activity" and no "Export CSV"
|
||||
And no strip, filter bar, chips, table or pager is shown
|
||||
And an empty panel with icon lock shows "You can't view this activity" and "Your role
|
||||
doesn't include this page. Ask an owner or admin if you need access." with the action "Back
|
||||
to home"
|
||||
When the user activates "Back to home"
|
||||
Then the router navigates to /home
|
||||
|
||||
Scenario: An unverified 403 and a 401 are handled globally
|
||||
Given GET /v1/activity resolves 401 (or 403 for an unverified session)
|
||||
When the user navigates to /activity
|
||||
Then useApi redirects to /login?redirect=/activity&reason=expired (or to /register/pending)
|
||||
And no Forbidden or Error panel is shown
|
||||
|
||||
Scenario Outline: Strip cards — each card applies its filter
|
||||
Given the Default state has loaded with the 7 days range
|
||||
When the user activates the "<card>" card
|
||||
Then <effect>
|
||||
And <state>
|
||||
|
||||
Examples:
|
||||
| card | effect | state |
|
||||
| Total actions | every filter is cleared and GET /v1/activity is sent with range=7d | no card is marked active |
|
||||
| Active people | nothing changes and no request is sent | no card is marked active |
|
||||
| Failed actions | GET /v1/activity is sent with outcome=failed and the page 1 | the chip "Outcome: Failed" and "Clear all" appear and the card has aria-pressed="true" with a --ui-primary border |
|
||||
| Security events | GET /v1/activity is sent with security=true and the page 1 | the chip "Security events" and "Clear all" appear and the card has aria-pressed="true" with a --ui-primary border |
|
||||
|
||||
Scenario: Filtered, card applied — the Failed card on top of other filters
|
||||
Given the user has chosen Category "Authentication" and 30 days
|
||||
When the user activates the "Failed actions" card and GET /v1/activity resolves 200 with 1
|
||||
item and total 6
|
||||
Then the chips read "Category: Authentication", "Outcome: Failed", "Dates: 30 days" and
|
||||
"Clear all"
|
||||
And GET /v1/activity/summary was requested for range=30d and is not requested again
|
||||
And the pager reads "Showing 1–6 of 6"
|
||||
And below md the "Filters" button shows the count badge "3"
|
||||
|
||||
Scenario Outline: Filter bar — each control sends its query parameter and resets the page
|
||||
Given the user is on page 3 of the Default list
|
||||
When <action>
|
||||
Then GET /v1/activity is sent with <query>, page=1 and size=25
|
||||
|
||||
Examples:
|
||||
| action | query |
|
||||
| the user types "spring" and waits 300 ms | q=spring and range=7d |
|
||||
| the user types "spring" and presses Enter at once | q=spring and range=7d |
|
||||
| the user chooses Person "Marta Alves" (user 3) | actor=user:3 and range=7d |
|
||||
| the user chooses Person "Website sync" (API key 7) | actor=key:7 and range=7d |
|
||||
| the user chooses Person "System" | actor=system and range=7d |
|
||||
| the user chooses Category "Users and invites" | category=users and range=7d |
|
||||
| the user chooses Outcome "Succeeded" | outcome=success and range=7d |
|
||||
| the user chooses "Today" | range=today |
|
||||
| the user chooses "30 days" | range=30d |
|
||||
|
||||
Scenario: Filter bar — Custom opens a date-range popover and applies on the second date
|
||||
Given the Default state has loaded
|
||||
When the user chooses "Custom" and picks 1 Sep 2026 then 30 Sep 2026 in the popover
|
||||
Then GET /v1/activity and GET /v1/activity/summary are sent with range=custom, from=2026-09-01
|
||||
and to=2026-09-30
|
||||
And the chip reads "Dates: 1 – 30 Sep 2026"
|
||||
And no request is sent after the first date alone
|
||||
|
||||
Scenario: Chips — removing one resets only that filter, Clear all resets everything
|
||||
Given the chips "Search: spring", "Outcome: Failed" and "Dates: 30 days" are shown
|
||||
When the user activates the x of "Outcome: Failed"
|
||||
Then GET /v1/activity is sent with q=spring and range=30d and without outcome
|
||||
When the user activates "Clear all"
|
||||
Then GET /v1/activity is sent with range=7d only, and the summary is requested for range=7d
|
||||
And no chip and no "Clear all" is shown
|
||||
|
||||
Scenario: The Person menu loads its options once, on first open
|
||||
Given the Default state has loaded
|
||||
Then GET /v1/user/ and GET /v1/api-key/ have not been requested
|
||||
When the user opens the Person menu
|
||||
Then both are requested once and the menu lists the users, then the API keys, then "System"
|
||||
And opening it again does not request them again
|
||||
|
||||
Scenario: Pagination — next page, page size and totals
|
||||
Given the Default state has loaded with total 1284
|
||||
When the user activates page "2" of the pager
|
||||
Then GET /v1/activity is sent with page=2 and size=25 and the pager reads "Showing 26–50 of
|
||||
1,284"
|
||||
When the user chooses "50" in "Rows per page"
|
||||
Then GET /v1/activity is sent with page=1 and size=50 and the pager reads "Showing 1–50 of
|
||||
1,284"
|
||||
And on the last page 52 of size 25 the pager reads "Showing 1,276–1,284 of 1,284" and next is
|
||||
disabled
|
||||
|
||||
Scenario: Mobile — the list is cards and the controls collapse into a sheet
|
||||
Given the Default state has loaded below md with the filters Category "Authentication" and
|
||||
Outcome "Failed" applied and GET /v1/activity resolves with total 6
|
||||
Then the page shows the search field and "Filters" with the badge "2", and no Person,
|
||||
Category, Outcome or date control
|
||||
And the table is replaced by cards, each with its actor, time, sentence and badges
|
||||
And the pager reads "Page 1 of 1" with "1–6 of 6"
|
||||
When the user activates "Filters"
|
||||
Then a bottom sheet titled "Filters" shows Person, Category, Outcome, Dates, "Clear all" and
|
||||
"Show 6 results"
|
||||
When the user chooses "Custom"
|
||||
Then two date fields appear in the sheet
|
||||
When the user activates "Show 6 results"
|
||||
Then the sheet closes and the list stays filtered
|
||||
|
||||
Scenario: Export — the file is prepared, then downloads
|
||||
Given the Default state has loaded with the filters Category "Authentication" and 30 days
|
||||
When the user activates "Export CSV"
|
||||
Then GET /v1/activity/export is sent with category=auth and range=30d and without page or size
|
||||
And while it is in flight the button reads "Preparing file…" and is disabled
|
||||
When it resolves 200 with a text/csv body
|
||||
Then the file is downloaded as "activity.csv" and a toast appears: color success, icon
|
||||
i-lucide-circle-check, title "Export started. Your file will download shortly."
|
||||
And the button returns to "Export CSV", enabled
|
||||
|
||||
Scenario Outline: Export — a failure raises an error toast and re-enables the button
|
||||
Given the Default state has loaded
|
||||
When the user activates "Export CSV" and GET /v1/activity/export <failure>
|
||||
Then a toast appears: color error, icon i-lucide-circle-x, title "Couldn't export activity.
|
||||
Try again."
|
||||
And nothing is downloaded and the button returns to "Export CSV", enabled
|
||||
|
||||
Examples:
|
||||
| failure |
|
||||
| resolves with a 500 response |
|
||||
| resolves with a 403 response |
|
||||
| never reaches the server |
|
||||
|
||||
Scenario: Details — a row opens the slide-over and focus returns on close
|
||||
Given the Default state has loaded
|
||||
When the user activates the second row (click, or Enter with the row focused)
|
||||
Then the slide-over opens with the title "Activity details", the row is marked selected and
|
||||
the footer reads "Event 2 of 1,284"
|
||||
When the user presses Escape (or activates the close button, or the scrim)
|
||||
Then the slide-over closes and focus is on that row
|
||||
|
||||
Scenario: Details — the Edit with a diff event
|
||||
Given the event "Why we reuse original floorboards" has action "article.edit" with changes
|
||||
[{"field": "title", "kind": "text", "before": "Why we reuse floorboards", "after": "Why we
|
||||
reuse original floorboards"}, {"field": "body", "kind": "body", "delta": 312}]
|
||||
When its details open
|
||||
Then the sentence reads "Edited the article Why we reuse original floorboards"
|
||||
And the Changes section shows a "Title" row with Before "Why we reuse floorboards" and After
|
||||
"Why we reuse original floorboards", then a "Body" row with "Body edited (+312 characters)"
|
||||
And no body text appears anywhere in the panel
|
||||
|
||||
Scenario: Details — long values clamp to two lines with Show more
|
||||
Given an event with a text change whose Before and After are 170-character values, and
|
||||
another text change with short values
|
||||
When its details open and the long values overflow two lines
|
||||
Then the long row's values are clamped and "Show more" appears under that row only
|
||||
When the user activates "Show more"
|
||||
Then the values are fully shown and the link reads "Show less"
|
||||
|
||||
Scenario: Details — a failed action with a very long target title shows its reason
|
||||
Given an event with action "article.publish", outcome "failed", failure_reason "The article
|
||||
has no cover image. Add one, then publish again." and a 150-character target name
|
||||
When its details open
|
||||
Then the sentence wraps with the title in semibold, the "Failed" badge is shown and an error
|
||||
alert "Why it failed" reads the reason
|
||||
And the Target section wraps the long name
|
||||
|
||||
Scenario: Details — a deleted target has no diff
|
||||
Given an event of action "media.delete" with target {"type": "image", "id": "9", "name":
|
||||
"old-banner.png", "exists": false} and no changes
|
||||
When its details open
|
||||
Then the Target section shows "old-banner.png" struck through, a "Deleted" badge and "This
|
||||
item was deleted. Its name is kept for reference."
|
||||
And there is no Changes section
|
||||
|
||||
Scenario: Details — a system-initiated event has no human actor
|
||||
Given an event with actor {"type": "system", "id": null, "name": "System"}, source "system",
|
||||
ip null and user_agent null
|
||||
When its details open
|
||||
Then Actor shows the cog avatar, "System" and a "System" badge, Source reads "System task",
|
||||
and IP address and Browser read "Not applicable"
|
||||
|
||||
Scenario: Details — a failed login shows the actor as not signed in
|
||||
Given an event with action "auth.login_failed", actor {"type": "anonymous", "id": null,
|
||||
"name": "alex@example.com"}, security true, outcome "failed", failure_reason "Wrong username
|
||||
or password.", ip "198.51.100.77" and user_agent "Chrome 129 on Windows"
|
||||
When its details open
|
||||
Then the sentence reads "Failed login attempt for alex@example.com" with a "Security" badge
|
||||
and a "Failed" badge
|
||||
And Actor reads "Not signed in", the alert "Why it failed" shows the reason, and the IP
|
||||
address and the Browser are shown
|
||||
|
||||
Scenario: Details — the times are shown in local time and in UTC
|
||||
Given an event created_at "2026-10-06T10:42:18Z"
|
||||
When its details open
|
||||
Then the When line ends with "(your time)" and the muted line below reads "6 Oct 2026,
|
||||
10:42:18 UTC"
|
||||
|
||||
Scenario Outline: Details — Source reads the event's source
|
||||
Given an event with source "<source>"
|
||||
When its details open
|
||||
Then Source reads "<label>"
|
||||
|
||||
Examples:
|
||||
| source | label |
|
||||
| web | Web app |
|
||||
| api_key | API key |
|
||||
| system | System task |
|
||||
|
||||
Scenario: Details — Previous and Next move through the filtered list across pages
|
||||
Given the list is on page 1 of size 25 with total 1284 and the first row's details are open
|
||||
Then "Previous event" is disabled and the footer reads "Event 1 of 1,284"
|
||||
When the user activates "Next event" 24 times
|
||||
Then the footer reads "Event 25 of 1,284" with no extra request
|
||||
When the user activates "Next event" once more
|
||||
Then GET /v1/activity is sent with the same filters, page=2 and size=25
|
||||
And the panel shows the first item of page 2, the footer reads "Event 26 of 1,284" and the
|
||||
pager under the table reads "Showing 26–50 of 1,284"
|
||||
When the user activates "Previous event"
|
||||
Then GET /v1/activity is sent with page=1 and the panel shows the last item of page 1 as
|
||||
"Event 25 of 1,284"
|
||||
And on the last event "Next event" is disabled
|
||||
|
||||
Scenario: Details — a failed neighbour page keeps the panel on the current event
|
||||
Given the last item of page 1 is open
|
||||
When the user activates "Next event" and GET /v1/activity resolves 500
|
||||
Then the panel still shows the same event, the button is enabled again and the list is
|
||||
unchanged
|
||||
|
||||
Scenario: Details — below md the slide-over is a bottom sheet
|
||||
Given the Default state has loaded below md
|
||||
When the user activates the second card
|
||||
Then a bottom sheet opens, 88 % high, with a handle, the title "Activity details", "2 of
|
||||
1,284" in the header, and full-width "Previous event" and "Next event" buttons
|
||||
And there is no "Event 2 of 1,284" footer text
|
||||
|
||||
Scenario: My activity — a role that is not owner or admin sees its own events
|
||||
Given the session role is "editor" (or absent) and GET /v1/activity resolves 200 with 25
|
||||
items and total 41
|
||||
When the user navigates to /activity
|
||||
Then the title reads "My activity", the subtitle "Everything you've done on this site." and
|
||||
the breadcrumb "My activity"
|
||||
And no summary strip, no Person filter and no "Export CSV" are rendered
|
||||
And the table has the columns When, Action, Target, Outcome and no Actor column
|
||||
And the cards show no actor line below md
|
||||
And GET /v1/activity is sent with range=7d, page=1, size=25 and no actor parameter, and
|
||||
GET /v1/activity/summary is never sent
|
||||
And the pager reads "Showing 1–25 of 41"
|
||||
|
||||
Scenario: My activity, empty — the user has not done anything yet
|
||||
Given the session role is "editor" and GET /v1/activity resolves 200 with items [] and total 0
|
||||
When the user navigates to /activity
|
||||
Then an empty panel with icon history shows "No activity yet" and "Your actions will appear
|
||||
here once you start working on the site."
|
||||
|
||||
Scenario: My activity, details — the slide-over on one of the user's own events
|
||||
Given the session role is "editor" and the Default list has loaded
|
||||
When the first row's details open
|
||||
Then the footer reads "Event 1 of 41", "Previous event" is disabled and the panel is the same
|
||||
as for an owner, with the Actor row showing the user's own name
|
||||
|
||||
Scenario: The French interface composes sentences and chrome in French
|
||||
Given the language is French and the Default state has loaded as owner
|
||||
Then the title reads "Activité", the strip labels, filter placeholders, badges, pager and
|
||||
"Exporter en CSV" are in French, the sentences use the French templates and day
|
||||
separators read "Aujourd'hui", "Hier" or "3 oct. 2026"
|
||||
And the Security sub-line reads "11 signalés"
|
||||
And every new English string of this screen has a French entry in messages.ts
|
||||
|
||||
Scenario: Hover shows the exact time
|
||||
Given the Default state has loaded
|
||||
Then each When cell has a title attribute holding the full local time with seconds
|
||||
@@ -0,0 +1,257 @@
|
||||
# QA: Activity screen, details slide-over and My activity
|
||||
|
||||
## Implementation note for coder (informational, not a QA assertion)
|
||||
|
||||
The activity API is already built and covered by features/activity-log-backend.qa.md and
|
||||
features/activity-log-producers.qa.md: this slice has no Go change and no HTTP-level Go section.
|
||||
|
||||
The shell specs that list the navigation are amended to seven destinations (Home, Articles, Media,
|
||||
Users, Activity, Integration, Settings): features/dashboard_shell_branding.feature,
|
||||
features/media_browse.feature, features/users_screen.feature and .qa.md,
|
||||
features/settings_page.feature and .qa.md, features/profile_page.feature. Their Vitest tests
|
||||
(DashboardShell.spec.ts, where Settings moves from index 5 to 6) must be updated in the same change.
|
||||
|
||||
Export needs the response body as a file, which `useApi().request` does not return (it calls
|
||||
`response.json()`). Add a raw or blob read to `useApi` that keeps the global 401 handling, builds
|
||||
an object URL and clicks a temporary anchor with `download="activity.csv"`. The toasts use
|
||||
`useToast().add({ color, icon, title })` with the one sentence as the title.
|
||||
|
||||
The page needs a date-range popover and a Custom date pair in the mobile sheet. The clamp ("Show
|
||||
more") is measured from the rendered element, so tests stub `scrollHeight` and `clientHeight`.
|
||||
|
||||
Every new English string needs a French entry in `web/src/i18n/messages.ts`. Sentences are
|
||||
composed by a composable (`useActivitySentence`) that returns segments so the target name can be
|
||||
rendered semibold. The Security sub-line is "{n} flagged" with n = `summary.security`, French
|
||||
"{n} signalés".
|
||||
|
||||
## Automated (Vitest + Vue Test Utils, `task test:web`)
|
||||
|
||||
Mock `useApi`'s `request` (and the export transport) per case, matching on method, path and parsed
|
||||
query (`URLSearchParams`); no real backend. Mount ActivityView inside DashboardShell with a
|
||||
resolved verified session (id 2, username "ines", display_name "Inès Duarte", role "owner") and a
|
||||
test router at /activity; the regular-user cases use role "editor" (and one with no role). Run
|
||||
each layout case at a desktop and a below-md width (mock `matchMedia`, see
|
||||
`components/__tests__/viewport.ts`). Timers: use `vi.useFakeTimers({ toFake: ['setTimeout',
|
||||
'clearTimeout'] })` so the 150 ms skeleton delay and the 300 ms search debounce can be advanced
|
||||
while `Date` stays real. No test freezes or asserts the system date.
|
||||
|
||||
Clock-independence rules for fixtures: every event in the Vitest fixtures is dated 3 Oct 2026 or
|
||||
earlier, created at 12:00 to 13:00 UTC so its local day is the same in any time zone from UTC-11
|
||||
to UTC+11; assert day separators with the absolute label "3 Oct 2026" and When cells only against
|
||||
the pattern `^\d{2}:\d{2}$` and a non-empty `title`; never assert "Today", "Yesterday" or a
|
||||
relative "N min ago". UTC lines can be asserted exactly; the local time line only with its
|
||||
"(your time)" suffix.
|
||||
|
||||
Fixtures: `activityItems` (a 15-item list built from the board's events, mapped to the API shape
|
||||
with the action codes of the "Visual" section), `summaryDefault` (total 1284, previous_total
|
||||
1146, active_people {users 7, api_keys 2}, failed 6, security 11, series total [4,6,5,8,7,9,6],
|
||||
active [5,6,6,7,7,8,9], failed [0,1,0,2,1,0,2], security [1,0,2,1,3,2,2]), `summaryZero` (total
|
||||
214, previous_total 223, active_people {4, 1}, failed 0, security 0, series failed and security
|
||||
zeros) and `summaryNone` (all zeros). The list response is `{ items, total, page, size }`.
|
||||
|
||||
1. **Navigation** — sidebar and drawer list Home, Articles, Media, Users, Activity, Integration,
|
||||
Settings in order; "Activity" has the `i-lucide-history` icon, links to /activity, has
|
||||
aria-current="page" on /activity and none elsewhere; breadcrumb and mobile title "Activity".
|
||||
2. **Default (owner)** — assert header texts, enabled "Export CSV", the four strip cards (labels,
|
||||
"1,284", "9", "6", "11", sub-lines "12% vs previous 7 days" with the trending-up icon, "7
|
||||
people, 2 API keys", "0.5% of all actions" with icon `circle-x`, "11 flagged" with icon
|
||||
`shield-alert`), the filter bar (placeholder, "All people", "All categories", Outcome All
|
||||
selected, 7 days selected), no chip, the pager text "Showing 1–25 of 1,284", and that `request`
|
||||
was called exactly twice at mount with the expected queries (`range=7d&page=1&size=25` and
|
||||
`range=7d`).
|
||||
3. **Rows, Examples-driven** — one fixture row per Examples row of the feature; assert the
|
||||
Actor cell (initials, key or cog or user-round-x icon, badge, "Not signed in"), the sentence
|
||||
text and that the target name is in a semibold element, the shield marker only on security
|
||||
rows, and the Outcome badge word.
|
||||
4. **Target cell** — deleted (struck-through class, "Deleted" badge, type line), living (link
|
||||
with `href` "/article/8/edit", "/media", "/users", "/integration/api-keys" per type, plain text
|
||||
for "release"), and null ("—"); clicking the link navigates and does not open the details.
|
||||
5. **Day grouping and When cell** — fixture across 3 Oct and 2 Oct 2026; assert two separators
|
||||
"3 Oct 2026" and "2 Oct 2026" in order, the When cell pattern and `title`.
|
||||
6. **Sentences, Examples-driven** — for each Examples row set the locale (`setLocale`), render
|
||||
and assert the exact sentence, including plurals, `failed_groups`, "Forever", the dropped slot
|
||||
for a missing target name, the unknown code `article.archive`, and the success sentence used
|
||||
for an unlisted failed action (`auth.login` failed). Add a completeness test: every action in
|
||||
the catalogue has an English and a French template, and every key used on the page exists in
|
||||
`messages.ts`.
|
||||
7. **Loading** — unresolved requests; at 149 ms nothing in place of strip and table; at 150 ms 4
|
||||
skeleton cards, 9 skeleton rows (5 cards below md), `aria-busy="true"`, no pager; resolve and
|
||||
assert the Default content.
|
||||
8. **Zero-value strip** — `summaryZero`; assert "214" "4% vs previous 7 days" with the
|
||||
trending-down icon, "5" "4 people, 1 API key", "0" "Nothing failed" and "0" "Nothing to flag"
|
||||
with `circle-check`, no error or warning fill class, bars at 2 px.
|
||||
9. **Total sub-line without a comparison, Examples-driven** — previous_total 0 gives "No earlier
|
||||
data" and equal totals give "No change", both without a trend icon; French "Aucune donnée
|
||||
antérieure" and "Aucun changement"; ranges Today, 30 days and Custom give "vs previous day",
|
||||
"vs previous 30 days" and "vs previous period".
|
||||
10. **Empty, no activity yet** — `summaryNone`, items []; assert "No data yet" on two cards, the
|
||||
calm lines on the other two, the empty panel copy and icon, a disabled "Export CSV", no table
|
||||
header and no pager.
|
||||
11. **Empty, filters match nothing** — type "sitemap", choose Category "Media" and Outcome
|
||||
"Failed", resolve items []; assert chips, empty panel copy, "Clear filters", enabled export;
|
||||
click "Clear filters" and assert a request with `range=7d&page=1&size=25` only.
|
||||
12. **Error, Examples-driven** — list rejects with `ApiError(500)`, `ApiError(400)`, then
|
||||
`NetworkError`, summary resolves; assert the strip, the alert copy, "Try again", a disabled
|
||||
export and no table; click "Try again"; assert the list request is repeated and the Default
|
||||
rows replace the alert.
|
||||
13. **Summary alone fails** — summary rejects with `ApiError(500)`, `ApiError(403)`, then
|
||||
`NetworkError`; assert the strip is absent, nothing is drawn in its place, and the list is
|
||||
fully rendered.
|
||||
14. **Forbidden** — list rejects with `ApiError(403)` and a verified session; assert no export
|
||||
button, no strip, no filter bar, the lock panel copy, and that "Back to home" navigates to
|
||||
/home. Also `ApiError(401)` triggers the global redirect and no panel.
|
||||
15. **Strip cards, Examples-driven** — click each card; assert the effect and `aria-pressed`
|
||||
(Failed: request with `outcome=failed&page=1`, chip "Outcome: Failed"; Security: request with
|
||||
`security=true`, chip "Security events"; Total: filters cleared; Active people: no request)
|
||||
and that the summary is not requested again.
|
||||
16. **Filtered, card applied** — Category "Authentication", 30 days, then the Failed card; assert
|
||||
the chip texts and order, "Clear all", the summary requested only once for `range=30d`, the
|
||||
pager "Showing 1–6 of 6", and below md the Filters badge "3".
|
||||
17. **Filter bar, Examples-driven** — for each Examples row assert the exact query, `page=1` after
|
||||
starting on page 3, and the 300 ms debounce with fake timers (no request at 299 ms, one at
|
||||
300 ms; Enter applies at once and the pending debounce does not send a second request).
|
||||
18. **Custom dates** — pick two dates in the popover (stub the calendar's model); assert no request
|
||||
after the first date, then `range=custom&from=2026-09-01&to=2026-09-30` on the list and the
|
||||
summary, and the chip "Dates: 1 – 30 Sep 2026"; also "28 Sep – 3 Oct 2026".
|
||||
19. **Chips** — remove one chip, then "Clear all"; assert the queries and that the summary is
|
||||
requested again only when the range changes back.
|
||||
20. **Person options** — assert no `GET /v1/user/` or `GET /v1/api-key/` call at mount, both on
|
||||
first open of the menu, none on the second; assert group order and the three query values.
|
||||
21. **Pagination** — next page, size 50, last page "Showing 1,276–1,284 of 1,284" with next
|
||||
disabled; mobile "Page 1 of 52" and previous and next.
|
||||
22. **Mobile list and sheet** — below md assert cards (actor line, sentence, badges, "Target
|
||||
deleted"), "Filters" with the badge count, the sheet content and footer ("Show 6 results", "Show
|
||||
1 result"), two date fields for Custom, and that "Show … results" closes it.
|
||||
23. **Export success** — click "Export CSV" with filters applied; assert one request to
|
||||
`/v1/activity/export` with the list filters and no `page` or `size`, "Preparing file…" and
|
||||
disabled while pending, an object URL created and an anchor clicked with
|
||||
`download="activity.csv"`, the success toast (color success, icon `i-lucide-circle-check`,
|
||||
title "Export started. Your file will download shortly."), and the button back to "Export CSV".
|
||||
24. **Export failure, Examples-driven** — 500, 403 and `NetworkError`; assert the error toast
|
||||
(color error, icon `i-lucide-circle-x`, title "Couldn't export activity. Try again."), no
|
||||
download, button re-enabled.
|
||||
25. **Details open and close** — click, then Enter on a focused row; assert the title, the selected
|
||||
row class, the footer "Event 2 of 1,284"; close with Escape, the close button and the scrim and
|
||||
assert focus on the row.
|
||||
26. **Details content, Examples-driven** — the six board events: Edit with a diff (Title row with
|
||||
both values and the "Body edited (+312 characters)" row, "(+1 character)", "(-48 characters)"),
|
||||
long text (stub overflow; "Show more" on that row only, toggles to "Show less"), failed with a
|
||||
long title (alert "Why it failed", wrapping), deleted target (struck-through name, "Deleted"
|
||||
badge, explanation, no Changes), system event ("System" badge, "System task", two "Not
|
||||
applicable"), failed login ("Not signed in", "Security" badge, reason, IP and Browser). Assert
|
||||
the field labels and the Draft and Published values for `online`, the raw code for an unknown
|
||||
field, and that no body text is ever rendered.
|
||||
27. **Details times** — `created_at` "2026-10-06T10:42:18Z": the UTC line is exactly "6 Oct 2026,
|
||||
10:42:18 UTC" and the local line ends with "(your time)".
|
||||
28. **Previous and Next across pages** — first item: previous disabled, "Event 1 of 1,284"; 24
|
||||
nexts without a request; the 25th next sends `page=2&size=25` with the same filters, shows
|
||||
page 2's first item, "Event 26 of 1,284" and the pager "Showing 26–50 of 1,284"; previous
|
||||
sends `page=1` and shows "Event 25 of 1,284"; the last event disables next; a 500 on the
|
||||
neighbour page keeps the panel on the current event and re-enables the button; the position
|
||||
also follows a non-default size (size 50, page 2, index 0 is "Event 51").
|
||||
29. **Details below md** — the bottom-sheet variant: header "2 of 1,284", full-width buttons, no
|
||||
footer text.
|
||||
30. **My activity, Examples-driven** — role "editor" then no role: title, subtitle, breadcrumb "My
|
||||
activity", no strip, no Person filter, no export, no Actor column or card actor line; assert no
|
||||
request to `/summary` or `/export` and no `actor` parameter; pager "Showing 1–25 of 41"; empty
|
||||
state copy; details "Event 1 of 41" with previous disabled.
|
||||
31. **French** — `setLocale('fr')`; assert the chrome strings (Activity "Activité", My activity
|
||||
"Mon activité", Export CSV "Exporter en CSV", Preparing file… "Préparation du fichier…",
|
||||
Total actions "Actions au total", Active people "Personnes actives", Failed actions "Actions
|
||||
en échec", Security events "Événements de sécurité", Succeeded "Réussie", Failed "Échec", Not
|
||||
signed in "Non connecté", Activity details "Détails de l'activité", Previous event and Next
|
||||
event "Événement précédent" and "Événement suivant", "11 signalés"), the French sentences, the
|
||||
plural and "Forever" ("Toujours") forms, and the day separator format.
|
||||
32. **Hover time** — each When cell has a `title` that is non-empty and contains seconds.
|
||||
|
||||
## Visual (task visual:check -- activity, desktop and mobile, light and dark)
|
||||
|
||||
Reference: `docs/design/screens/activity.md`, board `docs/design/source/RenewCMS Screens
|
||||
Activity.dc.html`, groups "Activity, owner and admin", "Details slide-over" and "My activity"
|
||||
(the last two are already declared as `extraSources` in `web/visual/check.ts`); exact values in
|
||||
`docs/design/source/ActivityFrame.dc.html`. Add each state to `visualStates.activity` in
|
||||
`web/visual/states.ts` with the board's own sample data. The gate is hard: every capture of every
|
||||
non-skipped state must be within 10 %, for both viewports and both themes (6 gated states, 24
|
||||
captures). No state uses the `now` option or a frozen clock. Mocks are keyed by method and path
|
||||
(`GET /v1/activity`, `GET /v1/activity/summary`, `GET /v1/auth/me`); use `bodyFor` to read the
|
||||
query. Do not mock or call `/v1/user/` or `/v1/api-key/` (Person options load only when the menu
|
||||
opens, which no state does), so there is no unmocked request.
|
||||
|
||||
Sessions: add `activityOwnerSession` (id 2, display_name "Inès Duarte", role "owner") and
|
||||
`activityEditorSession` (id 3, display_name "Marta Alves", role "editor"), as the board draws them
|
||||
in its sidebar chip. The board draws a 4-item sidebar for the regular user as illustrative; the
|
||||
real shell keeps all 7 items, a known small difference in the My activity Empty capture.
|
||||
|
||||
Fixtures (all taken from the frame's script): `activityBoardEvents`, the 15 events of the frame in
|
||||
its order, mapped to API items. Where the frame's sample has no real producer the nearest real
|
||||
code is used and the sentence differs from the board (accepted; those rows only appear in skipped
|
||||
states): 1 `article.publish`, 2 `article.edit` with title and body (+312) changes, 3
|
||||
`auth.login_failed` (anonymous, name "alex@example.com", reason from the frame), 4
|
||||
`api_key.revoke`, 5 `system.media_purge` (system, ip and user_agent null), 6 `invite.accept`
|
||||
(replaces "Changed the role"), 7 `media.upload` by the API key "Website sync", 8 `media.delete`
|
||||
(target not existing), 9 `article.publish` failed with the 150-character title, 10
|
||||
`system.activity_cleanup`, 11 `article.edit` with long title values (replaces the "Site
|
||||
description" setting diff), 12 `api_key.create`, 13 `user.update_profile`, 14 `media.upload`, 15
|
||||
`auth.password_reset_completed`; times are the frame's local times minus one hour as UTC
|
||||
(`created_at`), IPs and browsers as in the frame, security as in the frame. Summary fixtures are
|
||||
the three in the Automated section; the strip of the Default state is "1,284 / 9 / 6 / 11" with
|
||||
the sub-line "11 flagged". Total is 1284 with page size 25 for the owner and 41 for the editor.
|
||||
|
||||
- [ ] Loading: both requests with `hang: true`; the setup waits 150 ms or more so the skeleton
|
||||
strip and rows are drawn before the capture.
|
||||
- [ ] Empty, no activity yet: list `items: [], total: 0`, summary `summaryNone`; "Export CSV"
|
||||
disabled.
|
||||
- [ ] Empty, filters match nothing: the board applies Search "sitemap", Category "Media", Outcome
|
||||
"Failed" (steps type and select them, or the mock answers with the filtered query), list
|
||||
`items: [], total: 0`, summary Default.
|
||||
- [ ] Error with retry: list 500, summary Default; the alert, disabled export, strip drawn.
|
||||
- [ ] Forbidden (403): list 403, summary 403; session with a non-admin role as the board does
|
||||
(`role: 'user'`), so no "Export CSV".
|
||||
- [ ] My activity, Empty: `activityEditorSession`, list `items: [], total: 0`.
|
||||
- [ ] skip: clock-sensitive. Default, large dataset: `skip: 'clock-sensitive'` (day separators and
|
||||
"2 min ago" depend on the real clock). Prints SKIP, never investigated or date-frozen.
|
||||
- [ ] skip: clock-sensitive. Zero-value summary strip, Filtered card applied and Mobile filters
|
||||
sheet (all draw Today rows behind the strip or sheet).
|
||||
- [ ] skip: clock-sensitive. The six Details slide-over states (Edit with a diff, Settings diff
|
||||
long text, Failed action very long target title, Deleted target no diff, System-initiated no
|
||||
human actor, Security event failed login) and the My activity states Default and Details:
|
||||
the rows behind the scrim show relative times and the panel shows the browser's local
|
||||
time. Their Vitest coverage is cases 3, 5, 25 to 30.
|
||||
- [ ] Layout, from `ActivityFrame.dc.html`, not `components.md`: header row is the title block
|
||||
(20 px/28 px title) on the left and the 32 px (44 px mobile) "Export CSV" button on the right,
|
||||
aligned to the bottom; the strip is 4 columns with a 12 px gap on desktop and a clipped row
|
||||
of 156 px cards with an 8 px gap on mobile; the table header is 36 px, day rows 32 px, rows at
|
||||
least 52 px; the slide-over is 480 px wide on desktop, the bottom sheet 88 % high.
|
||||
- [ ] Error is drawn as the frame's alert (error soft background, circle-x, title, text, 28 px
|
||||
outline "Try again"), the empty states as the frame's centred panel (44 px icon box, 15 px
|
||||
semibold title, muted text up to 340 px).
|
||||
- [ ] Not covered by screenshots: keyboard and focus order (Tab reaches Export CSV, the strip
|
||||
cards, the search, the selects, the groups, a row; Enter opens the details; focus is trapped
|
||||
in the slide-over and returns to the row on close; Escape closes it); hover (the row, the
|
||||
native `title` tooltip), the 240 ms slide-over and sheet motion and the skeleton pulse, all
|
||||
stopped under reduced motion; the date-range popover.
|
||||
|
||||
## Manual verification
|
||||
|
||||
- **Real round trips against `task dev`** — Vitest only mocks `useApi`. As owner: perform an
|
||||
article publish, an image upload and a failed login, then confirm they appear newest first with
|
||||
the right sentences, the shield marker on the failed login, the Succeeded and Failed badges, the
|
||||
summary numbers and the sparkline bars; apply each filter and each card; page through with size
|
||||
25, 50 and 100; open the details across a page boundary.
|
||||
- **Real CSV** — click "Export CSV" with and without filters and open `activity.csv`: columns
|
||||
`when,actor,action,target,outcome,source,ip`, the filtered rows only, no page or size applied,
|
||||
neutralised cells that start with `=`, `+`, `-` or `@`; confirm the browser download prompt and
|
||||
the two toasts.
|
||||
- **Admin session** — sign in as an admin: the same full view as the owner.
|
||||
- **Time zones** — set the OS time zone to UTC-8 and UTC+9, reload, and confirm the day
|
||||
separators, the clock times, the "(your time)" line and the UTC line are consistent.
|
||||
- **Regular user** — no real account can reach "My activity" today; confirm by a mocked
|
||||
`/v1/auth/me` in the browser (or a future role) that the view has no strip, no Person filter and
|
||||
no export, and that the server answers 403 for `actor=user:2` and for the summary and export.
|
||||
- **API key** — calling `GET /v1/activity` with an API key answers 403 "This action needs a real
|
||||
session." and the SPA never does it.
|
||||
- **"Today", "Yesterday" and relative times** — confirm them against the real clock, since
|
||||
visual:check skips every state that shows them.
|
||||
- **French** — switch the language and read every state, the sentences and the toasts.
|
||||
- **Real screen readers and zoom** — row selection, the sparkline buttons (labelled by the card
|
||||
text), the sheet focus trap.
|
||||
@@ -0,0 +1,87 @@
|
||||
Feature: Container-aware update guidance
|
||||
|
||||
As the owner of a RenewCMS site running in Docker
|
||||
I want Settings to say "update by pulling a new image" instead of "Not prepared"
|
||||
So that I never see a browser-update flow that cannot work in a container
|
||||
|
||||
Background:
|
||||
Given browser updates need Linux with systemd and the helper that "renewcms enable-browser-updates" configures
|
||||
And a binary replaced inside a container is lost when the container is recreated,
|
||||
so in a container the update is a new image: "docker compose pull && docker compose up -d"
|
||||
And config.Settings gains Container bool, read from the env var RENEWCMS_CONTAINER (default "false", set to "true" by the Dockerfile and docker-compose.yml)
|
||||
And RENEWCMS_CONTAINER is added to envDefaults in cmd/server/main.go, to .env.example and to docs/configuration.md; it is not in requiredEnvVars
|
||||
And only "true" (case-insensitive) enables container mode; any other value means false
|
||||
And the update status use case receives the bool through its constructor from api/dependecyInjection.go; internal/domain/update stays stdlib only
|
||||
And the update check, update-status.json and the release feed work unchanged in container mode
|
||||
And GET /v1/system/update gains a 13th key, container (bool), after started_at; the other 12 keys are unchanged
|
||||
And in container mode can_apply is false for everyone, even if update-helper.json exists in the data directory
|
||||
And POST /v1/system/update/download and /restart keep their guards and answer 409 because can_apply is false (existing behaviour, not re-specified)
|
||||
And errors stay plain text from http.Error
|
||||
And the user approved the copy below and the 13th key on 2026-10-05; docs/design is read-only for agents, so the design files are updated by the user, and the visual gate for container states stays PENDING until a board frame exists
|
||||
|
||||
Scenario Outline: The status carries the container flag
|
||||
Given RENEWCMS_CONTAINER is "<value>"
|
||||
When a verified user sends GET /v1/system/update
|
||||
Then the JSON key container is <flag>
|
||||
|
||||
Examples:
|
||||
| value | flag |
|
||||
| true | true |
|
||||
| TRUE | true |
|
||||
| false | false |
|
||||
| | false |
|
||||
| yes | false |
|
||||
|
||||
Scenario: Container mode disables apply even with a helper
|
||||
Given RENEWCMS_CONTAINER is "true" and HelperDetector reports the helper installed
|
||||
When the owner sends GET /v1/system/update
|
||||
Then can_apply is false and container is true
|
||||
And a newer stored result still reports available true, latest and notes
|
||||
|
||||
Scenario: The check still works in a container
|
||||
Given container mode and a feed that reports "v2.9.0-beta" newer than the running "v2.8.0-beta"
|
||||
When the owner sends POST /v1/system/update/check
|
||||
Then the status is 200 and available is true and the result is stored in update-status.json
|
||||
|
||||
Scenario Outline: Download and restart are refused in a container
|
||||
Given container mode and a stored newer release
|
||||
When the owner sends POST <path>
|
||||
Then the status is 409 and no job starts and the fetcher and the restart request writer are not called
|
||||
|
||||
Examples:
|
||||
| path |
|
||||
| /v1/system/update/download |
|
||||
| /v1/system/update/restart |
|
||||
|
||||
Scenario: Host installs are unchanged
|
||||
Given RENEWCMS_CONTAINER is unset
|
||||
Then container is false and can_apply follows the helper as before
|
||||
And "Not prepared" still shows its "renewcms enable-browser-updates" command
|
||||
|
||||
Scenario: Settings, owner, container, update available (route /settings)
|
||||
Given GET /v1/system/update resolves available true, container true, can_apply false, latest "v2.9.0-beta"
|
||||
Then the block uses the "Not prepared" frame's layout: icon info, tone info
|
||||
And the title reads "Version 2.9.0-beta is available"
|
||||
And the text reads "You have v2.8.0-beta. This site runs in Docker, so it updates by pulling a new image. Run the commands below on the server."
|
||||
And a server-command block reads "docker compose pull && docker compose up -d" with the Copy button ("Copied" with a check for 2 s, announced)
|
||||
And a link "How to update" with arrow-up-right points to docs/docker.md
|
||||
And no Update, Restart or "Check for updates" button is rendered
|
||||
And the "renewcms enable-browser-updates" command and its note are not rendered
|
||||
|
||||
Scenario: Settings, owner, container, up to date
|
||||
Given container true and available false
|
||||
Then "You're up to date" shows as in the existing Up to date state, the Restart button is not rendered and the container hint is not rendered
|
||||
And "Check for updates" is still offered
|
||||
|
||||
Scenario: Settings, admin, container
|
||||
Given the session role is "admin", container true and available true
|
||||
Then the existing Admin view shows unchanged ("Only the owner can update RenewCMS.") with no command block
|
||||
|
||||
Scenario: Settings, non-container, no helper
|
||||
Given container false and can_apply false
|
||||
Then the existing "Not prepared" state shows unchanged
|
||||
|
||||
Scenario: The shipped Docker setup turns container mode on
|
||||
Given the Dockerfile and docker-compose.yml
|
||||
Then both set RENEWCMS_CONTAINER=true
|
||||
And docs/docker.md and docs/configuration.md describe the variable
|
||||
@@ -0,0 +1,25 @@
|
||||
# QA: Container update guidance
|
||||
|
||||
## Automated (Go, test/, httptest)
|
||||
1. Fakes: in-memory update status store, fake FeedClient, fake HelperDetector, fake JobStore and restart writer, validator.InitValidator(). Mount NewSystemRouter on a fresh chi.NewRouter() with jwtauth.Verifier + Authenticator and a test jwtauth.New("HS256", ...).
|
||||
2. Parse RENEWCMS_CONTAINER through config.NewSettingsFromEnv with t.Setenv over the table in the feature (true, TRUE, false, empty, yes).
|
||||
3. GET status: container key present and equal to the table; the other 12 keys are unchanged.
|
||||
4. Container true with a helper present: can_apply false for the owner.
|
||||
5. POST check in container mode: 200, available true, stored.
|
||||
6. POST download and restart in container mode: 409, no fake called.
|
||||
7. Container false: the existing updates_* tests still pass.
|
||||
8. An admin receives can_apply false and container true.
|
||||
|
||||
## SPA (Vitest, mocked /v1)
|
||||
- Container + available: the block, command, Copy, "How to update", and no buttons. Reuse the "Not prepared" sample data from the board.
|
||||
- Container + up to date: no Restart.
|
||||
- Admin: the existing admin view.
|
||||
- Non-container "Not prepared": unchanged.
|
||||
- Visual: container states are PENDING until a board frame exists; report as PENDING, never as passing. Existing states must still pass `task visual:check -- settings`.
|
||||
|
||||
## Manual (real Docker, ./docker-compose.yml)
|
||||
1. `docker compose up -d --build`; confirm RENEWCMS_CONTAINER=true with `docker compose exec renewcms env`.
|
||||
2. Point UPDATE_FEED_URL at a feed with a newer release; log in as owner; /settings shows the container block, no Update or Restart.
|
||||
3. The Copy button puts the exact command on the clipboard.
|
||||
4. Run the shown command on a host that has a newer image; Settings shows the new version afterwards.
|
||||
5. Run the binary on the host without the variable: "Not prepared" shows the old command.
|
||||
@@ -0,0 +1,45 @@
|
||||
Feature: Docker deployment
|
||||
RenewCMS can run in a container built from the repository. The container
|
||||
reuses the existing configuration contract in cmd/server/main.go
|
||||
(requiredEnvVars, envDefaults); no new variables are introduced.
|
||||
|
||||
Background:
|
||||
Given the image is built with the default build, without the nofrontend tag
|
||||
And the container runs the binary directly as PID 1 under a non-root user
|
||||
And the environment sets DB_FILE and UPLOAD_DIR to paths on a named volume
|
||||
And the required variables PUBLIC_URL, JWT_SECRET, SMTP_LOGIN, SMTP_SENDER, SMTP_PASSWORD, SMTP_HOST and SMTP_PORT come from an env file at runtime
|
||||
|
||||
Scenario: Healthy start
|
||||
When the container starts with every required variable set
|
||||
Then it logs "RenewCMS <version> (production) listening on :8080"
|
||||
And GET /healthz on the published port returns 200 with the body "ok"
|
||||
|
||||
Scenario: Frontend and API are both served
|
||||
When the container is running
|
||||
Then GET / returns 200 with the SPA index.html
|
||||
And GET /v1/auth/registration returns 200 with a JSON body
|
||||
|
||||
Scenario Outline: A missing required variable stops the container
|
||||
When the container starts without <variable>
|
||||
Then the process exits with a non-zero status
|
||||
And the log contains "Environment variable <variable> is not set"
|
||||
|
||||
Examples:
|
||||
| variable |
|
||||
| PUBLIC_URL |
|
||||
| JWT_SECRET |
|
||||
| SMTP_LOGIN |
|
||||
| SMTP_SENDER |
|
||||
| SMTP_PASSWORD |
|
||||
| SMTP_HOST |
|
||||
| SMTP_PORT |
|
||||
|
||||
Scenario: Data survives container recreation
|
||||
Given an account was registered while the container was running
|
||||
When the container is removed and recreated on the same named volume
|
||||
Then the same account can log in
|
||||
|
||||
Scenario: Graceful shutdown on SIGTERM
|
||||
When the container receives SIGTERM
|
||||
Then the log contains "shutting down..."
|
||||
And the container exits with status 0 within 10 seconds
|
||||
@@ -0,0 +1,98 @@
|
||||
# QA: Docker deployment
|
||||
|
||||
Packaging and docs only. There is no Go test in test/ and no Vitest spec for this slice. All steps are manual command steps run from a clean checkout on a machine with Docker and Compose v2. Record results in the Notes table. Anything not run is reported NOT RUN, never PASS.
|
||||
|
||||
## Preconditions
|
||||
- Clean checkout, no bin/, tmp/, node_modules or internal/infrastructure/assets/dist present.
|
||||
- docker.env created from docker.env.example. JWT_SECRET is generated (openssl rand -hex 32) and SMTP values are real or placeholders. Nothing is committed.
|
||||
- For local HTTP trial, ENVIRONMENT=development (otherwise the Secure cookie is not stored over HTTP).
|
||||
|
||||
## Static checks (no Docker daemon needed)
|
||||
1. Dockerfile, .dockerignore and docker-compose.yml contain no comment lines (no line starting with `#`, no trailing ` #`).
|
||||
2. .dockerignore lists tmp/, bin/, node_modules (also web/node_modules), .git, internal/infrastructure/assets/dist, .env and docker.env.
|
||||
3. Dockerfile: three stages (bun, go, final). The Go build sets CGO_ENABLED=0, has no `-tags nofrontend`, and uses `-ldflags` containing `-X main.version=` from a build arg. The final stage has USER non-root, exec-form ENTRYPOINT (no `sh -c`), and no ENV, ARG or COPY holding JWT_SECRET or any SMTP_* value.
|
||||
4. docker-compose.yml: one service, a named volume mounted at the data directory, a `ports` entry, `env_file`, a healthcheck on /healthz, `stop_grace_period` >= 15s.
|
||||
5. git diff --stat shows no change under api/, internal/, web/src/, cmd/ and test/. Only Dockerfile, .dockerignore, docker-compose.yml, docker.env.example, Taskfile.yml, .gitignore, README.md and docs/ changed. No new env var appears in cmd/server/main.go.
|
||||
6. docs/docker.md exists and is linked from README.md and docs/deployment.md.
|
||||
7. `task --list` shows docker:build, docker:run, docker:up, docker:down and docker:logs.
|
||||
|
||||
## Build
|
||||
8. `task docker:build` succeeds from the clean checkout, with no pre-built SPA.
|
||||
9. `docker run --rm <image> --version` prints the version, which is not `dev`.
|
||||
10. `docker image ls` size recorded. Flag it if it is above roughly 40 MB.
|
||||
11. `docker image inspect` shows a non-root User, and `docker run --rm --entrypoint id <image>` shows uid not 0.
|
||||
12. `docker history --no-trunc <image>` contains none of the values in docker.env.
|
||||
|
||||
## Run
|
||||
13. `task docker:up` starts the service. `docker compose ps` shows it healthy within the healthcheck start period.
|
||||
14. `curl -i http://localhost:8080/healthz` returns 200 and body `ok`.
|
||||
15. `curl -i http://localhost:8080/` returns 200 text/html, and the SPA loads in a browser.
|
||||
16. `curl -i http://localhost:8080/v1/auth/registration` returns 200 with JSON.
|
||||
17. Logs show the listening line, and no permission error creating DB_FILE or UPLOAD_DIR on the volume.
|
||||
18. Register an account in the SPA, then upload an image, to confirm both the DB and UPLOAD_DIR are writable by the container user.
|
||||
|
||||
## Persistence
|
||||
19. `docker compose down` (no -v), then `task docker:up`. The account from step 18 can log in and the image is still served.
|
||||
20. `docker compose down -v` removes the volume, and the next start gives an empty instance. Documented in docs/docker.md.
|
||||
21. The backup procedure in docs/docker.md is followed once and restores into a fresh volume.
|
||||
|
||||
## Failure and shutdown
|
||||
22. For each required variable in turn, remove it from the env file and run `docker compose up`. The container exits non-zero and the log contains `Environment variable <NAME> is not set`.
|
||||
23. `docker compose stop` (timed) completes in under 10 s, the log contains `shutting down...`, and the exit code is 0. This proves PID 1 receives SIGTERM.
|
||||
24. Break the healthcheck (stop the app inside the container, or point it at the wrong port): the service becomes unhealthy.
|
||||
|
||||
## SMTP (manual, needs real SMTP)
|
||||
25. With an external SMTP host, trigger a registration or password-reset email and confirm delivery.
|
||||
26. With a host MTA via host.docker.internal, repeat. On Linux this needs the extra_hosts mapping documented.
|
||||
27. Confirm the failure mode is documented when SMTP is unreachable.
|
||||
|
||||
## Reverse proxy and TLS (manual)
|
||||
28. Put Caddy or nginx in front, per docs/docker.md, with ENVIRONMENT=production over HTTPS. Log in and confirm the Secure cookie is accepted.
|
||||
29. Set TRUSTED_PROXIES to the proxy address as seen by the container and confirm the login rate limiter uses X-Real-IP.
|
||||
|
||||
## Upgrade
|
||||
30. Follow docs/docker.md: back up, rebuild with `task docker:build`, `task docker:up`. Data is intact and `--version` shows the new version.
|
||||
31. Confirm docs state that browser-update Restart is unsupported in Docker (Settings shows "Not prepared").
|
||||
|
||||
## Notes to record
|
||||
| Item | Value |
|
||||
| --- | --- |
|
||||
| Image size | |
|
||||
| Base images and tags used | |
|
||||
| Container uid | |
|
||||
| Build time (cold) | |
|
||||
| Docker and Compose versions | |
|
||||
| Steps 25-29 run? | |
|
||||
|
||||
Not run in this slice: CI, registry publishing, Kubernetes or Helm, multi-arch. SPA tests and visual checks are out of scope.
|
||||
|
||||
## Results
|
||||
Run on Windows 11, Docker 29.8.1, Compose 5.5.1, with a throwaway docker.env (fake secrets, ENVIRONMENT=development) that was deleted afterwards.
|
||||
|
||||
| Steps | Result |
|
||||
| --- | --- |
|
||||
| 1-7, 31 | PASS |
|
||||
| 8 | PASS after a fix: `bun run build` failed in the image because vue-tsc cannot resolve .vue modules under the Bun-only node fallback; the Dockerfile now runs `bun run build-only`. vue-tsc passes on the host and under real node 22 |
|
||||
| 9, 11, 12 | PASS (version v2.8.0-beta-dirty, uid 10001, entrypoint exec form, no secret names in history) |
|
||||
| 10 | 54.5 MB, above the 40 MB flag (binary 29.9 MB on alpine 3.22) |
|
||||
| 13-17 | PASS (healthy, /healthz 200 ok, / 200 text/html, /v1/auth/registration 200 JSON, no permission errors) |
|
||||
| 18-19 | PASS (register, verify, login, image upload to /data/uploads as uid 10001; all survive `down` and recreate; uploaded image still served) |
|
||||
| 20 | PASS (`down -v` removed the volume) |
|
||||
| 21 | PASS (tar backup, `down -v`, restore, login and media list intact) |
|
||||
| 22 | PASS for all 7 vars (exit 2, `Environment variable X is not set`) |
|
||||
| 23 | PASS (stop 0.4 s, exit 0, `shutting down...` logged) |
|
||||
| 24 | PASS (wrong-port healthcheck reports unhealthy) |
|
||||
| 25 | PARTIAL: mail delivered over STARTTLS to a Mailpit container on the compose network with the CA trusted via SSL_CERT_FILE; no real external host |
|
||||
| 26-29 | NOT RUN (host MTA, Caddy/nginx TLS, TRUSTED_PROXIES) |
|
||||
| 30 | NOT RUN as a before/after upgrade |
|
||||
| `task docker:run` | PASS (healthz 200, graceful stop, separate volume renewcms-data) |
|
||||
|
||||
Notes
|
||||
| Item | Value |
|
||||
| --- | --- |
|
||||
| Image size | 54.5 MB |
|
||||
| Base images and tags used | oven/bun:1-alpine, golang:1.27.1-alpine, alpine:3.22 |
|
||||
| Container uid | 10001 |
|
||||
| Build time (cold) | not measured, layers were partly cached; about 18 s warm |
|
||||
| Docker and Compose versions | 29.8.1, 5.5.1 |
|
||||
| Steps 25-29 run? | 25 partial, 26-29 no |
|
||||
@@ -0,0 +1,71 @@
|
||||
Feature: Publish the RenewCMS image to a registry so an upgrade is "docker compose pull"
|
||||
|
||||
As a maintainer
|
||||
I want one task that builds and pushes a versioned multi-arch image
|
||||
So that site operators upgrade with "docker compose pull && docker compose up -d" and never build from source
|
||||
|
||||
Background:
|
||||
Given there is no CI pipeline on purpose; publishing is a maintainer-run task, like "task release"
|
||||
And the registry is the Gitea registry of git.floriansylvain.fr and REGISTRY_IMAGE defaults to "git.floriansylvain.fr/fsylvain/renewcms"
|
||||
And the Dockerfile and docker-compose.yml from docker-deployment exist and are unchanged except where stated here
|
||||
And Taskfile.yml gains "docker:publish", with variables REGISTRY_IMAGE and VERSION (default: git describe --tags)
|
||||
And "docker:publish" refuses to run, before building anything, when REGISTRY_IMAGE is empty, when VERSION is not an exact tag "vX.Y.Z" or "vX.Y.Z-<pre>", or when the work tree is dirty
|
||||
And the build is "docker buildx build --platform linux/amd64,linux/arm64 --build-arg VERSION=<tag>"; the tag is baked in through the existing version ldflags path, so the image's "renewcms --version" prints the tag
|
||||
And tags pushed for vX.Y.Z-<pre>: "vX.Y.Z-<pre>" and "X.Y.Z-<pre>" and the moving tag "<pre-name>" (for example "beta"); "latest" is NOT moved
|
||||
And tags pushed for vX.Y.Z: "vX.Y.Z", "X.Y.Z", "X.Y", and "latest"
|
||||
And the maintainer authenticates beforehand with "docker login git.floriansylvain.fr"; the task never reads, stores or echoes a password or token, and never passes credentials on a command line
|
||||
And no secret, .env, signing key or JWT_SECRET is copied into the image (already enforced by .dockerignore; checked again here)
|
||||
And no image signing or attestation is part of this slice; the Ed25519 key signs release binaries only
|
||||
And "task docker:publish -- --dry-run" builds for the host platform only with "--load", pushes nothing, needs no login and prints the tags it would push
|
||||
And docker-compose.yml uses "image: ${RENEWCMS_IMAGE:-git.floriansylvain.fr/fsylvain/renewcms:beta}" (the default tag is "beta" because "latest" does not exist until the first stable release; it is changed to "latest" then) and its "build:" block moves to docker-compose.build.yml, used as "docker compose -f docker-compose.yml -f docker-compose.build.yml up -d --build"
|
||||
And docs/docker.md, docs/deployment.md and README.md document pull-based upgrade, the build override, the tag scheme and the maintainer publish steps; no comments in any file
|
||||
And the real push is a manual item: it needs a registry account and network
|
||||
|
||||
Scenario Outline: Tags for a version
|
||||
Given VERSION is "<version>"
|
||||
When "task docker:publish -- --dry-run" runs
|
||||
Then it prints exactly these tags: <tags>
|
||||
|
||||
Examples:
|
||||
| version | tags |
|
||||
| v2.9.0 | v2.9.0, 2.9.0, 2.9, latest |
|
||||
| v2.9.0-beta | v2.9.0-beta, 2.9.0-beta, beta |
|
||||
| v2.9.0-rc.1 | v2.9.0-rc.1, 2.9.0-rc.1, rc |
|
||||
|
||||
Scenario Outline: Refusals before any build
|
||||
Given <condition>
|
||||
When "task docker:publish" runs
|
||||
Then it exits non-zero with a message naming the problem and docker is not invoked
|
||||
|
||||
Examples:
|
||||
| condition |
|
||||
| REGISTRY_IMAGE is empty |
|
||||
| VERSION is "dev" |
|
||||
| VERSION is "2.9.0" (no leading v) |
|
||||
| the work tree has uncommitted changes |
|
||||
|
||||
Scenario: Dry run builds and loads a working image
|
||||
When "task docker:publish -- --dry-run" runs with VERSION "v2.9.0-beta"
|
||||
Then an image is loaded locally for the host platform and no registry is contacted
|
||||
And running it prints "v2.9.0-beta" for --version
|
||||
And it serves GET /healthz as ok with the env from docker.env.example
|
||||
And it runs as the non-root user with the data volume as in docker-deployment
|
||||
|
||||
Scenario: No secrets in the image
|
||||
Given the dry-run image
|
||||
Then "docker history" and a file listing show no .env, no release.key, no JWT_SECRET value and no SMTP password
|
||||
|
||||
Scenario: Compose pulls by default
|
||||
Given docker-compose.yml with RENEWCMS_IMAGE unset
|
||||
Then "docker compose config" shows the image "git.floriansylvain.fr/fsylvain/renewcms:beta" and no build section
|
||||
And with RENEWCMS_IMAGE set to a tag, that tag is used
|
||||
And with docker-compose.build.yml added, the build section is present and the image builds from the local Dockerfile
|
||||
|
||||
Scenario: Upgrade is a pull
|
||||
Given a running stack on tag "v2.8.0-beta" with data in the volume
|
||||
When the operator runs "docker compose pull && docker compose up -d" after a newer tag is pushed
|
||||
Then the container is recreated on the new image and the articles, uploads and database in the volume are intact
|
||||
And GET /v1/settings reports the new version
|
||||
|
||||
Scenario: Docs say the same thing
|
||||
Then docs/docker.md, docs/deployment.md and README.md each show the pull upgrade, the tag scheme and the maintainer flow, and none claims CI exists
|
||||
@@ -0,0 +1,29 @@
|
||||
# QA: Docker image publishing
|
||||
|
||||
Everything runs on the real local Docker daemon, with no registry push. On Windows use MSYS_NO_PATHCONV=1 under Git Bash and put the Docker bin on PATH (memory: docker-windows-gotchas).
|
||||
|
||||
## Automated / scripted
|
||||
1. Refusals: run "task docker:publish" with REGISTRY_IMAGE empty, VERSION=dev, VERSION=2.9.0, and a dirty tree; assert non-zero exit and that no docker build started (compare "docker buildx ls" and the image list before and after).
|
||||
2. Tag table: run "task docker:publish -- --dry-run" with VERSION=v2.9.0, v2.9.0-beta and v2.9.0-rc.1 (the VERSION override is accepted only with --dry-run); compare the printed tags with the feature.
|
||||
3. Dry run: the image is loaded; "docker run --rm <image> --version" prints the tag; start the stack from docker.env.example and check GET /healthz is ok, and "docker exec <ctr> id -u" is not 0.
|
||||
4. Secrets: "docker history --no-trunc" and a file search for .env and release.key inside the image are clean; grep the layers for the sample JWT_SECRET value.
|
||||
5. Compose: "docker compose config" with and without RENEWCMS_IMAGE, and with the build override.
|
||||
6. Upgrade simulation: tag two local images v-old and v-new, run the stack on v-old, write an article, switch RENEWCMS_IMAGE to v-new, "docker compose up -d", and verify the article and the new version.
|
||||
7. Docs: grep the three docs for "docker compose pull", the tag scheme, and the absence of CI claims.
|
||||
8. No credentials: grep Taskfile.yml and docs for token or password literals; the task must not call "docker login".
|
||||
|
||||
## Manual (user performs; needs the registry)
|
||||
- `docker login git.floriansylvain.fr`; "task docker:publish" with a real tag; confirm "docker buildx imagetools inspect <image>:<tag>" lists linux/amd64 and linux/arm64.
|
||||
- On a clean machine, "docker compose pull && docker compose up -d" against the pushed image; confirm /settings shows the new version and the container guidance from container_update_guidance.
|
||||
- Confirm the moving "beta" tag points at the intended digest and that "latest" did not move for a prerelease.
|
||||
|
||||
## Results
|
||||
- 1 Refusals: PASS (coder and hardener runs).
|
||||
- 2 Tag table: PASS (v2.9.0, v2.9.0-beta, v2.9.0-rc.1; re-seen with v2.9.0-beta and v2.9.1-beta, tags "vX, X, beta").
|
||||
- 3 Dry run: PASS (--version prints the tag, /healthz ok, uid 10001).
|
||||
- 4 Secrets: PASS.
|
||||
- 5 Compose: PASS.
|
||||
- 6 Upgrade simulation: PASS. Local images v2.9.0-beta and v2.9.1-beta, stack on the old tag, register + verify (mail captured over TLS by Mailpit) + login, article created (201), RENEWCMS_IMAGE switched, "docker compose up -d" recreated the container on the same volume. Old cookie still accepted, login 200, article 1 intact, GET /v1/settings reports v2.9.1-beta (was v2.9.0-beta).
|
||||
- 7 Docs: PASS.
|
||||
- 8 No credentials: PASS.
|
||||
- Manual items: NOT RUN (need the registry): docker login, real multi-arch push and imagetools inspect, "docker compose pull" against the pushed image, moving beta/latest tag checks, builder failure paths of a real run.
|
||||
@@ -0,0 +1,385 @@
|
||||
Feature: Media cleanup in the SPA: Settings card, purge dialog and Media Duplicates button
|
||||
|
||||
As a RenewCMS owner or admin
|
||||
I want to turn on automatic duplicate purging and to purge duplicates by hand
|
||||
So that identical images are merged without leaving Settings or the Media library
|
||||
|
||||
Background:
|
||||
Given the API of media_purge.feature and media_purge_scheduler.feature is live
|
||||
And every call goes through useApi; 401 is global (/login?redirect=<path>&reason=expired);
|
||||
a 403 on a verified account has no local copy and redirects nowhere
|
||||
And the calls are:
|
||||
| call | body or response |
|
||||
| GET /v1/media/purge-settings | {auto_purge_enabled, auto_purge_interval_days, last_purge_at, last_purge_merged, last_purge_articles_updated, enabled_at, next_purge_at} |
|
||||
| PATCH /v1/media/purge-settings | {"auto_purge_enabled": bool} or {"auto_purge_interval_days": int}, answers the settings above |
|
||||
| GET /v1/media/purge-preview | {groups, copies, articles, list: [{keep_id, filename, url, used_in, merged}], more, fingerprint} |
|
||||
| POST /v1/media/purge | {"fingerprint": "<from the preview>"}, answers {groups_total, groups_purged, merged, articles_updated, failed_groups} |
|
||||
And errors are plain text; 409 carries "The library changed, so nothing was purged. Check the new
|
||||
summary and try again." (stale fingerprint) or "A purge is already running." (busy)
|
||||
And "owner or admin" is session.role in ('owner', 'admin'); only those two roles exist, so any
|
||||
other role is a mocked value in tests
|
||||
And /settings is the verified route of settings_page.feature; the Media cleanup card is its second
|
||||
card, below Version and Updates, 20 px under the previous card, in a page at most 720 px wide
|
||||
And the card is --ui-bg with a 1 px --ui-border, 20 px padding (16 px on mobile), 16 px gap, as
|
||||
drawn in docs/design/source/SettingsFrame.dc.html
|
||||
And the components are Nuxt UI v4 only (UCard, USwitch, UInputNumber, UFormField, UAlert, UButton,
|
||||
UModal, USkeleton, useToast) and design tokens only
|
||||
And PurgeDuplicatesDialog is one component shared by the card and the Media Duplicates tab: props
|
||||
open, preview, status (loading, ready, purging, nothing, error, partial, conflict); it emits
|
||||
purged with the counts; the parent shows the toast
|
||||
And plural rules: "1 group" / "N groups", "1 copy" / "N copies", "1 article" / "N articles"
|
||||
And skeleton placeholders show only after 150 ms of loading
|
||||
And a Partial result is a 200 whose failed_groups is above 0 (the board's 207 is only a label)
|
||||
And dates are shown in the browser's timezone, from last_purge_at and next_purge_at in fixtures
|
||||
And no state here is clock-sensitive
|
||||
|
||||
Rule: The Media cleanup card on /settings
|
||||
|
||||
Scenario: Default: off, never run
|
||||
Given GET /v1/media/purge-settings resolves 200 with auto_purge_enabled false,
|
||||
auto_purge_interval_days 30, last_purge_at null, next_purge_at null
|
||||
And the user is the owner
|
||||
When the user opens /settings
|
||||
Then the card titled "Media cleanup" reads "Merge identical images so your library stays tidy.
|
||||
Articles that use a copy are switched to the most used one."
|
||||
And "Automatic purge" with the helper "Runs in the background. Off by default." and a switch that is off
|
||||
And no "Every" row and no "Next purge" line
|
||||
And the line "Never run" is shown
|
||||
And a divider, then "Purge now" with the helper "Merge every group of duplicates that has an image
|
||||
in use, right away." and an outline button "Purge duplicates" with the merge icon
|
||||
And the card has no Save button
|
||||
|
||||
Scenario: On: 30 days, last run
|
||||
Given GET resolves auto_purge_enabled true, auto_purge_interval_days 30,
|
||||
last_purge_at "2026-06-14T09:00:00Z", last_purge_merged 3, last_purge_articles_updated 5,
|
||||
next_purge_at "2026-07-14T09:00:00Z"
|
||||
When the user opens /settings
|
||||
Then the switch is on
|
||||
And "Every" with the helper "Between 1 and 365 days." and a number input showing 30 with the suffix "days"
|
||||
And the lines "Last purge: 14 Jun 2026 · 3 copies merged · 5 articles updated" and "Next purge: 14 Jul 2026"
|
||||
|
||||
Scenario Outline: The Last purge line is pluralised
|
||||
Given last_purge_at is "2026-06-14T09:00:00Z", last_purge_merged <merged> and last_purge_articles_updated <articles>
|
||||
When the card renders
|
||||
Then the line reads "<line>"
|
||||
|
||||
Examples:
|
||||
| merged | articles | line |
|
||||
| 3 | 5 | Last purge: 14 Jun 2026 · 3 copies merged · 5 articles updated |
|
||||
| 1 | 1 | Last purge: 14 Jun 2026 · 1 copy merged · 1 article updated |
|
||||
| 0 | 0 | Last purge: 14 Jun 2026 · 0 copies merged · 0 articles updated |
|
||||
|
||||
Scenario: Turning the switch on saves at once
|
||||
Given the Default state
|
||||
When the user turns the switch on
|
||||
Then PATCH /v1/media/purge-settings is sent with exactly {"auto_purge_enabled": true}
|
||||
And while it is pending "Saving…" with loader-circle sits in the card header and the controls stay usable
|
||||
When it resolves 200 with auto_purge_interval_days 30 and next_purge_at "2026-11-04T09:00:00Z"
|
||||
Then "Saving…" is replaced by "Saved" with a check, in the success colour, in the card header
|
||||
And the "Every" row shows 30 and "Next purge: 4 Nov 2026" is shown
|
||||
And "Saved" is announced politely and disappears after 2 s
|
||||
|
||||
Scenario: Turning the switch off saves at once and hides the schedule
|
||||
Given the On state
|
||||
When the user turns the switch off
|
||||
Then PATCH is sent with exactly {"auto_purge_enabled": false}
|
||||
And on 200 with next_purge_at null the "Every" row and the "Next purge" line are hidden
|
||||
And the "Last purge" line is unchanged
|
||||
|
||||
Scenario Outline: The days field saves on blur or Enter, once
|
||||
Given the On state and the days field holds 30
|
||||
When the user types "<typed>" and <commit>
|
||||
Then PATCH is sent exactly once with {"auto_purge_interval_days": <sent>}
|
||||
And on 200 "Saved" shows in the header and "Next purge" follows the response's next_purge_at
|
||||
|
||||
Examples:
|
||||
| typed | commit | sent |
|
||||
| 45 | leaves the field | 45 |
|
||||
| 45 | presses Enter | 45 |
|
||||
| 1 | presses Enter | 1 |
|
||||
| 365 | leaves the field | 365 |
|
||||
|
||||
Scenario: Enter then the resulting blur is one request
|
||||
Given the On state
|
||||
When the user types 45, presses Enter and the field then loses focus
|
||||
Then exactly one PATCH is sent
|
||||
|
||||
Scenario: An unchanged value sends nothing
|
||||
Given the On state and the days field holds 30
|
||||
When the user focuses the field and leaves it without changing it
|
||||
Then no PATCH is sent and no "Saving…" shows
|
||||
|
||||
Scenario Outline: Validation error: nothing is sent
|
||||
Given the On state
|
||||
When the user types "<typed>" and leaves the field or presses Enter
|
||||
Then no PATCH is sent
|
||||
And the field keeps "<typed>" and gets the error border
|
||||
And under the field, with circle-alert, the message "Enter a whole number of days between 1 and 365." shows
|
||||
And no "Saving…" and no "Saved" show
|
||||
|
||||
Examples:
|
||||
| typed |
|
||||
| |
|
||||
| 0 |
|
||||
| 366 |
|
||||
| 400 |
|
||||
| 1.5 |
|
||||
|
||||
Scenario: A validation error clears when the value becomes valid
|
||||
Given the Validation error state with "400"
|
||||
When the user types 60 and presses Enter
|
||||
Then the message disappears and PATCH is sent with {"auto_purge_interval_days": 60}
|
||||
|
||||
Scenario Outline: Save failed: the control reverts
|
||||
Given the On state with 30 days
|
||||
When the user <action> and PATCH answers <failure>
|
||||
Then the control returns to its previous value (<previous>)
|
||||
And "Couldn't save. Try again in a moment." shows under the field (under the "Automatic purge" row for the switch)
|
||||
And no "Saved" shows
|
||||
|
||||
Examples:
|
||||
| action | failure | previous |
|
||||
| types 45 and presses Enter | 500 | 30 |
|
||||
| types 45 and presses Enter | a network error | 30 |
|
||||
| turns the switch off | 500 | on |
|
||||
|
||||
Scenario: The save-failed message goes away on the next successful save
|
||||
Given the Save failed state
|
||||
When the user types 45 and presses Enter and PATCH answers 200
|
||||
Then the failure message is gone and "Saved" shows
|
||||
|
||||
Scenario: Role changed (403 on PATCH)
|
||||
Given the On state for an owner who has been changed to a role that is not owner or admin
|
||||
When the user turns the switch off and PATCH answers 403
|
||||
Then the switch returns to on, with no message, no toast and no redirect
|
||||
And the session is refreshed and the card switches to the Read-only state
|
||||
|
||||
Scenario: Read-only (non-admin)
|
||||
Given the session role is not owner or admin
|
||||
When the user opens /settings
|
||||
Then GET /v1/media/purge-settings is not sent
|
||||
And the switch is off, the days input shows 30 and the line "Never run" shows, as placeholders
|
||||
And the switch and the days input are disabled
|
||||
And an info alert reads "Only owners and admins can change this."
|
||||
And the "Purge now" divider, row and button are not rendered
|
||||
And no PATCH can be sent from the card
|
||||
|
||||
Scenario: Loading skeleton after 150 ms
|
||||
Given GET /v1/media/purge-settings has not resolved
|
||||
When the user opens /settings
|
||||
Then during the first 150 ms the card shows no skeleton
|
||||
And after 150 ms a skeleton replaces the card body (title bar, two text bars, switch row, schedule bar, divider, button row), the card is aria-busy="true" and the button is disabled
|
||||
When it resolves
|
||||
Then the Default or On state replaces it and aria-busy is gone
|
||||
|
||||
Scenario Outline: The settings could not be loaded
|
||||
Given GET /v1/media/purge-settings <failure>
|
||||
When the user opens /settings
|
||||
Then the card shows an error alert "Couldn't reach RenewCMS. Check your connection and try again." with "Try again" (rotate-cw)
|
||||
And the Version and Updates cards are unaffected
|
||||
When the user activates "Try again"
|
||||
Then the loading state shows and GET is sent again
|
||||
|
||||
Examples:
|
||||
| failure |
|
||||
| answers 500 |
|
||||
| fails (network) |
|
||||
|
||||
Scenario: Offline
|
||||
Given navigator.onLine is false
|
||||
When the user opens /settings
|
||||
Then a banner reads "You're offline. Changing automatic purge and purging duplicates need a connection."
|
||||
And the switch, the days input and "Purge duplicates" are disabled (40 % opacity)
|
||||
And nothing is sent when they are activated
|
||||
|
||||
Scenario: 401 is global
|
||||
Given any call of the card answers 401
|
||||
Then the user goes to /login?redirect=/settings&reason=expired and the card shows no copy of its own
|
||||
|
||||
Rule: PurgeDuplicatesDialog opened from "Purge duplicates" in the card
|
||||
|
||||
Background:
|
||||
Given an owner on /settings and the card in the On state
|
||||
|
||||
Scenario: Opening shows the preview loading
|
||||
When the user activates "Purge duplicates"
|
||||
Then GET /v1/media/purge-preview is sent
|
||||
And a dialog opens (UModal, sm:max-w-md, a bottom sheet on mobile) titled "Purge duplicates?" with the description
|
||||
"Articles that use a duplicate will be switched to the most used copy of that image, and the other copies will be deleted. This can't be undone."
|
||||
And focus is on the first button, "Cancel"
|
||||
And after 150 ms skeleton rows replace the summary and list, and the confirm button, labelled
|
||||
"Purge duplicates", is disabled
|
||||
|
||||
Scenario: Confirm: summary, up to 5 groups and the link
|
||||
Given the preview is groups 4, copies 9, articles 12, a list of 4 groups, more 0
|
||||
When it resolves
|
||||
Then the summary reads "4 groups · 9 copies will be deleted · 12 articles will be updated"
|
||||
And a bordered list shows a row per group: a 40 x 30 thumbnail of its url, the filename, "Kept · used in N articles"
|
||||
and, right-aligned, "N copies merged"
|
||||
And no "and N more" line
|
||||
And the link "Review in Duplicates" (arrow-right) points to /media?filter=duplicates
|
||||
And the buttons are "Cancel" and the danger confirm "Purge 4 groups"
|
||||
|
||||
Scenario Outline: The summary and rows are pluralised
|
||||
Given the preview is groups <g>, copies <c>, articles <a> with one group kept in <u> article(s) and <m> merged
|
||||
Then the summary reads "<summary>"
|
||||
And the confirm reads "<confirm>"
|
||||
And the row reads "Kept · used in <ku>" and "<km>"
|
||||
|
||||
Examples:
|
||||
| g | c | a | u | m | summary | confirm | ku | km |
|
||||
| 4 | 9 | 12 | 4 | 2 | 4 groups · 9 copies will be deleted · 12 articles will be updated | Purge 4 groups | 4 articles | 2 copies merged |
|
||||
| 1 | 1 | 2 | 2 | 1 | 1 group · 1 copy will be deleted · 2 articles will be updated | Purge 1 group | 2 articles | 1 copy merged |
|
||||
| 1 | 1 | 1 | 1 | 1 | 1 group · 1 copy will be deleted · 1 article will be updated | Purge 1 group | 1 article | 1 copy merged |
|
||||
|
||||
Scenario: Confirm: many groups
|
||||
Given the preview is groups 8, copies 15, articles 24, a list of 5 groups and more 3
|
||||
When it resolves
|
||||
Then exactly 5 rows show, followed by "and 3 more groups"
|
||||
And the confirm reads "Purge 8 groups"
|
||||
|
||||
Scenario: "and 1 more group" is singular
|
||||
Given the preview has more 1
|
||||
Then the line reads "and 1 more group"
|
||||
|
||||
Scenario: Nothing to purge
|
||||
Given the preview is groups 0 with an empty list
|
||||
When it resolves
|
||||
Then the dialog shows an info icon, "Nothing to purge" and "No duplicate is used in an article, so there is nothing to merge."
|
||||
And the only button is "Close"; there is no confirm, no summary and no link
|
||||
And "Close" closes the dialog and sends nothing else
|
||||
|
||||
Scenario: The preview could not be loaded
|
||||
Given GET /v1/media/purge-preview answers 500 or fails
|
||||
Then the dialog shows the error notice "Couldn't purge the duplicates. Nothing was changed. Try again in a moment."
|
||||
And the confirm is disabled and offers no purge until the preview loads again
|
||||
|
||||
Scenario: Cancelling changes nothing
|
||||
Given the Confirm state
|
||||
When the user activates "Cancel", presses Escape or clicks the overlay
|
||||
Then the dialog closes, no POST is sent and focus returns to "Purge duplicates"
|
||||
|
||||
Scenario: The fingerprint goes from the preview to the purge
|
||||
Given the preview carries fingerprint "ab12"
|
||||
When the user activates "Purge 4 groups"
|
||||
Then POST /v1/media/purge is sent once with exactly {"fingerprint": "ab12"}
|
||||
|
||||
Scenario: In progress: the dialog cannot be dismissed
|
||||
Given POST /v1/media/purge has not resolved
|
||||
Then the confirm reads "Purging…" with loader-circle and is disabled
|
||||
And "Cancel" is disabled
|
||||
And Escape, a click on the overlay and a swipe-down do nothing
|
||||
|
||||
Scenario: Success: the dialog closes, a toast shows and Last purge updates in place
|
||||
Given the preview was groups 4, copies 9, articles 12
|
||||
When POST answers 200 with {groups_total 4, groups_purged 4, merged 9, articles_updated 12, failed_groups 0}
|
||||
Then the dialog closes, the dialog emits purged with merged 9 and articles_updated 12
|
||||
And a toast with circle-check shows for 5 s reading "Duplicates purged" / "9 copies merged. 12 articles updated."
|
||||
And GET /v1/media/purge-settings is sent again
|
||||
And with last_purge_at "2026-10-05T09:00:00Z", last_purge_merged 9, last_purge_articles_updated 12 the line reads
|
||||
"Last purge: 5 Oct 2026 · 9 copies merged · 12 articles updated", without a page reload
|
||||
And focus returns to "Purge duplicates"
|
||||
|
||||
Scenario: Success with automatic purge on moves Next purge forward
|
||||
Given automatic purge is on with interval 30
|
||||
When a purge succeeds and the refreshed settings carry next_purge_at "2026-11-04T09:00:00Z"
|
||||
Then "Next purge: 4 Nov 2026" shows, and with automatic purge off no "Next purge" line shows
|
||||
|
||||
Scenario: The toast is pluralised
|
||||
When POST answers 200 with merged 1, articles_updated 1 and failed_groups 0
|
||||
Then the toast reads "Duplicates purged" / "1 copy merged. 1 article updated."
|
||||
|
||||
Scenario Outline: Error: the dialog stays open and confirm retries
|
||||
Given the Confirm state with fingerprint "ab12"
|
||||
When POST answers <failure>
|
||||
Then an error notice (soft alert, circle-x) reads "Couldn't purge the duplicates. Nothing was changed. Try again in a moment." above the summary
|
||||
And the dialog stays open with the summary and list unchanged, and "Cancel" and "Purge 4 groups" enabled
|
||||
When the user activates "Purge 4 groups" again
|
||||
Then POST is sent again with {"fingerprint": "ab12"}
|
||||
And no toast shows and "Last purge" is unchanged
|
||||
|
||||
Examples:
|
||||
| failure |
|
||||
| 500 |
|
||||
| a network error |
|
||||
|
||||
Scenario: Partial: some groups failed
|
||||
Given the preview was 4 groups
|
||||
When POST answers 200 with {groups_total 4, groups_purged 3, merged 8, articles_updated 10, failed_groups 1}
|
||||
Then a warning notice (soft alert, triangle-alert) reads "Some groups couldn't be merged. 3 of 4 groups were purged. Try again to finish."
|
||||
And GET /v1/media/purge-preview is sent again and the summary and list refresh to the remaining groups
|
||||
And GET /v1/media/purge-settings is sent again so "Last purge" refreshes
|
||||
And the buttons are "Close" and "Retry" (rotate-cw, danger)
|
||||
And no toast shows and the dialog does not close
|
||||
When the user activates "Retry"
|
||||
Then POST is sent with the refreshed fingerprint
|
||||
|
||||
Scenario: Partial that leaves nothing to purge
|
||||
Given a Partial result and the refreshed preview has groups 0
|
||||
Then the dialog shows "Nothing to purge" with "Close"
|
||||
|
||||
Scenario: Conflict (409): the library changed
|
||||
Given the Confirm state with fingerprint "ab12"
|
||||
When POST answers 409 with "The library changed, so nothing was purged. Check the new summary and try again."
|
||||
Then a warning notice reads "The library changed, so nothing was purged. Check the new summary and try again."
|
||||
And GET /v1/media/purge-preview is sent again and the summary refreshes (for example 3 groups · 7 copies · 10 articles)
|
||||
And the buttons are "Cancel" and "Purge 3 groups"
|
||||
And no toast shows and "Last purge" is unchanged
|
||||
When the user activates "Purge 3 groups"
|
||||
Then POST is sent with the new fingerprint
|
||||
|
||||
Scenario: Conflict (409): a purge is already running
|
||||
When POST answers 409 with "A purge is already running."
|
||||
Then a warning notice shows the server's text, the preview refreshes and the dialog stays open
|
||||
|
||||
Scenario: Offline
|
||||
Given navigator.onLine is false
|
||||
Then "Purge duplicates" in the card is disabled and the dialog cannot be opened
|
||||
|
||||
Rule: The Duplicates toolbar button on /media
|
||||
|
||||
Scenario: The button shows on the Duplicates tab for an owner or admin
|
||||
Given the user is owner or admin and /media?filter=duplicates holds at least one duplicate group
|
||||
Then the secondary (outline) button "Purge duplicates" with the merge icon shows after the sort menu
|
||||
and before the view toggle in the toolbar, as MediaFrame.dc.html draws it
|
||||
And on a stacked mobile toolbar it is the last row, full width and 44 px high, under the sort and view row
|
||||
|
||||
Scenario Outline: The button is hidden
|
||||
Given <condition>
|
||||
Then the toolbar has no "Purge duplicates" button
|
||||
|
||||
Examples:
|
||||
| condition |
|
||||
| the filter is All |
|
||||
| the filter is Unused |
|
||||
| the Duplicates tab has no groups ("No duplicates" shows) |
|
||||
| the session role is not owner or admin |
|
||||
|
||||
Scenario: Offline
|
||||
Given navigator.onLine is false on the Duplicates tab
|
||||
Then the button is disabled, the existing offline banner of Media shows and nothing opens
|
||||
|
||||
Scenario: The button opens the same dialog
|
||||
When the user activates "Purge duplicates" on the Duplicates tab
|
||||
Then GET /v1/media/purge-preview is sent and PurgeDuplicatesDialog opens in the loading state
|
||||
And every status, notice, button and request of the previous Rule behaves identically
|
||||
|
||||
Scenario: After a purge from Media the list reloads like after a merge
|
||||
Given the Duplicates tab lists 4 groups
|
||||
When a purge succeeds (200, failed_groups 0)
|
||||
Then the toast "Duplicates purged" / "9 copies merged. 12 articles updated." shows
|
||||
And the list and the tab counts are requested again, as after "Merge into this one"
|
||||
And when no group is left "No duplicates" shows and the button is gone
|
||||
|
||||
Scenario: Partial from Media reloads the list when the dialog closes
|
||||
When the purge answers 200 with failed_groups 1 and the user activates "Close"
|
||||
Then the list and the tab counts are requested again
|
||||
|
||||
Scenario: "Review in Duplicates" from Media only closes the dialog
|
||||
Given the dialog is open on /media?filter=duplicates
|
||||
When the user follows "Review in Duplicates"
|
||||
Then the dialog closes and the route is unchanged
|
||||
|
||||
Scenario: "Merge into this one" is unchanged
|
||||
Given the Duplicates tab lists a group
|
||||
Then each group still shows "Merge into this one" and it still sends POST /v1/media/merge with {keep_id, merge_ids}
|
||||
@@ -0,0 +1,102 @@
|
||||
# QA: Media cleanup in the SPA (Settings card, purge dialog, Media Duplicates button)
|
||||
|
||||
No Go test: the API contract is already covered by media_purge.qa.md and media_purge_scheduler.qa.md. This slice has no Go change. The SPA never reaches `persistence`, `mailer` or `gorm`, and every `/v1` call goes through `useApi`.
|
||||
|
||||
Settled decisions: a non-admin sees the Read-only card with placeholders and no settings GET; Partial is a 200 with `failed_groups > 0`; a 403 on PATCH reverts silently, refreshes the session and switches the card to Read-only; dates use the browser timezone.
|
||||
|
||||
## Automated SPA (Vitest + Vue Test Utils, `task test:web`)
|
||||
|
||||
Suggested files, next to the existing view specs: `web/src/views/__tests__/SettingsMediaCleanup.spec.ts`, `web/src/views/__tests__/PurgeDuplicatesDialog.spec.ts`, `web/src/views/__tests__/MediaPurgeEntry.spec.ts`. Reuse `mediaTestKit.ts` and the existing Settings/Media mount helpers.
|
||||
|
||||
Setup:
|
||||
- Mock `useApi().request` per route and method: GET and PATCH `/v1/media/purge-settings`, GET `/v1/media/purge-preview`, POST `/v1/media/purge`. Assert the exact path, method and JSON body of every call.
|
||||
- Mock the session store: `role: 'owner'`, `role: 'admin'`, and for the non-admin cases a role cast to `UserRole` (for example `'editor' as never`) or undefined.
|
||||
- Fake timers for the 150 ms skeleton, the 2 s "Saved" and the 5 s toast. No test freezes the system date: every date comes from a fixture ISO string with a midday UTC time (for example `2026-06-14T09:00:00Z`), formatted by the SPA.
|
||||
- Offline: set `navigator.onLine` to false and dispatch `offline`, or set `isOffline` from `useApi`.
|
||||
- Reject with `ApiError(status, undefined, body)` and `NetworkError` as in the existing specs.
|
||||
- Fixtures reuse the board data: settings `last 14 Jun 2026 / 3 copies / 5 articles`, `next 14 Jul 2026`; preview 4 groups / 9 copies / 12 articles with floorboards.jpg (4, 2), IMG_4471.jpg (2, 1), kitchen-after.webp (3, 3), logo-renew.svg (5, 3); many: 8 / 15 / 24 with lime-plaster.jpg (2, 1) and more 3.
|
||||
|
||||
Cases (one `it` per scenario or Examples row; `it.each` for outlines):
|
||||
|
||||
Settings card
|
||||
1. Default: one GET on open, title, description, label, helper, switch off, no "Every", no "Next purge", "Never run", divider, "Purge now" row, button "Purge duplicates", no Save button.
|
||||
2. On: switch on, "Every" 30 with "days" suffix and helper, both lines; the Last purge pluralisation rows (3/5, 1/1, 0/0).
|
||||
3. Toggle on: PATCH body is exactly `{ auto_purge_enabled: true }`; "Saving…" while pending; then "Saved" in a polite live region; gone after 2 s; the "Every" row and "Next purge" follow the response.
|
||||
4. Toggle off: body exactly `{ auto_purge_enabled: false }`; schedule hidden, Last purge kept.
|
||||
5. Days commit, table-driven (45 blur, 45 Enter, 1 Enter, 365 blur): exactly one PATCH with `{ auto_purge_interval_days: n }`. Enter then blur is one PATCH. Unchanged value sends nothing.
|
||||
6. Validation, table-driven ("", 0, 366, 400, 1.5): no `request` call, field keeps the typed text, error message "Enter a whole number of days between 1 and 365." under the field, error border; a valid value afterwards clears it and sends.
|
||||
7. Save failed (PATCH 500, then NetworkError, for the days field and for the toggle): value reverts, the "Couldn't save. Try again in a moment." copy shows in the right place, no "Saved"; a later success clears it.
|
||||
8. Read-only: role not owner/admin leads to no call to `/v1/media/purge-settings`, placeholders (switch off, 30, "Never run"), disabled switch and input, the info alert "Only owners and admins can change this.", no divider, no "Purge now" row, no button.
|
||||
9. 403 on PATCH: the control reverts, no toast, no redirect, `session.refresh` is called, and a role that is no longer owner/admin yields the Read-only card.
|
||||
10. Loading: 149 ms no skeleton, 150 ms skeleton and `aria-busy="true"`, button disabled; resolve and assert both are gone.
|
||||
11. Settings load failure (500, network): the inline alert, "Try again" reloads.
|
||||
12. Offline: banner text "You're offline. Changing automatic purge and purging duplicates need a connection.", switch, input and button disabled, nothing sent.
|
||||
13. 401 on any call: `redirectToLogin` path used, no local copy in the card.
|
||||
|
||||
Dialog
|
||||
14. Open: GET preview sent, title and description, focus on "Cancel", skeleton after 150 ms (not before), confirm disabled with the label "Purge duplicates".
|
||||
15. Confirm: summary, rows (thumbnail src from `url`, filename, "Kept · used in N articles", "N copies merged"), link `href="/media?filter=duplicates"` with "Review in Duplicates", buttons, confirm label "Purge 4 groups". Singular rows (1/1/2, 1/1/1).
|
||||
16. Many groups: 5 rows then "and 3 more groups"; "and 1 more group" for more 1.
|
||||
17. Nothing: groups 0 gives "Nothing to purge" copy and a single "Close"; no confirm, summary or link.
|
||||
18. Cancel, Escape and overlay close without POST and return focus to the opener.
|
||||
19. Fingerprint: POST body is exactly `{ fingerprint: '<preview fingerprint>' }`, sent once.
|
||||
20. In progress (POST unresolved): label "Purging…" with the loader icon, confirm and Cancel disabled; Escape, overlay click and a close attempt do nothing.
|
||||
21. Success: dialog closes, `purged` emitted with `{ merged, articles_updated }`, toast "Duplicates purged" / "9 copies merged. 12 articles updated." with a 5 s duration, a second GET `/v1/media/purge-settings`, "Last purge" and "Next purge" updated without remount, focus back on the opener; singular toast.
|
||||
22. Error (500 and network): error notice, dialog open, summary unchanged, confirm retries with the same fingerprint, no toast, Last purge unchanged.
|
||||
23. Partial (200, failed_groups 1, groups_total 4, groups_purged 3): the warning text with "3 of 4", a second GET preview, summary refreshed, settings refetched, buttons "Close" and "Retry", no toast, a Retry POST uses the new fingerprint; a refreshed empty preview gives "Nothing to purge".
|
||||
24. Conflict (409 with the conflict text): warning, preview refetched, summary and confirm label refreshed, no toast; the next confirm sends the new fingerprint. Busy 409 text shows the server's text.
|
||||
25. Preview failure: error notice and no purge possible until it loads.
|
||||
26. Offline: the button is disabled and no dialog opens.
|
||||
|
||||
Media
|
||||
27. Button presence table: shown for owner and admin on the Duplicates tab with at least one group; hidden on All, on Unused, with no groups, and for a non-owner/admin role.
|
||||
28. Offline: disabled, the existing banner shows, nothing opens.
|
||||
29. DOM order of the toolbar: Sort, then "Purge duplicates", then the view toggle; the button carries the mobile classes for full width and a 44 px height (`h-11`) in the stacked toolbar.
|
||||
30. Opening from Media: same dialog statuses as cases 14 to 25, driven through the same mocks.
|
||||
31. After a successful purge: the toast, and the media list and counts are requested again (same assertion helper as the merge success case in `MediaDuplicates.spec.ts`); the last group leaving shows "No duplicates" and removes the button. Partial then "Close" reloads the list.
|
||||
32. "Review in Duplicates" from Media closes the dialog and leaves the route unchanged.
|
||||
33. "Merge into this one" regression: the existing merge cases still pass unchanged.
|
||||
|
||||
Amendments to existing specs, in the same change: `SettingsView.spec.ts` and the other Settings specs must tolerate a second card (any assertion about "no switch or input" is scoped to the Version card). `features/settings_page.feature` wording ("one card") is updated at its next amendment, not in this slice.
|
||||
|
||||
## Visual (`task visual:check -- settings` and `task visual:check -- media`, desktop and mobile, light and dark)
|
||||
|
||||
References: `docs/design/screens/settings.md` ("Media cleanup (second card)"), `docs/design/screens/media.md`, `docs/design/source/RenewCMS Screens Media cleanup.dc.html` and the frames `SettingsFrame.dc.html`, `MediaFrame.dc.html` (exact values). Add every state below to `web/visual/states.ts`, with the mocked `/v1` calls the state needs. The gate is hard: every capture within 10 %. Nothing is skipped: no state depends on the clock, because dates come from fixture timestamps (midday UTC, so the day is stable).
|
||||
|
||||
Board sample data to reuse:
|
||||
- Last purge fixture: `last_purge_at` 14 Jun 2026, merged 3, articles 5; "on" has 30 days and next 14 Jul 2026 (`next_purge_at` "2026-07-14T09:00:00Z").
|
||||
- Success fixtures: last 5 Oct 2026, 9 copies, 12 articles. "Success" has automatic purge off (no Next purge); "Success, automatic purge on" has 30 days and next 4 Nov 2026. Both show the toast "Duplicates purged" / "9 copies merged. 12 articles updated."
|
||||
- Preview groups (ids 12, 7, 2, 4, 11): floorboards.jpg (used 4, merged 2), IMG_4471.jpg (2, 1), kitchen-after.webp (3, 3), logo-renew.svg (5, 3, SVG with alpha), lime-plaster.jpg (2, 1). Default preview: 4 groups, 9 copies, 12 articles with the first four. Many: 8 / 15 / 24 with all five and more 3. Partial: refreshed preview 1 group / 1 copy / 2 articles (IMG_4471.jpg), POST answers 200 `groups_total 4, groups_purged 3, failed_groups 1`. Conflict: POST 409, refreshed preview 3 groups / 7 copies / 10 articles (floorboards.jpg, kitchen-after.webp, logo-renew.svg).
|
||||
- Thumbnails: reuse the thumbnail fixtures the existing media states draw.
|
||||
- Frame values: page max width 720, 20 px between cards; card padding 20 (16 mobile), 16 gap; title 15/24 semibold; switch 36 x 20 with a 44 px hit area on mobile; days input 140 px wide (100 % on mobile), 40 px high (44 mobile), IBM Plex Mono 13 px (16 px on mobile), suffix "days" in mono; rows stack on mobile; "Purge duplicates" is 32 px high (44 mobile, full width); dialog 448 px wide, 140 px from the top on desktop, bottom sheet on mobile with the buttons column-reversed; toast 356 px wide at the bottom right (full width on mobile).
|
||||
|
||||
Settings screen, group "Settings: Media cleanup":
|
||||
- [ ] Default: off, never run
|
||||
- [ ] On: 30 days, last run
|
||||
- [ ] Saving (PATCH hangs, days field focused)
|
||||
- [ ] Saved
|
||||
- [ ] Save failed
|
||||
- [ ] Validation error (days field holds "400")
|
||||
- [ ] Read-only (non-admin) (mocked role, placeholders, no GET)
|
||||
- [ ] Loading skeleton
|
||||
- [ ] Offline
|
||||
|
||||
Settings screen, group "Purge duplicates: dialog":
|
||||
- [ ] Loading preview, [ ] Confirm, [ ] Confirm: many groups, [ ] In progress, [ ] Nothing to purge, [ ] Error, [ ] Partial failure, [ ] Conflict (409)
|
||||
|
||||
Settings screen, group "Results and feedback":
|
||||
- [ ] Success, [ ] Success, automatic purge on (the setup holds the toast open so its 5 s timeout cannot end before the capture)
|
||||
|
||||
Media screen, group "Media: Duplicates tab entry point":
|
||||
- [ ] Duplicates tab with the button, [ ] Dialog from Media, [ ] Member: button hidden (mocked role), [ ] Offline (button disabled, existing banner copy)
|
||||
|
||||
Reuse the existing `mediaDuplicatesApi` fixtures for the Duplicates tab and add the purge-preview mock.
|
||||
|
||||
Not covered by screenshots, check by hand: Tab order (switch, days field, "Purge duplicates"; in the dialog Cancel then confirm then the link); focus ring on the switch and field; Enter and Escape in the dialog; focus return to the opener after close; the polite announcement of "Saving…" then "Saved"; hover states; reduced motion on the skeleton and the loader icons; swipe-down is ignored while purging on a touch device.
|
||||
|
||||
## Manual verification (real API, `task dev`)
|
||||
|
||||
- With an owner, turn automatic purge on, set 7 days, reload /settings: the values persist and "Next purge" matches `next_purge_at`.
|
||||
- Upload two identical images, use the copy in an article, press "Purge duplicates": the preview matches, the purge merges them, the toast shows, "Last purge" updates, the file of the copy is gone from `UPLOAD_DIR`.
|
||||
- Open the dialog, upload another duplicate from a second tab, confirm: the Conflict notice and the refreshed summary show.
|
||||
- Confirm the embedded SPA (built without `-tags nofrontend`) serves /settings and /media?filter=duplicates on a hard refresh, since Vitest never exercises the embedded frontend.
|
||||
@@ -0,0 +1,432 @@
|
||||
Feature: Owners and admins can merge duplicate images in use, with persisted purge settings
|
||||
|
||||
As a RenewCMS owner or admin
|
||||
I want to see what a duplicate purge would do, run it, and keep the automatic-purge settings
|
||||
So that articles end up pointing at the most used copy of an image and the other copies are removed
|
||||
|
||||
Background:
|
||||
Given internal/domain/purge defines the entity Settings with AutoPurgeEnabled (default false),
|
||||
AutoPurgeIntervalDays (default 30, valid 1 to 365), LastPurgeAt, LastPurgeMerged,
|
||||
LastPurgeArticlesUpdated and EnabledAt (unset or 0 by default), plus a Repository
|
||||
interface with Get, Save and SaveLastRun, and the sentinels ErrInvalidPurgeInterval, ErrPurgeConflict
|
||||
(the library changed since the fingerprint) and ErrPurgeBusy (a run is already in progress),
|
||||
all asserted with errors.Is
|
||||
And Repository.Get returns the defaults when nothing was ever saved (a single-row table)
|
||||
And the use cases GetPurgeSettings, UpdatePurgeSettings, PreviewPurge and RunPurge depend only
|
||||
on domain interfaces, take no HTTP types and are reusable by a scheduler
|
||||
And the use cases receive an injectable clock and an injected *log.Logger
|
||||
And an image's "used in" is the count of articles, online or draft, whose body contains the
|
||||
image's stored name, read from article bodies each time, as in ListMedia
|
||||
And a duplicate group is the set of images sharing one non-empty sha256, as from
|
||||
image.Repository.ListDuplicates
|
||||
And the purge plan is: a group is planned only if at least one copy is used in an article, the
|
||||
kept copy is the most used one (ties: oldest created_at, then lowest id), and every other
|
||||
copy of the group, used or unused, is merged into it. A group with no used copy is skipped
|
||||
And the fingerprint is a hex SHA-256 of the planned groups ordered by kept id, with their kept
|
||||
id and sorted merged ids; an empty plan has a fixed fingerprint
|
||||
And useCases.MergeMediaResult keeps ArticlesUpdated and Merged, and gains UpdatedArticleIDs,
|
||||
the ids of the articles actually rewritten (empty when the merge was rolled back or failed)
|
||||
And RunPurge merges each group with the MergeMedia semantics and unions UpdatedArticleIDs of the
|
||||
fully merged groups; it never recomputes the articles from the plan
|
||||
And next_purge_at is max(last_purge_at, enabled_at) + auto_purge_interval_days when automatic
|
||||
purge is on and null when it is off; with no last_purge_at it is enabled_at + interval
|
||||
And /v1/media/purge-settings, /v1/media/purge-preview and /v1/media/purge are mounted in
|
||||
api/router.go inside the NewMediaRouter chain (Verifier, Authenticator, RequireCurrentToken,
|
||||
RequireVerifiedUser), then a group guarded by the new middleware.RequireOwnerOrAdmin, and
|
||||
registered before the /{id} routes
|
||||
And RequireOwnerOrAdmin is an allowlist: only the roles owner and admin pass; any other value,
|
||||
including an empty role, answers 403 with apperr.OwnerOrAdminRequiredErrorMessage
|
||||
("Only owners and admins can do this.") as plain text from http.Error
|
||||
And a stale fingerprint answers 409 with apperr.PurgeConflictErrorMessage ("The library changed,
|
||||
so nothing was purged. Check the new summary and try again.") and a busy run answers 409 with
|
||||
apperr.PurgeBusyErrorMessage ("A purge is already running.")
|
||||
And the purge settings range error is apperr.PurgeIntervalErrorMessage ("Enter a whole number of
|
||||
days between 1 and 365.")
|
||||
And request and response bodies are JSON with snake_case fields; errors are plain text
|
||||
And the request body of PATCH is validated by validate tags and validator.Validate.Struct
|
||||
|
||||
Scenario Outline: Every purge route refuses callers that are not allowed
|
||||
When the client sends <method> <path> <credentials>
|
||||
Then the response status is <status>
|
||||
And the body is <body>
|
||||
And no purge setting, image or article is read or changed
|
||||
|
||||
Examples:
|
||||
| method | path | credentials | status | body |
|
||||
| GET | /v1/media/purge-settings | no cookie | 401 | the existing jwtauth response (status only) |
|
||||
| PATCH | /v1/media/purge-settings | no cookie | 401 | the existing jwtauth response (status only) |
|
||||
| GET | /v1/media/purge-preview | no cookie | 401 | the existing jwtauth response (status only) |
|
||||
| POST | /v1/media/purge | no cookie | 401 | the existing jwtauth response (status only) |
|
||||
| GET | /v1/media/purge-settings | a stale token_version cookie | 401 | apperr.UnauthorizedErrorMessage |
|
||||
| GET | /v1/media/purge-settings | cookie of an unverified owner | 403 | apperr.VerificationRequiredErrorMessage |
|
||||
| GET | /v1/media/purge-settings | cookie of a verified user, role "editor" | 403 | apperr.OwnerOrAdminRequiredErrorMessage |
|
||||
| PATCH | /v1/media/purge-settings | cookie of a verified user, role "editor" | 403 | apperr.OwnerOrAdminRequiredErrorMessage |
|
||||
| GET | /v1/media/purge-preview | cookie of a verified user, role "editor" | 403 | apperr.OwnerOrAdminRequiredErrorMessage |
|
||||
| POST | /v1/media/purge | cookie of a verified user, role "editor" | 403 | apperr.OwnerOrAdminRequiredErrorMessage |
|
||||
| GET | /v1/media/purge-settings | cookie of a verified user, empty role | 403 | apperr.OwnerOrAdminRequiredErrorMessage |
|
||||
| PATCH | /v1/media/purge-settings | cookie of a verified user, empty role | 403 | apperr.OwnerOrAdminRequiredErrorMessage |
|
||||
| GET | /v1/media/purge-preview | cookie of a verified user, empty role | 403 | apperr.OwnerOrAdminRequiredErrorMessage |
|
||||
| POST | /v1/media/purge | cookie of a verified user, empty role | 403 | apperr.OwnerOrAdminRequiredErrorMessage |
|
||||
|
||||
Scenario Outline: Owners and admins both pass the role guard
|
||||
When the client sends <method> <path> with a valid cookie of a verified <role>
|
||||
Then the response status is not 401 and not 403
|
||||
|
||||
Examples:
|
||||
| role | method | path |
|
||||
| owner | GET | /v1/media/purge-settings |
|
||||
| admin | GET | /v1/media/purge-settings |
|
||||
| owner | GET | /v1/media/purge-preview |
|
||||
| admin | GET | /v1/media/purge-preview |
|
||||
|
||||
Rule: Purge settings
|
||||
|
||||
Scenario: Nothing was ever saved, so the defaults are served
|
||||
When the client sends GET /v1/media/purge-settings with a valid cookie of the owner
|
||||
Then the response status is 200
|
||||
And the body is {"auto_purge_enabled": false, "auto_purge_interval_days": 30,
|
||||
"last_purge_at": null, "last_purge_merged": 0, "last_purge_articles_updated": 0,
|
||||
"enabled_at": null, "next_purge_at": null}
|
||||
|
||||
Scenario: Turning automatic purge on stamps enabled_at and computes next_purge_at
|
||||
Given the clock reads 2026-10-05T09:00:00Z and the settings are the defaults
|
||||
When the client sends PATCH /v1/media/purge-settings with {"auto_purge_enabled": true}
|
||||
Then the response status is 200
|
||||
And "auto_purge_enabled" is true, "auto_purge_interval_days" is 30
|
||||
And "enabled_at" is "2026-10-05T09:00:00Z" and "last_purge_at" is null
|
||||
And "next_purge_at" is "2026-11-04T09:00:00Z"
|
||||
And a following GET /v1/media/purge-settings returns the same body
|
||||
|
||||
Scenario: next_purge_at counts from the last purge when it is later than enabled_at
|
||||
Given automatic purge is on with interval 10, enabled_at 2026-10-01T00:00:00Z and last_purge_at 2026-10-04T12:00:00Z
|
||||
When the client sends GET /v1/media/purge-settings
|
||||
Then "next_purge_at" is "2026-10-14T12:00:00Z"
|
||||
|
||||
Scenario: next_purge_at counts from enabled_at when it is later than the last purge
|
||||
Given automatic purge is on with interval 10, enabled_at 2026-10-05T09:00:00Z and last_purge_at 2026-09-01T00:00:00Z
|
||||
When the client sends GET /v1/media/purge-settings
|
||||
Then "next_purge_at" is "2026-10-15T09:00:00Z"
|
||||
|
||||
Scenario: Turning automatic purge off and on again never yields a next_purge_at in the past
|
||||
Given automatic purge was on, last_purge_at is 2026-09-01T00:00:00Z, and it was turned off
|
||||
And the clock reads 2026-10-05T09:00:00Z and the interval is 30
|
||||
When the client sends PATCH /v1/media/purge-settings with {"auto_purge_enabled": true}
|
||||
Then "enabled_at" is "2026-10-05T09:00:00Z"
|
||||
And "last_purge_at" is still "2026-09-01T00:00:00Z"
|
||||
And "next_purge_at" is "2026-11-04T09:00:00Z"
|
||||
|
||||
Scenario: Changing only the interval keeps enabled_at
|
||||
Given automatic purge is on since 2026-10-01T00:00:00Z with interval 30, no purge yet, and the clock reads 2026-10-05T09:00:00Z
|
||||
When the client sends PATCH /v1/media/purge-settings with {"auto_purge_interval_days": 7}
|
||||
Then the response status is 200
|
||||
And "auto_purge_interval_days" is 7 and "enabled_at" is still "2026-10-01T00:00:00Z"
|
||||
And "next_purge_at" is "2026-10-08T00:00:00Z"
|
||||
|
||||
Scenario: Patching "on" while already on does not restamp enabled_at
|
||||
Given automatic purge is on since 2026-10-01T00:00:00Z and the clock reads 2026-10-05T09:00:00Z
|
||||
When the client sends PATCH /v1/media/purge-settings with {"auto_purge_enabled": true}
|
||||
Then "enabled_at" is still "2026-10-01T00:00:00Z"
|
||||
|
||||
Scenario: Turning automatic purge off nulls next_purge_at and keeps the history
|
||||
Given automatic purge is on with last_purge_at 2026-10-04T12:00:00Z, last_purge_merged 3 and last_purge_articles_updated 5
|
||||
When the client sends PATCH /v1/media/purge-settings with {"auto_purge_enabled": false}
|
||||
Then "auto_purge_enabled" is false and "next_purge_at" is null
|
||||
And "last_purge_at", "last_purge_merged" (3) and "last_purge_articles_updated" (5) are unchanged
|
||||
|
||||
Scenario Outline: The interval accepts whole numbers from 1 to 365
|
||||
When the client sends PATCH /v1/media/purge-settings with {"auto_purge_interval_days": <days>}
|
||||
Then the response status is 200 and "auto_purge_interval_days" is <days>
|
||||
|
||||
Examples:
|
||||
| days |
|
||||
| 1 |
|
||||
| 30 |
|
||||
| 365 |
|
||||
|
||||
Scenario Outline: An invalid patch is 400 and nothing changes
|
||||
When the client sends PATCH /v1/media/purge-settings with <body>
|
||||
Then the response status is 400
|
||||
And the body is <message>
|
||||
And the stored settings are unchanged
|
||||
|
||||
Examples:
|
||||
| body | message |
|
||||
| {"auto_purge_interval_days": 0} | apperr.PurgeIntervalErrorMessage |
|
||||
| {"auto_purge_interval_days": 366} | apperr.PurgeIntervalErrorMessage |
|
||||
| {"auto_purge_interval_days": -1} | apperr.PurgeIntervalErrorMessage |
|
||||
| {"auto_purge_interval_days": 1.5} | apperr.BodyErrorMessage |
|
||||
| {"auto_purge_interval_days": "7"} | apperr.BodyErrorMessage |
|
||||
| {"auto_purge_enabled": "yes"} | apperr.BodyErrorMessage |
|
||||
| not json | apperr.BodyErrorMessage |
|
||||
|
||||
Scenario: An empty object changes nothing
|
||||
When the client sends PATCH /v1/media/purge-settings with {}
|
||||
Then the response status is 200 and the body equals the current settings
|
||||
|
||||
Scenario: The use case rejects an out-of-range interval with the domain sentinel
|
||||
When UpdatePurgeSettings receives interval 0, or 366
|
||||
Then errors.Is(err, purge.ErrInvalidPurgeInterval) is true
|
||||
And Repository.Save is not called
|
||||
|
||||
Scenario: A repository failure is a generic 500
|
||||
Given the purge settings repository's next Get fails
|
||||
When the client sends GET /v1/media/purge-settings
|
||||
Then the response status is 500 and the body is "Internal server error"
|
||||
|
||||
Scenario: A failing Save on PATCH is a generic 500
|
||||
Given the purge settings repository's next Save fails
|
||||
When the client sends PATCH /v1/media/purge-settings with {"auto_purge_enabled": true}
|
||||
Then the response status is 500 and the body is "Internal server error"
|
||||
|
||||
Scenario: The purge routes are not taken by the /{id} routes
|
||||
When the client sends PATCH /v1/media/purge-settings with {"auto_purge_interval_days": 7}
|
||||
Then the settings are updated and no image is renamed
|
||||
When the client sends GET /v1/media/purge-preview
|
||||
Then the preview use case is invoked
|
||||
|
||||
Rule: Purge plan and preview
|
||||
|
||||
Background:
|
||||
Given the clock reads 2026-10-05T09:00:00Z
|
||||
And images are created in id order, oldest first
|
||||
And articles reference images by their stored name
|
||||
|
||||
Scenario: A group with a used copy is planned, merging every other copy into the most used
|
||||
Given images 1, 2 and 3 share sha256 A with filenames "hero.jpg", "hero-copy.jpg", "hero-2.jpg"
|
||||
And article 10 uses image 1, articles 11 and 12 use image 2, article 13 uses image 3
|
||||
When the client sends GET /v1/media/purge-preview
|
||||
Then the response status is 200
|
||||
And "groups" is 1, "copies" is 2 and "articles" is 2
|
||||
And "list" is [{"keep_id": 2, "filename": "hero-copy.jpg", "url": "<stored url of image 2>",
|
||||
"used_in": 2, "merged": 2}]
|
||||
And "more" is 0 and "fingerprint" is a 64-character hex string
|
||||
|
||||
Scenario: Ties on usage keep the oldest copy, then the lowest id
|
||||
Given images 4 and 5 share sha256 B, created at the same instant, each used in one article
|
||||
And images 6 and 7 share sha256 C, image 7 is older than image 6, each used in one article
|
||||
When the client sends GET /v1/media/purge-preview
|
||||
Then the "keep_id" of the group B is 4 and the "keep_id" of the group C is 7
|
||||
|
||||
Scenario: A single used copy with unused siblings keeps the used one
|
||||
Given images 8, 9 and 10 share sha256 D and only image 10 is used, in one article
|
||||
When the client sends GET /v1/media/purge-preview
|
||||
Then the group has keep_id 10, used_in 1, merged 2
|
||||
And "articles" is 0, since no article references a merged-away copy
|
||||
|
||||
Scenario: A group whose copies are all unused is skipped
|
||||
Given images 11 and 12 share sha256 E and no article references either
|
||||
When the client sends GET /v1/media/purge-preview
|
||||
Then "groups" is 0, "copies" is 0, "articles" is 0, "list" is [] and "more" is 0
|
||||
|
||||
Scenario: A library with no duplicates
|
||||
Given the library holds three images with distinct sha256 values
|
||||
When the client sends GET /v1/media/purge-preview
|
||||
Then the response status is 200
|
||||
And "groups" is 0 and "list" is an empty array in the raw JSON, not null
|
||||
And "fingerprint" equals the fingerprint of any other empty plan
|
||||
|
||||
Scenario: Images without a hash are never grouped
|
||||
Given images 13 and 14 both have an empty sha256 and both are used
|
||||
When the client sends GET /v1/media/purge-preview
|
||||
Then "groups" is 0
|
||||
|
||||
Scenario: An article that uses copies of two groups counts once
|
||||
Given groups A (keep 1, copy 2) and B (keep 3, copy 4) and one article that uses images 2 and 4
|
||||
When the client sends GET /v1/media/purge-preview
|
||||
Then "groups" is 2, "copies" is 2 and "articles" is 1
|
||||
|
||||
Scenario: An article that uses both the kept copy and a copy is rewritten
|
||||
Given an article uses images 1 and 2 of one group where image 1 is kept
|
||||
When the client sends GET /v1/media/purge-preview
|
||||
Then "articles" is 1
|
||||
|
||||
Scenario: The list shows at most 5 groups and reports the rest
|
||||
Given 7 planned groups
|
||||
When the client sends GET /v1/media/purge-preview
|
||||
Then "groups" is 7, "list" has 5 entries in ascending keep_id order and "more" is 2
|
||||
|
||||
Scenario: The preview changes nothing
|
||||
Given a planned group
|
||||
When the client sends GET /v1/media/purge-preview twice
|
||||
Then both bodies are equal
|
||||
And no image is deleted, no article body changes and no purge setting is saved
|
||||
|
||||
Scenario Outline: The fingerprint reacts to the plan and to nothing else
|
||||
Given a planned group with kept image 2 and merged image 3
|
||||
When <change>
|
||||
Then the preview fingerprint is <result>
|
||||
|
||||
Examples:
|
||||
| change | result |
|
||||
| nothing changes | identical |
|
||||
| a new unused copy is uploaded to the group | different |
|
||||
| a copy is deleted from the group | different |
|
||||
| article usage changes so that another copy becomes kept | different |
|
||||
| an unrelated image with a unique sha256 is uploaded | identical |
|
||||
| an article body changes without changing which is kept | identical |
|
||||
| a group with no used copy gains an unused copy | identical |
|
||||
|
||||
Rule: Running a purge
|
||||
|
||||
Background:
|
||||
Given the clock reads 2026-10-05T09:00:00Z
|
||||
|
||||
Scenario: A run merges every planned group and persists the result
|
||||
Given group A: images 1 (used in article 10), 2 (used in articles 11 and 12) and 3 (unused)
|
||||
And group B: images 4 (used in article 12) and 5 (unused)
|
||||
And group C: images 6 and 7, both unused
|
||||
When the client sends POST /v1/media/purge with {}
|
||||
Then the response status is 200
|
||||
And the body is {"groups_total": 2, "groups_purged": 2, "merged": 3, "articles_updated": 1, "failed_groups": 0}
|
||||
And images 1 and 3 are gone from the image repository, image 2 remains
|
||||
And image 5 is gone, image 4 remains, and images 6 and 7 both remain
|
||||
And article 10's body now uses image 2's stored name
|
||||
And the stored settings have last_purge_at "2026-10-05T09:00:00Z", last_purge_merged 3 and last_purge_articles_updated 1
|
||||
And only article 10 is rewritten, since article 12 uses only kept copies
|
||||
And "auto_purge_enabled", "auto_purge_interval_days" and "enabled_at" are untouched
|
||||
|
||||
Scenario: An article rewritten by two groups is counted once in articles_updated
|
||||
Given an article uses a copy of group A and a copy of group B
|
||||
When the client sends POST /v1/media/purge with {}
|
||||
Then "articles_updated" counts that article once, from the union of the groups' UpdatedArticleIDs
|
||||
|
||||
Scenario: A single used copy with unused siblings is merged without rewriting any article
|
||||
Given images 8, 9 and 10 share a hash and only image 10 is used
|
||||
When the client sends POST /v1/media/purge with {}
|
||||
Then the body is {"groups_total": 1, "groups_purged": 1, "merged": 2, "articles_updated": 0, "failed_groups": 0}
|
||||
And images 8 and 9 are gone, image 10 remains and no article body changed
|
||||
|
||||
Scenario: A matching fingerprint is accepted
|
||||
Given the preview returned fingerprint F
|
||||
When the client sends POST /v1/media/purge with {"fingerprint": "F"}
|
||||
Then the response status is 200 and the purge ran
|
||||
|
||||
Scenario: A stale fingerprint answers 409 and changes nothing
|
||||
Given the preview returned fingerprint F
|
||||
And an unused copy is then uploaded to a planned group
|
||||
When the client sends POST /v1/media/purge with {"fingerprint": "F"}
|
||||
Then the response status is 409
|
||||
And the body is apperr.PurgeConflictErrorMessage
|
||||
And no image is deleted, no article body changes and the stored last_purge_* values are unchanged
|
||||
|
||||
Scenario: A fingerprint that matches no plan at all is a conflict, not a validation error
|
||||
When the client sends POST /v1/media/purge with {"fingerprint": "not-a-real-fingerprint"}
|
||||
Then the response status is 409 and the body is apperr.PurgeConflictErrorMessage
|
||||
And nothing changed
|
||||
|
||||
Scenario: A run without a fingerprint always runs the current plan
|
||||
Given the library changed since any preview
|
||||
When the client sends POST /v1/media/purge with an empty body
|
||||
Then the response status is 200 and the current plan ran
|
||||
|
||||
Scenario: A library with nothing to purge answers zeros and is still recorded
|
||||
Given the library holds no planned group
|
||||
When the client sends POST /v1/media/purge with {}
|
||||
Then the response status is 200
|
||||
And the body is {"groups_total": 0, "groups_purged": 0, "merged": 0, "articles_updated": 0, "failed_groups": 0}
|
||||
And the stored settings have last_purge_at "2026-10-05T09:00:00Z", last_purge_merged 0 and last_purge_articles_updated 0
|
||||
And a scheduler reading next_purge_at sees it move forward from that run
|
||||
|
||||
Scenario: The fingerprint of the empty plan on an empty plan runs as a no-op
|
||||
Given the library holds no planned group
|
||||
When the client sends POST /v1/media/purge with the empty plan's fingerprint
|
||||
Then the response status is 200 and the body has groups_total 0
|
||||
|
||||
Scenario: A group that fails while rewriting bodies is rolled back and contributes no ids
|
||||
Given groups A, B and C are planned, in that order
|
||||
And the article repository's UpdateBody fails for the second article that group B must rewrite
|
||||
When the client sends POST /v1/media/purge with {}
|
||||
Then the response status is 200
|
||||
And the body is {"groups_total": 3, "groups_purged": 2, "merged": <copies of A and C>, "articles_updated": <distinct articles of A and C>, "failed_groups": 1}
|
||||
And group B's article bodies are back to their original text and all of group B's images remain
|
||||
And group B's UpdatedArticleIDs is empty, so no article of group B is in "articles_updated"
|
||||
And the stored last_purge_merged and last_purge_articles_updated equal the response totals
|
||||
|
||||
Scenario: A group that fails after rewriting bodies is reported as failed and contributes nothing
|
||||
Given the image repository's Delete fails for a merged image of group A, and group B is planned
|
||||
When the client sends POST /v1/media/purge with {}
|
||||
Then the response status is 200, "failed_groups" is 1 and "groups_purged" is 1
|
||||
And group A contributes 0 to "merged" and its article ids are not added to "articles_updated", even though its article bodies stay switched to the kept copy
|
||||
And "articles_updated" and "merged" count only the fully merged group B
|
||||
And a following GET /v1/media/purge-preview plans group A again, and the next run converges because the plan is recomputed from current data
|
||||
|
||||
Scenario: Every group failing is still a 200 with the counters
|
||||
Given every UpdateBody fails
|
||||
When the client sends POST /v1/media/purge with {}
|
||||
Then the response status is 200
|
||||
And "groups_purged" is 0 and "failed_groups" equals "groups_total"
|
||||
And the stored last_purge_at is set
|
||||
|
||||
Scenario: A second run while one is in progress is rejected as busy, not queued
|
||||
Given a purge run is in progress and blocked inside the image repository
|
||||
When a second POST /v1/media/purge arrives, with or without a fingerprint
|
||||
Then it answers 409 with apperr.PurgeBusyErrorMessage at once
|
||||
And it changed nothing and saved no last_purge_* values
|
||||
When the first run is released
|
||||
Then it finishes normally with its own counters
|
||||
And a later POST /v1/media/purge is accepted
|
||||
|
||||
Scenario: The use case distinguishes a busy run from a stale fingerprint
|
||||
When RunPurge receives a non-matching fingerprint
|
||||
Then errors.Is(err, purge.ErrPurgeConflict) is true and errors.Is(err, purge.ErrPurgeBusy) is false
|
||||
When RunPurge is called while another run holds the guard
|
||||
Then errors.Is(err, purge.ErrPurgeBusy) is true and errors.Is(err, purge.ErrPurgeConflict) is false
|
||||
And in both cases no repository write happens
|
||||
|
||||
Scenario: A run via the use case with no fingerprint needs no HTTP types
|
||||
When a scheduler later calls RunPurge with no fingerprint
|
||||
Then it gets the result struct and the same last_purge_* persistence applies
|
||||
|
||||
Scenario: A malformed body is 400
|
||||
When the client sends POST /v1/media/purge with "not json"
|
||||
Then the response status is 400 and the body is apperr.BodyErrorMessage
|
||||
And nothing changed
|
||||
|
||||
Scenario: A repository failure while planning is a generic 500 and nothing changes
|
||||
Given the image repository's ListDuplicates fails
|
||||
When the client sends POST /v1/media/purge with {}
|
||||
Then the response status is 500 and the body is "Internal server error"
|
||||
And no last_purge_* values are saved
|
||||
|
||||
Scenario: A failing save of the run result is a logged 500 and the merges stay
|
||||
Given the purge settings repository's SaveLastRun fails after the merges succeeded
|
||||
When the client sends POST /v1/media/purge with {}
|
||||
Then the response status is 500 and the body is "Internal server error"
|
||||
And the merges that already happened stay done
|
||||
And the error is written to the injected *log.Logger
|
||||
And the next POST /v1/media/purge converges, because the plan is recomputed from current data
|
||||
|
||||
Scenario: The preview and the run use the same plan
|
||||
When the client sends GET /v1/media/purge-preview and then POST /v1/media/purge with the returned fingerprint
|
||||
Then the run's "groups_total" equals the preview's "groups"
|
||||
And the run's "merged" equals the preview's "copies" and its "articles_updated" equals the preview's "articles"
|
||||
|
||||
Rule: MergeMedia keeps its contract while exposing the rewritten ids
|
||||
|
||||
Scenario: Existing merges report the same counts and now also the ids
|
||||
When MergeMedia.Merge succeeds for a group that rewrites articles 1 and 2
|
||||
Then Merged and ArticlesUpdated are unchanged for existing callers
|
||||
And UpdatedArticleIDs is [1, 2]
|
||||
|
||||
Scenario: A rolled-back merge returns no ids
|
||||
When MergeMedia.Merge fails while rewriting a body
|
||||
Then it returns the error and an empty result
|
||||
|
||||
@manual
|
||||
Rule: Persistence and wiring (verified by hand, see the qa.md)
|
||||
|
||||
Scenario: The purge settings table is a single row created by AutoMigrate
|
||||
Given a database with no purge settings table
|
||||
When the server boots
|
||||
Then the table exists, GET /v1/media/purge-settings returns the defaults and no row is required beforehand
|
||||
And repeated PATCH calls keep exactly one row
|
||||
|
||||
Scenario: Settings survive a restart
|
||||
Given automatic purge was turned on with interval 7
|
||||
When the server restarts
|
||||
Then GET /v1/media/purge-settings returns the same values
|
||||
|
||||
Scenario: A real purge removes the merged files from UPLOAD_DIR
|
||||
Given duplicates uploaded through the real server
|
||||
When an owner runs POST /v1/media/purge
|
||||
Then the merged copies' files are gone from UPLOAD_DIR and the kept file remains
|
||||
@@ -0,0 +1,78 @@
|
||||
# QA procedure: media_purge
|
||||
|
||||
Package: `test` (black-box). New files `test/media_purge_http_test.go` and `test/media_purge_test.go` (use case level). Fake type names must be unique in the package. Go only: no SPA, no scheduler, no env var.
|
||||
|
||||
## Setup shared by every case
|
||||
1. Call `validator.InitValidator()` once.
|
||||
2. Fakes:
|
||||
- `mockPurgeSettingsRepo` (domain `purge.Repository`): holds one `Settings`, `Get` returns the defaults when nothing was saved, `Save` records the call count, with armed failure switches `failGet` and `failSave`.
|
||||
- Reuse or extend `mockImageRepo` (`test/image_test.go`): `ListDuplicates()` (images with a non-empty sha256 shared by at least two rows, ordered `sha256, id`), `Delete` with `failDeleteFor`, `Get`, `FindByHash`, `Counts`. Add a hook that lets a test block inside `ListDuplicates` or `Delete` on a channel, to hold a run in progress.
|
||||
- Reuse or extend `mockArticleRepo` (`test/article_test.go`): `FindByFilters`, `FindByBodyContaining`, `UpdateBody` with a per-id failure switch.
|
||||
- The user fake: a verified owner (id 1), a verified admin (id 2), a verified user with `Role("editor")` (id 3), a verified user with an empty role (id 5), and an unverified owner (id 4).
|
||||
3. Wire the real use cases over the fakes: the four purge use cases (constructors chosen by the coder), `NewGetUserUseCase(userRepo)`, plus the existing media use cases. Inject a fixed clock (2026-10-05T09:00:00Z) and a `*log.Logger` writing to a `bytes.Buffer`. Add the four purge handler methods to the media handler, built with `media.NewHandler(...)`.
|
||||
4. Build `jwtauth.New("HS256", []byte("test-secret"), nil)` and mount on a fresh `chi.NewRouter()` mirroring `NewMediaRouter` in `api/router.go`:
|
||||
- `jwtauth.Verifier` at the top, then `Authenticator`, `RequireCurrentToken(getUser)`, `RequireVerifiedUser(getUser)`.
|
||||
- The purge routes inside `r.Group` with `middleware.RequireOwnerOrAdmin(getUser)`, registered BEFORE `Patch("/{id}")` and `Delete("/{id}")`.
|
||||
- Also register `Get("/")`, `Post("/merge")`, `Patch("/{id}")` and `Delete("/{id}")` so route precedence is exercised.
|
||||
- Mount under `/v1/media`.
|
||||
5. Mint cookies via `token.Encode(map[string]any{"user_id": uint32(n), "token_version": ...})` as in other HTTP tests. Build a stale-token cookie with a lower `token_version`.
|
||||
6. Seeding helper `seedGroup(images ...)` assigning ids in order, strictly increasing `CreatedAt` unless the case says otherwise, and `articleUsing(id, images ...)` building bodies that contain each image's stored name (`image.StoredName(path)`).
|
||||
7. Compare JSON bodies by decoding, except where the case says "raw body" (null versus `[]`, null versus absent). Error bodies are plain text: compare to `message + "\n"`.
|
||||
|
||||
## Auth and role checks (table-driven)
|
||||
8. For each row of the "refuse callers" outline (4 routes x no cookie, stale, unverified, role "editor", empty role), assert the status and body. Assert the settings, image and article fakes saw no call (only the user lookup of the middleware).
|
||||
9. Owner and admin cookies: GET `/v1/media/purge-settings` and GET `/v1/media/purge-preview` return a status other than 401 and 403.
|
||||
10. Allowlist guard: add a case proving a role other than `owner`/`admin` (for example "OWNER" in upper case, or "superuser") is refused with 403, so the middleware is not a blocklist.
|
||||
|
||||
## Settings
|
||||
11. Defaults: empty repo, GET returns the exact default body. Assert the raw JSON has `"last_purge_at":null`, `"enabled_at":null` and `"next_purge_at":null` (not omitted).
|
||||
12. PATCH on: assert `enabled_at` equals the fixed clock, `next_purge_at` = enabled_at + 30 days, the GET after returns the same body, and `Save` was called once.
|
||||
13. next_purge_at basis (max rule): last_purge_at later than enabled_at counts from last_purge_at; enabled_at later than last_purge_at counts from enabled_at; no last_purge_at counts from enabled_at.
|
||||
14. Re-enable step: seed on with `last_purge_at` 2026-09-01, PATCH `false`, then PATCH `true` at the fixed clock with interval 30. Assert `enabled_at` is the clock, `last_purge_at` is still 2026-09-01, and `next_purge_at` is 2026-11-04T09:00:00Z, strictly after the clock.
|
||||
15. Interval-only PATCH keeps `enabled_at`. PATCH `true` while already on keeps `enabled_at`. PATCH `false` nulls `next_purge_at` and keeps last_purge_*.
|
||||
16. Interval table (1, 30, 365 accepted). Invalid table (0, 366, -1 give `apperr.PurgeIntervalErrorMessage`; 1.5, "7", "yes", not json give `apperr.BodyErrorMessage`). Assert the repo state and the `Save` count are unchanged after each 400.
|
||||
17. `{}` PATCH returns 200 equal to the current settings.
|
||||
18. Use-case-level: `UpdatePurgeSettings` with interval 0 and 366 returns an error with `errors.Is(err, purge.ErrInvalidPurgeInterval)` and no `Save`. At the handler, a value of 0 must be rejected by the validate tag and never reach the use case.
|
||||
19. Failure rows: `failGet` gives 500 "Internal server error\n"; `failSave` on PATCH gives 500.
|
||||
20. Route precedence: PATCH `/v1/media/purge-settings` reaches the settings handler and does not touch `Rename`.
|
||||
|
||||
## Preview
|
||||
21. Build each Background scenario with the seed helpers and assert the decoded `groups`, `copies`, `articles`, `list` and `more`:
|
||||
- used group (keep 2 with used_in 2; copies 2; articles 2);
|
||||
- tie-breaks: same CreatedAt gives the lowest id, different CreatedAt gives the oldest (assert both orders by swapping ids);
|
||||
- a single used copy with unused siblings (articles 0);
|
||||
- an all-unused group;
|
||||
- no duplicates (raw `"list":[]`);
|
||||
- empty-hash images ignored;
|
||||
- an article touching two groups counts once;
|
||||
- an article using kept plus a copy counts once;
|
||||
- 7 groups give 5 list entries in ascending keep_id order and `more` 2. Each entry has `keep_id`, `filename`, `url`, `used_in`, `merged`.
|
||||
22. Preview purity: call twice, assert equal bodies, assert zero `Delete`, `UpdateBody` and `Save` calls.
|
||||
23. Fingerprint table: compute a baseline, apply each change from the outline, and assert equal or different. Assert it is a 64-char lowercase hex string and that two empty plans share it.
|
||||
|
||||
## Run
|
||||
24. Full run scenario: assert the response body, the remaining images, the rewritten article bodies (check bytes), and `Save` called once with `last_purge_at` = clock, `last_purge_merged` = 3 and `last_purge_articles_updated` = 1, and that `auto_purge_*` and `enabled_at` are untouched. Article 12 uses only kept copies, so only article 10 is rewritten.
|
||||
25. `articles_updated` source: it must come from the union of `MergeMediaResult.UpdatedArticleIDs` returned by the group merges, never from the plan. Prove it with the rolled-back case in step 29: the plan contains B's articles, the response does not.
|
||||
26. Used-sibling run with zero article rewrites (`articles_updated` 0, `UpdateBody` never called).
|
||||
27. Fingerprint: match gives 200. A stale fingerprint gives 409 with `apperr.PurgeConflictErrorMessage + "\n"`, no `Delete`, no `UpdateBody`, no `Save`. A garbage string gives 409 with the same body. No fingerprint, `{}` and an empty body all run.
|
||||
28. Empty plan: 200 zeros, `Save` with `last_purge_at` = clock and zero counters, and a run with the empty plan's fingerprint is also 200.
|
||||
29. Partial failure, rolled back: arm `UpdateBody` to fail for the second article of group B (plan order A, B, C). Assert the body counters, B's bodies restored byte for byte, all B images present, A and C merged, B's articles absent from `articles_updated`, and `Save` counters equal the response.
|
||||
30. Partial failure, delete: arm `failDeleteFor` for one of group A's merged images and plan B too. Assert `failed_groups` 1, `groups_purged` 1, A contributes 0 to `merged` and none of its ids to `articles_updated` (only B counts), A's article bodies stay switched to the kept copy, and a following preview plans A again. Run the purge again with the failure disarmed and assert it converges (A's leftover copy is merged away). Then arm every `UpdateBody` and assert `groups_purged` 0, `failed_groups` = `groups_total` and `last_purge_at` still saved.
|
||||
31. Concurrency: start run 1 in a goroutine, blocked in the image fake (wait on a "started" channel). Send run 2 (once with a fingerprint, once without). Assert both answer 409 with `apperr.PurgeBusyErrorMessage + "\n"` immediately (use a timeout) and no `Save`/`Delete` calls from them. Release run 1, assert its 200 body and that a later run is accepted. Run this test with `-race`.
|
||||
32. Sentinels at the use-case level, asserted separately: a stale fingerprint gives `errors.Is(err, purge.ErrPurgeConflict)` true and `errors.Is(err, purge.ErrPurgeBusy)` false; a busy guard gives `ErrPurgeBusy` true and `ErrPurgeConflict` false. Call RunPurge without any HTTP package to prove it is reusable (that test file section imports no `net/http`).
|
||||
33. Malformed body gives 400 `apperr.BodyErrorMessage`. `ListDuplicates` failure gives 500 and no `Save`.
|
||||
34. Persist failure: arm `failSave` on the run-result save after merges succeed. Assert 500 "Internal server error\n", the merged images are still gone, the logger buffer contains the error text, then disarm and run again and assert 200 with no remaining groups (convergence from current data).
|
||||
35. Preview versus run parity: the run's `groups_total`, `merged` and `articles_updated` equal the preview's `groups`, `copies` and `articles` on the same seed.
|
||||
36. MergeMedia contract: extend the existing merge tests only by adding assertions. `Merged` and `ArticlesUpdated` are unchanged, `UpdatedArticleIDs` lists the rewritten ids in ascending order, and a rolled-back merge returns the error and an empty result. Existing callers and tests must compile and pass unchanged.
|
||||
|
||||
## Layering guards
|
||||
37. `api/handlers/media` imports no `persistence`, `mailer`, `models` or `gorm` package. The new use cases import no `net/http`, chi, gorm, `persistence`, `mailer` or `api/...` package. `internal/domain/purge` imports only the stdlib and other `internal/domain/*` packages. Use a `go list -deps` or grep-style test as in media_library.qa.md.
|
||||
|
||||
## Manual verification (not exercised by `task test`)
|
||||
- AutoMigrate: boot against a scratch SQLite file (`task dev`) and check the purge settings table exists, GET returns the defaults, repeated PATCH keeps one row, and values survive a restart. The mapper round-trips `last_purge_at`/`enabled_at` including null.
|
||||
- Real purge against real uploads: upload duplicates through `POST /v1/media/`, reference them from articles, run the preview then the purge. The merged files disappear from `UPLOAD_DIR`, the kept file loads through `/static/uploadedImages/`, and article bodies are switched.
|
||||
- Concurrent real requests with two shells (`curl` with the `jwt` cookie): the second gets 409 "A purge is already running." while the first runs.
|
||||
- Role check with a real admin account (invite then accept). Use a hand-edited user row with an empty or unknown role to check the 403.
|
||||
- The persist-failure log line appears in the server output (check by making the database read-only during a purge).
|
||||
- Docs: `docs/architecture.md` lists the `purge` domain package, the four use cases, the settings repository, the new routes and `RequireOwnerOrAdmin`. Read it and compare. No SMTP and no env var are involved.
|
||||
- Out of scope: the scheduler (part 2) and the SPA (part 3).
|
||||
@@ -0,0 +1,286 @@
|
||||
Feature: Duplicate images are purged automatically on the configured schedule
|
||||
|
||||
As a RenewCMS owner or admin
|
||||
I want the duplicate purge to run by itself every few days once I turn it on
|
||||
So that I do not have to remember to purge, and the Settings "Last purge" line stays current
|
||||
|
||||
Background:
|
||||
Given part 1 provides purge.Settings, purge.Repository (Get, Save and SaveLastRun) and RunPurgeUseCase.Run(fingerprint *string)
|
||||
And a new use case PurgeSchedulerUseCase in internal/infrastructure/useCases depends only on purge.Repository
|
||||
and a RunPurge port, takes no HTTP types and imports no persistence, mailer, models, gorm, net/http, chi or api
|
||||
And PurgeSchedulerUseCase.Tick(now time.Time) (result purge.RunResult, ran bool, err error) reads the settings on every call,
|
||||
where result carries groups purged, copies merged and articles updated and is empty when ran is false
|
||||
And a run is due when auto_purge_enabled is true, enabled_at is set and now is not before
|
||||
Settings.NextPurgeAt(), that is max(last_purge_at, enabled_at) + auto_purge_interval_days days
|
||||
And a scheduled run is RunPurge.Run(nil): no fingerprint, no HTTP, no user-visible message
|
||||
And a run that finds nothing to purge still records last_purge_at with merged 0 and articles_updated 0,
|
||||
so the anchor moves forward (this is RunPurge's existing behaviour)
|
||||
And the scheduler never writes settings itself: only RunPurge records last_purge_*,
|
||||
through Repository.SaveLastRun, which writes only the last_purge_* fields and never the whole settings row
|
||||
And there is no env var: the schedule (auto_purge_enabled, auto_purge_interval_days, enabled_at,
|
||||
last_purge_at) lives in the purge settings row, and is read again on every tick
|
||||
And a runner in the new package internal/infrastructure/scheduler calls Tick on a ticker every 1 hour,
|
||||
runs one Tick after a startup delay of 1 minute, and stops when its context is cancelled
|
||||
And the runner takes its clock, its tick source and its startup delay by injection, so tests use a fake clock
|
||||
and hand-fed channels, with no real sleeping and no real time.Ticker
|
||||
And api/dependecyInjection.go builds the runner and cmd/server/main.go only starts it
|
||||
And the runner uses the *log.Logger already used by RunPurge (log.Default() in the container)
|
||||
|
||||
Rule: When a run is due
|
||||
|
||||
Background:
|
||||
Given the fake clock reads 2026-10-05T09:00:00Z
|
||||
|
||||
Scenario: Automatic purge off never runs, whatever the dates are
|
||||
Given auto_purge_enabled is false, interval 1, enabled_at 2020-01-01T00:00:00Z and last_purge_at 2020-01-02T00:00:00Z
|
||||
When Tick is called with the fake clock's time
|
||||
Then ran is false and err is nil
|
||||
And RunPurge was not called and Save was not called
|
||||
|
||||
Scenario: The defaults never run
|
||||
Given nothing was ever saved, so the settings are the defaults
|
||||
When Tick is called
|
||||
Then ran is false and RunPurge was not called
|
||||
|
||||
Scenario: On, never ran, not due yet
|
||||
Given auto_purge_enabled is true, interval 30, enabled_at 2026-09-20T09:00:00Z and no last_purge_at
|
||||
When Tick is called
|
||||
Then ran is false and RunPurge was not called
|
||||
|
||||
Scenario: On, never ran, due since enabled_at plus the interval
|
||||
Given auto_purge_enabled is true, interval 10, enabled_at 2026-09-25T09:00:00Z and no last_purge_at
|
||||
When Tick is called
|
||||
Then RunPurge is called once with no fingerprint and ran is true
|
||||
|
||||
Scenario Outline: The anchor is the later of last_purge_at and enabled_at
|
||||
Given auto_purge_enabled is true and interval 10
|
||||
And enabled_at is <enabled_at> and last_purge_at is <last_purge_at>
|
||||
When Tick is called at 2026-10-05T09:00:00Z
|
||||
Then RunPurge is called <calls> time(s)
|
||||
|
||||
Examples:
|
||||
| enabled_at | last_purge_at | calls | why |
|
||||
| 2026-09-01T00:00:00Z | 2026-09-20T00:00:00Z | 1 | last purge + 10 days = 2026-09-30, due |
|
||||
| 2026-09-01T00:00:00Z | 2026-09-28T00:00:00Z | 0 | last purge + 10 days = 2026-10-08, not due |
|
||||
| 2026-09-28T00:00:00Z | 2026-09-01T00:00:00Z | 0 | enabled_at is later, 2026-10-08, not due |
|
||||
| 2026-09-20T00:00:00Z | 2026-09-01T00:00:00Z | 1 | enabled_at is later, 2026-09-30, due |
|
||||
| 2026-09-25T09:00:00Z | none | 1 | exactly now is due (2026-10-05T09:00:00Z) |
|
||||
| 2026-09-25T09:00:01Z | none | 0 | one second before due |
|
||||
|
||||
Scenario: Due exactly at the boundary, not before
|
||||
Given the next purge is 2026-10-05T09:00:00Z
|
||||
When Tick is called at 2026-10-05T08:59:59Z
|
||||
Then RunPurge is not called
|
||||
When Tick is called at 2026-10-05T09:00:00Z
|
||||
Then RunPurge is called once
|
||||
|
||||
Scenario: A run advances the anchor, so the next tick is not due
|
||||
Given a due run with RunPurge backed by the fake settings repository
|
||||
When Tick is called twice with the same time
|
||||
Then RunPurge is called once
|
||||
And last_purge_at equals the fake clock's time
|
||||
|
||||
Scenario: A due run that finds nothing to purge still moves the clock forward
|
||||
Given a due schedule and a library with no planned group
|
||||
When Tick is called
|
||||
Then the stored last_purge_at is the clock's time, last_purge_merged is 0 and last_purge_articles_updated is 0
|
||||
And a second Tick at the same time does not run
|
||||
And a Tick after the interval has elapsed runs again
|
||||
|
||||
Scenario: A run keeps auto_purge_enabled, the interval and enabled_at
|
||||
Given a due schedule
|
||||
When Tick runs
|
||||
Then auto_purge_enabled, auto_purge_interval_days and enabled_at are unchanged in the stored settings
|
||||
|
||||
Scenario: Enabled with no enabled_at is not due
|
||||
Given auto_purge_enabled is true and enabled_at is unset
|
||||
When Tick is called
|
||||
Then RunPurge is not called and the scheduler does not stamp enabled_at itself
|
||||
|
||||
Rule: Settings changes take effect on the next tick, with no restart
|
||||
|
||||
Background:
|
||||
Given the fake clock reads 2026-10-05T09:00:00Z
|
||||
|
||||
Scenario: Shortening the interval makes a run due on the next tick
|
||||
Given auto_purge_enabled is true since 2026-09-25T09:00:00Z with interval 30 and no purge yet
|
||||
And Tick did not run
|
||||
When UpdatePurgeSettings sets auto_purge_interval_days to 7
|
||||
And Tick is called again
|
||||
Then RunPurge is called once
|
||||
|
||||
Scenario: Lengthening the interval postpones the run
|
||||
Given auto_purge_enabled is true since 2026-09-20T09:00:00Z with interval 10 and no purge yet, so a run is due
|
||||
When UpdatePurgeSettings sets auto_purge_interval_days to 30
|
||||
And Tick is called
|
||||
Then RunPurge is not called
|
||||
|
||||
Scenario: Turning it off stops runs
|
||||
Given a due schedule
|
||||
When UpdatePurgeSettings sets auto_purge_enabled to false
|
||||
And Tick is called
|
||||
Then RunPurge is not called
|
||||
|
||||
Scenario: Turning it on resets the anchor to the moment of the switch
|
||||
Given auto_purge_enabled was turned off, last_purge_at is 2026-09-01T00:00:00Z, the interval is 30 and the clock reads 2026-10-05T09:00:00Z
|
||||
When UpdatePurgeSettings sets auto_purge_enabled to true
|
||||
And Tick is called at once
|
||||
Then RunPurge is not called, because enabled_at is now
|
||||
When Tick is called at 2026-11-04T09:00:00Z
|
||||
Then RunPurge is called once
|
||||
|
||||
Scenario: The scheduler reads the settings at every tick
|
||||
When Tick is called three times with different stored settings
|
||||
Then Repository.Get is called three times and no value is cached between ticks
|
||||
|
||||
Rule: Failures never crash the loop and are retried at the next tick
|
||||
|
||||
Background:
|
||||
Given a due schedule and the fake clock reads 2026-10-05T09:00:00Z
|
||||
|
||||
Scenario: Reading the settings fails
|
||||
Given the purge settings repository's next Get fails
|
||||
When Tick is called
|
||||
Then ran is false and err is the repository error
|
||||
And RunPurge was not called
|
||||
And the next Tick, with Get working again, runs the purge
|
||||
|
||||
Scenario: RunPurge returns an error
|
||||
Given RunPurge fails, for example ListDuplicates fails
|
||||
When Tick is called
|
||||
Then err is that error and last_purge_at is unchanged
|
||||
And RunPurge was called once, not retried inside the same Tick
|
||||
And the next Tick, with the failure cleared, runs the purge
|
||||
|
||||
Scenario: Saving the run result fails after the merges succeeded
|
||||
Given the purge settings repository's SaveLastRun fails
|
||||
When Tick is called
|
||||
Then err is returned and the merges stay done
|
||||
And the next Tick plans again from current data and converges
|
||||
|
||||
Scenario: A run that partially fails still records what it merged
|
||||
Given group A merges and group B's UpdateBody fails
|
||||
When Tick is called
|
||||
Then ran is true and err is nil
|
||||
And last_purge_at is set and last_purge_merged and last_purge_articles_updated count only group A
|
||||
And group B is planned again by the next run
|
||||
|
||||
Scenario: A settings change made during a run survives
|
||||
Given a run is in progress and about to record its result
|
||||
When UpdatePurgeSettings turns auto_purge_enabled off and sets the interval to 5 meanwhile
|
||||
And the run completes
|
||||
Then the stored auto_purge_enabled is false and the interval is 5
|
||||
And last_purge_at, last_purge_merged and last_purge_articles_updated hold the run's values
|
||||
|
||||
Scenario: A manual run is in progress
|
||||
Given a manual POST /v1/media/purge holds the RunPurge guard
|
||||
When Tick is called with a due schedule
|
||||
Then the scheduler does not queue or wait: it returns at once with errors.Is(err, purge.ErrPurgeBusy)
|
||||
And nothing is saved by the scheduler
|
||||
And the runner treats it as a skip, not a failure, and the next tick re-evaluates
|
||||
When the manual run finishes and records last_purge_at
|
||||
Then the next Tick finds the schedule not due and does not run
|
||||
|
||||
Scenario: A failing tick does not stop the runner
|
||||
Given the runner is running with a hand-fed tick channel
|
||||
And the first Tick returns an error and the second returns ran true
|
||||
When two ticks are fed
|
||||
Then the error is written to the injected logger
|
||||
And the runner is still running and the second Tick was executed
|
||||
And no Tick is called between the two fed ticks (no tight retry loop)
|
||||
|
||||
Scenario: A completed run logs one line with the counts
|
||||
Given Tick returns ran true with groups purged 3, merged 5, articles updated 2 and failed groups 1
|
||||
When the runner receives the tick
|
||||
Then exactly one line is logged: "scheduler: automatic purge completed: groups_purged=3 merged=5 articles_updated=2 failed_groups=1"
|
||||
And a run that found nothing to purge logs the same line with every count at 0
|
||||
|
||||
Scenario: A due Tick returns what the run did
|
||||
Given a due schedule and one planned group
|
||||
When Tick is called
|
||||
Then ran is true and the result holds the run's groups purged, merged and articles updated
|
||||
And a Tick that is not due returns an empty result
|
||||
|
||||
Scenario: A busy skip is not logged as an error
|
||||
Given Tick returns ErrPurgeBusy
|
||||
When the runner receives the tick
|
||||
Then it logs no error line and keeps waiting for the next tick
|
||||
|
||||
Rule: The runner starts, ticks and stops with the server
|
||||
|
||||
Scenario: One Tick shortly after startup
|
||||
Given a runner with a hand-fed startup signal standing in for the 1 minute delay
|
||||
When the startup signal fires
|
||||
Then Tick is called once with the fake clock's time
|
||||
And no Tick is called before the signal
|
||||
|
||||
Scenario: A due run is caught up at startup after downtime
|
||||
Given the server was down for 40 days, the interval is 30 and last_purge_at is 40 days old
|
||||
When the runner's startup Tick fires
|
||||
Then RunPurge is called once, and only once however many ticks were missed
|
||||
|
||||
Scenario: A Tick on each hourly tick
|
||||
Given the runner is running
|
||||
When three hourly ticks are fed
|
||||
Then Tick is called three times, each with the fake clock's time at that tick
|
||||
|
||||
Scenario: Cancelling the context stops the runner
|
||||
Given the runner is idle
|
||||
When its context is cancelled
|
||||
Then Run returns promptly
|
||||
And no further Tick is called even if a tick is fed after cancel
|
||||
|
||||
Scenario: Shutdown waits for a run in progress without a time limit
|
||||
Given Tick is blocked inside RunPurge (fake image repository blocked on a channel)
|
||||
When the context is cancelled
|
||||
Then Run does not return yet
|
||||
When the blocked run is released and completes
|
||||
Then Run returns after the run has recorded last_purge_at
|
||||
And the database is closed only after Run has returned
|
||||
|
||||
Scenario: Cancelled before the startup signal
|
||||
Given the context is cancelled before the startup signal
|
||||
Then Tick is never called and Run returns
|
||||
|
||||
Rule: The Settings page shows the result (HTTP observable, no SPA change)
|
||||
|
||||
Scenario: A scheduled run updates the Last purge values served by the settings endpoint
|
||||
Given an owner, automatic purge on with interval 7, enabled_at 2026-09-20T09:00:00Z and a planned group
|
||||
And the fake clock reads 2026-10-05T09:00:00Z
|
||||
When Tick runs
|
||||
And the client sends GET /v1/media/purge-settings with a valid cookie of the owner
|
||||
Then "last_purge_at" is "2026-10-05T09:00:00Z"
|
||||
And "last_purge_merged" and "last_purge_articles_updated" hold the run's counts
|
||||
And "next_purge_at" is "2026-10-12T09:00:00Z"
|
||||
And "auto_purge_enabled", "auto_purge_interval_days" and "enabled_at" are unchanged
|
||||
|
||||
Scenario: A scheduled run with nothing to purge shows zeros, not "Never run"
|
||||
Given automatic purge on and due and no planned group
|
||||
When Tick runs and the client sends GET /v1/media/purge-settings
|
||||
Then "last_purge_at" is "2026-10-05T09:00:00Z", "last_purge_merged" is 0 and "last_purge_articles_updated" is 0
|
||||
|
||||
Scenario: A scheduled run answers no HTTP request and returns no message
|
||||
When Tick runs
|
||||
Then no HTTP handler is involved and the only observable results are the stored settings, the images, the article bodies and a log line
|
||||
|
||||
Rule: Layering and documentation
|
||||
|
||||
Scenario: The scheduler stays inside the domain boundary
|
||||
Then PurgeSchedulerUseCase imports only the stdlib, internal/domain/* and package-local use case types
|
||||
And internal/infrastructure/scheduler imports no persistence, mailer, models or gorm package
|
||||
And only api/dependecyInjection.go constructs it and only cmd/server/main.go starts it
|
||||
|
||||
Scenario: The docs describe the scheduler and state that there is no env var
|
||||
Then docs/architecture.md describes the scheduler, the hourly tick, the startup tick, the due rule, the overlap guard and the unbounded shutdown wait
|
||||
And docs/configuration.md states that automatic purge has no env var and that its schedule lives in the database
|
||||
And no new Go, TS, docs or config file contains a comment
|
||||
|
||||
@manual
|
||||
Rule: Wiring verified by hand (see the qa.md)
|
||||
|
||||
Scenario: The runner starts with the real server and stops on SIGINT or SIGTERM
|
||||
Given the server runs and automatic purge is on and due
|
||||
When one minute elapses after startup
|
||||
Then a purge ran and the Settings "Last purge" line shows the new date on the next visit
|
||||
When SIGTERM is sent during a run
|
||||
Then the process exits only after the run finished, and the SQLite file is not corrupted
|
||||
@@ -0,0 +1,56 @@
|
||||
# QA procedure: media_purge_scheduler
|
||||
|
||||
Package: `test` (black-box). New file `test/media_purge_scheduler_test.go`. Go only: no SPA. No env var. Fake type names must be unique in the package. Reuse part 1's `mockPurgeSettingsRepo`, `mockImageRepo`, `mockArticleRepo`, `seedGroup` and `articleUsing` from test/media_purge_test.go and test/media_purge_http_test.go. No real sleeping, no `time.Ticker`, no `time.Sleep`, no live server, no real SQLite file.
|
||||
|
||||
## Setup shared by every case
|
||||
1. `validator.InitValidator()` once (only needed by the HTTP cases).
|
||||
2. Fakes:
|
||||
- `fakeClock` (mutable, `Now()` and `Set(t)`), starting at 2026-10-05T09:00:00Z. Pass `clock.Now` as the `now func() time.Time` of RunPurge and UpdatePurgeSettings so last_purge_at and enabled_at come from it.
|
||||
- `mockPurgeSettingsRepo` with `Get`, `Save` and `SaveLastRun` (writes only the last_purge_* fields), failure switches for each, a write counter shared by Save and SaveLastRun, and a hook called before SaveLastRun writes.
|
||||
- A `fakePurgeRunPort` wrapping the real `RunPurgeUseCase`. It counts calls, records the fingerprint argument (must be nil), and can be armed to return an error. A second variant uses the real RunPurge directly for the integration cases.
|
||||
- Hand-fed `tickCh` and `startCh` channels passed to the runner in `internal/infrastructure/scheduler` in place of time.NewTicker and the startup delay. Use a `bytes.Buffer` logger.
|
||||
3. Wire the real `PurgeSchedulerUseCase` over the settings fake, the run port and the clock.
|
||||
|
||||
## Tick decisions (table-driven)
|
||||
4. Off never runs: off with ancient dates; the defaults; assert ran false, err nil, zero RunPurge calls, zero Save calls.
|
||||
5. Not due, due, boundary: enabled_at + interval one second after now gives no run; exactly now gives a run; assert the fingerprint argument is nil.
|
||||
6. Anchor table: the six rows of the outline. Cross-check each row against `Settings.NextPurgeAt()` to prove the scheduler reuses it.
|
||||
7. Enabled with unset enabled_at never runs and the scheduler never stamps it.
|
||||
8. Repeat the same Tick twice with the real RunPurge: one run (anchor moved), and `last_purge_at` equals the clock. Advance the clock past the interval: a third Tick runs.
|
||||
9. Empty library: due Tick records `last_purge_at` = clock, merged 0, articles_updated 0, and leaves auto_purge_enabled, interval and enabled_at untouched.
|
||||
10. Settings changes: shorten the interval (via `UpdatePurgeSettings` on the same fake) and a run becomes due; lengthen it and a due run is postponed; toggle off and no run; toggle off then on gives enabled_at = clock, no run now, a run once the clock passes +30 days. Assert `Get` is called once per Tick.
|
||||
|
||||
## Failures
|
||||
11. `failGet`: Tick returns the error, RunPurge count 0, the next Tick with the fake healed runs.
|
||||
12. RunPurge error (`ListDuplicates` failure): error returned, `last_purge_at` unchanged, one RunPurge call per Tick, recovery on the next Tick.
|
||||
13. `SaveLastRun` failure after merges: error returned, merged images still gone, next Tick converges.
|
||||
14. Partial failure (UpdateBody failing for group B): ran true, err nil, `last_purge_at` set, counters count group A only.
|
||||
15. Busy: block a manual `RunPurge.Run(nil)` inside the image fake (started channel). Call Tick with a due schedule and assert it returns promptly (use a timeout) with `errors.Is(err, purge.ErrPurgeBusy)`, with no `Save` from the scheduler. Release; the next Tick is not due because the manual run recorded `last_purge_at`. Run with `-race`.
|
||||
|
||||
## Runner
|
||||
16. Feed a startup signal: exactly one Tick, with the clock's time; no Tick before the signal.
|
||||
17. Feed three ticks: three Ticks. Feed none: no Tick. Assert no Tick between fed ticks.
|
||||
18. Error then success from Tick: the error text is in the logger buffer, the loop continues, the second Tick executes, and `Run` has not returned.
|
||||
19. Busy from Tick: no error line in the logger; the loop continues.
|
||||
19b. Completed run: Tick returns ran true with a known `purge.RunResult`; the logger buffer holds exactly one line with groups_purged, merged, articles_updated and failed_groups. An empty result logs zeros.
|
||||
19c. Tick result: a due Tick returns the run result; a Tick that is not due returns the empty result.
|
||||
19d. Lost update: block the settings fake just before `SaveLastRun` writes, PATCH auto_purge_enabled off and the interval to 5 through `UpdatePurgeSettings`, release; the stored auto_purge_* survive and last_purge_* are the run's. The persistence repository test checks that `SaveLastRun` leaves the other columns untouched and creates the default row when none exists.
|
||||
20. Cancel the context while idle: `Run` returns within a timeout; then feed a tick and assert no Tick happens. Cancel before the startup signal: no Tick at all.
|
||||
21. Shutdown waits without a bound: block RunPurge in the image fake, cancel the context, assert `Run` has not returned (select with a short timeout), release, assert `Run` returns and `last_purge_at` was recorded. `-race`.
|
||||
22. The runner and the use case spawn no goroutine that outlives `Run` (verify with a done channel closed on return).
|
||||
|
||||
## HTTP-observable check (existing settings endpoint)
|
||||
23. Mount the part 1 media router (same setup as media_purge.qa.md) over the same fakes. Seed on, interval 7, enabled_at 2026-09-20T09:00:00Z, one planned group. Tick, then GET `/v1/media/purge-settings` with the owner cookie. Assert `last_purge_at` 2026-10-05T09:00:00Z, counts equal the run, `next_purge_at` 2026-10-12T09:00:00Z, and the other fields unchanged. Repeat with an empty library: zeros and a non-null `last_purge_at`.
|
||||
|
||||
## Layering guards
|
||||
24. The scheduler use case imports no `net/http`, chi, gorm, `persistence`, `mailer` or `api/...`. Same grep-style test as media_purge.qa.md. `internal/infrastructure/scheduler` imports no `persistence`, `mailer`, `models` or `gorm`, and no `api/...`.
|
||||
25. Grep the new and edited files (Go, docs) for comments: none; only tool directives.
|
||||
|
||||
## Docs
|
||||
26. `docs/architecture.md` has a scheduler paragraph (hourly tick, startup tick, due rule from `NextPurgeAt`, overlap guard from RunPurge, unbounded shutdown wait, failures retried at the next tick). `docs/configuration.md` states that there is no env var and that the schedule lives in the database. Read both files and confirm.
|
||||
|
||||
## Manual (not automated)
|
||||
27. Real boot with `task dev:api` or the built binary: turn automatic purge on, set interval 1 and backdate `enabled_at` in the SQLite file (or wait); about a minute after start, a purge runs, `GET /v1/media/purge-settings` shows the new `last_purge_at`, and Settings "Last purge" is updated on the next visit.
|
||||
28. Send SIGINT or SIGTERM during a long run: the process exits only after the run ended, with no database error on close.
|
||||
29. A scheduled run and a manual "Purge now" at the same moment: one wins, the other answers 409 busy (HTTP) or is skipped (scheduler).
|
||||
30. Docker: the same behaviour in the container, with state on /data.
|
||||
@@ -0,0 +1,160 @@
|
||||
Feature: Optional MySQL/MariaDB database driver
|
||||
An operator who cannot use a local SQLite file runs RenewCMS against MySQL or
|
||||
MariaDB by setting environment variables. SQLite remains the default and
|
||||
behaves exactly as before.
|
||||
|
||||
Background:
|
||||
Given the database drivers are registered by name in database.drivers
|
||||
And the database is chosen by the DB_DRIVER environment variable
|
||||
And repositories, use cases and handlers are unchanged
|
||||
|
||||
Scenario Outline: Driver selection
|
||||
Given DB_DRIVER is "<value>"
|
||||
When the server starts
|
||||
Then the outcome is "<outcome>"
|
||||
|
||||
Examples:
|
||||
| value | outcome |
|
||||
| unset | the sqlite driver opens DB_FILE |
|
||||
| sqlite | the sqlite driver opens DB_FILE |
|
||||
| mysql | the mysql driver opens the DB_HOST connection |
|
||||
| oracle | startup fails with: unsupported database driver "oracle" |
|
||||
|
||||
Scenario Outline: Missing MySQL variable
|
||||
Given DB_DRIVER is "mysql"
|
||||
And "<missing>" is not set
|
||||
When the environment is validated
|
||||
Then startup fails with "Environment variable <missing> is not set"
|
||||
|
||||
Examples:
|
||||
| missing |
|
||||
| DB_HOST |
|
||||
| DB_USER |
|
||||
| DB_PASSWORD |
|
||||
| DB_NAME |
|
||||
|
||||
Scenario: SQLite does not require MySQL variables
|
||||
Given DB_DRIVER is "sqlite"
|
||||
And DB_HOST, DB_USER, DB_PASSWORD and DB_NAME are not set
|
||||
When the environment is validated
|
||||
Then validation succeeds
|
||||
|
||||
Scenario Outline: DSN built from variables
|
||||
Given DB_HOST "<host>", DB_PORT "<port>", DB_USER "app", DB_NAME "cms", DB_TLS "<tls>"
|
||||
When the MySQL DSN is built
|
||||
Then the DSN targets "<host>:<expectedPort>" and database "cms"
|
||||
And it contains parseTime=true, loc=UTC, charset=utf8mb4, clientFoundRows=true
|
||||
And it contains tls="<tls>"
|
||||
|
||||
Examples:
|
||||
| host | port | expectedPort | tls |
|
||||
| db.local | | 3306 | false |
|
||||
| db.local | 3307 | 3307 | true |
|
||||
| mysql | 3306 | 3306 | skip-verify |
|
||||
|
||||
Scenario: Invalid TLS option is rejected
|
||||
Given DB_DRIVER is "mysql" and DB_TLS is "maybe"
|
||||
When the settings are read
|
||||
Then startup fails naming DB_TLS and the accepted values
|
||||
|
||||
Scenario Outline: Fail fast when the server cannot be used
|
||||
Given DB_DRIVER is "mysql" and "<fault>"
|
||||
When the server starts
|
||||
Then startup stops with "Unable to open the database"
|
||||
And the message names the host and port
|
||||
And the message does not contain the DB_PASSWORD value
|
||||
|
||||
Examples:
|
||||
| fault |
|
||||
| nothing listens on DB_PORT |
|
||||
| the password is wrong |
|
||||
| the database does not exist |
|
||||
|
||||
Scenario: Data directory with MySQL
|
||||
Given DB_DRIVER is "mysql" and DATA_DIR is "/data"
|
||||
Then update state, staging and backups live in "/data"
|
||||
And DB_FILE is ignored
|
||||
|
||||
Scenario Outline: DataDir resolution
|
||||
Given DB_FILE "<dbfile>" and DATA_DIR "<datadir>"
|
||||
Then Settings.DataDir() is "<result>"
|
||||
|
||||
Examples:
|
||||
| dbfile | datadir | result |
|
||||
| ./data/renewcms.db | | ./data |
|
||||
| /var/lib/cms/cms.db | | /var/lib/cms |
|
||||
| ./data/renewcms.db | /srv/x | /srv/x |
|
||||
|
||||
Scenario: Connection pool
|
||||
Given DB_MAX_OPEN_CONNS 4, DB_MAX_IDLE_CONNS 2 and DB_CONN_MAX_LIFETIME_SECONDS 60
|
||||
When the MySQL connection is opened
|
||||
Then the pool limits are 4 open, 2 idle and 60 seconds
|
||||
|
||||
Scenario: Migration on MySQL
|
||||
Given an empty MySQL database
|
||||
When database.Migrate runs
|
||||
Then the tables articles, images, users, invites, api_keys and purge_settings exist
|
||||
And the unique indexes on users and api_keys exist
|
||||
And the first user lacking a role becomes "owner" and the others "admin"
|
||||
|
||||
Scenario: SQLite schema is untouched by the model changes
|
||||
Given a SQLite file created by the models before this change, holding rows
|
||||
When database.Migrate runs
|
||||
Then the schema and every row are unchanged
|
||||
|
||||
Scenario Outline: Repository behaviour is the same on both drivers
|
||||
Given the "<driver>" driver
|
||||
Then the repository contract "<case>" holds
|
||||
|
||||
Examples:
|
||||
| driver | case |
|
||||
| sqlite | create user and read it back with its id and timestamps |
|
||||
| mysql | create user and read it back with its id and timestamps |
|
||||
| sqlite | username "Bob" and "bob" are distinct users |
|
||||
| mysql | username "Bob" and "bob" are distinct users |
|
||||
| sqlite | duplicate email returns an error and stores nothing |
|
||||
| mysql | duplicate email returns an error and stores nothing |
|
||||
| sqlite | unknown id returns domain ErrUserNotFound |
|
||||
| mysql | unknown id returns domain ErrUserNotFound |
|
||||
| sqlite | accept invite twice: the second returns ErrInviteNotFound |
|
||||
| mysql | accept invite twice: the second returns ErrInviteNotFound |
|
||||
| sqlite | accept invite with a taken username leaves the invite unaccepted |
|
||||
| mysql | accept invite with a taken username leaves the invite unaccepted |
|
||||
| sqlite | renew invite with unchanged values still succeeds |
|
||||
| mysql | renew invite with unchanged values still succeeds |
|
||||
| sqlite | duplicate image hashes are grouped and counted |
|
||||
| mysql | duplicate image hashes are grouped and counted |
|
||||
| sqlite | media search treats % and _ literally |
|
||||
| mysql | media search treats % and _ literally |
|
||||
| sqlite | article body of 1 MB round-trips |
|
||||
| mysql | article body of 1 MB round-trips |
|
||||
| sqlite | zero reset expiration round-trips as a zero time |
|
||||
| mysql | zero reset expiration round-trips as a zero time |
|
||||
| sqlite | purge settings save, then SaveLastRun with identical values succeeds |
|
||||
| mysql | purge settings save, then SaveLastRun with identical values succeeds |
|
||||
|
||||
Scenario Outline: Backup before an update
|
||||
Given the "<driver>" driver with a populated database
|
||||
When the pre-update backup runs
|
||||
Then a file "<pattern>" exists in DATA_DIR/backups
|
||||
And only the 3 newest backups are kept
|
||||
And a failure returns an error, so the update download fails instead of claiming a backup
|
||||
|
||||
Examples:
|
||||
| driver | pattern |
|
||||
| sqlite | renewcms-<time>.db |
|
||||
| mysql | renewcms-<time>.sql |
|
||||
|
||||
Scenario: MySQL backup content
|
||||
Given a MySQL database with 2 users and 1 article
|
||||
When the pre-update backup runs
|
||||
Then the dump contains the schema and the rows of every table
|
||||
And it does not contain the DB_PASSWORD value
|
||||
And replaying it into an empty database restores the same rows
|
||||
|
||||
Scenario: Secrets are never exposed
|
||||
Given DB_DRIVER is "mysql"
|
||||
Then no log line, error message or file in the repository contains the DB_PASSWORD value
|
||||
|
||||
Scenario: Static builds
|
||||
Then the module builds with CGO_ENABLED=0 for the 6 task release targets
|
||||
@@ -0,0 +1,42 @@
|
||||
# QA: MySQL/MariaDB database driver
|
||||
|
||||
Backend only. Go tests in test/, black-box, no live server by default.
|
||||
|
||||
Decisions: separate DB_* vars, new DATA_DIR (DB_FILE ignored under mysql), pure-Go logical dump backup, `task test:mysql` against mysql:8.4 and mariadb:11, SQLite-to-MySQL data migration documented as manual export/import only.
|
||||
|
||||
## Automated, no server (task test)
|
||||
1. Registry: Connect with Driver "sqlite" works against a temp file; Driver "oracle" returns `unsupported database driver "oracle"`; the registry contains sqlite and mysql.
|
||||
2. Env validation: table-driven over the Driver selection and Missing MySQL variable scenarios. SQLite needs no DB_* vars. DB_DRIVER defaults to sqlite. An invalid DB_TLS or a non-numeric pool var returns an error naming the variable.
|
||||
3. DSN builder: table-driven over the DSN scenario. Assert host:port, database, parseTime, loc=UTC, charset=utf8mb4, clientFoundRows, tls. Assert that the redacted form used in errors never contains the password.
|
||||
4. Settings.DataDir: table-driven (DB_FILE only, DATA_DIR overrides).
|
||||
5. Fail fast without a server: point mysql at a closed local port with a 1s timeout. Assert the error mentions the host and port, and not the password.
|
||||
6. Pool settings: assert sql.DB stats MaxOpenConnections via a stub opener, or via the real mysql test below.
|
||||
7. SQLite regression: build a database file using the pre-change schema (a fixture created with the old model definitions), run Migrate, and assert that the schema (sqlite_master) and the rows are unchanged. The existing suite must pass unchanged.
|
||||
8. Backup (SQLite): existing tests unchanged.
|
||||
9. Dump writer: unit-test the SQL dump formatter with a fake row source (quoting, NULL, time, bool, binary-safe strings, 3-file retention, existing-file handling, failure leaves no partial file).
|
||||
|
||||
## Automated, real server (task test:mysql)
|
||||
Setup: the task runs `docker run` for mysql:8.4 and, in a second pass, mariadb:11, waits for readiness, exports RENEWCMS_TEST_MYSQL_DSN, runs `go test -tags nofrontend ./test/... -run MySQL -count=1`, then removes the container. Tests call t.Skip when the var is unset, so `task test` skips them. The repository tests are parameterised over both drivers (SQLite temp file and the MySQL DSN, each with a fresh database), and run the "Repository behaviour is the same on both drivers" Examples.
|
||||
10. Migrate on an empty database; table and index existence via information_schema; the backfill role rules.
|
||||
11. User create/read, case-sensitive username and email, duplicate email, ErrUserNotFound.
|
||||
12. Invite accept twice, accept with a taken username (invite stays unaccepted), renew with unchanged values.
|
||||
13. Image duplicates grouping and counts, search with % and _, filename backfill listing.
|
||||
14. Article body 1 MB, title lookup, online filter.
|
||||
15. API key unique hashed_key, revoke, last_used.
|
||||
16. Zero-time round trip, UTC times.
|
||||
17. Purge settings upsert.
|
||||
18. Backup: run on a populated MySQL database, assert the .sql file, the 3-file retention, a replay into a second empty database giving identical rows, and that the file contains no password.
|
||||
19. Wrong password and missing database fail with the "Unable to open the database" prefix and no password in the message.
|
||||
|
||||
## Build and lint
|
||||
20. `task lint:go` and `task test` pass. `task test:mysql` passes on MySQL 8.4 and MariaDB 11 (record both; NOT RUN if Docker is unavailable).
|
||||
21. `CGO_ENABLED=0 go build -tags nofrontend ./...` passes. Cross-compile the 6 `task release` targets (e.g. `GOOS=linux GOARCH=arm64`) with CGO_ENABLED=0.
|
||||
22. `task mutate` impact: none, because it only targets useCases. No change.
|
||||
23. No comment lines in the added Go files (the hook enforces this).
|
||||
|
||||
## Manual (needs Compose and real SMTP)
|
||||
24. docker-compose.mysql.yml example: renewcms plus a mysql service with a named volume and a healthcheck, `depends_on: condition: service_healthy`, DB_DRIVER=mysql, DB_HOST=mysql, DATA_DIR=/data. `docker compose up`: /healthz returns 200, register, log in, create an article, restart the stack, and the data persists.
|
||||
25. Stop the mysql container and start renewcms: it exits with a clear error and no password in the logs. docker.env.example holds placeholders only.
|
||||
26. In the Docker image (non-root uid 10001), trigger the backup path and confirm the .sql file lands under /data/backups.
|
||||
27. Docs: configuration.md, docker.md, deployment.md, `.env.example`, `docker.env.example` and the architecture.md "Storage and database seams" section list the new vars and the manual SQLite to MySQL export/import note.
|
||||
28. Verify the SQLite default end to end with no new vars set: same behaviour as before.
|
||||
@@ -0,0 +1,153 @@
|
||||
Feature: Optional PostgreSQL database driver
|
||||
An operator who prefers PostgreSQL runs RenewCMS against it by setting
|
||||
environment variables. SQLite stays the default, and MySQL/MariaDB behaves
|
||||
exactly as before.
|
||||
|
||||
Background:
|
||||
Given the database drivers are registered by name in database.drivers
|
||||
And the database is chosen by the DB_DRIVER environment variable
|
||||
And repositories, use cases and handlers are unchanged
|
||||
|
||||
Scenario Outline: Driver selection
|
||||
Given DB_DRIVER is "<value>"
|
||||
When the server starts
|
||||
Then the outcome is "<outcome>"
|
||||
|
||||
Examples:
|
||||
| value | outcome |
|
||||
| unset | the sqlite driver opens DB_FILE |
|
||||
| sqlite | the sqlite driver opens DB_FILE |
|
||||
| mysql | the mysql driver opens the DB_HOST connection |
|
||||
| postgres | the postgres driver opens the DB_HOST connection |
|
||||
| postgresql | the postgres driver opens the DB_HOST connection |
|
||||
| oracle | startup fails with: unsupported database driver "oracle" |
|
||||
|
||||
Scenario Outline: Missing PostgreSQL variable
|
||||
Given DB_DRIVER is "postgres"
|
||||
And "<missing>" is not set
|
||||
When the environment is validated
|
||||
Then startup fails with "Environment variable <missing> is not set"
|
||||
|
||||
Examples:
|
||||
| missing |
|
||||
| DB_HOST |
|
||||
| DB_USER |
|
||||
| DB_PASSWORD |
|
||||
| DB_NAME |
|
||||
|
||||
Scenario: SQLite and MySQL do not require PostgreSQL-only variables
|
||||
Given DB_DRIVER is "sqlite" or "mysql"
|
||||
And DB_SSLMODE is "maybe"
|
||||
When the settings are read
|
||||
Then DB_SSLMODE is ignored and validation succeeds
|
||||
|
||||
Scenario Outline: Connection string built from variables
|
||||
Given DB_HOST "<host>", DB_PORT "<port>", DB_USER "app", DB_NAME "cms", DB_SSLMODE "<sslmode>"
|
||||
When the PostgreSQL DSN is built
|
||||
Then the DSN targets "<host>:<expectedPort>" and database "cms"
|
||||
And it sets sslmode "<expectedSslmode>"
|
||||
And it sets the session time zone to UTC
|
||||
And a password containing spaces, quotes, "@" or "/" is escaped so that parsing the DSN returns it unchanged
|
||||
|
||||
Examples:
|
||||
| host | port | expectedPort | sslmode | expectedSslmode |
|
||||
| db.local | | 5432 | | disable |
|
||||
| db.local | 5433 | 5433 | require | require |
|
||||
| pg | 5432 | 5432 | verify-full | verify-full |
|
||||
|
||||
Scenario: Invalid SSL mode is rejected
|
||||
Given DB_DRIVER is "postgres" and DB_SSLMODE is "maybe"
|
||||
When the settings are read
|
||||
Then startup fails naming DB_SSLMODE and the accepted values
|
||||
|
||||
Scenario Outline: Fail fast when the server cannot be used
|
||||
Given DB_DRIVER is "postgres" and "<fault>"
|
||||
When the server starts
|
||||
Then startup stops with "Unable to open the database"
|
||||
And the message names the host and port
|
||||
And the message does not contain the DB_PASSWORD value
|
||||
|
||||
Examples:
|
||||
| fault |
|
||||
| nothing listens on DB_PORT |
|
||||
| the password is wrong |
|
||||
| the database does not exist |
|
||||
| the server requires TLS and DB_SSLMODE is disable |
|
||||
|
||||
Scenario: Data directory with PostgreSQL
|
||||
Given DB_DRIVER is "postgres" and DATA_DIR is "/data"
|
||||
Then update state, staging and backups live in "/data"
|
||||
And DB_FILE is ignored
|
||||
|
||||
Scenario: Connection pool
|
||||
Given DB_MAX_OPEN_CONNS 4, DB_MAX_IDLE_CONNS 2 and DB_CONN_MAX_LIFETIME_SECONDS 60
|
||||
When the PostgreSQL connection is opened
|
||||
Then the pool limits are 4 open, 2 idle and 60 seconds
|
||||
|
||||
Scenario: Migration on PostgreSQL
|
||||
Given an empty PostgreSQL database
|
||||
When database.Migrate runs
|
||||
Then the tables articles, images, users, invites, api_keys and purge_settings exist in schema "public"
|
||||
And the unique indexes on users and api_keys exist
|
||||
And the first user lacking a role becomes "owner" and the others "admin"
|
||||
And running database.Migrate a second time changes nothing
|
||||
|
||||
Scenario: Existing drivers are untouched by the PostgreSQL addition
|
||||
Given a SQLite file holding rows, and a MySQL database holding rows
|
||||
When database.Migrate runs on each
|
||||
Then the schema and every row are unchanged on both
|
||||
|
||||
Scenario Outline: Repository behaviour is the same on every driver
|
||||
Given the "<driver>" driver
|
||||
Then the repository contract "<case>" holds
|
||||
|
||||
Examples:
|
||||
| driver | case |
|
||||
| postgres | create user and read it back with its id and timestamps |
|
||||
| postgres | username "Bob" and "bob" are distinct users |
|
||||
| postgres | email "A@x.io" and "a@x.io" follow the same rule as on sqlite |
|
||||
| postgres | duplicate email returns an error and stores nothing |
|
||||
| postgres | the failed duplicate insert leaves the connection usable |
|
||||
| postgres | unknown id returns domain ErrUserNotFound |
|
||||
| postgres | accept invite twice: the second returns ErrInviteNotFound |
|
||||
| postgres | accept invite with a taken username leaves the invite unaccepted |
|
||||
| postgres | renew invite with unchanged values still succeeds |
|
||||
| postgres | duplicate image hashes are grouped and counted |
|
||||
| postgres | media search treats % and _ literally |
|
||||
| postgres | media search is case-sensitive exactly as on sqlite |
|
||||
| postgres | article body of 1 MB round-trips |
|
||||
| postgres | article title with emoji and accents round-trips |
|
||||
| postgres | zero reset expiration round-trips as a zero time |
|
||||
| postgres | stored times read back in UTC |
|
||||
| postgres | purge settings save, then SaveLastRun with identical values succeeds |
|
||||
| postgres | api key unique hashed_key, revoke and last_used |
|
||||
| postgres | offline article reads as not found publicly, online article is listed |
|
||||
| postgres | listing order and pagination match sqlite for the same data |
|
||||
|
||||
Scenario Outline: Backup before an update
|
||||
Given the "<driver>" driver with a populated database
|
||||
When the pre-update backup runs
|
||||
Then a file "<pattern>" exists in DATA_DIR/backups
|
||||
And only the 3 newest backups are kept
|
||||
And a failure returns an error, so the update download fails instead of claiming a backup
|
||||
|
||||
Examples:
|
||||
| driver | pattern |
|
||||
| sqlite | renewcms-<time>.db |
|
||||
| mysql | renewcms-<time>.sql |
|
||||
| postgres | renewcms-<time>.sql |
|
||||
|
||||
Scenario: PostgreSQL backup content
|
||||
Given a PostgreSQL database with 2 users and 1 article
|
||||
When the pre-update backup runs
|
||||
Then the dump contains the schema, indexes and the rows of every table
|
||||
And it does not contain the DB_PASSWORD value
|
||||
And replaying it into an empty database restores the same rows
|
||||
And sequences are reset so the next insert gets an unused id
|
||||
|
||||
Scenario: Secrets are never exposed
|
||||
Given DB_DRIVER is "postgres"
|
||||
Then no log line, error message or file in the repository contains the DB_PASSWORD value
|
||||
|
||||
Scenario: Static builds
|
||||
Then the module builds with CGO_ENABLED=0 for the 6 task release targets
|
||||
@@ -0,0 +1,43 @@
|
||||
# QA: PostgreSQL database driver
|
||||
|
||||
Backend only. Go tests in test/, black-box, no live server by default.
|
||||
|
||||
Decisions: DB_DRIVER=postgres (alias postgresql), gorm.io/driver/postgres (pgx, pure Go), shared DB_* vars plus DB_SSLMODE, pure-Go logical dump backup, `task test:postgres` against postgres:16 and postgres:17, SQLite or MySQL to PostgreSQL migration is a documented manual export/import only.
|
||||
|
||||
## Automated, no server (task test)
|
||||
1. Registry: contains sqlite, mysql and postgres. Alias "postgresql" resolves to postgres. "oracle" returns `unsupported database driver "oracle"`.
|
||||
2. Env validation: table-driven over Driver selection and Missing PostgreSQL variable. Invalid DB_SSLMODE or a non-numeric pool var returns an error naming the variable. DB_SSLMODE is ignored under sqlite and mysql. MySQL validation unchanged.
|
||||
3. DSN builder: table-driven over the DSN scenario. Parse the output with the pgx config parser and assert host, port, database, sslmode and the UTC time zone. Round-trip a password such as `s3cr3t/Value@42:x 'q'`. The form used in errors never contains the password.
|
||||
4. Settings.DataDir under postgres: DATA_DIR wins, DB_FILE ignored.
|
||||
5. Fail fast without a server: closed 127.0.0.1 port, 1s connect timeout. Error has the "Unable to open the database" prefix, mentions host and port, not the password.
|
||||
6. Pool settings via a stub opener or the real test below.
|
||||
7. Regression: the existing SQLite and MySQL tests pass unchanged. A pre-change SQLite fixture is unchanged after Migrate.
|
||||
8. Dump writer: reuse the existing formatter tests. Add PostgreSQL value formatting with a fake row source: bool as true/false, bytea hex escapes, timestamptz in UTC, NULL, identifier and string quoting.
|
||||
9. Migrate dialect handling: no MySQL table options are applied under postgres (tableOptionsFor).
|
||||
|
||||
## Automated, real server (task test:postgres)
|
||||
Setup: the task runs `docker run` for postgres:16 then postgres:17, waits with pg_isready, exports RENEWCMS_TEST_POSTGRES_DSN, runs `go test -tags nofrontend ./test/... -run Postgres -count=1`, then removes the container. Tests t.Skip when the variable is unset. Repository contract tests are parameterised over sqlite, mysql and postgres with a fresh database each.
|
||||
10. Migrate on an empty database: tables and indexes via information_schema and pg_indexes, role backfill, idempotent second run.
|
||||
11. User create/read, case-sensitive username and email, duplicate email, ErrUserNotFound. After the unique violation the next query on the same pool succeeds.
|
||||
12. Invite accept twice, accept with a taken username (invite stays unaccepted), renew with unchanged values.
|
||||
13. Image duplicates grouping and counts (GROUP BY strictness), search with % and _ and its case rule, filename backfill listing.
|
||||
14. Article body 1 MB, unicode title, title lookup, online filter, listing order and pagination parity with sqlite.
|
||||
15. API key unique hashed_key, revoke, last_used.
|
||||
16. Zero-time round trip, UTC times.
|
||||
17. Purge settings upsert.
|
||||
18. Backup on a populated database: .sql file, 3-file retention, replay into a second empty database gives identical rows, no password in the file, next insert after replay gets a fresh id.
|
||||
19. Wrong password, missing database and TLS-required-but-disabled fail with the "Unable to open the database" prefix and no password.
|
||||
|
||||
## Build and lint
|
||||
20. `task lint:go` and `task test` pass. `task test:postgres` passes on 16 and 17 (record both; NOT RUN if Docker is unavailable).
|
||||
21. `CGO_ENABLED=0 go build -tags nofrontend ./...` passes. Cross-compile the 6 `task release` targets.
|
||||
22. `task mutate` impact: none (useCases only).
|
||||
23. No comment lines in the added Go files.
|
||||
24. Layering: `go list -deps` on useCases and domain shows no pgx or gorm. Only api/dependecyInjection.go builds the postgres config.
|
||||
|
||||
## Manual (needs Compose and real SMTP)
|
||||
25. docker-compose.postgres.yml: renewcms plus a postgres service with a named volume and a pg_isready healthcheck, depends_on service_healthy, DB_DRIVER=postgres, DB_HOST=postgres, DATA_DIR=/data. `docker compose up`: /healthz 200, register, log in, create an article, restart the stack, data persists.
|
||||
26. Stop the postgres container and start renewcms: clear error, no password in the logs.
|
||||
27. In the Docker image (uid 10001), trigger the backup path and confirm the .sql file lands under /data/backups.
|
||||
28. Docs: .env.example, docker.env.example, docs/configuration.md, docs/docker.md, docs/deployment.md, docs/development.md and the docs/architecture.md "Storage and database seams" section list DB_DRIVER=postgres, DB_SSLMODE and the manual migration note. DB_SSLMODE goes into cmd/server/main.go envDefaults.
|
||||
29. SQLite default end to end with no new vars set behaves as before.
|
||||
@@ -0,0 +1,143 @@
|
||||
Feature: Uploaded images can be stored in an S3-compatible bucket
|
||||
|
||||
As an operator whose host disk is ephemeral or who runs several hosts
|
||||
I want to set STORAGE_DRIVER=s3 and bucket settings
|
||||
So that uploads live in the bucket while images are still served by the app
|
||||
at /static/uploadedImages/<name>, with unchanged URLs
|
||||
|
||||
Background:
|
||||
Given STORAGE_DRIVER defaults to "local" and local disk behaves exactly as today
|
||||
And an S3 storage implements image.Storage (Put, Open, Delete) in internal/infrastructure/storage
|
||||
And storage.Open has a case for the driver "s3" and openImageStorage selects it from the settings
|
||||
And no domain, use case, repository or staticimages handler file changes
|
||||
And the bucket is faked by an in-process httptest S3 server listening on 127.0.0.1
|
||||
And Open returns a Blob whose Content is an io.ReadSeeker, so http.ServeContent handles Range and conditional requests
|
||||
And the Blob ModTime and Size come from the S3 object metadata
|
||||
And no ETag is emitted and conditional requests are validated by Last-Modified only
|
||||
|
||||
Scenario Outline: Startup configuration validation
|
||||
Given STORAGE_DRIVER is <driver> and the S3 variables are <vars>
|
||||
When the storage settings are validated
|
||||
Then the result is <result>
|
||||
|
||||
Examples:
|
||||
| driver | vars | result |
|
||||
| (unset) | none | driver local, no S3 variable required |
|
||||
| local | S3_BUCKET set | driver local, S3 variables ignored |
|
||||
| s3 | S3_BUCKET and S3_REGION set | valid, default AWS credential chain |
|
||||
| s3 | S3_BUCKET set, S3_REGION unset | region defaults to us-east-1 |
|
||||
| s3 | S3_REGION set, S3_BUCKET unset | error "Environment variable S3_BUCKET is not set" |
|
||||
| s3 | key id set, secret unset | error naming S3_SECRET_ACCESS_KEY |
|
||||
| s3 | secret set, key id unset | error naming S3_ACCESS_KEY_ID |
|
||||
| s3 | UPLOAD_DIR set | valid, UPLOAD_DIR ignored without error |
|
||||
| gcs | none | error unsupported storage driver "gcs" |
|
||||
|
||||
Scenario: The registry opens the S3 storage after a bucket check
|
||||
Given the fake S3 answers HEAD bucket with 200
|
||||
When storage.Open is called with driver "s3"
|
||||
Then it returns a storage and no error
|
||||
|
||||
Scenario Outline: Startup fails fast with a clear secret-free error
|
||||
Given the fake S3 answers HEAD bucket with <status>
|
||||
When storage.Open is called with driver "s3" and secret access key "TOPSECRET"
|
||||
Then it returns an error mentioning the bucket name
|
||||
And the error text does not contain "TOPSECRET"
|
||||
And openImageStorage panics with that same text
|
||||
|
||||
Examples:
|
||||
| status |
|
||||
| 404 |
|
||||
| 403 |
|
||||
| 500 |
|
||||
|
||||
Scenario: Put stores the object under the prefix with its content type
|
||||
Given S3_KEY_PREFIX is "uploads/"
|
||||
When Put is called with stored name "a1b2.png", content type "image/png" and 3 bytes
|
||||
Then the fake S3 holds key "uploads/a1b2.png" with those bytes and Content-Type "image/png"
|
||||
|
||||
Scenario: Put failure is returned as an error
|
||||
Given the fake S3 answers PUT with 500
|
||||
When Put is called
|
||||
Then Put returns a non-nil error that is not image.ErrBlobNotFound
|
||||
|
||||
Scenario: Open returns content, size and modification time
|
||||
Given the fake S3 holds "uploads/a1b2.png" with 10 bytes and a known Last-Modified
|
||||
When Open("a1b2.png") is called
|
||||
Then the Blob has Size 10 and that ModTime
|
||||
And its Content implements io.ReadSeeker and reads the 10 bytes
|
||||
|
||||
Scenario: Open of a missing key is not found
|
||||
Given the fake S3 answers GET with 404 NoSuchKey
|
||||
When Open("missing.png") is called
|
||||
Then the result is image.ErrBlobNotFound, asserted with errors.Is
|
||||
|
||||
Scenario: Open with a backend failure is not a not-found
|
||||
Given the fake S3 answers GET with 500
|
||||
When Open is called
|
||||
Then the error is non-nil and is not image.ErrBlobNotFound
|
||||
|
||||
Scenario Outline: Delete is idempotent
|
||||
Given the fake S3 <state>
|
||||
When Delete("a1b2.png") is called
|
||||
Then it returns no error
|
||||
|
||||
Examples:
|
||||
| state |
|
||||
| holds the key |
|
||||
| does not hold the key (404) |
|
||||
|
||||
Scenario: A backend failure on Delete is returned
|
||||
Given the fake S3 answers DELETE with 500
|
||||
When Delete is called
|
||||
Then it returns a non-nil error
|
||||
|
||||
Scenario Outline: Serving /static/uploadedImages/<name> from the bucket
|
||||
Given the bucket holds "a1b2.png" with 10 bytes and a known Last-Modified
|
||||
When <method> /static/uploadedImages/a1b2.png is requested <with>
|
||||
Then the status is <status>
|
||||
And the response has <result>
|
||||
|
||||
Examples:
|
||||
| method | with | status | result |
|
||||
| GET | no extra header | 200 | Content-Type image/png, Content-Length 10, Last-Modified, Accept-Ranges bytes, Cache-Control "public, no-cache", X-Content-Type-Options nosniff |
|
||||
| HEAD | no extra header | 200 | the same headers and an empty body |
|
||||
| GET | If-Modified-Since = Last-Modified | 304 | an empty body |
|
||||
| GET | Range bytes=0-3 | 206 | Content-Range "bytes 0-3/10" and Content-Length 4 |
|
||||
| GET | Range bytes=50-60 | 416 | Content-Range "bytes */10" |
|
||||
|
||||
Scenario: SVG from the bucket keeps its security headers
|
||||
Given the bucket holds "logo.svg"
|
||||
When GET /static/uploadedImages/logo.svg is requested
|
||||
Then Content-Type is image/svg+xml
|
||||
And Content-Security-Policy is "default-src 'none'; style-src 'unsafe-inline'; sandbox"
|
||||
|
||||
Scenario: Missing object is 404 and a backend failure is 500
|
||||
Given the bucket does not hold "missing.png"
|
||||
When GET /static/uploadedImages/missing.png is requested
|
||||
Then the status is 404
|
||||
Given the fake S3 answers GET with 500
|
||||
When GET /static/uploadedImages/a1b2.png is requested
|
||||
Then the status is 500 and the plain text body is "Unable to read the file"
|
||||
|
||||
Scenario: Path traversal never reaches another key
|
||||
When GET /static/uploadedImages/../secret.png is requested
|
||||
Then the key requested from the fake S3 is the prefix plus "secret.png"
|
||||
|
||||
Scenario: Handler as-is for a non-seekable blob
|
||||
Given a storage fake returning a Blob whose Content is not an io.ReadSeeker, with Size 10 and a ModTime
|
||||
When GET and HEAD /static/uploadedImages/a1b2.png are requested
|
||||
Then GET returns 200 with Content-Length 10, Last-Modified and the body
|
||||
And HEAD returns 200 with the same headers and an empty body
|
||||
And no Range or ETag behaviour is specified for this path
|
||||
|
||||
Scenario: Boot backfill logs how many images it read from the bucket
|
||||
Given the driver is s3 and 3 stored images lack metadata
|
||||
When InitContainer runs the boot backfill
|
||||
Then a log line reports 3 images read from storage and contains no credential
|
||||
And backfill behaviour and errors are unchanged
|
||||
And no cap is applied and BackfillImageMetadataUseCase is not modified
|
||||
|
||||
Scenario: The local driver is unchanged
|
||||
Given STORAGE_DRIVER is unset and UPLOAD_DIR is a temp directory
|
||||
When an image is uploaded and requested
|
||||
Then it is stored in UPLOAD_DIR and served exactly as before
|
||||
@@ -0,0 +1,26 @@
|
||||
# QA: S3 image storage
|
||||
|
||||
All tests live in test/ (package test). No cloud account, no real SMTP, no real SQLite file. Test names carry "S3Storage".
|
||||
|
||||
Settled decisions: boot backfill gets a log line only, wired in the composition root, with no cap and no use case change. No ETag is emitted; the staticimages handler is unchanged and conditional requests are validated by Last-Modified only.
|
||||
|
||||
## Automated
|
||||
1. Config validation: table test over STORAGE_DRIVER and S3_* lookup functions (the MissingEnvVarError pattern, a `StorageRequiredEnvVars(driver)` helper like `DatabaseRequiredEnvVars`). Covers the validation table, UPLOAD_DIR ignored, unknown driver error.
|
||||
2. Registry: storage.Open with "local" returns local storage, "s3" returns the S3 storage after HEAD bucket, anything else returns the "unsupported storage driver" error.
|
||||
3. Fake S3: an httptest.Server on 127.0.0.1 only (never bind :port) implementing HEAD bucket, PUT, GET with Range, HEAD and DELETE object, with injectable statuses and recorded keys, headers and bodies.
|
||||
4. Adapter tests against the fake: the Put, Open (found, 404, 500) and Delete scenarios, HEAD bucket 404/403/500, prefix handling, ModTime and Size from metadata, Content as io.ReadSeeker (seek then read issues a ranged GET), secret absent from error text.
|
||||
5. Handler tests: staticimages.NewHandler over OpenMediaUseCase over the S3 storage on the fake server, mounted on a fresh chi.NewRouter() at /static/uploadedImages/*. Cover GET, HEAD, 304 via If-Modified-Since, 206, 416, SVG CSP, 404, 500, traversal.
|
||||
6. Handler regression: a storage fake returning a non-seekable Blob. GET and HEAD behave exactly as today.
|
||||
7. Backfill log: the boot log line for the number of images read, captured with a log writer, containing no credential.
|
||||
8. Local regression: all existing image tests pass unchanged.
|
||||
9. Gates: `task test` and `task lint:go` pass. `task mutate` is not part of this slice, but the new storage package becomes a target if the mutation scope is widened. The 404 to ErrBlobNotFound and idempotent-Delete branches need killing tests.
|
||||
|
||||
## Optional integration (task test:s3)
|
||||
Start a SeaweedFS container with Docker, create a bucket, set RENEWCMS_TEST_S3_ENDPOINT and run the adapter tests against it. Skip with a message when Docker is unavailable. Not part of `task test`.
|
||||
|
||||
## Manual
|
||||
- Run docker-compose.s3.yml, upload an image in the SPA, confirm the object is in the bucket and /static/uploadedImages/<name> serves it, including after the container is recreated.
|
||||
- Real AWS S3, R2, B2 or Scaleway smoke test, and an instance-role credential check.
|
||||
- Start with a wrong bucket and confirm a clear, secret-free startup error.
|
||||
- Sync an old UPLOAD_DIR with `aws s3 sync` or `mc mirror` and confirm old images load.
|
||||
- Confirm docs describe: variables (configuration.md, .env.example, docker.env.example, Docker docs), UPLOAD_DIR ignored with s3, manual migration, update backups covering the DB only and not the bucket, and the updated architecture.md "Storage and database seams" section.
|
||||
@@ -4,22 +4,46 @@ go 1.27.1
|
||||
|
||||
require (
|
||||
github.com/MadAppGang/httplog v1.3.0
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.1
|
||||
github.com/aws/aws-sdk-go-v2/config v1.33.6
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.20.6
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.0
|
||||
github.com/glebarez/sqlite v1.11.0
|
||||
github.com/go-chi/chi/v5 v5.2.3
|
||||
github.com/go-chi/cors v1.2.2
|
||||
github.com/go-chi/jwtauth/v5 v5.3.3
|
||||
github.com/go-playground/validator/v10 v10.28.0
|
||||
github.com/go-sql-driver/mysql v1.8.1
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.10.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/lestrrat-go/jwx/v2 v2.1.6
|
||||
golang.org/x/crypto v0.57.0
|
||||
golang.org/x/image v0.46.0
|
||||
golang.org/x/net v0.59.0
|
||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
|
||||
gorm.io/gorm v1.31.1
|
||||
gorm.io/driver/mysql v1.6.0
|
||||
gorm.io/driver/postgres v1.6.3
|
||||
gorm.io/gorm v1.31.2
|
||||
)
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.20 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.4 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.4 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.4 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.19 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.5 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.4 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.4 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.10.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.38.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.43.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.51.1 // indirect
|
||||
github.com/aws/smithy-go v1.28.1 // indirect
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/fatih/color v1.18.0 // indirect
|
||||
@@ -28,6 +52,9 @@ require (
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
@@ -35,7 +62,6 @@ require (
|
||||
github.com/lestrrat-go/httpcc v1.0.1 // indirect
|
||||
github.com/lestrrat-go/httprc v1.0.6 // indirect
|
||||
github.com/lestrrat-go/iter v1.0.2 // indirect
|
||||
github.com/lestrrat-go/jwx/v2 v2.1.6 // indirect
|
||||
github.com/lestrrat-go/option v1.0.1 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
@@ -43,6 +69,7 @@ require (
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/segmentio/asm v1.2.1 // indirect
|
||||
golang.org/x/exp v0.0.0-20251113190631-e25ba8c21ef6 // indirect
|
||||
golang.org/x/sync v0.23.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
|
||||
|
||||
@@ -1,7 +1,45 @@
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
github.com/MadAppGang/httplog v1.3.0 h1:1XU54TO8kiqTeO+7oZLKAM3RP/cJ7SadzslRcKspVHo=
|
||||
github.com/MadAppGang/httplog v1.3.0/go.mod h1:gpYEdkjh/Cda6YxtDy4AB7KY+fR7mb3SqBZw74A5hJ4=
|
||||
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
|
||||
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.1 h1:uOIZnp4PK3ZhKI0dNrJrhTEsLxbpXHTAJlwoS1pvAtw=
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.1/go.mod h1:bttEH6JqnUL8LepvDVfdrds/fZ5bCIxzpe3abyUrhDU=
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.20 h1:GPRlPwz40I2B2VrBEASOA3Bi77NyeqejNLkifosX0rs=
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.20/go.mod h1:g7PNzKcsOKWb4fkSRBA7BZVAS6Y8IcxzN+nRohhQ1Q8=
|
||||
github.com/aws/aws-sdk-go-v2/config v1.33.6 h1:MBjkSTLczek/UgiK+EYPIoRTqE7gP8vtW3OFbFo7Nug=
|
||||
github.com/aws/aws-sdk-go-v2/config v1.33.6/go.mod h1:grRAFzdAZJrwcbasJRg2MPvIrVjtlfXllHssN6+E1JE=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.20.6 h1:NpAFXCU7NzXNkdGK3zQTtsRJ+3v9tZQV0xcdRw8uBdw=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.20.6/go.mod h1:mcZCoiPnyMvP8VMNbygNX5lLqSlkYJIMPODylQMurOk=
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.1 h1:8gALAAmacnIXh+z6VkdDanv4/IkG5APdg4DZLDTmLog=
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.1/go.mod h1:Z7IJhJU+poOdJjUR2wpyY21ossQ1XS/R3Lk9Msq5kM4=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.4 h1:CLq4+8UHCI+ZZYl/EuJxXovaIVN2xeeT8JV+dsApQ5E=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.4/go.mod h1:Wv4q5sAM04xAMkoOedxLx2inVf6K5FdxYp+A61L+q/0=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.4 h1:dD4MR81I7YkpEBRk6UP9rocC2QnT3qVuXwzlYTtfGEs=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.4/go.mod h1:EcXV1kAFd5XwSkDHlj94gnF3q5CkJyYiIJfH8N0VmrE=
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.4 h1:7Wo47d/xn/7KttCSBd8EGYeZ7ULRFRkUHr6vkZPBzVQ=
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.4/go.mod h1:tDB2IVC1xC3vX8o+6uRlzhTxP3g1b77CZXFX/oD2FnQ=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.19 h1:bAdDl/HkGCcGPoe25ToSHEw23VIxt6CT5fLcg111BKg=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.19/go.mod h1:KaUzbLxv4CeSxh6ZCl9B4m7CuFenS8kUEaDs+f/DQr4=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.5 h1:/TYsZXdA8UTa+WCtCYSAJIr1vwl0+eho6TUgJGwFFO8=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.5/go.mod h1:qPqp1Uwd/BqdhPufv6oem9j5J7HNsgc2V22dUiDPn+s=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.4 h1:29SvnfGhXjTl8ONxFwbj2rs6lbhiFXD2CgFQmbT/bXY=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.4/go.mod h1:wm04I5DMuNVvZHFe/dHnUxincvNbbK7AiNBbYsQivek=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.4 h1:pPiWfgeNxqluKEph7hvU88kuGKBPOWzO+Dk9t2zqqNs=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.4/go.mod h1:YlwGoIUDG/3kBQbdNOVs/xKZ9J01G8e/6D1mRBj9uTk=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.0 h1:VMAdYqr4Jn/8ATs9BHC5riwrs0d6m1Z2ohFriSwZwm0=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.0/go.mod h1:9APRWGLFITKD+xzWSIyT9V7QV4bNlEuIieWlzXgGFlI=
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.10.1 h1:DzCCWLzcIRQ77F3DEUljud7bEjTgFOIKXP52NmVRyhU=
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.10.1/go.mod h1:xpo/geVldu8payT375WekctUzopG/hBU7miiqItMUlw=
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.38.1 h1:Umtl/0YZhng4xndfW3lKJrYYP7NLEjI6bGXVomwLcs0=
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.38.1/go.mod h1:rRD/dnm7q0HYE/I5TMaPgkWyyUGLcwuxHLABsLnQ3e0=
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.43.1 h1:orIWdNiLgzrhu/11RcPPKO/SBzUUymbUQuZbSPImghg=
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.43.1/go.mod h1:skwM/xsbR/1ReUTesv9BhpJp1VjajR7DWQnuVLwiXsQ=
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.51.1 h1:0HOqZXRvMytH6bFHVIc0oJX07sZjfhz0zXtjs6gdE8s=
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.51.1/go.mod h1:26zA0GhDrLo+yiLI2yXWxqB1PdsShfLikoI7GOEgugM=
|
||||
github.com/aws/smithy-go v1.28.1 h1:R/nXH00c8qcfCzQVELtRw+eLQWtzv+VAIEFJ1/xxXlQ=
|
||||
github.com/aws/smithy-go v1.28.1/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -31,6 +69,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.28.0 h1:Q7ibns33JjyW48gHkuFT91qX48KG0ktULL6FgHdG688=
|
||||
github.com/go-playground/validator/v10 v10.28.0/go.mod h1:GoI6I1SjPBh9p7ykNE/yj3fFYbyDOpwMn5KXd+m2hUU=
|
||||
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
||||
@@ -41,6 +81,14 @@ github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8=
|
||||
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.10.0 h1:VhSvgU2jSli8o3AqIEOTJr7rZwAEUVo4E4XhR94Zfr0=
|
||||
github.com/jackc/pgx/v5 v5.10.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
@@ -65,6 +113,8 @@ github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHP
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
||||
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
@@ -74,10 +124,12 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq
|
||||
github.com/segmentio/asm v1.2.1 h1:DTNbBqs57ioxAD4PrArqftgypG4/qNpXoJx8TVXxPR0=
|
||||
github.com/segmentio/asm v1.2.1/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
||||
golang.org/x/exp v0.0.0-20251113190631-e25ba8c21ef6 h1:zfMcR1Cs4KNuomFFgGefv5N0czO2XZpUbxGUy8i8ug0=
|
||||
@@ -105,8 +157,14 @@ gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkp
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/gorm v1.31.1 h1:7CA8FTFz/gRfgqgpeKIBcervUn3xSyPUmr6B2WXJ7kg=
|
||||
gorm.io/gorm v1.31.1/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs=
|
||||
gorm.io/driver/mysql v1.6.0 h1:eNbLmNTpPpTOVZi8MMxCi2aaIm0ZpInbORNXDwyLGvg=
|
||||
gorm.io/driver/mysql v1.6.0/go.mod h1:D/oCC2GWK3M/dqoLxnOlaNKmXz8WNTfcS9y5ovaSqKo=
|
||||
gorm.io/driver/postgres v1.6.3 h1:bAn6O2pUa8LtpWEvL5NFU4+52Tfx8Ut7IVaIacCLcI0=
|
||||
gorm.io/driver/postgres v1.6.3/go.mod h1:0c4fQA44XhOklXDkgtuKqysHCycTa5i9e3EIpDGCwXk=
|
||||
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
|
||||
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
|
||||
gorm.io/gorm v1.31.2 h1:3o8FXNo9v9S858gil+3LlZA1LkCOzgb4g5BL64FgaCo=
|
||||
gorm.io/gorm v1.31.2/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs=
|
||||
modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis=
|
||||
modernc.org/cc/v4 v4.27.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
|
||||
modernc.org/ccgo/v4 v4.30.1 h1:4r4U1J6Fhj98NKfSjnPUN7Ze2c6MnAdL0hWw6+LrJpc=
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/article"
|
||||
"strconv"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func TextChange(field string, before string, after string) Change {
|
||||
return Change{Field: field, Kind: ChangeText, Before: &before, After: &after}
|
||||
}
|
||||
|
||||
func BodyChange(before string, after string) Change {
|
||||
delta := utf8.RuneCountInString(after) - utf8.RuneCountInString(before)
|
||||
return Change{Field: FieldBody, Kind: ChangeBody, Delta: &delta}
|
||||
}
|
||||
|
||||
func ArticleChanges(before article.Article, after article.Article) []Change {
|
||||
changes := []Change{}
|
||||
if before.Title != after.Title {
|
||||
changes = append(changes, TextChange(FieldTitle, before.Title, after.Title))
|
||||
}
|
||||
if before.IsOnline != after.IsOnline {
|
||||
changes = append(changes, TextChange(FieldOnline, strconv.FormatBool(before.IsOnline), strconv.FormatBool(after.IsOnline)))
|
||||
}
|
||||
if before.Body != after.Body {
|
||||
changes = append(changes, BodyChange(before.Body, after.Body))
|
||||
}
|
||||
return changes
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"RenewCMS/internal/domain/user"
|
||||
"errors"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrInvalidFilter = errors.New("invalid activity filter")
|
||||
var ErrInvalidRetention = errors.New("invalid activity retention period")
|
||||
var ErrInvalidEvent = errors.New("invalid activity event")
|
||||
var ErrActorNotAllowed = errors.New("activity of another actor is not allowed")
|
||||
|
||||
type ActorType string
|
||||
|
||||
const (
|
||||
ActorUser ActorType = "user"
|
||||
ActorAPIKey ActorType = "api_key"
|
||||
ActorSystem ActorType = "system"
|
||||
ActorAnonymous ActorType = "anonymous"
|
||||
)
|
||||
|
||||
type Category string
|
||||
|
||||
const (
|
||||
CategoryArticles Category = "articles"
|
||||
CategoryMedia Category = "media"
|
||||
CategoryUsers Category = "users"
|
||||
CategoryAPIKeys Category = "api_keys"
|
||||
CategoryAuth Category = "auth"
|
||||
CategorySystem Category = "system"
|
||||
)
|
||||
|
||||
type Outcome string
|
||||
|
||||
const (
|
||||
OutcomeSuccess Outcome = "success"
|
||||
OutcomeFailed Outcome = "failed"
|
||||
)
|
||||
|
||||
type Source string
|
||||
|
||||
const (
|
||||
SourceWeb Source = "web"
|
||||
SourceAPIKey Source = "api_key"
|
||||
SourceSystem Source = "system"
|
||||
)
|
||||
|
||||
type ChangeKind string
|
||||
|
||||
const (
|
||||
ChangeText ChangeKind = "text"
|
||||
ChangeBody ChangeKind = "body"
|
||||
)
|
||||
|
||||
const (
|
||||
ActionLogin = "auth.login"
|
||||
ActionLoginFailed = "auth.login_failed"
|
||||
ActionLogout = "auth.logout"
|
||||
ActionPasswordResetRequest = "auth.password_reset_requested"
|
||||
ActionPasswordResetDone = "auth.password_reset_completed"
|
||||
ActionAPIKeyCreate = "api_key.create"
|
||||
ActionAPIKeyRevoke = "api_key.revoke"
|
||||
ActionRetentionChanged = "system.activity_retention_changed"
|
||||
ActionCleanup = "system.activity_cleanup"
|
||||
ActionArticleCreate = "article.create"
|
||||
ActionArticleEdit = "article.edit"
|
||||
ActionArticlePublish = "article.publish"
|
||||
ActionArticleUnpublish = "article.unpublish"
|
||||
ActionArticleDelete = "article.delete"
|
||||
ActionMediaUpload = "media.upload"
|
||||
ActionMediaRename = "media.rename"
|
||||
ActionMediaDelete = "media.delete"
|
||||
ActionMediaMerge = "media.merge"
|
||||
ActionMediaPurge = "media.purge"
|
||||
ActionUserRegister = "user.register"
|
||||
ActionUserVerify = "user.verify"
|
||||
ActionUserUpdateProfile = "user.update_profile"
|
||||
ActionInviteCreate = "invite.create"
|
||||
ActionInviteCancel = "invite.cancel"
|
||||
ActionInviteRenew = "invite.renew"
|
||||
ActionInviteAccept = "invite.accept"
|
||||
ActionMediaPurgeScheduled = "system.media_purge"
|
||||
ActionUpdateChecked = "system.update_checked"
|
||||
ActionUpdateDownloaded = "system.update_downloaded"
|
||||
ActionRestartRequested = "system.restart_requested"
|
||||
)
|
||||
|
||||
const (
|
||||
TargetAPIKey = "api_key"
|
||||
TargetUser = "user"
|
||||
TargetArticle = "article"
|
||||
TargetImage = "image"
|
||||
TargetInvite = "invite"
|
||||
TargetRelease = "release"
|
||||
)
|
||||
|
||||
const (
|
||||
SystemActorName = "System"
|
||||
ReasonWrongLogin = "Wrong username or password."
|
||||
MaxUserAgentLength = 255
|
||||
ParamRemoved = "removed"
|
||||
ParamFrom = "from"
|
||||
ParamTo = "to"
|
||||
ParamMerged = "merged"
|
||||
ParamArticlesUpdated = "articles_updated"
|
||||
ParamFailedGroups = "failed_groups"
|
||||
ParamUpdateAvailable = "update_available"
|
||||
ParamKind = "kind"
|
||||
ParamTag = "tag"
|
||||
FieldTitle = "title"
|
||||
FieldOnline = "online"
|
||||
FieldBody = "body"
|
||||
FieldFilename = "filename"
|
||||
FieldDisplayName = "display_name"
|
||||
maxNameLength = 255
|
||||
maxActionLength = 64
|
||||
maxFailureReasonSize = 255
|
||||
)
|
||||
|
||||
func ActorTypes() []ActorType {
|
||||
return []ActorType{ActorUser, ActorAPIKey, ActorSystem, ActorAnonymous}
|
||||
}
|
||||
|
||||
func Categories() []Category {
|
||||
return []Category{CategoryArticles, CategoryMedia, CategoryUsers, CategoryAPIKeys, CategoryAuth, CategorySystem}
|
||||
}
|
||||
|
||||
func Outcomes() []Outcome {
|
||||
return []Outcome{OutcomeSuccess, OutcomeFailed}
|
||||
}
|
||||
|
||||
func Sources() []Source {
|
||||
return []Source{SourceWeb, SourceAPIKey, SourceSystem}
|
||||
}
|
||||
|
||||
func ChangeKinds() []ChangeKind {
|
||||
return []ChangeKind{ChangeText, ChangeBody}
|
||||
}
|
||||
|
||||
type Actor struct {
|
||||
Type ActorType `json:"type"`
|
||||
ID *uint32 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Target struct {
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Change struct {
|
||||
Field string `json:"field"`
|
||||
Kind ChangeKind `json:"kind"`
|
||||
Before *string `json:"before,omitempty"`
|
||||
After *string `json:"after,omitempty"`
|
||||
Delta *int `json:"delta,omitempty"`
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
ID uint64 `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Action string `json:"action"`
|
||||
Category Category `json:"category"`
|
||||
Actor Actor `json:"actor"`
|
||||
Target Target `json:"target"`
|
||||
Outcome Outcome `json:"outcome"`
|
||||
FailureReason *string `json:"failure_reason"`
|
||||
Security bool `json:"security"`
|
||||
Source Source `json:"source"`
|
||||
IP *string `json:"ip"`
|
||||
UserAgent *string `json:"user_agent"`
|
||||
Params map[string]any `json:"params"`
|
||||
Changes []Change `json:"changes"`
|
||||
}
|
||||
|
||||
func NewEvent(action string, category Category) Event {
|
||||
return Event{
|
||||
Action: action,
|
||||
Category: category,
|
||||
Outcome: OutcomeSuccess,
|
||||
Params: map[string]any{},
|
||||
Changes: []Change{},
|
||||
}
|
||||
}
|
||||
|
||||
func SystemActor() Actor {
|
||||
return Actor{Type: ActorSystem, Name: SystemActorName}
|
||||
}
|
||||
|
||||
func UserActor(u user.User) Actor {
|
||||
id := u.ID
|
||||
name := strings.TrimSpace(u.DisplayName)
|
||||
if name == "" {
|
||||
name = u.Username
|
||||
}
|
||||
return Actor{Type: ActorUser, ID: &id, Name: name}
|
||||
}
|
||||
|
||||
func AnonymousActor(name string) Actor {
|
||||
return Actor{Type: ActorAnonymous, Name: name}
|
||||
}
|
||||
|
||||
func APIKeyActor(id uint32, name string) Actor {
|
||||
return Actor{Type: ActorAPIKey, ID: &id, Name: name}
|
||||
}
|
||||
|
||||
func APIKeyTarget(id uint32, name string) Target {
|
||||
return Target{Type: TargetAPIKey, ID: strconv.FormatUint(uint64(id), 10), Name: name}
|
||||
}
|
||||
|
||||
func (e Event) Validate() error {
|
||||
if e.Action == "" || len(e.Action) > maxActionLength {
|
||||
return ErrInvalidEvent
|
||||
}
|
||||
if !slices.Contains(Categories(), e.Category) || !slices.Contains(Outcomes(), e.Outcome) || !slices.Contains(Sources(), e.Source) || !slices.Contains(ActorTypes(), e.Actor.Type) {
|
||||
return ErrInvalidEvent
|
||||
}
|
||||
if e.FailureReason != nil && len(*e.FailureReason) > maxFailureReasonSize {
|
||||
return ErrInvalidEvent
|
||||
}
|
||||
for _, change := range e.Changes {
|
||||
if change.Kind != ChangeText && change.Kind != ChangeBody {
|
||||
return ErrInvalidEvent
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e Event) Truncated() Event {
|
||||
e.Actor.Name = truncate(e.Actor.Name, maxNameLength)
|
||||
e.Target.Name = truncate(e.Target.Name, maxNameLength)
|
||||
return e
|
||||
}
|
||||
|
||||
func truncate(value string, limit int) string {
|
||||
runes := []rune(value)
|
||||
return string(runes[:min(len(runes), limit)])
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const SeriesBuckets = 7
|
||||
|
||||
type ActorRef struct {
|
||||
Type ActorType
|
||||
ID *uint32
|
||||
}
|
||||
|
||||
const (
|
||||
userActorPrefix = "user:"
|
||||
apiKeyActorPrefix = "key:"
|
||||
systemActorRef = "system"
|
||||
)
|
||||
|
||||
func ParseActor(raw string) (ActorRef, error) {
|
||||
if raw == systemActorRef {
|
||||
return ActorRef{Type: ActorSystem}, nil
|
||||
}
|
||||
if rest, ok := strings.CutPrefix(raw, userActorPrefix); ok {
|
||||
return parseActorID(ActorUser, rest)
|
||||
}
|
||||
if rest, ok := strings.CutPrefix(raw, apiKeyActorPrefix); ok {
|
||||
return parseActorID(ActorAPIKey, rest)
|
||||
}
|
||||
return ActorRef{}, ErrInvalidFilter
|
||||
}
|
||||
|
||||
func parseActorID(actorType ActorType, raw string) (ActorRef, error) {
|
||||
id, err := strconv.ParseUint(raw, 10, 32)
|
||||
if err != nil {
|
||||
return ActorRef{}, ErrInvalidFilter
|
||||
}
|
||||
value := uint32(id)
|
||||
return ActorRef{Type: actorType, ID: &value}, nil
|
||||
}
|
||||
|
||||
func ParseCategory(raw string) (Category, error) {
|
||||
category := Category(raw)
|
||||
if !slices.Contains(Categories(), category) {
|
||||
return "", ErrInvalidFilter
|
||||
}
|
||||
return category, nil
|
||||
}
|
||||
|
||||
type Filter struct {
|
||||
Query string
|
||||
Actor *ActorRef
|
||||
Category Category
|
||||
Outcome Outcome
|
||||
Security bool
|
||||
From *time.Time
|
||||
To *time.Time
|
||||
Page int
|
||||
Size int
|
||||
}
|
||||
|
||||
type Page struct {
|
||||
Items []Event
|
||||
Total int
|
||||
}
|
||||
|
||||
type SummaryQuery struct {
|
||||
From time.Time
|
||||
To time.Time
|
||||
}
|
||||
|
||||
func (q SummaryQuery) Previous() SummaryQuery {
|
||||
return SummaryQuery{From: q.From.Add(-q.To.Sub(q.From)), To: q.From}
|
||||
}
|
||||
|
||||
type ActivePeople struct {
|
||||
Users int `json:"users"`
|
||||
APIKeys int `json:"api_keys"`
|
||||
}
|
||||
|
||||
type Series struct {
|
||||
Total []int `json:"total"`
|
||||
Active []int `json:"active"`
|
||||
Failed []int `json:"failed"`
|
||||
Security []int `json:"security"`
|
||||
}
|
||||
|
||||
type Summary struct {
|
||||
Total int `json:"total"`
|
||||
PreviousTotal int `json:"previous_total"`
|
||||
ActivePeople ActivePeople `json:"active_people"`
|
||||
Failed int `json:"failed"`
|
||||
Security int `json:"security"`
|
||||
Series Series `json:"series"`
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package audit
|
||||
|
||||
import "context"
|
||||
|
||||
type Recorder interface {
|
||||
Record(ctx context.Context, event Event)
|
||||
}
|
||||
|
||||
type discardRecorder struct{}
|
||||
|
||||
func (discardRecorder) Record(context.Context, Event) {}
|
||||
|
||||
var Discard Recorder = discardRecorder{}
|
||||
|
||||
type Origin struct {
|
||||
Actor Actor
|
||||
Source Source
|
||||
IP string
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
type originContextKey struct{}
|
||||
|
||||
func WithOrigin(ctx context.Context, origin Origin) context.Context {
|
||||
return context.WithValue(ctx, originContextKey{}, origin)
|
||||
}
|
||||
|
||||
func OriginFrom(ctx context.Context) (Origin, bool) {
|
||||
origin, ok := ctx.Value(originContextKey{}).(Origin)
|
||||
return origin, ok
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package audit
|
||||
|
||||
import "time"
|
||||
|
||||
type Repository interface {
|
||||
Append(event Event) (Event, error)
|
||||
List(filter Filter) (Page, error)
|
||||
Summary(query SummaryQuery) (Summary, error)
|
||||
DeleteOlderThan(cutoff time.Time) (int, error)
|
||||
}
|
||||
|
||||
type SettingsRepository interface {
|
||||
Get() (Settings, error)
|
||||
SaveRetention(days int) error
|
||||
SaveLastCleanup(at time.Time, removed int) error
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package audit
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
DefaultRetentionDays = 90
|
||||
ForeverRetentionDays = 0
|
||||
CleanupHour = 3
|
||||
KeepRecentHours = 24
|
||||
)
|
||||
|
||||
func RetentionOptions() []int {
|
||||
return []int{30, 90, 365, ForeverRetentionDays}
|
||||
}
|
||||
|
||||
func ValidateRetentionDays(days int) error {
|
||||
for _, option := range RetentionOptions() {
|
||||
if option == days {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return ErrInvalidRetention
|
||||
}
|
||||
|
||||
type Settings struct {
|
||||
RetentionDays int `json:"activity_retention_days"`
|
||||
LastCleanupAt *time.Time `json:"activity_last_cleanup_at"`
|
||||
LastCleanupRemoved int `json:"activity_last_cleanup_removed"`
|
||||
NextCleanupAt *time.Time `json:"activity_next_cleanup_at"`
|
||||
}
|
||||
|
||||
func DefaultSettings() Settings {
|
||||
return Settings{RetentionDays: DefaultRetentionDays}
|
||||
}
|
||||
|
||||
func (s Settings) CleanupDue(now time.Time) bool {
|
||||
todayRun := time.Date(now.Year(), now.Month(), now.Day(), CleanupHour, 0, 0, 0, now.Location())
|
||||
if now.Before(todayRun) {
|
||||
return false
|
||||
}
|
||||
return s.LastCleanupAt == nil || s.LastCleanupAt.Before(todayRun)
|
||||
}
|
||||
|
||||
func (s Settings) WithNextCleanup(now time.Time) Settings {
|
||||
s.NextCleanupAt = nil
|
||||
if s.RetentionDays == ForeverRetentionDays {
|
||||
return s
|
||||
}
|
||||
next := time.Date(now.Year(), now.Month(), now.Day(), CleanupHour, 0, 0, 0, now.Location())
|
||||
if !next.After(now) {
|
||||
next = time.Date(now.Year(), now.Month(), now.Day()+1, CleanupHour, 0, 0, 0, now.Location())
|
||||
}
|
||||
s.NextCleanupAt = &next
|
||||
return s
|
||||
}
|
||||
|
||||
func ClampCutoff(now time.Time, cutoff time.Time) time.Time {
|
||||
newest := now.Add(-KeepRecentHours * time.Hour)
|
||||
if cutoff.After(newest) {
|
||||
return newest
|
||||
}
|
||||
return cutoff
|
||||
}
|
||||
|
||||
func RetentionCutoff(now time.Time, retentionDays int) time.Time {
|
||||
return ClampCutoff(now, now.Add(-time.Duration(retentionDays)*24*time.Hour))
|
||||
}
|
||||
|
||||
type CleanupResult struct {
|
||||
Removed int
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package audit
|
||||
|
||||
import "strconv"
|
||||
|
||||
func NewTarget(targetType string, id uint32, name string) Target {
|
||||
return Target{Type: targetType, ID: strconv.FormatUint(uint64(id), 10), Name: name}
|
||||
}
|
||||
|
||||
func ReleaseTarget(tag string) Target {
|
||||
return Target{Type: TargetRelease, ID: tag, Name: tag}
|
||||
}
|
||||
Loaded 100 of 364 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user