X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fpleroma%2Fweb%2Factivity_pub%2Fviews%2Fuser_view.ex;h=d9c1bcb2c4998ae7b8dfe0c1ed3d197c7f82b19a;hb=1f6ac7680d1ae07be7c7dfd81a8cec2ba52f1c82;hp=f0c268755ceace9ae7ba3758856a33868eff0c21;hpb=bee6acd51dc4e84e44caecf9d123dfff2f640a38;p=akkoma
diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex
index f0c268755..d9c1bcb2c 100644
--- a/lib/pleroma/web/activity_pub/views/user_view.ex
+++ b/lib/pleroma/web/activity_pub/views/user_view.ex
@@ -1,25 +1,45 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2018 Pleroma Authors
+# Copyright © 2017-2019 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.UserView do
use Pleroma.Web, :view
- alias Pleroma.Web.Salmon
- alias Pleroma.Web.WebFinger
- alias Pleroma.User
+
+ alias Pleroma.Keys
alias Pleroma.Repo
+ alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Web.ActivityPub.Utils
+ alias Pleroma.Web.Endpoint
+ alias Pleroma.Web.Router.Helpers
+
import Ecto.Query
+ def render("endpoints.json", %{user: %User{nickname: nil, local: true} = _user}) do
+ %{"sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)}
+ end
+
+ def render("endpoints.json", %{user: %User{local: true} = _user}) do
+ %{
+ "oauthAuthorizationEndpoint" => Helpers.o_auth_url(Endpoint, :authorize),
+ "oauthRegistrationEndpoint" => Helpers.mastodon_api_url(Endpoint, :create_app),
+ "oauthTokenEndpoint" => Helpers.o_auth_url(Endpoint, :token_exchange),
+ "sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox)
+ }
+ end
+
+ def render("endpoints.json", _), do: %{}
+
# the instance itself is not a Person, but instead an Application
def render("user.json", %{user: %{nickname: nil} = user}) do
- {:ok, user} = WebFinger.ensure_keys_present(user)
- {:ok, _, public_key} = Salmon.keys_from_pem(user.info.keys)
+ {:ok, user} = User.ensure_keys_present(user)
+ {:ok, _, public_key} = Keys.keys_from_pem(user.info.keys)
public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
public_key = :public_key.pem_encode([public_key])
+ endpoints = render("endpoints.json", %{user: user})
+
%{
"id" => user.ap_id,
"type" => "Application",
@@ -35,19 +55,24 @@ defmodule Pleroma.Web.ActivityPub.UserView do
"owner" => user.ap_id,
"publicKeyPem" => public_key
},
- "endpoints" => %{
- "sharedInbox" => "#{Pleroma.Web.Endpoint.url()}/inbox"
- }
+ "endpoints" => endpoints
}
|> Map.merge(Utils.make_json_ld_header())
end
def render("user.json", %{user: user}) do
- {:ok, user} = WebFinger.ensure_keys_present(user)
- {:ok, _, public_key} = Salmon.keys_from_pem(user.info.keys)
+ {:ok, user} = User.ensure_keys_present(user)
+ {:ok, _, public_key} = Keys.keys_from_pem(user.info.keys)
public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
public_key = :public_key.pem_encode([public_key])
+ endpoints = render("endpoints.json", %{user: user})
+
+ user_tags =
+ user
+ |> Transmogrifier.add_emoji_tags()
+ |> Map.get("tag", [])
+
%{
"id" => user.ap_id,
"type" => "Person",
@@ -65,72 +90,105 @@ defmodule Pleroma.Web.ActivityPub.UserView do
"owner" => user.ap_id,
"publicKeyPem" => public_key
},
- "endpoints" => %{
- "sharedInbox" => "#{Pleroma.Web.Endpoint.url()}/inbox"
- },
- "icon" => %{
- "type" => "Image",
- "url" => User.avatar_url(user)
- },
- "image" => %{
- "type" => "Image",
- "url" => User.banner_url(user)
- },
- "tag" => user.info.source_data["tag"] || []
+ "endpoints" => endpoints,
+ "tag" => (user.info.source_data["tag"] || []) ++ user_tags
}
+ |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
+ |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
|> Map.merge(Utils.make_json_ld_header())
end
- def render("following.json", %{user: user, page: page}) do
+ def render("following.json", %{user: user, page: page} = opts) do
+ showing = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
query = User.get_friends_query(user)
query = from(user in query, select: [:ap_id])
following = Repo.all(query)
- collection(following, "#{user.ap_id}/following", page, !user.info.hide_network)
+ total =
+ if showing do
+ length(following)
+ else
+ 0
+ end
+
+ collection(following, "#{user.ap_id}/following", page, showing, total)
|> Map.merge(Utils.make_json_ld_header())
end
- def render("following.json", %{user: user}) do
+ def render("following.json", %{user: user} = opts) do
+ showing = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
query = User.get_friends_query(user)
query = from(user in query, select: [:ap_id])
following = Repo.all(query)
+ total =
+ if showing do
+ length(following)
+ else
+ 0
+ end
+
%{
"id" => "#{user.ap_id}/following",
"type" => "OrderedCollection",
- "totalItems" => length(following),
- "first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_network)
+ "totalItems" => total,
+ "first" =>
+ if showing do
+ collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
+ else
+ "#{user.ap_id}/following?page=1"
+ end
}
|> Map.merge(Utils.make_json_ld_header())
end
- def render("followers.json", %{user: user, page: page}) do
+ def render("followers.json", %{user: user, page: page} = opts) do
+ showing = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
+
query = User.get_followers_query(user)
query = from(user in query, select: [:ap_id])
followers = Repo.all(query)
- collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_network)
+ total =
+ if showing do
+ length(followers)
+ else
+ 0
+ end
+
+ collection(followers, "#{user.ap_id}/followers", page, showing, total)
|> Map.merge(Utils.make_json_ld_header())
end
- def render("followers.json", %{user: user}) do
+ def render("followers.json", %{user: user} = opts) do
+ showing = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
+
query = User.get_followers_query(user)
query = from(user in query, select: [:ap_id])
followers = Repo.all(query)
+ total =
+ if showing do
+ length(followers)
+ else
+ 0
+ end
+
%{
"id" => "#{user.ap_id}/followers",
"type" => "OrderedCollection",
- "totalItems" => length(followers),
- "first" => collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_network)
+ "totalItems" => total,
+ "first" =>
+ if showing do
+ collection(followers, "#{user.ap_id}/followers", 1, showing, total)
+ else
+ "#{user.ap_id}/followers?page=1"
+ end
}
|> 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 = %{
"limit" => "10"
}
@@ -143,6 +201,61 @@ defmodule Pleroma.Web.ActivityPub.UserView do
end
activities = ActivityPub.fetch_user_activities(user, nil, params)
+
+ {max_id, min_id, collection} =
+ if length(activities) > 0 do
+ {
+ Enum.at(Enum.reverse(activities), 0).id,
+ Enum.at(activities, 0).id,
+ Enum.map(activities, fn act ->
+ {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
+ data
+ end)
+ }
+ else
+ {
+ 0,
+ 0,
+ []
+ }
+ end
+
+ iri = "#{user.ap_id}/outbox"
+
+ page = %{
+ "id" => "#{iri}?max_id=#{max_id}",
+ "type" => "OrderedCollectionPage",
+ "partOf" => iri,
+ "orderedItems" => collection,
+ "next" => "#{iri}?max_id=#{min_id}"
+ }
+
+ if max_qid == nil do
+ %{
+ "id" => iri,
+ "type" => "OrderedCollection",
+ "first" => page
+ }
+ |> Map.merge(Utils.make_json_ld_header())
+ else
+ page |> Map.merge(Utils.make_json_ld_header())
+ end
+ end
+
+ def render("inbox.json", %{user: user, max_id: max_qid}) do
+ params = %{
+ "limit" => "10"
+ }
+
+ params =
+ if max_qid != nil do
+ Map.put(params, "max_id", max_qid)
+ else
+ params
+ end
+
+ activities = ActivityPub.fetch_activities([user.ap_id | user.following], params)
+
min_id = Enum.at(Enum.reverse(activities), 0).id
max_id = Enum.at(activities, 0).id
@@ -152,22 +265,20 @@ defmodule Pleroma.Web.ActivityPub.UserView do
data
end)
- iri = "#{user.ap_id}/outbox"
+ iri = "#{user.ap_id}/inbox"
page = %{
"id" => "#{iri}?max_id=#{max_id}",
"type" => "OrderedCollectionPage",
"partOf" => iri,
- "totalItems" => info.note_count,
"orderedItems" => collection,
- "next" => "#{iri}?max_id=#{min_id - 1}"
+ "next" => "#{iri}?max_id=#{min_id}"
}
if max_qid == nil do
%{
"id" => iri,
"type" => "OrderedCollection",
- "totalItems" => info.note_count,
"first" => page
}
|> Map.merge(Utils.make_json_ld_header())
@@ -192,6 +303,21 @@ defmodule Pleroma.Web.ActivityPub.UserView do
if offset < total do
Map.put(map, "next", "#{iri}?page=#{page + 1}")
+ else
+ map
+ end
+ end
+
+ defp maybe_make_image(func, key, user) do
+ if image = func.(user, no_default: true) do
+ %{
+ key => %{
+ "type" => "Image",
+ "url" => image
+ }
+ }
+ else
+ %{}
end
end
end