diff options
-rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 7 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub_controller.ex | 49 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/utils.ex | 20 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/views/user_view.ex | 118 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 9 |
5 files changed, 188 insertions, 15 deletions
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 7b1207ce2..bdc1b5df7 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -185,6 +185,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do or_where: activity.actor == ^user.ap_id end + defp restrict_limit(query, %{"limit" => limit}) do + from activity in query, + limit: ^limit + end + defp restrict_limit(query, _), do: query + defp restrict_local(query, %{"local_only" => true}) do from activity in query, where: activity.local == true end @@ -248,6 +254,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do |> restrict_tag(opts) |> restrict_since(opts) |> restrict_local(opts) + |> restrict_limit(opts) |> restrict_max(opts) |> restrict_actor(opts) |> restrict_type(opts) diff --git a/lib/pleroma/web/activity_pub/activity_pub_controller.ex b/lib/pleroma/web/activity_pub/activity_pub_controller.ex index b9a70a33f..47a8bf5ab 100644 --- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex +++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex @@ -27,6 +27,55 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do end end + def following(conn, %{"nickname" => nickname, "page" => page}) do + with %User{} = user <- User.get_cached_by_nickname(nickname), + {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do + {page, _} = Integer.parse(page) + conn + |> put_resp_header("content-type", "application/activity+json") + |> json(UserView.render("following.json", %{user: user, page: page})) + end + end + + def following(conn, %{"nickname" => nickname}) do + with %User{} = user <- User.get_cached_by_nickname(nickname), + {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do + conn + |> put_resp_header("content-type", "application/activity+json") + |> json(UserView.render("following.json", %{user: user})) + end + end + + def followers(conn, %{"nickname" => nickname, "page" => page}) do + with %User{} = user <- User.get_cached_by_nickname(nickname), + {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do + {page, _} = Integer.parse(page) + conn + |> put_resp_header("content-type", "application/activity+json") + |> json(UserView.render("followers.json", %{user: user, page: page})) + end + end + + def followers(conn, %{"nickname" => nickname}) do + with %User{} = user <- User.get_cached_by_nickname(nickname), + {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do + conn + |> put_resp_header("content-type", "application/activity+json") + |> json(UserView.render("followers.json", %{user: user})) + end + end + + def outbox(conn, %{"nickname" => nickname, "max_id" => max_id}) do + with %User{} = user <- User.get_cached_by_nickname(nickname), + {:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do + conn + |> put_resp_header("content-type", "application/activity+json") + |> json(UserView.render("outbox.json", %{user: user, max_id: max_id})) + end + end + + def outbox(conn, %{"nickname" => nickname}) do outbox(conn, %{"nickname" => nickname, "max_id" => nil}) end + # TODO: Ensure that this inbox is a recipient of the message def inbox(%{assigns: %{valid_signature: true}} = conn, params) do Federator.enqueue(:incoming_ap_doc, params) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index cda106283..a25b27aab 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -5,6 +5,26 @@ defmodule Pleroma.Web.ActivityPub.Utils do alias Ecto.{Changeset, UUID} import Ecto.Query + def make_json_ld_header do + %{ + "@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" + } + ] + } + end + def make_date do DateTime.utc_now() |> DateTime.to_iso8601 end diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex index 179636884..5266dc7be 100644 --- a/lib/pleroma/web/activity_pub/views/user_view.ex +++ b/lib/pleroma/web/activity_pub/views/user_view.ex @@ -3,6 +3,9 @@ defmodule Pleroma.Web.ActivityPub.UserView do alias Pleroma.Web.Salmon alias Pleroma.Web.WebFinger alias Pleroma.User + alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.ActivityPub.Transmogrifier + alias Pleroma.Web.ActivityPub.Utils def render("user.json", %{user: user}) do {:ok, user} = WebFinger.ensure_keys_present(user) @@ -10,21 +13,6 @@ defmodule Pleroma.Web.ActivityPub.UserView do 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", @@ -53,5 +41,105 @@ defmodule Pleroma.Web.ActivityPub.UserView do "url" => User.banner_url(user) } } + |> Map.merge(Utils.make_json_ld_header()) + end + + def collection(collection, iri, page) do + offset = (page - 1) * 10 + items = Enum.slice(collection, offset, 10) + items = Enum.map(items, fn (user) -> user.ap_id end) + map = %{ + "id" => "#{iri}?page=#{page}", + "type" => "OrderedCollectionPage", + "partOf" => iri, + "totalItems" => length(collection), + "orderedItems" => items + } + if offset < length(collection) do + Map.put(map, "next", "#{iri}?page=#{page+1}") + end + end + + def render("following.json", %{user: user, page: page}) do + {:ok, following} = User.get_friends(user) + collection(following, "#{user.ap_id}/following", page) + |> Map.merge(Utils.make_json_ld_header()) + end + + def render("following.json", %{user: user}) do + {:ok, following} = User.get_friends(user) + %{ + "id" => "#{user.ap_id}/following", + "type" => "OrderedCollection", + "totalItems" => length(following), + "first" => collection(following, "#{user.ap_id}/following", 1) + } + |> Map.merge(Utils.make_json_ld_header()) + end + + def render("followers.json", %{user: user, page: page}) do + {:ok, followers} = User.get_followers(user) + collection(followers, "#{user.ap_id}/followers", page) + |> Map.merge(Utils.make_json_ld_header()) + end + + def render("followers.json", %{user: user}) do + {:ok, followers} = User.get_followers(user) + %{ + "id" => "#{user.ap_id}/following", + "type" => "OrderedCollection", + "totalItems" => length(followers), + "first" => collection(followers, "#{user.ap_id}/followers", 1) + } + |> Map.merge(Utils.make_json_ld_header()) + end + + def render("outbox.json", %{user: user, max_id: max_qid}) do + # XXX: technically note_count is wrong for this, but it's better than nothing + info = User.user_info(user) + + params = %{ + "type" => ["Create", "Announce"], + "actor_id" => user.ap_id, + "whole_db" => true, + "limit" => "10" + } + + if max_qid != nil do + params = Map.put(params, "max_id", max_qid) + end + + activities = ActivityPub.fetch_public_activities(params) + min_id = Enum.at(activities, 0).id + + activities = Enum.reverse(activities) + max_id = Enum.at(activities, 0).id + + collection = Enum.map(activities, fn (act) -> + {:ok, data} = Transmogrifier.prepare_outgoing(act.data) + data + end) + + iri = "#{user.ap_id}/outbox" + page = %{ + "id" => "#{iri}?max_id=#{max_id}", + "type" => "OrderedCollectionPage", + "partOf" => iri, + "totalItems" => info.note_count, + "orderedItems" => collection, + "next" => "#{iri}?max_id=#{min_id-1}", + } + + if max_qid == nil do + %{ + "id" => iri, + "type" => "OrderedCollection", + "totalItems" => info.note_count, + "first" => page + } + |> Map.merge(Utils.make_json_ld_header()) + else + page |> Map.merge(Utils.make_json_ld_header()) + end end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8835239c5..22a5257d5 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -249,6 +249,15 @@ defmodule Pleroma.Web.Router do plug Pleroma.Web.Plugs.HTTPSignaturePlug end + scope "/", Pleroma.Web.ActivityPub do + # XXX: not really ostatus + pipe_through :ostatus + + get "/users/:nickname/followers", ActivityPubController, :followers + get "/users/:nickname/following", ActivityPubController, :following + get "/users/:nickname/outbox", ActivityPubController, :outbox + end + if @federating do scope "/", Pleroma.Web.ActivityPub do pipe_through :activitypub |