Basic incoming AP support.

This commit is contained in:
Roger Braun 2017-12-11 10:37:22 +01:00
parent 25118aeef7
commit 5599c5920c
5 changed files with 82 additions and 3 deletions

View File

@ -27,7 +27,8 @@
metadata: [:request_id]
config :mime, :types, %{
"application/xrd+xml" => ["xrd+xml"]
"application/xrd+xml" => ["xrd+xml"],
"application/activity+json" => ["activity+json"]
}
config :pleroma, :websub, Pleroma.Web.Websub

View File

@ -0,0 +1,17 @@
defmodule Pleroma.Web.ActivityPub.ActivityPubController do
use Pleroma.Web, :controller
alias Pleroma.{User, Repo}
alias Pleroma.Web.ActivityPub.UserView
alias Pleroma.Web.ActivityPub.ActivityPub
def user(conn, %{"nickname" => nickname}) do
with %User{} = user <- User.get_cached_by_nickname(nickname) do
json(conn, UserView.render("user.json", %{user: user}))
end
end
def inbox(conn, params) do
{:ok, activity} = ActivityPub.insert(params, false)
json(conn, "ok")
end
end

View File

@ -0,0 +1,51 @@
defmodule Pleroma.Web.ActivityPub.UserView do
use Pleroma.Web, :view
alias Pleroma.Web.Salmon
alias Pleroma.User
def render("user.json", %{user: user}) do
{:ok, _, public_key} = Salmon.keys_from_pem(user.info["keys"])
public_key = :public_key.pem_entry_encode(:RSAPublicKey, public_key)
public_key = :public_key.pem_encode([public_key])
%{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
%{
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"sensitive": "as:sensitive",
"Hashtag": "as:Hashtag",
"ostatus": "http://ostatus.org#",
"atomUri": "ostatus:atomUri",
"inReplyToAtomUri": "ostatus:inReplyToAtomUri",
"conversation": "ostatus:conversation",
"toot": "http://joinmastodon.org/ns#",
"Emoji": "toot:Emoji"
}
],
"id": user.ap_id,
"type": "Person",
"following": "#{user.ap_id}/following",
"followers": "#{user.ap_id}/followers",
"inbox": "#{user.ap_id}/inbox",
"outbox": "#{user.ap_id}/outbox",
"preferredUsername": user.nickname,
"name": user.name,
"summary": user.bio,
"url": user.ap_id,
"manuallyApprovesFollowers": false,
"publicKey": %{
"id": "#{user.ap_id}#main-key",
"owner": user.ap_id,
"publicKeyPem": public_key
},
"endpoints": %{
"sharedInbox": "#{Pleroma.Web.Endpoint.url}/inbox"
},
"icon": %{
"type": "Image",
"url": User.avatar_url(user)
}
}
end
end

View File

@ -6,13 +6,15 @@ defmodule Pleroma.Web.OStatus.OStatusController do
alias Pleroma.Repo
alias Pleroma.Web.{OStatus, Federator}
alias Pleroma.Web.XML
alias Pleroma.Web.ActivityPub.ActivityPubController
import Ecto.Query
def feed_redirect(conn, %{"nickname" => nickname}) do
def feed_redirect(conn, %{"nickname" => nickname} = params) do
user = User.get_cached_by_nickname(nickname)
case get_format(conn) do
"html" -> Fallback.RedirectController.redirector(conn, nil)
"activity+json" -> ActivityPubController.user(conn, params)
_ -> redirect conn, external: OStatus.feed_path(user)
end
end

View File

@ -199,7 +199,7 @@ def user_fetcher(username) do
end
pipeline :ostatus do
plug :accepts, ["xml", "atom", "html"]
plug :accepts, ["xml", "atom", "html", "activity+json"]
end
scope "/", Pleroma.Web do
@ -217,6 +217,14 @@ def user_fetcher(username) do
post "/push/subscriptions/:id", Websub.WebsubController, :websub_incoming
end
pipeline :activitypub do
plug :accepts, ["activity+json"]
end
scope "/", Pleroma.Web.ActivityPub do
post "/users/:nickname/inbox", ActivityPubController, :inbox
end
scope "/.well-known", Pleroma.Web do
pipe_through :well_known