Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8f163b5ce | ||
|
|
70b4c45eb0 | ||
|
|
0028e96e76 | ||
|
|
b54cdf78b2 | ||
|
|
4d9be37c1c | ||
|
|
e6a6fe46d7 | ||
|
|
7ff3a61e75 | ||
|
|
afd2e727d0 | ||
|
|
b34c71775c | ||
|
|
e99b76dbd7 | ||
|
|
a76cc34557 | ||
|
|
61f6ca0676 | ||
|
|
8af50dad9c | ||
|
|
60bf21b019 | ||
|
|
1c78da34a9 | ||
|
|
5a13597c00 | ||
|
|
e6a362c0b8 | ||
|
|
9ec1f9930b | ||
|
|
e54e17708f | ||
|
|
e9529310fe | ||
|
|
796f06acf4 | ||
|
|
d1a91c40c4 | ||
|
|
934b6fe687 |
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"
|
||||||
@@ -37,26 +37,19 @@ actor-scoped route gets a name in the same spirit, agreed with the owner, and a
|
|||||||
|
|
||||||
| Thing | Route |
|
| Thing | Route |
|
||||||
|---|---|
|
|---|---|
|
||||||
| Actor | `/peasants/{name}` (`/users/{name}` 301-redirects here) |
|
| Actor | `/peasants/{name}` (`/users/{name}` 301-redirects here; browsers are sent to `/@{name}`) |
|
||||||
| Inbox | `/peasants/{name}/mouth` |
|
| Inbox | `/peasants/{name}/mouth` |
|
||||||
| Outbox | `/peasants/{name}/anus` |
|
| Outbox | `/peasants/{name}/anus` (`?page=true[&max_id=]` for pages) |
|
||||||
| Shared inbox | `/human-centipede` (also `/peasants/{name}/human-centipede`) |
|
| Shared inbox | `/human-centipede` (also `/peasants/{name}/human-centipede`) |
|
||||||
|
| Followers | `/peasants/{name}/groupies` |
|
||||||
|
| Following | `/peasants/{name}/stalking` |
|
||||||
|
| Notes | `/peasants/{name}/scribbles/{id}` |
|
||||||
|
| Activities | `/peasants/{name}/grunts/{id}` (`create-{postId}` resolves) |
|
||||||
|
| DM context | `/peasants/{name}/whispers/{id}` |
|
||||||
| Token refresh | `/clientapi/user/sniff/again` |
|
| Token refresh | `/clientapi/user/sniff/again` |
|
||||||
|
|
||||||
These are agreed for the roadmap and replace the conventional names still in the code today:
|
Agreed for later phases: `/gossip`, `/drool`, `/echoes` (replies, likes, shares), `/trophies` (featured), `/tattoos`
|
||||||
|
(featured tags), `/flock` and `/wardens` (group members and moderators).
|
||||||
| Thing | Route | Replaces |
|
|
||||||
|---|---|---|
|
|
||||||
| Followers | `/groupies` | `/followers` |
|
|
||||||
| Following | `/stalking` | `/following` |
|
|
||||||
| Notes | `/scribbles/{id}` | `/posts/{id}` |
|
|
||||||
| Activities | `/grunts/{id}` | `/activities/{id}` |
|
|
||||||
| Replies, likes, shares | `/gossip`, `/drool`, `/echoes` | |
|
|
||||||
| Featured | `/trophies` | |
|
|
||||||
| Featured tags | `/tattoos` | |
|
|
||||||
| Group members | `/flock` | |
|
|
||||||
| Group moderators | `/wardens` | |
|
|
||||||
| DM context | `/whispers/{id}` | |
|
|
||||||
|
|
||||||
Routes other software looks up by name stay conventional:
|
Routes other software looks up by name stay conventional:
|
||||||
- `/.well-known/*` and `/nodeinfo/*`;
|
- `/.well-known/*` and `/nodeinfo/*`;
|
||||||
@@ -75,6 +68,8 @@ PrivaPub/ ASP.NET Core Web API, net10.0
|
|||||||
Group, Post (posts + DMs), Admin, Data
|
Group, Post (posts + DMs), Admin, Data
|
||||||
Infrastructure/
|
Infrastructure/
|
||||||
Http/ FederationHttp + SafeHttpHandlerFactory + IpRangeGuard: the only way out
|
Http/ FederationHttp + SafeHttpHandlerFactory + IpRangeGuard: the only way out
|
||||||
|
Jobs/ JobQueue (leases), JobWorker, Backoff, HostCircuitBreaker
|
||||||
|
Ids/ PrivacyIds (day-only ids for personas and groups, published-time 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
|
||||||
@@ -83,13 +78,28 @@ PrivaPub/ ASP.NET Core Web API, net10.0
|
|||||||
WellKnownController (webfinger, nodeinfo), UsersController (redirect)
|
WellKnownController (webfinger, nodeinfo), UsersController (redirect)
|
||||||
Actors/ LocalActorService (LocalActor, Keys, ReservedName), RemoteActorService
|
Actors/ LocalActorService (LocalActor, Keys, ReservedName), RemoteActorService
|
||||||
(authoritative fetch, key verification, WebFinger), ActorDocument (parser)
|
(authoritative fetch, key verification, WebFinger), ActorDocument (parser)
|
||||||
Objects/ Origin (same-origin rules), ContentSanitizer (HtmlSanitizer, Mastodon allowlist)
|
Objects/ Origin, ActivityJson, NoteParser, Addressing, ContentSanitizer
|
||||||
|
Moderation/ DomainBlocks (suspend / silence / reject media)
|
||||||
Signing/ HttpSignatures (draft-cavage sign/verify)
|
Signing/ HttpSignatures (draft-cavage sign/verify)
|
||||||
Inbox/ InboxService (Follow/Undo/Create/Delete/Update)
|
Inbox/ InboxReceiver (verify, queue, 202) → InboxProcessor (job) → Handlers/{Follow,Accept,Reject,
|
||||||
Outbox/ DeliveryService + DeliveryWorker
|
Undo,Create,Update,Delete,Like,Announce}; RemotePosts (build, fetch parents, FetchAncestors)
|
||||||
Rendering/ ActivityPubRenderer (JsonObject builders)
|
Outbox/ OutboxPublisher (who a post goes to), DeliveryService (queues jobs) + DeliveryJobHandler
|
||||||
|
Rendering/ ActivityPubRenderer (Mastodon @context, actors, notes, collections)
|
||||||
|
Domain/
|
||||||
|
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)
|
||||||
|
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/, 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
|
||||||
Data/InitDb.cs first-run seeding (languages)
|
Data/InitDb.cs first-run seeding (languages)
|
||||||
PrivaPub.ClientModels/ DTOs + validation resources shared with clients
|
PrivaPub.ClientModels/ DTOs + validation resources shared with clients
|
||||||
@@ -157,8 +167,28 @@ cd /var/www/privapub.thepra.dev && sudo -u www-data ASPNETCORE_ENVIRONMENT=Produ
|
|||||||
8. **Circles never federate.** A circle's actor, collections, WebFinger and inbox answer 404, and its posts are
|
8. **Circles never federate.** A circle's actor, collections, WebFinger and inbox answer 404, and its posts are
|
||||||
`IsLocalOnly`. Only communities are Group actors.
|
`IsLocalOnly`. Only communities are Group actors.
|
||||||
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.
|
`context` decides nothing. DMs are `Post`s with `Visibility = Direct` and a `ConversationId` (`DmPost` is legacy).
|
||||||
10. **Durable delivery:** activities go out through the delivery queue, never inline in a request.
|
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
|
||||||
|
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`.
|
||||||
|
12. **Remote content is stored only when someone here asked for it:** a persona follows the author, is addressed or
|
||||||
|
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.
|
||||||
|
|
||||||
## Privacy invariants
|
## Privacy invariants
|
||||||
|
|
||||||
@@ -222,7 +252,8 @@ the owner's GoToSocial at social.arasaka.software. **Ask before acting from the
|
|||||||
- **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`.
|
||||||
|
|||||||
+100
@@ -0,0 +1,100 @@
|
|||||||
|
# Federation
|
||||||
|
|
||||||
|
PrivaPub is an ActivityPub server written in C#. This document follows
|
||||||
|
[FEP-67ff](https://codeberg.org/fediverse/fep/src/branch/main/fep/67ff/fep-67ff.md) and describes how it federates.
|
||||||
|
|
||||||
|
## Supported federation protocols and standards
|
||||||
|
|
||||||
|
- [ActivityPub](https://www.w3.org/TR/activitypub/) (server-to-server)
|
||||||
|
- [WebFinger](https://webfinger.net/)
|
||||||
|
- [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
|
||||||
|
|
||||||
|
## Supported FEPs
|
||||||
|
|
||||||
|
- [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-2c59: Discovery of a WebFinger address from an ActivityPub actor](https://codeberg.org/fediverse/fep/src/branch/main/fep/2c59/fep-2c59.md)
|
||||||
|
|
||||||
|
Planned: FEP-1b12 (communities), FEP-8fcf (followers synchronisation), FEP-5feb (`indexable`), FEP-7628 (Move),
|
||||||
|
FEP-044f (quotes).
|
||||||
|
|
||||||
|
## Actors
|
||||||
|
|
||||||
|
Every account is an *avatar*: one private login can own several, and they are deliberately unlinkable. Nothing in an
|
||||||
|
actor document, a collection, NodeInfo or a delivery relates two avatars of the same login.
|
||||||
|
|
||||||
|
| Thing | Address |
|
||||||
|
|---|---|
|
||||||
|
| Actor (Person, Group, Application) | `/peasants/{name}` (`/users/{name}` redirects) |
|
||||||
|
| Inbox | `/peasants/{name}/mouth` |
|
||||||
|
| Outbox | `/peasants/{name}/anus` |
|
||||||
|
| Shared inbox | `/human-centipede` |
|
||||||
|
| Followers / following | `/peasants/{name}/groupies`, `/peasants/{name}/stalking` |
|
||||||
|
| Objects | `/peasants/{name}/scribbles/{id}` |
|
||||||
|
| Activities | `/peasants/{name}/grunts/{id}` |
|
||||||
|
| Direct-message context | `/peasants/{name}/whispers/{id}` |
|
||||||
|
| Profile and post pages | `/@{name}`, `/@{name}/{id}` |
|
||||||
|
|
||||||
|
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.
|
||||||
|
- `published` on an actor is truncated to the day. `indexable` is `false`.
|
||||||
|
- 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.
|
||||||
|
|
||||||
|
## Groups
|
||||||
|
|
||||||
|
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
|
||||||
|
it. Full FEP-1b12 behaviour (announcing activities, `audience`, moderation) is planned.
|
||||||
|
- A circle is private and does not federate yet: its actor, collections and WebFinger answer 404, and its posts are never
|
||||||
|
delivered.
|
||||||
|
|
||||||
|
## Activities
|
||||||
|
|
||||||
|
Received:
|
||||||
|
|
||||||
|
| Activity | Effect |
|
||||||
|
|---|---|
|
||||||
|
| `Follow` | follows an avatar or community; `Accept` is sent unless the community approves members by hand |
|
||||||
|
| `Accept{Follow}`, `Reject{Follow}` | completes or ends a follow an avatar requested |
|
||||||
|
| `Undo{Follow, Like, Announce}` | reverses it |
|
||||||
|
| `Create{Note, Article, Page, Question, …}` | 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{Person}` | refetches the actor |
|
||||||
|
| `Like` | counted and notified, on posts the liker could see |
|
||||||
|
| `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 |
|
||||||
|
|
||||||
|
Sent: `Follow`, `Undo{Follow}`, `Create{Note}`, `Update{Note}`, `Update{Person}`, `Delete{Tombstone}`, `Accept{Follow}`,
|
||||||
|
`Announce` (communities). A deleted post answers 410 with a `Tombstone`.
|
||||||
|
|
||||||
|
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
|
||||||
|
`name`. A content warning without its own text uses the title, or "Content warning".
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
## Security rules a peer will notice
|
||||||
|
|
||||||
|
- **Signatures.** Inbox POSTs must be signed over `(request-target)`, `host`, `digest` and `date` (or `(created)`). The
|
||||||
|
date may be at most one hour old and fifteen minutes ahead. A bad signature gets 401, malformed input 400, an accepted
|
||||||
|
activity 202, too many requests 429. Activities are processed after the 202.
|
||||||
|
- **Origins.** An actor document is accepted only from the address it names as its `id`. A key only if its actor lists
|
||||||
|
it with `owner` set to the actor and on the actor's origin. An activity's `id`, and any object it creates, updates or
|
||||||
|
deletes, must be on its actor's origin. An embedded object from another origin is fetched from that origin.
|
||||||
|
- **Fetching.** All fetches are signed by the instance actor. They go only to public addresses, follow at most three
|
||||||
|
redirects and read at most 1 MB.
|
||||||
|
- **HTML.** Received HTML is sanitised to Mastodon's allowlist.
|
||||||
|
- **Delivery.** Failed deliveries are retried with Mastodon's backoff (16 attempts). A host that keeps failing is paused,
|
||||||
|
starting at an hour and growing to a week.
|
||||||
|
|
||||||
|
## Known limitations
|
||||||
|
|
||||||
|
- Avatars cannot send likes or boosts yet (that arrives with the Mastodon client API), and media uploads and polls are not
|
||||||
|
implemented.
|
||||||
|
- Collections expose counts, not members.
|
||||||
|
- Only `rsa-sha256`-style keys are verified. RFC 9421 signatures are planned.
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using PrivaPub.ClientModels.Resources;
|
||||||
|
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace PrivaPub.ClientModels.Admin
|
||||||
|
{
|
||||||
|
public class DomainBlockForm
|
||||||
|
{
|
||||||
|
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ErrorsResource)),
|
||||||
|
StringLength(253, MinimumLength = 3, ErrorMessageResourceName = "StringLengthMinMax", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public string Domain { get; set; }
|
||||||
|
|
||||||
|
public bool Suspend { get; set; } = true;
|
||||||
|
public bool RejectMedia { get; set; }
|
||||||
|
|
||||||
|
[StringLength(1000, ErrorMessageResourceName = "MaxLengthString", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public string PublicComment { get; set; }
|
||||||
|
|
||||||
|
[StringLength(1000, ErrorMessageResourceName = "MaxLengthString", ErrorMessageResourceType = typeof(ErrorsResource))]
|
||||||
|
public string PrivateComment { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ViewDomainBlock
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Domain { get; set; }
|
||||||
|
public string Severity { get; set; }
|
||||||
|
public bool RejectMedia { get; set; }
|
||||||
|
public string PublicComment { get; set; }
|
||||||
|
public string PrivateComment { get; set; }
|
||||||
|
public DateTime CreatedAt { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,32 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ namespace PrivaPub.ClientModels.Post
|
|||||||
public string AuthorActorURI { get; set; }
|
public string AuthorActorURI { get; set; }
|
||||||
public string GroupId { get; set; }
|
public string GroupId { get; set; }
|
||||||
public string DmGroupId { get; set; }
|
public string DmGroupId { get; set; }
|
||||||
|
public string Visibility { get; set; }
|
||||||
public string AnsweringToPostId { get; set; }
|
public string AnsweringToPostId { get; set; }
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
public string Text { get; set; }
|
public string Text { get; set; }
|
||||||
@@ -15,6 +16,7 @@ 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 class ViewDmGroup
|
public class ViewDmGroup
|
||||||
|
|||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,91 @@
|
|||||||
|
using PrivaPub.Domain.Content;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Infrastructure.Http;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
|
||||||
|
using GroupEntity = PrivaPub.Models.Group.Group;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Domain
|
||||||
|
{
|
||||||
|
public class ContentRendererTests
|
||||||
|
{
|
||||||
|
const string Base = "https://privapub.test";
|
||||||
|
readonly ContentRenderer _renderer = new(new StubLocalActors(), new StubRemoteActors());
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Decorates_local_and_remote_mentions_and_hashtags()
|
||||||
|
{
|
||||||
|
var rendered = await _renderer.Markdown("hello @alice and @bob@m.example about #Fedi **now** <script>x</script>", TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
Assert.Contains("<span class=\"h-card\" translate=\"no\"><a href=\"https://privapub.test/peasants/alice\" class=\"u-url mention\"", rendered.Html);
|
||||||
|
Assert.Contains("href=\"https://m.example/@bob\"", rendered.Html);
|
||||||
|
Assert.Contains("href=\"https://privapub.test/tags/fedi\" class=\"mention hashtag\"", rendered.Html);
|
||||||
|
Assert.Contains("<strong>now</strong>", rendered.Html);
|
||||||
|
Assert.DoesNotContain("<script>", rendered.Html);
|
||||||
|
Assert.Equal(new[] { "https://m.example/users/bob", "https://privapub.test/peasants/alice" }, rendered.Mentions.Select(m => m.ActorUri).Order(StringComparer.Ordinal));
|
||||||
|
Assert.Equal("fedi", Assert.Single(rendered.Tags));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("`@alice` in code")]
|
||||||
|
[InlineData("mail me@alice.example")]
|
||||||
|
[InlineData("@nobody is unknown")]
|
||||||
|
[InlineData("see https://x.example/page#alice")]
|
||||||
|
public async Task Leaves_what_is_not_a_mention_alone(string text)
|
||||||
|
{
|
||||||
|
var rendered = await _renderer.Markdown(text, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
Assert.Empty(rendered.Mentions);
|
||||||
|
Assert.DoesNotContain("h-card", rendered.Html);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("issue #123")]
|
||||||
|
[InlineData("it's fine")]
|
||||||
|
[InlineData("[link](https://x.example/#tag)")]
|
||||||
|
public async Task Leaves_what_is_not_a_hashtag_alone(string text) =>
|
||||||
|
Assert.Empty((await _renderer.Markdown(text, TestContext.Current.CancellationToken)).Tags);
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Renders_plain_text_like_mastodon()
|
||||||
|
{
|
||||||
|
var rendered = await _renderer.PlainText("line one\nline <two>\n\nsee https://x.example/a?b=1. #Tag", TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
Assert.Equal("<p>line one<br>line <two></p><p>see <a href=\"https://x.example/a?b=1\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">https://x.example/a?b=1</a>. "
|
||||||
|
+ "<a href=\"https://privapub.test/tags/tag\" class=\"mention hashtag\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">#<span>Tag</span></a></p>", rendered.Html);
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class StubLocalActors : ILocalActorService
|
||||||
|
{
|
||||||
|
public string BaseAddress => Base;
|
||||||
|
|
||||||
|
public Task<LocalActor> FindByUserName(string userName, CancellationToken token) =>
|
||||||
|
Task.FromResult(userName.Equals("alice", StringComparison.OrdinalIgnoreCase)
|
||||||
|
? new LocalActor { Id = "a1", UserName = "alice", BaseAddress = Base, Kind = LocalActorKind.Person }
|
||||||
|
: default);
|
||||||
|
|
||||||
|
public Task<LocalActor> FindById(LocalActorKind kind, string id, CancellationToken token) => throw new NotSupportedException();
|
||||||
|
public Task<LocalActor> FindByUri(string actorUri, CancellationToken token) => throw new NotSupportedException();
|
||||||
|
public Task<LocalActor> GetInstanceActor(CancellationToken token) => throw new NotSupportedException();
|
||||||
|
public Task<bool> IsUserNameTaken(string userName, CancellationToken token) => throw new NotSupportedException();
|
||||||
|
public Task<bool> TryReserveUserName(string userName, LocalActorKind kind, string ownerId, CancellationToken token) => throw new NotSupportedException();
|
||||||
|
public LocalActor FromAvatar(Avatar avatar) => throw new NotSupportedException();
|
||||||
|
public LocalActor FromGroup(GroupEntity group) => throw new NotSupportedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class StubRemoteActors : IRemoteActorService
|
||||||
|
{
|
||||||
|
public Task<string> ResolveHandle(string handle, CancellationToken token) =>
|
||||||
|
Task.FromResult(handle == "bob@m.example" ? "https://m.example/users/bob" : default);
|
||||||
|
|
||||||
|
public Task<ForeignAvatar> GetActor(string actorUri, bool refresh, CancellationToken token) =>
|
||||||
|
Task.FromResult(actorUri == "https://m.example/users/bob"
|
||||||
|
? new ForeignAvatar { ID = "f1", ActorURI = actorUri, UserName = "bob", Url = "https://m.example/@bob", InboxURL = actorUri + "/inbox" }
|
||||||
|
: default);
|
||||||
|
|
||||||
|
public Task<FetchedJson> FetchObject(string uri, CancellationToken token) => throw new NotSupportedException();
|
||||||
|
public Task<ForeignAvatar> GetActorByKeyId(string keyId, bool refresh, CancellationToken token) => throw new NotSupportedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,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,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,42 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Privacy;
|
||||||
|
using PrivaPub.Models.Group;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Tests.Support;
|
||||||
|
|
||||||
|
using GroupEntity = PrivaPub.Models.Group.Group;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Domain
|
||||||
|
{
|
||||||
|
[Trait("Category", "Integration")]
|
||||||
|
public class VisibilityPolicyTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Who_sees_what()
|
||||||
|
{
|
||||||
|
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var conversation = new DmGroup { Members = new() { new GroupMember { AvatarId = "alice" }, new GroupMember { AvatarId = "https://r.example/u/bob", IsForeign = true } } };
|
||||||
|
var circle = new GroupEntity { UserName = $"c{Guid.NewGuid():N}"[..20], Members = new() { new GroupMember { AvatarId = "carol" } } };
|
||||||
|
await DB.Default.SaveAsync(conversation, token);
|
||||||
|
await DB.Default.SaveAsync(circle, token);
|
||||||
|
|
||||||
|
var direct = new Post { Visibility = PostVisibility.Direct, ConversationId = conversation.ID, GroupUserId = "dave" };
|
||||||
|
var circlePost = new Post { Visibility = PostVisibility.Circle, GroupId = circle.ID };
|
||||||
|
var followersOnly = new Post { Visibility = PostVisibility.FollowersOnly, Mentions = new() { new PostMention { IsLocal = true, AccountId = "erin" } } };
|
||||||
|
var deleted = new Post { Visibility = PostVisibility.Public, DeletedAt = DateTime.UtcNow };
|
||||||
|
|
||||||
|
Assert.True(await VisibilityPolicy.CanSee(new Post(), default, token));
|
||||||
|
Assert.True(await VisibilityPolicy.CanSee(direct, "alice", token));
|
||||||
|
Assert.True(await VisibilityPolicy.CanSee(direct, "dave", token));
|
||||||
|
Assert.False(await VisibilityPolicy.CanSee(direct, "carol", token));
|
||||||
|
Assert.False(await VisibilityPolicy.CanSee(direct, default, token));
|
||||||
|
Assert.True(await VisibilityPolicy.CanSee(circlePost, "carol", token));
|
||||||
|
Assert.False(await VisibilityPolicy.CanSee(circlePost, "alice", token));
|
||||||
|
Assert.True(await VisibilityPolicy.CanSee(followersOnly, "erin", token));
|
||||||
|
Assert.False(await VisibilityPolicy.CanSee(followersOnly, "alice", token));
|
||||||
|
Assert.False(await VisibilityPolicy.CanSee(deleted, "alice", token));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,13 +3,20 @@ 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;
|
using PrivaPub.Federation.Inbox;
|
||||||
|
using PrivaPub.Federation.Moderation;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
using PrivaPub.Federation.Outbox;
|
using PrivaPub.Federation.Outbox;
|
||||||
using PrivaPub.Models;
|
using PrivaPub.Infrastructure.Jobs;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
using PrivaPub.Models.Group;
|
using PrivaPub.Models.Group;
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
using PrivaPub.Models.Post;
|
using PrivaPub.Models.Post;
|
||||||
using PrivaPub.Models.User;
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.Models;
|
||||||
using PrivaPub.StaticServices;
|
using PrivaPub.StaticServices;
|
||||||
using PrivaPub.Tests.Support;
|
using PrivaPub.Tests.Support;
|
||||||
|
|
||||||
@@ -27,7 +34,9 @@ namespace PrivaPub.Tests.Federation
|
|||||||
|
|
||||||
Peer _peer;
|
Peer _peer;
|
||||||
LocalActorService _local;
|
LocalActorService _local;
|
||||||
InboxService _inbox;
|
InboxReceiver _receiver;
|
||||||
|
InboxProcessor _processor;
|
||||||
|
DomainBlocks _blocks;
|
||||||
|
|
||||||
public async ValueTask InitializeAsync()
|
public async ValueTask InitializeAsync()
|
||||||
{
|
{
|
||||||
@@ -36,7 +45,19 @@ namespace PrivaPub.Tests.Federation
|
|||||||
var cache = new MemoryCache(new MemoryCacheOptions());
|
var cache = new MemoryCache(new MemoryCacheOptions());
|
||||||
_local = new LocalActorService(new DbEntities(), new StaticOptions<AppConfiguration>(new AppConfiguration { BackendBaseAddress = Base }));
|
_local = new LocalActorService(new DbEntities(), new StaticOptions<AppConfiguration>(new AppConfiguration { BackendBaseAddress = Base }));
|
||||||
var remote = new RemoteActorService(Peer.Http(cache), _local, cache, new DbEntities());
|
var remote = new RemoteActorService(Peer.Http(cache), _local, cache, new DbEntities());
|
||||||
_inbox = new InboxService(new DbEntities(), _local, remote, new DeliveryService(new DbEntities()), NullLogger<InboxService>.Instance);
|
var queue = new JobQueue();
|
||||||
|
var delivery = new DeliveryService(new DbEntities(), queue);
|
||||||
|
var db = new DbEntities();
|
||||||
|
_blocks = new DomainBlocks(NullLogger<DomainBlocks>.Instance);
|
||||||
|
_receiver = new InboxReceiver(_local, remote, queue, _blocks, NullLogger<InboxReceiver>.Instance);
|
||||||
|
_processor = new InboxProcessor(remote, new IActivityHandler[]
|
||||||
|
{
|
||||||
|
new FollowHandler(db, _local, remote, delivery),
|
||||||
|
new UndoHandler(db, _local),
|
||||||
|
new CreateHandler(db, _local, remote, delivery, _blocks, new Fanout(db), new RemotePosts(db, _local, remote, _blocks, queue)),
|
||||||
|
new DeleteHandler(db, _local, remote, delivery),
|
||||||
|
new UpdateHandler(db, _local, remote)
|
||||||
|
}, NullLogger<InboxProcessor>.Instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask DisposeAsync()
|
public async ValueTask DisposeAsync()
|
||||||
@@ -77,6 +98,17 @@ namespace PrivaPub.Tests.Federation
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async Task<InboxResult> Deliver(RemoteActor sender, string path, JsonNode activity)
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var result = await _receiver.Receive(sender.Post(Host, path, activity), default, token);
|
||||||
|
var dedupe = "inbox|" + (activity is JsonObject ? activity["id"]?.GetValue<string>() : default);
|
||||||
|
var job = await DB.Default.Find<Job>().Match(j => j.DedupeKey == dedupe).ExecuteFirstAsync(token);
|
||||||
|
if (job != default)
|
||||||
|
Assert.Equal(JobResult.Done, (await _processor.Handle(job, token)).Result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
static string Origin(string uri) => new Uri(uri).GetLeftPart(UriPartial.Authority);
|
static string Origin(string uri) => new Uri(uri).GetLeftPart(UriPartial.Authority);
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -88,15 +120,15 @@ namespace PrivaPub.Tests.Federation
|
|||||||
var mallory = new RemoteActor(_peer, "mallory");
|
var mallory = new RemoteActor(_peer, "mallory");
|
||||||
var context = $"{_peer.A}/contexts/{Guid.NewGuid():N}";
|
var context = $"{_peer.A}/contexts/{Guid.NewGuid():N}";
|
||||||
|
|
||||||
var first = await _inbox.Receive(bob.Post(Host, $"/peasants/{alice.UserName}/mouth", DirectCreate(bob, alice.Uri, context)), alice, token);
|
var first = await Deliver(bob, $"/peasants/{alice.UserName}/mouth", DirectCreate(bob, alice.Uri, context));
|
||||||
var injected = await _inbox.Receive(mallory.Post(Host, $"/peasants/{alice.UserName}/mouth", DirectCreate(mallory, alice.Uri, context)), alice, token);
|
var injected = await Deliver(mallory, $"/peasants/{alice.UserName}/mouth", DirectCreate(mallory, alice.Uri, context));
|
||||||
|
|
||||||
Assert.Equal(202, first.StatusCode);
|
Assert.Equal(202, first.StatusCode);
|
||||||
Assert.Equal(202, injected.StatusCode);
|
Assert.Equal(202, injected.StatusCode);
|
||||||
var bobDm = await DB.Default.Find<DmPost>().Match(p => p.ActorURI == bob.Id).ExecuteSingleAsync(token);
|
var bobDm = await DB.Default.Find<Post>().Match(p => p.ActorURI == bob.Id && p.Visibility == PostVisibility.Direct).ExecuteSingleAsync(token);
|
||||||
var malloryDm = await DB.Default.Find<DmPost>().Match(p => p.ActorURI == mallory.Id).ExecuteSingleAsync(token);
|
var malloryDm = await DB.Default.Find<Post>().Match(p => p.ActorURI == mallory.Id).ExecuteSingleAsync(token);
|
||||||
Assert.NotEqual(bobDm.GroupId, malloryDm.GroupId);
|
Assert.NotEqual(bobDm.ConversationId, malloryDm.ConversationId);
|
||||||
var bobConversation = await DB.Default.Find<DmGroup>().OneAsync(bobDm.GroupId, token);
|
var bobConversation = await DB.Default.Find<DmGroup>().OneAsync(bobDm.ConversationId, token);
|
||||||
Assert.DoesNotContain(bobConversation.Members, m => m.AvatarId == mallory.Id);
|
Assert.DoesNotContain(bobConversation.Members, m => m.AvatarId == mallory.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,12 +139,12 @@ namespace PrivaPub.Tests.Federation
|
|||||||
var alice = await LocalAvatar("alice");
|
var alice = await LocalAvatar("alice");
|
||||||
var bob = new RemoteActor(_peer, "bob");
|
var bob = new RemoteActor(_peer, "bob");
|
||||||
|
|
||||||
await _inbox.Receive(bob.Post(Host, $"/peasants/{alice.UserName}/mouth", DirectCreate(bob, alice.Uri)), alice, token);
|
await Deliver(bob, $"/peasants/{alice.UserName}/mouth", DirectCreate(bob, alice.Uri));
|
||||||
await _inbox.Receive(bob.Post(Host, $"/peasants/{alice.UserName}/mouth", DirectCreate(bob, alice.Uri)), alice, token);
|
await Deliver(bob, $"/peasants/{alice.UserName}/mouth", DirectCreate(bob, alice.Uri));
|
||||||
|
|
||||||
var dms = await DB.Default.Find<DmPost>().Match(p => p.ActorURI == bob.Id).ExecuteAsync(token);
|
var dms = await DB.Default.Find<Post>().Match(p => p.ActorURI == bob.Id && p.Visibility == PostVisibility.Direct).ExecuteAsync(token);
|
||||||
Assert.Equal(2, dms.Count);
|
Assert.Equal(2, dms.Count);
|
||||||
Assert.Single(dms.Select(d => d.GroupId).Distinct());
|
Assert.Single(dms.Select(d => d.ConversationId).Distinct());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -124,7 +156,7 @@ namespace PrivaPub.Tests.Federation
|
|||||||
var create = DirectCreate(mallory, alice.Uri);
|
var create = DirectCreate(mallory, alice.Uri);
|
||||||
create["id"] = $"{_peer.B}/activities/{Guid.NewGuid():N}";
|
create["id"] = $"{_peer.B}/activities/{Guid.NewGuid():N}";
|
||||||
|
|
||||||
var result = await _inbox.Receive(mallory.Post(Host, $"/peasants/{alice.UserName}/mouth", create), alice, token);
|
var result = await Deliver(mallory, $"/peasants/{alice.UserName}/mouth", create);
|
||||||
|
|
||||||
Assert.Equal(400, result.StatusCode);
|
Assert.Equal(400, result.StatusCode);
|
||||||
}
|
}
|
||||||
@@ -137,11 +169,10 @@ namespace PrivaPub.Tests.Federation
|
|||||||
var mallory = new RemoteActor(_peer, "mallory");
|
var mallory = new RemoteActor(_peer, "mallory");
|
||||||
var victim = new RemoteActor(_peer, "victim");
|
var victim = new RemoteActor(_peer, "victim");
|
||||||
|
|
||||||
var result = await _inbox.Receive(mallory.Post(Host, $"/peasants/{alice.UserName}/mouth",
|
var result = await Deliver(mallory, $"/peasants/{alice.UserName}/mouth", DirectCreate(mallory, alice.Uri, attributedTo: victim.Id));
|
||||||
DirectCreate(mallory, alice.Uri, attributedTo: victim.Id)), alice, token);
|
|
||||||
|
|
||||||
Assert.Equal(400, result.StatusCode);
|
Assert.Equal(400, result.StatusCode);
|
||||||
Assert.False(await DB.Default.Find<DmPost>().Match(p => p.ActorURI == victim.Id).ExecuteAnyAsync(token));
|
Assert.False(await DB.Default.Find<Post>().Match(p => p.ActorURI == victim.Id).ExecuteAnyAsync(token));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -151,11 +182,121 @@ namespace PrivaPub.Tests.Federation
|
|||||||
var alice = await LocalAvatar("alice");
|
var alice = await LocalAvatar("alice");
|
||||||
var mallory = new RemoteActor(_peer, "mallory");
|
var mallory = new RemoteActor(_peer, "mallory");
|
||||||
|
|
||||||
var result = await _inbox.Receive(mallory.Post(Host, $"/peasants/{alice.UserName}/mouth",
|
var result = await Deliver(mallory, $"/peasants/{alice.UserName}/mouth", DirectCreate(mallory, alice.Uri, objectOrigin: _peer.B));
|
||||||
DirectCreate(mallory, alice.Uri, objectOrigin: _peer.B)), alice, token);
|
|
||||||
|
|
||||||
Assert.Equal(202, result.StatusCode);
|
Assert.Equal(202, result.StatusCode);
|
||||||
Assert.False(await DB.Default.Find<DmPost>().Match(p => p.ActorURI == mallory.Id).ExecuteAnyAsync(token));
|
Assert.False(await DB.Default.Find<Post>().Match(p => p.ActorURI == mallory.Id).ExecuteAnyAsync(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
static JsonObject PublicCreate(RemoteActor author, string inReplyTo = default, params string[] cc)
|
||||||
|
{
|
||||||
|
var id = $"{Origin(author.Id)}/notes/{Guid.NewGuid():N}";
|
||||||
|
var note = new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = id,
|
||||||
|
["type"] = "Note",
|
||||||
|
["attributedTo"] = author.Id,
|
||||||
|
["content"] = "<p>hello</p>",
|
||||||
|
["to"] = new JsonArray(Addressing.Public),
|
||||||
|
["cc"] = new JsonArray(cc.Prepend(author.Id + "/followers").Select(c => (JsonNode)c).ToArray())
|
||||||
|
};
|
||||||
|
if (inReplyTo != default)
|
||||||
|
note["inReplyTo"] = inReplyTo;
|
||||||
|
return new JsonObject
|
||||||
|
{
|
||||||
|
["id"] = $"{Origin(author.Id)}/activities/{Guid.NewGuid():N}",
|
||||||
|
["type"] = "Create",
|
||||||
|
["actor"] = author.Id,
|
||||||
|
["object"] = note
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_public_reply_to_a_local_post_is_kept_and_counted()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var alice = await LocalAvatar("alice");
|
||||||
|
var parent = new Post { GroupUserId = alice.Id, AuthorAccountId = alice.Id, ActorURI = alice.Uri, Text = "hi" };
|
||||||
|
parent.ID = (string)parent.GenerateNewID();
|
||||||
|
parent.ObjectURI = alice.PostUri(parent.ID);
|
||||||
|
await DB.Default.SaveAsync(parent, token);
|
||||||
|
var bob = new RemoteActor(_peer, "bob");
|
||||||
|
|
||||||
|
await Deliver(bob, "/human-centipede", PublicCreate(bob, parent.ObjectURI));
|
||||||
|
|
||||||
|
var reply = await DB.Default.Find<Post>().Match(p => p.ActorURI == bob.Id).ExecuteSingleAsync(token);
|
||||||
|
Assert.Equal(PostVisibility.Public, reply.Visibility);
|
||||||
|
Assert.Equal(parent.ID, reply.AnsweringToPostId);
|
||||||
|
Assert.Equal(alice.Id, reply.InReplyToAccountId);
|
||||||
|
Assert.Equal(1, (await DB.Default.Find<Post>().OneAsync(parent.ID, token)).RepliesCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_public_post_nobody_here_asked_for_is_dropped()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var bob = new RemoteActor(_peer, "bob");
|
||||||
|
|
||||||
|
await Deliver(bob, "/human-centipede", PublicCreate(bob));
|
||||||
|
|
||||||
|
Assert.False(await DB.Default.Find<Post>().Match(p => p.ActorURI == bob.Id).ExecuteAnyAsync(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_mention_is_kept_and_linked_to_the_local_persona()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var alice = await LocalAvatar("alice");
|
||||||
|
var bob = new RemoteActor(_peer, "bob");
|
||||||
|
var create = PublicCreate(bob, default, alice.Uri);
|
||||||
|
create["object"]!["tag"] = new JsonArray(new JsonObject { ["type"] = "Mention", ["href"] = alice.Uri, ["name"] = "@" + alice.Handle });
|
||||||
|
|
||||||
|
await Deliver(bob, "/human-centipede", create);
|
||||||
|
|
||||||
|
var post = await DB.Default.Find<Post>().Match(p => p.ActorURI == bob.Id).ExecuteSingleAsync(token);
|
||||||
|
var mention = Assert.Single(post.Mentions);
|
||||||
|
Assert.True(mention.IsLocal);
|
||||||
|
Assert.Equal(alice.Id, mention.AccountId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_suspended_domain_is_dropped_before_its_key_is_fetched()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var alice = await LocalAvatar("alice");
|
||||||
|
var bob = new RemoteActor(_peer, "bob", _peer.B);
|
||||||
|
await DB.Default.SaveAsync(new DomainBlock { Domain = "localhost", Severity = DomainBlockSeverity.Suspend }, token);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _blocks.Reload(token);
|
||||||
|
var before = _peer.Requests.Count;
|
||||||
|
|
||||||
|
var result = await Deliver(bob, $"/peasants/{alice.UserName}/mouth", DirectCreate(bob, alice.Uri));
|
||||||
|
|
||||||
|
Assert.Equal(202, result.StatusCode);
|
||||||
|
Assert.Equal(before, _peer.Requests.Count);
|
||||||
|
Assert.False(await DB.Default.Find<Post>().Match(p => p.ActorURI == bob.Id).ExecuteAnyAsync(token));
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
await DB.Default.DeleteAsync<DomainBlock>(b => b.Domain == "localhost");
|
||||||
|
await _blocks.Reload(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_block_covers_subdomains_but_not_lookalikes()
|
||||||
|
{
|
||||||
|
var blocks = new DomainBlocks(NullLogger<DomainBlocks>.Instance);
|
||||||
|
typeof(DomainBlocks).GetField("_blocks", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
|
||||||
|
.SetValue(blocks, new Dictionary<string, DomainBlock> { ["evil.example"] = new() { Domain = "evil.example", Severity = DomainBlockSeverity.Silence } });
|
||||||
|
typeof(DomainBlocks).GetField("_loadedAt", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
|
||||||
|
.SetValue(blocks, DateTime.UtcNow);
|
||||||
|
|
||||||
|
Assert.NotNull(blocks.Find("evil.example"));
|
||||||
|
Assert.NotNull(blocks.Find("A.Evil.Example."));
|
||||||
|
Assert.Null(blocks.Find("notevil.example"));
|
||||||
|
Assert.False(blocks.IsSuspended("evil.example"));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -167,7 +308,7 @@ namespace PrivaPub.Tests.Federation
|
|||||||
var request = mallory.Post(Host, $"/peasants/{alice.UserName}/mouth", DirectCreate(mallory, alice.Uri));
|
var request = mallory.Post(Host, $"/peasants/{alice.UserName}/mouth", DirectCreate(mallory, alice.Uri));
|
||||||
request.Headers["Signature"] = request.Headers["Signature"].ToString().Replace("signature=\"", "signature=\"AAAA");
|
request.Headers["Signature"] = request.Headers["Signature"].ToString().Replace("signature=\"", "signature=\"AAAA");
|
||||||
|
|
||||||
Assert.Equal(401, (await _inbox.Receive(request, alice, token)).StatusCode);
|
Assert.Equal(401, (await _receiver.Receive(request, alice, token)).StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -178,7 +319,7 @@ namespace PrivaPub.Tests.Federation
|
|||||||
var mallory = new RemoteActor(_peer, "mallory");
|
var mallory = new RemoteActor(_peer, "mallory");
|
||||||
|
|
||||||
foreach (var junk in new JsonNode[] { new JsonArray(1, 2), JsonValue.Create("x"), new JsonObject { ["type"] = "Create" } })
|
foreach (var junk in new JsonNode[] { new JsonArray(1, 2), JsonValue.Create("x"), new JsonObject { ["type"] = "Create" } })
|
||||||
Assert.Equal(400, (await _inbox.Receive(mallory.Post(Host, $"/peasants/{alice.UserName}/mouth", junk), alice, token)).StatusCode);
|
Assert.Equal(400, (await Deliver(mallory, $"/peasants/{alice.UserName}/mouth", junk)).StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -199,7 +340,7 @@ namespace PrivaPub.Tests.Federation
|
|||||||
};
|
};
|
||||||
|
|
||||||
Assert.False(actor.IsFederated);
|
Assert.False(actor.IsFederated);
|
||||||
Assert.Equal(404, (await _inbox.Receive(bob.Post(Host, "/human-centipede", follow), default, token)).StatusCode);
|
Assert.Equal(404, (await Deliver(bob, "/human-centipede", follow)).StatusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Federation
|
||||||
|
{
|
||||||
|
public class NoteParserTests
|
||||||
|
{
|
||||||
|
const string MastodonNote = """
|
||||||
|
{
|
||||||
|
"id": "https://m.example/users/alice/statuses/1",
|
||||||
|
"type": "Note",
|
||||||
|
"summary": "spoilers",
|
||||||
|
"inReplyTo": "https://privapub.test/peasants/bob/posts/abc",
|
||||||
|
"published": "2026-09-30T10:00:00Z",
|
||||||
|
"url": "https://m.example/@alice/1",
|
||||||
|
"attributedTo": "https://m.example/users/alice",
|
||||||
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
||||||
|
"cc": ["https://m.example/users/alice/followers", "https://privapub.test/peasants/bob"],
|
||||||
|
"sensitive": true,
|
||||||
|
"conversation": "tag:m.example,2026-09-30:objectId=1:objectType=Conversation",
|
||||||
|
"context": "https://m.example/contexts/1",
|
||||||
|
"content": "<p><span class=\"h-card\"><a href=\"https://privapub.test/peasants/bob\" class=\"u-url mention\">@<span>bob</span></a></span> hi <a href=\"https://m.example/tags/Fedi\" class=\"mention hashtag\" rel=\"tag\">#<span>Fedi</span></a><script>x</script></p>",
|
||||||
|
"contentMap": { "en": "<p>hi</p>" },
|
||||||
|
"attachment": [{
|
||||||
|
"type": "Document", "mediaType": "image/png", "url": "https://files.m.example/a.png",
|
||||||
|
"name": "a cat", "blurhash": "UBL_:rOpGG-oBUNG,qRj2so|=eE1w^n4S5NH", "focalPoint": [0.5, -2.0], "width": 640, "height": 480
|
||||||
|
}],
|
||||||
|
"tag": [
|
||||||
|
{ "type": "Mention", "href": "https://privapub.test/peasants/bob", "name": "@bob@privapub.test" },
|
||||||
|
{ "type": "Hashtag", "href": "https://m.example/tags/fedi", "name": "#Fedi" },
|
||||||
|
{ "type": "Emoji", "name": ":blob:", "icon": { "type": "Image", "url": "https://files.m.example/blob.png" } }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Reads_a_mastodon_note()
|
||||||
|
{
|
||||||
|
var note = NoteParser.Parse(JsonNode.Parse(MastodonNote));
|
||||||
|
|
||||||
|
Assert.Equal("https://m.example/users/alice", note.AttributedTo);
|
||||||
|
Assert.Equal("spoilers", note.SpoilerText);
|
||||||
|
Assert.True(note.Sensitive);
|
||||||
|
Assert.Equal("en", note.Language);
|
||||||
|
Assert.Equal("https://m.example/@alice/1", note.Url);
|
||||||
|
Assert.Equal("https://m.example/contexts/1", note.Context);
|
||||||
|
Assert.DoesNotContain("<script>", note.ContentHtml);
|
||||||
|
Assert.Contains("u-url mention", note.ContentHtml);
|
||||||
|
Assert.Equal("https://privapub.test/peasants/bob", Assert.Single(note.Mentions).ActorURI);
|
||||||
|
Assert.Equal("fedi", Assert.Single(note.Tags));
|
||||||
|
var media = Assert.Single(note.Attachments);
|
||||||
|
Assert.Equal("a cat", media.Description);
|
||||||
|
Assert.Equal(new[] { 0.5f, -1f }, media.Focus);
|
||||||
|
Assert.Equal(new DateTime(2026, 9, 30, 10, 0, 0, DateTimeKind.Utc), note.Published);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Reads_a_lemmy_page_with_a_title()
|
||||||
|
{
|
||||||
|
var note = NoteParser.Parse(JsonNode.Parse("""
|
||||||
|
{
|
||||||
|
"id": "https://l.example/post/7", "type": "Page", "name": "A title",
|
||||||
|
"attributedTo": "https://l.example/u/carol", "audience": "https://l.example/c/cats",
|
||||||
|
"content": "<p>body</p>", "to": ["https://l.example/c/cats", "https://www.w3.org/ns/activitystreams#Public"],
|
||||||
|
"published": "2026-09-30T10:00:00.123456+00:00"
|
||||||
|
}
|
||||||
|
"""));
|
||||||
|
|
||||||
|
Assert.Equal("A title", note.Title);
|
||||||
|
Assert.Equal("https://l.example/c/cats", note.Audience);
|
||||||
|
Assert.False(note.Sensitive);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Falls_back_to_misskey_content_and_takes_the_person_from_a_peertube_attribution()
|
||||||
|
{
|
||||||
|
var note = NoteParser.Parse(JsonNode.Parse("""
|
||||||
|
{
|
||||||
|
"id": "https://k.example/notes/9", "type": "Note",
|
||||||
|
"attributedTo": [{ "type": "Group", "id": "https://k.example/c/chan" }, { "type": "Person", "id": "https://k.example/users/9" }],
|
||||||
|
"_misskey_content": "hello <b>there</b>\nnext", "_misskey_quote": "https://k.example/notes/8"
|
||||||
|
}
|
||||||
|
"""));
|
||||||
|
|
||||||
|
Assert.Equal("https://k.example/users/9", note.AttributedTo);
|
||||||
|
Assert.Equal("hello <b>there</b><br>next", note.ContentHtml);
|
||||||
|
Assert.Equal("https://k.example/notes/8", note.QuoteUri);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("""{ "id": "https://x.example/1", "type": "Person" }""")]
|
||||||
|
[InlineData("""{ "id": "not a uri", "type": "Note" }""")]
|
||||||
|
[InlineData("""[1, 2]""")]
|
||||||
|
public void Refuses_what_is_not_a_note(string json) =>
|
||||||
|
Assert.Null(NoteParser.Parse(JsonNode.Parse(json)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AddressingTests
|
||||||
|
{
|
||||||
|
const string Followers = "https://m.example/users/alice/followers";
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(new[] { Addressing.Public }, new[] { Followers }, PostVisibility.Public)]
|
||||||
|
[InlineData(new[] { "as:Public" }, new string[0], PostVisibility.Public)]
|
||||||
|
[InlineData(new[] { Followers }, new[] { Addressing.Public }, PostVisibility.Unlisted)]
|
||||||
|
[InlineData(new[] { Followers }, new[] { "https://privapub.test/peasants/bob" }, PostVisibility.FollowersOnly)]
|
||||||
|
[InlineData(new[] { "https://privapub.test/peasants/bob" }, new string[0], PostVisibility.Direct)]
|
||||||
|
[InlineData(new[] { "https://m.example/users/mallory/followers" }, new string[0], PostVisibility.Direct)]
|
||||||
|
public void Classifies_like_mastodon(string[] to, string[] cc, PostVisibility expected) =>
|
||||||
|
Assert.Equal(expected, Addressing.Classify(to, cc, Followers));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Rendering;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Federation
|
||||||
|
{
|
||||||
|
public class RendererTests
|
||||||
|
{
|
||||||
|
static readonly LocalActor Alice = new()
|
||||||
|
{
|
||||||
|
Id = "a1",
|
||||||
|
Kind = LocalActorKind.Person,
|
||||||
|
UserName = "alice",
|
||||||
|
Name = "Alice",
|
||||||
|
Summary = "I **like** <b>cats</b>",
|
||||||
|
PublicKeyPem = Keys.NewKeyPair().PublicKeyPem,
|
||||||
|
Published = new DateTime(2026, 9, 30, 17, 45, 12, DateTimeKind.Utc),
|
||||||
|
Fields = new Dictionary<string, string> { ["site"] = "https://alice.example", ["mood"] = "<i>ok</i>" },
|
||||||
|
BaseAddress = "https://privapub.test"
|
||||||
|
};
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void An_actor_looks_like_a_mastodon_actor_with_our_route_names()
|
||||||
|
{
|
||||||
|
var actor = ActivityPubRenderer.Actor(Alice);
|
||||||
|
|
||||||
|
Assert.Equal("https://privapub.test/peasants/alice", actor["id"]!.GetValue<string>());
|
||||||
|
Assert.Equal("https://privapub.test/@alice", actor["url"]!.GetValue<string>());
|
||||||
|
Assert.Equal("https://privapub.test/peasants/alice/mouth", actor["inbox"]!.GetValue<string>());
|
||||||
|
Assert.Equal("https://privapub.test/peasants/alice/anus", actor["outbox"]!.GetValue<string>());
|
||||||
|
Assert.Equal("https://privapub.test/peasants/alice/groupies", actor["followers"]!.GetValue<string>());
|
||||||
|
Assert.Equal("https://privapub.test/peasants/alice/stalking", actor["following"]!.GetValue<string>());
|
||||||
|
Assert.Equal("https://privapub.test/human-centipede", actor["endpoints"]!["sharedInbox"]!.GetValue<string>());
|
||||||
|
Assert.Equal("2026-09-30T00:00:00Z", actor["published"]!.GetValue<string>());
|
||||||
|
Assert.Equal("alice@privapub.test", actor["webfinger"]!.GetValue<string>());
|
||||||
|
Assert.False(actor["indexable"]!.GetValue<bool>());
|
||||||
|
Assert.Equal("<p>I <strong>like</strong> <b>cats</b></p>", actor["summary"]!.GetValue<string>());
|
||||||
|
var fields = actor["attachment"]!.AsArray();
|
||||||
|
Assert.Contains("rel=\"nofollow noopener noreferrer me\"", fields[0]!["value"]!.GetValue<string>());
|
||||||
|
Assert.Equal("<i>ok</i>", fields[1]!["value"]!.GetValue<string>());
|
||||||
|
Assert.Equal("https://privapub.test/peasants/alice#main-key", actor["publicKey"]!["id"]!.GetValue<string>());
|
||||||
|
Assert.Equal(actor["id"]!.GetValue<string>(), actor["publicKey"]!["owner"]!.GetValue<string>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_titled_post_with_a_warning_keeps_the_title_visible_on_mastodon()
|
||||||
|
{
|
||||||
|
var post = new Post
|
||||||
|
{
|
||||||
|
ID = "p1",
|
||||||
|
Title = "On <cats>",
|
||||||
|
ContentHtml = "<p>body</p>",
|
||||||
|
HasContentWarning = true,
|
||||||
|
Language = "en",
|
||||||
|
Mentions = new() { new PostMention { ActorURI = "https://m.example/users/bob", Handle = "@bob@m.example" } },
|
||||||
|
Tags = new() { "cats" },
|
||||||
|
CreationDate = new DateTime(2026, 9, 30, 17, 45, 12, DateTimeKind.Utc)
|
||||||
|
};
|
||||||
|
|
||||||
|
var note = ActivityPubRenderer.Note(post, Alice, default, "https://m.example/users/bob/statuses/1");
|
||||||
|
|
||||||
|
Assert.Equal("https://privapub.test/peasants/alice/scribbles/p1", note["id"]!.GetValue<string>());
|
||||||
|
Assert.Equal("https://privapub.test/@alice/p1", note["url"]!.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.True(note["sensitive"]!.GetValue<bool>());
|
||||||
|
Assert.Equal("2026-09-30T17:45:12Z", note["published"]!.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>()));
|
||||||
|
var tags = note["tag"]!.AsArray();
|
||||||
|
Assert.Equal("@bob@m.example", tags[0]!["name"]!.GetValue<string>());
|
||||||
|
Assert.Equal("https://privapub.test/tags/cats", tags[1]!["href"]!.GetValue<string>());
|
||||||
|
Assert.Equal("https://m.example/users/bob/statuses/1", note["inReplyTo"]!.GetValue<string>());
|
||||||
|
Assert.NotNull(note["contentMap"]!["en"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_warning_without_a_title_or_spoiler_gets_a_generic_summary()
|
||||||
|
{
|
||||||
|
var note = ActivityPubRenderer.Note(new Post { ID = "p2", ContentHtml = "<p>x</p>", HasContentWarning = true }, Alice, default, default);
|
||||||
|
|
||||||
|
Assert.Equal(ActivityPubRenderer.ContentWarning, note["summary"]!.GetValue<string>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_direct_note_is_addressed_to_its_recipients_only()
|
||||||
|
{
|
||||||
|
var post = new Post { ID = "p3", ContentHtml = "<p>psst</p>", Visibility = PostVisibility.Direct };
|
||||||
|
|
||||||
|
var note = ActivityPubRenderer.DirectNote(post, Alice, new[] { ("https://m.example/users/bob", "bob@m.example") },
|
||||||
|
Alice.ConversationUri("c1"));
|
||||||
|
|
||||||
|
Assert.Equal(new[] { "https://m.example/users/bob" }, note["to"]!.AsArray().Select(n => n!.GetValue<string>()));
|
||||||
|
Assert.Empty(note["cc"]!.AsArray());
|
||||||
|
Assert.Equal("https://privapub.test/peasants/alice/whispers/c1", note["context"]!.GetValue<string>());
|
||||||
|
Assert.StartsWith("<p><span class=\"h-card\"", note["content"]!.GetValue<string>());
|
||||||
|
Assert.Equal("Mention", note["tag"]![0]!["type"]!.GetValue<string>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,9 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Moderation;
|
||||||
using PrivaPub.Infrastructure.Http;
|
using PrivaPub.Infrastructure.Http;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
|
||||||
namespace PrivaPub.Tests.Infrastructure
|
namespace PrivaPub.Tests.Infrastructure
|
||||||
{
|
{
|
||||||
@@ -39,7 +41,7 @@ namespace PrivaPub.Tests.Infrastructure
|
|||||||
.ConfigurePrimaryHttpMessageHandler(() => SafeHttpHandlerFactory.Create(options));
|
.ConfigurePrimaryHttpMessageHandler(() => SafeHttpHandlerFactory.Create(options));
|
||||||
var provider = services.BuildServiceProvider();
|
var provider = services.BuildServiceProvider();
|
||||||
return new FederationHttp(provider.GetRequiredService<IHttpClientFactory>(), new MemoryCache(new MemoryCacheOptions()),
|
return new FederationHttp(provider.GetRequiredService<IHttpClientFactory>(), new MemoryCache(new MemoryCacheOptions()),
|
||||||
new StaticOptionsMonitor(options), NullLogger<FederationHttp>.Instance);
|
new StaticOptionsMonitor(options), new StaticBlocks(), NullLogger<FederationHttp>.Instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -91,6 +93,29 @@ namespace PrivaPub.Tests.Infrastructure
|
|||||||
public void IsAllowed_takes_https_dns_names_only(string url, bool allowed) =>
|
public void IsAllowed_takes_https_dns_names_only(string url, bool allowed) =>
|
||||||
Assert.Equal(allowed, Client(allowTestNetwork: false).IsAllowed(new Uri(url)));
|
Assert.Equal(allowed, Client(allowTestNetwork: false).IsAllowed(new Uri(url)));
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void IsAllowed_refuses_a_suspended_domain_and_its_subdomains()
|
||||||
|
{
|
||||||
|
var http = new FederationHttp(new ServiceCollection().AddHttpClient().BuildServiceProvider().GetRequiredService<IHttpClientFactory>(),
|
||||||
|
new MemoryCache(new MemoryCacheOptions()), new StaticOptionsMonitor(new FederationOptions()), new StaticBlocks("evil.example"),
|
||||||
|
NullLogger<FederationHttp>.Instance);
|
||||||
|
|
||||||
|
Assert.False(http.IsAllowed(new Uri("https://evil.example/users/x")));
|
||||||
|
Assert.False(http.IsAllowed(new Uri("https://cdn.evil.example/a.png")));
|
||||||
|
Assert.True(http.IsAllowed(new Uri("https://notevil.example/users/x")));
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class StaticBlocks : IDomainBlocks
|
||||||
|
{
|
||||||
|
readonly string[] _suspended;
|
||||||
|
public StaticBlocks(params string[] suspended) => _suspended = suspended;
|
||||||
|
public DomainBlock Find(string host) => _suspended.Any(s => host == s || host.EndsWith("." + s))
|
||||||
|
? new DomainBlock { Domain = host, Severity = DomainBlockSeverity.Suspend }
|
||||||
|
: default;
|
||||||
|
public bool IsSuspended(string host) => Find(host) != default;
|
||||||
|
public Task Reload(CancellationToken token) => Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
sealed class StaticOptionsMonitor : IOptionsMonitor<FederationOptions>
|
sealed class StaticOptionsMonitor : IOptionsMonitor<FederationOptions>
|
||||||
{
|
{
|
||||||
public StaticOptionsMonitor(FederationOptions value) => CurrentValue = value;
|
public StaticOptionsMonitor(FederationOptions value) => CurrentValue = value;
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ namespace PrivaPub.Tests.Infrastructure
|
|||||||
{
|
{
|
||||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||||
var token = TestContext.Current.CancellationToken;
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
await DB.Default.Index<ForeignAvatar>().DropAllAsync(token);
|
||||||
|
await DB.Default.Index<Follower>().DropAllAsync(token);
|
||||||
var actorUri = $"https://r.example/users/{Guid.NewGuid():N}";
|
var actorUri = $"https://r.example/users/{Guid.NewGuid():N}";
|
||||||
var localId = Guid.NewGuid().ToString("N")[..24];
|
var localId = Guid.NewGuid().ToString("N")[..24];
|
||||||
await DB.Default.SaveAsync(new[]
|
await DB.Default.SaveAsync(new[]
|
||||||
@@ -48,7 +50,6 @@ namespace PrivaPub.Tests.Infrastructure
|
|||||||
{
|
{
|
||||||
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||||
var token = TestContext.Current.CancellationToken;
|
var token = TestContext.Current.CancellationToken;
|
||||||
await Indexes.Create(token);
|
|
||||||
var local = new LocalActorService(new DbEntities(), new StaticOptions<AppConfiguration>(new AppConfiguration { BackendBaseAddress = "https://privapub.test" }));
|
var local = new LocalActorService(new DbEntities(), new StaticOptions<AppConfiguration>(new AppConfiguration { BackendBaseAddress = "https://privapub.test" }));
|
||||||
var name = $"n{Guid.NewGuid():N}"[..20];
|
var name = $"n{Guid.NewGuid():N}"[..20];
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,145 @@
|
|||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Infrastructure.Jobs;
|
||||||
|
using PrivaPub.Models;
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
using PrivaPub.Tests.Support;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Infrastructure
|
||||||
|
{
|
||||||
|
public class BackoffTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Grows_like_mastodons()
|
||||||
|
{
|
||||||
|
Assert.InRange(Backoff.After(1).TotalSeconds, 16, 16 + 9 * 2);
|
||||||
|
Assert.InRange(Backoff.After(10).TotalSeconds, 10015, 10015 + 9 * 11);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Quarantines_a_host_only_past_the_threshold()
|
||||||
|
{
|
||||||
|
Assert.Equal(TimeSpan.Zero, Backoff.HostQuarantine(9, 10));
|
||||||
|
Assert.Equal(TimeSpan.FromHours(1), Backoff.HostQuarantine(10, 10));
|
||||||
|
Assert.Equal(TimeSpan.FromHours(4), Backoff.HostQuarantine(12, 10));
|
||||||
|
Assert.Equal(TimeSpan.FromDays(7), Backoff.HostQuarantine(40, 10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Trait("Category", "Integration")]
|
||||||
|
public sealed class JobQueueTests : IAsyncLifetime
|
||||||
|
{
|
||||||
|
Peer _peer;
|
||||||
|
|
||||||
|
public async ValueTask InitializeAsync()
|
||||||
|
{
|
||||||
|
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||||
|
_peer = await Peer.Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (_peer != default)
|
||||||
|
await _peer.DisposeAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
static Job NewJob(string host = default, string dedupe = default) => new()
|
||||||
|
{
|
||||||
|
Kind = (JobKind)99,
|
||||||
|
Host = host,
|
||||||
|
DedupeKey = dedupe,
|
||||||
|
Payload = "{}"
|
||||||
|
};
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_dedupe_key_is_queued_once()
|
||||||
|
{
|
||||||
|
var queue = new JobQueue();
|
||||||
|
var key = Guid.NewGuid().ToString("N");
|
||||||
|
|
||||||
|
var inserted = await queue.EnqueueMany(new[] { NewJob(dedupe: key), NewJob(dedupe: key) }, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
Assert.Equal(1, inserted);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_busy_host_is_skipped_and_a_failed_job_backs_off()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var queue = new JobQueue();
|
||||||
|
var kind = (JobKind)(100 + Random.Shared.Next(1000));
|
||||||
|
var busy = $"busy{Guid.NewGuid():N}.example";
|
||||||
|
var free = $"free{Guid.NewGuid():N}.example";
|
||||||
|
await queue.EnqueueMany(new[] { new Job { Kind = kind, Host = busy }, new Job { Kind = kind, Host = free } }, token);
|
||||||
|
|
||||||
|
var leased = await queue.Lease(kind, new[] { busy }, token);
|
||||||
|
Assert.Equal(free, leased.Host);
|
||||||
|
Assert.Equal(1, leased.Attempts);
|
||||||
|
|
||||||
|
await queue.Finish(leased, JobOutcome.Retry("boom"), maxAttempts: 3, token);
|
||||||
|
var after = await DB.Default.Find<Job>().OneAsync(leased.ID, token);
|
||||||
|
Assert.Equal(JobState.Pending, after.State);
|
||||||
|
Assert.True(after.RunAt > DateTime.UtcNow.AddSeconds(10));
|
||||||
|
|
||||||
|
leased.Attempts = 3;
|
||||||
|
await queue.Finish(leased, JobOutcome.Retry("boom"), maxAttempts: 3, token);
|
||||||
|
Assert.Equal(JobState.Dead, (await DB.Default.Find<Job>().OneAsync(leased.ID, token)).State);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task An_expired_lease_returns_to_the_queue()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var job = new Job { Kind = (JobKind)98, State = JobState.Running, LeasedUntil = DateTime.UtcNow.AddMinutes(-1) };
|
||||||
|
await DB.Default.SaveAsync(job, token);
|
||||||
|
|
||||||
|
await new JobQueue().Reap(token);
|
||||||
|
|
||||||
|
Assert.Equal(JobState.Pending, (await DB.Default.Find<Job>().OneAsync(job.ID, token)).State);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task A_dead_host_does_not_hold_up_deliveries_to_live_ones()
|
||||||
|
{
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var (privateKey, publicKey) = Keys.NewKeyPair();
|
||||||
|
var avatar = new Avatar { UserName = $"sender{Guid.NewGuid():N}"[..20], PrivateKey = privateKey, PublicKey = publicKey };
|
||||||
|
await DB.Default.SaveAsync(avatar, token);
|
||||||
|
var local = new LocalActorService(new DbEntities(), new StaticOptions<AppConfiguration>(new AppConfiguration { BackendBaseAddress = "https://privapub.test" }));
|
||||||
|
var sender = local.FromAvatar(avatar);
|
||||||
|
var queue = new JobQueue();
|
||||||
|
var delivery = new DeliveryService(new DbEntities(), queue);
|
||||||
|
_peer.Answer("/dead/inbox", 503, TimeSpan.FromMilliseconds(300));
|
||||||
|
_peer.Answer("/live/inbox", 202);
|
||||||
|
|
||||||
|
var deadInboxes = Enumerable.Range(0, 60).Select(i => $"{_peer.B}/dead/inbox?{i}");
|
||||||
|
await delivery.Enqueue(sender, deadInboxes, new JsonObject { ["id"] = $"https://privapub.test/a/{Guid.NewGuid():N}", ["type"] = "Create" }, token);
|
||||||
|
await delivery.Enqueue(sender, new[] { $"{_peer.A}/live/inbox" }, new JsonObject { ["id"] = $"https://privapub.test/a/{Guid.NewGuid():N}", ["type"] = "Create" }, token);
|
||||||
|
|
||||||
|
var handler = new DeliveryJobHandler(local, Peer.Http(), new HostCircuitBreaker(new MemoryCache(new MemoryCacheOptions())), NullLogger<DeliveryJobHandler>.Instance);
|
||||||
|
using var worker = new JobWorker(queue, new IJobHandler[] { handler }, NullLogger<JobWorker>.Instance);
|
||||||
|
await worker.StartAsync(token);
|
||||||
|
var deadline = DateTime.UtcNow.AddSeconds(5);
|
||||||
|
while (DateTime.UtcNow < deadline && !_peer.Requests.Any(r => r.Path == "/live/inbox"))
|
||||||
|
await Task.Delay(50, token);
|
||||||
|
var live = Assert.Single(_peer.Requests, r => r.Path == "/live/inbox");
|
||||||
|
Assert.True(_peer.Requests.Count(r => r.Path == "/dead/inbox") < 30);
|
||||||
|
while (DateTime.UtcNow < deadline && !await DB.Default.Find<RemoteInstance>().Match(i => i.Host == "localhost").ExecuteAnyAsync(token))
|
||||||
|
await Task.Delay(50, token);
|
||||||
|
await worker.StopAsync(token);
|
||||||
|
|
||||||
|
Assert.Contains($"keyId=\"{sender.KeyId}\"", live.Signature);
|
||||||
|
var instance = await DB.Default.Find<RemoteInstance>().Match(i => i.Host == "localhost").ExecuteFirstAsync(token);
|
||||||
|
Assert.True(instance.ConsecutiveFailures > 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,5 +29,21 @@ namespace PrivaPub.Tests.Infrastructure
|
|||||||
Assert.Equal("<p><strong>bold</strong> <b>raw</b></p>", migratedLocal.ContentHtml);
|
Assert.Equal("<p><strong>bold</strong> <b>raw</b></p>", migratedLocal.ContentHtml);
|
||||||
Assert.Equal(ContentFormat.Markdown, migratedLocal.ContentFormat);
|
Assert.Equal(ContentFormat.Markdown, migratedLocal.ContentFormat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Direct_posts_move_into_posts_with_their_conversation()
|
||||||
|
{
|
||||||
|
Assert.SkipUnless(MongoFixture.Enabled, MongoFixture.Skip);
|
||||||
|
var token = TestContext.Current.CancellationToken;
|
||||||
|
var dm = new DmPost { GroupId = "conversation1", Text = "psst", ObjectURI = $"https://privapub.test/{Guid.NewGuid():N}", GroupUserId = "a1" };
|
||||||
|
await DB.Default.SaveAsync(dm, token);
|
||||||
|
|
||||||
|
await new _005_direct_posts_join_posts().UpgradeAsync();
|
||||||
|
|
||||||
|
var post = await DB.Default.Find<Post>().OneAsync(dm.ID, token);
|
||||||
|
Assert.Equal(PostVisibility.Direct, post.Visibility);
|
||||||
|
Assert.Equal("conversation1", post.ConversationId);
|
||||||
|
Assert.Equal("a1", post.AuthorAccountId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
using MongoDB.Bson;
|
||||||
|
|
||||||
|
using PrivaPub.Infrastructure.Ids;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
|
||||||
|
using GroupEntity = PrivaPub.Models.Group.Group;
|
||||||
|
|
||||||
|
namespace PrivaPub.Tests.Infrastructure
|
||||||
|
{
|
||||||
|
public class PrivacyIdsTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Persona_ids_carry_only_the_day_and_share_nothing_else()
|
||||||
|
{
|
||||||
|
var first = (string)new Avatar().GenerateNewID();
|
||||||
|
var second = (string)new Avatar().GenerateNewID();
|
||||||
|
|
||||||
|
Assert.True(ObjectId.TryParse(first, out var a));
|
||||||
|
Assert.True(ObjectId.TryParse(second, out var b));
|
||||||
|
Assert.Equal(DateTime.UtcNow.Date, a.CreationTime);
|
||||||
|
Assert.Equal(a.CreationTime, b.CreationTime);
|
||||||
|
Assert.NotEqual(first[8..14], second[8..14]);
|
||||||
|
Assert.Equal(DateTime.UtcNow.Date, ObjectId.Parse((string)new GroupEntity().GenerateNewID()).CreationTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_remote_post_id_sorts_by_its_published_time()
|
||||||
|
{
|
||||||
|
var older = PrivacyIds.At(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc));
|
||||||
|
var newer = PrivacyIds.At(new DateTime(2025, 1, 2, 0, 0, 0, DateTimeKind.Utc));
|
||||||
|
|
||||||
|
Assert.True(string.CompareOrdinal(older, newer) < 0);
|
||||||
|
Assert.Equal(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc), ObjectId.Parse(older).CreationTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Microsoft.Extensions.Localization;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Content;
|
||||||
|
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.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);
|
||||||
|
RemotePosts = new RemotePosts(Db, Local, Remote, new NoBlocks(), Queue);
|
||||||
|
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),
|
||||||
|
new RejectHandler(Db, Local),
|
||||||
|
new UndoHandler(Db, Local),
|
||||||
|
new LikeHandler(Db),
|
||||||
|
new AnnounceHandler(Db, Local, RemotePosts, Fanout),
|
||||||
|
new CreateHandler(Db, Local, Remote, Delivery, new NoBlocks(), Fanout, RemotePosts),
|
||||||
|
new DeleteHandler(Db, Local, Remote, Delivery),
|
||||||
|
new UpdateHandler(Db, Local, Remote)
|
||||||
|
}, NullLogger<InboxProcessor>.Instance);
|
||||||
|
Follows = new FollowService(Db, Local, Remote, Delivery, new KeyLocalizer<GenericRes>(), NullLogger<FollowService>.Instance);
|
||||||
|
Content = new ContentRenderer(Local, Remote);
|
||||||
|
Outbox = new OutboxPublisher(Db, Local, Delivery);
|
||||||
|
Statuses = new StatusService(Db, Local, Remote, Delivery, Content, Outbox, Fanout);
|
||||||
|
Posts = new PostsService(Db, Local, Statuses, new KeyLocalizer<GenericRes>(), NullLogger<PostsService>.Instance);
|
||||||
|
Timelines = new TimelineService(Db, new KeyLocalizer<GenericRes>());
|
||||||
|
}
|
||||||
|
|
||||||
|
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 Fanout Fanout { get; }
|
||||||
|
public RemotePosts RemotePosts { get; }
|
||||||
|
public TimelineService Timelines { 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>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,6 +32,7 @@ namespace PrivaPub.Tests.Support
|
|||||||
var connection = Environment.GetEnvironmentVariable("PRIVAPUB_TEST_MONGO") ?? "mongodb://127.0.0.1:27017";
|
var connection = Environment.GetEnvironmentVariable("PRIVAPUB_TEST_MONGO") ?? "mongodb://127.0.0.1:27017";
|
||||||
await DB.InitAsync(Database, MongoClientSettings.FromConnectionString(connection));
|
await DB.InitAsync(Database, MongoClientSettings.FromConnectionString(connection));
|
||||||
EntityMaps.Warm();
|
EntityMaps.Warm();
|
||||||
|
await Indexes.Create();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask DisposeAsync()
|
public async ValueTask DisposeAsync()
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Moderation;
|
||||||
using PrivaPub.Infrastructure.Http;
|
using PrivaPub.Infrastructure.Http;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
@@ -16,6 +18,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();
|
||||||
|
|
||||||
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}";
|
||||||
@@ -38,6 +41,13 @@ 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._answers.TryGetValue(key, out var answer))
|
||||||
|
{
|
||||||
|
if (answer.Delay > TimeSpan.Zero)
|
||||||
|
await Task.Delay(answer.Delay);
|
||||||
|
context.Response.StatusCode = answer.Status;
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!peer._documents.TryGetValue(key, out var document))
|
if (!peer._documents.TryGetValue(key, out var document))
|
||||||
{
|
{
|
||||||
context.Response.StatusCode = StatusCodes.Status404NotFound;
|
context.Response.StatusCode = StatusCodes.Status404NotFound;
|
||||||
@@ -53,7 +63,9 @@ 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 static FederationHttp Http(IMemoryCache cache = default)
|
public void Answer(string path, int status, TimeSpan delay = default) => _answers[path] = (status, delay);
|
||||||
|
|
||||||
|
public static FederationHttp Http(IMemoryCache cache = default, IDomainBlocks blocks = default)
|
||||||
{
|
{
|
||||||
var options = new FederationOptions { AllowPrivateNetworks = true, AllowPlainHttp = true };
|
var options = new FederationOptions { AllowPrivateNetworks = true, AllowPlainHttp = true };
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
@@ -61,12 +73,19 @@ namespace PrivaPub.Tests.Support
|
|||||||
.ConfigurePrimaryHttpMessageHandler(() => SafeHttpHandlerFactory.Create(options));
|
.ConfigurePrimaryHttpMessageHandler(() => SafeHttpHandlerFactory.Create(options));
|
||||||
return new FederationHttp(services.BuildServiceProvider().GetRequiredService<IHttpClientFactory>(),
|
return new FederationHttp(services.BuildServiceProvider().GetRequiredService<IHttpClientFactory>(),
|
||||||
cache ?? new MemoryCache(new MemoryCacheOptions()), new StaticOptions<FederationOptions>(options),
|
cache ?? new MemoryCache(new MemoryCacheOptions()), new StaticOptions<FederationOptions>(options),
|
||||||
NullLogger<FederationHttp>.Instance);
|
blocks ?? new NoBlocks(), NullLogger<FederationHttp>.Instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask DisposeAsync() => await _app.DisposeAsync();
|
public async ValueTask DisposeAsync() => await _app.DisposeAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public sealed class NoBlocks : IDomainBlocks
|
||||||
|
{
|
||||||
|
public DomainBlock Find(string host) => default;
|
||||||
|
public bool IsSuspended(string host) => false;
|
||||||
|
public Task Reload(CancellationToken token) => Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
public sealed record HttpRequestRecord(string Method, string Path, string Signature);
|
public sealed record HttpRequestRecord(string Method, string Path, string Signature);
|
||||||
|
|
||||||
public sealed class StaticOptions<T> : IOptionsMonitor<T>
|
public sealed class StaticOptions<T> : IOptionsMonitor<T>
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ namespace PrivaPub.Tests.Support
|
|||||||
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()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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,294 @@
|
|||||||
|
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.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;
|
||||||
|
|
||||||
|
public AccountsController(MastodonMapper mapper, DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors,
|
||||||
|
IFollowService follows, IOutboxPublisher outbox)
|
||||||
|
{
|
||||||
|
_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")]
|
||||||
|
public async Task<IActionResult> UpdateCredentials(CancellationToken token)
|
||||||
|
{
|
||||||
|
var avatar = await _dbEntities.Avatars.MatchID(Me.Id).ExecuteFirstAsync(token);
|
||||||
|
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, 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)
|
||||||
|
return Json(Array.Empty<Status>());
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
[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 } 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);
|
||||||
|
return new Relationship
|
||||||
|
{
|
||||||
|
Id = id,
|
||||||
|
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,107 @@
|
|||||||
|
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,
|
||||||
|
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,149 @@
|
|||||||
|
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 })
|
||||||
|
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/blocks"), Scope("read:blocks")]
|
||||||
|
public IActionResult Blocks() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/mutes"), Scope("read:mutes")]
|
||||||
|
public IActionResult Mutes() => Json(Array.Empty<object>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/domain_blocks"), Scope("read:blocks")]
|
||||||
|
public IActionResult DomainBlocks() => Json(Array.Empty<string>());
|
||||||
|
|
||||||
|
[HttpGet("/api/v1/bookmarks"), Scope("read:bookmarks")]
|
||||||
|
public IActionResult Bookmarks() => 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,276 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
if (Params.List("media_ids").Count > 0)
|
||||||
|
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: Media attachments are not supported yet");
|
||||||
|
if (Params.Has("poll[options]"))
|
||||||
|
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: Polls are not supported yet");
|
||||||
|
|
||||||
|
var outcome = await _statuses.Publish(Me, new StatusDraft
|
||||||
|
{
|
||||||
|
Text = Params.Get("status"),
|
||||||
|
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
|
||||||
|
}, 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)
|
||||||
|
{
|
||||||
|
if (Params.List("media_ids").Count > 0)
|
||||||
|
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: Media attachments are not supported yet");
|
||||||
|
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")
|
||||||
|
}, 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());
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses/{id}/bookmark"), Scope("write:bookmarks")]
|
||||||
|
public Task<IActionResult> Bookmark(string id, CancellationToken token) => Unchanged(id, token);
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses/{id}/unbookmark"), Scope("write:bookmarks")]
|
||||||
|
public Task<IActionResult> Unbookmark(string id, CancellationToken token) => Unchanged(id, 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 IActionResult Pin(string id) => Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: Pinning is not supported yet");
|
||||||
|
|
||||||
|
[HttpPost("/api/v1/statuses/{id}/unpin"), Scope("write:accounts")]
|
||||||
|
public Task<IActionResult> Unpin(string id, CancellationToken token) => 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,243 @@
|
|||||||
|
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"
|
||||||
|
};
|
||||||
|
|
||||||
|
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]
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
static NotificationType? Parse(string name) => Names.FirstOrDefault(n => n.Value == name) is { Value: not null } pair ? pair.Key : (NotificationType?)null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,200 @@
|
|||||||
|
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<object> Emojis { get; set; } = new();
|
||||||
|
public List<object> Roles { get; set; } = new();
|
||||||
|
public List<Field> Fields { get; set; } = new();
|
||||||
|
public Source Source { 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 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<object> 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 object Card { get; set; }
|
||||||
|
public object 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 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 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<object> 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,256 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Api.Mastodon.Entities;
|
||||||
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Rendering;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public MastodonMapper(DbEntities dbEntities, ILocalActorService localActors)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
Discoverable = foreign.IsDiscoverable,
|
||||||
|
CreatedAt = MastodonJson.Day(foreign.CreatedAt),
|
||||||
|
Note = foreign.Biography ?? string.Empty,
|
||||||
|
Url = foreign.Url ?? foreign.ActorURI,
|
||||||
|
Uri = foreign.ActorURI,
|
||||||
|
Avatar = foreign.PictureURL ?? MissingAvatar,
|
||||||
|
AvatarStatic = foreign.PictureURL ?? MissingAvatar,
|
||||||
|
Header = foreign.ThumbnailURL ?? MissingHeader,
|
||||||
|
HeaderStatic = 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).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 async Task<List<Status>> Statuses(IReadOnlyCollection<PostEntity> posts, string viewerId, 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 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),
|
||||||
|
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(),
|
||||||
|
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 mapped = new List<Status>();
|
||||||
|
foreach (var post in posts)
|
||||||
|
{
|
||||||
|
var status = Map(post);
|
||||||
|
if (status == default)
|
||||||
|
continue;
|
||||||
|
if (post.ReblogOfPostId != default)
|
||||||
|
{
|
||||||
|
if (!originals.TryGetValue(post.ReblogOfPostId, out var original) || Map(original) is not { } inner)
|
||||||
|
continue;
|
||||||
|
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;
|
||||||
|
|
||||||
|
public static string Content(PostEntity post)
|
||||||
|
{
|
||||||
|
var content = post.ContentHtml ?? ActivityPubRenderer.Html(post.Text);
|
||||||
|
return string.IsNullOrEmpty(post.Title) || post.IsFederatedCopy
|
||||||
|
? content ?? string.Empty
|
||||||
|
: $"<p><strong>{WebUtility.HtmlEncode(post.Title)}</strong></p>{content}";
|
||||||
|
}
|
||||||
|
|
||||||
|
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 MediaAttachment Media(PostMedia media) => new()
|
||||||
|
{
|
||||||
|
Id = 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 ?? media.RemoteURL,
|
||||||
|
PreviewUrl = media.URL ?? media.RemoteURL,
|
||||||
|
RemoteUrl = media.RemoteURL,
|
||||||
|
Description = media.Description,
|
||||||
|
Blurhash = media.Blurhash,
|
||||||
|
Meta = media.Width.HasValue && media.Height.HasValue
|
||||||
|
? new { original = new { width = media.Width, height = media.Height, aspect = (double)media.Width / media.Height.Value } }
|
||||||
|
: default
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Localization;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.ClientModels;
|
||||||
|
using PrivaPub.ClientModels.Admin;
|
||||||
|
using PrivaPub.Federation.Moderation;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Resources;
|
||||||
|
|
||||||
|
namespace PrivaPub.Controllers.ClientToServer
|
||||||
|
{
|
||||||
|
[ApiController,
|
||||||
|
Route("clientapi/admin/domainblocks"),
|
||||||
|
Authorize(Policy = Policies.IsAdmin)]
|
||||||
|
public class DomainBlockController : ControllerBase
|
||||||
|
{
|
||||||
|
readonly IDomainBlocks _domainBlocks;
|
||||||
|
readonly IStringLocalizer _localizer;
|
||||||
|
|
||||||
|
public DomainBlockController(IDomainBlocks domainBlocks, IStringLocalizer<GenericRes> localizer)
|
||||||
|
{
|
||||||
|
_domainBlocks = domainBlocks;
|
||||||
|
_localizer = localizer;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet, Route("/clientapi/admin/domainblocks/list")]
|
||||||
|
public async Task<IActionResult> List(CancellationToken token) =>
|
||||||
|
Ok((await DB.Default.Find<DomainBlock>().Sort(b => b.Domain, Order.Ascending).ExecuteAsync(token)).Select(ToView).ToList());
|
||||||
|
|
||||||
|
[HttpPost, Route("/clientapi/admin/domainblocks/insert")]
|
||||||
|
public async Task<IActionResult> Insert(DomainBlockForm form, CancellationToken token)
|
||||||
|
{
|
||||||
|
var domain = DomainBlocks.Normalise(form.Domain);
|
||||||
|
if (!ModelState.IsValid || Uri.CheckHostName(domain) != UriHostNameType.Dns)
|
||||||
|
return BadRequest(new WebResult().Invalidate(_localizer["Invalid model."]));
|
||||||
|
|
||||||
|
var block = await DB.Default.UpdateAndGet<DomainBlock>()
|
||||||
|
.Match(b => b.Domain == domain)
|
||||||
|
.Modify(b => b.Domain, domain)
|
||||||
|
.Modify(b => b.Severity, form.Suspend ? DomainBlockSeverity.Suspend : DomainBlockSeverity.Silence)
|
||||||
|
.Modify(b => b.RejectMedia, form.RejectMedia)
|
||||||
|
.Modify(b => b.PublicComment, form.PublicComment)
|
||||||
|
.Modify(b => b.PrivateComment, form.PrivateComment)
|
||||||
|
.Modify(b => b.SetOnInsert(x => x.CreatedAt, DateTime.UtcNow))
|
||||||
|
.Option(o => o.IsUpsert = true)
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
await _domainBlocks.Reload(token);
|
||||||
|
return Ok(ToView(block));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost, Route("/clientapi/admin/domainblocks/delete")]
|
||||||
|
public async Task<IActionResult> Delete([FromQuery] string domain, CancellationToken token)
|
||||||
|
{
|
||||||
|
domain = DomainBlocks.Normalise(domain);
|
||||||
|
await DB.Default.DeleteAsync<DomainBlock>(b => b.Domain == domain);
|
||||||
|
await _domainBlocks.Reload(token);
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
static ViewDomainBlock ToView(DomainBlock block) => new()
|
||||||
|
{
|
||||||
|
Id = block.ID,
|
||||||
|
Domain = block.Domain,
|
||||||
|
Severity = block.Severity.ToString(),
|
||||||
|
RejectMedia = block.RejectMedia,
|
||||||
|
PublicComment = block.PublicComment,
|
||||||
|
PrivateComment = block.PrivateComment,
|
||||||
|
CreatedAt = block.CreatedAt
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,6 +32,10 @@ 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));
|
||||||
|
|
||||||
|
[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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
using AngleSharp.Dom;
|
||||||
|
using AngleSharp.Html.Parser;
|
||||||
|
|
||||||
|
using Markdig;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
|
||||||
|
using System.Net;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace PrivaPub.Domain.Content
|
||||||
|
{
|
||||||
|
public sealed record ResolvedMention(string ActorUri, string UserName, string Domain, string ProfileUrl, bool IsLocal, string AccountId, string Inbox)
|
||||||
|
{
|
||||||
|
public string Handle => $"{UserName}@{Domain}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed record RenderedContent(string Html, IReadOnlyList<ResolvedMention> Mentions, IReadOnlyList<string> Tags);
|
||||||
|
|
||||||
|
public interface IContentRenderer
|
||||||
|
{
|
||||||
|
Task<RenderedContent> Markdown(string text, CancellationToken token);
|
||||||
|
Task<RenderedContent> PlainText(string text, CancellationToken token);
|
||||||
|
string TagUrl(string tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class ContentRenderer : IContentRenderer
|
||||||
|
{
|
||||||
|
const int MaxMentions = 20;
|
||||||
|
static readonly MarkdownPipeline Pipeline = new MarkdownPipelineBuilder().DisableHtml().UseAutoLinks().UseEmphasisExtras().Build();
|
||||||
|
static readonly HtmlParser Parser = new();
|
||||||
|
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
|
||||||
|
public ContentRenderer(ILocalActorService localActors, IRemoteActorService remoteActors)
|
||||||
|
{
|
||||||
|
_localActors = localActors;
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string TagUrl(string tag) => $"{_localActors.BaseAddress}/tags/{Uri.EscapeDataString(tag)}";
|
||||||
|
|
||||||
|
public Task<RenderedContent> Markdown(string text, CancellationToken token) =>
|
||||||
|
Decorate(string.IsNullOrWhiteSpace(text) ? string.Empty : Markdig.Markdown.ToHtml(text, Pipeline), token);
|
||||||
|
|
||||||
|
public Task<RenderedContent> PlainText(string text, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(text))
|
||||||
|
return Decorate(string.Empty, token);
|
||||||
|
var paragraphs = Regex.Split(text.Replace("\r\n", "\n").Trim(), "\n{2,}")
|
||||||
|
.Select(p => "<p>" + Linkify(WebUtility.HtmlEncode(p)).Replace("\n", "<br>") + "</p>");
|
||||||
|
return Decorate(string.Concat(paragraphs), token);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<RenderedContent> Decorate(string html, CancellationToken token)
|
||||||
|
{
|
||||||
|
var document = Parser.ParseDocument($"<body>{html}</body>");
|
||||||
|
var textNodes = document.Body.Descendants<IText>()
|
||||||
|
.Where(node => !node.Ancestors<IElement>().Any(e => e.LocalName is "a" or "code" or "pre"))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
var handles = textNodes.SelectMany(node => MentionPattern().Matches(node.Data).Select(m => m.Value))
|
||||||
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||||
|
.Take(MaxMentions)
|
||||||
|
.ToList();
|
||||||
|
var mentions = new Dictionary<string, ResolvedMention>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
foreach (var handle in handles)
|
||||||
|
{
|
||||||
|
var resolved = await Resolve(handle, token);
|
||||||
|
if (resolved != default)
|
||||||
|
mentions[handle] = resolved;
|
||||||
|
}
|
||||||
|
|
||||||
|
var tags = new List<string>();
|
||||||
|
foreach (var node in textNodes)
|
||||||
|
{
|
||||||
|
var replaced = Replace(node.Data, mentions, tags);
|
||||||
|
if (replaced == default)
|
||||||
|
continue;
|
||||||
|
var fragment = Parser.ParseFragment(replaced, node.ParentElement);
|
||||||
|
foreach (var child in fragment.ToList())
|
||||||
|
node.Parent.InsertBefore(child, node);
|
||||||
|
node.Parent.RemoveChild(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new RenderedContent(
|
||||||
|
ContentSanitizer.Html(document.Body.InnerHtml),
|
||||||
|
mentions.Values.DistinctBy(m => m.ActorUri).ToList(),
|
||||||
|
tags.Distinct().ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
string Replace(string text, IReadOnlyDictionary<string, ResolvedMention> mentions, List<string> tags)
|
||||||
|
{
|
||||||
|
var changed = false;
|
||||||
|
var encoded = WebUtility.HtmlEncode(text);
|
||||||
|
encoded = MentionPattern().Replace(encoded, match =>
|
||||||
|
{
|
||||||
|
if (!mentions.TryGetValue(WebUtility.HtmlDecode(match.Value), out var mention))
|
||||||
|
return match.Value;
|
||||||
|
changed = true;
|
||||||
|
return $"<span class=\"h-card\" translate=\"no\"><a href=\"{WebUtility.HtmlEncode(mention.ProfileUrl)}\" class=\"u-url mention\">@<span>{WebUtility.HtmlEncode(mention.UserName)}</span></a></span>";
|
||||||
|
});
|
||||||
|
encoded = HashtagPattern().Replace(encoded, match =>
|
||||||
|
{
|
||||||
|
var tag = match.Groups["tag"].Value;
|
||||||
|
if (tag.All(char.IsDigit))
|
||||||
|
return match.Value;
|
||||||
|
changed = true;
|
||||||
|
tags.Add(tag.ToLowerInvariant());
|
||||||
|
return $"{match.Groups["lead"].Value}<a href=\"{WebUtility.HtmlEncode(TagUrl(tag.ToLowerInvariant()))}\" class=\"mention hashtag\" rel=\"tag\">#<span>{tag}</span></a>";
|
||||||
|
});
|
||||||
|
return changed ? encoded : default;
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<ResolvedMention> Resolve(string handle, CancellationToken token)
|
||||||
|
{
|
||||||
|
var parts = handle.TrimStart('@').Split('@');
|
||||||
|
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, Kind: not LocalActorKind.Application }
|
||||||
|
? new ResolvedMention(local.Uri, local.UserName, local.Domain, local.Uri, true, local.Id, local.Inbox)
|
||||||
|
: default;
|
||||||
|
}
|
||||||
|
|
||||||
|
var actorUri = await _remoteActors.ResolveHandle($"{parts[0]}@{parts[1]}", token);
|
||||||
|
var actor = actorUri == default ? default : await _remoteActors.GetActor(actorUri, refresh: false, token);
|
||||||
|
return actor == default
|
||||||
|
? default
|
||||||
|
: new ResolvedMention(actor.ActorURI, actor.UserName ?? parts[0], parts[1].ToLowerInvariant(), actor.Url ?? actor.ActorURI, false, actor.ID,
|
||||||
|
actor.InboxURL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static string Linkify(string encoded) =>
|
||||||
|
UrlPattern().Replace(encoded, match => $"<a href=\"{match.Value}\">{match.Value}</a>");
|
||||||
|
|
||||||
|
[GeneratedRegex(@"(?<![\w@/.])@[a-zA-Z0-9_]+(?:@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)+(?::\d+)?)?(?![\w@])")]
|
||||||
|
private static partial Regex MentionPattern();
|
||||||
|
|
||||||
|
[GeneratedRegex(@"(?<lead>^|[^\w&#/])#(?<tag>[\p{L}\p{N}_]{1,100})(?![\p{L}\p{N}_])")]
|
||||||
|
private static partial Regex HashtagPattern();
|
||||||
|
|
||||||
|
[GeneratedRegex(@"https?://[^\s<]+[^\s<.,;:!?)\]'""]")]
|
||||||
|
private static partial Regex UrlPattern();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Models.Group;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
using GroupEntity = PrivaPub.Models.Group.Group;
|
||||||
|
|
||||||
|
namespace PrivaPub.Domain.Privacy
|
||||||
|
{
|
||||||
|
public static class VisibilityPolicy
|
||||||
|
{
|
||||||
|
public static readonly Expression<Func<Post, bool>> IsPublic = p =>
|
||||||
|
!p.DeletedAt.HasValue && (p.Visibility == PostVisibility.Public || p.Visibility == PostVisibility.Unlisted);
|
||||||
|
|
||||||
|
static readonly Func<Post, bool> IsPublicCompiled = IsPublic.Compile();
|
||||||
|
|
||||||
|
public static async Task<bool> CanSee(Post post, string viewerAvatarId, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (post == default || post.DeletedAt.HasValue)
|
||||||
|
return false;
|
||||||
|
if (IsPublicCompiled(post))
|
||||||
|
return true;
|
||||||
|
if (string.IsNullOrEmpty(viewerAvatarId))
|
||||||
|
return false;
|
||||||
|
if (post.GroupUserId == viewerAvatarId || post.Mentions.Any(m => m.IsLocal && m.AccountId == viewerAvatarId))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return post.Visibility switch
|
||||||
|
{
|
||||||
|
PostVisibility.Direct => !string.IsNullOrEmpty(post.ConversationId) && await DB.Default.Find<DmGroup>()
|
||||||
|
.Match(g => g.ID == post.ConversationId && g.Members.Any(m => !m.IsForeign && m.AvatarId == viewerAvatarId))
|
||||||
|
.ExecuteAnyAsync(token),
|
||||||
|
PostVisibility.Circle => !string.IsNullOrEmpty(post.GroupId) && await DB.Default.Find<GroupEntity>()
|
||||||
|
.Match(g => g.ID == post.GroupId && g.Members.Any(m => !m.IsForeign && m.AvatarId == viewerAvatarId))
|
||||||
|
.ExecuteAnyAsync(token),
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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.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,32 @@
|
|||||||
|
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)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(avatarId) || avatarId == fromAccountId)
|
||||||
|
return;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(new Notification
|
||||||
|
{
|
||||||
|
AvatarId = avatarId,
|
||||||
|
Type = type,
|
||||||
|
FromAccountId = fromAccountId,
|
||||||
|
FromActorURI = fromActorUri,
|
||||||
|
PostId = postId,
|
||||||
|
DedupeKey = $"{type}|{avatarId}|{fromActorUri}|{postId}"
|
||||||
|
}, token);
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,489 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Content;
|
||||||
|
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.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 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;
|
||||||
|
|
||||||
|
public StatusService(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery,
|
||||||
|
IContentRenderer content, IOutboxPublisher outbox, IFanout fanout)
|
||||||
|
{
|
||||||
|
_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)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(draft.Text))
|
||||||
|
return StatusOutcome.Fail(StatusCodes.Status422UnprocessableEntity, "Validation failed: Text can't be blank");
|
||||||
|
|
||||||
|
LocalActor group = default;
|
||||||
|
if (!string.IsNullOrEmpty(draft.GroupId))
|
||||||
|
{
|
||||||
|
var groupEntity = await _dbEntities.Groups.MatchID(draft.GroupId).ExecuteFirstAsync(token);
|
||||||
|
if (groupEntity == default || groupEntity.DeletionAt.HasValue || !groupEntity.Members.Any(m => !m.IsForeign && m.AvatarId == author.Id))
|
||||||
|
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");
|
||||||
|
|
||||||
|
var rendered = draft.PlainText ? await _content.PlainText(draft.Text, token) : await _content.Markdown(draft.Text, token);
|
||||||
|
var isLocalOnly = group is { IsFederated: false };
|
||||||
|
var visibility = isLocalOnly ? 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(),
|
||||||
|
AnsweringToPostId = parent?.ID,
|
||||||
|
InReplyToURI = parent?.ObjectURI ?? (IsRemoteUri(draft.InReplyTo) ? draft.InReplyTo : default),
|
||||||
|
InReplyToAccountId = parent?.AuthorAccountId ?? parent?.GroupUserId,
|
||||||
|
IsLocalOnly = isLocalOnly,
|
||||||
|
ActorURI = author.Uri
|
||||||
|
};
|
||||||
|
post.ID = (string)post.GenerateNewID();
|
||||||
|
post.ObjectURI = author.PostUri(post.ID);
|
||||||
|
post.Url = author.PostHtmlUrl(post.ID);
|
||||||
|
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
if (create != default)
|
||||||
|
await _outbox.Publish(author, post, create, token);
|
||||||
|
if (group is { IsFederated: true } && visibility is PostVisibility.Public or PostVisibility.Unlisted)
|
||||||
|
await _delivery.EnqueueToFollowers(group, ActivityPubRenderer.Announce(group, post.ObjectURI, $"announce-{post.ID}"), 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");
|
||||||
|
if (string.IsNullOrWhiteSpace(draft.Text))
|
||||||
|
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, token) : await _content.Markdown(draft.Text, 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);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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,93 @@
|
|||||||
|
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);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,6 +18,8 @@ namespace PrivaPub.Federation.Actors
|
|||||||
public string Url { get; init; }
|
public string Url { get; init; }
|
||||||
public string Inbox { get; init; }
|
public string Inbox { get; init; }
|
||||||
public string Outbox { get; init; }
|
public string Outbox { get; init; }
|
||||||
|
public string Followers { get; init; }
|
||||||
|
public string Following { get; init; }
|
||||||
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;
|
||||||
@@ -45,6 +47,8 @@ namespace PrivaPub.Federation.Actors
|
|||||||
Url = RemoteActorService.Text(root, "url") ?? id,
|
Url = RemoteActorService.Text(root, "url") ?? id,
|
||||||
Inbox = Origin.Same(inbox, id) ? inbox : default,
|
Inbox = Origin.Same(inbox, id) ? inbox : default,
|
||||||
Outbox = RemoteActorService.Text(root, "outbox"),
|
Outbox = RemoteActorService.Text(root, "outbox"),
|
||||||
|
Followers = RemoteActorService.Text(root, "followers"),
|
||||||
|
Following = RemoteActorService.Text(root, "following"),
|
||||||
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,
|
||||||
|
|||||||
@@ -29,20 +29,27 @@ 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 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 string Uri => $"{BaseAddress}/peasants/{UserName}";
|
public string Uri => $"{BaseAddress}/peasants/{UserName}";
|
||||||
public string KeyId => $"{Uri}#main-key";
|
public string KeyId => $"{Uri}#main-key";
|
||||||
public string Inbox => $"{Uri}/mouth";
|
public string Inbox => $"{Uri}/mouth";
|
||||||
public string Outbox => $"{Uri}/anus";
|
public string Outbox => $"{Uri}/anus";
|
||||||
public string Followers => $"{Uri}/followers";
|
public string Followers => $"{Uri}/groupies";
|
||||||
public string Following => $"{Uri}/following";
|
public string Following => $"{Uri}/stalking";
|
||||||
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}";
|
||||||
public string PostUri(string postId) => $"{Uri}/posts/{postId}";
|
public string HtmlUrl => $"{BaseAddress}/@{UserName}";
|
||||||
public string ActivityUri(string activityId) => $"{Uri}/activities/{activityId}";
|
public string PostUri(string postId) => $"{Uri}/scribbles/{postId}";
|
||||||
|
public string PostHtmlUrl(string postId) => $"{HtmlUrl}/{postId}";
|
||||||
|
public string ActivityUri(string activityId) => $"{Uri}/grunts/{activityId}";
|
||||||
|
public string ConversationUri(string conversationId) => $"{Uri}/whispers/{conversationId}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface ILocalActorService
|
public interface ILocalActorService
|
||||||
@@ -190,6 +197,12 @@ namespace PrivaPub.Federation.Actors
|
|||||||
PrivateKeyPem = avatar.PrivateKey,
|
PrivateKeyPem = avatar.PrivateKey,
|
||||||
PublicKeyPem = avatar.PublicKey,
|
PublicKeyPem = avatar.PublicKey,
|
||||||
Published = avatar.CreatedAt,
|
Published = avatar.CreatedAt,
|
||||||
|
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
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -163,6 +163,8 @@ namespace PrivaPub.Federation.Actors
|
|||||||
.Modify(a => a.Domain, new Uri(actor.Id).Authority)
|
.Modify(a => a.Domain, new Uri(actor.Id).Authority)
|
||||||
.Modify(a => a.InboxURL, actor.Inbox)
|
.Modify(a => a.InboxURL, actor.Inbox)
|
||||||
.Modify(a => a.OutboxURL, actor.Outbox)
|
.Modify(a => a.OutboxURL, actor.Outbox)
|
||||||
|
.Modify(a => a.FollowersURL, actor.Followers)
|
||||||
|
.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.IsDiscoverable, actor.Discoverable)
|
.Modify(a => a.IsDiscoverable, actor.Discoverable)
|
||||||
|
|||||||
@@ -4,11 +4,13 @@ using Microsoft.AspNetCore.RateLimiting;
|
|||||||
using MongoDB.Entities;
|
using MongoDB.Entities;
|
||||||
|
|
||||||
using PrivaPub.Models.Federation;
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
using PrivaPub.StaticServices;
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
using PostEntity = PrivaPub.Models.Post.Post;
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
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;
|
||||||
@@ -23,11 +25,11 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
const int OutboxSize = 20;
|
const int OutboxSize = 20;
|
||||||
|
|
||||||
readonly ILocalActorService _localActors;
|
readonly ILocalActorService _localActors;
|
||||||
readonly IInboxService _inbox;
|
readonly IInboxReceiver _inbox;
|
||||||
readonly DbEntities _dbEntities;
|
readonly DbEntities _dbEntities;
|
||||||
readonly ILogger<PeasantsController> _logger;
|
readonly ILogger<PeasantsController> _logger;
|
||||||
|
|
||||||
public PeasantsController(ILocalActorService localActors, IInboxService inbox, DbEntities dbEntities,
|
public PeasantsController(ILocalActorService localActors, IInboxReceiver inbox, DbEntities dbEntities,
|
||||||
ILogger<PeasantsController> logger)
|
ILogger<PeasantsController> logger)
|
||||||
{
|
{
|
||||||
_localActors = localActors;
|
_localActors = localActors;
|
||||||
@@ -40,26 +42,35 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
public async Task<IActionResult> GetActor(string actor, CancellationToken token)
|
public async Task<IActionResult> GetActor(string actor, CancellationToken token)
|
||||||
{
|
{
|
||||||
var local = await _localActors.FindByUserName(actor, token);
|
var local = await _localActors.FindByUserName(actor, token);
|
||||||
return local is not { IsFederated: true } ? NotFound() : Activity(ActivityPubRenderer.Actor(local));
|
if (local is not { IsFederated: true })
|
||||||
|
return NotFound();
|
||||||
|
if (WantsHtml() && local.Kind != LocalActorKind.Application)
|
||||||
|
return Redirect(local.HtmlUrl);
|
||||||
|
return Activity(ActivityPubRenderer.Actor(local));
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet, Route("{actor}/anus")]
|
[HttpGet, Route("{actor}/anus")]
|
||||||
public async Task<IActionResult> Outbox(string actor, CancellationToken token)
|
public async Task<IActionResult> Outbox(string actor, [FromQuery] bool page, [FromQuery(Name = "max_id")] string maxId,
|
||||||
|
CancellationToken token)
|
||||||
{
|
{
|
||||||
var local = await _localActors.FindByUserName(actor, token);
|
var local = await _localActors.FindByUserName(actor, token);
|
||||||
if (local is not { IsFederated: true })
|
if (local is not { IsFederated: true })
|
||||||
return NotFound();
|
return NotFound();
|
||||||
|
|
||||||
var total = local.Kind == LocalActorKind.Group
|
if (!page)
|
||||||
? await DB.Default.CountAsync<PostEntity>(p => p.GroupId == local.Id && !p.IsLocalOnly, token)
|
{
|
||||||
: await DB.Default.CountAsync<PostEntity>(p => p.GroupUserId == local.Id && !p.IsFederatedCopy && !p.IsLocalOnly, token);
|
var total = local.Kind == LocalActorKind.Group
|
||||||
|
? await DB.Default.CountAsync<PostEntity>(f => f.Where(p => p.GroupId == local.Id) & f.Where(VisibilityPolicy.IsPublic), token)
|
||||||
|
: await DB.Default.CountAsync<PostEntity>(f => f.Where(p => p.GroupUserId == local.Id && !p.IsFederatedCopy) & f.Where(VisibilityPolicy.IsPublic), token);
|
||||||
|
return Activity(ActivityPubRenderer.OrderedCollection(local.Outbox, (int)total, default, $"{local.Outbox}?page=true"));
|
||||||
|
}
|
||||||
|
|
||||||
var latest = await (local.Kind == LocalActorKind.Group
|
var query = local.Kind == LocalActorKind.Group
|
||||||
? _dbEntities.Posts.Match(p => p.GroupId == local.Id && !p.IsLocalOnly)
|
? _dbEntities.Posts.Match(p => p.GroupId == local.Id).Match(VisibilityPolicy.IsPublic)
|
||||||
: _dbEntities.Posts.Match(p => p.GroupUserId == local.Id && !p.IsFederatedCopy && !p.IsLocalOnly))
|
: _dbEntities.Posts.Match(p => p.GroupUserId == local.Id && !p.IsFederatedCopy).Match(VisibilityPolicy.IsPublic);
|
||||||
.Sort(p => p.CreationDate, Order.Descending)
|
if (!string.IsNullOrEmpty(maxId))
|
||||||
.Limit(OutboxSize)
|
query.Match(f => f.Lt(p => p.ID, maxId));
|
||||||
.ExecuteAsync(token);
|
var latest = await query.Sort(p => p.ID, Order.Descending).Limit(OutboxSize).ExecuteAsync(token);
|
||||||
|
|
||||||
var items = new List<JsonNode>();
|
var items = new List<JsonNode>();
|
||||||
foreach (var post in latest)
|
foreach (var post in latest)
|
||||||
@@ -69,15 +80,21 @@ 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;
|
||||||
}
|
}
|
||||||
var group = string.IsNullOrEmpty(post.GroupId) ? default : await _localActors.FindById(LocalActorKind.Group, post.GroupId, token);
|
if (post.ReblogOfPostId != default)
|
||||||
var note = ActivityPubRenderer.Note(post, local, group, default);
|
{
|
||||||
items.Add(ActivityPubRenderer.Create(local, note, $"create-{post.ID}"));
|
if (await AnnounceFor(post, token) is { } announce)
|
||||||
|
items.Add(announce);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
items.Add(await CreateFor(post, local, token));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Activity(ActivityPubRenderer.OrderedCollection(local.Outbox, (int)total, items));
|
var pageId = string.IsNullOrEmpty(maxId) ? $"{local.Outbox}?page=true" : $"{local.Outbox}?page=true&max_id={maxId}";
|
||||||
|
var next = latest.Count == OutboxSize ? $"{local.Outbox}?page=true&max_id={latest[^1].ID}" : default;
|
||||||
|
return Activity(ActivityPubRenderer.OrderedCollectionPage(pageId, local.Outbox, items, next, default));
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet, Route("{actor}/followers")]
|
[HttpGet, Route("{actor}/groupies")]
|
||||||
public async Task<IActionResult> Followers(string actor, CancellationToken token)
|
public async Task<IActionResult> Followers(string actor, CancellationToken token)
|
||||||
{
|
{
|
||||||
var local = await _localActors.FindByUserName(actor, token);
|
var local = await _localActors.FindByUserName(actor, token);
|
||||||
@@ -88,33 +105,45 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
return Activity(ActivityPubRenderer.OrderedCollection(local.Followers, (int)count, default));
|
return Activity(ActivityPubRenderer.OrderedCollection(local.Followers, (int)count, default));
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet, Route("{actor}/following")]
|
[HttpGet, Route("{actor}/stalking")]
|
||||||
public async Task<IActionResult> Following(string actor, CancellationToken token)
|
public async Task<IActionResult> Following(string actor, CancellationToken token)
|
||||||
{
|
{
|
||||||
var local = await _localActors.FindByUserName(actor, token);
|
var local = await _localActors.FindByUserName(actor, token);
|
||||||
return local is not { IsFederated: true } ? NotFound() : Activity(ActivityPubRenderer.OrderedCollection(local.Following, 0, Enumerable.Empty<JsonNode>()));
|
return local is not { IsFederated: true } ? NotFound() : Activity(ActivityPubRenderer.OrderedCollection(local.Following, 0, default));
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet, Route("{actor}/posts/{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 = await _localActors.FindByUserName(actor, token);
|
var (local, post) = await PublicPost(actor, postId, token);
|
||||||
if (local == default || local.Kind != LocalActorKind.Person)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
var post = await _dbEntities.Posts
|
|
||||||
.Match(p => p.ID == postId && p.GroupUserId == local.Id && !p.IsFederatedCopy && !p.IsLocalOnly)
|
|
||||||
.ExecuteFirstAsync(token);
|
|
||||||
if (post == default)
|
if (post == default)
|
||||||
return NotFound();
|
return await Tombstone(actor, postId, token) ?? NotFound();
|
||||||
|
if (WantsHtml())
|
||||||
|
return Redirect(local.PostHtmlUrl(post.ID));
|
||||||
|
|
||||||
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);
|
||||||
var replyTo = await ReplyTarget(post.AnsweringToPostId, token);
|
var note = ActivityPubRenderer.Note(post, local, group, post.InReplyToURI);
|
||||||
var note = ActivityPubRenderer.Note(post, local, group, replyTo);
|
note["@context"] = ActivityPubRenderer.Context();
|
||||||
note["@context"] = ActivityPubRenderer.ActivityStreams;
|
|
||||||
return Activity(note);
|
return Activity(note);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet, Route("{actor}/grunts/{activityId}")]
|
||||||
|
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))
|
||||||
|
return NotFound();
|
||||||
|
var (local, post) = await PublicPost(actor, activityId["create-".Length..], token);
|
||||||
|
return post == default ? NotFound() : Activity(await CreateFor(post, local, token));
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPost, Route("{actor}/mouth"), EnableRateLimiting(RateLimiting.Inbox)]
|
[HttpPost, Route("{actor}/mouth"), EnableRateLimiting(RateLimiting.Inbox)]
|
||||||
public async Task<IActionResult> Inbox(string actor, CancellationToken token)
|
public async Task<IActionResult> Inbox(string actor, CancellationToken token)
|
||||||
{
|
{
|
||||||
@@ -132,13 +161,71 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
public async Task<IActionResult> SharedInbox(CancellationToken token) =>
|
public async Task<IActionResult> SharedInbox(CancellationToken token) =>
|
||||||
Answer(await _inbox.Receive(Request, default, token));
|
Answer(await _inbox.Receive(Request, default, token));
|
||||||
|
|
||||||
async Task<string> ReplyTarget(string answeringToPostId, CancellationToken token)
|
async Task<(LocalActor Actor, PostEntity Post)> PublicPost(string actor, string postId, CancellationToken token)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(answeringToPostId))
|
var local = await _localActors.FindByUserName(actor, token);
|
||||||
|
if (local is not { IsFederated: true, Kind: LocalActorKind.Person })
|
||||||
return default;
|
return default;
|
||||||
if (answeringToPostId.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
|
var post = await _dbEntities.Posts
|
||||||
return answeringToPostId;
|
.Match(p => p.ID == postId && p.GroupUserId == local.Id && !p.IsFederatedCopy && p.ReblogOfPostId == null)
|
||||||
return (await _dbEntities.Posts.MatchID(answeringToPostId).ExecuteFirstAsync(token))?.ObjectURI;
|
.Match(VisibilityPolicy.IsPublic)
|
||||||
|
.ExecuteFirstAsync(token);
|
||||||
|
return (local, 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)
|
||||||
|
{
|
||||||
|
var group = string.IsNullOrEmpty(post.GroupId) ? default : await _localActors.FindById(LocalActorKind.Group, post.GroupId, token);
|
||||||
|
var note = ActivityPubRenderer.Note(post, author, group, post.InReplyToURI);
|
||||||
|
return ActivityPubRenderer.Create(author, note, $"create-{post.ID}");
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WantsHtml()
|
||||||
|
{
|
||||||
|
var accept = Request.Headers.Accept.ToString();
|
||||||
|
return accept.Contains("text/html", StringComparison.OrdinalIgnoreCase)
|
||||||
|
&& !accept.Contains("activity+json", StringComparison.OrdinalIgnoreCase)
|
||||||
|
&& !accept.Contains("ld+json", StringComparison.OrdinalIgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
IActionResult Answer(InboxResult result)
|
IActionResult Answer(InboxResult result)
|
||||||
|
|||||||
@@ -3,12 +3,14 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using MongoDB.Entities;
|
using MongoDB.Entities;
|
||||||
|
|
||||||
using PrivaPub.Models.Federation;
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
using PrivaPub.Models.User;
|
using PrivaPub.Models.User;
|
||||||
using PrivaPub.StaticServices;
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
using PostEntity = PrivaPub.Models.Post.Post;
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
using PrivaPub.Domain.Privacy;
|
||||||
using PrivaPub.Federation.Actors;
|
using PrivaPub.Federation.Actors;
|
||||||
|
|
||||||
namespace PrivaPub.Federation.Controllers
|
namespace PrivaPub.Federation.Controllers
|
||||||
@@ -49,10 +51,12 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
var document = new JsonObject
|
var document = new JsonObject
|
||||||
{
|
{
|
||||||
["subject"] = $"acct:{actor.Handle}",
|
["subject"] = $"acct:{actor.Handle}",
|
||||||
["aliases"] = new JsonArray(actor.Uri),
|
["aliases"] = actor.Kind == LocalActorKind.Application ? new JsonArray(actor.Uri) : new JsonArray(actor.HtmlUrl, actor.Uri),
|
||||||
["links"] = new JsonArray(
|
["links"] = actor.Kind == LocalActorKind.Application
|
||||||
new JsonObject { ["rel"] = "self", ["type"] = "application/activity+json", ["href"] = actor.Uri },
|
? new JsonArray(new JsonObject { ["rel"] = "self", ["type"] = "application/activity+json", ["href"] = actor.Uri })
|
||||||
new JsonObject { ["rel"] = "http://webfinger.net/rel/profile-page", ["type"] = "text/html", ["href"] = actor.Uri })
|
: new JsonArray(
|
||||||
|
new JsonObject { ["rel"] = "http://webfinger.net/rel/profile-page", ["type"] = "text/html", ["href"] = actor.HtmlUrl },
|
||||||
|
new JsonObject { ["rel"] = "self", ["type"] = "application/activity+json", ["href"] = actor.Uri })
|
||||||
};
|
};
|
||||||
return Content(document.ToJsonString(), "application/jrd+json; charset=utf-8");
|
return Content(document.ToJsonString(), "application/jrd+json; charset=utf-8");
|
||||||
}
|
}
|
||||||
@@ -62,24 +66,36 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
{
|
{
|
||||||
var document = new JsonObject
|
var document = new JsonObject
|
||||||
{
|
{
|
||||||
["links"] = new JsonArray(new JsonObject
|
["links"] = new JsonArray(
|
||||||
{
|
new JsonObject
|
||||||
["rel"] = "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
{
|
||||||
["href"] = $"{_localActors.BaseAddress}/nodeinfo/2.0"
|
["rel"] = "http://nodeinfo.diaspora.software/ns/schema/2.1",
|
||||||
})
|
["href"] = $"{_localActors.BaseAddress}/nodeinfo/2.1"
|
||||||
|
},
|
||||||
|
new JsonObject
|
||||||
|
{
|
||||||
|
["rel"] = "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
||||||
|
["href"] = $"{_localActors.BaseAddress}/nodeinfo/2.0"
|
||||||
|
})
|
||||||
};
|
};
|
||||||
return Content(document.ToJsonString(), "application/json; charset=utf-8");
|
return Content(document.ToJsonString(), "application/json; charset=utf-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet, Route("/nodeinfo/2.0")]
|
[HttpGet, Route("/nodeinfo/{version:regex(^2\\.[[01]]$)}")]
|
||||||
public async Task<IActionResult> NodeInfo(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>(p => !p.IsFederatedCopy, 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 };
|
||||||
|
if (version == "2.1")
|
||||||
|
{
|
||||||
|
software["repository"] = "https://git.thepra.dev/thepra/SocialPub";
|
||||||
|
software["homepage"] = "https://git.thepra.dev/thepra/SocialPub";
|
||||||
|
}
|
||||||
var document = new JsonObject
|
var document = new JsonObject
|
||||||
{
|
{
|
||||||
["version"] = "2.0",
|
["version"] = version,
|
||||||
["software"] = new JsonObject { ["name"] = "privapub", ["version"] = BuildInfo.Ref },
|
["software"] = software,
|
||||||
["protocols"] = new JsonArray("activitypub"),
|
["protocols"] = new JsonArray("activitypub"),
|
||||||
["services"] = new JsonObject { ["inbound"] = new JsonArray(), ["outbound"] = new JsonArray() },
|
["services"] = new JsonObject { ["inbound"] = new JsonArray(), ["outbound"] = new JsonArray() },
|
||||||
["openRegistrations"] = true,
|
["openRegistrations"] = true,
|
||||||
@@ -88,9 +104,15 @@ namespace PrivaPub.Federation.Controllers
|
|||||||
["users"] = new JsonObject { ["total"] = users },
|
["users"] = new JsonObject { ["total"] = users },
|
||||||
["localPosts"] = posts
|
["localPosts"] = posts
|
||||||
},
|
},
|
||||||
["metadata"] = new JsonObject()
|
["metadata"] = new JsonObject
|
||||||
|
{
|
||||||
|
["nodeName"] = "PrivaPub",
|
||||||
|
["nodeDescription"] = "A small ActivityPub server where one private login keeps several unlinkable public personas.",
|
||||||
|
["federation"] = new JsonObject { ["document"] = "https://git.thepra.dev/thepra/SocialPub/src/branch/master/FEDERATION.md" }
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return Content(document.ToJsonString(), "application/json; profile=\"http://nodeinfo.diaspora.software/ns/schema/2.0#\"; charset=utf-8");
|
return Content(document.ToJsonString(),
|
||||||
|
$"application/json; profile=\"http://nodeinfo.diaspora.software/ns/schema/{version}#\"; charset=utf-8");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Group;
|
||||||
|
|
||||||
|
using GroupEntity = PrivaPub.Models.Group.Group;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Inbox
|
||||||
|
{
|
||||||
|
public static class ForeignMembers
|
||||||
|
{
|
||||||
|
public static async Task<bool> IsAcceptedFollower(LocalActor group, string actorUri, CancellationToken token) =>
|
||||||
|
await DB.Default.Find<Follower>()
|
||||||
|
.Match(f => f.LocalActorId == group.Id && f.LocalActorKind == LocalActorKind.Group && f.ActorURI == actorUri && f.IsAccepted)
|
||||||
|
.ExecuteAnyAsync(token);
|
||||||
|
|
||||||
|
public static async Task AddForeignMember(string groupId, string actorUri, CancellationToken token)
|
||||||
|
{
|
||||||
|
var group = await DB.Default.Find<GroupEntity>().MatchID(groupId).ExecuteFirstAsync(token);
|
||||||
|
if (group == default || group.Members.Any(m => m.IsForeign && m.AvatarId == actorUri))
|
||||||
|
return;
|
||||||
|
group.Members.Add(new GroupMember { AvatarId = actorUri, IsForeign = true });
|
||||||
|
group.UpdatedAt = DateTime.UtcNow;
|
||||||
|
await DB.Default.SaveAsync(group, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task RemoveForeignMember(string groupId, string actorUri, CancellationToken token)
|
||||||
|
{
|
||||||
|
var group = await DB.Default.Find<GroupEntity>().MatchID(groupId).ExecuteFirstAsync(token);
|
||||||
|
if (group == default || group.Members.RemoveAll(m => m.IsForeign && m.AvatarId == actorUri) == 0)
|
||||||
|
return;
|
||||||
|
group.UpdatedAt = DateTime.UtcNow;
|
||||||
|
await DB.Default.SaveAsync<GroupEntity>(group, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public AcceptHandler(DbEntities dbEntities, ILocalActorService localActors)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual string Type => "Accept";
|
||||||
|
|
||||||
|
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
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) : base(dbEntities, localActors)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string Type => "Reject";
|
||||||
|
|
||||||
|
protected override Task Apply(Following following, CancellationToken token) => DB.Default.DeleteAsync<Following>(following.ID);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
public AnnounceHandler(DbEntities dbEntities, ILocalActorService localActors, IRemotePosts remotePosts, IFanout fanout)
|
||||||
|
{
|
||||||
|
_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")
|
||||||
|
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.At(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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>()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Timelines;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Moderation;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Infrastructure.Ids;
|
||||||
|
using PrivaPub.Federation.Rendering;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Group;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using static PrivaPub.Federation.Inbox.ForeignMembers;
|
||||||
|
using static PrivaPub.Federation.Objects.ActivityJson;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Inbox.Handlers
|
||||||
|
{
|
||||||
|
public class CreateHandler : IActivityHandler
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
readonly IDeliveryService _delivery;
|
||||||
|
readonly IDomainBlocks _domainBlocks;
|
||||||
|
readonly IFanout _fanout;
|
||||||
|
readonly IRemotePosts _remotePosts;
|
||||||
|
|
||||||
|
public CreateHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery,
|
||||||
|
IDomainBlocks domainBlocks, IFanout fanout, IRemotePosts remotePosts)
|
||||||
|
{
|
||||||
|
_fanout = fanout;
|
||||||
|
_remotePosts = remotePosts;
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
_delivery = delivery;
|
||||||
|
_domainBlocks = domainBlocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Type => "Create";
|
||||||
|
|
||||||
|
public async Task Handle(JsonNode activity, ForeignAvatar author, CancellationToken token)
|
||||||
|
{
|
||||||
|
var node = activity["object"];
|
||||||
|
if (node is not JsonObject || !Origin.Same(Id(node), author.ActorURI))
|
||||||
|
{
|
||||||
|
using var fetched = await _remoteActors.FetchObject(Id(node), token);
|
||||||
|
node = fetched == default ? default : JsonNode.Parse(fetched.Root.GetRawText());
|
||||||
|
}
|
||||||
|
var note = NoteParser.Parse(node);
|
||||||
|
if (note == default || note.AttributedTo != author.ActorURI)
|
||||||
|
return;
|
||||||
|
if (await _dbEntities.Posts.Match(p => p.ObjectURI == note.Id).ExecuteAnyAsync(token))
|
||||||
|
return;
|
||||||
|
|
||||||
|
var to = note.To.Concat(Strings(activity["to"])).Distinct(StringComparer.Ordinal).ToList();
|
||||||
|
var cc = note.Cc.Concat(Strings(activity["cc"])).Distinct(StringComparer.Ordinal).ToList();
|
||||||
|
var visibility = Addressing.Classify(to, cc, author.FollowersURL);
|
||||||
|
|
||||||
|
var addressed = to.Concat(cc)
|
||||||
|
.Concat(Addresses(activity))
|
||||||
|
.Concat(note.Mentions.Select(m => m.ActorURI))
|
||||||
|
.Append(note.Audience)
|
||||||
|
.Where(a => a != default && !Addressing.IsPublic(a) && a != author.FollowersURL)
|
||||||
|
.Distinct(StringComparer.Ordinal)
|
||||||
|
.ToList();
|
||||||
|
var localTargets = new List<LocalActor>();
|
||||||
|
foreach (var uri in addressed)
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUri(uri, token);
|
||||||
|
if (local is { IsFederated: true } && localTargets.All(l => l.Id != local.Id))
|
||||||
|
localTargets.Add(local);
|
||||||
|
}
|
||||||
|
var persons = localTargets.Where(t => t.Kind == LocalActorKind.Person).ToList();
|
||||||
|
|
||||||
|
var parent = string.IsNullOrEmpty(note.InReplyTo)
|
||||||
|
? default
|
||||||
|
: await _dbEntities.Posts.Match(p => p.ObjectURI == note.InReplyTo && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
var group = visibility is PostVisibility.Public or PostVisibility.Unlisted
|
||||||
|
? localTargets.FirstOrDefault(t => t.Kind == LocalActorKind.Group)
|
||||||
|
: default;
|
||||||
|
if (group != default && !await IsAcceptedFollower(group, author.ActorURI, token))
|
||||||
|
group = default;
|
||||||
|
|
||||||
|
var repliesToLocal = parent is { IsFederatedCopy: false } && visibility != PostVisibility.Direct;
|
||||||
|
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;
|
||||||
|
if (parent == default && visibility != PostVisibility.Direct)
|
||||||
|
parent = await _remotePosts.Parent(note.InReplyTo, 1, token);
|
||||||
|
|
||||||
|
DmGroup conversation = default;
|
||||||
|
if (visibility == PostVisibility.Direct)
|
||||||
|
{
|
||||||
|
var participants = persons.Select(p => p.Uri)
|
||||||
|
.Concat(addressed.Where(a => !IsCollection(a)))
|
||||||
|
.Append(author.ActorURI)
|
||||||
|
.ToList();
|
||||||
|
conversation = await FindOrCreateConversation(participants, Origin.Same(note.Context, author.ActorURI) ? note.Context : default, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
var post = await _remotePosts.Build(note, author, visibility, to, cc, parent, token);
|
||||||
|
post.ActivityURI = Id(activity);
|
||||||
|
post.GroupId = group?.Id;
|
||||||
|
post.ConversationId = conversation?.ID;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(post, token);
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
if (conversation != default)
|
||||||
|
await DB.Default.Update<DmGroup>().MatchID(conversation.ID).Modify(g => g.UpdatedAt, DateTime.UtcNow).ExecuteAsync(token);
|
||||||
|
if (group != default)
|
||||||
|
{
|
||||||
|
var announce = ActivityPubRenderer.Announce(group, note.Id, $"announce-{post.ID}");
|
||||||
|
await _delivery.EnqueueToFollowers(group, announce, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<DmGroup> FindOrCreateConversation(List<string> participantUris, string context, CancellationToken token)
|
||||||
|
{
|
||||||
|
var members = new List<GroupMember>();
|
||||||
|
foreach (var uri in participantUris.Distinct(StringComparer.Ordinal))
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUri(uri, token);
|
||||||
|
var member = local != default && local.Kind == LocalActorKind.Person
|
||||||
|
? new GroupMember { AvatarId = local.Id }
|
||||||
|
: new GroupMember { AvatarId = uri, IsForeign = true };
|
||||||
|
if (members.All(m => m.AvatarId != member.AvatarId || m.IsForeign != member.IsForeign))
|
||||||
|
members.Add(member);
|
||||||
|
}
|
||||||
|
|
||||||
|
var key = DmGroup.KeyOf(members);
|
||||||
|
var match = await _dbEntities.DmGroups.Match(g => g.ParticipantsKey == key && !g.DeletionAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
if (match != default)
|
||||||
|
return match;
|
||||||
|
|
||||||
|
var conversation = new DmGroup { Members = members, ConversationURI = context, ParticipantsKey = key };
|
||||||
|
await DB.Default.SaveAsync(conversation, token);
|
||||||
|
return conversation;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool IsCollection(string uri) =>
|
||||||
|
uri.EndsWith("/followers", StringComparison.Ordinal) || uri.EndsWith("/following", StringComparison.Ordinal);
|
||||||
|
|
||||||
|
static IEnumerable<string> Strings(JsonNode node) => node switch
|
||||||
|
{
|
||||||
|
JsonArray array => array.Select(Id).Where(id => id != default),
|
||||||
|
JsonNode single when Id(single) is { } id => new[] { id },
|
||||||
|
_ => Enumerable.Empty<string>()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Federation.Rendering;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Group;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using static PrivaPub.Federation.Inbox.ForeignMembers;
|
||||||
|
using static PrivaPub.Federation.Objects.ActivityJson;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Inbox.Handlers
|
||||||
|
{
|
||||||
|
public class DeleteHandler : IActivityHandler
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
readonly IDeliveryService _delivery;
|
||||||
|
|
||||||
|
public DeleteHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
_delivery = delivery;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Type => "Delete";
|
||||||
|
|
||||||
|
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var delete = activity;
|
||||||
|
|
||||||
|
var objectUri = Id(delete["object"]);
|
||||||
|
if (!Origin.Same(objectUri, actor.ActorURI))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (objectUri == actor.ActorURI)
|
||||||
|
{
|
||||||
|
actor.DeletionAt = DateTime.UtcNow;
|
||||||
|
actor.AccountState = AvatarAccountState.Deleted;
|
||||||
|
await DB.Default.SaveAsync(actor, token);
|
||||||
|
var followers = await _dbEntities.Followers.Match(f => f.ActorURI == actor.ActorURI).ExecuteAsync(token);
|
||||||
|
foreach (var follower in followers)
|
||||||
|
{
|
||||||
|
await DB.Default.DeleteAsync<Follower>(follower.ID);
|
||||||
|
if (follower.LocalActorKind == LocalActorKind.Group)
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
||||||
|
if (post == default)
|
||||||
|
return;
|
||||||
|
await DB.Default.DeleteAsync<PostEntity>(post.ID);
|
||||||
|
await DB.Default.DeleteAsync<TimelineEntry>(e => e.PostId == post.ID || e.ReblogOfPostId == post.ID);
|
||||||
|
await DB.Default.DeleteAsync<PostEntity>(p => p.ReblogOfPostId == post.ID);
|
||||||
|
if (!string.IsNullOrEmpty(post.AnsweringToPostId))
|
||||||
|
await DB.Default.Update<PostEntity>().MatchID(post.AnsweringToPostId).Modify(b => b.Inc(p => p.RepliesCount, -1)).ExecuteAsync(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Social;
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Federation.Rendering;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Group;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using static PrivaPub.Federation.Inbox.ForeignMembers;
|
||||||
|
using static PrivaPub.Federation.Objects.ActivityJson;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Inbox.Handlers
|
||||||
|
{
|
||||||
|
public class FollowHandler : IActivityHandler
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
readonly IDeliveryService _delivery;
|
||||||
|
|
||||||
|
public FollowHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
_delivery = delivery;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Type => "Follow";
|
||||||
|
|
||||||
|
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var follow = activity;
|
||||||
|
var follower = actor;
|
||||||
|
|
||||||
|
var target = await _localActors.FindByUri(Id(follow["object"]), token);
|
||||||
|
if (target is not { IsFederated: true } || target.Kind == LocalActorKind.Application)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var existing = await _dbEntities.Followers
|
||||||
|
.Match(f => f.LocalActorId == target.Id && f.LocalActorKind == target.Kind && f.ActorURI == follower.ActorURI)
|
||||||
|
.ExecuteFirstAsync(token);
|
||||||
|
var record = existing ?? new Follower
|
||||||
|
{
|
||||||
|
LocalActorId = target.Id,
|
||||||
|
LocalActorKind = target.Kind,
|
||||||
|
ActorURI = follower.ActorURI
|
||||||
|
};
|
||||||
|
record.InboxURL = follower.InboxURL;
|
||||||
|
record.SharedInboxURL = follower.SharedInboxURL;
|
||||||
|
record.FollowActivityURI = Id(follow);
|
||||||
|
record.IsAccepted = existing?.IsAccepted == true || !target.ManuallyApprovesFollowers;
|
||||||
|
await DB.Default.SaveAsync(record, token);
|
||||||
|
|
||||||
|
if (target.Kind == LocalActorKind.Person)
|
||||||
|
await Notifications.Add(target.Id, record.IsAccepted ? NotificationType.Follow : NotificationType.FollowRequest,
|
||||||
|
follower.ID, follower.ActorURI, default, token);
|
||||||
|
if (!record.IsAccepted)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (target.Kind == LocalActorKind.Group)
|
||||||
|
await AddForeignMember(target.Id, follower.ActorURI, token);
|
||||||
|
|
||||||
|
var accept = ActivityPubRenderer.Accept(target, follow, $"accept-{record.ID}-{DateTime.UtcNow.Ticks}");
|
||||||
|
await _delivery.Enqueue(target, new[] { follower.InboxURL }, accept, token);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Domain.Social;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
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 LikeHandler : IActivityHandler
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
|
||||||
|
public LikeHandler(DbEntities dbEntities)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Type => "Like";
|
||||||
|
|
||||||
|
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 || !MaySee(post, actor))
|
||||||
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(new Favourite { 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.FavouritesCount, 1)).ExecuteAsync(token);
|
||||||
|
if (!post.IsFederatedCopy)
|
||||||
|
await Notifications.Add(post.GroupUserId, NotificationType.Favourite, actor.ID, actor.ActorURI, post.ID, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool MaySee(PostEntity post, ForeignAvatar actor) =>
|
||||||
|
post.Visibility is PostVisibility.Public or PostVisibility.Unlisted
|
||||||
|
|| post.Visibility is PostVisibility.FollowersOnly or PostVisibility.Direct
|
||||||
|
&& (post.To.Contains(actor.ActorURI) || post.Cc.Contains(actor.ActorURI) || post.Mentions.Any(m => m.ActorURI == actor.ActorURI));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
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.Inbox.ForeignMembers;
|
||||||
|
using static PrivaPub.Federation.Objects.ActivityJson;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Inbox.Handlers
|
||||||
|
{
|
||||||
|
public class UndoHandler : IActivityHandler
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
|
||||||
|
public UndoHandler(DbEntities dbEntities, ILocalActorService localActors)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Type => "Undo";
|
||||||
|
|
||||||
|
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var inner = activity["object"];
|
||||||
|
var innerType = inner is JsonObject ? Value(inner, "type") : default;
|
||||||
|
var innerId = Id(inner);
|
||||||
|
|
||||||
|
if (innerType is null or "Follow")
|
||||||
|
await UndoFollow(inner, innerId, actor, token);
|
||||||
|
if (innerType is null or "Like")
|
||||||
|
await UndoLike(inner, innerId, actor, token);
|
||||||
|
if (innerType is null or "Announce")
|
||||||
|
await UndoAnnounce(innerId, actor, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task UndoFollow(JsonNode inner, string innerId, ForeignAvatar actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var followers = await _dbEntities.Followers.Match(f => f.ActorURI == actor.ActorURI).ExecuteAsync(token);
|
||||||
|
var targetUri = inner is JsonObject ? Id(inner["object"]) : default;
|
||||||
|
var target = targetUri == default ? default : await _localActors.FindByUri(targetUri, token);
|
||||||
|
foreach (var follower in followers)
|
||||||
|
{
|
||||||
|
var matchesActivity = innerId != default && follower.FollowActivityURI == innerId;
|
||||||
|
var matchesTarget = target != default && target.Id == follower.LocalActorId && target.Kind == follower.LocalActorKind;
|
||||||
|
if (!matchesActivity && !matchesTarget)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
await DB.Default.DeleteAsync<Follower>(follower.ID);
|
||||||
|
if (follower.LocalActorKind == LocalActorKind.Group)
|
||||||
|
await RemoveForeignMember(follower.LocalActorId, actor.ActorURI, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task UndoLike(JsonNode inner, string innerId, ForeignAvatar actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var favourite = innerId == default
|
||||||
|
? default
|
||||||
|
: await _dbEntities.Favourites.Match(f => f.ActivityURI == innerId && f.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
||||||
|
if (favourite == default && inner is JsonObject && Id(inner["object"]) is { } objectUri)
|
||||||
|
{
|
||||||
|
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
|
||||||
|
favourite = post == default
|
||||||
|
? default
|
||||||
|
: await _dbEntities.Favourites.Match(f => f.PostId == post.ID && f.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
||||||
|
}
|
||||||
|
if (favourite == default)
|
||||||
|
return;
|
||||||
|
await DB.Default.DeleteAsync<Favourite>(favourite.ID);
|
||||||
|
await DB.Default.Update<PostEntity>().MatchID(favourite.PostId).Modify(b => b.Inc(p => p.FavouritesCount, -1)).ExecuteAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task UndoAnnounce(string innerId, ForeignAvatar actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (innerId == default)
|
||||||
|
return;
|
||||||
|
var reblog = await _dbEntities.Posts
|
||||||
|
.Match(p => p.ObjectURI == innerId && p.ActorURI == actor.ActorURI && p.ReblogOfPostId != null)
|
||||||
|
.ExecuteFirstAsync(token);
|
||||||
|
if (reblog == default)
|
||||||
|
return;
|
||||||
|
await DB.Default.DeleteAsync<PostEntity>(reblog.ID);
|
||||||
|
await DB.Default.DeleteAsync<TimelineEntry>(e => e.PostId == reblog.ID);
|
||||||
|
await DB.Default.Update<PostEntity>().MatchID(reblog.ReblogOfPostId).Modify(b => b.Inc(p => p.ReblogsCount, -1)).ExecuteAsync(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
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 UpdateHandler : IActivityHandler
|
||||||
|
{
|
||||||
|
const int MaxRevisions = 20;
|
||||||
|
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
|
||||||
|
public UpdateHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Type => "Update";
|
||||||
|
|
||||||
|
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var inner = activity["object"];
|
||||||
|
if (Id(inner) == actor.ActorURI)
|
||||||
|
{
|
||||||
|
await _remoteActors.GetActor(actor.ActorURI, refresh: true, token);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (inner is not JsonObject || !Origin.Same(Id(inner), actor.ActorURI))
|
||||||
|
return;
|
||||||
|
var note = NoteParser.Parse(inner);
|
||||||
|
if (note == default || note.AttributedTo != actor.ActorURI)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == note.Id && p.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
|
||||||
|
if (post == default || post.DeletedAt.HasValue)
|
||||||
|
return;
|
||||||
|
|
||||||
|
post.Revisions.Add(new PostRevision
|
||||||
|
{
|
||||||
|
Title = post.Title,
|
||||||
|
SpoilerText = post.SpoilerText,
|
||||||
|
ContentHtml = post.ContentHtml,
|
||||||
|
HasContentWarning = post.HasContentWarning,
|
||||||
|
EditedAt = post.EditedAt ?? post.CreationDate
|
||||||
|
});
|
||||||
|
if (post.Revisions.Count > MaxRevisions)
|
||||||
|
post.Revisions.RemoveRange(0, post.Revisions.Count - MaxRevisions);
|
||||||
|
|
||||||
|
var mentions = new List<PostMention>();
|
||||||
|
foreach (var mention in note.Mentions)
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUri(mention.ActorURI, token);
|
||||||
|
mentions.Add(new PostMention { ActorURI = mention.ActorURI, Handle = mention.Handle, IsLocal = local != default, AccountId = local?.Id });
|
||||||
|
}
|
||||||
|
|
||||||
|
post.Title = note.Title;
|
||||||
|
post.SpoilerText = note.SpoilerText;
|
||||||
|
post.HasContentWarning = note.Sensitive;
|
||||||
|
post.Text = note.ContentHtml;
|
||||||
|
post.ContentHtml = note.ContentHtml;
|
||||||
|
post.Language = note.Language;
|
||||||
|
post.Tags = note.Tags.ToList();
|
||||||
|
post.Mentions = mentions;
|
||||||
|
post.Media = note.Attachments.ToList();
|
||||||
|
post.EditedAt = note.Updated ?? DateTime.UtcNow;
|
||||||
|
post.UpdateDate = DateTime.UtcNow;
|
||||||
|
await DB.Default.SaveAsync(post, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Infrastructure.Jobs;
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using static PrivaPub.Federation.Objects.ActivityJson;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Inbox
|
||||||
|
{
|
||||||
|
public interface IActivityHandler
|
||||||
|
{
|
||||||
|
string Type { get; }
|
||||||
|
Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class InboxProcessor : IJobHandler
|
||||||
|
{
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
readonly IReadOnlyDictionary<string, IActivityHandler> _handlers;
|
||||||
|
readonly ILogger<InboxProcessor> _logger;
|
||||||
|
|
||||||
|
public InboxProcessor(IRemoteActorService remoteActors, IEnumerable<IActivityHandler> handlers, ILogger<InboxProcessor> logger)
|
||||||
|
{
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
_handlers = handlers.ToDictionary(h => h.Type, StringComparer.Ordinal);
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JobKind Kind => JobKind.ProcessInbox;
|
||||||
|
public int Concurrency => 2;
|
||||||
|
public int MaxAttempts => 8;
|
||||||
|
public int PerHostLimit => 2;
|
||||||
|
|
||||||
|
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
|
||||||
|
{
|
||||||
|
var payload = JsonSerializer.Deserialize<InboxPayload>(job.Payload);
|
||||||
|
var activity = payload == default ? default : JsonNode.Parse(payload.Activity);
|
||||||
|
var type = Value(activity, "type");
|
||||||
|
if (type == default || !_handlers.TryGetValue(type, out var handler))
|
||||||
|
return JobOutcome.Done;
|
||||||
|
|
||||||
|
var actor = await _remoteActors.GetActor(payload.ActorURI, refresh: false, token);
|
||||||
|
if (actor == default)
|
||||||
|
return JobOutcome.Retry("the actor could not be loaded");
|
||||||
|
|
||||||
|
await handler.Handle(activity, actor, token);
|
||||||
|
_logger.LogInformation("Processed {Type} {Id} from {Actor}", type, Id(activity), actor.ActorURI);
|
||||||
|
return JobOutcome.Done;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Moderation;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Federation.Signing;
|
||||||
|
using PrivaPub.Infrastructure.Jobs;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using static PrivaPub.Federation.Objects.ActivityJson;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Inbox
|
||||||
|
{
|
||||||
|
public sealed record InboxResult(int StatusCode, string Error = default);
|
||||||
|
|
||||||
|
public sealed record InboxPayload(string ActorURI, string Activity);
|
||||||
|
|
||||||
|
public interface IInboxReceiver
|
||||||
|
{
|
||||||
|
Task<InboxResult> Receive(HttpRequest request, LocalActor recipient, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class InboxReceiver : IInboxReceiver
|
||||||
|
{
|
||||||
|
const int MaxBodyBytes = 1024 * 1024;
|
||||||
|
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
readonly IJobQueue _queue;
|
||||||
|
readonly IDomainBlocks _domainBlocks;
|
||||||
|
readonly ILogger<InboxReceiver> _logger;
|
||||||
|
|
||||||
|
public InboxReceiver(ILocalActorService localActors, IRemoteActorService remoteActors, IJobQueue queue, IDomainBlocks domainBlocks,
|
||||||
|
ILogger<InboxReceiver> logger)
|
||||||
|
{
|
||||||
|
_localActors = localActors;
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
_queue = queue;
|
||||||
|
_domainBlocks = domainBlocks;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<InboxResult> Receive(HttpRequest request, LocalActor recipient, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (request.ContentLength > MaxBodyBytes)
|
||||||
|
return new(StatusCodes.Status413PayloadTooLarge);
|
||||||
|
|
||||||
|
using var buffer = new MemoryStream();
|
||||||
|
await request.Body.CopyToAsync(buffer, token);
|
||||||
|
if (buffer.Length > MaxBodyBytes)
|
||||||
|
return new(StatusCodes.Status413PayloadTooLarge);
|
||||||
|
var body = buffer.ToArray();
|
||||||
|
|
||||||
|
JsonNode activity;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
activity = JsonNode.Parse(body);
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
return new(StatusCodes.Status400BadRequest, "the body is not JSON");
|
||||||
|
}
|
||||||
|
if (activity is not JsonObject)
|
||||||
|
return new(StatusCodes.Status400BadRequest, "the body is not an activity");
|
||||||
|
|
||||||
|
var type = Value(activity, "type");
|
||||||
|
var actorUri = Id(activity["actor"]);
|
||||||
|
if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(actorUri))
|
||||||
|
return new(StatusCodes.Status400BadRequest, "type and actor are required");
|
||||||
|
|
||||||
|
var parameters = HttpSignatures.Parse(request.Headers["Signature"].ToString());
|
||||||
|
if (parameters == default)
|
||||||
|
return new(StatusCodes.Status401Unauthorized, "missing or unreadable Signature header");
|
||||||
|
if (_domainBlocks.IsSuspended(HostOf(parameters.KeyId)) || _domainBlocks.IsSuspended(HostOf(actorUri)))
|
||||||
|
return new(StatusCodes.Status202Accepted);
|
||||||
|
|
||||||
|
var requestProblem = HttpSignatures.CheckRequest(request, parameters, body);
|
||||||
|
if (requestProblem != default)
|
||||||
|
return new(StatusCodes.Status401Unauthorized, requestProblem);
|
||||||
|
|
||||||
|
var signingString = HttpSignatures.SigningString(request, parameters);
|
||||||
|
var keyOwner = await _remoteActors.GetActorByKeyId(parameters.KeyId, refresh: false, token);
|
||||||
|
if (keyOwner == default && type == "Delete" && Id(activity["object"]) == actorUri)
|
||||||
|
return new(StatusCodes.Status202Accepted);
|
||||||
|
if (keyOwner == default || !HttpSignatures.Verify(keyOwner.PublicKey, signingString, parameters.Signature))
|
||||||
|
{
|
||||||
|
keyOwner = await _remoteActors.GetActorByKeyId(parameters.KeyId, refresh: true, token);
|
||||||
|
if (keyOwner == default || !HttpSignatures.Verify(keyOwner.PublicKey, signingString, parameters.Signature))
|
||||||
|
return new(StatusCodes.Status401Unauthorized, "the signature does not verify");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.Equals(keyOwner.ActorURI, actorUri, StringComparison.Ordinal))
|
||||||
|
return new(StatusCodes.Status401Unauthorized, "the activity's actor is not the key's owner");
|
||||||
|
|
||||||
|
var shapeProblem = await ShapeProblem(type, activity, actorUri, token);
|
||||||
|
if (shapeProblem != default)
|
||||||
|
return shapeProblem;
|
||||||
|
|
||||||
|
var activityId = Id(activity);
|
||||||
|
await _queue.Enqueue(JobKind.ProcessInbox, JsonSerializer.Serialize(new InboxPayload(actorUri, activity.ToJsonString())),
|
||||||
|
new Uri(actorUri).Host.ToLowerInvariant(), activityId == default ? default : "inbox|" + activityId, token);
|
||||||
|
_logger.LogInformation("Inbox {Recipient}: {Type} from {Actor} queued", recipient?.Handle ?? "shared", type, actorUri);
|
||||||
|
return new(StatusCodes.Status202Accepted);
|
||||||
|
}
|
||||||
|
|
||||||
|
static string HostOf(string uri) => Uri.TryCreate(uri, UriKind.Absolute, out var parsed) ? parsed.Host : default;
|
||||||
|
|
||||||
|
async Task<InboxResult> ShapeProblem(string type, JsonNode activity, string actorUri, CancellationToken token)
|
||||||
|
{
|
||||||
|
var activityId = Id(activity);
|
||||||
|
if (activityId != default && !Origin.Same(activityId, actorUri))
|
||||||
|
return new(StatusCodes.Status400BadRequest, "the activity's id is not on its actor's origin");
|
||||||
|
|
||||||
|
var inner = activity["object"];
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case "Follow":
|
||||||
|
var target = await _localActors.FindByUri(Id(inner), token);
|
||||||
|
if (target is not { IsFederated: true } || target.Kind == LocalActorKind.Application)
|
||||||
|
return new(StatusCodes.Status404NotFound, "no such local actor");
|
||||||
|
break;
|
||||||
|
case "Undo" when inner is JsonObject && Id(inner["actor"]) != actorUri:
|
||||||
|
return new(StatusCodes.Status400BadRequest, "an actor can only undo its own activities");
|
||||||
|
case "Create" or "Update" when inner is JsonObject && Origin.Same(Id(inner), actorUri)
|
||||||
|
&& inner["attributedTo"] != default && Id(inner["attributedTo"]) != actorUri
|
||||||
|
&& Value(inner, "type") is not ("Person" or "Service" or "Application" or "Group" or "Organization"):
|
||||||
|
return new(StatusCodes.Status400BadRequest, "the object is not attributed to the actor");
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,413 +0,0 @@
|
|||||||
using MongoDB.Entities;
|
|
||||||
|
|
||||||
using PrivaPub.Models.Federation;
|
|
||||||
using PrivaPub.Models.Group;
|
|
||||||
using PrivaPub.Models.Post;
|
|
||||||
using PrivaPub.Models.User;
|
|
||||||
using PrivaPub.StaticServices;
|
|
||||||
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Nodes;
|
|
||||||
|
|
||||||
using DmPostEntity = PrivaPub.Models.Post.DmPost;
|
|
||||||
using GroupEntity = PrivaPub.Models.Group.Group;
|
|
||||||
using PostEntity = PrivaPub.Models.Post.Post;
|
|
||||||
using PrivaPub.Federation.Actors;
|
|
||||||
using PrivaPub.Federation.Signing;
|
|
||||||
using PrivaPub.Federation.Rendering;
|
|
||||||
using PrivaPub.Federation.Objects;
|
|
||||||
using PrivaPub.Federation.Outbox;
|
|
||||||
|
|
||||||
namespace PrivaPub.Federation.Inbox
|
|
||||||
{
|
|
||||||
public sealed record InboxResult(int StatusCode, string Error = default);
|
|
||||||
|
|
||||||
public interface IInboxService
|
|
||||||
{
|
|
||||||
Task<InboxResult> Receive(HttpRequest request, LocalActor recipient, CancellationToken token);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class InboxService : IInboxService
|
|
||||||
{
|
|
||||||
const int MaxBodyBytes = 1024 * 1024;
|
|
||||||
|
|
||||||
readonly DbEntities _dbEntities;
|
|
||||||
readonly ILocalActorService _localActors;
|
|
||||||
readonly IRemoteActorService _remoteActors;
|
|
||||||
readonly IDeliveryService _delivery;
|
|
||||||
readonly ILogger<InboxService> _logger;
|
|
||||||
|
|
||||||
public InboxService(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors,
|
|
||||||
IDeliveryService delivery, ILogger<InboxService> logger)
|
|
||||||
{
|
|
||||||
_dbEntities = dbEntities;
|
|
||||||
_localActors = localActors;
|
|
||||||
_remoteActors = remoteActors;
|
|
||||||
_delivery = delivery;
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<InboxResult> Receive(HttpRequest request, LocalActor recipient, CancellationToken token)
|
|
||||||
{
|
|
||||||
if (request.ContentLength > MaxBodyBytes)
|
|
||||||
return new(StatusCodes.Status413PayloadTooLarge);
|
|
||||||
|
|
||||||
using var buffer = new MemoryStream();
|
|
||||||
await request.Body.CopyToAsync(buffer, token);
|
|
||||||
if (buffer.Length > MaxBodyBytes)
|
|
||||||
return new(StatusCodes.Status413PayloadTooLarge);
|
|
||||||
var body = buffer.ToArray();
|
|
||||||
|
|
||||||
JsonNode activity;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
activity = JsonNode.Parse(body);
|
|
||||||
}
|
|
||||||
catch (JsonException)
|
|
||||||
{
|
|
||||||
return new(StatusCodes.Status400BadRequest, "the body is not JSON");
|
|
||||||
}
|
|
||||||
if (activity is not JsonObject)
|
|
||||||
return new(StatusCodes.Status400BadRequest, "the body is not an activity");
|
|
||||||
|
|
||||||
var type = Value(activity, "type");
|
|
||||||
var actorUri = Id(activity["actor"]);
|
|
||||||
if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(actorUri))
|
|
||||||
return new(StatusCodes.Status400BadRequest, "type and actor are required");
|
|
||||||
|
|
||||||
var parameters = HttpSignatures.Parse(request.Headers["Signature"].ToString());
|
|
||||||
if (parameters == default)
|
|
||||||
return new(StatusCodes.Status401Unauthorized, "missing or unreadable Signature header");
|
|
||||||
|
|
||||||
var requestProblem = HttpSignatures.CheckRequest(request, parameters, body);
|
|
||||||
if (requestProblem != default)
|
|
||||||
return new(StatusCodes.Status401Unauthorized, requestProblem);
|
|
||||||
|
|
||||||
var signingString = HttpSignatures.SigningString(request, parameters);
|
|
||||||
var keyOwner = await _remoteActors.GetActorByKeyId(parameters.KeyId, refresh: false, token);
|
|
||||||
if (keyOwner == default && type == "Delete" && Id(activity["object"]) == actorUri)
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
if (keyOwner == default || !HttpSignatures.Verify(keyOwner.PublicKey, signingString, parameters.Signature))
|
|
||||||
{
|
|
||||||
keyOwner = await _remoteActors.GetActorByKeyId(parameters.KeyId, refresh: true, token);
|
|
||||||
if (keyOwner == default || !HttpSignatures.Verify(keyOwner.PublicKey, signingString, parameters.Signature))
|
|
||||||
return new(StatusCodes.Status401Unauthorized, "the signature does not verify");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.Equals(keyOwner.ActorURI, actorUri, StringComparison.Ordinal))
|
|
||||||
return new(StatusCodes.Status401Unauthorized, "the activity's actor is not the key's owner");
|
|
||||||
|
|
||||||
var activityId = Id(activity);
|
|
||||||
if (activityId != default && !Origin.Same(activityId, actorUri))
|
|
||||||
return new(StatusCodes.Status400BadRequest, "the activity's id is not on its actor's origin");
|
|
||||||
|
|
||||||
_logger.LogInformation("Inbox {Recipient}: {Type} from {Actor}", recipient?.Handle ?? "shared", type, actorUri);
|
|
||||||
|
|
||||||
return type switch
|
|
||||||
{
|
|
||||||
"Follow" => await Follow(activity, keyOwner, token),
|
|
||||||
"Undo" => await Undo(activity, keyOwner, token),
|
|
||||||
"Create" => await Create(activity, keyOwner, token),
|
|
||||||
"Delete" => await Delete(activity, keyOwner, token),
|
|
||||||
"Update" => await Update(activity, keyOwner, token),
|
|
||||||
_ => new(StatusCodes.Status202Accepted)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task<InboxResult> Follow(JsonNode follow, ForeignAvatar follower, CancellationToken token)
|
|
||||||
{
|
|
||||||
var target = await _localActors.FindByUri(Id(follow["object"]), token);
|
|
||||||
if (target is not { IsFederated: true } || target.Kind == LocalActorKind.Application)
|
|
||||||
return new(StatusCodes.Status404NotFound, "no such local actor");
|
|
||||||
|
|
||||||
var existing = await _dbEntities.Followers
|
|
||||||
.Match(f => f.LocalActorId == target.Id && f.LocalActorKind == target.Kind && f.ActorURI == follower.ActorURI)
|
|
||||||
.ExecuteFirstAsync(token);
|
|
||||||
var record = existing ?? new Follower
|
|
||||||
{
|
|
||||||
LocalActorId = target.Id,
|
|
||||||
LocalActorKind = target.Kind,
|
|
||||||
ActorURI = follower.ActorURI
|
|
||||||
};
|
|
||||||
record.InboxURL = follower.InboxURL;
|
|
||||||
record.SharedInboxURL = follower.SharedInboxURL;
|
|
||||||
record.FollowActivityURI = Id(follow);
|
|
||||||
record.IsAccepted = existing?.IsAccepted == true || !target.ManuallyApprovesFollowers;
|
|
||||||
await DB.Default.SaveAsync(record, token);
|
|
||||||
|
|
||||||
if (!record.IsAccepted)
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
|
|
||||||
if (target.Kind == LocalActorKind.Group)
|
|
||||||
await AddForeignMember(target.Id, follower.ActorURI, token);
|
|
||||||
|
|
||||||
var accept = ActivityPubRenderer.Accept(target, follow, $"accept-{record.ID}-{DateTime.UtcNow.Ticks}");
|
|
||||||
await _delivery.Enqueue(target, new[] { follower.InboxURL }, accept, token);
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task<InboxResult> Undo(JsonNode undo, ForeignAvatar actor, CancellationToken token)
|
|
||||||
{
|
|
||||||
var inner = undo["object"];
|
|
||||||
var innerType = inner is JsonObject ? Value(inner, "type") : default;
|
|
||||||
var innerId = Id(inner);
|
|
||||||
|
|
||||||
if (inner is JsonObject && Id(inner["actor"]) != actor.ActorURI)
|
|
||||||
return new(StatusCodes.Status400BadRequest, "an actor can only undo its own activities");
|
|
||||||
if (innerType is not (null or "Follow"))
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
|
|
||||||
var followers = await _dbEntities.Followers.Match(f => f.ActorURI == actor.ActorURI).ExecuteAsync(token);
|
|
||||||
var targetUri = inner is JsonObject ? Id(inner["object"]) : default;
|
|
||||||
foreach (var follower in followers)
|
|
||||||
{
|
|
||||||
var matchesActivity = innerId != default && follower.FollowActivityURI == innerId;
|
|
||||||
var target = targetUri == default ? default : await _localActors.FindByUri(targetUri, token);
|
|
||||||
var matchesTarget = target != default && target.Id == follower.LocalActorId && target.Kind == follower.LocalActorKind;
|
|
||||||
if (!matchesActivity && !matchesTarget)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
await DB.Default.DeleteAsync<Follower>(follower.ID);
|
|
||||||
if (follower.LocalActorKind == LocalActorKind.Group)
|
|
||||||
await RemoveForeignMember(follower.LocalActorId, actor.ActorURI, token);
|
|
||||||
}
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task<InboxResult> Create(JsonNode create, ForeignAvatar author, CancellationToken token)
|
|
||||||
{
|
|
||||||
var note = create["object"];
|
|
||||||
if (note is not JsonObject || !Origin.Same(Id(note), author.ActorURI))
|
|
||||||
{
|
|
||||||
using var fetched = await _remoteActors.FetchObject(Id(note), token);
|
|
||||||
note = fetched == default ? default : JsonNode.Parse(fetched.Root.GetRawText());
|
|
||||||
}
|
|
||||||
if (note is not JsonObject || Value(note, "type") is not ("Note" or "Article" or "Page" or "Question"))
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
|
|
||||||
var objectUri = Id(note);
|
|
||||||
if (string.IsNullOrEmpty(objectUri) || Id(note["attributedTo"]) != author.ActorURI)
|
|
||||||
return new(StatusCodes.Status400BadRequest, "the object is not attributed to the actor");
|
|
||||||
|
|
||||||
var addressed = Addresses(note).Concat(Addresses(create)).Distinct(StringComparer.Ordinal).ToList();
|
|
||||||
var isPublic = addressed.Contains(ActivityPubRenderer.Public) || addressed.Contains("as:Public") || addressed.Contains("Public");
|
|
||||||
var inReplyTo = Id(note["inReplyTo"]);
|
|
||||||
|
|
||||||
var localTargets = new List<LocalActor>();
|
|
||||||
foreach (var uri in addressed.Concat(new[] { Id(note["audience"]) }).Where(u => u != default).Distinct())
|
|
||||||
{
|
|
||||||
var local = await _localActors.FindByUri(uri, token);
|
|
||||||
if (local is { IsFederated: true } && localTargets.All(l => l.Id != local.Id))
|
|
||||||
localTargets.Add(local);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isPublic || addressed.Any(a => a == author.ActorURI + "/followers" || a.EndsWith("/followers")))
|
|
||||||
{
|
|
||||||
var group = localTargets.FirstOrDefault(t => t.Kind == LocalActorKind.Group);
|
|
||||||
if (group != default && !await IsAcceptedFollower(group, author.ActorURI, token))
|
|
||||||
group = default;
|
|
||||||
if (group == default && localTargets.All(t => t.Kind != LocalActorKind.Person))
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
|
|
||||||
if (await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteAnyAsync(token))
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
|
|
||||||
var html = ContentSanitizer.Html(Value(note, "content"));
|
|
||||||
var post = new PostEntity
|
|
||||||
{
|
|
||||||
ObjectURI = objectUri,
|
|
||||||
ActorURI = author.ActorURI,
|
|
||||||
GroupId = group?.Id,
|
|
||||||
Title = Value(note, "summary") ?? Value(note, "name"),
|
|
||||||
Text = html,
|
|
||||||
ContentHtml = html,
|
|
||||||
ContentFormat = ContentFormat.Html,
|
|
||||||
HasContentWarning = note["sensitive"] is JsonValue sensitive && sensitive.TryGetValue<bool>(out var s) && s,
|
|
||||||
AnsweringToPostId = await LocalPostId(inReplyTo, token) ?? inReplyTo,
|
|
||||||
IsFederatedCopy = true,
|
|
||||||
CreationDate = DateTime.TryParse(Value(note, "published"), out var published) ? published.ToUniversalTime() : DateTime.UtcNow
|
|
||||||
};
|
|
||||||
await DB.Default.SaveAsync(post, token);
|
|
||||||
|
|
||||||
if (group != default)
|
|
||||||
{
|
|
||||||
var announce = ActivityPubRenderer.Announce(group, objectUri, $"announce-{post.ID}");
|
|
||||||
await _delivery.EnqueueToFollowers(group, announce, token);
|
|
||||||
}
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
}
|
|
||||||
|
|
||||||
var recipients = localTargets.Where(t => t.Kind == LocalActorKind.Person).ToList();
|
|
||||||
if (recipients.Count == 0)
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
if (await _dbEntities.DmPosts.Match(p => p.ObjectURI == objectUri).ExecuteAnyAsync(token))
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
|
|
||||||
var participants = recipients.Select(r => r.Uri)
|
|
||||||
.Concat(addressed.Where(a => a != ActivityPubRenderer.Public && !a.EndsWith("/followers") && !a.EndsWith("/following")))
|
|
||||||
.Append(author.ActorURI)
|
|
||||||
.ToList();
|
|
||||||
var context = Value(note, "context") ?? Value(note, "conversation");
|
|
||||||
var dmGroup = await FindOrCreateDmGroup(participants, Origin.Same(context, author.ActorURI) ? context : default, token);
|
|
||||||
var dmHtml = ContentSanitizer.Html(Value(note, "content"));
|
|
||||||
var dm = new DmPostEntity
|
|
||||||
{
|
|
||||||
ObjectURI = objectUri,
|
|
||||||
ActorURI = author.ActorURI,
|
|
||||||
GroupId = dmGroup.ID,
|
|
||||||
Title = Value(note, "summary"),
|
|
||||||
Text = dmHtml,
|
|
||||||
ContentHtml = dmHtml,
|
|
||||||
ContentFormat = ContentFormat.Html,
|
|
||||||
HasContentWarning = note["sensitive"] is JsonValue dmSensitive && dmSensitive.TryGetValue<bool>(out var ds) && ds,
|
|
||||||
AnsweringToPostId = inReplyTo,
|
|
||||||
IsFederatedCopy = true,
|
|
||||||
CreationDate = DateTime.TryParse(Value(note, "published"), out var dmPublished) ? dmPublished.ToUniversalTime() : DateTime.UtcNow
|
|
||||||
};
|
|
||||||
await DB.Default.SaveAsync(dm, token);
|
|
||||||
await DB.Default.Update<DmGroup>().MatchID(dmGroup.ID).Modify(g => g.UpdatedAt, DateTime.UtcNow).ExecuteAsync(token);
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task<InboxResult> Delete(JsonNode delete, ForeignAvatar actor, CancellationToken token)
|
|
||||||
{
|
|
||||||
var objectUri = Id(delete["object"]);
|
|
||||||
if (!Origin.Same(objectUri, actor.ActorURI))
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
|
|
||||||
if (objectUri == actor.ActorURI)
|
|
||||||
{
|
|
||||||
actor.DeletionAt = DateTime.UtcNow;
|
|
||||||
actor.AccountState = AvatarAccountState.Deleted;
|
|
||||||
await DB.Default.SaveAsync(actor, token);
|
|
||||||
var followers = await _dbEntities.Followers.Match(f => f.ActorURI == actor.ActorURI).ExecuteAsync(token);
|
|
||||||
foreach (var follower in followers)
|
|
||||||
{
|
|
||||||
await DB.Default.DeleteAsync<Follower>(follower.ID);
|
|
||||||
if (follower.LocalActorKind == LocalActorKind.Group)
|
|
||||||
await RemoveForeignMember(follower.LocalActorId, actor.ActorURI, token);
|
|
||||||
}
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
}
|
|
||||||
|
|
||||||
await DB.Default.DeleteAsync<PostEntity>(p => p.ObjectURI == objectUri && p.ActorURI == actor.ActorURI);
|
|
||||||
await DB.Default.DeleteAsync<DmPostEntity>(p => p.ObjectURI == objectUri && p.ActorURI == actor.ActorURI);
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task<InboxResult> Update(JsonNode update, ForeignAvatar actor, CancellationToken token)
|
|
||||||
{
|
|
||||||
var inner = update["object"];
|
|
||||||
if (Id(inner) == actor.ActorURI)
|
|
||||||
{
|
|
||||||
await _remoteActors.GetActor(actor.ActorURI, refresh: true, token);
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
}
|
|
||||||
if (inner is not JsonObject || Id(inner["attributedTo"]) != actor.ActorURI || !Origin.Same(Id(inner), actor.ActorURI))
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
|
|
||||||
var objectUri = Id(inner);
|
|
||||||
var text = ContentSanitizer.Html(Value(inner, "content"));
|
|
||||||
await DB.Default.Update<PostEntity>()
|
|
||||||
.Match(p => p.ObjectURI == objectUri && p.ActorURI == actor.ActorURI)
|
|
||||||
.Modify(p => p.Text, text)
|
|
||||||
.Modify(p => p.ContentHtml, text)
|
|
||||||
.Modify(p => p.UpdateDate, DateTime.UtcNow)
|
|
||||||
.ExecuteAsync(token);
|
|
||||||
await DB.Default.Update<DmPostEntity>()
|
|
||||||
.Match(p => p.ObjectURI == objectUri && p.ActorURI == actor.ActorURI)
|
|
||||||
.Modify(p => p.Text, text)
|
|
||||||
.Modify(p => p.ContentHtml, text)
|
|
||||||
.Modify(p => p.UpdateDate, DateTime.UtcNow)
|
|
||||||
.ExecuteAsync(token);
|
|
||||||
return new(StatusCodes.Status202Accepted);
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task<bool> IsAcceptedFollower(LocalActor group, string actorUri, CancellationToken token) =>
|
|
||||||
await _dbEntities.Followers
|
|
||||||
.Match(f => f.LocalActorId == group.Id && f.LocalActorKind == LocalActorKind.Group && f.ActorURI == actorUri && f.IsAccepted)
|
|
||||||
.ExecuteAnyAsync(token);
|
|
||||||
|
|
||||||
async Task AddForeignMember(string groupId, string actorUri, CancellationToken token)
|
|
||||||
{
|
|
||||||
var group = await _dbEntities.Groups.MatchID(groupId).ExecuteFirstAsync(token);
|
|
||||||
if (group == default || group.Members.Any(m => m.IsForeign && m.AvatarId == actorUri))
|
|
||||||
return;
|
|
||||||
group.Members.Add(new GroupMember { AvatarId = actorUri, IsForeign = true });
|
|
||||||
group.UpdatedAt = DateTime.UtcNow;
|
|
||||||
await DB.Default.SaveAsync(group, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task RemoveForeignMember(string groupId, string actorUri, CancellationToken token)
|
|
||||||
{
|
|
||||||
var group = await _dbEntities.Groups.MatchID(groupId).ExecuteFirstAsync(token);
|
|
||||||
if (group == default || group.Members.RemoveAll(m => m.IsForeign && m.AvatarId == actorUri) == 0)
|
|
||||||
return;
|
|
||||||
group.UpdatedAt = DateTime.UtcNow;
|
|
||||||
await DB.Default.SaveAsync<GroupEntity>(group, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task<DmGroup> FindOrCreateDmGroup(List<string> participantUris, string context, CancellationToken token)
|
|
||||||
{
|
|
||||||
var members = new List<GroupMember>();
|
|
||||||
foreach (var uri in participantUris.Distinct(StringComparer.Ordinal))
|
|
||||||
{
|
|
||||||
var local = await _localActors.FindByUri(uri, token);
|
|
||||||
var member = local != default && local.Kind == LocalActorKind.Person
|
|
||||||
? new GroupMember { AvatarId = local.Id }
|
|
||||||
: new GroupMember { AvatarId = uri, IsForeign = true };
|
|
||||||
if (members.All(m => m.AvatarId != member.AvatarId || m.IsForeign != member.IsForeign))
|
|
||||||
members.Add(member);
|
|
||||||
}
|
|
||||||
|
|
||||||
var key = DmGroup.KeyOf(members);
|
|
||||||
var match = await _dbEntities.DmGroups.Match(g => g.ParticipantsKey == key && !g.DeletionAt.HasValue).ExecuteFirstAsync(token);
|
|
||||||
if (match != default)
|
|
||||||
return match;
|
|
||||||
|
|
||||||
var dmGroup = new DmGroup { Members = members, ConversationURI = context, ParticipantsKey = key };
|
|
||||||
await DB.Default.SaveAsync(dmGroup, token);
|
|
||||||
return dmGroup;
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task<string> LocalPostId(string uri, CancellationToken token)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(uri) || !uri.StartsWith(_localActors.BaseAddress + "/peasants/", StringComparison.OrdinalIgnoreCase))
|
|
||||||
return default;
|
|
||||||
var id = uri[(uri.LastIndexOf('/') + 1)..];
|
|
||||||
return await _dbEntities.Posts.MatchID(id).ExecuteAnyAsync(token) ? id : default;
|
|
||||||
}
|
|
||||||
|
|
||||||
static IEnumerable<string> Addresses(JsonNode node)
|
|
||||||
{
|
|
||||||
foreach (var field in new[] { "to", "cc", "bto", "bcc", "audience" })
|
|
||||||
{
|
|
||||||
switch (node[field])
|
|
||||||
{
|
|
||||||
case JsonArray array:
|
|
||||||
foreach (var item in array)
|
|
||||||
{
|
|
||||||
var id = Id(item);
|
|
||||||
if (id != default)
|
|
||||||
yield return id;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case JsonNode single when Id(single) is { } id:
|
|
||||||
yield return id;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string Id(JsonNode node) => node switch
|
|
||||||
{
|
|
||||||
JsonValue value when value.TryGetValue<string>(out var text) => text,
|
|
||||||
JsonObject obj => Value(obj, "id") ?? Value(obj, "href"),
|
|
||||||
JsonArray array => array.Select(Id).FirstOrDefault(i => i != default),
|
|
||||||
_ => default
|
|
||||||
};
|
|
||||||
|
|
||||||
public static string Value(JsonNode node, string property) =>
|
|
||||||
node is JsonObject obj && obj[property] is JsonValue value && value.TryGetValue<string>(out var text) ? text : default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Moderation;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
|
using PrivaPub.Infrastructure.Ids;
|
||||||
|
using PrivaPub.Infrastructure.Jobs;
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Inbox
|
||||||
|
{
|
||||||
|
public sealed record AncestorsPayload(string PostId, int Depth);
|
||||||
|
|
||||||
|
public interface IRemotePosts
|
||||||
|
{
|
||||||
|
Task<PostEntity> Build(NoteDocument note, ForeignAvatar author, PostVisibility visibility, IReadOnlyList<string> to, IReadOnlyList<string> cc,
|
||||||
|
PostEntity parent, CancellationToken token);
|
||||||
|
Task<PostEntity> Parent(string inReplyTo, int depth, CancellationToken token);
|
||||||
|
Task<PostEntity> StoreContext(string objectUri, int depth, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RemotePosts : IRemotePosts
|
||||||
|
{
|
||||||
|
public const int MaxDepth = 10;
|
||||||
|
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IRemoteActorService _remoteActors;
|
||||||
|
readonly IDomainBlocks _domainBlocks;
|
||||||
|
readonly IJobQueue _queue;
|
||||||
|
|
||||||
|
public RemotePosts(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDomainBlocks domainBlocks,
|
||||||
|
IJobQueue queue)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_remoteActors = remoteActors;
|
||||||
|
_domainBlocks = domainBlocks;
|
||||||
|
_queue = queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<PostEntity> Build(NoteDocument note, ForeignAvatar author, PostVisibility visibility, IReadOnlyList<string> to,
|
||||||
|
IReadOnlyList<string> cc, PostEntity parent, CancellationToken token)
|
||||||
|
{
|
||||||
|
var mentions = new List<PostMention>();
|
||||||
|
foreach (var mention in note.Mentions)
|
||||||
|
{
|
||||||
|
var local = await _localActors.FindByUri(mention.ActorURI, token);
|
||||||
|
mentions.Add(new PostMention { ActorURI = mention.ActorURI, Handle = mention.Handle, IsLocal = local != default, AccountId = local?.Id });
|
||||||
|
}
|
||||||
|
|
||||||
|
return new PostEntity
|
||||||
|
{
|
||||||
|
ID = PrivacyIds.At(note.Published),
|
||||||
|
ObjectURI = note.Id,
|
||||||
|
ActorURI = author.ActorURI,
|
||||||
|
AuthorAccountId = author.ID,
|
||||||
|
Visibility = visibility,
|
||||||
|
To = to.ToList(),
|
||||||
|
Cc = cc.ToList(),
|
||||||
|
Url = note.Url,
|
||||||
|
ContextURI = note.Context,
|
||||||
|
QuoteURI = note.QuoteUri,
|
||||||
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<PostEntity> Parent(string inReplyTo, int depth, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(inReplyTo))
|
||||||
|
return default;
|
||||||
|
var known = await _dbEntities.Posts.Match(p => p.ObjectURI == inReplyTo && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
if (known != default || inReplyTo.StartsWith(_localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|
||||||
|
return known;
|
||||||
|
return await StoreContext(inReplyTo, depth, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<PostEntity> StoreContext(string objectUri, int depth, CancellationToken token)
|
||||||
|
{
|
||||||
|
var existing = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
|
||||||
|
if (existing != default || depth > MaxDepth)
|
||||||
|
return existing;
|
||||||
|
|
||||||
|
using var fetched = await _remoteActors.FetchObject(objectUri, token);
|
||||||
|
var note = fetched == default ? default : NoteParser.Parse(JsonNode.Parse(fetched.Root.GetRawText()));
|
||||||
|
if (note == default || !Origin.Same(note.Id, note.AttributedTo))
|
||||||
|
return default;
|
||||||
|
var visibility = Addressing.Classify(note.To, note.Cc, default);
|
||||||
|
if (visibility is not (PostVisibility.Public or PostVisibility.Unlisted))
|
||||||
|
return default;
|
||||||
|
var author = await _remoteActors.GetActor(note.AttributedTo, refresh: false, token);
|
||||||
|
if (author == default)
|
||||||
|
return default;
|
||||||
|
|
||||||
|
var grandparent = string.IsNullOrEmpty(note.InReplyTo)
|
||||||
|
? default
|
||||||
|
: await _dbEntities.Posts.Match(p => p.ObjectURI == note.InReplyTo && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||||
|
var post = await Build(note, author, visibility, note.To, note.Cc, grandparent, token);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(post, token);
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
return await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (grandparent == default && !string.IsNullOrEmpty(note.InReplyTo) && depth < MaxDepth)
|
||||||
|
await _queue.Enqueue(JobKind.FetchAncestors, JsonSerializer.Serialize(new AncestorsPayload(post.ID, depth + 1)), new Uri(note.InReplyTo).Host,
|
||||||
|
"ancestors|" + post.ID, token);
|
||||||
|
return post;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AncestorsJobHandler : IJobHandler
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly IRemotePosts _remotePosts;
|
||||||
|
|
||||||
|
public AncestorsJobHandler(DbEntities dbEntities, IRemotePosts remotePosts)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_remotePosts = remotePosts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JobKind Kind => JobKind.FetchAncestors;
|
||||||
|
public int Concurrency => 1;
|
||||||
|
public int MaxAttempts => 3;
|
||||||
|
public int PerHostLimit => 1;
|
||||||
|
|
||||||
|
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
|
||||||
|
{
|
||||||
|
var payload = JsonSerializer.Deserialize<AncestorsPayload>(job.Payload);
|
||||||
|
var child = await _dbEntities.Posts.MatchID(payload.PostId).ExecuteFirstAsync(token);
|
||||||
|
if (child == default || string.IsNullOrEmpty(child.InReplyToURI) || !string.IsNullOrEmpty(child.AnsweringToPostId))
|
||||||
|
return JobOutcome.Done;
|
||||||
|
|
||||||
|
var parent = await _remotePosts.Parent(child.InReplyToURI, payload.Depth, token);
|
||||||
|
if (parent == default)
|
||||||
|
return JobOutcome.Done;
|
||||||
|
await DB.Default.Update<PostEntity>().MatchID(child.ID)
|
||||||
|
.Modify(p => p.AnsweringToPostId, parent.ID)
|
||||||
|
.Modify(p => p.InReplyToAccountId, parent.AuthorAccountId ?? parent.GroupUserId)
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
return JobOutcome.Done;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Moderation
|
||||||
|
{
|
||||||
|
public interface IDomainBlocks
|
||||||
|
{
|
||||||
|
DomainBlock Find(string host);
|
||||||
|
bool IsSuspended(string host);
|
||||||
|
Task Reload(CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DomainBlocks : IDomainBlocks
|
||||||
|
{
|
||||||
|
static readonly TimeSpan Staleness = TimeSpan.FromMinutes(5);
|
||||||
|
|
||||||
|
readonly ILogger<DomainBlocks> _logger;
|
||||||
|
IReadOnlyDictionary<string, DomainBlock> _blocks = new Dictionary<string, DomainBlock>();
|
||||||
|
DateTime _loadedAt = DateTime.MinValue;
|
||||||
|
int _reloading;
|
||||||
|
|
||||||
|
public DomainBlocks(ILogger<DomainBlocks> logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string Normalise(string domain) => domain?.Trim().Trim('.').ToLowerInvariant();
|
||||||
|
|
||||||
|
public DomainBlock Find(string host)
|
||||||
|
{
|
||||||
|
RefreshIfStale();
|
||||||
|
host = Normalise(host);
|
||||||
|
var blocks = _blocks;
|
||||||
|
while (!string.IsNullOrEmpty(host))
|
||||||
|
{
|
||||||
|
if (blocks.TryGetValue(host, out var block))
|
||||||
|
return block;
|
||||||
|
var dot = host.IndexOf('.');
|
||||||
|
host = dot < 0 ? default : host[(dot + 1)..];
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsSuspended(string host) => Find(host)?.Severity == DomainBlockSeverity.Suspend;
|
||||||
|
|
||||||
|
public async Task Reload(CancellationToken token)
|
||||||
|
{
|
||||||
|
var blocks = await DB.Default.Find<DomainBlock>().ExecuteAsync(token);
|
||||||
|
_blocks = blocks.Where(b => !string.IsNullOrEmpty(b.Domain))
|
||||||
|
.GroupBy(b => Normalise(b.Domain))
|
||||||
|
.ToDictionary(g => g.Key, g => g.First());
|
||||||
|
_loadedAt = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RefreshIfStale()
|
||||||
|
{
|
||||||
|
if (DateTime.UtcNow - _loadedAt < Staleness || Interlocked.Exchange(ref _reloading, 1) == 1)
|
||||||
|
return;
|
||||||
|
_ = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await Reload(CancellationToken.None);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "Domain blocks could not be reloaded");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Interlocked.Exchange(ref _reloading, 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Objects
|
||||||
|
{
|
||||||
|
public static class ActivityJson
|
||||||
|
{
|
||||||
|
public static string Id(JsonNode node) => node switch
|
||||||
|
{
|
||||||
|
JsonValue value when value.TryGetValue<string>(out var text) => text,
|
||||||
|
JsonObject obj => Value(obj, "id") ?? Value(obj, "href"),
|
||||||
|
JsonArray array => array.Select(Id).FirstOrDefault(i => i != default),
|
||||||
|
_ => default
|
||||||
|
};
|
||||||
|
|
||||||
|
public static string Value(JsonNode node, string property) =>
|
||||||
|
node is JsonObject obj && obj[property] is JsonValue value && value.TryGetValue<string>(out var text) ? text : default;
|
||||||
|
|
||||||
|
public static IEnumerable<string> Addresses(JsonNode node)
|
||||||
|
{
|
||||||
|
foreach (var field in new[] { "to", "cc", "bto", "bcc", "audience" })
|
||||||
|
{
|
||||||
|
switch (node[field])
|
||||||
|
{
|
||||||
|
case JsonArray array:
|
||||||
|
foreach (var item in array)
|
||||||
|
{
|
||||||
|
var id = Id(item);
|
||||||
|
if (id != default)
|
||||||
|
yield return id;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case JsonNode single when Id(single) is { } id:
|
||||||
|
yield return id;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using PrivaPub.Models.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Objects
|
||||||
|
{
|
||||||
|
public static class Addressing
|
||||||
|
{
|
||||||
|
public const string Public = "https://www.w3.org/ns/activitystreams#Public";
|
||||||
|
|
||||||
|
public static bool IsPublic(string address) => address is Public or "as:Public" or "Public";
|
||||||
|
|
||||||
|
public static PostVisibility Classify(IReadOnlyCollection<string> to, IReadOnlyCollection<string> cc, string authorFollowers)
|
||||||
|
{
|
||||||
|
if (to.Any(IsPublic))
|
||||||
|
return PostVisibility.Public;
|
||||||
|
if (cc.Any(IsPublic))
|
||||||
|
return PostVisibility.Unlisted;
|
||||||
|
if (!string.IsNullOrEmpty(authorFollowers) && (to.Contains(authorFollowers) || cc.Contains(authorFollowers)))
|
||||||
|
return PostVisibility.FollowersOnly;
|
||||||
|
return PostVisibility.Direct;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,179 @@
|
|||||||
|
using PrivaPub.Models.Post;
|
||||||
|
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using static PrivaPub.Federation.Objects.ActivityJson;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Objects
|
||||||
|
{
|
||||||
|
public sealed class NoteDocument
|
||||||
|
{
|
||||||
|
public string Id { get; init; }
|
||||||
|
public string Type { get; init; }
|
||||||
|
public string AttributedTo { get; init; }
|
||||||
|
public string ContentHtml { get; init; }
|
||||||
|
public string Title { get; init; }
|
||||||
|
public string SpoilerText { get; init; }
|
||||||
|
public bool Sensitive { get; init; }
|
||||||
|
public string Language { get; init; }
|
||||||
|
public string Url { get; init; }
|
||||||
|
public string InReplyTo { get; init; }
|
||||||
|
public string Context { get; init; }
|
||||||
|
public string Audience { get; init; }
|
||||||
|
public string QuoteUri { get; init; }
|
||||||
|
public DateTime Published { get; init; }
|
||||||
|
public DateTime? Updated { get; init; }
|
||||||
|
public IReadOnlyList<string> To { get; init; } = Array.Empty<string>();
|
||||||
|
public IReadOnlyList<string> Cc { get; init; } = Array.Empty<string>();
|
||||||
|
public IReadOnlyList<PostMention> Mentions { get; init; } = Array.Empty<PostMention>();
|
||||||
|
public IReadOnlyList<string> Tags { get; init; } = Array.Empty<string>();
|
||||||
|
public IReadOnlyList<PostMedia> Attachments { get; init; } = Array.Empty<PostMedia>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class NoteParser
|
||||||
|
{
|
||||||
|
static readonly string[] NoteTypes = { "Note", "Article", "Page", "Question", "Event", "Video", "Audio", "Image" };
|
||||||
|
const int MaxAttachments = 16;
|
||||||
|
const int MaxTags = 64;
|
||||||
|
|
||||||
|
public static NoteDocument Parse(JsonNode node)
|
||||||
|
{
|
||||||
|
if (node is not JsonObject note)
|
||||||
|
return default;
|
||||||
|
var type = Value(note, "type");
|
||||||
|
var id = Id(note);
|
||||||
|
if (!NoteTypes.Contains(type) || Origin.Of(id) == default)
|
||||||
|
return default;
|
||||||
|
|
||||||
|
var (contentHtml, language) = Content(note);
|
||||||
|
var summary = Value(note, "summary");
|
||||||
|
return new NoteDocument
|
||||||
|
{
|
||||||
|
Id = id,
|
||||||
|
Type = type,
|
||||||
|
AttributedTo = Attribution(note["attributedTo"]),
|
||||||
|
ContentHtml = ContentSanitizer.Html(contentHtml),
|
||||||
|
Title = Plain(Value(note, "name"), 500),
|
||||||
|
SpoilerText = Plain(summary, 500),
|
||||||
|
Sensitive = note["sensitive"] is JsonValue sensitive && sensitive.TryGetValue<bool>(out var s) && s
|
||||||
|
|| !string.IsNullOrWhiteSpace(summary),
|
||||||
|
Language = language,
|
||||||
|
Url = UrlOf(note["url"]) ?? id,
|
||||||
|
InReplyTo = Id(note["inReplyTo"]),
|
||||||
|
Context = Id(note["context"]) ?? Value(note, "conversation"),
|
||||||
|
Audience = Id(note["audience"]),
|
||||||
|
QuoteUri = Value(note, "quote") ?? Value(note, "quoteUrl") ?? Value(note, "quoteUri") ?? Value(note, "_misskey_quote"),
|
||||||
|
Published = Time(Value(note, "published")) ?? DateTime.UtcNow,
|
||||||
|
Updated = Time(Value(note, "updated")),
|
||||||
|
To = List(note["to"]),
|
||||||
|
Cc = List(note["cc"]),
|
||||||
|
Mentions = Tags(note, "Mention").Select(t => new PostMention { ActorURI = Value(t, "href"), Handle = Value(t, "name") })
|
||||||
|
.Where(m => Origin.Of(m.ActorURI) != default)
|
||||||
|
.DistinctBy(m => m.ActorURI)
|
||||||
|
.Take(MaxTags)
|
||||||
|
.ToList(),
|
||||||
|
Tags = Tags(note, "Hashtag").Select(t => Value(t, "name")?.TrimStart('#').ToLowerInvariant())
|
||||||
|
.Where(t => !string.IsNullOrEmpty(t))
|
||||||
|
.Distinct()
|
||||||
|
.Take(MaxTags)
|
||||||
|
.ToList(),
|
||||||
|
Attachments = Attachments(note)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static (string Html, string Language) Content(JsonObject note)
|
||||||
|
{
|
||||||
|
var content = Value(note, "content");
|
||||||
|
string language = default;
|
||||||
|
if (note["contentMap"] is JsonObject map && map.Count > 0)
|
||||||
|
{
|
||||||
|
var first = map.First();
|
||||||
|
language = first.Key;
|
||||||
|
if (string.IsNullOrEmpty(content) && first.Value is JsonValue value && value.TryGetValue<string>(out var mapped))
|
||||||
|
content = mapped;
|
||||||
|
}
|
||||||
|
if (string.IsNullOrEmpty(content) && Value(note, "_misskey_content") is { } misskey)
|
||||||
|
content = System.Net.WebUtility.HtmlEncode(misskey).Replace("\n", "<br>");
|
||||||
|
return (content, language);
|
||||||
|
}
|
||||||
|
|
||||||
|
static string Attribution(JsonNode node) => node switch
|
||||||
|
{
|
||||||
|
JsonArray array => array.OfType<JsonObject>().Where(o => Value(o, "type") is "Person" or "Service" or "Application" or "Organization").Select(Id)
|
||||||
|
.Concat(array.OfType<JsonValue>().Select(Id))
|
||||||
|
.Concat(array.OfType<JsonObject>().Where(o => Value(o, "type") == "Group").Select(Id))
|
||||||
|
.FirstOrDefault(id => id != default),
|
||||||
|
_ => Id(node)
|
||||||
|
};
|
||||||
|
|
||||||
|
static IEnumerable<JsonObject> Tags(JsonObject note, string type) =>
|
||||||
|
(note["tag"] switch
|
||||||
|
{
|
||||||
|
JsonArray array => array.OfType<JsonObject>(),
|
||||||
|
JsonObject single => new[] { single },
|
||||||
|
_ => Enumerable.Empty<JsonObject>()
|
||||||
|
}).Where(t => Value(t, "type") == type);
|
||||||
|
|
||||||
|
static List<PostMedia> Attachments(JsonObject note)
|
||||||
|
{
|
||||||
|
var items = note["attachment"] switch
|
||||||
|
{
|
||||||
|
JsonArray array => array.OfType<JsonObject>(),
|
||||||
|
JsonObject single => new[] { single },
|
||||||
|
_ => Enumerable.Empty<JsonObject>()
|
||||||
|
};
|
||||||
|
return items
|
||||||
|
.Where(a => Value(a, "type") is "Document" or "Image" or "Video" or "Audio")
|
||||||
|
.Select(a => new PostMedia
|
||||||
|
{
|
||||||
|
RemoteURL = UrlOf(a["url"]),
|
||||||
|
ContentType = Value(a, "mediaType"),
|
||||||
|
Description = Plain(Value(a, "name") ?? Value(a, "summary"), 1500),
|
||||||
|
Blurhash = Value(a, "blurhash"),
|
||||||
|
Width = Int(a["width"]),
|
||||||
|
Height = Int(a["height"]),
|
||||||
|
Focus = a["focalPoint"] is JsonArray focus && focus.Count == 2
|
||||||
|
? focus.Select(f => f is JsonValue v && v.TryGetValue<double>(out var d) ? (float)Math.Clamp(d, -1, 1) : 0f).ToArray()
|
||||||
|
: default
|
||||||
|
})
|
||||||
|
.Where(m => Origin.Of(m.RemoteURL) != default)
|
||||||
|
.Take(MaxAttachments)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
static string UrlOf(JsonNode node) => node switch
|
||||||
|
{
|
||||||
|
JsonValue value when value.TryGetValue<string>(out var text) => text,
|
||||||
|
JsonObject obj => Value(obj, "href") ?? Value(obj, "id"),
|
||||||
|
JsonArray array => array.Select(item => item is JsonObject link && Value(link, "mediaType") is { } mediaType && !mediaType.StartsWith("text/html")
|
||||||
|
? default
|
||||||
|
: UrlOf(item))
|
||||||
|
.FirstOrDefault(url => url != default) ?? array.Select(UrlOf).FirstOrDefault(url => url != default),
|
||||||
|
_ => default
|
||||||
|
};
|
||||||
|
|
||||||
|
static IReadOnlyList<string> List(JsonNode node) => node switch
|
||||||
|
{
|
||||||
|
JsonArray array => array.Select(Id).Where(id => id != default).Distinct(StringComparer.Ordinal).ToList(),
|
||||||
|
JsonNode single when Id(single) is { } id => new[] { id },
|
||||||
|
_ => Array.Empty<string>()
|
||||||
|
};
|
||||||
|
|
||||||
|
static DateTime? Time(string value) =>
|
||||||
|
DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var parsed)
|
||||||
|
? parsed.UtcDateTime
|
||||||
|
: default;
|
||||||
|
|
||||||
|
static int? Int(JsonNode node) =>
|
||||||
|
node is JsonValue value && value.TryGetValue<int>(out var number) && number is > 0 and < 100_000 ? number : (int?)null;
|
||||||
|
|
||||||
|
static string Plain(string value, int max)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(value))
|
||||||
|
return default;
|
||||||
|
value = value.Trim();
|
||||||
|
return value.Length <= max ? value : value[..max];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,10 @@ using System.Text.Json.Nodes;
|
|||||||
using PrivaPub.Federation.Actors;
|
using PrivaPub.Federation.Actors;
|
||||||
using PrivaPub.Federation.Signing;
|
using PrivaPub.Federation.Signing;
|
||||||
using PrivaPub.Infrastructure.Http;
|
using PrivaPub.Infrastructure.Http;
|
||||||
|
using PrivaPub.Infrastructure.Jobs;
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace PrivaPub.Federation.Outbox
|
namespace PrivaPub.Federation.Outbox
|
||||||
{
|
{
|
||||||
@@ -20,31 +24,38 @@ namespace PrivaPub.Federation.Outbox
|
|||||||
Task<IReadOnlyList<string>> FollowerInboxes(LocalActor actor, CancellationToken token);
|
Task<IReadOnlyList<string>> FollowerInboxes(LocalActor actor, CancellationToken token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public sealed record DeliveryPayload(string SignerId, LocalActorKind SignerKind, string Inbox, string Body);
|
||||||
|
|
||||||
public class DeliveryService : IDeliveryService
|
public class DeliveryService : IDeliveryService
|
||||||
{
|
{
|
||||||
readonly DbEntities _dbEntities;
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly IJobQueue _queue;
|
||||||
|
|
||||||
public DeliveryService(DbEntities dbEntities)
|
public DeliveryService(DbEntities dbEntities, IJobQueue queue)
|
||||||
{
|
{
|
||||||
_dbEntities = dbEntities;
|
_dbEntities = dbEntities;
|
||||||
|
_queue = queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Enqueue(LocalActor signer, IEnumerable<string> inboxes, JsonObject activity, CancellationToken token)
|
public async Task Enqueue(LocalActor signer, IEnumerable<string> inboxes, JsonObject activity, CancellationToken token)
|
||||||
{
|
{
|
||||||
var body = activity.ToJsonString();
|
var body = activity.ToJsonString();
|
||||||
var deliveries = inboxes
|
var activityId = activity["id"] is JsonValue id && id.TryGetValue<string>(out var text) ? text : default;
|
||||||
|
var jobs = inboxes
|
||||||
.Where(i => !string.IsNullOrEmpty(i) && !i.StartsWith(signer.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|
.Where(i => !string.IsNullOrEmpty(i) && !i.StartsWith(signer.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|
||||||
.Distinct(StringComparer.Ordinal)
|
.Distinct(StringComparer.Ordinal)
|
||||||
.Select(inbox => new Delivery
|
.Select(inbox => Uri.TryCreate(inbox, UriKind.Absolute, out var uri) ? (inbox, uri) : default)
|
||||||
|
.Where(target => target.uri != default)
|
||||||
|
.Select(target => new Job
|
||||||
{
|
{
|
||||||
SignerId = signer.Id,
|
Kind = JobKind.Deliver,
|
||||||
SignerKind = signer.Kind,
|
Host = target.uri.Host.ToLowerInvariant(),
|
||||||
InboxURL = inbox,
|
DedupeKey = activityId == default ? default : $"{activityId}|{target.inbox}",
|
||||||
Body = body
|
Payload = JsonSerializer.Serialize(new DeliveryPayload(signer.Id, signer.Kind, target.inbox, body))
|
||||||
})
|
})
|
||||||
.ToList();
|
.ToList();
|
||||||
if (deliveries.Count > 0)
|
if (jobs.Count > 0)
|
||||||
await DB.Default.SaveAsync(deliveries, token);
|
await _queue.EnqueueMany(jobs, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task EnqueueToFollowers(LocalActor signer, JsonObject activity, CancellationToken token, IEnumerable<string> extraInboxes = default)
|
public async Task EnqueueToFollowers(LocalActor signer, JsonObject activity, CancellationToken token, IEnumerable<string> extraInboxes = default)
|
||||||
@@ -64,118 +75,81 @@ namespace PrivaPub.Federation.Outbox
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class DeliveryWorker : BackgroundService
|
public class DeliveryJobHandler : IJobHandler
|
||||||
{
|
{
|
||||||
const int MaxAttempts = 8;
|
readonly ILocalActorService _actors;
|
||||||
static readonly TimeSpan Poll = TimeSpan.FromSeconds(3);
|
|
||||||
|
|
||||||
readonly IServiceProvider _services;
|
|
||||||
readonly IFederationHttp _http;
|
readonly IFederationHttp _http;
|
||||||
readonly ILogger<DeliveryWorker> _logger;
|
readonly IHostCircuitBreaker _breaker;
|
||||||
|
readonly ILogger<DeliveryJobHandler> _logger;
|
||||||
|
|
||||||
public DeliveryWorker(IServiceProvider services, IFederationHttp http, ILogger<DeliveryWorker> logger)
|
public DeliveryJobHandler(ILocalActorService actors, IFederationHttp http, IHostCircuitBreaker breaker, ILogger<DeliveryJobHandler> logger)
|
||||||
{
|
{
|
||||||
_services = services;
|
_actors = actors;
|
||||||
_http = http;
|
_http = http;
|
||||||
|
_breaker = breaker;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
public JobKind Kind => JobKind.Deliver;
|
||||||
{
|
public int Concurrency => 8;
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
public int MaxAttempts => 16;
|
||||||
{
|
public int PerHostLimit => 2;
|
||||||
try
|
|
||||||
{
|
|
||||||
await DeliverDue(stoppingToken);
|
|
||||||
}
|
|
||||||
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "{Worker} pass failed", nameof(DeliveryWorker));
|
|
||||||
}
|
|
||||||
await Task.Delay(Poll, stoppingToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task DeliverDue(CancellationToken token)
|
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
|
||||||
{
|
{
|
||||||
using var scope = _services.CreateScope();
|
var payload = JsonSerializer.Deserialize<DeliveryPayload>(job.Payload);
|
||||||
var dbEntities = scope.ServiceProvider.GetRequiredService<DbEntities>();
|
if (payload == default || !Uri.TryCreate(payload.Inbox, UriKind.Absolute, out var inbox) || !_http.IsAllowed(inbox))
|
||||||
var actors = scope.ServiceProvider.GetRequiredService<ILocalActorService>();
|
return JobOutcome.Dead("not a deliverable inbox");
|
||||||
var now = DateTime.UtcNow;
|
|
||||||
var due = await dbEntities.Deliveries
|
|
||||||
.Match(d => !d.DeliveredAt.HasValue && !d.AbandonedAt.HasValue && d.NextAttemptAt <= now)
|
|
||||||
.Sort(d => d.NextAttemptAt, Order.Ascending)
|
|
||||||
.Limit(20)
|
|
||||||
.ExecuteAsync(token);
|
|
||||||
|
|
||||||
foreach (var delivery in due)
|
var unavailableUntil = await _breaker.UnavailableUntil(job.Host, token);
|
||||||
{
|
if (unavailableUntil.HasValue)
|
||||||
var signer = await actors.FindById(delivery.SignerKind, delivery.SignerId, token);
|
return JobOutcome.Defer(unavailableUntil.Value, "the host is unavailable");
|
||||||
if (signer == default)
|
|
||||||
{
|
var signer = await _actors.FindById(payload.SignerKind, payload.SignerId, token);
|
||||||
delivery.AbandonedAt = DateTime.UtcNow;
|
if (signer == default)
|
||||||
delivery.LastError = "the signing actor no longer exists";
|
return JobOutcome.Dead("the signing actor no longer exists");
|
||||||
await DB.Default.SaveAsync(delivery, token);
|
|
||||||
continue;
|
var body = Encoding.UTF8.GetBytes(payload.Body);
|
||||||
}
|
using var request = new HttpRequestMessage(HttpMethod.Post, inbox) { Content = new ByteArrayContent(body) };
|
||||||
await Deliver(delivery, signer, token);
|
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(RemoteActorService.ActivityJson);
|
||||||
await DB.Default.SaveAsync(delivery, token);
|
HttpSignatures.Sign(request, signer, body);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task Deliver(Delivery delivery, LocalActor signer, CancellationToken token)
|
|
||||||
{
|
|
||||||
delivery.Attempts++;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!Uri.TryCreate(delivery.InboxURL, UriKind.Absolute, out var inbox) || !_http.IsAllowed(inbox))
|
|
||||||
{
|
|
||||||
delivery.AbandonedAt = DateTime.UtcNow;
|
|
||||||
delivery.LastError = "not a deliverable inbox";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var body = Encoding.UTF8.GetBytes(delivery.Body);
|
|
||||||
using var request = new HttpRequestMessage(HttpMethod.Post, inbox)
|
|
||||||
{
|
|
||||||
Content = new ByteArrayContent(body)
|
|
||||||
};
|
|
||||||
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(RemoteActorService.ActivityJson);
|
|
||||||
HttpSignatures.Sign(request, signer, body);
|
|
||||||
|
|
||||||
using var response = await _http.Send(request, token);
|
using var response = await _http.Send(request, token);
|
||||||
|
var status = (int)response.StatusCode;
|
||||||
if (response.IsSuccessStatusCode)
|
if (response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
delivery.DeliveredAt = DateTime.UtcNow;
|
await _breaker.Succeeded(job.Host, token);
|
||||||
delivery.LastError = default;
|
return JobOutcome.Done;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
if (response.StatusCode == HttpStatusCode.TooManyRequests)
|
||||||
delivery.LastError = $"{(int)response.StatusCode} {response.ReasonPhrase}";
|
|
||||||
if (response.StatusCode is HttpStatusCode.Gone or HttpStatusCode.NotFound or HttpStatusCode.BadRequest or HttpStatusCode.Forbidden)
|
|
||||||
{
|
{
|
||||||
delivery.AbandonedAt = DateTime.UtcNow;
|
var retryAfter = response.Headers.RetryAfter?.Delta ?? (response.Headers.RetryAfter?.Date - DateTimeOffset.UtcNow);
|
||||||
_logger.LogWarning("Delivery {Id} to {Inbox} abandoned: {Status}", delivery.ID, delivery.InboxURL, delivery.LastError);
|
return retryAfter > TimeSpan.Zero
|
||||||
return;
|
? JobOutcome.Defer(DateTime.UtcNow + Min(retryAfter.Value, TimeSpan.FromHours(6)), "429")
|
||||||
|
: JobOutcome.Retry("429");
|
||||||
}
|
}
|
||||||
|
if (status is >= 400 and < 500 && status != 408)
|
||||||
|
{
|
||||||
|
await _breaker.Succeeded(job.Host, token);
|
||||||
|
_logger.LogInformation("Delivery to {Inbox} refused with {Status}", payload.Inbox, status);
|
||||||
|
return JobOutcome.Dead($"{status} {response.ReasonPhrase}");
|
||||||
|
}
|
||||||
|
await _breaker.Failed(job.Host, $"{status}", token);
|
||||||
|
return JobOutcome.Retry($"{status} {response.ReasonPhrase}");
|
||||||
}
|
}
|
||||||
catch (Exception ex) when (ex is HttpRequestException or BlockedDestinationException or TaskCanceledException && !token.IsCancellationRequested)
|
catch (BlockedDestinationException ex)
|
||||||
{
|
{
|
||||||
delivery.LastError = ex.Message;
|
return JobOutcome.Dead(ex.Message);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException && !token.IsCancellationRequested)
|
||||||
if (delivery.Attempts >= MaxAttempts)
|
|
||||||
{
|
{
|
||||||
delivery.AbandonedAt = DateTime.UtcNow;
|
await _breaker.Failed(job.Host, ex.GetType().Name, token);
|
||||||
_logger.LogWarning("Delivery {Id} to {Inbox} abandoned after {Attempts} attempts: {Error}",
|
return JobOutcome.Retry(ex.Message);
|
||||||
delivery.ID, delivery.InboxURL, delivery.Attempts, delivery.LastError);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
delivery.NextAttemptAt = DateTime.UtcNow.AddMinutes(Math.Pow(2, delivery.Attempts));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static TimeSpan Min(TimeSpan a, TimeSpan b) => a < b ? a : b;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Rendering;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.StaticServices;
|
||||||
|
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
|
|
||||||
|
namespace PrivaPub.Federation.Outbox
|
||||||
|
{
|
||||||
|
public interface IOutboxPublisher
|
||||||
|
{
|
||||||
|
Task<IReadOnlyList<string>> Audience(LocalActor author, PostEntity post, CancellationToken token);
|
||||||
|
Task Publish(LocalActor author, PostEntity post, JsonObject activity, CancellationToken token);
|
||||||
|
Task PublishProfile(LocalActor actor, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class OutboxPublisher : IOutboxPublisher
|
||||||
|
{
|
||||||
|
readonly DbEntities _dbEntities;
|
||||||
|
readonly ILocalActorService _localActors;
|
||||||
|
readonly IDeliveryService _delivery;
|
||||||
|
|
||||||
|
public OutboxPublisher(DbEntities dbEntities, ILocalActorService localActors, IDeliveryService delivery)
|
||||||
|
{
|
||||||
|
_dbEntities = dbEntities;
|
||||||
|
_localActors = localActors;
|
||||||
|
_delivery = delivery;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IReadOnlyList<string>> Audience(LocalActor author, PostEntity post, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (post.Visibility is PostVisibility.Circle or PostVisibility.LocalGeo || post.IsLocalOnly)
|
||||||
|
return Array.Empty<string>();
|
||||||
|
|
||||||
|
var inboxes = new List<string>();
|
||||||
|
if (post.Visibility is PostVisibility.Public or PostVisibility.Unlisted or PostVisibility.FollowersOnly)
|
||||||
|
inboxes.AddRange(await _delivery.FollowerInboxes(author, token));
|
||||||
|
|
||||||
|
var addressed = post.Mentions.Where(m => !m.IsLocal).Select(m => m.ActorURI)
|
||||||
|
.Concat(post.Visibility == PostVisibility.Direct ? post.To.Concat(post.Cc) : Enumerable.Empty<string>())
|
||||||
|
.Where(uri => !uri.StartsWith(author.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|
||||||
|
.Distinct(StringComparer.Ordinal)
|
||||||
|
.ToList();
|
||||||
|
foreach (var uri in addressed)
|
||||||
|
{
|
||||||
|
var actor = await _dbEntities.ForeignAvatars.Match(a => a.ActorURI == uri).ExecuteFirstAsync(token);
|
||||||
|
if (actor != default && !string.IsNullOrEmpty(actor.InboxURL))
|
||||||
|
inboxes.Add(actor.InboxURL);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (post.Visibility != PostVisibility.Direct && !string.IsNullOrEmpty(post.InReplyToAccountId))
|
||||||
|
{
|
||||||
|
var parentAuthor = await _dbEntities.ForeignAvatars.MatchID(post.InReplyToAccountId).ExecuteFirstAsync(token);
|
||||||
|
if (parentAuthor != default && !string.IsNullOrEmpty(parentAuthor.InboxURL))
|
||||||
|
inboxes.Add(parentAuthor.InboxURL);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(post.GroupId) && post.Visibility is PostVisibility.Public or PostVisibility.Unlisted)
|
||||||
|
{
|
||||||
|
var group = await _localActors.FindById(LocalActorKind.Group, post.GroupId, token);
|
||||||
|
if (group is { IsFederated: true })
|
||||||
|
inboxes.AddRange(await _delivery.FollowerInboxes(group, token));
|
||||||
|
}
|
||||||
|
|
||||||
|
return inboxes.Where(i => !string.IsNullOrEmpty(i)).Distinct(StringComparer.Ordinal).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Publish(LocalActor author, PostEntity post, JsonObject activity, CancellationToken token)
|
||||||
|
{
|
||||||
|
var inboxes = await Audience(author, post, token);
|
||||||
|
if (inboxes.Count > 0)
|
||||||
|
await _delivery.Enqueue(author, inboxes, activity, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task PublishProfile(LocalActor actor, CancellationToken token)
|
||||||
|
{
|
||||||
|
var document = ActivityPubRenderer.Actor(actor);
|
||||||
|
document.Remove("@context");
|
||||||
|
var update = new JsonObject
|
||||||
|
{
|
||||||
|
["@context"] = ActivityPubRenderer.Context(),
|
||||||
|
["id"] = actor.ActivityUri($"update-profile-{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}"),
|
||||||
|
["type"] = "Update",
|
||||||
|
["actor"] = actor.Uri,
|
||||||
|
["to"] = new JsonArray(ActivityPubRenderer.Public),
|
||||||
|
["cc"] = new JsonArray(actor.Followers),
|
||||||
|
["object"] = document
|
||||||
|
};
|
||||||
|
await _delivery.EnqueueToFollowers(actor, update, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,59 +1,99 @@
|
|||||||
using Markdig;
|
using Markdig;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Actors;
|
||||||
|
using PrivaPub.Federation.Objects;
|
||||||
using PrivaPub.Models.Federation;
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using System.Net;
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
using PostEntity = PrivaPub.Models.Post.Post;
|
using PostEntity = PrivaPub.Models.Post.Post;
|
||||||
using DmPostEntity = PrivaPub.Models.Post.DmPost;
|
|
||||||
using PrivaPub.Federation.Actors;
|
|
||||||
|
|
||||||
namespace PrivaPub.Federation.Rendering
|
namespace PrivaPub.Federation.Rendering
|
||||||
{
|
{
|
||||||
public static class ActivityPubRenderer
|
public static class ActivityPubRenderer
|
||||||
{
|
{
|
||||||
public const string ActivityStreams = "https://www.w3.org/ns/activitystreams";
|
public const string ActivityStreams = "https://www.w3.org/ns/activitystreams";
|
||||||
public const string Public = "https://www.w3.org/ns/activitystreams#Public";
|
public const string Public = Addressing.Public;
|
||||||
|
public const string ContentWarning = "Content warning";
|
||||||
|
|
||||||
static readonly MarkdownPipeline Pipeline = new MarkdownPipelineBuilder().DisableHtml().Build();
|
static readonly MarkdownPipeline Pipeline = new MarkdownPipelineBuilder().DisableHtml().Build();
|
||||||
|
|
||||||
|
public static JsonArray Context() => new(
|
||||||
|
ActivityStreams,
|
||||||
|
"https://w3id.org/security/v1",
|
||||||
|
new JsonObject
|
||||||
|
{
|
||||||
|
["manuallyApprovesFollowers"] = "as:manuallyApprovesFollowers",
|
||||||
|
["sensitive"] = "as:sensitive",
|
||||||
|
["Hashtag"] = "as:Hashtag",
|
||||||
|
["alsoKnownAs"] = new JsonObject { ["@id"] = "as:alsoKnownAs", ["@type"] = "@id" },
|
||||||
|
["movedTo"] = new JsonObject { ["@id"] = "as:movedTo", ["@type"] = "@id" },
|
||||||
|
["toot"] = "http://joinmastodon.org/ns#",
|
||||||
|
["featured"] = new JsonObject { ["@id"] = "toot:featured", ["@type"] = "@id" },
|
||||||
|
["featuredTags"] = new JsonObject { ["@id"] = "toot:featuredTags", ["@type"] = "@id" },
|
||||||
|
["discoverable"] = "toot:discoverable",
|
||||||
|
["indexable"] = "toot:indexable",
|
||||||
|
["blurhash"] = "toot:blurhash",
|
||||||
|
["focalPoint"] = new JsonObject { ["@container"] = "@list", ["@id"] = "toot:focalPoint" },
|
||||||
|
["schema"] = "http://schema.org#",
|
||||||
|
["PropertyValue"] = "schema:PropertyValue",
|
||||||
|
["value"] = "schema:value",
|
||||||
|
["webfinger"] = "https://purl.archive.org/socialweb/webfinger#webfinger"
|
||||||
|
});
|
||||||
|
|
||||||
public static string Html(string markdown) =>
|
public static string Html(string markdown) =>
|
||||||
string.IsNullOrEmpty(markdown) ? string.Empty : Markdown.ToHtml(markdown, Pipeline).Trim();
|
string.IsNullOrEmpty(markdown) ? string.Empty : Markdown.ToHtml(markdown, Pipeline).Trim();
|
||||||
|
|
||||||
public static string Timestamp(DateTime value) =>
|
public static string Timestamp(DateTime value) =>
|
||||||
DateTime.SpecifyKind(value, DateTimeKind.Utc).ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
|
DateTime.SpecifyKind(value, DateTimeKind.Utc).ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
|
public static string Day(DateTime value) =>
|
||||||
|
DateTime.SpecifyKind(value, DateTimeKind.Utc).Date.ToString("yyyy-MM-ddT00:00:00Z", CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
|
public static string TagUrl(string baseAddress, string tag) => $"{baseAddress}/tags/{Uri.EscapeDataString(tag)}";
|
||||||
|
|
||||||
public static JsonObject Actor(LocalActor actor)
|
public static JsonObject Actor(LocalActor actor)
|
||||||
{
|
{
|
||||||
var document = new JsonObject
|
var document = new JsonObject
|
||||||
{
|
{
|
||||||
["@context"] = new JsonArray(ActivityStreams, "https://w3id.org/security/v1"),
|
["@context"] = Context(),
|
||||||
["id"] = actor.Uri,
|
["id"] = actor.Uri,
|
||||||
["type"] = actor.Kind switch
|
["type"] = actor.Kind switch
|
||||||
{
|
{
|
||||||
LocalActorKind.Group => "Group",
|
LocalActorKind.Group => "Group",
|
||||||
LocalActorKind.Application => "Application",
|
LocalActorKind.Application => "Application",
|
||||||
|
_ when actor.IsBot => "Service",
|
||||||
_ => "Person"
|
_ => "Person"
|
||||||
},
|
},
|
||||||
["preferredUsername"] = actor.UserName,
|
["preferredUsername"] = actor.UserName,
|
||||||
["name"] = actor.Name,
|
["name"] = actor.Name,
|
||||||
["summary"] = Html(actor.Summary),
|
["summary"] = Html(actor.Summary),
|
||||||
["url"] = actor.Uri,
|
["url"] = actor.Kind == LocalActorKind.Application ? actor.Uri : actor.HtmlUrl,
|
||||||
["inbox"] = actor.Inbox,
|
["inbox"] = actor.Inbox,
|
||||||
["outbox"] = actor.Outbox,
|
["outbox"] = actor.Outbox,
|
||||||
["followers"] = actor.Followers,
|
["followers"] = actor.Followers,
|
||||||
["following"] = actor.Following,
|
["following"] = actor.Following,
|
||||||
["published"] = Timestamp(actor.Published),
|
["published"] = Day(actor.Published),
|
||||||
["manuallyApprovesFollowers"] = actor.ManuallyApprovesFollowers,
|
["manuallyApprovesFollowers"] = actor.ManuallyApprovesFollowers,
|
||||||
["discoverable"] = actor.Discoverable,
|
["discoverable"] = actor.Discoverable,
|
||||||
|
["indexable"] = actor.Indexable,
|
||||||
|
["webfinger"] = actor.Handle,
|
||||||
["endpoints"] = new JsonObject { ["sharedInbox"] = actor.SharedInbox },
|
["endpoints"] = new JsonObject { ["sharedInbox"] = actor.SharedInbox },
|
||||||
["publicKey"] = new JsonObject
|
["publicKey"] = new JsonObject
|
||||||
{
|
{
|
||||||
["id"] = actor.KeyId,
|
["id"] = actor.KeyId,
|
||||||
["owner"] = actor.Uri,
|
["owner"] = actor.Uri,
|
||||||
["publicKeyPem"] = Keys.ToSubjectPublicKeyInfoPem(actor.PublicKeyPem)
|
["publicKeyPem"] = Keys.ToSubjectPublicKeyInfoPem(actor.PublicKeyPem)
|
||||||
}
|
},
|
||||||
|
["attachment"] = new JsonArray(actor.Fields.Select(field => (JsonNode)new JsonObject
|
||||||
|
{
|
||||||
|
["type"] = "PropertyValue",
|
||||||
|
["name"] = field.Key,
|
||||||
|
["value"] = FieldValue(field.Value)
|
||||||
|
}).ToArray())
|
||||||
};
|
};
|
||||||
if (!string.IsNullOrEmpty(actor.PictureURL))
|
if (!string.IsNullOrEmpty(actor.PictureURL))
|
||||||
document["icon"] = new JsonObject { ["type"] = "Image", ["url"] = actor.PictureURL };
|
document["icon"] = new JsonObject { ["type"] = "Image", ["url"] = actor.PictureURL };
|
||||||
@@ -64,63 +104,104 @@ namespace PrivaPub.Federation.Rendering
|
|||||||
|
|
||||||
public static JsonObject Note(PostEntity post, LocalActor author, LocalActor group, string inReplyTo)
|
public static JsonObject Note(PostEntity post, LocalActor author, LocalActor group, string inReplyTo)
|
||||||
{
|
{
|
||||||
var to = new JsonArray(Public);
|
var mentions = post.Mentions.Select(m => (JsonNode)m.ActorURI).ToArray();
|
||||||
var cc = new JsonArray(author.Followers);
|
var (to, cc) = post.Visibility switch
|
||||||
|
{
|
||||||
|
PostVisibility.Unlisted => (new JsonArray(author.Followers), new JsonArray(mentions.Prepend(Public).ToArray())),
|
||||||
|
PostVisibility.FollowersOnly => (new JsonArray(author.Followers), new JsonArray(mentions)),
|
||||||
|
PostVisibility.Direct => (new JsonArray(mentions), new JsonArray()),
|
||||||
|
_ => (new JsonArray(Public), new JsonArray(mentions.Prepend(author.Followers).ToArray()))
|
||||||
|
};
|
||||||
if (group != default)
|
if (group != default)
|
||||||
cc.Add(group.Uri);
|
cc.Add(group.Uri);
|
||||||
|
|
||||||
var note = NoteBody(author.PostUri(post.ID), author, post.Title, post.Text, post.HasContentWarning,
|
var note = NoteBody(post, author, to, cc, inReplyTo);
|
||||||
post.CreationDate, to, cc, inReplyTo);
|
|
||||||
if (group != default)
|
if (group != default)
|
||||||
note["audience"] = group.Uri;
|
note["audience"] = group.Uri;
|
||||||
return note;
|
return note;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JsonObject DirectNote(DmPostEntity post, LocalActor author, IReadOnlyList<(string Uri, string Handle)> recipients,
|
public static JsonObject DirectNote(PostEntity post, LocalActor author, IReadOnlyList<(string Uri, string Handle)> recipients,
|
||||||
string context)
|
string context)
|
||||||
{
|
{
|
||||||
var note = NoteBody(author.PostUri(post.ID), author, post.Title, post.Text, post.HasContentWarning,
|
var note = NoteBody(post, author, new JsonArray(recipients.Select(r => (JsonNode)r.Uri).ToArray()), new JsonArray(), post.InReplyToURI);
|
||||||
post.CreationDate, new JsonArray(recipients.Select(r => (JsonNode)r.Uri).ToArray()), new JsonArray(), default);
|
var named = post.Mentions.Select(m => m.ActorURI).ToHashSet();
|
||||||
note["tag"] = new JsonArray(recipients.Select(r => (JsonNode)new JsonObject
|
var tags = note["tag"]!.AsArray();
|
||||||
{
|
foreach (var recipient in recipients.Where(r => !named.Contains(r.Uri)))
|
||||||
["type"] = "Mention",
|
tags.Add(new JsonObject { ["type"] = "Mention", ["href"] = recipient.Uri, ["name"] = "@" + recipient.Handle });
|
||||||
["href"] = r.Uri,
|
var unmentioned = recipients.Where(r => !named.Contains(r.Uri)).ToList();
|
||||||
["name"] = "@" + r.Handle
|
if (unmentioned.Count > 0)
|
||||||
}).ToArray());
|
note["content"] = "<p>" + string.Join(" ", unmentioned.Select(r =>
|
||||||
note["content"] = string.Join(" ", recipients.Select(r => $"<span class=\"h-card\"><a href=\"{r.Uri}\" class=\"u-url mention\">@{r.Handle}</a></span>"))
|
$"<span class=\"h-card\" translate=\"no\"><a href=\"{WebUtility.HtmlEncode(r.Uri)}\" class=\"u-url mention\">@<span>{WebUtility.HtmlEncode(r.Handle.Split('@')[0])}</span></a></span>"))
|
||||||
+ " " + note["content"]!.GetValue<string>();
|
+ "</p>" + note["content"]!.GetValue<string>();
|
||||||
if (!string.IsNullOrEmpty(context))
|
if (!string.IsNullOrEmpty(context))
|
||||||
note["context"] = context;
|
note["context"] = context;
|
||||||
return note;
|
return note;
|
||||||
}
|
}
|
||||||
|
|
||||||
static JsonObject NoteBody(string id, LocalActor author, string title, string text, bool sensitive,
|
static JsonObject NoteBody(PostEntity post, LocalActor author, JsonArray to, JsonArray cc, string inReplyTo)
|
||||||
DateTime published, JsonArray to, JsonArray cc, string inReplyTo)
|
|
||||||
{
|
{
|
||||||
|
var content = post.ContentHtml ?? Html(post.Text);
|
||||||
|
if (!string.IsNullOrEmpty(post.Title))
|
||||||
|
content = $"<p><strong>{WebUtility.HtmlEncode(post.Title)}</strong></p>{content}";
|
||||||
|
|
||||||
var note = new JsonObject
|
var note = new JsonObject
|
||||||
{
|
{
|
||||||
["id"] = id,
|
["id"] = author.PostUri(post.ID),
|
||||||
["type"] = "Note",
|
["type"] = "Note",
|
||||||
["attributedTo"] = author.Uri,
|
["attributedTo"] = author.Uri,
|
||||||
["content"] = Html(text),
|
["content"] = content,
|
||||||
["published"] = Timestamp(published),
|
["published"] = Timestamp(post.CreationDate),
|
||||||
["url"] = id,
|
["url"] = author.PostHtmlUrl(post.ID),
|
||||||
["to"] = to,
|
["to"] = to,
|
||||||
["cc"] = cc,
|
["cc"] = cc,
|
||||||
["sensitive"] = sensitive
|
["sensitive"] = post.HasContentWarning,
|
||||||
|
["tag"] = new JsonArray(post.Mentions
|
||||||
|
.Select(m => (JsonNode)new JsonObject { ["type"] = "Mention", ["href"] = m.ActorURI, ["name"] = MentionName(m) })
|
||||||
|
.Concat(post.Tags.Select(t => (JsonNode)new JsonObject
|
||||||
|
{
|
||||||
|
["type"] = "Hashtag",
|
||||||
|
["href"] = TagUrl(author.BaseAddress, t),
|
||||||
|
["name"] = "#" + t
|
||||||
|
}))
|
||||||
|
.ToArray())
|
||||||
};
|
};
|
||||||
if (sensitive && !string.IsNullOrEmpty(title))
|
if (!string.IsNullOrEmpty(post.Language))
|
||||||
note["summary"] = title;
|
note["contentMap"] = new JsonObject { [post.Language] = content };
|
||||||
else if (!string.IsNullOrEmpty(title))
|
if (!string.IsNullOrEmpty(post.Title))
|
||||||
note["name"] = title;
|
note["name"] = post.Title;
|
||||||
|
var summary = post.SpoilerText ?? (post.HasContentWarning ? post.Title ?? ContentWarning : default);
|
||||||
|
if (!string.IsNullOrEmpty(summary))
|
||||||
|
{
|
||||||
|
note["summary"] = summary;
|
||||||
|
note["sensitive"] = true;
|
||||||
|
}
|
||||||
if (!string.IsNullOrEmpty(inReplyTo))
|
if (!string.IsNullOrEmpty(inReplyTo))
|
||||||
note["inReplyTo"] = inReplyTo;
|
note["inReplyTo"] = inReplyTo;
|
||||||
|
if (post.EditedAt.HasValue)
|
||||||
|
note["updated"] = Timestamp(post.EditedAt.Value);
|
||||||
return note;
|
return note;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static string MentionName(PostMention mention)
|
||||||
|
{
|
||||||
|
var handle = mention.Handle?.TrimStart('@');
|
||||||
|
if (string.IsNullOrEmpty(handle))
|
||||||
|
return "@" + mention.ActorURI;
|
||||||
|
return "@" + (handle.Contains('@') ? handle : $"{handle}@{new Uri(mention.ActorURI).Authority}");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string FieldValue(string value)
|
||||||
|
{
|
||||||
|
var encoded = WebUtility.HtmlEncode(value ?? string.Empty);
|
||||||
|
return Uri.TryCreate(value, UriKind.Absolute, out var link) && link.Scheme is "https" or "http"
|
||||||
|
? $"<a href=\"{encoded}\" target=\"_blank\" rel=\"nofollow noopener noreferrer me\" translate=\"no\">{encoded}</a>"
|
||||||
|
: encoded;
|
||||||
|
}
|
||||||
|
|
||||||
public static JsonObject Create(LocalActor actor, JsonObject note, string activityId) => new()
|
public static JsonObject Create(LocalActor actor, JsonObject note, string activityId) => new()
|
||||||
{
|
{
|
||||||
["@context"] = ActivityStreams,
|
["@context"] = Context(),
|
||||||
["id"] = actor.ActivityUri(activityId),
|
["id"] = actor.ActivityUri(activityId),
|
||||||
["type"] = "Create",
|
["type"] = "Create",
|
||||||
["actor"] = actor.Uri,
|
["actor"] = actor.Uri,
|
||||||
@@ -162,7 +243,7 @@ namespace PrivaPub.Federation.Rendering
|
|||||||
["object"] = follow.DeepClone()
|
["object"] = follow.DeepClone()
|
||||||
};
|
};
|
||||||
|
|
||||||
public static JsonObject OrderedCollection(string id, int totalItems, IEnumerable<JsonNode> items)
|
public static JsonObject OrderedCollection(string id, int totalItems, IEnumerable<JsonNode> items, string first = default)
|
||||||
{
|
{
|
||||||
var collection = new JsonObject
|
var collection = new JsonObject
|
||||||
{
|
{
|
||||||
@@ -171,9 +252,28 @@ namespace PrivaPub.Federation.Rendering
|
|||||||
["type"] = "OrderedCollection",
|
["type"] = "OrderedCollection",
|
||||||
["totalItems"] = totalItems
|
["totalItems"] = totalItems
|
||||||
};
|
};
|
||||||
|
if (first != default)
|
||||||
|
collection["first"] = first;
|
||||||
if (items != default)
|
if (items != default)
|
||||||
collection["orderedItems"] = new JsonArray(items.ToArray());
|
collection["orderedItems"] = new JsonArray(items.ToArray());
|
||||||
return collection;
|
return collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static JsonObject OrderedCollectionPage(string id, string partOf, IEnumerable<JsonNode> items, string next, string prev)
|
||||||
|
{
|
||||||
|
var page = new JsonObject
|
||||||
|
{
|
||||||
|
["@context"] = Context(),
|
||||||
|
["id"] = id,
|
||||||
|
["type"] = "OrderedCollectionPage",
|
||||||
|
["partOf"] = partOf,
|
||||||
|
["orderedItems"] = new JsonArray(items.ToArray())
|
||||||
|
};
|
||||||
|
if (next != default)
|
||||||
|
page["next"] = next;
|
||||||
|
if (prev != default)
|
||||||
|
page["prev"] = prev;
|
||||||
|
return page;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,9 @@ using MongoDB.Entities;
|
|||||||
|
|
||||||
using PrivaPub.Models.Federation;
|
using PrivaPub.Models.Federation;
|
||||||
using PrivaPub.Models.Group;
|
using PrivaPub.Models.Group;
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
using PrivaPub.Models.Post;
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.Social;
|
||||||
using PrivaPub.Models.User;
|
using PrivaPub.Models.User;
|
||||||
|
|
||||||
namespace PrivaPub.Infrastructure.Data
|
namespace PrivaPub.Infrastructure.Data
|
||||||
@@ -17,6 +19,8 @@ namespace PrivaPub.Infrastructure.Data
|
|||||||
await Plain<Post>(token, p => p.GroupUserId, p => p.ID);
|
await Plain<Post>(token, p => p.GroupUserId, p => p.ID);
|
||||||
await Plain<Post>(token, p => p.GroupId, p => p.ID);
|
await Plain<Post>(token, p => p.GroupId, p => p.ID);
|
||||||
await Plain<Post>(token, p => p.ActorURI);
|
await Plain<Post>(token, p => p.ActorURI);
|
||||||
|
await Plain<Post>(token, p => p.ConversationId, p => p.ID);
|
||||||
|
await Plain<Post>(token, p => p.InReplyToURI);
|
||||||
|
|
||||||
await Unique<DmPost>(p => p.ObjectURI, Builders<DmPost>.Filter.Type(p => p.ObjectURI, BsonType.String), token);
|
await Unique<DmPost>(p => p.ObjectURI, Builders<DmPost>.Filter.Type(p => p.ObjectURI, BsonType.String), token);
|
||||||
await Plain<DmPost>(token, p => p.GroupId, p => p.ID);
|
await Plain<DmPost>(token, p => p.GroupId, p => p.ID);
|
||||||
@@ -48,6 +52,43 @@ namespace PrivaPub.Infrastructure.Data
|
|||||||
await Plain<DmGroup>(token, g => g.ParticipantsKey);
|
await Plain<DmGroup>(token, g => g.ParticipantsKey);
|
||||||
|
|
||||||
await Plain<Delivery>(token, d => d.DeliveredAt, d => d.AbandonedAt, d => d.NextAttemptAt);
|
await Plain<Delivery>(token, d => d.DeliveredAt, d => d.AbandonedAt, d => d.NextAttemptAt);
|
||||||
|
|
||||||
|
await Plain<Job>(token, j => j.Kind, j => j.State, j => j.RunAt);
|
||||||
|
await Plain<Job>(token, j => j.State, j => j.LeasedUntil);
|
||||||
|
await Unique<Job>(j => j.DedupeKey, Builders<Job>.Filter.Type(j => j.DedupeKey, BsonType.String), token);
|
||||||
|
await DB.Default.Index<Job>()
|
||||||
|
.Key(j => j.FinishedAt, KeyType.Ascending)
|
||||||
|
.Option(o => o.ExpireAfter = TimeSpan.FromDays(7))
|
||||||
|
.CreateAsync(token);
|
||||||
|
await DB.Default.Index<Following>()
|
||||||
|
.Key(f => f.AvatarId, KeyType.Ascending)
|
||||||
|
.Key(f => f.TargetActorURI, KeyType.Ascending)
|
||||||
|
.Option(o => o.Unique = true)
|
||||||
|
.CreateAsync(token);
|
||||||
|
await Plain<Following>(token, f => f.TargetActorURI, f => f.State);
|
||||||
|
await Plain<Following>(token, f => f.TargetAccountId, f => f.State);
|
||||||
|
await Plain<Following>(token, f => f.FollowActivityURI);
|
||||||
|
await DB.Default.Index<TimelineEntry>()
|
||||||
|
.Key(e => e.AvatarId, KeyType.Ascending)
|
||||||
|
.Key(e => e.PostId, KeyType.Ascending)
|
||||||
|
.Option(o => o.Unique = true)
|
||||||
|
.CreateAsync(token);
|
||||||
|
await Plain<TimelineEntry>(token, e => e.PostId);
|
||||||
|
await Unique<Notification>(n => n.DedupeKey, Builders<Notification>.Filter.Type(n => n.DedupeKey, BsonType.String), token);
|
||||||
|
await Plain<Notification>(token, n => n.AvatarId, n => n.ID);
|
||||||
|
await DB.Default.Index<Favourite>()
|
||||||
|
.Key(f => f.AccountId, KeyType.Ascending)
|
||||||
|
.Key(f => f.PostId, KeyType.Ascending)
|
||||||
|
.Option(o => o.Unique = true)
|
||||||
|
.CreateAsync(token);
|
||||||
|
await Plain<Favourite>(token, f => f.ActivityURI);
|
||||||
|
await DB.Default.Index<Marker>()
|
||||||
|
.Key(m => m.AvatarId, KeyType.Ascending)
|
||||||
|
.Key(m => m.Timeline, KeyType.Ascending)
|
||||||
|
.Option(o => o.Unique = true)
|
||||||
|
.CreateAsync(token);
|
||||||
|
await Unique<DomainBlock>(b => b.Domain, Builders<DomainBlock>.Filter.Type(b => b.Domain, BsonType.String), token);
|
||||||
|
await Unique<RemoteInstance>(i => i.Host, Builders<RemoteInstance>.Filter.Type(i => i.Host, BsonType.String), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async Task Unique<T>(System.Linq.Expressions.Expression<Func<T, object>> key, FilterDefinition<T> partial,
|
static async Task Unique<T>(System.Linq.Expressions.Expression<Func<T, object>> key, FilterDefinition<T> partial,
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Outbox;
|
||||||
|
using PrivaPub.Models.Federation;
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
|
namespace PrivaPub.Infrastructure.Data.Migrations
|
||||||
|
{
|
||||||
|
public class _004_pending_deliveries_become_jobs : IMigration
|
||||||
|
{
|
||||||
|
public async Task UpgradeAsync()
|
||||||
|
{
|
||||||
|
var pending = await DB.Default.Find<Delivery>()
|
||||||
|
.Match(d => !d.DeliveredAt.HasValue && !d.AbandonedAt.HasValue)
|
||||||
|
.ExecuteAsync();
|
||||||
|
foreach (var delivery in pending)
|
||||||
|
{
|
||||||
|
if (Uri.TryCreate(delivery.InboxURL, UriKind.Absolute, out var inbox))
|
||||||
|
{
|
||||||
|
var activityId = JsonNode.Parse(delivery.Body)?["id"]?.GetValue<string>();
|
||||||
|
var job = new Job
|
||||||
|
{
|
||||||
|
Kind = JobKind.Deliver,
|
||||||
|
Host = inbox.Host.ToLowerInvariant(),
|
||||||
|
DedupeKey = activityId == default ? default : $"{activityId}|{delivery.InboxURL}",
|
||||||
|
Attempts = delivery.Attempts,
|
||||||
|
RunAt = delivery.NextAttemptAt,
|
||||||
|
Payload = JsonSerializer.Serialize(new DeliveryPayload(delivery.SignerId, delivery.SignerKind, delivery.InboxURL, delivery.Body))
|
||||||
|
};
|
||||||
|
if (!await DB.Default.Find<Job>().Match(j => j.DedupeKey == job.DedupeKey && job.DedupeKey != null).ExecuteAnyAsync())
|
||||||
|
await DB.Default.SaveAsync(job);
|
||||||
|
}
|
||||||
|
await DB.Default.Update<Delivery>().MatchID(delivery.ID)
|
||||||
|
.Modify(d => d.AbandonedAt, DateTime.UtcNow)
|
||||||
|
.Modify(d => d.LastError, "moved to the job queue")
|
||||||
|
.ExecuteAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Models.Post;
|
||||||
|
using PrivaPub.Models.User;
|
||||||
|
|
||||||
|
namespace PrivaPub.Infrastructure.Data.Migrations
|
||||||
|
{
|
||||||
|
public class _005_direct_posts_join_posts : IMigration
|
||||||
|
{
|
||||||
|
public async Task UpgradeAsync()
|
||||||
|
{
|
||||||
|
foreach (var dm in await DB.Default.Find<DmPost>().ExecuteAsync())
|
||||||
|
{
|
||||||
|
if (await DB.Default.Find<Post>().Match(p => p.ID == dm.ID || p.ObjectURI == dm.ObjectURI).ExecuteAnyAsync())
|
||||||
|
continue;
|
||||||
|
await DB.Default.SaveAsync(new Post
|
||||||
|
{
|
||||||
|
ID = dm.ID,
|
||||||
|
GroupUserId = dm.GroupUserId,
|
||||||
|
AuthorAccountId = dm.IsFederatedCopy ? await ForeignAccountId(dm.ActorURI) : dm.GroupUserId,
|
||||||
|
ConversationId = dm.GroupId,
|
||||||
|
Visibility = PostVisibility.Direct,
|
||||||
|
AnsweringToPostId = Local(dm.AnsweringToPostId),
|
||||||
|
InReplyToURI = Remote(dm.AnsweringToPostId),
|
||||||
|
Title = dm.Title,
|
||||||
|
Text = dm.Text,
|
||||||
|
ContentHtml = dm.ContentHtml,
|
||||||
|
ContentFormat = dm.ContentFormat,
|
||||||
|
Media = dm.Media,
|
||||||
|
HasContentWarning = dm.HasContentWarning,
|
||||||
|
IsFederatedCopy = dm.IsFederatedCopy,
|
||||||
|
ObjectURI = dm.ObjectURI,
|
||||||
|
ActorURI = dm.ActorURI,
|
||||||
|
Url = dm.ObjectURI,
|
||||||
|
CreationDate = dm.CreationDate,
|
||||||
|
UpdateDate = dm.UpdateDate
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var post in await DB.Default.Find<Post>().Match(p => p.Visibility != PostVisibility.Direct).ExecuteAsync())
|
||||||
|
{
|
||||||
|
var parent = Local(post.AnsweringToPostId) is { } parentId
|
||||||
|
? await DB.Default.Find<Post>().OneAsync(parentId)
|
||||||
|
: Remote(post.AnsweringToPostId) is { } parentUri
|
||||||
|
? await DB.Default.Find<Post>().Match(p => p.ObjectURI == parentUri).ExecuteFirstAsync()
|
||||||
|
: default;
|
||||||
|
await DB.Default.Update<Post>().MatchID(post.ID)
|
||||||
|
.Modify(p => p.Visibility, post.IsLocalOnly ? PostVisibility.Circle : PostVisibility.Public)
|
||||||
|
.Modify(p => p.AuthorAccountId, post.IsFederatedCopy ? await ForeignAccountId(post.ActorURI) : post.GroupUserId)
|
||||||
|
.Modify(p => p.AnsweringToPostId, parent?.ID)
|
||||||
|
.Modify(p => p.InReplyToURI, parent?.ObjectURI ?? Remote(post.AnsweringToPostId))
|
||||||
|
.Modify(p => p.InReplyToAccountId, parent?.AuthorAccountId ?? parent?.GroupUserId)
|
||||||
|
.Modify(p => p.Url, post.Url ?? post.ObjectURI)
|
||||||
|
.ExecuteAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static string Local(string reference) =>
|
||||||
|
string.IsNullOrEmpty(reference) || reference.Contains("://") ? default : reference;
|
||||||
|
|
||||||
|
static string Remote(string reference) =>
|
||||||
|
reference != default && reference.Contains("://") ? reference : default;
|
||||||
|
|
||||||
|
static async Task<string> ForeignAccountId(string actorUri) =>
|
||||||
|
(await DB.Default.Find<ForeignAvatar>().Match(a => a.ActorURI == actorUri).ExecuteFirstAsync())?.ID;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
using PrivaPub.Federation.Moderation;
|
||||||
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
@@ -41,14 +43,16 @@ namespace PrivaPub.Infrastructure.Http
|
|||||||
readonly IHttpClientFactory _httpClientFactory;
|
readonly IHttpClientFactory _httpClientFactory;
|
||||||
readonly IMemoryCache _cache;
|
readonly IMemoryCache _cache;
|
||||||
readonly IOptionsMonitor<FederationOptions> _options;
|
readonly IOptionsMonitor<FederationOptions> _options;
|
||||||
|
readonly IDomainBlocks _domainBlocks;
|
||||||
readonly ILogger<FederationHttp> _logger;
|
readonly ILogger<FederationHttp> _logger;
|
||||||
|
|
||||||
public FederationHttp(IHttpClientFactory httpClientFactory, IMemoryCache cache, IOptionsMonitor<FederationOptions> options,
|
public FederationHttp(IHttpClientFactory httpClientFactory, IMemoryCache cache, IOptionsMonitor<FederationOptions> options,
|
||||||
ILogger<FederationHttp> logger)
|
IDomainBlocks domainBlocks, ILogger<FederationHttp> logger)
|
||||||
{
|
{
|
||||||
_httpClientFactory = httpClientFactory;
|
_httpClientFactory = httpClientFactory;
|
||||||
_cache = cache;
|
_cache = cache;
|
||||||
_options = options;
|
_options = options;
|
||||||
|
_domainBlocks = domainBlocks;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,6 +60,8 @@ namespace PrivaPub.Infrastructure.Http
|
|||||||
{
|
{
|
||||||
if (target is not { IsAbsoluteUri: true } || !string.IsNullOrEmpty(target.UserInfo))
|
if (target is not { IsAbsoluteUri: true } || !string.IsNullOrEmpty(target.UserInfo))
|
||||||
return false;
|
return false;
|
||||||
|
if (_domainBlocks?.IsSuspended(target.Host) == true)
|
||||||
|
return false;
|
||||||
var options = _options.CurrentValue;
|
var options = _options.CurrentValue;
|
||||||
if (target.Scheme != Uri.UriSchemeHttps && !(options.AllowPlainHttp && target.Scheme == Uri.UriSchemeHttp))
|
if (target.Scheme != Uri.UriSchemeHttps && !(options.AllowPlainHttp && target.Scheme == Uri.UriSchemeHttp))
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using System.Buffers.Binary;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
|
namespace PrivaPub.Infrastructure.Ids
|
||||||
|
{
|
||||||
|
public static class PrivacyIds
|
||||||
|
{
|
||||||
|
public static string ForDay(DateTime when) => At(DateTime.SpecifyKind(when, DateTimeKind.Utc).Date);
|
||||||
|
|
||||||
|
public static string At(DateTime when)
|
||||||
|
{
|
||||||
|
var seconds = Math.Clamp(new DateTimeOffset(DateTime.SpecifyKind(when, DateTimeKind.Utc)).ToUnixTimeSeconds(), 0,
|
||||||
|
DateTimeOffset.UtcNow.AddDays(1).ToUnixTimeSeconds());
|
||||||
|
Span<byte> bytes = stackalloc byte[12];
|
||||||
|
BinaryPrimitives.WriteInt32BigEndian(bytes, (int)seconds);
|
||||||
|
RandomNumberGenerator.Fill(bytes[4..]);
|
||||||
|
return Convert.ToHexStringLower(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
namespace PrivaPub.Infrastructure.Jobs
|
||||||
|
{
|
||||||
|
public static class Backoff
|
||||||
|
{
|
||||||
|
public static TimeSpan After(int attempt) =>
|
||||||
|
TimeSpan.FromSeconds(Math.Pow(attempt, 4) + 15 + Random.Shared.Next(10) * (attempt + 1));
|
||||||
|
|
||||||
|
public static TimeSpan HostQuarantine(int consecutiveFailures, int threshold) =>
|
||||||
|
consecutiveFailures < threshold
|
||||||
|
? TimeSpan.Zero
|
||||||
|
: TimeSpan.FromHours(Math.Min(Math.Pow(2, consecutiveFailures - threshold), 24 * 7));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
|
||||||
|
namespace PrivaPub.Infrastructure.Jobs
|
||||||
|
{
|
||||||
|
public interface IHostCircuitBreaker
|
||||||
|
{
|
||||||
|
Task<DateTime?> UnavailableUntil(string host, CancellationToken token);
|
||||||
|
Task Succeeded(string host, CancellationToken token);
|
||||||
|
Task Failed(string host, string error, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class HostCircuitBreaker : IHostCircuitBreaker
|
||||||
|
{
|
||||||
|
public const int Threshold = 10;
|
||||||
|
static readonly TimeSpan CacheLifetime = TimeSpan.FromSeconds(30);
|
||||||
|
|
||||||
|
readonly IMemoryCache _cache;
|
||||||
|
|
||||||
|
public HostCircuitBreaker(IMemoryCache cache)
|
||||||
|
{
|
||||||
|
_cache = cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<DateTime?> UnavailableUntil(string host, CancellationToken token)
|
||||||
|
{
|
||||||
|
var instance = await Instance(host, token);
|
||||||
|
return instance?.UnavailableUntil > DateTime.UtcNow ? instance.UnavailableUntil : default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Succeeded(string host, CancellationToken token)
|
||||||
|
{
|
||||||
|
var instance = await Instance(host, token);
|
||||||
|
if (instance is not { ConsecutiveFailures: > 0 } && instance?.LastSuccessAt > DateTime.UtcNow.AddHours(-1))
|
||||||
|
return;
|
||||||
|
await DB.Default.Update<RemoteInstance>()
|
||||||
|
.Match(i => i.Host == host)
|
||||||
|
.Modify(i => i.ConsecutiveFailures, 0)
|
||||||
|
.Modify(i => i.UnavailableUntil, null)
|
||||||
|
.Modify(i => i.LastSuccessAt, DateTime.UtcNow)
|
||||||
|
.Option(o => o.IsUpsert = true)
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
_cache.Remove(Key(host));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Failed(string host, string error, CancellationToken token)
|
||||||
|
{
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
var instance = await DB.Default.UpdateAndGet<RemoteInstance>()
|
||||||
|
.Match(i => i.Host == host)
|
||||||
|
.Modify(b => b.Inc(i => i.ConsecutiveFailures, 1))
|
||||||
|
.Modify(i => i.LastFailureAt, now)
|
||||||
|
.Modify(i => i.LastError, error)
|
||||||
|
.Option(o => o.IsUpsert = true)
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
var quarantine = Backoff.HostQuarantine(instance.ConsecutiveFailures, Threshold);
|
||||||
|
if (quarantine > TimeSpan.Zero)
|
||||||
|
await DB.Default.Update<RemoteInstance>().MatchID(instance.ID)
|
||||||
|
.Modify(i => i.UnavailableUntil, now + quarantine)
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
_cache.Remove(Key(host));
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task<RemoteInstance> Instance(string host, CancellationToken token) =>
|
||||||
|
await _cache.GetOrCreateAsync(Key(host), async entry =>
|
||||||
|
{
|
||||||
|
entry.AbsoluteExpirationRelativeToNow = CacheLifetime;
|
||||||
|
return await DB.Default.Find<RemoteInstance>().Match(i => i.Host == host).ExecuteFirstAsync(token);
|
||||||
|
});
|
||||||
|
|
||||||
|
static string Key(string host) => "remote-instance:" + host;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
|
namespace PrivaPub.Infrastructure.Jobs
|
||||||
|
{
|
||||||
|
public sealed record JobOutcome(JobResult Result, string Error = default, DateTime? RetryAt = default)
|
||||||
|
{
|
||||||
|
public static readonly JobOutcome Done = new(JobResult.Done);
|
||||||
|
public static JobOutcome Retry(string error) => new(JobResult.Retry, error);
|
||||||
|
public static JobOutcome Dead(string error) => new(JobResult.Dead, error);
|
||||||
|
public static JobOutcome Defer(DateTime until, string error) => new(JobResult.Defer, error, until);
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum JobResult
|
||||||
|
{
|
||||||
|
Done,
|
||||||
|
Retry,
|
||||||
|
Dead,
|
||||||
|
Defer
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IJobQueue
|
||||||
|
{
|
||||||
|
Task<bool> Enqueue(JobKind kind, string payload, string host, string dedupeKey, CancellationToken token);
|
||||||
|
Task<int> EnqueueMany(IEnumerable<Job> jobs, CancellationToken token);
|
||||||
|
Task<Job> Lease(JobKind kind, IReadOnlyCollection<string> busyHosts, CancellationToken token);
|
||||||
|
Task Finish(Job job, JobOutcome outcome, int maxAttempts, CancellationToken token);
|
||||||
|
Task<long> Reap(CancellationToken token);
|
||||||
|
Task WaitForWork(JobKind kind, TimeSpan poll, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class JobQueue : IJobQueue
|
||||||
|
{
|
||||||
|
public static readonly TimeSpan LeaseTime = TimeSpan.FromMinutes(2);
|
||||||
|
|
||||||
|
readonly string _owner = $"{Environment.MachineName}:{Environment.ProcessId}";
|
||||||
|
readonly ConcurrentDictionary<JobKind, SemaphoreSlim> _signals = new();
|
||||||
|
|
||||||
|
public async Task<bool> Enqueue(JobKind kind, string payload, string host, string dedupeKey, CancellationToken token) =>
|
||||||
|
await EnqueueMany(new[] { new Job { Kind = kind, Payload = payload, Host = host, DedupeKey = dedupeKey } }, token) == 1;
|
||||||
|
|
||||||
|
public async Task<int> EnqueueMany(IEnumerable<Job> jobs, CancellationToken token)
|
||||||
|
{
|
||||||
|
var inserted = 0;
|
||||||
|
foreach (var job in jobs)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await DB.Default.SaveAsync(job, token);
|
||||||
|
inserted++;
|
||||||
|
Signal(job.Kind);
|
||||||
|
}
|
||||||
|
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inserted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Job> Lease(JobKind kind, IReadOnlyCollection<string> busyHosts, CancellationToken token)
|
||||||
|
{
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
var busy = busyHosts?.ToList() ?? new List<string>();
|
||||||
|
return await DB.Default.UpdateAndGet<Job>()
|
||||||
|
.Match(j => j.Kind == kind && j.State == JobState.Pending && j.RunAt <= now && !busy.Contains(j.Host))
|
||||||
|
.Modify(j => j.State, JobState.Running)
|
||||||
|
.Modify(j => j.LeasedUntil, now + LeaseTime)
|
||||||
|
.Modify(j => j.LeaseOwner, _owner)
|
||||||
|
.Modify(b => b.Inc(j => j.Attempts, 1))
|
||||||
|
.Option(o => o.Sort = Builders<Job>.Sort.Ascending(j => j.RunAt))
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Finish(Job job, JobOutcome outcome, int maxAttempts, CancellationToken token)
|
||||||
|
{
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
var update = DB.Default.Update<Job>().MatchID(job.ID)
|
||||||
|
.Modify(j => j.LeasedUntil, null)
|
||||||
|
.Modify(j => j.LeaseOwner, null)
|
||||||
|
.Modify(j => j.LastError, outcome.Error);
|
||||||
|
switch (outcome.Result)
|
||||||
|
{
|
||||||
|
case JobResult.Done:
|
||||||
|
update.Modify(j => j.State, JobState.Done).Modify(j => j.FinishedAt, now);
|
||||||
|
break;
|
||||||
|
case JobResult.Retry when job.Attempts < maxAttempts:
|
||||||
|
update.Modify(j => j.State, JobState.Pending).Modify(j => j.RunAt, now + Backoff.After(job.Attempts));
|
||||||
|
break;
|
||||||
|
case JobResult.Defer:
|
||||||
|
update.Modify(j => j.State, JobState.Pending)
|
||||||
|
.Modify(j => j.RunAt, outcome.RetryAt ?? now + Backoff.After(job.Attempts))
|
||||||
|
.Modify(b => b.Inc(j => j.Attempts, -1));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
update.Modify(j => j.State, JobState.Dead).Modify(j => j.FinishedAt, now);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
await update.ExecuteAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<long> Reap(CancellationToken token)
|
||||||
|
{
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
var result = await DB.Default.Update<Job>()
|
||||||
|
.Match(j => j.State == JobState.Running && j.LeasedUntil < now)
|
||||||
|
.Modify(j => j.State, JobState.Pending)
|
||||||
|
.Modify(j => j.RunAt, now)
|
||||||
|
.Modify(j => j.LeasedUntil, null)
|
||||||
|
.Modify(j => j.LeaseOwner, null)
|
||||||
|
.ExecuteAsync(token);
|
||||||
|
return result.ModifiedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task WaitForWork(JobKind kind, TimeSpan poll, CancellationToken token)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await SignalFor(kind).WaitAsync(poll, token);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException) when (token.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Signal(JobKind kind)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
SignalFor(kind).Release();
|
||||||
|
}
|
||||||
|
catch (SemaphoreFullException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SemaphoreSlim SignalFor(JobKind kind) => _signals.GetOrAdd(kind, _ => new SemaphoreSlim(0, 64));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
using PrivaPub.Models.Jobs;
|
||||||
|
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
|
namespace PrivaPub.Infrastructure.Jobs
|
||||||
|
{
|
||||||
|
public interface IJobHandler
|
||||||
|
{
|
||||||
|
JobKind Kind { get; }
|
||||||
|
int Concurrency { get; }
|
||||||
|
int MaxAttempts { get; }
|
||||||
|
int PerHostLimit { get; }
|
||||||
|
Task<JobOutcome> Handle(Job job, CancellationToken token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class JobWorker : BackgroundService
|
||||||
|
{
|
||||||
|
static readonly TimeSpan Poll = TimeSpan.FromSeconds(5);
|
||||||
|
static readonly TimeSpan ReapInterval = TimeSpan.FromSeconds(30);
|
||||||
|
|
||||||
|
readonly IJobQueue _queue;
|
||||||
|
readonly IEnumerable<IJobHandler> _handlers;
|
||||||
|
readonly ILogger<JobWorker> _logger;
|
||||||
|
|
||||||
|
public JobWorker(IJobQueue queue, IEnumerable<IJobHandler> handlers, ILogger<JobWorker> logger)
|
||||||
|
{
|
||||||
|
_queue = queue;
|
||||||
|
_handlers = handlers;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
var loops = _handlers.SelectMany(handler =>
|
||||||
|
{
|
||||||
|
var inFlight = new ConcurrentDictionary<string, int>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
return Enumerable.Range(0, handler.Concurrency).Select(_ => Run(handler, inFlight, stoppingToken));
|
||||||
|
}).Append(Reap(stoppingToken));
|
||||||
|
await Task.WhenAll(loops);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task Run(IJobHandler handler, ConcurrentDictionary<string, int> inFlight, CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
Job job;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var busy = inFlight.Where(h => h.Value >= handler.PerHostLimit).Select(h => h.Key).ToList();
|
||||||
|
job = await _queue.Lease(handler.Kind, busy, stoppingToken);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "{Worker} could not lease a {Kind} job", nameof(JobWorker), handler.Kind);
|
||||||
|
await Delay(Poll, stoppingToken);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (job == default)
|
||||||
|
{
|
||||||
|
await _queue.WaitForWork(handler.Kind, Poll, stoppingToken);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var host = job.Host ?? string.Empty;
|
||||||
|
inFlight.AddOrUpdate(host, 1, (_, count) => count + 1);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
JobOutcome outcome;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
outcome = await handler.Handle(job, stoppingToken);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "{Kind} job {Id} threw", handler.Kind, job.ID);
|
||||||
|
outcome = JobOutcome.Retry(ex.GetType().Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (outcome.Result != JobResult.Done && job.Attempts >= handler.MaxAttempts && outcome.Result == JobResult.Retry)
|
||||||
|
_logger.LogWarning("{Kind} job {Id} for {Host} is dead after {Attempts} attempts: {Error}",
|
||||||
|
handler.Kind, job.ID, job.Host, job.Attempts, outcome.Error);
|
||||||
|
await _queue.Finish(job, outcome, handler.MaxAttempts, CancellationToken.None);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "{Kind} job {Id} could not be finished; its lease will expire", handler.Kind, job.ID);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
inFlight.AddOrUpdate(host, 0, (_, count) => count - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task Reap(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var reaped = await _queue.Reap(stoppingToken);
|
||||||
|
if (reaped > 0)
|
||||||
|
_logger.LogWarning("{Worker} returned {Count} expired leases to the queue", nameof(JobWorker), reaped);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "{Worker} reaper failed", nameof(JobWorker));
|
||||||
|
}
|
||||||
|
await Delay(ReapInterval, stoppingToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async Task Delay(TimeSpan delay, CancellationToken token)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await Task.Delay(delay, token);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.AspNetCore.ResponseCompression;
|
using Microsoft.AspNetCore.ResponseCompression;
|
||||||
|
|
||||||
|
using OpenIddict.Validation.AspNetCore;
|
||||||
|
|
||||||
using PrivaPub.ClientModels;
|
using PrivaPub.ClientModels;
|
||||||
using PrivaPub.Extensions;
|
using PrivaPub.Extensions;
|
||||||
using PrivaPub.Models;
|
using PrivaPub.Models;
|
||||||
@@ -14,13 +16,22 @@ using PrivaPub.Services.ClientToServer.Public;
|
|||||||
using PrivaPub.Federation.Actors;
|
using PrivaPub.Federation.Actors;
|
||||||
using PrivaPub.Federation.Outbox;
|
using PrivaPub.Federation.Outbox;
|
||||||
using PrivaPub.Federation.Inbox;
|
using PrivaPub.Federation.Inbox;
|
||||||
|
using PrivaPub.Federation.Moderation;
|
||||||
|
using PrivaPub.Federation.Inbox.Handlers;
|
||||||
|
using PrivaPub.Domain.Content;
|
||||||
|
using PrivaPub.Domain.Social;
|
||||||
|
using PrivaPub.Domain.Statuses;
|
||||||
|
using PrivaPub.Domain.Timelines;
|
||||||
using PrivaPub.Infrastructure.Http;
|
using PrivaPub.Infrastructure.Http;
|
||||||
|
using PrivaPub.Infrastructure.Jobs;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace PrivaPub.Middleware
|
namespace PrivaPub.Middleware
|
||||||
{
|
{
|
||||||
public static class PrivaPubConfigurations
|
public static class PrivaPubConfigurations
|
||||||
{
|
{
|
||||||
|
const string SchemeSelector = "PrivaPub";
|
||||||
|
|
||||||
public static IServiceCollection PrivaPubAppSettingsConfiguration(this IServiceCollection service, IConfiguration configuration)
|
public static IServiceCollection PrivaPubAppSettingsConfiguration(this IServiceCollection service, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
return service
|
return service
|
||||||
@@ -47,11 +58,30 @@ namespace PrivaPub.Middleware
|
|||||||
SafeHttpHandlerFactory.Create(provider.GetRequiredService<IOptions<FederationOptions>>().Value));
|
SafeHttpHandlerFactory.Create(provider.GetRequiredService<IOptions<FederationOptions>>().Value));
|
||||||
return service
|
return service
|
||||||
.AddSingleton<IFederationHttp, FederationHttp>()
|
.AddSingleton<IFederationHttp, FederationHttp>()
|
||||||
|
.AddSingleton<IDomainBlocks, DomainBlocks>()
|
||||||
|
.AddSingleton<IContentRenderer, ContentRenderer>()
|
||||||
.AddSingleton<ILocalActorService, LocalActorService>()
|
.AddSingleton<ILocalActorService, LocalActorService>()
|
||||||
.AddSingleton<IRemoteActorService, RemoteActorService>()
|
.AddSingleton<IRemoteActorService, RemoteActorService>()
|
||||||
.AddSingleton<IDeliveryService, DeliveryService>()
|
.AddSingleton<IDeliveryService, DeliveryService>()
|
||||||
.AddTransient<IInboxService, InboxService>()
|
.AddSingleton<IOutboxPublisher, OutboxPublisher>()
|
||||||
.AddHostedService<DeliveryWorker>();
|
.AddSingleton<IFanout, Fanout>()
|
||||||
|
.AddSingleton<IInboxReceiver, InboxReceiver>()
|
||||||
|
.AddSingleton<IActivityHandler, FollowHandler>()
|
||||||
|
.AddSingleton<IActivityHandler, AcceptHandler>()
|
||||||
|
.AddSingleton<IActivityHandler, RejectHandler>()
|
||||||
|
.AddSingleton<IActivityHandler, UndoHandler>()
|
||||||
|
.AddSingleton<IActivityHandler, LikeHandler>()
|
||||||
|
.AddSingleton<IActivityHandler, AnnounceHandler>()
|
||||||
|
.AddSingleton<IActivityHandler, CreateHandler>()
|
||||||
|
.AddSingleton<IActivityHandler, DeleteHandler>()
|
||||||
|
.AddSingleton<IActivityHandler, UpdateHandler>()
|
||||||
|
.AddSingleton<IJobHandler, InboxProcessor>()
|
||||||
|
.AddSingleton<IRemotePosts, RemotePosts>()
|
||||||
|
.AddSingleton<IJobHandler, AncestorsJobHandler>()
|
||||||
|
.AddSingleton<IJobQueue, JobQueue>()
|
||||||
|
.AddSingleton<IHostCircuitBreaker, HostCircuitBreaker>()
|
||||||
|
.AddSingleton<IJobHandler, DeliveryJobHandler>()
|
||||||
|
.AddHostedService<JobWorker>();
|
||||||
}
|
}
|
||||||
public static IServiceCollection PrivaPubAuthServicesConfiguration(this IServiceCollection service, IConfiguration configuration)
|
public static IServiceCollection PrivaPubAuthServicesConfiguration(this IServiceCollection service, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
@@ -64,10 +94,14 @@ namespace PrivaPub.Middleware
|
|||||||
})
|
})
|
||||||
.AddAuthentication(options =>
|
.AddAuthentication(options =>
|
||||||
{
|
{
|
||||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
options.DefaultAuthenticateScheme = SchemeSelector;
|
||||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
options.DefaultChallengeScheme = SchemeSelector;
|
||||||
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
options.DefaultScheme = SchemeSelector;
|
||||||
})
|
})
|
||||||
|
.AddPolicyScheme(SchemeSelector, SchemeSelector, options => options.ForwardDefaultSelector = context =>
|
||||||
|
context.Request.Path.StartsWithSegments("/api")
|
||||||
|
? OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme
|
||||||
|
: JwtBearerDefaults.AuthenticationScheme)
|
||||||
.AddPrivaPubAuth(configuration)
|
.AddPrivaPubAuth(configuration)
|
||||||
.Services
|
.Services
|
||||||
.AddSingleton<AuthTokenManager>()
|
.AddSingleton<AuthTokenManager>()
|
||||||
@@ -103,6 +137,9 @@ namespace PrivaPub.Middleware
|
|||||||
.AddTransient<IPrivateAvatarUsersService, PrivateAvatarUsersService>()
|
.AddTransient<IPrivateAvatarUsersService, PrivateAvatarUsersService>()
|
||||||
.AddTransient<IGroupUsersService, GroupUsersService>()
|
.AddTransient<IGroupUsersService, GroupUsersService>()
|
||||||
.AddTransient<IPostsService, PostsService>()
|
.AddTransient<IPostsService, PostsService>()
|
||||||
|
.AddTransient<IStatusService, StatusService>()
|
||||||
|
.AddTransient<IFollowService, FollowService>()
|
||||||
|
.AddTransient<ITimelineService, TimelineService>()
|
||||||
.AddSingleton<AppConfigurationService>()
|
.AddSingleton<AppConfigurationService>()
|
||||||
.AddHttpContextAccessor()
|
.AddHttpContextAccessor()
|
||||||
.AddMemoryCache()
|
.AddMemoryCache()
|
||||||
@@ -129,6 +166,8 @@ namespace PrivaPub.Middleware
|
|||||||
[new OpenApiSecuritySchemeReference("Bearer", document)] = []
|
[new OpenApiSecuritySchemeReference("Bearer", document)] = []
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
.AddRazorPages(options => options.RootDirectory = "/Web/Pages")
|
||||||
|
.Services
|
||||||
.AddControllers(options => { options.Filters.Add<OperationCancelledExceptionFilter>(); })
|
.AddControllers(options => { options.Filters.Add<OperationCancelledExceptionFilter>(); })
|
||||||
.AddJsonOptions(options =>
|
.AddJsonOptions(options =>
|
||||||
{
|
{
|
||||||
@@ -150,7 +189,7 @@ namespace PrivaPub.Middleware
|
|||||||
configure.AllowAnyMethod()
|
configure.AllowAnyMethod()
|
||||||
.AllowAnyHeader()
|
.AllowAnyHeader()
|
||||||
.AllowAnyOrigin()
|
.AllowAnyOrigin()
|
||||||
.AllowAnyMethod()
|
.WithExposedHeaders("Link", "X-RateLimit-Remaining", "X-RateLimit-Reset")
|
||||||
.DisallowCredentials();
|
.DisallowCredentials();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
namespace PrivaPub.Models.Federation
|
||||||
|
{
|
||||||
|
public class DomainBlock : Entity
|
||||||
|
{
|
||||||
|
public string Domain { get; set; }
|
||||||
|
public DomainBlockSeverity Severity { get; set; }
|
||||||
|
public bool RejectMedia { get; set; }
|
||||||
|
public string PublicComment { get; set; }
|
||||||
|
public string PrivateComment { get; set; }
|
||||||
|
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum DomainBlockSeverity
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
Silence,
|
||||||
|
Suspend
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
using MongoDB.Entities;
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
using PrivaPub.Infrastructure.Ids;
|
||||||
|
|
||||||
namespace PrivaPub.Models.Group
|
namespace PrivaPub.Models.Group
|
||||||
{
|
{
|
||||||
public class Group : Entity
|
public class Group : Entity
|
||||||
@@ -29,6 +31,8 @@ namespace PrivaPub.Models.Group
|
|||||||
public DateTime CreationDate { get; set; } = DateTime.UtcNow;
|
public DateTime CreationDate { get; set; } = DateTime.UtcNow;
|
||||||
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
|
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
|
||||||
public DateTime? DeletionAt { get; set; }
|
public DateTime? DeletionAt { get; set; }
|
||||||
|
|
||||||
|
public override object GenerateNewID() => PrivacyIds.ForDay(DateTime.UtcNow);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class GroupMember
|
public class GroupMember
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
namespace PrivaPub.Models.Jobs
|
||||||
|
{
|
||||||
|
public class Job : Entity
|
||||||
|
{
|
||||||
|
public JobKind Kind { get; set; }
|
||||||
|
public JobState State { get; set; }
|
||||||
|
public string Payload { get; set; }
|
||||||
|
public string Host { get; set; }
|
||||||
|
public string DedupeKey { get; set; }
|
||||||
|
public int Attempts { get; set; }
|
||||||
|
public DateTime RunAt { get; set; } = DateTime.UtcNow;
|
||||||
|
public DateTime? LeasedUntil { get; set; }
|
||||||
|
public string LeaseOwner { get; set; }
|
||||||
|
public string LastError { get; set; }
|
||||||
|
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||||
|
public DateTime? FinishedAt { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum JobKind
|
||||||
|
{
|
||||||
|
Deliver,
|
||||||
|
ProcessInbox,
|
||||||
|
FetchAncestors
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum JobState
|
||||||
|
{
|
||||||
|
Pending,
|
||||||
|
Running,
|
||||||
|
Done,
|
||||||
|
Dead
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
namespace PrivaPub.Models.Jobs
|
||||||
|
{
|
||||||
|
public class RemoteInstance : Entity
|
||||||
|
{
|
||||||
|
public string Host { get; set; }
|
||||||
|
public int ConsecutiveFailures { get; set; }
|
||||||
|
public DateTime? UnavailableUntil { get; set; }
|
||||||
|
public DateTime? LastSuccessAt { get; set; }
|
||||||
|
public DateTime? LastFailureAt { get; set; }
|
||||||
|
public string LastError { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ namespace PrivaPub.Models.Post
|
|||||||
public enum ContentFormat
|
public enum ContentFormat
|
||||||
{
|
{
|
||||||
Markdown,
|
Markdown,
|
||||||
Html
|
Html,
|
||||||
|
Plain
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,13 +4,23 @@ namespace PrivaPub.Models.Post
|
|||||||
{
|
{
|
||||||
public class Post : Entity
|
public class Post : Entity
|
||||||
{
|
{
|
||||||
public string GroupUserId { get; set; }
|
public string GroupUserId { get; set; }//local author's Avatar.ID
|
||||||
public string AnsweringToPostId { get; set; }
|
public string AuthorAccountId { get; set; }//Avatar.ID or ForeignAvatar.ID
|
||||||
|
public string AnsweringToPostId { get; set; }//the parent's Post.ID when we hold it
|
||||||
|
public string InReplyToURI { get; set; }
|
||||||
|
public string InReplyToAccountId { get; set; }
|
||||||
public string GroupId { get; set; }
|
public string GroupId { get; set; }
|
||||||
|
public string ConversationId { get; set; }//DmGroup.ID of a direct post
|
||||||
|
public string ReblogOfPostId { get; set; }
|
||||||
|
public PostVisibility Visibility { get; set; }
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
|
public string SpoilerText { get; set; }
|
||||||
public string Text { get; set; }
|
public string Text { get; set; }
|
||||||
public string ContentHtml { get; set; }
|
public string ContentHtml { get; set; }
|
||||||
public ContentFormat ContentFormat { get; set; }
|
public ContentFormat ContentFormat { get; set; }
|
||||||
|
public string Language { get; set; }
|
||||||
|
public List<PostMention> Mentions { get; set; } = new();
|
||||||
|
public List<string> Tags { get; set; } = new();
|
||||||
public List<PostMedia> Media { get; set; } = new();
|
public List<PostMedia> Media { get; set; } = new();
|
||||||
public List<float> Location { get; set; } = new();
|
public List<float> Location { get; set; } = new();
|
||||||
public float RangeKm { get; set; } = 5.0f;
|
public float RangeKm { get; set; } = 5.0f;
|
||||||
@@ -19,10 +29,50 @@ namespace PrivaPub.Models.Post
|
|||||||
|
|
||||||
public bool IsFederatedCopy { get; set; }
|
public bool IsFederatedCopy { get; set; }
|
||||||
public string ObjectURI { get; set; }//the Note's id
|
public string ObjectURI { get; set; }//the Note's id
|
||||||
|
public string ActivityURI { get; set; }//the Create's id
|
||||||
public string ActorURI { get; set; }//attributedTo
|
public string ActorURI { get; set; }//attributedTo
|
||||||
|
public string Url { get; set; }
|
||||||
|
public string ContextURI { get; set; }
|
||||||
|
public string QuoteURI { get; set; }
|
||||||
|
public List<string> To { get; set; } = new();
|
||||||
|
public List<string> Cc { get; set; } = new();
|
||||||
|
|
||||||
|
public int RepliesCount { get; set; }
|
||||||
|
public int FavouritesCount { get; set; }
|
||||||
|
public int ReblogsCount { get; set; }
|
||||||
|
public List<PostRevision> Revisions { get; set; } = new();
|
||||||
|
|
||||||
public DateTime CreationDate { get; set; } = DateTime.UtcNow;
|
public DateTime CreationDate { get; set; } = DateTime.UtcNow;
|
||||||
public DateTime? UpdateDate { get; set; } = DateTime.UtcNow;
|
public DateTime? UpdateDate { get; set; } = DateTime.UtcNow;
|
||||||
|
public DateTime? EditedAt { get; set; }
|
||||||
|
public DateTime? DeletedAt { get; set; }
|
||||||
public string UpdateReason { get; set; }
|
public string UpdateReason { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum PostVisibility
|
||||||
|
{
|
||||||
|
Public,
|
||||||
|
Unlisted,
|
||||||
|
FollowersOnly,
|
||||||
|
Direct,
|
||||||
|
Circle,
|
||||||
|
LocalGeo
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PostMention
|
||||||
|
{
|
||||||
|
public string ActorURI { get; set; }
|
||||||
|
public string AccountId { get; set; }
|
||||||
|
public string Handle { get; set; }
|
||||||
|
public bool IsLocal { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PostRevision
|
||||||
|
{
|
||||||
|
public string Title { get; set; }
|
||||||
|
public string SpoilerText { get; set; }
|
||||||
|
public string ContentHtml { get; set; }
|
||||||
|
public bool HasContentWarning { get; set; }
|
||||||
|
public DateTime EditedAt { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,18 @@
|
|||||||
namespace PrivaPub.Models.Post
|
namespace PrivaPub.Models.Post
|
||||||
{
|
{
|
||||||
public class PostMedia
|
public class PostMedia
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; } = Guid.NewGuid();
|
public Guid Id { get; set; } = Guid.NewGuid();
|
||||||
public string ContentType { get; set; }
|
public string ContentType { get; set; }
|
||||||
public string FileName { get; set; }
|
public string FileName { get; set; }
|
||||||
public string Extension { get; set; }
|
public string Extension { get; set; }
|
||||||
public string Path { get; set; }
|
public string Path { get; set; }
|
||||||
public string URL { get; set; }
|
public string URL { get; set; }
|
||||||
|
public string RemoteURL { get; set; }
|
||||||
|
public string Description { get; set; }
|
||||||
|
public string Blurhash { get; set; }
|
||||||
|
public int? Width { get; set; }
|
||||||
|
public int? Height { get; set; }
|
||||||
|
public float[] Focus { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
namespace PrivaPub.Models.Social
|
||||||
|
{
|
||||||
|
public class Favourite : Entity
|
||||||
|
{
|
||||||
|
public string AccountId { get; set; }//Avatar.ID or ForeignAvatar.ID
|
||||||
|
public string ActorURI { get; set; }
|
||||||
|
public string PostId { get; set; }
|
||||||
|
public string ActivityURI { get; set; }
|
||||||
|
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
using MongoDB.Entities;
|
||||||
|
|
||||||
|
namespace PrivaPub.Models.Social
|
||||||
|
{
|
||||||
|
public class Following : Entity
|
||||||
|
{
|
||||||
|
public string AvatarId { get; set; }//the local follower
|
||||||
|
public string TargetActorURI { get; set; }
|
||||||
|
public string TargetAccountId { get; set; }//Avatar.ID, Group.ID or ForeignAvatar.ID
|
||||||
|
public bool TargetIsLocal { get; set; }
|
||||||
|
public string TargetInboxURL { get; set; }
|
||||||
|
public FollowState State { get; set; }
|
||||||
|
public string FollowActivityURI { get; set; }
|
||||||
|
public bool ShowReblogs { get; set; } = true;
|
||||||
|
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum FollowState
|
||||||
|
{
|
||||||
|
Requested,
|
||||||
|
Accepted
|
||||||
|
}
|
||||||
|
}
|
||||||
Loaded 100 of 124 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user