Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6f1ab0073c | ||
|
|
001fdac3be | ||
|
|
fdcf5176bc | ||
|
|
7be6749a23 | ||
|
|
81de470f64 | ||
|
|
b691c6766d | ||
|
|
fc111d8c46 | ||
|
|
dcd024100a | ||
|
|
bb9bd71391 | ||
|
|
4c94254502 | ||
|
|
829eae3ccf | ||
|
|
8f4d6cbdf9 | ||
|
|
31542a181e | ||
|
|
51f2c10c6f | ||
|
|
a5d9a89445 | ||
|
|
0ccbcd558f | ||
|
|
c00865d805 | ||
|
|
28b581b6b1 | ||
|
|
4a713f3fb6 | ||
|
|
8f75317050 | ||
|
|
525b5368a1 | ||
|
|
d8f163b5ce | ||
|
|
70b4c45eb0 | ||
|
|
0028e96e76 | ||
|
|
b54cdf78b2 | ||
|
|
4d9be37c1c | ||
|
|
e6a6fe46d7 | ||
|
|
7ff3a61e75 | ||
|
|
afd2e727d0 | ||
|
|
b34c71775c | ||
|
|
e99b76dbd7 | ||
|
|
a76cc34557 | ||
|
|
61f6ca0676 | ||
|
|
8af50dad9c |
No files matched your search
@@ -104,4 +104,5 @@ jobs:
|
|||||||
code=$(curl -s -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/activity+json' \
|
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")
|
--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; }
|
[ "$code" = "401" ] || { echo "::error::an unsigned inbox POST answered $code"; exit 1; }
|
||||||
|
tools/smoke/mastodon-api.sh "$PUBLIC_URL"
|
||||||
echo "::notice::serving $served"
|
echo "::notice::serving $served"
|
||||||
@@ -398,3 +398,7 @@ FodyWeavers.xsd
|
|||||||
# JetBrains Rider
|
# JetBrains Rider
|
||||||
*.sln.iml
|
*.sln.iml
|
||||||
|
|
||||||
|
|
||||||
|
PrivaPub/media-store/
|
||||||
|
PrivaPub/media-store-proxy/
|
||||||
|
tools/pasture/.publish/
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
# CLAUDE.md
|
# CLAUDE.md
|
||||||
|
|
||||||
Guidance for working in this repository. `docs/ROADMAP.md` holds the owner's decisions and the phased plan to full
|
Guidance for working in this repository. `docs/ROADMAP.md` holds the owner's decisions and the phased plan to full
|
||||||
ActivityPub interop; read it before changing anything federation-, privacy- or API-shaped.
|
ActivityPub interop; read it before changing anything federation-, privacy- or API-shaped. `docs/INTEROP.md` is the
|
||||||
|
per-platform evidence behind phases P5 to P8: what each peer sends, what it expects, and what PrivaPub still drops.
|
||||||
|
Check the platform's section there before writing a parser or a renderer, and add to it whatever you learn.
|
||||||
|
|
||||||
## What this is
|
## What this is
|
||||||
|
|
||||||
@@ -69,7 +71,7 @@ PrivaPub/ ASP.NET Core Web API, net10.0
|
|||||||
Infrastructure/
|
Infrastructure/
|
||||||
Http/ FederationHttp + SafeHttpHandlerFactory + IpRangeGuard: the only way out
|
Http/ FederationHttp + SafeHttpHandlerFactory + IpRangeGuard: the only way out
|
||||||
Jobs/ JobQueue (leases), JobWorker, Backoff, HostCircuitBreaker
|
Jobs/ JobQueue (leases), JobWorker, Backoff, HostCircuitBreaker
|
||||||
Ids/ PrivacyIds (day-only ids for personas and groups, published-time ids for remote posts)
|
Ids/ PrivacyIds (day-only ids for personas and groups, arrival-ordered ids for remote posts)
|
||||||
Data/ Indexes (created at start), EntityMaps.Warm, Migrations/_NNN_*.cs
|
Data/ Indexes (created at start), EntityMaps.Warm, Migrations/_NNN_*.cs
|
||||||
Cli/ AdminCommands (`PrivaPub admin promote|demote <root>`)
|
Cli/ AdminCommands (`PrivaPub admin promote|demote <root>`)
|
||||||
RateLimiting.cs accounts (per client address) and inbox (per sending origin) policies
|
RateLimiting.cs accounts (per client address) and inbox (per sending origin) policies
|
||||||
@@ -81,13 +83,25 @@ PrivaPub/ ASP.NET Core Web API, net10.0
|
|||||||
Objects/ Origin, ActivityJson, NoteParser, Addressing, ContentSanitizer
|
Objects/ Origin, ActivityJson, NoteParser, Addressing, ContentSanitizer
|
||||||
Moderation/ DomainBlocks (suspend / silence / reject media)
|
Moderation/ DomainBlocks (suspend / silence / reject media)
|
||||||
Signing/ HttpSignatures (draft-cavage sign/verify)
|
Signing/ HttpSignatures (draft-cavage sign/verify)
|
||||||
Inbox/ InboxReceiver (verify, queue, 202) → InboxProcessor (job) → Handlers/{Follow,Undo,Create,Update,Delete}
|
Inbox/ InboxReceiver (verify, queue, 202) → InboxProcessor (job) → Handlers/{Follow,Accept,Reject,
|
||||||
Outbox/ DeliveryService (queues jobs) + DeliveryJobHandler
|
Undo,Create,Update,Delete,Like,Announce}; RemotePosts (build, fetch parents, FetchAncestors)
|
||||||
|
Outbox/ OutboxPublisher (who a post goes to), DeliveryService (queues jobs) + DeliveryJobHandler
|
||||||
Rendering/ ActivityPubRenderer (Mastodon @context, actors, notes, collections)
|
Rendering/ ActivityPubRenderer (Mastodon @context, actors, notes, collections)
|
||||||
Domain/
|
Domain/
|
||||||
Content/ ContentRenderer (Markdown or plain text → HTML with h-card mentions and hashtags)
|
Content/ ContentRenderer (Markdown or plain text → HTML with h-card mentions and hashtags)
|
||||||
|
Social/ FollowService (local in-process, remote Follow/Accept), Notifications
|
||||||
|
Timelines/ Fanout (TimelineEntry rows, Mastodon's home rules), TimelineService
|
||||||
Privacy/ VisibilityPolicy (IsPublic expression, CanSee)
|
Privacy/ VisibilityPolicy (IsPublic expression, CanSee)
|
||||||
Web/Pages/ Razor: /@{user}, /@{user}/{id} (public posts only, strict CSP, noindex)
|
Relationships/ RelationshipService (blocks, mutes, account domain blocks; Hidden), ReportService
|
||||||
|
Media/ MediaService (libvips, ffmpeg remux, blurhash), MediaProxy, MediaJanitor
|
||||||
|
Domain/Statuses/ StatusService: publish, edit, remove, favourite, reblog, for a persona (both client APIs use it)
|
||||||
|
Api/Mastodon/
|
||||||
|
Auth/ OpenIddict setup (keys in Mongo), MastodonScopes, TokenController, OAuthPruner
|
||||||
|
Infrastructure/ MastodonController (avatar context, scopes, errors, Link), MastodonParams, MastodonJson, Page
|
||||||
|
Entities/ Mappers/ Mastodon entities; MastodonMapper (Account, Status), AccountSearch
|
||||||
|
Controllers/ apps, instance, accounts, statuses, timelines, notifications, search, stubs
|
||||||
|
Web/Pages/ Razor: /@{user}, /@{user}/{id} (public posts only, strict CSP, noindex);
|
||||||
|
OAuth/: /oauth/login (root password), /oauth/authorize (choose persona, consent)
|
||||||
Services/ RootUsersService, GroupUsersService, PostsService, AppConfigurationService, …
|
Services/ RootUsersService, GroupUsersService, PostsService, AppConfigurationService, …
|
||||||
Models/ Mongo entities: User/, Group/, Post/, Federation/, Jobs/, AppConfiguration
|
Models/ Mongo entities: User/, Group/, Post/, Federation/, Jobs/, AppConfiguration
|
||||||
StaticServices/ DbEntities (Find<T> accessors), AuthTokenManager (JWT), PasswordHasher
|
StaticServices/ DbEntities (Find<T> accessors), AuthTokenManager (JWT), PasswordHasher
|
||||||
@@ -117,7 +131,8 @@ cd PrivaPub && ASPNETCORE_ENVIRONMENT=Development \
|
|||||||
|
|
||||||
Development config (`appsettings.Development.json`) binds HTTPS 7195 with HTTP/2 only; the overrides above make it
|
Development config (`appsettings.Development.json`) binds HTTPS 7195 with HTTP/2 only; the overrides above make it
|
||||||
curl-able. Outbound fetches only go to https DNS names resolving to public addresses; a test network (Pasture) sets
|
curl-able. Outbound fetches only go to https DNS names resolving to public addresses; a test network (Pasture) sets
|
||||||
`Federation__AllowPrivateNetworks=true` and `Federation__AllowPlainHttp=true`, which startup refuses in Production.
|
`Federation__AllowPrivateNetworks=true`, `Federation__AllowPlainHttp=true` and `Federation__AcceptAnyCertificate=true`,
|
||||||
|
which startup refuses in Production.
|
||||||
|
|
||||||
Promoting an admin on the box (signing up as "admin" grants nothing):
|
Promoting an admin on the box (signing up as "admin" grants nothing):
|
||||||
|
|
||||||
@@ -154,16 +169,49 @@ cd /var/www/privapub.thepra.dev && sudo -u www-data ASPNETCORE_ENVIRONMENT=Produ
|
|||||||
- the shared inbox is advertised in `endpoints.sharedInbox`.
|
- the shared inbox is advertised in `endpoints.sharedInbox`.
|
||||||
7. **Remote HTML is sanitized before it is stored** (`ContentSanitizer`); `Post.ContentHtml` is what is shown,
|
7. **Remote HTML is sanitized before it is stored** (`ContentSanitizer`); `Post.ContentHtml` is what is shown,
|
||||||
`ContentFormat` says what `Text` holds. Remote names are plain text.
|
`ContentFormat` says what `Text` holds. Remote names are plain text.
|
||||||
8. **Circles never federate.** A circle's actor, collections, WebFinger and inbox answer 404, and its posts are
|
8. **Circles federate to members only.** A circle is an undiscoverable Group actor that takes follow requests (the owner
|
||||||
`IsLocalOnly`. Only communities are Group actors.
|
approves); its posts are addressed to the circle and its `/flock`, delivered to members' personal inboxes, never
|
||||||
|
announced, and served only to a signed request from a member or a member's instance actor
|
||||||
|
(`SignedFetchAuthorizer`), 404 otherwise. Circles never appear in search, lookups, mentions or profile pages.
|
||||||
|
**Communities** are FEP-1b12 groups: `GroupDistributor` announces the whole activity (plus the object for new posts,
|
||||||
|
for Mastodon), top-level posts are `Page`s with a `name`, posting follows `Group.PostingPolicy`.
|
||||||
|
Located posts (`LocalGeo`) are the only local-only posts.
|
||||||
9. **A DM joins a conversation only by `DmGroup.ParticipantsKey`**, the exact set of its participants; a remote
|
9. **A DM joins a conversation only by `DmGroup.ParticipantsKey`**, the exact set of its participants; a remote
|
||||||
`context` decides nothing. DMs are `Post`s with `Visibility = Direct` and a `ConversationId` (`DmPost` is legacy).
|
`context` decides nothing. DMs are `Post`s with `Visibility = Direct` and a `ConversationId` (`DmPost` is legacy).
|
||||||
10. **Nothing slow happens inside a request.** Deliveries and inbox processing are `Job`s (`Infrastructure/Jobs`):
|
10. **Nothing slow happens inside a request.** Deliveries and inbox processing are `Job`s (`Infrastructure/Jobs`):
|
||||||
leased, retried on Mastodon's curve, at most two per host, paused per host by `RemoteInstance`. The inbox answers
|
leased, retried on Mastodon's curve, at most two per host, paused per host by `RemoteInstance`. The inbox answers
|
||||||
202 once it has verified and queued; a handler must be idempotent (unique `ObjectURI`, job `DedupeKey`).
|
202 once it has verified and queued; a handler must be idempotent (unique `ObjectURI`, job `DedupeKey`).
|
||||||
11. **Every "may anyone see this" goes through `VisibilityPolicy.IsPublic`;** a persona-specific read uses `CanSee`.
|
11. **Every "may anyone see this" goes through `VisibilityPolicy.IsPublic`;** a persona-specific read uses `CanSee`.
|
||||||
12. **Remote content is stored only when someone here asked for it:** a local persona addressed or mentioned, a reply to
|
12. **Remote content is stored only when someone here asked for it:** a persona follows the author, is addressed or
|
||||||
a local post, or a community the author follows. Followers-only is detected by the author's stored `followers` URL.
|
mentioned, it replies to a local post, or it is addressed to a community the author follows; a public parent is
|
||||||
|
fetched as context. Followers-only is detected by the author's stored `followers` URL.
|
||||||
|
13. **Home timelines are written, not computed:** every stored or created post goes through `Fanout.Distribute`, and
|
||||||
|
every delete removes its `TimelineEntry` rows. Local deletes are soft (content cleared, 410 Tombstone).
|
||||||
|
|
||||||
|
## Mastodon client API invariants
|
||||||
|
|
||||||
|
1. **A token is one persona.** Its subject is the avatar id; the root id lives only in the fifteen-minute `/oauth`
|
||||||
|
cookie used while choosing the persona, and never in a token, an authorization or a response.
|
||||||
|
2. `/api/*` authenticates with OpenIddict validation, everything else with the old JWT (`PrivaPub` policy scheme).
|
||||||
|
Every `/api` request re-checks that the persona's root is neither banned nor deleted (`MastodonController`).
|
||||||
|
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.
|
||||||
|
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
|
||||||
|
through the proxy; only page links (`link.url`, an event's online link) point at the remote site, because following
|
||||||
|
one is the reader's choice.
|
||||||
|
|
||||||
|
## Media invariants
|
||||||
|
|
||||||
|
1. **No upload keeps its metadata.** Images are re-encoded by libvips with `keep=none`; audio and video are remuxed with
|
||||||
|
`-map_metadata -1`. `MediaProcessingTests` checks EXIF and XMP are gone.
|
||||||
|
2. Files live under `Media:Root` (`/var/lib/privapub/media`), never in the published directory; the proxy cache is the
|
||||||
|
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`.
|
||||||
|
|
||||||
## Privacy invariants
|
## Privacy invariants
|
||||||
|
|
||||||
@@ -173,7 +221,17 @@ cd /var/www/privapub.thepra.dev && sudo -u www-data ASPNETCORE_ENVIRONMENT=Produ
|
|||||||
equal to the login.
|
equal to the login.
|
||||||
- **One username space:** personas, groups and the instance reserve their name in `ReservedName` (unique index) before
|
- **One username space:** personas, groups and the instance reserve their name in `ReservedName` (unique index) before
|
||||||
they are saved; `LocalActorService.TryReserveUserName` is the only way to claim one.
|
they are saved; `LocalActorService.TryReserveUserName` is the only way to claim one.
|
||||||
|
- **Usernames are `Constants.UserNameRegex`** (`^[a-z0-9_]+$`): the intersection of what Mastodon and Misskey accept. A
|
||||||
|
name outside it creates a persona nobody on those servers can reach.
|
||||||
|
- **An actor's `published` is `PublishedOn`**, a random whole day up to two weeks before creation, so personas made the
|
||||||
|
same day do not share a date. New persona and group ids carry that same day (`GenerateNewID`), because local clients
|
||||||
|
see ids. Never emit `CreatedAt`.
|
||||||
- **Per-avatar state stays per avatar:** blocks, mutes, notifications, follows. Nothing may relate sibling avatars.
|
- **Per-avatar state stays per avatar:** blocks, mutes, notifications, follows. Nothing may relate sibling avatars.
|
||||||
|
- **Blocks federate** (owner decision, 2026-10-01): a block is sent as `Block` from the blocking avatar, an unblock as
|
||||||
|
`Undo{Block}`. Reports still leave as `Flag` from the instance actor, never from the reporting avatar.
|
||||||
|
- **What PrivaPub reveals is the owner's call.** Previews, blocks, website authorship, views, bridging and reactions were
|
||||||
|
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.**
|
- **Location-ranged posts never federate.**
|
||||||
|
|
||||||
## Data
|
## Data
|
||||||
@@ -184,6 +242,44 @@ cd /var/www/privapub.thepra.dev && sudo -u www-data ASPNETCORE_ENVIRONMENT=Produ
|
|||||||
- **Collection name = class name, so never rename an entity class.**
|
- **Collection name = class name, so never rename an entity class.**
|
||||||
- `Entity.ID` is a 24-character lowercase hex string; `GenerateNewID()` returns `object`, so cast it.
|
- `Entity.ID` is a 24-character lowercase hex string; `GenerateNewID()` returns `object`, so cast it.
|
||||||
- New fields must be additive: a deploy rollback restores the binary, not the database.
|
- New fields must be additive: a deploy rollback restores the binary, not the database.
|
||||||
|
- **Every stored remote object has an `ObjectRecord`** (raw JSON up to 256 KB plus its hash; delivered or fetched; the
|
||||||
|
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
|
||||||
|
`/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** (`UpdateHandler.IsEdit`). 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.
|
||||||
|
- **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
|
||||||
|
published within the last hour (or in the future) a fresh `ObjectId`, which sorts after every post already stored,
|
||||||
|
and only backfill keeps a `published`-derived id. With `published` ids a reply arriving in the same second could sort
|
||||||
|
under the post it answers, and a late arrival landed behind a client's `since_id` and was never seen. `created_at`
|
||||||
|
comes from `CreationDate`, never from the id.
|
||||||
|
- **The same ordering problem exists on the other side, so `published` carries milliseconds**
|
||||||
|
(`ActivityPubRenderer.Timestamp`). GoToSocial (ULIDs) and Mastodon (Snowflakes) derive a remote status's id from
|
||||||
|
`published` at millisecond resolution. With whole seconds, two of our posts from the same second sorted at random
|
||||||
|
there.
|
||||||
- **Startup order:** `EntityMaps.Warm()` (every entity's class map, one at a time; two mapped at once throw "An item
|
- **Startup order:** `EntityMaps.Warm()` (every entity's class map, one at a time; two mapped at once throw "An item
|
||||||
with the same key has already been added" and stay broken), then `MigrateAsync` (`Infrastructure/Data/Migrations`,
|
with the same key has already been added" and stay broken), then `MigrateAsync` (`Infrastructure/Data/Migrations`,
|
||||||
`_NNN_` order, each runs once), then `Indexes.Create()`. A new entity needs nothing; a new unique index needs a
|
`_NNN_` order, each runs once), then `Indexes.Create()`. A new entity needs nothing; a new unique index needs a
|
||||||
@@ -201,6 +297,9 @@ cd /var/www/privapub.thepra.dev && sudo -u www-data ASPNETCORE_ENVIRONMENT=Produ
|
|||||||
- Localised strings go through `IStringLocalizer<GenericRes>`.
|
- Localised strings go through `IStringLocalizer<GenericRes>`.
|
||||||
- Every `Display`/`ErrorMessage` resource key must exist in `FieldsNameResource`/`ErrorsResource`, including the
|
- Every `Display`/`ErrorMessage` resource key must exist in `FieldsNameResource`/`ErrorsResource`, including the
|
||||||
`Designer.cs`, which the CLI build doesn't regenerate. A missing key throws at validation time.
|
`Designer.cs`, which the CLI build doesn't regenerate. A missing key throws at validation time.
|
||||||
|
- **`cond ? value : default` with a value-type branch is the type's default, not null:** `false`, `0`, or year one
|
||||||
|
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
|
- 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 `InboxService.Id`/`Value`, which handle string, object and array.
|
||||||
- Libraries chosen for the roadmap: HtmlSanitizer, Markdig (`DisableHtml`), NSign (RFC 9421 inbound), OpenIddict +
|
- Libraries chosen for the roadmap: HtmlSanitizer, Markdig (`DisableHtml`), NSign (RFC 9421 inbound), OpenIddict +
|
||||||
@@ -219,15 +318,47 @@ Beyond the tests, verify by building, running locally, and exercising:
|
|||||||
- the client API (sign up, create an avatar, a group, a post);
|
- the client API (sign up, create an avatar, a group, a post);
|
||||||
- the ActivityPub endpoints with curl and `Accept: application/activity+json`.
|
- the ActivityPub endpoints with curl and `Accept: application/activity+json`.
|
||||||
|
|
||||||
Interop is checked against real servers: Fediverse Pasture with podman on the workstation, verify.funfedi.dev, and
|
Interop is checked against real servers, starting with the workstation's own pasture:
|
||||||
the owner's GoToSocial at social.arasaka.software. **Ask before acting from the owner's GoToSocial account.**
|
|
||||||
|
```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/run.sh down
|
||||||
|
```
|
||||||
|
|
||||||
|
- **Two sites on one podman network, one Caddy in front.** `privapub.test` and `gts.test` are network aliases of the
|
||||||
|
Caddy container, which serves both with its internal CA (`tls internal`). Both servers are told to accept any
|
||||||
|
certificate: PrivaPub through `appsettings.Pasture.json`, GoToSocial through `GTS_HTTP_CLIENT_TLS_INSECURE_SKIP_VERIFY`.
|
||||||
|
GoToSocial WebFingers and fetches over https only, so plain http between them is not an option.
|
||||||
|
- From the workstation, PrivaPub's API is `http://127.0.0.1:6971`. GoToSocial is reached as `https://gts.test:6443`
|
||||||
|
with `curl -k --resolve gts.test:6443:127.0.0.1`, because its sign-in cookie is bound to the host name.
|
||||||
|
- **GoToSocial's cached home timeline can stop taking new posts after its first read**, its owner's own included, while
|
||||||
|
a `min_id` query shows them all. So a delivery is checked by looking the object up by URI with `resolve=false`
|
||||||
|
(`on_gts`), which answers from GoToSocial's database and never fetches from us. A home-timeline check there proves
|
||||||
|
nothing, in either direction. A deleted status still turns up in that search as a "deleted status" stub, so a delete
|
||||||
|
is checked as a 404 on `/api/v1/statuses/{id}`.
|
||||||
|
- **GoToSocial creates its accounts locked**, so `interop.sh` approves alice's request through
|
||||||
|
`/api/v1/follow_requests`. That also checks our pending (`requested`) state and the manual Accept.
|
||||||
|
- The scenario covers:
|
||||||
|
- discovery and follows both ways;
|
||||||
|
- posts and CW;
|
||||||
|
- a reply and its notification;
|
||||||
|
- likes and boosts both ways;
|
||||||
|
- DMs both ways, and the DM staying off public timelines;
|
||||||
|
- edit, delete both ways, and unfollow.
|
||||||
|
- `run.sh up` replaces every container, Mongo included, so each run starts clean. To keep the data, republish into
|
||||||
|
`tools/pasture/.publish` and `podman restart pasture-privapub`; that is how a migration is tried on dirty data.
|
||||||
|
|
||||||
|
After the pasture, verify.funfedi.dev and the owner's GoToSocial at social.arasaka.software. **Ask before acting from
|
||||||
|
the owner's GoToSocial account.**
|
||||||
|
|
||||||
## Deploy
|
## Deploy
|
||||||
|
|
||||||
- **CI/CD:** push to `master` runs `build.yml` (build + tests) on the instance-wide `build` runner. A `v*` tag runs
|
- **CI/CD:** push to `master` runs `build.yml` (build + tests) on the instance-wide `build` runner. A `v*` tag runs
|
||||||
`deploy.yml`: tests, self-contained linux-x64 publish, snapshot and `mongodump` to `/var/backups/privapub.thepra.dev`,
|
`deploy.yml`: tests, self-contained linux-x64 publish, snapshot and `mongodump` to `/var/backups/privapub.thepra.dev`,
|
||||||
stop → rsync → start, a `127.0.0.1:6970/build.json` health loop with rollback, then public checks (actor, NodeInfo,
|
stop → rsync → start, a `127.0.0.1:6970/build.json` health loop with rollback, then public checks (actor, NodeInfo,
|
||||||
Swagger 404, inbox junk 400, unsigned 401).
|
Swagger 404, inbox junk 400, unsigned 401) and `tools/smoke/mastodon-api.sh` (app registration, client credentials,
|
||||||
|
discovery, instance, public timeline; pass a persona token as a second argument to check the signed-in side).
|
||||||
- **The box:** Max (`nuvola.xyz`). Unit `privapub` runs as www-data from `/var/www/privapub.thepra.dev` with
|
- **The box:** Max (`nuvola.xyz`). Unit `privapub` runs as www-data from `/var/www/privapub.thepra.dev` with
|
||||||
`ASPNETCORE_ENVIRONMENT=Production`.
|
`ASPNETCORE_ENVIRONMENT=Production`.
|
||||||
- **One-time root setup:** `deploy/max/setup.sh`, run through `../arasaka.software/tools/max/run.sh`.
|
- **One-time root setup:** `deploy/max/setup.sh`, run through `../arasaka.software/tools/max/run.sh`.
|
||||||
|
|||||||
+104
-13
@@ -10,14 +10,31 @@ PrivaPub is an ActivityPub server written in C#. This document follows
|
|||||||
- [HTTP Signatures](https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures), `rsa-sha256` / `hs2019` with RSA keys
|
- [HTTP Signatures](https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures), `rsa-sha256` / `hs2019` with RSA keys
|
||||||
- [NodeInfo](https://nodeinfo.diaspora.software/) 2.0 and 2.1
|
- [NodeInfo](https://nodeinfo.diaspora.software/) 2.0 and 2.1
|
||||||
|
|
||||||
|
## Tested against
|
||||||
|
|
||||||
|
- **GoToSocial 0.22.1, end to end.** It runs in a private network on the workstation (`tools/pasture/`) and is driven
|
||||||
|
through its own client API. Checked both ways:
|
||||||
|
- follows, including to a locked account;
|
||||||
|
- public posts with a content warning;
|
||||||
|
- replies with notifications;
|
||||||
|
- likes and boosts;
|
||||||
|
- direct messages;
|
||||||
|
- edits and deletes;
|
||||||
|
- unfollow.
|
||||||
|
- **Mastodon, Misskey, Lemmy and PeerTube, by unit tests only.** The tests feed the parser documents written in each
|
||||||
|
server's shape. No live exchange with any of them has run yet.
|
||||||
|
|
||||||
## Supported FEPs
|
## Supported FEPs
|
||||||
|
|
||||||
- [FEP-67ff: FEDERATION.md](https://codeberg.org/fediverse/fep/src/branch/main/fep/67ff/fep-67ff.md)
|
- [FEP-67ff: FEDERATION.md](https://codeberg.org/fediverse/fep/src/branch/main/fep/67ff/fep-67ff.md)
|
||||||
- [FEP-f1d5: NodeInfo in Fediverse Software](https://codeberg.org/fediverse/fep/src/branch/main/fep/f1d5/fep-f1d5.md)
|
- [FEP-f1d5: NodeInfo in Fediverse Software](https://codeberg.org/fediverse/fep/src/branch/main/fep/f1d5/fep-f1d5.md)
|
||||||
- [FEP-2c59: Discovery of a WebFinger address from an ActivityPub actor](https://codeberg.org/fediverse/fep/src/branch/main/fep/2c59/fep-2c59.md)
|
- [FEP-2c59: Discovery of a WebFinger address from an ActivityPub actor](https://codeberg.org/fediverse/fep/src/branch/main/fep/2c59/fep-2c59.md)
|
||||||
|
- [FEP-1b12: Group federation](https://codeberg.org/fediverse/fep/src/branch/main/fep/1b12/fep-1b12.md) (communities; see "Groups")
|
||||||
|
|
||||||
Planned: FEP-1b12 (communities), FEP-8fcf (followers synchronisation), FEP-5feb (`indexable`), FEP-7628 (Move),
|
Planned (see `docs/ROADMAP.md`, phases P5 to P8, and the per-platform notes in `docs/INTEROP.md`): FEP-044f (quotes),
|
||||||
FEP-044f (quotes).
|
FEP-9967 (polls), FEP-c0e0 (emoji reactions), FEP-9098 (custom emoji), FEP-7888 and FEP-f228 (threads), FEP-7628 (Move),
|
||||||
|
FEP-8fcf (followers synchronisation), FEP-5feb (`indexable`), FEP-8967 (link attachments), FEP-521a and FEP-8b32 (keys
|
||||||
|
and integrity proofs), FEP-ae0c (relays).
|
||||||
|
|
||||||
## Actors
|
## Actors
|
||||||
|
|
||||||
@@ -39,7 +56,8 @@ actor document, a collection, NodeInfo or a delivery relates two avatars of the
|
|||||||
The names are the project's own and are stable; resolve actors through WebFinger, not by guessing a path.
|
The names are the project's own and are stable; resolve actors through WebFinger, not by guessing a path.
|
||||||
|
|
||||||
- The key is `{actor}#main-key`, RSA 2048, served as SPKI PEM with `owner` set to the actor.
|
- The key is `{actor}#main-key`, RSA 2048, served as SPKI PEM with `owner` set to the actor.
|
||||||
- `published` on an actor is truncated to the day. `indexable` is `false`.
|
- `published` on an actor is a whole day, chosen at random up to two weeks before the account was made, so two
|
||||||
|
personas made on the same day do not share a date. `indexable` is `false`.
|
||||||
- The instance actor is `/peasants/privapub` (type `Application`). It signs every fetch PrivaPub makes, so no avatar's key
|
- The instance actor is `/peasants/privapub` (type `Application`). It signs every fetch PrivaPub makes, so no avatar's key
|
||||||
is used to read another server's content.
|
is used to read another server's content.
|
||||||
|
|
||||||
@@ -47,10 +65,18 @@ The names are the project's own and are stable; resolve actors through WebFinger
|
|||||||
|
|
||||||
A group is either a **community** or a **circle**.
|
A group is either a **community** or a **circle**.
|
||||||
|
|
||||||
- A community is a `Group` actor. It accepts `Follow` and re-shares (`Announce`) posts from its followers that address
|
- A **community** follows [FEP-1b12](https://codeberg.org/fediverse/fep/src/branch/main/fep/1b12/fep-1b12.md). Posts
|
||||||
it. Full FEP-1b12 behaviour (announcing activities, `audience`, moderation) is planned.
|
addressed to it (in `to`, `cc` or `audience`) are accepted according to its posting policy (followers, anyone, or
|
||||||
- A circle is private and does not federate yet: its actor, collections and WebFinger answer 404, and its posts are never
|
moderators only) and the group `Announce`s the whole activity, with `audience` set, to its followers. A new post is
|
||||||
delivered.
|
also announced as an object so Mastodon shows it. Updates and deletes of community content are announced too. A
|
||||||
|
top-level post is a `Page` with a `name`. Members are counted at `/flock`; moderators are listed at `/wardens`, which
|
||||||
|
the actor's `attributedTo` points to, with `postingRestrictedToMods` as Lemmy expects. A mention of a community posts into it.
|
||||||
|
- A **circle** is private. Its actor is not discoverable and every follow is a request. Its posts are addressed to the
|
||||||
|
circle and its members collection, delivered to each member's own inbox and never announced. They are served only
|
||||||
|
to a signed request from a member, or from the instance actor of a member's server; anyone else gets 404. A
|
||||||
|
Mastodon member's replies reach only the people they mention.
|
||||||
|
- Announces from **remote** groups (Lemmy communities) are followed through to the activity: the object is fetched
|
||||||
|
from its own origin, never taken from the announce.
|
||||||
|
|
||||||
## Activities
|
## Activities
|
||||||
|
|
||||||
@@ -59,21 +85,80 @@ Received:
|
|||||||
| Activity | Effect |
|
| Activity | Effect |
|
||||||
|---|---|
|
|---|---|
|
||||||
| `Follow` | follows an avatar or community; `Accept` is sent unless the community approves members by hand |
|
| `Follow` | follows an avatar or community; `Accept` is sent unless the community approves members by hand |
|
||||||
| `Undo{Follow}` | unfollows |
|
| `Accept{Follow}`, `Reject{Follow}` | completes or ends a follow an avatar requested |
|
||||||
| `Create{Note, Article, Page, Question, …}` | stored when it addresses or mentions a local avatar, replies to a local post, or is addressed to a community the author follows |
|
| `Undo{Follow, Like, Announce}` | reverses it |
|
||||||
|
| `Create{Note, Article, Page, Question, Video, Audio, Event, ChatMessage, …}` | stored when a local avatar follows the author, is addressed or mentioned, when it replies to a local post, or when it is addressed to a community the author follows; a public parent is fetched to complete the thread |
|
||||||
| `Update{Note}` | replaces the content; the previous version is kept |
|
| `Update{Note}` | replaces the content; the previous version is kept |
|
||||||
| `Update{Person}` | refetches the actor |
|
| `Update{Person}` | refetches the actor |
|
||||||
| `Delete` | deletes the object, or the actor and its follows |
|
| `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: `Create{Note}`, `Delete{Tombstone}`, `Accept{Follow}`, `Announce` (communities).
|
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`.
|
||||||
|
Uploaded files have all metadata removed.
|
||||||
|
- **Pinned posts** are the actor's `featured` collection (`/trophies`); `featuredTags` is `/tattoos`.
|
||||||
|
- **Blocks are sent.** A blocked remote account receives `Block` from the blocking account (and `Reject{Follow}` if it
|
||||||
|
followed); an unblock sends `Undo{Block}`.
|
||||||
|
- **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`
|
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.
|
||||||
|
- **Not yet.** Our own posts cannot be quoted by others yet: we do not issue stamps.
|
||||||
|
|
||||||
|
**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".
|
`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`,
|
||||||
|
`Page`, `Event`, `Video` or `Audio` it is an excerpt or description, kept as such and never hidden.
|
||||||
|
|
||||||
Visibility is expressed in `to`/`cc` the way Mastodon does it: public, unlisted, followers-only and direct. Inbound
|
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.
|
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.
|
||||||
|
|
||||||
## Security rules a peer will notice
|
## Security rules a peer will notice
|
||||||
|
|
||||||
- **Signatures.** Inbox POSTs must be signed over `(request-target)`, `host`, `digest` and `date` (or `(created)`). The
|
- **Signatures.** Inbox POSTs must be signed over `(request-target)`, `host`, `digest` and `date` (or `(created)`). The
|
||||||
@@ -85,11 +170,17 @@ followers-only posts are recognised by the author's own `followers` collection.
|
|||||||
- **Fetching.** All fetches are signed by the instance actor. They go only to public addresses, follow at most three
|
- **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.
|
redirects and read at most 1 MB.
|
||||||
- **HTML.** Received HTML is sanitised to Mastodon's allowlist.
|
- **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,
|
- **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.
|
starting at an hour and growing to a week.
|
||||||
|
|
||||||
## Known limitations
|
## Known limitations
|
||||||
|
|
||||||
- Likes, boosts, follow requests to remote accounts, media uploads and polls are not implemented yet.
|
- 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.
|
||||||
- Collections expose counts, not members.
|
- Collections expose counts, not members.
|
||||||
- Only `rsa-sha256`-style keys are verified. RFC 9421 signatures are planned.
|
- Only `rsa-sha256`-style keys are verified. RFC 9421 signatures are planned.
|
||||||
@@ -9,5 +9,6 @@
|
|||||||
public const int GroupInvitationLength = 64;
|
public const int GroupInvitationLength = 64;
|
||||||
|
|
||||||
public const string PasswordRegex = @"^(?=.*)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$";
|
public const string PasswordRegex = @"^(?=.*)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$";
|
||||||
|
public const string UserNameRegex = "^[a-z0-9_]+$";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,7 @@ namespace PrivaPub.ClientModels.Group
|
|||||||
|
|
||||||
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ErrorsResource)),
|
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ErrorsResource)),
|
||||||
StringLength(32, MinimumLength = 3, ErrorMessageResourceName = "StringLengthMinMax", ErrorMessageResourceType = typeof(ErrorsResource)),
|
StringLength(32, MinimumLength = 3, ErrorMessageResourceName = "StringLengthMinMax", ErrorMessageResourceType = typeof(ErrorsResource)),
|
||||||
RegularExpression("^[a-z0-9_]+$", ErrorMessageResourceName = "EmptySpacesNotAllowed", ErrorMessageResourceType = typeof(ErrorsResource)),
|
RegularExpression(Constants.UserNameRegex, ErrorMessageResourceName = "InvalidUserName", ErrorMessageResourceType = typeof(ErrorsResource)),
|
||||||
NoWhiteSpaces]
|
NoWhiteSpaces]
|
||||||
public string UserName { get; set; }
|
public string UserName { get; set; }
|
||||||
|
|
||||||
@@ -25,6 +25,7 @@ namespace PrivaPub.ClientModels.Group
|
|||||||
|
|
||||||
public string InvitationPassword { get; set; }
|
public string InvitationPassword { get; set; }
|
||||||
public bool IsCommunity { get; set; }
|
public bool IsCommunity { get; set; }
|
||||||
|
public string PostingPolicy { get; set; }//followers (default), anyone, moderators
|
||||||
public bool IsDiscoverable { get; set; } = true;
|
public bool IsDiscoverable { get; set; } = true;
|
||||||
public bool ManuallyApprovesMembers { get; set; }
|
public bool ManuallyApprovesMembers { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ namespace PrivaPub.ClientModels.Group
|
|||||||
public bool RemoveInvitationPassword { get; set; }
|
public bool RemoveInvitationPassword { get; set; }
|
||||||
public bool RegenerateInvitationCode { get; set; }
|
public bool RegenerateInvitationCode { get; set; }
|
||||||
public bool? IsDiscoverable { get; set; }
|
public bool? IsDiscoverable { get; set; }
|
||||||
|
public string PostingPolicy { get; set; }
|
||||||
public bool? ManuallyApprovesMembers { get; set; }
|
public bool? ManuallyApprovesMembers { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@ namespace PrivaPub.ClientModels.Group
|
|||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
public string Handle { get; set; }
|
public string Handle { get; set; }
|
||||||
public bool IsCommunity { get; set; }
|
public bool IsCommunity { get; set; }
|
||||||
|
public string PostingPolicy { get; set; }
|
||||||
public bool IsDiscoverable { get; set; }
|
public bool IsDiscoverable { get; set; }
|
||||||
public bool ManuallyApprovesMembers { get; set; }
|
public bool ManuallyApprovesMembers { get; set; }
|
||||||
public bool IsOwner { get; set; }
|
public bool IsOwner { get; set; }
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace PrivaPub.ClientModels
|
|||||||
public string InvitationCode { get; set; }
|
public string InvitationCode { get; set; }
|
||||||
|
|
||||||
[StringLength(32, MinimumLength = 3, ErrorMessageResourceName = "StringLengthMinMax", ErrorMessageResourceType = typeof(ErrorsResource)),
|
[StringLength(32, MinimumLength = 3, ErrorMessageResourceName = "StringLengthMinMax", ErrorMessageResourceType = typeof(ErrorsResource)),
|
||||||
RegularExpression("^[a-z0-9_]+$", ErrorMessageResourceName = "EmptySpacesNotAllowed", ErrorMessageResourceType = typeof(ErrorsResource))]
|
RegularExpression(Constants.UserNameRegex, ErrorMessageResourceName = "InvalidUserName", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
public string AvatarUserName { get; set; }
|
public string AvatarUserName { get; set; }
|
||||||
|
|
||||||
[StringLength(64, ErrorMessageResourceName = "MaxLengthString", ErrorMessageResourceType = typeof(ErrorsResource))]
|
[StringLength(64, ErrorMessageResourceName = "MaxLengthString", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
|||||||
@@ -19,6 +19,41 @@ namespace PrivaPub.ClientModels.Post
|
|||||||
public string GroupId { get; set; }
|
public string GroupId { get; set; }
|
||||||
public string AnsweringToPostId { get; set; }
|
public string AnsweringToPostId { get; set; }
|
||||||
public bool HasContentWarning { get; set; }
|
public bool HasContentWarning { get; set; }
|
||||||
|
|
||||||
|
[StringLength(500, ErrorMessageResourceName = "MaxLengthString", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public string SpoilerText { get; set; }
|
||||||
|
|
||||||
|
public string Visibility { get; set; }//public (default), unlisted, followersonly
|
||||||
|
|
||||||
|
[Range(-90, 90, ErrorMessageResourceName = "Range", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public double? Latitude { get; set; }
|
||||||
|
|
||||||
|
[Range(-180, 180, ErrorMessageResourceName = "Range", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public double? Longitude { get; set; }
|
||||||
|
|
||||||
|
[Range(1, 50, ErrorMessageResourceName = "Range", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public double? RangeKm { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class UpdatePostForm
|
||||||
|
{
|
||||||
|
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public string AvatarId { get; set; }
|
||||||
|
|
||||||
|
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public string PostId { get; set; }
|
||||||
|
|
||||||
|
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ErrorsResource)),
|
||||||
|
StringLength(5000, ErrorMessageResourceName = "MaxLengthString", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public string Text { get; set; }
|
||||||
|
|
||||||
|
[StringLength(200, ErrorMessageResourceName = "MaxLengthString", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public string Title { get; set; }
|
||||||
|
|
||||||
|
public bool HasContentWarning { get; set; }
|
||||||
|
|
||||||
|
[StringLength(500, ErrorMessageResourceName = "MaxLengthString", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public string SpoilerText { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class InsertDmForm
|
public class InsertDmForm
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ namespace PrivaPub.ClientModels.Post
|
|||||||
public bool HasContentWarning { get; set; }
|
public bool HasContentWarning { get; set; }
|
||||||
public bool IsFederatedCopy { get; set; }
|
public bool IsFederatedCopy { get; set; }
|
||||||
public DateTime CreationDate { get; set; }
|
public DateTime CreationDate { get; set; }
|
||||||
|
public ViewPost ReblogOf { get; set; }
|
||||||
|
public double? RangeKm { get; set; }
|
||||||
|
public double? DistanceKm { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ViewDmGroup
|
public class ViewDmGroup
|
||||||
|
|||||||
@@ -105,6 +105,15 @@ namespace PrivaPub.ClientModels.Resources {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to Use only lower-case letters, digits and underscores..
|
||||||
|
/// </summary>
|
||||||
|
public static string InvalidUserName {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("InvalidUserName", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Looks up a localized string similar to Invalid password, upper-case, lower-case and number characters required..
|
/// Looks up a localized string similar to Invalid password, upper-case, lower-case and number characters required..
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -132,6 +132,9 @@
|
|||||||
<data name="InvalidEmail" xml:space="preserve">
|
<data name="InvalidEmail" xml:space="preserve">
|
||||||
<value>{0} è invalida.</value>
|
<value>{0} è invalida.</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="InvalidUserName" xml:space="preserve">
|
||||||
|
<value>Usa solo lettere minuscole, cifre e trattini bassi.</value>
|
||||||
|
</data>
|
||||||
<data name="InvalidPassword" xml:space="preserve">
|
<data name="InvalidPassword" xml:space="preserve">
|
||||||
<value>Password non valida, deve avere almeno un carattere grande, un carattere piccolo ed un numero.</value>
|
<value>Password non valida, deve avere almeno un carattere grande, un carattere piccolo ed un numero.</value>
|
||||||
</data>
|
</data>
|
||||||
|
|||||||
@@ -132,6 +132,9 @@
|
|||||||
<data name="InvalidEmail" xml:space="preserve">
|
<data name="InvalidEmail" xml:space="preserve">
|
||||||
<value>{0} is invalid.</value>
|
<value>{0} is invalid.</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="InvalidUserName" xml:space="preserve">
|
||||||
|
<value>Use only lower-case letters, digits and underscores.</value>
|
||||||
|
</data>
|
||||||
<data name="InvalidPassword" xml:space="preserve">
|
<data name="InvalidPassword" xml:space="preserve">
|
||||||
<value>Invalid password, upper-case, lower-case and number characters required.</value>
|
<value>Invalid password, upper-case, lower-case and number characters required.</value>
|
||||||
</data>
|
</data>
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
using PrivaPub.ClientModels.Resources;
|
||||||
|
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace PrivaPub.ClientModels.Social
|
||||||
|
{
|
||||||
|
public class FollowForm
|
||||||
|
{
|
||||||
|
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public string AvatarId { get; set; }
|
||||||
|
|
||||||
|
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ErrorsResource)),
|
||||||
|
StringLength(2048, ErrorMessageResourceName = "MaxLengthString", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public string Target { get; set; }//user@host, @user, or an actor URI
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ViewFollowing
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string TargetActorURI { get; set; }
|
||||||
|
public string TargetAccountId { get; set; }
|
||||||
|
public bool TargetIsLocal { get; set; }
|
||||||
|
public string State { get; set; }
|
||||||
|
public DateTime CreatedAt { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ViewNotification
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Type { get; set; }
|
||||||
|
public string FromAccountId { get; set; }
|
||||||
|
public string FromActorURI { get; set; }
|
||||||
|
public string PostId { get; set; }
|
||||||
|
public bool IsRead { get; set; }
|
||||||
|
public DateTime CreatedAt { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ namespace PrivaPub.ClientModels.User.Avatar
|
|||||||
|
|
||||||
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ErrorsResource)),
|
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ErrorsResource)),
|
||||||
StringLength(Constants.MaxAvatarNameLength, ErrorMessageResourceName = "StringLength", ErrorMessageResourceType = typeof(ErrorsResource)),
|
StringLength(Constants.MaxAvatarNameLength, ErrorMessageResourceName = "StringLength", ErrorMessageResourceType = typeof(ErrorsResource)),
|
||||||
|
RegularExpression(Constants.UserNameRegex, ErrorMessageResourceName = "InvalidUserName", ErrorMessageResourceType = typeof(ErrorsResource)),
|
||||||
Display(Name = nameof(UserName), ResourceType = typeof(FieldsNameResource))]
|
Display(Name = nameof(UserName), ResourceType = typeof(FieldsNameResource))]
|
||||||
public string UserName { get; set; }//preferredUsername
|
public string UserName { get; set; }//preferredUsername
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Auth;
|
||||||
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||||
|
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Api
|
||||||
|
{
|
||||||
|
public class MastodonParamsTests
|
||||||
|
{
|
||||||
|
static async Task<MastodonParams> Read(string query = "", string contentType = default, string body = default)
|
||||||
|
{
|
||||||
|
var context = new DefaultHttpContext();
|
||||||
|
context.Request.QueryString = new QueryString(query);
|
||||||
|
if (body != default)
|
||||||
|
{
|
||||||
|
context.Request.ContentType = contentType;
|
||||||
|
var bytes = Encoding.UTF8.GetBytes(body);
|
||||||
|
context.Request.Body = new MemoryStream(bytes);
|
||||||
|
context.Request.ContentLength = bytes.Length;
|
||||||
|
}
|
||||||
|
return await MastodonParams.Read(context.Request, TestContext.Current.CancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Absent_values_are_null_not_false_or_zero()
|
||||||
|
{
|
||||||
|
var parameters = await Read();
|
||||||
|
|
||||||
|
Assert.Null(parameters.Bool("sensitive"));
|
||||||
|
Assert.Null(parameters.Int("limit"));
|
||||||
|
Assert.Null(parameters.Get("status"));
|
||||||
|
Assert.Empty(parameters.List("media_ids"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Reads_rails_style_arrays_from_the_query()
|
||||||
|
{
|
||||||
|
var parameters = await Read("?id[]=1&id[]=2&limit=5&local=true");
|
||||||
|
|
||||||
|
Assert.Equal(new[] { "1", "2" }, parameters.List("id"));
|
||||||
|
Assert.Equal(5, parameters.Int("limit"));
|
||||||
|
Assert.True(parameters.Bool("local"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Reads_forms_and_nested_keys()
|
||||||
|
{
|
||||||
|
var parameters = await Read(contentType: "application/x-www-form-urlencoded",
|
||||||
|
body: "status=hi&media_ids[]=a&media_ids[]=b&poll[options][]=x&poll[options][]=y&sensitive=0");
|
||||||
|
|
||||||
|
Assert.Equal("hi", parameters.Get("status"));
|
||||||
|
Assert.Equal(new[] { "a", "b" }, parameters.List("media_ids"));
|
||||||
|
Assert.Equal(new[] { "x", "y" }, parameters.List("poll[options]"));
|
||||||
|
Assert.False(parameters.Bool("sensitive"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Reads_json_bodies_the_same_way()
|
||||||
|
{
|
||||||
|
var parameters = await Read(contentType: "application/json",
|
||||||
|
body: """{"status":"hi","media_ids":["a","b"],"poll":{"options":["x","y"],"expires_in":300},"sensitive":true,"source":{"privacy":"unlisted"}}""");
|
||||||
|
|
||||||
|
Assert.Equal(new[] { "a", "b" }, parameters.List("media_ids"));
|
||||||
|
Assert.Equal(new[] { "x", "y" }, parameters.List("poll[options]"));
|
||||||
|
Assert.Equal(300, parameters.Int("poll[expires_in]"));
|
||||||
|
Assert.True(parameters.Bool("sensitive"));
|
||||||
|
Assert.Equal("unlisted", parameters.Get("source[privacy]"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(new[] { "read" }, "read:statuses", true)]
|
||||||
|
[InlineData(new[] { "read:statuses" }, "read:statuses", true)]
|
||||||
|
[InlineData(new[] { "read:accounts" }, "read:statuses", false)]
|
||||||
|
[InlineData(new[] { "write" }, "read:statuses", false)]
|
||||||
|
[InlineData(new[] { "follow" }, "write:follows", true)]
|
||||||
|
[InlineData(new[] { "follow" }, "write:statuses", false)]
|
||||||
|
public void Scopes_grant_like_mastodons(string[] granted, string required, bool expected) =>
|
||||||
|
Assert.Equal(expected, MastodonScopes.Grants(granted, required));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||||
|
using PrivaPub.Api.Mastodon.Mappers;
|
||||||
|
using PrivaPub.ClientModels.Post;
|
||||||
|
using PrivaPub.ClientModels.Social;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Tests.Support;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Api
|
||||||
|
{
|
||||||
|
[Trait("Category", "Integration")]
|
||||||
|
public sealed class PersonaSeparationTests : 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Nothing_shown_to_one_persona_names_its_sibling()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (root, alice) = await _harness.Persona("alice");
|
||||||
|
var (_, sibling) = await _harness.Persona("sibling", root);
|
||||||
|
var (bobRoot, bob) = await _harness.Persona("bob");
|
||||||
|
await _harness.Follows.Follow(root, new FollowForm { AvatarId = alice.Id, Target = bob.UserName }, token);
|
||||||
|
await _harness.Follows.Follow(root, new FollowForm { AvatarId = sibling.Id, Target = bob.UserName }, token);
|
||||||
|
await _harness.Posts.InsertPost(root, new InsertPostForm { AvatarId = sibling.Id, Text = "from the sibling" }, token);
|
||||||
|
await _harness.Posts.InsertPost(root, new InsertPostForm { AvatarId = alice.Id, Text = "from alice" }, token);
|
||||||
|
await _harness.Posts.InsertPost(bobRoot, new InsertPostForm { AvatarId = bob.Id, Text = "hi both" }, token);
|
||||||
|
|
||||||
|
var mapper = new MastodonMapper(_harness.Db, _harness.Local);
|
||||||
|
var account = await mapper.Local(alice, withSource: true, token);
|
||||||
|
var homeIds = (await _harness.Db.TimelineEntries.Match(e => e.AvatarId == alice.Id).ExecuteAsync(token)).Select(e => e.PostId).ToList();
|
||||||
|
var home = await _harness.Db.Posts.Match(p => homeIds.Contains(p.ID)).ExecuteAsync(token);
|
||||||
|
var statuses = await mapper.Statuses(home, alice.Id, token);
|
||||||
|
var bobAsSeenByAlice = await mapper.Account(bob.Id, token);
|
||||||
|
|
||||||
|
var json = JsonSerializer.Serialize(new object[] { account, statuses, bobAsSeenByAlice }, MastodonJson.Options);
|
||||||
|
Assert.DoesNotContain(sibling.Id, json);
|
||||||
|
Assert.DoesNotContain(sibling.UserName, json);
|
||||||
|
Assert.DoesNotContain(root, json);
|
||||||
|
Assert.Contains("hi both", json);
|
||||||
|
Assert.Equal(2, bobAsSeenByAlice.FollowersCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
using PrivaPub.Api.Mastodon.Mappers;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Api
|
||||||
|
{
|
||||||
|
public class StatusContentTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void A_remote_article_shows_its_title_above_its_body() =>
|
||||||
|
Assert.Equal("<p><strong>Rain & sun</strong></p><p>body</p>", MastodonMapper.Content(new Post
|
||||||
|
{
|
||||||
|
IsFederatedCopy = true, ObjectType = "Article", Title = "Rain & sun", ContentHtml = "<p>body</p>"
|
||||||
|
}));
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_remote_note_keeps_its_content_alone() =>
|
||||||
|
Assert.Equal("<p>body</p>", MastodonMapper.Content(new Post
|
||||||
|
{
|
||||||
|
IsFederatedCopy = true, ObjectType = "Note", Title = "a poll choice", ContentHtml = "<p>body</p>"
|
||||||
|
}));
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_remote_event_without_a_body_shows_its_excerpt() =>
|
||||||
|
Assert.Equal("<p><strong>Meetup</strong></p><p>Saturday <10:00></p>", MastodonMapper.Content(new Post
|
||||||
|
{
|
||||||
|
IsFederatedCopy = true, ObjectType = "Event", Title = "Meetup", Excerpt = "Saturday <10:00>", ContentHtml = string.Empty
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels.Social;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Tests.Support;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Domain
|
||||||
|
{
|
||||||
|
[Trait("Category", "Integration")]
|
||||||
|
public sealed class FollowTests : 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_local_follow_is_accepted_at_once_and_notified()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (root, alice) = await _harness.Persona("alice");
|
||||||
|
var (_, bob) = await _harness.Persona("bob");
|
||||||
|
|
||||||
|
var result = await _harness.Follows.Follow(root, new FollowForm { AvatarId = alice.Id, Target = "@" + bob.UserName }, token);
|
||||||
|
|
||||||
|
Assert.True(result.IsValid);
|
||||||
|
var following = await DB.Default.Find<Following>().Match(f => f.AvatarId == alice.Id).ExecuteSingleAsync(token);
|
||||||
|
Assert.Equal(FollowState.Accepted, following.State);
|
||||||
|
Assert.True(following.TargetIsLocal);
|
||||||
|
Assert.True((await DB.Default.Find<Follower>().Match(f => f.LocalActorId == bob.Id && f.ActorURI == alice.Uri).ExecuteSingleAsync(token)).IsAccepted);
|
||||||
|
Assert.Equal(NotificationType.Follow, (await DB.Default.Find<Notification>().Match(n => n.AvatarId == bob.Id).ExecuteSingleAsync(token)).Type);
|
||||||
|
Assert.Empty(await _harness.Outgoing(bob.Inbox));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Someone_elses_persona_cannot_be_made_to_follow()
|
||||||
|
{
|
||||||
|
var (_, alice) = await _harness.Persona("alice");
|
||||||
|
var (_, bob) = await _harness.Persona("bob");
|
||||||
|
|
||||||
|
var result = await _harness.Follows.Follow("someone-else", new FollowForm { AvatarId = alice.Id, Target = bob.UserName }, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
Assert.False(result.IsValid);
|
||||||
|
Assert.Equal(404, result.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_remote_follow_is_requested_then_accepted()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (root, alice) = await _harness.Persona("alice");
|
||||||
|
var carol = new RemoteActor(_harness.Peer, "carol");
|
||||||
|
|
||||||
|
await _harness.Follows.Follow(root, new FollowForm { AvatarId = alice.Id, Target = carol.Id }, token);
|
||||||
|
|
||||||
|
var following = await DB.Default.Find<Following>().Match(f => f.AvatarId == alice.Id).ExecuteSingleAsync(token);
|
||||||
|
Assert.Equal(FollowState.Requested, following.State);
|
||||||
|
var follow = Assert.Single(await _harness.Outgoing(carol.Id + "/inbox"));
|
||||||
|
Assert.Equal("Follow", follow["type"]!.GetValue<string>());
|
||||||
|
Assert.Equal(alice.Uri, follow["actor"]!.GetValue<string>());
|
||||||
|
Assert.StartsWith(alice.Uri + "/grunts/follow-", follow["id"]!.GetValue<string>());
|
||||||
|
|
||||||
|
var accept = new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{carol.Id}#accepts/{Guid.NewGuid():N}",
|
||||||
|
["type"] = "Accept",
|
||||||
|
["actor"] = carol.Id,
|
||||||
|
["object"] = follow.DeepClone()
|
||||||
|
};
|
||||||
|
Assert.Equal(202, (await _harness.Deliver(carol, $"/peasants/{alice.UserName}/mouth", accept)).StatusCode);
|
||||||
|
Assert.Equal(FollowState.Accepted, (await DB.Default.Find<Following>().OneAsync(following.ID, token)).State);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task An_accept_from_someone_else_changes_nothing_and_a_reject_ends_the_request()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (root, alice) = await _harness.Persona("alice");
|
||||||
|
var carol = new RemoteActor(_harness.Peer, "carol");
|
||||||
|
var mallory = new RemoteActor(_harness.Peer, "mallory");
|
||||||
|
await _harness.Follows.Follow(root, new FollowForm { AvatarId = alice.Id, Target = carol.Id }, token);
|
||||||
|
var following = await DB.Default.Find<Following>().Match(f => f.AvatarId == alice.Id).ExecuteSingleAsync(token);
|
||||||
|
|
||||||
|
await _harness.Deliver(mallory, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{mallory.Id}#accepts/{Guid.NewGuid():N}", ["type"] = "Accept", ["actor"] = mallory.Id, ["object"] = following.FollowActivityURI
|
||||||
|
});
|
||||||
|
Assert.Equal(FollowState.Requested, (await DB.Default.Find<Following>().OneAsync(following.ID, token)).State);
|
||||||
|
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{carol.Id}#rejects/{Guid.NewGuid():N}", ["type"] = "Reject", ["actor"] = carol.Id, ["object"] = following.FollowActivityURI
|
||||||
|
});
|
||||||
|
Assert.Null(await DB.Default.Find<Following>().OneAsync(following.ID, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Unfollowing_a_remote_account_sends_an_undo()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (root, alice) = await _harness.Persona("alice");
|
||||||
|
var carol = new RemoteActor(_harness.Peer, "carol");
|
||||||
|
await _harness.Follows.Follow(root, new FollowForm { AvatarId = alice.Id, Target = carol.Id }, token);
|
||||||
|
|
||||||
|
await _harness.Follows.Unfollow(root, new FollowForm { AvatarId = alice.Id, Target = carol.Id }, token);
|
||||||
|
|
||||||
|
Assert.False(await DB.Default.Find<Following>().Match(f => f.AvatarId == alice.Id).ExecuteAnyAsync(token));
|
||||||
|
var undo = (await _harness.Outgoing(carol.Id + "/inbox")).Single(a => a["type"]!.GetValue<string>() == "Undo");
|
||||||
|
Assert.Equal("Follow", undo["object"]!["type"]!.GetValue<string>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,102 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using NetVips;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Media;
|
||||||
|
using PrivaPub.Domain.Statuses;
|
||||||
|
using PrivaPub.Federation.Rendering;
|
||||||
|
using PrivaPub.Models.Media;
|
||||||
|
using PrivaPub.Tests.Support;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Domain
|
||||||
|
{
|
||||||
|
[Trait("Category", "Integration")]
|
||||||
|
public sealed class MediaFlowTests : 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 byte[] Png(int width, int height)
|
||||||
|
{
|
||||||
|
using var image = (Image.Black(width, height, bands: 3) + new double[] { 10, 120, 200 }).Cast(Enums.BandFormat.Uchar);
|
||||||
|
return image.WriteToBuffer(".png");
|
||||||
|
}
|
||||||
|
|
||||||
|
static IFormFile Upload(byte[] bytes, string contentType) =>
|
||||||
|
new FormFile(new MemoryStream(bytes), 0, bytes.Length, "file", "picture") { Headers = new HeaderDictionary(), ContentType = contentType };
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task An_upload_is_attached_once_and_federated_with_its_alt_text()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, alice) = await _harness.Persona("alice");
|
||||||
|
var (_, mallory) = await _harness.Persona("mallory");
|
||||||
|
|
||||||
|
var upload = await _harness.Media.Upload(alice, Upload(Png(300, 200), "image/png"), "a blue square", "0.25,-0.5", token);
|
||||||
|
Assert.True(upload.Ok);
|
||||||
|
Assert.True(File.Exists(Path.Combine(_harness.Media.Root, upload.Attachment.FilePath)));
|
||||||
|
|
||||||
|
var stolen = await _harness.Statuses.Publish(mallory, new StatusDraft { Text = "mine", MediaIds = new[] { upload.Attachment.ID } }, token);
|
||||||
|
Assert.False(stolen.Ok);
|
||||||
|
|
||||||
|
var posted = await _harness.Statuses.Publish(alice, new StatusDraft { Text = "look", MediaIds = new[] { upload.Attachment.ID } }, token);
|
||||||
|
Assert.True(posted.Ok);
|
||||||
|
Assert.Equal(posted.Post.ID, (await DB.Default.Find<MediaAttachment>().OneAsync(upload.Attachment.ID, token)).PostId);
|
||||||
|
|
||||||
|
var note = ActivityPubRenderer.Note(posted.Post, alice, default, default);
|
||||||
|
var attachment = note["attachment"]![0]!;
|
||||||
|
Assert.Equal("a blue square", attachment["name"]!.GetValue<string>());
|
||||||
|
Assert.Equal(300, attachment["width"]!.GetValue<int>());
|
||||||
|
Assert.Equal(-0.5f, attachment["focalPoint"]![1]!.GetValue<float>());
|
||||||
|
Assert.StartsWith("https://privapub.test/media/files/", attachment["url"]!.GetValue<string>());
|
||||||
|
|
||||||
|
var reused = await _harness.Statuses.Publish(alice, new StatusDraft { Text = "again", MediaIds = new[] { upload.Attachment.ID } }, token);
|
||||||
|
Assert.False(reused.Ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Unsupported_files_are_refused()
|
||||||
|
{
|
||||||
|
var (_, alice) = await _harness.Persona("alice");
|
||||||
|
|
||||||
|
var outcome = await _harness.Media.Upload(alice, Upload(System.Text.Encoding.UTF8.GetBytes("<svg/>"), "image/svg+xml"), default, default, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
Assert.False(outcome.Ok);
|
||||||
|
Assert.Equal(422, outcome.Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task The_proxy_serves_signed_remote_media_and_nothing_else()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var path = $"/files/{Guid.NewGuid():N}.png";
|
||||||
|
_harness.Peer.ServeFile(path, Png(10, 10), "image/png");
|
||||||
|
var proxy = new MediaProxy(_harness.Local, Peer.Http(), _harness.Media, new StaticOptions<MediaOptions>(new MediaOptions()));
|
||||||
|
|
||||||
|
var wrapped = proxy.Wrap(_harness.Peer.A + path);
|
||||||
|
var parts = new Uri(wrapped).AbsolutePath.Split('/');
|
||||||
|
var (file, contentType) = await proxy.Fetch(parts[3], parts[4], token);
|
||||||
|
var (tampered, _) = await proxy.Fetch(parts[3].Replace(parts[3][0], parts[3][0] == 'A' ? 'B' : 'A'), parts[4], token);
|
||||||
|
|
||||||
|
Assert.StartsWith("https://privapub.test/media/proxy/", wrapped);
|
||||||
|
Assert.Equal("image/png", contentType);
|
||||||
|
Assert.True(File.Exists(file));
|
||||||
|
Assert.StartsWith(_harness.Media.ProxyRoot, file);
|
||||||
|
Assert.Null(tampered);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
using NetVips;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Media;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Domain
|
||||||
|
{
|
||||||
|
public class MediaProcessingTests
|
||||||
|
{
|
||||||
|
static byte[] JpegWithMetadata(int width, int height)
|
||||||
|
{
|
||||||
|
using var image = (Image.Black(width, height, bands: 3) + new double[] { 200, 40, 90 }).Cast(Enums.BandFormat.Uchar);
|
||||||
|
using var tagged = image.Mutate(m =>
|
||||||
|
{
|
||||||
|
m.Set(GValue.GStrType, "exif-ifd0-ImageDescription", "where I live");
|
||||||
|
m.Set(GValue.GStrType, "exif-ifd0-Artist", "Alice Smith");
|
||||||
|
m.Set(GValue.GStrType, "exif-ifd2-UserComment", "at home");
|
||||||
|
m.Set(GValue.BlobType, "xmp-data", System.Text.Encoding.UTF8.GetBytes("<x:xmpmeta xmlns:x='adobe:ns:meta/'><secret/></x:xmpmeta>"));
|
||||||
|
});
|
||||||
|
return tagged.WriteToBuffer(".jpg");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Uploaded_images_lose_every_kind_of_metadata()
|
||||||
|
{
|
||||||
|
var input = JpegWithMetadata(800, 600);
|
||||||
|
using (var original = Image.NewFromBuffer(input))
|
||||||
|
Assert.Contains("exif-data", original.GetFields());
|
||||||
|
|
||||||
|
var processed = MediaService.ProcessImage(input, 4096, 640, animated: false);
|
||||||
|
|
||||||
|
using var output = Image.NewFromBuffer(processed.Bytes);
|
||||||
|
var fields = output.GetFields();
|
||||||
|
Assert.DoesNotContain("exif-data", fields);
|
||||||
|
Assert.DoesNotContain("xmp-data", fields);
|
||||||
|
Assert.DoesNotContain("iptc-data", fields);
|
||||||
|
Assert.DoesNotContain(fields, f => f.StartsWith("exif-ifd"));
|
||||||
|
Assert.Equal((800, 600), (processed.Width, processed.Height));
|
||||||
|
Assert.Equal("image/jpeg", processed.ContentType);
|
||||||
|
using var preview = Image.NewFromBuffer(processed.Preview);
|
||||||
|
Assert.Equal(640, Math.Max(preview.Width, preview.Height));
|
||||||
|
Assert.DoesNotContain("exif-data", preview.GetFields());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Large_images_are_capped()
|
||||||
|
{
|
||||||
|
var processed = MediaService.ProcessImage(JpegWithMetadata(5000, 2500), 4096, 640, animated: false);
|
||||||
|
|
||||||
|
Assert.Equal((4096, 2048), (processed.Width, processed.Height));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_blurhash_is_well_formed()
|
||||||
|
{
|
||||||
|
var processed = MediaService.ProcessImage(JpegWithMetadata(64, 64), 4096, 640, animated: false);
|
||||||
|
|
||||||
|
Assert.Equal(28, processed.Blurhash.Length);
|
||||||
|
Assert.Equal('L', processed.Blurhash[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_white_image_encodes_white_as_its_average()
|
||||||
|
{
|
||||||
|
var pixels = Enumerable.Range(0, 16 * 16).SelectMany(_ => new byte[] { 255, 255, 255 }).ToArray();
|
||||||
|
|
||||||
|
var hash = Blurhash.Encode(pixels, 16, 16);
|
||||||
|
|
||||||
|
Assert.Equal(28, hash.Length);
|
||||||
|
Assert.Equal("TSUA", hash[2..6]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels.Post;
|
||||||
|
using PrivaPub.Domain.Privacy;
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Tests.Support;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Domain
|
||||||
|
{
|
||||||
|
[Trait("Category", "Integration")]
|
||||||
|
public sealed class NearbyTests : 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_located_post_reaches_only_those_within_its_radius_and_never_leaves_the_server()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var latitude = 10 + Random.Shared.NextDouble() * 50;
|
||||||
|
var longitude = 10 + Random.Shared.NextDouble() * 50;
|
||||||
|
var (aliceRoot, alice) = await _harness.Persona("alice");
|
||||||
|
var (bobRoot, bob) = await _harness.Persona("bob");
|
||||||
|
var (carolRoot, carol) = await _harness.Persona("carol");
|
||||||
|
var follower = new RemoteActor(_harness.Peer, "follower");
|
||||||
|
await _harness.FollowedBy(alice, follower);
|
||||||
|
|
||||||
|
var created = await _harness.Posts.InsertPost(aliceRoot, new InsertPostForm
|
||||||
|
{
|
||||||
|
AvatarId = alice.Id, Text = "free bread at the bakery", Latitude = latitude, Longitude = longitude, RangeKm = 5
|
||||||
|
}, token);
|
||||||
|
var post = await DB.Default.Find<Post>().OneAsync(((ViewPost)created.Data).Id, token);
|
||||||
|
|
||||||
|
var three = (List<ViewPost>)(await _harness.Posts.Nearby(bobRoot, bob.Id, latitude + 0.027, longitude, token)).Data;
|
||||||
|
var eight = (List<ViewPost>)(await _harness.Posts.Nearby(carolRoot, carol.Id, latitude + 0.072, longitude, token)).Data;
|
||||||
|
|
||||||
|
Assert.Equal(PostVisibility.LocalGeo, post.Visibility);
|
||||||
|
Assert.Equal(Math.Round(latitude, 2), post.Geo.Coordinates[1]);
|
||||||
|
Assert.Contains(three, p => p.Id == post.ID);
|
||||||
|
Assert.InRange(three.First(p => p.Id == post.ID).DistanceKm.Value, 2.0, 4.0);
|
||||||
|
Assert.DoesNotContain(eight, p => p.Id == post.ID);
|
||||||
|
Assert.False(await VisibilityPolicy.CanSee(post, bob.Id, token));
|
||||||
|
var outgoing = (await DB.Default.Find<Job>().Match(j => j.Kind == JobKind.Deliver).ExecuteAsync(token))
|
||||||
|
.Select(j => JsonSerializer.Deserialize<DeliveryPayload>(j.Payload))
|
||||||
|
.Where(p => p.Body.Contains(post.ID));
|
||||||
|
Assert.Empty(outgoing);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_located_post_cannot_be_direct_or_in_a_group()
|
||||||
|
{
|
||||||
|
var (root, alice) = await _harness.Persona("alice");
|
||||||
|
|
||||||
|
var result = await _harness.Statuses.Publish(alice, new PrivaPub.Domain.Statuses.StatusDraft
|
||||||
|
{
|
||||||
|
Text = "x", Latitude = 45, Longitude = 7, Visibility = PostVisibility.Direct
|
||||||
|
}, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
Assert.False(result.Ok);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels.Post;
|
||||||
|
using PrivaPub.ClientModels;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.Tests.Support;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Domain
|
||||||
|
{
|
||||||
|
[Trait("Category", "Integration")]
|
||||||
|
public sealed class OutboxTests : 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<ForeignAvatar> Known(RemoteActor actor) =>
|
||||||
|
await _harness.Remote.GetActor(actor.Id, refresh: false, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task The_audience_follows_mastodons_table()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, alice) = await _harness.Persona("alice");
|
||||||
|
var follower = new RemoteActor(_harness.Peer, "follower");
|
||||||
|
var mentioned = await Known(new RemoteActor(_harness.Peer, "mentioned"));
|
||||||
|
var parentAuthor = await Known(new RemoteActor(_harness.Peer, "parent"));
|
||||||
|
await _harness.FollowedBy(alice, follower);
|
||||||
|
var mention = new PostMention { ActorURI = mentioned.ActorURI };
|
||||||
|
|
||||||
|
async Task<IReadOnlyList<string>> Audience(PostVisibility visibility, string replyTo = default) =>
|
||||||
|
await _harness.Outbox.Audience(alice, new Post { Visibility = visibility, Mentions = new() { mention }, To = new() { mentioned.ActorURI }, InReplyToAccountId = replyTo }, token);
|
||||||
|
|
||||||
|
Assert.Equal(new[] { follower.SharedInbox, mentioned.InboxURL }, await Audience(PostVisibility.Public));
|
||||||
|
Assert.Equal(new[] { follower.SharedInbox, mentioned.InboxURL, parentAuthor.InboxURL }, await Audience(PostVisibility.Unlisted, parentAuthor.ID));
|
||||||
|
Assert.Equal(new[] { follower.SharedInbox, mentioned.InboxURL }, await Audience(PostVisibility.FollowersOnly));
|
||||||
|
Assert.Equal(new[] { mentioned.InboxURL }, await Audience(PostVisibility.Direct, parentAuthor.ID));
|
||||||
|
Assert.Empty(await Audience(PostVisibility.Circle));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_post_is_created_edited_and_deleted_for_its_audience()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (root, alice) = await _harness.Persona("alice");
|
||||||
|
var follower = new RemoteActor(_harness.Peer, "follower");
|
||||||
|
await _harness.FollowedBy(alice, follower);
|
||||||
|
|
||||||
|
var created = await _harness.Posts.InsertPost(root, new InsertPostForm { AvatarId = alice.Id, Text = "first", Visibility = "followersonly" }, token);
|
||||||
|
var postId = ((ViewPost)created.Data).Id;
|
||||||
|
await _harness.Posts.UpdatePost(root, new UpdatePostForm { AvatarId = alice.Id, PostId = postId, Text = "second", SpoilerText = "cw" }, token);
|
||||||
|
await _harness.Posts.DeletePost(root, new DeletePostForm { AvatarId = alice.Id, PostId = postId }, token);
|
||||||
|
|
||||||
|
var sent = await _harness.Outgoing(follower.SharedInbox);
|
||||||
|
Assert.Equal(new[] { "Create", "Update", "Delete" }, sent.Select(a => a["type"]!.GetValue<string>()));
|
||||||
|
Assert.DoesNotContain(sent[0]["to"]!.AsArray(), n => n!.GetValue<string>().EndsWith("#Public"));
|
||||||
|
Assert.Equal("cw", sent[1]["object"]!["summary"]!.GetValue<string>());
|
||||||
|
Assert.NotNull(sent[1]["object"]!["updated"]);
|
||||||
|
Assert.Equal(sent[0]["to"]!.ToJsonString(), sent[2]["to"]!.ToJsonString());
|
||||||
|
|
||||||
|
var stored = await DB.Default.Find<Post>().OneAsync(postId, token);
|
||||||
|
Assert.NotNull(stored.DeletedAt);
|
||||||
|
Assert.Null(stored.ContentHtml);
|
||||||
|
Assert.Empty(stored.Revisions);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Someone_elses_post_cannot_be_edited()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (root, alice) = await _harness.Persona("alice");
|
||||||
|
var (otherRoot, bob) = await _harness.Persona("bob");
|
||||||
|
var created = await _harness.Posts.InsertPost(root, new InsertPostForm { AvatarId = alice.Id, Text = "mine" }, token);
|
||||||
|
|
||||||
|
var result = await _harness.Posts.UpdatePost(otherRoot, new UpdatePostForm { AvatarId = bob.Id, PostId = ((ViewPost)created.Data).Id, Text = "yours" }, token);
|
||||||
|
|
||||||
|
Assert.False(result.IsValid);
|
||||||
|
Assert.Equal(404, result.StatusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels.Post;
|
||||||
|
using PrivaPub.ClientModels.Social;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Tests.Support;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Domain
|
||||||
|
{
|
||||||
|
[Trait("Category", "Integration")]
|
||||||
|
public sealed class RelationshipTests : 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);
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_block_cuts_follows_both_ways_and_hides_the_account()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (aliceRoot, alice) = await _harness.Persona("alice");
|
||||||
|
var (bobRoot, bob) = await _harness.Persona("bob");
|
||||||
|
await _harness.Follows.Follow(aliceRoot, new FollowForm { AvatarId = alice.Id, Target = bob.UserName }, token);
|
||||||
|
await _harness.Follows.Follow(bobRoot, new FollowForm { AvatarId = bob.Id, Target = alice.UserName }, token);
|
||||||
|
|
||||||
|
await _harness.Relationships.Block(alice, bob.Uri, bob.Id, token);
|
||||||
|
await _harness.Posts.InsertPost(bobRoot, new InsertPostForm { AvatarId = bob.Id, Text = $"hey @{alice.UserName}" }, token);
|
||||||
|
|
||||||
|
Assert.False(await DB.Default.Find<Following>().Match(f => f.AvatarId == alice.Id || f.AvatarId == bob.Id).ExecuteAnyAsync(token));
|
||||||
|
Assert.False(await DB.Default.Find<Follower>().Match(f => f.LocalActorId == alice.Id || f.LocalActorId == bob.Id).ExecuteAnyAsync(token));
|
||||||
|
Assert.False(await DB.Default.Find<TimelineEntry>().Match(e => e.AvatarId == alice.Id && e.AuthorAccountId == bob.Id).ExecuteAnyAsync(token));
|
||||||
|
Assert.False(await DB.Default.Find<Notification>().Match(n => n.AvatarId == alice.Id).ExecuteAnyAsync(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_blocked_remote_account_is_refused_when_it_follows()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, alice) = await _harness.Persona("alice");
|
||||||
|
var mallory = new RemoteActor(_harness.Peer, "mallory");
|
||||||
|
await _harness.Relationships.Block(alice, mallory.Id, default, token);
|
||||||
|
|
||||||
|
await _harness.Deliver(mallory, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{Origin(mallory)}/follows/{Guid.NewGuid():N}", ["type"] = "Follow", ["actor"] = mallory.Id, ["object"] = alice.Uri
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.False(await DB.Default.Find<Follower>().Match(f => f.LocalActorId == alice.Id).ExecuteAnyAsync(token));
|
||||||
|
var sent = (await _harness.Outgoing(mallory.Id + "/inbox")).Select(a => a["type"]!.GetValue<string>()).ToList();
|
||||||
|
Assert.Equal(new[] { "Block", "Reject" }, sent.Order());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_block_is_told_to_the_blocked_server_and_so_is_the_unblock()
|
||||||
|
{
|
||||||
|
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"] = $"{Origin(mallory)}/follows/{Guid.NewGuid():N}", ["type"] = "Follow", ["actor"] = mallory.Id, ["object"] = alice.Uri
|
||||||
|
});
|
||||||
|
|
||||||
|
await _harness.Relationships.Block(alice, mallory.Id, default, token);
|
||||||
|
await _harness.Relationships.Unblock(alice, mallory.Id, token);
|
||||||
|
|
||||||
|
var sent = await _harness.Outgoing(mallory.Id + "/inbox");
|
||||||
|
var block = Assert.Single(sent, a => a["type"]!.GetValue<string>() == "Block");
|
||||||
|
Assert.Equal(mallory.Id, block["object"]!.GetValue<string>());
|
||||||
|
Assert.Equal(alice.Uri, block["actor"]!.GetValue<string>());
|
||||||
|
var undo = Assert.Single(sent, a => a["type"]!.GetValue<string>() == "Undo");
|
||||||
|
Assert.Equal(block["id"]!.GetValue<string>(), undo["object"]!["id"]!.GetValue<string>());
|
||||||
|
Assert.False(await DB.Default.Find<Block>().Match(b => b.AvatarId == alice.Id).ExecuteAnyAsync(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_muted_account_stays_out_of_home_and_notifications()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (aliceRoot, alice) = await _harness.Persona("alice");
|
||||||
|
var (bobRoot, bob) = await _harness.Persona("bob");
|
||||||
|
await _harness.Follows.Follow(aliceRoot, new FollowForm { AvatarId = alice.Id, Target = bob.UserName }, token);
|
||||||
|
await _harness.Relationships.Mute(alice, bob.Uri, bob.Id, hideNotifications: true, default, token);
|
||||||
|
|
||||||
|
await _harness.Posts.InsertPost(bobRoot, new InsertPostForm { AvatarId = bob.Id, Text = $"hey @{alice.UserName}" }, token);
|
||||||
|
|
||||||
|
Assert.False(await DB.Default.Find<TimelineEntry>().Match(e => e.AvatarId == alice.Id).ExecuteAnyAsync(token));
|
||||||
|
Assert.False(await DB.Default.Find<Notification>().Match(n => n.AvatarId == alice.Id && n.Type == NotificationType.Mention).ExecuteAnyAsync(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_report_is_forwarded_by_the_instance_not_the_persona()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, alice) = await _harness.Persona("alice");
|
||||||
|
var mallory = new RemoteActor(_harness.Peer, "mallory");
|
||||||
|
var foreign = await _harness.Remote.GetActor(mallory.Id, refresh: false, token);
|
||||||
|
|
||||||
|
var report = await _harness.Reports.File(alice, foreign.ID, Array.Empty<string>(), "spam everywhere", "spam", forward: true, token);
|
||||||
|
|
||||||
|
Assert.True(report.Forwarded);
|
||||||
|
var flag = Assert.Single(await _harness.Outgoing(mallory.Id + "/inbox"));
|
||||||
|
Assert.Equal("Flag", flag["type"]!.GetValue<string>());
|
||||||
|
Assert.EndsWith("/peasants/privapub", flag["actor"]!.GetValue<string>());
|
||||||
|
Assert.DoesNotContain(alice.UserName, flag.ToJsonString());
|
||||||
|
Assert.DoesNotContain(alice.Id, flag.ToJsonString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task An_inbound_flag_becomes_a_report_for_moderators()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (root, alice) = await _harness.Persona("alice");
|
||||||
|
var reporter = new RemoteActor(_harness.Peer, "reporter");
|
||||||
|
await _harness.Posts.InsertPost(root, new InsertPostForm { AvatarId = alice.Id, Text = "bad post" }, token);
|
||||||
|
var post = await DB.Default.Find<PrivaPub.Models.Post.Post>().Match(p => p.GroupUserId == alice.Id).ExecuteSingleAsync(token);
|
||||||
|
|
||||||
|
await _harness.Deliver(reporter, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{Origin(reporter)}/flags/{Guid.NewGuid():N}", ["type"] = "Flag", ["actor"] = reporter.Id,
|
||||||
|
["content"] = "please look", ["object"] = new JsonArray(alice.Uri, post.ObjectURI)
|
||||||
|
});
|
||||||
|
|
||||||
|
var report = await DB.Default.Find<Report>().Match(r => r.TargetAccountId == alice.Id).ExecuteSingleAsync(token);
|
||||||
|
Assert.Equal(reporter.Id, report.ReporterActorURI);
|
||||||
|
Assert.Equal(new[] { post.ID }, report.PostIds);
|
||||||
|
Assert.Equal("please look", report.Comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,162 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels.Post;
|
||||||
|
using PrivaPub.ClientModels.Social;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Tests.Support;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Domain
|
||||||
|
{
|
||||||
|
[Trait("Category", "Integration")]
|
||||||
|
public sealed class TimelineTests : 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<List<ViewPost>> Home(string root, string avatarId) =>
|
||||||
|
(List<ViewPost>)(await _harness.Timelines.Home(root, avatarId, default, 40, TestContext.Current.CancellationToken)).Data;
|
||||||
|
|
||||||
|
async Task Accepted(string root, string avatarId, RemoteActor target)
|
||||||
|
{
|
||||||
|
await _harness.Follows.Follow(root, new FollowForm { AvatarId = avatarId, Target = target.Id }, TestContext.Current.CancellationToken);
|
||||||
|
await DB.Default.Update<Following>().Match(f => f.AvatarId == avatarId && f.TargetActorURI == target.Id)
|
||||||
|
.Modify(f => f.State, FollowState.Accepted).ExecuteAsync(TestContext.Current.CancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
static JsonObject Create(RemoteActor author, string inReplyTo = default, string text = "hello", params (string Uri, string Name)[] mentions)
|
||||||
|
{
|
||||||
|
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>{text}</p>",
|
||||||
|
["to"] = new JsonArray(Addressing.Public),
|
||||||
|
["cc"] = new JsonArray(mentions.Select(m => (JsonNode)m.Uri).Prepend(author.Id + "/followers").ToArray()),
|
||||||
|
["tag"] = new JsonArray(mentions.Select(m => (JsonNode)new JsonObject { ["type"] = "Mention", ["href"] = m.Uri, ["name"] = m.Name }).ToArray())
|
||||||
|
};
|
||||||
|
if (inReplyTo != default)
|
||||||
|
note["inReplyTo"] = inReplyTo;
|
||||||
|
return new JsonObject { ["id"] = $"{origin}/activities/{Guid.NewGuid():N}", ["type"] = "Create", ["actor"] = author.Id, ["object"] = note };
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_local_post_reaches_the_author_and_local_followers_only()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (aliceRoot, alice) = await _harness.Persona("alice");
|
||||||
|
var (bobRoot, bob) = await _harness.Persona("bob");
|
||||||
|
var (carolRoot, carol) = await _harness.Persona("carol");
|
||||||
|
await _harness.Follows.Follow(bobRoot, new FollowForm { AvatarId = bob.Id, Target = alice.UserName }, token);
|
||||||
|
|
||||||
|
await _harness.Posts.InsertPost(aliceRoot, new InsertPostForm { AvatarId = alice.Id, Text = "for my followers" }, token);
|
||||||
|
|
||||||
|
Assert.Single(await Home(aliceRoot, alice.Id));
|
||||||
|
Assert.Equal("<p>for my followers</p>", Assert.Single(await Home(bobRoot, bob.Id)).ContentHtml);
|
||||||
|
Assert.Empty(await Home(carolRoot, carol.Id));
|
||||||
|
Assert.Equal(404, (await _harness.Timelines.Home(carolRoot, bob.Id, default, 20, token)).StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_followed_remote_account_is_kept_and_its_replies_follow_mastodons_rule()
|
||||||
|
{
|
||||||
|
var (bobRoot, bob) = await _harness.Persona("bob");
|
||||||
|
var carol = new RemoteActor(_harness.Peer, "carol");
|
||||||
|
var dave = new RemoteActor(_harness.Peer, "dave");
|
||||||
|
await Accepted(bobRoot, bob.Id, carol);
|
||||||
|
|
||||||
|
var top = Create(carol, text: "top");
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", top);
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", Create(carol, top["object"]!["id"]!.GetValue<string>(), "self reply"));
|
||||||
|
await _harness.Deliver(dave, "/human-centipede", Create(dave, text: "dave alone"));
|
||||||
|
var daveRoot = Create(dave, text: "unfollowed root");
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", Create(carol, $"{new Uri(dave.Id).GetLeftPart(UriPartial.Authority)}/notes/unknown", "reply to a stranger"));
|
||||||
|
|
||||||
|
var home = (await Home(bobRoot, bob.Id)).Select(p => p.ContentHtml).ToList();
|
||||||
|
Assert.Contains("<p>top</p>", home);
|
||||||
|
Assert.Contains("<p>self reply</p>", home);
|
||||||
|
Assert.DoesNotContain("<p>dave alone</p>", home);
|
||||||
|
Assert.DoesNotContain("<p>reply to a stranger</p>", home);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_reply_brings_its_public_parent_and_the_grandparent_is_queued()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (bobRoot, bob) = await _harness.Persona("bob");
|
||||||
|
var carol = new RemoteActor(_harness.Peer, "carol");
|
||||||
|
var dave = new RemoteActor(_harness.Peer, "dave");
|
||||||
|
await Accepted(bobRoot, bob.Id, carol);
|
||||||
|
var origin = new Uri(dave.Id).GetLeftPart(UriPartial.Authority);
|
||||||
|
var grandparentId = $"{origin}/notes/{Guid.NewGuid():N}";
|
||||||
|
var parentId = $"{origin}/notes/{Guid.NewGuid():N}";
|
||||||
|
_harness.Peer.Serve(new Uri(parentId).AbsolutePath, new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = parentId, ["type"] = "Note", ["attributedTo"] = dave.Id, ["content"] = "<p>parent</p>",
|
||||||
|
["to"] = new JsonArray(Addressing.Public), ["inReplyTo"] = grandparentId
|
||||||
|
}.ToJsonString());
|
||||||
|
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", Create(carol, parentId, "reply to dave"));
|
||||||
|
|
||||||
|
var parent = await DB.Default.Find<PrivaPub.Models.Post.Post>().Match(p => p.ObjectURI == parentId).ExecuteSingleAsync(token);
|
||||||
|
var reply = await DB.Default.Find<PrivaPub.Models.Post.Post>().Match(p => p.InReplyToURI == parentId).ExecuteSingleAsync(token);
|
||||||
|
Assert.Equal(parent.ID, reply.AnsweringToPostId);
|
||||||
|
Assert.Equal(parent.AuthorAccountId, reply.InReplyToAccountId);
|
||||||
|
Assert.True(await DB.Default.Find<PrivaPub.Models.Jobs.Job>().Match(j => j.DedupeKey == "ancestors|" + parent.ID).ExecuteAnyAsync(token));
|
||||||
|
Assert.DoesNotContain("<p>reply to dave</p>", (await Home(bobRoot, bob.Id)).Select(p => p.ContentHtml));
|
||||||
|
Assert.DoesNotContain("<p>parent</p>", (await Home(bobRoot, bob.Id)).Select(p => p.ContentHtml));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_remote_delete_takes_the_post_out_of_home()
|
||||||
|
{
|
||||||
|
var (bobRoot, bob) = await _harness.Persona("bob");
|
||||||
|
var carol = new RemoteActor(_harness.Peer, "carol");
|
||||||
|
await Accepted(bobRoot, bob.Id, carol);
|
||||||
|
var create = Create(carol, text: "soon gone");
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", create);
|
||||||
|
Assert.Single(await Home(bobRoot, bob.Id));
|
||||||
|
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{new Uri(carol.Id).GetLeftPart(UriPartial.Authority)}/deletes/{Guid.NewGuid():N}", ["type"] = "Delete", ["actor"] = carol.Id,
|
||||||
|
["object"] = new JsonObject { ["id"] = create["object"]!["id"]!.GetValue<string>(), ["type"] = "Tombstone" }
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.Empty(await Home(bobRoot, bob.Id));
|
||||||
|
Assert.False(await DB.Default.Find<TimelineEntry>().Match(e => e.AvatarId == bob.Id).ExecuteAnyAsync(TestContext.Current.CancellationToken));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_mention_notifies_the_persona()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (bobRoot, bob) = await _harness.Persona("bob");
|
||||||
|
var carol = new RemoteActor(_harness.Peer, "carol");
|
||||||
|
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", Create(carol, mentions: (bob.Uri, "@" + bob.Handle)));
|
||||||
|
|
||||||
|
var notifications = (List<ViewNotification>)(await _harness.Timelines.Notifications(bobRoot, bob.Id, default, 20, token)).Data;
|
||||||
|
var mention = Assert.Single(notifications);
|
||||||
|
Assert.Equal("Mention", mention.Type);
|
||||||
|
Assert.Equal(carol.Id, mention.FromActorURI);
|
||||||
|
await _harness.Timelines.MarkNotificationsRead(bobRoot, bob.Id, token);
|
||||||
|
Assert.True(((List<ViewNotification>)(await _harness.Timelines.Notifications(bobRoot, bob.Id, default, 20, token)).Data).Single().IsRead);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,198 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels.Social;
|
||||||
|
using PrivaPub.Domain.Statuses;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Federation.Rendering;
|
||||||
|
using PrivaPub.Federation.Signing;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Group;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Tests.Support;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using GroupEntity = PrivaPub.Models.Group.Group;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Federation
|
||||||
|
{
|
||||||
|
[Trait("Category", "Integration")]
|
||||||
|
public sealed class GroupTests : 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<(GroupEntity Entity, PrivaPub.Federation.Actors.LocalActor Actor)> Group(GroupKind kind, PostingPolicy policy, string ownerId, params string[] remoteMembers)
|
||||||
|
{
|
||||||
|
var (privateKey, publicKey) = PrivaPub.Federation.Actors.Keys.NewKeyPair();
|
||||||
|
var group = new GroupEntity
|
||||||
|
{
|
||||||
|
UserName = $"{kind}{Guid.NewGuid():N}"[..20].ToLowerInvariant(),
|
||||||
|
Kind = kind,
|
||||||
|
PostingPolicy = policy,
|
||||||
|
PrivateKey = privateKey,
|
||||||
|
PublicKey = publicKey,
|
||||||
|
Members = new() { new GroupMember { AvatarId = ownerId, Role = GroupRole.Owner } }
|
||||||
|
};
|
||||||
|
group.Members.AddRange(remoteMembers.Select(m => new GroupMember { AvatarId = m, IsForeign = true }));
|
||||||
|
await DB.Default.SaveAsync(group);
|
||||||
|
return (group, _harness.Local.FromGroup(group));
|
||||||
|
}
|
||||||
|
|
||||||
|
static string Origin(RemoteActor actor) => new Uri(actor.Id).GetLeftPart(UriPartial.Authority);
|
||||||
|
|
||||||
|
static JsonObject Create(RemoteActor author, IEnumerable<string> to, string audience = default)
|
||||||
|
{
|
||||||
|
var note = new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{Origin(author)}/notes/{Guid.NewGuid():N}",
|
||||||
|
["type"] = "Note",
|
||||||
|
["attributedTo"] = author.Id,
|
||||||
|
["content"] = "<p>to the group</p>",
|
||||||
|
["to"] = new JsonArray(to.Select(t => (JsonNode)t).ToArray())
|
||||||
|
};
|
||||||
|
if (audience != default)
|
||||||
|
note["audience"] = audience;
|
||||||
|
return new JsonObject { ["id"] = $"{Origin(author)}/activities/{Guid.NewGuid():N}", ["type"] = "Create", ["actor"] = author.Id, ["object"] = note };
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_follower_posting_to_a_community_is_announced_activity_and_object()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, owner) = await _harness.Persona("owner");
|
||||||
|
var (_, community) = await Group(GroupKind.Community, PostingPolicy.Followers, owner.Id);
|
||||||
|
var lemmy = new RemoteActor(_harness.Peer, "lemmy");
|
||||||
|
var stranger = new RemoteActor(_harness.Peer, "stranger");
|
||||||
|
await _harness.FollowedBy(community, lemmy);
|
||||||
|
|
||||||
|
await _harness.Deliver(lemmy, "/human-centipede", Create(lemmy, new[] { community.Uri, Addressing.Public }, community.Uri));
|
||||||
|
await _harness.Deliver(stranger, "/human-centipede", Create(stranger, new[] { community.Uri, Addressing.Public }, community.Uri));
|
||||||
|
|
||||||
|
Assert.True(await DB.Default.Find<Post>().Match(p => p.GroupId == community.Id && p.ActorURI == lemmy.Id).ExecuteAnyAsync(token));
|
||||||
|
Assert.False(await DB.Default.Find<Post>().Match(p => p.GroupId == community.Id && p.ActorURI == stranger.Id).ExecuteAnyAsync(token));
|
||||||
|
var announces = (await _harness.Outgoing(lemmy.SharedInbox)).Where(a => a["actor"]!.GetValue<string>() == community.Uri).ToList();
|
||||||
|
Assert.Equal(2, announces.Count);
|
||||||
|
Assert.Contains(announces, a => a["object"] is JsonObject inner && inner["type"]!.GetValue<string>() == "Create" && a["audience"]!.GetValue<string>() == community.Uri);
|
||||||
|
Assert.Contains(announces, a => a["object"] is JsonValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task An_open_community_takes_posts_from_anyone()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, owner) = await _harness.Persona("owner");
|
||||||
|
var (_, community) = await Group(GroupKind.Community, PostingPolicy.Anyone, owner.Id);
|
||||||
|
var stranger = new RemoteActor(_harness.Peer, "stranger");
|
||||||
|
|
||||||
|
await _harness.Deliver(stranger, "/human-centipede", Create(stranger, new[] { community.Uri, Addressing.Public }));
|
||||||
|
|
||||||
|
Assert.True(await DB.Default.Find<Post>().Match(p => p.GroupId == community.Id && p.ActorURI == stranger.Id).ExecuteAnyAsync(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Mentioning_a_community_posts_into_it_as_a_titled_page()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, alice) = await _harness.Persona("alice");
|
||||||
|
var (_, community) = await Group(GroupKind.Community, PostingPolicy.Anyone, alice.Id);
|
||||||
|
var follower = new RemoteActor(_harness.Peer, "follower");
|
||||||
|
await _harness.FollowedBy(community, follower);
|
||||||
|
|
||||||
|
var outcome = await _harness.Statuses.Publish(alice, new StatusDraft { Text = $"@{community.UserName} a new thread", PlainText = true }, token);
|
||||||
|
|
||||||
|
Assert.Equal(community.Id, outcome.Post.GroupId);
|
||||||
|
var note = ActivityPubRenderer.Note(outcome.Post, alice, community, default);
|
||||||
|
Assert.Equal("Page", note["type"]!.GetValue<string>());
|
||||||
|
Assert.Equal(community.Uri, note["audience"]!.GetValue<string>());
|
||||||
|
Assert.False(string.IsNullOrEmpty(note["name"]!.GetValue<string>()));
|
||||||
|
Assert.Contains(await _harness.Outgoing(follower.SharedInbox), a => a["type"]!.GetValue<string>() == "Announce");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_circle_post_goes_only_to_members_and_only_members_may_read_it()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, alice) = await _harness.Persona("alice");
|
||||||
|
var member = new RemoteActor(_harness.Peer, "member");
|
||||||
|
var outsider = new RemoteActor(_harness.Peer, "outsider");
|
||||||
|
var (circleEntity, circle) = await Group(GroupKind.Circle, PostingPolicy.Followers, alice.Id, member.Id);
|
||||||
|
await _harness.Remote.GetActor(member.Id, refresh: false, token);
|
||||||
|
var follower = new RemoteActor(_harness.Peer, "follower");
|
||||||
|
await _harness.FollowedBy(alice, follower);
|
||||||
|
|
||||||
|
var outcome = await _harness.Statuses.Publish(alice, new StatusDraft { Text = "just us", GroupId = circle.Id }, token);
|
||||||
|
|
||||||
|
Assert.Equal(PostVisibility.Circle, outcome.Post.Visibility);
|
||||||
|
var create = Assert.Single(await _harness.Outgoing(member.Id + "/inbox"));
|
||||||
|
Assert.Equal(new[] { circle.Uri, circle.Flock }, create["object"]!["to"]!.AsArray().Select(t => t!.GetValue<string>()));
|
||||||
|
Assert.DoesNotContain(Addressing.Public, create.ToJsonString());
|
||||||
|
Assert.Empty(await _harness.Outgoing(follower.SharedInbox));
|
||||||
|
Assert.Empty((await _harness.Outgoing(member.SharedInbox)).Where(a => a["type"]!.GetValue<string>() == "Announce"));
|
||||||
|
|
||||||
|
var authorizer = new SignedFetchAuthorizer(_harness.Remote);
|
||||||
|
var path = new Uri(outcome.Post.ObjectURI).AbsolutePath;
|
||||||
|
var asMember = await authorizer.Requester(member.Get(Harness.Host, path), token);
|
||||||
|
var asOutsider = await authorizer.Requester(outsider.Get(Harness.Host, path), token);
|
||||||
|
Assert.True(SignedFetchAuthorizer.MayReadCircle(circleEntity, asMember));
|
||||||
|
Assert.False(SignedFetchAuthorizer.MayReadCircle(circleEntity, asOutsider));
|
||||||
|
Assert.False(SignedFetchAuthorizer.MayReadCircle(circleEntity, default));
|
||||||
|
Assert.True(circle.IsCircle);
|
||||||
|
Assert.False(circle.Discoverable);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Only_circle_members_can_post_into_a_circle()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (aliceRoot, alice) = await _harness.Persona("alice");
|
||||||
|
var member = new RemoteActor(_harness.Peer, "member");
|
||||||
|
var outsider = new RemoteActor(_harness.Peer, "outsider");
|
||||||
|
var (_, circle) = await Group(GroupKind.Circle, PostingPolicy.Followers, alice.Id, member.Id);
|
||||||
|
|
||||||
|
await _harness.Deliver(member, "/human-centipede", Create(member, new[] { circle.Uri, circle.Flock }));
|
||||||
|
await _harness.Deliver(outsider, "/human-centipede", Create(outsider, new[] { circle.Uri, circle.Flock }));
|
||||||
|
|
||||||
|
var stored = await DB.Default.Find<Post>().Match(p => p.GroupId == circle.Id).ExecuteAsync(token);
|
||||||
|
Assert.Equal(member.Id, Assert.Single(stored).ActorURI);
|
||||||
|
Assert.Equal(PostVisibility.Circle, stored[0].Visibility);
|
||||||
|
Assert.True(await DB.Default.Find<TimelineEntry>().Match(e => e.AvatarId == alice.Id && e.PostId == stored[0].ID).ExecuteAnyAsync(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_followed_remote_community_announcing_a_post_puts_it_in_home()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (root, alice) = await _harness.Persona("alice");
|
||||||
|
var lemmyCommunity = 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 = lemmyCommunity.Id }, token);
|
||||||
|
await DB.Default.Update<Following>().Match(f => f.AvatarId == alice.Id).Modify(f => f.State, FollowState.Accepted).ExecuteAsync(token);
|
||||||
|
var create = Create(poster, new[] { lemmyCommunity.Id, Addressing.Public }, lemmyCommunity.Id);
|
||||||
|
var noteId = create["object"]!["id"]!.GetValue<string>();
|
||||||
|
_harness.Peer.Serve(new Uri(noteId).AbsolutePath, create["object"]!.ToJsonString());
|
||||||
|
|
||||||
|
await _harness.Deliver(lemmyCommunity, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{Origin(lemmyCommunity)}/activities/announce/{Guid.NewGuid():N}", ["type"] = "Announce", ["actor"] = lemmyCommunity.Id,
|
||||||
|
["to"] = new JsonArray(Addressing.Public), ["object"] = create
|
||||||
|
});
|
||||||
|
|
||||||
|
var post = await DB.Default.Find<Post>().Match(p => p.ObjectURI == noteId).ExecuteSingleAsync(token);
|
||||||
|
Assert.Equal(lemmyCommunity.Id, post.AudienceURI);
|
||||||
|
Assert.True(await DB.Default.Find<TimelineEntry>().Match(e => e.AvatarId == alice.Id && e.PostId == post.ID).ExecuteAnyAsync(token));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels.Post;
|
||||||
|
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 InboundRoutingTests : 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(string text)
|
||||||
|
{
|
||||||
|
var (root, alice) = await _harness.Persona("alice");
|
||||||
|
await _harness.Posts.InsertPost(root, new InsertPostForm { AvatarId = alice.Id, Text = text }, TestContext.Current.CancellationToken);
|
||||||
|
return await DB.Default.Find<Post>().Match(p => p.GroupUserId == alice.Id).ExecuteFirstAsync(TestContext.Current.CancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_downvote_counts_and_its_undo_takes_it_back()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var post = await LocalPost("vote on me");
|
||||||
|
var lemmy = new RemoteActor(_harness.Peer, "voter");
|
||||||
|
var dislike = new JsonObject { ["id"] = NewId(lemmy, "activities/dislike"), ["type"] = "Dislike", ["actor"] = lemmy.Id, ["object"] = post.ObjectURI };
|
||||||
|
|
||||||
|
await _harness.Deliver(lemmy, "/human-centipede", dislike);
|
||||||
|
Assert.Equal(1, (await DB.Default.Find<Post>().OneAsync(post.ID, token)).DownvotesCount);
|
||||||
|
|
||||||
|
await _harness.Deliver(lemmy, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = NewId(lemmy, "activities/undo"), ["type"] = "Undo", ["actor"] = lemmy.Id, ["object"] = dislike.DeepClone()
|
||||||
|
});
|
||||||
|
Assert.Equal(0, (await DB.Default.Find<Post>().OneAsync(post.ID, token)).DownvotesCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_delete_that_arrives_first_keeps_the_post_from_coming_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/delete"), ["type"] = "Delete", ["actor"] = mallory.Id,
|
||||||
|
["object"] = new JsonObject { ["id"] = noteId, ["type"] = "Tombstone" }
|
||||||
|
});
|
||||||
|
await _harness.Deliver(mallory, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = NewId(mallory, "activities/create"), ["type"] = "Create", ["actor"] = mallory.Id,
|
||||||
|
["object"] = new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = noteId, ["type"] = "Note", ["attributedTo"] = mallory.Id, ["content"] = "<p>late</p>",
|
||||||
|
["to"] = new JsonArray(Addressing.Public), ["cc"] = new JsonArray(alice.Uri),
|
||||||
|
["tag"] = new JsonArray(new JsonObject { ["type"] = "Mention", ["href"] = alice.Uri, ["name"] = "@alice" })
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.False(await DB.Default.Find<Post>().Match(p => p.ObjectURI == noteId).ExecuteAnyAsync(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_lemmy_private_message_arrives_as_a_direct_message()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, alice) = await _harness.Persona("alice");
|
||||||
|
var lemmy = new RemoteActor(_harness.Peer, "pm");
|
||||||
|
var messageId = NewId(lemmy, "private_message");
|
||||||
|
|
||||||
|
await _harness.Deliver(lemmy, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = NewId(lemmy, "activities/create"), ["type"] = "Create", ["actor"] = lemmy.Id, ["to"] = new JsonArray(alice.Uri),
|
||||||
|
["object"] = new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = messageId, ["type"] = "ChatMessage", ["attributedTo"] = lemmy.Id, ["to"] = new JsonArray(alice.Uri),
|
||||||
|
["content"] = "<p>psst</p>", ["mediaType"] = "text/html", ["published"] = DateTime.UtcNow.ToString("O")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var message = await DB.Default.Find<Post>().Match(p => p.ObjectURI == messageId).ExecuteFirstAsync(token);
|
||||||
|
Assert.NotNull(message);
|
||||||
|
Assert.Equal(PostVisibility.Direct, message.Visibility);
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
var post = await LocalPost("not an event");
|
||||||
|
var mobilizon = new RemoteActor(_harness.Peer, "joiner");
|
||||||
|
|
||||||
|
await _harness.Deliver(mobilizon, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = NewId(mobilizon, "join"), ["type"] = "Join", ["actor"] = mobilizon.Id, ["object"] = post.ObjectURI
|
||||||
|
});
|
||||||
|
|
||||||
|
var answer = Assert.Single(await _harness.Outgoing(mobilizon.Id + "/inbox"));
|
||||||
|
Assert.Equal("Ignore", answer["type"]!.GetValue<string>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,10 +3,13 @@ using Microsoft.Extensions.Logging.Abstractions;
|
|||||||
|
|
||||||
using MongoDB.Entities;
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Timelines;
|
||||||
using PrivaPub.Federation.Actors;
|
using PrivaPub.Federation.Actors;
|
||||||
using PrivaPub.Federation.Inbox.Handlers;
|
using PrivaPub.Federation.Inbox.Handlers;
|
||||||
using PrivaPub.Federation.Inbox;
|
using PrivaPub.Federation.Inbox;
|
||||||
using PrivaPub.Federation.Moderation;
|
using PrivaPub.Federation.Moderation;
|
||||||
|
using PrivaPub.Domain.Social;
|
||||||
|
using PrivaPub.Domain.Statuses;
|
||||||
using PrivaPub.Federation.Objects;
|
using PrivaPub.Federation.Objects;
|
||||||
using PrivaPub.Federation.Outbox;
|
using PrivaPub.Federation.Outbox;
|
||||||
using PrivaPub.Infrastructure.Jobs;
|
using PrivaPub.Infrastructure.Jobs;
|
||||||
@@ -49,13 +52,15 @@ namespace PrivaPub.Tests.Federation
|
|||||||
var db = new DbEntities();
|
var db = new DbEntities();
|
||||||
_blocks = new DomainBlocks(NullLogger<DomainBlocks>.Instance);
|
_blocks = new DomainBlocks(NullLogger<DomainBlocks>.Instance);
|
||||||
_receiver = new InboxReceiver(_local, remote, queue, _blocks, NullLogger<InboxReceiver>.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[]
|
_processor = new InboxProcessor(remote, new IActivityHandler[]
|
||||||
{
|
{
|
||||||
new FollowHandler(db, _local, remote, delivery),
|
new FollowHandler(db, _local, remote, delivery),
|
||||||
new UndoHandler(db, _local, remote, delivery),
|
new UndoHandler(db, _local, new Reactions(db, delivery)),
|
||||||
new CreateHandler(db, _local, remote, delivery, _blocks),
|
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 DeleteHandler(db, _local, remote, delivery, new GroupDistributor(delivery), quotes),
|
||||||
new UpdateHandler(db, _local, remote)
|
new UpdateHandler(db, _local, remote, new GroupDistributor(delivery), new ObjectRecords(queue), quotes)
|
||||||
}, NullLogger<InboxProcessor>.Instance);
|
}, NullLogger<InboxProcessor>.Instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -322,7 +327,7 @@ namespace PrivaPub.Tests.Federation
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task A_circle_is_not_a_federated_actor()
|
public async Task A_circle_only_takes_follow_requests()
|
||||||
{
|
{
|
||||||
var token = TestContext.Current.CancellationToken;
|
var token = TestContext.Current.CancellationToken;
|
||||||
var (privateKey, publicKey) = Keys.NewKeyPair();
|
var (privateKey, publicKey) = Keys.NewKeyPair();
|
||||||
@@ -338,8 +343,13 @@ namespace PrivaPub.Tests.Federation
|
|||||||
["object"] = actor.Uri
|
["object"] = actor.Uri
|
||||||
};
|
};
|
||||||
|
|
||||||
Assert.False(actor.IsFederated);
|
Assert.True(actor.IsCircle);
|
||||||
Assert.Equal(404, (await Deliver(bob, "/human-centipede", follow)).StatusCode);
|
Assert.True(actor.ManuallyApprovesFollowers);
|
||||||
|
Assert.False(actor.Discoverable);
|
||||||
|
Assert.Equal(202, (await Deliver(bob, "/human-centipede", follow)).StatusCode);
|
||||||
|
var request = await DB.Default.Find<Follower>().Match(f => f.LocalActorId == circle.ID).ExecuteSingleAsync(token);
|
||||||
|
Assert.False(request.IsAccepted);
|
||||||
|
Assert.DoesNotContain(circle.Members, m => m.AvatarId == bob.Id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels.Post;
|
||||||
|
using PrivaPub.ClientModels.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
|
||||||
|
{
|
||||||
|
[Trait("Category", "Integration")]
|
||||||
|
public sealed class InteractionTests : 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<(string Root, Post Post)> LocalPost(string visibility = default)
|
||||||
|
{
|
||||||
|
var (root, alice) = await _harness.Persona("alice");
|
||||||
|
var view = (ViewPost)(await _harness.Posts.InsertPost(root, new InsertPostForm { AvatarId = alice.Id, Text = "like me", Visibility = visibility },
|
||||||
|
TestContext.Current.CancellationToken)).Data;
|
||||||
|
return (root, await DB.Default.Find<Post>().OneAsync(view.Id));
|
||||||
|
}
|
||||||
|
|
||||||
|
static string Origin(RemoteActor actor) => new Uri(actor.Id).GetLeftPart(UriPartial.Authority);
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_like_counts_once_notifies_and_can_be_undone()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, post) = await LocalPost();
|
||||||
|
var carol = new RemoteActor(_harness.Peer, "carol");
|
||||||
|
var like = new JsonObject { ["id"] = $"{Origin(carol)}/likes/{Guid.NewGuid():N}", ["type"] = "Like", ["actor"] = carol.Id, ["object"] = post.ObjectURI };
|
||||||
|
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", like);
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", new JsonObject { ["id"] = $"{Origin(carol)}/likes/{Guid.NewGuid():N}", ["type"] = "Like", ["actor"] = carol.Id, ["object"] = post.ObjectURI });
|
||||||
|
|
||||||
|
Assert.Equal(1, (await DB.Default.Find<Post>().OneAsync(post.ID, token)).FavouritesCount);
|
||||||
|
Assert.Equal(NotificationType.Favourite, (await DB.Default.Find<Notification>().Match(n => n.AvatarId == post.GroupUserId).ExecuteSingleAsync(token)).Type);
|
||||||
|
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{Origin(carol)}/undos/{Guid.NewGuid():N}", ["type"] = "Undo", ["actor"] = carol.Id, ["object"] = like
|
||||||
|
});
|
||||||
|
Assert.Equal(0, (await DB.Default.Find<Post>().OneAsync(post.ID, token)).FavouritesCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_stranger_cannot_like_a_followers_only_post()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, post) = await LocalPost("followersonly");
|
||||||
|
var mallory = new RemoteActor(_harness.Peer, "mallory");
|
||||||
|
|
||||||
|
await _harness.Deliver(mallory, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{Origin(mallory)}/likes/{Guid.NewGuid():N}", ["type"] = "Like", ["actor"] = mallory.Id, ["object"] = post.ObjectURI
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.Equal(0, (await DB.Default.Find<Post>().OneAsync(post.ID, token)).FavouritesCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_boost_of_a_local_post_counts_notifies_and_can_be_undone()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, post) = await LocalPost();
|
||||||
|
var carol = new RemoteActor(_harness.Peer, "carol");
|
||||||
|
var announce = new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{Origin(carol)}/announces/{Guid.NewGuid():N}", ["type"] = "Announce", ["actor"] = carol.Id, ["object"] = post.ObjectURI,
|
||||||
|
["to"] = new JsonArray(Addressing.Public), ["cc"] = new JsonArray(carol.Id + "/followers")
|
||||||
|
};
|
||||||
|
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", announce);
|
||||||
|
|
||||||
|
Assert.Equal(1, (await DB.Default.Find<Post>().OneAsync(post.ID, token)).ReblogsCount);
|
||||||
|
Assert.Equal(NotificationType.Reblog, (await DB.Default.Find<Notification>().Match(n => n.AvatarId == post.GroupUserId).ExecuteSingleAsync(token)).Type);
|
||||||
|
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{Origin(carol)}/undos/{Guid.NewGuid():N}", ["type"] = "Undo", ["actor"] = carol.Id, ["object"] = announce["id"]!.GetValue<string>()
|
||||||
|
});
|
||||||
|
Assert.Equal(0, (await DB.Default.Find<Post>().OneAsync(post.ID, token)).ReblogsCount);
|
||||||
|
Assert.False(await DB.Default.Find<Post>().Match(p => p.ObjectURI == announce["id"]!.GetValue<string>()).ExecuteAnyAsync(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_followed_account_boosting_a_remote_post_puts_it_in_home()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (bobRoot, bob) = await _harness.Persona("bob");
|
||||||
|
var carol = new RemoteActor(_harness.Peer, "carol");
|
||||||
|
var dave = new RemoteActor(_harness.Peer, "dave");
|
||||||
|
var stranger = new RemoteActor(_harness.Peer, "stranger");
|
||||||
|
await _harness.Follows.Follow(bobRoot, new FollowForm { AvatarId = bob.Id, Target = carol.Id }, token);
|
||||||
|
await DB.Default.Update<Following>().Match(f => f.AvatarId == bob.Id).Modify(f => f.State, FollowState.Accepted).ExecuteAsync(token);
|
||||||
|
var noteId = $"{Origin(dave)}/notes/{Guid.NewGuid():N}";
|
||||||
|
_harness.Peer.Serve(new Uri(noteId).AbsolutePath, new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = noteId, ["type"] = "Note", ["attributedTo"] = dave.Id, ["content"] = "<p>worth sharing</p>", ["to"] = new JsonArray(Addressing.Public)
|
||||||
|
}.ToJsonString());
|
||||||
|
|
||||||
|
await _harness.Deliver(stranger, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{Origin(stranger)}/announces/{Guid.NewGuid():N}", ["type"] = "Announce", ["actor"] = stranger.Id, ["object"] = noteId,
|
||||||
|
["to"] = new JsonArray(Addressing.Public)
|
||||||
|
});
|
||||||
|
Assert.False(await DB.Default.Find<Post>().Match(p => p.ObjectURI == noteId).ExecuteAnyAsync(token));
|
||||||
|
|
||||||
|
await _harness.Deliver(carol, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{Origin(carol)}/announces/{Guid.NewGuid():N}", ["type"] = "Announce", ["actor"] = carol.Id, ["object"] = noteId,
|
||||||
|
["to"] = new JsonArray(Addressing.Public)
|
||||||
|
});
|
||||||
|
|
||||||
|
var home = (List<ViewPost>)(await _harness.Timelines.Home(bobRoot, bob.Id, default, 20, token)).Data;
|
||||||
|
var boosted = Assert.Single(home);
|
||||||
|
Assert.Equal("<p>worth sharing</p>", boosted.ReblogOf.ContentHtml);
|
||||||
|
Assert.Equal(carol.Id, boosted.AuthorActorURI);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -54,6 +54,7 @@ namespace PrivaPub.Tests.Federation
|
|||||||
Assert.Equal("a cat", media.Description);
|
Assert.Equal("a cat", media.Description);
|
||||||
Assert.Equal(new[] { 0.5f, -1f }, media.Focus);
|
Assert.Equal(new[] { 0.5f, -1f }, media.Focus);
|
||||||
Assert.Equal(new DateTime(2026, 9, 30, 10, 0, 0, DateTimeKind.Utc), note.Published);
|
Assert.Equal(new DateTime(2026, 9, 30, 10, 0, 0, DateTimeKind.Utc), note.Published);
|
||||||
|
Assert.Null(note.Updated);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -89,6 +90,28 @@ namespace PrivaPub.Tests.Federation
|
|||||||
Assert.Equal("https://k.example/notes/8", note.QuoteUri);
|
Assert.Equal("https://k.example/notes/8", note.QuoteUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("Article", false, false)]
|
||||||
|
[InlineData("Event", false, false)]
|
||||||
|
[InlineData("Video", false, false)]
|
||||||
|
[InlineData("Page", false, false)]
|
||||||
|
[InlineData("Note", false, true)]
|
||||||
|
[InlineData("Question", false, true)]
|
||||||
|
[InlineData("Article", true, true)]
|
||||||
|
public void A_summary_warns_only_on_notes_or_when_marked_sensitive(string type, bool sensitive, bool warns)
|
||||||
|
{
|
||||||
|
var note = NoteParser.Parse(JsonNode.Parse($$"""
|
||||||
|
{
|
||||||
|
"id": "https://w.example/?p=12", "type": "{{type}}", "name": "Twelve", "summary": "Saturday, 10:00 at the library",
|
||||||
|
"sensitive": {{(sensitive ? "true" : "false")}}, "attributedTo": "https://w.example/author", "content": "<p>body</p>"
|
||||||
|
}
|
||||||
|
"""));
|
||||||
|
|
||||||
|
Assert.Equal(warns, note.Sensitive);
|
||||||
|
Assert.Equal(warns ? "Saturday, 10:00 at the library" : null, note.SpoilerText);
|
||||||
|
Assert.Equal(warns ? null : "Saturday, 10:00 at the library", note.Excerpt);
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("""{ "id": "https://x.example/1", "type": "Person" }""")]
|
[InlineData("""{ "id": "https://x.example/1", "type": "Person" }""")]
|
||||||
[InlineData("""{ "id": "not a uri", "type": "Note" }""")]
|
[InlineData("""{ "id": "not a uri", "type": "Note" }""")]
|
||||||
|
|||||||
@@ -0,0 +1,224 @@
|
|||||||
|
using PrivaPub.Api.Mastodon.Mappers;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Federation
|
||||||
|
{
|
||||||
|
public class ObjectShapesTests
|
||||||
|
{
|
||||||
|
static NoteDocument Parse(string json) => NoteParser.Parse(JsonNode.Parse(json));
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Keeps_a_peertube_video_with_its_files_poster_captions_and_markdown_description()
|
||||||
|
{
|
||||||
|
var note = Parse("""
|
||||||
|
{
|
||||||
|
"id": "https://tube.example/videos/watch/1", "type": "Video", "name": "A *talk*", "duration": "PT6299S",
|
||||||
|
"mediaType": "text/markdown", "content": "**Bold** words", "sensitive": false, "views": 14771, "state": 1,
|
||||||
|
"isLiveBroadcast": false, "commentsPolicy": 2, "downloadEnabled": true,
|
||||||
|
"licence": { "identifier": "5", "name": "Attribution - Share Alike" }, "category": { "identifier": "2", "name": "Films" },
|
||||||
|
"attributedTo": [{ "type": "Person", "id": "https://tube.example/accounts/ann" }, { "type": "Group", "id": "https://tube.example/video-channels/films" }],
|
||||||
|
"icon": [
|
||||||
|
{ "type": "Image", "url": "https://tube.example/thumb-280.jpg", "mediaType": "image/jpeg", "width": 280, "height": 157 },
|
||||||
|
{ "type": "Image", "url": "https://tube.example/thumb-1920.jpg", "mediaType": "image/jpeg", "width": 1920, "height": 1080 },
|
||||||
|
{ "type": "Image", "url": "https://tube.example/thumb-850.jpg", "mediaType": "image/jpeg", "width": 850, "height": 480 }
|
||||||
|
],
|
||||||
|
"subtitleLanguage": [{ "identifier": "en", "url": [{ "mediaType": "text/vtt", "href": "https://tube.example/en.vtt" }] }],
|
||||||
|
"url": [
|
||||||
|
{ "type": "Link", "mediaType": "text/html", "href": "https://tube.example/w/abc" },
|
||||||
|
{ "type": "Link", "mediaType": "video/mp4", "href": "https://tube.example/720.mp4", "height": 720, "width": 1280, "fps": 24, "size": 942586378,
|
||||||
|
"attachment": [{ "type": "PropertyValue", "name": "ffprobe_codec_type", "value": "audio" }, { "type": "PropertyValue", "name": "ffprobe_codec_type", "value": "video" }] },
|
||||||
|
{ "type": "Link", "mediaType": "application/x-mpegURL", "href": "https://tube.example/master.m3u8" },
|
||||||
|
{ "type": "Link", "mediaType": "application/x-bittorrent;x-scheme-handler/magnet", "href": "magnet:?xt=urn:btih:abc" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
Assert.Equal("https://tube.example/w/abc", note.Url);
|
||||||
|
Assert.Equal("A *talk*", note.Title);
|
||||||
|
Assert.Contains("<strong>Bold</strong>", note.ContentHtml);
|
||||||
|
Assert.Equal("https://tube.example/thumb-850.jpg", note.CoverURL);
|
||||||
|
Assert.Equal(6299, note.Video.DurationSeconds);
|
||||||
|
var file = Assert.Single(note.Video.Variants);
|
||||||
|
Assert.Equal(new[] { "audio", "video" }, file.Streams);
|
||||||
|
Assert.Equal(942586378, file.Size);
|
||||||
|
Assert.Equal("https://tube.example/master.m3u8", note.Video.PlaylistURL);
|
||||||
|
Assert.Equal(2, note.Video.CommentsPolicy);
|
||||||
|
Assert.Equal("Attribution - Share Alike", note.Video.Licence);
|
||||||
|
Assert.Equal("https://tube.example/video-channels/films", note.Video.ChannelURI);
|
||||||
|
Assert.Equal("https://tube.example/en.vtt", Assert.Single(note.Video.Captions).Url);
|
||||||
|
Assert.Equal("https://tube.example/accounts/ann", note.AttributedTo);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Keeps_a_mobilizon_event_with_its_time_place_and_online_link()
|
||||||
|
{
|
||||||
|
var note = Parse("""
|
||||||
|
{
|
||||||
|
"id": "https://events.example/events/9", "type": "Event", "name": "Repair café",
|
||||||
|
"summary": "<p>Saturday at the library</p>", "content": "<p>Bring broken things.</p>",
|
||||||
|
"startTime": "2026-10-03T10:00:00+02:00", "endTime": "2026-10-03T13:00:00+02:00", "timezone": "Europe/Rome",
|
||||||
|
"joinMode": "free", "participantCount": 12, "maximumAttendeeCapacity": 40, "ical:status": "CONFIRMED",
|
||||||
|
"location": { "type": "Place", "name": "Biblioteca", "latitude": 45.07, "longitude": 7.68,
|
||||||
|
"address": { "type": "PostalAddress", "streetAddress": "Via Roma 1", "addressLocality": "Torino", "addressCountry": "IT" } },
|
||||||
|
"attachment": [{ "type": "Link", "name": "Website", "href": "https://meet.example/repair" }],
|
||||||
|
"attributedTo": "https://events.example/@club"
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
Assert.Null(note.SpoilerText);
|
||||||
|
Assert.False(note.Sensitive);
|
||||||
|
Assert.Equal("Saturday at the library", note.Excerpt);
|
||||||
|
Assert.Equal(new DateTime(2026, 10, 3, 8, 0, 0, DateTimeKind.Utc), note.Event.Start);
|
||||||
|
Assert.Equal("Via Roma 1, Torino, IT", Assert.Single(note.Event.Places).Address);
|
||||||
|
Assert.Equal("https://meet.example/repair", note.Event.OnlineURL);
|
||||||
|
Assert.Equal(40, note.Event.Capacity);
|
||||||
|
Assert.Equal("CONFIRMED", note.Event.Status);
|
||||||
|
Assert.Equal("2026-10-03 10:00 – 13:00 (Europe/Rome) · Biblioteca", MastodonMapper.EventLine(note.Event));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Keeps_a_lemmy_link_post_as_a_link_with_its_thumbnail()
|
||||||
|
{
|
||||||
|
var note = Parse("""
|
||||||
|
{
|
||||||
|
"id": "https://lemmy.example/post/5", "type": "Page", "name": "A good read", "attributedTo": "https://lemmy.example/u/bo",
|
||||||
|
"attachment": [{ "type": "Link", "href": "https://news.example/story", "mediaType": "text/html" }],
|
||||||
|
"image": { "type": "Image", "url": "https://lemmy.example/pictrs/thumb.jpg" },
|
||||||
|
"language": { "identifier": "en", "name": "English" }
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
Assert.Equal("https://news.example/story", note.Link.Href);
|
||||||
|
Assert.Equal("https://lemmy.example/pictrs/thumb.jpg", note.Link.ImageURL);
|
||||||
|
Assert.False(note.Link.FromPublisher);
|
||||||
|
Assert.Empty(note.Attachments);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Takes_a_publisher_supplied_preview_from_a_link_attachment()
|
||||||
|
{
|
||||||
|
var note = Parse("""
|
||||||
|
{
|
||||||
|
"id": "https://m.example/statuses/1", "type": "Note", "attributedTo": "https://m.example/users/a", "content": "<p>look</p>",
|
||||||
|
"attachment": [{ "type": "Link", "href": "https://news.example/a",
|
||||||
|
"preview": { "type": "Article", "name": "Headline & more", "summary": "<p>What happened</p>", "image": { "type": "Image", "url": "https://news.example/a.jpg" } } }]
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
Assert.Equal("Headline & more", note.Link.Title);
|
||||||
|
Assert.Equal("What happened", note.Link.Description);
|
||||||
|
Assert.Equal("https://news.example/a.jpg", note.Link.ImageURL);
|
||||||
|
Assert.True(note.Link.FromPublisher);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Reads_media_in_every_shape_and_infers_what_is_missing()
|
||||||
|
{
|
||||||
|
var note = Parse("""
|
||||||
|
{
|
||||||
|
"id": "https://bsky.brid.gy/convert/ap/at://did:plc:x/post/1", "type": "Note", "attributedTo": "https://bsky.brid.gy/ap/did:plc:x",
|
||||||
|
"content": "<p>pics</p>",
|
||||||
|
"@context": ["https://www.w3.org/ns/activitystreams", { "@language": "de" }],
|
||||||
|
"attachment": [
|
||||||
|
{ "type": "Image", "url": "https://cdn.example/a.jpeg", "summary": "alt from summary" },
|
||||||
|
{ "type": "Document", "url": [{ "type": "Link", "href": "https://cdn.example/clip", "mediaType": "video/mp4" }],
|
||||||
|
"icon": { "type": "Image", "url": "https://cdn.example/clip.jpg" }, "duration": "PT12.4S", "sensitive": true },
|
||||||
|
{ "type": "Image", "url": "https://cdn.example/nameless" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
Assert.Equal("de", note.Language);
|
||||||
|
Assert.Equal("image/jpeg", note.Attachments[0].ContentType);
|
||||||
|
Assert.Equal("alt from summary", note.Attachments[0].Description);
|
||||||
|
Assert.Equal("https://cdn.example/clip", note.Attachments[1].RemoteURL);
|
||||||
|
Assert.Equal("video/mp4", note.Attachments[1].ContentType);
|
||||||
|
Assert.Equal("https://cdn.example/clip.jpg", note.Attachments[1].RemotePreviewURL);
|
||||||
|
Assert.Equal(12.4, note.Attachments[1].DurationSeconds);
|
||||||
|
Assert.True(note.Attachments[1].Sensitive);
|
||||||
|
Assert.Equal("image/*", note.Attachments[2].ContentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Keeps_a_funkwhale_track_and_a_loops_poster()
|
||||||
|
{
|
||||||
|
var track = Parse("""
|
||||||
|
{
|
||||||
|
"id": "https://music.example/federation/music/uploads/1", "type": "Audio", "name": "Song", "attributedTo": "https://music.example/@band",
|
||||||
|
"summary": "#rock #live", "duration": 241, "position": 3, "album": "https://music.example/albums/2", "license": "https://creativecommons.org/licenses/by/4.0/",
|
||||||
|
"url": [{ "type": "Link", "mediaType": "text/html", "href": "https://music.example/library/tracks/1" },
|
||||||
|
{ "type": "Link", "mediaType": "audio/mpeg", "href": "https://music.example/listen/1.mp3", "bitrate": 320000, "size": 9640000 }]
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
var loop = Parse("""
|
||||||
|
{
|
||||||
|
"id": "https://loops.example/v/1", "type": "Note", "attributedTo": "https://loops.example/@dancer", "content": "",
|
||||||
|
"preview": { "type": "Image", "url": { "href": "https://loops.example/poster.jpg", "mediaType": "image/jpeg" } },
|
||||||
|
"attachment": [{ "type": "Document", "mediaType": "video/mp4", "url": "https://loops.example/v/1.mp4", "width": 720, "height": 1280 }]
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
Assert.Equal("#rock #live", track.Excerpt);
|
||||||
|
Assert.Null(track.SpoilerText);
|
||||||
|
Assert.Equal(241, track.Audio.DurationSeconds);
|
||||||
|
Assert.Equal(3, track.Audio.Position);
|
||||||
|
Assert.Equal("https://music.example/listen/1.mp3", Assert.Single(track.Audio.Variants).Url);
|
||||||
|
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 Normalises_hashtags_like_mastodon() =>
|
||||||
|
Assert.Equal("fedi".Normalize(System.Text.NormalizationForm.FormKC).ToLowerInvariant(), Assert.Single(Parse("""
|
||||||
|
{ "id": "https://x.example/1", "type": "Note", "attributedTo": "https://x.example/u", "content": "",
|
||||||
|
"tag": [{ "type": "Hashtag", "name": "#FEDI" }] }
|
||||||
|
""").Tags));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
using PrivaPub.Tests.Support;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Federation
|
||||||
|
{
|
||||||
|
[Trait("Category", "Integration")]
|
||||||
|
public sealed class ProvenanceTests : 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_delivered_post_keeps_its_raw_object_how_it_came_and_every_edit()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (_, alice) = await _harness.Persona("alice");
|
||||||
|
var mallory = new RemoteActor(_harness.Peer, "mallory");
|
||||||
|
var origin = new Uri(mallory.Id).GetLeftPart(UriPartial.Authority);
|
||||||
|
var note = new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{origin}/notes/{Guid.NewGuid():N}",
|
||||||
|
["type"] = "Note",
|
||||||
|
["attributedTo"] = mallory.Id,
|
||||||
|
["content"] = "<p>hi</p>",
|
||||||
|
["_misskey_quote"] = $"{origin}/notes/elsewhere",
|
||||||
|
["to"] = new JsonArray(Addressing.Public),
|
||||||
|
["cc"] = new JsonArray(alice.Uri),
|
||||||
|
["tag"] = new JsonArray(new JsonObject { ["type"] = "Mention", ["href"] = alice.Uri, ["name"] = "@alice" })
|
||||||
|
};
|
||||||
|
var create = new JsonObject { ["id"] = $"{origin}/activities/{Guid.NewGuid():N}", ["type"] = "Create", ["actor"] = mallory.Id, ["object"] = note.DeepClone() };
|
||||||
|
|
||||||
|
await _harness.Deliver(mallory, "/human-centipede", create);
|
||||||
|
var edited = note.DeepClone().AsObject();
|
||||||
|
edited["content"] = "<p>hi again</p>";
|
||||||
|
edited["updated"] = DateTime.UtcNow.ToString("O");
|
||||||
|
await _harness.Deliver(mallory, "/human-centipede", new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{origin}/activities/{Guid.NewGuid():N}", ["type"] = "Update", ["actor"] = mallory.Id, ["object"] = edited
|
||||||
|
});
|
||||||
|
|
||||||
|
var record = await DB.Default.Find<ObjectRecord>().Match(r => r.ObjectURI == note["id"]!.GetValue<string>()).ExecuteFirstAsync(token);
|
||||||
|
Assert.NotNull(record);
|
||||||
|
Assert.Equal(ObjectPath.Delivered, record.Path);
|
||||||
|
Assert.False(record.Refetched);
|
||||||
|
Assert.Equal("shared", record.Inbox);
|
||||||
|
Assert.Equal(mallory.KeyId, record.KeyId);
|
||||||
|
Assert.Equal(create["id"]!.GetValue<string>(), record.ActivityId);
|
||||||
|
Assert.Contains("digest", record.SignedHeaders);
|
||||||
|
Assert.Equal("<p>hi</p>", JsonNode.Parse(record.Raw)!["content"]!.GetValue<string>());
|
||||||
|
Assert.Contains("legacy-quote", ObjectFeatures.Detect(JsonNode.Parse(record.Raw)!.AsObject()));
|
||||||
|
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,159 @@
|
|||||||
|
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_another_persona_cannot_be_quoted_yet()
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
await _harness.Posts.InsertPost(bobRoot, new InsertPostForm { AvatarId = bob.Id, Text = "mine" }, token);
|
||||||
|
var bobs = await DB.Default.Find<Post>().Match(p => p.GroupUserId == bob.Id).ExecuteFirstAsync(token);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -57,7 +57,7 @@ namespace PrivaPub.Tests.Federation
|
|||||||
Language = "en",
|
Language = "en",
|
||||||
Mentions = new() { new PostMention { ActorURI = "https://m.example/users/bob", Handle = "@bob@m.example" } },
|
Mentions = new() { new PostMention { ActorURI = "https://m.example/users/bob", Handle = "@bob@m.example" } },
|
||||||
Tags = new() { "cats" },
|
Tags = new() { "cats" },
|
||||||
CreationDate = new DateTime(2026, 9, 30, 17, 45, 12, DateTimeKind.Utc)
|
CreationDate = new DateTime(2026, 9, 30, 17, 45, 12, 345, DateTimeKind.Utc)
|
||||||
};
|
};
|
||||||
|
|
||||||
var note = ActivityPubRenderer.Note(post, Alice, default, "https://m.example/users/bob/statuses/1");
|
var note = ActivityPubRenderer.Note(post, Alice, default, "https://m.example/users/bob/statuses/1");
|
||||||
@@ -67,7 +67,7 @@ namespace PrivaPub.Tests.Federation
|
|||||||
Assert.Equal("<p><strong>On <cats></strong></p><p>body</p>", note["content"]!.GetValue<string>());
|
Assert.Equal("<p><strong>On <cats></strong></p><p>body</p>", note["content"]!.GetValue<string>());
|
||||||
Assert.Equal("On <cats>", note["summary"]!.GetValue<string>());
|
Assert.Equal("On <cats>", note["summary"]!.GetValue<string>());
|
||||||
Assert.True(note["sensitive"]!.GetValue<bool>());
|
Assert.True(note["sensitive"]!.GetValue<bool>());
|
||||||
Assert.Equal("2026-09-30T17:45:12Z", note["published"]!.GetValue<string>());
|
Assert.Equal("2026-09-30T17:45:12.345Z", note["published"]!.GetValue<string>());
|
||||||
Assert.Equal(new[] { ActivityPubRenderer.Public }, note["to"]!.AsArray().Select(n => n!.GetValue<string>()));
|
Assert.Equal(new[] { ActivityPubRenderer.Public }, note["to"]!.AsArray().Select(n => n!.GetValue<string>()));
|
||||||
Assert.Equal(new[] { Alice.Followers, "https://m.example/users/bob" }, note["cc"]!.AsArray().Select(n => n!.GetValue<string>()));
|
Assert.Equal(new[] { Alice.Followers, "https://m.example/users/bob" }, note["cc"]!.AsArray().Select(n => n!.GetValue<string>()));
|
||||||
var tags = note["tag"]!.AsArray();
|
var tags = note["tag"]!.AsArray();
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ namespace PrivaPub.Tests.Infrastructure
|
|||||||
while (DateTime.UtcNow < deadline && !_peer.Requests.Any(r => r.Path == "/live/inbox"))
|
while (DateTime.UtcNow < deadline && !_peer.Requests.Any(r => r.Path == "/live/inbox"))
|
||||||
await Task.Delay(50, token);
|
await Task.Delay(50, token);
|
||||||
var live = Assert.Single(_peer.Requests, r => r.Path == "/live/inbox");
|
var live = Assert.Single(_peer.Requests, r => r.Path == "/live/inbox");
|
||||||
Assert.True(_peer.Requests.Count(r => r.Path == "/dead/inbox") < 10);
|
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))
|
while (DateTime.UtcNow < deadline && !await DB.Default.Find<RemoteInstance>().Match(i => i.Host == "localhost").ExecuteAnyAsync(token))
|
||||||
await Task.Delay(50, token);
|
await Task.Delay(50, token);
|
||||||
await worker.StopAsync(token);
|
await worker.StopAsync(token);
|
||||||
|
|||||||
@@ -12,15 +12,18 @@ namespace PrivaPub.Tests.Infrastructure
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void Persona_ids_carry_only_the_day_and_share_nothing_else()
|
public void Persona_ids_carry_only_the_day_and_share_nothing_else()
|
||||||
{
|
{
|
||||||
var first = (string)new Avatar().GenerateNewID();
|
var avatar = new Avatar();
|
||||||
var second = (string)new Avatar().GenerateNewID();
|
var first = (string)avatar.GenerateNewID();
|
||||||
|
var second = (string)new Avatar { PublishedOn = avatar.PublishedOn }.GenerateNewID();
|
||||||
|
var group = new GroupEntity();
|
||||||
|
|
||||||
Assert.True(ObjectId.TryParse(first, out var a));
|
Assert.True(ObjectId.TryParse(first, out var a));
|
||||||
Assert.True(ObjectId.TryParse(second, out var b));
|
Assert.True(ObjectId.TryParse(second, out var b));
|
||||||
Assert.Equal(DateTime.UtcNow.Date, a.CreationTime);
|
Assert.Equal(avatar.PublishedOn, a.CreationTime);
|
||||||
|
Assert.InRange(a.CreationTime, DateTime.UtcNow.Date.AddDays(-13), DateTime.UtcNow.Date);
|
||||||
Assert.Equal(a.CreationTime, b.CreationTime);
|
Assert.Equal(a.CreationTime, b.CreationTime);
|
||||||
Assert.NotEqual(first[8..14], second[8..14]);
|
Assert.NotEqual(first[8..14], second[8..14]);
|
||||||
Assert.Equal(DateTime.UtcNow.Date, ObjectId.Parse((string)new GroupEntity().GenerateNewID()).CreationTime);
|
Assert.Equal(group.PublishedOn, ObjectId.Parse((string)group.GenerateNewID()).CreationTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -33,6 +36,33 @@ namespace PrivaPub.Tests.Infrastructure
|
|||||||
Assert.Equal(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc), ObjectId.Parse(older).CreationTime);
|
Assert.Equal(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc), ObjectId.Parse(older).CreationTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_post_arriving_live_sorts_after_everything_already_stored()
|
||||||
|
{
|
||||||
|
var local = ObjectId.GenerateNewId().ToString();
|
||||||
|
var reply = PrivacyIds.Arrived(DateTime.UtcNow.AddSeconds(-30));
|
||||||
|
var skewed = PrivacyIds.Arrived(DateTime.UtcNow.AddYears(50));
|
||||||
|
|
||||||
|
Assert.True(string.CompareOrdinal(local, reply) < 0);
|
||||||
|
Assert.True(string.CompareOrdinal(reply, skewed) < 0);
|
||||||
|
Assert.True(ObjectId.Parse(skewed).CreationTime < DateTime.UtcNow.AddMinutes(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_backfilled_post_keeps_its_published_time() =>
|
||||||
|
Assert.Equal(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc), ObjectId.Parse(PrivacyIds.Arrived(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc))).CreationTime);
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_persona_publishes_a_day_within_two_weeks_before_its_creation()
|
||||||
|
{
|
||||||
|
var created = new DateTime(2026, 10, 1, 15, 30, 0, DateTimeKind.Utc);
|
||||||
|
var days = Enumerable.Range(0, 200).Select(_ => PrivacyIds.PublishedDay(created)).ToList();
|
||||||
|
|
||||||
|
Assert.All(days, d => Assert.Equal(TimeSpan.Zero, d.TimeOfDay));
|
||||||
|
Assert.All(days, d => Assert.InRange(d, created.Date.AddDays(-13), created.Date));
|
||||||
|
Assert.True(days.Distinct().Count() > 1);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void A_published_time_in_the_future_is_clamped() =>
|
public void A_published_time_in_the_future_is_clamped() =>
|
||||||
Assert.True(ObjectId.Parse(PrivacyIds.At(DateTime.UtcNow.AddYears(50))).CreationTime < DateTime.UtcNow.AddDays(2));
|
Assert.True(ObjectId.Parse(PrivacyIds.At(DateTime.UtcNow.AddYears(50))).CreationTime < DateTime.UtcNow.AddDays(2));
|
||||||
|
|||||||
@@ -0,0 +1,158 @@
|
|||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Microsoft.Extensions.Localization;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Content;
|
||||||
|
using PrivaPub.Domain.Media;
|
||||||
|
using PrivaPub.Domain.Relationships;
|
||||||
|
using PrivaPub.Domain.Social;
|
||||||
|
using PrivaPub.Domain.Statuses;
|
||||||
|
using PrivaPub.Domain.Timelines;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Inbox;
|
||||||
|
using PrivaPub.Federation.Inbox.Handlers;
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Infrastructure.Jobs;
|
||||||
|
using PrivaPub.Models;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.Resources;
|
||||||
|
using PrivaPub.Services;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Support
|
||||||
|
{
|
||||||
|
public sealed class Harness : IAsyncDisposable
|
||||||
|
{
|
||||||
|
public const string Host = "privapub.test";
|
||||||
|
public const string Base = "https://" + Host;
|
||||||
|
|
||||||
|
Harness(Peer peer)
|
||||||
|
{
|
||||||
|
Peer = peer;
|
||||||
|
var cache = new MemoryCache(new MemoryCacheOptions());
|
||||||
|
Local = new LocalActorService(Db, new StaticOptions<AppConfiguration>(new AppConfiguration { BackendBaseAddress = Base }));
|
||||||
|
Remote = new RemoteActorService(Peer.Http(cache), Local, cache, Db);
|
||||||
|
Delivery = new DeliveryService(Db, Queue);
|
||||||
|
Fanout = new Fanout(Db);
|
||||||
|
Groups = new GroupDistributor(Delivery);
|
||||||
|
Records = new ObjectRecords(Queue);
|
||||||
|
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);
|
||||||
|
Processor = new InboxProcessor(Remote, new IActivityHandler[]
|
||||||
|
{
|
||||||
|
new FollowHandler(Db, Local, Remote, Delivery),
|
||||||
|
new AcceptHandler(Db, Local, Quotes),
|
||||||
|
new RejectHandler(Db, Local, Quotes),
|
||||||
|
new UndoHandler(Db, Local, Reactions),
|
||||||
|
new LikeHandler(Db, Reactions),
|
||||||
|
new EmojiReactHandler(Db, Reactions),
|
||||||
|
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, 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);
|
||||||
|
Follows = new FollowService(Db, Local, Remote, Delivery, new KeyLocalizer<GenericRes>(), NullLogger<FollowService>.Instance);
|
||||||
|
Content = new ContentRenderer(Local, Remote);
|
||||||
|
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, 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);
|
||||||
|
Reports = new ReportService(Db, Local, Delivery);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Peer Peer { get; }
|
||||||
|
public DbEntities Db { 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 FollowService Follows { get; }
|
||||||
|
public ContentRenderer Content { get; }
|
||||||
|
public OutboxPublisher Outbox { get; }
|
||||||
|
public PostsService Posts { get; }
|
||||||
|
public StatusService Statuses { get; }
|
||||||
|
public MediaService Media { get; }
|
||||||
|
public GroupDistributor Groups { get; }
|
||||||
|
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; }
|
||||||
|
|
||||||
|
public async Task FollowedBy(LocalActor local, RemoteActor follower) =>
|
||||||
|
await DB.Default.SaveAsync(new Follower
|
||||||
|
{
|
||||||
|
LocalActorId = local.Id,
|
||||||
|
LocalActorKind = local.Kind,
|
||||||
|
ActorURI = follower.Id,
|
||||||
|
InboxURL = follower.Id + "/inbox",
|
||||||
|
SharedInboxURL = follower.SharedInbox
|
||||||
|
});
|
||||||
|
|
||||||
|
public static async Task<Harness> Start() => new(await Peer.Start());
|
||||||
|
|
||||||
|
public async Task<(string RootId, LocalActor Avatar)> Persona(string name, string rootId = default)
|
||||||
|
{
|
||||||
|
rootId ??= Guid.NewGuid().ToString("N")[..24];
|
||||||
|
var (privateKey, publicKey) = Keys.NewKeyPair();
|
||||||
|
var avatar = new Avatar { UserName = $"{name}{Guid.NewGuid():N}"[..20], Name = name, PrivateKey = privateKey, PublicKey = publicKey };
|
||||||
|
await DB.Default.SaveAsync(avatar);
|
||||||
|
await DB.Default.SaveAsync(new RootToAvatar { RootId = rootId, AvatarId = avatar.ID });
|
||||||
|
return (rootId, Local.FromAvatar(avatar));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<InboxResult> Deliver(RemoteActor sender, string path, JsonNode activity)
|
||||||
|
{
|
||||||
|
var result = await Receiver.Receive(sender.Post(Host, path, activity), default, CancellationToken.None);
|
||||||
|
var dedupe = "inbox|" + (activity is JsonObject ? activity["id"]?.GetValue<string>() : default);
|
||||||
|
var job = await DB.Default.Find<Job>().Match(j => j.DedupeKey == dedupe).ExecuteFirstAsync();
|
||||||
|
if (job != default)
|
||||||
|
Assert.Equal(JobResult.Done, (await Processor.Handle(job, CancellationToken.None)).Result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<JsonObject>> Outgoing(string inbox) =>
|
||||||
|
(await DB.Default.Find<Job>().Match(j => j.Kind == JobKind.Deliver).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();
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class KeyLocalizer<T> : IStringLocalizer<T>
|
||||||
|
{
|
||||||
|
public LocalizedString this[string name] => new(name, name);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ namespace PrivaPub.Tests.Support
|
|||||||
readonly WebApplication _app;
|
readonly WebApplication _app;
|
||||||
readonly ConcurrentDictionary<string, string> _documents = new();
|
readonly ConcurrentDictionary<string, string> _documents = new();
|
||||||
readonly ConcurrentDictionary<string, (int Status, TimeSpan Delay)> _answers = new();
|
readonly ConcurrentDictionary<string, (int Status, TimeSpan Delay)> _answers = new();
|
||||||
|
readonly ConcurrentDictionary<string, (byte[] Bytes, string ContentType)> _files = new();
|
||||||
|
|
||||||
public int Port { get; }
|
public int Port { get; }
|
||||||
public string A => $"http://127.0.0.1:{Port}";
|
public string A => $"http://127.0.0.1:{Port}";
|
||||||
@@ -41,6 +42,12 @@ namespace PrivaPub.Tests.Support
|
|||||||
{
|
{
|
||||||
peer.Requests.Enqueue(new(context.Request.Method, context.Request.Path, context.Request.Headers["Signature"].ToString()));
|
peer.Requests.Enqueue(new(context.Request.Method, context.Request.Path, context.Request.Headers["Signature"].ToString()));
|
||||||
var key = context.Request.Path.Value;
|
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);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (peer._answers.TryGetValue(key, out var answer))
|
if (peer._answers.TryGetValue(key, out var answer))
|
||||||
{
|
{
|
||||||
if (answer.Delay > TimeSpan.Zero)
|
if (answer.Delay > TimeSpan.Zero)
|
||||||
@@ -63,6 +70,8 @@ namespace PrivaPub.Tests.Support
|
|||||||
|
|
||||||
public void Serve(string path, string json) => _documents[path] = json;
|
public void Serve(string path, string json) => _documents[path] = json;
|
||||||
|
|
||||||
|
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 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)
|
||||||
|
|||||||
@@ -14,21 +14,25 @@ namespace PrivaPub.Tests.Support
|
|||||||
{
|
{
|
||||||
readonly RSA _key = RSA.Create(2048);
|
readonly RSA _key = RSA.Create(2048);
|
||||||
|
|
||||||
public RemoteActor(Peer peer, string name, string origin = default)
|
public RemoteActor(Peer peer, string name, string origin = default, string type = "Person")
|
||||||
{
|
{
|
||||||
Name = $"{name}{Guid.NewGuid():N}"[..20];
|
Name = $"{name}{Guid.NewGuid():N}"[..20];
|
||||||
Id = $"{origin ?? peer.A}/users/{Name}";
|
Id = $"{origin ?? peer.A}/users/{Name}";
|
||||||
|
Type = type;
|
||||||
peer.Serve($"/users/{Name}", Document().ToJsonString());
|
peer.Serve($"/users/{Name}", Document().ToJsonString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string Type { get; }
|
||||||
|
|
||||||
public string Name { get; }
|
public string Name { get; }
|
||||||
public string Id { get; }
|
public string Id { get; }
|
||||||
public string KeyId => Id + "#main-key";
|
public string KeyId => Id + "#main-key";
|
||||||
|
public string SharedInbox => Id.Split("/users/")[0] + "/inbox";
|
||||||
|
|
||||||
public JsonObject Document() => new()
|
public JsonObject Document() => new()
|
||||||
{
|
{
|
||||||
["id"] = Id,
|
["id"] = Id,
|
||||||
["type"] = "Person",
|
["type"] = Type,
|
||||||
["preferredUsername"] = Name,
|
["preferredUsername"] = Name,
|
||||||
["inbox"] = Id + "/inbox",
|
["inbox"] = Id + "/inbox",
|
||||||
["followers"] = Id + "/followers",
|
["followers"] = Id + "/followers",
|
||||||
@@ -40,6 +44,21 @@ namespace PrivaPub.Tests.Support
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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}\"";
|
||||||
|
return context.Request;
|
||||||
|
}
|
||||||
|
|
||||||
public HttpRequest Post(string host, string path, JsonNode activity)
|
public HttpRequest Post(string host, string path, JsonNode activity)
|
||||||
{
|
{
|
||||||
var body = Encoding.UTF8.GetBytes(activity.ToJsonString());
|
var body = Encoding.UTF8.GetBytes(activity.ToJsonString());
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
namespace PrivaPub.Api.Mastodon.Auth
|
||||||
|
{
|
||||||
|
public static class MastodonScopes
|
||||||
|
{
|
||||||
|
public static readonly string[] All =
|
||||||
|
{
|
||||||
|
"read", "write", "follow", "push", "profile",
|
||||||
|
"read:accounts", "read:blocks", "read:bookmarks", "read:favourites", "read:filters", "read:follows", "read:lists",
|
||||||
|
"read:mutes", "read:notifications", "read:search", "read:statuses",
|
||||||
|
"write:accounts", "write:blocks", "write:bookmarks", "write:conversations", "write:favourites", "write:filters",
|
||||||
|
"write:follows", "write:lists", "write:media", "write:mutes", "write:notifications", "write:reports", "write:statuses"
|
||||||
|
};
|
||||||
|
|
||||||
|
public static IReadOnlyList<string> Parse(string scopes)
|
||||||
|
{
|
||||||
|
var parsed = (scopes ?? string.Empty).Split(new[] { ' ', '+', ',' }, StringSplitOptions.RemoveEmptyEntries)
|
||||||
|
.Where(s => All.Contains(s))
|
||||||
|
.Distinct()
|
||||||
|
.ToList();
|
||||||
|
return parsed.Count == 0 ? new[] { "read" } : parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool Grants(IEnumerable<string> granted, string required)
|
||||||
|
{
|
||||||
|
var scopes = granted as ICollection<string> ?? granted.ToList();
|
||||||
|
if (scopes.Contains(required))
|
||||||
|
return true;
|
||||||
|
var colon = required.IndexOf(':');
|
||||||
|
if (colon > 0 && scopes.Contains(required[..colon]))
|
||||||
|
return true;
|
||||||
|
return required.StartsWith("read:follows") || required.StartsWith("write:follows") || required.StartsWith("write:blocks")
|
||||||
|
|| required.StartsWith("read:blocks") || required.StartsWith("write:mutes") || required.StartsWith("read:mutes")
|
||||||
|
? scopes.Contains("follow")
|
||||||
|
: false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
using OpenIddict.Abstractions;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Auth
|
||||||
|
{
|
||||||
|
public class OAuthPruner : BackgroundService
|
||||||
|
{
|
||||||
|
static readonly TimeSpan Interval = TimeSpan.FromHours(6);
|
||||||
|
static readonly TimeSpan Threshold = TimeSpan.FromDays(14);
|
||||||
|
|
||||||
|
readonly IServiceProvider _services;
|
||||||
|
readonly ILogger<OAuthPruner> _logger;
|
||||||
|
|
||||||
|
public OAuthPruner(IServiceProvider services, ILogger<OAuthPruner> logger)
|
||||||
|
{
|
||||||
|
_services = services;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await Task.Delay(Interval, stoppingToken);
|
||||||
|
using var scope = _services.CreateScope();
|
||||||
|
var threshold = DateTimeOffset.UtcNow - Threshold;
|
||||||
|
var tokens = await scope.ServiceProvider.GetRequiredService<IOpenIddictTokenManager>().PruneAsync(threshold, stoppingToken);
|
||||||
|
var authorizations = await scope.ServiceProvider.GetRequiredService<IOpenIddictAuthorizationManager>().PruneAsync(threshold, stoppingToken);
|
||||||
|
if (tokens + authorizations > 0)
|
||||||
|
_logger.LogInformation("{Service} removed {Tokens} tokens and {Authorizations} authorizations", nameof(OAuthPruner), tokens, authorizations);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "{Service} pass failed", nameof(OAuthPruner));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
using Microsoft.AspNetCore;
|
||||||
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using OpenIddict.Abstractions;
|
||||||
|
using OpenIddict.Server;
|
||||||
|
using OpenIddict.Server.AspNetCore;
|
||||||
|
|
||||||
|
using PrivaPub.Models;
|
||||||
|
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
using static OpenIddict.Server.OpenIddictServerEvents;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Auth
|
||||||
|
{
|
||||||
|
public static class OAuthSetup
|
||||||
|
{
|
||||||
|
public const string LoginScheme = "PrivaPub.OAuth";
|
||||||
|
public const string OutOfBand = "urn:ietf:wg:oauth:2.0:oob";
|
||||||
|
|
||||||
|
public static IServiceCollection PrivaPubOAuth(this IServiceCollection services, IWebHostEnvironment environment)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IMongoDatabase>(_ => DB.Default.Database());
|
||||||
|
services.AddHostedService<OAuthPruner>();
|
||||||
|
|
||||||
|
services.AddAuthentication()
|
||||||
|
.AddCookie(LoginScheme, options =>
|
||||||
|
{
|
||||||
|
options.Cookie.Name = "privapub.oauth";
|
||||||
|
options.Cookie.Path = "/oauth";
|
||||||
|
options.Cookie.HttpOnly = true;
|
||||||
|
options.Cookie.SameSite = SameSiteMode.Lax;
|
||||||
|
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
|
||||||
|
options.ExpireTimeSpan = TimeSpan.FromMinutes(15);
|
||||||
|
options.SlidingExpiration = false;
|
||||||
|
options.LoginPath = "/oauth/login";
|
||||||
|
});
|
||||||
|
|
||||||
|
services.AddOpenIddict()
|
||||||
|
.AddCore(options => options.UseMongoDb())
|
||||||
|
.AddServer(options =>
|
||||||
|
{
|
||||||
|
options.SetAuthorizationEndpointUris("/oauth/authorize")
|
||||||
|
.SetTokenEndpointUris("/oauth/token")
|
||||||
|
.SetRevocationEndpointUris("/oauth/revoke")
|
||||||
|
.SetConfigurationEndpointUris("/.well-known/openid-configuration", "/.well-known/oauth-authorization-server");
|
||||||
|
options.AllowAuthorizationCodeFlow().AllowClientCredentialsFlow();
|
||||||
|
options.RegisterScopes(MastodonScopes.All);
|
||||||
|
options.UseReferenceAccessTokens();
|
||||||
|
options.SetAccessTokenLifetime(null);
|
||||||
|
options.SetAuthorizationCodeLifetime(TimeSpan.FromMinutes(10));
|
||||||
|
options.DisableAccessTokenEncryption();
|
||||||
|
|
||||||
|
var aspNetCore = options.UseAspNetCore()
|
||||||
|
.EnableAuthorizationEndpointPassthrough()
|
||||||
|
.EnableTokenEndpointPassthrough();
|
||||||
|
if (!environment.IsProduction())
|
||||||
|
aspNetCore.DisableTransportSecurityRequirement();
|
||||||
|
|
||||||
|
options.AddEventHandler<ApplyAuthorizationResponseContext>(builder => builder.UseInlineHandler(OutOfBandCode)
|
||||||
|
.SetOrder(OpenIddictServerAspNetCoreHandlers.Authentication.ProcessFormPostResponse.Descriptor.Order - 1_000)
|
||||||
|
.SetType(OpenIddictServerHandlerType.Custom));
|
||||||
|
options.AddEventHandler<ApplyTokenResponseContext>(builder => builder.UseInlineHandler(context =>
|
||||||
|
{
|
||||||
|
if (context.Response.AccessToken != default)
|
||||||
|
context.Response["created_at"] = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||||
|
return default;
|
||||||
|
}));
|
||||||
|
})
|
||||||
|
.AddValidation(options =>
|
||||||
|
{
|
||||||
|
options.UseLocalServer();
|
||||||
|
options.UseAspNetCore();
|
||||||
|
});
|
||||||
|
|
||||||
|
services.AddOptions<OpenIddictServerOptions>().Configure<IOptionsMonitor<AppConfiguration>>((options, app) =>
|
||||||
|
{
|
||||||
|
options.Issuer = new Uri(app.CurrentValue.BackendBaseAddress.TrimEnd('/') + "/");
|
||||||
|
options.SigningCredentials.Add(new SigningCredentials(OidcKey.Load(OidcKey.Signing), SecurityAlgorithms.RsaSha256));
|
||||||
|
options.EncryptionCredentials.Add(new EncryptingCredentials(OidcKey.Load(OidcKey.Encryption),
|
||||||
|
SecurityAlgorithms.RsaOAEP, SecurityAlgorithms.Aes256CbcHmacSha512));
|
||||||
|
});
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async ValueTask OutOfBandCode(ApplyAuthorizationResponseContext context)
|
||||||
|
{
|
||||||
|
if (context.RedirectUri != OutOfBand || context.Response.Code == default)
|
||||||
|
return;
|
||||||
|
var http = context.Transaction.GetHttpRequest()?.HttpContext;
|
||||||
|
if (http == default)
|
||||||
|
return;
|
||||||
|
var code = WebUtility.HtmlEncode(context.Response.Code);
|
||||||
|
http.Response.StatusCode = StatusCodes.Status200OK;
|
||||||
|
http.Response.ContentType = "text/html; charset=utf-8";
|
||||||
|
http.Response.Headers["Content-Security-Policy"] = "default-src 'none'; style-src 'unsafe-inline'";
|
||||||
|
http.Response.Headers["Cache-Control"] = "no-store";
|
||||||
|
await http.Response.WriteAsync($$"""
|
||||||
|
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>Authorization code</title><style>body{font:16px/1.5 system-ui,sans-serif;max-width:480px;margin:48px auto;padding:0 16px}
|
||||||
|
code{display:block;font-size:1.1rem;padding:12px;border:1px solid #ccc;word-break:break-all}</style></head>
|
||||||
|
<body><h1>Authorization code</h1><p>Copy this code into the application:</p><code>{{code}}</code></body></html>
|
||||||
|
""");
|
||||||
|
context.HandleRequest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Auth
|
||||||
|
{
|
||||||
|
public class OidcKey : Entity
|
||||||
|
{
|
||||||
|
public string Use { get; set; }
|
||||||
|
public string PrivateKeyPem { get; set; }
|
||||||
|
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||||
|
|
||||||
|
public const string Signing = "signing";
|
||||||
|
public const string Encryption = "encryption";
|
||||||
|
|
||||||
|
public static RsaSecurityKey Load(string use)
|
||||||
|
{
|
||||||
|
var key = DB.Default.Find<OidcKey>().Match(k => k.Use == use).Sort(k => k.CreatedAt, Order.Ascending).ExecuteFirstAsync().GetAwaiter().GetResult();
|
||||||
|
if (key == default)
|
||||||
|
{
|
||||||
|
using var created = RSA.Create(2048);
|
||||||
|
key = new OidcKey { Use = use, PrivateKeyPem = created.ExportRSAPrivateKeyPem() };
|
||||||
|
DB.Default.SaveAsync(key).GetAwaiter().GetResult();
|
||||||
|
key = DB.Default.Find<OidcKey>().Match(k => k.Use == use).Sort(k => k.CreatedAt, Order.Ascending).ExecuteFirstAsync().GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
var rsa = RSA.Create();
|
||||||
|
rsa.ImportFromPem(key.PrivateKeyPem);
|
||||||
|
return new RsaSecurityKey(rsa) { KeyId = key.ID };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
using Microsoft.AspNetCore;
|
||||||
|
using Microsoft.AspNetCore.Authentication;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
|
using OpenIddict.Abstractions;
|
||||||
|
using OpenIddict.Server.AspNetCore;
|
||||||
|
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Security.Claims;
|
||||||
|
|
||||||
|
using static OpenIddict.Abstractions.OpenIddictConstants;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Auth
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
public class TokenController : ControllerBase
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
|
||||||
|
public TokenController(DbEntities dbEntities)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/oauth/token"), IgnoreAntiforgeryToken, Produces("application/json")]
|
||||||
|
public async Task<IActionResult> Exchange(CancellationToken token)
|
||||||
|
{
|
||||||
|
var request = HttpContext.GetOpenIddictServerRequest();
|
||||||
|
if (request == default)
|
||||||
|
return BadRequest();
|
||||||
|
|
||||||
|
if (request.IsAuthorizationCodeGrantType())
|
||||||
|
{
|
||||||
|
var principal = (await HttpContext.AuthenticateAsync(OpenIddictServerAspNetCoreDefaults.AuthenticationScheme)).Principal;
|
||||||
|
if (principal == default || !await Usable(principal.GetClaim(Claims.Subject), token))
|
||||||
|
return Refuse(Errors.InvalidGrant, "The persona can no longer be used.");
|
||||||
|
return SignIn(principal, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.IsClientCredentialsGrantType())
|
||||||
|
{
|
||||||
|
var identity = new ClaimsIdentity(TokenValidationParameters.DefaultAuthenticationType, Claims.Name, Claims.Role);
|
||||||
|
identity.SetClaim(Claims.Subject, "app:" + request.ClientId);
|
||||||
|
identity.SetScopes(MastodonScopes.Parse(request.Scope));
|
||||||
|
identity.SetDestinations(_ => new[] { Destinations.AccessToken });
|
||||||
|
return SignIn(new ClaimsPrincipal(identity), OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Refuse(Errors.UnsupportedGrantType, "The grant type is not supported.");
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<bool> Usable(string avatarId, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(avatarId))
|
||||||
|
return false;
|
||||||
|
var link = await _dbEntities.RootToAvatars.Match(r => r.AvatarId == avatarId).ExecuteFirstAsync(token);
|
||||||
|
var root = link == default ? default : await _dbEntities.RootUsers.MatchID(link.RootId).ExecuteFirstAsync(token);
|
||||||
|
var avatar = await _dbEntities.Avatars.MatchID(avatarId).ExecuteFirstAsync(token);
|
||||||
|
return root is { IsBanned: false, DeletedAt: null } && avatar is { DeletionAt: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
ForbidResult Refuse(string error, string description) => Forbid(new AuthenticationProperties(new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
[OpenIddictServerAspNetCoreConstants.Properties.Error] = error,
|
||||||
|
[OpenIddictServerAspNetCoreConstants.Properties.ErrorDescription] = description
|
||||||
|
}), OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,399 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Entities;
|
||||||
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||||
|
using PrivaPub.Api.Mastodon.Mappers;
|
||||||
|
using PrivaPub.Domain.Media;
|
||||||
|
using PrivaPub.Domain.Privacy;
|
||||||
|
using PrivaPub.Domain.Relationships;
|
||||||
|
using PrivaPub.Domain.Social;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Controllers
|
||||||
|
{
|
||||||
|
public class AccountsController : MastodonController
|
||||||
|
{
|
||||||
|
readonly MastodonMapper _mapper;
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
readonly IFollowService _follows;
|
||||||
|
readonly IOutboxPublisher _outbox;
|
||||||
|
readonly IRelationshipService _relationships;
|
||||||
|
|
||||||
|
public AccountsController(MastodonMapper mapper, DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors,
|
||||||
|
IFollowService follows, IOutboxPublisher outbox, IRelationshipService relationships)
|
||||||
|
{
|
||||||
|
_relationships = relationships;
|
||||||
|
_mapper = mapper;
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
_follows = follows;
|
||||||
|
_outbox = outbox;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/accounts/verify_credentials"), Scope("read:accounts")]
|
||||||
|
public async Task<IActionResult> VerifyCredentials(CancellationToken token) => Json(await _mapper.Local(Me, withSource: true, token));
|
||||||
|
|
||||||
|
[HttpPatch("/api/v1/accounts/update_credentials"), Scope("write:accounts"), RequestSizeLimit(20 * 1024 * 1024),
|
||||||
|
RequestFormLimits(MultipartBodyLengthLimit = 20 * 1024 * 1024)]
|
||||||
|
public async Task<IActionResult> UpdateCredentials([FromServices] IMediaService media, CancellationToken token)
|
||||||
|
{
|
||||||
|
var avatar = await _dbEntities.Avatars.MatchID(Me.Id).ExecuteFirstAsync(token);
|
||||||
|
if (Request.HasFormContentType)
|
||||||
|
{
|
||||||
|
var form = await Request.ReadFormAsync(token);
|
||||||
|
if (form.Files["avatar"] is { } picture && await media.ProfileImage(picture, 400, 400, token) is { } pictureUrl)
|
||||||
|
avatar.PictureURL = pictureUrl;
|
||||||
|
if (form.Files["header"] is { } header && await media.ProfileImage(header, 1500, 500, token) is { } headerUrl)
|
||||||
|
avatar.ThumbnailURL = headerUrl;
|
||||||
|
}
|
||||||
|
if (Params.Has("display_name"))
|
||||||
|
avatar.Name = Params.Get("display_name")?.Trim();
|
||||||
|
if (Params.Has("note"))
|
||||||
|
avatar.Biography = Params.Get("note");
|
||||||
|
if (Params.Bool("locked") is { } locked)
|
||||||
|
avatar.Settings.IsLocked = locked;
|
||||||
|
if (Params.Bool("bot") is { } bot)
|
||||||
|
avatar.Settings.IsBot = bot;
|
||||||
|
if (Params.Bool("discoverable") is { } discoverable)
|
||||||
|
avatar.Settings.IsDiscoverable = discoverable;
|
||||||
|
if (Params.Bool("indexable") is { } indexable)
|
||||||
|
avatar.Settings.IsIndexable = indexable;
|
||||||
|
if (Params.Bool("hide_collections") is { } hide)
|
||||||
|
avatar.Settings.HideCollections = hide;
|
||||||
|
if (Params.Get("source[privacy]") is { } privacy && privacy is "public" or "unlisted" or "private")
|
||||||
|
avatar.Settings.DefaultVisibility = privacy;
|
||||||
|
if (Params.Bool("source[sensitive]") is { } sensitive)
|
||||||
|
avatar.Settings.DefaultSensitive = sensitive;
|
||||||
|
if (Params.Has("source[language]"))
|
||||||
|
avatar.Settings.DefaultLanguage = Params.Get("source[language]");
|
||||||
|
var fields = new Dictionary<string, string>();
|
||||||
|
for (var i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
var name = Params.Get($"fields_attributes[{i}][name]") ?? Params.Get($"fields_attributes[][name]");
|
||||||
|
if (!string.IsNullOrWhiteSpace(name))
|
||||||
|
fields[name.Trim()] = Params.Get($"fields_attributes[{i}][value]")?.Trim() ?? string.Empty;
|
||||||
|
}
|
||||||
|
if (Params.List("fields_attributes[][name]").Count > 0)
|
||||||
|
{
|
||||||
|
var names = Params.List("fields_attributes[][name]");
|
||||||
|
var values = Params.List("fields_attributes[][value]");
|
||||||
|
fields = names.Select((n, i) => (n, v: i < values.Count ? values[i] : string.Empty)).Where(f => !string.IsNullOrWhiteSpace(f.n))
|
||||||
|
.Take(4).ToDictionary(f => f.n.Trim(), f => f.v.Trim());
|
||||||
|
}
|
||||||
|
if (fields.Count > 0 || Params.Has("fields_attributes[0][name]"))
|
||||||
|
avatar.Fields = fields;
|
||||||
|
avatar.UpdatedAt = DateTime.UtcNow;
|
||||||
|
await DB.Default.SaveAsync(avatar, token);
|
||||||
|
|
||||||
|
var actor = _localActors.FromAvatar(avatar);
|
||||||
|
await _outbox.PublishProfile(actor, token);
|
||||||
|
return Json(await _mapper.Local(actor, withSource: true, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/accounts/lookup"), Scope("read:accounts", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Lookup(CancellationToken token)
|
||||||
|
{
|
||||||
|
var acct = Params.Get("acct")?.Trim().TrimStart('@');
|
||||||
|
var parts = acct?.Split('@');
|
||||||
|
if (parts is not { Length: 1 or 2 } || string.IsNullOrEmpty(parts[0]))
|
||||||
|
return NotFoundError();
|
||||||
|
var localDomain = new Uri(_localActors.BaseAddress).Authority;
|
||||||
|
if (parts.Length == 1 || parts[1].Equals(localDomain, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUserName(parts[0], token);
|
||||||
|
return local is { IsFederated: true, IsCircle: false, Kind: not LocalActorKind.Application } ? Json(await _mapper.Local(local, false, token)) : NotFoundError();
|
||||||
|
}
|
||||||
|
var userName = parts[0];
|
||||||
|
var domain = parts[1].ToLowerInvariant();
|
||||||
|
var foreign = await _dbEntities.ForeignAvatars.Match(f => f.UserName == userName && f.Domain == domain).ExecuteFirstAsync(token);
|
||||||
|
return foreign == default ? NotFoundError() : Json(_mapper.Foreign(foreign));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/accounts/relationships"), Scope("read:follows")]
|
||||||
|
public async Task<IActionResult> Relationships(CancellationToken token)
|
||||||
|
{
|
||||||
|
var relationships = new List<Relationship>();
|
||||||
|
foreach (var id in Params.List("id").Distinct().Take(40))
|
||||||
|
relationships.Add(await Relationship(id, token));
|
||||||
|
return Json(relationships);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/accounts/search"), Scope("read:accounts")]
|
||||||
|
public async Task<IActionResult> Search([FromServices] AccountSearch search, CancellationToken token) =>
|
||||||
|
Json(await search.Find(Params.Get("q"), Params.Bool("resolve") == true && MyId != default, Limit(), token));
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/accounts/{id}"), Scope("read:accounts", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Get(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var account = await _mapper.Account(id, token);
|
||||||
|
return account == default ? NotFoundError() : Json(account);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/accounts/{id}/statuses"), Scope("read:statuses", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Statuses(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, remote) = await Find(id, token);
|
||||||
|
if (local == default && remote == default)
|
||||||
|
return NotFoundError();
|
||||||
|
if (Params.Bool("pinned") == true)
|
||||||
|
{
|
||||||
|
if (local == default)
|
||||||
|
return Json(Array.Empty<Status>());
|
||||||
|
var pinIds = (await DB.Default.Find<Pin>().Match(p => p.AvatarId == local.Id).Sort(p => p.ID, Order.Descending).ExecuteAsync(token)).Select(p => p.PostId).ToList();
|
||||||
|
var pinnedPosts = await _dbEntities.Posts.Match(p => pinIds.Contains(p.ID) && !p.DeletedAt.HasValue).Match(VisibilityPolicy.IsPublic).ExecuteAsync(token);
|
||||||
|
return Json(await _mapper.Statuses(pinIds.Select(id => pinnedPosts.FirstOrDefault(p => p.ID == id)).Where(p => p != default).ToList(), MyId, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
var query = local != default
|
||||||
|
? _dbEntities.Posts.Match(p => p.GroupUserId == local.Id && !p.IsFederatedCopy && !p.DeletedAt.HasValue)
|
||||||
|
: _dbEntities.Posts.Match(p => p.ActorURI == remote.ActorURI && p.IsFederatedCopy && !p.DeletedAt.HasValue);
|
||||||
|
var viewerFollows = MyId != default && local != default
|
||||||
|
&& await _dbEntities.Followings.Match(f => f.AvatarId == MyId && f.TargetAccountId == local.Id && f.State == FollowState.Accepted).ExecuteAnyAsync(token);
|
||||||
|
if (MyId != id)
|
||||||
|
query.Match(viewerFollows
|
||||||
|
? p => p.Visibility == PostVisibility.Public || p.Visibility == PostVisibility.Unlisted || p.Visibility == PostVisibility.FollowersOnly
|
||||||
|
: p => p.Visibility == PostVisibility.Public || p.Visibility == PostVisibility.Unlisted);
|
||||||
|
else
|
||||||
|
query.Match(p => p.Visibility != PostVisibility.LocalGeo);
|
||||||
|
if (Params.Bool("exclude_replies") == true)
|
||||||
|
query.Match(p => p.InReplyToURI == null);
|
||||||
|
if (Params.Bool("exclude_reblogs") == true)
|
||||||
|
query.Match(p => p.ReblogOfPostId == null);
|
||||||
|
if (Params.Bool("only_media") == true)
|
||||||
|
query.Match(p => p.Media.Count > 0);
|
||||||
|
if (Params.Get("tagged") is { } tag)
|
||||||
|
query.Match(p => p.Tags.Contains(tag.ToLowerInvariant()));
|
||||||
|
|
||||||
|
var posts = await Page.From(Params, Limit()).Fetch(query, p => p.ID, token);
|
||||||
|
var statuses = await _mapper.Statuses(posts, MyId, token);
|
||||||
|
Link($"/api/v1/accounts/{id}/statuses", posts.LastOrDefault()?.ID, posts.FirstOrDefault()?.ID);
|
||||||
|
return Json(statuses);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/accounts/{id}/followers"), Scope("read:accounts", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Followers(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, _) = await Find(id, token);
|
||||||
|
if (local == default || local.Id != MyId)
|
||||||
|
return Json(Array.Empty<Account>());
|
||||||
|
var followers = await _dbEntities.Followers.Match(f => f.LocalActorId == local.Id && f.LocalActorKind == local.Kind && f.IsAccepted)
|
||||||
|
.Sort(f => f.ID, Order.Descending).Limit(Limit(40, 80)).ExecuteAsync(token);
|
||||||
|
return Json(await AccountsFor(followers.Select(f => f.ActorURI), token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/accounts/{id}/following"), Scope("read:accounts", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Following(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, _) = await Find(id, token);
|
||||||
|
if (local == default || local.Id != MyId)
|
||||||
|
return Json(Array.Empty<Account>());
|
||||||
|
var following = await _dbEntities.Followings.Match(f => f.AvatarId == local.Id && f.State == FollowState.Accepted)
|
||||||
|
.Sort(f => f.ID, Order.Descending).Limit(Limit(40, 80)).ExecuteAsync(token);
|
||||||
|
var accounts = await _mapper.Accounts(following.Select(f => f.TargetAccountId), token);
|
||||||
|
return Json(following.Select(f => accounts.GetValueOrDefault(f.TargetAccountId)).Where(a => a != default).ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/accounts/{id}/follow"), Scope("write:follows")]
|
||||||
|
public async Task<IActionResult> Follow(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, remote) = await Find(id, token);
|
||||||
|
if (local == default && remote == default)
|
||||||
|
return NotFoundError();
|
||||||
|
var following = await _follows.FollowAs(Me, local?.Uri ?? remote.ActorURI, Params.Bool("reblogs") != false, token);
|
||||||
|
return following == default ? Error(StatusCodes.Status403Forbidden, "This action is not allowed") : Json(await Relationship(id, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/accounts/{id}/unfollow"), Scope("write:follows")]
|
||||||
|
public async Task<IActionResult> Unfollow(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, remote) = await Find(id, token);
|
||||||
|
if (local == default && remote == default)
|
||||||
|
return NotFoundError();
|
||||||
|
await _follows.UnfollowAs(Me, local?.Uri ?? remote.ActorURI, token);
|
||||||
|
return Json(await Relationship(id, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/accounts/{id}/remove_from_followers"), Scope("write:follows")]
|
||||||
|
public async Task<IActionResult> RemoveFromFollowers(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, remote) = await Find(id, token);
|
||||||
|
var uri = local?.Uri ?? remote?.ActorURI;
|
||||||
|
if (uri == default)
|
||||||
|
return NotFoundError();
|
||||||
|
await DB.Default.DeleteAsync<Follower>(f => f.LocalActorId == Me.Id && f.ActorURI == uri);
|
||||||
|
if (local != default)
|
||||||
|
await DB.Default.DeleteAsync<Following>(f => f.AvatarId == local.Id && f.TargetActorURI == Me.Uri);
|
||||||
|
return Json(await Relationship(id, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/accounts/{id}/block"), Scope("write:blocks")]
|
||||||
|
public async Task<IActionResult> Block(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, remote) = await Find(id, token);
|
||||||
|
if (local == default && remote == default || local?.Id == MyId)
|
||||||
|
return NotFoundError();
|
||||||
|
await _relationships.Block(Me, local?.Uri ?? remote.ActorURI, id, token);
|
||||||
|
return Json(await Relationship(id, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/accounts/{id}/unblock"), Scope("write:blocks")]
|
||||||
|
public async Task<IActionResult> Unblock(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, remote) = await Find(id, token);
|
||||||
|
if (local == default && remote == default)
|
||||||
|
return NotFoundError();
|
||||||
|
await _relationships.Unblock(Me, local?.Uri ?? remote.ActorURI, token);
|
||||||
|
return Json(await Relationship(id, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/accounts/{id}/mute"), Scope("write:mutes")]
|
||||||
|
public async Task<IActionResult> Mute(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, remote) = await Find(id, token);
|
||||||
|
if (local == default && remote == default || local?.Id == MyId)
|
||||||
|
return NotFoundError();
|
||||||
|
var duration = Params.Int("duration") is { } seconds and > 0 ? TimeSpan.FromSeconds(seconds) : (TimeSpan?)null;
|
||||||
|
await _relationships.Mute(Me, local?.Uri ?? remote.ActorURI, id, Params.Bool("notifications") != false, duration, token);
|
||||||
|
return Json(await Relationship(id, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/accounts/{id}/unmute"), Scope("write:mutes")]
|
||||||
|
public async Task<IActionResult> Unmute(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, remote) = await Find(id, token);
|
||||||
|
if (local == default && remote == default)
|
||||||
|
return NotFoundError();
|
||||||
|
await _relationships.Unmute(Me, local?.Uri ?? remote.ActorURI, token);
|
||||||
|
return Json(await Relationship(id, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/blocks"), Scope("read:blocks")]
|
||||||
|
public async Task<IActionResult> Blocks(CancellationToken token)
|
||||||
|
{
|
||||||
|
var blocks = await Page.From(Params, Limit(40, 80)).Fetch(DB.Default.Find<Block>().Match(b => b.AvatarId == MyId), b => b.ID, token);
|
||||||
|
var accounts = await _mapper.Accounts(blocks.Select(b => b.TargetAccountId), token);
|
||||||
|
Link("/api/v1/blocks", blocks.LastOrDefault()?.ID, blocks.FirstOrDefault()?.ID);
|
||||||
|
return Json(blocks.Select(b => accounts.GetValueOrDefault(b.TargetAccountId)).Where(a => a != default).ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/mutes"), Scope("read:mutes")]
|
||||||
|
public async Task<IActionResult> Mutes(CancellationToken token)
|
||||||
|
{
|
||||||
|
var mutes = await Page.From(Params, Limit(40, 80)).Fetch(DB.Default.Find<Mute>().Match(m => m.AvatarId == MyId), m => m.ID, token);
|
||||||
|
var accounts = await _mapper.Accounts(mutes.Select(m => m.TargetAccountId), token);
|
||||||
|
Link("/api/v1/mutes", mutes.LastOrDefault()?.ID, mutes.FirstOrDefault()?.ID);
|
||||||
|
return Json(mutes.Select(m => accounts.GetValueOrDefault(m.TargetAccountId)).Where(a => a != default).ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/domain_blocks"), Scope("read:blocks")]
|
||||||
|
public async Task<IActionResult> DomainBlocks(CancellationToken token) =>
|
||||||
|
Json((await DB.Default.Find<AccountDomainBlock>().Match(b => b.AvatarId == MyId).Sort(b => b.Domain, Order.Ascending).ExecuteAsync(token)).Select(b => b.Domain).ToList());
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/domain_blocks"), Scope("write:blocks")]
|
||||||
|
public async Task<IActionResult> BlockDomain(CancellationToken token)
|
||||||
|
{
|
||||||
|
await _relationships.BlockDomain(Me, Params.Get("domain"), token);
|
||||||
|
return Json(new { });
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("/api/v1/domain_blocks"), Scope("write:blocks")]
|
||||||
|
public async Task<IActionResult> UnblockDomain(CancellationToken token)
|
||||||
|
{
|
||||||
|
await _relationships.UnblockDomain(Me, Params.Get("domain"), token);
|
||||||
|
return Json(new { });
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/follow_requests"), Scope("read:follows")]
|
||||||
|
public async Task<IActionResult> FollowRequests(CancellationToken token)
|
||||||
|
{
|
||||||
|
var requests = await _dbEntities.Followers.Match(f => f.LocalActorId == Me.Id && f.LocalActorKind == LocalActorKind.Person && !f.IsAccepted)
|
||||||
|
.Sort(f => f.ID, Order.Descending).Limit(Limit(40, 80)).ExecuteAsync(token);
|
||||||
|
return Json(await AccountsFor(requests.Select(r => r.ActorURI), token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/follow_requests/{id}/authorize"), Scope("write:follows")]
|
||||||
|
public async Task<IActionResult> Authorize(string id, CancellationToken token) =>
|
||||||
|
await _follows.Decide(Me, id, accept: true, token) ? Json(await Relationship(id, token)) : NotFoundError();
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/follow_requests/{id}/reject"), Scope("write:follows")]
|
||||||
|
public async Task<IActionResult> Reject(string id, CancellationToken token) =>
|
||||||
|
await _follows.Decide(Me, id, accept: false, token) ? Json(await Relationship(id, token)) : NotFoundError();
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/accounts/{id}/featured_tags"), Scope("read:accounts", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public IActionResult FeaturedTags(string id) => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/accounts/{id}/lists"), Scope("read:lists")]
|
||||||
|
public IActionResult Lists(string id) => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/accounts/familiar_followers"), Scope("read:follows")]
|
||||||
|
public IActionResult FamiliarFollowers() => Json(Params.List("id").Select(id => new { id, accounts = Array.Empty<Account>() }).ToList());
|
||||||
|
|
||||||
|
async Task<List<Account>> AccountsFor(IEnumerable<string> actorUris, CancellationToken token)
|
||||||
|
{
|
||||||
|
var accounts = new List<Account>();
|
||||||
|
foreach (var uri in actorUris)
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUri(uri, token);
|
||||||
|
if (local != default)
|
||||||
|
{
|
||||||
|
accounts.Add(await _mapper.Local(local, false, token));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var foreign = await _dbEntities.ForeignAvatars.Match(f => f.ActorURI == uri).ExecuteFirstAsync(token);
|
||||||
|
if (foreign != default)
|
||||||
|
accounts.Add(_mapper.Foreign(foreign));
|
||||||
|
}
|
||||||
|
return accounts;
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<(LocalActor Local, ForeignAvatar Remote)> Find(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var avatar = await _dbEntities.Avatars.MatchID(id).ExecuteFirstAsync(token);
|
||||||
|
if (avatar is { DeletionAt: null })
|
||||||
|
return (_localActors.FromAvatar(avatar), default);
|
||||||
|
var group = await _dbEntities.Groups.MatchID(id).ExecuteFirstAsync(token);
|
||||||
|
if (group is { DeletionAt: null } && _localActors.FromGroup(group) is { IsFederated: true, IsCircle: false } community)
|
||||||
|
return (community, default);
|
||||||
|
return (default, await _dbEntities.ForeignAvatars.MatchID(id).ExecuteFirstAsync(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<Relationship> Relationship(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, remote) = await Find(id, token);
|
||||||
|
var uri = local?.Uri ?? remote?.ActorURI;
|
||||||
|
var following = uri == default ? default : await _dbEntities.Followings.Match(f => f.AvatarId == MyId && f.TargetActorURI == uri).ExecuteFirstAsync(token);
|
||||||
|
var followedBy = uri == default ? default : await _dbEntities.Followers.Match(f => f.LocalActorId == MyId && f.ActorURI == uri).ExecuteFirstAsync(token);
|
||||||
|
var blocking = uri != default && await DB.Default.Find<Block>().Match(b => b.AvatarId == MyId && b.TargetActorURI == uri).ExecuteAnyAsync(token);
|
||||||
|
var mute = uri == default ? default : await DB.Default.Find<Mute>().Match(m => m.AvatarId == MyId && m.TargetActorURI == uri).ExecuteFirstAsync(token);
|
||||||
|
var blockedBy = local != default && await DB.Default.Find<Block>().Match(b => b.AvatarId == local.Id && b.TargetActorURI == Me.Uri).ExecuteAnyAsync(token);
|
||||||
|
var domainBlocked = remote != default && await DB.Default.Find<AccountDomainBlock>().Match(b => b.AvatarId == MyId && b.Domain == remote.Domain).ExecuteAnyAsync(token);
|
||||||
|
return new Relationship
|
||||||
|
{
|
||||||
|
Id = id,
|
||||||
|
Blocking = blocking,
|
||||||
|
BlockedBy = blockedBy,
|
||||||
|
Muting = mute != default && (mute.ExpiresAt == default || mute.ExpiresAt > DateTime.UtcNow),
|
||||||
|
MutingNotifications = mute?.HideNotifications == true,
|
||||||
|
DomainBlocking = domainBlocked,
|
||||||
|
Following = following?.State == FollowState.Accepted,
|
||||||
|
Requested = following?.State == FollowState.Requested,
|
||||||
|
ShowingReblogs = following?.ShowReblogs ?? false,
|
||||||
|
FollowedBy = followedBy?.IsAccepted == true,
|
||||||
|
RequestedBy = followedBy is { IsAccepted: false }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
using OpenIddict.Abstractions;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Auth;
|
||||||
|
using PrivaPub.Api.Mastodon.Entities;
|
||||||
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||||
|
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
|
using static OpenIddict.Abstractions.OpenIddictConstants;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Controllers
|
||||||
|
{
|
||||||
|
public class AppsController : MastodonController
|
||||||
|
{
|
||||||
|
readonly IOpenIddictApplicationManager _applications;
|
||||||
|
|
||||||
|
public AppsController(IOpenIddictApplicationManager applications)
|
||||||
|
{
|
||||||
|
_applications = applications;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/apps"), AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Create(CancellationToken token)
|
||||||
|
{
|
||||||
|
var name = Params.Get("client_name")?.Trim();
|
||||||
|
if (string.IsNullOrEmpty(name) || name.Length > 200)
|
||||||
|
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: Application name can't be blank");
|
||||||
|
|
||||||
|
var redirects = Params.List("redirect_uris").SelectMany(r => r.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)).Distinct().ToList();
|
||||||
|
if (redirects.Count == 0 || redirects.Any(r => !Uri.TryCreate(r, UriKind.Absolute, out var uri) || uri.Scheme is "javascript" or "data"))
|
||||||
|
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: Redirect URI must be an absolute URI.");
|
||||||
|
|
||||||
|
var scopes = MastodonScopes.Parse(string.Join(' ', Params.List("scopes")));
|
||||||
|
var descriptor = new OpenIddictApplicationDescriptor
|
||||||
|
{
|
||||||
|
ClientId = Secret(32),
|
||||||
|
ClientSecret = Secret(32),
|
||||||
|
ClientType = ClientTypes.Confidential,
|
||||||
|
ConsentType = ConsentTypes.Explicit,
|
||||||
|
DisplayName = name,
|
||||||
|
Permissions =
|
||||||
|
{
|
||||||
|
Permissions.Endpoints.Authorization,
|
||||||
|
Permissions.Endpoints.Token,
|
||||||
|
Permissions.Endpoints.Revocation,
|
||||||
|
Permissions.GrantTypes.AuthorizationCode,
|
||||||
|
Permissions.GrantTypes.ClientCredentials,
|
||||||
|
Permissions.ResponseTypes.Code
|
||||||
|
}
|
||||||
|
};
|
||||||
|
foreach (var scope in scopes)
|
||||||
|
descriptor.Permissions.Add(Permissions.Prefixes.Scope + scope);
|
||||||
|
foreach (var redirect in redirects)
|
||||||
|
descriptor.RedirectUris.Add(new Uri(redirect));
|
||||||
|
var website = Params.Get("website");
|
||||||
|
if (Uri.TryCreate(website, UriKind.Absolute, out var site) && site.Scheme is "https" or "http")
|
||||||
|
descriptor.Properties["website"] = System.Text.Json.JsonSerializer.SerializeToElement(website);
|
||||||
|
|
||||||
|
var application = await _applications.CreateAsync(descriptor, token);
|
||||||
|
return Json(new Application
|
||||||
|
{
|
||||||
|
Id = await _applications.GetIdAsync(application, token),
|
||||||
|
Name = name,
|
||||||
|
Website = website,
|
||||||
|
Scopes = scopes.ToList(),
|
||||||
|
RedirectUris = redirects,
|
||||||
|
RedirectUri = string.Join("\n", redirects),
|
||||||
|
ClientId = descriptor.ClientId,
|
||||||
|
ClientSecret = descriptor.ClientSecret
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/apps/verify_credentials")]
|
||||||
|
public async Task<IActionResult> Verify(CancellationToken token)
|
||||||
|
{
|
||||||
|
var clientId = User.GetPresenters().FirstOrDefault() ?? User.GetClaim(Claims.ClientId);
|
||||||
|
var application = clientId == default ? default : await _applications.FindByClientIdAsync(clientId, token);
|
||||||
|
if (application == default)
|
||||||
|
return Error(StatusCodes.Status401Unauthorized, "The access token is invalid");
|
||||||
|
var properties = await _applications.GetPropertiesAsync(application, token);
|
||||||
|
return Json(new
|
||||||
|
{
|
||||||
|
id = await _applications.GetIdAsync(application, token),
|
||||||
|
name = await _applications.GetDisplayNameAsync(application, token),
|
||||||
|
website = properties.TryGetValue("website", out var website) ? website.GetString() : default,
|
||||||
|
scopes = User.GetScopes().ToList(),
|
||||||
|
redirect_uris = (await _applications.GetRedirectUrisAsync(application, token)).ToList(),
|
||||||
|
vapid_key = string.Empty
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static string Secret(int bytes) => Convert.ToBase64String(RandomNumberGenerator.GetBytes(bytes)).TrimEnd('=').Replace('+', '-').Replace('/', '_');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||||
|
using PrivaPub.Domain.Privacy;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Controllers
|
||||||
|
{
|
||||||
|
public class InstanceController : MastodonController
|
||||||
|
{
|
||||||
|
public const string Version = "4.2.0 (compatible; PrivaPub)";
|
||||||
|
const string Description = "A small ActivityPub server where one private login keeps several unlinkable public personas.";
|
||||||
|
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
|
||||||
|
public InstanceController(ILocalActorService localActors)
|
||||||
|
{
|
||||||
|
_localActors = localActors;
|
||||||
|
}
|
||||||
|
|
||||||
|
string Domain => new Uri(_localActors.BaseAddress).Authority;
|
||||||
|
|
||||||
|
static object Statuses => new { max_characters = 5000, max_media_attachments = 4, characters_reserved_per_url = 23 };
|
||||||
|
|
||||||
|
static object MediaAttachments => new
|
||||||
|
{
|
||||||
|
supported_mime_types = new[] { "image/jpeg", "image/png", "image/gif", "image/webp", "video/mp4", "video/webm", "audio/mpeg", "audio/ogg" },
|
||||||
|
image_size_limit = 16 * 1024 * 1024,
|
||||||
|
image_matrix_limit = 16_777_216,
|
||||||
|
video_size_limit = 99 * 1024 * 1024,
|
||||||
|
video_frame_rate_limit = 60,
|
||||||
|
video_matrix_limit = 2_304_000
|
||||||
|
};
|
||||||
|
|
||||||
|
static object Polls => new { max_options = 4, max_characters_per_option = 50, min_expiration = 300, max_expiration = 2_629_746 };
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/instance"), AllowAnonymous]
|
||||||
|
public async Task<IActionResult> V1(CancellationToken token)
|
||||||
|
{
|
||||||
|
var users = await DB.Default.CountAsync<Avatar>(a => !a.DeletionAt.HasValue, token);
|
||||||
|
var statuses = await DB.Default.CountAsync<PostEntity>(f => f.Where(p => !p.IsFederatedCopy) & f.Where(VisibilityPolicy.IsPublic), token);
|
||||||
|
var domains = (await DB.Default.Find<ForeignAvatar, string>().Project(a => a.Domain).ExecuteAsync(token)).Distinct().Count();
|
||||||
|
return Json(new
|
||||||
|
{
|
||||||
|
uri = Domain,
|
||||||
|
title = "PrivaPub",
|
||||||
|
short_description = Description,
|
||||||
|
description = Description,
|
||||||
|
email = string.Empty,
|
||||||
|
version = Version,
|
||||||
|
urls = new { streaming_api = $"wss://{Domain}" },
|
||||||
|
stats = new { user_count = users, status_count = statuses, domain_count = domains },
|
||||||
|
thumbnail = $"{_localActors.BaseAddress}/media/missing-header.png",
|
||||||
|
languages = new[] { "en" },
|
||||||
|
registrations = false,
|
||||||
|
approval_required = false,
|
||||||
|
invites_enabled = false,
|
||||||
|
configuration = new { accounts = new { max_featured_tags = 0 }, statuses = Statuses, media_attachments = MediaAttachments, polls = Polls },
|
||||||
|
contact_account = default(object),
|
||||||
|
rules = Array.Empty<object>()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v2/instance"), AllowAnonymous]
|
||||||
|
public IActionResult V2() => Json(new
|
||||||
|
{
|
||||||
|
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 } },
|
||||||
|
thumbnail = new { url = $"{_localActors.BaseAddress}/media/missing-header.png" },
|
||||||
|
languages = new[] { "en" },
|
||||||
|
configuration = new
|
||||||
|
{
|
||||||
|
urls = new { streaming = $"wss://{Domain}" },
|
||||||
|
accounts = new { max_featured_tags = 0, max_pinned_statuses = 0 },
|
||||||
|
statuses = Statuses,
|
||||||
|
media_attachments = MediaAttachments,
|
||||||
|
polls = Polls,
|
||||||
|
translation = new { enabled = false }
|
||||||
|
},
|
||||||
|
registrations = new { enabled = false, approval_required = false, message = default(string) },
|
||||||
|
contact = new { email = string.Empty, account = default(object) },
|
||||||
|
rules = Array.Empty<object>()
|
||||||
|
});
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/instance/peers"), AllowAnonymous]
|
||||||
|
public IActionResult Peers() => Json(Array.Empty<string>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/instance/activity"), AllowAnonymous]
|
||||||
|
public IActionResult Activity() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/instance/rules"), AllowAnonymous]
|
||||||
|
public IActionResult Rules() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/instance/extended_description"), AllowAnonymous]
|
||||||
|
public IActionResult ExtendedDescription() => Json(new { updated_at = MastodonJson.Day(DateTime.UtcNow), content = $"<p>{Description}</p>" });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||||
|
using PrivaPub.Domain.Media;
|
||||||
|
using PrivaPub.Models.Media;
|
||||||
|
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Controllers
|
||||||
|
{
|
||||||
|
public class MediaController : MastodonController
|
||||||
|
{
|
||||||
|
const long UploadLimit = 100L * 1024 * 1024;
|
||||||
|
|
||||||
|
readonly IMediaService _media;
|
||||||
|
readonly IMediaProxy _proxy;
|
||||||
|
|
||||||
|
public MediaController(IMediaService media, IMediaProxy proxy)
|
||||||
|
{
|
||||||
|
_media = media;
|
||||||
|
_proxy = proxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/media"), HttpPost("/api/v2/media"), Scope("write:media"), RequestSizeLimit(UploadLimit),
|
||||||
|
RequestFormLimits(MultipartBodyLengthLimit = UploadLimit)]
|
||||||
|
public async Task<IActionResult> Upload(CancellationToken token)
|
||||||
|
{
|
||||||
|
if (!Request.HasFormContentType)
|
||||||
|
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: File can't be blank");
|
||||||
|
var form = await Request.ReadFormAsync(token);
|
||||||
|
var outcome = await _media.Upload(Me, form.Files["file"], form["description"], form["focus"], token);
|
||||||
|
return outcome.Ok ? Json(View(outcome.Attachment)) : Error(outcome.Status, outcome.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/media/{id}"), Scope("write:media")]
|
||||||
|
public async Task<IActionResult> Get(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var attachment = await DB.Default.Find<MediaAttachment>().Match(m => m.ID == id && m.OwnerAvatarId == MyId).ExecuteFirstAsync(token);
|
||||||
|
return attachment == default ? NotFoundError() : Json(View(attachment));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("/api/v1/media/{id}"), Scope("write:media")]
|
||||||
|
public async Task<IActionResult> Update(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var attachment = await DB.Default.Find<MediaAttachment>().Match(m => m.ID == id && m.OwnerAvatarId == MyId).ExecuteFirstAsync(token);
|
||||||
|
if (attachment == default)
|
||||||
|
return NotFoundError();
|
||||||
|
if (Params.Has("description"))
|
||||||
|
attachment.Description = Params.Get("description")?.Trim() is { Length: > 0 } description ? description[..Math.Min(description.Length, 1500)] : default;
|
||||||
|
if (Params.Get("focus")?.Split(',') is [var x, var y]
|
||||||
|
&& float.TryParse(x, NumberStyles.Float, CultureInfo.InvariantCulture, out var fx) && float.TryParse(y, NumberStyles.Float, CultureInfo.InvariantCulture, out var fy))
|
||||||
|
attachment.Focus = new[] { Math.Clamp(fx, -1, 1), Math.Clamp(fy, -1, 1) };
|
||||||
|
await DB.Default.SaveAsync(attachment, token);
|
||||||
|
return Json(View(attachment));
|
||||||
|
}
|
||||||
|
|
||||||
|
[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)
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
object View(MediaAttachment attachment) => new
|
||||||
|
{
|
||||||
|
id = attachment.ID,
|
||||||
|
type = attachment.Kind,
|
||||||
|
url = _media.Url(attachment.FilePath),
|
||||||
|
preview_url = _media.Url(attachment.PreviewPath ?? attachment.FilePath),
|
||||||
|
remote_url = default(string),
|
||||||
|
text_url = default(string),
|
||||||
|
meta = new
|
||||||
|
{
|
||||||
|
original = attachment.Width.HasValue && attachment.Height > 0
|
||||||
|
? new { width = attachment.Width, height = attachment.Height, size = $"{attachment.Width}x{attachment.Height}", aspect = (double)attachment.Width / attachment.Height.Value }
|
||||||
|
: default,
|
||||||
|
focus = attachment.Focus is { Length: 2 } ? new { x = attachment.Focus[0], y = attachment.Focus[1] } : default
|
||||||
|
},
|
||||||
|
description = attachment.Description,
|
||||||
|
blurhash = attachment.Blurhash
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,181 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||||
|
using PrivaPub.Domain.Privacy;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Controllers
|
||||||
|
{
|
||||||
|
public class ProvenanceController : MastodonController
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
|
||||||
|
public ProvenanceController(DbEntities dbEntities) => _dbEntities = dbEntities;
|
||||||
|
|
||||||
|
[HttpGet("/api/privapub/v1/statuses/{id}/provenance"), Scope("read:statuses", requiresUser: false), AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Status(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var post = await _dbEntities.Posts.Match(p => p.ID == id && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
if (post?.ReblogOfPostId != default)
|
||||||
|
post = await _dbEntities.Posts.Match(p => p.ID == post.ReblogOfPostId && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
if (post == default || post.Visibility == PostVisibility.LocalGeo || !await VisibilityPolicy.CanSee(post, MyId, token))
|
||||||
|
return NotFoundError();
|
||||||
|
if (!post.IsFederatedCopy)
|
||||||
|
return Json(new Provenance { ObjectUri = post.ObjectURI, Url = post.Url, Path = "local" });
|
||||||
|
|
||||||
|
var record = await DB.Default.Find<ObjectRecord>().Match(r => r.PostId == post.ID).ExecuteFirstAsync(token)
|
||||||
|
?? await DB.Default.Find<ObjectRecord>().Match(r => r.ObjectURI == post.ObjectURI).ExecuteFirstAsync(token);
|
||||||
|
var host = record?.Host ?? (Uri.TryCreate(post.ObjectURI, UriKind.Absolute, out var uri) ? uri.Host.ToLowerInvariant() : default);
|
||||||
|
var instance = host == default ? default : await DB.Default.Find<RemoteInstance>().Match(i => i.Host == host).ExecuteFirstAsync(token);
|
||||||
|
return Json(Describe(post, record, instance));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/privapub/v1/instances/{host}"), Scope("read", requiresUser: false), AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Instance(string host, CancellationToken token)
|
||||||
|
{
|
||||||
|
host = host?.Trim().ToLowerInvariant();
|
||||||
|
var instance = await DB.Default.Find<RemoteInstance>().Match(i => i.Host == host).ExecuteFirstAsync(token);
|
||||||
|
return instance == default ? NotFoundError() : Json(Describe(instance));
|
||||||
|
}
|
||||||
|
|
||||||
|
static Provenance Describe(PostEntity post, ObjectRecord record, RemoteInstance instance)
|
||||||
|
{
|
||||||
|
var raw = record?.Raw == default ? default : JsonNode.Parse(record.Raw) as JsonObject;
|
||||||
|
return new Provenance
|
||||||
|
{
|
||||||
|
ObjectUri = post.ObjectURI,
|
||||||
|
ObjectType = record?.ObjectType ?? post.ObjectType,
|
||||||
|
Url = post.Url,
|
||||||
|
Path = record == default ? "unrecorded" : record.Path == ObjectPath.Delivered ? "delivered" : "fetched",
|
||||||
|
Refetched = record?.Refetched == true,
|
||||||
|
Activity = record?.ActivityId == default ? default : new ProvenanceActivity
|
||||||
|
{
|
||||||
|
Id = record.ActivityId, Type = record.ActivityType, Actor = record.ActivityActorURI
|
||||||
|
},
|
||||||
|
Inbox = record?.Inbox,
|
||||||
|
Signature = record?.KeyId == default ? 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),
|
||||||
|
Raw = raw,
|
||||||
|
RawBytes = record?.RawBytes ?? 0,
|
||||||
|
RawHash = record?.RawHash,
|
||||||
|
RawTruncated = record?.RawTruncated == true,
|
||||||
|
Revisions = record?.Revisions.Select(r => new ProvenanceRevision
|
||||||
|
{
|
||||||
|
ActivityId = r.ActivityId,
|
||||||
|
Updated = r.Updated is { } at ? MastodonJson.Time(at) : default,
|
||||||
|
ReceivedAt = MastodonJson.Time(r.ReceivedAt),
|
||||||
|
RawHash = r.RawHash,
|
||||||
|
Raw = r.Raw == default ? default : JsonNode.Parse(r.Raw)
|
||||||
|
}).ToList() ?? new(),
|
||||||
|
Instance = instance == default ? default : Describe(instance)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static InstanceDescription Describe(RemoteInstance instance) => new()
|
||||||
|
{
|
||||||
|
Host = instance.Host,
|
||||||
|
Software = instance.Software,
|
||||||
|
Version = instance.SoftwareVersion,
|
||||||
|
NodeName = instance.NodeName,
|
||||||
|
Protocols = instance.Protocols ?? new(),
|
||||||
|
OpenRegistrations = instance.OpenRegistrations,
|
||||||
|
DescribedAt = instance.DescribedAt is { } described ? MastodonJson.Time(described) : default,
|
||||||
|
DescriptionError = instance.DescriptionError,
|
||||||
|
NodeInfo = instance.NodeInfo == default ? default : JsonNode.Parse(instance.NodeInfo),
|
||||||
|
Delivery = new InstanceDelivery
|
||||||
|
{
|
||||||
|
ConsecutiveFailures = instance.ConsecutiveFailures,
|
||||||
|
UnavailableUntil = instance.UnavailableUntil is { } until ? MastodonJson.Time(until) : default,
|
||||||
|
LastSuccessAt = instance.LastSuccessAt is { } success ? MastodonJson.Time(success) : default,
|
||||||
|
LastFailureAt = instance.LastFailureAt is { } failure ? MastodonJson.Time(failure) : default
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public class Provenance
|
||||||
|
{
|
||||||
|
public string ObjectUri { get; set; }
|
||||||
|
public string ObjectType { get; set; }
|
||||||
|
public string Url { get; set; }
|
||||||
|
public string Path { get; set; }
|
||||||
|
public bool Refetched { get; set; }
|
||||||
|
public ProvenanceActivity Activity { get; set; }
|
||||||
|
public string Inbox { get; set; }
|
||||||
|
public ProvenanceSignature Signature { get; set; }
|
||||||
|
public string ReceivedAt { get; set; }
|
||||||
|
public string Published { get; set; }
|
||||||
|
public string Updated { get; set; }
|
||||||
|
public List<string> Extensions { get; set; } = new();
|
||||||
|
public List<string> ContextNamespaces { get; set; } = new();
|
||||||
|
public JsonNode Raw { get; set; }
|
||||||
|
public int RawBytes { get; set; }
|
||||||
|
public string RawHash { get; set; }
|
||||||
|
public bool RawTruncated { get; set; }
|
||||||
|
public List<ProvenanceRevision> Revisions { get; set; } = new();
|
||||||
|
public InstanceDescription Instance { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ProvenanceActivity
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Type { get; set; }
|
||||||
|
public string Actor { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ProvenanceSignature
|
||||||
|
{
|
||||||
|
public string Scheme { get; set; }
|
||||||
|
public string KeyId { get; set; }
|
||||||
|
public string Algorithm { get; set; }
|
||||||
|
public List<string> Headers { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ProvenanceRevision
|
||||||
|
{
|
||||||
|
public string ActivityId { get; set; }
|
||||||
|
public string Updated { get; set; }
|
||||||
|
public string ReceivedAt { get; set; }
|
||||||
|
public string RawHash { get; set; }
|
||||||
|
public JsonNode Raw { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class InstanceDescription
|
||||||
|
{
|
||||||
|
public string Host { get; set; }
|
||||||
|
public string Software { get; set; }
|
||||||
|
public string Version { get; set; }
|
||||||
|
public string NodeName { get; set; }
|
||||||
|
public List<string> Protocols { get; set; } = new();
|
||||||
|
public bool? OpenRegistrations { get; set; }
|
||||||
|
public string DescribedAt { get; set; }
|
||||||
|
public string DescriptionError { get; set; }
|
||||||
|
public JsonNode NodeInfo { get; set; }
|
||||||
|
public InstanceDelivery Delivery { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class InstanceDelivery
|
||||||
|
{
|
||||||
|
public int ConsecutiveFailures { get; set; }
|
||||||
|
public string UnavailableUntil { get; set; }
|
||||||
|
public string LastSuccessAt { get; set; }
|
||||||
|
public string LastFailureAt { 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||||
|
using PrivaPub.Api.Mastodon.Mappers;
|
||||||
|
using PrivaPub.Domain.Relationships;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Controllers
|
||||||
|
{
|
||||||
|
public class ReportsController : MastodonController
|
||||||
|
{
|
||||||
|
readonly IReportService _reports;
|
||||||
|
readonly MastodonMapper _mapper;
|
||||||
|
|
||||||
|
public ReportsController(IReportService reports, MastodonMapper mapper)
|
||||||
|
{
|
||||||
|
_reports = reports;
|
||||||
|
_mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/reports"), Scope("write:reports")]
|
||||||
|
public async Task<IActionResult> Create(CancellationToken token)
|
||||||
|
{
|
||||||
|
var report = await _reports.File(Me, Params.Get("account_id"), Params.List("status_ids"), Params.Get("comment"), Params.Get("category"),
|
||||||
|
Params.Bool("forward") == true, token);
|
||||||
|
if (report == default)
|
||||||
|
return NotFoundError();
|
||||||
|
return Json(new
|
||||||
|
{
|
||||||
|
id = report.ID,
|
||||||
|
action_taken = false,
|
||||||
|
action_taken_at = default(string),
|
||||||
|
category = report.Category,
|
||||||
|
comment = report.Comment ?? string.Empty,
|
||||||
|
forwarded = report.Forwarded,
|
||||||
|
created_at = MastodonJson.Time(report.CreatedAt),
|
||||||
|
status_ids = report.PostIds,
|
||||||
|
rule_ids = Array.Empty<string>(),
|
||||||
|
target_account = await _mapper.Account(report.TargetAccountId, token)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Entities;
|
||||||
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||||
|
using PrivaPub.Api.Mastodon.Mappers;
|
||||||
|
using PrivaPub.Domain.Privacy;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Inbox;
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Domain.Social;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Controllers
|
||||||
|
{
|
||||||
|
public class SearchController : MastodonController
|
||||||
|
{
|
||||||
|
readonly MastodonMapper _mapper;
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
readonly IRemotePosts _remotePosts;
|
||||||
|
readonly AccountSearch _search;
|
||||||
|
|
||||||
|
public SearchController(MastodonMapper mapper, DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors,
|
||||||
|
IRemotePosts remotePosts, AccountSearch search)
|
||||||
|
{
|
||||||
|
_mapper = mapper;
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
_remotePosts = remotePosts;
|
||||||
|
_search = search;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v2/search"), Scope("read:search", requiresUser: false)]
|
||||||
|
public async Task<IActionResult> Search(CancellationToken token)
|
||||||
|
{
|
||||||
|
var q = Params.Get("q")?.Trim();
|
||||||
|
var type = Params.Get("type");
|
||||||
|
var resolve = Params.Bool("resolve") == true && MyId != default;
|
||||||
|
var results = new SearchResults();
|
||||||
|
if (string.IsNullOrEmpty(q))
|
||||||
|
return Json(results);
|
||||||
|
|
||||||
|
if (q.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
var post = await _dbEntities.Posts.Match(p => (p.ObjectURI == q || p.Url == q) && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
if (post == default && resolve && type is null or "statuses")
|
||||||
|
post = await _remotePosts.StoreContext(q, 0, token);
|
||||||
|
if (post != default && type is null or "statuses" && await VisibilityPolicy.CanSee(post, MyId, token))
|
||||||
|
results.Statuses.Add(await _mapper.Status(post, MyId, token));
|
||||||
|
if (results.Statuses.Count == 0 && type is null or "accounts")
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUri(q, token);
|
||||||
|
var foreign = local == default ? (resolve ? await _remoteActors.GetActor(q, refresh: false, token) : default) : default;
|
||||||
|
if (local is { IsFederated: true, IsCircle: false })
|
||||||
|
results.Accounts.Add(await _mapper.Local(local, false, token));
|
||||||
|
else if (foreign != default)
|
||||||
|
results.Accounts.Add(_mapper.Foreign(foreign));
|
||||||
|
}
|
||||||
|
return Json(results);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type is null or "accounts")
|
||||||
|
{
|
||||||
|
results.Accounts = await _search.Find(q, resolve, Limit(), token);
|
||||||
|
}
|
||||||
|
if (type is null or "hashtags" && q.TrimStart('#') is { Length: > 0 } tag)
|
||||||
|
results.Hashtags.Add(new { name = tag.ToLowerInvariant(), url = $"{_localActors.BaseAddress}/tags/{Uri.EscapeDataString(tag.ToLowerInvariant())}", history = Array.Empty<object>() });
|
||||||
|
return Json(results);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StubsController : MastodonController
|
||||||
|
{
|
||||||
|
[HttpGet("/api/v1/custom_emojis"), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public IActionResult CustomEmojis() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/filters"), Scope("read:filters")]
|
||||||
|
public IActionResult FiltersV1() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v2/filters"), Scope("read:filters")]
|
||||||
|
public IActionResult FiltersV2() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/lists"), Scope("read:lists")]
|
||||||
|
public IActionResult Lists() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/announcements"), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public IActionResult Announcements() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/trends"), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public IActionResult Trends() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/trends/tags"), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public IActionResult TrendTags() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/trends/statuses"), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public IActionResult TrendStatuses() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/trends/links"), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public IActionResult TrendLinks() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/suggestions"), Scope("read")]
|
||||||
|
public IActionResult SuggestionsV1() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v2/suggestions"), Scope("read")]
|
||||||
|
public IActionResult SuggestionsV2() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/followed_tags"), Scope("read:follows")]
|
||||||
|
public IActionResult FollowedTags() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/endorsements"), Scope("read:accounts")]
|
||||||
|
public IActionResult Endorsements() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/featured_tags"), Scope("read:accounts")]
|
||||||
|
public IActionResult FeaturedTags() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/scheduled_statuses"), Scope("read:statuses")]
|
||||||
|
public IActionResult ScheduledStatuses() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/preferences"), Scope("read:accounts")]
|
||||||
|
public IActionResult Preferences() => Json(new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
["posting:default:visibility"] = Me.Settings.DefaultVisibility ?? "public",
|
||||||
|
["posting:default:sensitive"] = Me.Settings.DefaultSensitive,
|
||||||
|
["posting:default:language"] = Me.Settings.DefaultLanguage,
|
||||||
|
["reading:expand:media"] = "default",
|
||||||
|
["reading:expand:spoilers"] = false
|
||||||
|
});
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/push/subscription"), Scope("push")]
|
||||||
|
public IActionResult PushSubscription() => Error(StatusCodes.Status404NotFound, "Record not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,345 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Entities;
|
||||||
|
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 StatusesController : MastodonController
|
||||||
|
{
|
||||||
|
const int MaxContext = 200;
|
||||||
|
public const int MaxPins = 5;
|
||||||
|
|
||||||
|
readonly IStatusService _statuses;
|
||||||
|
readonly MastodonMapper _mapper;
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly IMemoryCache _cache;
|
||||||
|
|
||||||
|
public StatusesController(IStatusService statuses, MastodonMapper mapper, DbEntities dbEntities, IMemoryCache cache)
|
||||||
|
{
|
||||||
|
_statuses = statuses;
|
||||||
|
_mapper = mapper;
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_cache = cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses"), Scope("write:statuses")]
|
||||||
|
public async Task<IActionResult> Create(CancellationToken token)
|
||||||
|
{
|
||||||
|
var idempotency = Request.Headers["Idempotency-Key"].ToString();
|
||||||
|
var cacheKey = string.IsNullOrEmpty(idempotency) ? default : $"idempotency:{MyId}:{idempotency}";
|
||||||
|
if (cacheKey != default && _cache.TryGetValue(cacheKey, out string earlier))
|
||||||
|
{
|
||||||
|
var existing = await _dbEntities.Posts.MatchID(earlier).ExecuteFirstAsync(token);
|
||||||
|
if (existing != default)
|
||||||
|
return Json(await _mapper.Status(existing, MyId, token));
|
||||||
|
}
|
||||||
|
var outcome = await _statuses.Publish(Me, new StatusDraft
|
||||||
|
{
|
||||||
|
Text = Params.Get("status"),
|
||||||
|
PlainText = true,
|
||||||
|
SpoilerText = Params.Get("spoiler_text"),
|
||||||
|
Sensitive = Params.Bool("sensitive") ?? Me.Settings.DefaultSensitive,
|
||||||
|
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"),
|
||||||
|
QuotedStatusId = Params.Get("quoted_status_id"),
|
||||||
|
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);
|
||||||
|
if (cacheKey != default)
|
||||||
|
_cache.Set(cacheKey, outcome.Post.ID, TimeSpan.FromHours(1));
|
||||||
|
return Json(await _mapper.Status(outcome.Post, MyId, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/statuses"), Scope("read:statuses", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Many(CancellationToken token)
|
||||||
|
{
|
||||||
|
var ids = Params.List("id").Take(20).ToList();
|
||||||
|
var posts = await _dbEntities.Posts.Match(p => ids.Contains(p.ID) && !p.DeletedAt.HasValue).ExecuteAsync(token);
|
||||||
|
var visible = new List<PostEntity>();
|
||||||
|
foreach (var post in posts)
|
||||||
|
if (await VisibilityPolicy.CanSee(post, MyId, token))
|
||||||
|
visible.Add(post);
|
||||||
|
return Json(await _mapper.Statuses(visible, MyId, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/statuses/{id}"), Scope("read:statuses", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Get(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var post = await Visible(id, token);
|
||||||
|
return post == default ? NotFoundError() : Json(await _mapper.Status(post, MyId, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("/api/v1/statuses/{id}"), Scope("write:statuses")]
|
||||||
|
public async Task<IActionResult> Edit(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var outcome = await _statuses.Edit(Me, id, new StatusDraft
|
||||||
|
{
|
||||||
|
Text = Params.Get("status"),
|
||||||
|
PlainText = true,
|
||||||
|
SpoilerText = Params.Get("spoiler_text"),
|
||||||
|
Sensitive = Params.Bool("sensitive") ?? false,
|
||||||
|
Language = Params.Get("language"),
|
||||||
|
MediaIds = Params.Has("media_ids") ? Params.List("media_ids") : default
|
||||||
|
}, token);
|
||||||
|
return outcome.Ok ? Json(await _mapper.Status(outcome.Post, MyId, token)) : Error(outcome.Status, outcome.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("/api/v1/statuses/{id}"), Scope("write:statuses")]
|
||||||
|
public async Task<IActionResult> Delete(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var before = await _dbEntities.Posts.Match(p => p.ID == id && p.GroupUserId == MyId && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
if (before == default)
|
||||||
|
return NotFoundError();
|
||||||
|
var status = await _mapper.Status(before, MyId, token);
|
||||||
|
var outcome = await _statuses.Remove(Me, id, token);
|
||||||
|
if (!outcome.Ok)
|
||||||
|
return Error(outcome.Status, outcome.Error);
|
||||||
|
if (status != default)
|
||||||
|
{
|
||||||
|
status.Text = before.Text;
|
||||||
|
status.SpoilerText = before.SpoilerText ?? string.Empty;
|
||||||
|
}
|
||||||
|
return Json(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/statuses/{id}/context"), Scope("read:statuses", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Context(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var post = await Visible(id, token);
|
||||||
|
if (post == default)
|
||||||
|
return NotFoundError();
|
||||||
|
|
||||||
|
var ancestors = new List<PostEntity>();
|
||||||
|
var cursor = post;
|
||||||
|
while (!string.IsNullOrEmpty(cursor.AnsweringToPostId) && ancestors.Count < MaxContext)
|
||||||
|
{
|
||||||
|
cursor = await _dbEntities.Posts.MatchID(cursor.AnsweringToPostId).ExecuteFirstAsync(token);
|
||||||
|
if (cursor == default || cursor.DeletedAt.HasValue || !await VisibilityPolicy.CanSee(cursor, MyId, token))
|
||||||
|
break;
|
||||||
|
ancestors.Insert(0, cursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
var descendants = new List<PostEntity>();
|
||||||
|
var frontier = new List<string> { post.ID };
|
||||||
|
while (frontier.Count > 0 && descendants.Count < MaxContext)
|
||||||
|
{
|
||||||
|
var parents = frontier;
|
||||||
|
var children = await _dbEntities.Posts.Match(p => parents.Contains(p.AnsweringToPostId) && !p.DeletedAt.HasValue && p.ReblogOfPostId == null)
|
||||||
|
.Sort(p => p.ID, Order.Ascending).Limit(MaxContext).ExecuteAsync(token);
|
||||||
|
frontier = new List<string>();
|
||||||
|
foreach (var child in children)
|
||||||
|
if (await VisibilityPolicy.CanSee(child, MyId, token))
|
||||||
|
{
|
||||||
|
descendants.Add(child);
|
||||||
|
frontier.Add(child.ID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Json(new Context
|
||||||
|
{
|
||||||
|
Ancestors = await _mapper.Statuses(ancestors, MyId, token),
|
||||||
|
Descendants = await _mapper.Statuses(descendants.OrderBy(d => d.ID, StringComparer.Ordinal).ToList(), MyId, token)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/statuses/{id}/history"), Scope("read:statuses", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> History(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var post = await Visible(id, token);
|
||||||
|
if (post == default)
|
||||||
|
return NotFoundError();
|
||||||
|
var account = await _mapper.Account(MastodonMapper.AuthorOf(post), token);
|
||||||
|
var edits = post.Revisions.Select(r => new StatusEdit
|
||||||
|
{
|
||||||
|
Content = r.ContentHtml ?? string.Empty,
|
||||||
|
SpoilerText = r.SpoilerText ?? string.Empty,
|
||||||
|
Sensitive = r.HasContentWarning,
|
||||||
|
CreatedAt = MastodonJson.Time(r.EditedAt),
|
||||||
|
Account = account
|
||||||
|
}).ToList();
|
||||||
|
edits.Add(new StatusEdit
|
||||||
|
{
|
||||||
|
Content = MastodonMapper.Content(post),
|
||||||
|
SpoilerText = post.SpoilerText ?? string.Empty,
|
||||||
|
Sensitive = post.HasContentWarning,
|
||||||
|
CreatedAt = MastodonJson.Time(post.EditedAt ?? post.CreationDate),
|
||||||
|
Account = account
|
||||||
|
});
|
||||||
|
return Json(edits);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/statuses/{id}/source"), Scope("read:statuses")]
|
||||||
|
public async Task<IActionResult> Source(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var post = await _dbEntities.Posts.Match(p => p.ID == id && p.GroupUserId == MyId && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
return post == default ? NotFoundError() : Json(new StatusSource { Id = post.ID, Text = post.Text ?? string.Empty, SpoilerText = post.SpoilerText ?? string.Empty });
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses/{id}/favourite"), Scope("write:favourites")]
|
||||||
|
public Task<IActionResult> Favourite(string id, CancellationToken token) => Toggle(_statuses.Favourite(Me, id, true, token), id, token);
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses/{id}/unfavourite"), Scope("write:favourites")]
|
||||||
|
public Task<IActionResult> Unfavourite(string id, CancellationToken token) => Toggle(_statuses.Favourite(Me, id, false, token), id, token);
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses/{id}/reblog"), Scope("write:statuses")]
|
||||||
|
public async Task<IActionResult> Reblog(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var outcome = await _statuses.Reblog(Me, id, true, Visibility(Params.Get("visibility")), token);
|
||||||
|
return outcome.Ok ? Json(await _mapper.Status(outcome.Post, MyId, token)) : Error(outcome.Status, outcome.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses/{id}/unreblog"), Scope("write:statuses")]
|
||||||
|
public Task<IActionResult> Unreblog(string id, CancellationToken token) => Toggle(_statuses.Reblog(Me, id, false, PostVisibility.Public, token), id, token);
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/statuses/{id}/favourited_by"), Scope("read:accounts", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> FavouritedBy(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (await Visible(id, token) == default)
|
||||||
|
return NotFoundError();
|
||||||
|
var favourites = await _dbEntities.Favourites.Match(f => f.PostId == id).Sort(f => f.ID, Order.Descending).Limit(Limit(40, 80)).ExecuteAsync(token);
|
||||||
|
var accounts = await _mapper.Accounts(favourites.Select(f => f.AccountId), token);
|
||||||
|
return Json(favourites.Select(f => accounts.GetValueOrDefault(f.AccountId)).Where(a => a != default).ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/statuses/{id}/reblogged_by"), Scope("read:accounts", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> RebloggedBy(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (await Visible(id, token) == default)
|
||||||
|
return NotFoundError();
|
||||||
|
var reblogs = await _dbEntities.Posts.Match(p => p.ReblogOfPostId == id && !p.DeletedAt.HasValue).Sort(p => p.ID, Order.Descending).Limit(Limit(40, 80)).ExecuteAsync(token);
|
||||||
|
var accounts = await _mapper.Accounts(reblogs.Select(MastodonMapper.AuthorOf), token);
|
||||||
|
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}/bookmark"), Scope("write:bookmarks")]
|
||||||
|
public async Task<IActionResult> Bookmark(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var post = await Visible(id, token);
|
||||||
|
if (post == default)
|
||||||
|
return NotFoundError();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(new Models.Social.Bookmark { AvatarId = MyId, PostId = post.ID }, token);
|
||||||
|
}
|
||||||
|
catch (MongoDB.Driver.MongoWriteException ex) when (ex.WriteError?.Category == MongoDB.Driver.ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
return Json(await _mapper.Status(post, MyId, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses/{id}/unbookmark"), Scope("write:bookmarks")]
|
||||||
|
public async Task<IActionResult> Unbookmark(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
await DB.Default.DeleteAsync<Models.Social.Bookmark>(b => b.AvatarId == MyId && b.PostId == id);
|
||||||
|
return await Unchanged(id, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/bookmarks"), Scope("read:bookmarks")]
|
||||||
|
public async Task<IActionResult> Bookmarks(CancellationToken token)
|
||||||
|
{
|
||||||
|
var bookmarks = await Page.From(Params, Limit()).Fetch(DB.Default.Find<Models.Social.Bookmark>().Match(b => b.AvatarId == MyId), b => b.ID, token);
|
||||||
|
var ids = bookmarks.Select(b => b.PostId).ToList();
|
||||||
|
var posts = (await _dbEntities.Posts.Match(p => ids.Contains(p.ID) && !p.DeletedAt.HasValue).ExecuteAsync(token)).ToDictionary(p => p.ID);
|
||||||
|
var visible = new List<PostEntity>();
|
||||||
|
foreach (var postId in ids.Where(posts.ContainsKey))
|
||||||
|
if (await VisibilityPolicy.CanSee(posts[postId], MyId, token))
|
||||||
|
visible.Add(posts[postId]);
|
||||||
|
Link("/api/v1/bookmarks", bookmarks.LastOrDefault()?.ID, bookmarks.FirstOrDefault()?.ID);
|
||||||
|
return Json(await _mapper.Statuses(visible, MyId, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses/{id}/mute"), Scope("write:mutes")]
|
||||||
|
public Task<IActionResult> Mute(string id, CancellationToken token) => Unchanged(id, token);
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses/{id}/unmute"), Scope("write:mutes")]
|
||||||
|
public Task<IActionResult> Unmute(string id, CancellationToken token) => Unchanged(id, token);
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses/{id}/pin"), Scope("write:accounts")]
|
||||||
|
public async Task<IActionResult> Pin(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var post = await _dbEntities.Posts.Match(p => p.ID == id && p.GroupUserId == MyId && !p.IsFederatedCopy && !p.DeletedAt.HasValue && p.ReblogOfPostId == null)
|
||||||
|
.ExecuteFirstAsync(token);
|
||||||
|
if (post == default)
|
||||||
|
return NotFoundError();
|
||||||
|
if (post.Visibility is not (PostVisibility.Public or PostVisibility.Unlisted))
|
||||||
|
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: Only public posts can be pinned");
|
||||||
|
if (await DB.Default.CountAsync<Models.Social.Pin>(p => p.AvatarId == MyId, token) >= MaxPins)
|
||||||
|
return Error(StatusCodes.Status422UnprocessableEntity, $"Validation failed: You can pin at most {MaxPins} posts");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(new Models.Social.Pin { AvatarId = MyId, PostId = post.ID }, token);
|
||||||
|
}
|
||||||
|
catch (MongoDB.Driver.MongoWriteException ex) when (ex.WriteError?.Category == MongoDB.Driver.ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
return Json(await _mapper.Status(post, MyId, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses/{id}/unpin"), Scope("write:accounts")]
|
||||||
|
public async Task<IActionResult> Unpin(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
await DB.Default.DeleteAsync<Models.Social.Pin>(p => p.AvatarId == MyId && p.PostId == id);
|
||||||
|
return await Unchanged(id, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<IActionResult> Toggle(Task<StatusOutcome> action, string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var outcome = await action;
|
||||||
|
if (!outcome.Ok)
|
||||||
|
return Error(outcome.Status, outcome.Error);
|
||||||
|
var post = await _dbEntities.Posts.MatchID(id).ExecuteFirstAsync(token);
|
||||||
|
return Json(await _mapper.Status(post, MyId, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<IActionResult> Unchanged(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var post = await Visible(id, token);
|
||||||
|
return post == default ? NotFoundError() : 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).ExecuteFirstAsync(token);
|
||||||
|
return post != default && post.Visibility != PostVisibility.LocalGeo && await VisibilityPolicy.CanSee(post, MyId, token) ? post : default;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PostVisibility Visibility(string value) => value?.ToLowerInvariant() switch
|
||||||
|
{
|
||||||
|
"unlisted" => PostVisibility.Unlisted,
|
||||||
|
"private" => PostVisibility.FollowersOnly,
|
||||||
|
"direct" => PostVisibility.Direct,
|
||||||
|
_ => PostVisibility.Public
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,248 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Entities;
|
||||||
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||||
|
using PrivaPub.Api.Mastodon.Mappers;
|
||||||
|
using PrivaPub.Domain.Privacy;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
using NotificationEntity = PrivaPub.Models.Social.Notification;
|
||||||
|
using MarkerEntity = PrivaPub.Models.Social.Marker;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Controllers
|
||||||
|
{
|
||||||
|
public class TimelinesController : MastodonController
|
||||||
|
{
|
||||||
|
readonly MastodonMapper _mapper;
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
|
||||||
|
public TimelinesController(MastodonMapper mapper, DbEntities dbEntities)
|
||||||
|
{
|
||||||
|
_mapper = mapper;
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/timelines/home"), Scope("read:statuses")]
|
||||||
|
public async Task<IActionResult> Home(CancellationToken token)
|
||||||
|
{
|
||||||
|
var entries = await Page.From(Params, Limit()).Fetch(_dbEntities.TimelineEntries.Match(e => e.AvatarId == MyId), e => e.PostId, token);
|
||||||
|
var ids = entries.Select(e => e.PostId).ToList();
|
||||||
|
var posts = (await _dbEntities.Posts.Match(p => ids.Contains(p.ID) && !p.DeletedAt.HasValue).ExecuteAsync(token)).ToDictionary(p => p.ID);
|
||||||
|
var statuses = await _mapper.Statuses(ids.Where(posts.ContainsKey).Select(id => posts[id]).ToList(), MyId, token);
|
||||||
|
Link("/api/v1/timelines/home", entries.LastOrDefault()?.PostId, entries.FirstOrDefault()?.PostId);
|
||||||
|
return Json(statuses);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/timelines/public"), Scope("read:statuses", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Public(CancellationToken token)
|
||||||
|
{
|
||||||
|
var query = _dbEntities.Posts.Match(p => p.Visibility == PostVisibility.Public && !p.DeletedAt.HasValue && p.ReblogOfPostId == null);
|
||||||
|
if (Params.Bool("local") == true)
|
||||||
|
query.Match(p => !p.IsFederatedCopy);
|
||||||
|
if (Params.Bool("remote") == true)
|
||||||
|
query.Match(p => p.IsFederatedCopy);
|
||||||
|
if (Params.Bool("only_media") == true)
|
||||||
|
query.Match(p => p.Media.Count > 0);
|
||||||
|
return await Respond(query, "/api/v1/timelines/public", token);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/timelines/tag/{hashtag}"), Scope("read:statuses", requiresUser: false), Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||||
|
public async Task<IActionResult> Tag(string hashtag, CancellationToken token)
|
||||||
|
{
|
||||||
|
var tag = hashtag.TrimStart('#').ToLowerInvariant();
|
||||||
|
var query = _dbEntities.Posts.Match(p => p.Tags.Contains(tag) && p.Visibility == PostVisibility.Public && !p.DeletedAt.HasValue && p.ReblogOfPostId == null);
|
||||||
|
if (Params.Bool("local") == true)
|
||||||
|
query.Match(p => !p.IsFederatedCopy);
|
||||||
|
return await Respond(query, $"/api/v1/timelines/tag/{hashtag}", token);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/timelines/list/{id}"), Scope("read:lists")]
|
||||||
|
public IActionResult List(string id) => Json(Array.Empty<Status>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/favourites"), Scope("read:favourites")]
|
||||||
|
public async Task<IActionResult> Favourites(CancellationToken token)
|
||||||
|
{
|
||||||
|
var favourites = await Page.From(Params, Limit()).Fetch(_dbEntities.Favourites.Match(f => f.AccountId == MyId), f => f.ID, token);
|
||||||
|
var ids = favourites.Select(f => f.PostId).ToList();
|
||||||
|
var posts = (await _dbEntities.Posts.Match(p => ids.Contains(p.ID) && !p.DeletedAt.HasValue).ExecuteAsync(token)).ToDictionary(p => p.ID);
|
||||||
|
var visible = new List<PostEntity>();
|
||||||
|
foreach (var id in ids.Where(posts.ContainsKey))
|
||||||
|
if (await VisibilityPolicy.CanSee(posts[id], MyId, token))
|
||||||
|
visible.Add(posts[id]);
|
||||||
|
Link("/api/v1/favourites", favourites.LastOrDefault()?.ID, favourites.FirstOrDefault()?.ID);
|
||||||
|
return Json(await _mapper.Statuses(visible, MyId, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/conversations"), Scope("read:statuses")]
|
||||||
|
public async Task<IActionResult> Conversations(CancellationToken token)
|
||||||
|
{
|
||||||
|
var groups = await _dbEntities.DmGroups.Match(g => !g.DeletionAt.HasValue && g.Members.Any(m => !m.IsForeign && m.AvatarId == MyId))
|
||||||
|
.Sort(g => g.UpdatedAt, Order.Descending).Limit(Limit()).ExecuteAsync(token);
|
||||||
|
var conversations = new List<Conversation>();
|
||||||
|
foreach (var group in groups)
|
||||||
|
{
|
||||||
|
var last = await _dbEntities.Posts.Match(p => p.ConversationId == group.ID && !p.DeletedAt.HasValue).Sort(p => p.ID, Order.Descending).ExecuteFirstAsync(token);
|
||||||
|
if (last == default)
|
||||||
|
continue;
|
||||||
|
var others = new List<string>();
|
||||||
|
foreach (var member in group.Members.Where(m => !(m.AvatarId == MyId && !m.IsForeign)))
|
||||||
|
others.Add(member.IsForeign
|
||||||
|
? (await _dbEntities.ForeignAvatars.Match(f => f.ActorURI == member.AvatarId).ExecuteFirstAsync(token))?.ID
|
||||||
|
: member.AvatarId);
|
||||||
|
var accounts = await _mapper.Accounts(others, token);
|
||||||
|
conversations.Add(new Conversation
|
||||||
|
{
|
||||||
|
Id = group.ID,
|
||||||
|
Accounts = accounts.Values.ToList(),
|
||||||
|
LastStatus = await _mapper.Status(last, MyId, token)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return Json(conversations);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/conversations/{id}/read"), Scope("write:conversations")]
|
||||||
|
public async Task<IActionResult> ReadConversation(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var group = await _dbEntities.DmGroups.Match(g => g.ID == id && g.Members.Any(m => !m.IsForeign && m.AvatarId == MyId)).ExecuteFirstAsync(token);
|
||||||
|
if (group == default)
|
||||||
|
return NotFoundError();
|
||||||
|
var last = await _dbEntities.Posts.Match(p => p.ConversationId == group.ID && !p.DeletedAt.HasValue).Sort(p => p.ID, Order.Descending).ExecuteFirstAsync(token);
|
||||||
|
return Json(new Conversation { Id = group.ID, LastStatus = last == default ? default : await _mapper.Status(last, MyId, token) });
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/markers"), Scope("read:statuses")]
|
||||||
|
public async Task<IActionResult> Markers(CancellationToken token)
|
||||||
|
{
|
||||||
|
var wanted = Params.List("timeline");
|
||||||
|
var markers = await DB.Default.Find<MarkerEntity>().Match(m => m.AvatarId == MyId).ExecuteAsync(token);
|
||||||
|
return Json(markers.Where(m => wanted.Count == 0 || wanted.Contains(m.Timeline))
|
||||||
|
.ToDictionary(m => m.Timeline, m => new Entities.Marker { LastReadId = m.LastReadId, Version = m.Version, UpdatedAt = MastodonJson.Time(m.UpdatedAt) }));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/markers"), Scope("write:statuses")]
|
||||||
|
public async Task<IActionResult> SaveMarkers(CancellationToken token)
|
||||||
|
{
|
||||||
|
var result = new Dictionary<string, Entities.Marker>();
|
||||||
|
foreach (var timeline in new[] { "home", "notifications" })
|
||||||
|
{
|
||||||
|
var lastRead = Params.Get($"{timeline}[last_read_id]");
|
||||||
|
if (string.IsNullOrEmpty(lastRead))
|
||||||
|
continue;
|
||||||
|
var marker = await DB.Default.UpdateAndGet<MarkerEntity>()
|
||||||
|
.Match(m => m.AvatarId == MyId && m.Timeline == timeline)
|
||||||
|
.Modify(m => m.LastReadId, lastRead)
|
||||||
|
.Modify(m => m.UpdatedAt, DateTime.UtcNow)
|
||||||
|
.Modify(b => b.Inc(m => m.Version, 1))
|
||||||
|
.Option(o => o.IsUpsert = true)
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
result[timeline] = new Entities.Marker { LastReadId = marker.LastReadId, Version = marker.Version, UpdatedAt = MastodonJson.Time(marker.UpdatedAt) };
|
||||||
|
}
|
||||||
|
return Json(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<IActionResult> Respond(Find<PostEntity, PostEntity> query, string path, CancellationToken token)
|
||||||
|
{
|
||||||
|
var posts = await Page.From(Params, Limit()).Fetch(query, p => p.ID, token);
|
||||||
|
Link(path, posts.LastOrDefault()?.ID, posts.FirstOrDefault()?.ID);
|
||||||
|
return Json(await _mapper.Statuses(posts, MyId, token));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NotificationsController : MastodonController
|
||||||
|
{
|
||||||
|
static readonly Dictionary<NotificationType, string> Names = new()
|
||||||
|
{
|
||||||
|
[NotificationType.Mention] = "mention",
|
||||||
|
[NotificationType.Follow] = "follow",
|
||||||
|
[NotificationType.FollowRequest] = "follow_request",
|
||||||
|
[NotificationType.Favourite] = "favourite",
|
||||||
|
[NotificationType.Reblog] = "reblog",
|
||||||
|
[NotificationType.Update] = "update",
|
||||||
|
[NotificationType.Poll] = "poll",
|
||||||
|
[NotificationType.Reaction] = "pleroma:emoji_reaction",
|
||||||
|
[NotificationType.Quote] = "quote"
|
||||||
|
};
|
||||||
|
|
||||||
|
readonly MastodonMapper _mapper;
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
|
||||||
|
public NotificationsController(MastodonMapper mapper, DbEntities dbEntities)
|
||||||
|
{
|
||||||
|
_mapper = mapper;
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/notifications"), Scope("read:notifications")]
|
||||||
|
public async Task<IActionResult> List(CancellationToken token)
|
||||||
|
{
|
||||||
|
var types = Params.List("types").Select(Parse).Where(t => t.HasValue).Select(t => t.Value).ToList();
|
||||||
|
var excluded = Params.List("exclude_types").Select(Parse).Where(t => t.HasValue).Select(t => t.Value).ToList();
|
||||||
|
var query = _dbEntities.Notifications.Match(n => n.AvatarId == MyId);
|
||||||
|
if (types.Count > 0)
|
||||||
|
query.Match(n => types.Contains(n.Type));
|
||||||
|
if (excluded.Count > 0)
|
||||||
|
query.Match(n => !excluded.Contains(n.Type));
|
||||||
|
if (Params.Get("account_id") is { } accountId)
|
||||||
|
query.Match(n => n.FromAccountId == accountId);
|
||||||
|
var notifications = await Page.From(Params, Limit(15, 30)).Fetch(query, n => n.ID, token);
|
||||||
|
Link("/api/v1/notifications", notifications.LastOrDefault()?.ID, notifications.FirstOrDefault()?.ID);
|
||||||
|
return Json(await Map(notifications, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/notifications/{id}"), Scope("read:notifications")]
|
||||||
|
public async Task<IActionResult> Get(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var notification = await _dbEntities.Notifications.Match(n => n.ID == id && n.AvatarId == MyId).ExecuteFirstAsync(token);
|
||||||
|
return notification == default ? NotFoundError() : Json((await Map(new List<NotificationEntity> { notification }, token)).FirstOrDefault());
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/notifications/clear"), Scope("write:notifications")]
|
||||||
|
public async Task<IActionResult> Clear(CancellationToken token)
|
||||||
|
{
|
||||||
|
await DB.Default.DeleteAsync<NotificationEntity>(n => n.AvatarId == MyId);
|
||||||
|
return Json(new { });
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/notifications/{id}/dismiss"), Scope("write:notifications")]
|
||||||
|
public async Task<IActionResult> Dismiss(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
await DB.Default.DeleteAsync<NotificationEntity>(n => n.ID == id && n.AvatarId == MyId);
|
||||||
|
return Json(new { });
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/notifications/unread_count"), Scope("read:notifications")]
|
||||||
|
public async Task<IActionResult> UnreadCount(CancellationToken token) =>
|
||||||
|
Json(new { count = await DB.Default.CountAsync<NotificationEntity>(n => n.AvatarId == MyId && !n.IsRead, token) });
|
||||||
|
|
||||||
|
async Task<List<Entities.Notification>> Map(List<NotificationEntity> notifications, CancellationToken token)
|
||||||
|
{
|
||||||
|
var accounts = await _mapper.Accounts(notifications.Select(n => n.FromAccountId), token);
|
||||||
|
var postIds = notifications.Where(n => n.PostId != default).Select(n => n.PostId).Distinct().ToList();
|
||||||
|
var posts = postIds.Count == 0
|
||||||
|
? new List<PostEntity>()
|
||||||
|
: await _dbEntities.Posts.Match(p => postIds.Contains(p.ID) && !p.DeletedAt.HasValue).ExecuteAsync(token);
|
||||||
|
var statuses = (await _mapper.Statuses(posts, MyId, token)).ToDictionary(s => s.Id);
|
||||||
|
return notifications
|
||||||
|
.Where(n => accounts.ContainsKey(n.FromAccountId ?? string.Empty) && (n.PostId == default || statuses.ContainsKey(n.PostId)))
|
||||||
|
.Select(n => new Entities.Notification
|
||||||
|
{
|
||||||
|
Id = n.ID,
|
||||||
|
Type = Names[n.Type],
|
||||||
|
CreatedAt = MastodonJson.Time(n.CreatedAt),
|
||||||
|
GroupKey = $"ungrouped-{n.ID}",
|
||||||
|
Account = accounts[n.FromAccountId],
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
static NotificationType? Parse(string name) => Names.FirstOrDefault(n => n.Value == name) is { Value: not null } pair ? pair.Key : (NotificationType?)null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,390 @@
|
|||||||
|
namespace PrivaPub.Api.Mastodon.Entities
|
||||||
|
{
|
||||||
|
public class Account
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Username { get; set; }
|
||||||
|
public string Acct { get; set; }
|
||||||
|
public string DisplayName { get; set; }
|
||||||
|
public bool Locked { get; set; }
|
||||||
|
public bool Bot { get; set; }
|
||||||
|
public bool? Discoverable { get; set; }
|
||||||
|
public bool Indexable { get; set; }
|
||||||
|
public bool Group { get; set; }
|
||||||
|
public string CreatedAt { get; set; }
|
||||||
|
public string Note { get; set; }
|
||||||
|
public string Url { get; set; }
|
||||||
|
public string Uri { get; set; }
|
||||||
|
public string Avatar { get; set; }
|
||||||
|
public string AvatarStatic { get; set; }
|
||||||
|
public string Header { get; set; }
|
||||||
|
public string HeaderStatic { get; set; }
|
||||||
|
public int FollowersCount { get; set; }
|
||||||
|
public int FollowingCount { get; set; }
|
||||||
|
public int StatusesCount { get; set; }
|
||||||
|
public string LastStatusAt { get; set; }
|
||||||
|
public bool? HideCollections { get; set; }
|
||||||
|
public bool Noindex { get; set; } = true;
|
||||||
|
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; }
|
||||||
|
public string Value { get; set; }
|
||||||
|
public string VerifiedAt { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Source
|
||||||
|
{
|
||||||
|
public string Privacy { get; set; } = "public";
|
||||||
|
public bool Sensitive { get; set; }
|
||||||
|
public string Language { get; set; }
|
||||||
|
public string Note { get; set; } = string.Empty;
|
||||||
|
public List<Field> Fields { get; set; } = new();
|
||||||
|
public int FollowRequestsCount { get; set; }
|
||||||
|
public string AttributionDomains { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PrivaPubStatus
|
||||||
|
{
|
||||||
|
public string ObjectType { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
public string Excerpt { get; set; }
|
||||||
|
public string Cover { get; set; }
|
||||||
|
public PrivaPubSource Source { get; set; }
|
||||||
|
public PrivaPubLink Link { get; set; }
|
||||||
|
public PrivaPubVideo Video { get; set; }
|
||||||
|
public PrivaPubAudio Audio { get; set; }
|
||||||
|
public PrivaPubEvent Event { get; set; }
|
||||||
|
public PrivaPubVotes Votes { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PrivaPubSource
|
||||||
|
{
|
||||||
|
public string Content { get; set; }
|
||||||
|
public string MediaType { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PrivaPubLink
|
||||||
|
{
|
||||||
|
public string Url { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
public string Description { get; set; }
|
||||||
|
public string Image { get; set; }
|
||||||
|
public bool FromPublisher { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PrivaPubVariant
|
||||||
|
{
|
||||||
|
public string Url { get; set; }
|
||||||
|
public string MediaType { get; set; }
|
||||||
|
public int? Width { get; set; }
|
||||||
|
public int? Height { get; set; }
|
||||||
|
public double? Fps { get; set; }
|
||||||
|
public long? Size { get; set; }
|
||||||
|
public List<string> Streams { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PrivaPubVideo
|
||||||
|
{
|
||||||
|
public double? Duration { get; set; }
|
||||||
|
public List<PrivaPubVariant> Variants { get; set; } = new();
|
||||||
|
public bool IsLive { get; set; }
|
||||||
|
public int? State { get; set; }
|
||||||
|
public long? Views { get; set; }
|
||||||
|
public int? CommentsPolicy { get; set; }
|
||||||
|
public bool? DownloadEnabled { get; set; }
|
||||||
|
public string Licence { get; set; }
|
||||||
|
public string Category { get; set; }
|
||||||
|
public string Channel { get; set; }
|
||||||
|
public List<PrivaPubCaption> Captions { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PrivaPubCaption
|
||||||
|
{
|
||||||
|
public string Language { get; set; }
|
||||||
|
public string Url { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PrivaPubAudio
|
||||||
|
{
|
||||||
|
public double? Duration { get; set; }
|
||||||
|
public List<PrivaPubVariant> Variants { get; set; } = new();
|
||||||
|
public string Album { get; set; }
|
||||||
|
public int? Position { get; set; }
|
||||||
|
public string Licence { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PrivaPubEvent
|
||||||
|
{
|
||||||
|
public string Start { get; set; }
|
||||||
|
public string End { get; set; }
|
||||||
|
public string TimeZone { get; set; }
|
||||||
|
public bool Floating { get; set; }
|
||||||
|
public List<PrivaPubPlace> Places { get; set; } = new();
|
||||||
|
public string OnlineUrl { get; set; }
|
||||||
|
public string JoinMode { get; set; }
|
||||||
|
public int? Participants { get; set; }
|
||||||
|
public int? Capacity { get; set; }
|
||||||
|
public string Status { get; set; }
|
||||||
|
public string Category { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PrivaPubPlace
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Address { get; set; }
|
||||||
|
public double? Latitude { get; set; }
|
||||||
|
public double? Longitude { get; set; }
|
||||||
|
public string Url { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PrivaPubVotes
|
||||||
|
{
|
||||||
|
public int Up { get; set; }
|
||||||
|
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; }
|
||||||
|
public string Title { get; set; } = string.Empty;
|
||||||
|
public string Description { get; set; } = string.Empty;
|
||||||
|
public string Type { get; set; } = "link";
|
||||||
|
public string AuthorName { get; set; } = string.Empty;
|
||||||
|
public string AuthorUrl { get; set; } = string.Empty;
|
||||||
|
public string ProviderName { get; set; } = string.Empty;
|
||||||
|
public string ProviderUrl { get; set; } = string.Empty;
|
||||||
|
public string Html { get; set; } = string.Empty;
|
||||||
|
public int Width { get; set; }
|
||||||
|
public int Height { get; set; }
|
||||||
|
public string Image { get; set; }
|
||||||
|
public string ImageDescription { get; set; } = string.Empty;
|
||||||
|
public string EmbedUrl { get; set; } = string.Empty;
|
||||||
|
public string Blurhash { get; set; }
|
||||||
|
public string Language { get; set; }
|
||||||
|
public string PublishedAt { get; set; }
|
||||||
|
public List<object> Authors { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Status
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Uri { get; set; }
|
||||||
|
public string Url { get; set; }
|
||||||
|
public Account Account { get; set; }
|
||||||
|
public string InReplyToId { get; set; }
|
||||||
|
public string InReplyToAccountId { get; set; }
|
||||||
|
public Status Reblog { get; set; }
|
||||||
|
public string Content { get; set; } = string.Empty;
|
||||||
|
public string CreatedAt { get; set; }
|
||||||
|
public string EditedAt { get; set; }
|
||||||
|
public List<CustomEmojiEntity> Emojis { get; set; } = new();
|
||||||
|
public int RepliesCount { get; set; }
|
||||||
|
public int ReblogsCount { get; set; }
|
||||||
|
public int FavouritesCount { get; set; }
|
||||||
|
public bool Reblogged { get; set; }
|
||||||
|
public bool Favourited { get; set; }
|
||||||
|
public bool Muted { get; set; }
|
||||||
|
public bool Bookmarked { get; set; }
|
||||||
|
public bool Pinned { get; set; }
|
||||||
|
public bool Sensitive { get; set; }
|
||||||
|
public string SpoilerText { get; set; } = string.Empty;
|
||||||
|
public string Visibility { get; set; }
|
||||||
|
public List<MediaAttachment> MediaAttachments { get; set; } = new();
|
||||||
|
public List<StatusMention> Mentions { get; set; } = new();
|
||||||
|
public List<StatusTag> Tags { get; set; } = new();
|
||||||
|
public PreviewCard Card { 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
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Type { get; set; }
|
||||||
|
public string Url { get; set; }
|
||||||
|
public string PreviewUrl { get; set; }
|
||||||
|
public string RemoteUrl { get; set; }
|
||||||
|
public object Meta { get; set; }
|
||||||
|
public string Description { get; set; }
|
||||||
|
public string Blurhash { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StatusMention
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Username { get; set; }
|
||||||
|
public string Url { get; set; }
|
||||||
|
public string Acct { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StatusTag
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Url { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Application
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Website { get; set; }
|
||||||
|
public List<string> Scopes { get; set; } = new();
|
||||||
|
public List<string> RedirectUris { get; set; } = new();
|
||||||
|
public string RedirectUri { get; set; }
|
||||||
|
public string ClientId { get; set; }
|
||||||
|
public string ClientSecret { get; set; }
|
||||||
|
public string VapidKey { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Relationship
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public bool Following { get; set; }
|
||||||
|
public bool ShowingReblogs { get; set; }
|
||||||
|
public bool Notifying { get; set; }
|
||||||
|
public List<string> Languages { get; set; }
|
||||||
|
public bool FollowedBy { get; set; }
|
||||||
|
public bool Blocking { get; set; }
|
||||||
|
public bool BlockedBy { get; set; }
|
||||||
|
public bool Muting { get; set; }
|
||||||
|
public bool MutingNotifications { get; set; }
|
||||||
|
public bool Requested { get; set; }
|
||||||
|
public bool RequestedBy { get; set; }
|
||||||
|
public bool DomainBlocking { get; set; }
|
||||||
|
public bool Endorsed { get; set; }
|
||||||
|
public string Note { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Notification
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Type { get; set; }
|
||||||
|
public string CreatedAt { get; set; }
|
||||||
|
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
|
||||||
|
{
|
||||||
|
public List<Status> Ancestors { get; set; } = new();
|
||||||
|
public List<Status> Descendants { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StatusEdit
|
||||||
|
{
|
||||||
|
public string Content { get; set; }
|
||||||
|
public string SpoilerText { get; set; }
|
||||||
|
public bool Sensitive { get; set; }
|
||||||
|
public string CreatedAt { get; set; }
|
||||||
|
public Account Account { get; set; }
|
||||||
|
public List<MediaAttachment> MediaAttachments { get; set; } = new();
|
||||||
|
public List<CustomEmojiEntity> Emojis { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StatusSource
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Text { get; set; }
|
||||||
|
public string SpoilerText { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SearchResults
|
||||||
|
{
|
||||||
|
public List<Account> Accounts { get; set; } = new();
|
||||||
|
public List<Status> Statuses { get; set; } = new();
|
||||||
|
public List<object> Hashtags { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Conversation
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public bool Unread { get; set; }
|
||||||
|
public List<Account> Accounts { get; set; } = new();
|
||||||
|
public Status LastStatus { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Marker
|
||||||
|
{
|
||||||
|
public string LastReadId { get; set; }
|
||||||
|
public int Version { get; set; }
|
||||||
|
public string UpdatedAt { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using OpenIddict.Abstractions;
|
||||||
|
using OpenIddict.Validation.AspNetCore;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Auth;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using static OpenIddict.Abstractions.OpenIddictConstants;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Infrastructure
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
|
||||||
|
public sealed class ScopeAttribute : Attribute
|
||||||
|
{
|
||||||
|
public ScopeAttribute(string scope, bool requiresUser = true)
|
||||||
|
{
|
||||||
|
Scope = scope;
|
||||||
|
RequiresUser = requiresUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Scope { get; }
|
||||||
|
public bool RequiresUser { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[ApiController, Authorize(AuthenticationSchemes = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme)]
|
||||||
|
public abstract class MastodonController : ControllerBase, IAsyncActionFilter
|
||||||
|
{
|
||||||
|
MastodonParams _params;
|
||||||
|
|
||||||
|
protected LocalActor Me { get; private set; }
|
||||||
|
|
||||||
|
protected string MyId => Me?.Id;
|
||||||
|
|
||||||
|
protected MastodonParams Params => _params;
|
||||||
|
|
||||||
|
[NonAction]
|
||||||
|
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
||||||
|
{
|
||||||
|
_params = await MastodonParams.Read(Request, HttpContext.RequestAborted);
|
||||||
|
var scope = context.ActionDescriptor.EndpointMetadata.OfType<ScopeAttribute>().LastOrDefault();
|
||||||
|
if (User.Identity?.IsAuthenticated == true)
|
||||||
|
{
|
||||||
|
if (scope != default && !MastodonScopes.Grants(User.GetScopes(), scope.Scope))
|
||||||
|
{
|
||||||
|
context.Result = Error(StatusCodes.Status403Forbidden, "This action is outside the authorized scopes");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Me = await Resolve(User.GetClaim(Claims.Subject), HttpContext.RequestAborted);
|
||||||
|
}
|
||||||
|
if (scope?.RequiresUser == true && Me == default)
|
||||||
|
{
|
||||||
|
context.Result = Error(StatusCodes.Status401Unauthorized, "This method requires an authenticated user");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await next();
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<LocalActor> Resolve(string avatarId, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(avatarId))
|
||||||
|
return default;
|
||||||
|
var services = HttpContext.RequestServices;
|
||||||
|
var db = services.GetRequiredService<DbEntities>();
|
||||||
|
var link = await db.RootToAvatars.Match(r => r.AvatarId == avatarId).ExecuteFirstAsync(token);
|
||||||
|
if (link == default)
|
||||||
|
return default;
|
||||||
|
var root = await db.RootUsers.MatchID(link.RootId).ExecuteFirstAsync(token);
|
||||||
|
if (root is not { IsBanned: false, DeletedAt: null })
|
||||||
|
return default;
|
||||||
|
return await services.GetRequiredService<ILocalActorService>().FindById(LocalActorKind.Person, avatarId, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected JsonResult Json(object value, int status = StatusCodes.Status200OK) =>
|
||||||
|
new(value, MastodonJson.Options) { StatusCode = status };
|
||||||
|
|
||||||
|
protected static JsonResult Error(int status, string message) =>
|
||||||
|
new(new { error = message }, MastodonJson.Options) { StatusCode = status };
|
||||||
|
|
||||||
|
protected JsonResult NotFoundError() => Error(StatusCodes.Status404NotFound, "Record not found");
|
||||||
|
|
||||||
|
protected void Link(string path, string maxId, string minId)
|
||||||
|
{
|
||||||
|
var links = new List<string>();
|
||||||
|
var baseUrl = $"{Request.Scheme}://{Request.Host}{path}";
|
||||||
|
var query = Request.Query.Where(q => q.Key is not ("max_id" or "min_id" or "since_id")).Select(q => $"{q.Key}={Uri.EscapeDataString(q.Value.ToString())}").ToList();
|
||||||
|
string With(string key, string value) => baseUrl + "?" + string.Join("&", query.Append($"{key}={value}"));
|
||||||
|
if (maxId != default)
|
||||||
|
links.Add($"<{With("max_id", maxId)}>; rel=\"next\"");
|
||||||
|
if (minId != default)
|
||||||
|
links.Add($"<{With("min_id", minId)}>; rel=\"prev\"");
|
||||||
|
if (links.Count > 0)
|
||||||
|
Response.Headers["Link"] = string.Join(", ", links);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int Limit(int fallback = 20, int max = 40) => Math.Clamp(Params.Int("limit") ?? fallback, 1, max);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using System.Text.Encodings.Web;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Infrastructure
|
||||||
|
{
|
||||||
|
public static class MastodonJson
|
||||||
|
{
|
||||||
|
public static readonly JsonSerializerOptions Options = new()
|
||||||
|
{
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
|
||||||
|
DictionaryKeyPolicy = default,
|
||||||
|
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
|
||||||
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||||
|
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseLower) }
|
||||||
|
};
|
||||||
|
|
||||||
|
public static string Time(DateTime value) =>
|
||||||
|
DateTime.SpecifyKind(value, DateTimeKind.Utc).ToString("yyyy-MM-ddTHH:mm:ss.fffZ", System.Globalization.CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
|
public static string Day(DateTime value) =>
|
||||||
|
DateTime.SpecifyKind(value, DateTimeKind.Utc).Date.ToString("yyyy-MM-ddT00:00:00.000Z", System.Globalization.CultureInfo.InvariantCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
using Microsoft.AspNetCore.Http.Features;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Infrastructure
|
||||||
|
{
|
||||||
|
public sealed class MastodonParams
|
||||||
|
{
|
||||||
|
readonly Dictionary<string, List<string>> _values;
|
||||||
|
|
||||||
|
MastodonParams(Dictionary<string, List<string>> values)
|
||||||
|
{
|
||||||
|
_values = values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<MastodonParams> Read(HttpRequest request, CancellationToken token)
|
||||||
|
{
|
||||||
|
var values = new Dictionary<string, List<string>>(StringComparer.Ordinal);
|
||||||
|
foreach (var pair in request.Query)
|
||||||
|
foreach (var value in pair.Value)
|
||||||
|
Add(values, pair.Key, value);
|
||||||
|
|
||||||
|
if (request.HasFormContentType)
|
||||||
|
{
|
||||||
|
var form = await request.ReadFormAsync(token);
|
||||||
|
foreach (var pair in form)
|
||||||
|
foreach (var value in pair.Value)
|
||||||
|
Add(values, pair.Key, value);
|
||||||
|
}
|
||||||
|
else if (request.ContentType?.Contains("json", StringComparison.OrdinalIgnoreCase) == true && request.ContentLength != 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var document = await JsonDocument.ParseAsync(request.Body, cancellationToken: token);
|
||||||
|
Flatten(values, default, document.RootElement);
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new MastodonParams(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MastodonParams From(IEnumerable<KeyValuePair<string, string>> pairs)
|
||||||
|
{
|
||||||
|
var values = new Dictionary<string, List<string>>(StringComparer.Ordinal);
|
||||||
|
foreach (var pair in pairs)
|
||||||
|
Add(values, pair.Key, pair.Value);
|
||||||
|
return new MastodonParams(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Has(string name) => _values.ContainsKey(name);
|
||||||
|
|
||||||
|
public string Get(string name) => _values.TryGetValue(name, out var list) && list.Count > 0 ? list[^1] : default;
|
||||||
|
|
||||||
|
public IReadOnlyList<string> List(string name) => _values.TryGetValue(name, out var list) ? list : (IReadOnlyList<string>)Array.Empty<string>();
|
||||||
|
|
||||||
|
public bool? Bool(string name) => Get(name)?.ToLowerInvariant() switch
|
||||||
|
{
|
||||||
|
"true" or "1" or "on" or "yes" => true,
|
||||||
|
"false" or "0" or "off" or "no" or "" => false,
|
||||||
|
_ => (bool?)null
|
||||||
|
};
|
||||||
|
|
||||||
|
public int? Int(string name) => int.TryParse(Get(name), out var value) ? value : (int?)null;
|
||||||
|
|
||||||
|
static void Add(Dictionary<string, List<string>> values, string key, string value)
|
||||||
|
{
|
||||||
|
key = key.EndsWith("[]", StringComparison.Ordinal) ? key[..^2] : key;
|
||||||
|
if (!values.TryGetValue(key, out var list))
|
||||||
|
values[key] = list = new List<string>();
|
||||||
|
list.Add(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Flatten(Dictionary<string, List<string>> values, string prefix, JsonElement element)
|
||||||
|
{
|
||||||
|
switch (element.ValueKind)
|
||||||
|
{
|
||||||
|
case JsonValueKind.Object:
|
||||||
|
foreach (var property in element.EnumerateObject())
|
||||||
|
Flatten(values, prefix == default ? property.Name : $"{prefix}[{property.Name}]", property.Value);
|
||||||
|
break;
|
||||||
|
case JsonValueKind.Array:
|
||||||
|
foreach (var item in element.EnumerateArray())
|
||||||
|
{
|
||||||
|
if (item.ValueKind is JsonValueKind.Object or JsonValueKind.Array)
|
||||||
|
Flatten(values, prefix + "[]", item);
|
||||||
|
else
|
||||||
|
Add(values, prefix, Scalar(item));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case JsonValueKind.Null or JsonValueKind.Undefined:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (prefix != default)
|
||||||
|
Add(values, prefix, Scalar(element));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static string Scalar(JsonElement element) => element.ValueKind switch
|
||||||
|
{
|
||||||
|
JsonValueKind.String => element.GetString(),
|
||||||
|
JsonValueKind.True => "true",
|
||||||
|
JsonValueKind.False => "false",
|
||||||
|
_ => element.GetRawText()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Infrastructure
|
||||||
|
{
|
||||||
|
public sealed record Page(string MaxId, string SinceId, string MinId, int Limit)
|
||||||
|
{
|
||||||
|
public static Page From(MastodonParams parameters, int limit) =>
|
||||||
|
new(parameters.Get("max_id"), parameters.Get("since_id"), parameters.Get("min_id"), limit);
|
||||||
|
|
||||||
|
public async Task<List<T>> Fetch<T>(Find<T, T> query, Expression<Func<T, string>> id, CancellationToken token) where T : IEntity
|
||||||
|
{
|
||||||
|
var field = new ExpressionFieldDefinition<T, string>(id);
|
||||||
|
if (!string.IsNullOrEmpty(MaxId))
|
||||||
|
query.Match(Builders<T>.Filter.Lt(field, MaxId));
|
||||||
|
if (!string.IsNullOrEmpty(SinceId))
|
||||||
|
query.Match(Builders<T>.Filter.Gt(field, SinceId));
|
||||||
|
if (!string.IsNullOrEmpty(MinId))
|
||||||
|
query.Match(Builders<T>.Filter.Gt(field, MinId));
|
||||||
|
var ascending = !string.IsNullOrEmpty(MinId);
|
||||||
|
var sortField = new ExpressionFieldDefinition<T>(Expression.Lambda<Func<T, object>>(Expression.Convert(id.Body, typeof(object)), id.Parameters));
|
||||||
|
var items = await query.Sort(s => ascending ? s.Ascending(sortField) : s.Descending(sortField)).Limit(Limit).ExecuteAsync(token);
|
||||||
|
if (ascending)
|
||||||
|
items.Reverse();
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
using PrivaPub.Api.Mastodon.Entities;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Mappers
|
||||||
|
{
|
||||||
|
public class AccountSearch
|
||||||
|
{
|
||||||
|
readonly MastodonMapper _mapper;
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
|
||||||
|
public AccountSearch(MastodonMapper mapper, DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors)
|
||||||
|
{
|
||||||
|
_mapper = mapper;
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<Account>> Find(string q, bool resolve, int limit, CancellationToken token)
|
||||||
|
{
|
||||||
|
q = q?.Trim().TrimStart('@');
|
||||||
|
if (string.IsNullOrEmpty(q))
|
||||||
|
return new List<Account>();
|
||||||
|
var results = new List<Account>();
|
||||||
|
var parts = q.Split('@');
|
||||||
|
var localDomain = new Uri(_localActors.BaseAddress).Authority;
|
||||||
|
if (parts.Length == 2 && !parts[1].Equals(localDomain, StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(parts[0]))
|
||||||
|
{
|
||||||
|
var userName = parts[0];
|
||||||
|
var domain = parts[1].ToLowerInvariant();
|
||||||
|
var known = await _dbEntities.ForeignAvatars.Match(f => f.UserName == userName && f.Domain == domain).ExecuteFirstAsync(token);
|
||||||
|
if (known == default && resolve)
|
||||||
|
{
|
||||||
|
var actorUri = await _remoteActors.ResolveHandle(q, token);
|
||||||
|
known = actorUri == default ? default : await _remoteActors.GetActor(actorUri, refresh: false, token);
|
||||||
|
}
|
||||||
|
if (known != default)
|
||||||
|
results.Add(_mapper.Foreign(known));
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
var prefix = parts[0].ToLowerInvariant();
|
||||||
|
foreach (var avatar in await _dbEntities.Avatars.Match(a => a.UserName.StartsWith(prefix) && !a.DeletionAt.HasValue && a.Settings.IsDiscoverable != false)
|
||||||
|
.Limit(limit).ExecuteAsync(token))
|
||||||
|
results.Add(await _mapper.Local(_localActors.FromAvatar(avatar), false, token));
|
||||||
|
foreach (var foreign in await _dbEntities.ForeignAvatars.Match(f => f.UserName.StartsWith(parts[0]) && f.IsDiscoverable)
|
||||||
|
.Limit(Math.Max(0, limit - results.Count)).ExecuteAsync(token))
|
||||||
|
results.Add(_mapper.Foreign(foreign));
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,534 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Entities;
|
||||||
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||||
|
using PrivaPub.Domain.Media;
|
||||||
|
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;
|
||||||
|
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
using GroupEntity = PrivaPub.Models.Group.Group;
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Api.Mastodon.Mappers
|
||||||
|
{
|
||||||
|
public class MastodonMapper
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IMediaProxy _proxy;
|
||||||
|
|
||||||
|
public MastodonMapper(DbEntities dbEntities, ILocalActorService localActors, IMediaProxy proxy = default)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_proxy = proxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
|
|
||||||
|
public async Task<Account> Local(LocalActor actor, bool withSource, CancellationToken token)
|
||||||
|
{
|
||||||
|
var followers = await DB.Default.CountAsync<Follower>(f => f.LocalActorId == actor.Id && f.LocalActorKind == actor.Kind && f.IsAccepted, token);
|
||||||
|
var following = actor.Kind == LocalActorKind.Person
|
||||||
|
? await DB.Default.CountAsync<Following>(f => f.AvatarId == actor.Id && f.State == FollowState.Accepted, token)
|
||||||
|
: 0;
|
||||||
|
var latest = await _dbEntities.Posts
|
||||||
|
.Match(p => (actor.Kind == LocalActorKind.Group ? p.GroupId == actor.Id : p.GroupUserId == actor.Id) && !p.IsFederatedCopy
|
||||||
|
&& !p.DeletedAt.HasValue && p.Visibility != PostVisibility.Direct && p.ReblogOfPostId == null)
|
||||||
|
.Sort(p => p.ID, Order.Descending)
|
||||||
|
.Limit(1)
|
||||||
|
.ExecuteFirstAsync(token);
|
||||||
|
var statuses = latest == default
|
||||||
|
? 0
|
||||||
|
: await DB.Default.CountAsync<PostEntity>(p => (actor.Kind == LocalActorKind.Group ? p.GroupId == actor.Id : p.GroupUserId == actor.Id)
|
||||||
|
&& !p.IsFederatedCopy && !p.DeletedAt.HasValue && p.Visibility != PostVisibility.Direct && p.ReblogOfPostId == null, token);
|
||||||
|
|
||||||
|
var account = new Account
|
||||||
|
{
|
||||||
|
Id = actor.Id,
|
||||||
|
Username = actor.UserName,
|
||||||
|
Acct = actor.UserName,
|
||||||
|
DisplayName = actor.Name ?? actor.UserName,
|
||||||
|
Locked = actor.ManuallyApprovesFollowers,
|
||||||
|
Bot = actor.IsBot,
|
||||||
|
Discoverable = actor.Discoverable,
|
||||||
|
Indexable = actor.Indexable,
|
||||||
|
Noindex = !actor.Indexable,
|
||||||
|
HideCollections = actor.Settings.HideCollections,
|
||||||
|
Group = actor.Kind == LocalActorKind.Group,
|
||||||
|
CreatedAt = MastodonJson.Day(actor.Published),
|
||||||
|
Note = ActivityPubRenderer.Html(actor.Summary),
|
||||||
|
Url = actor.HtmlUrl,
|
||||||
|
Uri = actor.Uri,
|
||||||
|
Avatar = actor.PictureURL ?? MissingAvatar,
|
||||||
|
AvatarStatic = actor.PictureURL ?? MissingAvatar,
|
||||||
|
Header = actor.ThumbnailURL ?? MissingHeader,
|
||||||
|
HeaderStatic = actor.ThumbnailURL ?? MissingHeader,
|
||||||
|
FollowersCount = (int)followers,
|
||||||
|
FollowingCount = (int)following,
|
||||||
|
StatusesCount = (int)statuses,
|
||||||
|
LastStatusAt = latest == default ? default : latest.CreationDate.ToString("yyyy-MM-dd"),
|
||||||
|
Fields = actor.Fields.Select(f => new Field { Name = f.Key, Value = ActivityPubRenderer.FieldValue(f.Value) }).ToList()
|
||||||
|
};
|
||||||
|
if (withSource)
|
||||||
|
account.Source = new Source
|
||||||
|
{
|
||||||
|
Privacy = actor.Settings.DefaultVisibility ?? "public",
|
||||||
|
Sensitive = actor.Settings.DefaultSensitive,
|
||||||
|
Language = actor.Settings.DefaultLanguage,
|
||||||
|
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)
|
||||||
|
};
|
||||||
|
return account;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Account Foreign(ForeignAvatar foreign) => new()
|
||||||
|
{
|
||||||
|
Id = foreign.ID,
|
||||||
|
Username = foreign.UserName,
|
||||||
|
Acct = $"{foreign.UserName}@{foreign.Domain}",
|
||||||
|
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,
|
||||||
|
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,
|
||||||
|
Avatar = Proxied(foreign.PictureURL) ?? MissingAvatar,
|
||||||
|
AvatarStatic = Proxied(foreign.PictureURL) ?? MissingAvatar,
|
||||||
|
Header = Proxied(foreign.ThumbnailURL) ?? MissingHeader,
|
||||||
|
HeaderStatic = Proxied(foreign.ThumbnailURL) ?? MissingHeader
|
||||||
|
};
|
||||||
|
|
||||||
|
public async Task<Account> Account(string id, CancellationToken token) =>
|
||||||
|
(await Accounts(new[] { id }, token)).GetValueOrDefault(id);
|
||||||
|
|
||||||
|
public async Task<Dictionary<string, Account>> Accounts(IEnumerable<string> ids, CancellationToken token)
|
||||||
|
{
|
||||||
|
var wanted = ids.Where(id => !string.IsNullOrEmpty(id)).Distinct().ToList();
|
||||||
|
var accounts = new Dictionary<string, Account>();
|
||||||
|
if (wanted.Count == 0)
|
||||||
|
return accounts;
|
||||||
|
|
||||||
|
foreach (var avatar in await _dbEntities.Avatars.Match(a => wanted.Contains(a.ID) && !a.DeletionAt.HasValue).ExecuteAsync(token))
|
||||||
|
accounts[avatar.ID] = await Local(_localActors.FromAvatar(avatar), false, token);
|
||||||
|
foreach (var group in await _dbEntities.Groups.Match(g => wanted.Contains(g.ID) && !g.DeletionAt.HasValue && g.Kind == GroupKind.Community).ExecuteAsync(token))
|
||||||
|
accounts[group.ID] = await Local(_localActors.FromGroup(group), false, token);
|
||||||
|
foreach (var foreign in await _dbEntities.ForeignAvatars.Match(f => wanted.Contains(f.ID)).ExecuteAsync(token))
|
||||||
|
accounts[foreign.ID] = Foreign(foreign);
|
||||||
|
return accounts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Status> Status(PostEntity post, string viewerId, CancellationToken token) =>
|
||||||
|
(await Statuses(new[] { post }, viewerId, token)).FirstOrDefault();
|
||||||
|
|
||||||
|
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
|
||||||
|
? new Dictionary<string, PostEntity>()
|
||||||
|
: (await _dbEntities.Posts.Match(p => originalIds.Contains(p.ID) && !p.DeletedAt.HasValue).ExecuteAsync(token)).ToDictionary(p => p.ID);
|
||||||
|
var all = posts.Concat(originals.Values).ToList();
|
||||||
|
|
||||||
|
var mentionUris = all.SelectMany(p => p.Mentions).Where(m => !m.IsLocal).Select(m => m.ActorURI).Distinct().ToList();
|
||||||
|
var mentionAccounts = mentionUris.Count == 0
|
||||||
|
? new Dictionary<string, ForeignAvatar>()
|
||||||
|
: (await _dbEntities.ForeignAvatars.Match(f => mentionUris.Contains(f.ActorURI)).ExecuteAsync(token)).ToDictionary(f => f.ActorURI);
|
||||||
|
var accounts = await Accounts(all.Select(AuthorOf), token);
|
||||||
|
|
||||||
|
var ids = all.Select(p => p.ID).ToList();
|
||||||
|
var favourited = viewerId == default
|
||||||
|
? new HashSet<string>()
|
||||||
|
: (await _dbEntities.Favourites.Match(f => f.AccountId == viewerId && ids.Contains(f.PostId)).ExecuteAsync(token)).Select(f => f.PostId).ToHashSet();
|
||||||
|
var bookmarked = viewerId == default
|
||||||
|
? 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);
|
||||||
|
var reblogged = viewerId == default
|
||||||
|
? new HashSet<string>()
|
||||||
|
: (await _dbEntities.Posts.Match(p => p.AuthorAccountId == viewerId && ids.Contains(p.ReblogOfPostId) && !p.DeletedAt.HasValue).ExecuteAsync(token))
|
||||||
|
.Select(p => p.ReblogOfPostId).ToHashSet();
|
||||||
|
|
||||||
|
Status Map(PostEntity post)
|
||||||
|
{
|
||||||
|
if (!accounts.TryGetValue(AuthorOf(post), out var account))
|
||||||
|
return default;
|
||||||
|
var status = new Status
|
||||||
|
{
|
||||||
|
Id = post.ID,
|
||||||
|
Uri = post.ObjectURI,
|
||||||
|
Url = post.Url ?? post.ObjectURI,
|
||||||
|
Account = account,
|
||||||
|
InReplyToId = post.AnsweringToPostId,
|
||||||
|
InReplyToAccountId = post.InReplyToAccountId,
|
||||||
|
Content = Content(post),
|
||||||
|
CreatedAt = MastodonJson.Time(post.CreationDate),
|
||||||
|
EditedAt = post.EditedAt.HasValue ? MastodonJson.Time(post.EditedAt.Value) : default,
|
||||||
|
RepliesCount = post.RepliesCount,
|
||||||
|
ReblogsCount = post.ReblogsCount,
|
||||||
|
FavouritesCount = post.FavouritesCount,
|
||||||
|
Favourited = favourited.Contains(post.ID),
|
||||||
|
Reblogged = reblogged.Contains(post.ID),
|
||||||
|
Bookmarked = bookmarked.Contains(post.ID),
|
||||||
|
Pinned = pinned.Contains(post.ID),
|
||||||
|
Sensitive = post.HasContentWarning,
|
||||||
|
SpoilerText = post.SpoilerText ?? (post.HasContentWarning ? post.Title ?? ActivityPubRenderer.ContentWarning : string.Empty),
|
||||||
|
Visibility = Visibility(post.Visibility),
|
||||||
|
Language = post.Language,
|
||||||
|
MediaAttachments = post.Media.Select(Media).ToList(),
|
||||||
|
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 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 = Approval(post, viewerId);
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if (hidden.Contains(post.ActorURI) || post.ReblogOfPostId != default && originals.TryGetValue(post.ReblogOfPostId, out var hiddenOriginal)
|
||||||
|
&& hidden.Contains(hiddenOriginal.ActorURI))
|
||||||
|
continue;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
mapped.Add(status);
|
||||||
|
}
|
||||||
|
return mapped;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
if (post.IsFederatedCopy && string.IsNullOrWhiteSpace(content) && !string.IsNullOrEmpty(post.Excerpt))
|
||||||
|
content = $"<p>{WebUtility.HtmlEncode(post.Excerpt)}</p>";
|
||||||
|
if (post.Event is { } happening && EventLine(happening) is { } line)
|
||||||
|
content = $"<p>{WebUtility.HtmlEncode(line)}</p>{content}";
|
||||||
|
var titled = !string.IsNullOrEmpty(post.Title) && (!post.IsFederatedCopy || post.ObjectType is not (null or "Note" or "Question"));
|
||||||
|
return titled
|
||||||
|
? $"<p><strong>{WebUtility.HtmlEncode(post.Title)}</strong></p>{content}"
|
||||||
|
: content ?? string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string EventLine(EventDetails happening)
|
||||||
|
{
|
||||||
|
if (happening.Start is not { } start)
|
||||||
|
return default;
|
||||||
|
var zone = Zone(happening.FloatingTime ? default : happening.TimeZone);
|
||||||
|
var local = zone == default ? start : TimeZoneInfo.ConvertTimeFromUtc(start, zone);
|
||||||
|
var when = local.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
|
||||||
|
if (happening.End is { } end && end > start)
|
||||||
|
{
|
||||||
|
var localEnd = zone == default ? end : TimeZoneInfo.ConvertTimeFromUtc(end, zone);
|
||||||
|
when += " – " + localEnd.ToString(localEnd.Date == local.Date ? "HH:mm" : "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
|
||||||
|
}
|
||||||
|
when += happening.FloatingTime ? " (local time)" : zone == default ? " UTC" : $" ({happening.TimeZone})";
|
||||||
|
var where = happening.Places.Select(p => p.Name ?? p.Address).FirstOrDefault(p => p != default)
|
||||||
|
?? (happening.OnlineURL != default ? "online" : default);
|
||||||
|
return where == default ? when : $"{when} · {where}";
|
||||||
|
}
|
||||||
|
|
||||||
|
static TimeZoneInfo Zone(string id)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(id))
|
||||||
|
return default;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return TimeZoneInfo.FindSystemTimeZoneById(id);
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex is TimeZoneNotFoundException or InvalidTimeZoneException)
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
Title = post.IsFederatedCopy ? post.Title : default,
|
||||||
|
Excerpt = post.Excerpt,
|
||||||
|
Cover = Proxied(post.CoverURL),
|
||||||
|
Source = post.Source == default ? default : new PrivaPubSource { Content = post.Source.Content, MediaType = post.Source.MediaType },
|
||||||
|
Link = post.Link == default ? default : new PrivaPubLink
|
||||||
|
{
|
||||||
|
Url = post.Link.Href, Title = post.Link.Title, Description = post.Link.Description, Image = Proxied(post.Link.ImageURL), FromPublisher = post.Link.FromPublisher
|
||||||
|
},
|
||||||
|
Video = post.Video == default ? default : new PrivaPubVideo
|
||||||
|
{
|
||||||
|
Duration = post.Video.DurationSeconds,
|
||||||
|
Variants = post.Video.Variants.Select(Variant).ToList(),
|
||||||
|
IsLive = post.Video.IsLive,
|
||||||
|
State = post.Video.State,
|
||||||
|
Views = post.Video.Views,
|
||||||
|
CommentsPolicy = post.Video.CommentsPolicy,
|
||||||
|
DownloadEnabled = post.Video.DownloadEnabled,
|
||||||
|
Licence = post.Video.Licence,
|
||||||
|
Category = post.Video.Category,
|
||||||
|
Channel = post.Video.ChannelURI,
|
||||||
|
Captions = post.Video.Captions.Select(c => new PrivaPubCaption { Language = c.Language, Url = Proxied(c.Url) }).ToList()
|
||||||
|
},
|
||||||
|
Audio = post.Audio == default ? default : new PrivaPubAudio
|
||||||
|
{
|
||||||
|
Duration = post.Audio.DurationSeconds,
|
||||||
|
Variants = post.Audio.Variants.Select(Variant).ToList(),
|
||||||
|
Album = post.Audio.Album,
|
||||||
|
Position = post.Audio.Position,
|
||||||
|
Licence = post.Audio.Licence
|
||||||
|
},
|
||||||
|
Event = post.Event == default ? default : new PrivaPubEvent
|
||||||
|
{
|
||||||
|
Start = post.Event.Start is { } start ? MastodonJson.Time(start) : default,
|
||||||
|
End = post.Event.End is { } end ? MastodonJson.Time(end) : default,
|
||||||
|
TimeZone = post.Event.TimeZone,
|
||||||
|
Floating = post.Event.FloatingTime,
|
||||||
|
Places = post.Event.Places.Select(p => new PrivaPubPlace { Name = p.Name, Address = p.Address, Latitude = p.Latitude, Longitude = p.Longitude, Url = p.Url }).ToList(),
|
||||||
|
OnlineUrl = post.Event.OnlineURL,
|
||||||
|
JoinMode = post.Event.JoinMode,
|
||||||
|
Participants = post.Event.ParticipantCount,
|
||||||
|
Capacity = post.Event.Capacity,
|
||||||
|
Status = post.Event.Status,
|
||||||
|
Category = post.Event.Category
|
||||||
|
},
|
||||||
|
Votes = new PrivaPubVotes { Up = post.FavouritesCount, Down = post.DownvotesCount }
|
||||||
|
};
|
||||||
|
|
||||||
|
PrivaPubVariant Variant(MediaVariant variant) => new()
|
||||||
|
{
|
||||||
|
Url = Proxied(variant.Url),
|
||||||
|
MediaType = variant.MediaType,
|
||||||
|
Width = variant.Width,
|
||||||
|
Height = variant.Height,
|
||||||
|
Fps = variant.Fps,
|
||||||
|
Size = variant.Size,
|
||||||
|
Streams = variant.Streams
|
||||||
|
};
|
||||||
|
|
||||||
|
PreviewCard Card(PostEntity post)
|
||||||
|
{
|
||||||
|
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.IsFederatedCopy && post.ObjectType is "Article" or "Event" or "Video" or "Audio" && post.Url != default && post.Media.Count == 0)
|
||||||
|
return Card(post.Url, post.Title, post.Excerpt, post.CoverURL);
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
PreviewCard Card(string url, string title, string description, string image)
|
||||||
|
{
|
||||||
|
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
|
||||||
|
return default;
|
||||||
|
return new PreviewCard
|
||||||
|
{
|
||||||
|
Url = url,
|
||||||
|
Title = title ?? uri.Host,
|
||||||
|
Description = description ?? string.Empty,
|
||||||
|
ProviderName = uri.Host,
|
||||||
|
ProviderUrl = uri.GetLeftPart(UriPartial.Authority),
|
||||||
|
Image = Proxied(image)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string Visibility(PostVisibility visibility) => visibility switch
|
||||||
|
{
|
||||||
|
PostVisibility.Unlisted => "unlisted",
|
||||||
|
PostVisibility.FollowersOnly or PostVisibility.Circle => "private",
|
||||||
|
PostVisibility.Direct => "direct",
|
||||||
|
_ => "public"
|
||||||
|
};
|
||||||
|
|
||||||
|
StatusMention Mention(PostMention mention, IReadOnlyDictionary<string, ForeignAvatar> foreign)
|
||||||
|
{
|
||||||
|
if (mention.IsLocal)
|
||||||
|
{
|
||||||
|
var handle = mention.Handle?.TrimStart('@').Split('@')[0];
|
||||||
|
return new StatusMention { Id = mention.AccountId, Username = handle, Acct = handle, Url = mention.ActorURI };
|
||||||
|
}
|
||||||
|
if (!foreign.TryGetValue(mention.ActorURI, out var account))
|
||||||
|
return default;
|
||||||
|
return new StatusMention { Id = account.ID, Username = account.UserName, Acct = $"{account.UserName}@{account.Domain}", Url = account.Url ?? account.ActorURI };
|
||||||
|
}
|
||||||
|
|
||||||
|
static Dictionary<string, object> Meta(PostMedia media)
|
||||||
|
{
|
||||||
|
var original = new Dictionary<string, object>();
|
||||||
|
if (media.Width > 0 && media.Height > 0)
|
||||||
|
{
|
||||||
|
original["width"] = media.Width;
|
||||||
|
original["height"] = media.Height;
|
||||||
|
original["size"] = $"{media.Width}x{media.Height}";
|
||||||
|
original["aspect"] = (double)media.Width.Value / media.Height.Value;
|
||||||
|
}
|
||||||
|
if (media.DurationSeconds is { } seconds)
|
||||||
|
original["duration"] = seconds;
|
||||||
|
var meta = new Dictionary<string, object>();
|
||||||
|
if (original.Count > 0)
|
||||||
|
meta["original"] = original;
|
||||||
|
if (media.DurationSeconds is { } length)
|
||||||
|
meta["length"] = TimeSpan.FromSeconds(length).ToString(@"h\:mm\:ss\.ff", CultureInfo.InvariantCulture);
|
||||||
|
if (media.Focus is { Length: 2 })
|
||||||
|
meta["focus"] = new { x = media.Focus[0], y = media.Focus[1] };
|
||||||
|
return meta;
|
||||||
|
}
|
||||||
|
|
||||||
|
MediaAttachment Media(PostMedia media) => new()
|
||||||
|
{
|
||||||
|
Id = media.AttachmentId ?? media.Id.ToString("N"),
|
||||||
|
Type = media.ContentType switch
|
||||||
|
{
|
||||||
|
{ } type when type.StartsWith("image/gif") => "gifv",
|
||||||
|
{ } type when type.StartsWith("image/") => "image",
|
||||||
|
{ } type when type.StartsWith("video/") => "video",
|
||||||
|
{ } type when type.StartsWith("audio/") => "audio",
|
||||||
|
_ => "unknown"
|
||||||
|
},
|
||||||
|
Url = media.URL ?? Proxied(media.RemoteURL),
|
||||||
|
PreviewUrl = media.PreviewURL ?? Proxied(media.RemotePreviewURL)
|
||||||
|
?? (media.ContentType?.StartsWith("image/") == true || media.ContentType == default ? media.URL ?? Proxied(media.RemoteURL) : default),
|
||||||
|
RemoteUrl = media.RemoteURL,
|
||||||
|
Description = media.Description,
|
||||||
|
Blurhash = media.Blurhash,
|
||||||
|
Meta = Meta(media)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,47 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels;
|
||||||
|
using PrivaPub.Extensions;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
|
||||||
namespace PrivaPub.Controllers.ClientToServer
|
namespace PrivaPub.Controllers.ClientToServer
|
||||||
{
|
{
|
||||||
[ApiController,
|
[ApiController,
|
||||||
Route("clientapi/moderator")]
|
Route("clientapi/moderator"),
|
||||||
|
Authorize(Policy = Policies.IsModerator)]
|
||||||
public class ModeratorController : ControllerBase
|
public class ModeratorController : ControllerBase
|
||||||
{
|
{
|
||||||
|
[HttpGet, Route("/clientapi/moderator/reports")]
|
||||||
|
public async Task<IActionResult> Reports([FromQuery] bool resolved, CancellationToken token) =>
|
||||||
|
Ok((await DB.Default.Find<Report>().Match(r => r.IsResolved == resolved).Sort(r => r.ID, Order.Descending).Limit(100).ExecuteAsync(token))
|
||||||
|
.Select(r => new
|
||||||
|
{
|
||||||
|
r.ID,
|
||||||
|
Reporter = r.ReporterAvatarId != default ? "local" : r.ReporterActorURI,
|
||||||
|
r.TargetAccountId,
|
||||||
|
r.TargetActorURI,
|
||||||
|
r.PostIds,
|
||||||
|
r.ObjectURIs,
|
||||||
|
r.Category,
|
||||||
|
r.Comment,
|
||||||
|
r.Forwarded,
|
||||||
|
r.IsResolved,
|
||||||
|
r.CreatedAt,
|
||||||
|
r.ResolvedAt
|
||||||
|
}));
|
||||||
|
|
||||||
|
[HttpPost, Route("/clientapi/moderator/reports/{id}/resolve")]
|
||||||
|
public async Task<IActionResult> Resolve(string id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var result = await DB.Default.Update<Report>().MatchID(id)
|
||||||
|
.Modify(r => r.IsResolved, true)
|
||||||
|
.Modify(r => r.ResolvedAt, DateTime.UtcNow)
|
||||||
|
.Modify(r => r.ResolvedBy, User.GetUserId())
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
return result.MatchedCount == 0 ? NotFound() : Ok();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,6 +32,14 @@ namespace PrivaPub.Controllers.ClientToServer
|
|||||||
public async Task<IActionResult> Insert(InsertPostForm form, CancellationToken token) =>
|
public async Task<IActionResult> Insert(InsertPostForm form, CancellationToken token) =>
|
||||||
!ModelState.IsValid ? Invalid() : Answer(await _postsService.InsertPost(User.GetUserId(), form, token));
|
!ModelState.IsValid ? Invalid() : Answer(await _postsService.InsertPost(User.GetUserId(), form, token));
|
||||||
|
|
||||||
|
[HttpGet, Route("/clientapi/post/nearby")]
|
||||||
|
public async Task<IActionResult> Nearby([FromQuery] string avatarId, [FromQuery] double latitude, [FromQuery] double longitude, CancellationToken token) =>
|
||||||
|
Answer(await _postsService.Nearby(User.GetUserId(), avatarId, latitude, longitude, token));
|
||||||
|
|
||||||
|
[HttpPost, Route("/clientapi/post/update")]
|
||||||
|
public async Task<IActionResult> Update(UpdatePostForm form, CancellationToken token) =>
|
||||||
|
!ModelState.IsValid ? Invalid() : Answer(await _postsService.UpdatePost(User.GetUserId(), form, token));
|
||||||
|
|
||||||
[HttpPost, Route("/clientapi/post/delete")]
|
[HttpPost, Route("/clientapi/post/delete")]
|
||||||
public async Task<IActionResult> Delete(DeletePostForm form, CancellationToken token) =>
|
public async Task<IActionResult> Delete(DeletePostForm form, CancellationToken token) =>
|
||||||
!ModelState.IsValid ? Invalid() : Answer(await _postsService.DeletePost(User.GetUserId(), form, token));
|
!ModelState.IsValid ? Invalid() : Answer(await _postsService.DeletePost(User.GetUserId(), form, token));
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Localization;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels;
|
||||||
|
using PrivaPub.ClientModels.Social;
|
||||||
|
using PrivaPub.Domain.Social;
|
||||||
|
using PrivaPub.Extensions;
|
||||||
|
using PrivaPub.Resources;
|
||||||
|
|
||||||
|
namespace PrivaPub.Controllers.ClientToServer
|
||||||
|
{
|
||||||
|
[ApiController,
|
||||||
|
Route("clientapi/social"),
|
||||||
|
Authorize(Policy = Policies.IsUser)]
|
||||||
|
public class SocialController : ControllerBase
|
||||||
|
{
|
||||||
|
readonly IFollowService _follows;
|
||||||
|
readonly IStringLocalizer _localizer;
|
||||||
|
|
||||||
|
public SocialController(IFollowService follows, IStringLocalizer<GenericRes> localizer)
|
||||||
|
{
|
||||||
|
_follows = follows;
|
||||||
|
_localizer = localizer;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost, Route("/clientapi/follow")]
|
||||||
|
public async Task<IActionResult> Follow(FollowForm form, CancellationToken token) =>
|
||||||
|
!ModelState.IsValid ? Invalid() : Answer(await _follows.Follow(User.GetUserId(), form, token));
|
||||||
|
|
||||||
|
[HttpPost, Route("/clientapi/unfollow")]
|
||||||
|
public async Task<IActionResult> Unfollow(FollowForm form, CancellationToken token) =>
|
||||||
|
!ModelState.IsValid ? Invalid() : Answer(await _follows.Unfollow(User.GetUserId(), form, token));
|
||||||
|
|
||||||
|
[HttpGet, Route("/clientapi/following")]
|
||||||
|
public async Task<IActionResult> Following([FromQuery] string avatarId, CancellationToken token) =>
|
||||||
|
Answer(await _follows.Followings(User.GetUserId(), avatarId, token));
|
||||||
|
|
||||||
|
IActionResult Invalid() => BadRequest(new WebResult().Invalidate(_localizer["Invalid model."]));
|
||||||
|
|
||||||
|
IActionResult Answer(WebResult result) => result.IsValid ? Ok(result.Data) : StatusCode(result.StatusCode, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels;
|
||||||
|
using PrivaPub.Domain.Timelines;
|
||||||
|
using PrivaPub.Extensions;
|
||||||
|
|
||||||
|
namespace PrivaPub.Controllers.ClientToServer
|
||||||
|
{
|
||||||
|
[ApiController,
|
||||||
|
Route("clientapi/timeline"),
|
||||||
|
Authorize(Policy = Policies.IsUser)]
|
||||||
|
public class TimelineController : ControllerBase
|
||||||
|
{
|
||||||
|
readonly ITimelineService _timelines;
|
||||||
|
|
||||||
|
public TimelineController(ITimelineService timelines)
|
||||||
|
{
|
||||||
|
_timelines = timelines;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet, Route("/clientapi/timeline/home")]
|
||||||
|
public async Task<IActionResult> Home([FromQuery] string avatarId, [FromQuery(Name = "max_id")] string maxId, [FromQuery] int limit = 20,
|
||||||
|
CancellationToken token = default) =>
|
||||||
|
Answer(await _timelines.Home(User.GetUserId(), avatarId, maxId, limit, token));
|
||||||
|
|
||||||
|
[HttpGet, Route("/clientapi/notifications")]
|
||||||
|
public async Task<IActionResult> Notifications([FromQuery] string avatarId, [FromQuery(Name = "max_id")] string maxId, [FromQuery] int limit = 20,
|
||||||
|
CancellationToken token = default) =>
|
||||||
|
Answer(await _timelines.Notifications(User.GetUserId(), avatarId, maxId, limit, token));
|
||||||
|
|
||||||
|
[HttpPost, Route("/clientapi/notifications/read")]
|
||||||
|
public async Task<IActionResult> Read([FromQuery] string avatarId, CancellationToken token) =>
|
||||||
|
Answer(await _timelines.MarkNotificationsRead(User.GetUserId(), avatarId, token));
|
||||||
|
|
||||||
|
IActionResult Answer(WebResult result) => result.IsValid ? Ok(result.Data) : StatusCode(result.StatusCode, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -122,7 +122,7 @@ namespace PrivaPub.Domain.Content
|
|||||||
if (parts.Length == 1 || parts[1].Equals(localDomain, StringComparison.OrdinalIgnoreCase))
|
if (parts.Length == 1 || parts[1].Equals(localDomain, StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
var local = await _localActors.FindByUserName(parts[0], token);
|
var local = await _localActors.FindByUserName(parts[0], token);
|
||||||
return local is { IsFederated: true, Kind: not LocalActorKind.Application }
|
return local is { IsFederated: true, IsCircle: false, Kind: not LocalActorKind.Application }
|
||||||
? new ResolvedMention(local.Uri, local.UserName, local.Domain, local.Uri, true, local.Id, local.Inbox)
|
? new ResolvedMention(local.Uri, local.UserName, local.Domain, local.Uri, true, local.Id, local.Inbox)
|
||||||
: default;
|
: default;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,155 @@
|
|||||||
|
using AngleSharp.Html.Parser;
|
||||||
|
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public LinkPreviews(DbEntities dbEntities, ILocalActorService localActors, IFederationHttp http, IJobQueue queue,
|
||||||
|
IOptionsMonitor<FederationOptions> options)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_http = http;
|
||||||
|
_queue = queue;
|
||||||
|
_options = options;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (finalUri, html) = await _http.GetPage(url, token);
|
||||||
|
var preview = html == default ? new LinkPreview { Url = url, Failed = true } : Read(url, finalUri, html);
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
namespace PrivaPub.Domain.Media
|
||||||
|
{
|
||||||
|
public static class Blurhash
|
||||||
|
{
|
||||||
|
const string Characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~";
|
||||||
|
|
||||||
|
public static string Encode(byte[] rgb, int width, int height, int componentsX = 4, int componentsY = 3)
|
||||||
|
{
|
||||||
|
var factors = new double[componentsY, componentsX, 3];
|
||||||
|
for (var j = 0; j < componentsY; j++)
|
||||||
|
for (var i = 0; i < componentsX; i++)
|
||||||
|
{
|
||||||
|
var normalisation = i == 0 && j == 0 ? 1 : 2;
|
||||||
|
double r = 0, g = 0, b = 0;
|
||||||
|
for (var y = 0; y < height; y++)
|
||||||
|
for (var x = 0; x < width; x++)
|
||||||
|
{
|
||||||
|
var basis = normalisation * Math.Cos(Math.PI * i * x / width) * Math.Cos(Math.PI * j * y / height);
|
||||||
|
var offset = (y * width + x) * 3;
|
||||||
|
r += basis * ToLinear(rgb[offset]);
|
||||||
|
g += basis * ToLinear(rgb[offset + 1]);
|
||||||
|
b += basis * ToLinear(rgb[offset + 2]);
|
||||||
|
}
|
||||||
|
var scale = 1.0 / (width * height);
|
||||||
|
factors[j, i, 0] = r * scale;
|
||||||
|
factors[j, i, 1] = g * scale;
|
||||||
|
factors[j, i, 2] = b * scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
var hash = new System.Text.StringBuilder();
|
||||||
|
hash.Append(Base83((componentsX - 1) + (componentsY - 1) * 9, 1));
|
||||||
|
|
||||||
|
double maximum = 0;
|
||||||
|
for (var j = 0; j < componentsY; j++)
|
||||||
|
for (var i = 0; i < componentsX; i++)
|
||||||
|
if (i != 0 || j != 0)
|
||||||
|
for (var c = 0; c < 3; c++)
|
||||||
|
maximum = Math.Max(maximum, Math.Abs(factors[j, i, c]));
|
||||||
|
double maximumValue;
|
||||||
|
if (componentsX * componentsY > 1)
|
||||||
|
{
|
||||||
|
var quantised = (int)Math.Max(0, Math.Min(82, Math.Floor(maximum * 166 - 0.5)));
|
||||||
|
maximumValue = (quantised + 1) / 166.0;
|
||||||
|
hash.Append(Base83(quantised, 1));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
maximumValue = 1;
|
||||||
|
hash.Append(Base83(0, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
var dc = (ToSrgb(factors[0, 0, 0]) << 16) + (ToSrgb(factors[0, 0, 1]) << 8) + ToSrgb(factors[0, 0, 2]);
|
||||||
|
hash.Append(Base83(dc, 4));
|
||||||
|
for (var j = 0; j < componentsY; j++)
|
||||||
|
for (var i = 0; i < componentsX; i++)
|
||||||
|
{
|
||||||
|
if (i == 0 && j == 0)
|
||||||
|
continue;
|
||||||
|
int Quantise(double value) => (int)Math.Max(0, Math.Min(18, Math.Floor(SignPow(value / maximumValue, 0.5) * 9 + 9.5)));
|
||||||
|
var ac = Quantise(factors[j, i, 0]) * 19 * 19 + Quantise(factors[j, i, 1]) * 19 + Quantise(factors[j, i, 2]);
|
||||||
|
hash.Append(Base83(ac, 2));
|
||||||
|
}
|
||||||
|
return hash.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
static double ToLinear(byte value)
|
||||||
|
{
|
||||||
|
var v = value / 255.0;
|
||||||
|
return v <= 0.04045 ? v / 12.92 : Math.Pow((v + 0.055) / 1.055, 2.4);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ToSrgb(double value)
|
||||||
|
{
|
||||||
|
var v = Math.Max(0, Math.Min(1, value));
|
||||||
|
return v <= 0.0031308 ? (int)(v * 12.92 * 255 + 0.5) : (int)((1.055 * Math.Pow(v, 1 / 2.4) - 0.055) * 255 + 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
static double SignPow(double value, double exponent) => Math.CopySign(Math.Pow(Math.Abs(value), exponent), value);
|
||||||
|
|
||||||
|
static string Base83(int value, int length)
|
||||||
|
{
|
||||||
|
var result = new char[length];
|
||||||
|
for (var i = 1; i <= length; i++)
|
||||||
|
{
|
||||||
|
var digit = value / (int)Math.Pow(83, length - i) % 83;
|
||||||
|
result[i - 1] = Characters[digit];
|
||||||
|
}
|
||||||
|
return new string(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
namespace PrivaPub.Domain.Media
|
||||||
|
{
|
||||||
|
public class MediaOptions
|
||||||
|
{
|
||||||
|
public string Root { get; set; }
|
||||||
|
public long MaxImageBytes { get; set; } = 16 * 1024 * 1024;
|
||||||
|
public long MaxVideoBytes { get; set; } = 99 * 1024 * 1024;
|
||||||
|
public long MaxProxiedBytes { get; set; } = 40 * 1024 * 1024;
|
||||||
|
public long ProxyCacheBytes { get; set; } = 5L * 1024 * 1024 * 1024;
|
||||||
|
public int MaxImageSide { get; set; } = 4096;
|
||||||
|
public int PreviewSide { get; set; } = 640;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Infrastructure.Http;
|
||||||
|
using PrivaPub.Models.Media;
|
||||||
|
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace PrivaPub.Domain.Media
|
||||||
|
{
|
||||||
|
public interface IMediaProxy
|
||||||
|
{
|
||||||
|
string Wrap(string remoteUrl);
|
||||||
|
Task<(string Path, string ContentType)> Fetch(string signature, string encodedUrl, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MediaProxy : IMediaProxy
|
||||||
|
{
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IFederationHttp _http;
|
||||||
|
readonly IMediaService _media;
|
||||||
|
readonly IOptionsMonitor<MediaOptions> _options;
|
||||||
|
byte[] _key;
|
||||||
|
|
||||||
|
public MediaProxy(ILocalActorService localActors, IFederationHttp http, IMediaService media, IOptionsMonitor<MediaOptions> options)
|
||||||
|
{
|
||||||
|
_localActors = localActors;
|
||||||
|
_http = http;
|
||||||
|
_media = media;
|
||||||
|
_options = options;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] Key => _key ??= LoadKey();
|
||||||
|
|
||||||
|
public string Wrap(string remoteUrl)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(remoteUrl) || remoteUrl.StartsWith(_localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|
||||||
|
return remoteUrl;
|
||||||
|
var encoded = Base64Url(Encoding.UTF8.GetBytes(remoteUrl));
|
||||||
|
return $"{_localActors.BaseAddress}/media/proxy/{Sign(remoteUrl)}/{encoded}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<(string Path, string ContentType)> Fetch(string signature, string encodedUrl, CancellationToken token)
|
||||||
|
{
|
||||||
|
string url;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
url = Encoding.UTF8.GetString(FromBase64Url(encodedUrl));
|
||||||
|
}
|
||||||
|
catch (FormatException)
|
||||||
|
{
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
var (bytes, contentType) = await _http.GetMedia(url, _options.CurrentValue.MaxProxiedBytes, token);
|
||||||
|
if (bytes == default)
|
||||||
|
return default;
|
||||||
|
Directory.CreateDirectory(directory);
|
||||||
|
await File.WriteAllBytesAsync(path, bytes, token);
|
||||||
|
await File.WriteAllTextAsync(typePath, contentType, token);
|
||||||
|
return (path, contentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
string Sign(string url) => Base64Url(HMACSHA256.HashData(Key, Encoding.UTF8.GetBytes(url))[..16]);
|
||||||
|
|
||||||
|
static byte[] LoadKey()
|
||||||
|
{
|
||||||
|
var secret = DB.Default.Find<MediaSecret>().ExecuteFirstAsync().GetAwaiter().GetResult();
|
||||||
|
if (secret == default)
|
||||||
|
{
|
||||||
|
secret = new MediaSecret { Key = Convert.ToBase64String(RandomNumberGenerator.GetBytes(32)) };
|
||||||
|
DB.Default.SaveAsync(secret).GetAwaiter().GetResult();
|
||||||
|
secret = DB.Default.Find<MediaSecret>().ExecuteFirstAsync().GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
return Convert.FromBase64String(secret.Key);
|
||||||
|
}
|
||||||
|
|
||||||
|
static string Base64Url(byte[] bytes) => Convert.ToBase64String(bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_');
|
||||||
|
|
||||||
|
static byte[] FromBase64Url(string value)
|
||||||
|
{
|
||||||
|
var padded = value.Replace('-', '+').Replace('_', '/');
|
||||||
|
return Convert.FromBase64String(padded + new string('=', (4 - padded.Length % 4) % 4));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MediaJanitor : BackgroundService
|
||||||
|
{
|
||||||
|
static readonly TimeSpan Interval = TimeSpan.FromHours(1);
|
||||||
|
static readonly TimeSpan UnattachedLifetime = TimeSpan.FromDays(1);
|
||||||
|
|
||||||
|
readonly IMediaService _media;
|
||||||
|
readonly IOptionsMonitor<MediaOptions> _options;
|
||||||
|
readonly ILogger<MediaJanitor> _logger;
|
||||||
|
|
||||||
|
public MediaJanitor(IMediaService media, IOptionsMonitor<MediaOptions> options, ILogger<MediaJanitor> logger)
|
||||||
|
{
|
||||||
|
_media = media;
|
||||||
|
_options = options;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await Task.Delay(Interval, stoppingToken);
|
||||||
|
var cutoff = DateTime.UtcNow - UnattachedLifetime;
|
||||||
|
foreach (var stale in await DB.Default.Find<MediaAttachment>().Match(m => m.PostId == null && m.CreatedAt < cutoff).Limit(500).ExecuteAsync(stoppingToken))
|
||||||
|
await _media.Delete(stale);
|
||||||
|
TrimProxyCache();
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "{Service} pass failed", nameof(MediaJanitor));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TrimProxyCache()
|
||||||
|
{
|
||||||
|
var directory = new DirectoryInfo(_media.ProxyRoot);
|
||||||
|
if (!directory.Exists)
|
||||||
|
return;
|
||||||
|
var files = directory.EnumerateFiles("*", SearchOption.AllDirectories).Where(f => f.Extension != ".type").OrderBy(f => f.LastWriteTimeUtc).ToList();
|
||||||
|
var total = files.Sum(f => f.Length);
|
||||||
|
foreach (var file in files)
|
||||||
|
{
|
||||||
|
if (total <= _options.CurrentValue.ProxyCacheBytes)
|
||||||
|
break;
|
||||||
|
total -= file.Length;
|
||||||
|
file.Delete();
|
||||||
|
var type = new FileInfo(file.FullName + ".type");
|
||||||
|
if (type.Exists)
|
||||||
|
type.Delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,263 @@
|
|||||||
|
using FFMpegCore;
|
||||||
|
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using NetVips;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Models.Media;
|
||||||
|
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
|
namespace PrivaPub.Domain.Media
|
||||||
|
{
|
||||||
|
public sealed record MediaOutcome(MediaAttachment Attachment, int Status = StatusCodes.Status200OK, string Error = default)
|
||||||
|
{
|
||||||
|
public bool Ok => Error == default;
|
||||||
|
public static MediaOutcome Fail(int status, string error) => new(default, status, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed record ProcessedImage(byte[] Bytes, string Extension, string ContentType, int Width, int Height, byte[] Preview, string Blurhash);
|
||||||
|
|
||||||
|
public interface IMediaService
|
||||||
|
{
|
||||||
|
string Root { get; }
|
||||||
|
string ProxyRoot { get; }
|
||||||
|
string Url(string relativePath);
|
||||||
|
Task<MediaOutcome> Upload(LocalActor owner, IFormFile file, string description, string focus, CancellationToken token);
|
||||||
|
Task<string> ProfileImage(IFormFile file, int width, int height, CancellationToken token);
|
||||||
|
Task Delete(MediaAttachment attachment);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MediaService : IMediaService
|
||||||
|
{
|
||||||
|
static readonly string[] ImageTypes = { "image/jpeg", "image/png", "image/gif", "image/webp", "image/heic", "image/heif", "image/avif" };
|
||||||
|
static readonly Dictionary<string, string> AvTypes = new()
|
||||||
|
{
|
||||||
|
["video/mp4"] = "mp4", ["video/quicktime"] = "mp4", ["video/webm"] = "webm",
|
||||||
|
["audio/mpeg"] = "mp3", ["audio/mp3"] = "mp3", ["audio/ogg"] = "ogg", ["audio/wav"] = "wav", ["audio/x-wav"] = "wav",
|
||||||
|
["audio/mp4"] = "m4a", ["audio/x-m4a"] = "m4a", ["audio/flac"] = "flac", ["audio/webm"] = "webm"
|
||||||
|
};
|
||||||
|
|
||||||
|
readonly IOptionsMonitor<MediaOptions> _options;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IWebHostEnvironment _environment;
|
||||||
|
readonly ILogger<MediaService> _logger;
|
||||||
|
|
||||||
|
public MediaService(IOptionsMonitor<MediaOptions> options, ILocalActorService localActors, IWebHostEnvironment environment, ILogger<MediaService> logger)
|
||||||
|
{
|
||||||
|
_options = options;
|
||||||
|
_localActors = localActors;
|
||||||
|
_environment = environment;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Root => Path.GetFullPath(_options.CurrentValue.Root ?? Path.Combine(_environment.ContentRootPath, "media-store"));
|
||||||
|
|
||||||
|
public string ProxyRoot => Root.TrimEnd(Path.DirectorySeparatorChar) + "-proxy";
|
||||||
|
|
||||||
|
public string Url(string relativePath) => relativePath == default ? default : $"{_localActors.BaseAddress}/media/files/{relativePath.Replace('\\', '/')}";
|
||||||
|
|
||||||
|
public async Task<MediaOutcome> Upload(LocalActor owner, IFormFile file, string description, string focus, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (file == default || file.Length == 0)
|
||||||
|
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: File can't be blank");
|
||||||
|
var options = _options.CurrentValue;
|
||||||
|
var contentType = file.ContentType?.Split(';')[0].Trim().ToLowerInvariant();
|
||||||
|
|
||||||
|
var attachment = new MediaAttachment
|
||||||
|
{
|
||||||
|
OwnerAvatarId = owner.Id,
|
||||||
|
Description = Clean(description, 1500),
|
||||||
|
Focus = Focus(focus)
|
||||||
|
};
|
||||||
|
if (ImageTypes.Contains(contentType))
|
||||||
|
{
|
||||||
|
if (file.Length > options.MaxImageBytes)
|
||||||
|
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: File is too big");
|
||||||
|
await using var stream = file.OpenReadStream();
|
||||||
|
using var buffer = new MemoryStream();
|
||||||
|
await stream.CopyToAsync(buffer, token);
|
||||||
|
ProcessedImage processed;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
processed = ProcessImage(buffer.ToArray(), options.MaxImageSide, options.PreviewSide, contentType == "image/gif");
|
||||||
|
}
|
||||||
|
catch (VipsException ex)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Refused an image upload: {Error}", ex.Message);
|
||||||
|
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: The file is not a readable image");
|
||||||
|
}
|
||||||
|
attachment.Kind = contentType == "image/gif" ? "gifv" : "image";
|
||||||
|
attachment.ContentType = processed.ContentType;
|
||||||
|
attachment.FilePath = await Save(processed.Bytes, processed.Extension, token);
|
||||||
|
attachment.PreviewPath = await Save(processed.Preview, "jpg", token);
|
||||||
|
attachment.Width = processed.Width;
|
||||||
|
attachment.Height = processed.Height;
|
||||||
|
attachment.Blurhash = processed.Blurhash;
|
||||||
|
attachment.Size = processed.Bytes.Length;
|
||||||
|
}
|
||||||
|
else if (contentType != default && AvTypes.TryGetValue(contentType, out var extension))
|
||||||
|
{
|
||||||
|
if (file.Length > options.MaxVideoBytes)
|
||||||
|
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: File is too big");
|
||||||
|
var outcome = await ProcessAv(file, extension, contentType, attachment, token);
|
||||||
|
if (!outcome.Ok)
|
||||||
|
return outcome;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: File type is not supported");
|
||||||
|
|
||||||
|
await DB.Default.SaveAsync(attachment, token);
|
||||||
|
return new MediaOutcome(attachment);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> ProfileImage(IFormFile file, int width, int height, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (file == default || file.Length == 0 || file.Length > _options.CurrentValue.MaxImageBytes)
|
||||||
|
return default;
|
||||||
|
await using var stream = file.OpenReadStream();
|
||||||
|
using var buffer = new MemoryStream();
|
||||||
|
await stream.CopyToAsync(buffer, token);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var image = Image.ThumbnailBuffer(buffer.ToArray(), width, height: height, crop: Enums.Interesting.Centre, size: Enums.Size.Down);
|
||||||
|
using var flat = Flatten(image);
|
||||||
|
return Url(await Save(flat.WriteToBuffer(".jpg[Q=85,keep=none]"), "jpg", token));
|
||||||
|
}
|
||||||
|
catch (VipsException)
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task Delete(MediaAttachment attachment)
|
||||||
|
{
|
||||||
|
foreach (var path in new[] { attachment.FilePath, attachment.PreviewPath }.Where(p => p != default))
|
||||||
|
{
|
||||||
|
var full = Path.Combine(Root, path);
|
||||||
|
if (File.Exists(full))
|
||||||
|
File.Delete(full);
|
||||||
|
}
|
||||||
|
return DB.Default.DeleteAsync<MediaAttachment>(attachment.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ProcessedImage ProcessImage(byte[] input, int maxSide, int previewSide, bool animated)
|
||||||
|
{
|
||||||
|
using var loaded = animated ? Image.NewFromBuffer(input, kwargs: new VOption { { "n", -1 } }) : Image.NewFromBuffer(input);
|
||||||
|
using var rotated = animated ? loaded.Copy() : loaded.Autorot();
|
||||||
|
var pageHeight = animated && rotated.Contains("page-height") ? (int)rotated.Get("page-height") : rotated.Height;
|
||||||
|
var longest = Math.Max(rotated.Width, pageHeight);
|
||||||
|
|
||||||
|
byte[] bytes;
|
||||||
|
string extension, contentType;
|
||||||
|
int width, height;
|
||||||
|
if (animated)
|
||||||
|
{
|
||||||
|
bytes = rotated.WriteToBuffer(".gif");
|
||||||
|
(extension, contentType, width, height) = ("gif", "image/gif", rotated.Width, pageHeight);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
using var resized = longest > maxSide ? rotated.ThumbnailImage(maxSide, height: maxSide, size: Enums.Size.Down) : rotated.Copy();
|
||||||
|
var keepsAlpha = resized.HasAlpha();
|
||||||
|
bytes = keepsAlpha ? resized.WriteToBuffer(".png[keep=none]") : resized.WriteToBuffer(".jpg[Q=88,keep=none]");
|
||||||
|
(extension, contentType, width, height) = keepsAlpha ? ("png", "image/png", resized.Width, resized.Height) : ("jpg", "image/jpeg", resized.Width, resized.Height);
|
||||||
|
}
|
||||||
|
|
||||||
|
using var firstFrame = animated ? rotated.Crop(0, 0, rotated.Width, pageHeight) : rotated.Copy();
|
||||||
|
using var preview = firstFrame.ThumbnailImage(previewSide, height: previewSide, size: Enums.Size.Down);
|
||||||
|
using var previewFlat = Flatten(preview);
|
||||||
|
var previewBytes = previewFlat.WriteToBuffer(".jpg[Q=80,keep=none]");
|
||||||
|
|
||||||
|
using var tiny = firstFrame.ThumbnailImage(32, height: 32);
|
||||||
|
using var tinyFlat = Flatten(tiny);
|
||||||
|
var pixels = tinyFlat.WriteToMemory();
|
||||||
|
return new ProcessedImage(bytes, extension, contentType, width, height, previewBytes, Blurhash.Encode(pixels, tinyFlat.Width, tinyFlat.Height));
|
||||||
|
}
|
||||||
|
|
||||||
|
static Image Flatten(Image image)
|
||||||
|
{
|
||||||
|
var srgb = image.Interpretation == Enums.Interpretation.Srgb ? image.Copy() : image.Colourspace(Enums.Interpretation.Srgb);
|
||||||
|
var flat = srgb.HasAlpha() ? srgb.Flatten(background: new double[] { 255, 255, 255 }) : srgb.Copy();
|
||||||
|
srgb.Dispose();
|
||||||
|
var bands = flat.Bands > 3 ? flat.ExtractBand(0, n: 3) : flat.Bands == 1 ? flat.Bandjoin(flat, flat) : flat.Copy();
|
||||||
|
flat.Dispose();
|
||||||
|
using var cast = bands;
|
||||||
|
return bands.Format == Enums.BandFormat.Uchar ? bands.Copy() : bands.Cast(Enums.BandFormat.Uchar);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<MediaOutcome> ProcessAv(IFormFile file, string extension, string contentType, MediaAttachment attachment, CancellationToken token)
|
||||||
|
{
|
||||||
|
var temp = Path.Combine(Path.GetTempPath(), $"privapub-{Guid.NewGuid():N}");
|
||||||
|
var input = temp + ".in";
|
||||||
|
var output = temp + "." + extension;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await using (var target = File.Create(input))
|
||||||
|
await file.CopyToAsync(target, token);
|
||||||
|
var probe = await FFProbe.AnalyseAsync(input, cancellationToken: token);
|
||||||
|
var isVideo = contentType.StartsWith("video/") && probe.PrimaryVideoStream != default;
|
||||||
|
await FFMpegArguments.FromFileInput(input)
|
||||||
|
.OutputToFile(output, true, o => o.WithCustomArgument("-map 0 -map_metadata -1 -map_chapters -1 -c copy" + (extension is "mp4" or "m4a" ? " -movflags +faststart" : string.Empty)))
|
||||||
|
.CancellableThrough(token)
|
||||||
|
.ProcessAsynchronously();
|
||||||
|
|
||||||
|
attachment.Kind = isVideo ? "video" : "audio";
|
||||||
|
attachment.ContentType = extension switch { "mp4" => "video/mp4", "webm" => isVideo ? "video/webm" : "audio/webm", "m4a" => "audio/mp4", "mp3" => "audio/mpeg", _ => contentType };
|
||||||
|
attachment.FilePath = await Save(await File.ReadAllBytesAsync(output, token), extension, token);
|
||||||
|
attachment.Size = new FileInfo(output).Length;
|
||||||
|
if (isVideo)
|
||||||
|
{
|
||||||
|
attachment.Width = probe.PrimaryVideoStream.Width;
|
||||||
|
attachment.Height = probe.PrimaryVideoStream.Height;
|
||||||
|
var frame = temp + ".png";
|
||||||
|
await FFMpeg.SnapshotAsync(input, frame, captureTime: TimeSpan.FromSeconds(Math.Min(1, probe.Duration.TotalSeconds / 2)));
|
||||||
|
var still = ProcessImage(await File.ReadAllBytesAsync(frame, token), 640, _options.CurrentValue.PreviewSide, false);
|
||||||
|
attachment.PreviewPath = await Save(still.Preview, "jpg", token);
|
||||||
|
attachment.Blurhash = still.Blurhash;
|
||||||
|
File.Delete(frame);
|
||||||
|
}
|
||||||
|
return new MediaOutcome(attachment);
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(ex, "Refused an audio or video upload");
|
||||||
|
return MediaOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: The file could not be processed");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
foreach (var path in new[] { input, output })
|
||||||
|
if (File.Exists(path))
|
||||||
|
File.Delete(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<string> Save(byte[] bytes, string extension, CancellationToken token)
|
||||||
|
{
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
var relative = Path.Combine(now.ToString("yyyy", CultureInfo.InvariantCulture), now.ToString("MM", CultureInfo.InvariantCulture),
|
||||||
|
$"{Convert.ToHexStringLower(RandomNumberGenerator.GetBytes(16))}.{extension}");
|
||||||
|
var full = Path.Combine(Root, relative);
|
||||||
|
Directory.CreateDirectory(Path.GetDirectoryName(full)!);
|
||||||
|
await File.WriteAllBytesAsync(full, bytes, token);
|
||||||
|
return relative.Replace('\\', '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
static string Clean(string value, int max) =>
|
||||||
|
string.IsNullOrWhiteSpace(value) ? default : value.Trim().Length <= max ? value.Trim() : value.Trim()[..max];
|
||||||
|
|
||||||
|
static float[] Focus(string focus)
|
||||||
|
{
|
||||||
|
var parts = focus?.Split(',');
|
||||||
|
if (parts is not { Length: 2 }
|
||||||
|
|| !float.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out var x)
|
||||||
|
|| !float.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out var y))
|
||||||
|
return default;
|
||||||
|
return new[] { Math.Clamp(x, -1, 1), Math.Clamp(y, -1, 1) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,205 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Social;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Federation.Rendering;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Domain.Relationships
|
||||||
|
{
|
||||||
|
public interface IRelationshipService
|
||||||
|
{
|
||||||
|
Task Block(LocalActor me, string targetActorUri, string targetAccountId, CancellationToken token);
|
||||||
|
Task Unblock(LocalActor me, string targetActorUri, CancellationToken token);
|
||||||
|
Task Mute(LocalActor me, string targetActorUri, string targetAccountId, bool hideNotifications, TimeSpan? duration, CancellationToken token);
|
||||||
|
Task Unmute(LocalActor me, string targetActorUri, CancellationToken token);
|
||||||
|
Task BlockDomain(LocalActor me, string domain, CancellationToken token);
|
||||||
|
Task UnblockDomain(LocalActor me, string domain, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RelationshipService : IRelationshipService
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly IFollowService _follows;
|
||||||
|
readonly IDeliveryService _delivery;
|
||||||
|
|
||||||
|
public RelationshipService(DbEntities dbEntities, IFollowService follows, IDeliveryService delivery)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_follows = follows;
|
||||||
|
_delivery = delivery;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Block(LocalActor me, string targetActorUri, string targetAccountId, CancellationToken token)
|
||||||
|
{
|
||||||
|
var block = new Block { AvatarId = me.Id, TargetAccountId = targetAccountId, TargetActorURI = targetActorUri };
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(block, token);
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
block = await DB.Default.Find<Block>().Match(b => b.AvatarId == me.Id && b.TargetActorURI == targetActorUri).ExecuteFirstAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _follows.UnfollowAs(me, targetActorUri, token);
|
||||||
|
var follower = await _dbEntities.Followers.Match(f => f.LocalActorId == me.Id && f.ActorURI == targetActorUri).ExecuteFirstAsync(token);
|
||||||
|
if (follower != default)
|
||||||
|
{
|
||||||
|
await DB.Default.DeleteAsync<Follower>(follower.ID);
|
||||||
|
if (!targetActorUri.StartsWith(me.BaseAddress + "/", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(follower.FollowActivityURI))
|
||||||
|
{
|
||||||
|
var reject = new JsonObject
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||||
|
["id"] = me.ActivityUri($"reject-{follower.ID}-{DateTime.UtcNow.Ticks}"),
|
||||||
|
["type"] = "Reject",
|
||||||
|
["actor"] = me.Uri,
|
||||||
|
["object"] = new JsonObject { ["id"] = follower.FollowActivityURI, ["type"] = "Follow", ["actor"] = targetActorUri, ["object"] = me.Uri }
|
||||||
|
};
|
||||||
|
await _delivery.Enqueue(me, new[] { follower.InboxURL }, reject, token);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
await DB.Default.DeleteAsync<Following>(f => f.TargetActorURI == me.Uri && f.AvatarId == targetAccountId);
|
||||||
|
}
|
||||||
|
await Forget(me, targetAccountId, token);
|
||||||
|
if (block != default)
|
||||||
|
await Tell(me, targetActorUri, BlockActivity(me, block), token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Unblock(LocalActor me, string targetActorUri, CancellationToken token)
|
||||||
|
{
|
||||||
|
var block = await DB.Default.Find<Block>().Match(b => b.AvatarId == me.Id && b.TargetActorURI == targetActorUri).ExecuteFirstAsync(token);
|
||||||
|
if (block == default)
|
||||||
|
return;
|
||||||
|
await DB.Default.DeleteAsync<Block>(block.ID);
|
||||||
|
await Tell(me, targetActorUri, new JsonObject
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||||
|
["id"] = me.ActivityUri($"undo-block-{block.ID}"),
|
||||||
|
["type"] = "Undo",
|
||||||
|
["actor"] = me.Uri,
|
||||||
|
["object"] = BlockActivity(me, block)
|
||||||
|
}, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
static JsonObject BlockActivity(LocalActor me, Block block) => new()
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||||
|
["id"] = me.ActivityUri($"block-{block.ID}"),
|
||||||
|
["type"] = "Block",
|
||||||
|
["actor"] = me.Uri,
|
||||||
|
["object"] = block.TargetActorURI,
|
||||||
|
["to"] = new JsonArray(block.TargetActorURI)
|
||||||
|
};
|
||||||
|
|
||||||
|
async Task Tell(LocalActor me, string targetActorUri, JsonObject activity, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (targetActorUri.StartsWith(me.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|
||||||
|
return;
|
||||||
|
var target = await _dbEntities.ForeignAvatars.Match(f => f.ActorURI == targetActorUri).ExecuteFirstAsync(token);
|
||||||
|
if (!string.IsNullOrEmpty(target?.InboxURL))
|
||||||
|
await _delivery.Enqueue(me, new[] { target.InboxURL }, activity, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Mute(LocalActor me, string targetActorUri, string targetAccountId, bool hideNotifications, TimeSpan? duration, CancellationToken token)
|
||||||
|
{
|
||||||
|
await DB.Default.Update<Mute>()
|
||||||
|
.Match(m => m.AvatarId == me.Id && m.TargetActorURI == targetActorUri)
|
||||||
|
.Modify(m => m.TargetAccountId, targetAccountId)
|
||||||
|
.Modify(m => m.HideNotifications, hideNotifications)
|
||||||
|
.Modify(m => m.ExpiresAt, duration.HasValue && duration > TimeSpan.Zero ? DateTime.UtcNow + duration : null)
|
||||||
|
.Modify(b => b.SetOnInsert(m => m.CreatedAt, DateTime.UtcNow))
|
||||||
|
.Option(o => o.IsUpsert = true)
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
await Forget(me, targetAccountId, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Unmute(LocalActor me, string targetActorUri, CancellationToken token) =>
|
||||||
|
await DB.Default.DeleteAsync<Mute>(m => m.AvatarId == me.Id && m.TargetActorURI == targetActorUri);
|
||||||
|
|
||||||
|
public async Task BlockDomain(LocalActor me, string domain, CancellationToken token)
|
||||||
|
{
|
||||||
|
domain = domain?.Trim().ToLowerInvariant();
|
||||||
|
if (string.IsNullOrEmpty(domain) || Uri.CheckHostName(domain) != UriHostNameType.Dns)
|
||||||
|
return;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(new AccountDomainBlock { AvatarId = me.Id, Domain = domain }, token);
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
foreach (var following in await _dbEntities.Followings.Match(f => f.AvatarId == me.Id && !f.TargetIsLocal).ExecuteAsync(token))
|
||||||
|
if (Hidden.HostMatches(following.TargetActorURI, domain))
|
||||||
|
await _follows.UnfollowAs(me, following.TargetActorURI, token);
|
||||||
|
foreach (var follower in await _dbEntities.Followers.Match(f => f.LocalActorId == me.Id).ExecuteAsync(token))
|
||||||
|
if (Hidden.HostMatches(follower.ActorURI, domain))
|
||||||
|
await DB.Default.DeleteAsync<Follower>(follower.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UnblockDomain(LocalActor me, string domain, CancellationToken token)
|
||||||
|
{
|
||||||
|
domain = domain?.Trim().ToLowerInvariant();
|
||||||
|
await DB.Default.DeleteAsync<AccountDomainBlock>(b => b.AvatarId == me.Id && b.Domain == domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
static async Task Forget(LocalActor me, string targetAccountId, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(targetAccountId))
|
||||||
|
return;
|
||||||
|
await DB.Default.DeleteAsync<TimelineEntry>(e => e.AvatarId == me.Id && e.AuthorAccountId == targetAccountId);
|
||||||
|
await DB.Default.DeleteAsync<Notification>(n => n.AvatarId == me.Id && n.FromAccountId == targetAccountId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Hidden
|
||||||
|
{
|
||||||
|
public static bool HostMatches(string actorUri, string domain) =>
|
||||||
|
Uri.TryCreate(actorUri, UriKind.Absolute, out var uri)
|
||||||
|
&& (uri.Host.Equals(domain, StringComparison.OrdinalIgnoreCase) || uri.Host.EndsWith("." + domain, StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
|
public static async Task<HashSet<string>> AuthorsHiddenFrom(string avatarId, IEnumerable<string> actorUris, bool forNotifications, CancellationToken token)
|
||||||
|
{
|
||||||
|
var uris = actorUris.Where(u => !string.IsNullOrEmpty(u)).Distinct().ToList();
|
||||||
|
var hidden = new HashSet<string>(StringComparer.Ordinal);
|
||||||
|
if (string.IsNullOrEmpty(avatarId) || uris.Count == 0)
|
||||||
|
return hidden;
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
foreach (var block in await DB.Default.Find<Block>().Match(b => b.AvatarId == avatarId && uris.Contains(b.TargetActorURI)).ExecuteAsync(token))
|
||||||
|
hidden.Add(block.TargetActorURI);
|
||||||
|
foreach (var mute in await DB.Default.Find<Mute>().Match(m => m.AvatarId == avatarId && uris.Contains(m.TargetActorURI)).ExecuteAsync(token))
|
||||||
|
if ((mute.ExpiresAt == null || mute.ExpiresAt > now) && (!forNotifications || mute.HideNotifications))
|
||||||
|
hidden.Add(mute.TargetActorURI);
|
||||||
|
var domains = await DB.Default.Find<AccountDomainBlock>().Match(b => b.AvatarId == avatarId).ExecuteAsync(token);
|
||||||
|
foreach (var uri in uris)
|
||||||
|
if (domains.Any(d => HostMatches(uri, d.Domain)))
|
||||||
|
hidden.Add(uri);
|
||||||
|
return hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<HashSet<string>> RecipientsHiding(IEnumerable<string> avatarIds, string actorUri, CancellationToken token)
|
||||||
|
{
|
||||||
|
var ids = avatarIds.Distinct().ToList();
|
||||||
|
var hiding = new HashSet<string>(StringComparer.Ordinal);
|
||||||
|
if (ids.Count == 0 || string.IsNullOrEmpty(actorUri))
|
||||||
|
return hiding;
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
foreach (var block in await DB.Default.Find<Block>().Match(b => ids.Contains(b.AvatarId) && b.TargetActorURI == actorUri).ExecuteAsync(token))
|
||||||
|
hiding.Add(block.AvatarId);
|
||||||
|
foreach (var mute in await DB.Default.Find<Mute>().Match(m => ids.Contains(m.AvatarId) && m.TargetActorURI == actorUri).ExecuteAsync(token))
|
||||||
|
if (mute.ExpiresAt == null || mute.ExpiresAt > now)
|
||||||
|
hiding.Add(mute.AvatarId);
|
||||||
|
foreach (var block in await DB.Default.Find<AccountDomainBlock>().Match(b => ids.Contains(b.AvatarId)).ExecuteAsync(token))
|
||||||
|
if (HostMatches(actorUri, block.Domain))
|
||||||
|
hiding.Add(block.AvatarId);
|
||||||
|
return hiding;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Federation.Rendering;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Domain.Relationships
|
||||||
|
{
|
||||||
|
public interface IReportService
|
||||||
|
{
|
||||||
|
Task<Report> File(LocalActor reporter, string targetAccountId, IReadOnlyList<string> postIds, string comment, string category, bool forward,
|
||||||
|
CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ReportService : IReportService
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IDeliveryService _delivery;
|
||||||
|
|
||||||
|
public ReportService(DbEntities dbEntities, ILocalActorService localActors, IDeliveryService delivery)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_delivery = delivery;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Report> File(LocalActor reporter, string targetAccountId, IReadOnlyList<string> postIds, string comment, string category,
|
||||||
|
bool forward, CancellationToken token)
|
||||||
|
{
|
||||||
|
var localTarget = await _dbEntities.Avatars.MatchID(targetAccountId).ExecuteFirstAsync(token);
|
||||||
|
var remoteTarget = localTarget == default ? await _dbEntities.ForeignAvatars.MatchID(targetAccountId).ExecuteFirstAsync(token) : default;
|
||||||
|
if (localTarget == default && remoteTarget == default)
|
||||||
|
return default;
|
||||||
|
var targetUri = localTarget != default ? _localActors.FromAvatar(localTarget).Uri : remoteTarget.ActorURI;
|
||||||
|
var posts = postIds.Count == 0
|
||||||
|
? new List<Models.Post.Post>()
|
||||||
|
: await _dbEntities.Posts.Match(p => postIds.Contains(p.ID) && p.ActorURI == targetUri).ExecuteAsync(token);
|
||||||
|
|
||||||
|
var report = new Report
|
||||||
|
{
|
||||||
|
ReporterAvatarId = reporter.Id,
|
||||||
|
TargetAccountId = targetAccountId,
|
||||||
|
TargetActorURI = targetUri,
|
||||||
|
PostIds = posts.Select(p => p.ID).ToList(),
|
||||||
|
ObjectURIs = posts.Select(p => p.ObjectURI).ToList(),
|
||||||
|
Comment = comment?.Trim(),
|
||||||
|
Category = category is "spam" or "legal" or "violation" ? category : "other",
|
||||||
|
Forwarded = forward && remoteTarget != default
|
||||||
|
};
|
||||||
|
await DB.Default.SaveAsync(report, token);
|
||||||
|
|
||||||
|
if (report.Forwarded)
|
||||||
|
{
|
||||||
|
var instance = await _localActors.GetInstanceActor(token);
|
||||||
|
var flag = new JsonObject
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||||
|
["id"] = instance.ActivityUri($"flag-{report.ID}"),
|
||||||
|
["type"] = "Flag",
|
||||||
|
["actor"] = instance.Uri,
|
||||||
|
["content"] = report.Comment ?? string.Empty,
|
||||||
|
["object"] = new JsonArray(report.ObjectURIs.Prepend(targetUri).Select(u => (JsonNode)u).ToArray())
|
||||||
|
};
|
||||||
|
await _delivery.Enqueue(instance, new[] { remoteTarget.SharedInboxURL ?? remoteTarget.InboxURL }, flag, token);
|
||||||
|
}
|
||||||
|
return report;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,299 @@
|
|||||||
|
using Microsoft.Extensions.Localization;
|
||||||
|
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels;
|
||||||
|
using PrivaPub.ClientModels.Social;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Federation.Rendering;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Group;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.Resources;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using GroupEntity = PrivaPub.Models.Group.Group;
|
||||||
|
|
||||||
|
namespace PrivaPub.Domain.Social
|
||||||
|
{
|
||||||
|
public interface IFollowService
|
||||||
|
{
|
||||||
|
Task<WebResult> Follow(string rootUserId, FollowForm form, CancellationToken token);
|
||||||
|
Task<WebResult> Unfollow(string rootUserId, FollowForm form, CancellationToken token);
|
||||||
|
Task<WebResult> Followings(string rootUserId, string avatarId, CancellationToken token);
|
||||||
|
Task<Following> FollowAs(LocalActor follower, string target, bool showReblogs, CancellationToken token);
|
||||||
|
Task UnfollowAs(LocalActor follower, string target, CancellationToken token);
|
||||||
|
Task<bool> Decide(LocalActor me, string followerAccountId, bool accept, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class FollowService : IFollowService
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
readonly IDeliveryService _delivery;
|
||||||
|
readonly IStringLocalizer<GenericRes> _localizer;
|
||||||
|
readonly ILogger<FollowService> _logger;
|
||||||
|
|
||||||
|
public FollowService(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery,
|
||||||
|
IStringLocalizer<GenericRes> localizer, ILogger<FollowService> logger)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
_delivery = delivery;
|
||||||
|
_localizer = localizer;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<WebResult> Follow(string rootUserId, FollowForm form, CancellationToken token)
|
||||||
|
{
|
||||||
|
var result = new WebResult();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var follower = await OwnedAvatar(rootUserId, form.AvatarId, token);
|
||||||
|
if (follower == default)
|
||||||
|
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||||
|
var following = await FollowAs(follower, form.Target, true, token);
|
||||||
|
if (following == default)
|
||||||
|
return result.Invalidate(_localizer["Account not found."], StatusCodes.Status404NotFound);
|
||||||
|
result.Data = ToView(following);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, $"{nameof(FollowService)}.{nameof(Follow)}");
|
||||||
|
return result.Invalidate(_localizer["Something went wrong."], exception: ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<WebResult> Unfollow(string rootUserId, FollowForm form, CancellationToken token)
|
||||||
|
{
|
||||||
|
var result = new WebResult();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var follower = await OwnedAvatar(rootUserId, form.AvatarId, token);
|
||||||
|
if (follower == default)
|
||||||
|
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||||
|
await UnfollowAs(follower, form.Target, token);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, $"{nameof(FollowService)}.{nameof(Unfollow)}");
|
||||||
|
return result.Invalidate(_localizer["Something went wrong."], exception: ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Following> FollowAs(LocalActor follower, string target, bool showReblogs, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, remote) = await ResolveTarget(target, token);
|
||||||
|
if (local == default && remote == default)
|
||||||
|
return default;
|
||||||
|
if (local != default && (local.Id == follower.Id || !local.IsFederated || local.IsCircle || local.Kind == LocalActorKind.Application))
|
||||||
|
return default;
|
||||||
|
|
||||||
|
var targetUri = local?.Uri ?? remote.ActorURI;
|
||||||
|
var existing = await _dbEntities.Followings.Match(f => f.AvatarId == follower.Id && f.TargetActorURI == targetUri).ExecuteFirstAsync(token);
|
||||||
|
if (existing != default)
|
||||||
|
{
|
||||||
|
if (existing.ShowReblogs != showReblogs)
|
||||||
|
await DB.Default.Update<Following>().MatchID(existing.ID).Modify(f => f.ShowReblogs, showReblogs).ExecuteAsync(token);
|
||||||
|
existing.ShowReblogs = showReblogs;
|
||||||
|
return existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
var following = new Following
|
||||||
|
{
|
||||||
|
AvatarId = follower.Id,
|
||||||
|
TargetActorURI = targetUri,
|
||||||
|
TargetAccountId = local?.Id ?? remote.ID,
|
||||||
|
TargetIsLocal = local != default,
|
||||||
|
TargetInboxURL = local?.Inbox ?? remote.InboxURL,
|
||||||
|
ShowReblogs = showReblogs,
|
||||||
|
State = local is { ManuallyApprovesFollowers: false } ? FollowState.Accepted : FollowState.Requested
|
||||||
|
};
|
||||||
|
following.ID = (string)following.GenerateNewID();
|
||||||
|
following.FollowActivityURI = follower.ActivityUri($"follow-{following.ID}");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(following, token);
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
return await _dbEntities.Followings.Match(f => f.AvatarId == follower.Id && f.TargetActorURI == targetUri).ExecuteFirstAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (local != default)
|
||||||
|
await FollowLocally(follower, local, following, token);
|
||||||
|
else
|
||||||
|
await _delivery.Enqueue(follower, new[] { remote.InboxURL }, FollowActivity(follower, following), token);
|
||||||
|
return following;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UnfollowAs(LocalActor follower, string target, CancellationToken token)
|
||||||
|
{
|
||||||
|
var (local, remote) = await ResolveTarget(target, token);
|
||||||
|
var targetUri = local?.Uri ?? remote?.ActorURI ?? target;
|
||||||
|
var following = await _dbEntities.Followings.Match(f => f.AvatarId == follower.Id && f.TargetActorURI == targetUri).ExecuteFirstAsync(token);
|
||||||
|
if (following == default)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await DB.Default.DeleteAsync<Following>(following.ID);
|
||||||
|
if (following.TargetIsLocal)
|
||||||
|
{
|
||||||
|
await DB.Default.DeleteAsync<Follower>(f => f.LocalActorId == following.TargetAccountId && f.ActorURI == follower.Uri);
|
||||||
|
await DB.Default.Update<GroupEntity>().MatchID(following.TargetAccountId)
|
||||||
|
.Modify(b => b.PullFilter(g => g.Members, m => !m.IsForeign && m.AvatarId == follower.Id && m.Role == GroupRole.Member))
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var undo = new JsonObject
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||||
|
["id"] = follower.ActivityUri($"undo-follow-{following.ID}"),
|
||||||
|
["type"] = "Undo",
|
||||||
|
["actor"] = follower.Uri,
|
||||||
|
["object"] = FollowActivity(follower, following)
|
||||||
|
};
|
||||||
|
await _delivery.Enqueue(follower, new[] { following.TargetInboxURL }, undo, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> Decide(LocalActor me, string followerAccountId, bool accept, CancellationToken token)
|
||||||
|
{
|
||||||
|
var localFollower = await _localActors.FindById(LocalActorKind.Person, followerAccountId, token);
|
||||||
|
var remoteFollower = localFollower == default ? await _dbEntities.ForeignAvatars.MatchID(followerAccountId).ExecuteFirstAsync(token) : default;
|
||||||
|
var followerUri = localFollower?.Uri ?? remoteFollower?.ActorURI;
|
||||||
|
if (followerUri == default)
|
||||||
|
return false;
|
||||||
|
var request = await _dbEntities.Followers.Match(f => f.LocalActorId == me.Id && f.LocalActorKind == me.Kind && f.ActorURI == followerUri && !f.IsAccepted)
|
||||||
|
.ExecuteFirstAsync(token);
|
||||||
|
if (request == default)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (accept)
|
||||||
|
await DB.Default.Update<Follower>().MatchID(request.ID).Modify(f => f.IsAccepted, true).ExecuteAsync(token);
|
||||||
|
else
|
||||||
|
await DB.Default.DeleteAsync<Follower>(request.ID);
|
||||||
|
|
||||||
|
if (localFollower != default)
|
||||||
|
{
|
||||||
|
if (accept)
|
||||||
|
await DB.Default.Update<Following>().Match(f => f.AvatarId == localFollower.Id && f.TargetActorURI == me.Uri)
|
||||||
|
.Modify(f => f.State, FollowState.Accepted).ExecuteAsync(token);
|
||||||
|
else
|
||||||
|
await DB.Default.DeleteAsync<Following>(f => f.AvatarId == localFollower.Id && f.TargetActorURI == me.Uri);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var follow = new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = request.FollowActivityURI,
|
||||||
|
["type"] = "Follow",
|
||||||
|
["actor"] = followerUri,
|
||||||
|
["object"] = me.Uri
|
||||||
|
};
|
||||||
|
var answer = accept
|
||||||
|
? ActivityPubRenderer.Accept(me, follow, $"accept-{request.ID}-{DateTime.UtcNow.Ticks}")
|
||||||
|
: new JsonObject
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||||
|
["id"] = me.ActivityUri($"reject-{request.ID}-{DateTime.UtcNow.Ticks}"),
|
||||||
|
["type"] = "Reject",
|
||||||
|
["actor"] = me.Uri,
|
||||||
|
["object"] = follow
|
||||||
|
};
|
||||||
|
await _delivery.Enqueue(me, new[] { request.InboxURL }, answer, token);
|
||||||
|
}
|
||||||
|
if (accept)
|
||||||
|
await Notifications.Add(me.Id, NotificationType.Follow, followerAccountId, followerUri, default, token);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<WebResult> Followings(string rootUserId, string avatarId, CancellationToken token)
|
||||||
|
{
|
||||||
|
var result = new WebResult();
|
||||||
|
var follower = await OwnedAvatar(rootUserId, avatarId, token);
|
||||||
|
if (follower == default)
|
||||||
|
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||||
|
result.Data = (await _dbEntities.Followings.Match(f => f.AvatarId == follower.Id).Sort(f => f.ID, Order.Descending).ExecuteAsync(token))
|
||||||
|
.Select(ToView).ToList();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task FollowLocally(LocalActor follower, LocalActor target, Following following, CancellationToken token)
|
||||||
|
{
|
||||||
|
await DB.Default.Update<Follower>()
|
||||||
|
.Match(f => f.LocalActorId == target.Id && f.LocalActorKind == target.Kind && f.ActorURI == follower.Uri)
|
||||||
|
.Modify(f => f.InboxURL, follower.Inbox)
|
||||||
|
.Modify(f => f.SharedInboxURL, follower.SharedInbox)
|
||||||
|
.Modify(f => f.FollowActivityURI, following.FollowActivityURI)
|
||||||
|
.Modify(f => f.IsAccepted, following.State == FollowState.Accepted)
|
||||||
|
.Modify(b => b.SetOnInsert(f => f.CreationDate, DateTime.UtcNow))
|
||||||
|
.Option(o => o.IsUpsert = true)
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
|
||||||
|
if (target.Kind == LocalActorKind.Group && following.State == FollowState.Accepted)
|
||||||
|
await DB.Default.Update<GroupEntity>()
|
||||||
|
.Match(g => g.ID == target.Id && !g.Members.Any(m => !m.IsForeign && m.AvatarId == follower.Id))
|
||||||
|
.Modify(b => b.Push(g => g.Members, new GroupMember { AvatarId = follower.Id }))
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
if (target.Kind == LocalActorKind.Person)
|
||||||
|
await Notifications.Add(target.Id, following.State == FollowState.Accepted ? NotificationType.Follow : NotificationType.FollowRequest,
|
||||||
|
follower.Id, follower.Uri, default, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<(LocalActor Local, ForeignAvatar Remote)> ResolveTarget(string target, CancellationToken token)
|
||||||
|
{
|
||||||
|
target = target?.Trim();
|
||||||
|
if (string.IsNullOrEmpty(target))
|
||||||
|
return default;
|
||||||
|
if (target.StartsWith("https://", StringComparison.OrdinalIgnoreCase) || target.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
var byUri = await _localActors.FindByUri(target, token);
|
||||||
|
return byUri != default ? (byUri, default) : (default, await _remoteActors.GetActor(target, refresh: false, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
var parts = target.TrimStart('@').Split('@');
|
||||||
|
var localDomain = new Uri(_localActors.BaseAddress).Authority;
|
||||||
|
if (parts.Length == 1 || parts[1].Equals(localDomain, StringComparison.OrdinalIgnoreCase))
|
||||||
|
return (await _localActors.FindByUserName(parts[0], token), default);
|
||||||
|
|
||||||
|
var actorUri = await _remoteActors.ResolveHandle($"{parts[0]}@{parts[1]}", token);
|
||||||
|
return (default, actorUri == default ? default : await _remoteActors.GetActor(actorUri, refresh: false, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
static JsonObject FollowActivity(LocalActor follower, Following following) => new()
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||||
|
["id"] = following.FollowActivityURI,
|
||||||
|
["type"] = "Follow",
|
||||||
|
["actor"] = follower.Uri,
|
||||||
|
["object"] = following.TargetActorURI
|
||||||
|
};
|
||||||
|
|
||||||
|
async Task<LocalActor> OwnedAvatar(string rootUserId, string avatarId, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(rootUserId) || string.IsNullOrEmpty(avatarId))
|
||||||
|
return default;
|
||||||
|
if (!await _dbEntities.RootToAvatars.Match(ra => ra.RootId == rootUserId && ra.AvatarId == avatarId).ExecuteAnyAsync(token))
|
||||||
|
return default;
|
||||||
|
return await _localActors.FindById(LocalActorKind.Person, avatarId, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ViewFollowing ToView(Following following) => new()
|
||||||
|
{
|
||||||
|
Id = following.ID,
|
||||||
|
TargetActorURI = following.TargetActorURI,
|
||||||
|
TargetAccountId = following.TargetAccountId,
|
||||||
|
TargetIsLocal = following.TargetIsLocal,
|
||||||
|
State = following.State.ToString(),
|
||||||
|
CreatedAt = following.CreatedAt
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
|
||||||
|
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, string emoji = default, string emojiUrl = default)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(avatarId) || avatarId == fromAccountId && type != NotificationType.Poll)
|
||||||
|
return;
|
||||||
|
if ((await Relationships.Hidden.AuthorsHiddenFrom(avatarId, new[] { fromActorUri }, forNotifications: true, token)).Count > 0)
|
||||||
|
return;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(new Notification
|
||||||
|
{
|
||||||
|
AvatarId = avatarId,
|
||||||
|
Type = type,
|
||||||
|
FromAccountId = fromAccountId,
|
||||||
|
FromActorURI = fromActorUri,
|
||||||
|
PostId = 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,179 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
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)
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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,196 @@
|
|||||||
|
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);
|
||||||
|
QuotePermission Permission(PostEntity quoted, LocalActor author);
|
||||||
|
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-";
|
||||||
|
|
||||||
|
public QuotePermission Permission(PostEntity quoted, LocalActor author)
|
||||||
|
{
|
||||||
|
if (!quoted.IsFederatedCopy)
|
||||||
|
return quoted.GroupUserId == author.Id && quoted.Visibility is PostVisibility.Public or PostVisibility.Unlisted
|
||||||
|
? 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) ? QuoteState.Unauthorized : QuoteState.Accepted
|
||||||
|
: 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,631 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Content;
|
||||||
|
using PrivaPub.Domain.Media;
|
||||||
|
using PrivaPub.Domain.Privacy;
|
||||||
|
using PrivaPub.Domain.Social;
|
||||||
|
using PrivaPub.Domain.Timelines;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Federation.Rendering;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Group;
|
||||||
|
using PrivaPub.Models.Media;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Domain.Statuses
|
||||||
|
{
|
||||||
|
public sealed class StatusDraft
|
||||||
|
{
|
||||||
|
public string Text { get; init; }
|
||||||
|
public bool PlainText { get; init; }
|
||||||
|
public string Title { get; init; }
|
||||||
|
public string SpoilerText { get; init; }
|
||||||
|
public bool Sensitive { get; init; }
|
||||||
|
public PostVisibility Visibility { get; init; } = PostVisibility.Public;
|
||||||
|
public string InReplyTo { get; init; }
|
||||||
|
public string GroupId { get; init; }
|
||||||
|
public string Language { get; init; }
|
||||||
|
public string ConversationId { get; init; }
|
||||||
|
public IReadOnlyList<string> Recipients { get; init; } = Array.Empty<string>();
|
||||||
|
public IReadOnlyList<string> MediaIds { get; init; }
|
||||||
|
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 sealed record StatusOutcome(PostEntity Post, int Status = StatusCodes.Status200OK, string Error = default)
|
||||||
|
{
|
||||||
|
public bool Ok => Error == default;
|
||||||
|
public static StatusOutcome Fail(int status, string error) => new(default, status, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IStatusService
|
||||||
|
{
|
||||||
|
Task<StatusOutcome> Publish(LocalActor author, StatusDraft draft, CancellationToken token);
|
||||||
|
Task<StatusOutcome> Edit(LocalActor author, string postId, StatusDraft draft, CancellationToken token);
|
||||||
|
Task<StatusOutcome> Remove(LocalActor author, string postId, CancellationToken token);
|
||||||
|
Task<StatusOutcome> Favourite(LocalActor me, string postId, bool on, CancellationToken token);
|
||||||
|
Task<StatusOutcome> Reblog(LocalActor me, string postId, bool on, PostVisibility visibility, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StatusService : IStatusService
|
||||||
|
{
|
||||||
|
const int MaxRevisions = 20;
|
||||||
|
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
readonly IDeliveryService _delivery;
|
||||||
|
readonly IContentRenderer _content;
|
||||||
|
readonly IOutboxPublisher _outbox;
|
||||||
|
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, IPollService polls,
|
||||||
|
ILinkPreviews previews, IQuoteService quotes)
|
||||||
|
{
|
||||||
|
_previews = previews;
|
||||||
|
_quotes = quotes;
|
||||||
|
_polls = polls;
|
||||||
|
_media = media;
|
||||||
|
_groups = groups;
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
_delivery = delivery;
|
||||||
|
_content = content;
|
||||||
|
_outbox = outbox;
|
||||||
|
_fanout = fanout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<StatusOutcome> Publish(LocalActor author, StatusDraft draft, CancellationToken token)
|
||||||
|
{
|
||||||
|
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 && 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))
|
||||||
|
{
|
||||||
|
var groupEntity = await _dbEntities.Groups.MatchID(draft.GroupId).ExecuteFirstAsync(token);
|
||||||
|
if (groupEntity == default || groupEntity.DeletionAt.HasValue || !await MayPost(groupEntity, author, token))
|
||||||
|
return StatusOutcome.Fail(StatusCodes.Status404NotFound, "Group not found");
|
||||||
|
group = _localActors.FromGroup(groupEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
var parent = await Parent(draft.InReplyTo, token);
|
||||||
|
if (!string.IsNullOrEmpty(draft.InReplyTo) && parent == default && !IsRemoteUri(draft.InReplyTo))
|
||||||
|
return StatusOutcome.Fail(StatusCodes.Status404NotFound, "Record not found");
|
||||||
|
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 = _quotes.Permission(quoted, author);
|
||||||
|
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))
|
||||||
|
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: A located post needs a valid position and no group or recipients");
|
||||||
|
|
||||||
|
var rendered = draft.PlainText ? await _content.PlainText(draft.Text ?? string.Empty, token) : await _content.Markdown(draft.Text ?? string.Empty, token);
|
||||||
|
string audienceUri = default;
|
||||||
|
if (group == default && !located && draft.Visibility is PostVisibility.Public or PostVisibility.Unlisted)
|
||||||
|
foreach (var mention in rendered.Mentions)
|
||||||
|
{
|
||||||
|
if (mention.IsLocal)
|
||||||
|
{
|
||||||
|
var mentioned = await _dbEntities.Groups.MatchID(mention.AccountId).ExecuteFirstAsync(token);
|
||||||
|
if (mentioned is { DeletionAt: null, Kind: GroupKind.Community } && await MayPost(mentioned, author, token))
|
||||||
|
{
|
||||||
|
group = _localActors.FromGroup(mentioned);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (await _dbEntities.ForeignAvatars.MatchID(mention.AccountId).ExecuteFirstAsync(token) is { AvatarType: Models.User.AvatarType.Group } remoteGroup)
|
||||||
|
{
|
||||||
|
audienceUri = remoteGroup.ActorURI;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var isLocalOnly = located;
|
||||||
|
var visibility = located ? PostVisibility.LocalGeo
|
||||||
|
: group is { IsCircle: true } ? PostVisibility.Circle
|
||||||
|
: draft.Visibility is PostVisibility.Circle or PostVisibility.LocalGeo ? PostVisibility.Public : draft.Visibility;
|
||||||
|
var post = new PostEntity
|
||||||
|
{
|
||||||
|
GroupUserId = author.Id,
|
||||||
|
AuthorAccountId = author.Id,
|
||||||
|
GroupId = group?.Id,
|
||||||
|
Visibility = visibility,
|
||||||
|
Title = Clean(draft.Title),
|
||||||
|
SpoilerText = Clean(draft.SpoilerText),
|
||||||
|
HasContentWarning = draft.Sensitive || Clean(draft.SpoilerText) != default,
|
||||||
|
Text = draft.Text,
|
||||||
|
ContentHtml = rendered.Html,
|
||||||
|
ContentFormat = draft.PlainText ? ContentFormat.Plain : ContentFormat.Markdown,
|
||||||
|
Language = Clean(draft.Language),
|
||||||
|
Mentions = rendered.Mentions.Select(ToMention).ToList(),
|
||||||
|
Tags = rendered.Tags.ToList(),
|
||||||
|
Media = media.Select(ToPostMedia).ToList(),
|
||||||
|
AudienceURI = audienceUri,
|
||||||
|
AnsweringToPostId = parent?.ID,
|
||||||
|
InReplyToURI = parent?.ObjectURI ?? (IsRemoteUri(draft.InReplyTo) ? draft.InReplyTo : default),
|
||||||
|
InReplyToAccountId = parent?.AuthorAccountId ?? parent?.GroupUserId,
|
||||||
|
IsLocalOnly = isLocalOnly,
|
||||||
|
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);
|
||||||
|
if (located)
|
||||||
|
{
|
||||||
|
post.Geo = new Coordinates2D(Math.Round(draft.Longitude.Value, 2), Math.Round(draft.Latitude.Value, 2));
|
||||||
|
post.RangeKm = (float)Math.Clamp(draft.RangeKm ?? 5, 1, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject create = default;
|
||||||
|
if (visibility == PostVisibility.Direct)
|
||||||
|
{
|
||||||
|
var conversation = await Conversation(author, draft, rendered.Mentions, token);
|
||||||
|
if (!conversation.Ok)
|
||||||
|
return StatusOutcome.Fail(conversation.Status, conversation.Error);
|
||||||
|
var (dmGroup, recipients) = (conversation.Group, conversation.Recipients);
|
||||||
|
post.ConversationId = dmGroup.ID;
|
||||||
|
post.ContextURI = dmGroup.ConversationURI;
|
||||||
|
post.Mentions = recipients.Select(r => new PostMention { ActorURI = r.Uri, Handle = "@" + r.Handle, IsLocal = r.LocalId != default, AccountId = r.LocalId ?? r.ForeignId })
|
||||||
|
.Concat(post.Mentions)
|
||||||
|
.DistinctBy(m => m.ActorURI)
|
||||||
|
.ToList();
|
||||||
|
var note = ActivityPubRenderer.DirectNote(post, author, recipients.Select(r => (r.Uri, r.Handle)).ToList(), dmGroup.ConversationURI);
|
||||||
|
create = ActivityPubRenderer.Create(author, note, $"create-{post.ID}");
|
||||||
|
post.To = Strings(note["to"]);
|
||||||
|
await DB.Default.Update<DmGroup>().MatchID(dmGroup.ID).Modify(g => g.UpdatedAt, DateTime.UtcNow).ExecuteAsync(token);
|
||||||
|
}
|
||||||
|
else if (!isLocalOnly)
|
||||||
|
{
|
||||||
|
var note = ActivityPubRenderer.Note(post, author, group, post.InReplyToURI);
|
||||||
|
create = ActivityPubRenderer.Create(author, note, $"create-{post.ID}");
|
||||||
|
post.To = Strings(note["to"]);
|
||||||
|
post.Cc = Strings(note["cc"]);
|
||||||
|
}
|
||||||
|
post.ActivityURI = create?["id"]?.GetValue<string>();
|
||||||
|
|
||||||
|
await DB.Default.SaveAsync(post, token);
|
||||||
|
await Attach(media, post.ID, token);
|
||||||
|
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)
|
||||||
|
await _groups.Announce(group, create, post.ObjectURI, isNewPost: string.IsNullOrEmpty(post.InReplyToURI), token);
|
||||||
|
return new StatusOutcome(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<StatusOutcome> Edit(LocalActor author, string postId, StatusDraft draft, CancellationToken token)
|
||||||
|
{
|
||||||
|
var post = await Own(author, postId, token);
|
||||||
|
if (post == default)
|
||||||
|
return StatusOutcome.Fail(StatusCodes.Status404NotFound, "Record not found");
|
||||||
|
var media = draft.MediaIds == default ? default : await Media(author, draft.MediaIds, post.ID, token);
|
||||||
|
if (draft.MediaIds != default && media == default)
|
||||||
|
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: Media attachments are invalid");
|
||||||
|
if (string.IsNullOrWhiteSpace(draft.Text) && (media ?? new List<MediaAttachment>()).Count == 0 && post.Media.Count == 0)
|
||||||
|
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: Text can't be blank");
|
||||||
|
|
||||||
|
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 plain = draft.PlainText || post.ContentFormat == ContentFormat.Plain;
|
||||||
|
var rendered = plain ? await _content.PlainText(draft.Text ?? string.Empty, token) : await _content.Markdown(draft.Text ?? string.Empty, token);
|
||||||
|
if (media != default)
|
||||||
|
{
|
||||||
|
post.Media = media.Select(ToPostMedia).ToList();
|
||||||
|
await Attach(media, post.ID, token);
|
||||||
|
}
|
||||||
|
post.Title = draft.Title == default ? post.Title : Clean(draft.Title);
|
||||||
|
post.SpoilerText = Clean(draft.SpoilerText);
|
||||||
|
post.HasContentWarning = draft.Sensitive || post.SpoilerText != default;
|
||||||
|
post.Text = draft.Text;
|
||||||
|
post.ContentHtml = rendered.Html;
|
||||||
|
post.Language = Clean(draft.Language) ?? post.Language;
|
||||||
|
post.Mentions = post.Visibility == PostVisibility.Direct
|
||||||
|
? post.Mentions.Concat(rendered.Mentions.Select(ToMention)).DistinctBy(m => m.ActorURI).ToList()
|
||||||
|
: rendered.Mentions.Select(ToMention).ToList();
|
||||||
|
post.Tags = rendered.Tags.ToList();
|
||||||
|
post.EditedAt = DateTime.UtcNow;
|
||||||
|
post.UpdateDate = post.EditedAt;
|
||||||
|
await DB.Default.SaveAsync(post, token);
|
||||||
|
|
||||||
|
if (!post.IsLocalOnly)
|
||||||
|
{
|
||||||
|
var group = string.IsNullOrEmpty(post.GroupId) ? default : await _localActors.FindById(LocalActorKind.Group, post.GroupId, token);
|
||||||
|
var note = post.Visibility == PostVisibility.Direct
|
||||||
|
? ActivityPubRenderer.DirectNote(post, author, Array.Empty<(string, string)>(), post.ContextURI)
|
||||||
|
: ActivityPubRenderer.Note(post, author, group, post.InReplyToURI);
|
||||||
|
note["to"] = new JsonArray(post.To.Select(t => (JsonNode)t).ToArray());
|
||||||
|
note["cc"] = new JsonArray(post.Cc.Select(c => (JsonNode)c).ToArray());
|
||||||
|
var update = new JsonObject
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.Context(),
|
||||||
|
["id"] = author.ActivityUri($"update-{post.ID}-{new DateTimeOffset(post.EditedAt.Value).ToUnixTimeSeconds()}"),
|
||||||
|
["type"] = "Update",
|
||||||
|
["actor"] = author.Uri,
|
||||||
|
["to"] = note["to"]!.DeepClone(),
|
||||||
|
["cc"] = note["cc"]!.DeepClone(),
|
||||||
|
["object"] = note
|
||||||
|
};
|
||||||
|
await _outbox.Publish(author, post, update, token);
|
||||||
|
if (group is { IsCircle: false })
|
||||||
|
await _groups.Announce(group, update, post.ObjectURI, isNewPost: false, token);
|
||||||
|
}
|
||||||
|
return new StatusOutcome(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<StatusOutcome> Remove(LocalActor author, string postId, CancellationToken token)
|
||||||
|
{
|
||||||
|
var post = await _dbEntities.Posts.Match(p => p.ID == postId && p.GroupUserId == author.Id && !p.IsFederatedCopy).ExecuteFirstAsync(token);
|
||||||
|
if (post == default || post.DeletedAt.HasValue)
|
||||||
|
return StatusOutcome.Fail(StatusCodes.Status404NotFound, "Record not found");
|
||||||
|
if (post.ReblogOfPostId != default)
|
||||||
|
return await Reblog(author, post.ReblogOfPostId, false, post.Visibility, token);
|
||||||
|
|
||||||
|
var audience = await _outbox.Audience(author, post, token);
|
||||||
|
await DB.Default.Update<PostEntity>().MatchID(post.ID)
|
||||||
|
.Modify(p => p.DeletedAt, DateTime.UtcNow)
|
||||||
|
.Modify(p => p.Text, null)
|
||||||
|
.Modify(p => p.ContentHtml, null)
|
||||||
|
.Modify(p => p.Title, null)
|
||||||
|
.Modify(p => p.SpoilerText, null)
|
||||||
|
.Modify(p => p.Media, new List<PostMedia>())
|
||||||
|
.Modify(p => p.Revisions, new List<PostRevision>())
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
await DB.Default.DeleteAsync<TimelineEntry>(e => e.PostId == post.ID || e.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 (audience.Count > 0)
|
||||||
|
{
|
||||||
|
var delete = ActivityPubRenderer.Delete(author, post.ObjectURI, $"delete-{post.ID}",
|
||||||
|
new JsonArray(post.To.Select(t => (JsonNode)t).ToArray()), new JsonArray(post.Cc.Select(c => (JsonNode)c).ToArray()));
|
||||||
|
await _delivery.Enqueue(author, audience, delete, token);
|
||||||
|
var group = string.IsNullOrEmpty(post.GroupId) ? default : await _localActors.FindById(LocalActorKind.Group, post.GroupId, token);
|
||||||
|
if (group is { IsCircle: false })
|
||||||
|
await _groups.Announce(group, delete, post.ObjectURI, isNewPost: false, token);
|
||||||
|
}
|
||||||
|
return new StatusOutcome(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<StatusOutcome> Favourite(LocalActor me, string postId, bool on, CancellationToken token)
|
||||||
|
{
|
||||||
|
var post = await Visible(me, postId, token);
|
||||||
|
if (post == default)
|
||||||
|
return StatusOutcome.Fail(StatusCodes.Status404NotFound, "Record not found");
|
||||||
|
|
||||||
|
var activityId = me.ActivityUri($"like-{post.ID}");
|
||||||
|
if (on)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(new Favourite { AccountId = me.Id, ActorURI = me.Uri, PostId = post.ID, ActivityURI = activityId }, token);
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
return new StatusOutcome(post);
|
||||||
|
}
|
||||||
|
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(b => b.Inc(p => p.FavouritesCount, 1)).ExecuteAsync(token);
|
||||||
|
post.FavouritesCount++;
|
||||||
|
if (!post.IsFederatedCopy)
|
||||||
|
await Notifications.Add(post.GroupUserId, NotificationType.Favourite, me.Id, me.Uri, post.ID, token);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var removed = await DB.Default.DeleteAsync<Favourite>(f => f.AccountId == me.Id && f.PostId == post.ID);
|
||||||
|
if (removed.DeletedCount == 0)
|
||||||
|
return new StatusOutcome(post);
|
||||||
|
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(b => b.Inc(p => p.FavouritesCount, -1)).ExecuteAsync(token);
|
||||||
|
post.FavouritesCount--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (post.IsFederatedCopy && await AuthorInbox(post, token) is { } inbox)
|
||||||
|
{
|
||||||
|
var like = new JsonObject
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||||
|
["id"] = activityId,
|
||||||
|
["type"] = "Like",
|
||||||
|
["actor"] = me.Uri,
|
||||||
|
["object"] = post.ObjectURI
|
||||||
|
};
|
||||||
|
await _delivery.Enqueue(me, new[] { inbox }, on ? like : Undo(me, like, $"undo-like-{post.ID}-{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}"), token);
|
||||||
|
}
|
||||||
|
return new StatusOutcome(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<StatusOutcome> Reblog(LocalActor me, string postId, bool on, PostVisibility visibility, CancellationToken token)
|
||||||
|
{
|
||||||
|
var original = await Visible(me, postId, token);
|
||||||
|
if (original?.ReblogOfPostId != default)
|
||||||
|
original = await Visible(me, original.ReblogOfPostId, token);
|
||||||
|
if (original == default)
|
||||||
|
return StatusOutcome.Fail(StatusCodes.Status404NotFound, "Record not found");
|
||||||
|
if (original.Visibility is not (PostVisibility.Public or PostVisibility.Unlisted))
|
||||||
|
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: This post can't be boosted");
|
||||||
|
|
||||||
|
var existing = await _dbEntities.Posts.Match(p => p.ReblogOfPostId == original.ID && p.AuthorAccountId == me.Id && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
var announceId = me.ActivityUri($"announce-{original.ID}");
|
||||||
|
var announce = new JsonObject
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||||
|
["id"] = announceId,
|
||||||
|
["type"] = "Announce",
|
||||||
|
["actor"] = me.Uri,
|
||||||
|
["published"] = ActivityPubRenderer.Timestamp(DateTime.UtcNow),
|
||||||
|
["to"] = new JsonArray(visibility == PostVisibility.Unlisted ? me.Followers : ActivityPubRenderer.Public),
|
||||||
|
["cc"] = new JsonArray(visibility == PostVisibility.Unlisted ? ActivityPubRenderer.Public : me.Followers, original.ActorURI),
|
||||||
|
["object"] = original.ObjectURI
|
||||||
|
};
|
||||||
|
|
||||||
|
if (on)
|
||||||
|
{
|
||||||
|
if (existing != default)
|
||||||
|
return new StatusOutcome(existing);
|
||||||
|
var reblog = new PostEntity
|
||||||
|
{
|
||||||
|
GroupUserId = me.Id,
|
||||||
|
AuthorAccountId = me.Id,
|
||||||
|
ReblogOfPostId = original.ID,
|
||||||
|
Visibility = visibility == PostVisibility.Unlisted ? PostVisibility.Unlisted : PostVisibility.Public,
|
||||||
|
ObjectURI = announceId,
|
||||||
|
ActivityURI = announceId,
|
||||||
|
ActorURI = me.Uri,
|
||||||
|
To = Strings(announce["to"]),
|
||||||
|
Cc = Strings(announce["cc"])
|
||||||
|
};
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(reblog, token);
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
return new StatusOutcome(await _dbEntities.Posts.Match(p => p.ObjectURI == announceId).ExecuteFirstAsync(token));
|
||||||
|
}
|
||||||
|
await DB.Default.Update<PostEntity>().MatchID(original.ID).Modify(b => b.Inc(p => p.ReblogsCount, 1)).ExecuteAsync(token);
|
||||||
|
if (!original.IsFederatedCopy)
|
||||||
|
await Notifications.Add(original.GroupUserId, NotificationType.Reblog, me.Id, me.Uri, original.ID, token);
|
||||||
|
await _fanout.Distribute(reblog, token);
|
||||||
|
await _delivery.EnqueueToFollowers(me, announce, token, await AuthorInbox(original, token) is { } inbox ? new[] { inbox } : default);
|
||||||
|
return new StatusOutcome(reblog);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existing == default)
|
||||||
|
return new StatusOutcome(original);
|
||||||
|
await DB.Default.DeleteAsync<PostEntity>(existing.ID);
|
||||||
|
await DB.Default.DeleteAsync<TimelineEntry>(e => e.PostId == existing.ID);
|
||||||
|
await DB.Default.Update<PostEntity>().MatchID(original.ID).Modify(b => b.Inc(p => p.ReblogsCount, -1)).ExecuteAsync(token);
|
||||||
|
await _delivery.EnqueueToFollowers(me, Undo(me, announce, $"undo-announce-{original.ID}-{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}"), token,
|
||||||
|
await AuthorInbox(original, token) is { } authorInbox ? new[] { authorInbox } : default);
|
||||||
|
return new StatusOutcome(original);
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed record Recipient(string Uri, string Handle, string LocalId, string ForeignId, string Inbox);
|
||||||
|
|
||||||
|
sealed record ConversationResult(DmGroup Group, IReadOnlyList<Recipient> Recipients, int Status = StatusCodes.Status200OK, string Error = default)
|
||||||
|
{
|
||||||
|
public bool Ok => Error == default;
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<ConversationResult> Conversation(LocalActor author, StatusDraft draft, IReadOnlyList<ResolvedMention> mentioned, CancellationToken token)
|
||||||
|
{
|
||||||
|
DmGroup dmGroup = default;
|
||||||
|
if (!string.IsNullOrEmpty(draft.ConversationId))
|
||||||
|
{
|
||||||
|
dmGroup = await _dbEntities.DmGroups.MatchID(draft.ConversationId).ExecuteFirstAsync(token);
|
||||||
|
if (dmGroup == default || !dmGroup.Members.Any(m => !m.IsForeign && m.AvatarId == author.Id))
|
||||||
|
return new(default, default, StatusCodes.Status404NotFound, "Conversation not found");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var members = new List<GroupMember> { new() { AvatarId = author.Id } };
|
||||||
|
foreach (var handle in draft.Recipients.Distinct(StringComparer.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
var member = await ResolveRecipient(handle, token);
|
||||||
|
if (member == default)
|
||||||
|
return new(default, default, StatusCodes.Status404NotFound, $"Recipient '{handle}' not found");
|
||||||
|
if (members.All(m => m.AvatarId != member.AvatarId))
|
||||||
|
members.Add(member);
|
||||||
|
}
|
||||||
|
foreach (var mention in mentioned.Where(m => m.AccountId != author.Id))
|
||||||
|
{
|
||||||
|
var member = mention.IsLocal ? new GroupMember { AvatarId = mention.AccountId } : new GroupMember { AvatarId = mention.ActorUri, IsForeign = true };
|
||||||
|
if (members.All(m => m.AvatarId != member.AvatarId))
|
||||||
|
members.Add(member);
|
||||||
|
}
|
||||||
|
if (members.Count < 2)
|
||||||
|
return new(default, default, StatusCodes.Status422UnprocessableEntity, "Validation failed: A direct message needs at least one recipient");
|
||||||
|
|
||||||
|
var key = DmGroup.KeyOf(members);
|
||||||
|
dmGroup = await _dbEntities.DmGroups.Match(g => g.ParticipantsKey == key && !g.DeletionAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
if (dmGroup == default)
|
||||||
|
{
|
||||||
|
dmGroup = new DmGroup { Members = members, ParticipantsKey = key };
|
||||||
|
dmGroup.ID = (string)dmGroup.GenerateNewID();
|
||||||
|
dmGroup.ConversationURI = author.ConversationUri(dmGroup.ID);
|
||||||
|
await DB.Default.SaveAsync(dmGroup, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var recipients = new List<Recipient>();
|
||||||
|
foreach (var member in dmGroup.Members.Where(m => !(m.AvatarId == author.Id && !m.IsForeign)))
|
||||||
|
{
|
||||||
|
if (member.IsForeign)
|
||||||
|
{
|
||||||
|
var foreign = await _dbEntities.ForeignAvatars.Match(a => a.ActorURI == member.AvatarId).ExecuteFirstAsync(token);
|
||||||
|
if (foreign != default)
|
||||||
|
recipients.Add(new Recipient(foreign.ActorURI, $"{foreign.UserName}@{foreign.Domain}", default, foreign.ID, foreign.InboxURL));
|
||||||
|
}
|
||||||
|
else if (await _localActors.FindById(LocalActorKind.Person, member.AvatarId, token) is { } local)
|
||||||
|
recipients.Add(new Recipient(local.Uri, local.Handle, local.Id, default, default));
|
||||||
|
}
|
||||||
|
return new(dmGroup, recipients);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<GroupMember> ResolveRecipient(string recipient, CancellationToken token)
|
||||||
|
{
|
||||||
|
var handle = recipient.Trim().TrimStart('@');
|
||||||
|
if (!handle.Contains('@') || handle.EndsWith("@" + new Uri(_localActors.BaseAddress).Authority, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUserName(handle.Split('@')[0], token);
|
||||||
|
return local is { Kind: LocalActorKind.Person } ? new GroupMember { AvatarId = local.Id } : default;
|
||||||
|
}
|
||||||
|
var actorUri = await _remoteActors.ResolveHandle(handle, token);
|
||||||
|
var foreign = actorUri == default ? default : await _remoteActors.GetActor(actorUri, refresh: false, token);
|
||||||
|
return foreign == default ? default : new GroupMember { AvatarId = foreign.ActorURI, IsForeign = true };
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<PostEntity> Parent(string reference, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(reference))
|
||||||
|
return default;
|
||||||
|
return IsRemoteUri(reference)
|
||||||
|
? await _dbEntities.Posts.Match(p => p.ObjectURI == reference && !p.DeletedAt.HasValue).ExecuteFirstAsync(token)
|
||||||
|
: await _dbEntities.Posts.Match(p => p.ID == reference && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<PostEntity> Own(LocalActor author, string postId, CancellationToken token) =>
|
||||||
|
await _dbEntities.Posts.Match(p => p.ID == postId && p.GroupUserId == author.Id && !p.IsFederatedCopy && !p.DeletedAt.HasValue && p.ReblogOfPostId == null)
|
||||||
|
.ExecuteFirstAsync(token);
|
||||||
|
|
||||||
|
async Task<PostEntity> Visible(LocalActor me, string postId, CancellationToken token)
|
||||||
|
{
|
||||||
|
var post = string.IsNullOrEmpty(postId) ? default : await _dbEntities.Posts.Match(p => p.ID == postId && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
return post != default && await VisibilityPolicy.CanSee(post, me.Id, token) ? post : default;
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<string> AuthorInbox(PostEntity post, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (!post.IsFederatedCopy || string.IsNullOrEmpty(post.AuthorAccountId))
|
||||||
|
return default;
|
||||||
|
return (await _dbEntities.ForeignAvatars.MatchID(post.AuthorAccountId).ExecuteFirstAsync(token))?.InboxURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<bool> MayPost(Models.Group.Group group, LocalActor author, CancellationToken token)
|
||||||
|
{
|
||||||
|
var member = group.Members.FirstOrDefault(m => !m.IsForeign && m.AvatarId == author.Id);
|
||||||
|
if (group.Kind == GroupKind.Circle)
|
||||||
|
return member != default;
|
||||||
|
return group.PostingPolicy switch
|
||||||
|
{
|
||||||
|
PostingPolicy.Anyone => true,
|
||||||
|
PostingPolicy.Moderators => member?.Role is GroupRole.Owner or GroupRole.Moderator,
|
||||||
|
_ => member != default || await _dbEntities.Followings
|
||||||
|
.Match(f => f.AvatarId == author.Id && f.TargetAccountId == group.ID && f.State == FollowState.Accepted).ExecuteAnyAsync(token)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<List<MediaAttachment>> Media(LocalActor author, IReadOnlyList<string> ids, string postId, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (ids == default || ids.Count == 0)
|
||||||
|
return new List<MediaAttachment>();
|
||||||
|
if (ids.Count > 4)
|
||||||
|
return default;
|
||||||
|
var wanted = ids.Distinct().ToList();
|
||||||
|
var found = await DB.Default.Find<MediaAttachment>()
|
||||||
|
.Match(m => wanted.Contains(m.ID) && m.OwnerAvatarId == author.Id && (m.PostId == null || m.PostId == postId))
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
return found.Count == wanted.Count ? wanted.Select(id => found.First(m => m.ID == id)).ToList() : default;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async Task Attach(IEnumerable<MediaAttachment> media, string postId, CancellationToken token)
|
||||||
|
{
|
||||||
|
var ids = media.Select(m => m.ID).ToList();
|
||||||
|
if (ids.Count > 0)
|
||||||
|
await DB.Default.Update<MediaAttachment>().Match(m => ids.Contains(m.ID))
|
||||||
|
.Modify(m => m.PostId, postId).Modify(m => m.AttachedAt, DateTime.UtcNow).ExecuteAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
PostMedia ToPostMedia(MediaAttachment attachment) => new()
|
||||||
|
{
|
||||||
|
AttachmentId = attachment.ID,
|
||||||
|
ContentType = attachment.ContentType,
|
||||||
|
URL = _media.Url(attachment.FilePath),
|
||||||
|
PreviewURL = _media.Url(attachment.PreviewPath ?? attachment.FilePath),
|
||||||
|
Description = attachment.Description,
|
||||||
|
Blurhash = attachment.Blurhash,
|
||||||
|
Width = attachment.Width,
|
||||||
|
Height = attachment.Height,
|
||||||
|
Focus = attachment.Focus
|
||||||
|
};
|
||||||
|
|
||||||
|
static JsonObject Undo(LocalActor actor, JsonObject inner, string activityId) => new()
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||||
|
["id"] = actor.ActivityUri(activityId),
|
||||||
|
["type"] = "Undo",
|
||||||
|
["actor"] = actor.Uri,
|
||||||
|
["object"] = inner.DeepClone()
|
||||||
|
};
|
||||||
|
|
||||||
|
static PostMention ToMention(ResolvedMention mention) => new()
|
||||||
|
{
|
||||||
|
ActorURI = mention.ActorUri,
|
||||||
|
Handle = "@" + mention.Handle,
|
||||||
|
IsLocal = mention.IsLocal,
|
||||||
|
AccountId = mention.AccountId
|
||||||
|
};
|
||||||
|
|
||||||
|
static bool IsRemoteUri(string reference) =>
|
||||||
|
reference != default && (reference.StartsWith("https://", StringComparison.OrdinalIgnoreCase) || reference.StartsWith("http://", StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
|
static string Clean(string value) => string.IsNullOrWhiteSpace(value) ? default : value.Trim();
|
||||||
|
|
||||||
|
static List<string> Strings(JsonNode node) =>
|
||||||
|
node is JsonArray array ? array.Select(n => n?.GetValue<string>()).Where(s => s != default).ToList() : new List<string>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Social;
|
||||||
|
using PrivaPub.Models.Group;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using GroupEntity = PrivaPub.Models.Group.Group;
|
||||||
|
|
||||||
|
namespace PrivaPub.Domain.Timelines
|
||||||
|
{
|
||||||
|
public interface IFanout
|
||||||
|
{
|
||||||
|
Task Distribute(Post post, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Fanout : IFanout
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
|
||||||
|
public Fanout(DbEntities dbEntities)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Distribute(Post post, CancellationToken token)
|
||||||
|
{
|
||||||
|
var recipients = new HashSet<string>(StringComparer.Ordinal);
|
||||||
|
if (!post.IsFederatedCopy && !string.IsNullOrEmpty(post.GroupUserId))
|
||||||
|
recipients.Add(post.GroupUserId);
|
||||||
|
|
||||||
|
switch (post.Visibility)
|
||||||
|
{
|
||||||
|
case PostVisibility.Public or PostVisibility.Unlisted or PostVisibility.FollowersOnly:
|
||||||
|
var followers = post.IsFederatedCopy
|
||||||
|
? await _dbEntities.Followings.Match(f => f.TargetActorURI == post.ActorURI && f.State == FollowState.Accepted).ExecuteAsync(token)
|
||||||
|
: await _dbEntities.Followings.Match(f => f.TargetAccountId == post.GroupUserId && f.TargetIsLocal && f.State == FollowState.Accepted).ExecuteAsync(token);
|
||||||
|
foreach (var following in followers)
|
||||||
|
if (await Shows(following, post, token))
|
||||||
|
recipients.Add(following.AvatarId);
|
||||||
|
if (!string.IsNullOrEmpty(post.AudienceURI))
|
||||||
|
foreach (var member in await _dbEntities.Followings.Match(f => f.TargetActorURI == post.AudienceURI && f.State == FollowState.Accepted).ExecuteAsync(token))
|
||||||
|
recipients.Add(member.AvatarId);
|
||||||
|
break;
|
||||||
|
case PostVisibility.Direct when !string.IsNullOrEmpty(post.ConversationId):
|
||||||
|
var conversation = await _dbEntities.DmGroups.MatchID(post.ConversationId).ExecuteFirstAsync(token);
|
||||||
|
foreach (var member in conversation?.Members.Where(m => !m.IsForeign) ?? Enumerable.Empty<GroupMember>())
|
||||||
|
recipients.Add(member.AvatarId);
|
||||||
|
break;
|
||||||
|
case PostVisibility.Circle when !string.IsNullOrEmpty(post.GroupId):
|
||||||
|
var circle = await DB.Default.Find<GroupEntity>().MatchID(post.GroupId).ExecuteFirstAsync(token);
|
||||||
|
foreach (var member in circle?.Members.Where(m => !m.IsForeign) ?? Enumerable.Empty<GroupMember>())
|
||||||
|
recipients.Add(member.AvatarId);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
recipients.ExceptWith(await Relationships.Hidden.RecipientsHiding(recipients, post.ActorURI, token));
|
||||||
|
foreach (var avatarId in recipients)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(new TimelineEntry
|
||||||
|
{
|
||||||
|
AvatarId = avatarId,
|
||||||
|
PostId = post.ID,
|
||||||
|
AuthorAccountId = post.AuthorAccountId ?? post.GroupUserId,
|
||||||
|
ReblogOfPostId = post.ReblogOfPostId
|
||||||
|
}, token);
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(post.ReblogOfPostId))
|
||||||
|
foreach (var mention in post.Mentions.Where(m => m.IsLocal && !string.IsNullOrEmpty(m.AccountId)))
|
||||||
|
await Notifications.Add(mention.AccountId, NotificationType.Mention, post.AuthorAccountId ?? post.GroupUserId, post.ActorURI, post.ID, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<bool> Shows(Following following, Post post, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(post.ReblogOfPostId))
|
||||||
|
return following.ShowReblogs;
|
||||||
|
var repliedTo = post.InReplyToAccountId;
|
||||||
|
var author = post.AuthorAccountId ?? post.GroupUserId;
|
||||||
|
if (string.IsNullOrEmpty(post.InReplyToURI) || repliedTo == author || repliedTo == following.AvatarId)
|
||||||
|
return true;
|
||||||
|
if (string.IsNullOrEmpty(repliedTo))
|
||||||
|
return false;
|
||||||
|
return await _dbEntities.Followings
|
||||||
|
.Match(f => f.AvatarId == following.AvatarId && f.TargetAccountId == repliedTo && f.State == FollowState.Accepted)
|
||||||
|
.ExecuteAnyAsync(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
using Microsoft.Extensions.Localization;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels;
|
||||||
|
using PrivaPub.ClientModels.Post;
|
||||||
|
using PrivaPub.ClientModels.Social;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Resources;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
namespace PrivaPub.Domain.Timelines
|
||||||
|
{
|
||||||
|
public interface ITimelineService
|
||||||
|
{
|
||||||
|
Task<WebResult> Home(string rootUserId, string avatarId, string maxId, int limit, CancellationToken token);
|
||||||
|
Task<WebResult> Notifications(string rootUserId, string avatarId, string maxId, int limit, CancellationToken token);
|
||||||
|
Task<WebResult> MarkNotificationsRead(string rootUserId, string avatarId, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TimelineService : ITimelineService
|
||||||
|
{
|
||||||
|
const int MaxLimit = 40;
|
||||||
|
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly IStringLocalizer<GenericRes> _localizer;
|
||||||
|
|
||||||
|
public TimelineService(DbEntities dbEntities, IStringLocalizer<GenericRes> localizer)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localizer = localizer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<WebResult> Home(string rootUserId, string avatarId, string maxId, int limit, CancellationToken token)
|
||||||
|
{
|
||||||
|
var result = new WebResult();
|
||||||
|
if (!await Owns(rootUserId, avatarId, token))
|
||||||
|
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||||
|
|
||||||
|
var query = _dbEntities.TimelineEntries.Match(e => e.AvatarId == avatarId);
|
||||||
|
if (!string.IsNullOrEmpty(maxId))
|
||||||
|
query.Match(f => f.Lt(e => e.PostId, maxId));
|
||||||
|
var entries = await query.Sort(e => e.PostId, Order.Descending).Limit(Math.Clamp(limit, 1, MaxLimit)).ExecuteAsync(token);
|
||||||
|
|
||||||
|
var ids = entries.Select(e => e.PostId).Concat(entries.Where(e => e.ReblogOfPostId != default).Select(e => e.ReblogOfPostId)).Distinct().ToList();
|
||||||
|
var posts = ids.Count == 0
|
||||||
|
? new Dictionary<string, Post>()
|
||||||
|
: (await _dbEntities.Posts.Match(p => ids.Contains(p.ID) && !p.DeletedAt.HasValue).ExecuteAsync(token)).ToDictionary(p => p.ID);
|
||||||
|
|
||||||
|
result.Data = entries
|
||||||
|
.Where(e => posts.ContainsKey(e.PostId) && (e.ReblogOfPostId == default || posts.ContainsKey(e.ReblogOfPostId)))
|
||||||
|
.Select(e =>
|
||||||
|
{
|
||||||
|
var view = ToView(posts[e.PostId]);
|
||||||
|
if (e.ReblogOfPostId != default)
|
||||||
|
view.ReblogOf = ToView(posts[e.ReblogOfPostId]);
|
||||||
|
return view;
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<WebResult> Notifications(string rootUserId, string avatarId, string maxId, int limit, CancellationToken token)
|
||||||
|
{
|
||||||
|
var result = new WebResult();
|
||||||
|
if (!await Owns(rootUserId, avatarId, token))
|
||||||
|
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||||
|
|
||||||
|
var query = _dbEntities.Notifications.Match(n => n.AvatarId == avatarId);
|
||||||
|
if (!string.IsNullOrEmpty(maxId))
|
||||||
|
query.Match(f => f.Lt(n => n.ID, maxId));
|
||||||
|
result.Data = (await query.Sort(n => n.ID, Order.Descending).Limit(Math.Clamp(limit, 1, MaxLimit)).ExecuteAsync(token))
|
||||||
|
.Select(n => new ViewNotification
|
||||||
|
{
|
||||||
|
Id = n.ID,
|
||||||
|
Type = n.Type.ToString(),
|
||||||
|
FromAccountId = n.FromAccountId,
|
||||||
|
FromActorURI = n.FromActorURI,
|
||||||
|
PostId = n.PostId,
|
||||||
|
IsRead = n.IsRead,
|
||||||
|
CreatedAt = n.CreatedAt
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<WebResult> MarkNotificationsRead(string rootUserId, string avatarId, CancellationToken token)
|
||||||
|
{
|
||||||
|
var result = new WebResult();
|
||||||
|
if (!await Owns(rootUserId, avatarId, token))
|
||||||
|
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||||
|
await DB.Default.Update<Notification>().Match(n => n.AvatarId == avatarId && !n.IsRead).Modify(n => n.IsRead, true).ExecuteAsync(token);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<bool> Owns(string rootUserId, string avatarId, CancellationToken token) =>
|
||||||
|
!string.IsNullOrEmpty(rootUserId) && !string.IsNullOrEmpty(avatarId)
|
||||||
|
&& await _dbEntities.RootToAvatars.Match(ra => ra.RootId == rootUserId && ra.AvatarId == avatarId).ExecuteAnyAsync(token);
|
||||||
|
|
||||||
|
static ViewPost ToView(Post post) => new()
|
||||||
|
{
|
||||||
|
Id = post.ID,
|
||||||
|
ObjectURI = post.ObjectURI,
|
||||||
|
AuthorAvatarId = post.IsFederatedCopy ? default : post.GroupUserId,
|
||||||
|
AuthorActorURI = post.ActorURI,
|
||||||
|
GroupId = post.GroupId,
|
||||||
|
DmGroupId = post.ConversationId,
|
||||||
|
Visibility = post.Visibility.ToString(),
|
||||||
|
AnsweringToPostId = post.AnsweringToPostId,
|
||||||
|
Title = post.Title,
|
||||||
|
Text = post.Text,
|
||||||
|
ContentHtml = post.ContentHtml,
|
||||||
|
HasContentWarning = post.HasContentWarning,
|
||||||
|
IsFederatedCopy = post.IsFederatedCopy,
|
||||||
|
CreationDate = post.CreationDate
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
|
using System.Globalization;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
using PrivaPub.Federation.Objects;
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
|
||||||
namespace PrivaPub.Federation.Actors
|
namespace PrivaPub.Federation.Actors
|
||||||
{
|
{
|
||||||
@@ -23,6 +26,16 @@ namespace PrivaPub.Federation.Actors
|
|||||||
public string SharedInbox { get; init; }
|
public string SharedInbox { get; init; }
|
||||||
public string Icon { get; init; }
|
public string Icon { get; init; }
|
||||||
public bool Discoverable { get; init; } = true;
|
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 IReadOnlyList<ActorKey> Keys { get; init; } = Array.Empty<ActorKey>();
|
||||||
|
|
||||||
public ActorKey Key(string keyId) => Keys.FirstOrDefault(k => k.Id == keyId);
|
public ActorKey Key(string keyId) => Keys.FirstOrDefault(k => k.Id == keyId);
|
||||||
@@ -52,10 +65,46 @@ namespace PrivaPub.Federation.Actors
|
|||||||
SharedInbox = root.TryGetProperty("endpoints", out var endpoints) ? RemoteActorService.Text(endpoints, "sharedInbox") : default,
|
SharedInbox = root.TryGetProperty("endpoints", out var endpoints) ? RemoteActorService.Text(endpoints, "sharedInbox") : default,
|
||||||
Icon = root.TryGetProperty("icon", out var icon) ? RemoteActorService.Text(icon, "url") : default,
|
Icon = root.TryGetProperty("icon", out var icon) ? RemoteActorService.Text(icon, "url") : default,
|
||||||
Discoverable = !root.TryGetProperty("discoverable", out var discoverable) || discoverable.ValueKind != JsonValueKind.False,
|
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)
|
static List<ActorKey> ParseKeys(JsonElement root, string actorId)
|
||||||
{
|
{
|
||||||
var keys = new List<ActorKey>();
|
var keys = new List<ActorKey>();
|
||||||
|
|||||||
@@ -29,6 +29,11 @@ namespace PrivaPub.Federation.Actors
|
|||||||
public bool Discoverable { get; init; } = true;
|
public bool Discoverable { get; init; } = true;
|
||||||
public bool ManuallyApprovesFollowers { get; init; }
|
public bool ManuallyApprovesFollowers { get; init; }
|
||||||
public bool IsFederated { get; init; } = true;
|
public bool IsFederated { get; init; } = true;
|
||||||
|
public bool IsCircle { get; init; }
|
||||||
|
public bool PostingRestrictedToModerators { get; init; }
|
||||||
|
public bool IsBot { get; init; }
|
||||||
|
public bool Indexable { get; init; }
|
||||||
|
public AvatarSettings Settings { get; init; } = new();
|
||||||
public DateTime Published { get; init; }
|
public DateTime Published { get; init; }
|
||||||
public string BaseAddress { get; init; }
|
public string BaseAddress { get; init; }
|
||||||
public IReadOnlyDictionary<string, string> Fields { get; init; } = new Dictionary<string, string>();
|
public IReadOnlyDictionary<string, string> Fields { get; init; } = new Dictionary<string, string>();
|
||||||
@@ -39,6 +44,10 @@ namespace PrivaPub.Federation.Actors
|
|||||||
public string Outbox => $"{Uri}/anus";
|
public string Outbox => $"{Uri}/anus";
|
||||||
public string Followers => $"{Uri}/groupies";
|
public string Followers => $"{Uri}/groupies";
|
||||||
public string Following => $"{Uri}/stalking";
|
public string Following => $"{Uri}/stalking";
|
||||||
|
public string Featured => $"{Uri}/trophies";
|
||||||
|
public string FeaturedTags => $"{Uri}/tattoos";
|
||||||
|
public string Flock => $"{Uri}/flock";
|
||||||
|
public string Wardens => $"{Uri}/wardens";
|
||||||
public string SharedInbox => $"{BaseAddress}/human-centipede";
|
public string SharedInbox => $"{BaseAddress}/human-centipede";
|
||||||
public string Domain => new Uri(BaseAddress).Authority;
|
public string Domain => new Uri(BaseAddress).Authority;
|
||||||
public string Handle => $"{UserName}@{Domain}";
|
public string Handle => $"{UserName}@{Domain}";
|
||||||
@@ -193,8 +202,13 @@ namespace PrivaPub.Federation.Actors
|
|||||||
ThumbnailURL = avatar.ThumbnailURL,
|
ThumbnailURL = avatar.ThumbnailURL,
|
||||||
PrivateKeyPem = avatar.PrivateKey,
|
PrivateKeyPem = avatar.PrivateKey,
|
||||||
PublicKeyPem = avatar.PublicKey,
|
PublicKeyPem = avatar.PublicKey,
|
||||||
Published = avatar.CreatedAt,
|
Published = avatar.PublishedOn,
|
||||||
Fields = avatar.Fields ?? new Dictionary<string, string>(),
|
Fields = avatar.Fields ?? new Dictionary<string, string>(),
|
||||||
|
ManuallyApprovesFollowers = avatar.Settings?.IsLocked == true,
|
||||||
|
Discoverable = avatar.Settings?.IsDiscoverable != false,
|
||||||
|
IsBot = avatar.Settings?.IsBot == true,
|
||||||
|
Indexable = avatar.Settings?.IsIndexable == true,
|
||||||
|
Settings = avatar.Settings ?? new AvatarSettings(),
|
||||||
BaseAddress = BaseAddress
|
BaseAddress = BaseAddress
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -209,10 +223,11 @@ namespace PrivaPub.Federation.Actors
|
|||||||
ThumbnailURL = group.ThumbnailURL,
|
ThumbnailURL = group.ThumbnailURL,
|
||||||
PrivateKeyPem = group.PrivateKey,
|
PrivateKeyPem = group.PrivateKey,
|
||||||
PublicKeyPem = group.PublicKey,
|
PublicKeyPem = group.PublicKey,
|
||||||
Discoverable = group.IsDiscoverable,
|
Discoverable = group.Kind == GroupKind.Community && group.IsDiscoverable,
|
||||||
ManuallyApprovesFollowers = group.ManuallyApprovesMembers,
|
ManuallyApprovesFollowers = group.Kind == GroupKind.Circle || group.ManuallyApprovesMembers,
|
||||||
IsFederated = group.Kind == GroupKind.Community,
|
IsCircle = group.Kind == GroupKind.Circle,
|
||||||
Published = group.CreationDate,
|
PostingRestrictedToModerators = group.PostingPolicy == PostingPolicy.Moderators,
|
||||||
|
Published = group.PublishedOn,
|
||||||
BaseAddress = BaseAddress
|
BaseAddress = BaseAddress
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using PrivaPub.StaticServices;
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
using PrivaPub.Federation.Objects;
|
using PrivaPub.Federation.Objects;
|
||||||
using PrivaPub.Federation.Signing;
|
using PrivaPub.Federation.Signing;
|
||||||
@@ -18,6 +19,7 @@ namespace PrivaPub.Federation.Actors
|
|||||||
Task<FetchedJson> FetchObject(string uri, CancellationToken token);
|
Task<FetchedJson> FetchObject(string uri, CancellationToken token);
|
||||||
Task<ForeignAvatar> GetActor(string actorUri, bool refresh, CancellationToken token);
|
Task<ForeignAvatar> GetActor(string actorUri, bool refresh, CancellationToken token);
|
||||||
Task<ForeignAvatar> GetActorByKeyId(string keyId, 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);
|
Task<string> ResolveHandle(string handle, CancellationToken token);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,13 +34,16 @@ namespace PrivaPub.Federation.Actors
|
|||||||
readonly ILocalActorService _localActors;
|
readonly ILocalActorService _localActors;
|
||||||
readonly IMemoryCache _cache;
|
readonly IMemoryCache _cache;
|
||||||
readonly DbEntities _dbEntities;
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly IOptionsMonitor<FederationOptions> _options;
|
||||||
|
|
||||||
public RemoteActorService(IFederationHttp http, ILocalActorService localActors, IMemoryCache cache, DbEntities dbEntities)
|
public RemoteActorService(IFederationHttp http, ILocalActorService localActors, IMemoryCache cache, DbEntities dbEntities,
|
||||||
|
IOptionsMonitor<FederationOptions> options = default)
|
||||||
{
|
{
|
||||||
_http = http;
|
_http = http;
|
||||||
_localActors = localActors;
|
_localActors = localActors;
|
||||||
_cache = cache;
|
_cache = cache;
|
||||||
_dbEntities = dbEntities;
|
_dbEntities = dbEntities;
|
||||||
|
_options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<FetchedJson> FetchObject(string uri, CancellationToken token)
|
public async Task<FetchedJson> FetchObject(string uri, CancellationToken token)
|
||||||
@@ -116,14 +121,19 @@ namespace PrivaPub.Federation.Actors
|
|||||||
return await Upsert(actor, key, token);
|
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)
|
public async Task<string> ResolveHandle(string handle, CancellationToken token)
|
||||||
{
|
{
|
||||||
var parts = handle?.TrimStart('@').Split('@');
|
var parts = handle?.TrimStart('@').Split('@');
|
||||||
if (parts is not { Length: 2 } || string.IsNullOrEmpty(parts[0]) || string.IsNullOrEmpty(parts[1]))
|
if (parts is not { Length: 2 } || string.IsNullOrEmpty(parts[0]) || string.IsNullOrEmpty(parts[1]))
|
||||||
return default;
|
return default;
|
||||||
|
|
||||||
var url = $"https://{parts[1]}/.well-known/webfinger?resource={Uri.EscapeDataString($"acct:{parts[0]}@{parts[1]}")}";
|
var query = $"/.well-known/webfinger?resource={Uri.EscapeDataString($"acct:{parts[0]}@{parts[1]}")}";
|
||||||
using var fetched = await _http.GetJson(url, "application/jrd+json, application/json", sign: default, token);
|
using var fetched = await _http.GetJson($"https://{parts[1]}{query}", "application/jrd+json, application/json", sign: default, token)
|
||||||
|
?? (_options?.CurrentValue.AllowPlainHttp == true
|
||||||
|
? await _http.GetJson($"http://{parts[1]}{query}", "application/jrd+json, application/json", sign: default, token)
|
||||||
|
: default);
|
||||||
if (fetched == default)
|
if (fetched == default)
|
||||||
return default;
|
return default;
|
||||||
var document = fetched.Document;
|
var document = fetched.Document;
|
||||||
@@ -167,7 +177,17 @@ namespace PrivaPub.Federation.Actors
|
|||||||
.Modify(a => a.FollowingURL, actor.Following)
|
.Modify(a => a.FollowingURL, actor.Following)
|
||||||
.Modify(a => a.SharedInboxURL, actor.SharedInbox)
|
.Modify(a => a.SharedInboxURL, actor.SharedInbox)
|
||||||
.Modify(a => a.PictureURL, actor.Icon)
|
.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.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.AvatarType, Enum.TryParse<AvatarType>(actor.Type, out var type) ? type : AvatarType.Person)
|
.Modify(a => a.AvatarType, Enum.TryParse<AvatarType>(actor.Type, out var type) ? type : AvatarType.Person)
|
||||||
.Modify(a => a.PublicKeyId, key?.Id)
|
.Modify(a => a.PublicKeyId, key?.Id)
|
||||||
.Modify(a => a.PublicKey, key?.Pem)
|
.Modify(a => a.PublicKey, key?.Pem)
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.AspNetCore.RateLimiting;
|
using Microsoft.AspNetCore.RateLimiting;
|
||||||
|
|
||||||
using MongoDB.Entities;
|
using MongoDB.Entities;
|
||||||
|
|
||||||
using PrivaPub.Models.Federation;
|
using PrivaPub.Models.Federation;
|
||||||
using PrivaPub.Models.Post;
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
using PrivaPub.StaticServices;
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
@@ -14,12 +17,15 @@ using PrivaPub.Domain.Privacy;
|
|||||||
using PrivaPub.Federation.Actors;
|
using PrivaPub.Federation.Actors;
|
||||||
using PrivaPub.Federation.Rendering;
|
using PrivaPub.Federation.Rendering;
|
||||||
using PrivaPub.Federation.Inbox;
|
using PrivaPub.Federation.Inbox;
|
||||||
|
using PrivaPub.Federation.Signing;
|
||||||
|
using PrivaPub.Infrastructure.Http;
|
||||||
|
using PrivaPub.Models.Group;
|
||||||
using PrivaPub.Infrastructure;
|
using PrivaPub.Infrastructure;
|
||||||
|
|
||||||
namespace PrivaPub.Federation.Controllers
|
namespace PrivaPub.Federation.Controllers
|
||||||
{
|
{
|
||||||
[ApiController, Route("peasants")]
|
[ApiController, Route("peasants")]
|
||||||
public class PeasantsController : ControllerBase
|
public class PeasantsController : ControllerBase, IAsyncActionFilter
|
||||||
{
|
{
|
||||||
const string ActivityContentType = "application/activity+json; charset=utf-8";
|
const string ActivityContentType = "application/activity+json; charset=utf-8";
|
||||||
const int OutboxSize = 20;
|
const int OutboxSize = 20;
|
||||||
@@ -28,10 +34,14 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
readonly IInboxReceiver _inbox;
|
readonly IInboxReceiver _inbox;
|
||||||
readonly DbEntities _dbEntities;
|
readonly DbEntities _dbEntities;
|
||||||
readonly ILogger<PeasantsController> _logger;
|
readonly ILogger<PeasantsController> _logger;
|
||||||
|
readonly ISignedFetchAuthorizer _fetches;
|
||||||
|
readonly IOptionsMonitor<FederationOptions> _federation;
|
||||||
|
|
||||||
public PeasantsController(ILocalActorService localActors, IInboxReceiver inbox, DbEntities dbEntities,
|
public PeasantsController(ILocalActorService localActors, IInboxReceiver inbox, DbEntities dbEntities,
|
||||||
ILogger<PeasantsController> logger)
|
ILogger<PeasantsController> logger, ISignedFetchAuthorizer fetches, IOptionsMonitor<FederationOptions> federation)
|
||||||
{
|
{
|
||||||
|
_fetches = fetches;
|
||||||
|
_federation = federation;
|
||||||
_localActors = localActors;
|
_localActors = localActors;
|
||||||
_inbox = inbox;
|
_inbox = inbox;
|
||||||
_dbEntities = dbEntities;
|
_dbEntities = dbEntities;
|
||||||
@@ -80,6 +90,12 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
items.Add(ActivityPubRenderer.Announce(local, post.ObjectURI, $"announce-{post.ID}"));
|
items.Add(ActivityPubRenderer.Announce(local, post.ObjectURI, $"announce-{post.ID}"));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (post.ReblogOfPostId != default)
|
||||||
|
{
|
||||||
|
if (await AnnounceFor(post, token) is { } announce)
|
||||||
|
items.Add(announce);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
items.Add(await CreateFor(post, local, token));
|
items.Add(await CreateFor(post, local, token));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,12 +122,74 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
return local is not { IsFederated: true } ? NotFound() : Activity(ActivityPubRenderer.OrderedCollection(local.Following, 0, default));
|
return local is not { IsFederated: true } ? NotFound() : Activity(ActivityPubRenderer.OrderedCollection(local.Following, 0, default));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet, Route("{actor}/flock")]
|
||||||
|
public async Task<IActionResult> Members(string actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUserName(actor, token);
|
||||||
|
if (local is not { IsFederated: true, Kind: LocalActorKind.Group })
|
||||||
|
return NotFound();
|
||||||
|
var group = await _dbEntities.Groups.MatchID(local.Id).ExecuteFirstAsync(token);
|
||||||
|
if (local.IsCircle && !SignedFetchAuthorizer.MayReadCircle(group, await _fetches.Requester(Request, token)))
|
||||||
|
return NotFound();
|
||||||
|
return Activity(ActivityPubRenderer.OrderedCollection(local.Flock, group.Members.Count, default));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet, Route("{actor}/wardens")]
|
||||||
|
public async Task<IActionResult> Moderators(string actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUserName(actor, token);
|
||||||
|
if (local is not { IsFederated: true, Kind: LocalActorKind.Group })
|
||||||
|
return NotFound();
|
||||||
|
var group = await _dbEntities.Groups.MatchID(local.Id).ExecuteFirstAsync(token);
|
||||||
|
if (local.IsCircle && !SignedFetchAuthorizer.MayReadCircle(group, await _fetches.Requester(Request, token)))
|
||||||
|
return NotFound();
|
||||||
|
var moderators = new List<JsonNode>();
|
||||||
|
foreach (var member in group.Members.Where(m => !m.IsForeign && m.Role is GroupRole.Owner or GroupRole.Moderator))
|
||||||
|
if (await _localActors.FindById(LocalActorKind.Person, member.AvatarId, token) is { } moderator)
|
||||||
|
moderators.Add(moderator.Uri);
|
||||||
|
return Activity(ActivityPubRenderer.OrderedCollection(local.Wardens, moderators.Count, moderators));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet, Route("{actor}/trophies")]
|
||||||
|
public async Task<IActionResult> Featured(string actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUserName(actor, token);
|
||||||
|
if (local is not { IsFederated: true })
|
||||||
|
return NotFound();
|
||||||
|
var pinIds = (await DB.Default.Find<Pin>().Match(p => p.AvatarId == local.Id).Sort(p => p.ID, Order.Descending).ExecuteAsync(token))
|
||||||
|
.Select(p => p.PostId).ToList();
|
||||||
|
var posts = pinIds.Count == 0
|
||||||
|
? new List<PostEntity>()
|
||||||
|
: await _dbEntities.Posts.Match(p => pinIds.Contains(p.ID) && p.GroupUserId == local.Id && p.ReblogOfPostId == null)
|
||||||
|
.Match(VisibilityPolicy.IsPublic).ExecuteAsync(token);
|
||||||
|
var notes = new List<JsonNode>();
|
||||||
|
foreach (var id in pinIds)
|
||||||
|
if (posts.FirstOrDefault(p => p.ID == id) is { } post)
|
||||||
|
notes.Add(ActivityPubRenderer.Note(post, local, default, post.InReplyToURI));
|
||||||
|
return Activity(ActivityPubRenderer.OrderedCollection(local.Featured, notes.Count, notes));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet, Route("{actor}/tattoos")]
|
||||||
|
public async Task<IActionResult> FeaturedTags(string actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUserName(actor, token);
|
||||||
|
return local is not { IsFederated: true }
|
||||||
|
? NotFound()
|
||||||
|
: Activity(ActivityPubRenderer.OrderedCollection(local.FeaturedTags, 0, Enumerable.Empty<JsonNode>()));
|
||||||
|
}
|
||||||
|
|
||||||
[HttpGet, Route("{actor}/scribbles/{postId}")]
|
[HttpGet, Route("{actor}/scribbles/{postId}")]
|
||||||
public async Task<IActionResult> Post(string actor, string postId, CancellationToken token)
|
public async Task<IActionResult> Post(string actor, string postId, CancellationToken token)
|
||||||
{
|
{
|
||||||
var (local, post) = await PublicPost(actor, postId, token);
|
var (local, post) = await PublicPost(actor, postId, token);
|
||||||
|
if (post == default && await CirclePost(actor, postId, token) is { } circlePost)
|
||||||
|
{
|
||||||
|
var circleNote = ActivityPubRenderer.Note(circlePost.Post, circlePost.Author, circlePost.Circle, circlePost.Post.InReplyToURI);
|
||||||
|
circleNote["@context"] = ActivityPubRenderer.Context();
|
||||||
|
return Activity(circleNote);
|
||||||
|
}
|
||||||
if (post == default)
|
if (post == default)
|
||||||
return NotFound();
|
return await Tombstone(actor, postId, token) ?? NotFound();
|
||||||
if (WantsHtml())
|
if (WantsHtml())
|
||||||
return Redirect(local.PostHtmlUrl(post.ID));
|
return Redirect(local.PostHtmlUrl(post.ID));
|
||||||
|
|
||||||
@@ -124,6 +202,14 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
[HttpGet, Route("{actor}/grunts/{activityId}")]
|
[HttpGet, Route("{actor}/grunts/{activityId}")]
|
||||||
public async Task<IActionResult> Grunt(string actor, string activityId, CancellationToken token)
|
public async Task<IActionResult> Grunt(string actor, string activityId, CancellationToken token)
|
||||||
{
|
{
|
||||||
|
if (activityId.StartsWith("announce-", StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
var booster = await _localActors.FindByUserName(actor, token);
|
||||||
|
var uri = booster?.ActivityUri(activityId);
|
||||||
|
var reblog = uri == default ? default : await _dbEntities.Posts.Match(p => p.ObjectURI == uri && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
var rendered = reblog == default ? default : await AnnounceFor(reblog, token);
|
||||||
|
return rendered == default ? NotFound() : Activity(rendered);
|
||||||
|
}
|
||||||
if (!activityId.StartsWith("create-", StringComparison.Ordinal))
|
if (!activityId.StartsWith("create-", StringComparison.Ordinal))
|
||||||
return NotFound();
|
return NotFound();
|
||||||
var (local, post) = await PublicPost(actor, activityId["create-".Length..], token);
|
var (local, post) = await PublicPost(actor, activityId["create-".Length..], token);
|
||||||
@@ -153,12 +239,66 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
if (local is not { IsFederated: true, Kind: LocalActorKind.Person })
|
if (local is not { IsFederated: true, Kind: LocalActorKind.Person })
|
||||||
return default;
|
return default;
|
||||||
var post = await _dbEntities.Posts
|
var post = await _dbEntities.Posts
|
||||||
.Match(p => p.ID == postId && p.GroupUserId == local.Id && !p.IsFederatedCopy)
|
.Match(p => p.ID == postId && p.GroupUserId == local.Id && !p.IsFederatedCopy && p.ReblogOfPostId == null)
|
||||||
.Match(VisibilityPolicy.IsPublic)
|
.Match(VisibilityPolicy.IsPublic)
|
||||||
.ExecuteFirstAsync(token);
|
.ExecuteFirstAsync(token);
|
||||||
return (local, post);
|
return (local, post);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async Task<(LocalActor Author, LocalActor Circle, PostEntity Post)?> CirclePost(string actor, string postId, CancellationToken token)
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUserName(actor, token);
|
||||||
|
if (local is not { Kind: LocalActorKind.Person })
|
||||||
|
return default;
|
||||||
|
var post = await _dbEntities.Posts
|
||||||
|
.Match(p => p.ID == postId && p.GroupUserId == local.Id && !p.IsFederatedCopy && !p.DeletedAt.HasValue && p.Visibility == PostVisibility.Circle)
|
||||||
|
.ExecuteFirstAsync(token);
|
||||||
|
var circle = post == default ? default : await _dbEntities.Groups.MatchID(post.GroupId).ExecuteFirstAsync(token);
|
||||||
|
if (circle == default || !SignedFetchAuthorizer.MayReadCircle(circle, await _fetches.Requester(Request, token)))
|
||||||
|
return default;
|
||||||
|
return (local, _localActors.FromGroup(circle), post);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<IActionResult> Tombstone(string actor, string postId, CancellationToken token)
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUserName(actor, token);
|
||||||
|
if (local is not { IsFederated: true, Kind: LocalActorKind.Person })
|
||||||
|
return default;
|
||||||
|
var deleted = await _dbEntities.Posts
|
||||||
|
.Match(p => p.ID == postId && p.GroupUserId == local.Id && !p.IsFederatedCopy && p.DeletedAt.HasValue
|
||||||
|
&& (p.Visibility == PostVisibility.Public || p.Visibility == PostVisibility.Unlisted))
|
||||||
|
.ExecuteFirstAsync(token);
|
||||||
|
if (deleted == default)
|
||||||
|
return default;
|
||||||
|
var tombstone = new JsonObject
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||||
|
["id"] = local.PostUri(deleted.ID),
|
||||||
|
["type"] = "Tombstone",
|
||||||
|
["formerType"] = "Note",
|
||||||
|
["deleted"] = ActivityPubRenderer.Timestamp(deleted.DeletedAt.Value)
|
||||||
|
};
|
||||||
|
return new ContentResult { Content = tombstone.ToJsonString(), ContentType = ActivityContentType, StatusCode = StatusCodes.Status410Gone };
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<JsonObject> AnnounceFor(PostEntity reblog, CancellationToken token)
|
||||||
|
{
|
||||||
|
var original = await _dbEntities.Posts.MatchID(reblog.ReblogOfPostId).ExecuteFirstAsync(token);
|
||||||
|
if (original == default || original.DeletedAt.HasValue)
|
||||||
|
return default;
|
||||||
|
return new JsonObject
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||||
|
["id"] = reblog.ObjectURI,
|
||||||
|
["type"] = "Announce",
|
||||||
|
["actor"] = reblog.ActorURI,
|
||||||
|
["published"] = ActivityPubRenderer.Timestamp(reblog.CreationDate),
|
||||||
|
["to"] = new JsonArray(reblog.To.Select(t => (JsonNode)t).ToArray()),
|
||||||
|
["cc"] = new JsonArray(reblog.Cc.Select(c => (JsonNode)c).ToArray()),
|
||||||
|
["object"] = original.ObjectURI
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async Task<JsonObject> CreateFor(PostEntity post, LocalActor author, CancellationToken token)
|
async Task<JsonObject> CreateFor(PostEntity post, LocalActor author, CancellationToken token)
|
||||||
{
|
{
|
||||||
var group = string.IsNullOrEmpty(post.GroupId) ? default : await _localActors.FindById(LocalActorKind.Group, post.GroupId, token);
|
var group = string.IsNullOrEmpty(post.GroupId) ? default : await _localActors.FindById(LocalActorKind.Group, post.GroupId, token);
|
||||||
@@ -174,10 +314,27 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
&& !accept.Contains("ld+json", StringComparison.OrdinalIgnoreCase);
|
&& !accept.Contains("ld+json", StringComparison.OrdinalIgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[NonAction]
|
||||||
|
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
||||||
|
{
|
||||||
|
if (HttpMethods.IsGet(Request.Method))
|
||||||
|
Response.Headers.Vary = "Accept";
|
||||||
|
if (_federation.CurrentValue.SecureMode && HttpMethods.IsGet(Request.Method)
|
||||||
|
&& !string.Equals(context.RouteData.Values["actor"] as string, LocalActorService.InstanceUserName, StringComparison.OrdinalIgnoreCase)
|
||||||
|
&& await _fetches.Requester(Request, HttpContext.RequestAborted) == default)
|
||||||
|
{
|
||||||
|
context.Result = StatusCode(StatusCodes.Status401Unauthorized);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await next();
|
||||||
|
}
|
||||||
|
|
||||||
IActionResult Answer(InboxResult result)
|
IActionResult Answer(InboxResult result)
|
||||||
{
|
{
|
||||||
if (result.Error != default)
|
if (result.Error != default)
|
||||||
_logger.LogInformation("Inbox refused with {Status}: {Error}", result.StatusCode, result.Error);
|
_logger.LogInformation("Inbox refused with {Status}: {Error}", result.StatusCode, 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);
|
return result.Error == default ? StatusCode(result.StatusCode) : StatusCode(result.StatusCode, result.Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
public async Task<IActionResult> NodeInfo(string version, CancellationToken token)
|
public async Task<IActionResult> NodeInfo(string version, CancellationToken token)
|
||||||
{
|
{
|
||||||
var users = await DB.Default.CountAsync<Avatar>(a => !a.DeletionAt.HasValue, token);
|
var users = await DB.Default.CountAsync<Avatar>(a => !a.DeletionAt.HasValue, token);
|
||||||
var posts = await DB.Default.CountAsync<PostEntity>(f => f.Where(p => !p.IsFederatedCopy) & f.Where(VisibilityPolicy.IsPublic), token);
|
var posts = await DB.Default.CountAsync<PostEntity>(f => f.Where(p => !p.IsFederatedCopy && p.ReblogOfPostId == null) & f.Where(VisibilityPolicy.IsPublic), token);
|
||||||
var software = new JsonObject { ["name"] = "privapub", ["version"] = BuildInfo.Ref };
|
var software = new JsonObject { ["name"] = "privapub", ["version"] = BuildInfo.Ref };
|
||||||
if (version == "2.1")
|
if (version == "2.1")
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
namespace PrivaPub.Federation.Inbox
|
||||||
|
{
|
||||||
|
public sealed record Arrival(string ActivityId, string ActivityType, string ActorURI, string Inbox, string KeyId, string Algorithm,
|
||||||
|
IReadOnlyList<string> SignedHeaders, DateTime ReceivedAt, string Context = default)
|
||||||
|
{
|
||||||
|
static readonly AsyncLocal<Arrival> current = new();
|
||||||
|
|
||||||
|
public static Arrival Current
|
||||||
|
{
|
||||||
|
get => current.Value;
|
||||||
|
set => current.Value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Statuses;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Models.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 AcceptHandler : IActivityHandler
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IQuoteService _quotes;
|
||||||
|
|
||||||
|
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))
|
||||||
|
return;
|
||||||
|
var following = await FindFollowing(activity["object"], actor, _dbEntities, _localActors, token);
|
||||||
|
if (following == default)
|
||||||
|
return;
|
||||||
|
await Apply(following, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual Task Apply(Following following, CancellationToken token) =>
|
||||||
|
DB.Default.Update<Following>().MatchID(following.ID).Modify(f => f.State, FollowState.Accepted).ExecuteAsync(token);
|
||||||
|
|
||||||
|
public static async Task<Following> FindFollowing(JsonNode follow, ForeignAvatar target, DbEntities dbEntities, ILocalActorService localActors,
|
||||||
|
CancellationToken token)
|
||||||
|
{
|
||||||
|
if (follow is JsonObject && Value(follow, "type") is { } type && type != "Follow")
|
||||||
|
return default;
|
||||||
|
var followId = Id(follow);
|
||||||
|
var byId = followId == default
|
||||||
|
? default
|
||||||
|
: await dbEntities.Followings.Match(f => f.FollowActivityURI == followId && f.TargetActorURI == target.ActorURI).ExecuteFirstAsync(token);
|
||||||
|
if (byId != default || follow is not JsonObject)
|
||||||
|
return byId;
|
||||||
|
|
||||||
|
var follower = await localActors.FindByUri(Id(follow["actor"]), token);
|
||||||
|
return follower == default
|
||||||
|
? default
|
||||||
|
: await dbEntities.Followings.Match(f => f.AvatarId == follower.Id && f.TargetActorURI == target.ActorURI).ExecuteFirstAsync(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RejectHandler : AcceptHandler
|
||||||
|
{
|
||||||
|
public RejectHandler(DbEntities dbEntities, ILocalActorService localActors, IQuoteService quotes) : base(dbEntities, localActors, quotes)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string Type => "Reject";
|
||||||
|
|
||||||
|
protected override Task Apply(Following following, CancellationToken token) => DB.Default.DeleteAsync<Following>(following.ID);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Social;
|
||||||
|
using PrivaPub.Domain.Timelines;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Infrastructure.Ids;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using static PrivaPub.Federation.Objects.ActivityJson;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Inbox.Handlers
|
||||||
|
{
|
||||||
|
public class AnnounceHandler : IActivityHandler
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemotePosts _remotePosts;
|
||||||
|
readonly IFanout _fanout;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
|
||||||
|
public AnnounceHandler(DbEntities dbEntities, ILocalActorService localActors, IRemotePosts remotePosts, IFanout fanout, IRemoteActorService remoteActors)
|
||||||
|
{
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_remotePosts = remotePosts;
|
||||||
|
_fanout = fanout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Type => "Announce";
|
||||||
|
|
||||||
|
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var inner = activity["object"];
|
||||||
|
if (inner is JsonObject && Value(inner, "type") is "Create" or "Update" or "Delete" or "Like" or "Dislike" or "Undo" or "Add" or "Remove" or "Block")
|
||||||
|
{
|
||||||
|
await GroupActivity(inner, actor, token);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var objectUri = Id(inner);
|
||||||
|
var announceId = Id(activity);
|
||||||
|
if (objectUri == default || announceId == default)
|
||||||
|
return;
|
||||||
|
if (await _dbEntities.Posts.Match(p => p.ObjectURI == announceId).ExecuteAnyAsync(token))
|
||||||
|
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)
|
||||||
|
return;
|
||||||
|
if (original == default && !isLocal)
|
||||||
|
original = await _remotePosts.StoreContext(objectUri, 0, token);
|
||||||
|
if (original == default || original.Visibility is not (PostVisibility.Public or PostVisibility.Unlisted))
|
||||||
|
return;
|
||||||
|
|
||||||
|
var to = Strings(activity["to"]);
|
||||||
|
var cc = Strings(activity["cc"]);
|
||||||
|
var published = DateTimeOffset.TryParse(Value(activity, "published"), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var at)
|
||||||
|
? at.UtcDateTime
|
||||||
|
: DateTime.UtcNow;
|
||||||
|
var reblog = new PostEntity
|
||||||
|
{
|
||||||
|
ID = PrivacyIds.Arrived(published),
|
||||||
|
ObjectURI = announceId,
|
||||||
|
ActivityURI = announceId,
|
||||||
|
ActorURI = actor.ActorURI,
|
||||||
|
AuthorAccountId = actor.ID,
|
||||||
|
ReblogOfPostId = original.ID,
|
||||||
|
Visibility = Addressing.Classify(to, cc, actor.FollowersURL),
|
||||||
|
To = to,
|
||||||
|
Cc = cc,
|
||||||
|
IsFederatedCopy = true,
|
||||||
|
CreationDate = published,
|
||||||
|
UpdateDate = published
|
||||||
|
};
|
||||||
|
if (reblog.Visibility == PostVisibility.Direct)
|
||||||
|
return;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(reblog, token);
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await DB.Default.Update<PostEntity>().MatchID(original.ID).Modify(b => b.Inc(p => p.ReblogsCount, 1)).ExecuteAsync(token);
|
||||||
|
if (!original.IsFederatedCopy)
|
||||||
|
await Notifications.Add(original.GroupUserId, NotificationType.Reblog, actor.ID, actor.ActorURI, original.ID, token);
|
||||||
|
await _fanout.Distribute(reblog, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task GroupActivity(JsonNode inner, ForeignAvatar group, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (group.AvatarType != AvatarType.Group
|
||||||
|
|| !await _dbEntities.Followings.Match(f => f.TargetActorURI == group.ActorURI && f.State == FollowState.Accepted).ExecuteAnyAsync(token))
|
||||||
|
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))
|
||||||
|
return;
|
||||||
|
var post = await _remotePosts.StoreContext(objectUri, 0, token);
|
||||||
|
if (post == default)
|
||||||
|
return;
|
||||||
|
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);
|
||||||
|
break;
|
||||||
|
case "Update" when objectUri != default:
|
||||||
|
var stored = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.AudienceURI == group.ActorURI && !p.DeletedAt.HasValue)
|
||||||
|
.ExecuteFirstAsync(token);
|
||||||
|
if (stored == default)
|
||||||
|
return;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
return;
|
||||||
|
using (var check = await _remoteActors.FetchObject(objectUri, token))
|
||||||
|
if (check != default && Value(System.Text.Json.Nodes.JsonNode.Parse(check.Root.GetRawText()), "type") != "Tombstone")
|
||||||
|
return;
|
||||||
|
await DB.Default.DeleteAsync<PostEntity>(deleted.ID);
|
||||||
|
await DB.Default.DeleteAsync<TimelineEntry>(e => e.PostId == deleted.ID);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static List<string> Strings(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>()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using MongoDB.Entities;
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Timelines;
|
||||||
using PrivaPub.Federation.Actors;
|
using PrivaPub.Federation.Actors;
|
||||||
using PrivaPub.Federation.Moderation;
|
using PrivaPub.Federation.Moderation;
|
||||||
|
using PrivaPub.Domain.Content;
|
||||||
|
using PrivaPub.Domain.Statuses;
|
||||||
using PrivaPub.Federation.Objects;
|
using PrivaPub.Federation.Objects;
|
||||||
using PrivaPub.Federation.Outbox;
|
using PrivaPub.Federation.Outbox;
|
||||||
using PrivaPub.Infrastructure.Ids;
|
using PrivaPub.Infrastructure.Ids;
|
||||||
@@ -10,6 +13,7 @@ using PrivaPub.Federation.Rendering;
|
|||||||
using PrivaPub.Models.Federation;
|
using PrivaPub.Models.Federation;
|
||||||
using PrivaPub.Models.Group;
|
using PrivaPub.Models.Group;
|
||||||
using PrivaPub.Models.Post;
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
using PrivaPub.Models.User;
|
using PrivaPub.Models.User;
|
||||||
using PrivaPub.StaticServices;
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
@@ -29,10 +33,24 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
|||||||
readonly IRemoteActorService _remoteActors;
|
readonly IRemoteActorService _remoteActors;
|
||||||
readonly IDeliveryService _delivery;
|
readonly IDeliveryService _delivery;
|
||||||
readonly IDomainBlocks _domainBlocks;
|
readonly IDomainBlocks _domainBlocks;
|
||||||
|
readonly IFanout _fanout;
|
||||||
|
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,
|
public CreateHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery,
|
||||||
IDomainBlocks domainBlocks)
|
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;
|
||||||
_dbEntities = dbEntities;
|
_dbEntities = dbEntities;
|
||||||
_localActors = localActors;
|
_localActors = localActors;
|
||||||
_remoteActors = remoteActors;
|
_remoteActors = remoteActors;
|
||||||
@@ -45,7 +63,8 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
|||||||
public async Task Handle(JsonNode activity, ForeignAvatar author, CancellationToken token)
|
public async Task Handle(JsonNode activity, ForeignAvatar author, CancellationToken token)
|
||||||
{
|
{
|
||||||
var node = activity["object"];
|
var node = activity["object"];
|
||||||
if (node is not JsonObject || !Origin.Same(Id(node), author.ActorURI))
|
var refetched = node is not JsonObject || !Origin.Same(Id(node), author.ActorURI);
|
||||||
|
if (refetched)
|
||||||
{
|
{
|
||||||
using var fetched = await _remoteActors.FetchObject(Id(node), token);
|
using var fetched = await _remoteActors.FetchObject(Id(node), token);
|
||||||
node = fetched == default ? default : JsonNode.Parse(fetched.Root.GetRawText());
|
node = fetched == default ? default : JsonNode.Parse(fetched.Root.GetRawText());
|
||||||
@@ -53,7 +72,17 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
|||||||
var note = NoteParser.Parse(node);
|
var note = NoteParser.Parse(node);
|
||||||
if (note == default || note.AttributedTo != author.ActorURI)
|
if (note == default || note.AttributedTo != author.ActorURI)
|
||||||
return;
|
return;
|
||||||
if (await _dbEntities.Posts.Match(p => p.ObjectURI == note.Id).ExecuteAnyAsync(token))
|
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);
|
||||||
|
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))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var to = note.To.Concat(Strings(activity["to"])).Distinct(StringComparer.Ordinal).ToList();
|
var to = note.To.Concat(Strings(activity["to"])).Distinct(StringComparer.Ordinal).ToList();
|
||||||
@@ -79,28 +108,27 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
|||||||
var parent = string.IsNullOrEmpty(note.InReplyTo)
|
var parent = string.IsNullOrEmpty(note.InReplyTo)
|
||||||
? default
|
? default
|
||||||
: await _dbEntities.Posts.Match(p => p.ObjectURI == note.InReplyTo && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
: await _dbEntities.Posts.Match(p => p.ObjectURI == note.InReplyTo && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
var group = visibility is PostVisibility.Public or PostVisibility.Unlisted
|
var circle = localTargets.FirstOrDefault(t => t is { Kind: LocalActorKind.Group, IsCircle: true });
|
||||||
? localTargets.FirstOrDefault(t => t.Kind == LocalActorKind.Group)
|
if (circle != default)
|
||||||
: default;
|
{
|
||||||
if (group != default && !await IsAcceptedFollower(group, author.ActorURI, token))
|
if (!await IsCircleMember(circle, author.ActorURI, token))
|
||||||
|
return;
|
||||||
|
visibility = PostVisibility.Circle;
|
||||||
|
}
|
||||||
|
var group = circle ?? (visibility is PostVisibility.Public or PostVisibility.Unlisted
|
||||||
|
? localTargets.FirstOrDefault(t => t is { Kind: LocalActorKind.Group, IsCircle: false })
|
||||||
|
: default);
|
||||||
|
if (group is { IsCircle: false } && !await MayPost(group, author.ActorURI, token))
|
||||||
group = default;
|
group = default;
|
||||||
|
|
||||||
var repliesToLocal = parent is { IsFederatedCopy: false } && visibility != PostVisibility.Direct;
|
var repliesToLocal = parent is { IsFederatedCopy: false } && visibility != PostVisibility.Direct;
|
||||||
if (visibility == PostVisibility.Direct ? persons.Count == 0 : group == default && persons.Count == 0 && !repliesToLocal)
|
var followed = visibility != PostVisibility.Direct && await _dbEntities.Followings
|
||||||
|
.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)
|
||||||
return;
|
return;
|
||||||
|
if (parent == default && visibility != PostVisibility.Direct)
|
||||||
var mentions = new List<PostMention>();
|
parent = await _remotePosts.Parent(note.InReplyTo, 1, token);
|
||||||
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
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
DmGroup conversation = default;
|
DmGroup conversation = default;
|
||||||
if (visibility == PostVisibility.Direct)
|
if (visibility == PostVisibility.Direct)
|
||||||
@@ -112,39 +140,10 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
|||||||
conversation = await FindOrCreateConversation(participants, Origin.Same(note.Context, author.ActorURI) ? note.Context : default, token);
|
conversation = await FindOrCreateConversation(participants, Origin.Same(note.Context, author.ActorURI) ? note.Context : default, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
var post = new PostEntity
|
var post = await _remotePosts.Build(note, author, visibility, to, cc, parent, token);
|
||||||
{
|
post.ActivityURI = Id(activity);
|
||||||
ID = PrivacyIds.At(note.Published),
|
post.GroupId = group?.Id;
|
||||||
ObjectURI = note.Id,
|
post.ConversationId = conversation?.ID;
|
||||||
ActivityURI = Id(activity),
|
|
||||||
ActorURI = author.ActorURI,
|
|
||||||
AuthorAccountId = author.ID,
|
|
||||||
Visibility = visibility,
|
|
||||||
To = to,
|
|
||||||
Cc = cc,
|
|
||||||
Url = note.Url,
|
|
||||||
ContextURI = note.Context,
|
|
||||||
QuoteURI = note.QuoteUri,
|
|
||||||
GroupId = group?.Id,
|
|
||||||
ConversationId = conversation?.ID,
|
|
||||||
Title = note.Title,
|
|
||||||
SpoilerText = note.SpoilerText,
|
|
||||||
HasContentWarning = note.Sensitive,
|
|
||||||
Text = note.ContentHtml,
|
|
||||||
ContentHtml = note.ContentHtml,
|
|
||||||
ContentFormat = ContentFormat.Html,
|
|
||||||
Language = note.Language,
|
|
||||||
Mentions = mentions,
|
|
||||||
Tags = note.Tags.ToList(),
|
|
||||||
Media = _domainBlocks.Find(new Uri(author.ActorURI).Host)?.RejectMedia == true ? new() : note.Attachments.ToList(),
|
|
||||||
InReplyToURI = note.InReplyTo,
|
|
||||||
AnsweringToPostId = parent?.ID,
|
|
||||||
InReplyToAccountId = parent?.AuthorAccountId ?? parent?.GroupUserId,
|
|
||||||
IsFederatedCopy = true,
|
|
||||||
CreationDate = note.Published,
|
|
||||||
UpdateDate = note.Updated ?? note.Published,
|
|
||||||
EditedAt = note.Updated
|
|
||||||
};
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await DB.Default.SaveAsync(post, token);
|
await DB.Default.SaveAsync(post, token);
|
||||||
@@ -153,16 +152,31 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
await _records.Record(note, post, ObjectPath.Delivered, refetched, token);
|
||||||
|
await _previews.Wanted(post, token);
|
||||||
|
await _quotes.Resolve(post, note, token);
|
||||||
|
|
||||||
if (parent != default)
|
if (parent != default)
|
||||||
await DB.Default.Update<PostEntity>().MatchID(parent.ID).Modify(b => b.Inc(p => p.RepliesCount, 1)).ExecuteAsync(token);
|
await DB.Default.Update<PostEntity>().MatchID(parent.ID).Modify(b => b.Inc(p => p.RepliesCount, 1)).ExecuteAsync(token);
|
||||||
|
await _fanout.Distribute(post, token);
|
||||||
if (conversation != default)
|
if (conversation != default)
|
||||||
await DB.Default.Update<DmGroup>().MatchID(conversation.ID).Modify(g => g.UpdatedAt, DateTime.UtcNow).ExecuteAsync(token);
|
await DB.Default.Update<DmGroup>().MatchID(conversation.ID).Modify(g => g.UpdatedAt, DateTime.UtcNow).ExecuteAsync(token);
|
||||||
if (group != default)
|
if (group is { IsCircle: false })
|
||||||
|
await _groups.Announce(group, activity.AsObject(), note.Id, isNewPost: string.IsNullOrEmpty(note.InReplyTo), token);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<bool> IsCircleMember(LocalActor circle, string actorUri, CancellationToken token) =>
|
||||||
|
await _dbEntities.Groups.Match(g => g.ID == circle.Id && g.Members.Any(m => m.IsForeign && m.AvatarId == actorUri)).ExecuteAnyAsync(token);
|
||||||
|
|
||||||
|
async Task<bool> MayPost(LocalActor community, string actorUri, CancellationToken token)
|
||||||
|
{
|
||||||
|
var entity = await _dbEntities.Groups.MatchID(community.Id).ExecuteFirstAsync(token);
|
||||||
|
return entity?.PostingPolicy switch
|
||||||
{
|
{
|
||||||
var announce = ActivityPubRenderer.Announce(group, note.Id, $"announce-{post.ID}");
|
PostingPolicy.Anyone => true,
|
||||||
await _delivery.EnqueueToFollowers(group, announce, token);
|
PostingPolicy.Followers => await IsAcceptedFollower(community, actorUri, token),
|
||||||
}
|
_ => false
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task<DmGroup> FindOrCreateConversation(List<string> participantUris, string context, CancellationToken token)
|
async Task<DmGroup> FindOrCreateConversation(List<string> participantUris, string context, CancellationToken token)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using MongoDB.Entities;
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Statuses;
|
||||||
using PrivaPub.Federation.Actors;
|
using PrivaPub.Federation.Actors;
|
||||||
using PrivaPub.Federation.Objects;
|
using PrivaPub.Federation.Objects;
|
||||||
using PrivaPub.Federation.Outbox;
|
using PrivaPub.Federation.Outbox;
|
||||||
@@ -7,6 +8,7 @@ using PrivaPub.Federation.Rendering;
|
|||||||
using PrivaPub.Models.Federation;
|
using PrivaPub.Models.Federation;
|
||||||
using PrivaPub.Models.Group;
|
using PrivaPub.Models.Group;
|
||||||
using PrivaPub.Models.Post;
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
using PrivaPub.Models.User;
|
using PrivaPub.Models.User;
|
||||||
using PrivaPub.StaticServices;
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
@@ -25,9 +27,14 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
|||||||
readonly ILocalActorService _localActors;
|
readonly ILocalActorService _localActors;
|
||||||
readonly IRemoteActorService _remoteActors;
|
readonly IRemoteActorService _remoteActors;
|
||||||
readonly IDeliveryService _delivery;
|
readonly IDeliveryService _delivery;
|
||||||
|
readonly IGroupDistributor _groups;
|
||||||
|
readonly IQuoteService _quotes;
|
||||||
|
|
||||||
public DeleteHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery)
|
public DeleteHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery,
|
||||||
|
IGroupDistributor groups, IQuoteService quotes)
|
||||||
{
|
{
|
||||||
|
_quotes = quotes;
|
||||||
|
_groups = groups;
|
||||||
_dbEntities = dbEntities;
|
_dbEntities = dbEntities;
|
||||||
_localActors = localActors;
|
_localActors = localActors;
|
||||||
_remoteActors = remoteActors;
|
_remoteActors = remoteActors;
|
||||||
@@ -56,11 +63,30 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
|||||||
if (follower.LocalActorKind == LocalActorKind.Group)
|
if (follower.LocalActorKind == LocalActorKind.Group)
|
||||||
await RemoveForeignMember(follower.LocalActorId, actor.ActorURI, token);
|
await RemoveForeignMember(follower.LocalActorId, actor.ActorURI, token);
|
||||||
}
|
}
|
||||||
|
await DB.Default.DeleteAsync<Following>(f => f.TargetActorURI == actor.ActorURI);
|
||||||
|
await DB.Default.DeleteAsync<TimelineEntry>(e => e.AuthorAccountId == actor.ID);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await DB.Default.DeleteAsync<PostEntity>(p => p.ObjectURI == objectUri && p.ActorURI == actor.ActorURI);
|
await DB.Default.Update<DeletedObject>()
|
||||||
return;
|
.Match(d => d.ObjectURI == objectUri)
|
||||||
}
|
.Modify(d => d.DeletedAt, DateTime.UtcNow)
|
||||||
|
.Option(o => o.IsUpsert = true)
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
||||||
|
if (post == default)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using static PrivaPub.Federation.Objects.ActivityJson;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Inbox.Handlers
|
||||||
|
{
|
||||||
|
public class DislikeHandler : IActivityHandler
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
|
||||||
|
public DislikeHandler(DbEntities dbEntities)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Type => "Dislike";
|
||||||
|
|
||||||
|
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 || !LikeHandler.MaySee(post, actor))
|
||||||
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(new Downvote { AccountId = actor.ID, ActorURI = actor.ActorURI, PostId = post.ID, ActivityURI = Id(activity) }, token);
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(b => b.Inc(p => p.DownvotesCount, 1)).ExecuteAsync(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
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)
|
||||||
|
await _reactions.Receive(post, actor, Value(activity, "content"), activity["tag"], Id(activity), token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.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 FlagHandler : IActivityHandler
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
|
||||||
|
public FlagHandler(DbEntities dbEntities, ILocalActorService localActors)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Type => "Flag";
|
||||||
|
|
||||||
|
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var objects = activity["object"] switch
|
||||||
|
{
|
||||||
|
JsonArray array => array.Select(Id).Where(i => i != default).ToList(),
|
||||||
|
JsonNode single when Id(single) is { } id => new List<string> { id },
|
||||||
|
_ => new List<string>()
|
||||||
|
};
|
||||||
|
|
||||||
|
LocalActor target = default;
|
||||||
|
var postUris = new List<string>();
|
||||||
|
foreach (var uri in objects.Take(50))
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUri(uri, token);
|
||||||
|
if (local is { Kind: LocalActorKind.Person } && uri.TrimEnd('/') == local.Uri)
|
||||||
|
target ??= local;
|
||||||
|
else
|
||||||
|
postUris.Add(uri);
|
||||||
|
}
|
||||||
|
var posts = postUris.Count == 0
|
||||||
|
? new List<Models.Post.Post>()
|
||||||
|
: await _dbEntities.Posts.Match(p => postUris.Contains(p.ObjectURI) && !p.IsFederatedCopy).ExecuteAsync(token);
|
||||||
|
if (target == default && posts.Count > 0)
|
||||||
|
target = await _localActors.FindById(LocalActorKind.Person, posts[0].GroupUserId, token);
|
||||||
|
if (target == default)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await DB.Default.SaveAsync(new Report
|
||||||
|
{
|
||||||
|
ReporterActorURI = actor.ActorURI,
|
||||||
|
TargetAccountId = target.Id,
|
||||||
|
TargetActorURI = target.Uri,
|
||||||
|
PostIds = posts.Where(p => p.GroupUserId == target.Id).Select(p => p.ID).ToList(),
|
||||||
|
ObjectURIs = posts.Where(p => p.GroupUserId == target.Id).Select(p => p.ObjectURI).ToList(),
|
||||||
|
Comment = Value(activity, "content")
|
||||||
|
}, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loaded 100 of 169 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user