2018-12-23 21:04:54 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 16:41:47 +01:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 21:04:54 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-12-11 10:37:22 +01:00
|
|
|
defmodule Pleroma.Web.ActivityPub.UserView do
|
|
|
|
use Pleroma.Web, :view
|
2019-02-06 20:20:02 +01:00
|
|
|
|
2019-05-22 05:58:15 +02:00
|
|
|
alias Pleroma.Keys
|
2019-02-09 16:16:26 +01:00
|
|
|
alias Pleroma.Repo
|
2019-03-05 03:52:23 +01:00
|
|
|
alias Pleroma.User
|
2019-02-09 16:16:26 +01:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
|
|
|
alias Pleroma.Web.ActivityPub.Utils
|
2019-02-13 20:20:41 +01:00
|
|
|
alias Pleroma.Web.Endpoint
|
2019-03-05 03:52:23 +01:00
|
|
|
alias Pleroma.Web.Router.Helpers
|
2019-02-06 20:20:02 +01:00
|
|
|
|
2018-03-31 20:02:09 +02:00
|
|
|
import Ecto.Query
|
2017-12-11 10:37:22 +01:00
|
|
|
|
2019-02-14 03:41:21 +01:00
|
|
|
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
|
2019-02-12 22:28:11 +01:00
|
|
|
%{
|
2019-02-13 20:20:41 +01:00
|
|
|
"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)
|
2019-02-12 22:28:11 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-02-13 20:20:41 +01:00
|
|
|
def render("endpoints.json", _), do: %{}
|
2019-02-12 22:28:11 +01:00
|
|
|
|
2019-07-17 19:34:57 +02:00
|
|
|
def render("service.json", %{user: user}) do
|
2019-05-22 05:58:15 +02:00
|
|
|
{:ok, user} = User.ensure_keys_present(user)
|
|
|
|
{:ok, _, public_key} = Keys.keys_from_pem(user.info.keys)
|
2018-08-06 08:11:51 +02:00
|
|
|
public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
|
|
|
|
public_key = :public_key.pem_encode([public_key])
|
|
|
|
|
2019-02-12 22:28:11 +01:00
|
|
|
endpoints = render("endpoints.json", %{user: user})
|
|
|
|
|
2018-08-06 08:11:51 +02:00
|
|
|
%{
|
|
|
|
"id" => user.ap_id,
|
|
|
|
"type" => "Application",
|
2018-08-06 10:15:18 +02:00
|
|
|
"following" => "#{user.ap_id}/following",
|
|
|
|
"followers" => "#{user.ap_id}/followers",
|
2018-08-06 08:11:51 +02:00
|
|
|
"inbox" => "#{user.ap_id}/inbox",
|
|
|
|
"name" => "Pleroma",
|
2019-07-17 19:34:57 +02:00
|
|
|
"summary" =>
|
|
|
|
"An internal service actor for this Pleroma instance. No user-serviceable parts inside.",
|
2018-08-06 08:11:51 +02:00
|
|
|
"url" => user.ap_id,
|
|
|
|
"manuallyApprovesFollowers" => false,
|
|
|
|
"publicKey" => %{
|
|
|
|
"id" => "#{user.ap_id}#main-key",
|
|
|
|
"owner" => user.ap_id,
|
|
|
|
"publicKeyPem" => public_key
|
|
|
|
},
|
2019-02-12 22:28:11 +01:00
|
|
|
"endpoints" => endpoints
|
2018-08-06 08:11:51 +02:00
|
|
|
}
|
2018-11-08 17:51:48 +01:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
2018-08-06 08:11:51 +02:00
|
|
|
end
|
|
|
|
|
2019-07-17 19:34:57 +02:00
|
|
|
# the instance itself is not a Person, but instead an Application
|
|
|
|
def render("user.json", %{user: %User{nickname: nil} = user}),
|
|
|
|
do: render("service.json", %{user: user})
|
|
|
|
|
|
|
|
def render("user.json", %{user: %User{nickname: "internal." <> _} = user}),
|
|
|
|
do: render("service.json", %{user: user})
|
|
|
|
|
2017-12-11 10:37:22 +01:00
|
|
|
def render("user.json", %{user: user}) do
|
2019-05-22 05:58:15 +02:00
|
|
|
{:ok, user} = User.ensure_keys_present(user)
|
|
|
|
{:ok, _, public_key} = Keys.keys_from_pem(user.info.keys)
|
2018-07-12 04:45:48 +02:00
|
|
|
public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
|
2017-12-11 10:37:22 +01:00
|
|
|
public_key = :public_key.pem_encode([public_key])
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2019-02-12 22:28:11 +01:00
|
|
|
endpoints = render("endpoints.json", %{user: user})
|
|
|
|
|
2019-02-12 14:59:34 +01:00
|
|
|
user_tags =
|
|
|
|
user
|
|
|
|
|> Transmogrifier.add_emoji_tags()
|
|
|
|
|> Map.get("tag", [])
|
|
|
|
|
2017-12-11 10:37:22 +01:00
|
|
|
%{
|
2017-12-11 18:20:41 +01:00
|
|
|
"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,
|
2018-11-20 20:12:39 +01:00
|
|
|
"manuallyApprovesFollowers" => user.info.locked,
|
2017-12-11 18:20:41 +01:00
|
|
|
"publicKey" => %{
|
|
|
|
"id" => "#{user.ap_id}#main-key",
|
|
|
|
"owner" => user.ap_id,
|
|
|
|
"publicKeyPem" => public_key
|
2017-12-11 10:37:22 +01:00
|
|
|
},
|
2019-02-12 22:28:11 +01:00
|
|
|
"endpoints" => endpoints,
|
2019-02-12 14:59:34 +01:00
|
|
|
"tag" => (user.info.source_data["tag"] || []) ++ user_tags
|
2017-12-11 10:37:22 +01:00
|
|
|
}
|
2019-03-26 16:40:09 +01:00
|
|
|
|> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
|
|
|
|
|> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
|
2018-03-21 18:23:27 +01:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2019-07-12 18:41:05 +02:00
|
|
|
def render("following.json", %{user: user, page: page} = opts) do
|
|
|
|
showing = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
|
2018-03-31 20:02:09 +02:00
|
|
|
query = User.get_friends_query(user)
|
|
|
|
query = from(user in query, select: [:ap_id])
|
|
|
|
following = Repo.all(query)
|
2019-02-14 04:13:07 +01:00
|
|
|
|
2019-02-14 04:03:41 +01:00
|
|
|
total =
|
2019-07-12 18:41:05 +02:00
|
|
|
if showing do
|
2019-02-14 04:03:41 +01:00
|
|
|
length(following)
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2019-07-12 18:41:05 +02:00
|
|
|
collection(following, "#{user.ap_id}/following", page, showing, total)
|
2018-03-21 18:23:27 +01:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2019-07-12 18:41:05 +02:00
|
|
|
def render("following.json", %{user: user} = opts) do
|
|
|
|
showing = (opts[:for] && opts[:for] == user) || !user.info.hide_follows
|
2018-03-31 20:02:09 +02:00
|
|
|
query = User.get_friends_query(user)
|
|
|
|
query = from(user in query, select: [:ap_id])
|
|
|
|
following = Repo.all(query)
|
2019-02-14 04:13:07 +01:00
|
|
|
|
2019-02-14 04:03:41 +01:00
|
|
|
total =
|
2019-07-12 18:41:05 +02:00
|
|
|
if showing do
|
2019-02-14 04:03:41 +01:00
|
|
|
length(following)
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2018-03-21 18:23:27 +01:00
|
|
|
%{
|
|
|
|
"id" => "#{user.ap_id}/following",
|
|
|
|
"type" => "OrderedCollection",
|
2019-02-14 04:03:41 +01:00
|
|
|
"totalItems" => total,
|
2019-07-12 18:41:05 +02:00
|
|
|
"first" =>
|
|
|
|
if showing do
|
|
|
|
collection(following, "#{user.ap_id}/following", 1, !user.info.hide_follows)
|
|
|
|
else
|
|
|
|
"#{user.ap_id}/following?page=1"
|
|
|
|
end
|
2018-03-21 18:23:27 +01:00
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2019-07-12 18:41:05 +02:00
|
|
|
def render("followers.json", %{user: user, page: page} = opts) do
|
|
|
|
showing = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
|
|
|
|
|
2018-03-31 20:02:09 +02:00
|
|
|
query = User.get_followers_query(user)
|
|
|
|
query = from(user in query, select: [:ap_id])
|
|
|
|
followers = Repo.all(query)
|
2019-02-14 04:13:07 +01:00
|
|
|
|
2019-02-14 04:03:41 +01:00
|
|
|
total =
|
2019-07-12 18:41:05 +02:00
|
|
|
if showing do
|
2019-02-14 04:03:41 +01:00
|
|
|
length(followers)
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2019-07-12 18:41:05 +02:00
|
|
|
collection(followers, "#{user.ap_id}/followers", page, showing, total)
|
2018-03-21 18:23:27 +01:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2019-07-12 18:41:05 +02:00
|
|
|
def render("followers.json", %{user: user} = opts) do
|
|
|
|
showing = (opts[:for] && opts[:for] == user) || !user.info.hide_followers
|
|
|
|
|
2018-03-31 20:02:09 +02:00
|
|
|
query = User.get_followers_query(user)
|
|
|
|
query = from(user in query, select: [:ap_id])
|
|
|
|
followers = Repo.all(query)
|
2019-02-14 04:13:07 +01:00
|
|
|
|
2019-02-14 04:03:41 +01:00
|
|
|
total =
|
2019-07-12 18:41:05 +02:00
|
|
|
if showing do
|
2019-02-14 04:03:41 +01:00
|
|
|
length(followers)
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2018-03-21 18:23:27 +01:00
|
|
|
%{
|
2018-03-25 19:34:33 +02:00
|
|
|
"id" => "#{user.ap_id}/followers",
|
2018-03-21 18:23:27 +01:00
|
|
|
"type" => "OrderedCollection",
|
2019-02-14 04:03:41 +01:00
|
|
|
"totalItems" => total,
|
2019-02-14 04:13:07 +01:00
|
|
|
"first" =>
|
2019-07-12 18:41:05 +02:00
|
|
|
if showing do
|
|
|
|
collection(followers, "#{user.ap_id}/followers", 1, showing, total)
|
|
|
|
else
|
|
|
|
"#{user.ap_id}/followers?page=1"
|
|
|
|
end
|
2018-03-21 18:23:27 +01:00
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
2017-12-11 10:37:22 +01:00
|
|
|
end
|
2018-03-22 06:23:05 +01:00
|
|
|
|
|
|
|
def render("outbox.json", %{user: user, max_id: max_qid}) do
|
|
|
|
params = %{
|
|
|
|
"limit" => "10"
|
|
|
|
}
|
|
|
|
|
2018-05-06 08:58:59 +02:00
|
|
|
params =
|
|
|
|
if max_qid != nil do
|
|
|
|
Map.put(params, "max_id", max_qid)
|
|
|
|
else
|
|
|
|
params
|
|
|
|
end
|
2018-03-22 06:23:05 +01:00
|
|
|
|
2018-05-26 07:10:12 +02:00
|
|
|
activities = ActivityPub.fetch_user_activities(user, nil, params)
|
2019-02-22 05:37:48 +01:00
|
|
|
|
|
|
|
{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
|
2018-03-22 06:23:05 +01:00
|
|
|
|
|
|
|
iri = "#{user.ap_id}/outbox"
|
2018-03-30 15:01:53 +02:00
|
|
|
|
2018-03-22 06:23:05 +01:00
|
|
|
page = %{
|
|
|
|
"id" => "#{iri}?max_id=#{max_id}",
|
|
|
|
"type" => "OrderedCollectionPage",
|
|
|
|
"partOf" => iri,
|
|
|
|
"orderedItems" => collection,
|
2019-01-09 16:08:24 +01:00
|
|
|
"next" => "#{iri}?max_id=#{min_id}"
|
2018-03-22 06:23:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-05-05 00:31:46 +02:00
|
|
|
|
2018-12-29 18:15:28 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
collection =
|
|
|
|
Enum.map(activities, fn act ->
|
|
|
|
{:ok, data} = Transmogrifier.prepare_outgoing(act.data)
|
|
|
|
data
|
|
|
|
end)
|
|
|
|
|
|
|
|
iri = "#{user.ap_id}/inbox"
|
|
|
|
|
|
|
|
page = %{
|
|
|
|
"id" => "#{iri}?max_id=#{max_id}",
|
|
|
|
"type" => "OrderedCollectionPage",
|
|
|
|
"partOf" => iri,
|
|
|
|
"orderedItems" => collection,
|
2019-01-09 16:08:24 +01:00
|
|
|
"next" => "#{iri}?max_id=#{min_id}"
|
2018-12-29 18:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-12-02 17:35:32 +01:00
|
|
|
def collection(collection, iri, page, show_items \\ true, total \\ nil) do
|
2018-05-05 00:31:46 +02:00
|
|
|
offset = (page - 1) * 10
|
|
|
|
items = Enum.slice(collection, offset, 10)
|
|
|
|
items = Enum.map(items, fn user -> user.ap_id end)
|
2018-05-07 18:11:37 +02:00
|
|
|
total = total || length(collection)
|
2018-05-05 00:31:46 +02:00
|
|
|
|
|
|
|
map = %{
|
|
|
|
"id" => "#{iri}?page=#{page}",
|
|
|
|
"type" => "OrderedCollectionPage",
|
|
|
|
"partOf" => iri,
|
2018-05-07 18:11:37 +02:00
|
|
|
"totalItems" => total,
|
2018-12-02 17:35:32 +01:00
|
|
|
"orderedItems" => if(show_items, do: items, else: [])
|
2018-05-05 00:31:46 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 18:11:37 +02:00
|
|
|
if offset < total do
|
2018-05-05 00:31:46 +02:00
|
|
|
Map.put(map, "next", "#{iri}?page=#{page + 1}")
|
2019-02-06 18:42:19 +01:00
|
|
|
else
|
|
|
|
map
|
2018-05-05 00:31:46 +02:00
|
|
|
end
|
|
|
|
end
|
2019-03-26 16:40:09 +01:00
|
|
|
|
|
|
|
defp maybe_make_image(func, key, user) do
|
|
|
|
if image = func.(user, no_default: true) do
|
|
|
|
%{
|
|
|
|
key => %{
|
|
|
|
"type" => "Image",
|
|
|
|
"url" => image
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
%{}
|
|
|
|
end
|
|
|
|
end
|
2017-12-11 10:37:22 +01:00
|
|
|
end
|