2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 08:41:47 -07:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-12-11 02:37:22 -07:00
|
|
|
defmodule Pleroma.Web.ActivityPub.UserView do
|
|
|
|
use Pleroma.Web, :view
|
2019-02-06 12:20:02 -07:00
|
|
|
|
2019-05-21 21:58:15 -06:00
|
|
|
alias Pleroma.Keys
|
2019-02-09 08:16:26 -07:00
|
|
|
alias Pleroma.Repo
|
2019-03-04 19:52:23 -07:00
|
|
|
alias Pleroma.User
|
2019-02-09 08:16:26 -07:00
|
|
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
|
|
|
alias Pleroma.Web.ActivityPub.Utils
|
2019-02-13 12:20:41 -07:00
|
|
|
alias Pleroma.Web.Endpoint
|
2019-03-04 19:52:23 -07:00
|
|
|
alias Pleroma.Web.Router.Helpers
|
2019-02-06 12:20:02 -07:00
|
|
|
|
2018-03-31 12:02:09 -06:00
|
|
|
import Ecto.Query
|
2017-12-11 02:37:22 -07:00
|
|
|
|
2019-02-13 19:41:21 -07: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 14:28:11 -07:00
|
|
|
%{
|
2019-02-13 12:20:41 -07:00
|
|
|
"oauthAuthorizationEndpoint" => Helpers.o_auth_url(Endpoint, :authorize),
|
2019-10-01 02:21:46 -06:00
|
|
|
"oauthRegistrationEndpoint" => Helpers.app_url(Endpoint, :create),
|
2019-02-13 12:20:41 -07:00
|
|
|
"oauthTokenEndpoint" => Helpers.o_auth_url(Endpoint, :token_exchange),
|
2019-09-23 11:16:36 -06:00
|
|
|
"sharedInbox" => Helpers.activity_pub_url(Endpoint, :inbox),
|
|
|
|
"uploadMedia" => Helpers.activity_pub_url(Endpoint, :upload_media)
|
2019-02-12 14:28:11 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-02-13 12:20:41 -07:00
|
|
|
def render("endpoints.json", _), do: %{}
|
2019-02-12 14:28:11 -07:00
|
|
|
|
2019-07-17 11:34:57 -06:00
|
|
|
def render("service.json", %{user: user}) do
|
2019-05-21 21:58:15 -06:00
|
|
|
{:ok, user} = User.ensure_keys_present(user)
|
2019-10-06 07:22:35 -06:00
|
|
|
{:ok, _, public_key} = Keys.keys_from_pem(user.keys)
|
2018-08-06 00:11:51 -06:00
|
|
|
public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
|
|
|
|
public_key = :public_key.pem_encode([public_key])
|
|
|
|
|
2019-02-12 14:28:11 -07:00
|
|
|
endpoints = render("endpoints.json", %{user: user})
|
|
|
|
|
2018-08-06 00:11:51 -06:00
|
|
|
%{
|
|
|
|
"id" => user.ap_id,
|
|
|
|
"type" => "Application",
|
2018-08-06 02:15:18 -06:00
|
|
|
"following" => "#{user.ap_id}/following",
|
|
|
|
"followers" => "#{user.ap_id}/followers",
|
2018-08-06 00:11:51 -06:00
|
|
|
"inbox" => "#{user.ap_id}/inbox",
|
|
|
|
"name" => "Pleroma",
|
2019-07-17 11:34:57 -06:00
|
|
|
"summary" =>
|
|
|
|
"An internal service actor for this Pleroma instance. No user-serviceable parts inside.",
|
2018-08-06 00:11:51 -06:00
|
|
|
"url" => user.ap_id,
|
|
|
|
"manuallyApprovesFollowers" => false,
|
|
|
|
"publicKey" => %{
|
|
|
|
"id" => "#{user.ap_id}#main-key",
|
|
|
|
"owner" => user.ap_id,
|
|
|
|
"publicKeyPem" => public_key
|
|
|
|
},
|
2019-10-19 11:46:24 -06:00
|
|
|
"endpoints" => endpoints,
|
|
|
|
"invisible" => User.invisible?(user)
|
2018-08-06 00:11:51 -06:00
|
|
|
}
|
2018-11-08 09:51:48 -07:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
2018-08-06 00:11:51 -06:00
|
|
|
end
|
|
|
|
|
2019-07-17 11:34:57 -06: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}),
|
2019-08-03 12:37:20 -06:00
|
|
|
do: render("service.json", %{user: user}) |> Map.put("preferredUsername", user.nickname)
|
2019-07-17 11:34:57 -06:00
|
|
|
|
2017-12-11 02:37:22 -07:00
|
|
|
def render("user.json", %{user: user}) do
|
2019-05-21 21:58:15 -06:00
|
|
|
{:ok, user} = User.ensure_keys_present(user)
|
2019-10-06 07:22:35 -06:00
|
|
|
{:ok, _, public_key} = Keys.keys_from_pem(user.keys)
|
2018-07-11 20:45:48 -06:00
|
|
|
public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
|
2017-12-11 02:37:22 -07:00
|
|
|
public_key = :public_key.pem_encode([public_key])
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2019-02-12 14:28:11 -07:00
|
|
|
endpoints = render("endpoints.json", %{user: user})
|
|
|
|
|
2019-09-11 14:19:06 -06:00
|
|
|
emoji_tags = Transmogrifier.take_emoji_tags(user)
|
2019-02-12 06:59:34 -07:00
|
|
|
|
2019-07-25 06:35:34 -06:00
|
|
|
fields =
|
2019-10-16 12:59:21 -06:00
|
|
|
user
|
|
|
|
|> User.fields()
|
2019-08-06 05:21:25 -06:00
|
|
|
|> Enum.map(fn %{"name" => name, "value" => value} ->
|
|
|
|
%{
|
|
|
|
"name" => Pleroma.HTML.strip_tags(name),
|
|
|
|
"value" => Pleroma.HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly)
|
|
|
|
}
|
|
|
|
end)
|
2019-07-25 06:35:34 -06:00
|
|
|
|> Enum.map(&Map.put(&1, "type", "PropertyValue"))
|
|
|
|
|
2017-12-11 02:37:22 -07:00
|
|
|
%{
|
2017-12-11 10:20:41 -07:00
|
|
|
"id" => user.ap_id,
|
2019-12-10 06:19:26 -07:00
|
|
|
"type" => user.actor_type,
|
2017-12-11 10:20:41 -07:00
|
|
|
"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,
|
2019-10-16 12:59:21 -06:00
|
|
|
"manuallyApprovesFollowers" => user.locked,
|
2017-12-11 10:20:41 -07:00
|
|
|
"publicKey" => %{
|
|
|
|
"id" => "#{user.ap_id}#main-key",
|
|
|
|
"owner" => user.ap_id,
|
|
|
|
"publicKeyPem" => public_key
|
2017-12-11 02:37:22 -07:00
|
|
|
},
|
2019-02-12 14:28:11 -07:00
|
|
|
"endpoints" => endpoints,
|
2019-07-25 06:35:34 -06:00
|
|
|
"attachment" => fields,
|
2019-10-16 12:59:21 -06:00
|
|
|
"tag" => (user.source_data["tag"] || []) ++ emoji_tags,
|
|
|
|
"discoverable" => user.discoverable
|
2017-12-11 02:37:22 -07:00
|
|
|
}
|
2019-03-26 09:40:09 -06: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 11:23:27 -06:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2019-07-12 10:41:05 -06:00
|
|
|
def render("following.json", %{user: user, page: page} = opts) do
|
2019-10-16 12:59:21 -06:00
|
|
|
showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
|
|
|
|
showing_count = showing_items || !user.hide_follows_count
|
2019-09-13 08:37:30 -06:00
|
|
|
|
2018-03-31 12:02:09 -06:00
|
|
|
query = User.get_friends_query(user)
|
|
|
|
query = from(user in query, select: [:ap_id])
|
|
|
|
following = Repo.all(query)
|
2019-02-13 20:13:07 -07:00
|
|
|
|
2019-02-13 20:03:41 -07:00
|
|
|
total =
|
2019-09-13 08:37:30 -06:00
|
|
|
if showing_count do
|
2019-02-13 20:03:41 -07:00
|
|
|
length(following)
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2019-09-13 08:37:30 -06:00
|
|
|
collection(following, "#{user.ap_id}/following", page, showing_items, total)
|
2018-03-21 11:23:27 -06:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2019-07-12 10:41:05 -06:00
|
|
|
def render("following.json", %{user: user} = opts) do
|
2019-10-16 12:59:21 -06:00
|
|
|
showing_items = (opts[:for] && opts[:for] == user) || !user.hide_follows
|
|
|
|
showing_count = showing_items || !user.hide_follows_count
|
2019-09-13 08:37:30 -06:00
|
|
|
|
2018-03-31 12:02:09 -06:00
|
|
|
query = User.get_friends_query(user)
|
|
|
|
query = from(user in query, select: [:ap_id])
|
|
|
|
following = Repo.all(query)
|
2019-02-13 20:13:07 -07:00
|
|
|
|
2019-02-13 20:03:41 -07:00
|
|
|
total =
|
2019-09-13 08:37:30 -06:00
|
|
|
if showing_count do
|
2019-02-13 20:03:41 -07:00
|
|
|
length(following)
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2018-03-21 11:23:27 -06:00
|
|
|
%{
|
|
|
|
"id" => "#{user.ap_id}/following",
|
|
|
|
"type" => "OrderedCollection",
|
2019-02-13 20:03:41 -07:00
|
|
|
"totalItems" => total,
|
2019-07-12 10:41:05 -06:00
|
|
|
"first" =>
|
2019-09-13 08:37:30 -06:00
|
|
|
if showing_items do
|
2019-10-16 12:59:21 -06:00
|
|
|
collection(following, "#{user.ap_id}/following", 1, !user.hide_follows)
|
2019-07-12 10:41:05 -06:00
|
|
|
else
|
|
|
|
"#{user.ap_id}/following?page=1"
|
|
|
|
end
|
2018-03-21 11:23:27 -06:00
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2019-07-12 10:41:05 -06:00
|
|
|
def render("followers.json", %{user: user, page: page} = opts) do
|
2019-10-16 12:59:21 -06:00
|
|
|
showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
|
|
|
|
showing_count = showing_items || !user.hide_followers_count
|
2019-07-12 10:41:05 -06:00
|
|
|
|
2018-03-31 12:02:09 -06:00
|
|
|
query = User.get_followers_query(user)
|
|
|
|
query = from(user in query, select: [:ap_id])
|
|
|
|
followers = Repo.all(query)
|
2019-02-13 20:13:07 -07:00
|
|
|
|
2019-02-13 20:03:41 -07:00
|
|
|
total =
|
2019-09-13 08:37:30 -06:00
|
|
|
if showing_count do
|
2019-02-13 20:03:41 -07:00
|
|
|
length(followers)
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2019-09-13 08:37:30 -06:00
|
|
|
collection(followers, "#{user.ap_id}/followers", page, showing_items, total)
|
2018-03-21 11:23:27 -06:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2019-07-12 10:41:05 -06:00
|
|
|
def render("followers.json", %{user: user} = opts) do
|
2019-10-16 12:59:21 -06:00
|
|
|
showing_items = (opts[:for] && opts[:for] == user) || !user.hide_followers
|
|
|
|
showing_count = showing_items || !user.hide_followers_count
|
2019-07-12 10:41:05 -06:00
|
|
|
|
2018-03-31 12:02:09 -06:00
|
|
|
query = User.get_followers_query(user)
|
|
|
|
query = from(user in query, select: [:ap_id])
|
|
|
|
followers = Repo.all(query)
|
2019-02-13 20:13:07 -07:00
|
|
|
|
2019-02-13 20:03:41 -07:00
|
|
|
total =
|
2019-09-13 08:37:30 -06:00
|
|
|
if showing_count do
|
2019-02-13 20:03:41 -07:00
|
|
|
length(followers)
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2018-03-21 11:23:27 -06:00
|
|
|
%{
|
2018-03-25 11:34:33 -06:00
|
|
|
"id" => "#{user.ap_id}/followers",
|
2018-03-21 11:23:27 -06:00
|
|
|
"type" => "OrderedCollection",
|
2019-02-13 20:03:41 -07:00
|
|
|
"totalItems" => total,
|
2019-02-13 20:13:07 -07:00
|
|
|
"first" =>
|
2019-09-13 08:37:30 -06:00
|
|
|
if showing_items do
|
|
|
|
collection(followers, "#{user.ap_id}/followers", 1, showing_items, total)
|
2019-07-12 10:41:05 -06:00
|
|
|
else
|
|
|
|
"#{user.ap_id}/followers?page=1"
|
|
|
|
end
|
2018-03-21 11:23:27 -06:00
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
2017-12-11 02:37:22 -07:00
|
|
|
end
|
2018-03-21 23:23:05 -06:00
|
|
|
|
2019-09-25 06:59:04 -06:00
|
|
|
def render("activity_collection.json", %{iri: iri}) do
|
|
|
|
%{
|
|
|
|
"id" => iri,
|
|
|
|
"type" => "OrderedCollection",
|
|
|
|
"first" => "#{iri}?page=true"
|
2018-03-21 23:23:05 -06:00
|
|
|
}
|
2019-09-25 06:59:04 -06:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
2018-03-21 23:23:05 -06:00
|
|
|
|
2019-09-25 06:59:04 -06:00
|
|
|
def render("activity_collection_page.json", %{activities: activities, iri: iri}) do
|
2019-09-19 08:09:07 -06:00
|
|
|
# this is sorted chronologically, so first activity is the newest (max)
|
2019-02-21 21:37:48 -07:00
|
|
|
{max_id, min_id, collection} =
|
|
|
|
if length(activities) > 0 do
|
|
|
|
{
|
|
|
|
Enum.at(activities, 0).id,
|
2019-09-19 08:09:07 -06:00
|
|
|
Enum.at(Enum.reverse(activities), 0).id,
|
2019-02-21 21:37:48 -07:00
|
|
|
Enum.map(activities, fn act ->
|
|
|
|
{:ok, data} = Transmogrifier.prepare_outgoing(act.data)
|
|
|
|
data
|
|
|
|
end)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
[]
|
|
|
|
}
|
|
|
|
end
|
2018-03-21 23:23:05 -06:00
|
|
|
|
2019-09-25 07:38:45 -06:00
|
|
|
%{
|
2019-09-25 06:59:04 -06:00
|
|
|
"id" => "#{iri}?max_id=#{max_id}&page=true",
|
2018-03-21 23:23:05 -06:00
|
|
|
"type" => "OrderedCollectionPage",
|
|
|
|
"partOf" => iri,
|
|
|
|
"orderedItems" => collection,
|
2019-09-25 06:59:04 -06:00
|
|
|
"next" => "#{iri}?max_id=#{min_id}&page=true"
|
2018-03-21 23:23:05 -06:00
|
|
|
}
|
2019-09-25 07:38:45 -06:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
2018-12-29 10:15:28 -07:00
|
|
|
end
|
|
|
|
|
2018-12-02 09:35:32 -07:00
|
|
|
def collection(collection, iri, page, show_items \\ true, total \\ nil) do
|
2018-05-04 16:31:46 -06:00
|
|
|
offset = (page - 1) * 10
|
|
|
|
items = Enum.slice(collection, offset, 10)
|
|
|
|
items = Enum.map(items, fn user -> user.ap_id end)
|
2018-05-07 10:11:37 -06:00
|
|
|
total = total || length(collection)
|
2018-05-04 16:31:46 -06:00
|
|
|
|
|
|
|
map = %{
|
|
|
|
"id" => "#{iri}?page=#{page}",
|
|
|
|
"type" => "OrderedCollectionPage",
|
|
|
|
"partOf" => iri,
|
2018-05-07 10:11:37 -06:00
|
|
|
"totalItems" => total,
|
2018-12-02 09:35:32 -07:00
|
|
|
"orderedItems" => if(show_items, do: items, else: [])
|
2018-05-04 16:31:46 -06:00
|
|
|
}
|
|
|
|
|
2018-05-07 10:11:37 -06:00
|
|
|
if offset < total do
|
2018-05-04 16:31:46 -06:00
|
|
|
Map.put(map, "next", "#{iri}?page=#{page + 1}")
|
2019-02-06 10:42:19 -07:00
|
|
|
else
|
|
|
|
map
|
2018-05-04 16:31:46 -06:00
|
|
|
end
|
|
|
|
end
|
2019-03-26 09:40:09 -06: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 02:37:22 -07:00
|
|
|
end
|