Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90a7e38ce9 | ||
|
|
d37999970c | ||
|
|
e247001bdb | ||
|
|
ac7cbd136b | ||
|
|
4fa53f63bd | ||
|
|
15cd034b29 | ||
|
|
125a49a1a0 | ||
|
|
5e34517e73 | ||
|
|
85fdff606d | ||
|
|
00b2685cf4 | ||
|
|
7c6fe80f1b | ||
|
|
e6729c77e7 | ||
|
|
f9658a8e7a | ||
|
|
6f1ab0073c | ||
|
|
001fdac3be | ||
|
|
fdcf5176bc | ||
|
|
7be6749a23 | ||
|
|
81de470f64 |
No files matched your search
@@ -15,5 +15,5 @@ jobs:
|
||||
- name: Build
|
||||
run: dotnet build PrivaPub.sln -c Release
|
||||
|
||||
- name: Test
|
||||
run: dotnet test PrivaPub.sln -c Release --no-build
|
||||
- name: Test (unit and integration, on a throwaway mongod)
|
||||
run: tools/ci/with-test-mongod.sh dotnet test PrivaPub.sln -c Release --no-build
|
||||
@@ -28,8 +28,8 @@ jobs:
|
||||
echo "BUILD_REF=${GITHUB_REF_NAME:-master}" >> "$GITHUB_ENV"
|
||||
echo "BUILD_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_ENV"
|
||||
|
||||
- name: Test
|
||||
run: dotnet test PrivaPub.sln -c Release
|
||||
- name: Test (unit and integration, on a throwaway mongod)
|
||||
run: tools/ci/with-test-mongod.sh dotnet test PrivaPub.sln -c Release
|
||||
|
||||
- name: Publish
|
||||
run: |
|
||||
@@ -90,6 +90,8 @@ jobs:
|
||||
fi
|
||||
|
||||
- name: Verify what is being served
|
||||
env:
|
||||
SMOKE_TOKEN: ${{ secrets.PRIVAPUB_SMOKE_TOKEN }}
|
||||
run: |
|
||||
served=$(curl -fsS "$PUBLIC_URL/build.json" | python3 -c "import json,sys; print(json.load(sys.stdin).get('commit',''))")
|
||||
[ "$served" = "$BUILD_COMMIT" ] || { echo "::error::served build is '$served', expected '$BUILD_COMMIT'"; exit 1; }
|
||||
@@ -104,5 +106,6 @@ jobs:
|
||||
code=$(curl -s -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/activity+json' \
|
||||
--data '{"type":"Follow","actor":"https://example.org/users/x","object":"'"$PUBLIC_URL"'/peasants/privapub"}' "$PUBLIC_URL/human-centipede")
|
||||
[ "$code" = "401" ] || { echo "::error::an unsigned inbox POST answered $code"; exit 1; }
|
||||
tools/smoke/mastodon-api.sh "$PUBLIC_URL"
|
||||
tools/smoke/mastodon-api.sh "$PUBLIC_URL" "$SMOKE_TOKEN"
|
||||
[ -n "$SMOKE_TOKEN" ] || echo "::warning::PRIVAPUB_SMOKE_TOKEN is not set, so the signed-in API was not checked"
|
||||
echo "::notice::serving $served"
|
||||
@@ -48,6 +48,7 @@ actor-scoped route gets a name in the same spirit, agreed with the owner, and a
|
||||
| Notes | `/peasants/{name}/scribbles/{id}` |
|
||||
| Activities | `/peasants/{name}/grunts/{id}` (`create-{postId}` resolves) |
|
||||
| DM context | `/peasants/{name}/whispers/{id}` |
|
||||
| Quote permission (FEP-044f `QuoteAuthorization`) | `/peasants/{name}/parrot-licences/{id}` |
|
||||
| Token refresh | `/clientapi/user/sniff/again` |
|
||||
|
||||
Agreed for later phases: `/gossip`, `/drool`, `/echoes` (replies, likes, shares), `/trophies` (featured), `/tattoos`
|
||||
@@ -123,6 +124,7 @@ dotnet build PrivaPub.sln -c Release
|
||||
dotnet test PrivaPub.sln # unit tests; integration tests skip
|
||||
PRIVAPUB_TEST_MONGOD=1 dotnet test PrivaPub.sln # all of them, against mongod on 127.0.0.1:27017
|
||||
# (PRIVAPUB_TEST_MONGO overrides; a fresh database per run, dropped after)
|
||||
tools/ci/with-test-mongod.sh dotnet test PrivaPub.sln # all of them, on a throwaway mongod, as CI runs them
|
||||
cd PrivaPub && ASPNETCORE_ENVIRONMENT=Development \
|
||||
Kestrel__Endpoints__Http__Url=http://127.0.0.1:6970 Kestrel__Endpoints__Http__Protocols=Http1AndHttp2 \
|
||||
AppConfiguration__BackendBaseAddress=http://127.0.0.1:6970 MongoSettings__Database=PrivaPubTest \
|
||||
@@ -197,7 +199,10 @@ cd /var/www/privapub.thepra.dev && sudo -u www-data ASPNETCORE_ENVIRONMENT=Produ
|
||||
3. Read parameters through `Params` (query, form and JSON merged Rails-style), never MVC binding. A value type read
|
||||
from a conditional must say `(int?)null`, not `default`: that bug once made every list one item long.
|
||||
4. Answer with `Json(...)` (snake_case, explicit nulls) or `Error(status, message)`; page lists with `Page` and `Link`.
|
||||
5. Unsupported features answer empty lists or 422 with a message, never 404 or 500, so clients degrade.
|
||||
5. Unsupported features answer empty lists or 422 with a message, never 404 or 500, so clients degrade. Any `/api`
|
||||
error that leaves without a body (a challenge, a 404 from routing, a 429) gets Mastodon's `{"error": ...}` from
|
||||
`UseMastodonErrorBodies`. `NeverFiveHundredTests` walks every route with junk ids, anonymously, as a persona and
|
||||
with a junk token.
|
||||
6. Advertise `4.2.0 (compatible; PrivaPub)` until grouped notifications exist.
|
||||
7. **What a Mastodon `Status` cannot say goes in `Status.privapub`** (`PrivaPubStatus`): object type, title, excerpt,
|
||||
cover, the author's source, link, video, audio and event details, and up/down votes. Every media URL in it goes
|
||||
@@ -212,6 +217,16 @@ cd /var/www/privapub.thepra.dev && sudo -u www-data ASPNETCORE_ENVIRONMENT=Produ
|
||||
sibling `media-proxy`, which `/media/files` does not serve.
|
||||
3. **A client never contacts a remote server for media:** every remote URL the API returns goes through
|
||||
`IMediaProxy.Wrap`, an HMAC-signed `/media/proxy/` URL fetched by `IFederationHttp.GetMedia`.
|
||||
4. **The proxy serves three ways:**
|
||||
- **Cached:** a file already cached is served from disk, ranges included.
|
||||
- **Downloaded:** a request without a `Range` is downloaded whole, up to `Media:MaxProxiedBytes`, then cached.
|
||||
- **Streamed:** a ranged request, or anything too big to cache, is streamed from the origin with the range passed on,
|
||||
and never cached. That is how remote video plays.
|
||||
|
||||
nginx has a `/media/proxy/` location with `proxy_buffering off` and a 600 s read timeout for those streams.
|
||||
5. **Remote video and audio become one playable attachment** in the Mastodon API (`MastodonMapper.Playable`): the best MP4
|
||||
up to 720p that carries both sound and picture, including PeerTube's fragmented files inside an HLS entry. HLS
|
||||
playlists themselves are not rewritten.
|
||||
|
||||
## Privacy invariants
|
||||
|
||||
@@ -233,6 +248,17 @@ cd /var/www/privapub.thepra.dev && sudo -u www-data ASPNETCORE_ENVIRONMENT=Produ
|
||||
decided in `docs/ROADMAP.md` ("Owner decisions on what PrivaPub reveals"). Anything new that tells another server
|
||||
something about an avatar gets the same treatment: ask, then record it there.
|
||||
- **Location-ranged posts never federate.**
|
||||
- **Statistics name servers, never people** (owner decisions on statistics, 2026-10-03). Every interaction is recorded
|
||||
through `IInteractionLedger` (`Infrastructure/Statistics`) as an `InteractionEvent` (90 days), but an event never
|
||||
holds a root, persona or group id, an activity id, an inbox URL, a remote actor URI or a sender IP:
|
||||
- Distinct remote accounts are counted with `ActorHash`, an HMAC keyed by that day's `InteractionSalt`, which the
|
||||
day's rollup deletes.
|
||||
- `LocalKind` survives `Interactions.Sanitize` only on public and unlisted traffic, so a circle can show neither by
|
||||
presence nor by absence, and no reason code names one.
|
||||
- Traffic a reader causes (the media proxy, lookups, the client API, fetches of our own documents) is only counted
|
||||
per day (`Count`, `CountServer`), never logged per event.
|
||||
- A host claimed by an unverified sender is kept only if it is already a `RemoteInstance`.
|
||||
- `Record` never blocks and never throws: it writes to a bounded channel, and a full channel drops and counts.
|
||||
|
||||
## Data
|
||||
|
||||
@@ -246,12 +272,35 @@ cd /var/www/privapub.thepra.dev && sudo -u www-data ASPNETCORE_ENVIRONMENT=Produ
|
||||
activity, inbox, key and signed headers; received time; up to 10 later revisions). Write it right after the post's
|
||||
save: `CreateHandler` (delivered) and `RemotePosts.StoreContext` (fetched) do, and `UpdateHandler` appends a
|
||||
revision. Delivery details reach them through `Arrival.Current`, which `InboxProcessor` sets for the handler's
|
||||
duration. The raw form lives outside `Post` so timelines never load it. Read through
|
||||
duration. A fetched record has no signature, key or `@context` of the activity that caused the fetch (its activity
|
||||
fields name that trigger), and every record stores its `Extensions` and `ContextNamespaces` for statistics. The raw form lives outside `Post` so timelines never load it. Read through
|
||||
`/api/privapub/v1/statuses/:id/provenance` and `/api/privapub/v1/instances/:host`.
|
||||
- **Remote objects are parsed for every shape in `ObjectShapes`:** `url`/`icon`/`image` as a value, an object or an
|
||||
array; Markdown `content`; missing `mediaType`s inferred; thumbnails from any of their five places; and the typed
|
||||
`Link`, `Video`, `Audio` and `Event` details. A Mastodon API card is built from those, never by fetching the linked page
|
||||
(that fetch is the owner's decision 1, for P6).
|
||||
- **An `Update` is an edit only when its `updated` is newer than ours** (`RemoteEdits.IsEdit`; plain and community-wrapped
|
||||
Updates both go through `RemoteEdits.Apply`, and deletes through `RemoteDeletes.Remove`). Otherwise it refreshes
|
||||
the poll, video, audio and event details and nothing else, and leaves no revision: Mastodon and Misskey refresh poll
|
||||
counts with bare Updates.
|
||||
- **A poll vote is a `Note` with a `name`, an `inReplyTo` that is a poll we hold, and no content.** `CreateHandler`
|
||||
hands it to `PollService.Receive` before anything else, so it never becomes a reply. Our votes on other servers' polls
|
||||
go only to the poll's author, without `published` (PieFed counts a vote only then).
|
||||
- **Link previews follow owner decision 1** (`Domain/Content/LinkPreviews.cs`): only public posts, queued on arrival with
|
||||
0–60 s of jitter, one cached `LinkPreview` per address for the whole server, never fetched when someone reads.
|
||||
`LinkPreviews.Wanted` is called where posts are saved (`CreateHandler`, `StoreContext`, `StatusService.Publish`).
|
||||
- **Quote states come from `QuoteService.Resolve`:**
|
||||
- with a stamp: accepted only if `Verified` (fetched, on the quoted author's origin, naming both posts exactly);
|
||||
- a FEP-044f `quote` without a stamp: pending;
|
||||
- older keys only: accepted when the quoted post is public.
|
||||
|
||||
`QuotesCount` moves with the accepted state, never with the raw key. When a persona quotes, `QuotePermission` decides:
|
||||
asking first for posts that state a policy, quoting at once for posts that state none, refusing otherwise. Only `quote`
|
||||
of a post that asks for consent puts `quote` in our JSON; older-key quotes leave it out, as Sharkey learned they must.
|
||||
- **Our quote policy is `ActivityPubRenderer.QuotableBy`:** the post's `LocalQuotePolicy`, falling back to the persona's
|
||||
`Settings.QuotePolicy` (default `public`, owner decision), and always `nobody` for anything but public and unlisted. The
|
||||
same rule writes `canQuote`, answers `QuoteRequest`s (`QuoteService.ReceiveRequest`) and fills `quote_approval`, so they
|
||||
cannot disagree. A persona quoting another persona gets a parrot-licence too, so other servers see an approved quote.
|
||||
- **A server is described on arrival, never on read.** The first record from a host enqueues `DescribeInstance` (its
|
||||
NodeInfo, at most once a week, into `RemoteInstance`), so opening the details view tells nobody anything.
|
||||
- **Post ids are the timeline order, so they follow arrival, not `published`.** `PrivacyIds.Arrived` gives a remote post
|
||||
@@ -284,18 +333,37 @@ cd /var/www/privapub.thepra.dev && sudo -u www-data ASPNETCORE_ENVIRONMENT=Produ
|
||||
stored as an edit date on every remote post. Write `(T?)null`. It has shipped four times (`MastodonParams.Bool/Int`,
|
||||
`NoteParser.Int`, `NoteParser.Time`).
|
||||
- ActivityPub output is built with `System.Text.Json.Nodes` in `ActivityPubRenderer`, not typed models. Inbound
|
||||
documents are read through `InboxService.Id`/`Value`, which handle string, object and array.
|
||||
documents are read through `ActivityJson.Id`/`Value`, which handle string, object and array.
|
||||
- Libraries chosen for the roadmap: HtmlSanitizer, Markdig (`DisableHtml`), NSign (RFC 9421 inbound), OpenIddict +
|
||||
OpenIddict.MongoDb, NetVips, Blurhash.Core, FFMpegCore. No ImageSharp (licence key enforced), no MassTransit.
|
||||
|
||||
## Testing
|
||||
|
||||
`PrivaPub.Tests` (xUnit v3). Unit tests need nothing; tests marked `Category=Integration` need a mongod and skip
|
||||
without `PRIVAPUB_TEST_MONGOD=1`. CI runs the unit tests only (the box's mongods are production).
|
||||
without `PRIVAPUB_TEST_MONGOD=1`. CI (`build.yml` and `deploy.yml`) runs all of them through
|
||||
`tools/ci/with-test-mongod.sh`, which starts a throwaway mongod on a random localhost port and deletes it afterwards.
|
||||
The box's own mongods are production, so `MongoFixture` refuses port 27022, a data directory under `/var/lib/privapub`,
|
||||
and in CI anything but the wrapper's mongod. With `PRIVAPUB_TEST_REQUIRE_MONGOD=1`, which the wrapper sets, a missing
|
||||
mongod fails the run instead of silently skipping half the tests.
|
||||
|
||||
- `Support/Peer` is an in-process HTTP server answering on two origins (`127.0.0.1` and `localhost`), so origin rules
|
||||
can be tested; `Support/RemoteActor` signs real deliveries with its own key.
|
||||
- Inbox scenarios go through `InboxService.Receive` with a signed request, not through the private handlers.
|
||||
- Inbox scenarios go through `InboxReceiver.Receive` with a signed request, not through the private handlers.
|
||||
- `Support/Host/PrivaPubHost` is the whole server under test (`WebApplicationFactory<Program>`, environment `Testing`,
|
||||
configured only through `UseSetting`, on the fixture's database). `PrivaPubHost.Shared()` boots it once per run;
|
||||
`SecureModeHost` is the same with `Federation:SecureMode`. Background workers are removed, so a test runs the jobs it
|
||||
queued with `host.Run(j => ...)` or `host.RunInbox(activityId)`. `Accounts` signs a root up, adds personas, and gets a
|
||||
Mastodon token through the real `/oauth` code flow; `RemoteActor.SignedPost`/`SignedGet` sign `HttpRequestMessage`s
|
||||
for the real `/peasants` routes. Each client gets its own `X-Test-Client` address, so rate limits don't collide.
|
||||
- All test classes share one database and run in parallel, so a test touches only rows it made:
|
||||
- random names and GUIDs;
|
||||
- `Harness.Outgoing` sees only deliveries queued since that harness started, because Peer ports are reused;
|
||||
- a worker gets a scoped `new JobQueue(j => ...)` so it never leases another test's jobs;
|
||||
- domain blocks are set with `DomainBlocks.Load`, never written to the database.
|
||||
|
||||
A test that must change something database-wide (drop indexes, run a migration over every post, let deliveries to
|
||||
`localhost` fail and trip its breaker) goes in `[Xunit.Collection(nameof(Exclusive))]`, which runs alone, and
|
||||
cleans up after itself. Pure logic belongs in an unconditional unit test, not in a Mongo-gated class.
|
||||
|
||||
Beyond the tests, verify by building, running locally, and exercising:
|
||||
- the client API (sign up, create an avatar, a group, a post);
|
||||
@@ -305,7 +373,7 @@ Interop is checked against real servers, starting with the workstation's own pas
|
||||
|
||||
```bash
|
||||
DOTNET=~/.dotnet/dotnet tools/pasture/run.sh up # podman: PrivaPub + the latest GoToSocial + Mongo, behind Caddy
|
||||
tools/pasture/interop.sh # 25 checks, each side driven through its own Mastodon API
|
||||
tools/pasture/interop.sh # 33 checks, each side driven through its own Mastodon API
|
||||
tools/pasture/run.sh down
|
||||
```
|
||||
|
||||
|
||||
+55
-4
@@ -50,6 +50,7 @@ actor document, a collection, NodeInfo or a delivery relates two avatars of the
|
||||
| Followers / following | `/peasants/{name}/groupies`, `/peasants/{name}/stalking` |
|
||||
| Objects | `/peasants/{name}/scribbles/{id}` |
|
||||
| Activities | `/peasants/{name}/grunts/{id}` |
|
||||
| Quote permissions (`QuoteAuthorization`) | `/peasants/{name}/parrot-licences/{id}` |
|
||||
| Direct-message context | `/peasants/{name}/whispers/{id}` |
|
||||
| Profile and post pages | `/@{name}`, `/@{name}/{id}` |
|
||||
|
||||
@@ -92,12 +93,13 @@ Received:
|
||||
| `Update{Person}` | refetches the actor |
|
||||
| `Like` | counted and notified, on posts the liker could see |
|
||||
| `Dislike` | counted as a downvote (Lemmy, PieFed, Mbin, Friendica); `Undo` takes it back |
|
||||
| `EmojiReact`, `Like` with an emoji `content` | an emoji reaction (FEP-c0e0; Pleroma, Akkoma, Iceshrimp.NET, Misskey, Sharkey), Unicode or a custom emoji from its `tag`; a `Like` whose content is ❤ stays a favourite; `Undo` takes it back |
|
||||
| `Join` | answered with `Ignore`: PrivaPub hosts no events yet (FEP-8a8e) |
|
||||
| `Announce` | counted and notified for local posts; shown to followers of the announcer, with the original refetched from its origin |
|
||||
| `Delete` | deletes the object, or the actor and its follows; a deleted object id is remembered for 90 days, so a late `Create` cannot bring it back |
|
||||
| `Flag` | becomes a report for this server's moderators |
|
||||
|
||||
Sent: `Follow`, `Undo{Follow}`, `Create{Note}`, `Update{Note}`, `Update{Person}`, `Delete{Tombstone}`, `Accept{Follow}`,
|
||||
Sent: `Follow`, `Undo{Follow}`, `Create{Note}`, `Create{Question}` and poll votes, `EmojiReact` and its `Undo`, `Update{Note}`, `Update{Person}`, `Delete{Tombstone}`, `Accept{Follow}`,
|
||||
`Reject{Follow}`, `Like`, `Announce` and their `Undo`, `Flag`. A deleted post answers 410 with a `Tombstone`.
|
||||
|
||||
- **Attachments** are `Document`s with `mediaType`, `name` (alt text), `blurhash`, `focalPoint`, `width` and `height`.
|
||||
@@ -108,7 +110,38 @@ Sent: `Follow`, `Undo{Follow}`, `Create{Note}`, `Update{Note}`, `Update{Person}`
|
||||
- **Reports** are sent as `Flag` by the instance actor, never by the reporting account.
|
||||
|
||||
A `Create`'s `Note` carries Mastodon's `content`, `contentMap`, `summary` and `sensitive`, plus `Mention` and `Hashtag`
|
||||
tags. A post's title becomes `name` and is also the first, bold line of `content`, because Mastodon does not show
|
||||
tags.
|
||||
|
||||
**Polls** (FEP-9967) go out as a `Question` with `oneOf` or `anyOf`, each option's count in `replies.totalItems`,
|
||||
`endTime`, `votersCount`, and `closed` once it has ended. Incoming votes are `Create{Note{name, inReplyTo}}` with no
|
||||
content, one per choice, counted once per voter. Counts are refreshed with an `Update{Question}` at most every three
|
||||
minutes. Our own votes on other servers' polls are sent the same way to the poll's author only, without `published`.
|
||||
An incoming `Update` without a newer `updated` only refreshes counts and details; it is never recorded as an edit.
|
||||
|
||||
**Quotes** (FEP-044f, with the older keys):
|
||||
- **Received.** A quote is read from `quote`, `quoteUrl`, `quoteUri`, `_misskey_quote` or a FEP-e232 `Link` tag.
|
||||
- With a `quoteAuthorization`, the stamp is fetched and must be a `QuoteAuthorization` on the quoted author's origin,
|
||||
attributed to them, naming exactly the two posts.
|
||||
- A FEP-044f `quote` without a stamp is pending until an `Update` brings one.
|
||||
- A quote made with the older keys only, by software that asks nobody, is shown when the quoted post is public.
|
||||
- A `Delete` of a stamp revokes the quote.
|
||||
- **Sent.** Our accounts quote with every key, plus the `RE:` fallback.
|
||||
- When the quoted post states an `interactionPolicy.canQuote` that allows us, `quote` is included and a `QuoteRequest`
|
||||
(with the quoting post as `instrument`) goes to the quoted author. The quote stays pending until their `Accept` brings a
|
||||
stamp we can verify; the post is then updated with `quoteAuthorization`.
|
||||
- Posts that state no policy are quoted the older way, without `quote`.
|
||||
- Quoting posts are also delivered to the quoted author.
|
||||
- **Quoting our accounts.** Our public and unlisted posts state `interactionPolicy.canQuote`. By default anyone may quote,
|
||||
automatically (as on Mastodon); an account can choose followers only or nobody, and so can each post.
|
||||
- A `QuoteRequest` is answered with `Accept{result}` naming a stamp at `/peasants/{name}/parrot-licences/{id}`, or
|
||||
with `Reject`.
|
||||
- A stamp is a `QuoteAuthorization` naming both posts. A revoked stamp answers 410 and is announced with
|
||||
`Delete{stamp}`.
|
||||
- Followers-only posts and DMs can never be quoted.
|
||||
|
||||
**Custom emoji** (`Emoji` tags) are read on posts, display names, bios and profile fields, at most 64 per object.
|
||||
**Profiles** keep their header, profile fields, `manuallyApprovesFollowers`, `published`, `movedTo`, `indexable`,
|
||||
`memorial` and avatar and header descriptions. A post's title becomes `name` and is also the first, bold line of `content`, because Mastodon does not show
|
||||
`name`. A content warning without its own text uses the title, or "Content warning".
|
||||
|
||||
Received `summary` is a content warning only on a `Note` or `Question`, or when `sensitive` is `true`. On an `Article`,
|
||||
@@ -117,6 +150,18 @@ Received `summary` is a content warning only on a `Note` or `Question`, or when
|
||||
Visibility is expressed in `to`/`cc` the way Mastodon does it: public, unlisted, followers-only and direct. Inbound
|
||||
followers-only posts are recognised by the author's own `followers` collection.
|
||||
|
||||
## Link previews
|
||||
|
||||
For a public post that links to a page, PrivaPub builds a preview card. It uses, in order:
|
||||
1. a Link attachment's own `preview` (FEP-8967) or the post's own image, title and summary;
|
||||
2. otherwise, the linked page, read once by the server between 0 and 60 seconds after the post arrives (never when
|
||||
someone reads it), with an `Accept: text/html` request that reads at most 512 KB, through the same address checks as
|
||||
every other fetch.
|
||||
|
||||
The page's OpenGraph and Twitter tags give the title, description and image; the result is cached per address for 7
|
||||
days and shared by every account on the server. Images are served to clients only through PrivaPub's media proxy.
|
||||
`Federation:FetchLinkPreviews=false` turns page fetching off.
|
||||
|
||||
## Local-only posts
|
||||
|
||||
Posts with a location (shown to nearby users of this server) never leave the server, in any form.
|
||||
@@ -132,12 +177,18 @@ Posts with a location (shown to nearby users of this server) never leave the ser
|
||||
- **Fetching.** All fetches are signed by the instance actor. They go only to public addresses, follow at most three
|
||||
redirects and read at most 1 MB.
|
||||
- **HTML.** Received HTML is sanitised to Mastodon's allowlist.
|
||||
- **Keys we cannot fetch for now.** When a sender's key cannot be fetched because its server timed out or answered
|
||||
5xx, the inbox answers 503 with `Retry-After: 300` rather than 401.
|
||||
- **Activity ids.** `Create` and `Announce` ids dereference. `Follow`, `Like`, `Block` and the `Accept`, `Reject` and
|
||||
`Undo` that answer them do not, because serving them would reveal who follows, likes and blocks whom; they are always
|
||||
sent with their object embedded.
|
||||
- **Delivery.** Failed deliveries are retried with Mastodon's backoff (16 attempts). A host that keeps failing is paused,
|
||||
starting at an hour and growing to a week.
|
||||
|
||||
## Known limitations
|
||||
|
||||
- Polls and custom emoji are not implemented.
|
||||
- Remote media is fetched through this server's proxy when a local client displays it.
|
||||
- Our own posts carry no custom emoji: emoji are received and shown, not offered.
|
||||
- Remote media is fetched through this server's proxy when a local client displays it. Video and audio are streamed
|
||||
through it with byte ranges passed on, so a PeerTube file is never fetched whole for one viewer.
|
||||
- Collections expose counts, not members.
|
||||
- Only `rsa-sha256`-style keys are verified. RFC 9421 signatures are planned.
|
||||
@@ -0,0 +1,43 @@
|
||||
using PrivaPub.Domain.Content;
|
||||
using PrivaPub.Models.Post;
|
||||
|
||||
namespace PrivaPub.Tests.Domain
|
||||
{
|
||||
public class LinkPreviewTests
|
||||
{
|
||||
[Fact]
|
||||
public void Reads_open_graph_then_twitter_then_the_page_title()
|
||||
{
|
||||
var preview = LinkPreviews.Read("https://news.example/a?utm_source=x", new Uri("https://news.example/a"), """
|
||||
<html><head><title>Fallback title</title>
|
||||
<meta property="og:title" content="Rain & sun">
|
||||
<meta name="twitter:description" content="A <b>long</b> story">
|
||||
<meta property="og:image" content="/img/cover.jpg">
|
||||
<meta property="og:site_name" content="News">
|
||||
</head><body></body></html>
|
||||
""");
|
||||
|
||||
Assert.Equal("Rain & sun", preview.Title);
|
||||
Assert.Equal("A long story", preview.Description);
|
||||
Assert.Equal("https://news.example/img/cover.jpg", preview.ImageURL);
|
||||
Assert.Equal("News", preview.SiteName);
|
||||
Assert.False(preview.Failed);
|
||||
Assert.True(LinkPreviews.Read("https://x.example", new Uri("https://x.example"), "<html><body>nothing</body></html>").Failed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Picks_the_first_real_link_and_skips_mentions_tags_quotes_and_ourselves() =>
|
||||
Assert.Equal("https://blog.example/post", LinkPreviews.FirstLink(new Post
|
||||
{
|
||||
Url = "https://m.example/@a/1",
|
||||
ContentHtml = """
|
||||
<p><span class="h-card"><a href="https://m.example/@bob" class="u-url mention">@bob</a></span>
|
||||
<a href="https://m.example/tags/x" class="mention hashtag" rel="tag">#x</a>
|
||||
<span class="quote-inline">RE: <a href="https://k.example/notes/1">link</a></span>
|
||||
<a href="https://privapub.test/@me">me</a>
|
||||
<a href="https://m.example/@a/1">self</a>
|
||||
<a href="https://blog.example/post">read this</a></p>
|
||||
"""
|
||||
}, "https://privapub.test"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using PrivaPub.Federation.Actors;
|
||||
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Federation
|
||||
{
|
||||
public class ActorFeaturesTests
|
||||
{
|
||||
[Fact]
|
||||
public void A_rich_actor_reports_the_extensions_it_uses()
|
||||
{
|
||||
using var rsa = RSA.Create(4096);
|
||||
var actor = new JsonObject
|
||||
{
|
||||
["id"] = "https://social.example/users/a",
|
||||
["type"] = "Person",
|
||||
["featured"] = "https://social.example/users/a/featured",
|
||||
["alsoKnownAs"] = new JsonArray("https://old.example/users/a"),
|
||||
["endpoints"] = new JsonObject { ["sharedInbox"] = "https://social.example/inbox" },
|
||||
["assertionMethod"] = new JsonArray(),
|
||||
["attachment"] = new JsonArray(new JsonObject { ["type"] = "PropertyValue", ["name"] = "x", ["value"] = "y" }),
|
||||
["manuallyApprovesFollowers"] = true,
|
||||
["discoverable"] = false,
|
||||
["indexable"] = true,
|
||||
["isCat"] = true,
|
||||
["_misskey_summary"] = "hi",
|
||||
["publicKey"] = new JsonObject { ["id"] = "https://social.example/users/a#main-key", ["publicKeyPem"] = rsa.ExportSubjectPublicKeyInfoPem() }
|
||||
};
|
||||
|
||||
var features = ActorFeatures.Detect(JsonDocument.Parse(actor.ToJsonString()).RootElement);
|
||||
|
||||
Assert.Equal(new[]
|
||||
{
|
||||
"featured", "also-known-as", "shared-inbox", "fep-521a-assertion-method", "property-value", "misskey", "is-cat",
|
||||
"indexable", "undiscoverable", "locked", "key:rsa-4096"
|
||||
}, features);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_bare_actor_reports_nothing_and_junk_is_survived()
|
||||
{
|
||||
Assert.Empty(ActorFeatures.Detect(JsonDocument.Parse("{\"id\":\"https://a.example/u\",\"type\":\"Person\"}").RootElement));
|
||||
Assert.Equal(new[] { "public-key-array" },
|
||||
ActorFeatures.Detect(JsonDocument.Parse("{\"publicKey\":[{\"publicKeyPem\":\"not a key\"}]}").RootElement));
|
||||
Assert.Empty(ActorFeatures.Detect(JsonDocument.Parse("[1,2]").RootElement));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using PrivaPub.Federation.Actors;
|
||||
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PrivaPub.Tests.Federation
|
||||
{
|
||||
public class ActorProfileTests
|
||||
{
|
||||
[Fact]
|
||||
public void Reads_a_mastodon_profile_with_its_header_fields_emoji_and_lock()
|
||||
{
|
||||
using var json = JsonDocument.Parse("""
|
||||
{
|
||||
"id": "https://m.example/users/ann", "type": "Person", "preferredUsername": "ann", "name": "Ann :sparkle:",
|
||||
"inbox": "https://m.example/users/ann/inbox", "manuallyApprovesFollowers": true, "indexable": true, "memorial": false,
|
||||
"published": "2023-04-01T00:00:00Z", "movedTo": "https://n.example/users/ann",
|
||||
"icon": { "type": "Image", "url": "https://m.example/a.png", "summary": "a cat" },
|
||||
"image": { "type": "Image", "url": "https://m.example/h.png", "name": "mountains" },
|
||||
"tag": [{ "type": "Emoji", "name": ":sparkle:", "icon": { "type": "Image", "url": "https://m.example/emoji/sparkle.png" } }],
|
||||
"attachment": [
|
||||
{ "type": "PropertyValue", "name": "Site", "value": "<a href=\"https://ann.example\" rel=\"me\">ann.example</a><script>x</script>" },
|
||||
{ "type": "PropertyValue", "name": "Pronouns", "value": "she/her" }
|
||||
]
|
||||
}
|
||||
""");
|
||||
|
||||
var actor = ActorDocument.Parse(json.RootElement);
|
||||
|
||||
Assert.True(actor.Locked);
|
||||
Assert.True(actor.Indexable);
|
||||
Assert.Equal("https://m.example/h.png", actor.Header);
|
||||
Assert.Equal("a cat", actor.IconDescription);
|
||||
Assert.Equal("mountains", actor.HeaderDescription);
|
||||
Assert.Equal("https://n.example/users/ann", actor.MovedTo);
|
||||
Assert.Equal(new DateTime(2023, 4, 1, 0, 0, 0, DateTimeKind.Utc), actor.Published);
|
||||
Assert.Equal("sparkle", Assert.Single(actor.Emojis).Shortcode);
|
||||
Assert.Equal(new[] { "Site", "Pronouns" }, actor.Fields.Keys);
|
||||
Assert.DoesNotContain("<script>", actor.Fields["Site"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.ClientModels.Social;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Models.Social;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Federation
|
||||
{
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class CommunityEditsTests : IAsyncLifetime
|
||||
{
|
||||
Harness _harness;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
_harness = await Harness.Start();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_harness != default)
|
||||
await _harness.DisposeAsync();
|
||||
}
|
||||
|
||||
static string Origin(RemoteActor actor) => new Uri(actor.Id).GetLeftPart(UriPartial.Authority);
|
||||
|
||||
async Task<(RemoteActor Community, RemoteActor Poster, JsonObject Note, Post Post)> Announced()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (root, alice) = await _harness.Persona("alice");
|
||||
var community = new RemoteActor(_harness.Peer, "cats", type: "Group");
|
||||
var poster = new RemoteActor(_harness.Peer, "poster");
|
||||
await _harness.Follows.Follow(root, new FollowForm { AvatarId = alice.Id, Target = community.Id }, token);
|
||||
await DB.Default.Update<Following>().Match(f => f.AvatarId == alice.Id).Modify(f => f.State, FollowState.Accepted).ExecuteAsync(token);
|
||||
var note = new JsonObject
|
||||
{
|
||||
["id"] = $"{Origin(poster)}/notes/{Guid.NewGuid():N}",
|
||||
["type"] = "Page",
|
||||
["name"] = "a title",
|
||||
["attributedTo"] = poster.Id,
|
||||
["content"] = "<p>first</p>",
|
||||
["to"] = new JsonArray(community.Id, Addressing.Public),
|
||||
["audience"] = community.Id,
|
||||
["published"] = DateTime.UtcNow.AddMinutes(-5).ToString("O")
|
||||
};
|
||||
_harness.Peer.Serve(new Uri(note["id"]!.GetValue<string>()).AbsolutePath, note.ToJsonString());
|
||||
await Announce(community, new JsonObject { ["id"] = $"{Origin(poster)}/create/{Guid.NewGuid():N}", ["type"] = "Create", ["actor"] = poster.Id, ["object"] = note.DeepClone() });
|
||||
var post = await DB.Default.Find<Post>().Match(p => p.ObjectURI == note["id"]!.GetValue<string>()).ExecuteSingleAsync(token);
|
||||
return (community, poster, note, post);
|
||||
}
|
||||
|
||||
Task Announce(RemoteActor community, JsonObject inner) =>
|
||||
_harness.Deliver(community, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = $"{Origin(community)}/announce/{Guid.NewGuid():N}",
|
||||
["type"] = "Announce",
|
||||
["actor"] = community.Id,
|
||||
["to"] = new JsonArray(Addressing.Public),
|
||||
["object"] = inner
|
||||
});
|
||||
|
||||
[Fact]
|
||||
public async Task A_community_edit_is_an_edit_only_when_newer_and_keeps_its_history()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (community, poster, note, post) = await Announced();
|
||||
|
||||
var refresh = (JsonObject)note.DeepClone();
|
||||
_harness.Peer.Serve(new Uri(note["id"]!.GetValue<string>()).AbsolutePath, refresh.ToJsonString());
|
||||
await Announce(community, new JsonObject { ["id"] = $"{Origin(poster)}/update/{Guid.NewGuid():N}", ["type"] = "Update", ["actor"] = poster.Id, ["object"] = note["id"]!.GetValue<string>() });
|
||||
var afterRefresh = await DB.Default.Find<Post>().OneAsync(post.ID, token);
|
||||
|
||||
var edit = (JsonObject)note.DeepClone();
|
||||
edit["content"] = "<p>second</p>";
|
||||
edit["updated"] = DateTime.UtcNow.ToString("O");
|
||||
_harness.Peer.Serve(new Uri(note["id"]!.GetValue<string>()).AbsolutePath, edit.ToJsonString());
|
||||
await Announce(community, new JsonObject { ["id"] = $"{Origin(poster)}/update/{Guid.NewGuid():N}", ["type"] = "Update", ["actor"] = poster.Id, ["object"] = note["id"]!.GetValue<string>() });
|
||||
var afterEdit = await DB.Default.Find<Post>().OneAsync(post.ID, token);
|
||||
var record = await DB.Default.Find<ObjectRecord>().Match(r => r.ObjectURI == post.ObjectURI).ExecuteSingleAsync(token);
|
||||
|
||||
Assert.Empty(afterRefresh.Revisions);
|
||||
Assert.Null(afterRefresh.EditedAt);
|
||||
Assert.Equal("<p>second</p>", afterEdit.ContentHtml);
|
||||
Assert.Equal("a title", afterEdit.Title);
|
||||
Assert.Single(afterEdit.Revisions);
|
||||
Assert.NotNull(afterEdit.EditedAt);
|
||||
Assert.Equal(2, record.Revisions.Count);
|
||||
Assert.Equal(new[] { "refresh", "edit" }, _harness.Ledger.Of("in").Where(e => e.Activity == "Announce" && e.Object == "Update").Select(e => e.Reason));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_community_delete_leaves_a_tombstone_and_no_trace()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (community, poster, note, post) = await Announced();
|
||||
_harness.Peer.Serve(new Uri(note["id"]!.GetValue<string>()).AbsolutePath, new JsonObject { ["id"] = note["id"]!.GetValue<string>(), ["type"] = "Tombstone" }.ToJsonString());
|
||||
|
||||
await Announce(community, new JsonObject { ["id"] = $"{Origin(poster)}/delete/{Guid.NewGuid():N}", ["type"] = "Delete", ["actor"] = poster.Id, ["object"] = note["id"]!.GetValue<string>() });
|
||||
|
||||
Assert.False(await DB.Default.Find<Post>().Match(p => p.ID == post.ID).ExecuteAnyAsync(token));
|
||||
Assert.False(await DB.Default.Find<ObjectRecord>().Match(r => r.ObjectURI == post.ObjectURI).ExecuteAnyAsync(token));
|
||||
Assert.False(await DB.Default.Find<TimelineEntry>().Match(e => e.PostId == post.ID).ExecuteAnyAsync(token));
|
||||
Assert.True(await DB.Default.Find<DeletedObject>().Match(d => d.ObjectURI == post.ObjectURI).ExecuteAnyAsync(token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_fetched_object_does_not_wear_the_signature_of_the_activity_that_caused_the_fetch()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (_, _, _, post) = await Announced();
|
||||
|
||||
var record = await DB.Default.Find<ObjectRecord>().Match(r => r.ObjectURI == post.ObjectURI).ExecuteSingleAsync(token);
|
||||
|
||||
Assert.Equal(ObjectPath.Fetched, record.Path);
|
||||
Assert.Null(record.KeyId);
|
||||
Assert.Null(record.SignatureScheme);
|
||||
Assert.Empty(record.SignedHeaders);
|
||||
Assert.Null(record.ActivityContext);
|
||||
Assert.Equal("Announce", record.ActivityType);
|
||||
Assert.InRange(record.ReceivedAt, DateTime.UtcNow.AddMinutes(-1), DateTime.UtcNow.AddSeconds(1));
|
||||
Assert.Contains("fep-1b12-audience", record.Extensions);
|
||||
Assert.NotNull(record.ContextNamespaces);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
using PrivaPub.Federation.Moderation;
|
||||
using PrivaPub.Models.Federation;
|
||||
|
||||
namespace PrivaPub.Tests.Federation
|
||||
{
|
||||
public class DomainBlocksTests
|
||||
{
|
||||
[Fact]
|
||||
public void A_block_covers_subdomains_but_not_lookalikes()
|
||||
{
|
||||
var blocks = new DomainBlocks(NullLogger<DomainBlocks>.Instance);
|
||||
blocks.Load(new[] { new DomainBlock { Domain = "evil.example", Severity = DomainBlockSeverity.Silence } });
|
||||
|
||||
Assert.NotNull(blocks.Find("evil.example"));
|
||||
Assert.NotNull(blocks.Find("A.Evil.Example."));
|
||||
Assert.Null(blocks.Find("notevil.example"));
|
||||
Assert.False(blocks.IsSuspended("evil.example"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_suspension_is_found_from_any_subdomain()
|
||||
{
|
||||
var blocks = new DomainBlocks(NullLogger<DomainBlocks>.Instance);
|
||||
blocks.Load(new[]
|
||||
{
|
||||
new DomainBlock { Domain = " Spam.Example. ", Severity = DomainBlockSeverity.Suspend },
|
||||
new DomainBlock { Domain = "", Severity = DomainBlockSeverity.Suspend }
|
||||
});
|
||||
|
||||
Assert.True(blocks.IsSuspended("deep.inside.spam.example"));
|
||||
Assert.False(blocks.IsSuspended("example"));
|
||||
Assert.Null(blocks.Find(default));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,6 +106,21 @@ namespace PrivaPub.Tests.Federation
|
||||
Assert.NotNull(message.ConversationId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task An_unreachable_key_asks_the_sender_to_retry_instead_of_refusing()
|
||||
{
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var offline = new RemoteActor(_harness.Peer, "offline", origin: "http://127.0.0.1:9");
|
||||
|
||||
var result = await _harness.Deliver(offline, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = NewId(offline, "follows"), ["type"] = "Follow", ["actor"] = offline.Id, ["object"] = alice.Uri
|
||||
});
|
||||
|
||||
Assert.Equal(503, result.StatusCode);
|
||||
Assert.Equal(300, result.RetryAfterSeconds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_join_of_a_local_post_is_answered_with_ignore()
|
||||
{
|
||||
|
||||
@@ -8,6 +8,8 @@ using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Inbox.Handlers;
|
||||
using PrivaPub.Federation.Inbox;
|
||||
using PrivaPub.Federation.Moderation;
|
||||
using PrivaPub.Domain.Social;
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Federation.Outbox;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
@@ -50,13 +52,15 @@ namespace PrivaPub.Tests.Federation
|
||||
var db = new DbEntities();
|
||||
_blocks = new DomainBlocks(NullLogger<DomainBlocks>.Instance);
|
||||
_receiver = new InboxReceiver(_local, remote, queue, _blocks, NullLogger<InboxReceiver>.Instance);
|
||||
var remotePosts = new RemotePosts(db, _local, remote, _blocks, queue, new ObjectRecords(queue), new NoPreviews());
|
||||
var quotes = new QuoteService(db, remote, remotePosts, _local, delivery, new OutboxPublisher(db, _local, delivery));
|
||||
_processor = new InboxProcessor(remote, new IActivityHandler[]
|
||||
{
|
||||
new FollowHandler(db, _local, remote, delivery),
|
||||
new UndoHandler(db, _local),
|
||||
new CreateHandler(db, _local, remote, delivery, _blocks, new Fanout(db), new RemotePosts(db, _local, remote, _blocks, queue, new ObjectRecords(queue)), new GroupDistributor(delivery), new ObjectRecords(queue)),
|
||||
new DeleteHandler(db, _local, remote, delivery, new GroupDistributor(delivery)),
|
||||
new UpdateHandler(db, _local, remote, new GroupDistributor(delivery), new ObjectRecords(queue))
|
||||
new UndoHandler(db, _local, new Reactions(db, delivery)),
|
||||
new CreateHandler(db, _local, remote, delivery, _blocks, new Fanout(db), remotePosts, new GroupDistributor(delivery), new ObjectRecords(queue), new PollService(db, _local, delivery, queue), new NoPreviews(), quotes),
|
||||
new DeleteHandler(db, _local, remote, delivery, new GroupDistributor(delivery), quotes),
|
||||
new UpdateHandler(db, _local, remote, new GroupDistributor(delivery), new ObjectRecords(queue), quotes)
|
||||
}, NullLogger<InboxProcessor>.Instance);
|
||||
}
|
||||
|
||||
@@ -265,10 +269,7 @@ namespace PrivaPub.Tests.Federation
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var alice = await LocalAvatar("alice");
|
||||
var bob = new RemoteActor(_peer, "bob", _peer.B);
|
||||
await DB.Default.SaveAsync(new DomainBlock { Domain = "localhost", Severity = DomainBlockSeverity.Suspend }, token);
|
||||
try
|
||||
{
|
||||
await _blocks.Reload(token);
|
||||
_blocks.Load(new[] { new DomainBlock { Domain = "localhost", Severity = DomainBlockSeverity.Suspend } });
|
||||
var before = _peer.Requests.Count;
|
||||
|
||||
var result = await Deliver(bob, $"/peasants/{alice.UserName}/mouth", DirectCreate(bob, alice.Uri));
|
||||
@@ -277,27 +278,6 @@ namespace PrivaPub.Tests.Federation
|
||||
Assert.Equal(before, _peer.Requests.Count);
|
||||
Assert.False(await DB.Default.Find<Post>().Match(p => p.ActorURI == bob.Id).ExecuteAnyAsync(token));
|
||||
}
|
||||
finally
|
||||
{
|
||||
await DB.Default.DeleteAsync<DomainBlock>(b => b.Domain == "localhost");
|
||||
await _blocks.Reload(token);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_block_covers_subdomains_but_not_lookalikes()
|
||||
{
|
||||
var blocks = new DomainBlocks(NullLogger<DomainBlocks>.Instance);
|
||||
typeof(DomainBlocks).GetField("_blocks", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
|
||||
.SetValue(blocks, new Dictionary<string, DomainBlock> { ["evil.example"] = new() { Domain = "evil.example", Severity = DomainBlockSeverity.Silence } });
|
||||
typeof(DomainBlocks).GetField("_loadedAt", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
|
||||
.SetValue(blocks, DateTime.UtcNow);
|
||||
|
||||
Assert.NotNull(blocks.Find("evil.example"));
|
||||
Assert.NotNull(blocks.Find("A.Evil.Example."));
|
||||
Assert.Null(blocks.Find("notevil.example"));
|
||||
Assert.False(blocks.IsSuspended("evil.example"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_bad_signature_is_a_401()
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
using PrivaPub.Federation.Objects;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Federation
|
||||
{
|
||||
public class ObjectRecordsTests
|
||||
{
|
||||
[Fact]
|
||||
public void An_object_larger_than_the_cap_keeps_only_its_hash()
|
||||
{
|
||||
var big = new JsonObject { ["content"] = new string('x', ObjectRecords.MaxRawBytes + 1) };
|
||||
|
||||
var (text, hash, bytes, truncated) = ObjectRecords.Capture(big);
|
||||
|
||||
Assert.Null(text);
|
||||
Assert.True(truncated);
|
||||
Assert.True(bytes > ObjectRecords.MaxRawBytes);
|
||||
Assert.Equal(64, hash.Length);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_small_object_is_kept_whole_with_its_hash()
|
||||
{
|
||||
var small = new JsonObject { ["type"] = "Note", ["content"] = "hi" };
|
||||
|
||||
var (text, hash, bytes, truncated) = ObjectRecords.Capture(small);
|
||||
|
||||
Assert.Equal(small.ToJsonString(), text);
|
||||
Assert.False(truncated);
|
||||
Assert.Equal(text.Length, bytes);
|
||||
Assert.Equal(64, hash.Length);
|
||||
Assert.Equal(default, ObjectRecords.Capture(default));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -169,6 +169,75 @@ namespace PrivaPub.Tests.Federation
|
||||
Assert.Equal("https://loops.example/poster.jpg", loop.CoverURL);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Keeps_custom_emoji_and_drops_ones_that_could_not_render()
|
||||
{
|
||||
var note = Parse("""
|
||||
{
|
||||
"id": "https://k.example/notes/1", "type": "Note", "attributedTo": "https://k.example/users/1", "content": ":blobcat: hi :bad:",
|
||||
"tag": [
|
||||
{ "type": "Emoji", "name": ":blobcat:", "updated": "2026-01-01T00:00:00Z", "icon": { "type": "Image", "mediaType": "image/png", "url": "https://k.example/emoji/blobcat.png" } },
|
||||
{ "type": "Emoji", "name": ":blobcat:", "icon": { "type": "Image", "url": "https://k.example/emoji/duplicate.png" } },
|
||||
{ "type": "Emoji", "name": ":bad name:", "icon": { "type": "Image", "url": "https://k.example/emoji/x.png" } },
|
||||
{ "type": "Emoji", "name": ":nourl:", "icon": { "type": "Image", "url": "data:image/png;base64,AAAA" } }
|
||||
]
|
||||
}
|
||||
""");
|
||||
|
||||
var emoji = Assert.Single(note.Emojis);
|
||||
Assert.Equal("blobcat", emoji.Shortcode);
|
||||
Assert.Equal("https://k.example/emoji/blobcat.png", emoji.URL);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reads_polls_from_mastodon_misskey_and_closed_ones()
|
||||
{
|
||||
var mastodon = Parse("""
|
||||
{ "id": "https://m.example/q/1", "type": "Question", "attributedTo": "https://m.example/u", "content": "?", "endTime": "2026-10-02T00:00:00Z", "votersCount": 7,
|
||||
"oneOf": [{ "type": "Note", "name": "a", "replies": { "type": "Collection", "totalItems": 4 } }, { "type": "Note", "name": "b", "replies": { "type": "Collection", "totalItems": 3 } }] }
|
||||
""");
|
||||
var misskey = Parse("""
|
||||
{ "id": "https://k.example/q/1", "type": "Question", "attributedTo": "https://k.example/u", "_misskey_content": "?",
|
||||
"anyOf": [{ "type": "Note", "name": "x", "_misskey_votes": 2 }, { "type": "Note", "name": "y", "_misskey_votes": 5 }] }
|
||||
""");
|
||||
var closed = Parse("""
|
||||
{ "id": "https://g.example/q/1", "type": "Question", "attributedTo": "https://g.example/u", "content": "?", "closed": "2026-09-30T12:00:00Z",
|
||||
"oneOf": [{ "type": "Note", "name": "p" }, { "type": "Note", "name": "q" }] }
|
||||
""");
|
||||
|
||||
Assert.False(mastodon.Poll.Multiple);
|
||||
Assert.Equal(new[] { 4, 3 }, mastodon.Poll.Options.Select(o => o.Votes));
|
||||
Assert.Equal(7, mastodon.Poll.VotersCount);
|
||||
Assert.True(misskey.Poll.Multiple);
|
||||
Assert.Equal(new[] { 2, 5 }, misskey.Poll.Options.Select(o => o.Votes));
|
||||
Assert.Null(misskey.Poll.VotersCount);
|
||||
Assert.Equal(new DateTime(2026, 9, 30, 12, 0, 0, DateTimeKind.Utc), closed.Poll.ClosedAt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Finds_playable_files_inside_an_hls_only_peertube_video_and_picks_one_with_sound()
|
||||
{
|
||||
var note = Parse("""
|
||||
{
|
||||
"id": "https://tube.example/videos/watch/2", "type": "Video", "name": "HLS only", "attributedTo": "https://tube.example/accounts/ann",
|
||||
"url": [
|
||||
{ "type": "Link", "mediaType": "text/html", "href": "https://tube.example/w/def" },
|
||||
{ "type": "Link", "mediaType": "application/x-mpegURL", "href": "https://tube.example/master.m3u8", "tag": [
|
||||
{ "type": "Link", "mediaType": "video/mp4", "href": "https://tube.example/1080-fragmented.mp4", "height": 1080,
|
||||
"attachment": [{ "type": "PropertyValue", "name": "ffprobe_codec_type", "value": "video" }, { "type": "PropertyValue", "name": "ffprobe_codec_type", "value": "audio" }] },
|
||||
{ "type": "Link", "mediaType": "video/mp4", "href": "https://tube.example/720-fragmented.mp4", "height": 720,
|
||||
"attachment": [{ "type": "PropertyValue", "name": "ffprobe_codec_type", "value": "video" }] },
|
||||
{ "type": "Link", "mediaType": "video/mp4", "href": "https://tube.example/480-fragmented.mp4", "height": 480,
|
||||
"attachment": [{ "type": "PropertyValue", "name": "ffprobe_codec_type", "value": "video" }, { "type": "PropertyValue", "name": "ffprobe_codec_type", "value": "audio" }] }
|
||||
] }
|
||||
]
|
||||
}
|
||||
""");
|
||||
|
||||
Assert.Equal(3, note.Video.Variants.Count);
|
||||
Assert.Equal("https://tube.example/480-fragmented.mp4", MastodonMapper.BestVideo(note.Video.Variants).Url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Normalises_hashtags_like_mastodon() =>
|
||||
Assert.Equal("fedi".Normalize(System.Text.NormalizationForm.FormKC).ToLowerInvariant(), Assert.Single(Parse("""
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Models.Social;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Federation
|
||||
{
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class PollTests : IAsyncLifetime
|
||||
{
|
||||
Harness _harness;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
_harness = await Harness.Start();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_harness != default)
|
||||
await _harness.DisposeAsync();
|
||||
}
|
||||
|
||||
static string Origin(RemoteActor actor) => new Uri(actor.Id).GetLeftPart(UriPartial.Authority);
|
||||
|
||||
static string NewId(RemoteActor actor, string kind) => $"{Origin(actor)}/{kind}/{Guid.NewGuid():N}";
|
||||
|
||||
async Task Follows(RemoteActor follower, string target) =>
|
||||
await _harness.Deliver(follower, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = NewId(follower, "follows"), ["type"] = "Follow", ["actor"] = follower.Id, ["object"] = target
|
||||
});
|
||||
|
||||
JsonObject Vote(RemoteActor voter, string question, string owner, string choice) => new()
|
||||
{
|
||||
["id"] = NewId(voter, "activities"), ["type"] = "Create", ["actor"] = voter.Id, ["to"] = new JsonArray(owner),
|
||||
["object"] = new JsonObject
|
||||
{
|
||||
["id"] = NewId(voter, "votes"), ["type"] = "Note", ["name"] = choice, ["attributedTo"] = voter.Id,
|
||||
["inReplyTo"] = question, ["to"] = new JsonArray(owner)
|
||||
}
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public async Task A_local_poll_goes_out_as_a_question_and_counts_each_remote_voter_once()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var mallory = new RemoteActor(_harness.Peer, "mallory");
|
||||
await Follows(mallory, alice.Uri);
|
||||
|
||||
var outcome = await _harness.Statuses.Publish(alice, new StatusDraft { Text = "which?", Poll = new PollDraft(new[] { "tea", "coffee" }, 3600, false, false) }, token);
|
||||
Assert.True(outcome.Ok);
|
||||
var question = (await _harness.Outgoing(mallory.Id + "/inbox")).Single(a => a["type"]!.GetValue<string>() == "Create")["object"]!;
|
||||
Assert.Equal("Question", question["type"]!.GetValue<string>());
|
||||
Assert.Equal(new[] { "tea", "coffee" }, question["oneOf"]!.AsArray().Select(o => o!["name"]!.GetValue<string>()));
|
||||
|
||||
await _harness.Deliver(mallory, "/human-centipede", Vote(mallory, outcome.Post.ObjectURI, alice.Uri, "coffee"));
|
||||
await _harness.Deliver(mallory, "/human-centipede", Vote(mallory, outcome.Post.ObjectURI, alice.Uri, "tea"));
|
||||
|
||||
var poll = (await DB.Default.Find<Post>().OneAsync(outcome.Post.ID, token)).Poll;
|
||||
Assert.Equal(new[] { 0, 1 }, poll.Options.Select(o => o.Votes));
|
||||
Assert.Equal(1, poll.VotersCount);
|
||||
Assert.False(await DB.Default.Find<Post>().Match(p => p.InReplyToURI == outcome.Post.ObjectURI).ExecuteAnyAsync(token));
|
||||
Assert.True(await DB.Default.Find<Job>().Match(j => j.Kind == JobKind.PollRefresh && j.Payload == outcome.Post.ID).ExecuteAnyAsync(token));
|
||||
Assert.True(await DB.Default.Find<Job>().Match(j => j.Kind == JobKind.PollClose && j.Payload == outcome.Post.ID).ExecuteAnyAsync(token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_persona_votes_on_a_remote_poll_and_a_count_refresh_is_not_an_edit()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var mallory = new RemoteActor(_harness.Peer, "mallory");
|
||||
var questionId = NewId(mallory, "questions");
|
||||
JsonObject Question(int first, int second) => new()
|
||||
{
|
||||
["id"] = questionId, ["type"] = "Question", ["attributedTo"] = mallory.Id, ["content"] = "<p>pick</p>",
|
||||
["to"] = new JsonArray(Addressing.Public), ["cc"] = new JsonArray(alice.Uri),
|
||||
["tag"] = new JsonArray(new JsonObject { ["type"] = "Mention", ["href"] = alice.Uri, ["name"] = "@alice" }),
|
||||
["endTime"] = DateTime.UtcNow.AddDays(1).ToString("O"),
|
||||
["anyOf"] = new JsonArray(
|
||||
new JsonObject { ["type"] = "Note", ["name"] = "red", ["replies"] = new JsonObject { ["type"] = "Collection", ["totalItems"] = first } },
|
||||
new JsonObject { ["type"] = "Note", ["name"] = "blue", ["replies"] = new JsonObject { ["type"] = "Collection", ["totalItems"] = second } })
|
||||
};
|
||||
await _harness.Deliver(mallory, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = NewId(mallory, "activities"), ["type"] = "Create", ["actor"] = mallory.Id, ["object"] = Question(3, 1)
|
||||
});
|
||||
var post = await DB.Default.Find<Post>().Match(p => p.ObjectURI == questionId).ExecuteFirstAsync(token);
|
||||
Assert.True(post.Poll.Multiple);
|
||||
|
||||
Assert.True((await _harness.Polls.Vote(alice, post, new[] { 0, 1 }, token)).Ok);
|
||||
Assert.False((await _harness.Polls.Vote(alice, post, new[] { 0 }, token)).Ok);
|
||||
|
||||
var votes = (await _harness.Outgoing(mallory.Id + "/inbox")).Where(a => a["type"]!.GetValue<string>() == "Create").Select(a => a["object"]!).ToList();
|
||||
Assert.Equal(new[] { "red", "blue" }, votes.Select(v => v["name"]!.GetValue<string>()).Order().Reverse());
|
||||
Assert.All(votes, v => Assert.Equal(questionId, v["inReplyTo"]!.GetValue<string>()));
|
||||
Assert.All(votes, v => Assert.Null(v["published"]));
|
||||
Assert.Equal(new[] { 0, 1 }, (await _harness.Polls.OwnVotes(post.ID, alice.Id, token)).Order());
|
||||
|
||||
await _harness.Deliver(mallory, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = NewId(mallory, "activities"), ["type"] = "Update", ["actor"] = mallory.Id, ["object"] = Question(9, 5)
|
||||
});
|
||||
var refreshed = await DB.Default.Find<Post>().OneAsync(post.ID, token);
|
||||
Assert.Equal(new[] { 9, 5 }, refreshed.Poll.Options.Select(o => o.Votes));
|
||||
Assert.Empty(refreshed.Revisions);
|
||||
Assert.Null(refreshed.EditedAt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Closing_a_poll_tells_its_voters_and_its_author()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var (_, bob) = await _harness.Persona("bob");
|
||||
var outcome = await _harness.Statuses.Publish(alice, new StatusDraft { Text = "quick", Poll = new PollDraft(new[] { "yes", "no" }, 300, false, false) }, token);
|
||||
Assert.True((await _harness.Polls.Vote(bob, outcome.Post, new[] { 0 }, token)).Ok);
|
||||
Assert.False((await _harness.Polls.Vote(alice, outcome.Post, new[] { 0 }, token)).Ok);
|
||||
|
||||
var job = await DB.Default.Find<Job>().Match(j => j.Kind == JobKind.PollClose && j.Payload == outcome.Post.ID).ExecuteFirstAsync(token);
|
||||
await new PollCloseJob(_harness.Db, _harness.Local, _harness.Outbox).Handle(job, token);
|
||||
|
||||
Assert.NotNull((await DB.Default.Find<Post>().OneAsync(outcome.Post.ID, token)).Poll.ClosedAt);
|
||||
var told = await DB.Default.Find<Notification>().Match(n => n.Type == NotificationType.Poll && n.PostId == outcome.Post.ID).ExecuteAsync(token);
|
||||
Assert.Equal(new[] { alice.Id, bob.Id }.Order(), told.Select(n => n.AvatarId).Order());
|
||||
Assert.False((await _harness.Polls.Vote(bob, await DB.Default.Find<Post>().OneAsync(outcome.Post.ID, token), new[] { 1 }, token)).Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,18 +68,5 @@ namespace PrivaPub.Tests.Federation
|
||||
Assert.Equal("<p>hi again</p>", JsonNode.Parse(Assert.Single(record.Revisions).Raw)!["content"]!.GetValue<string>());
|
||||
Assert.True(await DB.Default.Find<Job>().Match(j => j.Kind == JobKind.DescribeInstance && j.Payload == record.Host).ExecuteAnyAsync(token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_object_larger_than_the_cap_keeps_only_its_hash()
|
||||
{
|
||||
var big = new JsonObject { ["content"] = new string('x', ObjectRecords.MaxRawBytes + 1) };
|
||||
|
||||
var (text, hash, bytes, truncated) = ObjectRecords.Capture(big);
|
||||
|
||||
Assert.Null(text);
|
||||
Assert.True(truncated);
|
||||
Assert.True(bytes > ObjectRecords.MaxRawBytes);
|
||||
Assert.Equal(64, hash.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.ClientModels.Post;
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Federation
|
||||
{
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class QuoteTests : IAsyncLifetime
|
||||
{
|
||||
Harness _harness;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
_harness = await Harness.Start();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_harness != default)
|
||||
await _harness.DisposeAsync();
|
||||
}
|
||||
|
||||
static string Origin(RemoteActor actor) => new Uri(actor.Id).GetLeftPart(UriPartial.Authority);
|
||||
|
||||
static string NewId(RemoteActor actor, string kind) => $"{Origin(actor)}/{kind}/{Guid.NewGuid():N}";
|
||||
|
||||
async Task<Post> Delivered(RemoteActor author, string mentioned, Action<JsonObject> shape = default)
|
||||
{
|
||||
var note = new JsonObject
|
||||
{
|
||||
["id"] = NewId(author, "notes"), ["type"] = "Note", ["attributedTo"] = author.Id, ["content"] = "<p>hello</p>",
|
||||
["to"] = new JsonArray(Addressing.Public), ["cc"] = new JsonArray(mentioned),
|
||||
["tag"] = new JsonArray(new JsonObject { ["type"] = "Mention", ["href"] = mentioned, ["name"] = "@someone" })
|
||||
};
|
||||
shape?.Invoke(note);
|
||||
await _harness.Deliver(author, "/human-centipede", new JsonObject { ["id"] = NewId(author, "activities"), ["type"] = "Create", ["actor"] = author.Id, ["object"] = note });
|
||||
return await DB.Default.Find<Post>().Match(p => p.ObjectURI == note["id"]!.GetValue<string>()).ExecuteFirstAsync(TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
string Stamp(RemoteActor quotedAuthor, string quoting, string quoted)
|
||||
{
|
||||
var path = $"/stamps/{Guid.NewGuid():N}";
|
||||
var id = Origin(quotedAuthor) + path;
|
||||
_harness.Peer.Serve(path, new JsonObject
|
||||
{
|
||||
["@context"] = "https://www.w3.org/ns/activitystreams", ["id"] = id, ["type"] = "QuoteAuthorization",
|
||||
["attributedTo"] = quotedAuthor.Id, ["interactingObject"] = quoting, ["interactionTarget"] = quoted
|
||||
}.ToJsonString());
|
||||
return id;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_misskey_quote_of_a_public_post_is_shown_and_counted()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var ann = new RemoteActor(_harness.Peer, "ann");
|
||||
var kitty = new RemoteActor(_harness.Peer, "kitty");
|
||||
var original = await Delivered(ann, alice.Uri);
|
||||
|
||||
var quoting = await Delivered(kitty, alice.Uri, n =>
|
||||
{
|
||||
n["_misskey_quote"] = original.ObjectURI;
|
||||
n["content"] = $"<p>look<span class=\"quote-inline\"><br>RE: <a href=\"{original.ObjectURI}\">{original.ObjectURI}</a></span></p>";
|
||||
});
|
||||
|
||||
Assert.Equal(QuoteState.Accepted, quoting.QuoteState);
|
||||
Assert.Equal(original.ID, quoting.QuotedPostId);
|
||||
Assert.Equal(1, (await DB.Default.Find<Post>().OneAsync(original.ID, token)).QuotesCount);
|
||||
var status = await new PrivaPub.Api.Mastodon.Mappers.MastodonMapper(_harness.Db, _harness.Local).Status(quoting, alice.Id, token);
|
||||
Assert.Equal("accepted", status.Quote.State);
|
||||
Assert.Equal(original.ID, status.Quote.QuotedStatus.Id);
|
||||
Assert.DoesNotContain("RE:", status.Content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_stamped_quote_is_accepted_a_forged_one_is_not_and_revoking_takes_it_back()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var ann = new RemoteActor(_harness.Peer, "ann");
|
||||
var bob = new RemoteActor(_harness.Peer, "bob");
|
||||
var original = await Delivered(ann, alice.Uri);
|
||||
var quotingId = NewId(bob, "notes");
|
||||
var stamp = Stamp(ann, quotingId, original.ObjectURI);
|
||||
var forged = Stamp(ann, NewId(bob, "notes"), original.ObjectURI);
|
||||
|
||||
var quoting = await Delivered(bob, alice.Uri, n => { n["id"] = quotingId; n["quote"] = original.ObjectURI; n["quoteAuthorization"] = stamp; });
|
||||
var cheat = await Delivered(bob, alice.Uri, n => { n["quote"] = original.ObjectURI; n["quoteAuthorization"] = forged; });
|
||||
var waiting = await Delivered(bob, alice.Uri, n => n["quote"] = original.ObjectURI);
|
||||
|
||||
Assert.Equal(QuoteState.Accepted, quoting.QuoteState);
|
||||
Assert.Equal(QuoteState.Unauthorized, cheat.QuoteState);
|
||||
Assert.Equal(QuoteState.Pending, waiting.QuoteState);
|
||||
|
||||
await _harness.Deliver(ann, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = NewId(ann, "activities"), ["type"] = "Delete", ["actor"] = ann.Id, ["object"] = stamp
|
||||
});
|
||||
Assert.Equal(QuoteState.Revoked, (await DB.Default.Find<Post>().OneAsync(quoting.ID, token)).QuoteState);
|
||||
Assert.Equal(0, (await DB.Default.Find<Post>().OneAsync(original.ID, token)).QuotesCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_persona_asks_before_quoting_and_is_stamped_when_accepted()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var ann = new RemoteActor(_harness.Peer, "ann");
|
||||
var original = await Delivered(ann, alice.Uri, n => n["interactionPolicy"] = new JsonObject
|
||||
{
|
||||
["canQuote"] = new JsonObject { ["automaticApproval"] = new JsonArray(Addressing.Public) }
|
||||
});
|
||||
|
||||
var outcome = await _harness.Statuses.Publish(alice, new StatusDraft { Text = "this", QuotedStatusId = original.ID }, token);
|
||||
Assert.Equal(QuoteState.Pending, outcome.Post.QuoteState);
|
||||
var request = Assert.Single(await _harness.Outgoing(ann.Id + "/inbox"), a => a["type"]!.GetValue<string>() == "QuoteRequest");
|
||||
Assert.Equal(original.ObjectURI, request["object"]!.GetValue<string>());
|
||||
Assert.Equal(original.ObjectURI, request["instrument"]!["quote"]!.GetValue<string>());
|
||||
|
||||
await _harness.Deliver(ann, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = NewId(ann, "activities"), ["type"] = "Accept", ["actor"] = ann.Id,
|
||||
["object"] = request["id"]!.GetValue<string>(), ["result"] = Stamp(ann, outcome.Post.ObjectURI, original.ObjectURI)
|
||||
});
|
||||
|
||||
var quoting = await DB.Default.Find<Post>().OneAsync(outcome.Post.ID, token);
|
||||
Assert.Equal(QuoteState.Accepted, quoting.QuoteState);
|
||||
Assert.NotNull(quoting.QuoteAuthorizationURI);
|
||||
Assert.Contains(await _harness.Outgoing(ann.Id + "/inbox"), a => a["type"]!.GetValue<string>() == "Update"
|
||||
&& a["object"]!["quoteAuthorization"]?.GetValue<string>() == quoting.QuoteAuthorizationURI);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_legacy_post_is_quoted_without_asking_and_a_persona_that_allows_nobody_cannot_be_quoted()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (aliceRoot, alice) = await _harness.Persona("alice");
|
||||
var (bobRoot, bob) = await _harness.Persona("bob");
|
||||
var kitty = new RemoteActor(_harness.Peer, "kitty");
|
||||
var legacy = await Delivered(kitty, alice.Uri);
|
||||
bob.Settings.QuotePolicy = PrivaPub.Models.User.QuotePolicies.Nobody;
|
||||
var bobs = (await _harness.Statuses.Publish(bob, new StatusDraft { Text = "mine" }, token)).Post;
|
||||
|
||||
var outcome = await _harness.Statuses.Publish(alice, new StatusDraft { Text = "nice", QuotedStatusId = legacy.ID }, token);
|
||||
Assert.Equal(QuoteState.Accepted, outcome.Post.QuoteState);
|
||||
Assert.False(outcome.Post.QuoteByConsent);
|
||||
Assert.False((await _harness.Statuses.Publish(alice, new StatusDraft { Text = "and this", QuotedStatusId = bobs.ID }, token)).Ok);
|
||||
Assert.True((await _harness.Statuses.Publish(bob, new StatusDraft { Text = "me again", QuotedStatusId = bobs.ID }, token)).Ok);
|
||||
}
|
||||
|
||||
JsonObject Request(RemoteActor quoter, string quotedUri, string quotingId) => new()
|
||||
{
|
||||
["id"] = NewId(quoter, "activities"), ["type"] = "QuoteRequest", ["actor"] = quoter.Id, ["object"] = quotedUri,
|
||||
["instrument"] = new JsonObject { ["id"] = quotingId, ["type"] = "Note", ["attributedTo"] = quoter.Id, ["quote"] = quotedUri, ["content"] = "<p>so true</p>" }
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public async Task Anyone_may_quote_a_persona_by_default_and_the_licence_can_be_revoked()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var mallory = new RemoteActor(_harness.Peer, "mallory");
|
||||
await _harness.Deliver(mallory, "/human-centipede", new JsonObject { ["id"] = NewId(mallory, "follows"), ["type"] = "Follow", ["actor"] = mallory.Id, ["object"] = alice.Uri });
|
||||
var original = (await _harness.Statuses.Publish(alice, new StatusDraft { Text = "quote me" }, token)).Post;
|
||||
var created = (await _harness.Outgoing(mallory.Id + "/inbox")).Single(a => a["type"]!.GetValue<string>() == "Create")["object"]!;
|
||||
Assert.Equal(Addressing.Public, created["interactionPolicy"]!["canQuote"]!["automaticApproval"]![0]!.GetValue<string>());
|
||||
|
||||
var quotingId = NewId(mallory, "notes");
|
||||
await _harness.Deliver(mallory, "/human-centipede", Request(mallory, original.ObjectURI, quotingId));
|
||||
var accept = Assert.Single(await _harness.Outgoing(mallory.Id + "/inbox"), a => a["type"]!.GetValue<string>() == "Accept" && a["result"] != null);
|
||||
var licence = accept["result"]!.GetValue<string>();
|
||||
Assert.StartsWith(alice.Uri + "/parrot-licences/", licence);
|
||||
|
||||
var quoting = await Delivered(mallory, alice.Uri, n => { n["id"] = quotingId; n["quote"] = original.ObjectURI; n["quoteAuthorization"] = licence; });
|
||||
Assert.Equal(QuoteState.Accepted, quoting.QuoteState);
|
||||
Assert.Equal(1, (await DB.Default.Find<Post>().OneAsync(original.ID, token)).QuotesCount);
|
||||
|
||||
Assert.True(await _harness.Quotes.RevokeLicence(original, quoting, token));
|
||||
Assert.Equal(QuoteState.Revoked, (await DB.Default.Find<Post>().OneAsync(quoting.ID, token)).QuoteState);
|
||||
Assert.Contains(await _harness.Outgoing(mallory.Id + "/inbox"), a => a["type"]!.GetValue<string>() == "Delete" && a["object"]!.GetValue<string>() == licence);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_followers_only_persona_turns_a_stranger_down()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
alice.Settings.QuotePolicy = PrivaPub.Models.User.QuotePolicies.Followers;
|
||||
var original = (await _harness.Statuses.Publish(alice, new StatusDraft { Text = "friends only quoting" }, token)).Post;
|
||||
var stranger = new RemoteActor(_harness.Peer, "stranger");
|
||||
|
||||
await _harness.Deliver(stranger, "/human-centipede", Request(stranger, original.ObjectURI, NewId(stranger, "notes")));
|
||||
|
||||
Assert.Equal("Reject", Assert.Single(await _harness.Outgoing(stranger.Id + "/inbox"))["type"]!.GetValue<string>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task One_persona_quotes_another_with_a_licence()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var (_, bob) = await _harness.Persona("bob");
|
||||
var original = (await _harness.Statuses.Publish(alice, new StatusDraft { Text = "public thought" }, token)).Post;
|
||||
|
||||
var outcome = await _harness.Statuses.Publish(bob, new StatusDraft { Text = "agreed", QuotedStatusId = original.ID }, token);
|
||||
|
||||
Assert.True(outcome.Ok);
|
||||
Assert.Equal(QuoteState.Accepted, outcome.Post.QuoteState);
|
||||
Assert.StartsWith(alice.Uri + "/parrot-licences/", outcome.Post.QuoteAuthorizationURI);
|
||||
Assert.Equal(1, (await DB.Default.Find<Post>().OneAsync(original.ID, token)).QuotesCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.ClientModels.Post;
|
||||
using PrivaPub.Domain.Social;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Models.Social;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Federation
|
||||
{
|
||||
public class ReactionNamesTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(":blobcat@k.example:", "blobcat")]
|
||||
[InlineData("👍🏽", "👍🏽")]
|
||||
[InlineData("❤️", "❤️")]
|
||||
[InlineData("nope", null)]
|
||||
[InlineData("👍👍", null)]
|
||||
[InlineData(":unknown:", null)]
|
||||
public void Normalises_what_peers_send(string content, string expected) =>
|
||||
Assert.Equal(expected, Reactions.Normalise(content, new List<CustomEmoji> { new() { Shortcode = "blobcat", URL = "https://k.example/e.png" } })?.Name);
|
||||
}
|
||||
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class ReactionTests : IAsyncLifetime
|
||||
{
|
||||
Harness _harness;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
_harness = await Harness.Start();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_harness != default)
|
||||
await _harness.DisposeAsync();
|
||||
}
|
||||
|
||||
static string Origin(RemoteActor actor) => new Uri(actor.Id).GetLeftPart(UriPartial.Authority);
|
||||
|
||||
static string NewId(RemoteActor actor, string kind) => $"{Origin(actor)}/{kind}/{Guid.NewGuid():N}";
|
||||
|
||||
async Task<Post> LocalPost()
|
||||
{
|
||||
var (root, alice) = await _harness.Persona("alice");
|
||||
await _harness.Posts.InsertPost(root, new InsertPostForm { AvatarId = alice.Id, Text = "react to me" }, TestContext.Current.CancellationToken);
|
||||
return await DB.Default.Find<Post>().Match(p => p.GroupUserId == alice.Id).ExecuteFirstAsync(TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Misskey_and_pleroma_reactions_are_kept_apart_from_favourites()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var post = await LocalPost();
|
||||
var misskey = new RemoteActor(_harness.Peer, "kitty");
|
||||
var pleroma = new RemoteActor(_harness.Peer, "pleroma");
|
||||
|
||||
await _harness.Deliver(misskey, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = NewId(misskey, "likes"), ["type"] = "Like", ["actor"] = misskey.Id, ["object"] = post.ObjectURI, ["content"] = ":blobcat:",
|
||||
["_misskey_reaction"] = ":blobcat:",
|
||||
["tag"] = new JsonArray(new JsonObject { ["type"] = "Emoji", ["name"] = ":blobcat:", ["icon"] = new JsonObject { ["type"] = "Image", ["url"] = $"{Origin(misskey)}/emoji/blobcat.png" } })
|
||||
});
|
||||
await _harness.Deliver(misskey, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = NewId(misskey, "likes"), ["type"] = "Like", ["actor"] = misskey.Id, ["object"] = post.ObjectURI, ["content"] = "❤"
|
||||
});
|
||||
var fire = new JsonObject { ["id"] = NewId(pleroma, "activities"), ["type"] = "EmojiReact", ["actor"] = pleroma.Id, ["object"] = post.ObjectURI, ["content"] = "🔥" };
|
||||
await _harness.Deliver(pleroma, "/human-centipede", fire);
|
||||
|
||||
var kept = await DB.Default.Find<Reaction>().Match(r => r.PostId == post.ID).ExecuteAsync(token);
|
||||
Assert.Equal(new HashSet<string> { "blobcat", "🔥" }, kept.Select(r => r.Emoji).ToHashSet());
|
||||
Assert.Equal($"{Origin(misskey)}/emoji/blobcat.png", kept.Single(r => r.Emoji == "blobcat").EmojiURL);
|
||||
Assert.Equal(1, (await DB.Default.Find<Post>().OneAsync(post.ID, token)).FavouritesCount);
|
||||
Assert.True(await DB.Default.Find<Notification>().Match(n => n.Type == NotificationType.Reaction && n.Emoji == "🔥").ExecuteAnyAsync(token));
|
||||
|
||||
await _harness.Deliver(pleroma, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = NewId(pleroma, "activities"), ["type"] = "Undo", ["actor"] = pleroma.Id, ["object"] = fire.DeepClone()
|
||||
});
|
||||
Assert.Equal("blobcat", Assert.Single(await DB.Default.Find<Reaction>().Match(r => r.PostId == post.ID).ExecuteAsync(token)).Emoji);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_persona_reacts_to_a_remote_post_and_takes_it_back()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var mallory = new RemoteActor(_harness.Peer, "mallory");
|
||||
var noteId = NewId(mallory, "notes");
|
||||
await _harness.Deliver(mallory, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = NewId(mallory, "activities"), ["type"] = "Create", ["actor"] = mallory.Id,
|
||||
["object"] = new JsonObject
|
||||
{
|
||||
["id"] = noteId, ["type"] = "Note", ["attributedTo"] = mallory.Id, ["content"] = "<p>hi</p>",
|
||||
["to"] = new JsonArray(Addressing.Public), ["cc"] = new JsonArray(alice.Uri),
|
||||
["tag"] = new JsonArray(new JsonObject { ["type"] = "Mention", ["href"] = alice.Uri, ["name"] = "@alice" })
|
||||
}
|
||||
});
|
||||
var post = await DB.Default.Find<Post>().Match(p => p.ObjectURI == noteId).ExecuteFirstAsync(token);
|
||||
|
||||
Assert.True(await _harness.Reactions.React(alice, post, "🎉", token));
|
||||
Assert.False(await _harness.Reactions.React(alice, post, "not an emoji", token));
|
||||
Assert.True(await _harness.Reactions.Unreact(alice, post, "🎉", token));
|
||||
|
||||
var sent = await _harness.Outgoing(mallory.Id + "/inbox");
|
||||
var react = Assert.Single(sent, a => a["type"]!.GetValue<string>() == "EmojiReact");
|
||||
Assert.Equal("🎉", react["content"]!.GetValue<string>());
|
||||
Assert.Equal(noteId, react["object"]!.GetValue<string>());
|
||||
Assert.Equal(react["id"]!.GetValue<string>(), Assert.Single(sent, a => a["type"]!.GetValue<string>() == "Undo")["object"]!["id"]!.GetValue<string>());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,16 @@ namespace PrivaPub.Tests.Federation
|
||||
await _peer.DisposeAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_stored_actor_remembers_the_extensions_its_document_uses()
|
||||
{
|
||||
var bob = new RemoteActor(_peer, "bob");
|
||||
|
||||
var stored = await _service.GetActor(bob.Id, refresh: true, TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Contains("key:rsa-2048", stored.Features);
|
||||
}
|
||||
|
||||
static string Actor(string id, string keyId = default, string owner = default, string pem = default, string type = "Person") =>
|
||||
new JsonObject
|
||||
{
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Federation.Inbox;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
using PrivaPub.Models;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Tests.Support;
|
||||
using PrivaPub.Tests.Support.Host;
|
||||
|
||||
using System.Net;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Http
|
||||
{
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class HostBootTests : IAsyncLifetime
|
||||
{
|
||||
PrivaPubHost _host;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
_host = await PrivaPubHost.Shared();
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
||||
|
||||
[Fact]
|
||||
public async Task The_host_shares_the_fixture_database()
|
||||
{
|
||||
Assert.True(await DB.Default.Find<AppConfiguration>().ExecuteAnyAsync(TestContext.Current.CancellationToken));
|
||||
using var client = _host.Client();
|
||||
Assert.Equal(HttpStatusCode.OK, (await client.GetAsync("/build.json", TestContext.Current.CancellationToken)).StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task The_harness_handles_the_same_activities_as_the_server()
|
||||
{
|
||||
await using var harness = await Harness.Start();
|
||||
var registered = _host.Services.GetServices<IActivityHandler>().Select(h => h.GetType()).OrderBy(t => t.Name);
|
||||
var harnessed = harness.Handlers.Select(h => h.GetType()).OrderBy(t => t.Name);
|
||||
|
||||
Assert.Equal(registered, harnessed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Every_job_kind_has_exactly_one_handler()
|
||||
{
|
||||
var handlers = _host.Services.GetServices<IJobHandler>().GroupBy(h => h.Kind).ToDictionary(g => g.Key, g => g.Count());
|
||||
|
||||
foreach (var kind in Enum.GetValues<JobKind>())
|
||||
Assert.True(handlers.GetValueOrDefault(kind) == 1, $"{kind} has {handlers.GetValueOrDefault(kind)} handlers");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Every_controller_and_page_model_can_be_made()
|
||||
{
|
||||
using var scope = _host.Services.CreateScope();
|
||||
var controllers = _host.Get<IActionDescriptorCollectionProvider>().ActionDescriptors.Items
|
||||
.OfType<ControllerActionDescriptor>()
|
||||
.Select(d => d.ControllerTypeInfo.AsType())
|
||||
.Distinct()
|
||||
.ToList();
|
||||
var pages = typeof(Program).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(PageModel)) && !t.IsAbstract).ToList();
|
||||
|
||||
Assert.NotEmpty(controllers);
|
||||
Assert.NotEmpty(pages);
|
||||
foreach (var type in controllers.Concat(pages))
|
||||
Assert.NotNull(ActivatorUtilities.CreateInstance(scope.ServiceProvider, type));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_service_graph_is_valid()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
foreach (var descriptor in _host.Registered)
|
||||
((ICollection<ServiceDescriptor>)services).Add(descriptor);
|
||||
|
||||
using var provider = services.BuildServiceProvider(new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Swagger_is_not_served_outside_development()
|
||||
{
|
||||
using var client = _host.Client();
|
||||
|
||||
Assert.Equal(HttpStatusCode.NotFound, (await client.GetAsync("/swagger/index.html", TestContext.Current.CancellationToken)).StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_persona_gets_a_mastodon_token_through_the_real_code_flow()
|
||||
{
|
||||
var root = await _host.SignUp();
|
||||
var persona = await _host.Persona(root, "boot");
|
||||
|
||||
var token = await _host.MastodonToken(persona);
|
||||
|
||||
using var client = _host.As(token);
|
||||
var account = await client.GetStringAsync("/api/v1/accounts/verify_credentials", TestContext.Current.CancellationToken);
|
||||
Assert.Contains($"\"id\":\"{persona.Id}\"", account);
|
||||
Assert.DoesNotContain(root.Id, account);
|
||||
Assert.DoesNotContain(root.UserName, account);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_signed_delivery_through_the_real_route_is_queued_and_stored()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
await using var peer = await Peer.Start();
|
||||
var persona = await _host.Persona(await _host.SignUp(), "mouth");
|
||||
var bob = new RemoteActor(peer, "bob");
|
||||
var noteId = $"{peer.A}/notes/{Guid.NewGuid():N}";
|
||||
var activity = new JsonObject
|
||||
{
|
||||
["id"] = noteId + "/activity",
|
||||
["type"] = "Create",
|
||||
["actor"] = bob.Id,
|
||||
["to"] = new JsonArray($"{PrivaPubHost.Base}/peasants/{persona.UserName}"),
|
||||
["object"] = new JsonObject
|
||||
{
|
||||
["id"] = noteId,
|
||||
["type"] = "Note",
|
||||
["attributedTo"] = bob.Id,
|
||||
["to"] = new JsonArray($"{PrivaPubHost.Base}/peasants/{persona.UserName}"),
|
||||
["content"] = "<p>hello through the front door</p>",
|
||||
["published"] = DateTime.UtcNow.ToString("O")
|
||||
}
|
||||
};
|
||||
using var client = _host.Client();
|
||||
|
||||
var response = await client.SendAsync(bob.SignedPost($"/peasants/{persona.UserName}/mouth", activity), token);
|
||||
|
||||
Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
|
||||
Assert.Equal(1, await _host.RunInbox(noteId + "/activity", token));
|
||||
var stored = await DB.Default.Find<Post>().Match(p => p.ObjectURI == noteId).ExecuteFirstAsync(token);
|
||||
Assert.Equal(PostVisibility.Direct, stored.Visibility);
|
||||
Assert.Contains("front door", stored.ContentHtml);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.AspNetCore.Routing.Patterns;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
using PrivaPub.Tests.Support;
|
||||
using PrivaPub.Tests.Support.Host;
|
||||
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Http
|
||||
{
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class NeverFiveHundredTests : IAsyncLifetime
|
||||
{
|
||||
static readonly string[] Junk = { "x", "000000000000000000000000", "%2e%2e", new string('a', 5000), "5f0c1a2b3c4d5e6f70819203" };
|
||||
|
||||
PrivaPubHost _host;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
_host = await PrivaPubHost.Shared();
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
||||
|
||||
public static TheoryData<string> Credentials() => new() { "anonymous", "persona", "junk" };
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Credentials))]
|
||||
public async Task No_route_answers_500_to_junk(string credentials)
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var bearer = credentials switch
|
||||
{
|
||||
"persona" => await _host.MastodonToken(await _host.Persona(await _host.SignUp(), "junk")),
|
||||
"junk" => "not-a-token",
|
||||
_ => default
|
||||
};
|
||||
var jwt = credentials == "persona" ? (await _host.SignUp()).Jwt : bearer;
|
||||
var failures = new List<string>();
|
||||
|
||||
foreach (var (method, template) in Routes())
|
||||
foreach (var value in Junk)
|
||||
{
|
||||
var path = Fill(template, value);
|
||||
using var request = new HttpRequestMessage(new HttpMethod(method), path);
|
||||
request.Headers.Add(PrivaPubHost.ClientHeader, $"10.9.{Random.Shared.Next(256)}.{Random.Shared.Next(1, 255)}");
|
||||
var auth = path.StartsWith("/api/", StringComparison.Ordinal) || path.StartsWith("/oauth/", StringComparison.Ordinal) ? bearer : jwt;
|
||||
if (auth != default)
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", auth);
|
||||
if (method is "POST" or "PUT" or "PATCH")
|
||||
request.Content = new StringContent("{}", Encoding.UTF8, "application/json");
|
||||
using var client = _host.Client();
|
||||
HttpResponseMessage response;
|
||||
try
|
||||
{
|
||||
response = await client.SendAsync(request, token);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
failures.Add($"{method} {Short(path)} threw {ex.GetType().Name}: {ex.Message}");
|
||||
continue;
|
||||
}
|
||||
using (response)
|
||||
{
|
||||
if ((int)response.StatusCode >= 500)
|
||||
failures.Add($"{method} {Short(path)} answered {(int)response.StatusCode}");
|
||||
else if (path.StartsWith("/api/", StringComparison.Ordinal) && (int)response.StatusCode >= 400 && !await IsMastodonError(response))
|
||||
failures.Add($"{method} {Short(path)} answered {(int)response.StatusCode} without a Mastodon error body");
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(failures.Count == 0, $"{failures.Count} problems as {credentials}:\n" + string.Join("\n", failures.Distinct().Take(80)));
|
||||
}
|
||||
|
||||
IEnumerable<(string Method, string Template)> Routes()
|
||||
{
|
||||
var endpoints = _host.Services.GetRequiredService<EndpointDataSource>().Endpoints.OfType<RouteEndpoint>();
|
||||
foreach (var endpoint in endpoints)
|
||||
{
|
||||
var methods = endpoint.Metadata.GetMetadata<IHttpMethodMetadata>()?.HttpMethods;
|
||||
var template = "/" + endpoint.RoutePattern.RawText?.TrimStart('/');
|
||||
foreach (var method in methods is { Count: > 0 } ? methods : new[] { "GET", "POST" })
|
||||
yield return (method, template);
|
||||
}
|
||||
}
|
||||
|
||||
static string Fill(string template, string value)
|
||||
{
|
||||
var pattern = RoutePatternFactory.Parse(template);
|
||||
var builder = new StringBuilder();
|
||||
foreach (var segment in pattern.PathSegments)
|
||||
{
|
||||
builder.Append('/');
|
||||
foreach (var part in segment.Parts)
|
||||
builder.Append(part switch
|
||||
{
|
||||
RoutePatternLiteralPart literal => literal.Content,
|
||||
RoutePatternParameterPart => value,
|
||||
RoutePatternSeparatorPart separator => separator.Content,
|
||||
_ => string.Empty
|
||||
});
|
||||
}
|
||||
return builder.Length == 0 ? "/" : builder.ToString();
|
||||
}
|
||||
|
||||
static async Task<bool> IsMastodonError(HttpResponseMessage response)
|
||||
{
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
try
|
||||
{
|
||||
return JsonNode.Parse(body) is JsonObject error && error["error"] is JsonValue;
|
||||
}
|
||||
catch (System.Text.Json.JsonException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static string Short(string path) => path.Length > 120 ? path[..120] + "…" : path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Outbox;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
using PrivaPub.Models;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Models.User;
|
||||
using PrivaPub.StaticServices;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Infrastructure
|
||||
{
|
||||
[Xunit.Collection(nameof(Exclusive))]
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class DeliveryWorkerTests : IAsyncLifetime
|
||||
{
|
||||
static readonly string[] PeerHosts = { "localhost", "127.0.0.1" };
|
||||
|
||||
Peer _peer;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
_peer = await Peer.Start();
|
||||
await DB.Default.DeleteAsync<RemoteInstance>(i => PeerHosts.Contains(i.Host));
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_peer == default)
|
||||
return;
|
||||
await _peer.DisposeAsync();
|
||||
await DB.Default.DeleteAsync<RemoteInstance>(i => PeerHosts.Contains(i.Host));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_dead_host_does_not_hold_up_deliveries_to_live_ones()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (privateKey, publicKey) = Keys.NewKeyPair();
|
||||
var avatar = new Avatar { UserName = $"sender{Guid.NewGuid():N}"[..20], PrivateKey = privateKey, PublicKey = publicKey };
|
||||
await DB.Default.SaveAsync(avatar, token);
|
||||
var local = new LocalActorService(new DbEntities(), new StaticOptions<AppConfiguration>(new AppConfiguration { BackendBaseAddress = "https://privapub.test" }));
|
||||
var sender = local.FromAvatar(avatar);
|
||||
var run = $"https://privapub.test/a/{Guid.NewGuid():N}/";
|
||||
var queue = new JobQueue(j => j.DedupeKey.StartsWith(run));
|
||||
var delivery = new DeliveryService(new DbEntities(), queue);
|
||||
_peer.Answer("/dead/inbox", 503, TimeSpan.FromMilliseconds(300));
|
||||
_peer.Answer("/live/inbox", 202);
|
||||
|
||||
var deadInboxes = Enumerable.Range(0, 60).Select(i => $"{_peer.B}/dead/inbox?{i}");
|
||||
await delivery.Enqueue(sender, deadInboxes, new JsonObject { ["id"] = run + "dead", ["type"] = "Create" }, token);
|
||||
await delivery.Enqueue(sender, new[] { $"{_peer.A}/live/inbox" }, new JsonObject { ["id"] = run + "live", ["type"] = "Create" }, token);
|
||||
|
||||
var handler = new DeliveryJobHandler(local, Peer.Http(), new HostCircuitBreaker(new MemoryCache(new MemoryCacheOptions())), NullLogger<DeliveryJobHandler>.Instance);
|
||||
using var worker = new JobWorker(queue, new IJobHandler[] { handler }, NullLogger<JobWorker>.Instance);
|
||||
await worker.StartAsync(token);
|
||||
try
|
||||
{
|
||||
await Until(() => Task.FromResult(_peer.Requests.Any(r => r.Path == "/live/inbox")), token);
|
||||
var deadSoFar = _peer.Requests.Count(r => r.Path == "/dead/inbox");
|
||||
var live = Assert.Single(_peer.Requests, r => r.Path == "/live/inbox");
|
||||
Assert.Contains($"keyId=\"{sender.KeyId}\"", live.Signature);
|
||||
Assert.True(deadSoFar < 30, $"{deadSoFar} dead deliveries were tried before the live one");
|
||||
|
||||
await Until(() => DB.Default.Find<Job>().Match(j => j.DedupeKey.StartsWith(run + "dead") && j.State == JobState.Pending && j.Attempts > 0).ExecuteAnyAsync(token), token);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await worker.StopAsync(CancellationToken.None);
|
||||
}
|
||||
|
||||
var retried = await DB.Default.Find<Job>().Match(j => j.DedupeKey.StartsWith(run + "dead") && j.Attempts > 0 && j.State == JobState.Pending).ExecuteFirstAsync(token);
|
||||
Assert.Equal("503", retried.LastError?.Split(' ')[0]);
|
||||
Assert.True(retried.RunAt > DateTime.UtcNow);
|
||||
Assert.True((await DB.Default.Find<RemoteInstance>().Match(i => i.Host == "localhost").ExecuteFirstAsync(token))?.ConsecutiveFailures > 0);
|
||||
}
|
||||
|
||||
static async Task Until(Func<Task<bool>> condition, CancellationToken token)
|
||||
{
|
||||
var deadline = DateTime.UtcNow.AddSeconds(30);
|
||||
while (!await condition())
|
||||
{
|
||||
Assert.True(DateTime.UtcNow < deadline, "timed out");
|
||||
await Task.Delay(50, token);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
namespace PrivaPub.Tests.Infrastructure
|
||||
{
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class HostCircuitBreakerTests : IAsyncLifetime
|
||||
{
|
||||
readonly string _host = $"breaker{Guid.NewGuid():N}.example";
|
||||
|
||||
public ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (MongoFixture.Enabled)
|
||||
await DB.Default.DeleteAsync<RemoteInstance>(i => i.Host == _host);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_host_is_quarantined_only_past_the_threshold_and_freed_by_a_success()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var breaker = new HostCircuitBreaker(new MemoryCache(new MemoryCacheOptions()));
|
||||
|
||||
for (var i = 1; i < HostCircuitBreaker.Threshold; i++)
|
||||
await breaker.Failed(_host, "503", token);
|
||||
Assert.Null(await breaker.UnavailableUntil(_host, token));
|
||||
|
||||
await breaker.Failed(_host, "503", token);
|
||||
var until = await breaker.UnavailableUntil(_host, token);
|
||||
Assert.NotNull(until);
|
||||
Assert.InRange(until.Value, DateTime.UtcNow.AddMinutes(55), DateTime.UtcNow.AddMinutes(65));
|
||||
var instance = await DB.Default.Find<RemoteInstance>().Match(i => i.Host == _host).ExecuteFirstAsync(token);
|
||||
Assert.Equal(HostCircuitBreaker.Threshold, instance.ConsecutiveFailures);
|
||||
Assert.Equal("503", instance.LastError);
|
||||
|
||||
await breaker.Succeeded(_host, token);
|
||||
Assert.Null(await breaker.UnavailableUntil(_host, token));
|
||||
instance = await DB.Default.Find<RemoteInstance>().Match(i => i.Host == _host).ExecuteFirstAsync(token);
|
||||
Assert.Equal(0, instance.ConsecutiveFailures);
|
||||
Assert.NotNull(instance.LastSuccessAt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_healthy_host_is_written_at_most_once_an_hour()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var breaker = new HostCircuitBreaker(new MemoryCache(new MemoryCacheOptions()));
|
||||
|
||||
await breaker.Succeeded(_host, token);
|
||||
var first = (await DB.Default.Find<RemoteInstance>().Match(i => i.Host == _host).ExecuteFirstAsync(token)).LastSuccessAt;
|
||||
await breaker.Succeeded(_host, token);
|
||||
var second = (await DB.Default.Find<RemoteInstance>().Match(i => i.Host == _host).ExecuteFirstAsync(token)).LastSuccessAt;
|
||||
|
||||
Assert.Equal(first, second);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ using PrivaPub.Tests.Support;
|
||||
|
||||
namespace PrivaPub.Tests.Infrastructure
|
||||
{
|
||||
[Xunit.Collection(nameof(Exclusive))]
|
||||
[Trait("Category", "Integration")]
|
||||
public class IndexTests
|
||||
{
|
||||
|
||||
@@ -1,19 +1,9 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Outbox;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
using PrivaPub.Models;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Models.User;
|
||||
using PrivaPub.StaticServices;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Infrastructure
|
||||
{
|
||||
public class BackoffTests
|
||||
@@ -38,19 +28,13 @@ namespace PrivaPub.Tests.Infrastructure
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class JobQueueTests : IAsyncLifetime
|
||||
{
|
||||
Peer _peer;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
public ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
_peer = await Peer.Start();
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_peer != default)
|
||||
await _peer.DisposeAsync();
|
||||
}
|
||||
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
||||
|
||||
static Job NewJob(string host = default, string dedupe = default) => new()
|
||||
{
|
||||
@@ -102,44 +86,9 @@ namespace PrivaPub.Tests.Infrastructure
|
||||
var job = new Job { Kind = (JobKind)98, State = JobState.Running, LeasedUntil = DateTime.UtcNow.AddMinutes(-1) };
|
||||
await DB.Default.SaveAsync(job, token);
|
||||
|
||||
await new JobQueue().Reap(token);
|
||||
await new JobQueue(j => j.ID == job.ID).Reap(token);
|
||||
|
||||
Assert.Equal(JobState.Pending, (await DB.Default.Find<Job>().OneAsync(job.ID, token)).State);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_dead_host_does_not_hold_up_deliveries_to_live_ones()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var (privateKey, publicKey) = Keys.NewKeyPair();
|
||||
var avatar = new Avatar { UserName = $"sender{Guid.NewGuid():N}"[..20], PrivateKey = privateKey, PublicKey = publicKey };
|
||||
await DB.Default.SaveAsync(avatar, token);
|
||||
var local = new LocalActorService(new DbEntities(), new StaticOptions<AppConfiguration>(new AppConfiguration { BackendBaseAddress = "https://privapub.test" }));
|
||||
var sender = local.FromAvatar(avatar);
|
||||
var queue = new JobQueue();
|
||||
var delivery = new DeliveryService(new DbEntities(), queue);
|
||||
_peer.Answer("/dead/inbox", 503, TimeSpan.FromMilliseconds(300));
|
||||
_peer.Answer("/live/inbox", 202);
|
||||
|
||||
var deadInboxes = Enumerable.Range(0, 60).Select(i => $"{_peer.B}/dead/inbox?{i}");
|
||||
await delivery.Enqueue(sender, deadInboxes, new JsonObject { ["id"] = $"https://privapub.test/a/{Guid.NewGuid():N}", ["type"] = "Create" }, token);
|
||||
await delivery.Enqueue(sender, new[] { $"{_peer.A}/live/inbox" }, new JsonObject { ["id"] = $"https://privapub.test/a/{Guid.NewGuid():N}", ["type"] = "Create" }, token);
|
||||
|
||||
var handler = new DeliveryJobHandler(local, Peer.Http(), new HostCircuitBreaker(new MemoryCache(new MemoryCacheOptions())), NullLogger<DeliveryJobHandler>.Instance);
|
||||
using var worker = new JobWorker(queue, new IJobHandler[] { handler }, NullLogger<JobWorker>.Instance);
|
||||
await worker.StartAsync(token);
|
||||
var deadline = DateTime.UtcNow.AddSeconds(5);
|
||||
while (DateTime.UtcNow < deadline && !_peer.Requests.Any(r => r.Path == "/live/inbox"))
|
||||
await Task.Delay(50, token);
|
||||
var live = Assert.Single(_peer.Requests, r => r.Path == "/live/inbox");
|
||||
Assert.True(_peer.Requests.Count(r => r.Path == "/dead/inbox") < 30);
|
||||
while (DateTime.UtcNow < deadline && !await DB.Default.Find<RemoteInstance>().Match(i => i.Host == "localhost").ExecuteAnyAsync(token))
|
||||
await Task.Delay(50, token);
|
||||
await worker.StopAsync(token);
|
||||
|
||||
Assert.Contains($"keyId=\"{sender.KeyId}\"", live.Signature);
|
||||
var instance = await DB.Default.Find<RemoteInstance>().Match(i => i.Host == "localhost").ExecuteFirstAsync(token);
|
||||
Assert.True(instance.ConsecutiveFailures > 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Infrastructure.Data.Migrations;
|
||||
using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
namespace PrivaPub.Tests.Infrastructure
|
||||
{
|
||||
[Xunit.Collection(nameof(Exclusive))]
|
||||
[Trait("Category", "Integration")]
|
||||
public class MigrationTests
|
||||
{
|
||||
@@ -45,5 +47,37 @@ namespace PrivaPub.Tests.Infrastructure
|
||||
Assert.Equal("conversation1", post.ConversationId);
|
||||
Assert.Equal("a1", post.AuthorAccountId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Fetched_records_lose_the_signature_they_never_had_and_every_record_gets_its_extensions()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var fetched = new ObjectRecord
|
||||
{
|
||||
ObjectURI = $"https://r.example/{Guid.NewGuid():N}", Path = ObjectPath.Fetched, KeyId = "https://x.example/u#k", SignatureScheme = "draft-cavage",
|
||||
Algorithm = "rsa-sha256", SignedHeaders = new() { "host" }, ActivityContext = "\"https://www.w3.org/ns/activitystreams\"",
|
||||
Raw = "{\"type\":\"Note\",\"quoteUrl\":\"https://q.example/1\"}"
|
||||
};
|
||||
var delivered = new ObjectRecord
|
||||
{
|
||||
ObjectURI = $"https://r.example/{Guid.NewGuid():N}", Path = ObjectPath.Delivered, KeyId = "https://x.example/u#k", SignatureScheme = "draft-cavage",
|
||||
Raw = "not json"
|
||||
};
|
||||
await DB.Default.SaveAsync(new[] { fetched, delivered }, token);
|
||||
|
||||
await new _008_fetched_records_lose_the_trigger_signature().UpgradeAsync();
|
||||
await new _009_object_records_keep_their_extensions().UpgradeAsync();
|
||||
|
||||
var afterFetched = await DB.Default.Find<ObjectRecord>().OneAsync(fetched.ID, token);
|
||||
var afterDelivered = await DB.Default.Find<ObjectRecord>().OneAsync(delivered.ID, token);
|
||||
Assert.Null(afterFetched.KeyId);
|
||||
Assert.Null(afterFetched.SignatureScheme);
|
||||
Assert.Empty(afterFetched.SignedHeaders);
|
||||
Assert.Null(afterFetched.ActivityContext);
|
||||
Assert.Equal("https://x.example/u#k", afterDelivered.KeyId);
|
||||
Assert.Contains("legacy-quote", afterFetched.Extensions);
|
||||
Assert.Empty(afterDelivered.Extensions);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
|
||||
<PackageReference Include="xunit.v3" Version="3.2.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Outbox;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Models.Statistics;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Statistics
|
||||
{
|
||||
[Xunit.Collection(nameof(Exclusive))]
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class DeliveryAttemptTests : IAsyncLifetime
|
||||
{
|
||||
static readonly string[] PeerHosts = { "localhost", "127.0.0.1" };
|
||||
|
||||
Harness _harness;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
_harness = await Harness.Start();
|
||||
await DB.Default.DeleteAsync<RemoteInstance>(i => PeerHosts.Contains(i.Host));
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_harness == default)
|
||||
return;
|
||||
await _harness.DisposeAsync();
|
||||
await DB.Default.DeleteAsync<RemoteInstance>(i => PeerHosts.Contains(i.Host));
|
||||
}
|
||||
|
||||
async Task<(JobOutcome Outcome, InteractionEvent Event)> Attempt(string path, JsonObject activity, LocalActor signer = default)
|
||||
{
|
||||
var ledger = new MemoryLedger();
|
||||
var handler = new DeliveryJobHandler(_harness.Local, Peer.Http(), new HostCircuitBreaker(new MemoryCache(new MemoryCacheOptions())),
|
||||
NullLogger<DeliveryJobHandler>.Instance, ledger);
|
||||
signer ??= (await _harness.Persona("sender")).Avatar;
|
||||
var id = activity["id"]!.GetValue<string>();
|
||||
await _harness.Delivery.Enqueue(signer, new[] { _harness.Peer.A + path }, activity, TestContext.Current.CancellationToken);
|
||||
var job = await DB.Default.Find<Job>().Match(j => j.DedupeKey == $"{id}|{_harness.Peer.A + path}").ExecuteFirstAsync(TestContext.Current.CancellationToken);
|
||||
job.Attempts = 1;
|
||||
|
||||
var outcome = await handler.Handle(job, TestContext.Current.CancellationToken);
|
||||
|
||||
return (outcome, Assert.Single(ledger.Of("out")));
|
||||
}
|
||||
|
||||
static JsonObject Create(string audience) => new()
|
||||
{
|
||||
["id"] = $"https://privapub.test/a/{Guid.NewGuid():N}",
|
||||
["type"] = "Create",
|
||||
["to"] = new JsonArray(audience),
|
||||
["object"] = new JsonObject { ["type"] = "Note", ["content"] = "hi" }
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public async Task A_delivered_public_post_is_ok_with_its_shape_size_and_time()
|
||||
{
|
||||
_harness.Peer.Answer("/ok/inbox", 202);
|
||||
|
||||
var (outcome, e) = await Attempt("/ok/inbox", Create("https://www.w3.org/ns/activitystreams#Public"));
|
||||
|
||||
Assert.Equal(JobResult.Done, outcome.Result);
|
||||
Assert.Equal(("ok", "Create", "Note", "public", "person"), (e.Outcome, e.Activity, e.Object, e.Audience, e.LocalKind));
|
||||
Assert.Equal(202, e.Status);
|
||||
Assert.Equal("127.0.0.1", e.Host);
|
||||
Assert.Null(e.Reason);
|
||||
Assert.True(e.Bytes > 0);
|
||||
Assert.NotNull(e.LatencyMs);
|
||||
Assert.Equal(1, e.Attempt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Refusals_and_failures_carry_their_status_and_private_traffic_has_no_kind()
|
||||
{
|
||||
_harness.Peer.Answer("/gone/inbox", 410);
|
||||
_harness.Peer.Answer("/busy/inbox", 429);
|
||||
_harness.Peer.Answer("/broken/inbox", 500);
|
||||
|
||||
var (_, gone) = await Attempt("/gone/inbox", Create("https://elsewhere.example/users/x"));
|
||||
var (_, busy) = await Attempt("/busy/inbox", Create("https://elsewhere.example/users/x"));
|
||||
var (_, broken) = await Attempt("/broken/inbox", Create("https://elsewhere.example/users/x"));
|
||||
|
||||
Assert.Equal(("dead", "410", 410), (gone.Outcome, gone.Reason, gone.Status!.Value));
|
||||
Assert.Equal(("retry", "429"), (busy.Outcome, busy.Reason));
|
||||
Assert.Equal(("retry", "500"), (broken.Outcome, broken.Reason));
|
||||
Assert.All(new[] { gone, busy, broken }, e => Assert.Equal(("private", (string)null), (e.Audience, e.LocalKind)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_signer_that_no_longer_exists_is_dead()
|
||||
{
|
||||
var ghost = new LocalActor { Id = "000000000000000000000000", Kind = LocalActorKind.Person, UserName = "ghost", BaseAddress = Harness.Base };
|
||||
|
||||
var (outcome, e) = await Attempt("/never/inbox", Create("https://www.w3.org/ns/activitystreams#Public"), ghost);
|
||||
|
||||
Assert.Equal(JobResult.Dead, outcome.Result);
|
||||
Assert.Equal(("dead", "signer-gone"), (e.Outcome, e.Reason));
|
||||
Assert.Null(e.Status);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Inbox;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.Group;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Models.Statistics;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
using GroupEntity = PrivaPub.Models.Group.Group;
|
||||
|
||||
namespace PrivaPub.Tests.Statistics
|
||||
{
|
||||
public class ArrivalVerdictTests
|
||||
{
|
||||
[Fact]
|
||||
public void The_first_decision_wins_and_a_circle_has_no_kind()
|
||||
{
|
||||
var verdict = new ArrivalVerdict();
|
||||
|
||||
verdict.Drop("not-addressed");
|
||||
verdict.Accept("stored");
|
||||
verdict.About("Note", PostVisibility.Circle, DateTime.UtcNow.AddMinutes(-2), new LocalActor { Kind = LocalActorKind.Group, IsCircle = true });
|
||||
|
||||
Assert.Equal(("dropped", "not-addressed"), (verdict.Outcome, verdict.Reason));
|
||||
Assert.Equal("private", verdict.Audience);
|
||||
Assert.Null(verdict.LocalKind);
|
||||
Assert.InRange(verdict.AgeSeconds!.Value, 110, 130);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(PostVisibility.Public, "public")]
|
||||
[InlineData(PostVisibility.Unlisted, "unlisted")]
|
||||
[InlineData(PostVisibility.FollowersOnly, "private")]
|
||||
[InlineData(PostVisibility.Direct, "private")]
|
||||
[InlineData(PostVisibility.Circle, "private")]
|
||||
[InlineData(PostVisibility.LocalGeo, "private")]
|
||||
public void Every_visibility_but_public_and_unlisted_is_private(PostVisibility visibility, string audience) =>
|
||||
Assert.Equal(audience, ArrivalVerdict.AudienceOf(visibility));
|
||||
|
||||
[Fact]
|
||||
public void Outside_an_arrival_nothing_happens()
|
||||
{
|
||||
Arrival.Current = default;
|
||||
Arrival.Drop("x");
|
||||
Arrival.About("Note");
|
||||
}
|
||||
}
|
||||
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class HandlerVerdictTests : IAsyncLifetime
|
||||
{
|
||||
Harness _harness;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
_harness = await Harness.Start();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_harness != default)
|
||||
await _harness.DisposeAsync();
|
||||
}
|
||||
|
||||
InteractionEvent Processed(string activityType) =>
|
||||
_harness.Ledger.Of("in").Last(e => e.Activity == activityType);
|
||||
|
||||
JsonObject Create(RemoteActor author, IEnumerable<string> to, JsonObject extra = default)
|
||||
{
|
||||
var origin = new Uri(author.Id).GetLeftPart(UriPartial.Authority);
|
||||
var note = new JsonObject
|
||||
{
|
||||
["id"] = $"{origin}/notes/{Guid.NewGuid():N}",
|
||||
["type"] = "Note",
|
||||
["attributedTo"] = author.Id,
|
||||
["content"] = "<p>hello</p>",
|
||||
["to"] = new JsonArray(to.Select(t => (JsonNode)t).ToArray())
|
||||
};
|
||||
foreach (var (key, value) in extra ?? new JsonObject())
|
||||
note[key] = value?.DeepClone();
|
||||
return new JsonObject { ["id"] = $"{origin}/activities/{Guid.NewGuid():N}", ["type"] = "Create", ["actor"] = author.Id, ["object"] = note };
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_direct_message_is_stored_private_and_without_a_local_kind()
|
||||
{
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var bob = new RemoteActor(_harness.Peer, "bob");
|
||||
|
||||
await _harness.Deliver(bob, "/human-centipede", Create(bob, new[] { alice.Uri }));
|
||||
|
||||
var e = Processed("Create");
|
||||
Assert.Equal(("accepted", "stored", "private"), (e.Outcome, e.Reason, e.Audience));
|
||||
Assert.Equal("Note", e.Object);
|
||||
Assert.Null(e.LocalKind);
|
||||
Assert.Equal("127.0.0.1", e.Host);
|
||||
Assert.NotNull(e.Attempt);
|
||||
Assert.NotNull(e.WaitMs);
|
||||
Assert.Equal(bob.Id, _harness.Ledger.Events.Last(x => x.Event == e).ActorUri);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_public_reply_to_a_persona_names_its_kind_and_its_features()
|
||||
{
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var bob = new RemoteActor(_harness.Peer, "bob");
|
||||
var parent = await _harness.Statuses.Publish(alice, new StatusDraft { Text = "first" }, TestContext.Current.CancellationToken);
|
||||
|
||||
await _harness.Deliver(bob, "/human-centipede", Create(bob, new[] { Addressing.Public, alice.Uri },
|
||||
new JsonObject { ["inReplyTo"] = parent.Post.ObjectURI, ["interactionPolicy"] = new JsonObject(), ["quoteUrl"] = "https://elsewhere.example/1" }));
|
||||
|
||||
var e = Processed("Create");
|
||||
Assert.Equal(("accepted", "public", "person"), (e.Outcome, e.Audience, e.LocalKind));
|
||||
Assert.Contains("interaction-policy", e.Features);
|
||||
Assert.Contains("legacy-quote", e.Features);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Unwanted_things_are_dropped_with_a_reason()
|
||||
{
|
||||
var stranger = new RemoteActor(_harness.Peer, "stranger");
|
||||
|
||||
await _harness.Deliver(stranger, "/human-centipede", Create(stranger, new[] { Addressing.Public }));
|
||||
var unaddressed = Processed("Create");
|
||||
await _harness.Deliver(stranger, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = new Uri(stranger.Id).GetLeftPart(UriPartial.Authority) + "/likes/" + Guid.NewGuid().ToString("N"),
|
||||
["type"] = "Like",
|
||||
["actor"] = stranger.Id,
|
||||
["object"] = $"{Harness.Base}/peasants/x/scribbles/{Guid.NewGuid():N}"
|
||||
});
|
||||
var like = Processed("Like");
|
||||
await _harness.Deliver(stranger, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = new Uri(stranger.Id).GetLeftPart(UriPartial.Authority) + "/x/" + Guid.NewGuid().ToString("N"),
|
||||
["type"] = "Bite",
|
||||
["actor"] = stranger.Id,
|
||||
["object"] = stranger.Id
|
||||
});
|
||||
var bite = Processed("Bite");
|
||||
await _harness.Deliver(stranger, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = new Uri(stranger.Id).GetLeftPart(UriPartial.Authority) + "/undo/" + Guid.NewGuid().ToString("N"),
|
||||
["type"] = "Undo",
|
||||
["actor"] = stranger.Id,
|
||||
["object"] = new JsonObject { ["id"] = stranger.Id + "/likes/never", ["type"] = "Like", ["actor"] = stranger.Id, ["object"] = "https://nowhere.example/1" }
|
||||
});
|
||||
var undo = Processed("Undo");
|
||||
|
||||
Assert.Equal(("dropped", "not-addressed", "public"), (unaddressed.Outcome, unaddressed.Reason, unaddressed.Audience));
|
||||
Assert.Equal(("dropped", "unknown-object"), (like.Outcome, like.Reason));
|
||||
Assert.Equal(("dropped", "unknown-type"), (bite.Outcome, bite.Reason));
|
||||
Assert.Equal(("dropped", "unknown-object", "Like"), (undo.Outcome, undo.Reason, undo.Object));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_stranger_posting_into_a_circle_never_reveals_the_circle()
|
||||
{
|
||||
var (_, owner) = await _harness.Persona("owner");
|
||||
var (privateKey, publicKey) = Keys.NewKeyPair();
|
||||
var entity = new GroupEntity
|
||||
{
|
||||
UserName = $"circle{Guid.NewGuid():N}"[..20],
|
||||
Kind = GroupKind.Circle,
|
||||
PostingPolicy = PostingPolicy.Followers,
|
||||
PrivateKey = privateKey,
|
||||
PublicKey = publicKey,
|
||||
Members = new() { new GroupMember { AvatarId = owner.Id, Role = GroupRole.Owner } }
|
||||
};
|
||||
await DB.Default.SaveAsync(entity);
|
||||
var circle = _harness.Local.FromGroup(entity);
|
||||
var outsider = new RemoteActor(_harness.Peer, "outsider");
|
||||
|
||||
await _harness.Deliver(outsider, "/human-centipede", Create(outsider, new[] { circle.Uri, circle.Flock }));
|
||||
|
||||
var e = Processed("Create");
|
||||
Assert.Equal(("dropped", "not-addressed", "private"), (e.Outcome, e.Reason, e.Audience));
|
||||
Assert.Null(e.LocalKind);
|
||||
Assert.DoesNotContain(_harness.Ledger.Events, x => System.Text.Json.JsonSerializer.Serialize(x.Event).Contains("circle", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_follow_is_accepted_and_its_undo_is_recorded()
|
||||
{
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var bob = new RemoteActor(_harness.Peer, "bob");
|
||||
var follow = new JsonObject { ["id"] = bob.Id + "/follows/" + Guid.NewGuid().ToString("N"), ["type"] = "Follow", ["actor"] = bob.Id, ["object"] = alice.Uri };
|
||||
|
||||
await _harness.Deliver(bob, "/human-centipede", follow);
|
||||
var followed = Processed("Follow");
|
||||
await _harness.Deliver(bob, "/human-centipede", new JsonObject
|
||||
{
|
||||
["id"] = bob.Id + "/undo/" + Guid.NewGuid().ToString("N"),
|
||||
["type"] = "Undo",
|
||||
["actor"] = bob.Id,
|
||||
["object"] = follow.DeepClone()
|
||||
});
|
||||
var undone = Processed("Undo");
|
||||
|
||||
Assert.Equal(("accepted", "auto-accepted", "none"), (followed.Outcome, followed.Reason, followed.Audience));
|
||||
Assert.Equal(("accepted", "undone", "Follow"), (undone.Outcome, undone.Reason, undone.Object));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
using PrivaPub.Federation.Inbox;
|
||||
using PrivaPub.Federation.Moderation;
|
||||
using PrivaPub.Federation.Signing;
|
||||
using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.Statistics;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Statistics
|
||||
{
|
||||
public class SignatureProblemCodeTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("unsupported signature algorithm 'ed25519'", "algorithm-unsupported")]
|
||||
[InlineData("(request-target) is not signed", "headers-unsigned")]
|
||||
[InlineData("host is not signed", "headers-unsigned")]
|
||||
[InlineData("the digest is not signed", "headers-unsigned")]
|
||||
[InlineData("neither date nor (created) is signed", "headers-unsigned")]
|
||||
[InlineData("the digest does not match the body", "digest-mismatch")]
|
||||
[InlineData("unreadable Date header", "header-unreadable")]
|
||||
[InlineData("unreadable (created)", "header-unreadable")]
|
||||
[InlineData("unreadable (expires)", "header-unreadable")]
|
||||
[InlineData("the Date header is outside the allowed window", "date-skew")]
|
||||
[InlineData("(created) is outside the allowed window", "date-skew")]
|
||||
[InlineData("the signature has expired", "expired")]
|
||||
[InlineData("something new", "signature-problem")]
|
||||
public void Every_signature_problem_has_a_reason_code(string problem, string code) =>
|
||||
Assert.Equal(code, HttpSignatures.ProblemCode(problem));
|
||||
}
|
||||
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class InboxAnswerTests : IAsyncLifetime
|
||||
{
|
||||
Harness _harness;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
_harness = await Harness.Start();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_harness != default)
|
||||
await _harness.DisposeAsync();
|
||||
}
|
||||
|
||||
JsonObject Create(RemoteActor sender, string to)
|
||||
{
|
||||
var origin = new Uri(sender.Id).GetLeftPart(UriPartial.Authority);
|
||||
var id = $"{origin}/notes/{Guid.NewGuid():N}";
|
||||
return new JsonObject
|
||||
{
|
||||
["id"] = id + "/activity",
|
||||
["type"] = "Create",
|
||||
["actor"] = sender.Id,
|
||||
["to"] = new JsonArray(to),
|
||||
["object"] = new JsonObject { ["id"] = id, ["type"] = "Note", ["attributedTo"] = sender.Id, ["to"] = new JsonArray(to), ["content"] = "hi" }
|
||||
};
|
||||
}
|
||||
|
||||
(InboxResult Result, InteractionEvent Event, string ActorUri, bool Claimed) Last(InboxResult result)
|
||||
{
|
||||
var recorded = _harness.Ledger.Events.Last(e => e.Event.Channel == "recv");
|
||||
return (result, recorded.Event, recorded.ActorUri, recorded.HostClaimed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_verified_delivery_is_queued_once_and_then_a_duplicate()
|
||||
{
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var bob = new RemoteActor(_harness.Peer, "bob");
|
||||
var activity = Create(bob, alice.Uri);
|
||||
|
||||
var first = Last(await _harness.Receiver.Receive(bob.Post(Harness.Host, "/human-centipede", activity), default, CancellationToken.None));
|
||||
var second = Last(await _harness.Receiver.Receive(bob.Post(Harness.Host, "/human-centipede", activity), default, CancellationToken.None));
|
||||
|
||||
Assert.Equal(202, first.Event.Status);
|
||||
Assert.Equal("queued", first.Event.Reason);
|
||||
Assert.Equal("queued", first.Event.Outcome);
|
||||
Assert.Equal("127.0.0.1", first.Event.Host);
|
||||
Assert.Equal("Create", first.Event.Activity);
|
||||
Assert.Equal("Note", first.Event.Object);
|
||||
Assert.Equal("shared", first.Event.Inbox);
|
||||
Assert.Equal("cavage:rsa-sha256", first.Event.Signature);
|
||||
Assert.True(first.Event.Bytes > 0);
|
||||
Assert.Equal(bob.Id, first.ActorUri);
|
||||
Assert.False(first.Claimed);
|
||||
Assert.Equal("duplicate", second.Event.Reason);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Refusals_carry_their_reason_and_an_unverified_host_is_only_claimed()
|
||||
{
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var mallory = new RemoteActor(_harness.Peer, "mallory");
|
||||
|
||||
var unsigned = mallory.Post(Harness.Host, "/human-centipede", Create(mallory, alice.Uri));
|
||||
unsigned.Headers.Remove("Signature");
|
||||
var noSignature = Last(await _harness.Receiver.Receive(unsigned, default, CancellationToken.None));
|
||||
|
||||
var tampered = mallory.Post(Harness.Host, "/human-centipede", Create(mallory, alice.Uri));
|
||||
tampered.Headers["Digest"] = "SHA-256=AAAA";
|
||||
var badDigest = Last(await _harness.Receiver.Receive(tampered, default, CancellationToken.None));
|
||||
|
||||
var forged = mallory.Post(Harness.Host, "/human-centipede", Create(mallory, alice.Uri));
|
||||
forged.Headers["Signature"] = forged.Headers["Signature"].ToString().Replace("signature=\"", "signature=\"AAAA");
|
||||
var badSignature = Last(await _harness.Receiver.Receive(forged, default, CancellationToken.None));
|
||||
|
||||
var junk = Last(await _harness.Deliver(mallory, "/human-centipede", new JsonArray(1, 2)));
|
||||
|
||||
Assert.Equal((401, "no-signature", "none"), (noSignature.Event.Status!.Value, noSignature.Event.Reason, noSignature.Event.Signature));
|
||||
Assert.Equal((401, "digest-mismatch"), (badDigest.Event.Status!.Value, badDigest.Event.Reason));
|
||||
Assert.Equal((401, "signature-invalid"), (badSignature.Event.Status!.Value, badSignature.Event.Reason));
|
||||
Assert.Equal((400, "not-activity"), (junk.Event.Status!.Value, junk.Event.Reason));
|
||||
foreach (var refused in new[] { noSignature, badDigest, badSignature })
|
||||
{
|
||||
Assert.Equal("refused", refused.Event.Outcome);
|
||||
Assert.True(refused.Claimed);
|
||||
Assert.Null(refused.ActorUri);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_follow_of_nobody_is_a_404_and_a_self_delete_with_an_unknown_key_is_a_silent_202()
|
||||
{
|
||||
var stranger = new RemoteActor(_harness.Peer, "stranger");
|
||||
var follow = new JsonObject
|
||||
{
|
||||
["id"] = stranger.Id + "/follows/" + Guid.NewGuid().ToString("N"),
|
||||
["type"] = "Follow",
|
||||
["actor"] = stranger.Id,
|
||||
["object"] = $"{Harness.Base}/peasants/nobody{Guid.NewGuid():N}"
|
||||
};
|
||||
var notFound = Last(await _harness.Receiver.Receive(stranger.Post(Harness.Host, "/human-centipede", follow), default, CancellationToken.None));
|
||||
|
||||
var gone = new RemoteActor(_harness.Peer, "gone");
|
||||
_harness.Peer.Answer(new Uri(gone.Id).AbsolutePath, 410);
|
||||
var delete = new JsonObject { ["id"] = gone.Id + "#delete", ["type"] = "Delete", ["actor"] = gone.Id, ["object"] = gone.Id };
|
||||
var deleted = Last(await _harness.Receiver.Receive(gone.Post(Harness.Host, "/human-centipede", delete), default, CancellationToken.None));
|
||||
|
||||
Assert.Equal((404, "unknown-recipient"), (notFound.Event.Status!.Value, notFound.Event.Reason));
|
||||
Assert.Equal((202, "self-delete-unknown-key"), (deleted.Event.Status!.Value, deleted.Event.Reason));
|
||||
Assert.True(deleted.Claimed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_suspended_server_is_recorded_under_its_own_name()
|
||||
{
|
||||
var blocks = new DomainBlocks(NullLogger<DomainBlocks>.Instance);
|
||||
blocks.Load(new[] { new DomainBlock { Domain = "localhost", Severity = DomainBlockSeverity.Suspend } });
|
||||
var ledger = new MemoryLedger();
|
||||
var receiver = new InboxReceiver(_harness.Local, _harness.Remote, _harness.Queue, blocks, NullLogger<InboxReceiver>.Instance, ledger);
|
||||
var (_, alice) = await _harness.Persona("alice");
|
||||
var spammer = new RemoteActor(_harness.Peer, "spammer", _harness.Peer.B);
|
||||
|
||||
var result = await receiver.Receive(spammer.Post(Harness.Host, "/human-centipede", Create(spammer, alice.Uri)), default, CancellationToken.None);
|
||||
|
||||
Assert.Equal(202, result.StatusCode);
|
||||
var (recorded, actorUri, claimed) = Assert.Single(ledger.Events);
|
||||
Assert.Equal(("localhost", "suspended", "queued"), (recorded.Host, recorded.Reason, recorded.Outcome));
|
||||
Assert.False(claimed);
|
||||
Assert.Null(actorUri);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Models.Statistics;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
namespace PrivaPub.Tests.Statistics
|
||||
{
|
||||
public class InteractionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void The_local_kind_is_kept_only_on_public_and_unlisted_traffic()
|
||||
{
|
||||
Assert.Equal("person", Interactions.Sanitize(new InteractionEvent { Audience = "public", LocalKind = "person" }).LocalKind);
|
||||
Assert.Equal("group", Interactions.Sanitize(new InteractionEvent { Audience = "unlisted", LocalKind = "group" }).LocalKind);
|
||||
Assert.Null(Interactions.Sanitize(new InteractionEvent { Audience = "private", LocalKind = "group" }).LocalKind);
|
||||
Assert.Null(Interactions.Sanitize(new InteractionEvent { Audience = "none", LocalKind = "person" }).LocalKind);
|
||||
Assert.Null(Interactions.Sanitize(new InteractionEvent { LocalKind = "person" }).LocalKind);
|
||||
Assert.Null(Interactions.Sanitize(new InteractionEvent { Audience = "public", LocalKind = "circle" }).LocalKind);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Junk_becomes_other_and_hosts_are_normalised()
|
||||
{
|
||||
var e = Interactions.Sanitize(new InteractionEvent
|
||||
{
|
||||
Host = "Mastodon.Social.",
|
||||
Channel = "sideways",
|
||||
Activity = "https://www.w3.org/ns/activitystreams#Create",
|
||||
Object = "Note",
|
||||
Outcome = "Dropped!",
|
||||
Reason = "not-addressed",
|
||||
Audience = "everyone",
|
||||
Inbox = "front-door",
|
||||
Signature = "cavage:rsa-sha256",
|
||||
Features = new() { "fep-044f", "Bad Feature", "fep-044f", "source:text/markdown" }
|
||||
});
|
||||
|
||||
Assert.Equal("mastodon.social", e.Host);
|
||||
Assert.Equal("other", e.Channel);
|
||||
Assert.Equal("other", e.Activity);
|
||||
Assert.Equal("Note", e.Object);
|
||||
Assert.Equal("other", e.Outcome);
|
||||
Assert.Equal("not-addressed", e.Reason);
|
||||
Assert.Null(e.Audience);
|
||||
Assert.Null(e.Inbox);
|
||||
Assert.Equal("cavage:rsa-sha256", e.Signature);
|
||||
Assert.Equal(new[] { "fep-044f", "other", "source:text/markdown" }, e.Features);
|
||||
Assert.Equal("-", Interactions.Host("evil host/with path"));
|
||||
Assert.Equal("-", Interactions.Host(default));
|
||||
Assert.Equal("social.example", Interactions.HostOf("https://Social.Example:8443/users/x"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Latencies_and_waits_fall_into_buckets()
|
||||
{
|
||||
Assert.Equal("le50ms", Interactions.Latency(0));
|
||||
Assert.Equal("le250ms", Interactions.Latency(101));
|
||||
Assert.Equal("inf", Interactions.Latency(31_000));
|
||||
Assert.Equal("le1s", Interactions.Wait(900));
|
||||
Assert.Equal("le60s", Interactions.Wait(59_000));
|
||||
Assert.Equal("inf", Interactions.Wait(100_000_000));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_actor_hash_is_stable_for_a_salt_and_unlinkable_across_salts()
|
||||
{
|
||||
var monday = new byte[32];
|
||||
var tuesday = Enumerable.Repeat((byte)1, 32).ToArray();
|
||||
const string actor = "https://social.example/users/alice";
|
||||
|
||||
var first = InteractionSalts.Hash(monday, actor);
|
||||
|
||||
Assert.Equal(first, InteractionSalts.Hash(monday, actor));
|
||||
Assert.NotEqual(first, InteractionSalts.Hash(tuesday, actor));
|
||||
Assert.NotEqual(first, InteractionSalts.Hash(monday, actor + "2"));
|
||||
Assert.Equal(16, first.Length);
|
||||
Assert.DoesNotContain("alice", first);
|
||||
Assert.Null(InteractionSalts.Hash(default, actor));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_full_ledger_drops_and_counts_but_never_blocks_or_throws()
|
||||
{
|
||||
var ledger = new InteractionLedger(new InteractionSalts(), new StaticOptions<StatisticsOptions>(new StatisticsOptions()), NullLogger<InteractionLedger>.Instance);
|
||||
|
||||
for (var i = 0; i < 10_005; i++)
|
||||
ledger.Record(new InteractionEvent { Channel = Interactions.In });
|
||||
ledger.Record(default);
|
||||
|
||||
Assert.Equal(5, ledger.Dropped);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_disabled_ledger_records_nothing()
|
||||
{
|
||||
var ledger = new InteractionLedger(new InteractionSalts(), new StaticOptions<StatisticsOptions>(new StatisticsOptions { Enabled = false }), NullLogger<InteractionLedger>.Instance);
|
||||
|
||||
for (var i = 0; i < 10_005; i++)
|
||||
ledger.Record(new InteractionEvent { Channel = Interactions.In });
|
||||
|
||||
Assert.Equal(0, ledger.Dropped);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Models.Statistics;
|
||||
using PrivaPub.Tests.Support;
|
||||
using PrivaPub.Tests.Support.Host;
|
||||
|
||||
using System.Net;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Statistics
|
||||
{
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class LedgerOverHttpTests : IAsyncLifetime
|
||||
{
|
||||
PrivaPubHost _host;
|
||||
Peer _peer;
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
_host = await PrivaPubHost.Shared();
|
||||
_peer = await Peer.Start();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_peer != default)
|
||||
await _peer.DisposeAsync();
|
||||
}
|
||||
|
||||
JsonObject DirectNote(RemoteActor sender, Persona persona, string text)
|
||||
{
|
||||
var noteId = $"{_peer.A}/notes/{Guid.NewGuid():N}";
|
||||
var to = new JsonArray($"{PrivaPubHost.Base}/peasants/{persona.UserName}");
|
||||
return new JsonObject
|
||||
{
|
||||
["id"] = noteId + "/activity",
|
||||
["type"] = "Create",
|
||||
["actor"] = sender.Id,
|
||||
["to"] = to.DeepClone(),
|
||||
["object"] = new JsonObject { ["id"] = noteId, ["type"] = "Note", ["attributedTo"] = sender.Id, ["to"] = to.DeepClone(), ["content"] = text }
|
||||
};
|
||||
}
|
||||
|
||||
async Task<List<BsonDocument>> StoredSince(DateTime since, CancellationToken token)
|
||||
{
|
||||
await _host.Get<InteractionLedger>().Flush(token);
|
||||
return await DB.Default.Database().GetCollection<BsonDocument>(nameof(InteractionEvent))
|
||||
.Find(Builders<BsonDocument>.Filter.Gte(nameof(InteractionEvent.At), since) & Builders<BsonDocument>.Filter.In(nameof(InteractionEvent.Host), new[] { "127.0.0.1", Interactions.Unknown }))
|
||||
.ToListAsync(token);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_delivery_and_its_refusals_are_recorded_naming_only_the_server()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var since = DateTime.UtcNow.AddSeconds(-1);
|
||||
var persona = await _host.Persona(await _host.SignUp(), "ledger");
|
||||
var bob = new RemoteActor(_peer, "bob");
|
||||
using var client = _host.Client();
|
||||
|
||||
Assert.Equal(HttpStatusCode.Accepted, (await client.SendAsync(bob.SignedPost($"/peasants/{persona.UserName}/mouth", DirectNote(bob, persona, "psst")), token)).StatusCode);
|
||||
var forged = bob.SignedPost("/human-centipede", DirectNote(bob, persona, "forged"));
|
||||
forged.Headers.Remove("Signature");
|
||||
forged.Headers.TryAddWithoutValidation("Signature", bob.SignedPost("/elsewhere", new JsonObject()).Headers.GetValues("Signature").Single());
|
||||
Assert.Equal(HttpStatusCode.Unauthorized, (await client.SendAsync(forged, token)).StatusCode);
|
||||
Assert.Equal(HttpStatusCode.NotFound, (await client.SendAsync(bob.SignedPost($"/peasants/nobody{Guid.NewGuid():N}"[..20] + "/mouth", DirectNote(bob, persona, "lost")), token)).StatusCode);
|
||||
|
||||
var stored = await StoredSince(since, token);
|
||||
var reasons = stored.Select(e => e.GetValue(nameof(InteractionEvent.Reason), BsonNull.Value).ToString()).ToList();
|
||||
Assert.Contains("queued", reasons);
|
||||
Assert.Contains("signature-invalid", reasons);
|
||||
Assert.Contains("unknown-recipient", reasons);
|
||||
var everything = string.Join("\n", stored.Select(e => e.ToJson()));
|
||||
Assert.DoesNotContain(persona.UserName, everything);
|
||||
Assert.DoesNotContain(persona.Id, everything);
|
||||
Assert.DoesNotContain(persona.Root.Id, everything);
|
||||
Assert.DoesNotContain(bob.Name, everything);
|
||||
Assert.DoesNotContain("/notes/", everything);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Client_and_served_traffic_is_counted_per_day_never_per_server()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var persona = await _host.Persona(await _host.SignUp(), "served");
|
||||
var reader = new RemoteActor(_peer, "reader");
|
||||
var day = DateTime.UtcNow.Date;
|
||||
await _host.Get<InteractionLedger>().Flush(token);
|
||||
var before = await DB.Default.Find<ServerDay>().Match(d => d.Day == day).ExecuteFirstAsync(token) ?? new ServerDay();
|
||||
using var client = _host.Client();
|
||||
|
||||
Assert.Equal(HttpStatusCode.OK, (await client.GetAsync("/api/v1/instance", token)).StatusCode);
|
||||
using (var actorRequest = new HttpRequestMessage(HttpMethod.Get, $"/peasants/{persona.UserName}"))
|
||||
{
|
||||
actorRequest.Headers.Accept.ParseAdd("application/activity+json");
|
||||
Assert.Equal(HttpStatusCode.OK, (await client.SendAsync(actorRequest, token)).StatusCode);
|
||||
}
|
||||
Assert.Equal(HttpStatusCode.OK, (await client.SendAsync(reader.SignedGet($"/peasants/{persona.UserName}/anus"), token)).StatusCode);
|
||||
await _host.Get<InteractionLedger>().Flush(token);
|
||||
|
||||
var after = await DB.Default.Find<ServerDay>().Match(d => d.Day == day).ExecuteFirstAsync(token);
|
||||
long Grew(Dictionary<string, long> now, Dictionary<string, long> then, string key) => now.GetValueOrDefault(key) - then.GetValueOrDefault(key);
|
||||
Assert.True(Grew(after.Client, before.Client, "api/v1/instance:GET:2xx") >= 1);
|
||||
Assert.True(Grew(after.Served, before.Served, "actor:200:unsigned") >= 1);
|
||||
Assert.True(Grew(after.Served, before.Served, "outbox:200:signed") >= 1);
|
||||
Assert.DoesNotContain(after.Served.Keys, k => k.Contains(persona.UserName));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Models.Statistics;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
namespace PrivaPub.Tests.Statistics
|
||||
{
|
||||
[Trait("Category", "Integration")]
|
||||
public sealed class LedgerStoreTests : IAsyncLifetime
|
||||
{
|
||||
readonly string _known = $"known{Guid.NewGuid():N}.example";
|
||||
readonly string _stranger = $"stranger{Guid.NewGuid():N}.example";
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||
await DB.Default.SaveAsync(new RemoteInstance { Host = _known });
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
||||
|
||||
static InteractionLedger Ledger() =>
|
||||
new(new InteractionSalts(), new StaticOptions<StatisticsOptions>(new StatisticsOptions()), NullLogger<InteractionLedger>.Instance);
|
||||
|
||||
[Fact]
|
||||
public async Task Events_are_stored_with_a_hash_and_never_the_actor()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var ledger = Ledger();
|
||||
var actor = $"https://{_known}/users/someone{Guid.NewGuid():N}";
|
||||
|
||||
ledger.Record(new InteractionEvent { Host = _known, Channel = Interactions.In, Activity = "Create", Object = "Note", Outcome = Interactions.Accepted }, actor);
|
||||
ledger.Record(new InteractionEvent { Host = _stranger, Channel = Interactions.Receive, Status = 401, Outcome = Interactions.Refused, Reason = "no-signature" }, hostClaimed: true);
|
||||
ledger.Record(new InteractionEvent { Host = _known, Channel = Interactions.Receive, Status = 401, Outcome = Interactions.Refused, Reason = "signature-invalid" }, hostClaimed: true);
|
||||
await ledger.Flush(token);
|
||||
|
||||
var stored = await DB.Default.Find<InteractionEvent>().Match(e => e.Host == _known).ExecuteAsync(token);
|
||||
Assert.Equal(2, stored.Count);
|
||||
var accepted = Assert.Single(stored, e => e.Channel == Interactions.In);
|
||||
Assert.NotNull(accepted.ActorHash);
|
||||
Assert.Equal("signature-invalid", Assert.Single(stored, e => e.Channel == Interactions.Receive).Reason);
|
||||
Assert.False(await DB.Default.Find<InteractionEvent>().Match(e => e.Host == _stranger).ExecuteAnyAsync(token));
|
||||
var raw = await DB.Default.Database().GetCollection<BsonDocument>(nameof(InteractionEvent)).Find(new BsonDocument("_id", ObjectId.Parse(accepted.ID))).FirstAsync(token);
|
||||
Assert.DoesNotContain("someone", raw.ToJson());
|
||||
Assert.DoesNotContain("/users/", raw.ToJson());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Two_processes_agree_on_a_days_salt_and_no_salt_is_made_for_the_past()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
|
||||
var one = await new InteractionSalts().For(DateTime.UtcNow, token);
|
||||
var two = await new InteractionSalts().For(DateTime.UtcNow, token);
|
||||
|
||||
Assert.Equal(Convert.ToBase64String(one), Convert.ToBase64String(two));
|
||||
Assert.Null(await new InteractionSalts().For(new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc), token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Counters_add_up_per_day()
|
||||
{
|
||||
var token = TestContext.Current.CancellationToken;
|
||||
var ledger = Ledger();
|
||||
|
||||
ledger.Count(_known, "media:hit", 1000);
|
||||
ledger.Count(_known, "media:hit", 500);
|
||||
ledger.Count(_known, "media.miss");
|
||||
ledger.CountServer(ServerSections.Client, $"test{_known}:GET:2xx", 120);
|
||||
ledger.CountServer(ServerSections.Served, $"test{_known}:200:signed");
|
||||
await ledger.Flush(token);
|
||||
|
||||
var day = await DB.Default.Find<InstanceDay>().Match(d => d.Host == _known && d.Day == DateTime.UtcNow.Date).ExecuteFirstAsync(token);
|
||||
Assert.Equal(2, day.Reads["media:hit"]);
|
||||
Assert.Equal(1500, day.Reads["media:hit:bytes"]);
|
||||
Assert.Equal(1, day.Reads["media_miss"]);
|
||||
var server = await DB.Default.Find<ServerDay>().Match(d => d.Day == DateTime.UtcNow.Date).ExecuteFirstAsync(token);
|
||||
Assert.Equal(1, server.Client[$"test{_known.Replace('.', '_')}:GET:2xx"]);
|
||||
Assert.Equal(1, server.Served[$"test{_known.Replace('.', '_')}:200:signed"]);
|
||||
Assert.True(server.ClientLatency["le250ms"] >= 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
using PrivaPub.Infrastructure.Http;
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Tests.Support;
|
||||
|
||||
namespace PrivaPub.Tests.Statistics
|
||||
{
|
||||
public class HttpScopeTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Scopes_nest_and_restore_across_awaits()
|
||||
{
|
||||
Assert.Null(HttpScope.Purpose);
|
||||
Assert.Equal("request", HttpScope.Trigger);
|
||||
|
||||
using (HttpScope.Triggered("deliver"))
|
||||
{
|
||||
using (HttpScope.Default("object"))
|
||||
{
|
||||
Assert.Equal("object", HttpScope.Purpose);
|
||||
using (HttpScope.For("actor"))
|
||||
{
|
||||
await Task.Yield();
|
||||
Assert.Equal("actor", HttpScope.Purpose);
|
||||
using (HttpScope.Default("object"))
|
||||
Assert.Equal("actor", HttpScope.Purpose);
|
||||
}
|
||||
Assert.Equal("object", HttpScope.Purpose);
|
||||
}
|
||||
Assert.Null(HttpScope.Purpose);
|
||||
Assert.Equal("deliver", HttpScope.Trigger);
|
||||
Assert.False(HttpScope.Crawling);
|
||||
using (HttpScope.Crawl())
|
||||
Assert.True(HttpScope.Crawling);
|
||||
}
|
||||
Assert.Equal("request", HttpScope.Trigger);
|
||||
}
|
||||
}
|
||||
|
||||
public class TrafficMeterTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("api/v1/accounts/{id}/follow", "api/v1/accounts")]
|
||||
[InlineData("/api/v1/statuses", "api/v1/statuses")]
|
||||
[InlineData("/oauth/token", "oauth/token")]
|
||||
[InlineData("/clientapi/avatar/private/insert", "clientapi/avatar/private")]
|
||||
[InlineData("api/{x}", "api")]
|
||||
[InlineData(null, "unmatched")]
|
||||
public void Client_routes_group_by_their_first_literal_segments(string template, string group) =>
|
||||
Assert.Equal(group, TrafficMeter.Group(template));
|
||||
|
||||
[Theory]
|
||||
[InlineData("peasants/{actor}", "/peasants/alice", "actor")]
|
||||
[InlineData("peasants/{actor}/anus", "/peasants/alice/anus", "outbox")]
|
||||
[InlineData("peasants/{actor}/flock", "/peasants/c/flock", "collection")]
|
||||
[InlineData("peasants/{actor}/groupies", "/peasants/alice/groupies", "collection")]
|
||||
[InlineData("peasants/{actor}/scribbles/{postId}", "/peasants/alice/scribbles/1", "object")]
|
||||
[InlineData("peasants/{actor}/grunts/{activityId}", "/peasants/alice/grunts/1", "activity")]
|
||||
[InlineData("peasants/{actor}/parrot-licences/{licenceId}", "/peasants/alice/parrot-licences/1", "licence")]
|
||||
[InlineData("users/{actor}", "/users/alice", "actor")]
|
||||
[InlineData(".well-known/webfinger", "/.well-known/webfinger", "webfinger")]
|
||||
[InlineData("nodeinfo/2.1", "/nodeinfo/2.1", "nodeinfo")]
|
||||
[InlineData("@{user}", "/@alice", null)]
|
||||
public void Served_documents_are_counted_by_kind_never_by_name(string template, string path, string kind) =>
|
||||
Assert.Equal(kind, TrafficMeter.Served(template, new PathString(path)));
|
||||
}
|
||||
|
||||
public sealed class OutboundRequestTests : IAsyncLifetime
|
||||
{
|
||||
Peer _peer;
|
||||
|
||||
public async ValueTask InitializeAsync() => _peer = await Peer.Start();
|
||||
|
||||
public async ValueTask DisposeAsync() => await _peer.DisposeAsync();
|
||||
|
||||
[Fact]
|
||||
public async Task A_federation_fetch_is_recorded_with_its_purpose_trigger_and_size()
|
||||
{
|
||||
var ledger = new MemoryLedger();
|
||||
var http = Peer.Http(ledger: ledger);
|
||||
_peer.Serve("/users/a", "{\"id\":\"{A}/users/a\",\"type\":\"Person\"}");
|
||||
|
||||
using (HttpScope.Triggered("processinbox"))
|
||||
using (HttpScope.For("actor"))
|
||||
Assert.NotNull(await http.GetJson($"{_peer.A}/users/a", "application/activity+json", default, TestContext.Current.CancellationToken));
|
||||
|
||||
var e = Assert.Single(ledger.Of("http"));
|
||||
Assert.Equal(("127.0.0.1", "actor", "processinbox", "ok", 200), (e.Host, e.Purpose, e.Trigger, e.Outcome, e.Status!.Value));
|
||||
Assert.True(e.Bytes > 10);
|
||||
Assert.Equal(0, e.Redirects);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Refusals_are_recorded_and_a_second_try_is_remembered()
|
||||
{
|
||||
var ledger = new MemoryLedger();
|
||||
var http = Peer.Http(ledger: ledger);
|
||||
_peer.Answer("/gone", 410);
|
||||
_peer.ServeText("/page", "<html></html>", "text/html");
|
||||
|
||||
using (HttpScope.Triggered("fetchancestors"))
|
||||
{
|
||||
await http.GetJson($"{_peer.A}/gone", "application/activity+json", default, TestContext.Current.CancellationToken);
|
||||
await http.GetJson($"{_peer.A}/gone", "application/activity+json", default, TestContext.Current.CancellationToken);
|
||||
await http.GetJson($"{_peer.A}/page", "application/activity+json", default, TestContext.Current.CancellationToken);
|
||||
await http.GetJson("ftp://example.org/x", "application/activity+json", default, TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
var events = ledger.Of("http");
|
||||
Assert.Equal(new[] { ("refused", "410"), ("refused", "remembered"), ("refused", "content-type"), ("refused", "disallowed") },
|
||||
events.Select(e => (e.Outcome, e.Reason)));
|
||||
Assert.Equal("object", events[0].Purpose);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_fetch_a_reader_caused_is_only_counted()
|
||||
{
|
||||
var ledger = new MemoryLedger();
|
||||
var http = Peer.Http(ledger: ledger);
|
||||
_peer.Serve("/users/b", "{\"id\":\"{A}/users/b\",\"type\":\"Person\"}");
|
||||
|
||||
using (HttpScope.For("webfinger"))
|
||||
await http.GetJson($"{_peer.A}/users/b", "application/activity+json", default, TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.Empty(ledger.Events);
|
||||
Assert.Equal(1, ledger.Counts[("127.0.0.1", "http:webfinger:ok")]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace PrivaPub.Tests.Support
|
||||
{
|
||||
[CollectionDefinition(nameof(Exclusive), DisableParallelization = true)]
|
||||
public sealed class Exclusive
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,8 @@ namespace PrivaPub.Tests.Support
|
||||
public const string Host = "privapub.test";
|
||||
public const string Base = "https://" + Host;
|
||||
|
||||
readonly DateTime _started = Millisecond(DateTime.UtcNow);
|
||||
|
||||
Harness(Peer peer)
|
||||
{
|
||||
Peer = peer;
|
||||
@@ -44,29 +46,35 @@ namespace PrivaPub.Tests.Support
|
||||
Fanout = new Fanout(Db);
|
||||
Groups = new GroupDistributor(Delivery);
|
||||
Records = new ObjectRecords(Queue);
|
||||
RemotePosts = new RemotePosts(Db, Local, Remote, new NoBlocks(), Queue, Records);
|
||||
Receiver = new InboxReceiver(Local, Remote, Queue, new NoBlocks(), NullLogger<InboxReceiver>.Instance);
|
||||
Processor = new InboxProcessor(Remote, new IActivityHandler[]
|
||||
Polls = new PollService(Db, Local, Delivery, Queue);
|
||||
Reactions = new Reactions(Db, Delivery);
|
||||
RemotePosts = new RemotePosts(Db, Local, Remote, new NoBlocks(), Queue, Records, new NoPreviews());
|
||||
Outbox = new OutboxPublisher(Db, Local, Delivery);
|
||||
Quotes = new QuoteService(Db, Remote, RemotePosts, Local, Delivery, Outbox);
|
||||
Receiver = new InboxReceiver(Local, Remote, Queue, new NoBlocks(), NullLogger<InboxReceiver>.Instance, Ledger);
|
||||
Handlers = new IActivityHandler[]
|
||||
{
|
||||
new FollowHandler(Db, Local, Remote, Delivery),
|
||||
new AcceptHandler(Db, Local),
|
||||
new RejectHandler(Db, Local),
|
||||
new UndoHandler(Db, Local),
|
||||
new LikeHandler(Db),
|
||||
new AcceptHandler(Db, Local, Quotes),
|
||||
new RejectHandler(Db, Local, Quotes),
|
||||
new UndoHandler(Db, Local, Reactions),
|
||||
new LikeHandler(Db, Reactions),
|
||||
new EmojiReactHandler(Db, Reactions),
|
||||
new QuoteRequestHandler(Quotes),
|
||||
new DislikeHandler(Db),
|
||||
new JoinHandler(Db, Local, Delivery),
|
||||
new AnnounceHandler(Db, Local, RemotePosts, Fanout, Remote),
|
||||
new CreateHandler(Db, Local, Remote, Delivery, new NoBlocks(), Fanout, RemotePosts, Groups, Records),
|
||||
new DeleteHandler(Db, Local, Remote, Delivery, Groups),
|
||||
new UpdateHandler(Db, Local, Remote, Groups, Records),
|
||||
new AnnounceHandler(Db, Local, RemotePosts, Fanout, Remote, Records, Quotes),
|
||||
new CreateHandler(Db, Local, Remote, Delivery, new NoBlocks(), Fanout, RemotePosts, Groups, Records, Polls, new NoPreviews(), Quotes),
|
||||
new DeleteHandler(Db, Local, Remote, Delivery, Groups, Quotes),
|
||||
new UpdateHandler(Db, Local, Remote, Groups, Records, Quotes),
|
||||
new FlagHandler(Db, Local)
|
||||
}, NullLogger<InboxProcessor>.Instance);
|
||||
};
|
||||
Processor = new InboxProcessor(Remote, Handlers, NullLogger<InboxProcessor>.Instance, Ledger);
|
||||
Follows = new FollowService(Db, Local, Remote, Delivery, new KeyLocalizer<GenericRes>(), NullLogger<FollowService>.Instance);
|
||||
Content = new ContentRenderer(Local, Remote);
|
||||
Outbox = new OutboxPublisher(Db, Local, Delivery);
|
||||
Media = new MediaService(new StaticOptions<MediaOptions>(new MediaOptions { Root = Path.Combine(Path.GetTempPath(), $"privapub-media-{Guid.NewGuid():N}") }),
|
||||
Local, default, NullLogger<MediaService>.Instance);
|
||||
Statuses = new StatusService(Db, Local, Remote, Delivery, Content, Outbox, Fanout, Media, Groups);
|
||||
Statuses = new StatusService(Db, Local, Remote, Delivery, Content, Outbox, Fanout, Media, Groups, Polls, new NoPreviews(), Quotes);
|
||||
Posts = new PostsService(Db, Local, Statuses, new KeyLocalizer<GenericRes>(), NullLogger<PostsService>.Instance);
|
||||
Timelines = new TimelineService(Db, new KeyLocalizer<GenericRes>());
|
||||
Relationships = new RelationshipService(Db, Follows, Delivery);
|
||||
@@ -75,12 +83,14 @@ namespace PrivaPub.Tests.Support
|
||||
|
||||
public Peer Peer { get; }
|
||||
public DbEntities Db { get; } = new();
|
||||
public MemoryLedger Ledger { get; } = new();
|
||||
public JobQueue Queue { get; } = new();
|
||||
public LocalActorService Local { get; }
|
||||
public RemoteActorService Remote { get; }
|
||||
public DeliveryService Delivery { get; }
|
||||
public InboxReceiver Receiver { get; }
|
||||
public InboxProcessor Processor { get; }
|
||||
public IReadOnlyList<IActivityHandler> Handlers { get; }
|
||||
public FollowService Follows { get; }
|
||||
public ContentRenderer Content { get; }
|
||||
public OutboxPublisher Outbox { get; }
|
||||
@@ -91,6 +101,9 @@ namespace PrivaPub.Tests.Support
|
||||
public Fanout Fanout { get; }
|
||||
public RemotePosts RemotePosts { get; }
|
||||
public ObjectRecords Records { get; }
|
||||
public PollService Polls { get; }
|
||||
public Reactions Reactions { get; }
|
||||
public QuoteService Quotes { get; }
|
||||
public TimelineService Timelines { get; }
|
||||
public RelationshipService Relationships { get; }
|
||||
public ReportService Reports { get; }
|
||||
@@ -128,13 +141,15 @@ namespace PrivaPub.Tests.Support
|
||||
}
|
||||
|
||||
public async Task<List<JsonObject>> Outgoing(string inbox) =>
|
||||
(await DB.Default.Find<Job>().Match(j => j.Kind == JobKind.Deliver).ExecuteAsync())
|
||||
(await DB.Default.Find<Job>().Match(j => j.Kind == JobKind.Deliver && j.CreatedAt >= _started).ExecuteAsync())
|
||||
.Select(j => JsonSerializer.Deserialize<DeliveryPayload>(j.Payload))
|
||||
.Where(p => p.Inbox == inbox)
|
||||
.Select(p => JsonNode.Parse(p.Body)!.AsObject())
|
||||
.ToList();
|
||||
|
||||
public async ValueTask DisposeAsync() => await Peer.DisposeAsync();
|
||||
|
||||
static DateTime Millisecond(DateTime time) => new(time.Ticks - time.Ticks % TimeSpan.TicksPerMillisecond, DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
public sealed class KeyLocalizer<T> : IStringLocalizer<T>
|
||||
@@ -143,4 +158,9 @@ namespace PrivaPub.Tests.Support
|
||||
public LocalizedString this[string name, params object[] arguments] => new(name, string.Format(name, arguments));
|
||||
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) => Enumerable.Empty<LocalizedString>();
|
||||
}
|
||||
|
||||
public sealed class NoPreviews : ILinkPreviews
|
||||
{
|
||||
public Task Wanted(PrivaPub.Models.Post.Post post, CancellationToken token) => Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
using PrivaPub.Infrastructure.Cli;
|
||||
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace PrivaPub.Tests.Support.Host
|
||||
{
|
||||
public sealed record Root(string Id, string UserName, string Password, string Jwt);
|
||||
|
||||
public sealed record Persona(string Id, string UserName, Root Root);
|
||||
|
||||
public static partial class Accounts
|
||||
{
|
||||
public const string Password = "Test-Pass-1!";
|
||||
const string OutOfBand = "urn:ietf:wg:oauth:2.0:oob";
|
||||
|
||||
public static async Task<Root> SignUp(this PrivaPubHost host, string name = "root")
|
||||
{
|
||||
var userName = $"{name}{Guid.NewGuid():N}"[..24];
|
||||
using var client = host.Client();
|
||||
var response = await client.PostAsJsonAsync("/clientapi/user/signup", new { userName, password = Password });
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
var jwt = (await response.Content.ReadFromJsonAsync<JsonObject>())!;
|
||||
return new Root(jwt["userId"]!.GetValue<string>(), userName, Password, jwt["token"]!.GetValue<string>());
|
||||
}
|
||||
|
||||
public static async Task<Root> LogIn(this PrivaPubHost host, Root root)
|
||||
{
|
||||
using var client = host.Client();
|
||||
var response = await client.PostAsJsonAsync("/clientapi/user/login", new { userName = root.UserName, password = root.Password });
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
var jwt = (await response.Content.ReadFromJsonAsync<JsonObject>())!;
|
||||
return root with { Jwt = jwt["token"]!.GetValue<string>() };
|
||||
}
|
||||
|
||||
public static async Task<Root> Admin(this PrivaPubHost host)
|
||||
{
|
||||
var root = await host.SignUp("admin");
|
||||
Assert.Equal(0, await AdminCommands.Run(new[] { "promote", root.UserName }));
|
||||
return await host.LogIn(root);
|
||||
}
|
||||
|
||||
public static async Task<Persona> Persona(this PrivaPubHost host, Root root, string name = "persona")
|
||||
{
|
||||
var userName = $"{name}{Guid.NewGuid():N}"[..20];
|
||||
using var client = host.As(root.Jwt);
|
||||
var response = await client.PostAsJsonAsync("/clientapi/avatar/private/insert", new { userName, name, biography = "testing" });
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
var avatar = (await response.Content.ReadFromJsonAsync<JsonObject>())!;
|
||||
return new Persona(avatar["id"]!.GetValue<string>(), userName, root);
|
||||
}
|
||||
|
||||
public static async Task<string> MastodonToken(this PrivaPubHost host, Persona persona, string scopes = "read write follow")
|
||||
{
|
||||
using var client = host.Client(cookies: true);
|
||||
var app = await Form(client, "/api/v1/apps", ("client_name", "privapub-tests"), ("redirect_uris", OutOfBand), ("scopes", scopes));
|
||||
var clientId = app["client_id"]!.GetValue<string>();
|
||||
var clientSecret = app["client_secret"]!.GetValue<string>();
|
||||
var query = $"client_id={Uri.EscapeDataString(clientId)}&redirect_uri={Uri.EscapeDataString(OutOfBand)}&response_type=code&scope={Uri.EscapeDataString(scopes)}";
|
||||
|
||||
var code = await Authorize(client, persona, query);
|
||||
var token = await Form(client, "/oauth/token", ("grant_type", "authorization_code"), ("code", code), ("client_id", clientId),
|
||||
("client_secret", clientSecret), ("redirect_uri", OutOfBand));
|
||||
return token["access_token"]!.GetValue<string>();
|
||||
}
|
||||
|
||||
public static async Task<string> Authorize(HttpClient client, Persona persona, string query, string decision = "allow")
|
||||
{
|
||||
var returnUrl = "/oauth/authorize?" + query;
|
||||
var login = await client.GetStringAsync("/oauth/login?returnUrl=" + Uri.EscapeDataString(returnUrl));
|
||||
var antiforgery = AntiforgeryToken().Match(login).Groups[1].Value;
|
||||
var signedIn = await client.PostAsync("/oauth/login", new FormUrlEncodedContent(new Dictionary<string, string>
|
||||
{
|
||||
["returnUrl"] = returnUrl,
|
||||
["__RequestVerificationToken"] = antiforgery,
|
||||
["userName"] = persona.Root.UserName,
|
||||
["password"] = persona.Root.Password
|
||||
}));
|
||||
Assert.Equal(HttpStatusCode.Redirect, signedIn.StatusCode);
|
||||
|
||||
var choose = await client.GetStringAsync(returnUrl + "&signed_in=1");
|
||||
var fields = HiddenInput().Matches(choose).Select(m => new KeyValuePair<string, string>(m.Groups[1].Value, WebUtility.HtmlDecode(m.Groups[2].Value))).ToList();
|
||||
fields.Add(new("avatarId", persona.Id));
|
||||
fields.Add(new("decision", decision));
|
||||
var answer = await client.PostAsync("/oauth/authorize", new FormUrlEncodedContent(fields));
|
||||
var page = await answer.Content.ReadAsStringAsync();
|
||||
return Code().Match(page) is { Success: true } match ? match.Groups[1].Value : default;
|
||||
}
|
||||
|
||||
public static HttpClient As(this PrivaPubHost host, string bearer)
|
||||
{
|
||||
var client = host.Client();
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearer);
|
||||
return client;
|
||||
}
|
||||
|
||||
static async Task<JsonObject> Form(HttpClient client, string path, params (string Key, string Value)[] fields)
|
||||
{
|
||||
var response = await client.PostAsync(path, new FormUrlEncodedContent(fields.Select(f => new KeyValuePair<string, string>(f.Key, f.Value))));
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
Assert.True(response.IsSuccessStatusCode, $"{path} answered {(int)response.StatusCode}: {body}");
|
||||
return JsonNode.Parse(body)!.AsObject();
|
||||
}
|
||||
|
||||
[GeneratedRegex("name=\"__RequestVerificationToken\" type=\"hidden\" value=\"([^\"]*)\"")]
|
||||
private static partial Regex AntiforgeryToken();
|
||||
|
||||
[GeneratedRegex("<input type=\"hidden\" name=\"([^\"]*)\" value=\"([^\"]*)\"")]
|
||||
private static partial Regex HiddenInput();
|
||||
|
||||
[GeneratedRegex("<code>([^<]*)</code>")]
|
||||
private static partial Regex Code();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Federation.Outbox;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
using PrivaPub.Models.Jobs;
|
||||
|
||||
using System.Linq.Expressions;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Tests.Support.Host
|
||||
{
|
||||
public static class Jobs
|
||||
{
|
||||
public static async Task<int> Run(this PrivaPubHost host, Expression<Func<Job, bool>> which, CancellationToken token = default)
|
||||
{
|
||||
var handlers = host.Services.GetServices<IJobHandler>().ToDictionary(h => h.Kind);
|
||||
var queue = host.Get<IJobQueue>();
|
||||
var ran = 0;
|
||||
for (var round = 0; round < 50; round++)
|
||||
{
|
||||
var pending = await DB.Default.Find<Job>().Match(which).Match(j => j.State == JobState.Pending).ExecuteAsync(token);
|
||||
if (pending.Count == 0)
|
||||
return ran;
|
||||
foreach (var candidate in pending)
|
||||
{
|
||||
var job = await DB.Default.UpdateAndGet<Job>()
|
||||
.Match(j => j.ID == candidate.ID && j.State == JobState.Pending)
|
||||
.Modify(j => j.State, JobState.Running)
|
||||
.Modify(j => j.LeasedUntil, DateTime.UtcNow + JobQueue.LeaseTime)
|
||||
.Modify(b => b.Inc(j => j.Attempts, 1))
|
||||
.ExecuteAsync(token);
|
||||
if (job == default)
|
||||
continue;
|
||||
var handler = handlers[job.Kind];
|
||||
JobOutcome outcome;
|
||||
try
|
||||
{
|
||||
outcome = await handler.Handle(job, token);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
outcome = JobOutcome.Dead(ex.GetType().Name + ": " + ex.Message);
|
||||
}
|
||||
await queue.Finish(job, outcome, handler.MaxAttempts, token);
|
||||
ran++;
|
||||
}
|
||||
}
|
||||
return ran;
|
||||
}
|
||||
|
||||
public static Task<int> RunInbox(this PrivaPubHost host, string activityId, CancellationToken token = default) =>
|
||||
host.Run(j => j.Kind == JobKind.ProcessInbox && j.DedupeKey == "inbox|" + activityId, token);
|
||||
|
||||
public static async Task<List<JsonObject>> Deliveries(string inbox, DateTime since, CancellationToken token = default) =>
|
||||
(await DB.Default.Find<Job>().Match(j => j.Kind == JobKind.Deliver && j.CreatedAt >= since).ExecuteAsync(token))
|
||||
.Select(j => JsonSerializer.Deserialize<DeliveryPayload>(j.Payload))
|
||||
.Where(p => p.Inbox == inbox)
|
||||
.Select(p => JsonNode.Parse(p.Body)!.AsObject())
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
using PrivaPub.Api.Mastodon.Auth;
|
||||
using PrivaPub.Domain.Media;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
|
||||
using System.Net;
|
||||
|
||||
namespace PrivaPub.Tests.Support.Host
|
||||
{
|
||||
public class PrivaPubHost : WebApplicationFactory<Program>
|
||||
{
|
||||
public const string Host = "privapub.test";
|
||||
public const string Base = "https://" + Host;
|
||||
public const string ClientHeader = "X-Test-Client";
|
||||
|
||||
static readonly SemaphoreSlim Boot = new(1, 1);
|
||||
static readonly Type[] Unwanted = { typeof(JobWorker), typeof(MediaJanitor), typeof(OAuthPruner) };
|
||||
static PrivaPubHost _shared;
|
||||
|
||||
readonly string _mediaRoot = Path.Combine(Path.GetTempPath(), $"privapub-tests-{Guid.NewGuid():N}");
|
||||
|
||||
public IReadOnlyList<ServiceDescriptor> Registered { get; private set; }
|
||||
|
||||
public static async Task<PrivaPubHost> Shared()
|
||||
{
|
||||
await Boot.WaitAsync();
|
||||
try
|
||||
{
|
||||
if (_shared == default)
|
||||
{
|
||||
var host = new PrivaPubHost();
|
||||
_ = host.Services;
|
||||
_shared = host;
|
||||
}
|
||||
return _shared;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Boot.Release();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual IEnumerable<KeyValuePair<string, string>> Settings() => new Dictionary<string, string>
|
||||
{
|
||||
["MongoSettings:ConnectionString"] = MongoFixture.Connection,
|
||||
["MongoSettings:Database"] = MongoFixture.Database,
|
||||
["MongoSettings:LogsDatabase"] = "logs",
|
||||
["AppConfiguration:Version"] = "0.0.0-test",
|
||||
["AppConfiguration:BackendBaseAddress"] = Base,
|
||||
["AppConfiguration:MaxAllowedUploadFiles"] = "3",
|
||||
["AppConfiguration:MaxAllowedFileSize"] = "2097152",
|
||||
["AppConfiguration:SupportedLanguages:0"] = "en",
|
||||
["AppConfiguration:SupportedLanguages:1"] = "it",
|
||||
["AppConfiguration:HashingOptions:Iterations"] = "1000",
|
||||
["AppConfiguration:Jwt:Key"] = "privapub-tests-only-signing-key-0123456789abcdef0123456789abcdef",
|
||||
["AppConfiguration:Jwt:Issuer"] = Base,
|
||||
["AppConfiguration:Jwt:Audience"] = Base,
|
||||
["AppConfiguration:Jwt:HoursTimeout"] = "1",
|
||||
["AppConfiguration:EmailConfiguration:SmtpServer"] = "127.0.0.1",
|
||||
["AppConfiguration:EmailConfiguration:SmtpPort"] = "9",
|
||||
["AppConfiguration:EmailConfiguration:UseSSL"] = "false",
|
||||
["AppConfiguration:EmailConfiguration:SmtpUsername"] = "nobody@privapub.test",
|
||||
["AppConfiguration:EmailConfiguration:SmtpPassword"] = "none",
|
||||
["Federation:AllowPrivateNetworks"] = "true",
|
||||
["Federation:AllowPlainHttp"] = "true",
|
||||
["Federation:FetchLinkPreviews"] = "false",
|
||||
["Media:Root"] = _mediaRoot,
|
||||
["Logging:LogLevel:Default"] = "Warning",
|
||||
["Serilog:MinimumLevel:Default"] = Environment.GetEnvironmentVariable("PRIVAPUB_TEST_LOGS") == "1" ? "Information" : "Fatal"
|
||||
};
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
builder.UseEnvironment("Testing");
|
||||
foreach (var (key, value) in Settings())
|
||||
builder.UseSetting(key, value);
|
||||
builder.ConfigureTestServices(services =>
|
||||
{
|
||||
foreach (var hosted in services.Where(s => s.ServiceType == typeof(IHostedService) && Unwanted.Contains(s.ImplementationType)).ToList())
|
||||
services.Remove(hosted);
|
||||
services.AddDataProtection().UseEphemeralDataProtectionProvider();
|
||||
services.AddSingleton<IStartupFilter, ClientAddressFilter>();
|
||||
Registered = services.ToList();
|
||||
});
|
||||
}
|
||||
|
||||
protected override void ConfigureClient(HttpClient client)
|
||||
{
|
||||
client.BaseAddress = new Uri(Base + "/");
|
||||
client.DefaultRequestHeaders.Add(ClientHeader, $"10.{Random.Shared.Next(256)}.{Random.Shared.Next(256)}.{Random.Shared.Next(1, 255)}");
|
||||
}
|
||||
|
||||
public HttpClient Client(bool cookies = false) => CreateClient(new WebApplicationFactoryClientOptions
|
||||
{
|
||||
BaseAddress = new Uri(Base + "/"),
|
||||
AllowAutoRedirect = false,
|
||||
HandleCookies = cookies
|
||||
});
|
||||
|
||||
public T Get<T>() where T : notnull => Services.GetRequiredService<T>();
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
if (disposing && Directory.Exists(_mediaRoot))
|
||||
Directory.Delete(_mediaRoot, recursive: true);
|
||||
}
|
||||
|
||||
sealed class ClientAddressFilter : IStartupFilter
|
||||
{
|
||||
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) => app =>
|
||||
{
|
||||
app.Use(async (context, call) =>
|
||||
{
|
||||
if (IPAddress.TryParse(context.Request.Headers[ClientHeader].ToString(), out var address))
|
||||
context.Connection.RemoteIpAddress = address;
|
||||
await call(context);
|
||||
});
|
||||
next(app);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SecureModeHost : PrivaPubHost
|
||||
{
|
||||
static readonly SemaphoreSlim Boot = new(1, 1);
|
||||
static SecureModeHost _shared;
|
||||
|
||||
public static new async Task<SecureModeHost> Shared()
|
||||
{
|
||||
await PrivaPubHost.Shared();
|
||||
await Boot.WaitAsync();
|
||||
try
|
||||
{
|
||||
if (_shared == default)
|
||||
{
|
||||
var host = new SecureModeHost();
|
||||
_ = host.Services;
|
||||
_shared = host;
|
||||
}
|
||||
return _shared;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Boot.Release();
|
||||
}
|
||||
}
|
||||
|
||||
protected override IEnumerable<KeyValuePair<string, string>> Settings() =>
|
||||
base.Settings().Append(new KeyValuePair<string, string>("Federation:SecureMode", "true"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Models.Statistics;
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace PrivaPub.Tests.Support
|
||||
{
|
||||
public sealed class MemoryLedger : IInteractionLedger
|
||||
{
|
||||
public ConcurrentQueue<(InteractionEvent Event, string ActorUri, bool HostClaimed)> Events { get; } = new();
|
||||
public ConcurrentDictionary<(string Host, string Key), long> Counts { get; } = new();
|
||||
public ConcurrentDictionary<(string Section, string Key), long> ServerCounts { get; } = new();
|
||||
|
||||
public long Dropped => 0;
|
||||
|
||||
public void Record(InteractionEvent interaction, string actorUri = default, bool hostClaimed = false) =>
|
||||
Events.Enqueue((Interactions.Sanitize(interaction), actorUri, hostClaimed));
|
||||
|
||||
public void Count(string host, string key, long bytes = 0) =>
|
||||
Counts.AddOrUpdate((Interactions.Host(host), key), 1, (_, count) => count + 1);
|
||||
|
||||
public void CountServer(string section, string key, int? latencyMs = default) =>
|
||||
ServerCounts.AddOrUpdate((section, key), 1, (_, count) => count + 1);
|
||||
|
||||
public IReadOnlyList<InteractionEvent> Of(string channel) => Events.Select(e => e.Event).Where(e => e.Channel == channel).ToList();
|
||||
}
|
||||
}
|
||||
@@ -13,15 +13,24 @@ namespace PrivaPub.Tests.Support
|
||||
public sealed class MongoFixture : IAsyncLifetime
|
||||
{
|
||||
public const string Skip = "set PRIVAPUB_TEST_MONGOD=1 (and optionally PRIVAPUB_TEST_MONGO) to run the tests that need a mongod";
|
||||
const int ProductionPort = 27022;
|
||||
const int DefaultPort = 27017;
|
||||
|
||||
public static bool Enabled => Environment.GetEnvironmentVariable("PRIVAPUB_TEST_MONGOD") == "1";
|
||||
static bool Required => Environment.GetEnvironmentVariable("PRIVAPUB_TEST_REQUIRE_MONGOD") == "1";
|
||||
static bool InCi => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI")) || !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITEA_ACTIONS"));
|
||||
|
||||
public string Database { get; } = $"PrivaPubTests_{Guid.NewGuid():N}";
|
||||
public static string Connection { get; } = Environment.GetEnvironmentVariable("PRIVAPUB_TEST_MONGO") ?? $"mongodb://127.0.0.1:{DefaultPort}";
|
||||
public static string Database { get; } = $"PrivaPubTests_{Guid.NewGuid():N}";
|
||||
|
||||
public async ValueTask InitializeAsync()
|
||||
{
|
||||
if (Required && !Enabled)
|
||||
throw new InvalidOperationException("PRIVAPUB_TEST_REQUIRE_MONGOD=1 but PRIVAPUB_TEST_MONGOD is not 1: the integration tests would be skipped");
|
||||
if (!Enabled)
|
||||
return;
|
||||
var settings = MongoClientSettings.FromConnectionString(Connection);
|
||||
Refuse(settings);
|
||||
try
|
||||
{
|
||||
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
|
||||
@@ -29,9 +38,10 @@ namespace PrivaPub.Tests.Support
|
||||
catch (BsonSerializationException)
|
||||
{
|
||||
}
|
||||
var connection = Environment.GetEnvironmentVariable("PRIVAPUB_TEST_MONGO") ?? "mongodb://127.0.0.1:27017";
|
||||
await DB.InitAsync(Database, MongoClientSettings.FromConnectionString(connection));
|
||||
await DB.InitAsync(Database, settings);
|
||||
await RefuseProductionData();
|
||||
EntityMaps.Warm();
|
||||
await DB.Default.MigrateAsync<Program>();
|
||||
await Indexes.Create();
|
||||
}
|
||||
|
||||
@@ -40,5 +50,32 @@ namespace PrivaPub.Tests.Support
|
||||
if (Enabled)
|
||||
await DB.Default.Database().Client.DropDatabaseAsync(Database);
|
||||
}
|
||||
|
||||
static void Refuse(MongoClientSettings settings)
|
||||
{
|
||||
var ports = settings.Servers.Select(s => s.Port).ToList();
|
||||
if (ports.Contains(ProductionPort))
|
||||
throw new InvalidOperationException($"the tests never run against port {ProductionPort}: that is production's mongod");
|
||||
if (InCi && (Environment.GetEnvironmentVariable("PRIVAPUB_TEST_MONGO") == default || ports.Contains(DefaultPort)))
|
||||
throw new InvalidOperationException("in CI the tests run only against a throwaway mongod: use tools/ci/with-test-mongod.sh");
|
||||
}
|
||||
|
||||
static async Task RefuseProductionData()
|
||||
{
|
||||
BsonDocument options;
|
||||
try
|
||||
{
|
||||
options = await DB.Default.Database().Client.GetDatabase("admin").RunCommandAsync<BsonDocument>(new BsonDocument("getCmdLineOpts", 1));
|
||||
}
|
||||
catch (MongoCommandException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var dbPath = options.GetValue("parsed", new BsonDocument()).AsBsonDocument
|
||||
.GetValue("storage", new BsonDocument()).AsBsonDocument
|
||||
.GetValue("dbPath", BsonNull.Value);
|
||||
if (dbPath.IsString && dbPath.AsString.StartsWith("/var/lib/privapub", StringComparison.Ordinal))
|
||||
throw new InvalidOperationException($"the mongod at {Connection} keeps its data in {dbPath.AsString}: that is production's");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ using Microsoft.Extensions.Options;
|
||||
|
||||
using PrivaPub.Federation.Moderation;
|
||||
using PrivaPub.Infrastructure.Http;
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Models.Federation;
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
@@ -17,7 +18,7 @@ namespace PrivaPub.Tests.Support
|
||||
public sealed class Peer : IAsyncDisposable
|
||||
{
|
||||
readonly WebApplication _app;
|
||||
readonly ConcurrentDictionary<string, string> _documents = new();
|
||||
readonly ConcurrentDictionary<string, (string Text, string ContentType)> _documents = new();
|
||||
readonly ConcurrentDictionary<string, (int Status, TimeSpan Delay)> _answers = new();
|
||||
readonly ConcurrentDictionary<string, (byte[] Bytes, string ContentType)> _files = new();
|
||||
|
||||
@@ -40,12 +41,15 @@ namespace PrivaPub.Tests.Support
|
||||
Peer peer = default;
|
||||
app.Run(async context =>
|
||||
{
|
||||
peer.Requests.Enqueue(new(context.Request.Method, context.Request.Path, context.Request.Headers["Signature"].ToString()));
|
||||
var body = context.Request.ContentLength > 0 || context.Request.Headers.TransferEncoding.Count > 0
|
||||
? await new StreamReader(context.Request.Body).ReadToEndAsync()
|
||||
: default;
|
||||
peer.Requests.Enqueue(new(context.Request.Method, context.Request.Path, context.Request.Headers["Signature"].ToString(), body,
|
||||
context.Request.Headers.ToDictionary(h => h.Key, h => h.Value.ToString(), StringComparer.OrdinalIgnoreCase)));
|
||||
var key = context.Request.Path.Value;
|
||||
if (peer._files.TryGetValue(key, out var file))
|
||||
{
|
||||
context.Response.ContentType = file.ContentType;
|
||||
await context.Response.Body.WriteAsync(file.Bytes);
|
||||
await Results.Bytes(file.Bytes, file.ContentType, enableRangeProcessing: true).ExecuteAsync(context);
|
||||
return;
|
||||
}
|
||||
if (peer._answers.TryGetValue(key, out var answer))
|
||||
@@ -60,21 +64,23 @@ namespace PrivaPub.Tests.Support
|
||||
context.Response.StatusCode = StatusCodes.Status404NotFound;
|
||||
return;
|
||||
}
|
||||
context.Response.ContentType = "application/activity+json";
|
||||
await context.Response.WriteAsync(document.Replace("{A}", peer.A).Replace("{B}", peer.B));
|
||||
context.Response.ContentType = document.ContentType;
|
||||
await context.Response.WriteAsync(document.Text.Replace("{A}", peer.A).Replace("{B}", peer.B));
|
||||
});
|
||||
await app.StartAsync();
|
||||
peer = new Peer(app, new Uri(app.Urls.First()).Port);
|
||||
return peer;
|
||||
}
|
||||
|
||||
public void Serve(string path, string json) => _documents[path] = json;
|
||||
public void Serve(string path, string json) => _documents[path] = (json, "application/activity+json");
|
||||
|
||||
public void ServeText(string path, string text, string contentType) => _documents[path] = (text, contentType);
|
||||
|
||||
public void ServeFile(string path, byte[] bytes, string contentType) => _files[path] = (bytes, contentType);
|
||||
|
||||
public void Answer(string path, int status, TimeSpan delay = default) => _answers[path] = (status, delay);
|
||||
|
||||
public static FederationHttp Http(IMemoryCache cache = default, IDomainBlocks blocks = default)
|
||||
public static FederationHttp Http(IMemoryCache cache = default, IDomainBlocks blocks = default, IInteractionLedger ledger = default)
|
||||
{
|
||||
var options = new FederationOptions { AllowPrivateNetworks = true, AllowPlainHttp = true };
|
||||
var services = new ServiceCollection();
|
||||
@@ -82,7 +88,7 @@ namespace PrivaPub.Tests.Support
|
||||
.ConfigurePrimaryHttpMessageHandler(() => SafeHttpHandlerFactory.Create(options));
|
||||
return new FederationHttp(services.BuildServiceProvider().GetRequiredService<IHttpClientFactory>(),
|
||||
cache ?? new MemoryCache(new MemoryCacheOptions()), new StaticOptions<FederationOptions>(options),
|
||||
blocks ?? new NoBlocks(), NullLogger<FederationHttp>.Instance);
|
||||
blocks ?? new NoBlocks(), NullLogger<FederationHttp>.Instance, ledger);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync() => await _app.DisposeAsync();
|
||||
@@ -95,7 +101,8 @@ namespace PrivaPub.Tests.Support
|
||||
public Task Reload(CancellationToken token) => Task.CompletedTask;
|
||||
}
|
||||
|
||||
public sealed record HttpRequestRecord(string Method, string Path, string Signature);
|
||||
public sealed record HttpRequestRecord(string Method, string Path, string Signature, string Body = default,
|
||||
IReadOnlyDictionary<string, string> Headers = default);
|
||||
|
||||
public sealed class StaticOptions<T> : IOptionsMonitor<T>
|
||||
{
|
||||
|
||||
@@ -44,18 +44,46 @@ namespace PrivaPub.Tests.Support
|
||||
}
|
||||
};
|
||||
|
||||
public HttpRequestMessage SignedGet(string path, string host = "privapub.test")
|
||||
{
|
||||
var date = DateTimeOffset.UtcNow.ToString("r", CultureInfo.InvariantCulture);
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, path);
|
||||
request.Headers.TryAddWithoutValidation("Date", date);
|
||||
request.Headers.TryAddWithoutValidation("Accept", "application/activity+json");
|
||||
request.Headers.TryAddWithoutValidation("Signature", Signature($"(request-target): get {path}\nhost: {host}\ndate: {date}", "(request-target) host date"));
|
||||
return request;
|
||||
}
|
||||
|
||||
public HttpRequestMessage SignedPost(string path, JsonNode activity, string host = "privapub.test", string date = default)
|
||||
{
|
||||
var body = Encoding.UTF8.GetBytes(activity.ToJsonString());
|
||||
date ??= DateTimeOffset.UtcNow.ToString("r", CultureInfo.InvariantCulture);
|
||||
var digest = HttpSignatures.Digest(body);
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, path) { Content = new ByteArrayContent(body) };
|
||||
request.Content.Headers.TryAddWithoutValidation("Content-Type", "application/activity+json");
|
||||
request.Headers.TryAddWithoutValidation("Date", date);
|
||||
request.Headers.TryAddWithoutValidation("Digest", digest);
|
||||
request.Headers.TryAddWithoutValidation("Signature",
|
||||
Signature($"(request-target): post {path}\nhost: {host}\ndate: {date}\ndigest: {digest}", "(request-target) host date digest"));
|
||||
return request;
|
||||
}
|
||||
|
||||
string Signature(string signingString, string headers)
|
||||
{
|
||||
var signature = Convert.ToBase64String(_key.SignData(Encoding.UTF8.GetBytes(signingString), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
|
||||
return $"keyId=\"{KeyId}\",algorithm=\"rsa-sha256\",headers=\"{headers}\",signature=\"{signature}\"";
|
||||
}
|
||||
|
||||
public HttpRequest Get(string host, string path)
|
||||
{
|
||||
var date = DateTimeOffset.UtcNow.ToString("r", CultureInfo.InvariantCulture);
|
||||
var signingString = $"(request-target): get {path}\nhost: {host}\ndate: {date}";
|
||||
var signature = Convert.ToBase64String(_key.SignData(Encoding.UTF8.GetBytes(signingString), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Method = "GET";
|
||||
context.Request.Host = new HostString(host);
|
||||
context.Request.Path = path;
|
||||
context.Features.Get<IHttpRequestFeature>().RawTarget = path;
|
||||
context.Request.Headers["Date"] = date;
|
||||
context.Request.Headers["Signature"] = $"keyId=\"{KeyId}\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date\",signature=\"{signature}\"";
|
||||
context.Request.Headers["Signature"] = Signature($"(request-target): get {path}\nhost: {host}\ndate: {date}", "(request-target) host date");
|
||||
return context.Request;
|
||||
}
|
||||
|
||||
@@ -64,9 +92,6 @@ namespace PrivaPub.Tests.Support
|
||||
var body = Encoding.UTF8.GetBytes(activity.ToJsonString());
|
||||
var date = DateTimeOffset.UtcNow.ToString("r", CultureInfo.InvariantCulture);
|
||||
var digest = HttpSignatures.Digest(body);
|
||||
var signingString = $"(request-target): post {path}\nhost: {host}\ndate: {date}\ndigest: {digest}";
|
||||
var signature = Convert.ToBase64String(_key.SignData(Encoding.UTF8.GetBytes(signingString), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
|
||||
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Method = "POST";
|
||||
context.Request.Host = new HostString(host);
|
||||
@@ -75,7 +100,7 @@ namespace PrivaPub.Tests.Support
|
||||
context.Request.Headers["Date"] = date;
|
||||
context.Request.Headers["Digest"] = digest;
|
||||
context.Request.Headers["Content-Type"] = "application/activity+json";
|
||||
context.Request.Headers["Signature"] = $"keyId=\"{KeyId}\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date digest\",signature=\"{signature}\"";
|
||||
context.Request.Headers["Signature"] = Signature($"(request-target): post {path}\nhost: {host}\ndate: {date}\ndigest: {digest}", "(request-target) host date digest");
|
||||
context.Request.Body = new MemoryStream(body);
|
||||
context.Request.ContentLength = body.Length;
|
||||
return context.Request;
|
||||
|
||||
@@ -79,6 +79,8 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
avatar.Settings.DefaultSensitive = sensitive;
|
||||
if (Params.Has("source[language]"))
|
||||
avatar.Settings.DefaultLanguage = Params.Get("source[language]");
|
||||
if (Params.Get("source[quote_policy]") is { } quotePolicy && QuotePolicies.IsKnown(quotePolicy))
|
||||
avatar.Settings.QuotePolicy = quotePolicy;
|
||||
var fields = new Dictionary<string, string>();
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
|
||||
@@ -73,6 +73,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
domain = Domain,
|
||||
title = "PrivaPub",
|
||||
version = Version,
|
||||
api_versions = new { mastodon = 7 },
|
||||
source_url = "https://git.thepra.dev/thepra/SocialPub",
|
||||
description = Description,
|
||||
usage = new { users = new { active_month = 0 } },
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@@ -18,10 +19,13 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
readonly IMediaService _media;
|
||||
readonly IMediaProxy _proxy;
|
||||
|
||||
public MediaController(IMediaService media, IMediaProxy proxy)
|
||||
readonly IInteractionLedger _ledger;
|
||||
|
||||
public MediaController(IMediaService media, IMediaProxy proxy, IInteractionLedger ledger = default)
|
||||
{
|
||||
_media = media;
|
||||
_proxy = proxy;
|
||||
_ledger = ledger;
|
||||
}
|
||||
|
||||
[HttpPost("/api/v1/media"), HttpPost("/api/v2/media"), Scope("write:media"), RequestSizeLimit(UploadLimit),
|
||||
@@ -60,13 +64,41 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
[HttpGet("/media/proxy/{signature}/{encoded}"), AllowAnonymous, ApiExplorerSettings(IgnoreApi = true)]
|
||||
public async Task<IActionResult> Proxy(string signature, string encoded, CancellationToken token)
|
||||
{
|
||||
var (path, contentType) = await _proxy.Fetch(signature, encoded, token);
|
||||
if (path == default)
|
||||
var url = _proxy.Verified(signature, encoded);
|
||||
if (url == default)
|
||||
return NotFound();
|
||||
Response.Headers["X-Content-Type-Options"] = "nosniff";
|
||||
Response.Headers["Content-Security-Policy"] = "default-src 'none'; sandbox";
|
||||
Response.Headers["Cache-Control"] = "public, max-age=604800";
|
||||
return PhysicalFile(path, contentType);
|
||||
if (_proxy.Cached(url) is { Path: not null } cached)
|
||||
{
|
||||
_ledger?.Count(Interactions.HostOf(url), "media:hit");
|
||||
return PhysicalFile(cached.Path, cached.ContentType, enableRangeProcessing: true);
|
||||
}
|
||||
if (Request.Headers.Range.Count == 0)
|
||||
{
|
||||
var (path, contentType) = await _proxy.Fetch(signature, encoded, token);
|
||||
if (path != default)
|
||||
return PhysicalFile(path, contentType, enableRangeProcessing: true);
|
||||
}
|
||||
return await Stream(url, token);
|
||||
}
|
||||
|
||||
async Task<IActionResult> Stream(string url, CancellationToken token)
|
||||
{
|
||||
var range = System.Net.Http.Headers.RangeHeaderValue.TryParse(Request.Headers.Range.ToString(), out var asked) ? asked : default;
|
||||
using var upstream = await _proxy.Open(url, range, token);
|
||||
if (upstream == default)
|
||||
return NotFound();
|
||||
Response.StatusCode = (int)upstream.StatusCode;
|
||||
Response.ContentType = upstream.Content.Headers.ContentType?.ToString() ?? "application/octet-stream";
|
||||
if (upstream.Content.Headers.ContentLength is { } length)
|
||||
Response.ContentLength = length;
|
||||
if (upstream.Content.Headers.ContentRange is { } contentRange)
|
||||
Response.Headers.ContentRange = contentRange.ToString();
|
||||
Response.Headers.AcceptRanges = "bytes";
|
||||
await upstream.Content.CopyToAsync(Response.Body, token);
|
||||
return new EmptyResult();
|
||||
}
|
||||
|
||||
object View(MediaAttachment attachment) => new
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||
using PrivaPub.Api.Mastodon.Mappers;
|
||||
using PrivaPub.Domain.Privacy;
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.StaticServices;
|
||||
|
||||
using PostEntity = PrivaPub.Models.Post.Post;
|
||||
|
||||
namespace PrivaPub.Api.Mastodon.Controllers
|
||||
{
|
||||
public class PollsController : MastodonController
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly MastodonMapper _mapper;
|
||||
readonly IPollService _polls;
|
||||
|
||||
public PollsController(DbEntities dbEntities, MastodonMapper mapper, IPollService polls)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_mapper = mapper;
|
||||
_polls = polls;
|
||||
}
|
||||
|
||||
[HttpGet("/api/v1/polls/{id}"), Scope("read:statuses", requiresUser: false), AllowAnonymous]
|
||||
public async Task<IActionResult> Get(string id, CancellationToken token)
|
||||
{
|
||||
var post = await Visible(id, token);
|
||||
return post?.Poll == default ? NotFoundError() : Json(_mapper.Poll(post, MyId, await _polls.OwnVotes(post.ID, MyId, token)));
|
||||
}
|
||||
|
||||
[HttpPost("/api/v1/polls/{id}/votes"), Scope("write:statuses")]
|
||||
public async Task<IActionResult> Vote(string id, CancellationToken token)
|
||||
{
|
||||
var post = await Visible(id, token);
|
||||
if (post?.Poll == default)
|
||||
return NotFoundError();
|
||||
var choices = Params.List("choices").Select(c => int.TryParse(c, out var index) ? index : -1).ToList();
|
||||
var outcome = await _polls.Vote(Me, post, choices, token);
|
||||
if (!outcome.Ok)
|
||||
return Error(outcome.Status, outcome.Error);
|
||||
return Json(_mapper.Poll(outcome.Post, MyId, await _polls.OwnVotes(post.ID, MyId, token)));
|
||||
}
|
||||
|
||||
async Task<PostEntity> Visible(string id, CancellationToken token)
|
||||
{
|
||||
var post = await _dbEntities.Posts.Match(p => p.ID == id && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
return post != default && post.Visibility != PostVisibility.LocalGeo && await VisibilityPolicy.CanSee(post, MyId, token) ? post : default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,15 +64,16 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
Id = record.ActivityId, Type = record.ActivityType, Actor = record.ActivityActorURI
|
||||
},
|
||||
Inbox = record?.Inbox,
|
||||
Signature = record?.KeyId == default ? default : new ProvenanceSignature
|
||||
FetchedBy = record?.Path == ObjectPath.Fetched ? "instance-actor" : default,
|
||||
Signature = record?.KeyId == default || record.Path == ObjectPath.Fetched ? default : new ProvenanceSignature
|
||||
{
|
||||
Scheme = record.SignatureScheme, KeyId = record.KeyId, Algorithm = record.Algorithm, Headers = record.SignedHeaders
|
||||
},
|
||||
ReceivedAt = record == default ? default : MastodonJson.Time(record.ReceivedAt),
|
||||
Published = record?.Published is { } published ? MastodonJson.Time(published) : default,
|
||||
Updated = record?.Updated is { } updated ? MastodonJson.Time(updated) : default,
|
||||
Extensions = ObjectFeatures.Detect(raw),
|
||||
ContextNamespaces = ObjectFeatures.Namespaces(raw, record?.ActivityContext),
|
||||
Extensions = record?.Extensions ?? ObjectFeatures.Detect(raw),
|
||||
ContextNamespaces = record?.ContextNamespaces ?? ObjectFeatures.Namespaces(raw, record?.ActivityContext),
|
||||
Raw = raw,
|
||||
RawBytes = record?.RawBytes ?? 0,
|
||||
RawHash = record?.RawHash,
|
||||
@@ -118,6 +119,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
public bool Refetched { get; set; }
|
||||
public ProvenanceActivity Activity { get; set; }
|
||||
public string Inbox { get; set; }
|
||||
public string FetchedBy { get; set; }
|
||||
public ProvenanceSignature Signature { get; set; }
|
||||
public string ReceivedAt { get; set; }
|
||||
public string Published { get; set; }
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||
using PrivaPub.Api.Mastodon.Mappers;
|
||||
using PrivaPub.Domain.Privacy;
|
||||
using PrivaPub.Domain.Social;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Models.Social;
|
||||
using PrivaPub.StaticServices;
|
||||
|
||||
using PostEntity = PrivaPub.Models.Post.Post;
|
||||
|
||||
namespace PrivaPub.Api.Mastodon.Controllers
|
||||
{
|
||||
public class ReactionsController : MastodonController
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly MastodonMapper _mapper;
|
||||
readonly IReactions _reactions;
|
||||
|
||||
public ReactionsController(DbEntities dbEntities, MastodonMapper mapper, IReactions reactions)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_mapper = mapper;
|
||||
_reactions = reactions;
|
||||
}
|
||||
|
||||
[HttpGet("/api/v1/pleroma/statuses/{id}/reactions"), Scope("read:statuses", requiresUser: false), AllowAnonymous]
|
||||
public async Task<IActionResult> List(string id, CancellationToken token)
|
||||
{
|
||||
var post = await Visible(id, token);
|
||||
if (post == default)
|
||||
return NotFoundError();
|
||||
var reactions = await DB.Default.Find<Reaction>().Match(r => r.PostId == post.ID).ExecuteAsync(token);
|
||||
var accounts = await _mapper.Accounts(reactions.Select(r => r.AccountId), token);
|
||||
return Json(reactions.GroupBy(r => r.Emoji).Select(group => new
|
||||
{
|
||||
name = group.Key,
|
||||
count = group.Count(),
|
||||
me = MyId != default && group.Any(r => r.AccountId == MyId),
|
||||
url = _mapper.ProxiedUrl(group.First().EmojiURL),
|
||||
accounts = group.Select(r => accounts.GetValueOrDefault(r.AccountId)).Where(a => a != default).ToList()
|
||||
}).OrderByDescending(r => r.count).ToList());
|
||||
}
|
||||
|
||||
[HttpPut("/api/v1/pleroma/statuses/{id}/reactions/{emoji}"), Scope("write:favourites")]
|
||||
public async Task<IActionResult> React(string id, string emoji, CancellationToken token)
|
||||
{
|
||||
var post = await Visible(id, token);
|
||||
if (post == default)
|
||||
return NotFoundError();
|
||||
if (!await _reactions.React(Me, post, Uri.UnescapeDataString(emoji), token))
|
||||
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: That emoji cannot be used here");
|
||||
return Json(await _mapper.Status(post, MyId, token));
|
||||
}
|
||||
|
||||
[HttpDelete("/api/v1/pleroma/statuses/{id}/reactions/{emoji}"), Scope("write:favourites")]
|
||||
public async Task<IActionResult> Unreact(string id, string emoji, CancellationToken token)
|
||||
{
|
||||
var post = await Visible(id, token);
|
||||
if (post == default)
|
||||
return NotFoundError();
|
||||
await _reactions.Unreact(Me, post, Uri.UnescapeDataString(emoji), token);
|
||||
return Json(await _mapper.Status(post, MyId, token));
|
||||
}
|
||||
|
||||
async Task<PostEntity> Visible(string id, CancellationToken token)
|
||||
{
|
||||
var post = await _dbEntities.Posts.Match(p => p.ID == id && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
|
||||
return post != default && post.Visibility != PostVisibility.LocalGeo && await VisibilityPolicy.CanSee(post, MyId, token) ? post : default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,9 @@ using PrivaPub.Api.Mastodon.Mappers;
|
||||
using PrivaPub.Domain.Privacy;
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Models.User;
|
||||
using PrivaPub.Federation.Rendering;
|
||||
using PrivaPub.Federation.Outbox;
|
||||
using PrivaPub.StaticServices;
|
||||
|
||||
using PostEntity = PrivaPub.Models.Post.Post;
|
||||
@@ -24,9 +27,14 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
readonly MastodonMapper _mapper;
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly IMemoryCache _cache;
|
||||
readonly IQuoteService _quotes;
|
||||
readonly IOutboxPublisher _outbox;
|
||||
|
||||
public StatusesController(IStatusService statuses, MastodonMapper mapper, DbEntities dbEntities, IMemoryCache cache)
|
||||
public StatusesController(IStatusService statuses, MastodonMapper mapper, DbEntities dbEntities, IMemoryCache cache, IQuoteService quotes,
|
||||
IOutboxPublisher outbox)
|
||||
{
|
||||
_quotes = quotes;
|
||||
_outbox = outbox;
|
||||
_statuses = statuses;
|
||||
_mapper = mapper;
|
||||
_dbEntities = dbEntities;
|
||||
@@ -44,9 +52,6 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
if (existing != default)
|
||||
return Json(await _mapper.Status(existing, MyId, token));
|
||||
}
|
||||
if (Params.Has("poll[options]"))
|
||||
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: Polls are not supported yet");
|
||||
|
||||
var outcome = await _statuses.Publish(Me, new StatusDraft
|
||||
{
|
||||
Text = Params.Get("status"),
|
||||
@@ -56,7 +61,13 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
Visibility = Visibility(Params.Get("visibility") ?? Me.Settings.DefaultVisibility),
|
||||
InReplyTo = Params.Get("in_reply_to_id"),
|
||||
Language = Params.Get("language") ?? Me.Settings.DefaultLanguage,
|
||||
MediaIds = Params.List("media_ids")
|
||||
MediaIds = Params.List("media_ids"),
|
||||
QuotedStatusId = Params.Get("quoted_status_id"),
|
||||
QuotePolicy = Params.Get("quote_approval_policy"),
|
||||
Poll = Params.Has("poll[options]")
|
||||
? new PollDraft(Params.List("poll[options]"), Params.Int("poll[expires_in]") ?? 0, Params.Bool("poll[multiple]") == true,
|
||||
Params.Bool("poll[hide_totals]") == true)
|
||||
: default
|
||||
}, token);
|
||||
if (!outcome.Ok)
|
||||
return Error(outcome.Status, outcome.Error);
|
||||
@@ -225,6 +236,47 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
return Json(reblogs.Select(r => accounts.GetValueOrDefault(MastodonMapper.AuthorOf(r))).Where(a => a != default).ToList());
|
||||
}
|
||||
|
||||
[HttpGet("/api/v1/statuses/{id}/quotes"), Scope("read:statuses", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||
public async Task<IActionResult> Quotes(string id, CancellationToken token)
|
||||
{
|
||||
if (await Visible(id, token) == default)
|
||||
return NotFoundError();
|
||||
var query = _dbEntities.Posts.Match(p => p.QuotedPostId == id && p.QuoteState == QuoteState.Accepted && !p.DeletedAt.HasValue);
|
||||
var quotes = await Page.From(Params, Limit()).Fetch(query, p => p.ID, token);
|
||||
var visible = new List<PostEntity>();
|
||||
foreach (var quote in quotes)
|
||||
if (quote.Visibility != PostVisibility.LocalGeo && await VisibilityPolicy.CanSee(quote, MyId, token))
|
||||
visible.Add(quote);
|
||||
Link($"/api/v1/statuses/{id}/quotes", quotes.LastOrDefault()?.ID, quotes.FirstOrDefault()?.ID);
|
||||
return Json(await _mapper.Statuses(visible, MyId, token));
|
||||
}
|
||||
|
||||
[HttpPost("/api/v1/statuses/{id}/quotes/{quotingId}/revoke"), Scope("write:statuses")]
|
||||
public async Task<IActionResult> RevokeQuote(string id, string quotingId, CancellationToken token)
|
||||
{
|
||||
var quoted = await _dbEntities.Posts.Match(p => p.ID == id && p.GroupUserId == MyId && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
var quoting = quoted == default ? default : await _dbEntities.Posts.Match(p => p.ID == quotingId && p.QuotedPostId == quoted.ID).ExecuteFirstAsync(token);
|
||||
if (quoting == default || !await _quotes.RevokeLicence(quoted, quoting, token))
|
||||
return NotFoundError();
|
||||
return Json(await _mapper.Status(await _dbEntities.Posts.MatchID(quoting.ID).ExecuteFirstAsync(token), MyId, token));
|
||||
}
|
||||
|
||||
[HttpPut("/api/v1/statuses/{id}/interaction_policy"), Scope("write:statuses")]
|
||||
public async Task<IActionResult> InteractionPolicy(string id, CancellationToken token)
|
||||
{
|
||||
var post = await _dbEntities.Posts.Match(p => p.ID == id && p.GroupUserId == MyId && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
if (post == default)
|
||||
return NotFoundError();
|
||||
var policy = Params.Get("quote_approval_policy");
|
||||
if (!QuotePolicies.IsKnown(policy))
|
||||
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: Unknown quote approval policy");
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(p => p.LocalQuotePolicy, policy).ExecuteAsync(token);
|
||||
post.LocalQuotePolicy = policy;
|
||||
if (!post.IsLocalOnly)
|
||||
await _outbox.Publish(Me, post, ActivityPubRenderer.UpdateOf(post, Me, $"policy-{DateTime.UtcNow.Ticks}"), token);
|
||||
return Json(await _mapper.Status(post, MyId, token));
|
||||
}
|
||||
|
||||
[HttpPost("/api/v1/statuses/{id}/bookmark"), Scope("write:bookmarks")]
|
||||
public async Task<IActionResult> Bookmark(string id, CancellationToken token)
|
||||
{
|
||||
|
||||
@@ -162,7 +162,10 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
[NotificationType.FollowRequest] = "follow_request",
|
||||
[NotificationType.Favourite] = "favourite",
|
||||
[NotificationType.Reblog] = "reblog",
|
||||
[NotificationType.Update] = "update"
|
||||
[NotificationType.Update] = "update",
|
||||
[NotificationType.Poll] = "poll",
|
||||
[NotificationType.Reaction] = "pleroma:emoji_reaction",
|
||||
[NotificationType.Quote] = "quote"
|
||||
};
|
||||
|
||||
readonly MastodonMapper _mapper;
|
||||
@@ -233,7 +236,9 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
CreatedAt = MastodonJson.Time(n.CreatedAt),
|
||||
GroupKey = $"ungrouped-{n.ID}",
|
||||
Account = accounts[n.FromAccountId],
|
||||
Status = n.PostId == default ? default : statuses[n.PostId]
|
||||
Status = n.PostId == default ? default : statuses[n.PostId],
|
||||
Emoji = n.Emoji == default ? default : n.EmojiURL == default ? n.Emoji : $":{n.Emoji}:",
|
||||
EmojiUrl = _mapper.ProxiedUrl(n.EmojiURL)
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
@@ -25,12 +25,24 @@ namespace PrivaPub.Api.Mastodon.Entities
|
||||
public string LastStatusAt { get; set; }
|
||||
public bool? HideCollections { get; set; }
|
||||
public bool Noindex { get; set; } = true;
|
||||
public List<object> Emojis { get; set; } = new();
|
||||
public List<CustomEmojiEntity> Emojis { get; set; } = new();
|
||||
public List<object> Roles { get; set; } = new();
|
||||
public bool? Memorial { get; set; }
|
||||
public string AvatarDescription { get; set; } = string.Empty;
|
||||
public string HeaderDescription { get; set; } = string.Empty;
|
||||
public List<Field> Fields { get; set; } = new();
|
||||
public Source Source { get; set; }
|
||||
}
|
||||
|
||||
public class CustomEmojiEntity
|
||||
{
|
||||
public string Shortcode { get; set; }
|
||||
public string Url { get; set; }
|
||||
public string StaticUrl { get; set; }
|
||||
public bool VisibleInPicker { get; set; }
|
||||
public string Category { get; set; }
|
||||
}
|
||||
|
||||
public class Field
|
||||
{
|
||||
public string Name { get; set; }
|
||||
@@ -47,6 +59,7 @@ namespace PrivaPub.Api.Mastodon.Entities
|
||||
public List<Field> Fields { get; set; } = new();
|
||||
public int FollowRequestsCount { get; set; }
|
||||
public string AttributionDomains { get; set; }
|
||||
public string QuotePolicy { get; set; } = "public";
|
||||
}
|
||||
|
||||
public class PrivaPubStatus
|
||||
@@ -149,6 +162,54 @@ namespace PrivaPub.Api.Mastodon.Entities
|
||||
public int Down { get; set; }
|
||||
}
|
||||
|
||||
public class QuoteEntity
|
||||
{
|
||||
public string State { get; set; }
|
||||
public Status QuotedStatus { get; set; }
|
||||
public string QuotedStatusId { get; set; }
|
||||
}
|
||||
|
||||
public class QuoteApprovalEntity
|
||||
{
|
||||
public List<string> Automatic { get; set; } = new();
|
||||
public List<string> Manual { get; set; } = new();
|
||||
public string CurrentUser { get; set; } = "denied";
|
||||
}
|
||||
|
||||
public class EmojiReactionEntity
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public int Count { get; set; }
|
||||
public bool Me { get; set; }
|
||||
public string Url { get; set; }
|
||||
public string StaticUrl { get; set; }
|
||||
}
|
||||
|
||||
public class PleromaStatus
|
||||
{
|
||||
public List<EmojiReactionEntity> EmojiReactions { get; set; } = new();
|
||||
}
|
||||
|
||||
public class PollEntity
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string ExpiresAt { get; set; }
|
||||
public bool Expired { get; set; }
|
||||
public bool Multiple { get; set; }
|
||||
public int VotesCount { get; set; }
|
||||
public int? VotersCount { get; set; }
|
||||
public bool? Voted { get; set; }
|
||||
public List<int> OwnVotes { get; set; }
|
||||
public List<PollOptionEntity> Options { get; set; } = new();
|
||||
public List<CustomEmojiEntity> Emojis { get; set; } = new();
|
||||
}
|
||||
|
||||
public class PollOptionEntity
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public int? VotesCount { get; set; }
|
||||
}
|
||||
|
||||
public class PreviewCard
|
||||
{
|
||||
public string Url { get; set; }
|
||||
@@ -183,7 +244,7 @@ namespace PrivaPub.Api.Mastodon.Entities
|
||||
public string Content { get; set; } = string.Empty;
|
||||
public string CreatedAt { get; set; }
|
||||
public string EditedAt { get; set; }
|
||||
public List<object> Emojis { get; set; } = new();
|
||||
public List<CustomEmojiEntity> Emojis { get; set; } = new();
|
||||
public int RepliesCount { get; set; }
|
||||
public int ReblogsCount { get; set; }
|
||||
public int FavouritesCount { get; set; }
|
||||
@@ -199,12 +260,17 @@ namespace PrivaPub.Api.Mastodon.Entities
|
||||
public List<StatusMention> Mentions { get; set; } = new();
|
||||
public List<StatusTag> Tags { get; set; } = new();
|
||||
public PreviewCard Card { get; set; }
|
||||
public object Poll { get; set; }
|
||||
public PollEntity Poll { get; set; }
|
||||
public Application Application { get; set; }
|
||||
public string Language { get; set; }
|
||||
public string Text { get; set; }
|
||||
public List<object> Filtered { get; set; } = new();
|
||||
public PrivaPubStatus Privapub { get; set; }
|
||||
public List<EmojiReactionEntity> EmojiReactions { get; set; } = new();
|
||||
public PleromaStatus Pleroma { get; set; }
|
||||
public QuoteEntity Quote { get; set; }
|
||||
public int QuotesCount { get; set; }
|
||||
public QuoteApprovalEntity QuoteApproval { get; set; }
|
||||
}
|
||||
|
||||
public class MediaAttachment
|
||||
@@ -273,6 +339,8 @@ namespace PrivaPub.Api.Mastodon.Entities
|
||||
public string GroupKey { get; set; }
|
||||
public Account Account { get; set; }
|
||||
public Status Status { get; set; }
|
||||
public string Emoji { get; set; }
|
||||
public string EmojiUrl { get; set; }
|
||||
}
|
||||
|
||||
public class Context
|
||||
@@ -289,7 +357,7 @@ namespace PrivaPub.Api.Mastodon.Entities
|
||||
public string CreatedAt { get; set; }
|
||||
public Account Account { get; set; }
|
||||
public List<MediaAttachment> MediaAttachments { get; set; } = new();
|
||||
public List<object> Emojis { get; set; } = new();
|
||||
public List<CustomEmojiEntity> Emojis { get; set; } = new();
|
||||
}
|
||||
|
||||
public class StatusSource
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PrivaPub.Api.Mastodon.Infrastructure
|
||||
{
|
||||
public static class MastodonErrors
|
||||
{
|
||||
public static IApplicationBuilder UseMastodonErrorBodies(this IApplicationBuilder app) =>
|
||||
app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), api => api.UseStatusCodePages(async pages =>
|
||||
{
|
||||
var response = pages.HttpContext.Response;
|
||||
response.ContentType = "application/json; charset=utf-8";
|
||||
await response.WriteAsync(JsonSerializer.Serialize(new { error = Message(response.StatusCode) }, MastodonJson.Options));
|
||||
}));
|
||||
|
||||
public static string Message(int status) => status switch
|
||||
{
|
||||
StatusCodes.Status401Unauthorized => "The access token is invalid",
|
||||
StatusCodes.Status403Forbidden => "This action is outside the authorized scopes",
|
||||
StatusCodes.Status404NotFound => "Record not found",
|
||||
StatusCodes.Status405MethodNotAllowed => "Method not allowed",
|
||||
StatusCodes.Status413PayloadTooLarge => "Payload too large",
|
||||
StatusCodes.Status415UnsupportedMediaType => "Unsupported media type",
|
||||
StatusCodes.Status422UnprocessableEntity => "Validation failed",
|
||||
StatusCodes.Status429TooManyRequests => "Too many requests",
|
||||
>= 500 => "Something went wrong.",
|
||||
_ => "Bad request"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,10 @@ using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Rendering;
|
||||
using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.Group;
|
||||
using PrivaPub.Domain.Privacy;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Models.Social;
|
||||
using PrivaPub.Models.User;
|
||||
using PrivaPub.StaticServices;
|
||||
@@ -35,6 +38,8 @@ namespace PrivaPub.Api.Mastodon.Mappers
|
||||
|
||||
string Proxied(string url) => _proxy == default ? url : _proxy.Wrap(url);
|
||||
|
||||
public string ProxiedUrl(string url) => Proxied(url);
|
||||
|
||||
string MissingAvatar => $"{_localActors.BaseAddress}/media/missing-avatar.png";
|
||||
string MissingHeader => $"{_localActors.BaseAddress}/media/missing-header.png";
|
||||
|
||||
@@ -88,6 +93,7 @@ namespace PrivaPub.Api.Mastodon.Mappers
|
||||
Privacy = actor.Settings.DefaultVisibility ?? "public",
|
||||
Sensitive = actor.Settings.DefaultSensitive,
|
||||
Language = actor.Settings.DefaultLanguage,
|
||||
QuotePolicy = actor.Settings.QuotePolicy ?? QuotePolicies.Public,
|
||||
Note = actor.Summary ?? string.Empty,
|
||||
Fields = actor.Fields.Select(f => new Field { Name = f.Key, Value = f.Value }).ToList(),
|
||||
FollowRequestsCount = (int)await DB.Default.CountAsync<Follower>(f => f.LocalActorId == actor.Id && !f.IsAccepted, token)
|
||||
@@ -103,8 +109,15 @@ namespace PrivaPub.Api.Mastodon.Mappers
|
||||
DisplayName = foreign.Name ?? foreign.UserName ?? string.Empty,
|
||||
Bot = foreign.AvatarType is AvatarType.Service or AvatarType.Application,
|
||||
Group = foreign.AvatarType == AvatarType.Group,
|
||||
Locked = foreign.IsLocked,
|
||||
Discoverable = foreign.IsDiscoverable,
|
||||
CreatedAt = MastodonJson.Day(foreign.CreatedAt),
|
||||
Indexable = foreign.IsIndexable,
|
||||
Memorial = foreign.IsMemorial ? true : default(bool?),
|
||||
CreatedAt = MastodonJson.Day(foreign.Published ?? foreign.CreatedAt),
|
||||
Emojis = Emojis(foreign.Emojis),
|
||||
Fields = foreign.Fields.Select(f => new Field { Name = f.Key, Value = f.Value }).ToList(),
|
||||
AvatarDescription = foreign.PictureDescription ?? string.Empty,
|
||||
HeaderDescription = foreign.HeaderDescription ?? string.Empty,
|
||||
Note = foreign.Biography ?? string.Empty,
|
||||
Url = foreign.Url ?? foreign.ActorURI,
|
||||
Uri = foreign.ActorURI,
|
||||
@@ -136,7 +149,10 @@ namespace PrivaPub.Api.Mastodon.Mappers
|
||||
public async Task<Status> Status(PostEntity post, string viewerId, CancellationToken token) =>
|
||||
(await Statuses(new[] { post }, viewerId, token)).FirstOrDefault();
|
||||
|
||||
public async Task<List<Status>> Statuses(IReadOnlyCollection<PostEntity> posts, string viewerId, CancellationToken token)
|
||||
public Task<List<Status>> Statuses(IReadOnlyCollection<PostEntity> posts, string viewerId, CancellationToken token) =>
|
||||
Statuses(posts, viewerId, nested: false, token);
|
||||
|
||||
async Task<List<Status>> Statuses(IReadOnlyCollection<PostEntity> posts, string viewerId, bool nested, CancellationToken token)
|
||||
{
|
||||
var originalIds = posts.Where(p => p.ReblogOfPostId != default).Select(p => p.ReblogOfPostId).Distinct().ToList();
|
||||
var originals = originalIds.Count == 0
|
||||
@@ -158,6 +174,24 @@ namespace PrivaPub.Api.Mastodon.Mappers
|
||||
? new HashSet<string>()
|
||||
: (await DB.Default.Find<Bookmark>().Match(b => b.AvatarId == viewerId && ids.Contains(b.PostId)).ExecuteAsync(token)).Select(b => b.PostId).ToHashSet();
|
||||
var pinned = (await DB.Default.Find<Pin>().Match(p => ids.Contains(p.PostId)).ExecuteAsync(token)).Select(p => p.PostId).ToHashSet();
|
||||
var reactions = (await DB.Default.Find<Reaction>().Match(r => ids.Contains(r.PostId)).ExecuteAsync(token))
|
||||
.GroupBy(r => r.PostId)
|
||||
.ToDictionary(g => g.Key, g => g.GroupBy(r => r.Emoji)
|
||||
.Select(e => new EmojiReactionEntity
|
||||
{
|
||||
Name = e.Key,
|
||||
Count = e.Count(),
|
||||
Me = viewerId != default && e.Any(r => r.AccountId == viewerId),
|
||||
Url = Proxied(e.First().EmojiURL),
|
||||
StaticUrl = Proxied(e.First().EmojiURL)
|
||||
})
|
||||
.OrderByDescending(e => e.Count)
|
||||
.ToList());
|
||||
var pollIds = all.Where(p => p.Poll != default).Select(p => p.ID).ToList();
|
||||
var ownVotes = viewerId == default || pollIds.Count == 0
|
||||
? new Dictionary<string, List<int>>()
|
||||
: (await DB.Default.Find<PollVote>().Match(v => pollIds.Contains(v.PostId) && v.VoterAccountId == viewerId && v.IsLocalVoter).ExecuteAsync(token))
|
||||
.GroupBy(v => v.PostId).ToDictionary(g => g.Key, g => g.Select(v => v.Choice).ToList());
|
||||
var hidden = viewerId == default
|
||||
? new HashSet<string>()
|
||||
: await Domain.Relationships.Hidden.AuthorsHiddenFrom(viewerId, all.Select(p => p.ActorURI), forNotifications: false, token);
|
||||
@@ -192,15 +226,47 @@ namespace PrivaPub.Api.Mastodon.Mappers
|
||||
SpoilerText = post.SpoilerText ?? (post.HasContentWarning ? post.Title ?? ActivityPubRenderer.ContentWarning : string.Empty),
|
||||
Visibility = Visibility(post.Visibility),
|
||||
Language = post.Language,
|
||||
MediaAttachments = post.Media.Select(Media).ToList(),
|
||||
MediaAttachments = post.Media.Count > 0 ? post.Media.Select(Media).ToList() : Playable(post),
|
||||
Card = Card(post),
|
||||
Poll = Poll(post, viewerId, ownVotes.GetValueOrDefault(post.ID)),
|
||||
Emojis = Emojis(post.Emojis),
|
||||
Privapub = Extension(post),
|
||||
EmojiReactions = reactions.GetValueOrDefault(post.ID) ?? new(),
|
||||
Pleroma = new PleromaStatus { EmojiReactions = reactions.GetValueOrDefault(post.ID) ?? new() },
|
||||
Mentions = post.Mentions.Select(m => Mention(m, mentionAccounts)).Where(m => m != default).ToList(),
|
||||
Tags = post.Tags.Select(t => new StatusTag { Name = t, Url = ActivityPubRenderer.TagUrl(_localActors.BaseAddress, t) }).ToList()
|
||||
};
|
||||
return status;
|
||||
}
|
||||
|
||||
var localAuthors = all.Where(p => !p.IsFederatedCopy && p.GroupUserId != default).Select(p => p.GroupUserId).Distinct().ToList();
|
||||
var followsAuthor = viewerId == default || localAuthors.Count == 0
|
||||
? new HashSet<string>()
|
||||
: await FollowedLocalAuthors(viewerId, localAuthors, token);
|
||||
var quotedIds = nested ? new List<string>() : all.Where(p => p.QuoteState == QuoteState.Accepted && p.QuotedPostId != default).Select(p => p.QuotedPostId).Distinct().ToList();
|
||||
var quotedPosts = new List<PostEntity>();
|
||||
foreach (var quotedPost in quotedIds.Count == 0 ? new List<PostEntity>() : await _dbEntities.Posts.Match(p => quotedIds.Contains(p.ID) && !p.DeletedAt.HasValue).ExecuteAsync(token))
|
||||
if (quotedPost.Visibility != PostVisibility.LocalGeo && await VisibilityPolicy.CanSee(quotedPost, viewerId, token))
|
||||
quotedPosts.Add(quotedPost);
|
||||
var quotedStatuses = quotedPosts.Count == 0
|
||||
? new Dictionary<string, Status>()
|
||||
: (await Statuses(quotedPosts, viewerId, nested: true, token)).ToDictionary(q => q.Id);
|
||||
|
||||
void Quote(Status status, PostEntity post)
|
||||
{
|
||||
status.QuotesCount = post.QuotesCount;
|
||||
status.QuoteApproval = post.IsFederatedCopy ? Approval(post, viewerId) : LocalApproval(post, viewerId, followsAuthor.Contains(post.GroupUserId));
|
||||
if (post.QuoteState == QuoteState.None)
|
||||
return;
|
||||
var state = post.QuoteState.ToString().ToLowerInvariant();
|
||||
var quoted = post.QuotedPostId != default && quotedStatuses.TryGetValue(post.QuotedPostId, out var shown) ? shown : default;
|
||||
status.Quote = nested
|
||||
? new QuoteEntity { State = state, QuotedStatusId = post.QuotedPostId }
|
||||
: new QuoteEntity { State = state, QuotedStatus = post.QuoteState == QuoteState.Accepted ? quoted : default, QuotedStatusId = post.QuotedPostId };
|
||||
if (!nested && post.QuoteState == QuoteState.Accepted && quoted != default)
|
||||
status.Content = WithoutQuoteFallback(status.Content);
|
||||
}
|
||||
|
||||
var mapped = new List<Status>();
|
||||
foreach (var post in posts)
|
||||
{
|
||||
@@ -210,10 +276,12 @@ namespace PrivaPub.Api.Mastodon.Mappers
|
||||
var status = Map(post);
|
||||
if (status == default)
|
||||
continue;
|
||||
Quote(status, post);
|
||||
if (post.ReblogOfPostId != default)
|
||||
{
|
||||
if (!originals.TryGetValue(post.ReblogOfPostId, out var original) || Map(original) is not { } inner)
|
||||
continue;
|
||||
Quote(inner, original);
|
||||
status.Reblog = inner;
|
||||
status.Content = string.Empty;
|
||||
status.Reblogged = reblogged.Contains(original.ID);
|
||||
@@ -225,6 +293,54 @@ namespace PrivaPub.Api.Mastodon.Mappers
|
||||
|
||||
public static string AuthorOf(PostEntity post) => post.AuthorAccountId ?? post.GroupUserId;
|
||||
|
||||
static readonly AngleSharp.Html.Parser.HtmlParser Fragments = new();
|
||||
|
||||
public static string WithoutQuoteFallback(string html)
|
||||
{
|
||||
if (string.IsNullOrEmpty(html) || !html.Contains("quote-inline", StringComparison.Ordinal))
|
||||
return html;
|
||||
var document = Fragments.ParseDocument($"<body>{html}</body>");
|
||||
foreach (var fallback in document.QuerySelectorAll(".quote-inline").ToList())
|
||||
fallback.Remove();
|
||||
return document.Body!.InnerHtml;
|
||||
}
|
||||
|
||||
async Task<HashSet<string>> FollowedLocalAuthors(string viewerId, List<string> authorIds, CancellationToken token)
|
||||
{
|
||||
var uris = new Dictionary<string, string>();
|
||||
foreach (var avatar in await _dbEntities.Avatars.Match(a => authorIds.Contains(a.ID)).ExecuteAsync(token))
|
||||
uris[_localActors.FromAvatar(avatar).Uri] = avatar.ID;
|
||||
var targets = uris.Keys.ToList();
|
||||
return (await _dbEntities.Followings.Match(f => f.AvatarId == viewerId && targets.Contains(f.TargetActorURI) && f.State == FollowState.Accepted).ExecuteAsync(token))
|
||||
.Select(f => uris[f.TargetActorURI]).ToHashSet();
|
||||
}
|
||||
|
||||
static QuoteApprovalEntity LocalApproval(PostEntity post, string viewerId, bool viewerFollows)
|
||||
{
|
||||
var policy = ActivityPubRenderer.QuotableBy(post);
|
||||
var everyone = policy switch { QuotePolicies.Public => new List<string> { "public" }, QuotePolicies.Followers => new List<string> { "followers" }, _ => new List<string>() };
|
||||
var current = viewerId == default ? "unknown"
|
||||
: viewerId == post.GroupUserId && post.Visibility is PostVisibility.Public or PostVisibility.Unlisted ? "automatic"
|
||||
: policy == QuotePolicies.Public || policy == QuotePolicies.Followers && viewerFollows ? "automatic"
|
||||
: "denied";
|
||||
return new QuoteApprovalEntity { Automatic = everyone, CurrentUser = current };
|
||||
}
|
||||
|
||||
static QuoteApprovalEntity Approval(PostEntity post, string viewerId)
|
||||
{
|
||||
if (!post.IsFederatedCopy || post.Visibility is not (PostVisibility.Public or PostVisibility.Unlisted))
|
||||
return new QuoteApprovalEntity();
|
||||
if (post.QuotePolicy is not { } policy)
|
||||
return new QuoteApprovalEntity { Automatic = new List<string> { "public" }, CurrentUser = viewerId == default ? "unknown" : "automatic" };
|
||||
static List<string> Named(IEnumerable<string> who) => who.Select(w => Addressing.IsPublic(w) ? "public" : w.EndsWith("/followers") ? "followers" : "unsupported_policy").Distinct().ToList();
|
||||
return new QuoteApprovalEntity
|
||||
{
|
||||
Automatic = Named(policy.Automatic),
|
||||
Manual = Named(policy.Manual),
|
||||
CurrentUser = viewerId == default ? "unknown" : policy.Automatic.Any(Addressing.IsPublic) ? "automatic" : policy.Manual.Any(Addressing.IsPublic) ? "manual" : "denied"
|
||||
};
|
||||
}
|
||||
|
||||
public static string Content(PostEntity post)
|
||||
{
|
||||
var content = post.ContentHtml ?? ActivityPubRenderer.Html(post.Text);
|
||||
@@ -270,6 +386,30 @@ namespace PrivaPub.Api.Mastodon.Mappers
|
||||
}
|
||||
}
|
||||
|
||||
public PollEntity Poll(PostEntity post, string viewerId, IReadOnlyCollection<int> ownVotes)
|
||||
{
|
||||
if (post.Poll is not { } poll)
|
||||
return default;
|
||||
var expired = PollService.Ended(poll);
|
||||
var hidden = poll.HideTotals && !expired && post.GroupUserId != viewerId;
|
||||
return new PollEntity
|
||||
{
|
||||
Id = post.ID,
|
||||
ExpiresAt = poll.ExpiresAt is { } ends ? MastodonJson.Time(ends) : default,
|
||||
Expired = expired,
|
||||
Multiple = poll.Multiple,
|
||||
VotesCount = poll.Options.Sum(o => o.Votes),
|
||||
VotersCount = poll.Multiple ? poll.VotersCount : poll.VotersCount ?? poll.Options.Sum(o => o.Votes),
|
||||
Voted = viewerId == default ? default(bool?) : ownVotes?.Count > 0 || post.GroupUserId == viewerId,
|
||||
OwnVotes = viewerId == default ? default : ownVotes?.ToList() ?? new List<int>(),
|
||||
Options = poll.Options.Select(o => new PollOptionEntity { Title = o.Title, VotesCount = hidden ? default(int?) : o.Votes }).ToList(),
|
||||
Emojis = Emojis(post.Emojis.Where(e => poll.Options.Any(o => o.Title.Contains($":{e.Shortcode}:"))))
|
||||
};
|
||||
}
|
||||
|
||||
List<CustomEmojiEntity> Emojis(IEnumerable<CustomEmoji> emojis) =>
|
||||
emojis?.Select(e => new CustomEmojiEntity { Shortcode = e.Shortcode, Url = Proxied(e.URL), StaticUrl = Proxied(e.URL) }).ToList() ?? new();
|
||||
|
||||
PrivaPubStatus Extension(PostEntity post) => new()
|
||||
{
|
||||
ObjectType = post.ObjectType,
|
||||
@@ -331,13 +471,42 @@ namespace PrivaPub.Api.Mastodon.Mappers
|
||||
Streams = variant.Streams
|
||||
};
|
||||
|
||||
List<MediaAttachment> Playable(PostEntity post)
|
||||
{
|
||||
var (variant, kind, duration) = post.Video is { } video ? (BestVideo(video.Variants), "video", video.DurationSeconds)
|
||||
: post.Audio is { } audio ? (audio.Variants.FirstOrDefault(), "audio", audio.DurationSeconds)
|
||||
: default;
|
||||
if (variant == default)
|
||||
return new List<MediaAttachment>();
|
||||
return new List<MediaAttachment>
|
||||
{
|
||||
Media(new PostMedia
|
||||
{
|
||||
Id = new Guid(System.Security.Cryptography.MD5.HashData(System.Text.Encoding.UTF8.GetBytes(variant.Url))),
|
||||
RemoteURL = variant.Url,
|
||||
ContentType = variant.MediaType ?? kind + "/*",
|
||||
RemotePreviewURL = post.CoverURL,
|
||||
Width = variant.Width,
|
||||
Height = variant.Height,
|
||||
DurationSeconds = duration,
|
||||
Description = post.Title
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
public static MediaVariant BestVideo(IReadOnlyCollection<MediaVariant> variants)
|
||||
{
|
||||
var playable = variants.Where(v => v.MediaType?.StartsWith("video/") == true && (v.Streams.Count == 0 || v.Streams.Contains("audio") && v.Streams.Contains("video"))).ToList();
|
||||
return playable.Where(v => v.Height <= 720).OrderByDescending(v => v.Height).FirstOrDefault()
|
||||
?? playable.OrderBy(v => v.Height ?? int.MaxValue).FirstOrDefault();
|
||||
}
|
||||
|
||||
PreviewCard Card(PostEntity post)
|
||||
{
|
||||
if (!post.IsFederatedCopy)
|
||||
return default;
|
||||
if (post.Link is { } link)
|
||||
return Card(link.Href, link.Title ?? post.Title, link.Description ?? (post.Media.Count == 0 ? post.Excerpt : default), link.ImageURL);
|
||||
if (post.ObjectType is "Article" or "Event" or "Video" or "Audio" && post.Url != default && post.Media.Count == 0)
|
||||
if (post.IsFederatedCopy && post.ObjectType is "Article" or "Event" && post.Url != default && post.Media.Count == 0
|
||||
|| post.IsFederatedCopy && post.ObjectType is "Video" or "Audio" && post.Url != default && post.Media.Count == 0 && Playable(post).Count == 0)
|
||||
return Card(post.Url, post.Title, post.Excerpt, post.CoverURL);
|
||||
return default;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
using AngleSharp.Html.Parser;
|
||||
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Models.Statistics;
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Infrastructure.Http;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.StaticServices;
|
||||
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using PostEntity = PrivaPub.Models.Post.Post;
|
||||
|
||||
namespace PrivaPub.Domain.Content
|
||||
{
|
||||
public class LinkPreview : Entity
|
||||
{
|
||||
public string Url { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string ImageURL { get; set; }
|
||||
public string SiteName { get; set; }
|
||||
public bool Failed { get; set; }
|
||||
public DateTime FetchedAt { get; set; } = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public interface ILinkPreviews
|
||||
{
|
||||
Task Wanted(PostEntity post, CancellationToken token);
|
||||
}
|
||||
|
||||
public class LinkPreviews : ILinkPreviews, IJobHandler
|
||||
{
|
||||
const int MaxJitterSeconds = 60;
|
||||
static readonly TimeSpan Freshness = TimeSpan.FromDays(7);
|
||||
static readonly HtmlParser Parser = new();
|
||||
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IFederationHttp _http;
|
||||
readonly IJobQueue _queue;
|
||||
readonly IOptionsMonitor<FederationOptions> _options;
|
||||
|
||||
readonly IInteractionLedger _ledger;
|
||||
|
||||
public LinkPreviews(DbEntities dbEntities, ILocalActorService localActors, IFederationHttp http, IJobQueue queue,
|
||||
IOptionsMonitor<FederationOptions> options, IInteractionLedger ledger = default)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
_http = http;
|
||||
_queue = queue;
|
||||
_options = options;
|
||||
_ledger = ledger;
|
||||
}
|
||||
|
||||
public JobKind Kind => JobKind.FetchPreview;
|
||||
public int Concurrency => 2;
|
||||
public int MaxAttempts => 1;
|
||||
public int PerHostLimit => 1;
|
||||
|
||||
public async Task Wanted(PostEntity post, CancellationToken token)
|
||||
{
|
||||
if (!_options.CurrentValue.FetchLinkPreviews || post.Visibility is not (PostVisibility.Public or PostVisibility.Unlisted) || post.IsLocalOnly
|
||||
|| post.ReblogOfPostId != default || post.Link?.Title != default || post.Link == default && post.Media.Count > 0)
|
||||
return;
|
||||
var url = post.Link?.Href ?? FirstLink(post, _localActors.BaseAddress);
|
||||
if (url == default)
|
||||
return;
|
||||
await _queue.EnqueueMany(new[]
|
||||
{
|
||||
new Job
|
||||
{
|
||||
Kind = JobKind.FetchPreview, Payload = post.ID, Host = new Uri(url).Host.ToLowerInvariant(), DedupeKey = $"preview|{post.ID}",
|
||||
RunAt = DateTime.UtcNow.AddSeconds(RandomNumberGenerator.GetInt32(0, MaxJitterSeconds + 1))
|
||||
}
|
||||
}, token);
|
||||
}
|
||||
|
||||
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
|
||||
{
|
||||
var post = await _dbEntities.Posts.MatchID(job.Payload).ExecuteFirstAsync(token);
|
||||
if (post == default || post.DeletedAt.HasValue || post.Link?.Title != default)
|
||||
return JobOutcome.Done;
|
||||
var url = post.Link?.Href ?? FirstLink(post, _localActors.BaseAddress);
|
||||
if (url == default)
|
||||
return JobOutcome.Done;
|
||||
|
||||
var preview = await DB.Default.Find<LinkPreview>().Match(p => p.Url == url).ExecuteFirstAsync(token);
|
||||
if (preview == default || preview.FetchedAt < DateTime.UtcNow - Freshness)
|
||||
preview = await Fetch(url, token);
|
||||
if (preview == default || preview.Failed)
|
||||
return JobOutcome.Done;
|
||||
|
||||
var link = post.Link ?? new PostLink { Href = url };
|
||||
link.Title ??= preview.Title;
|
||||
link.Description ??= preview.Description;
|
||||
link.ImageURL ??= preview.ImageURL;
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(p => p.Link, link).ExecuteAsync(token);
|
||||
return JobOutcome.Done;
|
||||
}
|
||||
|
||||
async Task<LinkPreview> Fetch(string url, CancellationToken token)
|
||||
{
|
||||
var started = System.Diagnostics.Stopwatch.GetTimestamp();
|
||||
var (finalUri, html) = await _http.GetPage(url, token);
|
||||
var preview = html == default ? new LinkPreview { Url = url, Failed = true } : Read(url, finalUri, html);
|
||||
_ledger?.Record(new InteractionEvent
|
||||
{
|
||||
Channel = Interactions.Preview,
|
||||
Host = Interactions.HostOf(url),
|
||||
Outcome = html == default ? Interactions.Failed : Interactions.Ok,
|
||||
Reason = html == default ? default : preview.Title == default && preview.ImageURL == default ? "no-card" : "card",
|
||||
LatencyMs = (int)System.Diagnostics.Stopwatch.GetElapsedTime(started).TotalMilliseconds,
|
||||
Bytes = html == default ? default : System.Text.Encoding.UTF8.GetByteCount(html),
|
||||
Redirects = finalUri == default || finalUri.AbsoluteUri == url ? 0 : 1
|
||||
});
|
||||
await DB.Default.Update<LinkPreview>()
|
||||
.Match(p => p.Url == url)
|
||||
.Modify(p => p.Title, preview.Title)
|
||||
.Modify(p => p.Description, preview.Description)
|
||||
.Modify(p => p.ImageURL, preview.ImageURL)
|
||||
.Modify(p => p.SiteName, preview.SiteName)
|
||||
.Modify(p => p.Failed, preview.Failed)
|
||||
.Modify(p => p.FetchedAt, DateTime.UtcNow)
|
||||
.Option(o => o.IsUpsert = true)
|
||||
.ExecuteAsync(token);
|
||||
return preview;
|
||||
}
|
||||
|
||||
public static LinkPreview Read(string url, Uri finalUri, string html)
|
||||
{
|
||||
var document = Parser.ParseDocument(html);
|
||||
string Meta(params string[] names) => names
|
||||
.Select(name => document.QuerySelector($"meta[property='{name}'], meta[name='{name}']")?.GetAttribute("content"))
|
||||
.FirstOrDefault(value => !string.IsNullOrWhiteSpace(value));
|
||||
var image = Meta("og:image:secure_url", "og:image", "og:image:url", "twitter:image", "twitter:image:src");
|
||||
var imageUri = image != default && Uri.TryCreate(finalUri, image, out var resolved) && resolved.Scheme is "https" or "http" ? resolved.AbsoluteUri : default;
|
||||
var preview = new LinkPreview
|
||||
{
|
||||
Url = url,
|
||||
Title = ObjectShapes.Text(Meta("og:title", "twitter:title") ?? document.Title, 300),
|
||||
Description = ObjectShapes.Text(Meta("og:description", "twitter:description", "description"), 1000),
|
||||
SiteName = ObjectShapes.Text(Meta("og:site_name"), 200),
|
||||
ImageURL = imageUri
|
||||
};
|
||||
preview.Failed = preview.Title == default && preview.Description == default && preview.ImageURL == default;
|
||||
return preview;
|
||||
}
|
||||
|
||||
public static string FirstLink(PostEntity post, string ownBase)
|
||||
{
|
||||
if (string.IsNullOrEmpty(post.ContentHtml))
|
||||
return default;
|
||||
var document = Parser.ParseDocument(post.ContentHtml);
|
||||
return document.QuerySelectorAll("a[href]")
|
||||
.Where(a => !(a.ClassList.Contains("mention") || a.ClassList.Contains("hashtag") || a.GetAttribute("rel")?.Contains("tag") == true
|
||||
|| a.Closest(".quote-inline") != default))
|
||||
.Select(a => a.GetAttribute("href"))
|
||||
.FirstOrDefault(href => Uri.TryCreate(href, UriKind.Absolute, out var uri) && uri.Scheme is "https" or "http"
|
||||
&& href != post.Url && href != post.ObjectURI && !href.StartsWith(ownBase + "/", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,9 @@ namespace PrivaPub.Domain.Media
|
||||
{
|
||||
string Wrap(string remoteUrl);
|
||||
Task<(string Path, string ContentType)> Fetch(string signature, string encodedUrl, CancellationToken token);
|
||||
string Verified(string signature, string encodedUrl);
|
||||
(string Path, string ContentType) Cached(string url);
|
||||
Task<HttpResponseMessage> Open(string url, System.Net.Http.Headers.RangeHeaderValue range, CancellationToken token);
|
||||
}
|
||||
|
||||
public class MediaProxy : IMediaProxy
|
||||
@@ -43,7 +46,7 @@ namespace PrivaPub.Domain.Media
|
||||
return $"{_localActors.BaseAddress}/media/proxy/{Sign(remoteUrl)}/{encoded}";
|
||||
}
|
||||
|
||||
public async Task<(string Path, string ContentType)> Fetch(string signature, string encodedUrl, CancellationToken token)
|
||||
public string Verified(string signature, string encodedUrl)
|
||||
{
|
||||
string url;
|
||||
try
|
||||
@@ -54,19 +57,38 @@ namespace PrivaPub.Domain.Media
|
||||
{
|
||||
return default;
|
||||
}
|
||||
if (!CryptographicOperations.FixedTimeEquals(Encoding.ASCII.GetBytes(signature ?? string.Empty), Encoding.ASCII.GetBytes(Sign(url))))
|
||||
return default;
|
||||
|
||||
var name = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(url)));
|
||||
var directory = System.IO.Path.Combine(_media.ProxyRoot, name[..2]);
|
||||
var path = System.IO.Path.Combine(directory, name);
|
||||
var typePath = path + ".type";
|
||||
if (File.Exists(path) && File.Exists(typePath))
|
||||
{
|
||||
File.SetLastWriteTimeUtc(path, DateTime.UtcNow);
|
||||
return (path, await File.ReadAllTextAsync(typePath, token));
|
||||
return CryptographicOperations.FixedTimeEquals(Encoding.ASCII.GetBytes(signature ?? string.Empty), Encoding.ASCII.GetBytes(Sign(url))) ? url : default;
|
||||
}
|
||||
|
||||
public (string Path, string ContentType) Cached(string url)
|
||||
{
|
||||
var (path, typePath) = CachePaths(url);
|
||||
if (!File.Exists(path) || !File.Exists(typePath))
|
||||
return default;
|
||||
File.SetLastWriteTimeUtc(path, DateTime.UtcNow);
|
||||
return (path, File.ReadAllText(typePath));
|
||||
}
|
||||
|
||||
public Task<HttpResponseMessage> Open(string url, System.Net.Http.Headers.RangeHeaderValue range, CancellationToken token) =>
|
||||
_http.OpenMedia(url, range, token);
|
||||
|
||||
(string Path, string TypePath) CachePaths(string url)
|
||||
{
|
||||
var name = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(url)));
|
||||
var path = System.IO.Path.Combine(_media.ProxyRoot, name[..2], name);
|
||||
return (path, path + ".type");
|
||||
}
|
||||
|
||||
public async Task<(string Path, string ContentType)> Fetch(string signature, string encodedUrl, CancellationToken token)
|
||||
{
|
||||
var url = Verified(signature, encodedUrl);
|
||||
if (url == default)
|
||||
return default;
|
||||
if (Cached(url) is { Path: not null } cached)
|
||||
return cached;
|
||||
var (path, typePath) = CachePaths(url);
|
||||
var directory = System.IO.Path.GetDirectoryName(path);
|
||||
|
||||
var (bytes, contentType) = await _http.GetMedia(url, _options.CurrentValue.MaxProxiedBytes, token);
|
||||
if (bytes == default)
|
||||
return default;
|
||||
|
||||
@@ -8,9 +8,9 @@ namespace PrivaPub.Domain.Social
|
||||
public static class Notifications
|
||||
{
|
||||
public static async Task Add(string avatarId, NotificationType type, string fromAccountId, string fromActorUri, string postId,
|
||||
CancellationToken token)
|
||||
CancellationToken token, string emoji = default, string emojiUrl = default)
|
||||
{
|
||||
if (string.IsNullOrEmpty(avatarId) || avatarId == fromAccountId)
|
||||
if (string.IsNullOrEmpty(avatarId) || avatarId == fromAccountId && type != NotificationType.Poll)
|
||||
return;
|
||||
if ((await Relationships.Hidden.AuthorsHiddenFrom(avatarId, new[] { fromActorUri }, forNotifications: true, token)).Count > 0)
|
||||
return;
|
||||
@@ -23,7 +23,9 @@ namespace PrivaPub.Domain.Social
|
||||
FromAccountId = fromAccountId,
|
||||
FromActorURI = fromActorUri,
|
||||
PostId = postId,
|
||||
DedupeKey = $"{type}|{avatarId}|{fromActorUri}|{postId}"
|
||||
Emoji = emoji,
|
||||
EmojiURL = emojiUrl,
|
||||
DedupeKey = emoji == default ? $"{type}|{avatarId}|{fromActorUri}|{postId}" : $"{type}|{avatarId}|{fromActorUri}|{postId}|{emoji}"
|
||||
}, token);
|
||||
}
|
||||
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Inbox;
|
||||
using PrivaPub.Federation.Inbox.Handlers;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Federation.Outbox;
|
||||
using PrivaPub.Federation.Rendering;
|
||||
using PrivaPub.Models.Social;
|
||||
using PrivaPub.Models.User;
|
||||
using PrivaPub.StaticServices;
|
||||
|
||||
using System.Globalization;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
using PostEntity = PrivaPub.Models.Post.Post;
|
||||
|
||||
namespace PrivaPub.Domain.Social
|
||||
{
|
||||
public interface IReactions
|
||||
{
|
||||
Task Receive(PostEntity post, ForeignAvatar actor, string content, JsonNode tags, string activityId, CancellationToken token);
|
||||
Task<bool> Withdraw(ForeignAvatar actor, string activityId, string objectUri, string content, CancellationToken token);
|
||||
Task<bool> React(LocalActor me, PostEntity post, string emoji, CancellationToken token);
|
||||
Task<bool> Unreact(LocalActor me, PostEntity post, string emoji, CancellationToken token);
|
||||
}
|
||||
|
||||
public class Reactions : IReactions
|
||||
{
|
||||
static readonly string[] Hearts = { "❤", "❤️", "♥", "♥️" };
|
||||
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly IDeliveryService _delivery;
|
||||
|
||||
public Reactions(DbEntities dbEntities, IDeliveryService delivery)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_delivery = delivery;
|
||||
}
|
||||
|
||||
public static bool IsHeart(string content) => content == default || Hearts.Contains(content.Trim());
|
||||
|
||||
public async Task Receive(PostEntity post, ForeignAvatar actor, string content, JsonNode tags, string activityId, CancellationToken token)
|
||||
{
|
||||
if (!LikeHandler.MaySee(post, actor) || Normalise(content, ObjectShapes.Emojis(tags)) is not { } emoji)
|
||||
{
|
||||
Arrival.Drop(LikeHandler.MaySee(post, actor) ? "unparseable" : "not-visible");
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
await DB.Default.SaveAsync(new Reaction
|
||||
{
|
||||
PostId = post.ID, AccountId = actor.ID, ActorURI = actor.ActorURI, Emoji = emoji.Name, EmojiURL = emoji.Url, ActivityURI = activityId
|
||||
}, token);
|
||||
}
|
||||
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||
{
|
||||
Arrival.Drop("duplicate");
|
||||
return;
|
||||
}
|
||||
Arrival.Accept("reaction");
|
||||
if (!post.IsFederatedCopy)
|
||||
await Notifications.Add(post.GroupUserId, NotificationType.Reaction, actor.ID, actor.ActorURI, post.ID, token, emoji.Name, emoji.Url);
|
||||
}
|
||||
|
||||
public async Task<bool> Withdraw(ForeignAvatar actor, string activityId, string objectUri, string content, CancellationToken token)
|
||||
{
|
||||
var reaction = activityId == default
|
||||
? default
|
||||
: await DB.Default.Find<Reaction>().Match(r => r.ActivityURI == activityId && r.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
||||
if (reaction == default && objectUri != default && Normalise(content, new()) is { } emoji)
|
||||
{
|
||||
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
|
||||
reaction = post == default
|
||||
? default
|
||||
: await DB.Default.Find<Reaction>().Match(r => r.PostId == post.ID && r.ActorURI == actor.ActorURI && r.Emoji == emoji.Name).ExecuteFirstAsync(token);
|
||||
}
|
||||
if (reaction == default)
|
||||
return false;
|
||||
await DB.Default.DeleteAsync<Reaction>(reaction.ID);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> React(LocalActor me, PostEntity post, string emoji, CancellationToken token)
|
||||
{
|
||||
var known = post.Emojis.Select(e => (e.Shortcode, e.URL))
|
||||
.Concat((await DB.Default.Find<Reaction>().Match(r => r.PostId == post.ID && r.EmojiURL != null).ExecuteAsync(token)).Select(r => (r.Emoji, r.EmojiURL)))
|
||||
.Select(e => new Models.Post.CustomEmoji { Shortcode = e.Item1, URL = e.Item2 })
|
||||
.ToList();
|
||||
if (Normalise(emoji, known) is not { } picked)
|
||||
return false;
|
||||
var reaction = new Reaction
|
||||
{
|
||||
PostId = post.ID, AccountId = me.Id, ActorURI = me.Uri, IsLocal = true, Emoji = picked.Name, EmojiURL = picked.Url,
|
||||
ActivityURI = me.ActivityUri($"react-{post.ID}-{Guid.NewGuid():N}")
|
||||
};
|
||||
try
|
||||
{
|
||||
await DB.Default.SaveAsync(reaction, token);
|
||||
}
|
||||
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (!post.IsFederatedCopy)
|
||||
await Notifications.Add(post.GroupUserId, NotificationType.Reaction, me.Id, me.Uri, post.ID, token, picked.Name, picked.Url);
|
||||
else
|
||||
await Tell(me, post, Activity(me, post, reaction), token);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> Unreact(LocalActor me, PostEntity post, string emoji, CancellationToken token)
|
||||
{
|
||||
var name = Normalise(emoji, post.Emojis.ToList(), requireKnownCustom: false)?.Name;
|
||||
var reaction = name == default
|
||||
? default
|
||||
: await DB.Default.Find<Reaction>().Match(r => r.PostId == post.ID && r.ActorURI == me.Uri && r.Emoji == name).ExecuteFirstAsync(token);
|
||||
if (reaction == default)
|
||||
return false;
|
||||
await DB.Default.DeleteAsync<Reaction>(reaction.ID);
|
||||
if (post.IsFederatedCopy)
|
||||
await Tell(me, post, new JsonObject
|
||||
{
|
||||
["@context"] = ActivityPubRenderer.Context(),
|
||||
["id"] = me.ActivityUri($"undo-{reaction.ID}"),
|
||||
["type"] = "Undo",
|
||||
["actor"] = me.Uri,
|
||||
["object"] = Activity(me, post, reaction)
|
||||
}, token);
|
||||
return true;
|
||||
}
|
||||
|
||||
static JsonObject Activity(LocalActor me, PostEntity post, Reaction reaction)
|
||||
{
|
||||
var react = new JsonObject
|
||||
{
|
||||
["@context"] = ActivityPubRenderer.Context(),
|
||||
["id"] = reaction.ActivityURI,
|
||||
["type"] = "EmojiReact",
|
||||
["actor"] = me.Uri,
|
||||
["object"] = post.ObjectURI,
|
||||
["to"] = new JsonArray(post.ActorURI),
|
||||
["content"] = reaction.EmojiURL == default ? reaction.Emoji : $":{reaction.Emoji}:"
|
||||
};
|
||||
if (reaction.EmojiURL != default)
|
||||
react["tag"] = new JsonArray(new JsonObject
|
||||
{
|
||||
["type"] = "Emoji",
|
||||
["name"] = $":{reaction.Emoji}:",
|
||||
["icon"] = new JsonObject { ["type"] = "Image", ["url"] = reaction.EmojiURL }
|
||||
});
|
||||
return react;
|
||||
}
|
||||
|
||||
async Task Tell(LocalActor me, PostEntity post, JsonObject activity, CancellationToken token)
|
||||
{
|
||||
var author = await _dbEntities.ForeignAvatars.Match(f => f.ActorURI == post.ActorURI).ExecuteFirstAsync(token);
|
||||
if (!string.IsNullOrEmpty(author?.InboxURL))
|
||||
await _delivery.Enqueue(me, new[] { author.InboxURL }, activity, token);
|
||||
}
|
||||
|
||||
public sealed record Picked(string Name, string Url);
|
||||
|
||||
public static Picked Normalise(string content, List<Models.Post.CustomEmoji> emojis, bool requireKnownCustom = true)
|
||||
{
|
||||
var text = content?.Trim();
|
||||
if (string.IsNullOrEmpty(text) || text.Length > 100)
|
||||
return default;
|
||||
if (text.StartsWith(':') && text.EndsWith(':') && text.Length > 2)
|
||||
{
|
||||
var shortcode = text.Trim(':');
|
||||
var bare = shortcode.Split('@')[0];
|
||||
var emoji = emojis.FirstOrDefault(e => e.Shortcode == shortcode) ?? emojis.FirstOrDefault(e => e.Shortcode.Split('@')[0] == bare);
|
||||
if (emoji == default)
|
||||
return requireKnownCustom ? default : new Picked(bare, default);
|
||||
return new Picked(bare, emoji.URL);
|
||||
}
|
||||
if (text.Any(c => c < 128 || char.IsWhiteSpace(c)) || new StringInfo(text).LengthInTextElements != 1)
|
||||
return default;
|
||||
return new Picked(text, default);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Domain.Social;
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Inbox.Handlers;
|
||||
using PrivaPub.Federation.Outbox;
|
||||
using PrivaPub.Federation.Rendering;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Models.Social;
|
||||
using PrivaPub.Models.User;
|
||||
using PrivaPub.StaticServices;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
using PostEntity = PrivaPub.Models.Post.Post;
|
||||
|
||||
namespace PrivaPub.Domain.Statuses
|
||||
{
|
||||
public sealed record PollDraft(IReadOnlyList<string> Options, int ExpiresIn, bool Multiple, bool HideTotals);
|
||||
|
||||
public interface IPollService
|
||||
{
|
||||
string Invalid(PollDraft draft);
|
||||
PostPoll Create(PollDraft draft);
|
||||
Task Scheduled(PostEntity post, CancellationToken token);
|
||||
Task<StatusOutcome> Vote(LocalActor me, PostEntity post, IReadOnlyList<int> choices, CancellationToken token);
|
||||
Task Receive(PostEntity post, ForeignAvatar voter, string choice, string activityId, CancellationToken token);
|
||||
Task<HashSet<int>> OwnVotes(string postId, string avatarId, CancellationToken token);
|
||||
}
|
||||
|
||||
public class PollService : IPollService
|
||||
{
|
||||
public const int MaxOptions = 4;
|
||||
public const int MaxOptionLength = 50;
|
||||
public const int MinExpiration = 300;
|
||||
public const int MaxExpiration = 2_629_746;
|
||||
static readonly TimeSpan RefreshEvery = TimeSpan.FromMinutes(3);
|
||||
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IDeliveryService _delivery;
|
||||
readonly IJobQueue _queue;
|
||||
|
||||
public PollService(DbEntities dbEntities, ILocalActorService localActors, IDeliveryService delivery, IJobQueue queue)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
_delivery = delivery;
|
||||
_queue = queue;
|
||||
}
|
||||
|
||||
public string Invalid(PollDraft draft)
|
||||
{
|
||||
var options = draft.Options.Select(o => o?.Trim()).ToList();
|
||||
if (options.Count is < 2 or > MaxOptions)
|
||||
return $"Validation failed: A poll needs between 2 and {MaxOptions} options";
|
||||
if (options.Any(o => string.IsNullOrEmpty(o) || o.Length > MaxOptionLength))
|
||||
return $"Validation failed: Poll options must be between 1 and {MaxOptionLength} characters";
|
||||
if (options.Distinct(StringComparer.Ordinal).Count() != options.Count)
|
||||
return "Validation failed: Poll options must be unique";
|
||||
if (draft.ExpiresIn is < MinExpiration or > MaxExpiration)
|
||||
return "Validation failed: The poll's duration is out of range";
|
||||
return default;
|
||||
}
|
||||
|
||||
public PostPoll Create(PollDraft draft) => new()
|
||||
{
|
||||
Options = draft.Options.Select(o => new PollOption { Title = o.Trim() }).ToList(),
|
||||
Multiple = draft.Multiple,
|
||||
HideTotals = draft.HideTotals,
|
||||
ExpiresAt = DateTime.UtcNow.AddSeconds(draft.ExpiresIn),
|
||||
VotersCount = 0
|
||||
};
|
||||
|
||||
public async Task Scheduled(PostEntity post, CancellationToken token)
|
||||
{
|
||||
if (post.Poll?.ExpiresAt is not { } ends)
|
||||
return;
|
||||
await _queue.EnqueueMany(new[]
|
||||
{
|
||||
new Job { Kind = JobKind.PollClose, Payload = post.ID, Host = LocalHost, DedupeKey = $"poll-close|{post.ID}", RunAt = ends }
|
||||
}, token);
|
||||
}
|
||||
|
||||
public async Task<StatusOutcome> Vote(LocalActor me, PostEntity post, IReadOnlyList<int> choices, CancellationToken token)
|
||||
{
|
||||
var poll = post.Poll;
|
||||
if (poll == default)
|
||||
return StatusOutcome.Fail(StatusCodes.Status404NotFound, "Record not found");
|
||||
if (Ended(poll))
|
||||
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: The poll has already ended");
|
||||
if (!post.IsFederatedCopy && post.GroupUserId == me.Id)
|
||||
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: You cannot vote in your own poll");
|
||||
var picked = choices.Distinct().ToList();
|
||||
if (picked.Count == 0 || picked.Any(c => c < 0 || c >= poll.Options.Count) || !poll.Multiple && picked.Count > 1)
|
||||
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: The choices are invalid");
|
||||
if (await DB.Default.Find<PollVote>().Match(v => v.PostId == post.ID && v.ActorURI == me.Uri).ExecuteAnyAsync(token))
|
||||
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: You have already voted on this poll");
|
||||
|
||||
foreach (var choice in picked)
|
||||
await DB.Default.SaveAsync(new PollVote
|
||||
{
|
||||
PostId = post.ID, VoterAccountId = me.Id, ActorURI = me.Uri, IsLocalVoter = true, Choice = choice,
|
||||
ActivityURI = me.Uri + $"#votes/{post.ID}/{choice}"
|
||||
}, token);
|
||||
await Count(post, picked, newVoter: true, token);
|
||||
|
||||
if (post.IsFederatedCopy)
|
||||
await SendVotes(me, post, picked, token);
|
||||
else
|
||||
await Refreshed(post, token);
|
||||
await Scheduled(post, token);
|
||||
return new StatusOutcome(await _dbEntities.Posts.MatchID(post.ID).ExecuteFirstAsync(token));
|
||||
}
|
||||
|
||||
public async Task Receive(PostEntity post, ForeignAvatar voter, string choice, string activityId, CancellationToken token)
|
||||
{
|
||||
var poll = post.Poll;
|
||||
if (poll == default || post.IsFederatedCopy || Ended(poll) || !LikeHandler.MaySee(post, voter))
|
||||
return;
|
||||
var index = poll.Options.FindIndex(o => o.Title == choice);
|
||||
if (index < 0)
|
||||
return;
|
||||
var earlier = await DB.Default.Find<PollVote>().Match(v => v.PostId == post.ID && v.ActorURI == voter.ActorURI).ExecuteAsync(token);
|
||||
if (earlier.Count > 0 && (!poll.Multiple || earlier.Any(v => v.Choice == index)))
|
||||
return;
|
||||
try
|
||||
{
|
||||
await DB.Default.SaveAsync(new PollVote
|
||||
{
|
||||
PostId = post.ID, VoterAccountId = voter.ID, ActorURI = voter.ActorURI, Choice = index, ActivityURI = activityId
|
||||
}, token);
|
||||
}
|
||||
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||
{
|
||||
return;
|
||||
}
|
||||
await Count(post, new[] { index }, newVoter: earlier.Count == 0, token);
|
||||
await Refreshed(post, token);
|
||||
}
|
||||
|
||||
public async Task<HashSet<int>> OwnVotes(string postId, string avatarId, CancellationToken token) =>
|
||||
avatarId == default
|
||||
? new HashSet<int>()
|
||||
: (await DB.Default.Find<PollVote>().Match(v => v.PostId == postId && v.VoterAccountId == avatarId && v.IsLocalVoter).ExecuteAsync(token))
|
||||
.Select(v => v.Choice).ToHashSet();
|
||||
|
||||
public static bool Ended(PostPoll poll) => poll.ClosedAt.HasValue || poll.ExpiresAt <= DateTime.UtcNow;
|
||||
|
||||
async Task Count(PostEntity post, IReadOnlyList<int> choices, bool newVoter, CancellationToken token)
|
||||
{
|
||||
var update = DB.Default.Update<PostEntity>().MatchID(post.ID);
|
||||
foreach (var choice in choices)
|
||||
update = update.Modify(b => b.Inc($"{nameof(PostEntity.Poll)}.{nameof(PostPoll.Options)}.{choice}.{nameof(PollOption.Votes)}", 1));
|
||||
if (newVoter && post.Poll.VotersCount.HasValue)
|
||||
update = update.Modify(b => b.Inc($"{nameof(PostEntity.Poll)}.{nameof(PostPoll.VotersCount)}", 1));
|
||||
await update.ExecuteAsync(token);
|
||||
}
|
||||
|
||||
async Task SendVotes(LocalActor me, PostEntity post, IReadOnlyList<int> choices, CancellationToken token)
|
||||
{
|
||||
var owner = await _dbEntities.ForeignAvatars.Match(f => f.ActorURI == post.ActorURI).ExecuteFirstAsync(token);
|
||||
if (string.IsNullOrEmpty(owner?.InboxURL))
|
||||
return;
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
var voteId = me.Uri + $"#votes/{post.ID}/{choice}";
|
||||
await _delivery.Enqueue(me, new[] { owner.InboxURL }, new JsonObject
|
||||
{
|
||||
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||
["id"] = voteId + "/activity",
|
||||
["type"] = "Create",
|
||||
["actor"] = me.Uri,
|
||||
["to"] = new JsonArray(owner.ActorURI),
|
||||
["object"] = new JsonObject
|
||||
{
|
||||
["id"] = voteId,
|
||||
["type"] = "Note",
|
||||
["name"] = post.Poll.Options[choice].Title,
|
||||
["attributedTo"] = me.Uri,
|
||||
["to"] = new JsonArray(owner.ActorURI),
|
||||
["inReplyTo"] = post.ObjectURI
|
||||
}
|
||||
}, token);
|
||||
}
|
||||
}
|
||||
|
||||
async Task Refreshed(PostEntity post, CancellationToken token)
|
||||
{
|
||||
var bucket = DateTime.UtcNow.Ticks / RefreshEvery.Ticks;
|
||||
await _queue.EnqueueMany(new[]
|
||||
{
|
||||
new Job
|
||||
{
|
||||
Kind = JobKind.PollRefresh, Payload = post.ID, Host = LocalHost, DedupeKey = $"poll-refresh|{post.ID}|{bucket}",
|
||||
RunAt = new DateTime((bucket + 1) * RefreshEvery.Ticks, DateTimeKind.Utc)
|
||||
}
|
||||
}, token);
|
||||
}
|
||||
|
||||
string LocalHost => new Uri(_localActors.BaseAddress).Host;
|
||||
}
|
||||
|
||||
public class PollRefreshJob : IJobHandler
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IOutboxPublisher _outbox;
|
||||
|
||||
public PollRefreshJob(DbEntities dbEntities, ILocalActorService localActors, IOutboxPublisher outbox)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
_outbox = outbox;
|
||||
}
|
||||
|
||||
public virtual JobKind Kind => JobKind.PollRefresh;
|
||||
public int Concurrency => 1;
|
||||
public int MaxAttempts => 3;
|
||||
public int PerHostLimit => 1;
|
||||
|
||||
public virtual async Task<JobOutcome> Handle(Job job, CancellationToken token)
|
||||
{
|
||||
var post = await _dbEntities.Posts.MatchID(job.Payload).ExecuteFirstAsync(token);
|
||||
if (post?.Poll == default || post.IsFederatedCopy || post.DeletedAt.HasValue)
|
||||
return JobOutcome.Done;
|
||||
var author = await _localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
|
||||
if (author == default)
|
||||
return JobOutcome.Done;
|
||||
await Closing(post, author, token);
|
||||
if (!post.IsLocalOnly)
|
||||
await _outbox.Publish(author, post, ActivityPubRenderer.UpdateOf(post, author, $"poll-{DateTime.UtcNow.Ticks}"), token);
|
||||
return JobOutcome.Done;
|
||||
}
|
||||
|
||||
protected virtual Task Closing(PostEntity post, LocalActor author, CancellationToken token) => Task.CompletedTask;
|
||||
}
|
||||
|
||||
public class PollCloseJob : PollRefreshJob
|
||||
{
|
||||
readonly DbEntities _posts;
|
||||
|
||||
public PollCloseJob(DbEntities dbEntities, ILocalActorService localActors, IOutboxPublisher outbox) : base(dbEntities, localActors, outbox)
|
||||
{
|
||||
_posts = dbEntities;
|
||||
}
|
||||
|
||||
public override JobKind Kind => JobKind.PollClose;
|
||||
|
||||
public override async Task<JobOutcome> Handle(Job job, CancellationToken token)
|
||||
{
|
||||
var post = await _posts.Posts.MatchID(job.Payload).ExecuteFirstAsync(token);
|
||||
if (post?.Poll == default || post.DeletedAt.HasValue)
|
||||
return JobOutcome.Done;
|
||||
if (!post.IsFederatedCopy)
|
||||
return await base.Handle(job, token);
|
||||
await Notify(post, post.AuthorAccountId, post.ActorURI, token);
|
||||
return JobOutcome.Done;
|
||||
}
|
||||
|
||||
protected override async Task Closing(PostEntity post, LocalActor author, CancellationToken token)
|
||||
{
|
||||
post.Poll.ClosedAt ??= DateTime.UtcNow;
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(p => p.Poll.ClosedAt, post.Poll.ClosedAt).ExecuteAsync(token);
|
||||
await Notify(post, author.Id, author.Uri, token);
|
||||
}
|
||||
|
||||
public static async Task Notify(PostEntity post, string authorAccountId, string authorUri, CancellationToken token)
|
||||
{
|
||||
var voters = await DB.Default.Find<PollVote>().Match(v => v.PostId == post.ID && v.IsLocalVoter).ExecuteAsync(token);
|
||||
foreach (var voter in voters.Select(v => v.VoterAccountId).Distinct())
|
||||
await Notifications.Add(voter, NotificationType.Poll, authorAccountId, authorUri, post.ID, token);
|
||||
if (!post.IsFederatedCopy)
|
||||
await Notifications.Add(post.GroupUserId, NotificationType.Poll, post.GroupUserId, authorUri, post.ID, token);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Domain.Social;
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Inbox;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Federation.Outbox;
|
||||
using PrivaPub.Federation.Rendering;
|
||||
using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.User;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Models.Social;
|
||||
using PrivaPub.StaticServices;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
using static PrivaPub.Federation.Objects.ActivityJson;
|
||||
|
||||
using PostEntity = PrivaPub.Models.Post.Post;
|
||||
|
||||
namespace PrivaPub.Domain.Statuses
|
||||
{
|
||||
public interface IQuoteService
|
||||
{
|
||||
Task Resolve(PostEntity post, NoteDocument note, CancellationToken token);
|
||||
Task Revoke(string stampUri, CancellationToken token);
|
||||
Task<bool> Verified(string stampUri, string quotingUri, PostEntity quoted, CancellationToken token);
|
||||
Task<QuotePermission> Permission(PostEntity quoted, LocalActor author, CancellationToken token);
|
||||
Task<string> Grant(LocalActor author, PostEntity quoted, string quotingUri, string quoterUri, CancellationToken token);
|
||||
Task ReceiveRequest(JsonNode request, ForeignAvatar actor, CancellationToken token);
|
||||
Task<bool> RevokeLicence(PostEntity quoted, PostEntity quoting, CancellationToken token);
|
||||
Task Request(LocalActor author, PostEntity post, PostEntity quoted, JsonObject note, CancellationToken token);
|
||||
Task<bool> Answered(JsonNode answer, ForeignAvatar actor, bool accepted, CancellationToken token);
|
||||
}
|
||||
|
||||
public enum QuotePermission
|
||||
{
|
||||
Denied,
|
||||
Granted,
|
||||
AskFirst
|
||||
}
|
||||
|
||||
public class QuoteService : IQuoteService
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly IRemoteActorService _remoteActors;
|
||||
readonly IRemotePosts _remotePosts;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IDeliveryService _delivery;
|
||||
readonly IOutboxPublisher _outbox;
|
||||
|
||||
public QuoteService(DbEntities dbEntities, IRemoteActorService remoteActors, IRemotePosts remotePosts, ILocalActorService localActors,
|
||||
IDeliveryService delivery, IOutboxPublisher outbox)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_remoteActors = remoteActors;
|
||||
_remotePosts = remotePosts;
|
||||
_localActors = localActors;
|
||||
_delivery = delivery;
|
||||
_outbox = outbox;
|
||||
}
|
||||
|
||||
const string RequestPrefix = "quote-request-";
|
||||
|
||||
const string LicencePath = "/parrot-licences/";
|
||||
|
||||
public static string LicenceUri(LocalActor author, string licenceId) => author.Uri + LicencePath + licenceId;
|
||||
|
||||
async Task<bool> MayQuote(PostEntity quoted, string quoterUri, CancellationToken token)
|
||||
{
|
||||
var author = await _localActors.FindById(LocalActorKind.Person, quoted.GroupUserId, token);
|
||||
if (author == default)
|
||||
return false;
|
||||
if (quoterUri == author.Uri)
|
||||
return quoted.Visibility is PostVisibility.Public or PostVisibility.Unlisted;
|
||||
return ActivityPubRenderer.QuotableBy(quoted) switch
|
||||
{
|
||||
QuotePolicies.Public => true,
|
||||
QuotePolicies.Followers => await Follows(quoterUri, author, token),
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
|
||||
async Task<bool> Follows(string followerUri, LocalActor author, CancellationToken token)
|
||||
{
|
||||
if (await _dbEntities.Followers.Match(f => f.LocalActorId == author.Id && f.ActorURI == followerUri && f.IsAccepted).ExecuteAnyAsync(token))
|
||||
return true;
|
||||
var local = await _localActors.FindByUri(followerUri, token);
|
||||
return local != default
|
||||
&& await _dbEntities.Followings.Match(f => f.AvatarId == local.Id && f.TargetActorURI == author.Uri && f.State == FollowState.Accepted).ExecuteAnyAsync(token);
|
||||
}
|
||||
|
||||
public async Task<string> Grant(LocalActor author, PostEntity quoted, string quotingUri, string quoterUri, CancellationToken token)
|
||||
{
|
||||
var existing = await DB.Default.Find<QuoteLicence>().Match(l => l.PostId == quoted.ID && l.QuotingObjectURI == quotingUri && l.RevokedAt == null).ExecuteFirstAsync(token);
|
||||
if (existing != default)
|
||||
return LicenceUri(author, existing.ID);
|
||||
var licence = new QuoteLicence { PostId = quoted.ID, AuthorAvatarId = author.Id, QuotingObjectURI = quotingUri, QuoterActorURI = quoterUri };
|
||||
await DB.Default.SaveAsync(licence, token);
|
||||
return LicenceUri(author, licence.ID);
|
||||
}
|
||||
|
||||
public async Task ReceiveRequest(JsonNode request, ForeignAvatar actor, CancellationToken token)
|
||||
{
|
||||
var objectUri = Id(request["object"]);
|
||||
var instrument = request["instrument"];
|
||||
var quotingUri = Id(instrument);
|
||||
if (objectUri == default || quotingUri == default || !Origin.Same(quotingUri, actor.ActorURI)
|
||||
|| instrument is JsonObject embedded && Id(embedded["attributedTo"]) is { } by && by != actor.ActorURI)
|
||||
{
|
||||
Arrival.Drop("misattributed");
|
||||
return;
|
||||
}
|
||||
var quoted = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
var author = quoted == default ? default : await _localActors.FindById(LocalActorKind.Person, quoted.GroupUserId, token);
|
||||
if (author == default || string.IsNullOrEmpty(actor.InboxURL))
|
||||
{
|
||||
Arrival.Drop("unknown-object");
|
||||
return;
|
||||
}
|
||||
Arrival.About("Note", quoted.Visibility, quoted.CreationDate);
|
||||
var answer = new JsonObject
|
||||
{
|
||||
["@context"] = ActivityPubRenderer.Context(),
|
||||
["actor"] = author.Uri,
|
||||
["object"] = Id(request),
|
||||
["to"] = new JsonArray(actor.ActorURI)
|
||||
};
|
||||
if (await MayQuote(quoted, actor.ActorURI, token))
|
||||
{
|
||||
Arrival.Accept("quote-granted");
|
||||
answer["type"] = "Accept";
|
||||
answer["result"] = await Grant(author, quoted, quotingUri, actor.ActorURI, token);
|
||||
answer["id"] = author.ActivityUri($"accept-quote-{Guid.NewGuid():N}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Arrival.Reject("quote-refused");
|
||||
answer["type"] = "Reject";
|
||||
answer["id"] = author.ActivityUri($"reject-quote-{Guid.NewGuid():N}");
|
||||
}
|
||||
await _delivery.Enqueue(author, new[] { actor.InboxURL }, answer, token);
|
||||
}
|
||||
|
||||
public async Task<bool> RevokeLicence(PostEntity quoted, PostEntity quoting, CancellationToken token)
|
||||
{
|
||||
var licence = await DB.Default.Find<QuoteLicence>().Match(l => l.PostId == quoted.ID && l.QuotingObjectURI == quoting.ObjectURI && l.RevokedAt == null).ExecuteFirstAsync(token);
|
||||
var author = licence == default ? default : await _localActors.FindById(LocalActorKind.Person, quoted.GroupUserId, token);
|
||||
if (author == default)
|
||||
return false;
|
||||
await DB.Default.Update<QuoteLicence>().MatchID(licence.ID).Modify(l => l.RevokedAt, DateTime.UtcNow).ExecuteAsync(token);
|
||||
var uri = LicenceUri(author, licence.ID);
|
||||
await Revoke(uri, token);
|
||||
var quoter = quoting.IsFederatedCopy ? await _dbEntities.ForeignAvatars.Match(f => f.ActorURI == quoting.ActorURI).ExecuteFirstAsync(token) : default;
|
||||
var inboxes = (await _delivery.FollowerInboxes(author, token)).Append(quoter?.InboxURL).Where(i => !string.IsNullOrEmpty(i)).Distinct().ToList();
|
||||
if (inboxes.Count > 0)
|
||||
await _delivery.Enqueue(author, inboxes, new JsonObject
|
||||
{
|
||||
["@context"] = ActivityPubRenderer.Context(),
|
||||
["id"] = author.ActivityUri($"revoke-quote-{licence.ID}"),
|
||||
["type"] = "Delete",
|
||||
["actor"] = author.Uri,
|
||||
["object"] = uri,
|
||||
["to"] = new JsonArray(Addressing.Public)
|
||||
}, token);
|
||||
return true;
|
||||
}
|
||||
|
||||
async Task<bool> OurLicence(string stampUri, PostEntity quoting, PostEntity quoted, CancellationToken token)
|
||||
{
|
||||
var marker = stampUri?.LastIndexOf(LicencePath, StringComparison.Ordinal) ?? -1;
|
||||
if (marker < 0 || !stampUri.StartsWith(_localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
var id = stampUri[(marker + LicencePath.Length)..];
|
||||
return await DB.Default.Find<QuoteLicence>().Match(l => l.ID == id && l.PostId == quoted.ID && l.QuotingObjectURI == quoting.ObjectURI && l.RevokedAt == null)
|
||||
.ExecuteAnyAsync(token);
|
||||
}
|
||||
|
||||
public async Task<QuotePermission> Permission(PostEntity quoted, LocalActor author, CancellationToken token)
|
||||
{
|
||||
if (!quoted.IsFederatedCopy)
|
||||
return await MayQuote(quoted, author.Uri, token) ? QuotePermission.Granted : QuotePermission.Denied;
|
||||
if (quoted.Visibility is not (PostVisibility.Public or PostVisibility.Unlisted))
|
||||
return QuotePermission.Denied;
|
||||
if (quoted.QuotePolicy is not { } policy)
|
||||
return QuotePermission.Granted;
|
||||
return policy.Automatic.Concat(policy.Manual).Any(Addressing.IsPublic) || policy.Automatic.Concat(policy.Manual).Contains(author.Uri)
|
||||
? QuotePermission.AskFirst
|
||||
: QuotePermission.Denied;
|
||||
}
|
||||
|
||||
public async Task Request(LocalActor author, PostEntity post, PostEntity quoted, JsonObject note, CancellationToken token)
|
||||
{
|
||||
var owner = await _dbEntities.ForeignAvatars.Match(f => f.ActorURI == quoted.ActorURI).ExecuteFirstAsync(token);
|
||||
if (string.IsNullOrEmpty(owner?.InboxURL))
|
||||
return;
|
||||
await _delivery.Enqueue(author, new[] { owner.InboxURL }, new JsonObject
|
||||
{
|
||||
["@context"] = ActivityPubRenderer.Context(),
|
||||
["id"] = author.ActivityUri(RequestPrefix + post.ID),
|
||||
["type"] = "QuoteRequest",
|
||||
["actor"] = author.Uri,
|
||||
["object"] = quoted.ObjectURI,
|
||||
["instrument"] = note.DeepClone(),
|
||||
["to"] = new JsonArray(quoted.ActorURI)
|
||||
}, token);
|
||||
}
|
||||
|
||||
public async Task<bool> Answered(JsonNode answer, ForeignAvatar actor, bool accepted, CancellationToken token)
|
||||
{
|
||||
var request = answer["object"];
|
||||
var requestId = Id(request);
|
||||
var marker = requestId?.LastIndexOf("/grunts/" + RequestPrefix, StringComparison.Ordinal) ?? -1;
|
||||
if (marker < 0 && !(request is JsonObject && Value(request, "type") == "QuoteRequest"))
|
||||
return false;
|
||||
if (marker < 0)
|
||||
return true;
|
||||
var postId = requestId[(marker + "/grunts/".Length + RequestPrefix.Length)..];
|
||||
var post = await _dbEntities.Posts.Match(p => p.ID == postId && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
var author = post == default ? default : await _localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
|
||||
if (author == default || author.ActivityUri(RequestPrefix + post.ID) != requestId || post.QuoteState != QuoteState.Pending)
|
||||
return true;
|
||||
var quoted = await _dbEntities.Posts.MatchID(post.QuotedPostId).ExecuteFirstAsync(token);
|
||||
if (quoted?.ActorURI != actor.ActorURI)
|
||||
return true;
|
||||
if (!accepted)
|
||||
{
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(p => p.QuoteState, QuoteState.Rejected).ExecuteAsync(token);
|
||||
return true;
|
||||
}
|
||||
var stamp = Id(answer["result"]);
|
||||
if (stamp == default || !await Verified(stamp, post.ObjectURI, quoted, token))
|
||||
return true;
|
||||
post.QuoteAuthorizationURI = stamp;
|
||||
post.QuoteState = QuoteState.Accepted;
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID)
|
||||
.Modify(p => p.QuoteAuthorizationURI, stamp)
|
||||
.Modify(p => p.QuoteState, QuoteState.Accepted)
|
||||
.ExecuteAsync(token);
|
||||
await DB.Default.Update<PostEntity>().MatchID(quoted.ID).Modify(b => b.Inc(p => p.QuotesCount, 1)).ExecuteAsync(token);
|
||||
if (!post.IsLocalOnly)
|
||||
await _outbox.Publish(author, post, ActivityPubRenderer.UpdateOf(post, author, "quote-approved"), token);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task Resolve(PostEntity post, NoteDocument note, CancellationToken token)
|
||||
{
|
||||
if (note.QuoteUri == default && !note.QuoteDeleted)
|
||||
return;
|
||||
var quoted = note.QuoteUri == default
|
||||
? default
|
||||
: await _dbEntities.Posts.Match(p => p.ObjectURI == note.QuoteUri && !p.DeletedAt.HasValue).ExecuteFirstAsync(token)
|
||||
?? await _remotePosts.StoreContext(note.QuoteUri, RemotePosts.MaxDepth, token);
|
||||
|
||||
var state = note.QuoteDeleted ? QuoteState.Deleted
|
||||
: quoted == default ? QuoteState.Pending
|
||||
: note.QuoteAuthorization != default
|
||||
? (quoted.IsFederatedCopy ? await Verified(note.QuoteAuthorization, post.ObjectURI, quoted, token) : await OurLicence(note.QuoteAuthorization, post, quoted, token))
|
||||
? QuoteState.Accepted
|
||||
: QuoteState.Unauthorized
|
||||
: note.QuotesByConsent ? QuoteState.Pending
|
||||
: quoted.Visibility is PostVisibility.Public or PostVisibility.Unlisted ? QuoteState.Accepted
|
||||
: QuoteState.Unauthorized;
|
||||
|
||||
var wasCounted = post.QuoteState == QuoteState.Accepted ? post.QuotedPostId : default;
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID)
|
||||
.Modify(p => p.QuoteURI, note.QuoteUri)
|
||||
.Modify(p => p.QuotedPostId, quoted?.ID)
|
||||
.Modify(p => p.QuoteState, state)
|
||||
.Modify(p => p.QuoteAuthorizationURI, state == QuoteState.Accepted ? note.QuoteAuthorization : default)
|
||||
.ExecuteAsync(token);
|
||||
post.QuotedPostId = quoted?.ID;
|
||||
post.QuoteState = state;
|
||||
|
||||
var nowCounted = state == QuoteState.Accepted ? quoted?.ID : default;
|
||||
if (wasCounted == nowCounted)
|
||||
return;
|
||||
if (wasCounted != default)
|
||||
await DB.Default.Update<PostEntity>().MatchID(wasCounted).Modify(b => b.Inc(p => p.QuotesCount, -1)).ExecuteAsync(token);
|
||||
if (nowCounted != default)
|
||||
{
|
||||
await DB.Default.Update<PostEntity>().MatchID(nowCounted).Modify(b => b.Inc(p => p.QuotesCount, 1)).ExecuteAsync(token);
|
||||
if (!quoted.IsFederatedCopy)
|
||||
await Notifications.Add(quoted.GroupUserId, NotificationType.Quote, post.AuthorAccountId, post.ActorURI, post.ID, token);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Revoke(string stampUri, CancellationToken token)
|
||||
{
|
||||
var revoked = await _dbEntities.Posts.Match(p => p.QuoteAuthorizationURI == stampUri && p.QuoteState == QuoteState.Accepted).ExecuteAsync(token);
|
||||
foreach (var post in revoked)
|
||||
{
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(p => p.QuoteState, QuoteState.Revoked).ExecuteAsync(token);
|
||||
if (post.QuotedPostId != default)
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.QuotedPostId).Modify(b => b.Inc(p => p.QuotesCount, -1)).ExecuteAsync(token);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> Verified(string stampUri, string quotingUri, PostEntity quoted, CancellationToken token)
|
||||
{
|
||||
if (!Origin.Same(stampUri, quoted.ActorURI))
|
||||
return false;
|
||||
using var fetched = await _remoteActors.FetchObject(stampUri, token);
|
||||
if (fetched == default)
|
||||
return false;
|
||||
var stamp = JsonNode.Parse(fetched.Root.GetRawText());
|
||||
return Value(stamp, "type") == "QuoteAuthorization"
|
||||
&& Id(stamp["attributedTo"]) == quoted.ActorURI
|
||||
&& Id(stamp["interactingObject"]) == quotingUri
|
||||
&& Id(stamp["interactionTarget"]) == quoted.ObjectURI;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.Group;
|
||||
using PrivaPub.Models.Media;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Models.User;
|
||||
using PrivaPub.Models.Social;
|
||||
using PrivaPub.StaticServices;
|
||||
|
||||
@@ -39,6 +40,9 @@ namespace PrivaPub.Domain.Statuses
|
||||
public double? Latitude { get; init; }
|
||||
public double? Longitude { get; init; }
|
||||
public double? RangeKm { get; init; }
|
||||
public PollDraft Poll { get; init; }
|
||||
public string QuotedStatusId { get; init; }
|
||||
public string QuotePolicy { get; init; }
|
||||
}
|
||||
|
||||
public sealed record StatusOutcome(PostEntity Post, int Status = StatusCodes.Status200OK, string Error = default)
|
||||
@@ -69,10 +73,17 @@ namespace PrivaPub.Domain.Statuses
|
||||
readonly IFanout _fanout;
|
||||
readonly IMediaService _media;
|
||||
readonly IGroupDistributor _groups;
|
||||
readonly IPollService _polls;
|
||||
readonly ILinkPreviews _previews;
|
||||
readonly IQuoteService _quotes;
|
||||
|
||||
public StatusService(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery,
|
||||
IContentRenderer content, IOutboxPublisher outbox, IFanout fanout, IMediaService media, IGroupDistributor groups)
|
||||
IContentRenderer content, IOutboxPublisher outbox, IFanout fanout, IMediaService media, IGroupDistributor groups, IPollService polls,
|
||||
ILinkPreviews previews, IQuoteService quotes)
|
||||
{
|
||||
_previews = previews;
|
||||
_quotes = quotes;
|
||||
_polls = polls;
|
||||
_media = media;
|
||||
_groups = groups;
|
||||
_dbEntities = dbEntities;
|
||||
@@ -89,8 +100,10 @@ namespace PrivaPub.Domain.Statuses
|
||||
var media = await Media(author, draft.MediaIds, default, token);
|
||||
if (media == default)
|
||||
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: Media attachments are invalid");
|
||||
if (string.IsNullOrWhiteSpace(draft.Text) && media.Count == 0)
|
||||
if (string.IsNullOrWhiteSpace(draft.Text) && media.Count == 0 && draft.Poll == default)
|
||||
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: Text can't be blank");
|
||||
if (draft.Poll != default && _polls.Invalid(draft.Poll) is { } invalidPoll)
|
||||
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, invalidPoll);
|
||||
|
||||
LocalActor group = default;
|
||||
if (!string.IsNullOrEmpty(draft.GroupId))
|
||||
@@ -107,6 +120,18 @@ namespace PrivaPub.Domain.Statuses
|
||||
if (parent != default && !await VisibilityPolicy.CanSee(parent, author.Id, token))
|
||||
return StatusOutcome.Fail(StatusCodes.Status404NotFound, "Record not found");
|
||||
|
||||
PostEntity quoted = default;
|
||||
var quotePermission = QuotePermission.Denied;
|
||||
if (!string.IsNullOrEmpty(draft.QuotedStatusId))
|
||||
{
|
||||
quoted = await _dbEntities.Posts.Match(p => p.ID == draft.QuotedStatusId && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
|
||||
if (quoted == default || !await VisibilityPolicy.CanSee(quoted, author.Id, token))
|
||||
return StatusOutcome.Fail(StatusCodes.Status404NotFound, "Record not found");
|
||||
quotePermission = await _quotes.Permission(quoted, author, token);
|
||||
if (quotePermission == QuotePermission.Denied)
|
||||
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: This post cannot be quoted");
|
||||
}
|
||||
|
||||
var located = draft.Latitude.HasValue || draft.Longitude.HasValue;
|
||||
if (located && (draft.Latitude is not (>= -90 and <= 90) || draft.Longitude is not (>= -180 and <= 180) || group != default
|
||||
|| draft.Visibility == PostVisibility.Direct))
|
||||
@@ -157,11 +182,20 @@ namespace PrivaPub.Domain.Statuses
|
||||
InReplyToURI = parent?.ObjectURI ?? (IsRemoteUri(draft.InReplyTo) ? draft.InReplyTo : default),
|
||||
InReplyToAccountId = parent?.AuthorAccountId ?? parent?.GroupUserId,
|
||||
IsLocalOnly = isLocalOnly,
|
||||
ActorURI = author.Uri
|
||||
ActorURI = author.Uri,
|
||||
Poll = draft.Poll == default ? default : _polls.Create(draft.Poll),
|
||||
QuoteURI = quoted?.ObjectURI,
|
||||
QuotedPostId = quoted?.ID,
|
||||
QuoteByConsent = quoted != default && (quoted.QuotePolicy != default || !quoted.IsFederatedCopy),
|
||||
QuoteState = quoted == default ? QuoteState.None : quotePermission == QuotePermission.Granted ? QuoteState.Accepted : QuoteState.Pending
|
||||
};
|
||||
post.ID = (string)post.GenerateNewID();
|
||||
post.ObjectURI = author.PostUri(post.ID);
|
||||
post.Url = author.PostHtmlUrl(post.ID);
|
||||
post.LocalQuotePolicy = QuotePolicies.IsKnown(draft.QuotePolicy) ? draft.QuotePolicy : author.Settings.QuotePolicy ?? QuotePolicies.Public;
|
||||
if (quoted is { IsFederatedCopy: false } && post.QuoteState == QuoteState.Accepted
|
||||
&& await _localActors.FindById(LocalActorKind.Person, quoted.GroupUserId, token) is { } quotedAuthor)
|
||||
post.QuoteAuthorizationURI = await _quotes.Grant(quotedAuthor, quoted, post.ObjectURI, author.Uri, token);
|
||||
if (located)
|
||||
{
|
||||
post.Geo = new Coordinates2D(Math.Round(draft.Longitude.Value, 2), Math.Round(draft.Latitude.Value, 2));
|
||||
@@ -200,6 +234,12 @@ namespace PrivaPub.Domain.Statuses
|
||||
if (parent != default)
|
||||
await DB.Default.Update<PostEntity>().MatchID(parent.ID).Modify(b => b.Inc(p => p.RepliesCount, 1)).ExecuteAsync(token);
|
||||
await _fanout.Distribute(post, token);
|
||||
await _polls.Scheduled(post, token);
|
||||
if (post.QuoteState == QuoteState.Accepted)
|
||||
await DB.Default.Update<PostEntity>().MatchID(quoted.ID).Modify(b => b.Inc(p => p.QuotesCount, 1)).ExecuteAsync(token);
|
||||
if (post.QuoteState == QuoteState.Pending && create?["object"] is JsonObject quotingNote)
|
||||
await _quotes.Request(author, post, quoted, quotingNote, token);
|
||||
await _previews.Wanted(post, token);
|
||||
if (create != default)
|
||||
await _outbox.Publish(author, post, create, token);
|
||||
if (create != default && group is { IsCircle: false } && visibility is PostVisibility.Public or PostVisibility.Unlisted)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Models.Post;
|
||||
|
||||
namespace PrivaPub.Federation.Actors
|
||||
{
|
||||
@@ -23,7 +26,18 @@ namespace PrivaPub.Federation.Actors
|
||||
public string SharedInbox { get; init; }
|
||||
public string Icon { get; init; }
|
||||
public bool Discoverable { get; init; } = true;
|
||||
public bool Indexable { get; init; }
|
||||
public bool Locked { get; init; }
|
||||
public bool Memorial { get; init; }
|
||||
public string Header { get; init; }
|
||||
public string IconDescription { get; init; }
|
||||
public string HeaderDescription { get; init; }
|
||||
public string MovedTo { get; init; }
|
||||
public DateTime? Published { get; init; }
|
||||
public List<CustomEmoji> Emojis { get; init; } = new();
|
||||
public Dictionary<string, string> Fields { get; init; } = new();
|
||||
public IReadOnlyList<ActorKey> Keys { get; init; } = Array.Empty<ActorKey>();
|
||||
public List<string> Features { get; init; } = new();
|
||||
|
||||
public ActorKey Key(string keyId) => Keys.FirstOrDefault(k => k.Id == keyId);
|
||||
|
||||
@@ -41,6 +55,7 @@ namespace PrivaPub.Federation.Actors
|
||||
{
|
||||
Id = id,
|
||||
Type = type,
|
||||
Features = ActorFeatures.Detect(root),
|
||||
PreferredUsername = RemoteActorService.Text(root, "preferredUsername"),
|
||||
Name = RemoteActorService.Text(root, "name"),
|
||||
Summary = RemoteActorService.Text(root, "summary"),
|
||||
@@ -52,10 +67,46 @@ namespace PrivaPub.Federation.Actors
|
||||
SharedInbox = root.TryGetProperty("endpoints", out var endpoints) ? RemoteActorService.Text(endpoints, "sharedInbox") : default,
|
||||
Icon = root.TryGetProperty("icon", out var icon) ? RemoteActorService.Text(icon, "url") : default,
|
||||
Discoverable = !root.TryGetProperty("discoverable", out var discoverable) || discoverable.ValueKind != JsonValueKind.False,
|
||||
Keys = ParseKeys(root, id)
|
||||
Keys = ParseKeys(root, id),
|
||||
Indexable = Flag(root, "indexable"),
|
||||
Locked = Flag(root, "manuallyApprovesFollowers"),
|
||||
Memorial = Flag(root, "memorial"),
|
||||
Header = root.TryGetProperty("image", out var image) ? ObjectShapes.Image(JsonNode.Parse(image.GetRawText())) : default,
|
||||
IconDescription = root.TryGetProperty("icon", out var described) ? Alt(described) : default,
|
||||
HeaderDescription = root.TryGetProperty("image", out var headerImage) ? Alt(headerImage) : default,
|
||||
MovedTo = RemoteActorService.Text(root, "movedTo"),
|
||||
Published = DateTimeOffset.TryParse(RemoteActorService.Text(root, "published"), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var published)
|
||||
? published.UtcDateTime
|
||||
: default(DateTime?),
|
||||
Emojis = root.TryGetProperty("tag", out var tags) ? ObjectShapes.Emojis(JsonNode.Parse(tags.GetRawText())) : new(),
|
||||
Fields = root.TryGetProperty("attachment", out var attachment) ? ParseFields(JsonNode.Parse(attachment.GetRawText())) : new()
|
||||
};
|
||||
}
|
||||
|
||||
const int MaxFields = 16;
|
||||
|
||||
static bool Flag(JsonElement root, string property) =>
|
||||
root.TryGetProperty(property, out var value) && value.ValueKind == JsonValueKind.True;
|
||||
|
||||
static string Alt(JsonElement image) =>
|
||||
image.ValueKind == JsonValueKind.Object ? ObjectShapes.Text(RemoteActorService.Text(image, "name") ?? RemoteActorService.Text(image, "summary"), 1500) : default;
|
||||
|
||||
static Dictionary<string, string> ParseFields(JsonNode attachment)
|
||||
{
|
||||
var fields = new Dictionary<string, string>();
|
||||
foreach (var field in ObjectShapes.Objects(attachment).Where(a => ActivityJson.Value(a, "type") is "PropertyValue" or "Note"))
|
||||
{
|
||||
var name = ObjectShapes.Text(ActivityJson.Value(field, "name"), 255);
|
||||
var value = ActivityJson.Value(field, "value") ?? ActivityJson.Value(field, "content");
|
||||
if (name == default || value == default || fields.ContainsKey(name))
|
||||
continue;
|
||||
fields[name] = ContentSanitizer.Html(value.Length <= 2048 ? value : value[..2048]);
|
||||
if (fields.Count == MaxFields)
|
||||
break;
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
static List<ActorKey> ParseKeys(JsonElement root, string actorId)
|
||||
{
|
||||
var keys = new List<ActorKey>();
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PrivaPub.Federation.Actors
|
||||
{
|
||||
public static class ActorFeatures
|
||||
{
|
||||
public static List<string> Detect(JsonElement root)
|
||||
{
|
||||
var found = new List<string>();
|
||||
if (root.ValueKind != JsonValueKind.Object)
|
||||
return found;
|
||||
void When(bool condition, string feature)
|
||||
{
|
||||
if (condition)
|
||||
found.Add(feature);
|
||||
}
|
||||
bool Has(string name) => root.TryGetProperty(name, out var value) && value.ValueKind is not (JsonValueKind.Null or JsonValueKind.Undefined);
|
||||
bool True(string name) => root.TryGetProperty(name, out var value) && value.ValueKind == JsonValueKind.True;
|
||||
IEnumerable<JsonElement> Items(string name) => !root.TryGetProperty(name, out var value)
|
||||
? Enumerable.Empty<JsonElement>()
|
||||
: value.ValueKind == JsonValueKind.Array ? value.EnumerateArray() : new[] { value };
|
||||
bool Typed(JsonElement item, string type) =>
|
||||
item.ValueKind == JsonValueKind.Object && item.TryGetProperty("type", out var t) && t.ValueKind == JsonValueKind.String && t.GetString() == type;
|
||||
|
||||
When(Has("featured"), "featured");
|
||||
When(Has("featuredTags"), "featured-tags");
|
||||
When(Items("alsoKnownAs").Any(), "also-known-as");
|
||||
When(Has("movedTo"), "moved-to");
|
||||
When(root.TryGetProperty("endpoints", out var endpoints) && endpoints.ValueKind == JsonValueKind.Object && endpoints.TryGetProperty("sharedInbox", out _), "shared-inbox");
|
||||
When(Has("assertionMethod"), "fep-521a-assertion-method");
|
||||
When(Has("proof"), "fep-8b32-proof");
|
||||
When(Items("attachment").Any(a => Typed(a, "PropertyValue")), "property-value");
|
||||
When(Items("attachment").Any(a => Typed(a, "VerifiableIdentityStatement") || Typed(a, "IdentityProof")), "fep-c390-identity-proof");
|
||||
When(Items("tag").Any(t => Typed(t, "Emoji")), "custom-emoji");
|
||||
When(root.EnumerateObject().Any(p => p.Name.StartsWith("_misskey", StringComparison.Ordinal)), "misskey");
|
||||
When(True("isCat"), "is-cat");
|
||||
When(Has("webfinger"), "fep-2c59-webfinger");
|
||||
When(Has("attributionDomains"), "attribution-domains");
|
||||
When(Has("searchableBy"), "fep-268d-searchable-by");
|
||||
When(True("indexable"), "indexable");
|
||||
When(root.TryGetProperty("discoverable", out var discoverable) && discoverable.ValueKind == JsonValueKind.False, "undiscoverable");
|
||||
When(True("manuallyApprovesFollowers"), "locked");
|
||||
When(True("memorial"), "memorial");
|
||||
When(Has("generator"), "generator");
|
||||
When(root.TryGetProperty("publicKey", out var keys) && keys.ValueKind == JsonValueKind.Array, "public-key-array");
|
||||
if (KeyBits(root) is { } bits)
|
||||
found.Add($"key:rsa-{bits}");
|
||||
return found;
|
||||
}
|
||||
|
||||
static int? KeyBits(JsonElement root)
|
||||
{
|
||||
if (!root.TryGetProperty("publicKey", out var key))
|
||||
return default;
|
||||
if (key.ValueKind == JsonValueKind.Array)
|
||||
key = key.EnumerateArray().FirstOrDefault(k => k.ValueKind == JsonValueKind.Object);
|
||||
if (key.ValueKind != JsonValueKind.Object || !key.TryGetProperty("publicKeyPem", out var pem) || pem.ValueKind != JsonValueKind.String)
|
||||
return default;
|
||||
try
|
||||
{
|
||||
using var rsa = RSA.Create();
|
||||
rsa.ImportFromPem(pem.GetString());
|
||||
return rsa.KeySize;
|
||||
}
|
||||
catch (Exception ex) when (ex is ArgumentException or CryptographicException)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ namespace PrivaPub.Federation.Actors
|
||||
Task<FetchedJson> FetchObject(string uri, CancellationToken token);
|
||||
Task<ForeignAvatar> GetActor(string actorUri, bool refresh, CancellationToken token);
|
||||
Task<ForeignAvatar> GetActorByKeyId(string keyId, bool refresh, CancellationToken token);
|
||||
bool KeyTemporarilyUnavailable(string keyId) => false;
|
||||
Task<string> ResolveHandle(string handle, CancellationToken token);
|
||||
}
|
||||
|
||||
@@ -47,6 +48,7 @@ namespace PrivaPub.Federation.Actors
|
||||
|
||||
public async Task<FetchedJson> FetchObject(string uri, CancellationToken token)
|
||||
{
|
||||
using var scope = HttpScope.Default("object");
|
||||
var signer = await _localActors.GetInstanceActor(token);
|
||||
var fetched = await Get(uri, signer, token);
|
||||
if (fetched == default)
|
||||
@@ -80,6 +82,7 @@ namespace PrivaPub.Federation.Actors
|
||||
if (!MayFetch(actorUri))
|
||||
return cached;
|
||||
|
||||
using var scope = HttpScope.For("actor");
|
||||
using var fetched = await FetchObject(actorUri, token);
|
||||
var actor = fetched == default ? default : ActorDocument.Parse(fetched.Root);
|
||||
if (actor == default)
|
||||
@@ -100,6 +103,7 @@ namespace PrivaPub.Federation.Actors
|
||||
if (!MayFetch(keyId))
|
||||
return cached;
|
||||
|
||||
using var scope = HttpScope.For("key");
|
||||
using var fetched = await FetchObject(StripFragment(keyId), token);
|
||||
if (fetched == default)
|
||||
return cached;
|
||||
@@ -120,12 +124,15 @@ namespace PrivaPub.Federation.Actors
|
||||
return await Upsert(actor, key, token);
|
||||
}
|
||||
|
||||
public bool KeyTemporarilyUnavailable(string keyId) => Origin.Of(keyId) != default && _http.FailedTemporarily(StripFragment(keyId));
|
||||
|
||||
public async Task<string> ResolveHandle(string handle, CancellationToken token)
|
||||
{
|
||||
var parts = handle?.TrimStart('@').Split('@');
|
||||
if (parts is not { Length: 2 } || string.IsNullOrEmpty(parts[0]) || string.IsNullOrEmpty(parts[1]))
|
||||
return default;
|
||||
|
||||
using var scope = HttpScope.For("webfinger");
|
||||
var query = $"/.well-known/webfinger?resource={Uri.EscapeDataString($"acct:{parts[0]}@{parts[1]}")}";
|
||||
using var fetched = await _http.GetJson($"https://{parts[1]}{query}", "application/jrd+json, application/json", sign: default, token)
|
||||
?? (_options?.CurrentValue.AllowPlainHttp == true
|
||||
@@ -174,7 +181,18 @@ namespace PrivaPub.Federation.Actors
|
||||
.Modify(a => a.FollowingURL, actor.Following)
|
||||
.Modify(a => a.SharedInboxURL, actor.SharedInbox)
|
||||
.Modify(a => a.PictureURL, actor.Icon)
|
||||
.Modify(a => a.ThumbnailURL, actor.Header)
|
||||
.Modify(a => a.PictureDescription, actor.IconDescription)
|
||||
.Modify(a => a.HeaderDescription, actor.HeaderDescription)
|
||||
.Modify(a => a.IsDiscoverable, actor.Discoverable)
|
||||
.Modify(a => a.IsIndexable, actor.Indexable)
|
||||
.Modify(a => a.IsLocked, actor.Locked)
|
||||
.Modify(a => a.IsMemorial, actor.Memorial)
|
||||
.Modify(a => a.MovedToURL, Origin.Of(actor.MovedTo) == default ? default : actor.MovedTo)
|
||||
.Modify(a => a.Published, actor.Published)
|
||||
.Modify(a => a.Emojis, actor.Emojis)
|
||||
.Modify(a => a.Fields, actor.Fields)
|
||||
.Modify(a => a.Features, actor.Features)
|
||||
.Modify(a => a.AvatarType, Enum.TryParse<AvatarType>(actor.Type, out var type) ? type : AvatarType.Person)
|
||||
.Modify(a => a.PublicKeyId, key?.Id)
|
||||
.Modify(a => a.PublicKey, key?.Pem)
|
||||
|
||||
@@ -199,6 +199,33 @@ namespace PrivaPub.Federation.Controllers
|
||||
return Activity(note);
|
||||
}
|
||||
|
||||
[HttpGet, Route("{actor}/parrot-licences/{licenceId}")]
|
||||
public async Task<IActionResult> ParrotLicence(string actor, string licenceId, CancellationToken token)
|
||||
{
|
||||
var author = await _localActors.FindByUserName(actor, token);
|
||||
var licence = author == default ? default : await DB.Default.Find<QuoteLicence>().Match(l => l.ID == licenceId && l.AuthorAvatarId == author.Id).ExecuteFirstAsync(token);
|
||||
var quoted = licence == default ? default : await _dbEntities.Posts.MatchID(licence.PostId).ExecuteFirstAsync(token);
|
||||
if (quoted == default)
|
||||
return NotFound();
|
||||
var uri = Domain.Statuses.QuoteService.LicenceUri(author, licence.ID);
|
||||
if (licence.RevokedAt.HasValue || quoted.DeletedAt.HasValue)
|
||||
return new ContentResult
|
||||
{
|
||||
Content = new JsonObject { ["@context"] = ActivityPubRenderer.ActivityStreams, ["id"] = uri, ["type"] = "Tombstone", ["formerType"] = "QuoteAuthorization" }.ToJsonString(),
|
||||
ContentType = ActivityContentType,
|
||||
StatusCode = StatusCodes.Status410Gone
|
||||
};
|
||||
return Activity(new JsonObject
|
||||
{
|
||||
["@context"] = ActivityPubRenderer.Context(),
|
||||
["id"] = uri,
|
||||
["type"] = "QuoteAuthorization",
|
||||
["attributedTo"] = author.Uri,
|
||||
["interactingObject"] = licence.QuotingObjectURI,
|
||||
["interactionTarget"] = quoted.ObjectURI
|
||||
});
|
||||
}
|
||||
|
||||
[HttpGet, Route("{actor}/grunts/{activityId}")]
|
||||
public async Task<IActionResult> Grunt(string actor, string activityId, CancellationToken token)
|
||||
{
|
||||
@@ -221,7 +248,7 @@ namespace PrivaPub.Federation.Controllers
|
||||
{
|
||||
var local = await _localActors.FindByUserName(actor, token);
|
||||
if (local is not { IsFederated: true })
|
||||
return NotFound();
|
||||
return Answer(_inbox.NoSuchRecipient(Request));
|
||||
return Answer(await _inbox.Receive(Request, local, token));
|
||||
}
|
||||
|
||||
@@ -332,7 +359,9 @@ namespace PrivaPub.Federation.Controllers
|
||||
IActionResult Answer(InboxResult result)
|
||||
{
|
||||
if (result.Error != default)
|
||||
_logger.LogInformation("Inbox refused with {Status}: {Error}", result.StatusCode, result.Error);
|
||||
_logger.LogInformation("Inbox refused with {Status} ({Reason}): {Error}", result.StatusCode, result.Reason, result.Error);
|
||||
if (result.RetryAfterSeconds is { } seconds)
|
||||
Response.Headers.RetryAfter = seconds.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
return result.Error == default ? StatusCode(result.StatusCode) : StatusCode(result.StatusCode, result.Error);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.Post;
|
||||
|
||||
namespace PrivaPub.Federation.Inbox
|
||||
{
|
||||
public sealed record Arrival(string ActivityId, string ActivityType, string ActorURI, string Inbox, string KeyId, string Algorithm,
|
||||
@@ -10,5 +15,64 @@ namespace PrivaPub.Federation.Inbox
|
||||
get => current.Value;
|
||||
set => current.Value = value;
|
||||
}
|
||||
|
||||
public ArrivalVerdict Verdict { get; } = new();
|
||||
|
||||
public static void Drop(string reason) => Current?.Verdict.Drop(reason);
|
||||
|
||||
public static void Reject(string reason) => Current?.Verdict.Reject(reason);
|
||||
|
||||
public static void Accept(string reason) => Current?.Verdict.Accept(reason);
|
||||
|
||||
public static void About(string objectType = default, PostVisibility? visibility = default, DateTime? created = default, LocalActor local = default) =>
|
||||
Current?.Verdict.About(objectType, visibility, created, local);
|
||||
}
|
||||
|
||||
public sealed class ArrivalVerdict
|
||||
{
|
||||
public string Outcome { get; private set; }
|
||||
public string Reason { get; private set; }
|
||||
public string ObjectType { get; private set; }
|
||||
public string Audience { get; private set; }
|
||||
public string LocalKind { get; private set; }
|
||||
public long? AgeSeconds { get; private set; }
|
||||
|
||||
public void Drop(string reason) => Decide(Interactions.Dropped, reason);
|
||||
|
||||
public void Reject(string reason) => Decide(Interactions.Rejected, reason);
|
||||
|
||||
public void Accept(string reason) => Decide(Interactions.Accepted, reason);
|
||||
|
||||
void Decide(string outcome, string reason)
|
||||
{
|
||||
if (Outcome != default)
|
||||
return;
|
||||
Outcome = outcome;
|
||||
Reason = reason;
|
||||
}
|
||||
|
||||
public void About(string objectType, PostVisibility? visibility, DateTime? created, LocalActor local)
|
||||
{
|
||||
ObjectType = objectType ?? ObjectType;
|
||||
if (visibility is { } known)
|
||||
Audience = AudienceOf(known);
|
||||
if (created is { } at)
|
||||
AgeSeconds = Math.Max(0, (long)(DateTime.UtcNow - at).TotalSeconds);
|
||||
if (local != default)
|
||||
LocalKind = local.Kind switch
|
||||
{
|
||||
LocalActorKind.Person => "person",
|
||||
LocalActorKind.Group when !local.IsCircle => "group",
|
||||
LocalActorKind.Application => "application",
|
||||
_ => default
|
||||
};
|
||||
}
|
||||
|
||||
public static string AudienceOf(PostVisibility visibility) => visibility switch
|
||||
{
|
||||
PostVisibility.Public => Interactions.Public,
|
||||
PostVisibility.Unlisted => Interactions.Unlisted,
|
||||
_ => Interactions.Private
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Models.Social;
|
||||
using PrivaPub.Models.User;
|
||||
@@ -15,20 +16,32 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IQuoteService _quotes;
|
||||
|
||||
public AcceptHandler(DbEntities dbEntities, ILocalActorService localActors)
|
||||
public AcceptHandler(DbEntities dbEntities, ILocalActorService localActors, IQuoteService quotes)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
_quotes = quotes;
|
||||
}
|
||||
|
||||
public virtual string Type => "Accept";
|
||||
|
||||
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
|
||||
{
|
||||
if (await _quotes.Answered(activity, actor, accepted: Type == "Accept", token))
|
||||
{
|
||||
Arrival.Accept("quote-answer");
|
||||
return;
|
||||
}
|
||||
var following = await FindFollowing(activity["object"], actor, _dbEntities, _localActors, token);
|
||||
if (following == default)
|
||||
{
|
||||
Arrival.Drop("unknown-object");
|
||||
return;
|
||||
}
|
||||
Arrival.About("Follow");
|
||||
Arrival.Accept("follow-answer");
|
||||
await Apply(following, token);
|
||||
}
|
||||
|
||||
@@ -56,7 +69,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
|
||||
public class RejectHandler : AcceptHandler
|
||||
{
|
||||
public RejectHandler(DbEntities dbEntities, ILocalActorService localActors) : base(dbEntities, localActors)
|
||||
public RejectHandler(DbEntities dbEntities, ILocalActorService localActors, IQuoteService quotes) : base(dbEntities, localActors, quotes)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Domain.Social;
|
||||
using PrivaPub.Domain.Timelines;
|
||||
using PrivaPub.Federation.Actors;
|
||||
@@ -28,8 +29,14 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
readonly IFanout _fanout;
|
||||
readonly IRemoteActorService _remoteActors;
|
||||
|
||||
public AnnounceHandler(DbEntities dbEntities, ILocalActorService localActors, IRemotePosts remotePosts, IFanout fanout, IRemoteActorService remoteActors)
|
||||
readonly IObjectRecords _records;
|
||||
readonly IQuoteService _quotes;
|
||||
|
||||
public AnnounceHandler(DbEntities dbEntities, ILocalActorService localActors, IRemotePosts remotePosts, IFanout fanout, IRemoteActorService remoteActors,
|
||||
IObjectRecords records, IQuoteService quotes)
|
||||
{
|
||||
_records = records;
|
||||
_quotes = quotes;
|
||||
_remoteActors = remoteActors;
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
@@ -50,19 +57,37 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
var objectUri = Id(inner);
|
||||
var announceId = Id(activity);
|
||||
if (objectUri == default || announceId == default)
|
||||
{
|
||||
Arrival.Drop("unparseable");
|
||||
return;
|
||||
}
|
||||
if (await _dbEntities.Posts.Match(p => p.ObjectURI == announceId).ExecuteAnyAsync(token))
|
||||
{
|
||||
Arrival.Drop("duplicate");
|
||||
return;
|
||||
}
|
||||
|
||||
var isLocal = objectUri.StartsWith(_localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase);
|
||||
var original = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
|
||||
var followed = await _dbEntities.Followings.Match(f => f.TargetActorURI == actor.ActorURI && f.State == FollowState.Accepted).ExecuteAnyAsync(token);
|
||||
if (original is not { IsFederatedCopy: false } && !followed)
|
||||
{
|
||||
Arrival.Drop("not-followed");
|
||||
return;
|
||||
}
|
||||
if (original == default && !isLocal)
|
||||
original = await _remotePosts.StoreContext(objectUri, 0, token);
|
||||
if (original == default || original.Visibility is not (PostVisibility.Public or PostVisibility.Unlisted))
|
||||
if (original == default)
|
||||
{
|
||||
Arrival.Drop("fetch-failed");
|
||||
return;
|
||||
}
|
||||
Arrival.About(original.ObjectType ?? "Note", original.Visibility, original.CreationDate);
|
||||
if (original.Visibility is not (PostVisibility.Public or PostVisibility.Unlisted))
|
||||
{
|
||||
Arrival.Drop("not-public");
|
||||
return;
|
||||
}
|
||||
|
||||
var to = Strings(activity["to"]);
|
||||
var cc = Strings(activity["cc"]);
|
||||
@@ -85,15 +110,21 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
UpdateDate = published
|
||||
};
|
||||
if (reblog.Visibility == PostVisibility.Direct)
|
||||
{
|
||||
Arrival.Drop("not-public");
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
await DB.Default.SaveAsync(reblog, token);
|
||||
}
|
||||
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||
{
|
||||
Arrival.Drop("duplicate");
|
||||
return;
|
||||
}
|
||||
Arrival.Accept("stored");
|
||||
Arrival.About(local: original.IsFederatedCopy ? default : await _localActors.FindById(Models.Federation.LocalActorKind.Person, original.GroupUserId, token));
|
||||
|
||||
await DB.Default.Update<PostEntity>().MatchID(original.ID).Modify(b => b.Inc(p => p.ReblogsCount, 1)).ExecuteAsync(token);
|
||||
if (!original.IsFederatedCopy)
|
||||
@@ -103,18 +134,30 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
|
||||
async Task GroupActivity(JsonNode inner, ForeignAvatar group, CancellationToken token)
|
||||
{
|
||||
Arrival.About(Value(inner, "type"));
|
||||
if (group.AvatarType != AvatarType.Group
|
||||
|| !await _dbEntities.Followings.Match(f => f.TargetActorURI == group.ActorURI && f.State == FollowState.Accepted).ExecuteAnyAsync(token))
|
||||
{
|
||||
Arrival.Drop("not-followed");
|
||||
return;
|
||||
}
|
||||
var objectUri = Id(inner["object"]);
|
||||
switch (Value(inner, "type"))
|
||||
{
|
||||
case "Create" when objectUri != default:
|
||||
if (await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteAnyAsync(token))
|
||||
{
|
||||
Arrival.Drop("duplicate");
|
||||
return;
|
||||
}
|
||||
var post = await _remotePosts.StoreContext(objectUri, 0, token);
|
||||
if (post == default)
|
||||
{
|
||||
Arrival.Drop("fetch-failed");
|
||||
return;
|
||||
}
|
||||
Arrival.Accept("stored");
|
||||
Arrival.About(visibility: post.Visibility);
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(p => p.AudienceURI, group.ActorURI).ExecuteAsync(token);
|
||||
post.AudienceURI = group.ActorURI;
|
||||
await _fanout.Distribute(post, token);
|
||||
@@ -123,40 +166,41 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
var stored = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.AudienceURI == group.ActorURI && !p.DeletedAt.HasValue)
|
||||
.ExecuteFirstAsync(token);
|
||||
if (stored == default)
|
||||
{
|
||||
Arrival.Drop("unknown-object");
|
||||
return;
|
||||
}
|
||||
Arrival.About(visibility: stored.Visibility, created: stored.CreationDate);
|
||||
using (var fetched = await _remoteActors.FetchObject(objectUri, token))
|
||||
{
|
||||
var note = fetched == default ? default : NoteParser.Parse(System.Text.Json.Nodes.JsonNode.Parse(fetched.Root.GetRawText()));
|
||||
if (note == default || note.AttributedTo != stored.ActorURI)
|
||||
return;
|
||||
stored.Revisions.Add(new PostRevision
|
||||
{
|
||||
Title = stored.Title,
|
||||
SpoilerText = stored.SpoilerText,
|
||||
ContentHtml = stored.ContentHtml,
|
||||
HasContentWarning = stored.HasContentWarning,
|
||||
EditedAt = stored.EditedAt ?? stored.CreationDate
|
||||
});
|
||||
stored.Title = note.Title;
|
||||
stored.SpoilerText = note.SpoilerText;
|
||||
stored.HasContentWarning = note.Sensitive;
|
||||
stored.Text = note.ContentHtml;
|
||||
stored.ContentHtml = note.ContentHtml;
|
||||
stored.Tags = note.Tags.ToList();
|
||||
stored.Media = note.Attachments.ToList();
|
||||
stored.EditedAt = note.Updated ?? DateTime.UtcNow;
|
||||
await DB.Default.SaveAsync(stored, token);
|
||||
Arrival.Drop(note == default ? "fetch-failed" : "misattributed");
|
||||
return;
|
||||
}
|
||||
Arrival.Accept(await RemoteEdits.Apply(stored, note, Id(inner), _localActors, _records, _quotes, token) ? "edit" : "refresh");
|
||||
}
|
||||
break;
|
||||
case "Delete" when objectUri != default:
|
||||
var deleted = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.AudienceURI == group.ActorURI).ExecuteFirstAsync(token);
|
||||
if (deleted == default)
|
||||
{
|
||||
Arrival.Drop("unknown-object");
|
||||
return;
|
||||
}
|
||||
Arrival.About(visibility: deleted.Visibility, created: deleted.CreationDate);
|
||||
using (var check = await _remoteActors.FetchObject(objectUri, token))
|
||||
if (check != default && Value(System.Text.Json.Nodes.JsonNode.Parse(check.Root.GetRawText()), "type") != "Tombstone")
|
||||
{
|
||||
Arrival.Drop("not-deleted");
|
||||
return;
|
||||
await DB.Default.DeleteAsync<PostEntity>(deleted.ID);
|
||||
await DB.Default.DeleteAsync<TimelineEntry>(e => e.PostId == deleted.ID);
|
||||
}
|
||||
Arrival.Accept("removed");
|
||||
await RemoteDeletes.Remove(deleted, objectUri, token);
|
||||
break;
|
||||
default:
|
||||
Arrival.Drop("unsupported");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ using MongoDB.Entities;
|
||||
using PrivaPub.Domain.Timelines;
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Moderation;
|
||||
using PrivaPub.Domain.Content;
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Federation.Outbox;
|
||||
using PrivaPub.Infrastructure.Ids;
|
||||
@@ -35,11 +37,17 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
readonly IRemotePosts _remotePosts;
|
||||
readonly IGroupDistributor _groups;
|
||||
readonly IObjectRecords _records;
|
||||
readonly IPollService _polls;
|
||||
readonly ILinkPreviews _previews;
|
||||
readonly IQuoteService _quotes;
|
||||
|
||||
public CreateHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery,
|
||||
IDomainBlocks domainBlocks, IFanout fanout, IRemotePosts remotePosts, IGroupDistributor groups, IObjectRecords records)
|
||||
IDomainBlocks domainBlocks, IFanout fanout, IRemotePosts remotePosts, IGroupDistributor groups, IObjectRecords records, IPollService polls, ILinkPreviews previews, IQuoteService quotes)
|
||||
{
|
||||
_quotes = quotes;
|
||||
_previews = previews;
|
||||
_records = records;
|
||||
_polls = polls;
|
||||
_groups = groups;
|
||||
_fanout = fanout;
|
||||
_remotePosts = remotePosts;
|
||||
@@ -60,17 +68,42 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
{
|
||||
using var fetched = await _remoteActors.FetchObject(Id(node), token);
|
||||
node = fetched == default ? default : JsonNode.Parse(fetched.Root.GetRawText());
|
||||
if (node == default)
|
||||
Arrival.Drop("fetch-failed");
|
||||
}
|
||||
var note = NoteParser.Parse(node);
|
||||
if (note == default || note.AttributedTo != author.ActorURI)
|
||||
{
|
||||
Arrival.Drop(note == default ? "unparseable" : "misattributed");
|
||||
return;
|
||||
if (await _dbEntities.Posts.Match(p => p.ObjectURI == note.Id).ExecuteAnyAsync(token)
|
||||
|| await DB.Default.Find<DeletedObject>().Match(d => d.ObjectURI == note.Id).ExecuteAnyAsync(token))
|
||||
}
|
||||
Arrival.About(note.Type);
|
||||
if (note.Title != default && note.InReplyTo != default && ObjectShapes.Text(note.ContentHtml, 1) == default)
|
||||
{
|
||||
var question = await _dbEntities.Posts.Match(p => p.ObjectURI == note.InReplyTo && p.Poll != null).ExecuteFirstAsync(token);
|
||||
if (question != default)
|
||||
{
|
||||
await _polls.Receive(question, author, note.Title, Id(activity), token);
|
||||
Arrival.Accept("poll-vote");
|
||||
Arrival.About(visibility: question.Visibility);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (await _dbEntities.Posts.Match(p => p.ObjectURI == note.Id).ExecuteAnyAsync(token))
|
||||
{
|
||||
Arrival.Drop("duplicate");
|
||||
return;
|
||||
}
|
||||
if (await DB.Default.Find<DeletedObject>().Match(d => d.ObjectURI == note.Id).ExecuteAnyAsync(token))
|
||||
{
|
||||
Arrival.Drop("deleted");
|
||||
return;
|
||||
}
|
||||
|
||||
var to = note.To.Concat(Strings(activity["to"])).Distinct(StringComparer.Ordinal).ToList();
|
||||
var cc = note.Cc.Concat(Strings(activity["cc"])).Distinct(StringComparer.Ordinal).ToList();
|
||||
var visibility = Addressing.Classify(to, cc, author.FollowersURL);
|
||||
Arrival.About(visibility: visibility);
|
||||
|
||||
var addressed = to.Concat(cc)
|
||||
.Concat(Addresses(activity))
|
||||
@@ -94,9 +127,13 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
var circle = localTargets.FirstOrDefault(t => t is { Kind: LocalActorKind.Group, IsCircle: true });
|
||||
if (circle != default)
|
||||
{
|
||||
if (!await IsCircleMember(circle, author.ActorURI, token))
|
||||
return;
|
||||
visibility = PostVisibility.Circle;
|
||||
Arrival.About(visibility: visibility);
|
||||
if (!await IsCircleMember(circle, author.ActorURI, token))
|
||||
{
|
||||
Arrival.Drop("not-addressed");
|
||||
return;
|
||||
}
|
||||
}
|
||||
var group = circle ?? (visibility is PostVisibility.Public or PostVisibility.Unlisted
|
||||
? localTargets.FirstOrDefault(t => t is { Kind: LocalActorKind.Group, IsCircle: false })
|
||||
@@ -109,7 +146,10 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
.Match(f => f.TargetActorURI == author.ActorURI && f.State == FollowState.Accepted)
|
||||
.ExecuteAnyAsync(token);
|
||||
if (visibility == PostVisibility.Direct ? persons.Count == 0 : group == default && persons.Count == 0 && !repliesToLocal && !followed)
|
||||
{
|
||||
Arrival.Drop("not-addressed");
|
||||
return;
|
||||
}
|
||||
if (parent == default && visibility != PostVisibility.Direct)
|
||||
parent = await _remotePosts.Parent(note.InReplyTo, 1, token);
|
||||
|
||||
@@ -133,9 +173,14 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
}
|
||||
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||
{
|
||||
Arrival.Drop("duplicate");
|
||||
return;
|
||||
}
|
||||
Arrival.Accept("stored");
|
||||
Arrival.About(local: group ?? persons.FirstOrDefault());
|
||||
await _records.Record(note, post, ObjectPath.Delivered, refetched, token);
|
||||
await _previews.Wanted(post, token);
|
||||
await _quotes.Resolve(post, note, token);
|
||||
|
||||
if (parent != default)
|
||||
await DB.Default.Update<PostEntity>().MatchID(parent.ID).Modify(b => b.Inc(p => p.RepliesCount, 1)).ExecuteAsync(token);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Federation.Outbox;
|
||||
@@ -27,10 +28,12 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
readonly IRemoteActorService _remoteActors;
|
||||
readonly IDeliveryService _delivery;
|
||||
readonly IGroupDistributor _groups;
|
||||
readonly IQuoteService _quotes;
|
||||
|
||||
public DeleteHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery,
|
||||
IGroupDistributor groups)
|
||||
IGroupDistributor groups, IQuoteService quotes)
|
||||
{
|
||||
_quotes = quotes;
|
||||
_groups = groups;
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
@@ -46,10 +49,15 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
|
||||
var objectUri = Id(delete["object"]);
|
||||
if (!Origin.Same(objectUri, actor.ActorURI))
|
||||
{
|
||||
Arrival.Drop("cross-origin");
|
||||
return;
|
||||
}
|
||||
|
||||
if (objectUri == actor.ActorURI)
|
||||
{
|
||||
Arrival.Accept("actor-delete");
|
||||
Arrival.About(actor.AvatarType.ToString(), created: actor.Published);
|
||||
actor.DeletionAt = DateTime.UtcNow;
|
||||
actor.AccountState = AvatarAccountState.Deleted;
|
||||
await DB.Default.SaveAsync(actor, token);
|
||||
@@ -65,22 +73,19 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
return;
|
||||
}
|
||||
|
||||
await DB.Default.Update<DeletedObject>()
|
||||
.Match(d => d.ObjectURI == objectUri)
|
||||
.Modify(d => d.DeletedAt, DateTime.UtcNow)
|
||||
.Option(o => o.IsUpsert = true)
|
||||
.ExecuteAsync(token);
|
||||
await RemoteDeletes.Tombstone(objectUri, token);
|
||||
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
||||
if (post == default)
|
||||
{
|
||||
Arrival.Accept("tombstone-only");
|
||||
await _quotes.Revoke(objectUri, token);
|
||||
return;
|
||||
await DB.Default.DeleteAsync<PostEntity>(post.ID);
|
||||
await DB.Default.DeleteAsync<ObjectRecord>(r => r.PostId == post.ID || r.ObjectURI == objectUri);
|
||||
await DB.Default.DeleteAsync<TimelineEntry>(e => e.PostId == post.ID || e.ReblogOfPostId == post.ID);
|
||||
}
|
||||
Arrival.Accept("removed");
|
||||
Arrival.About(post.ObjectType, post.Visibility, post.CreationDate);
|
||||
await RemoteDeletes.Remove(post, objectUri, token);
|
||||
if (!string.IsNullOrEmpty(post.GroupId) && await _localActors.FindById(LocalActorKind.Group, post.GroupId, token) is { IsCircle: false } community)
|
||||
await _groups.Announce(community, activity.AsObject(), post.ObjectURI, isNewPost: false, token);
|
||||
await DB.Default.DeleteAsync<PostEntity>(p => p.ReblogOfPostId == post.ID);
|
||||
if (!string.IsNullOrEmpty(post.AnsweringToPostId))
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.AnsweringToPostId).Modify(b => b.Inc(p => p.RepliesCount, -1)).ExecuteAsync(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,11 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
? default
|
||||
: await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
|
||||
if (post == default || !LikeHandler.MaySee(post, actor))
|
||||
{
|
||||
Arrival.Drop(post == default ? "unknown-object" : "not-visible");
|
||||
return;
|
||||
}
|
||||
Arrival.About(post.ObjectType ?? "Note", post.Visibility, post.CreationDate);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -39,8 +43,10 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
}
|
||||
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||
{
|
||||
Arrival.Drop("duplicate");
|
||||
return;
|
||||
}
|
||||
Arrival.Accept("stored");
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(b => b.Inc(p => p.DownvotesCount, 1)).ExecuteAsync(token);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
using PrivaPub.Domain.Social;
|
||||
using PrivaPub.Models.User;
|
||||
using PrivaPub.StaticServices;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
using static PrivaPub.Federation.Objects.ActivityJson;
|
||||
|
||||
namespace PrivaPub.Federation.Inbox.Handlers
|
||||
{
|
||||
public class EmojiReactHandler : IActivityHandler
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly IReactions _reactions;
|
||||
|
||||
public EmojiReactHandler(DbEntities dbEntities, IReactions reactions)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_reactions = reactions;
|
||||
}
|
||||
|
||||
public string Type => "EmojiReact";
|
||||
|
||||
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
|
||||
{
|
||||
var objectUri = Id(activity["object"]);
|
||||
var post = objectUri == default
|
||||
? default
|
||||
: await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
|
||||
if (post == default)
|
||||
{
|
||||
Arrival.Drop("unknown-object");
|
||||
return;
|
||||
}
|
||||
Arrival.About(post.ObjectType ?? "Note", post.Visibility, post.CreationDate);
|
||||
await _reactions.Receive(post, actor, Value(activity, "content"), activity["tag"], Id(activity), token);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,11 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
if (target == default && posts.Count > 0)
|
||||
target = await _localActors.FindById(LocalActorKind.Person, posts[0].GroupUserId, token);
|
||||
if (target == default)
|
||||
{
|
||||
Arrival.Drop("unknown-object");
|
||||
return;
|
||||
}
|
||||
Arrival.Accept("reported");
|
||||
|
||||
await DB.Default.SaveAsync(new Report
|
||||
{
|
||||
|
||||
@@ -45,7 +45,10 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
|
||||
var target = await _localActors.FindByUri(Id(follow["object"]), token);
|
||||
if (target is not { IsFederated: true } || target.Kind == LocalActorKind.Application)
|
||||
{
|
||||
Arrival.Drop("unknown-recipient");
|
||||
return;
|
||||
}
|
||||
if (target.Kind == LocalActorKind.Person
|
||||
&& await DB.Default.Find<Models.Social.Block>().Match(b => b.AvatarId == target.Id && b.TargetActorURI == follower.ActorURI).ExecuteAnyAsync(token))
|
||||
{
|
||||
@@ -58,6 +61,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
["object"] = follow.DeepClone()
|
||||
};
|
||||
await _delivery.Enqueue(target, new[] { follower.InboxURL }, reject, token);
|
||||
Arrival.Reject("blocked");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -75,6 +79,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
record.FollowActivityURI = Id(follow);
|
||||
record.IsAccepted = existing?.IsAccepted == true || !target.ManuallyApprovesFollowers;
|
||||
await DB.Default.SaveAsync(record, token);
|
||||
Arrival.Accept(record.IsAccepted ? "auto-accepted" : "pending");
|
||||
|
||||
if (target.Kind == LocalActorKind.Person)
|
||||
await Notifications.Add(target.Id, record.IsAccepted ? NotificationType.Follow : NotificationType.FollowRequest,
|
||||
|
||||
@@ -30,11 +30,19 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
{
|
||||
var objectUri = Id(activity["object"]);
|
||||
if (objectUri == default || !objectUri.StartsWith(_localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
Arrival.Drop("unknown-object");
|
||||
return;
|
||||
}
|
||||
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
var owner = post == default ? default : await _localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
|
||||
if (owner == default || string.IsNullOrEmpty(actor.InboxURL))
|
||||
{
|
||||
Arrival.Drop("unknown-object");
|
||||
return;
|
||||
}
|
||||
Arrival.About(post.ObjectType ?? "Note", post.Visibility, post.CreationDate);
|
||||
Arrival.Reject("ignored");
|
||||
await _delivery.Enqueue(owner, new[] { actor.InboxURL }, new JsonObject
|
||||
{
|
||||
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||
|
||||
@@ -18,10 +18,12 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
public class LikeHandler : IActivityHandler
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly IReactions _reactions;
|
||||
|
||||
public LikeHandler(DbEntities dbEntities)
|
||||
public LikeHandler(DbEntities dbEntities, IReactions reactions)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_reactions = reactions;
|
||||
}
|
||||
|
||||
public string Type => "Like";
|
||||
@@ -33,7 +35,17 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
? default
|
||||
: await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
|
||||
if (post == default || !MaySee(post, actor))
|
||||
{
|
||||
Arrival.Drop(post == default ? "unknown-object" : "not-visible");
|
||||
return;
|
||||
}
|
||||
Arrival.About(post.ObjectType ?? "Note", post.Visibility, post.CreationDate);
|
||||
var content = Value(activity, "_misskey_reaction") ?? Value(activity, "content");
|
||||
if (!Reactions.IsHeart(content))
|
||||
{
|
||||
await _reactions.Receive(post, actor, content, activity["tag"], Id(activity), token);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
@@ -41,8 +53,10 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
}
|
||||
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||
{
|
||||
Arrival.Drop("duplicate");
|
||||
return;
|
||||
}
|
||||
Arrival.Accept("stored");
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(b => b.Inc(p => p.FavouritesCount, 1)).ExecuteAsync(token);
|
||||
if (!post.IsFederatedCopy)
|
||||
await Notifications.Add(post.GroupUserId, NotificationType.Favourite, actor.ID, actor.ActorURI, post.ID, token);
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Models.User;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Federation.Inbox.Handlers
|
||||
{
|
||||
public class QuoteRequestHandler : IActivityHandler
|
||||
{
|
||||
readonly IQuoteService _quotes;
|
||||
|
||||
public QuoteRequestHandler(IQuoteService quotes)
|
||||
{
|
||||
_quotes = quotes;
|
||||
}
|
||||
|
||||
public string Type => "QuoteRequest";
|
||||
|
||||
public Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token) => _quotes.ReceiveRequest(activity, actor, token);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Domain.Social;
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.Social;
|
||||
@@ -17,11 +18,13 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
{
|
||||
public class UndoHandler : IActivityHandler
|
||||
{
|
||||
readonly IReactions _reactions;
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
|
||||
public UndoHandler(DbEntities dbEntities, ILocalActorService localActors)
|
||||
public UndoHandler(DbEntities dbEntities, ILocalActorService localActors, IReactions reactions)
|
||||
{
|
||||
_reactions = reactions;
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
}
|
||||
@@ -34,17 +37,26 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
var innerType = inner is JsonObject ? Value(inner, "type") : default;
|
||||
var innerId = Id(inner);
|
||||
|
||||
Arrival.About(innerType);
|
||||
var undone = false;
|
||||
if (innerType is null or "Follow")
|
||||
await UndoFollow(inner, innerId, actor, token);
|
||||
undone |= await UndoFollow(inner, innerId, actor, token);
|
||||
if (innerType is null or "Like")
|
||||
await UndoLike(inner, innerId, actor, token);
|
||||
undone |= await UndoLike(inner, innerId, actor, token);
|
||||
if (innerType is null or "Announce")
|
||||
await UndoAnnounce(innerId, actor, token);
|
||||
undone |= await UndoAnnounce(innerId, actor, token);
|
||||
if (innerType is null or "Dislike")
|
||||
await UndoDislike(inner, innerId, actor, token);
|
||||
undone |= await UndoDislike(inner, innerId, actor, token);
|
||||
if (innerType is null or "Like" or "EmojiReact")
|
||||
undone |= await _reactions.Withdraw(actor, innerId, inner is JsonObject ? Id(inner["object"]) : default,
|
||||
inner is JsonObject ? Value(inner, "_misskey_reaction") ?? Value(inner, "content") : default, token);
|
||||
if (undone)
|
||||
Arrival.Accept("undone");
|
||||
else
|
||||
Arrival.Drop("unknown-object");
|
||||
}
|
||||
|
||||
async Task UndoDislike(JsonNode inner, string innerId, ForeignAvatar actor, CancellationToken token)
|
||||
async Task<bool> UndoDislike(JsonNode inner, string innerId, ForeignAvatar actor, CancellationToken token)
|
||||
{
|
||||
var downvote = innerId == default
|
||||
? default
|
||||
@@ -57,16 +69,18 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
: await DB.Default.Find<Downvote>().Match(d => d.PostId == post.ID && d.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
||||
}
|
||||
if (downvote == default)
|
||||
return;
|
||||
return false;
|
||||
await DB.Default.DeleteAsync<Downvote>(downvote.ID);
|
||||
await DB.Default.Update<PostEntity>().MatchID(downvote.PostId).Modify(b => b.Inc(p => p.DownvotesCount, -1)).ExecuteAsync(token);
|
||||
return true;
|
||||
}
|
||||
|
||||
async Task UndoFollow(JsonNode inner, string innerId, ForeignAvatar actor, CancellationToken token)
|
||||
async Task<bool> UndoFollow(JsonNode inner, string innerId, ForeignAvatar actor, CancellationToken token)
|
||||
{
|
||||
var followers = await _dbEntities.Followers.Match(f => f.ActorURI == actor.ActorURI).ExecuteAsync(token);
|
||||
var targetUri = inner is JsonObject ? Id(inner["object"]) : default;
|
||||
var target = targetUri == default ? default : await _localActors.FindByUri(targetUri, token);
|
||||
var undone = false;
|
||||
foreach (var follower in followers)
|
||||
{
|
||||
var matchesActivity = innerId != default && follower.FollowActivityURI == innerId;
|
||||
@@ -77,10 +91,12 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
await DB.Default.DeleteAsync<Follower>(follower.ID);
|
||||
if (follower.LocalActorKind == LocalActorKind.Group)
|
||||
await RemoveForeignMember(follower.LocalActorId, actor.ActorURI, token);
|
||||
undone = true;
|
||||
}
|
||||
return undone;
|
||||
}
|
||||
|
||||
async Task UndoLike(JsonNode inner, string innerId, ForeignAvatar actor, CancellationToken token)
|
||||
async Task<bool> UndoLike(JsonNode inner, string innerId, ForeignAvatar actor, CancellationToken token)
|
||||
{
|
||||
var favourite = innerId == default
|
||||
? default
|
||||
@@ -93,23 +109,25 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
: await _dbEntities.Favourites.Match(f => f.PostId == post.ID && f.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
||||
}
|
||||
if (favourite == default)
|
||||
return;
|
||||
return false;
|
||||
await DB.Default.DeleteAsync<Favourite>(favourite.ID);
|
||||
await DB.Default.Update<PostEntity>().MatchID(favourite.PostId).Modify(b => b.Inc(p => p.FavouritesCount, -1)).ExecuteAsync(token);
|
||||
return true;
|
||||
}
|
||||
|
||||
async Task UndoAnnounce(string innerId, ForeignAvatar actor, CancellationToken token)
|
||||
async Task<bool> UndoAnnounce(string innerId, ForeignAvatar actor, CancellationToken token)
|
||||
{
|
||||
if (innerId == default)
|
||||
return;
|
||||
return false;
|
||||
var reblog = await _dbEntities.Posts
|
||||
.Match(p => p.ObjectURI == innerId && p.ActorURI == actor.ActorURI && p.ReblogOfPostId != null)
|
||||
.ExecuteFirstAsync(token);
|
||||
if (reblog == default)
|
||||
return;
|
||||
return false;
|
||||
await DB.Default.DeleteAsync<PostEntity>(reblog.ID);
|
||||
await DB.Default.DeleteAsync<TimelineEntry>(e => e.PostId == reblog.ID);
|
||||
await DB.Default.Update<PostEntity>().MatchID(reblog.ReblogOfPostId).Modify(b => b.Inc(p => p.ReblogsCount, -1)).ExecuteAsync(token);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Federation.Outbox;
|
||||
using PrivaPub.Models.Post;
|
||||
@@ -17,17 +18,18 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
{
|
||||
public class UpdateHandler : IActivityHandler
|
||||
{
|
||||
const int MaxRevisions = 20;
|
||||
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IRemoteActorService _remoteActors;
|
||||
readonly IGroupDistributor _groups;
|
||||
readonly IObjectRecords _records;
|
||||
readonly IQuoteService _quotes;
|
||||
|
||||
public UpdateHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IGroupDistributor groups,
|
||||
IObjectRecords records)
|
||||
IObjectRecords records, IQuoteService quotes)
|
||||
{
|
||||
_quotes = quotes;
|
||||
_records = records;
|
||||
_groups = groups;
|
||||
_dbEntities = dbEntities;
|
||||
@@ -43,57 +45,33 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
if (Id(inner) == actor.ActorURI)
|
||||
{
|
||||
await _remoteActors.GetActor(actor.ActorURI, refresh: true, token);
|
||||
Arrival.Accept("actor-refresh");
|
||||
Arrival.About(actor.AvatarType.ToString());
|
||||
return;
|
||||
}
|
||||
if (inner is not JsonObject || !Origin.Same(Id(inner), actor.ActorURI))
|
||||
{
|
||||
Arrival.Drop("cross-origin");
|
||||
return;
|
||||
}
|
||||
var note = NoteParser.Parse(inner);
|
||||
if (note == default || note.AttributedTo != actor.ActorURI)
|
||||
{
|
||||
Arrival.Drop(note == default ? "unparseable" : "misattributed");
|
||||
return;
|
||||
}
|
||||
|
||||
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == note.Id && p.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
||||
if (post == default || post.DeletedAt.HasValue)
|
||||
{
|
||||
Arrival.Drop("unknown-object");
|
||||
return;
|
||||
|
||||
post.Revisions.Add(new PostRevision
|
||||
{
|
||||
Title = post.Title,
|
||||
SpoilerText = post.SpoilerText,
|
||||
ContentHtml = post.ContentHtml,
|
||||
HasContentWarning = post.HasContentWarning,
|
||||
EditedAt = post.EditedAt ?? post.CreationDate
|
||||
});
|
||||
if (post.Revisions.Count > MaxRevisions)
|
||||
post.Revisions.RemoveRange(0, post.Revisions.Count - MaxRevisions);
|
||||
|
||||
var mentions = new List<PostMention>();
|
||||
foreach (var mention in note.Mentions)
|
||||
{
|
||||
var local = await _localActors.FindByUri(mention.ActorURI, token);
|
||||
mentions.Add(new PostMention { ActorURI = mention.ActorURI, Handle = mention.Handle, IsLocal = local != default, AccountId = local?.Id });
|
||||
}
|
||||
Arrival.About(note.Type, post.Visibility, post.CreationDate);
|
||||
|
||||
post.Title = note.Title;
|
||||
post.SpoilerText = note.SpoilerText;
|
||||
post.Excerpt = note.Excerpt;
|
||||
post.Source = note.Source;
|
||||
post.CoverURL = note.CoverURL;
|
||||
post.Link = note.Link;
|
||||
post.Video = note.Video;
|
||||
post.Audio = note.Audio;
|
||||
post.Event = note.Event;
|
||||
post.HasContentWarning = note.Sensitive;
|
||||
post.Text = note.ContentHtml;
|
||||
post.ContentHtml = note.ContentHtml;
|
||||
post.Language = note.Language;
|
||||
post.Tags = note.Tags.ToList();
|
||||
post.Mentions = mentions;
|
||||
post.Media = note.Attachments.ToList();
|
||||
post.EditedAt = note.Updated ?? DateTime.UtcNow;
|
||||
post.UpdateDate = DateTime.UtcNow;
|
||||
await DB.Default.SaveAsync(post, token);
|
||||
await _records.Revise(note, Id(activity), token);
|
||||
if (!string.IsNullOrEmpty(post.GroupId) && await _localActors.FindById(Models.Federation.LocalActorKind.Group, post.GroupId, token) is { IsCircle: false } community)
|
||||
var edited = await RemoteEdits.Apply(post, note, Id(activity), _localActors, _records, _quotes, token);
|
||||
Arrival.Accept(edited ? "edit" : "refresh");
|
||||
if (edited && !string.IsNullOrEmpty(post.GroupId) && await _localActors.FindById(Models.Federation.LocalActorKind.Group, post.GroupId, token) is { IsCircle: false } community)
|
||||
await _groups.Announce(community, activity.AsObject(), post.ObjectURI, isNewPost: false, token);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Models.Statistics;
|
||||
using PrivaPub.Models.User;
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
@@ -21,12 +25,15 @@ namespace PrivaPub.Federation.Inbox
|
||||
readonly IRemoteActorService _remoteActors;
|
||||
readonly IReadOnlyDictionary<string, IActivityHandler> _handlers;
|
||||
readonly ILogger<InboxProcessor> _logger;
|
||||
readonly IInteractionLedger _ledger;
|
||||
|
||||
public InboxProcessor(IRemoteActorService remoteActors, IEnumerable<IActivityHandler> handlers, ILogger<InboxProcessor> logger)
|
||||
public InboxProcessor(IRemoteActorService remoteActors, IEnumerable<IActivityHandler> handlers, ILogger<InboxProcessor> logger,
|
||||
IInteractionLedger ledger = default)
|
||||
{
|
||||
_remoteActors = remoteActors;
|
||||
_handlers = handlers.ToDictionary(h => h.Type, StringComparer.Ordinal);
|
||||
_logger = logger;
|
||||
_ledger = ledger;
|
||||
}
|
||||
|
||||
public JobKind Kind => JobKind.ProcessInbox;
|
||||
@@ -36,28 +43,67 @@ namespace PrivaPub.Federation.Inbox
|
||||
|
||||
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
|
||||
{
|
||||
var started = Stopwatch.GetTimestamp();
|
||||
var payload = JsonSerializer.Deserialize<InboxPayload>(job.Payload);
|
||||
var activity = payload == default ? default : JsonNode.Parse(payload.Activity);
|
||||
var type = Value(activity, "type");
|
||||
if (type == default || !_handlers.TryGetValue(type, out var handler))
|
||||
{
|
||||
Record(job, payload, activity, type, started, new ArrivalVerdict(), Interactions.Dropped, "unknown-type");
|
||||
return JobOutcome.Done;
|
||||
}
|
||||
|
||||
var actor = await _remoteActors.GetActor(payload.ActorURI, refresh: false, token);
|
||||
if (actor == default)
|
||||
{
|
||||
Record(job, payload, activity, type, started, new ArrivalVerdict(), Interactions.Deferred, "actor-unavailable");
|
||||
return JobOutcome.Retry("the actor could not be loaded");
|
||||
}
|
||||
|
||||
Arrival.Current = new Arrival(Id(activity), type, actor.ActorURI, payload.Inbox, payload.KeyId, payload.Algorithm,
|
||||
var arrival = new Arrival(Id(activity), type, actor.ActorURI, payload.Inbox, payload.KeyId, payload.Algorithm,
|
||||
payload.SignedHeaders ?? Array.Empty<string>(), payload.ReceivedAt ?? job.CreatedAt, activity["@context"]?.ToJsonString());
|
||||
Arrival.Current = arrival;
|
||||
try
|
||||
{
|
||||
await handler.Handle(activity, actor, token);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
Record(job, payload, activity, type, started, arrival.Verdict, Interactions.Failed, ex.GetType().Name.ToLowerInvariant());
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Arrival.Current = default;
|
||||
}
|
||||
Record(job, payload, activity, type, started, arrival.Verdict, default, default);
|
||||
_logger.LogInformation("Processed {Type} {Id} from {Actor}", type, Id(activity), actor.ActorURI);
|
||||
return JobOutcome.Done;
|
||||
}
|
||||
|
||||
void Record(Job job, InboxPayload payload, JsonNode activity, string type, long started, ArrivalVerdict verdict, string outcome, string reason)
|
||||
{
|
||||
if (_ledger == default || payload == default)
|
||||
return;
|
||||
var embedded = activity?["object"] as JsonObject;
|
||||
_ledger.Record(new InteractionEvent
|
||||
{
|
||||
Channel = Interactions.In,
|
||||
Host = Interactions.HostOf(payload.ActorURI),
|
||||
Activity = type,
|
||||
Object = verdict.ObjectType ?? Value(embedded, "type"),
|
||||
Outcome = outcome ?? verdict.Outcome ?? Interactions.Accepted,
|
||||
Reason = reason ?? verdict.Reason,
|
||||
LatencyMs = (int)Stopwatch.GetElapsedTime(started).TotalMilliseconds,
|
||||
WaitMs = (int)Math.Min(int.MaxValue, Math.Max(0, (DateTime.UtcNow - (payload.ReceivedAt ?? job.CreatedAt)).TotalMilliseconds)),
|
||||
Attempt = job.Attempts,
|
||||
Audience = verdict.Audience ?? ActivityShape.Of(activity).Audience,
|
||||
LocalKind = verdict.LocalKind,
|
||||
AgeSeconds = verdict.AgeSeconds,
|
||||
Inbox = payload.Inbox,
|
||||
Signature = payload.Algorithm == default ? default : "cavage:" + payload.Algorithm,
|
||||
Features = type is "Create" or "Update" && embedded != default ? ObjectFeatures.Detect(embedded) : default
|
||||
}, payload.ActorURI);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,12 @@ using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Moderation;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Federation.Signing;
|
||||
using PrivaPub.Infrastructure.Http;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Models.Statistics;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
@@ -13,7 +16,7 @@ using static PrivaPub.Federation.Objects.ActivityJson;
|
||||
|
||||
namespace PrivaPub.Federation.Inbox
|
||||
{
|
||||
public sealed record InboxResult(int StatusCode, string Error = default);
|
||||
public sealed record InboxResult(int StatusCode, string Error = default, int? RetryAfterSeconds = default, string Reason = default);
|
||||
|
||||
public sealed record InboxPayload(string ActorURI, string Activity, string Inbox = default, string KeyId = default, string Algorithm = default,
|
||||
string[] SignedHeaders = default, DateTime? ReceivedAt = default);
|
||||
@@ -21,10 +24,13 @@ namespace PrivaPub.Federation.Inbox
|
||||
public interface IInboxReceiver
|
||||
{
|
||||
Task<InboxResult> Receive(HttpRequest request, LocalActor recipient, CancellationToken token);
|
||||
InboxResult NoSuchRecipient(HttpRequest request);
|
||||
}
|
||||
|
||||
public class InboxReceiver : IInboxReceiver
|
||||
{
|
||||
const int KeyRetrySeconds = 300;
|
||||
|
||||
const int MaxBodyBytes = 1024 * 1024;
|
||||
|
||||
readonly ILocalActorService _localActors;
|
||||
@@ -32,27 +38,88 @@ namespace PrivaPub.Federation.Inbox
|
||||
readonly IJobQueue _queue;
|
||||
readonly IDomainBlocks _domainBlocks;
|
||||
readonly ILogger<InboxReceiver> _logger;
|
||||
readonly IInteractionLedger _ledger;
|
||||
|
||||
public InboxReceiver(ILocalActorService localActors, IRemoteActorService remoteActors, IJobQueue queue, IDomainBlocks domainBlocks,
|
||||
ILogger<InboxReceiver> logger)
|
||||
ILogger<InboxReceiver> logger, IInteractionLedger ledger = default)
|
||||
{
|
||||
_localActors = localActors;
|
||||
_remoteActors = remoteActors;
|
||||
_queue = queue;
|
||||
_domainBlocks = domainBlocks;
|
||||
_logger = logger;
|
||||
_ledger = ledger;
|
||||
}
|
||||
|
||||
sealed class Receipt
|
||||
{
|
||||
public string Type;
|
||||
public string ObjectType;
|
||||
public string ClaimedHost;
|
||||
public string VerifiedActor;
|
||||
public string Inbox;
|
||||
public string Signature = "none";
|
||||
public long? Bytes;
|
||||
public bool Blocked;
|
||||
}
|
||||
|
||||
public InboxResult NoSuchRecipient(HttpRequest request)
|
||||
{
|
||||
var result = new InboxResult(StatusCodes.Status404NotFound, "no such local actor", Reason: "unknown-recipient");
|
||||
var keyId = HttpSignatures.Parse(request.Headers["Signature"].ToString())?.KeyId;
|
||||
Record(new Receipt { ClaimedHost = HostOf(keyId), Inbox = "personal", Bytes = request.ContentLength }, result, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<InboxResult> Receive(HttpRequest request, LocalActor recipient, CancellationToken token)
|
||||
{
|
||||
using var scope = HttpScope.Triggered("verify");
|
||||
var started = System.Diagnostics.Stopwatch.GetTimestamp();
|
||||
var receipt = new Receipt { Inbox = recipient == default ? "shared" : "personal", Bytes = request.ContentLength };
|
||||
InboxResult result = default;
|
||||
try
|
||||
{
|
||||
result = await Receive(request, recipient, receipt, token);
|
||||
return result;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Record(receipt, result, (int)System.Diagnostics.Stopwatch.GetElapsedTime(started).TotalMilliseconds);
|
||||
}
|
||||
}
|
||||
|
||||
void Record(Receipt receipt, InboxResult result, int latencyMs)
|
||||
{
|
||||
if (_ledger == default || result == default)
|
||||
return;
|
||||
var verified = receipt.VerifiedActor != default;
|
||||
_ledger.Record(new InteractionEvent
|
||||
{
|
||||
Channel = Interactions.Receive,
|
||||
Host = verified ? Interactions.HostOf(receipt.VerifiedActor) : receipt.ClaimedHost,
|
||||
Activity = receipt.Type,
|
||||
Object = receipt.ObjectType,
|
||||
Status = result.StatusCode,
|
||||
Outcome = result.StatusCode == StatusCodes.Status202Accepted ? Interactions.Queued : Interactions.Refused,
|
||||
Reason = result.Reason,
|
||||
LatencyMs = latencyMs,
|
||||
Bytes = receipt.Bytes,
|
||||
Inbox = receipt.Inbox,
|
||||
Signature = receipt.Signature
|
||||
}, verified ? receipt.VerifiedActor : default, hostClaimed: !verified && !receipt.Blocked);
|
||||
}
|
||||
|
||||
async Task<InboxResult> Receive(HttpRequest request, LocalActor recipient, Receipt receipt, CancellationToken token)
|
||||
{
|
||||
if (request.ContentLength > MaxBodyBytes)
|
||||
return new(StatusCodes.Status413PayloadTooLarge);
|
||||
return new(StatusCodes.Status413PayloadTooLarge, Reason: "too-large");
|
||||
|
||||
using var buffer = new MemoryStream();
|
||||
await request.Body.CopyToAsync(buffer, token);
|
||||
if (buffer.Length > MaxBodyBytes)
|
||||
return new(StatusCodes.Status413PayloadTooLarge);
|
||||
return new(StatusCodes.Status413PayloadTooLarge, Reason: "too-large");
|
||||
var body = buffer.ToArray();
|
||||
receipt.Bytes = body.Length;
|
||||
|
||||
JsonNode activity;
|
||||
try
|
||||
@@ -61,39 +128,51 @@ namespace PrivaPub.Federation.Inbox
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return new(StatusCodes.Status400BadRequest, "the body is not JSON");
|
||||
return new(StatusCodes.Status400BadRequest, "the body is not JSON", Reason: "not-json");
|
||||
}
|
||||
if (activity is not JsonObject)
|
||||
return new(StatusCodes.Status400BadRequest, "the body is not an activity");
|
||||
return new(StatusCodes.Status400BadRequest, "the body is not an activity", Reason: "not-activity");
|
||||
|
||||
var type = Value(activity, "type");
|
||||
var actorUri = Id(activity["actor"]);
|
||||
receipt.Type = type;
|
||||
receipt.ObjectType = activity["object"] is JsonObject embedded ? Value(embedded, "type") : default;
|
||||
receipt.ClaimedHost = HostOf(actorUri);
|
||||
if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(actorUri))
|
||||
return new(StatusCodes.Status400BadRequest, "type and actor are required");
|
||||
return new(StatusCodes.Status400BadRequest, "type and actor are required", Reason: "missing-type-or-actor");
|
||||
|
||||
var parameters = HttpSignatures.Parse(request.Headers["Signature"].ToString());
|
||||
if (parameters == default)
|
||||
return new(StatusCodes.Status401Unauthorized, "missing or unreadable Signature header");
|
||||
return new(StatusCodes.Status401Unauthorized, "missing or unreadable Signature header", Reason: "no-signature");
|
||||
receipt.Signature = "cavage:" + parameters.Algorithm;
|
||||
receipt.ClaimedHost ??= HostOf(parameters.KeyId);
|
||||
if (_domainBlocks.IsSuspended(HostOf(parameters.KeyId)) || _domainBlocks.IsSuspended(HostOf(actorUri)))
|
||||
return new(StatusCodes.Status202Accepted);
|
||||
{
|
||||
receipt.Blocked = true;
|
||||
receipt.ClaimedHost = _domainBlocks.IsSuspended(HostOf(actorUri)) ? HostOf(actorUri) : HostOf(parameters.KeyId);
|
||||
return new(StatusCodes.Status202Accepted, Reason: "suspended");
|
||||
}
|
||||
|
||||
var requestProblem = HttpSignatures.CheckRequest(request, parameters, body);
|
||||
if (requestProblem != default)
|
||||
return new(StatusCodes.Status401Unauthorized, requestProblem);
|
||||
return new(StatusCodes.Status401Unauthorized, requestProblem, Reason: HttpSignatures.ProblemCode(requestProblem));
|
||||
|
||||
var signingString = HttpSignatures.SigningString(request, parameters);
|
||||
var keyOwner = await _remoteActors.GetActorByKeyId(parameters.KeyId, refresh: false, token);
|
||||
if (keyOwner == default && type == "Delete" && Id(activity["object"]) == actorUri)
|
||||
return new(StatusCodes.Status202Accepted);
|
||||
return new(StatusCodes.Status202Accepted, Reason: "self-delete-unknown-key");
|
||||
if (keyOwner == default || !HttpSignatures.Verify(keyOwner.PublicKey, signingString, parameters.Signature))
|
||||
{
|
||||
keyOwner = await _remoteActors.GetActorByKeyId(parameters.KeyId, refresh: true, token);
|
||||
if (keyOwner == default && _remoteActors.KeyTemporarilyUnavailable(parameters.KeyId))
|
||||
return new(StatusCodes.Status503ServiceUnavailable, "the signing key could not be fetched; try again later", KeyRetrySeconds, "key-unavailable");
|
||||
if (keyOwner == default || !HttpSignatures.Verify(keyOwner.PublicKey, signingString, parameters.Signature))
|
||||
return new(StatusCodes.Status401Unauthorized, "the signature does not verify");
|
||||
return new(StatusCodes.Status401Unauthorized, "the signature does not verify", Reason: "signature-invalid");
|
||||
}
|
||||
|
||||
if (!string.Equals(keyOwner.ActorURI, actorUri, StringComparison.Ordinal))
|
||||
return new(StatusCodes.Status401Unauthorized, "the activity's actor is not the key's owner");
|
||||
return new(StatusCodes.Status401Unauthorized, "the activity's actor is not the key's owner", Reason: "actor-not-key-owner");
|
||||
receipt.VerifiedActor = actorUri;
|
||||
|
||||
var shapeProblem = await ShapeProblem(type, activity, actorUri, token);
|
||||
if (shapeProblem != default)
|
||||
@@ -102,19 +181,19 @@ namespace PrivaPub.Federation.Inbox
|
||||
var activityId = Id(activity);
|
||||
var payload = new InboxPayload(actorUri, activity.ToJsonString(), recipient == default ? "shared" : "personal", parameters.KeyId,
|
||||
parameters.Algorithm, parameters.Headers, DateTime.UtcNow);
|
||||
await _queue.Enqueue(JobKind.ProcessInbox, JsonSerializer.Serialize(payload),
|
||||
var queued = await _queue.Enqueue(JobKind.ProcessInbox, JsonSerializer.Serialize(payload),
|
||||
new Uri(actorUri).Host.ToLowerInvariant(), activityId == default ? default : "inbox|" + activityId, token);
|
||||
_logger.LogInformation("Inbox {Recipient}: {Type} from {Actor} queued", recipient?.Handle ?? "shared", type, actorUri);
|
||||
return new(StatusCodes.Status202Accepted);
|
||||
return new(StatusCodes.Status202Accepted, Reason: queued ? "queued" : "duplicate");
|
||||
}
|
||||
|
||||
static string HostOf(string uri) => Uri.TryCreate(uri, UriKind.Absolute, out var parsed) ? parsed.Host : default;
|
||||
static string HostOf(string uri) => Uri.TryCreate(uri, UriKind.Absolute, out var parsed) ? parsed.Host.ToLowerInvariant() : default;
|
||||
|
||||
async Task<InboxResult> ShapeProblem(string type, JsonNode activity, string actorUri, CancellationToken token)
|
||||
{
|
||||
var activityId = Id(activity);
|
||||
if (activityId != default && !Origin.Same(activityId, actorUri))
|
||||
return new(StatusCodes.Status400BadRequest, "the activity's id is not on its actor's origin");
|
||||
return new(StatusCodes.Status400BadRequest, "the activity's id is not on its actor's origin", Reason: "id-cross-origin");
|
||||
|
||||
var inner = activity["object"];
|
||||
switch (type)
|
||||
@@ -122,14 +201,14 @@ namespace PrivaPub.Federation.Inbox
|
||||
case "Follow":
|
||||
var target = await _localActors.FindByUri(Id(inner), token);
|
||||
if (target is not { IsFederated: true } || target.Kind == LocalActorKind.Application)
|
||||
return new(StatusCodes.Status404NotFound, "no such local actor");
|
||||
return new(StatusCodes.Status404NotFound, "no such local actor", Reason: "unknown-recipient");
|
||||
break;
|
||||
case "Undo" when inner is JsonObject && Id(inner["actor"]) != actorUri:
|
||||
return new(StatusCodes.Status400BadRequest, "an actor can only undo its own activities");
|
||||
return new(StatusCodes.Status400BadRequest, "an actor can only undo its own activities", Reason: "undo-foreign");
|
||||
case "Create" or "Update" when inner is JsonObject && Origin.Same(Id(inner), actorUri)
|
||||
&& inner["attributedTo"] != default && Id(inner["attributedTo"]) != actorUri
|
||||
&& Value(inner, "type") is not ("Person" or "Service" or "Application" or "Group" or "Organization"):
|
||||
return new(StatusCodes.Status400BadRequest, "the object is not attributed to the actor");
|
||||
return new(StatusCodes.Status400BadRequest, "the object is not attributed to the actor", Reason: "misattributed");
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Domain.Statuses;
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Models.Federation;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Models.Social;
|
||||
|
||||
using PostEntity = PrivaPub.Models.Post.Post;
|
||||
|
||||
namespace PrivaPub.Federation.Inbox
|
||||
{
|
||||
public static class RemoteEdits
|
||||
{
|
||||
public const int MaxRevisions = 20;
|
||||
|
||||
public static bool IsEdit(NoteDocument note, PostEntity post) =>
|
||||
note.Updated is { } updated && updated > (post.EditedAt ?? post.CreationDate);
|
||||
|
||||
public static async Task<bool> Apply(PostEntity post, NoteDocument note, string activityId, ILocalActorService localActors, IObjectRecords records,
|
||||
IQuoteService quotes, CancellationToken token)
|
||||
{
|
||||
post.Poll = note.Poll ?? post.Poll;
|
||||
post.QuotePolicy = note.QuotePolicy;
|
||||
post.Video = note.Video ?? post.Video;
|
||||
post.Audio = note.Audio ?? post.Audio;
|
||||
post.Event = note.Event ?? post.Event;
|
||||
if (!IsEdit(note, post))
|
||||
{
|
||||
await DB.Default.SaveAsync(post, token);
|
||||
await records.Revise(note, activityId, token);
|
||||
await Requote(post, note, quotes, token);
|
||||
return false;
|
||||
}
|
||||
|
||||
post.Revisions.Add(new PostRevision
|
||||
{
|
||||
Title = post.Title,
|
||||
SpoilerText = post.SpoilerText,
|
||||
ContentHtml = post.ContentHtml,
|
||||
HasContentWarning = post.HasContentWarning,
|
||||
EditedAt = post.EditedAt ?? post.CreationDate
|
||||
});
|
||||
if (post.Revisions.Count > MaxRevisions)
|
||||
post.Revisions.RemoveRange(0, post.Revisions.Count - MaxRevisions);
|
||||
|
||||
var mentions = new List<PostMention>();
|
||||
foreach (var mention in note.Mentions)
|
||||
{
|
||||
var local = await localActors.FindByUri(mention.ActorURI, token);
|
||||
mentions.Add(new PostMention { ActorURI = mention.ActorURI, Handle = mention.Handle, IsLocal = local != default, AccountId = local?.Id });
|
||||
}
|
||||
|
||||
post.Title = note.Title;
|
||||
post.SpoilerText = note.SpoilerText;
|
||||
post.Excerpt = note.Excerpt;
|
||||
post.Source = note.Source;
|
||||
post.Emojis = note.Emojis.ToList();
|
||||
post.CoverURL = note.CoverURL;
|
||||
post.Link = note.Link;
|
||||
post.HasContentWarning = note.Sensitive;
|
||||
post.Text = note.ContentHtml;
|
||||
post.ContentHtml = note.ContentHtml;
|
||||
post.Language = note.Language;
|
||||
post.Tags = note.Tags.ToList();
|
||||
post.Mentions = mentions;
|
||||
post.Media = note.Attachments.ToList();
|
||||
post.EditedAt = note.Updated ?? DateTime.UtcNow;
|
||||
post.UpdateDate = DateTime.UtcNow;
|
||||
await DB.Default.SaveAsync(post, token);
|
||||
await records.Revise(note, activityId, token);
|
||||
await Requote(post, note, quotes, token);
|
||||
return true;
|
||||
}
|
||||
|
||||
static async Task Requote(PostEntity post, NoteDocument note, IQuoteService quotes, CancellationToken token)
|
||||
{
|
||||
if (note.QuoteUri != post.QuoteURI || note.QuoteAuthorization != post.QuoteAuthorizationURI || post.QuoteState != QuoteState.Accepted)
|
||||
await quotes.Resolve(post, note, token);
|
||||
}
|
||||
}
|
||||
|
||||
public static class RemoteDeletes
|
||||
{
|
||||
public static async Task Tombstone(string objectUri, CancellationToken token) =>
|
||||
await DB.Default.Update<DeletedObject>()
|
||||
.Match(d => d.ObjectURI == objectUri)
|
||||
.Modify(d => d.DeletedAt, DateTime.UtcNow)
|
||||
.Option(o => o.IsUpsert = true)
|
||||
.ExecuteAsync(token);
|
||||
|
||||
public static async Task Remove(PostEntity post, string objectUri, CancellationToken token)
|
||||
{
|
||||
await Tombstone(objectUri, token);
|
||||
await DB.Default.DeleteAsync<PostEntity>(post.ID);
|
||||
await DB.Default.DeleteAsync<ObjectRecord>(r => r.PostId == post.ID || r.ObjectURI == objectUri);
|
||||
await DB.Default.DeleteAsync<TimelineEntry>(e => e.PostId == post.ID || e.ReblogOfPostId == post.ID);
|
||||
await DB.Default.DeleteAsync<PostEntity>(p => p.ReblogOfPostId == post.ID);
|
||||
if (!string.IsNullOrEmpty(post.AnsweringToPostId))
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.AnsweringToPostId).Modify(b => b.Inc(p => p.RepliesCount, -1)).ExecuteAsync(token);
|
||||
if (post.QuoteState == QuoteState.Accepted && !string.IsNullOrEmpty(post.QuotedPostId))
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.QuotedPostId).Modify(b => b.Inc(p => p.QuotesCount, -1)).ExecuteAsync(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Infrastructure.Http;
|
||||
using PrivaPub.Domain.Content;
|
||||
using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Moderation;
|
||||
using PrivaPub.Federation.Objects;
|
||||
@@ -39,10 +41,12 @@ namespace PrivaPub.Federation.Inbox
|
||||
readonly IDomainBlocks _domainBlocks;
|
||||
readonly IJobQueue _queue;
|
||||
readonly IObjectRecords _records;
|
||||
readonly ILinkPreviews _previews;
|
||||
|
||||
public RemotePosts(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDomainBlocks domainBlocks,
|
||||
IJobQueue queue, IObjectRecords records)
|
||||
IJobQueue queue, IObjectRecords records, ILinkPreviews previews)
|
||||
{
|
||||
_previews = previews;
|
||||
_records = records;
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
@@ -78,6 +82,9 @@ namespace PrivaPub.Federation.Inbox
|
||||
SpoilerText = note.SpoilerText,
|
||||
Excerpt = note.Excerpt,
|
||||
Source = note.Source,
|
||||
Poll = note.Poll,
|
||||
QuotePolicy = note.QuotePolicy,
|
||||
Emojis = note.Emojis.ToList(),
|
||||
CoverURL = note.CoverURL,
|
||||
Link = note.Link,
|
||||
Video = note.Video,
|
||||
@@ -117,6 +124,7 @@ namespace PrivaPub.Federation.Inbox
|
||||
if (existing != default || depth > MaxDepth || await DB.Default.Find<DeletedObject>().Match(d => d.ObjectURI == objectUri).ExecuteAnyAsync(token))
|
||||
return existing;
|
||||
|
||||
using var scope = HttpScope.For("context");
|
||||
using var fetched = await _remoteActors.FetchObject(objectUri, token);
|
||||
var note = fetched == default ? default : NoteParser.Parse(JsonNode.Parse(fetched.Root.GetRawText()));
|
||||
if (note == default || !Origin.Same(note.Id, note.AttributedTo))
|
||||
@@ -141,6 +149,7 @@ namespace PrivaPub.Federation.Inbox
|
||||
return await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
|
||||
}
|
||||
await _records.Record(note, post, ObjectPath.Fetched, refetched: false, token);
|
||||
await _previews.Wanted(post, token);
|
||||
|
||||
if (grandparent == default && !string.IsNullOrEmpty(note.InReplyTo) && depth < MaxDepth)
|
||||
await _queue.Enqueue(JobKind.FetchAncestors, JsonSerializer.Serialize(new AncestorsPayload(post.ID, depth + 1)), new Uri(note.InReplyTo).Host,
|
||||
|
||||
@@ -44,9 +44,11 @@ namespace PrivaPub.Federation.Moderation
|
||||
|
||||
public bool IsSuspended(string host) => Find(host)?.Severity == DomainBlockSeverity.Suspend;
|
||||
|
||||
public async Task Reload(CancellationToken token)
|
||||
public async Task Reload(CancellationToken token) =>
|
||||
Load(await DB.Default.Find<DomainBlock>().ExecuteAsync(token));
|
||||
|
||||
public void Load(IEnumerable<DomainBlock> blocks)
|
||||
{
|
||||
var blocks = await DB.Default.Find<DomainBlock>().ExecuteAsync(token);
|
||||
_blocks = blocks.Where(b => !string.IsNullOrEmpty(b.Domain))
|
||||
.GroupBy(b => Normalise(b.Domain))
|
||||
.ToDictionary(g => g.Key, g => g.First());
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
using static PrivaPub.Federation.Objects.ActivityJson;
|
||||
|
||||
namespace PrivaPub.Federation.Objects
|
||||
{
|
||||
public sealed record ActivityShape(string Type, string ObjectType, string Audience)
|
||||
{
|
||||
public static ActivityShape Of(JsonNode activity)
|
||||
{
|
||||
var embedded = activity?["object"] as JsonObject;
|
||||
return new ActivityShape(Value(activity, "type"), Value(embedded, "type"), AudienceOf(activity));
|
||||
}
|
||||
|
||||
public static ActivityShape Of(string json)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Of(JsonNode.Parse(json));
|
||||
}
|
||||
catch (System.Text.Json.JsonException)
|
||||
{
|
||||
return new ActivityShape(default, default, default);
|
||||
}
|
||||
}
|
||||
|
||||
static string AudienceOf(JsonNode activity)
|
||||
{
|
||||
var source = activity?["to"] != default || activity?["cc"] != default ? activity : activity?["object"] as JsonObject;
|
||||
var to = Addresses(source?["to"]);
|
||||
var cc = Addresses(source?["cc"]);
|
||||
if (to.Any(Addressing.IsPublic))
|
||||
return Interactions.Public;
|
||||
if (cc.Any(Addressing.IsPublic))
|
||||
return Interactions.Unlisted;
|
||||
return to.Count + cc.Count == 0 ? Interactions.None : Interactions.Private;
|
||||
}
|
||||
|
||||
static List<string> Addresses(JsonNode node) => node switch
|
||||
{
|
||||
JsonArray array => array.Select(Id).Where(id => id != default).ToList(),
|
||||
JsonNode single when Id(single) is { } id => new List<string> { id },
|
||||
_ => new List<string>()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ namespace PrivaPub.Federation.Objects
|
||||
{
|
||||
KeepChildNodes = true
|
||||
};
|
||||
foreach (var allowed in new[] { "mention", "hashtag", "ellipsis", "invisible" })
|
||||
foreach (var allowed in new[] { "mention", "hashtag", "ellipsis", "invisible", "quote-inline" })
|
||||
sanitizer.AllowedClasses.Add(allowed);
|
||||
sanitizer.RemovingCssClass += (_, e) => e.Cancel = MicroformatClass().IsMatch(e.CssClass);
|
||||
sanitizer.RemovingTag += (_, e) =>
|
||||
|
||||
@@ -29,6 +29,7 @@ namespace PrivaPub.Federation.Objects
|
||||
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
|
||||
{
|
||||
var host = job.Payload;
|
||||
using var scope = HttpScope.For("nodeinfo");
|
||||
using var links = await _http.GetJson($"https://{host}/.well-known/nodeinfo", "application/json", sign: default, token);
|
||||
var href = links == default ? default : Schemas.Select(schema => LinkFor(links.Root, schema)).FirstOrDefault(link => link != default);
|
||||
if (href == default)
|
||||
|
||||
@@ -25,6 +25,9 @@ namespace PrivaPub.Federation.Objects
|
||||
public string Context { get; init; }
|
||||
public string Audience { get; init; }
|
||||
public string QuoteUri { get; init; }
|
||||
public string QuoteAuthorization { get; init; }
|
||||
public bool QuotesByConsent { get; init; }//FEP-044f `quote`, as opposed to the legacy keys that ask nobody
|
||||
public bool QuoteDeleted { get; init; }
|
||||
public DateTime Published { get; init; }
|
||||
public DateTime? Updated { get; init; }
|
||||
public IReadOnlyList<string> To { get; init; } = Array.Empty<string>();
|
||||
@@ -33,6 +36,9 @@ namespace PrivaPub.Federation.Objects
|
||||
public IReadOnlyList<string> Tags { get; init; } = Array.Empty<string>();
|
||||
public IReadOnlyList<PostMedia> Attachments { get; init; } = Array.Empty<PostMedia>();
|
||||
public PostSource Source { get; init; }
|
||||
public PostPoll Poll { get; init; }
|
||||
public InteractionRule QuotePolicy { get; init; }
|
||||
public IReadOnlyList<CustomEmoji> Emojis { get; init; } = Array.Empty<CustomEmoji>();
|
||||
public string CoverURL { get; init; }
|
||||
public PostLink Link { get; init; }
|
||||
public VideoDetails Video { get; init; }
|
||||
@@ -77,7 +83,10 @@ namespace PrivaPub.Federation.Objects
|
||||
InReplyTo = Id(note["inReplyTo"]),
|
||||
Context = Id(note["context"]) ?? Value(note, "conversation"),
|
||||
Audience = Id(note["audience"]),
|
||||
QuoteUri = Value(note, "quote") ?? Value(note, "quoteUrl") ?? Value(note, "quoteUri") ?? Value(note, "_misskey_quote"),
|
||||
QuoteUri = Id(note["quote"]) ?? Value(note, "quoteUrl") ?? Value(note, "quoteUri") ?? Value(note, "_misskey_quote") ?? QuoteLink(note),
|
||||
QuoteAuthorization = Id(note["quoteAuthorization"]),
|
||||
QuotesByConsent = note.ContainsKey("quote"),
|
||||
QuoteDeleted = note["quote"] is JsonObject quoted && Value(quoted, "type") == "Tombstone",
|
||||
Published = Time(Value(note, "published")) ?? DateTime.UtcNow,
|
||||
Updated = Time(Value(note, "updated")),
|
||||
To = List(note["to"]),
|
||||
@@ -94,6 +103,9 @@ namespace PrivaPub.Federation.Objects
|
||||
.ToList(),
|
||||
Attachments = Attachments(note),
|
||||
Source = ObjectShapes.Source(note),
|
||||
Poll = ObjectShapes.Poll(note),
|
||||
QuotePolicy = ObjectShapes.QuotePolicy(note),
|
||||
Emojis = ObjectShapes.Emojis(note["tag"]),
|
||||
CoverURL = ObjectShapes.Cover(note),
|
||||
Link = ObjectShapes.Link(note),
|
||||
Video = ObjectShapes.Video(note),
|
||||
@@ -165,6 +177,12 @@ namespace PrivaPub.Federation.Objects
|
||||
};
|
||||
}
|
||||
|
||||
static string QuoteLink(JsonObject note) =>
|
||||
Tags(note, "Link")
|
||||
.Where(t => Value(t, "mediaType") is { } type && (type.StartsWith("application/activity+json") || type.StartsWith("application/ld+json")))
|
||||
.Select(t => Value(t, "href"))
|
||||
.FirstOrDefault(href => Origin.Of(href) != default);
|
||||
|
||||
static string FirstOf(JsonNode map) =>
|
||||
map is JsonObject languages ? languages.Select(l => l.Value is JsonValue v && v.TryGetValue<string>(out var text) ? text : default).FirstOrDefault(t => t != default) : default;
|
||||
|
||||
|
||||
@@ -34,8 +34,10 @@ namespace PrivaPub.Federation.Objects
|
||||
public async Task Record(NoteDocument note, PostEntity post, ObjectPath path, bool refetched, CancellationToken token)
|
||||
{
|
||||
var arrival = Arrival.Current;
|
||||
var delivered = path == ObjectPath.Delivered;
|
||||
var raw = Capture(note.Raw);
|
||||
var host = new Uri(note.Id).Host.ToLowerInvariant();
|
||||
var activityContext = !delivered || note.Raw?.ContainsKey("@context") == true || arrival?.Context?.Length > 16 * 1024 ? default : arrival?.Context;
|
||||
var record = new ObjectRecord
|
||||
{
|
||||
ObjectURI = note.Id,
|
||||
@@ -46,20 +48,22 @@ namespace PrivaPub.Federation.Objects
|
||||
RawHash = raw.Hash,
|
||||
RawBytes = raw.Bytes,
|
||||
RawTruncated = raw.Truncated,
|
||||
ActivityContext = note.Raw?.ContainsKey("@context") == true || arrival?.Context?.Length > 16 * 1024 ? default : arrival?.Context,
|
||||
ActivityContext = activityContext,
|
||||
Path = path,
|
||||
Refetched = refetched,
|
||||
ActivityId = arrival?.ActivityId,
|
||||
ActivityType = arrival?.ActivityType,
|
||||
ActivityActorURI = arrival?.ActorURI,
|
||||
Inbox = path == ObjectPath.Delivered ? arrival?.Inbox : default,
|
||||
SignatureScheme = arrival?.KeyId == default ? default : "draft-cavage",
|
||||
KeyId = arrival?.KeyId,
|
||||
Algorithm = arrival?.Algorithm,
|
||||
SignedHeaders = arrival?.SignedHeaders?.ToList() ?? new(),
|
||||
ReceivedAt = arrival?.ReceivedAt ?? DateTime.UtcNow,
|
||||
Inbox = delivered ? arrival?.Inbox : default,
|
||||
SignatureScheme = !delivered || arrival?.KeyId == default ? default : "draft-cavage",
|
||||
KeyId = delivered ? arrival?.KeyId : default,
|
||||
Algorithm = delivered ? arrival?.Algorithm : default,
|
||||
SignedHeaders = delivered ? arrival?.SignedHeaders?.ToList() ?? new() : new(),
|
||||
ReceivedAt = delivered ? arrival?.ReceivedAt ?? DateTime.UtcNow : DateTime.UtcNow,
|
||||
Published = note.Published,
|
||||
Updated = note.Updated
|
||||
Updated = note.Updated,
|
||||
Extensions = ObjectFeatures.Detect(note.Raw),
|
||||
ContextNamespaces = ObjectFeatures.Namespaces(note.Raw, activityContext)
|
||||
};
|
||||
try
|
||||
{
|
||||
|
||||
@@ -16,6 +16,9 @@ namespace PrivaPub.Federation.Objects
|
||||
const int MaxCaptions = 20;
|
||||
const int MaxPlaces = 4;
|
||||
const int CoverMaxWidth = 1280;
|
||||
const int MaxEmojis = 64;
|
||||
const int MaxPollOptions = 20;
|
||||
const int MaxAudience = 32;
|
||||
|
||||
static readonly Dictionary<string, string> MediaTypesByExtension = new(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
@@ -81,6 +84,69 @@ namespace PrivaPub.Federation.Objects
|
||||
?? usable.Select(c => c.Url).FirstOrDefault();
|
||||
}
|
||||
|
||||
public static List<CustomEmoji> Emojis(JsonNode tags) =>
|
||||
Objects(tags)
|
||||
.Where(t => Value(t, "type") == "Emoji")
|
||||
.Select(t => new CustomEmoji
|
||||
{
|
||||
Shortcode = Value(t, "name")?.Trim(':'),
|
||||
URL = MediaLink(t["icon"]).Url,
|
||||
Updated = Moment(Value(t, "updated"))
|
||||
})
|
||||
.Where(e => !string.IsNullOrEmpty(e.Shortcode) && e.Shortcode.Length <= 100 && Shortcode().IsMatch(e.Shortcode) && Origin.Of(e.URL) != default)
|
||||
.DistinctBy(e => e.Shortcode)
|
||||
.Take(MaxEmojis)
|
||||
.ToList();
|
||||
|
||||
public static InteractionRule QuotePolicy(JsonObject note)
|
||||
{
|
||||
if (note["interactionPolicy"] is not JsonObject policy || policy["canQuote"] is not JsonObject canQuote)
|
||||
return default;
|
||||
static List<string> Who(JsonNode node) => node switch
|
||||
{
|
||||
JsonArray array => array.Select(Id).Where(id => id != default).Take(MaxAudience).ToList(),
|
||||
JsonNode single when Id(single) is { } id => new List<string> { id },
|
||||
_ => new List<string>()
|
||||
};
|
||||
return new InteractionRule
|
||||
{
|
||||
Automatic = Who(canQuote["automaticApproval"] ?? canQuote["always"]),
|
||||
Manual = Who(canQuote["manualApproval"] ?? canQuote["approvalRequired"])
|
||||
};
|
||||
}
|
||||
|
||||
public static PostPoll Poll(JsonObject note)
|
||||
{
|
||||
if (Value(note, "type") != "Question")
|
||||
return default;
|
||||
var multiple = note["anyOf"] is JsonArray;
|
||||
var options = Objects(note[multiple ? "anyOf" : "oneOf"])
|
||||
.Select(o => new PollOption
|
||||
{
|
||||
Title = Text(Value(o, "name"), 200),
|
||||
Votes = o["replies"] is JsonObject replies && Int(replies, "totalItems") is { } total ? total : Int(o, "_misskey_votes") ?? 0
|
||||
})
|
||||
.Where(o => o.Title != default)
|
||||
.Take(MaxPollOptions)
|
||||
.ToList();
|
||||
if (options.Count < 2)
|
||||
return default;
|
||||
var closedAt = note["closed"] switch
|
||||
{
|
||||
JsonValue value when value.TryGetValue<bool>(out var closed) => closed ? Moment(Value(note, "endTime")) ?? DateTime.UtcNow : default(DateTime?),
|
||||
JsonValue value when value.TryGetValue<string>(out var at) => Moment(at),
|
||||
_ => default
|
||||
};
|
||||
return new PostPoll
|
||||
{
|
||||
Options = options,
|
||||
Multiple = multiple,
|
||||
ExpiresAt = Moment(Value(note, "endTime")),
|
||||
ClosedAt = closedAt,
|
||||
VotersCount = Int(note, "votersCount")
|
||||
};
|
||||
}
|
||||
|
||||
public static string Cover(JsonObject note) =>
|
||||
Image(note["image"]) ?? Image(note["icon"]) ?? (note["preview"] is JsonObject preview && Value(preview, "type") == "Image" ? Image(preview) : default);
|
||||
|
||||
@@ -151,10 +217,13 @@ namespace PrivaPub.Federation.Objects
|
||||
if (Value(note, "type") != "Video")
|
||||
return default;
|
||||
var links = Objects(note["url"]).ToList();
|
||||
var nested = links.Where(l => Value(l, "mediaType")?.Equals("application/x-mpegURL", StringComparison.OrdinalIgnoreCase) == true)
|
||||
.SelectMany(l => Objects(l["tag"]))
|
||||
.ToList();
|
||||
return new VideoDetails
|
||||
{
|
||||
DurationSeconds = Seconds(note["duration"]),
|
||||
Variants = Variants(links, "video/", "audio/"),
|
||||
Variants = Variants(links.Concat(nested).ToList(), "video/", "audio/"),
|
||||
PlaylistURL = links.Where(l => Value(l, "mediaType")?.Equals("application/x-mpegURL", StringComparison.OrdinalIgnoreCase) == true)
|
||||
.Select(l => Value(l, "href")).FirstOrDefault(IsWeb),
|
||||
IsLive = Bool(note, "isLiveBroadcast") == true,
|
||||
@@ -284,6 +353,9 @@ namespace PrivaPub.Federation.Objects
|
||||
static bool? Bool(JsonObject node, string property) =>
|
||||
node?[property] is JsonValue value && value.TryGetValue<bool>(out var flag) ? flag : (bool?)null;
|
||||
|
||||
[GeneratedRegex(@"^[\w+\-.@]+$")]
|
||||
private static partial Regex Shortcode();
|
||||
|
||||
[GeneratedRegex("<[^>]*>")]
|
||||
private static partial Regex Tags();
|
||||
|
||||
|
||||
@@ -11,8 +11,12 @@ using PrivaPub.Federation.Actors;
|
||||
using PrivaPub.Federation.Signing;
|
||||
using PrivaPub.Infrastructure.Http;
|
||||
using PrivaPub.Infrastructure.Jobs;
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Models.Statistics;
|
||||
using PrivaPub.Models.Jobs;
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PrivaPub.Federation.Outbox
|
||||
@@ -81,13 +85,25 @@ namespace PrivaPub.Federation.Outbox
|
||||
readonly IFederationHttp _http;
|
||||
readonly IHostCircuitBreaker _breaker;
|
||||
readonly ILogger<DeliveryJobHandler> _logger;
|
||||
readonly IInteractionLedger _ledger;
|
||||
|
||||
public DeliveryJobHandler(ILocalActorService actors, IFederationHttp http, IHostCircuitBreaker breaker, ILogger<DeliveryJobHandler> logger)
|
||||
public DeliveryJobHandler(ILocalActorService actors, IFederationHttp http, IHostCircuitBreaker breaker, ILogger<DeliveryJobHandler> logger,
|
||||
IInteractionLedger ledger = default)
|
||||
{
|
||||
_actors = actors;
|
||||
_http = http;
|
||||
_breaker = breaker;
|
||||
_logger = logger;
|
||||
_ledger = ledger;
|
||||
}
|
||||
|
||||
sealed class Attempt
|
||||
{
|
||||
public DeliveryPayload Payload;
|
||||
public LocalActor Signer;
|
||||
public int? Status;
|
||||
public string Reason;
|
||||
public int? LatencyMs;
|
||||
}
|
||||
|
||||
public JobKind Kind => JobKind.Deliver;
|
||||
@@ -96,28 +112,97 @@ namespace PrivaPub.Federation.Outbox
|
||||
public int PerHostLimit => 2;
|
||||
|
||||
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
|
||||
{
|
||||
var attempt = new Attempt();
|
||||
JobOutcome outcome = default;
|
||||
try
|
||||
{
|
||||
outcome = await Deliver(job, attempt, token);
|
||||
return outcome;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Record(job, attempt, outcome);
|
||||
}
|
||||
}
|
||||
|
||||
void Record(Job job, Attempt attempt, JobOutcome outcome)
|
||||
{
|
||||
if (_ledger == default)
|
||||
return;
|
||||
var shape = attempt.Payload == default ? default : ActivityShape.Of(attempt.Payload.Body);
|
||||
_ledger.Record(new InteractionEvent
|
||||
{
|
||||
Channel = Interactions.Out,
|
||||
Host = job.Host,
|
||||
Activity = shape?.Type,
|
||||
Object = shape?.ObjectType,
|
||||
Outcome = outcome?.Result switch
|
||||
{
|
||||
JobResult.Done => Interactions.Ok,
|
||||
JobResult.Defer => Interactions.Deferred,
|
||||
JobResult.Retry when job.Attempts >= MaxAttempts => Interactions.Dead,
|
||||
JobResult.Retry => Interactions.Retry,
|
||||
JobResult.Dead => Interactions.Dead,
|
||||
_ => Interactions.Failed
|
||||
},
|
||||
Reason = attempt.Reason,
|
||||
Status = attempt.Status,
|
||||
LatencyMs = attempt.LatencyMs,
|
||||
WaitMs = (int)Math.Min(int.MaxValue, Math.Max(0, (DateTime.UtcNow - job.CreatedAt).TotalMilliseconds)),
|
||||
Bytes = attempt.Payload?.Body == default ? default : Encoding.UTF8.GetByteCount(attempt.Payload.Body),
|
||||
Attempt = job.Attempts,
|
||||
Audience = shape?.Audience,
|
||||
LocalKind = attempt.Signer switch
|
||||
{
|
||||
null => default,
|
||||
{ IsCircle: true } => default,
|
||||
{ Kind: LocalActorKind.Person } => "person",
|
||||
{ Kind: LocalActorKind.Group } => "group",
|
||||
_ => "application"
|
||||
},
|
||||
Signature = "cavage:rsa-sha256"
|
||||
});
|
||||
}
|
||||
|
||||
async Task<JobOutcome> Deliver(Job job, Attempt attempt, CancellationToken token)
|
||||
{
|
||||
var payload = JsonSerializer.Deserialize<DeliveryPayload>(job.Payload);
|
||||
attempt.Payload = payload;
|
||||
if (payload == default || !Uri.TryCreate(payload.Inbox, UriKind.Absolute, out var inbox) || !_http.IsAllowed(inbox))
|
||||
{
|
||||
attempt.Reason = "not-deliverable";
|
||||
return JobOutcome.Dead("not a deliverable inbox");
|
||||
}
|
||||
|
||||
var unavailableUntil = await _breaker.UnavailableUntil(job.Host, token);
|
||||
if (unavailableUntil.HasValue)
|
||||
{
|
||||
attempt.Reason = "host-unavailable";
|
||||
return JobOutcome.Defer(unavailableUntil.Value, "the host is unavailable");
|
||||
}
|
||||
|
||||
var signer = await _actors.FindById(payload.SignerKind, payload.SignerId, token);
|
||||
attempt.Signer = signer;
|
||||
if (signer == default)
|
||||
{
|
||||
attempt.Reason = "signer-gone";
|
||||
return JobOutcome.Dead("the signing actor no longer exists");
|
||||
}
|
||||
|
||||
var body = Encoding.UTF8.GetBytes(payload.Body);
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, inbox) { Content = new ByteArrayContent(body) };
|
||||
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(RemoteActorService.ActivityJson);
|
||||
HttpSignatures.Sign(request, signer, body);
|
||||
|
||||
var started = Stopwatch.GetTimestamp();
|
||||
try
|
||||
{
|
||||
using var response = await _http.Send(request, token);
|
||||
attempt.LatencyMs = (int)Stopwatch.GetElapsedTime(started).TotalMilliseconds;
|
||||
var status = (int)response.StatusCode;
|
||||
attempt.Status = status;
|
||||
attempt.Reason = response.IsSuccessStatusCode ? default : status.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
await _breaker.Succeeded(job.Host, token);
|
||||
@@ -142,10 +227,13 @@ namespace PrivaPub.Federation.Outbox
|
||||
}
|
||||
catch (BlockedDestinationException ex)
|
||||
{
|
||||
attempt.Reason = "private-address";
|
||||
return JobOutcome.Dead(ex.Message);
|
||||
}
|
||||
catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException && !token.IsCancellationRequested)
|
||||
{
|
||||
attempt.LatencyMs = (int)Stopwatch.GetElapsedTime(started).TotalMilliseconds;
|
||||
attempt.Reason = ex is TaskCanceledException ? "timeout" : "network";
|
||||
await _breaker.Failed(job.Host, ex.GetType().Name, token);
|
||||
return JobOutcome.Retry(ex.Message);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,16 @@ namespace PrivaPub.Federation.Outbox
|
||||
inboxes.Add(parentAuthor.InboxURL);
|
||||
}
|
||||
|
||||
if (post.Visibility is PostVisibility.Public or PostVisibility.Unlisted && !string.IsNullOrEmpty(post.QuotedPostId))
|
||||
{
|
||||
var quoted = await _dbEntities.Posts.MatchID(post.QuotedPostId).ExecuteFirstAsync(token);
|
||||
var quotedAuthor = quoted is { IsFederatedCopy: true }
|
||||
? await _dbEntities.ForeignAvatars.Match(a => a.ActorURI == quoted.ActorURI).ExecuteFirstAsync(token)
|
||||
: default;
|
||||
if (!string.IsNullOrEmpty(quotedAuthor?.InboxURL))
|
||||
inboxes.Add(quotedAuthor.InboxURL);
|
||||
}
|
||||
|
||||
return inboxes.Where(i => !string.IsNullOrEmpty(i)).Distinct(StringComparer.Ordinal).ToList();
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,26 @@ namespace PrivaPub.Federation.Rendering
|
||||
["discoverable"] = "toot:discoverable",
|
||||
["indexable"] = "toot:indexable",
|
||||
["blurhash"] = "toot:blurhash",
|
||||
["votersCount"] = "toot:votersCount",
|
||||
["Emoji"] = "toot:Emoji",
|
||||
["quote"] = new JsonObject { ["@id"] = "https://w3id.org/fep/044f#quote", ["@type"] = "@id" },
|
||||
["quoteAuthorization"] = new JsonObject { ["@id"] = "https://w3id.org/fep/044f#quoteAuthorization", ["@type"] = "@id" },
|
||||
["QuoteRequest"] = "https://w3id.org/fep/044f#QuoteRequest",
|
||||
["QuoteAuthorization"] = "https://w3id.org/fep/044f#QuoteAuthorization",
|
||||
["interactingObject"] = new JsonObject { ["@id"] = "gts:interactingObject", ["@type"] = "@id" },
|
||||
["interactionTarget"] = new JsonObject { ["@id"] = "gts:interactionTarget", ["@type"] = "@id" },
|
||||
["misskey"] = "https://misskey-hub.net/ns#",
|
||||
["_misskey_quote"] = "misskey:_misskey_quote",
|
||||
["fedibird"] = "http://fedibird.com/ns#",
|
||||
["quoteUri"] = "fedibird:quoteUri",
|
||||
["quoteUrl"] = "as:quoteUrl",
|
||||
["gts"] = "https://gotosocial.org/ns#",
|
||||
["interactionPolicy"] = new JsonObject { ["@id"] = "gts:interactionPolicy", ["@type"] = "@id" },
|
||||
["canQuote"] = new JsonObject { ["@id"] = "gts:canQuote", ["@type"] = "@id" },
|
||||
["automaticApproval"] = new JsonObject { ["@id"] = "gts:automaticApproval", ["@type"] = "@id" },
|
||||
["manualApproval"] = new JsonObject { ["@id"] = "gts:manualApproval", ["@type"] = "@id" },
|
||||
["litepub"] = "http://litepub.social/ns#",
|
||||
["EmojiReact"] = "litepub:EmojiReact",
|
||||
["focalPoint"] = new JsonObject { ["@container"] = "@list", ["@id"] = "toot:focalPoint" },
|
||||
["schema"] = "http://schema.org#",
|
||||
["PropertyValue"] = "schema:PropertyValue",
|
||||
@@ -156,6 +176,67 @@ namespace PrivaPub.Federation.Rendering
|
||||
return note;
|
||||
}
|
||||
|
||||
public static JsonObject UpdateOf(PostEntity post, LocalActor author, string reason)
|
||||
{
|
||||
var note = post.Visibility == PostVisibility.Direct
|
||||
? DirectNote(post, author, Array.Empty<(string, string)>(), post.ContextURI)
|
||||
: Note(post, author, default, post.InReplyToURI);
|
||||
note["to"] = new JsonArray(post.To.Select(t => (JsonNode)t).ToArray());
|
||||
note["cc"] = new JsonArray(post.Cc.Select(c => (JsonNode)c).ToArray());
|
||||
return new JsonObject
|
||||
{
|
||||
["@context"] = Context(),
|
||||
["id"] = author.ActivityUri($"update-{post.ID}-{reason}"),
|
||||
["type"] = "Update",
|
||||
["actor"] = author.Uri,
|
||||
["to"] = note["to"]!.DeepClone(),
|
||||
["cc"] = note["cc"]!.DeepClone(),
|
||||
["object"] = note
|
||||
};
|
||||
}
|
||||
|
||||
public static string QuotableBy(PostEntity post) =>
|
||||
post.Visibility is PostVisibility.Public or PostVisibility.Unlisted ? post.LocalQuotePolicy ?? Models.User.QuotePolicies.Nobody : Models.User.QuotePolicies.Nobody;
|
||||
|
||||
static void AddQuote(JsonObject note, PostEntity post, ref string content)
|
||||
{
|
||||
if (string.IsNullOrEmpty(post.QuoteURI))
|
||||
return;
|
||||
var link = WebUtility.HtmlEncode(post.QuoteURI);
|
||||
content += $"<p class=\"quote-inline\">RE: <a href=\"{link}\">{link}</a></p>";
|
||||
if (post.QuoteByConsent)
|
||||
note["quote"] = post.QuoteURI;
|
||||
if (!string.IsNullOrEmpty(post.QuoteAuthorizationURI))
|
||||
note["quoteAuthorization"] = post.QuoteAuthorizationURI;
|
||||
note["_misskey_quote"] = post.QuoteURI;
|
||||
note["quoteUri"] = post.QuoteURI;
|
||||
note["quoteUrl"] = post.QuoteURI;
|
||||
(note["tag"] as JsonArray)?.Add(new JsonObject
|
||||
{
|
||||
["type"] = "Link",
|
||||
["mediaType"] = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"",
|
||||
["href"] = post.QuoteURI,
|
||||
["name"] = $"RE: {post.QuoteURI}"
|
||||
});
|
||||
}
|
||||
|
||||
static void AddPoll(JsonObject note, PostPoll poll)
|
||||
{
|
||||
note["type"] = "Question";
|
||||
note[poll.Multiple ? "anyOf" : "oneOf"] = new JsonArray(poll.Options.Select(option => (JsonNode)new JsonObject
|
||||
{
|
||||
["type"] = "Note",
|
||||
["name"] = option.Title,
|
||||
["replies"] = new JsonObject { ["type"] = "Collection", ["totalItems"] = option.Votes }
|
||||
}).ToArray());
|
||||
if (poll.ExpiresAt is { } ends)
|
||||
note["endTime"] = Timestamp(ends);
|
||||
if (poll.ClosedAt is { } closed)
|
||||
note["closed"] = Timestamp(closed);
|
||||
if (poll.VotersCount is { } voters)
|
||||
note["votersCount"] = voters;
|
||||
}
|
||||
|
||||
static string Headline(PostEntity post)
|
||||
{
|
||||
var text = System.Text.RegularExpressions.Regex.Replace(post.ContentHtml ?? post.Text ?? string.Empty, "<[^>]+>", " ");
|
||||
@@ -190,8 +271,25 @@ namespace PrivaPub.Federation.Rendering
|
||||
}))
|
||||
.ToArray())
|
||||
};
|
||||
AddQuote(note, post, ref content);
|
||||
if (!post.IsFederatedCopy)
|
||||
note["interactionPolicy"] = new JsonObject
|
||||
{
|
||||
["canQuote"] = new JsonObject
|
||||
{
|
||||
["automaticApproval"] = new JsonArray((JsonNode)(QuotableBy(post) switch
|
||||
{
|
||||
Models.User.QuotePolicies.Public => Objects.Addressing.Public,
|
||||
Models.User.QuotePolicies.Followers => author.Followers,
|
||||
_ => author.Uri
|
||||
}))
|
||||
}
|
||||
};
|
||||
note["content"] = content;
|
||||
if (!string.IsNullOrEmpty(post.Language))
|
||||
note["contentMap"] = new JsonObject { [post.Language] = content };
|
||||
if (post.Poll is { } poll)
|
||||
AddPoll(note, poll);
|
||||
var attachments = post.Media.Where(m => !string.IsNullOrEmpty(m.URL)).Select(m =>
|
||||
{
|
||||
var attachment = new JsonObject
|
||||
|
||||
@@ -185,6 +185,18 @@ namespace PrivaPub.Federation.Signing
|
||||
return default;
|
||||
}
|
||||
|
||||
public static string ProblemCode(string problem) => problem switch
|
||||
{
|
||||
null => default,
|
||||
_ when problem.StartsWith("unsupported signature algorithm", StringComparison.Ordinal) => "algorithm-unsupported",
|
||||
_ when problem.EndsWith("is not signed", StringComparison.Ordinal) || problem.StartsWith("neither", StringComparison.Ordinal) => "headers-unsigned",
|
||||
_ when problem.StartsWith("the digest does not match", StringComparison.Ordinal) => "digest-mismatch",
|
||||
_ when problem.StartsWith("unreadable", StringComparison.Ordinal) => "header-unreadable",
|
||||
_ when problem.Contains("outside the allowed window", StringComparison.Ordinal) => "date-skew",
|
||||
"the signature has expired" => "expired",
|
||||
_ => "signature-problem"
|
||||
};
|
||||
|
||||
static bool IsFresh(DateTimeOffset signedAt, DateTimeOffset now) =>
|
||||
signedAt >= now - MaxAge && signedAt <= now + MaxClockSkew;
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ using PrivaPub.Models.Group;
|
||||
using PrivaPub.Models.Jobs;
|
||||
using PrivaPub.Models.Post;
|
||||
using PrivaPub.Models.Social;
|
||||
using PrivaPub.Models.Statistics;
|
||||
using PrivaPub.Models.User;
|
||||
|
||||
namespace PrivaPub.Infrastructure.Data
|
||||
@@ -110,6 +111,22 @@ namespace PrivaPub.Infrastructure.Data
|
||||
await DB.Default.Index<DeletedObject>().Key(d => d.DeletedAt, KeyType.Ascending).Option(o => o.ExpireAfter = TombstoneLifetime).CreateAsync(token);
|
||||
await DB.Default.Index<Downvote>().Key(d => d.PostId, KeyType.Ascending).Key(d => d.ActorURI, KeyType.Ascending).Option(o => o.Unique = true).CreateAsync(token);
|
||||
await Plain<Downvote>(token, d => d.ActivityURI);
|
||||
await DB.Default.Index<PollVote>().Key(v => v.PostId, KeyType.Ascending).Key(v => v.ActorURI, KeyType.Ascending).Key(v => v.Choice, KeyType.Ascending)
|
||||
.Option(o => o.Unique = true).CreateAsync(token);
|
||||
await DB.Default.Index<PollVote>().Key(v => v.PostId, KeyType.Ascending).Key(v => v.VoterAccountId, KeyType.Ascending).CreateAsync(token);
|
||||
await DB.Default.Index<Reaction>().Key(r => r.PostId, KeyType.Ascending).Key(r => r.ActorURI, KeyType.Ascending).Key(r => r.Emoji, KeyType.Ascending)
|
||||
.Option(o => o.Unique = true).CreateAsync(token);
|
||||
await Plain<Reaction>(token, r => r.ActivityURI);
|
||||
await DB.Default.Index<QuoteLicence>().Key(l => l.PostId, KeyType.Ascending).Key(l => l.QuotingObjectURI, KeyType.Ascending).CreateAsync(token);
|
||||
await Unique<Domain.Content.LinkPreview>(p => p.Url, Builders<Domain.Content.LinkPreview>.Filter.Type(p => p.Url, BsonType.String), token);
|
||||
|
||||
await DB.Default.Index<InteractionEvent>().Key(e => e.At, KeyType.Ascending).Option(o => o.ExpireAfter = InteractionEvent.Retention).CreateAsync(token);
|
||||
await Plain<InteractionEvent>(token, e => e.Host, e => e.At);
|
||||
await DB.Default.Index<InteractionSalt>().Key(s => s.Day, KeyType.Ascending).Option(o => o.Unique = true).CreateAsync(token);
|
||||
await DB.Default.Index<InteractionSalt>().Key(s => s.ExpiresAt, KeyType.Ascending).Option(o => o.ExpireAfter = TimeSpan.Zero).CreateAsync(token);
|
||||
await DB.Default.Index<InstanceDay>().Key(d => d.Day, KeyType.Ascending).Key(d => d.Host, KeyType.Ascending).Option(o => o.Unique = true).CreateAsync(token);
|
||||
await Plain<InstanceDay>(token, d => d.Host, d => d.Day);
|
||||
await DB.Default.Index<ServerDay>().Key(d => d.Day, KeyType.Ascending).Option(o => o.Unique = true).CreateAsync(token);
|
||||
}
|
||||
|
||||
static async Task Unique<T>(System.Linq.Expressions.Expression<Func<T, object>> key, FilterDefinition<T> partial,
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Models.Federation;
|
||||
|
||||
namespace PrivaPub.Infrastructure.Data.Migrations
|
||||
{
|
||||
public class _008_fetched_records_lose_the_trigger_signature : IMigration
|
||||
{
|
||||
public async Task UpgradeAsync() =>
|
||||
await DB.Default.Update<ObjectRecord>()
|
||||
.Match(r => r.Path == ObjectPath.Fetched)
|
||||
.Modify(r => r.SignatureScheme, null)
|
||||
.Modify(r => r.KeyId, null)
|
||||
.Modify(r => r.Algorithm, null)
|
||||
.Modify(r => r.SignedHeaders, new List<string>())
|
||||
.Modify(r => r.ActivityContext, null)
|
||||
.ExecuteAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Federation.Objects;
|
||||
using PrivaPub.Models.Federation;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PrivaPub.Infrastructure.Data.Migrations
|
||||
{
|
||||
public class _009_object_records_keep_their_extensions : IMigration
|
||||
{
|
||||
public async Task UpgradeAsync()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var batch = await DB.Default.Find<ObjectRecord>()
|
||||
.Match(r => r.Extensions == null)
|
||||
.Limit(500)
|
||||
.ExecuteAsync();
|
||||
if (batch.Count == 0)
|
||||
return;
|
||||
foreach (var record in batch)
|
||||
{
|
||||
JsonObject raw = default;
|
||||
try
|
||||
{
|
||||
raw = record.Raw == default ? default : JsonNode.Parse(record.Raw) as JsonObject;
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
}
|
||||
await DB.Default.Update<ObjectRecord>().MatchID(record.ID)
|
||||
.Modify(r => r.Extensions, ObjectFeatures.Detect(raw))
|
||||
.Modify(r => r.ContextNamespaces, ObjectFeatures.Namespaces(raw, record.ActivityContext))
|
||||
.ExecuteAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using PrivaPub.Federation.Moderation;
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Models.Statistics;
|
||||
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
@@ -21,6 +23,9 @@ namespace PrivaPub.Infrastructure.Http
|
||||
{
|
||||
bool IsAllowed(Uri target);
|
||||
Task<FetchedJson> GetJson(string url, string accept, Action<HttpRequestMessage> sign, CancellationToken token);
|
||||
bool FailedTemporarily(string url);
|
||||
Task<(Uri FinalUri, string Html)> GetPage(string url, CancellationToken token);
|
||||
Task<HttpResponseMessage> OpenMedia(string url, System.Net.Http.Headers.RangeHeaderValue range, CancellationToken token);
|
||||
Task<HttpResponseMessage> Send(HttpRequestMessage request, CancellationToken token);
|
||||
Task<(byte[] Bytes, string ContentType)> GetMedia(string url, long maxBytes, CancellationToken token);
|
||||
}
|
||||
@@ -46,17 +51,71 @@ namespace PrivaPub.Infrastructure.Http
|
||||
readonly IOptionsMonitor<FederationOptions> _options;
|
||||
readonly IDomainBlocks _domainBlocks;
|
||||
readonly ILogger<FederationHttp> _logger;
|
||||
readonly IInteractionLedger _ledger;
|
||||
|
||||
public FederationHttp(IHttpClientFactory httpClientFactory, IMemoryCache cache, IOptionsMonitor<FederationOptions> options,
|
||||
IDomainBlocks domainBlocks, ILogger<FederationHttp> logger)
|
||||
IDomainBlocks domainBlocks, ILogger<FederationHttp> logger, IInteractionLedger ledger = default)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_cache = cache;
|
||||
_options = options;
|
||||
_domainBlocks = domainBlocks;
|
||||
_logger = logger;
|
||||
_ledger = ledger;
|
||||
}
|
||||
|
||||
sealed class Exchange
|
||||
{
|
||||
public Exchange(string url, string purpose)
|
||||
{
|
||||
Host = Interactions.HostOf(url);
|
||||
Purpose = purpose;
|
||||
}
|
||||
|
||||
public readonly string Host;
|
||||
public readonly string Purpose;
|
||||
public readonly long Started = System.Diagnostics.Stopwatch.GetTimestamp();
|
||||
public int? Status;
|
||||
public long? Bytes;
|
||||
public int Hops;
|
||||
public string Outcome = Interactions.Ok;
|
||||
public string Reason;
|
||||
|
||||
public void Refused(string reason) => (Outcome, Reason) = (Interactions.Refused, reason);
|
||||
|
||||
public void Failed(string reason) => (Outcome, Reason) = (Interactions.Failed, reason);
|
||||
|
||||
public void Answered(HttpResponseMessage response) => Status = (int)response.StatusCode;
|
||||
}
|
||||
|
||||
void Record(Exchange exchange)
|
||||
{
|
||||
if (_ledger == default)
|
||||
return;
|
||||
var trigger = HttpScope.Trigger;
|
||||
if (trigger == HttpScope.Request)
|
||||
{
|
||||
_ledger.Count(exchange.Host, $"http:{exchange.Purpose}:{exchange.Outcome}", exchange.Bytes ?? 0);
|
||||
return;
|
||||
}
|
||||
_ledger.Record(new InteractionEvent
|
||||
{
|
||||
Channel = Interactions.Http,
|
||||
Host = exchange.Host,
|
||||
Purpose = exchange.Purpose,
|
||||
Trigger = trigger,
|
||||
Outcome = exchange.Outcome,
|
||||
Reason = exchange.Reason,
|
||||
Status = exchange.Status,
|
||||
LatencyMs = (int)System.Diagnostics.Stopwatch.GetElapsedTime(exchange.Started).TotalMilliseconds,
|
||||
Bytes = exchange.Bytes,
|
||||
Redirects = exchange.Hops,
|
||||
Crawl = HttpScope.Crawling
|
||||
});
|
||||
}
|
||||
|
||||
static string StatusReason(HttpResponseMessage response) => ((int)response.StatusCode).ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
|
||||
public bool IsAllowed(Uri target)
|
||||
{
|
||||
if (target is not { IsAbsoluteUri: true } || !string.IsNullOrEmpty(target.UserInfo))
|
||||
@@ -76,12 +135,31 @@ namespace PrivaPub.Infrastructure.Http
|
||||
}
|
||||
|
||||
public async Task<FetchedJson> GetJson(string url, string accept, Action<HttpRequestMessage> sign, CancellationToken token)
|
||||
{
|
||||
var exchange = new Exchange(url, HttpScope.Purpose ?? "object");
|
||||
try
|
||||
{
|
||||
return await GetJson(url, accept, sign, exchange, token);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Record(exchange);
|
||||
}
|
||||
}
|
||||
|
||||
async Task<FetchedJson> GetJson(string url, string accept, Action<HttpRequestMessage> sign, Exchange exchange, CancellationToken token)
|
||||
{
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out var target) || !IsAllowed(target))
|
||||
{
|
||||
exchange.Refused("disallowed");
|
||||
return default;
|
||||
}
|
||||
var negativeKey = NegativeKey(target);
|
||||
if (_cache.TryGetValue(negativeKey, out _))
|
||||
{
|
||||
exchange.Refused("remembered");
|
||||
return default;
|
||||
}
|
||||
|
||||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(token);
|
||||
timeout.CancelAfter(RequestTimeout);
|
||||
@@ -95,48 +173,78 @@ namespace PrivaPub.Infrastructure.Http
|
||||
|
||||
using var response = await _httpClientFactory.CreateClient(ClientName)
|
||||
.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
||||
exchange.Answered(response);
|
||||
|
||||
if (IsRedirect(response.StatusCode))
|
||||
{
|
||||
var location = response.Headers.Location;
|
||||
var next = location == default ? default : location.IsAbsoluteUri ? location : new Uri(target, location);
|
||||
if (!IsAllowed(next))
|
||||
return Refuse(negativeKey, url, "a redirect to a disallowed location");
|
||||
return Refuse(negativeKey, url, "a redirect to a disallowed location", exchange, "bad-redirect");
|
||||
target = next;
|
||||
exchange.Hops++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return Refuse(negativeKey, url, $"status {(int)response.StatusCode}");
|
||||
return Refuse(negativeKey, url, $"status {(int)response.StatusCode}", exchange, StatusReason(response),
|
||||
transient: (int)response.StatusCode is >= 500 or 429 or 408);
|
||||
|
||||
var mediaType = response.Content.Headers.ContentType?.MediaType;
|
||||
if (mediaType == default || !JsonMediaTypes.Contains(mediaType, StringComparer.OrdinalIgnoreCase))
|
||||
return Refuse(negativeKey, url, $"content type '{mediaType}'");
|
||||
return Refuse(negativeKey, url, $"content type '{mediaType}'", exchange, "content-type");
|
||||
if (response.Content.Headers.ContentLength > MaxResponseBytes)
|
||||
return Refuse(negativeKey, url, "a body over the size limit");
|
||||
return Refuse(negativeKey, url, "a body over the size limit", exchange, "too-large");
|
||||
|
||||
var body = await ReadBounded(response.Content, MaxResponseBytes, timeout.Token);
|
||||
if (body == default)
|
||||
return Refuse(negativeKey, url, "a body over the size limit");
|
||||
return Refuse(negativeKey, url, "a body over the size limit", exchange, "too-large");
|
||||
exchange.Bytes = body.Length;
|
||||
|
||||
return new FetchedJson { FinalUri = target, Document = JsonDocument.Parse(body) };
|
||||
}
|
||||
return Refuse(negativeKey, url, "too many redirects");
|
||||
return Refuse(negativeKey, url, "too many redirects", exchange, "too-many-redirects");
|
||||
}
|
||||
catch (OperationCanceledException) when (!token.IsCancellationRequested)
|
||||
{
|
||||
return Refuse(negativeKey, url, "a timeout");
|
||||
return Refuse(negativeKey, url, "a timeout", exchange, "timeout", transient: true);
|
||||
}
|
||||
catch (Exception ex) when (ex is HttpRequestException or JsonException or BlockedDestinationException)
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
return Refuse(negativeKey, url, ex.Message);
|
||||
return Refuse(negativeKey, url, ex.Message, exchange, "network", transient: true);
|
||||
}
|
||||
catch (Exception ex) when (ex is JsonException or BlockedDestinationException)
|
||||
{
|
||||
return Refuse(negativeKey, url, ex.Message, exchange, ex is JsonException ? "bad-json" : "private-address");
|
||||
}
|
||||
}
|
||||
|
||||
public bool FailedTemporarily(string url) =>
|
||||
Uri.TryCreate(url, UriKind.Absolute, out var target) && _cache.TryGetValue(NegativeKey(target), out bool transient) && transient;
|
||||
|
||||
public async Task<(byte[] Bytes, string ContentType)> GetMedia(string url, long maxBytes, CancellationToken token)
|
||||
{
|
||||
var exchange = new Exchange(url, "media");
|
||||
try
|
||||
{
|
||||
var media = await GetMedia(url, maxBytes, exchange, token);
|
||||
if (media.Bytes == default && exchange.Outcome == Interactions.Ok)
|
||||
exchange.Refused("unusable");
|
||||
return media;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Record(exchange);
|
||||
}
|
||||
}
|
||||
|
||||
async Task<(byte[] Bytes, string ContentType)> GetMedia(string url, long maxBytes, Exchange exchange, CancellationToken token)
|
||||
{
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out var target) || !IsAllowed(target))
|
||||
{
|
||||
exchange.Refused("disallowed");
|
||||
return default;
|
||||
}
|
||||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(token);
|
||||
timeout.CancelAfter(TimeSpan.FromSeconds(60));
|
||||
try
|
||||
@@ -146,6 +254,70 @@ namespace PrivaPub.Infrastructure.Http
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, target);
|
||||
request.Headers.Accept.ParseAdd("image/*, video/*, audio/*");
|
||||
using var response = await _httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
||||
exchange.Answered(response);
|
||||
if (IsRedirect(response.StatusCode))
|
||||
{
|
||||
var location = response.Headers.Location;
|
||||
var next = location == default ? default : location.IsAbsoluteUri ? location : new Uri(target, location);
|
||||
if (!IsAllowed(next))
|
||||
{
|
||||
exchange.Refused("bad-redirect");
|
||||
return default;
|
||||
}
|
||||
target = next;
|
||||
exchange.Hops++;
|
||||
continue;
|
||||
}
|
||||
var mediaType = response.Content.Headers.ContentType?.MediaType?.ToLowerInvariant();
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
exchange.Failed(StatusReason(response));
|
||||
return default;
|
||||
}
|
||||
if (mediaType == default || !(mediaType.StartsWith("image/") || mediaType.StartsWith("video/") || mediaType.StartsWith("audio/"))
|
||||
|| mediaType.Contains("svg"))
|
||||
{
|
||||
exchange.Refused("content-type");
|
||||
return default;
|
||||
}
|
||||
if (response.Content.Headers.ContentLength > maxBytes)
|
||||
{
|
||||
exchange.Refused("too-large");
|
||||
return default;
|
||||
}
|
||||
var bytes = await ReadBounded(response.Content, (int)Math.Min(maxBytes, int.MaxValue), timeout.Token);
|
||||
if (bytes == default)
|
||||
{
|
||||
exchange.Refused("too-large");
|
||||
return default;
|
||||
}
|
||||
exchange.Bytes = bytes.Length;
|
||||
return (bytes, mediaType);
|
||||
}
|
||||
exchange.Refused("too-many-redirects");
|
||||
return default;
|
||||
}
|
||||
catch (Exception ex) when (ex is HttpRequestException or BlockedDestinationException or OperationCanceledException && !token.IsCancellationRequested)
|
||||
{
|
||||
exchange.Failed(ex switch { BlockedDestinationException => "private-address", OperationCanceledException => "timeout", _ => "network" });
|
||||
_logger.LogInformation("Media {Url} refused: {Reason}", url, ex.Message);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(Uri FinalUri, string Html)> GetPage(string url, CancellationToken token)
|
||||
{
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out var target) || !IsAllowed(target))
|
||||
return default;
|
||||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(token);
|
||||
timeout.CancelAfter(RequestTimeout);
|
||||
try
|
||||
{
|
||||
for (var hop = 0; hop <= MaxRedirects; hop++)
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, target);
|
||||
request.Headers.Accept.ParseAdd("text/html, application/xhtml+xml");
|
||||
using var response = await _httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
||||
if (IsRedirect(response.StatusCode))
|
||||
{
|
||||
var location = response.Headers.Location;
|
||||
@@ -156,22 +328,113 @@ namespace PrivaPub.Infrastructure.Http
|
||||
continue;
|
||||
}
|
||||
var mediaType = response.Content.Headers.ContentType?.MediaType?.ToLowerInvariant();
|
||||
if (!response.IsSuccessStatusCode || mediaType == default
|
||||
|| !(mediaType.StartsWith("image/") || mediaType.StartsWith("video/") || mediaType.StartsWith("audio/"))
|
||||
|| mediaType.Contains("svg") || response.Content.Headers.ContentLength > maxBytes)
|
||||
if (!response.IsSuccessStatusCode || mediaType is not ("text/html" or "application/xhtml+xml"))
|
||||
return default;
|
||||
var bytes = await ReadBounded(response.Content, (int)Math.Min(maxBytes, int.MaxValue), timeout.Token);
|
||||
return bytes == default ? default : (bytes, mediaType);
|
||||
var bytes = await ReadPrefix(response.Content, MaxPageBytes, timeout.Token);
|
||||
var charset = response.Content.Headers.ContentType?.CharSet?.Trim('"');
|
||||
var encoding = System.Text.Encoding.UTF8;
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrEmpty(charset))
|
||||
encoding = System.Text.Encoding.GetEncoding(charset);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
}
|
||||
return (target, encoding.GetString(bytes));
|
||||
}
|
||||
return default;
|
||||
}
|
||||
catch (Exception ex) when (ex is HttpRequestException or BlockedDestinationException or OperationCanceledException && !token.IsCancellationRequested)
|
||||
{
|
||||
_logger.LogInformation("Media {Url} refused: {Reason}", url, ex.Message);
|
||||
_logger.LogInformation("Page {Url} refused: {Reason}", url, ex.Message);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> OpenMedia(string url, System.Net.Http.Headers.RangeHeaderValue range, CancellationToken token)
|
||||
{
|
||||
var exchange = new Exchange(url, "stream");
|
||||
try
|
||||
{
|
||||
var response = await OpenMedia(url, range, exchange, token);
|
||||
if (response == default && exchange.Outcome == Interactions.Ok)
|
||||
exchange.Refused("unusable");
|
||||
exchange.Bytes = response?.Content.Headers.ContentLength;
|
||||
return response;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Record(exchange);
|
||||
}
|
||||
}
|
||||
|
||||
async Task<HttpResponseMessage> OpenMedia(string url, System.Net.Http.Headers.RangeHeaderValue range, Exchange exchange, CancellationToken token)
|
||||
{
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out var target) || !IsAllowed(target))
|
||||
{
|
||||
exchange.Refused("disallowed");
|
||||
return default;
|
||||
}
|
||||
try
|
||||
{
|
||||
for (var hop = 0; hop <= MaxRedirects; hop++)
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, target);
|
||||
request.Headers.Accept.ParseAdd("video/*, audio/*, image/*");
|
||||
request.Headers.Range = range;
|
||||
var response = await _httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token);
|
||||
exchange.Answered(response);
|
||||
if (IsRedirect(response.StatusCode))
|
||||
{
|
||||
var location = response.Headers.Location;
|
||||
response.Dispose();
|
||||
var next = location == default ? default : location.IsAbsoluteUri ? location : new Uri(target, location);
|
||||
if (!IsAllowed(next))
|
||||
{
|
||||
exchange.Refused("bad-redirect");
|
||||
return default;
|
||||
}
|
||||
target = next;
|
||||
exchange.Hops++;
|
||||
continue;
|
||||
}
|
||||
var mediaType = response.Content.Headers.ContentType?.MediaType?.ToLowerInvariant();
|
||||
if (!response.IsSuccessStatusCode || mediaType == default || mediaType.Contains("svg")
|
||||
|| !(mediaType.StartsWith("video/") || mediaType.StartsWith("audio/") || mediaType.StartsWith("image/") || mediaType == "application/octet-stream"))
|
||||
{
|
||||
if (response.IsSuccessStatusCode)
|
||||
exchange.Refused("content-type");
|
||||
else
|
||||
exchange.Failed(StatusReason(response));
|
||||
response.Dispose();
|
||||
return default;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
return default;
|
||||
}
|
||||
catch (Exception ex) when (ex is HttpRequestException or BlockedDestinationException or OperationCanceledException && !token.IsCancellationRequested)
|
||||
{
|
||||
exchange.Failed(ex switch { BlockedDestinationException => "private-address", OperationCanceledException => "timeout", _ => "network" });
|
||||
_logger.LogInformation("Media stream {Url} refused: {Reason}", url, ex.Message);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
const int MaxPageBytes = 512 * 1024;
|
||||
|
||||
static async Task<byte[]> ReadPrefix(HttpContent content, int limit, CancellationToken token)
|
||||
{
|
||||
await using var stream = await content.ReadAsStreamAsync(token);
|
||||
var buffer = new byte[limit];
|
||||
var total = 0;
|
||||
int read;
|
||||
while (total < limit && (read = await stream.ReadAsync(buffer.AsMemory(total, limit - total), token)) > 0)
|
||||
total += read;
|
||||
return buffer[..total];
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> Send(HttpRequestMessage request, CancellationToken token)
|
||||
{
|
||||
if (!IsAllowed(request.RequestUri))
|
||||
@@ -200,9 +463,13 @@ namespace PrivaPub.Infrastructure.Http
|
||||
|
||||
static string NegativeKey(Uri target) => "federation-http:refused:" + target.AbsoluteUri;
|
||||
|
||||
FetchedJson Refuse(string negativeKey, string url, string reason)
|
||||
FetchedJson Refuse(string negativeKey, string url, string reason, Exchange exchange, string code, bool transient = false)
|
||||
{
|
||||
_cache.Set(negativeKey, true, NegativeCacheLifetime);
|
||||
if (transient)
|
||||
exchange.Failed(code);
|
||||
else
|
||||
exchange.Refused(code);
|
||||
_cache.Set(negativeKey, transient, NegativeCacheLifetime);
|
||||
_logger.LogInformation("GET {Url} refused: {Reason}", url, reason);
|
||||
return default;
|
||||
}
|
||||
|
||||
@@ -6,5 +6,6 @@ namespace PrivaPub.Infrastructure.Http
|
||||
public bool AllowPlainHttp { get; set; }
|
||||
public bool SecureMode { get; set; }
|
||||
public bool AcceptAnyCertificate { get; set; }
|
||||
public bool FetchLinkPreviews { get; set; } = true;//owner decision 1: the server reads linked pages of public posts
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
namespace PrivaPub.Infrastructure.Http
|
||||
{
|
||||
public static class HttpScope
|
||||
{
|
||||
public const string Request = "request";
|
||||
|
||||
sealed record Tags(string Purpose, string Trigger, bool Crawl);
|
||||
|
||||
static readonly AsyncLocal<Tags> current = new();
|
||||
|
||||
public static string Purpose => current.Value?.Purpose;
|
||||
public static string Trigger => current.Value?.Trigger ?? Request;
|
||||
public static bool Crawling => current.Value?.Crawl == true;
|
||||
|
||||
public static IDisposable For(string purpose) => Push(tags => tags with { Purpose = purpose });
|
||||
|
||||
public static IDisposable Default(string purpose) => Push(tags => tags with { Purpose = tags.Purpose ?? purpose });
|
||||
|
||||
public static IDisposable Triggered(string trigger) => Push(tags => tags with { Trigger = trigger });
|
||||
|
||||
public static IDisposable Crawl() => Push(tags => tags with { Crawl = true });
|
||||
|
||||
static IDisposable Push(Func<Tags, Tags> change)
|
||||
{
|
||||
var previous = current.Value;
|
||||
current.Value = change(previous ?? new Tags(default, default, false));
|
||||
return new Restore(previous);
|
||||
}
|
||||
|
||||
sealed class Restore : IDisposable
|
||||
{
|
||||
readonly Tags _previous;
|
||||
|
||||
public Restore(Tags previous) => _previous = previous;
|
||||
|
||||
public void Dispose() => current.Value = _previous;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using MongoDB.Entities;
|
||||
using PrivaPub.Models.Jobs;
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace PrivaPub.Infrastructure.Jobs
|
||||
{
|
||||
@@ -39,6 +40,13 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
|
||||
readonly string _owner = $"{Environment.MachineName}:{Environment.ProcessId}";
|
||||
readonly ConcurrentDictionary<JobKind, SemaphoreSlim> _signals = new();
|
||||
readonly FilterDefinition<Job> _scope = Builders<Job>.Filter.Empty;
|
||||
|
||||
public JobQueue()
|
||||
{
|
||||
}
|
||||
|
||||
public JobQueue(Expression<Func<Job, bool>> scope) => _scope = Builders<Job>.Filter.Where(scope);
|
||||
|
||||
public async Task<bool> Enqueue(JobKind kind, string payload, string host, string dedupeKey, CancellationToken token) =>
|
||||
await EnqueueMany(new[] { new Job { Kind = kind, Payload = payload, Host = host, DedupeKey = dedupeKey } }, token) == 1;
|
||||
@@ -66,7 +74,7 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
var now = DateTime.UtcNow;
|
||||
var busy = busyHosts?.ToList() ?? new List<string>();
|
||||
return await DB.Default.UpdateAndGet<Job>()
|
||||
.Match(j => j.Kind == kind && j.State == JobState.Pending && j.RunAt <= now && !busy.Contains(j.Host))
|
||||
.Match(f => f.Where(j => j.Kind == kind && j.State == JobState.Pending && j.RunAt <= now && !busy.Contains(j.Host)) & _scope)
|
||||
.Modify(j => j.State, JobState.Running)
|
||||
.Modify(j => j.LeasedUntil, now + LeaseTime)
|
||||
.Modify(j => j.LeaseOwner, _owner)
|
||||
@@ -106,7 +114,7 @@ namespace PrivaPub.Infrastructure.Jobs
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var result = await DB.Default.Update<Job>()
|
||||
.Match(j => j.State == JobState.Running && j.LeasedUntil < now)
|
||||
.Match(f => f.Where(j => j.State == JobState.Running && j.LeasedUntil < now) & _scope)
|
||||
.Modify(j => j.State, JobState.Pending)
|
||||
.Modify(j => j.RunAt, now)
|
||||
.Modify(j => j.LeasedUntil, null)
|
||||
|
||||
Loaded 100 of 127 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user