Compare commits

...

72 commits

Author SHA1 Message Date
7e62cbc613 Merge release/0.1.0 into main
Some checks failed
Build and Push Docker Image / build (push) Has been cancelled
2026-04-29 23:29:27 +02:00
f80132dfae 6 - Fix ntfy threshold config typing and layout title default 2026-04-29 23:25:36 +02:00
31a53de9fb 6 - Wire PollFailed dispatch and listener 2026-04-29 23:21:02 +02:00
6ab175a466 6 - Send ntfy alert when poll failures cross threshold 2026-04-29 21:25:11 +02:00
8d063a8262 6 - Add PollFailed event to FediDiscover package 2026-04-29 21:15:11 +02:00
bbd74c1954 6 - Reset consecutive_poll_failures on successful poll 2026-04-29 21:09:44 +02:00
920985eec8 6 - Convert blade views to layouts/app inheritance 2026-04-29 21:07:43 +02:00
f9cebe5bae 6 - Add admin instances page and PollAlertService failure tracking 2026-04-28 23:33:32 +02:00
257dbfcf5f chore - remove example tests 2026-04-28 19:51:42 +02:00
6e097acf88 6 - Add admin instances page listing url and last poll 2026-04-28 18:52:09 +02:00
9cecc47b8b 6 - Log structured success entry on PollFediverseAction with url count and duration 2026-04-28 18:47:04 +02:00
a59c086da2 13 - Make page language sticky across re-crawls when new fetch returns null 2026-04-28 01:32:38 +02:00
1cba8f3fc9 13 - Persist detected language and confidence on Page after successful fetch 2026-04-28 01:20:13 +02:00
81b3c7f70b 13 - Wire LanguageDetectionService into FetchPageAction with lang attr fallback 2026-04-28 01:09:18 +02:00
cb83b0df90 13 - Add language and languageConfidence fields to FetchResult 2026-04-28 00:38:46 +02:00
829ce04282 13 - Add LanguageDetectionService wrapping patrickschur/language-detection 2026-04-28 00:32:54 +02:00
a37b1da145 13 - Add language_confidence column and Page model cast 2026-04-28 00:13:14 +02:00
cda1414cd8 9 - Add robots.txt handling with cache and politeness integration
Some checks failed
CI / ci (push) Failing after 3h0m1s
CI / ci (pull_request) Has been cancelled
2026-04-27 23:53:52 +02:00
264180cd36 chore - Move outcome → status mapping into CrawlOutcomeEnum methods
Some checks failed
CI / ci (push) Failing after 3h0m1s
2026-04-27 01:36:37 +02:00
1538ceeb6e 11 - Gate ProcessCrawlJob with per-domain politeness lock 2026-04-27 01:25:46 +02:00
7171348370 11 - Add PolitenessService and crawler delay config 2026-04-27 00:59:45 +02:00
69aa5d9d3e 10 - Add /bot page with crawler identity and opt-out instructions 2026-04-27 00:41:12 +02:00
c80be24e6e chore - Extract mockFetchPageAction helper in ProcessCrawlJobTest 2026-04-27 00:24:38 +02:00
3297c4bb3b 14 - Fix ProcessCrawlJob outcome write and status mapping bugs 2026-04-27 00:18:34 +02:00
e8a935ea31 14 - Document queue worker container in deployment README 2026-04-26 23:54:40 +02:00
720e4bcc1f 14 - Implement ProcessCrawlJob orchestration with retry logic 2026-04-26 23:50:57 +02:00
2a586ecac4 14 - Add PageCrawlObserver and ProcessCrawlJob skeleton 2026-04-26 21:15:07 +02:00
118de0023a 14 - Simplify page_crawls schema (queue moves to Redis) 2026-04-26 20:58:07 +02:00
6c0e1fe12d chore - Simplify call-site tests now that RegisterDiscoveredPageAction is unit-tested 2026-04-26 20:22:24 +02:00
649aeb3627 chore - Extract RegisterDiscoveredPageAction for shared Page::firstOrCreate logic 2026-04-26 20:18:18 +02:00
dda5b0f770 12 - Apply pr-reviewer follow-ups: validation, link filters, readonly VO, docs 2026-04-26 19:49:08 +02:00
35e1147823 12 - Add HTML content extraction (title, text, links, word count) 2026-04-26 19:35:04 +02:00
3e2fd0d2c4 chore - Add dev-composer command 2026-04-26 18:13:39 +02:00
1b7fbbfd0c 12 - Add FetchPageAction with Http::fake-driven outcome paths 2026-04-26 17:56:13 +02:00
bb7906e193 12 - Make FetchResult fields nullable and add missing crawler config 2026-04-26 16:50:43 +02:00
a9f2d689ae 12 - Add crawler config and FetchResult value object 2026-04-26 16:45:07 +02:00
abbcedf2e7 12 - Add Rejected case to CrawlOutcomeEnum and PageStatusEnum 2026-04-26 16:35:46 +02:00
6f75be7328 8 - Tighten UrlService validation and add observer integration tests 2026-04-26 16:09:28 +02:00
de14ae3ad4 8 - Wire PageObserver to enqueue page_crawls on Page creation 2026-04-26 15:56:38 +02:00
81209125a1 8 - Add UrlService with host extraction method 2026-04-26 14:52:40 +02:00
f2c1fab4e4 7 - Add int casts on PageCrawl and tests for cascade-delete + pending scope 2026-04-26 14:23:13 +02:00
fe8ca7fc10 7 - Add page_crawls migration, PageCrawl model, factory, and Page relationships 2026-04-26 14:15:49 +02:00
9dd6d84d65 7 - Add CrawlOutcomeEnum for crawl attempt outcomes 2026-04-26 13:06:22 +02:00
b1b7adeacd 7 - Add language column to pages for crawler-detected language 2026-04-26 12:53:21 +02:00
43837a99db 5 - Add UrlSubmissionForm Livewire component with rate limiting 2026-04-26 11:58:51 +02:00
b0a4102637 5 - Trust forwarded headers behind reverse proxy for real client IP 2026-04-26 11:56:39 +02:00
0f14c66c34 5 - Publish Livewire config with class-based components and no emoji 2026-04-26 11:54:23 +02:00
6b610b699e 4 - Drop status promotion in UrlDiscoveredListener; defer to keywords listener
Some checks failed
CI / ci (push) Failing after 3h0m0s
2026-04-26 03:52:12 +02:00
3ad473f4a1 4 - Add UrlDiscoveredListener wiring fediverse polling to pages graph 2026-04-26 03:31:32 +02:00
424ad2ff78 4 - Add Page and PageLink models with factories and unit tests 2026-04-26 02:51:49 +02:00
bc535c8c0b 4 - Add pages and page_links migrations with PageStatusEnum 2026-04-26 02:48:39 +02:00
ec2113710a 3 - Harden fediverse polling: timeouts, error handling, payload fields 2026-04-26 01:15:35 +02:00
2cb86f3337 3 - Schedule fedi-discover:poll every minute 2026-04-26 00:46:59 +02:00
1b713e3539 3 - Add LemmyClient with FediverseClientFactory dispatch 2026-04-26 00:42:21 +02:00
1b652752e1 3 - Add fedi-discover:poll command with failure isolation 2026-04-25 10:27:29 +02:00
fea8d48f6e 3 - Add MastodonClient with HTTP-faked tests 2026-04-25 03:19:14 +02:00
e5ee0184b5 3 - Add PollFediverseAction with FediverseClient interface 2026-04-25 02:09:38 +02:00
3eff919945 3 - Add UrlDiscovered event 2026-04-24 21:56:06 +02:00
52d6b493cb 2 - Add fedi-discover:validate console command 2026-04-24 20:11:05 +02:00
fc1c8ba020 2 - Add Instance Eloquent model with factory 2026-04-23 23:26:53 +02:00
bdd2b0f2e5 2 - Add InstanceConfig value object and InstanceType enum 2026-04-23 23:03:06 +02:00
7a2db5a14d chore - Set Pint concat_space to spaced + reformat 2026-04-23 23:02:50 +02:00
00e28c4868 2 - Add fedi_discover_instances migration 2026-04-23 20:53:22 +02:00
3706a81d3c 2 - Populate fedi-discover config with http settings and defaults 2026-04-23 20:48:35 +02:00
6c643373f2 1 - Add README with deployment docs 2026-04-23 20:12:05 +02:00
f0a8bdc1de 1 - Add production Dockerfile 2026-04-23 19:55:57 +02:00
fa85decccb 1 - Add CI and build workflows for Forgejo Actions 2026-04-23 18:12:22 +02:00
47375e2b4d 1 - Add Larastan static analysis + Package test suite 2026-04-23 18:01:24 +02:00
22680d45a7 1 - Fix npm install skipped on first boot due to volume-mounted node_modules 2026-04-23 17:45:43 +02:00
9929a837fe 1 - Add FediDiscover package skeleton 2026-04-23 17:41:24 +02:00
1fe6ae5cff 1 - Install Laravel 13 with Livewire 2026-04-23 03:13:33 +02:00
541cc8a3e7 1 - Add Nix shell and Docker dev environment 2026-04-23 03:00:07 +02:00
150 changed files with 20027 additions and 1 deletions

49
.dockerignore Normal file
View file

@ -0,0 +1,49 @@
# Version control
.git
.gitignore
.gitattributes
# Dev environment
shell.nix
Dockerfile.dev
docker/
# Tests (not needed in prod image)
tests/
phpunit.xml
.phpunit.result.cache
phpstan.neon
# Dependencies (rebuilt during image build)
node_modules/
vendor/
# Build artifacts (frontend stage produces these)
public/build/
public/hot
# Editor / OS
.editorconfig
.idea/
.vscode/
.DS_Store
*.swp
*.swo
# Env / secrets
.env
.env.*
!.env.example
# Logs and runtime caches
storage/logs/*.log
storage/framework/cache/data/
storage/framework/sessions/
storage/framework/views/
# CI
.forgejo/
# Docs / project meta
README.md
LICENSE

18
.editorconfig Normal file
View file

@ -0,0 +1,18 @@
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
[*.{yml,yaml}]
indent_size = 2
[{compose,docker-compose}.{yml,yaml}]
indent_size = 4

69
.env.example Normal file
View file

@ -0,0 +1,69 @@
APP_NAME=Trove
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
APP_LOCALE=en
APP_FALLBACK_LOCALE=en
APP_FAKER_LOCALE=en_US
APP_MAINTENANCE_DRIVER=file
# APP_MAINTENANCE_STORE=database
# PHP_CLI_SERVER_WORKERS=4
BCRYPT_ROUNDS=12
LOG_CHANNEL=stack
LOG_STACK=single
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=pgsql
DB_HOST=db
DB_PORT=5432
DB_DATABASE=trove
DB_USERNAME=trove
DB_PASSWORD=trove
SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null
BROADCAST_CONNECTION=log
FILESYSTEM_DISK=local
QUEUE_CONNECTION=redis
CACHE_STORE=redis
# CACHE_PREFIX=
REDIS_CLIENT=phpredis
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=log
MAIL_SCHEME=null
MAIL_HOST=127.0.0.1
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
VITE_APP_NAME="${APP_NAME}"
CRAWLER_MIN_DOMAIN_DELAY_SECONDS=10
NTFY_URL=
NTFY_TOPIC=
NTFY_THRESHOLD=

View file

@ -0,0 +1,55 @@
name: Build and Push Docker Image
on:
push:
branches: [main]
tags: ['v*']
paths:
- 'docker/prod/Dockerfile'
- 'app/**'
- 'bootstrap/**'
- 'config/**'
- 'database/**'
- 'packages/**'
- 'public/**'
- 'resources/**'
- 'routes/**'
- 'composer.lock'
- 'package-lock.json'
- '.forgejo/workflows/build.yml'
jobs:
build:
runs-on: docker
container:
image: catthehacker/ubuntu:act-latest
steps:
- uses: https://data.forgejo.org/actions/checkout@v4
- name: Set up Docker Buildx
uses: https://data.forgejo.org/docker/setup-buildx-action@v3
- name: Login to Forgejo Registry
uses: https://data.forgejo.org/docker/login-action@v3
with:
registry: forge.lvl0.xyz
username: ${{ github.actor }}
password: ${{ secrets.REGISTRY_TOKEN }}
- name: Determine tags
id: meta
run: |
if [[ "${{ github.ref }}" == refs/tags/v* ]]; then
TAG="${{ github.ref_name }}"
echo "tags=forge.lvl0.xyz/lvl0/trove:${TAG},forge.lvl0.xyz/lvl0/trove:latest" >> $GITHUB_OUTPUT
else
echo "tags=forge.lvl0.xyz/lvl0/trove:latest" >> $GITHUB_OUTPUT
fi
- name: Build and push
uses: https://data.forgejo.org/docker/build-push-action@v5
with:
context: .
file: docker/prod/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}

146
.forgejo/workflows/ci.yml Normal file
View file

@ -0,0 +1,146 @@
name: CI
on:
push:
branches: ['release/*']
pull_request:
branches: [main]
jobs:
ci:
runs-on: docker
container:
image: catthehacker/ubuntu:act-latest
services:
db:
image: postgres:17-alpine
env:
POSTGRES_DB: trove
POSTGRES_USER: trove
POSTGRES_PASSWORD: trove
redis:
image: redis:7-alpine
steps:
- uses: https://data.forgejo.org/actions/checkout@v4
- name: Set up PHP
uses: https://github.com/shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: pdo_pgsql, pdo_sqlite, redis, mbstring, xml, dom, intl, gd, zip
coverage: pcov
- name: Cache Composer dependencies
uses: https://data.forgejo.org/actions/cache@v4
with:
path: ~/.composer/cache
key: composer-${{ hashFiles('composer.lock') }}
restore-keys: composer-
- name: Install Composer dependencies
run: composer install --no-interaction --prefer-dist
- name: Cache npm dependencies
uses: https://data.forgejo.org/actions/cache@v4
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
restore-keys: npm-
- name: Install npm dependencies
run: npm ci
- name: Build assets
run: npm run build
- name: Prepare environment
run: |
cp .env.example .env
php artisan key:generate
- name: Wait for services
run: |
until pg_isready -h db -U trove; do sleep 1; done
until redis-cli -h redis ping | grep -q PONG; do sleep 1; done
- name: Lint (Pint)
run: vendor/bin/pint --test
- name: Static analysis (PHPStan)
run: vendor/bin/phpstan analyse --memory-limit=512M
- name: Tests with coverage
env:
DB_HOST: db
REDIS_HOST: redis
run: php artisan test --coverage-clover coverage.xml --coverage-text
- name: Parse coverage
if: github.event_name == 'pull_request'
id: coverage
run: |
COVERAGE=$(php -r '
$xml = simplexml_load_file("coverage.xml");
if ($xml === false || !isset($xml->project->metrics)) {
echo "0";
exit;
}
$metrics = $xml->project->metrics;
$statements = (int) $metrics["statements"];
$covered = (int) $metrics["coveredstatements"];
echo $statements > 0 ? round(($covered / $statements) * 100, 2) : 0;
')
echo "percentage=$COVERAGE" >> "$GITHUB_OUTPUT"
- name: Comment coverage on PR
if: github.event_name == 'pull_request'
continue-on-error: true
env:
FORGEJO_TOKEN: ${{ secrets.FORGEJO_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
COVERAGE: ${{ steps.coverage.outputs.percentage }}
REPO: ${{ github.repository }}
SERVER_URL: ${{ github.server_url }}
COMMIT_SHA: ${{ github.sha }}
run: |
API_URL="${SERVER_URL}/api/v1/repos/${REPO}/issues/${PR_NUMBER}/comments"
MARKER="<!-- trove-ci-coverage-report -->"
BODY="${MARKER}
## Code Coverage Report
| Metric | Value |
|--------|-------|
| **Line Coverage** | ${COVERAGE}% |
_Updated by CI — commit ${COMMIT_SHA}_"
# Find existing coverage comment
EXISTING=$(curl -sf -H "Authorization: token ${FORGEJO_TOKEN}" \
"${API_URL}?limit=50" | \
php -r '
$comments = json_decode(file_get_contents("php://stdin"), true);
if (!is_array($comments)) exit;
foreach ($comments as $c) {
if (str_contains($c["body"], "<!-- trove-ci-coverage-report -->")) {
echo $c["id"];
exit;
}
}
' || true)
if [ -n "$EXISTING" ]; then
curl -sf -X PATCH \
-H "Authorization: token ${FORGEJO_TOKEN}" \
-H "Content-Type: application/json" \
-d "$(php -r 'echo json_encode(["body" => $argv[1]]);' "$BODY")" \
"${SERVER_URL}/api/v1/repos/${REPO}/issues/comments/${EXISTING}" > /dev/null
else
curl -sf -X POST \
-H "Authorization: token ${FORGEJO_TOKEN}" \
-H "Content-Type: application/json" \
-d "$(php -r 'echo json_encode(["body" => $argv[1]]);' "$BODY")" \
"${API_URL}" > /dev/null
fi

11
.gitattributes vendored Normal file
View file

@ -0,0 +1,11 @@
* text=auto eol=lf
*.blade.php diff=html
*.css diff=css
*.html diff=html
*.md diff=markdown
*.php diff=php
/.github export-ignore
CHANGELOG.md export-ignore
.styleci.yml export-ignore

27
.gitignore vendored Normal file
View file

@ -0,0 +1,27 @@
*.log
.DS_Store
.env
.env.backup
.env.production
.phpactor.json
.phpunit.result.cache
/.codex
/.cursor/
/.idea
/.nova
/.phpunit.cache
/.vscode
/.zed
/.claude
/auth.json
/node_modules
/public/build
/public/hot
/public/storage
/storage/*.key
/storage/pail
/vendor
_ide_helper.php
Homestead.json
Homestead.yaml
Thumbs.db

2
.npmrc Normal file
View file

@ -0,0 +1,2 @@
ignore-scripts=true
audit=true

127
Dockerfile.dev Normal file
View file

@ -0,0 +1,127 @@
# Development Dockerfile with FrankenPHP
FROM dunglas/frankenphp:latest-php8.3-alpine
# Install system dependencies
RUN apk add --no-cache \
git \
postgresql-client \
vim \
bash \
nano \
curl
# Install Node.js 20.19.0+ from unofficial builds (musl-compatible for Alpine)
RUN curl -fsSL https://unofficial-builds.nodejs.org/download/release/v20.19.0/node-v20.19.0-linux-x64-musl.tar.xz | tar -xJ -C /usr/local --strip-components=1
# Install PHP extensions including xdebug for development
RUN install-php-extensions \
pdo_pgsql \
opcache \
zip \
gd \
intl \
redis \
xdebug
# Install Composer
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /app
# Configure PHP for development
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
# Configure Xdebug (disabled by default to reduce noise)
RUN echo "xdebug.mode=off" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo ";xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo ";xdebug.client_host=host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo ";xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
# Configure Caddy for development
RUN cat > /etc/caddy/Caddyfile <<EOF
{
frankenphp
order php_server before file_server
}
:8000 {
root * /app/public
php_server {
index index.php
}
encode gzip
file_server
# Less strict headers for development
header {
X-Frame-Options "SAMEORIGIN"
}
}
EOF
# Create startup script for development
RUN cat > /start.sh <<'EOF'
#!/bin/sh
set -e
# Create .env file if it doesn't exist
if [ ! -f ".env" ]; then
echo "Creating .env file from .env.example..."
cp .env.example .env
fi
# Install composer dependencies if vendor is empty
if [ ! -f "vendor/autoload.php" ]; then
echo "Installing Composer dependencies..."
composer install --no-interaction
fi
# Install npm dependencies if node_modules lacks an install marker.
# Can't use `[ ! -d node_modules ]` — the dir is always present because it's a
# named volume mount. Check for npm's post-install marker file instead.
if [ -f "package.json" ] && [ ! -f "node_modules/.package-lock.json" ]; then
echo "Installing npm dependencies..."
npm install
fi
# Clear Laravel caches
php artisan config:clear || true
php artisan cache:clear || true
# Wait for database
echo "Waiting for database..."
until pg_isready -h db -U "${DB_USERNAME:-trove}" -q; do
echo "Database not ready, retrying..."
sleep 2
done
echo "Database is ready!"
# Generate app key if not set
if grep -q "^APP_KEY=$" .env 2>/dev/null; then
echo "Generating application key..."
php artisan key:generate
fi
# Run migrations
php artisan migrate --force
# Start Vite dev server in background (if package.json exists)
if [ -f "package.json" ]; then
npm run dev &
fi
# Start FrankenPHP
exec frankenphp run --config /etc/caddy/Caddyfile
EOF
RUN chmod +x /start.sh
# Expose ports
EXPOSE 8000 5173
# Use the startup script
CMD ["/start.sh"]

131
README.md
View file

@ -1,2 +1,131 @@
# trove
# Trove
A federated search engine for the small web. Seeded by fediverse attention, ranked by domain coherence rather than commercial authority.
## Tech stack
Laravel 13 · Livewire 4 · PostgreSQL 17 (tsvector FTS) · Redis 7 · FrankenPHP · Vite 8 · Tailwind 4.
## Local development
Requires [Nix](https://nixos.org/download/) and [Podman](https://podman.io/).
```sh
nix-shell # enter dev shell
dev-up # start app, db, redis
```
App: `http://localhost:8200` · Vite HMR: `http://localhost:5175`
Other helpers inside the nix shell: `dev-down`, `dev-rebuild`, `dev-shell`, `dev-artisan <cmd>`, `dev-logs`.
## Self-hosting
Trove ships as a Docker image published to `forge.lvl0.xyz/lvl0/trove`. You provide the compose/stack config.
### Required environment
| Variable | Purpose |
|---|---|
| `APP_KEY` | Laravel app key. Generate with `docker run --rm forge.lvl0.xyz/lvl0/trove:latest php artisan key:generate --show`. **Must persist across deployments** or sessions/encrypted data break. |
| `APP_URL` | Public URL, e.g. `https://trove.example.org` |
| `DB_DATABASE`, `DB_USERNAME`, `DB_PASSWORD` | PostgreSQL credentials |
| `DB_HOST` | Hostname of the PostgreSQL service. Default `db`. Override if your service is named differently. |
| `REDIS_HOST` | Hostname of the Redis service. Default `redis`. Override if your service is named differently. |
### Services you need to provide
- **App**: pull `forge.lvl0.xyz/lvl0/trove:latest` (or a pinned `v*` tag). Exposes port `8000` inside the container. The image runs migrations and warms caches on boot.
- **Worker**: same image as `app`, with `command: php artisan queue:work --tries=3 --max-time=3600`. Processes the crawler queue (URL fetching, content extraction, retries). Crawls won't actually run without this — `app` only enqueues work. **Required for the crawler to function.**
- **PostgreSQL 17**. Hostname must be reachable as `db` (default) or set `DB_HOST`. Persist `/var/lib/postgresql/data`.
- **Redis 7** with `--appendonly yes` (queue jobs persist across restarts). Hostname `redis` or set `REDIS_HOST`.
On first boot the startup script waits for PostgreSQL, warms caches, then runs `php artisan migrate --force` automatically. The 60-second wait loop covers slow PG init; it exits with a clear error if PG never becomes reachable.
### Volumes
- `/app/storage` — Laravel writable paths (logs, cached views, uploads). Persist this.
### Healthcheck
The image exposes `GET /up` (Laravel's built-in health route). The Dockerfile declares a HEALTHCHECK; your orchestrator can use `curl -fsS http://localhost:8000/up` for liveness.
### Example compose stack
A minimal reference — adapt for your infra. DockGE, Portainer, `docker compose`, Kubernetes, and bare `podman play kube` all work with equivalent configs.
```yaml
services:
app:
image: forge.lvl0.xyz/lvl0/trove:latest
restart: always
ports: ["${APP_PORT:-8400}:8000"]
environment:
APP_KEY: "${APP_KEY}"
APP_URL: "${APP_URL}"
DB_DATABASE: "${DB_DATABASE}"
DB_USERNAME: "${DB_USERNAME}"
DB_PASSWORD: "${DB_PASSWORD}"
volumes:
- app_storage:/app/storage
depends_on:
db: { condition: service_healthy }
redis: { condition: service_healthy }
worker:
image: forge.lvl0.xyz/lvl0/trove:latest
restart: always
command: php artisan queue:work --tries=3 --max-time=3600
environment:
APP_KEY: "${APP_KEY}"
APP_URL: "${APP_URL}"
DB_DATABASE: "${DB_DATABASE}"
DB_USERNAME: "${DB_USERNAME}"
DB_PASSWORD: "${DB_PASSWORD}"
volumes:
- app_storage:/app/storage
depends_on:
db: { condition: service_healthy }
redis: { condition: service_healthy }
db:
image: postgres:17-alpine
restart: always
environment:
POSTGRES_DB: "${DB_DATABASE}"
POSTGRES_USER: "${DB_USERNAME}"
POSTGRES_PASSWORD: "${DB_PASSWORD}"
volumes:
- db_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
interval: 10s
retries: 5
start_period: 10s
redis:
image: redis:7-alpine
restart: always
command: redis-server --appendonly yes
volumes:
- redis_data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
retries: 5
volumes:
db_data:
redis_data:
app_storage:
```
### Upgrades
Pull the new image tag, recreate the app container. Migrations run on boot (`php artisan migrate --force` in the startup script). Rollback by pointing at the previous `v*` tag.
----
AGPL-3.0-or-later. See [LICENSE](LICENSE).
Part of [lvl0](https://lvl0.xyz), a collective for horizontal FOSS projects.

View file

@ -0,0 +1,194 @@
<?php
declare(strict_types=1);
namespace App\Actions;
use App\Enums\CrawlOutcomeEnum;
use App\Services\LanguageDetectionService;
use App\Services\UrlService;
use App\ValueObjects\FetchResult;
use fivefilters\Readability\Configuration;
use fivefilters\Readability\Readability;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Factory;
use Illuminate\Http\Client\Response;
use InvalidArgumentException;
use League\Uri\BaseUri;
use Symfony\Component\DomCrawler\Crawler;
use Throwable;
class FetchPageAction
{
private const MIN_WORDS_FOR_TEXT_DETECTION = 20;
private const MIN_TEXT_DETECTION_CONFIDENCE = 0.30;
public function __construct(
private Factory $http,
private UrlService $urlService,
private LanguageDetectionService $languageDetection,
) {}
public function __invoke(string $url): FetchResult
{
try {
$response = $this->http
->timeout(config('crawler.timeout'))
->withHeaders([
'User-Agent' => config('crawler.user_agent'),
'Accept' => 'text/html',
])
->withOptions([
'allow_redirects' => ['max' => config('crawler.max_redirects')],
])
->get($url);
} catch (ConnectionException|ConnectException $e) {
return $this->failureResult($e);
}
[$outcome, $error] = $this->validateResponse($response);
if ($outcome === CrawlOutcomeEnum::Success) {
[$title, $extractedText, $links, $crawler] = $this->extractTitleTextAndLinks($response->body(), $url);
$wordCount = $extractedText !== '' ? count(preg_split('/\s+/u', trim($extractedText))) : 0;
[$language, $languageConfidence] = $this->detectLanguage($crawler, $extractedText, $wordCount);
}
return new FetchResult(
outcome: $outcome,
statusCode: $response->status(),
finalUrl: $url,
title: $title ?? null,
extractedText: $extractedText ?? null,
outboundLinks: $links ?? collect(),
wordCount: $wordCount ?? null,
errorMessage: $error ?? null,
language: $language ?? null,
languageConfidence: $languageConfidence ?? null,
);
}
private function validateResponse(Response $response): array
{
$status = $response->status();
if ($status >= 400 && $status < 500) {
return [CrawlOutcomeEnum::Blocked4xx, "HTTP {$status}"];
}
if ($status >= 500) {
return [CrawlOutcomeEnum::Blocked5xx, "HTTP {$status}"];
}
$contentType = $response->header('Content-Type');
if (! str_starts_with(mb_strtolower($contentType), 'text/html')) {
return [CrawlOutcomeEnum::Rejected, "Unsupported file type: {$contentType}"];
}
return [CrawlOutcomeEnum::Success, null];
}
private function failureResult(ConnectionException|ConnectException $e): FetchResult
{
$guzzleException = $e instanceof ConnectException
? $e
: ($e->getPrevious() instanceof ConnectException
? $e->getPrevious()
: null);
$errno = $guzzleException?->getHandlerContext()['errno'] ?? null;
$outcome = $errno === CURLE_OPERATION_TIMEDOUT
? CrawlOutcomeEnum::Timeout
: CrawlOutcomeEnum::Failed;
return new FetchResult(
outcome: $outcome,
statusCode: null,
finalUrl: null,
title: null,
extractedText: null,
outboundLinks: collect(),
wordCount: null,
errorMessage: $e->getMessage(),
);
}
private function extractTitleTextAndLinks(string $body, string $url): array
{
$crawler = new Crawler($body);
$title = $crawler->filter('title')->count() > 0
? trim($crawler->filter('title')->text())
: null;
$readability = new Readability(new Configuration);
$readability->parse($body);
$mainContent = $readability->getContent() ?? '';
$extractedText = trim(strip_tags($mainContent));
$links = collect();
if ($mainContent !== '') {
$linkCrawler = new Crawler($mainContent);
if ($linkCrawler->filter('a[href]')->count() > 0) {
$links = collect($linkCrawler->filter('a[href]')->extract(['href']));
}
}
$linksResolved = $links
->map(fn (string $href) => $this->resolveAndValidateLink($href, $url))
->filter()
->unique()
->values();
return [$title, $extractedText, $linksResolved, $crawler];
}
private function resolveAndValidateLink(string $href, string $finalUrl): ?string
{
try {
$resolved = (string) BaseUri::from($finalUrl)->resolve($href);
$resolved = strstr($resolved, '#', true) ?: $resolved;
} catch (Throwable) {
return null;
}
if ($resolved === $finalUrl) {
return null;
}
try {
$this->urlService->host($resolved);
} catch (InvalidArgumentException) {
return null;
}
return $resolved;
}
/**
* @return array{0: ?string, 1: ?float}
*/
private function detectLanguage(Crawler $crawler, string $extractedText, ?int $wordCount = null): array
{
if ($wordCount >= self::MIN_WORDS_FOR_TEXT_DETECTION) {
$result = $this->languageDetection->detect($extractedText);
if ($result !== null && $result[1] >= self::MIN_TEXT_DETECTION_CONFIDENCE) {
return [$result[0], $result[1]];
}
}
$lang = $crawler->filter('html')->count() > 0
? trim($crawler->filter('html')->attr('lang') ?? '')
: '';
if ($lang !== '' && strlen($lang) <= 35) {
return [$lang, 1.0];
}
return [null, null];
}
}

View file

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Actions;
use App\Enums\PageStatusEnum;
use App\Models\Page;
class RegisterDiscoveredPageAction
{
public function __invoke(string $url, ?int $instanceId = null): Page
{
return Page::firstOrCreate(
['url' => $url],
[
'status' => PageStatusEnum::Discovered,
'instance_id' => $instanceId,
],
);
}
}

View file

@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace App\Enums;
enum CrawlOutcomeEnum: string
{
case Success = 'success';
case Failed = 'failed';
case Timeout = 'timeout';
case BlockedRobots = 'blocked_robots';
case Blocked4xx = 'blocked_4xx';
case Blocked5xx = 'blocked_5xx';
/**
* The HTTP fetch succeeded (2xx) but the response is unindexable in v0.1
* (non-HTML Content-Type). Worker MUST also write `pages.status = Rejected`
* on this outcome do NOT treat as Failed. Page row STAYS in the DB to
* prevent re-discovery loops as fediverse re-shares the URL.
*/
case Rejected = 'rejected';
/**
* The PageStatusEnum value the parent `pages` row should land on for this outcome.
*/
public function toPageStatus(): PageStatusEnum
{
return match ($this) {
self::Success => PageStatusEnum::Fetched,
self::Rejected => PageStatusEnum::Rejected,
self::Failed,
self::Timeout,
self::BlockedRobots,
self::Blocked4xx,
self::Blocked5xx => PageStatusEnum::Failed,
};
}
/**
* True if the worker should retry this outcome (transient failures only).
* Permanent failures (4xx, robots block, rejected content type) and successes do not retry.
*/
public function isRetryable(): bool
{
return match ($this) {
self::Failed, self::Timeout, self::Blocked5xx => true,
self::Success, self::Rejected, self::BlockedRobots, self::Blocked4xx => false,
};
}
/**
* True if the worker should register the outbound links discovered during the fetch.
* Only Success outcomes have meaningful links; everything else either failed or returned no usable HTML.
*/
public function shouldRegisterOutboundLinks(): bool
{
return $this === self::Success;
}
}

View file

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Enums;
enum PageStatusEnum: string
{
case Discovered = 'discovered';
case Fetched = 'fetched';
case Failed = 'failed';
/**
* The crawler fetched the page but rejected it as unindexable in v0.1
* (non-HTML Content-Type). Page row stays as a sentinel preventing
* re-discovery loops; future re-crawl could flip status back to
* Discovered Fetched if the URL starts serving HTML.
*/
case Rejected = 'rejected';
}

View file

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Admin;
use App\Enums\PageStatusEnum;
use App\Http\Controllers\Controller;
use Illuminate\View\View;
use Lvl0\FediDiscover\Models\Instance;
class InstancesController extends Controller
{
public function index(): View
{
$instances = Instance::withCount([
'pages',
'pages as failed_pages_count' => fn ($q) => $q->where('status', PageStatusEnum::Failed),
])->orderBy('url', 'asc')->get();
return view('admin.index', ['instances' => $instances]);
}
}

View file

@ -0,0 +1,8 @@
<?php
namespace App\Http\Controllers;
abstract class Controller
{
//
}

View file

@ -0,0 +1,127 @@
<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Actions\FetchPageAction;
use App\Actions\RegisterDiscoveredPageAction;
use App\Enums\CrawlOutcomeEnum;
use App\Enums\PageStatusEnum;
use App\Models\PageCrawl;
use App\Services\PolitenessService;
use App\Services\RobotsService;
use App\ValueObjects\FetchResult;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Cache;
class ProcessCrawlJob implements ShouldQueue
{
use Queueable;
public function __construct(
public PageCrawl $pageCrawl,
) {}
public function handle(): void
{
$robotsService = resolve(RobotsService::class);
if (! $robotsService->isAllowed($this->pageCrawl->page->url)) {
$this->pageCrawl->update([
'outcome' => CrawlOutcomeEnum::BlockedRobots,
'completed_at' => now(),
]);
$this->pageCrawl->page->update(['status' => PageStatusEnum::Failed]);
return;
}
$fetcher = resolve(FetchPageAction::class);
$register = resolve(RegisterDiscoveredPageAction::class);
$politenessService = resolve(PolitenessService::class);
$delay = $politenessService->minDelayFor($this->pageCrawl->domain);
$lock = Cache::lock("crawler:domain:{$this->pageCrawl->domain}", $delay);
if (! $lock->get()) {
$this->release($delay);
return;
}
$result = $fetcher($this->pageCrawl->page->url);
$this->writeOutcome($result);
$this->updatePageStatus($result);
if ($result->outcome->shouldRegisterOutboundLinks()) {
$result->outboundLinks->each(fn (string $url) => $register($url));
}
if ($result->outcome->isRetryable()) {
$this->scheduleRetryIfNeeded();
}
}
private function writeOutcome(FetchResult $result): void
{
$this->pageCrawl->update([
'outcome' => $result->outcome,
'completed_at' => now(),
'status_code' => $result->statusCode,
'error_message' => $result->errorMessage,
]);
}
private function updatePageStatus(FetchResult $result): void
{
$status = $result->outcome->toPageStatus();
$update = match ($status) {
PageStatusEnum::Fetched => [
'status' => $status,
'fetched_at' => now(),
'title' => $result->title,
// Sticky language: only write when detection produced a value, so a re-crawl
// returning null doesn't erase a previously-detected language. Guarding on
// language alone is sufficient because FetchPageAction::detectLanguage()
// always returns the pair as both-null or both-non-null (never mixed).
...($result->language !== null ? [
'language' => $result->language,
'language_confidence' => $result->languageConfidence,
] : []),
],
PageStatusEnum::Failed => [
'status' => $status,
'failed_at' => now(),
],
PageStatusEnum::Rejected => [
'status' => $status,
],
PageStatusEnum::Discovered => [
'status' => $status,
],
};
$this->pageCrawl->page->update($update);
}
private function scheduleRetryIfNeeded(): void
{
if (PageCrawl::where('page_id', $this->pageCrawl->page_id)->count() >= 3) {
return;
}
$newRow = PageCrawl::withoutEvents(
fn () => PageCrawl::create(
array_merge($this->pageCrawl->toArray(), [
'outcome' => null,
])
)
);
ProcessCrawlJob::dispatch($newRow)->delay(now()->addHour());
}
}

View file

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Listeners;
use App\Services\PollAlertService;
use Lvl0\FediDiscover\Events\PollFailed;
class PollFailedListener
{
public function __construct(private PollAlertService $service) {}
public function handle(PollFailed $event): void
{
$this->service->recordFailure($event->instance, $event->message);
}
}

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Listeners;
use App\Actions\RegisterDiscoveredPageAction;
use App\Models\PageLink;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB;
use Lvl0\FediDiscover\Events\UrlDiscovered;
class UrlDiscoveredListener implements ShouldQueue
{
public function __construct(
private RegisterDiscoveredPageAction $registerPage,
) {}
public function handle(UrlDiscovered $event): void
{
DB::transaction(function () use ($event) {
$targetPage = ($this->registerPage)($event->url, $event->instanceId);
if ($event->postUrl === null || $event->postUrl === $event->url) {
return;
}
$sourcePage = ($this->registerPage)($event->postUrl, $event->instanceId);
PageLink::firstOrCreate([
'source_page_id' => $sourcePage->id,
'target_page_id' => $targetPage->id,
]);
});
}
}

View file

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Livewire;
use App\Actions\RegisterDiscoveredPageAction;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Component;
class UrlSubmissionForm extends Component
{
public string $url = '';
public ?string $confirmedUrl = null;
public function submit(RegisterDiscoveredPageAction $registerPage): void
{
$key = 'submit-url:' . request()->ip();
if (RateLimiter::tooManyAttempts($key, 10)) {
$this->addError('rate_limit', 'Too many submissions, try again shortly.');
return;
}
RateLimiter::hit($key, 60);
$validated = $this->validate([
'url' => ['required', 'url:http,https'],
]);
$registerPage($validated['url']);
$this->confirmedUrl = $validated['url'];
$this->reset('url');
}
public function render(): View
{
return view('livewire.url-submission-form');
}
}

68
app/Models/Page.php Normal file
View file

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\PageStatusEnum;
use App\Observers\PageObserver;
use Database\Factories\PageFactory;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Lvl0\FediDiscover\Models\Instance;
#[ObservedBy([PageObserver::class])]
class Page extends Model
{
/** @use HasFactory<PageFactory> */
use HasFactory;
protected $fillable = [
'url',
'status',
'language',
'language_confidence',
'title',
'instance_id',
'posted_at',
'fetched_at',
'failed_at',
];
protected $casts = [
'status' => PageStatusEnum::class,
'language_confidence' => 'float',
'posted_at' => 'datetime',
'fetched_at' => 'datetime',
'failed_at' => 'datetime',
];
public function instance(): BelongsTo
{
return $this->belongsTo(Instance::class);
}
public function outgoingLinks(): HasMany
{
return $this->hasMany(PageLink::class, 'source_page_id');
}
public function incomingLinks(): HasMany
{
return $this->hasMany(PageLink::class, 'target_page_id');
}
public function crawls(): HasMany
{
return $this->hasMany(PageCrawl::class);
}
public function latestCrawl(): HasOne
{
return $this->hasOne(PageCrawl::class)->latestOfMany('created_at');
}
}

45
app/Models/PageCrawl.php Normal file
View file

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\CrawlOutcomeEnum;
use App\Observers\PageCrawlObserver;
use Database\Factories\PageCrawlFactory;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[ObservedBy(PageCrawlObserver::class)]
class PageCrawl extends Model
{
/** @use HasFactory<PageCrawlFactory> */
use HasFactory;
protected $fillable = [
'page_id',
'domain',
'priority',
'completed_at',
'outcome',
'status_code',
'error_message',
];
protected $casts = [
'priority' => 'integer',
'completed_at' => 'datetime',
'outcome' => CrawlOutcomeEnum::class,
'status_code' => 'integer',
];
/**
* @return BelongsTo<Page, $this>
*/
public function page(): BelongsTo
{
return $this->belongsTo(Page::class);
}
}

31
app/Models/PageLink.php Normal file
View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace App\Models;
use Database\Factories\PageLinkFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class PageLink extends Model
{
/** @use HasFactory<PageLinkFactory> */
use HasFactory;
protected $fillable = [
'source_page_id',
'target_page_id',
];
public function sourcePage(): BelongsTo
{
return $this->belongsTo(Page::class, 'source_page_id');
}
public function targetPage(): BelongsTo
{
return $this->belongsTo(Page::class, 'target_page_id');
}
}

32
app/Models/User.php Normal file
View file

@ -0,0 +1,32 @@
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
#[Fillable(['name', 'email', 'password'])]
#[Hidden(['password', 'remember_token'])]
class User extends Authenticatable
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace App\Observers;
use App\Jobs\ProcessCrawlJob;
use App\Models\PageCrawl;
class PageCrawlObserver
{
public function created(PageCrawl $pageCrawl): void
{
ProcessCrawlJob::dispatch($pageCrawl);
}
}

View file

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\Observers;
use App\Models\Page;
use App\Models\PageCrawl;
use App\Services\UrlService;
class PageObserver
{
public function __construct(private UrlService $urlService) {}
public function created(Page $page): void
{
PageCrawl::firstOrCreate(
['page_id' => $page->id],
[
'domain' => $this->urlService->host($page->url),
'priority' => 0,
],
);
}
}

View file

@ -0,0 +1,25 @@
<?php
namespace App\Providers;
use App\Listeners\PollFailedListener;
use App\Listeners\UrlDiscoveredListener;
use App\Services\LanguageDetectionService;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Lvl0\FediDiscover\Events\PollFailed;
use Lvl0\FediDiscover\Events\UrlDiscovered;
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(LanguageDetectionService::class);
}
public function boot(): void
{
Event::listen(UrlDiscovered::class, UrlDiscoveredListener::class);
Event::listen(PollFailed::class, PollFailedListener::class);
}
}

View file

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace App\Services;
use LanguageDetection\Language;
class LanguageDetectionService
{
private Language $language;
public function __construct()
{
$this->language = new Language;
}
/**
* @return array{0: string, 1: float}|null
*/
public function detect(string $text): ?array
{
if (trim($text) === '') {
return null;
}
$languages = $this->language->detect($text)->bestResults()->close();
if ($languages === []) {
return null;
}
// bestResults() keeps every candidate within 0.025 of the top score.
// array_key_first picks the highest-ranked one (arsort'd by the library).
$code = array_key_first($languages);
return [$code, $languages[$code]];
}
}

View file

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Services;
class PolitenessService
{
public function minDelayFor(string $domain): int
{
/** @var RobotsService $robotsService */
$robotsService = resolve(RobotsService::class);
$crawlDelay = $robotsService->crawlDelayFor($domain, config('crawler.user_agent'));
$configValue = config('crawler.min_domain_delay_seconds', 10);
return max($crawlDelay ?? 0, $configValue);
}
}

View file

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace App\Services;
use Exception;
use Illuminate\Support\Facades\Http;
use Lvl0\FediDiscover\Models\Instance;
class PollAlertService
{
public function recordFailure(Instance $instance, string $message): void
{
$instance->increment('consecutive_poll_failures');
$instance->refresh();
$ntfyUrl = config('services.ntfy.url');
$ntfyThreshold = config('services.ntfy.threshold');
$ntfyTopic = config('services.ntfy.topic');
if ($ntfyUrl === null || $ntfyThreshold === null || $ntfyThreshold === 0 || $ntfyTopic === null) {
return;
}
if ($instance->consecutive_poll_failures < $ntfyThreshold) {
return;
}
try {
Http::timeout(5)
->withBody($instance->url . ' - ' . $message, 'text/plain')
->post(rtrim($ntfyUrl, '/') . '/' . $ntfyTopic);
} catch (Exception $e) {
logger()->warning('ntfy alert failed', ['instance' => $instance->url, 'error' => $e->getMessage()]);
}
}
}

View file

@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace App\Services;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Spatie\Robots\RobotsTxt;
class RobotsService
{
public function __construct(
private UrlService $urlService,
) {}
public function isAllowed(string $url, ?string $userAgent = null): bool
{
$host = $this->urlService->host($url);
$path = parse_url($url, PHP_URL_PATH) ?? '/';
$body = Cache::remember(
"crawler:robots:{$host}",
config('crawler.robots_cache_ttl_seconds'),
function () use ($host) {
try {
$response = Http::get("https://{$host}/robots.txt");
return $response->successful() ? $response->body() : '';
} catch (ConnectionException) {
return '';
}
}
);
return (new RobotsTxt($body))->allows($path, $userAgent);
}
public function crawlDelayFor(string $host, string $userAgent): ?int
{
$body = Cache::remember(
"crawler:robots:{$host}",
config('crawler.robots_cache_ttl_seconds'),
function () use ($host) {
try {
$response = Http::get("https://{$host}/robots.txt");
return $response->successful() ? $response->body() : '';
} catch (ConnectionException) {
return '';
}
}
);
$delay = (new RobotsTxt($body))->crawlDelay($userAgent);
return $delay !== null ? (int) $delay : null;
}
}

View file

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Services;
use Illuminate\Support\Uri;
use InvalidArgumentException;
class UrlService
{
public function host(string $url): string
{
$uri = Uri::of($url);
$scheme = $uri->scheme();
if ($scheme === null || $scheme === '') {
throw new InvalidArgumentException("URL has no scheme: {$url}");
}
if (! in_array($scheme, ['http', 'https'], true)) {
throw new InvalidArgumentException("Invalid URL scheme: {$scheme}");
}
if ($uri->user() !== null) {
throw new InvalidArgumentException("URLs with embedded credentials not allowed: {$url}");
}
$host = $uri->host();
if ($host === null || $host === '') {
throw new InvalidArgumentException("URL has no host: {$url}");
}
$bareHost = preg_replace('/%.*$/', '', trim($host, '[]'));
if (filter_var($bareHost, FILTER_VALIDATE_IP) !== false) {
throw new InvalidArgumentException("IP literal hosts not allowed: {$host}");
}
return mb_strtolower($host);
}
}

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\ValueObjects;
use App\Enums\CrawlOutcomeEnum;
use Illuminate\Support\Collection;
final readonly class FetchResult
{
/**
* @param ?string $finalUrl Set to the request URL in v0.1; true post-redirect URL tracking is deferred (see ticket #12 spec). Downstream consumers MUST NOT trust this field as the post-redirect location until that lands.
* @param Collection<int, string> $outboundLinks
*/
public function __construct(
public CrawlOutcomeEnum $outcome,
public ?int $statusCode,
public ?string $finalUrl,
public ?string $title,
public ?string $extractedText,
public Collection $outboundLinks,
public ?int $wordCount,
public ?string $errorMessage,
public ?string $language = null,
public ?float $languageConfidence = null,
) {}
}

18
artisan Executable file
View file

@ -0,0 +1,18 @@
#!/usr/bin/env php
<?php
use Illuminate\Foundation\Application;
use Symfony\Component\Console\Input\ArgvInput;
define('LARAVEL_START', microtime(true));
// Register the Composer autoloader...
require __DIR__.'/vendor/autoload.php';
// Bootstrap Laravel and handle the command...
/** @var Application $app */
$app = require_once __DIR__.'/bootstrap/app.php';
$status = $app->handleCommand(new ArgvInput);
exit($status);

23
bootstrap/app.php Normal file
View file

@ -0,0 +1,23 @@
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__ . '/../routes/web.php',
commands: __DIR__ . '/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware): void {
$middleware->trustProxies(
at: '*',
headers: Request::HEADER_X_FORWARDED_FOR
| Request::HEADER_X_FORWARDED_PROTO,
);
})
->withExceptions(function (Exceptions $exceptions): void {
//
})->create();

2
bootstrap/cache/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*
!.gitignore

7
bootstrap/providers.php Normal file
View file

@ -0,0 +1,7 @@
<?php
use App\Providers\AppServiceProvider;
return [
AppServiceProvider::class,
];

103
composer.json Normal file
View file

@ -0,0 +1,103 @@
{
"$schema": "https://getcomposer.org/schema.json",
"name": "lvl0/trove",
"type": "project",
"description": "A federated search engine for the small web.",
"keywords": ["search", "fediverse", "small-web"],
"license": "AGPL-3.0-or-later",
"repositories": [
{
"type": "path",
"url": "packages/Lvl0/FediDiscover",
"options": {
"symlink": true
}
}
],
"require": {
"php": "^8.3",
"fivefilters/readability.php": "^3.3",
"laravel/framework": "^13.0",
"laravel/tinker": "^3.0",
"livewire/livewire": "^4.2",
"lvl0/fedi-discover": "@dev",
"patrickschur/language-detection": "^5.3",
"spatie/robots-txt": "^2.5",
"symfony/dom-crawler": "^7.4"
},
"require-dev": {
"fakerphp/faker": "^1.23",
"larastan/larastan": "^3.4",
"laravel/pail": "^1.2.5",
"laravel/pint": "^1.27",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.6",
"phpunit/phpunit": "^12.5.12"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"setup": [
"composer install",
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
"@php artisan key:generate",
"@php artisan migrate --force",
"npm install --ignore-scripts",
"npm run build"
],
"dev": [
"Composer\\Config::disableProcessTimeout",
"npx concurrently -c \"#93c5fd,#c4b5fd,#fb7185,#fdba74\" \"php artisan serve\" \"php artisan queue:listen --tries=1 --timeout=0\" \"php artisan pail --timeout=0\" \"npm run dev\" --names=server,queue,logs,vite --kill-others"
],
"test": [
"@php artisan config:clear --ansi",
"@php artisan test"
],
"lint": "pint",
"analyse": "phpstan analyse --memory-limit=512M",
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi",
"@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"",
"@php artisan migrate --graceful --ansi"
],
"pre-package-uninstall": [
"Illuminate\\Foundation\\ComposerScripts::prePackageUninstall"
]
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"php-http/discovery": true
}
},
"minimum-stability": "stable",
"prefer-stable": true
}

8772
composer.lock generated Normal file

File diff suppressed because it is too large Load diff

126
config/app.php Normal file
View file

@ -0,0 +1,126 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Application Name
|--------------------------------------------------------------------------
|
| This value is the name of your application, which will be used when the
| framework needs to place the application's name in a notification or
| other UI elements where an application name needs to be displayed.
|
*/
'name' => env('APP_NAME', 'Laravel'),
/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| This value determines the "environment" your application is currently
| running in. This may determine how you prefer to configure various
| services the application utilizes. Set this in your ".env" file.
|
*/
'env' => env('APP_ENV', 'production'),
/*
|--------------------------------------------------------------------------
| Application Debug Mode
|--------------------------------------------------------------------------
|
| When your application is in debug mode, detailed error messages with
| stack traces will be shown on every error that occurs within your
| application. If disabled, a simple generic error page is shown.
|
*/
'debug' => (bool) env('APP_DEBUG', false),
/*
|--------------------------------------------------------------------------
| Application URL
|--------------------------------------------------------------------------
|
| This URL is used by the console to properly generate URLs when using
| the Artisan command line tool. You should set this to the root of
| the application so that it's available within Artisan commands.
|
*/
'url' => env('APP_URL', 'http://localhost'),
/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. The timezone
| is set to "UTC" by default as it is suitable for most use cases.
|
*/
'timezone' => 'UTC',
/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by Laravel's translation / localization methods. This option can be
| set to any locale for which you plan to have translation strings.
|
*/
'locale' => env('APP_LOCALE', 'en'),
'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'),
'faker_locale' => env('APP_FAKER_LOCALE', 'en_US'),
/*
|--------------------------------------------------------------------------
| Encryption Key
|--------------------------------------------------------------------------
|
| This key is utilized by Laravel's encryption services and should be set
| to a random, 32 character string to ensure that all encrypted values
| are secure. You should do this prior to deploying the application.
|
*/
'cipher' => 'AES-256-CBC',
'key' => env('APP_KEY'),
'previous_keys' => [
...array_filter(
explode(',', (string) env('APP_PREVIOUS_KEYS', ''))
),
],
/*
|--------------------------------------------------------------------------
| Maintenance Mode Driver
|--------------------------------------------------------------------------
|
| These configuration options determine the driver used to determine and
| manage Laravel's "maintenance mode" status. The "cache" driver will
| allow maintenance mode to be controlled across multiple machines.
|
| Supported drivers: "file", "cache"
|
*/
'maintenance' => [
'driver' => env('APP_MAINTENANCE_DRIVER', 'file'),
'store' => env('APP_MAINTENANCE_STORE', 'database'),
],
];

117
config/auth.php Normal file
View file

@ -0,0 +1,117 @@
<?php
use App\Models\User;
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option defines the default authentication "guard" and password
| reset "broker" for your application. You may change these values
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => env('AUTH_GUARD', 'web'),
'passwords' => env('AUTH_PASSWORD_BROKER', 'users'),
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| which utilizes session storage plus the Eloquent user provider.
|
| All authentication guards have a user provider, which defines how the
| users are actually retrieved out of your database or other storage
| system used by the application. Typically, Eloquent is utilized.
|
| Supported: "session"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication guards have a user provider, which defines how the
| users are actually retrieved out of your database or other storage
| system used by the application. Typically, Eloquent is utilized.
|
| If you have multiple user tables or models you may configure multiple
| providers to represent the model / table. These providers may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => env('AUTH_MODEL', User::class),
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| These configuration options specify the behavior of Laravel's password
| reset functionality, including the table utilized for token storage
| and the user provider that is invoked to actually retrieve users.
|
| The expiry time is the number of minutes that each reset token will be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
| The throttle setting is the number of seconds a user must wait before
| generating more password reset tokens. This prevents the user from
| quickly generating a very large amount of password reset tokens.
|
*/
'passwords' => [
'users' => [
'provider' => 'users',
'table' => env('AUTH_PASSWORD_RESET_TOKEN_TABLE', 'password_reset_tokens'),
'expire' => 60,
'throttle' => 60,
],
],
/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Here you may define the number of seconds before a password confirmation
| window expires and users are asked to re-enter their password via the
| confirmation screen. By default, the timeout lasts for three hours.
|
*/
'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 10800),
];

130
config/cache.php Normal file
View file

@ -0,0 +1,130 @@
<?php
use Illuminate\Support\Str;
return [
/*
|--------------------------------------------------------------------------
| Default Cache Store
|--------------------------------------------------------------------------
|
| This option controls the default cache store that will be used by the
| framework. This connection is utilized if another isn't explicitly
| specified when running a cache operation inside the application.
|
*/
'default' => env('CACHE_STORE', 'database'),
/*
|--------------------------------------------------------------------------
| Cache Stores
|--------------------------------------------------------------------------
|
| Here you may define all of the cache "stores" for your application as
| well as their drivers. You may even define multiple stores for the
| same cache driver to group types of items stored in your caches.
|
| Supported drivers: "array", "database", "file", "memcached",
| "redis", "dynamodb", "octane",
| "failover", "null"
|
*/
'stores' => [
'array' => [
'driver' => 'array',
'serialize' => false,
],
'database' => [
'driver' => 'database',
'connection' => env('DB_CACHE_CONNECTION'),
'table' => env('DB_CACHE_TABLE', 'cache'),
'lock_connection' => env('DB_CACHE_LOCK_CONNECTION'),
'lock_table' => env('DB_CACHE_LOCK_TABLE'),
],
'file' => [
'driver' => 'file',
'path' => storage_path('framework/cache/data'),
'lock_path' => storage_path('framework/cache/data'),
],
'memcached' => [
'driver' => 'memcached',
'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),
'sasl' => [
env('MEMCACHED_USERNAME'),
env('MEMCACHED_PASSWORD'),
],
'options' => [
// Memcached::OPT_CONNECT_TIMEOUT => 2000,
],
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'),
'port' => env('MEMCACHED_PORT', 11211),
'weight' => 100,
],
],
],
'redis' => [
'driver' => 'redis',
'connection' => env('REDIS_CACHE_CONNECTION', 'cache'),
'lock_connection' => env('REDIS_CACHE_LOCK_CONNECTION', 'default'),
],
'dynamodb' => [
'driver' => 'dynamodb',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'table' => env('DYNAMODB_CACHE_TABLE', 'cache'),
'endpoint' => env('DYNAMODB_ENDPOINT'),
],
'octane' => [
'driver' => 'octane',
],
'failover' => [
'driver' => 'failover',
'stores' => [
'database',
'array',
],
],
],
/*
|--------------------------------------------------------------------------
| Cache Key Prefix
|--------------------------------------------------------------------------
|
| When utilizing the APC, database, memcached, Redis, and DynamoDB cache
| stores, there might be other applications using the same cache. For
| that reason, you may prefix every cache key to avoid collisions.
|
*/
'prefix' => env('CACHE_PREFIX', Str::slug((string) env('APP_NAME', 'laravel')) . '-cache-'),
/*
|--------------------------------------------------------------------------
| Serializable Classes
|--------------------------------------------------------------------------
|
| This value determines the classes that can be unserialized from cache
| storage. By default, no PHP classes will be unserialized from your
| cache to prevent gadget chain attacks if your APP_KEY is leaked.
|
*/
'serializable_classes' => false,
];

47
config/crawler.php Normal file
View file

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
return [
/*
|---------------------------------------------------------------------------
| HTTP timeout (seconds)
|---------------------------------------------------------------------------
|
| Hard cap on a single fetch. Guzzle's default is 0 (wait forever) never
| acceptable for a crawler. Tune up cautiously; longer timeouts amplify the
| impact of slow targets on overall throughput.
|
*/
'timeout' => env('CRAWLER_TIMEOUT', 10),
/*
|---------------------------------------------------------------------------
| Maximum redirects to follow
|---------------------------------------------------------------------------
|
| Guzzle default is 5. Cross-origin redirects are accepted in v0.1 the
| search engine treats the post-redirect URL as the canonical one for
| indexing.
|
*/
'max_redirects' => env('CRAWLER_MAX_REDIRECTS', 5),
/*
|---------------------------------------------------------------------------
| User-Agent
|---------------------------------------------------------------------------
|
| Identifies our crawler to target servers. The placeholder below is for
| v0.1 development; ticket #10 replaces it with the production identity
| and adds a `/bot` info page that the URL points at.
|
*/
'user_agent' => env('CRAWLER_USER_AGENT', 'TroveBot/0.1 (+https://trove.lvl0.xyz/bot)'),
'min_domain_delay_seconds' => env('CRAWLER_MIN_DOMAIN_DELAY_SECONDS', 10),
'robots_cache_ttl_seconds' => env('CRAWLER_ROBOTS_CACHE_TTL_SECONDS', 60 * 60 * 24),
];

184
config/database.php Normal file
View file

@ -0,0 +1,184 @@
<?php
use Illuminate\Support\Str;
use Pdo\Mysql;
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for database operations. This is
| the connection which will be utilized unless another connection
| is explicitly specified when you execute a query / statement.
|
*/
'default' => env('DB_CONNECTION', 'sqlite'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Below are all of the database connections defined for your application.
| An example configuration is provided for each database system which
| is supported by Laravel. You're free to add / remove connections.
|
*/
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DB_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
'busy_timeout' => null,
'journal_mode' => null,
'synchronous' => null,
'transaction_mode' => 'DEFERRED',
],
'mysql' => [
'driver' => 'mysql',
'url' => env('DB_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => env('DB_CHARSET', 'utf8mb4'),
'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
(PHP_VERSION_ID >= 80500 ? Mysql::ATTR_SSL_CA : PDO::MYSQL_ATTR_SSL_CA) => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mariadb' => [
'driver' => 'mariadb',
'url' => env('DB_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => env('DB_CHARSET', 'utf8mb4'),
'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
(PHP_VERSION_ID >= 80500 ? Mysql::ATTR_SSL_CA : PDO::MYSQL_ATTR_SSL_CA) => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DB_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => env('DB_CHARSET', 'utf8'),
'prefix' => '',
'prefix_indexes' => true,
'search_path' => 'public',
'sslmode' => env('DB_SSLMODE', 'prefer'),
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DB_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => env('DB_CHARSET', 'utf8'),
'prefix' => '',
'prefix_indexes' => true,
// 'encrypt' => env('DB_ENCRYPT', 'yes'),
// 'trust_server_certificate' => env('DB_TRUST_SERVER_CERTIFICATE', 'false'),
],
],
/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run on the database.
|
*/
'migrations' => [
'table' => 'migrations',
'update_date_on_publish' => true,
],
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer body of commands than a typical key-value system
| such as Memcached. You may define your connection settings here.
|
*/
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug((string) env('APP_NAME', 'laravel')) . '-database-'),
'persistent' => env('REDIS_PERSISTENT', false),
],
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
'max_retries' => env('REDIS_MAX_RETRIES', 3),
'backoff_algorithm' => env('REDIS_BACKOFF_ALGORITHM', 'decorrelated_jitter'),
'backoff_base' => env('REDIS_BACKOFF_BASE', 100),
'backoff_cap' => env('REDIS_BACKOFF_CAP', 1000),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
'max_retries' => env('REDIS_MAX_RETRIES', 3),
'backoff_algorithm' => env('REDIS_BACKOFF_ALGORITHM', 'decorrelated_jitter'),
'backoff_base' => env('REDIS_BACKOFF_BASE', 100),
'backoff_cap' => env('REDIS_BACKOFF_CAP', 1000),
],
],
];

80
config/filesystems.php Normal file
View file

@ -0,0 +1,80 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application for file storage.
|
*/
'default' => env('FILESYSTEM_DISK', 'local'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Below you may configure as many filesystem disks as necessary, and you
| may even configure multiple disks for the same driver. Examples for
| most supported storage drivers are configured here for reference.
|
| Supported drivers: "local", "ftp", "sftp", "s3"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app/private'),
'serve' => true,
'throw' => false,
'report' => false,
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => rtrim(env('APP_URL', 'http://localhost'), '/') . '/storage',
'visibility' => 'public',
'throw' => false,
'report' => false,
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'throw' => false,
'report' => false,
],
],
/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/
'links' => [
public_path('storage') => storage_path('app/public'),
],
];

282
config/livewire.php Normal file
View file

@ -0,0 +1,282 @@
<?php
return [
/*
|---------------------------------------------------------------------------
| Component Locations
|---------------------------------------------------------------------------
|
| This value sets the root directories that'll be used to resolve view-based
| components like single and multi-file components. The make command will
| use the first directory in this array to add new component files to.
|
*/
'component_locations' => [
resource_path('views/components'),
resource_path('views/livewire'),
],
/*
|---------------------------------------------------------------------------
| Component Namespaces
|---------------------------------------------------------------------------
|
| This value sets default namespaces that will be used to resolve view-based
| components like single-file and multi-file components. These folders'll
| also be referenced when creating new components via the make command.
|
*/
'component_namespaces' => [
'layouts' => resource_path('views/layouts'),
'pages' => resource_path('views/pages'),
],
/*
|---------------------------------------------------------------------------
| Page Layout
|---------------------------------------------------------------------------
| The view that will be used as the layout when rendering a single component as
| an entire page via `Route::livewire('/post/create', 'pages::create-post')`.
| In this case, the content of pages::create-post will render into $slot.
|
*/
'component_layout' => 'layouts::app',
/*
|---------------------------------------------------------------------------
| Lazy Loading Placeholder
|---------------------------------------------------------------------------
| Livewire allows you to lazy load components that would otherwise slow down
| the initial page load. Every component can have a custom placeholder or
| you can define the default placeholder view for all components below.
|
*/
'component_placeholder' => null, // Example: 'placeholders::skeleton'
/*
|---------------------------------------------------------------------------
| Make Command
|---------------------------------------------------------------------------
| This value determines the default configuration for the artisan make command
| You can configure the component type (sfc, mfc, class) and whether to use
| the high-voltage () emoji as a prefix in the sfc|mfc component names.
|
*/
'make_command' => [
'type' => 'class', // Options: 'sfc', 'mfc', 'class'
'emoji' => false, // Options: true, false
'with' => [
'js' => false,
'css' => false,
'test' => false,
],
],
/*
|---------------------------------------------------------------------------
| Class Namespace
|---------------------------------------------------------------------------
|
| This value sets the root class namespace for Livewire component classes in
| your application. This value will change where component auto-discovery
| finds components. It's also referenced by the file creation commands.
|
*/
'class_namespace' => 'App\\Livewire',
/*
|---------------------------------------------------------------------------
| Class Path
|---------------------------------------------------------------------------
|
| This value is used to specify the path where Livewire component class files
| are created when running creation commands like `artisan make:livewire`.
| This path is customizable to match your projects directory structure.
|
*/
'class_path' => app_path('Livewire'),
/*
|---------------------------------------------------------------------------
| View Path
|---------------------------------------------------------------------------
|
| This value is used to specify where Livewire component Blade templates are
| stored when running file creation commands like `artisan make:livewire`.
| It is also used if you choose to omit a component's render() method.
|
*/
'view_path' => resource_path('views/livewire'),
/*
|---------------------------------------------------------------------------
| Temporary File Uploads
|---------------------------------------------------------------------------
|
| Livewire handles file uploads by storing uploads in a temporary directory
| before the file is stored permanently. All file uploads are directed to
| a global endpoint for temporary storage. You may configure this below:
|
*/
'temporary_file_upload' => [
'disk' => env('LIVEWIRE_TEMPORARY_FILE_UPLOAD_DISK'), // Example: 'local', 's3' | Default: 'default'
'rules' => null, // Example: ['file', 'mimes:png,jpg'] | Default: ['required', 'file', 'max:12288'] (12MB)
'directory' => null, // Example: 'tmp' | Default: 'livewire-tmp'
'middleware' => null, // Example: 'throttle:5,1' | Default: 'throttle:60,1'
'preview_mimes' => [ // Supported file types for temporary pre-signed file URLs...
'png', 'gif', 'bmp', 'svg', 'wav', 'mp4',
'mov', 'avi', 'wmv', 'mp3', 'm4a',
'jpg', 'jpeg', 'mpga', 'webp', 'wma',
],
'max_upload_time' => 5, // Max duration (in minutes) before an upload is invalidated...
'cleanup' => true, // Should cleanup temporary uploads older than 24 hrs...
],
/*
|---------------------------------------------------------------------------
| Render On Redirect
|---------------------------------------------------------------------------
|
| This value determines if Livewire will run a component's `render()` method
| after a redirect has been triggered using something like `redirect(...)`
| Setting this to true will render the view once more before redirecting
|
*/
'render_on_redirect' => false,
/*
|---------------------------------------------------------------------------
| Eloquent Model Binding
|---------------------------------------------------------------------------
|
| Previous versions of Livewire supported binding directly to eloquent model
| properties using wire:model by default. However, this behavior has been
| deemed too "magical" and has therefore been put under a feature flag.
|
*/
'legacy_model_binding' => false,
/*
|---------------------------------------------------------------------------
| Auto-inject Frontend Assets
|---------------------------------------------------------------------------
|
| By default, Livewire automatically injects its JavaScript and CSS into the
| <head> and <body> of pages containing Livewire components. By disabling
| this behavior, you need to use @livewireStyles and @livewireScripts.
|
*/
'inject_assets' => true,
/*
|---------------------------------------------------------------------------
| Navigate (SPA mode)
|---------------------------------------------------------------------------
|
| By adding `wire:navigate` to links in your Livewire application, Livewire
| will prevent the default link handling and instead request those pages
| via AJAX, creating an SPA-like effect. Configure this behavior here.
|
*/
'navigate' => [
'show_progress_bar' => true,
'progress_bar_color' => '#2299dd',
],
/*
|---------------------------------------------------------------------------
| HTML Morph Markers
|---------------------------------------------------------------------------
|
| Livewire intelligently "morphs" existing HTML into the newly rendered HTML
| after each update. To make this process more reliable, Livewire injects
| "markers" into the rendered Blade surrounding @if, @class & @foreach.
|
*/
'inject_morph_markers' => true,
/*
|---------------------------------------------------------------------------
| Smart Wire Keys
|---------------------------------------------------------------------------
|
| Livewire uses loops and keys used within loops to generate smart keys that
| are applied to nested components that don't have them. This makes using
| nested components more reliable by ensuring that they all have keys.
|
*/
'smart_wire_keys' => true,
/*
|---------------------------------------------------------------------------
| Pagination Theme
|---------------------------------------------------------------------------
|
| When enabling Livewire's pagination feature by using the `WithPagination`
| trait, Livewire will use Tailwind templates to render pagination views
| on the page. If you want Bootstrap CSS, you can specify: "bootstrap"
|
*/
'pagination_theme' => 'tailwind',
/*
|---------------------------------------------------------------------------
| Release Token
|---------------------------------------------------------------------------
|
| This token is stored client-side and sent along with each request to check
| a users session to see if a new release has invalidated it. If there is
| a mismatch it will throw an error and prompt for a browser refresh.
|
*/
'release_token' => 'a',
/*
|---------------------------------------------------------------------------
| CSP Safe
|---------------------------------------------------------------------------
|
| This config is used to determine if Livewire will use the CSP-safe version
| of Alpine in its bundle. This is useful for applications that are using
| strict Content Security Policy (CSP) to protect against XSS attacks.
|
*/
'csp_safe' => false,
/*
|---------------------------------------------------------------------------
| Payload Guards
|---------------------------------------------------------------------------
|
| These settings protect against malicious or oversized payloads that could
| cause denial of service. The default values should feel reasonable for
| most web applications. Each can be set to null to disable the limit.
|
*/
'payload' => [
'max_size' => 1024 * 1024, // 1MB - maximum request payload size in bytes
'max_nesting_depth' => 10, // Maximum depth of dot-notation property paths
'max_calls' => 50, // Maximum method calls per request
'max_components' => 20, // Maximum components per batch request
],
];

132
config/logging.php Normal file
View file

@ -0,0 +1,132 @@
<?php
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
use Monolog\Processor\PsrLogMessageProcessor;
return [
/*
|--------------------------------------------------------------------------
| Default Log Channel
|--------------------------------------------------------------------------
|
| This option defines the default log channel that is utilized to write
| messages to your logs. The value provided here should match one of
| the channels present in the list of "channels" configured below.
|
*/
'default' => env('LOG_CHANNEL', 'stack'),
/*
|--------------------------------------------------------------------------
| Deprecations Log Channel
|--------------------------------------------------------------------------
|
| This option controls the log channel that should be used to log warnings
| regarding deprecated PHP and library features. This allows you to get
| your application ready for upcoming major versions of dependencies.
|
*/
'deprecations' => [
'channel' => env('LOG_DEPRECATIONS_CHANNEL', 'null'),
'trace' => env('LOG_DEPRECATIONS_TRACE', false),
],
/*
|--------------------------------------------------------------------------
| Log Channels
|--------------------------------------------------------------------------
|
| Here you may configure the log channels for your application. Laravel
| utilizes the Monolog PHP logging library, which includes a variety
| of powerful log handlers and formatters that you're free to use.
|
| Available drivers: "single", "daily", "slack", "syslog",
| "errorlog", "monolog", "custom", "stack"
|
*/
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => explode(',', (string) env('LOG_STACK', 'single')),
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
'replace_placeholders' => true,
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
'days' => env('LOG_DAILY_DAYS', 14),
'replace_placeholders' => true,
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => env('LOG_SLACK_USERNAME', env('APP_NAME', 'Laravel')),
'emoji' => env('LOG_SLACK_EMOJI', ':boom:'),
'level' => env('LOG_LEVEL', 'critical'),
'replace_placeholders' => true,
],
'papertrail' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => env('LOG_PAPERTRAIL_HANDLER', SyslogUdpHandler::class),
'handler_with' => [
'host' => env('PAPERTRAIL_URL'),
'port' => env('PAPERTRAIL_PORT'),
'connectionString' => 'tls://' . env('PAPERTRAIL_URL') . ':' . env('PAPERTRAIL_PORT'),
],
'processors' => [PsrLogMessageProcessor::class],
],
'stderr' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => StreamHandler::class,
'handler_with' => [
'stream' => 'php://stderr',
],
'formatter' => env('LOG_STDERR_FORMATTER'),
'processors' => [PsrLogMessageProcessor::class],
],
'syslog' => [
'driver' => 'syslog',
'level' => env('LOG_LEVEL', 'debug'),
'facility' => env('LOG_SYSLOG_FACILITY', LOG_USER),
'replace_placeholders' => true,
],
'errorlog' => [
'driver' => 'errorlog',
'level' => env('LOG_LEVEL', 'debug'),
'replace_placeholders' => true,
],
'null' => [
'driver' => 'monolog',
'handler' => NullHandler::class,
],
'emergency' => [
'path' => storage_path('logs/laravel.log'),
],
],
];

118
config/mail.php Normal file
View file

@ -0,0 +1,118 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Mailer
|--------------------------------------------------------------------------
|
| This option controls the default mailer that is used to send all email
| messages unless another mailer is explicitly specified when sending
| the message. All additional mailers can be configured within the
| "mailers" array. Examples of each type of mailer are provided.
|
*/
'default' => env('MAIL_MAILER', 'log'),
/*
|--------------------------------------------------------------------------
| Mailer Configurations
|--------------------------------------------------------------------------
|
| Here you may configure all of the mailers used by your application plus
| their respective settings. Several examples have been configured for
| you and you are free to add your own as your application requires.
|
| Laravel supports a variety of mail "transport" drivers that can be used
| when delivering an email. You may specify which one you're using for
| your mailers below. You may also add additional mailers if needed.
|
| Supported: "smtp", "sendmail", "mailgun", "ses", "ses-v2",
| "postmark", "resend", "log", "array",
| "failover", "roundrobin"
|
*/
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'scheme' => env('MAIL_SCHEME'),
'url' => env('MAIL_URL'),
'host' => env('MAIL_HOST', '127.0.0.1'),
'port' => env('MAIL_PORT', 2525),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'local_domain' => env('MAIL_EHLO_DOMAIN', parse_url((string) env('APP_URL', 'http://localhost'), PHP_URL_HOST)),
],
'ses' => [
'transport' => 'ses',
],
'postmark' => [
'transport' => 'postmark',
// 'message_stream_id' => env('POSTMARK_MESSAGE_STREAM_ID'),
// 'client' => [
// 'timeout' => 5,
// ],
],
'resend' => [
'transport' => 'resend',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => env('MAIL_SENDMAIL_PATH', '/usr/sbin/sendmail -bs -i'),
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
'failover' => [
'transport' => 'failover',
'mailers' => [
'smtp',
'log',
],
'retry_after' => 60,
],
'roundrobin' => [
'transport' => 'roundrobin',
'mailers' => [
'ses',
'postmark',
],
'retry_after' => 60,
],
],
/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all emails sent by your application to be sent from
| the same address. Here you may specify a name and address that is
| used globally for all emails that are sent by your application.
|
*/
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', env('APP_NAME', 'Laravel')),
],
];

129
config/queue.php Normal file
View file

@ -0,0 +1,129 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Queue Connection Name
|--------------------------------------------------------------------------
|
| Laravel's queue supports a variety of backends via a single, unified
| API, giving you convenient access to each backend using identical
| syntax for each. The default queue connection is defined below.
|
*/
'default' => env('QUEUE_CONNECTION', 'database'),
/*
|--------------------------------------------------------------------------
| Queue Connections
|--------------------------------------------------------------------------
|
| Here you may configure the connection options for every queue backend
| used by your application. An example configuration is provided for
| each backend supported by Laravel. You're also free to add more.
|
| Drivers: "sync", "database", "beanstalkd", "sqs", "redis",
| "deferred", "background", "failover", "null"
|
*/
'connections' => [
'sync' => [
'driver' => 'sync',
],
'database' => [
'driver' => 'database',
'connection' => env('DB_QUEUE_CONNECTION'),
'table' => env('DB_QUEUE_TABLE', 'jobs'),
'queue' => env('DB_QUEUE', 'default'),
'retry_after' => (int) env('DB_QUEUE_RETRY_AFTER', 90),
'after_commit' => false,
],
'beanstalkd' => [
'driver' => 'beanstalkd',
'host' => env('BEANSTALKD_QUEUE_HOST', 'localhost'),
'queue' => env('BEANSTALKD_QUEUE', 'default'),
'retry_after' => (int) env('BEANSTALKD_QUEUE_RETRY_AFTER', 90),
'block_for' => 0,
'after_commit' => false,
],
'sqs' => [
'driver' => 'sqs',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
'queue' => env('SQS_QUEUE', 'default'),
'suffix' => env('SQS_SUFFIX'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'after_commit' => false,
],
'redis' => [
'driver' => 'redis',
'connection' => env('REDIS_QUEUE_CONNECTION', 'default'),
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => (int) env('REDIS_QUEUE_RETRY_AFTER', 90),
'block_for' => null,
'after_commit' => false,
],
'deferred' => [
'driver' => 'deferred',
],
'background' => [
'driver' => 'background',
],
'failover' => [
'driver' => 'failover',
'connections' => [
'database',
'deferred',
],
],
],
/*
|--------------------------------------------------------------------------
| Job Batching
|--------------------------------------------------------------------------
|
| The following options configure the database and table that store job
| batching information. These options can be updated to any database
| connection and table which has been defined by your application.
|
*/
'batching' => [
'database' => env('DB_CONNECTION', 'sqlite'),
'table' => 'job_batches',
],
/*
|--------------------------------------------------------------------------
| Failed Queue Jobs
|--------------------------------------------------------------------------
|
| These options configure the behavior of failed queue job logging so you
| can control how and where failed jobs are stored. Laravel ships with
| support for storing failed jobs in a simple file or in a database.
|
| Supported drivers: "database-uuids", "dynamodb", "file", "null"
|
*/
'failed' => [
'driver' => env('QUEUE_FAILED_DRIVER', 'database-uuids'),
'database' => env('DB_CONNECTION', 'sqlite'),
'table' => 'failed_jobs',
],
];

44
config/services.php Normal file
View file

@ -0,0 +1,44 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Third Party Services
|--------------------------------------------------------------------------
|
| This file is for storing the credentials for third party services such
| as Mailgun, Postmark, AWS and more. This file provides the de facto
| location for this type of information, allowing packages to have
| a conventional file to locate the various service credentials.
|
*/
'ntfy' => [
'url' => env('NTFY_URL') ?: null,
'topic' => env('NTFY_TOPIC') ?: null,
'threshold' => env('NTFY_THRESHOLD') !== null ? (int) env('NTFY_THRESHOLD') : null,
],
'postmark' => [
'key' => env('POSTMARK_API_KEY'),
],
'resend' => [
'key' => env('RESEND_API_KEY'),
],
'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
'slack' => [
'notifications' => [
'bot_user_oauth_token' => env('SLACK_BOT_USER_OAUTH_TOKEN'),
'channel' => env('SLACK_BOT_USER_DEFAULT_CHANNEL'),
],
],
];

233
config/session.php Normal file
View file

@ -0,0 +1,233 @@
<?php
use Illuminate\Support\Str;
return [
/*
|--------------------------------------------------------------------------
| Default Session Driver
|--------------------------------------------------------------------------
|
| This option determines the default session driver that is utilized for
| incoming requests. Laravel supports a variety of storage options to
| persist session data. Database storage is a great default choice.
|
| Supported: "file", "cookie", "database", "memcached",
| "redis", "dynamodb", "array"
|
*/
'driver' => env('SESSION_DRIVER', 'database'),
/*
|--------------------------------------------------------------------------
| Session Lifetime
|--------------------------------------------------------------------------
|
| Here you may specify the number of minutes that you wish the session
| to be allowed to remain idle before it expires. If you want them
| to expire immediately when the browser is closed then you may
| indicate that via the expire_on_close configuration option.
|
*/
'lifetime' => (int) env('SESSION_LIFETIME', 120),
'expire_on_close' => env('SESSION_EXPIRE_ON_CLOSE', false),
/*
|--------------------------------------------------------------------------
| Session Encryption
|--------------------------------------------------------------------------
|
| This option allows you to easily specify that all of your session data
| should be encrypted before it's stored. All encryption is performed
| automatically by Laravel and you may use the session like normal.
|
*/
'encrypt' => env('SESSION_ENCRYPT', false),
/*
|--------------------------------------------------------------------------
| Session File Location
|--------------------------------------------------------------------------
|
| When utilizing the "file" session driver, the session files are placed
| on disk. The default storage location is defined here; however, you
| are free to provide another location where they should be stored.
|
*/
'files' => storage_path('framework/sessions'),
/*
|--------------------------------------------------------------------------
| Session Database Connection
|--------------------------------------------------------------------------
|
| When using the "database" or "redis" session drivers, you may specify a
| connection that should be used to manage these sessions. This should
| correspond to a connection in your database configuration options.
|
*/
'connection' => env('SESSION_CONNECTION'),
/*
|--------------------------------------------------------------------------
| Session Database Table
|--------------------------------------------------------------------------
|
| When using the "database" session driver, you may specify the table to
| be used to store sessions. Of course, a sensible default is defined
| for you; however, you're welcome to change this to another table.
|
*/
'table' => env('SESSION_TABLE', 'sessions'),
/*
|--------------------------------------------------------------------------
| Session Cache Store
|--------------------------------------------------------------------------
|
| When using one of the framework's cache driven session backends, you may
| define the cache store which should be used to store the session data
| between requests. This must match one of your defined cache stores.
|
| Affects: "dynamodb", "memcached", "redis"
|
*/
'store' => env('SESSION_STORE'),
/*
|--------------------------------------------------------------------------
| Session Sweeping Lottery
|--------------------------------------------------------------------------
|
| Some session drivers must manually sweep their storage location to get
| rid of old sessions from storage. Here are the chances that it will
| happen on a given request. By default, the odds are 2 out of 100.
|
*/
'lottery' => [2, 100],
/*
|--------------------------------------------------------------------------
| Session Cookie Name
|--------------------------------------------------------------------------
|
| Here you may change the name of the session cookie that is created by
| the framework. Typically, you should not need to change this value
| since doing so does not grant a meaningful security improvement.
|
*/
'cookie' => env(
'SESSION_COOKIE',
Str::slug((string) env('APP_NAME', 'laravel')) . '-session'
),
/*
|--------------------------------------------------------------------------
| Session Cookie Path
|--------------------------------------------------------------------------
|
| The session cookie path determines the path for which the cookie will
| be regarded as available. Typically, this will be the root path of
| your application, but you're free to change this when necessary.
|
*/
'path' => env('SESSION_PATH', '/'),
/*
|--------------------------------------------------------------------------
| Session Cookie Domain
|--------------------------------------------------------------------------
|
| This value determines the domain and subdomains the session cookie is
| available to. By default, the cookie will be available to the root
| domain without subdomains. Typically, this shouldn't be changed.
|
*/
'domain' => env('SESSION_DOMAIN'),
/*
|--------------------------------------------------------------------------
| HTTPS Only Cookies
|--------------------------------------------------------------------------
|
| By setting this option to true, session cookies will only be sent back
| to the server if the browser has a HTTPS connection. This will keep
| the cookie from being sent to you when it can't be done securely.
|
*/
'secure' => env('SESSION_SECURE_COOKIE'),
/*
|--------------------------------------------------------------------------
| HTTP Access Only
|--------------------------------------------------------------------------
|
| Setting this value to true will prevent JavaScript from accessing the
| value of the cookie and the cookie will only be accessible through
| the HTTP protocol. It's unlikely you should disable this option.
|
*/
'http_only' => env('SESSION_HTTP_ONLY', true),
/*
|--------------------------------------------------------------------------
| Same-Site Cookies
|--------------------------------------------------------------------------
|
| This option determines how your cookies behave when cross-site requests
| take place, and can be used to mitigate CSRF attacks. By default, we
| will set this value to "lax" to permit secure cross-site requests.
|
| See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value
|
| Supported: "lax", "strict", "none", null
|
*/
'same_site' => env('SESSION_SAME_SITE', 'lax'),
/*
|--------------------------------------------------------------------------
| Partitioned Cookies
|--------------------------------------------------------------------------
|
| Setting this value to true will tie the cookie to the top-level site for
| a cross-site context. Partitioned cookies are accepted by the browser
| when flagged "secure" and the Same-Site attribute is set to "none".
|
*/
'partitioned' => env('SESSION_PARTITIONED_COOKIE', false),
/*
|--------------------------------------------------------------------------
| Session Serialization
|--------------------------------------------------------------------------
|
| This value controls the serialization strategy for session data, which
| is JSON by default. Setting this to "php" allows the storage of PHP
| objects in the session but can make an application vulnerable to
| "gadget chain" serialization attacks if the APP_KEY is leaked.
|
| Supported: "json", "php"
|
*/
'serialization' => 'json',
];

1
database/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.sqlite*

View file

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Enums\CrawlOutcomeEnum;
use App\Models\Page;
use App\Models\PageCrawl;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<PageCrawl>
*/
class PageCrawlFactory extends Factory
{
public function definition(): array
{
return [
'page_id' => null,
'domain' => 'example.com',
'priority' => 0,
'completed_at' => null,
'outcome' => null,
'status_code' => null,
'error_message' => null,
];
}
public function page(Page $page): static
{
return $this->state(fn () => [
'page_id' => $page->id,
]);
}
public function successful(): static
{
return $this->state(fn () => [
'outcome' => CrawlOutcomeEnum::Success,
'completed_at' => now(),
]);
}
public function failed(string $errorMessage): static
{
return $this->state(fn () => [
'outcome' => CrawlOutcomeEnum::Failed,
'completed_at' => now(),
'error_message' => $errorMessage,
]);
}
}

View file

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Enums\PageStatusEnum;
use App\Models\Page;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Page>
*/
class PageFactory extends Factory
{
/**
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'url' => fake()->url(),
'status' => PageStatusEnum::Discovered,
];
}
}

View file

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use App\Models\Page;
use App\Models\PageLink;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<PageLink>
*/
class PageLinkFactory extends Factory
{
public function definition(): array
{
return [];
}
public function withSource(Page $page): static
{
return $this->state(fn () => [
'source_page_id' => $page->id,
]);
}
public function withTarget(Page $page): static
{
return $this->state(fn () => [
'target_page_id' => $page->id,
]);
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
/**
* @extends Factory<User>
*/
class UserFactory extends Factory
{
/**
* The current password being used by the factory.
*/
protected static ?string $password;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
];
}
/**
* Indicate that the model's email address should be unverified.
*/
public function unverified(): static
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}
}

View file

@ -0,0 +1,49 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::create('password_reset_tokens', function (Blueprint $table) {
$table->string('email')->primary();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->primary();
$table->foreignId('user_id')->nullable()->index();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->longText('payload');
$table->integer('last_activity')->index();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users');
Schema::dropIfExists('password_reset_tokens');
Schema::dropIfExists('sessions');
}
};

View file

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('cache', function (Blueprint $table) {
$table->string('key')->primary();
$table->mediumText('value');
$table->bigInteger('expiration')->index();
});
Schema::create('cache_locks', function (Blueprint $table) {
$table->string('key')->primary();
$table->string('owner');
$table->bigInteger('expiration')->index();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('cache');
Schema::dropIfExists('cache_locks');
}
};

View file

@ -0,0 +1,57 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('jobs', function (Blueprint $table) {
$table->id();
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedSmallInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
Schema::create('job_batches', function (Blueprint $table) {
$table->string('id')->primary();
$table->string('name');
$table->integer('total_jobs');
$table->integer('pending_jobs');
$table->integer('failed_jobs');
$table->longText('failed_job_ids');
$table->mediumText('options')->nullable();
$table->integer('cancelled_at')->nullable();
$table->integer('created_at');
$table->integer('finished_at')->nullable();
});
Schema::create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->string('uuid')->unique();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('jobs');
Schema::dropIfExists('job_batches');
Schema::dropIfExists('failed_jobs');
}
};

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
use App\Enums\PageStatusEnum;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('pages', function (Blueprint $table) {
$table->id();
$table->text('url')->unique();
$table->string('status')->default(PageStatusEnum::Discovered->value)->index();
$table->string('language', 35)->nullable()->index();
$table->decimal('language_confidence', 4, 3)->nullable();
$table->string('title')->nullable();
$table->foreignId('instance_id')
->nullable()
->constrained('fedi_discover_instances')
->nullOnDelete();
$table->timestampTz('posted_at')->nullable();
$table->timestampTz('fetched_at')->nullable();
$table->timestampTz('failed_at')->nullable();
$table->timestampsTz();
});
}
public function down(): void
{
Schema::dropIfExists('pages');
}
};

View file

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('page_links', function (Blueprint $table) {
$table->id();
$table->foreignId('source_page_id')->constrained('pages');
$table->foreignId('target_page_id')->constrained('pages');
$table->timestampsTz();
$table->unique(['source_page_id', 'target_page_id']);
});
}
public function down(): void
{
Schema::dropIfExists('page_links');
}
};

View file

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('page_crawls', function (Blueprint $table) {
$table->id();
$table->foreignId('page_id')
->constrained('pages')
->cascadeOnDelete();
$table->string('domain');
$table->smallInteger('priority')->default(0);
$table->timestampTz('completed_at')->nullable();
$table->string('outcome')->nullable();
$table->smallInteger('status_code')->nullable();
$table->text('error_message')->nullable();
$table->timestampsTz();
$table->index(['page_id', 'created_at']);
});
}
public function down(): void
{
Schema::dropIfExists('page_crawls');
}
};

View file

@ -0,0 +1,25 @@
<?php
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
use WithoutModelEvents;
/**
* Seed the application's database.
*/
public function run(): void
{
// User::factory(10)->create();
User::factory()->create([
'name' => 'Test User',
'email' => 'test@example.com',
]);
}
}

View file

@ -0,0 +1,98 @@
# ===================
# Trove Development Services
# ===================
# Port allocation:
# App: 8200 (frankenphp), 5175 (vite)
# DB: 5433 (postgresql)
# Redis: 6380
name: trove
services:
app:
build:
context: ../..
dockerfile: Dockerfile.dev
container_name: trove_dev_app
restart: unless-stopped
ports:
- "8200:8000"
- "5175:5173"
volumes:
- ../..:/app
- app_vendor:/app/vendor
- app_node_modules:/app/node_modules
environment:
APP_NAME: "${APP_NAME:-Trove}"
APP_ENV: "${APP_ENV:-local}"
APP_DEBUG: "${APP_DEBUG:-true}"
APP_URL: "${APP_URL:-http://localhost:8200}"
DB_CONNECTION: pgsql
DB_HOST: db
DB_PORT: 5432
DB_DATABASE: "${DB_DATABASE:-trove}"
DB_USERNAME: "${DB_USERNAME:-trove}"
DB_PASSWORD: "${DB_PASSWORD:-trove}"
REDIS_HOST: redis
REDIS_PORT: 6379
SESSION_DRIVER: "${SESSION_DRIVER:-database}"
CACHE_STORE: "${CACHE_STORE:-redis}"
QUEUE_CONNECTION: "${QUEUE_CONNECTION:-redis}"
MAIL_MAILER: "${MAIL_MAILER:-log}"
depends_on:
db:
condition: service_healthy
redis:
condition: service_healthy
networks:
- trove-network
db:
image: postgres:17-alpine
container_name: trove_dev_db
hostname: db
restart: unless-stopped
ports:
- "5433:5432"
environment:
POSTGRES_DB: "${DB_DATABASE:-trove}"
POSTGRES_USER: "${DB_USERNAME:-trove}"
POSTGRES_PASSWORD: "${DB_PASSWORD:-trove}"
volumes:
- db_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-trove}"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
networks:
- trove-network
redis:
image: redis:7-alpine
container_name: trove_dev_redis
hostname: redis
restart: unless-stopped
ports:
- "6380:6379"
volumes:
- redis_data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 5
start_period: 5s
networks:
- trove-network
networks:
trove-network:
driver: bridge
volumes:
db_data:
redis_data:
app_vendor:
app_node_modules:

128
docker/prod/Dockerfile Normal file
View file

@ -0,0 +1,128 @@
# syntax=docker/dockerfile:1
# ============================================================
# Stage 1: Build frontend assets
# ============================================================
FROM node:20-alpine AS frontend
WORKDIR /app
COPY package.json package-lock.json vite.config.js ./
COPY resources/ resources/
RUN npm ci --no-audit --no-fund
RUN npm run build
# ============================================================
# Stage 2: Runtime (FrankenPHP)
# ============================================================
FROM dunglas/frankenphp:1.1-php8.3-alpine AS runtime
RUN apk add --no-cache \
git \
postgresql-client \
curl
RUN install-php-extensions \
pdo_pgsql \
redis \
opcache \
zip \
gd \
intl
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
WORKDIR /app
ENV APP_ENV=production \
APP_DEBUG=false \
LOG_CHANNEL=stack \
LOG_LEVEL=warning \
DB_CONNECTION=pgsql \
DB_HOST=db \
DB_PORT=5432 \
REDIS_HOST=redis \
REDIS_PORT=6379 \
CACHE_STORE=redis \
QUEUE_CONNECTION=redis \
SESSION_DRIVER=redis \
BROADCAST_CONNECTION=log \
MAIL_MAILER=log
# Copy only the files composer needs before install, so the composer layer stays
# cached when application source changes. packages/ is required because composer.json
# declares it as a path repository.
COPY composer.json composer.lock ./
COPY packages/ packages/
# Skip post-autoload scripts (package:discover) during build — they need a runtime
# Laravel boot which fails without proper env. Discovery happens at runtime via
# start-prod.sh. --classmap-authoritative implies --optimize-autoloader.
RUN composer install --no-dev --no-interaction --prefer-dist --classmap-authoritative --no-scripts
COPY . .
COPY --from=frontend /app/public/build /app/public/build
RUN chown -R www-data:www-data /app/storage /app/bootstrap/cache
RUN cat > /etc/caddy/Caddyfile <<'EOF'
{
frankenphp
order php_server before file_server
}
:8000 {
root * /app/public
php_server {
index index.php
}
encode gzip zstd
file_server
header {
X-Frame-Options "SAMEORIGIN"
X-Content-Type-Options "nosniff"
Referrer-Policy "strict-origin-when-cross-origin"
}
}
EOF
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD curl -fsS http://localhost:8000/up || exit 1
RUN cat > /start-prod.sh <<'EOF'
#!/bin/sh
set -e
echo "Waiting for PostgreSQL at ${DB_HOST}:${DB_PORT}..."
for i in $(seq 1 60); do
if pg_isready -h "${DB_HOST}" -p "${DB_PORT}" -q; then
echo "PostgreSQL is ready."
break
fi
if [ "$i" = "60" ]; then
echo "Timed out waiting for PostgreSQL after 60s." >&2
exit 1
fi
sleep 1
done
php artisan package:discover --ansi
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan migrate --force
exec frankenphp run --config /etc/caddy/Caddyfile
EOF
RUN chmod +x /start-prod.sh
CMD ["/start-prod.sh"]

1608
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

16
package.json Normal file
View file

@ -0,0 +1,16 @@
{
"$schema": "https://www.schemastore.org/package.json",
"private": true,
"type": "module",
"scripts": {
"build": "vite build",
"dev": "vite"
},
"devDependencies": {
"@tailwindcss/vite": "^4.0.0",
"concurrently": "^9.0.1",
"laravel-vite-plugin": "^3.0.0",
"tailwindcss": "^4.0.0",
"vite": "^8.0.0"
}
}

View file

@ -0,0 +1,32 @@
{
"name": "lvl0/fedi-discover",
"description": "Fediverse polling library — discovers URLs from Mastodon and Lemmy posts.",
"license": "AGPL-3.0-or-later",
"type": "library",
"require": {
"php": "^8.3",
"illuminate/support": "^13.0"
},
"require-dev": {
"orchestra/testbench": "^11.0"
},
"autoload": {
"psr-4": {
"Lvl0\\FediDiscover\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Lvl0\\FediDiscover\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"Lvl0\\FediDiscover\\FediDiscoverServiceProvider"
]
}
},
"minimum-stability": "stable",
"prefer-stable": true
}

View file

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
return [
'http' => [
'timeout' => 10,
// Default points at the project site so fediverse admins can always trace a Trove poller
// back to the project. Operators running their own deployment should override this via
// `php artisan vendor:publish --tag=fedi-discover-config` with their own contact URL.
'user_agent' => 'Trove/1.0 (+https://trove.lvl0.xyz)',
'max_redirects' => 3,
],
'defaults' => [
// Minimum recommended: 60. Mastodon/Lemmy rate limits apply per-instance.
'interval_seconds' => 300,
],
// Instances are DB-managed (table: fedi_discover_instances).
// See the Instance model + admin UI (TBD). No instance list here.
];

View file

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('fedi_discover_instances', function (Blueprint $table) {
$table->id();
$table->string('type');
// Instance origin, e.g. https://mastodon.social. Not a full endpoint path.
$table->string('url');
$table->boolean('enabled')->default(true);
$table->unsignedInteger('interval_seconds')->default(300);
$table->json('extras')->default('{}');
$table->unsignedInteger('consecutive_poll_failures')->default(0);
$table->timestampTz('last_polled_at')->nullable();
$table->string('last_seen_id')->nullable();
$table->timestamps();
$table->unique(['type', 'url']);
});
}
public function down(): void
{
Schema::dropIfExists('fedi_discover_instances');
}
};

View file

@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Actions;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Log;
use Lvl0\FediDiscover\Clients\FediverseClientFactory;
use Lvl0\FediDiscover\Events\UrlDiscovered;
use Lvl0\FediDiscover\Models\Instance;
use Lvl0\FediDiscover\ValueObjects\FediversePost;
use Throwable;
class PollFediverseAction
{
public function __construct(private FediverseClientFactory $factory) {}
public function execute(Instance $instance): void
{
$start = microtime(true);
$client = $this->factory->for($instance);
$posts = $client->fetchPostsSince($instance, $instance->last_seen_id);
$urlCount = $posts
->map(function (FediversePost $post) use ($instance) {
try {
return $this->processLinks($post, $instance);
} catch (Throwable $e) {
Log::warning('fedi-discover:processLinks failed', [
'instance_id' => $instance->id,
'instance_url' => $instance->url,
'post_url' => $post->selfUrl,
'exception' => $e::class,
'message' => $e->getMessage(),
]);
}
})
->sum();
if ($posts->isNotEmpty()) {
$instance->last_seen_id = $posts->first()->cursorId;
}
$instance->consecutive_poll_failures = 0;
$instance->last_polled_at = now();
$instance->save();
Log::info('fedi-discover:poll succeeded', [
'instance_id' => $instance->id,
'url_count' => $urlCount,
'duration_ms' => (int) round((microtime(true) - $start) * 1000),
]);
}
private function processLinks(FediversePost $post, Instance $instance): int
{
if ($post->body === null) {
return 0;
}
$linksFound = preg_match_all('~https?://[^\s<>"\'()\[\]]+~', $post->body, $matches);
if ($linksFound === 0) {
return 0;
}
return collect($matches[0])
->map(fn (string $u) => rtrim($u, '.,;:!?'))
->filter(fn (string $u) => filter_var($u, FILTER_VALIDATE_URL) !== false)
->filter(fn (string $u) => parse_url($u, PHP_URL_HOST) !== parse_url($instance->url, PHP_URL_HOST))
->unique()
->each(fn (string $url) => UrlDiscovered::dispatch(
url: $url,
instanceId: $instance->id,
discoveredAt: CarbonImmutable::now(),
postUrl: $post->selfUrl,
postBody: $post->body,
))
->count();
}
}

View file

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Clients;
use Lvl0\FediDiscover\Config\InstanceType;
use Lvl0\FediDiscover\Models\Instance;
class FediverseClientFactory
{
public function __construct(
private MastodonClient $mastodonClient,
private LemmyClient $lemmyClient,
) {}
public function for(Instance $instance): FediverseClientInterface
{
return match ($instance->type) {
InstanceType::Mastodon => $this->mastodonClient,
InstanceType::Lemmy => $this->lemmyClient,
};
}
}

View file

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Clients;
use Illuminate\Support\Collection;
use Lvl0\FediDiscover\Models\Instance;
use Lvl0\FediDiscover\ValueObjects\FediversePost;
interface FediverseClientInterface
{
/**
* Fetch posts newer than the given cursor.
*
* MUST return posts in newest-first order. Callers treat the
* first item as the new high-water mark.
*
* @return Collection<int, FediversePost>
*/
public function fetchPostsSince(Instance $instance, ?string $lastSeenId): Collection;
}

View file

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Clients;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Lvl0\FediDiscover\Models\Instance;
use Lvl0\FediDiscover\ValueObjects\FediversePost;
class LemmyClient implements FediverseClientInterface
{
public function fetchPostsSince(Instance $instance, ?string $lastSeenId): Collection
{
$url = 'https://' . parse_url($instance->url, PHP_URL_HOST) . '/api/v3/post/list';
$params = $lastSeenId !== null ? ['min_id' => $lastSeenId] : [];
$response = Http::withHeaders([
'User-Agent' => config('fedi-discover.http.user_agent'),
])->timeout(config('fedi-discover.http.timeout'))->get($url, $params);
if (! $response->successful()) {
return collect();
}
return collect($response->json('posts', []))
->map(fn (array $p) => $p['post'])
->map(function (array $t) {
$parts = array_filter([$t['body'] ?? null, $t['url'] ?? null]);
$body = $parts ? implode(' ', $parts) : null;
return new FediversePost(
cursorId: (string) $t['id'],
selfUrl: $t['ap_id'],
body: $body,
title: $t['name'],
publishedAt: $t['published']
);
});
}
}

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Clients;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Lvl0\FediDiscover\Models\Instance;
use Lvl0\FediDiscover\ValueObjects\FediversePost;
class MastodonClient implements FediverseClientInterface
{
public function fetchPostsSince(Instance $instance, ?string $lastSeenId): Collection
{
$url = 'https://' . parse_url($instance->url, PHP_URL_HOST) . '/api/v1/timelines/public';
$params = $lastSeenId !== null ? ['min_id' => $lastSeenId] : [];
$response = Http::withHeaders([
'User-Agent' => config('fedi-discover.http.user_agent'),
])->timeout(config('fedi-discover.http.timeout'))->get($url, $params);
if (! $response->successful()) {
return collect();
}
return collect($response->json() ?? [])
->map(fn (array $t) => new FediversePost(
cursorId: $t['id'],
selfUrl: $t['url'] ?? $t['uri'] ?? null,
body: $t['content'],
publishedAt: $t['created_at'] ?? null
));
}
}

View file

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Config;
use InvalidArgumentException;
final readonly class InstanceConfig
{
/**
* @param array<string, mixed> $extras
*/
public function __construct(
public InstanceType $type,
public string $url,
public bool $enabled,
public int $intervalSeconds,
public array $extras
) {}
/**
* @throws InvalidArgumentException
*/
public static function fromArray(array $array): self
{
foreach (['type', 'url', 'enabled', 'interval_seconds'] as $key) {
if (! array_key_exists($key, $array)) {
throw new InvalidArgumentException("Missing required key: {$key}");
}
}
if ($array['interval_seconds'] <= 0) {
throw new InvalidArgumentException('Interval seconds needs to be larger than zero');
}
$type = InstanceType::tryFrom($array['type']);
if ($type === null) {
throw new InvalidArgumentException('Invalid type: ' . $array['type']);
}
if (filter_var($array['url'], FILTER_VALIDATE_URL) === false) {
throw new InvalidArgumentException('Invalid URL: ' . $array['url']);
}
return new self(
type: $type,
url: $array['url'],
enabled: $array['enabled'],
intervalSeconds: $array['interval_seconds'],
extras: $array['extras'] ?? []
);
}
public function toArray(): array
{
return [
'type' => $this->type->value,
'url' => $this->url,
'enabled' => $this->enabled,
'interval_seconds' => $this->intervalSeconds,
'extras' => $this->extras,
];
}
}

View file

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Config;
enum InstanceType: string
{
case Mastodon = 'mastodon';
case Lemmy = 'lemmy';
}

View file

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Console\Commands;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Lvl0\FediDiscover\Actions\PollFediverseAction;
use Lvl0\FediDiscover\Events\PollFailed;
use Lvl0\FediDiscover\Models\Instance;
use Throwable;
#[Signature('fedi-discover:poll')]
#[Description('Poll all enabled fediverse instances for new URLs')]
class PollInstancesCommand extends Command
{
public function __construct(
private readonly PollFediverseAction $action
) {
parent::__construct();
}
public function handle(): int
{
$errors = Instance::enabled()
->get()
->map(function (Instance $instance) {
try {
$this->action->execute($instance);
return ['instance_id' => $instance->id, 'status' => 'success'];
} catch (Throwable $e) {
$this->error("Failed to poll {$instance->url}: {$e->getMessage()}");
Log::warning('fedi-discover:poll failed', [
'instance_id' => $instance->id,
'instance_url' => $instance->url,
'exception' => $e::class,
'message' => $e->getMessage(),
]);
return ['instance' => $instance, 'status' => 'error', 'error' => $e->getMessage()];
}
})
->filter(fn (array $res) => $res['status'] === 'error');
if ($errors->isEmpty()) {
return self::SUCCESS;
}
$errors->each(fn (array $errorArr) => PollFailed::dispatch(
$errorArr['instance'],
$errorArr['error'],
now()->toImmutable(),
));
return self::FAILURE;
}
}

View file

@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Console\Commands;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Lvl0\FediDiscover\Models\Instance;
#[Signature('fedi-discover:validate {--enabled-only}')]
#[Description('Validate saved instances')]
class ValidateInstancesCommand extends Command
{
public function handle(): int
{
$instances = Instance::query();
if ($this->option('enabled-only')) {
$instances->enabled();
}
$instances = $instances->get();
$invalidInstances = collect();
$instances->each(function (Instance $instance) use ($invalidInstances) {
$reasons = collect();
if (filter_var($instance->url, FILTER_VALIDATE_URL) === false) {
$reasons->add('Invalid URL: ' . $instance->url);
}
if ($instance->interval_seconds < 1) {
$reasons->add('Invalid interval seconds: ' . $instance->interval_seconds);
}
if ($reasons->isNotEmpty()) {
$invalidInstances->add([
'instance' => $instance,
'reasons' => $reasons,
]);
}
});
$this->info((string) $instances->count());
$this->info(($instances->count() - $invalidInstances->count()) . ' valid');
$this->line($invalidInstances->count() . ' invalid');
if ($invalidInstances->isNotEmpty()) {
$invalidInstances->each(function (array $instanceArray) {
$instance = $instanceArray['instance'];
$reason = $instanceArray['reasons']->join(', ');
$this->warn($instance->id . ' - ' . $instance->url);
$this->line(' : ' . $reason);
});
return self::FAILURE;
}
return self::SUCCESS;
}
}

View file

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Lvl0\FediDiscover\Config\InstanceType;
use Lvl0\FediDiscover\Models\Instance;
/**
* @extends Factory<Instance>
*/
class InstanceFactory extends Factory
{
protected $model = Instance::class;
/**
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'type' => null,
'url' => fake()->url,
'enabled' => null,
'interval_seconds' => 600,
'extras' => [],
'last_seen_id' => null,
'last_polled_at' => now(),
];
}
public function type(InstanceType $type): self
{
return $this->state(fn () => [
'type' => $type->value,
]);
}
public function enabled(): self
{
return $this->state(fn () => [
'enabled' => true,
]);
}
public function disabled(): self
{
return $this->state(fn () => [
'enabled' => false,
]);
}
}

View file

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Events;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Lvl0\FediDiscover\Models\Instance;
class PollFailed
{
use Dispatchable, SerializesModels;
public function __construct(
public Instance $instance,
public string $message,
public CarbonImmutable $failedAt,
) {
//
}
}

View file

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Events;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class UrlDiscovered
{
use Dispatchable, SerializesModels;
public function __construct(
public string $url,
public int $instanceId,
public CarbonImmutable $discoveredAt,
public ?string $postUrl = null,
public ?string $postBody = null,
) {}
}

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover;
use Illuminate\Support\ServiceProvider;
use Lvl0\FediDiscover\Clients\FediverseClientFactory;
use Lvl0\FediDiscover\Console\Commands\PollInstancesCommand;
use Lvl0\FediDiscover\Console\Commands\ValidateInstancesCommand;
class FediDiscoverServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/../config/fedi-discover.php', 'fedi-discover');
$this->app->singleton(FediverseClientFactory::class);
}
public function boot(): void
{
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/fedi-discover.php' => config_path('fedi-discover.php'),
], 'fedi-discover-config');
$this->commands([
PollInstancesCommand::class,
ValidateInstancesCommand::class,
]);
}
}
}

View file

@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Models;
use App\Models\Page;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Carbon;
use Lvl0\FediDiscover\Config\InstanceType;
use Lvl0\FediDiscover\Database\Factories\InstanceFactory;
/**
* @property int $id
* @property InstanceType $type
* @property string $url
* @property bool $enabled
* @property int $interval_seconds
* @property array<string, mixed> $extras
* @property string|null $last_seen_id
* @property int $consecutive_poll_failures
* @property Carbon|null $last_polled_at
* @property Carbon $created_at
* @property Carbon $updated_at
*/
class Instance extends Model
{
/** @use HasFactory<InstanceFactory> */
use HasFactory;
protected $table = 'fedi_discover_instances';
protected $fillable = ['type', 'url', 'enabled', 'interval_seconds', 'extras', 'last_seen_id', 'last_polled_at', 'consecutive_poll_failures'];
protected $casts = [
'type' => InstanceType::class,
'enabled' => 'boolean',
'extras' => 'array',
'last_polled_at' => 'datetime',
];
/**
* @param Builder<self> $query
* @return Builder<self>
*/
public function scopeEnabled(Builder $query): Builder
{
return $query->where('enabled', true);
}
protected static function newFactory(): Factory
{
return InstanceFactory::new();
}
public function pages(): HasMany
{
return $this->hasMany(Page::class);
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\ValueObjects;
class FediversePost
{
public function __construct(
public string $cursorId,
public ?string $selfUrl,
public ?string $body = null,
public ?string $title = null,
public ?string $publishedAt = null,
) {}
}

View file

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Feature;
use Lvl0\FediDiscover\Clients\FediverseClientFactory;
use Lvl0\FediDiscover\Clients\LemmyClient;
use Lvl0\FediDiscover\Clients\MastodonClient;
use Lvl0\FediDiscover\Config\InstanceType;
use Lvl0\FediDiscover\Models\Instance;
use Tests\TestCase;
class FediverseClientFactoryTest extends TestCase
{
public function test_it_resolves_mastodon_client_for_mastodon_instance_type(): void
{
$factory = app(FediverseClientFactory::class);
$instance = new Instance(['type' => InstanceType::Mastodon, 'url' => 'https://mastodon.social']);
$client = $factory->for($instance);
$this->assertInstanceOf(MastodonClient::class, $client);
}
public function test_it_resolves_lemmy_client_for_lemmy_instance_type(): void
{
$factory = app(FediverseClientFactory::class);
$instance = new Instance(['type' => InstanceType::Lemmy, 'url' => 'https://lemmy.world']);
$client = $factory->for($instance);
$this->assertInstanceOf(LemmyClient::class, $client);
}
public function test_it_is_registered_as_a_singleton_in_the_container(): void
{
$a = $this->app->make(FediverseClientFactory::class);
$b = $this->app->make(FediverseClientFactory::class);
$this->assertSame($a, $b);
}
}

View file

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Lvl0\FediDiscover\Config\InstanceConfig;
use Lvl0\FediDiscover\Config\InstanceType;
use Lvl0\FediDiscover\Models\Instance;
use Tests\TestCase;
class InstanceConfigPersistenceTest extends TestCase
{
use RefreshDatabase;
public function test_instance_config_to_array_is_mass_assignable_on_the_model(): void
{
$config = InstanceConfig::fromArray([
'type' => InstanceType::Mastodon->value,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'extras' => ['token' => 'abc123'],
]);
Instance::create($config->toArray());
$this->artisan('fedi-discover:validate')
->assertExitCode(0);
}
public function test_an_instance_config_survives_a_write_read_cycle_through_the_model(): void
{
$original = InstanceConfig::fromArray([
'type' => InstanceType::Mastodon->value,
'url' => 'https://hachyderm.io',
'enabled' => false,
'interval_seconds' => 900,
'extras' => ['foo' => 'bar'],
]);
Instance::create($original->toArray());
$instance = Instance::query()->firstOrFail();
$roundTripped = InstanceConfig::fromArray([
'type' => $instance->type->value,
'url' => $instance->url,
'enabled' => $instance->enabled,
'interval_seconds' => $instance->interval_seconds,
'extras' => $instance->extras,
]);
$this->assertEquals($original, $roundTripped);
}
}

View file

@ -0,0 +1,113 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Lvl0\FediDiscover\Config\InstanceType;
use Lvl0\FediDiscover\Models\Instance;
use Tests\TestCase;
class InstanceModelTest extends TestCase
{
use RefreshDatabase;
public function test_it_persists_and_retrieves_an_instance(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'extras' => ['token' => 'abc123'],
]);
$instance = Instance::first();
$this->assertNotNull($instance);
$this->assertSame(InstanceType::Mastodon, $instance->type);
$this->assertSame('https://mastodon.social', $instance->url);
$this->assertTrue($instance->enabled);
$this->assertSame(600, $instance->interval_seconds);
$this->assertSame(['token' => 'abc123'], $instance->extras);
}
public function test_enabled_is_fillable_and_cast_to_boolean(): void
{
$instance = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => false,
'interval_seconds' => 600,
]);
$this->assertFalse($instance->fresh()->enabled);
}
public function test_last_polled_at_is_fillable_and_cast_to_datetime(): void
{
$polledAt = Carbon::parse('2026-04-23 12:00:00');
$instance = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'last_polled_at' => $polledAt,
]);
$fresh = $instance->fresh();
$this->assertInstanceOf(Carbon::class, $fresh->last_polled_at);
$this->assertTrue($fresh->last_polled_at->equalTo($polledAt));
}
public function test_last_seen_id_defaults_to_null(): void
{
$instance = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
]);
$this->assertNull($instance->fresh()->last_seen_id);
}
public function test_last_seen_id_is_fillable_and_persists_as_string(): void
{
$instance = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'last_seen_id' => '109876543210',
]);
$this->assertSame('109876543210', $instance->fresh()->last_seen_id);
}
public function test_enabled_scope_returns_only_enabled_instances(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://enabled.example',
'enabled' => true,
'interval_seconds' => 600,
]);
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://disabled.example',
'enabled' => false,
'interval_seconds' => 600,
]);
$enabled = Instance::enabled()->get();
$this->assertCount(1, $enabled);
$this->assertSame('https://enabled.example', $enabled->first()->url);
}
}

View file

@ -0,0 +1,150 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Feature;
use Illuminate\Support\Facades\Http;
use Lvl0\FediDiscover\Clients\LemmyClient;
use Lvl0\FediDiscover\Config\InstanceType;
use Lvl0\FediDiscover\Models\Instance;
use Lvl0\FediDiscover\ValueObjects\FediversePost;
use Tests\TestCase;
class LemmyClientTest extends TestCase
{
public function test_it_maps_each_post_to_a_fediverse_post(): void
{
Http::fake([
'*' => Http::response([
'posts' => [
$this->lemmyPost(
id: 42,
apId: 'https://lemmy.world/post/42',
name: 'My Great Post',
body: 'Some body text',
published: '2026-04-25T10:00:00.000000',
),
],
], 200),
]);
$posts = (new LemmyClient)->fetchPostsSince($this->lemmyInstance(), null);
$this->assertCount(1, $posts);
$this->assertInstanceOf(FediversePost::class, $posts->first());
$this->assertSame('42', $posts->first()->cursorId);
$this->assertSame('https://lemmy.world/post/42', $posts->first()->selfUrl);
$this->assertSame('My Great Post', $posts->first()->title);
$this->assertSame('Some body text', $posts->first()->body);
$this->assertSame('2026-04-25T10:00:00.000000', $posts->first()->publishedAt);
}
public function test_url_field_is_appended_to_body(): void
{
Http::fake([
'*' => Http::response([
'posts' => [
$this->lemmyPost(
id: 42,
apId: 'https://lemmy.world/post/42',
url: 'https://example-garden.blog/post-42',
body: 'Some original text.',
),
],
], 200),
]);
$post = (new LemmyClient)->fetchPostsSince($this->lemmyInstance(), null)->first();
$this->assertStringContainsString('Some original text.', $post->body);
$this->assertStringContainsString('https://example-garden.blog/post-42', $post->body);
}
public function test_url_absent_leaves_body_clean(): void
{
Http::fake([
'*' => Http::response([
'posts' => [
$this->lemmyPost(
id: 7,
apId: 'https://lemmy.world/post/7',
body: 'Just a regular post.',
),
],
], 200),
]);
$post = (new LemmyClient)->fetchPostsSince($this->lemmyInstance(), null)->first();
$this->assertSame('Just a regular post.', $post->body);
}
public function test_it_handles_posts_without_a_body_key(): void
{
Http::fake([
'*' => Http::response([
'posts' => [
[
'post' => [
'id' => 99,
'ap_id' => 'https://lemmy.world/post/99',
'url' => null,
'name' => 'Link-only post',
'published' => '2026-04-25T10:00:00.000000',
// 'body' key intentionally absent — real Lemmy API omits it for link-only posts
],
],
],
], 200),
]);
$post = (new LemmyClient)->fetchPostsSince($this->lemmyInstance(), null)->first();
$this->assertNull($post->body);
}
public function test_it_hits_the_post_list_endpoint_of_the_instance(): void
{
Http::fake([
'lemmy.world/api/v3/post/list*' => Http::response(['posts' => []], 200),
]);
(new LemmyClient)->fetchPostsSince($this->lemmyInstance(), null);
Http::assertSent(fn ($request) => str_starts_with($request->url(), 'https://lemmy.world/api/v3/post/list')
&& $request->method() === 'GET'
);
}
private function lemmyInstance(): Instance
{
return new Instance([
'type' => InstanceType::Lemmy,
'url' => 'https://lemmy.world',
]);
}
/**
* @return array<string, mixed>
*/
private function lemmyPost(
int $id,
string $apId,
?string $url = null,
string $body = '',
string $name = 'A post title',
string $published = '2026-04-25T10:00:00.000000',
): array {
return [
'post' => [
'id' => $id,
'ap_id' => $apId,
'url' => $url,
'body' => $body,
'name' => $name,
'published' => $published,
],
];
}
}

View file

@ -0,0 +1,191 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Feature;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Lvl0\FediDiscover\Clients\MastodonClient;
use Lvl0\FediDiscover\Config\InstanceType;
use Lvl0\FediDiscover\Models\Instance;
use Lvl0\FediDiscover\ValueObjects\FediversePost;
use Tests\TestCase;
class MastodonClientTest extends TestCase
{
public function test_it_hits_the_public_timeline_endpoint_of_the_instance(): void
{
Http::fake([
'mastodon.social/api/v1/timelines/public*' => Http::response([], 200),
]);
(new MastodonClient)->fetchPostsSince($this->mastodonInstance(), null);
Http::assertSent(fn ($request) => str_starts_with($request->url(), 'https://mastodon.social/api/v1/timelines/public')
&& $request->method() === 'GET'
);
}
public function test_it_omits_min_id_on_first_poll(): void
{
Http::fake(['*' => Http::response([], 200)]);
(new MastodonClient)->fetchPostsSince($this->mastodonInstance(), null);
Http::assertSent(fn ($request) => ! str_contains($request->url(), 'min_id'));
}
public function test_it_passes_min_id_on_subsequent_polls(): void
{
Http::fake(['*' => Http::response([], 200)]);
(new MastodonClient)->fetchPostsSince($this->mastodonInstance(), '109876543210');
Http::assertSent(fn ($request) => str_contains($request->url(), 'min_id=109876543210'));
}
public function test_it_returns_an_empty_collection_when_the_api_returns_no_posts(): void
{
Http::fake(['*' => Http::response([], 200)]);
$posts = (new MastodonClient)->fetchPostsSince($this->mastodonInstance(), null);
$this->assertInstanceOf(Collection::class, $posts);
$this->assertTrue($posts->isEmpty());
}
public function test_it_maps_each_status_to_a_fediverse_post(): void
{
Http::fake([
'*' => Http::response([
$this->mastodonStatus(id: '109876543210', url: 'https://mastodon.social/@alice/109876543210', content: '<p>Hello</p>'),
$this->mastodonStatus(id: '109876543211', url: 'https://mastodon.social/@bob/109876543211', content: '<p>World</p>'),
], 200),
]);
$posts = (new MastodonClient)->fetchPostsSince($this->mastodonInstance(), null);
$this->assertCount(2, $posts);
$this->assertInstanceOf(FediversePost::class, $posts->first());
$this->assertSame('109876543210', $posts->first()->cursorId);
$this->assertSame('https://mastodon.social/@alice/109876543210', $posts->first()->selfUrl);
$this->assertSame('<p>Hello</p>', $posts->first()->body);
}
public function test_it_maps_published_at_from_created_at(): void
{
Http::fake([
'*' => Http::response([
$this->mastodonStatus(id: '109876543210', url: 'https://mastodon.social/@alice/109876543210'),
], 200),
]);
$posts = (new MastodonClient)->fetchPostsSince($this->mastodonInstance(), null);
$this->assertSame('2026-04-25T10:00:00Z', $posts->first()->publishedAt);
}
public function test_it_sets_title_to_null_for_mastodon_statuses(): void
{
Http::fake([
'*' => Http::response([
$this->mastodonStatus(id: '109876543210', url: 'https://mastodon.social/@alice/109876543210'),
], 200),
]);
$posts = (new MastodonClient)->fetchPostsSince($this->mastodonInstance(), null);
$this->assertNull($posts->first()->title);
}
public function test_it_falls_back_to_uri_when_url_is_null(): void
{
Http::fake([
'*' => Http::response([
$this->mastodonStatus(
id: '109876543210',
url: null,
uri: 'https://hachyderm.io/users/bob/statuses/5678',
content: '<p>federated post</p>'
),
], 200),
]);
$posts = (new MastodonClient)->fetchPostsSince($this->mastodonInstance(), null);
$this->assertSame('https://hachyderm.io/users/bob/statuses/5678', $posts->first()->selfUrl);
}
public function test_it_preserves_newest_first_ordering_from_the_api(): void
{
Http::fake([
'*' => Http::response([
$this->mastodonStatus(id: '300', url: 'https://mastodon.social/@a/300'),
$this->mastodonStatus(id: '200', url: 'https://mastodon.social/@b/200'),
$this->mastodonStatus(id: '100', url: 'https://mastodon.social/@c/100'),
], 200),
]);
$posts = (new MastodonClient)->fetchPostsSince($this->mastodonInstance(), null);
$this->assertSame(['300', '200', '100'], $posts->pluck('cursorId')->all());
}
public function test_it_returns_an_empty_collection_on_a_non_2xx_response(): void
{
Http::fake(['*' => Http::response('Too many requests', 429)]);
$posts = (new MastodonClient)->fetchPostsSince($this->mastodonInstance(), null);
$this->assertInstanceOf(Collection::class, $posts);
$this->assertTrue($posts->isEmpty());
}
public function test_it_returns_an_empty_collection_when_the_response_body_is_not_json(): void
{
Http::fake(['*' => Http::response('<html>error</html>', 200)]);
$posts = (new MastodonClient)->fetchPostsSince($this->mastodonInstance(), null);
$this->assertInstanceOf(Collection::class, $posts);
$this->assertTrue($posts->isEmpty());
}
public function test_it_sends_the_configured_user_agent(): void
{
Http::fake(['*' => Http::response([], 200)]);
(new MastodonClient)->fetchPostsSince($this->mastodonInstance(), null);
$expected = config('fedi-discover.http.user_agent');
Http::assertSent(fn ($request) => $request->header('User-Agent')[0] === $expected);
}
private function mastodonInstance(): Instance
{
return new Instance([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
]);
}
/**
* @return array<string, mixed>
*/
private function mastodonStatus(
string $id,
?string $url = null,
?string $uri = null,
string $content = '<p>example</p>',
): array {
return [
'id' => $id,
'url' => $url,
'uri' => $uri ?? "https://mastodon.social/users/x/statuses/{$id}",
'content' => $content,
'created_at' => '2026-04-25T10:00:00Z',
'account' => ['acct' => 'alice@mastodon.social'],
];
}
}

View file

@ -0,0 +1,268 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Feature;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Lvl0\FediDiscover\Actions\PollFediverseAction;
use Lvl0\FediDiscover\Clients\FediverseClientFactory;
use Lvl0\FediDiscover\Clients\FediverseClientInterface;
use Lvl0\FediDiscover\Config\InstanceType;
use Lvl0\FediDiscover\Events\UrlDiscovered;
use Lvl0\FediDiscover\Models\Instance;
use Lvl0\FediDiscover\ValueObjects\FediversePost;
use Mockery;
use Tests\TestCase;
class PollFediverseActionTest extends TestCase
{
use RefreshDatabase;
public function test_it_fires_one_event_per_extracted_url(): void
{
Event::fake([UrlDiscovered::class]);
$this->poll([
new FediversePost('1', 'https://mastodon.social/@alice/1', 'See https://example.com/one and https://other.example/two'),
]);
Event::assertDispatched(UrlDiscovered::class, fn (UrlDiscovered $e) => $e->url === 'https://example.com/one');
Event::assertDispatched(UrlDiscovered::class, fn (UrlDiscovered $e) => $e->url === 'https://other.example/two');
Event::assertDispatchedTimes(UrlDiscovered::class, 2);
}
public function test_it_extracts_urls_from_html_anchor_tags(): void
{
Event::fake([UrlDiscovered::class]);
$this->poll([
new FediversePost('1', 'https://mastodon.social/@alice/1', '<p>Check <a href="https://example.com/article">this</a>!</p>'),
]);
Event::assertDispatched(UrlDiscovered::class, fn (UrlDiscovered $e) => $e->url === 'https://example.com/article');
Event::assertDispatchedTimes(UrlDiscovered::class, 1);
}
public function test_it_extracts_urls_from_markdown_links(): void
{
Event::fake([UrlDiscovered::class]);
$this->poll(
posts: [new FediversePost('1', 'https://lemmy.world/post/42', 'A [great article](https://example.com/article) about trees.')],
instanceUrl: 'https://lemmy.world',
);
Event::assertDispatched(UrlDiscovered::class, fn (UrlDiscovered $e) => $e->url === 'https://example.com/article');
Event::assertDispatchedTimes(UrlDiscovered::class, 1);
}
public function test_it_strips_trailing_punctuation_from_urls(): void
{
Event::fake([UrlDiscovered::class]);
$this->poll([
new FediversePost('1', 'https://mastodon.social/@alice/1', 'Check https://example.com/article, it is great. Also https://other.example/page.'),
]);
Event::assertDispatched(UrlDiscovered::class, fn (UrlDiscovered $e) => $e->url === 'https://example.com/article');
Event::assertDispatched(UrlDiscovered::class, fn (UrlDiscovered $e) => $e->url === 'https://other.example/page');
}
public function test_it_deduplicates_urls_within_a_single_post(): void
{
Event::fake([UrlDiscovered::class]);
$this->poll([
new FediversePost('1', 'https://mastodon.social/@alice/1', 'Here is https://example.com/article and again https://example.com/article'),
]);
Event::assertDispatchedTimes(UrlDiscovered::class, 1);
}
public function test_it_filters_urls_on_the_polling_instance_host(): void
{
Event::fake([UrlDiscovered::class]);
$this->poll([
new FediversePost('1', 'https://mastodon.social/@alice/1', 'See https://mastodon.social/@bob/42 and https://example.com/article'),
]);
Event::assertDispatched(UrlDiscovered::class, fn (UrlDiscovered $e) => $e->url === 'https://example.com/article');
Event::assertDispatchedTimes(UrlDiscovered::class, 1);
}
public function test_it_ignores_posts_with_a_null_body(): void
{
Event::fake([UrlDiscovered::class]);
$this->poll([
new FediversePost('1', 'https://mastodon.social/@alice/1', null),
]);
Event::assertNotDispatched(UrlDiscovered::class);
}
public function test_it_ignores_non_http_schemes(): void
{
Event::fake([UrlDiscovered::class]);
$this->poll([
new FediversePost('1', 'https://mastodon.social/@alice/1', 'Email mailto:alice@example.com or try ftp://files.example.com/x'),
]);
Event::assertNotDispatched(UrlDiscovered::class);
}
public function test_it_passes_post_self_url_and_body_through_to_the_event(): void
{
Event::fake([UrlDiscovered::class]);
$instance = $this->makeInstance();
$body = 'Here is https://example.com/article with surrounding context.';
$this->pollInstance($instance, [
new FediversePost('1', 'https://mastodon.social/@alice/1', $body),
]);
Event::assertDispatched(UrlDiscovered::class, fn (UrlDiscovered $e) => $e->postUrl === 'https://mastodon.social/@alice/1'
&& $e->postBody === $body
&& $e->instanceId === $instance->id
&& $e->discoveredAt instanceof CarbonImmutable
);
}
public function test_it_processes_multiple_posts(): void
{
Event::fake([UrlDiscovered::class]);
$this->poll([
new FediversePost('1', 'https://mastodon.social/@alice/1', 'See https://example.com/one'),
new FediversePost('2', 'https://mastodon.social/@bob/2', 'Also https://example.com/two'),
]);
Event::assertDispatchedTimes(UrlDiscovered::class, 2);
}
public function test_it_updates_last_seen_id_to_the_first_posts_cursor(): void
{
$instance = $this->makeInstance();
// Clients return newest-first; the action treats posts[0]
// as the new high-water mark without inspecting cursor values.
$this->pollInstance($instance, [
new FediversePost('newest-cursor', 'https://mastodon.social/@alice/3', 'x'),
new FediversePost('middle-cursor', 'https://mastodon.social/@bob/2', 'y'),
new FediversePost('oldest-cursor', 'https://mastodon.social/@carol/1', 'z'),
]);
$this->assertSame('newest-cursor', $instance->fresh()->last_seen_id);
}
public function test_it_updates_last_polled_at(): void
{
$instance = $this->makeInstance();
$this->assertNull($instance->last_polled_at);
$this->pollInstance($instance, [
new FediversePost('1', 'https://mastodon.social/@alice/1', 'x'),
]);
$this->assertNotNull($instance->fresh()->last_polled_at);
}
public function test_it_passes_the_existing_last_seen_id_to_the_client(): void
{
$instance = $this->makeInstance(['last_seen_id' => '999']);
$client = Mockery::mock(FediverseClientInterface::class);
$client->shouldReceive('fetchPostsSince')
->once()
->with($instance, $instance->last_seen_id)
->andReturn(collect());
$factory = Mockery::mock(FediverseClientFactory::class);
$factory->shouldReceive('for')->with($instance)->andReturn($client);
(new PollFediverseAction($factory))->execute($instance);
}
public function test_it_leaves_last_seen_id_unchanged_when_no_posts_are_returned(): void
{
$instance = $this->makeInstance(['last_seen_id' => '500']);
$this->pollInstance($instance, []);
$this->assertSame('500', $instance->fresh()->last_seen_id);
}
public function test_consecutive_poll_failures_reset_to_zero_after_successful_poll(): void
{
$instance = $this->makeInstance(['consecutive_poll_failures' => 5]);
$this->pollInstance($instance, []);
$this->assertSame(0, $instance->fresh()->consecutive_poll_failures);
}
public function test_poll_logs_a_structured_success_entry_with_url_count_and_duration(): void
{
Log::spy();
Event::fake([UrlDiscovered::class]);
$instance = $this->makeInstance();
$this->pollInstance($instance, [
new FediversePost('1', 'https://mastodon.social/@alice/1', 'See https://example.com/one and https://other.example/two'),
new FediversePost('2', 'https://mastodon.social/@bob/2', 'Also https://example.com/three'),
]);
Log::shouldHaveReceived('info')
->once()
->withArgs(function (string $message, array $context) use ($instance): bool {
return $message === 'fedi-discover:poll succeeded'
&& $context['instance_id'] === $instance->id
&& $context['url_count'] === 3
&& isset($context['duration_ms'])
&& $context['duration_ms'] >= 0;
});
}
/**
* @param array<FediversePost> $posts
*/
private function poll(array $posts, string $instanceUrl = 'https://mastodon.social'): void
{
$this->pollInstance($this->makeInstance(['url' => $instanceUrl]), $posts);
}
/**
* @param array<FediversePost> $posts
*/
private function pollInstance(Instance $instance, array $posts): void
{
$client = Mockery::mock(FediverseClientInterface::class);
$client->shouldReceive('fetchPostsSince')->andReturn(collect($posts));
$factory = Mockery::mock(FediverseClientFactory::class);
$factory->shouldReceive('for')->andReturn($client);
(new PollFediverseAction($factory))->execute($instance);
}
/**
* @param array<string, mixed> $overrides
*/
private function makeInstance(array $overrides = []): Instance
{
return Instance::create(array_merge([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
], $overrides));
}
}

View file

@ -0,0 +1,202 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Lvl0\FediDiscover\Actions\PollFediverseAction;
use Lvl0\FediDiscover\Clients\FediverseClientFactory;
use Lvl0\FediDiscover\Clients\FediverseClientInterface;
use Lvl0\FediDiscover\Config\InstanceType;
use Lvl0\FediDiscover\Events\PollFailed;
use Lvl0\FediDiscover\Models\Instance;
use Mockery;
use RuntimeException;
use Tests\TestCase;
class PollInstancesCommandTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
// Bind a no-op factory stub so the command can resolve PollFediverseAction
// from the container without making real HTTP calls.
$clientStub = Mockery::mock(FediverseClientInterface::class);
$clientStub->shouldReceive('fetchPostsSince')->andReturn(collect());
$factoryStub = Mockery::mock(FediverseClientFactory::class);
$factoryStub->shouldReceive('for')->andReturn($clientStub);
$this->app->instance(FediverseClientFactory::class, $factoryStub);
}
public function test_it_exits_zero_when_there_are_no_enabled_instances(): void
{
$this->artisan('fedi-discover:poll')
->assertExitCode(0);
}
public function test_it_calls_the_action_for_each_enabled_instance_and_skips_disabled(): void
{
$enabled1 = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
]);
$enabled2 = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://fosstodon.org',
'enabled' => true,
'interval_seconds' => 600,
]);
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://disabled.example',
'enabled' => false,
'interval_seconds' => 600,
]);
$calledWith = [];
$action = Mockery::mock(PollFediverseAction::class);
$action->shouldReceive('execute')
->twice()
->withArgs(function (Instance $instance) use (&$calledWith): bool {
$calledWith[] = $instance->url;
return true;
});
$this->app->instance(PollFediverseAction::class, $action);
$this->artisan('fedi-discover:poll')->assertExitCode(0);
$this->assertEqualsCanonicalizing(
[$enabled1->url, $enabled2->url],
$calledWith,
);
}
public function test_one_instance_throwing_does_not_stop_remaining_instances_from_being_polled(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://failing.example',
'enabled' => true,
'interval_seconds' => 600,
]);
$healthy = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://healthy.example',
'enabled' => true,
'interval_seconds' => 600,
]);
$calledWith = [];
$action = Mockery::mock(PollFediverseAction::class);
$action->shouldReceive('execute')
->twice()
->andReturnUsing(function (Instance $instance) use (&$calledWith): void {
$calledWith[] = $instance->url;
if ($instance->url === 'https://failing.example') {
throw new RuntimeException('Connection refused');
}
});
$this->app->instance(PollFediverseAction::class, $action);
$this->artisan('fedi-discover:poll')->assertExitCode(1);
$this->assertEqualsCanonicalizing(
['https://failing.example', $healthy->url],
$calledWith,
);
}
public function test_poll_failed_event_is_dispatched_when_action_throws(): void
{
Event::fake([PollFailed::class]);
$instance = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://failing.example',
'enabled' => true,
'interval_seconds' => 600,
]);
$action = Mockery::mock(PollFediverseAction::class);
$action->shouldReceive('execute')
->once()
->andReturnUsing(function (): void {
throw new RuntimeException('Connection refused');
});
$this->app->instance(PollFediverseAction::class, $action);
$this->artisan('fedi-discover:poll');
Event::assertDispatched(PollFailed::class, function (PollFailed $event) use ($instance): bool {
return $event->instance->id === $instance->id
&& $event->message === 'Connection refused';
});
}
public function test_poll_failed_event_is_not_dispatched_on_a_successful_poll(): void
{
Event::fake([PollFailed::class]);
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://healthy.example',
'enabled' => true,
'interval_seconds' => 600,
]);
// setUp() already binds a no-op action stub via the factory; no override needed.
$this->artisan('fedi-discover:poll');
Event::assertNotDispatched(PollFailed::class);
}
public function test_it_exits_one_when_at_least_one_instance_fails(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://failing.example',
'enabled' => true,
'interval_seconds' => 600,
]);
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://healthy.example',
'enabled' => true,
'interval_seconds' => 600,
]);
$action = Mockery::mock(PollFediverseAction::class);
$action->shouldReceive('execute')
->twice()
->andReturnUsing(function (Instance $instance): void {
if ($instance->url === 'https://failing.example') {
throw new RuntimeException('Connection refused');
}
});
$this->app->instance(PollFediverseAction::class, $action);
$this->artisan('fedi-discover:poll')->assertExitCode(1);
}
}

View file

@ -0,0 +1,221 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Lvl0\FediDiscover\Config\InstanceType;
use Lvl0\FediDiscover\Models\Instance;
use Tests\TestCase;
class ValidateInstancesCommandTest extends TestCase
{
use RefreshDatabase;
public function test_it_exits_zero_when_the_database_is_empty(): void
{
$this->artisan('fedi-discover:validate')
->assertExitCode(0);
}
public function test_it_exits_zero_when_all_instances_are_valid(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'extras' => [],
]);
$this->artisan('fedi-discover:validate')
->assertExitCode(0);
}
public function test_it_exits_nonzero_when_a_row_has_an_invalid_url(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'not-a-url',
'enabled' => true,
'interval_seconds' => 600,
'extras' => [],
]);
$this->artisan('fedi-discover:validate')
->assertExitCode(1);
}
public function test_it_exits_nonzero_when_a_row_has_a_zero_interval(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 0,
'extras' => [],
]);
$this->artisan('fedi-discover:validate')
->assertExitCode(1);
}
public function test_it_reports_summary_of_valid_and_invalid_counts(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'extras' => [],
]);
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://hachyderm.io',
'enabled' => true,
'interval_seconds' => 600,
'extras' => [],
]);
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'bogus',
'enabled' => true,
'interval_seconds' => 600,
'extras' => [],
]);
$this->artisan('fedi-discover:validate')
->expectsOutputToContain('3')
->expectsOutputToContain('2 valid')
->expectsOutputToContain('1 invalid')
->assertExitCode(1);
}
public function test_it_does_not_fail_fast_and_reports_every_invalid_row(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'bogus-one',
'enabled' => true,
'interval_seconds' => 600,
'extras' => [],
]);
$second = Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 0,
'extras' => [],
]);
$this->artisan('fedi-discover:validate')
->expectsOutputToContain('bogus-one')
->expectsOutputToContain((string) $second->id)
->assertExitCode(1);
}
public function test_it_includes_the_validation_error_message_for_each_invalid_row(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'not-a-url',
'enabled' => true,
'interval_seconds' => 600,
'extras' => [],
]);
$this->artisan('fedi-discover:validate')
->expectsOutputToContain('Invalid URL: not-a-url')
->assertExitCode(1);
}
public function test_summary_counts_are_accurate_when_mixed(): void
{
// 2 valid
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'extras' => [],
]);
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://hachyderm.io',
'enabled' => true,
'interval_seconds' => 600,
'extras' => [],
]);
// 3 invalid (different defects)
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'bogus-one',
'enabled' => true,
'interval_seconds' => 600,
'extras' => [],
]);
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://fosstodon.org',
'enabled' => true,
'interval_seconds' => 0,
'extras' => [],
]);
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'also-bad',
'enabled' => true,
'interval_seconds' => -5,
'extras' => [],
]);
$this->artisan('fedi-discover:validate')
->expectsOutputToContain('5')
->expectsOutputToContain('2 valid')
->expectsOutputToContain('3 invalid')
->assertExitCode(1);
}
public function test_it_exits_zero_with_enabled_only_when_no_enabled_instances_exist(): void
{
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => false,
'interval_seconds' => 600,
'extras' => [],
]);
$this->artisan('fedi-discover:validate', ['--enabled-only' => true])
->assertExitCode(0);
}
public function test_it_exits_zero_with_an_enabled_only_flag_when_disabled_rows_are_invalid(): void
{
// A disabled row that would fail InstanceConfig validation
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'broken-and-disabled',
'enabled' => false,
'interval_seconds' => 0,
'extras' => [],
]);
// A valid enabled row
Instance::create([
'type' => InstanceType::Mastodon,
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'extras' => [],
]);
$this->artisan('fedi-discover:validate', ['--enabled-only' => true])
->assertExitCode(0);
}
}

View file

@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Unit;
use Lvl0\FediDiscover\Config\InstanceConfig;
use Lvl0\FediDiscover\Config\InstanceType;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class InstanceConfigTest extends TestCase
{
public function test_from_array_returns_instance_config_with_correct_field_values(): void
{
$config = InstanceConfig::fromArray([
'type' => 'mastodon',
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'extras' => ['token' => 'abc123'],
]);
$this->assertSame(InstanceType::Mastodon, $config->type);
$this->assertSame('https://mastodon.social', $config->url);
$this->assertTrue($config->enabled);
$this->assertSame(600, $config->intervalSeconds);
$this->assertSame(['token' => 'abc123'], $config->extras);
}
public function test_from_array_rejects_non_positive_interval_seconds(): void
{
$this->expectException(\InvalidArgumentException::class);
InstanceConfig::fromArray([
'type' => 'mastodon',
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 0,
'extras' => [],
]);
}
public function test_extras_defaults_to_empty_array_when_omitted(): void
{
$config = InstanceConfig::fromArray([
'type' => 'mastodon',
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
]);
$this->assertSame([], $config->extras);
}
#[DataProvider('requiredKeyProvider')]
public function test_from_array_throws_when_required_key_is_missing(string $missingKey): void
{
$input = [
'type' => 'mastodon',
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
];
unset($input[$missingKey]);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/' . preg_quote($missingKey, '/') . '/');
InstanceConfig::fromArray($input);
}
public static function requiredKeyProvider(): array
{
return [
'type missing' => ['type'],
'url missing' => ['url'],
'enabled missing' => ['enabled'],
'interval_seconds missing' => ['interval_seconds'],
];
}
public function test_from_array_throws_invalid_argument_exception_for_unknown_type_string(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/pleroma/');
InstanceConfig::fromArray([
'type' => 'pleroma',
'url' => 'https://pleroma.example.com',
'enabled' => true,
'interval_seconds' => 600,
]);
}
public function test_from_array_rejects_malformed_url(): void
{
$this->expectException(\InvalidArgumentException::class);
InstanceConfig::fromArray([
'type' => 'mastodon',
'url' => 'not a url',
'enabled' => true,
'interval_seconds' => 600,
]);
}
public function test_to_array_produces_array_that_round_trips_through_from_array(): void
{
$original = [
'type' => 'mastodon',
'url' => 'https://mastodon.social',
'enabled' => true,
'interval_seconds' => 600,
'extras' => ['token' => 'abc123'],
];
$this->assertSame($original, InstanceConfig::fromArray($original)->toArray());
}
}

View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Unit;
use Carbon\CarbonImmutable;
use Lvl0\FediDiscover\Events\PollFailed;
use Lvl0\FediDiscover\Models\Instance;
use PHPUnit\Framework\TestCase;
class PollFailedTest extends TestCase
{
public function test_it_exposes_all_payload_fields(): void
{
$instance = new Instance;
$instance->id = 7;
$failedAt = CarbonImmutable::parse('2026-04-28T09:00:00');
$event = new PollFailed(
instance: $instance,
message: 'Connection timed out',
failedAt: $failedAt,
);
$this->assertSame($instance, $event->instance);
$this->assertSame('Connection timed out', $event->message);
$this->assertTrue($failedAt->eq($event->failedAt));
}
}

View file

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Lvl0\FediDiscover\Tests\Unit;
use Carbon\CarbonImmutable;
use Lvl0\FediDiscover\Events\UrlDiscovered;
use PHPUnit\Framework\TestCase;
class UrlDiscoveredTest extends TestCase
{
public function test_it_exposes_all_payload_fields(): void
{
$discoveredAt = CarbonImmutable::parse('2026-04-26T12:00:00');
$event = new UrlDiscovered(
url: 'https://example.com/article',
instanceId: 42,
discoveredAt: $discoveredAt,
postUrl: 'https://mastodon.social/@alice/109876543210',
postBody: 'Check out this article: https://example.com/article'
);
$this->assertSame('https://example.com/article', $event->url);
$this->assertSame(42, $event->instanceId);
$this->assertTrue($discoveredAt->eq($event->discoveredAt));
$this->assertSame('https://mastodon.social/@alice/109876543210', $event->postUrl);
$this->assertSame('Check out this article: https://example.com/article', $event->postBody);
}
public function test_post_body_is_nullable(): void
{
$event = new UrlDiscovered(
url: 'https://example.com/article',
instanceId: 1,
discoveredAt: CarbonImmutable::parse('2026-04-26T12:00:00'),
postUrl: 'https://mastodon.social/@alice/109876543210',
postBody: null
);
$this->assertNull($event->postBody);
}
}

12
phpstan.neon Normal file
View file

@ -0,0 +1,12 @@
includes:
- vendor/larastan/larastan/extension.neon
parameters:
paths:
- app
- packages/Lvl0/FediDiscover/src
level: 5
bootstrapFiles:
- vendor/autoload.php

47
phpunit.xml Normal file
View file

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
displayDetailsOnPhpunitDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerNotices="true"
>
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
<testsuite name="Package">
<directory>packages/Lvl0/FediDiscover/tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>app</directory>
<directory>packages/Lvl0/FediDiscover/src</directory>
</include>
</source>
<php>
<server name="APP_ENV" value="testing"/>
<server name="APP_MAINTENANCE_DRIVER" value="file"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="BROADCAST_CONNECTION" value="null"/>
<server name="CACHE_STORE" value="array"/>
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="DB_URL" value=""/>
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="PULSE_ENABLED" value="false"/>
<server name="TELESCOPE_ENABLED" value="false"/>
<server name="NIGHTWATCH_ENABLED" value="false"/>
<ini name="display_errors" value="On"/>
<ini name="error_reporting" value="-1"/>
</php>
</phpunit>

Some files were not shown because too many files have changed in this diff Show more