2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 15:41:47 +00:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-12-11 09:37:22 +00:00
|
|
|
defmodule Pleroma.Web.ActivityPub.UserView do
|
|
|
|
use Pleroma.Web, :view
|
|
|
|
alias Pleroma.Web.Salmon
|
2018-02-25 20:02:44 +00:00
|
|
|
alias Pleroma.Web.WebFinger
|
2017-12-11 09:37:22 +00:00
|
|
|
alias Pleroma.User
|
2018-03-31 18:02:09 +00:00
|
|
|
alias Pleroma.Repo
|
2018-03-22 05:23:05 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
2018-03-21 17:23:27 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.Utils
|
2018-03-31 18:02:09 +00:00
|
|
|
import Ecto.Query
|
2017-12-11 09:37:22 +00:00
|
|
|
|
2018-08-06 06:11:51 +00:00
|
|
|
# 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)
|
2018-11-20 19:12:39 +00:00
|
|
|
{:ok, _, public_key} = Salmon.keys_from_pem(user.info.keys)
|
2018-08-06 06:11:51 +00:00
|
|
|
public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
|
|
|
|
public_key = :public_key.pem_encode([public_key])
|
|
|
|
|
|
|
|
%{
|
|
|
|
"id" => user.ap_id,
|
|
|
|
"type" => "Application",
|
2018-08-06 08:15:18 +00:00
|
|
|
"following" => "#{user.ap_id}/following",
|
|
|
|
"followers" => "#{user.ap_id}/followers",
|
2018-08-06 06:11:51 +00:00
|
|
|
"inbox" => "#{user.ap_id}/inbox",
|
|
|
|
"name" => "Pleroma",
|
|
|
|
"summary" => "Virtual actor for Pleroma relay",
|
|
|
|
"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"
|
|
|
|
}
|
|
|
|
}
|
2018-11-08 16:51:48 +00:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
2018-08-06 06:11:51 +00:00
|
|
|
end
|
|
|
|
|
2017-12-11 09:37:22 +00:00
|
|
|
def render("user.json", %{user: user}) do
|
2018-02-25 20:02:44 +00:00
|
|
|
{:ok, user} = WebFinger.ensure_keys_present(user)
|
2018-11-20 19:12:39 +00:00
|
|
|
{:ok, _, public_key} = Salmon.keys_from_pem(user.info.keys)
|
2018-07-12 02:45:48 +00:00
|
|
|
public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
|
2017-12-11 09:37:22 +00:00
|
|
|
public_key = :public_key.pem_encode([public_key])
|
2018-03-30 13:01:53 +00:00
|
|
|
|
2017-12-11 09:37:22 +00:00
|
|
|
%{
|
2017-12-11 17:20:41 +00: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 19:12:39 +00:00
|
|
|
"manuallyApprovesFollowers" => user.info.locked,
|
2017-12-11 17:20:41 +00:00
|
|
|
"publicKey" => %{
|
|
|
|
"id" => "#{user.ap_id}#main-key",
|
|
|
|
"owner" => user.ap_id,
|
|
|
|
"publicKeyPem" => public_key
|
2017-12-11 09:37:22 +00:00
|
|
|
},
|
2017-12-11 17:20:41 +00:00
|
|
|
"endpoints" => %{
|
2018-03-30 13:01:53 +00:00
|
|
|
"sharedInbox" => "#{Pleroma.Web.Endpoint.url()}/inbox"
|
2017-12-11 09:37:22 +00:00
|
|
|
},
|
2017-12-11 17:20:41 +00:00
|
|
|
"icon" => %{
|
|
|
|
"type" => "Image",
|
|
|
|
"url" => User.avatar_url(user)
|
2018-02-22 07:24:18 +00:00
|
|
|
},
|
|
|
|
"image" => %{
|
|
|
|
"type" => "Image",
|
|
|
|
"url" => User.banner_url(user)
|
2018-08-13 13:11:48 +00:00
|
|
|
},
|
2018-11-20 19:12:39 +00:00
|
|
|
"tag" => user.info.source_data["tag"] || []
|
2017-12-11 09:37:22 +00:00
|
|
|
}
|
2018-03-21 17:23:27 +00:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("following.json", %{user: user, page: page}) do
|
2018-03-31 18:02:09 +00:00
|
|
|
query = User.get_friends_query(user)
|
|
|
|
query = from(user in query, select: [:ap_id])
|
|
|
|
following = Repo.all(query)
|
2018-03-30 13:01:53 +00:00
|
|
|
|
2019-02-01 20:22:58 +00:00
|
|
|
collection(following, "#{user.ap_id}/following", page, !user.info.hide_network)
|
2018-03-21 17:23:27 +00:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("following.json", %{user: user}) do
|
2018-03-31 18:02:09 +00:00
|
|
|
query = User.get_friends_query(user)
|
|
|
|
query = from(user in query, select: [:ap_id])
|
|
|
|
following = Repo.all(query)
|
2018-03-30 13:01:53 +00:00
|
|
|
|
2018-03-21 17:23:27 +00:00
|
|
|
%{
|
|
|
|
"id" => "#{user.ap_id}/following",
|
|
|
|
"type" => "OrderedCollection",
|
|
|
|
"totalItems" => length(following),
|
2019-02-01 20:22:58 +00:00
|
|
|
"first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_network)
|
2018-03-21 17:23:27 +00:00
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("followers.json", %{user: user, page: page}) do
|
2018-03-31 18:02:09 +00:00
|
|
|
query = User.get_followers_query(user)
|
|
|
|
query = from(user in query, select: [:ap_id])
|
|
|
|
followers = Repo.all(query)
|
2018-03-30 13:01:53 +00:00
|
|
|
|
2019-02-01 20:22:58 +00:00
|
|
|
collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_network)
|
2018-03-21 17:23:27 +00:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("followers.json", %{user: user}) do
|
2018-03-31 18:02:09 +00:00
|
|
|
query = User.get_followers_query(user)
|
|
|
|
query = from(user in query, select: [:ap_id])
|
|
|
|
followers = Repo.all(query)
|
2018-03-30 13:01:53 +00:00
|
|
|
|
2018-03-21 17:23:27 +00:00
|
|
|
%{
|
2018-03-25 17:34:33 +00:00
|
|
|
"id" => "#{user.ap_id}/followers",
|
2018-03-21 17:23:27 +00:00
|
|
|
"type" => "OrderedCollection",
|
|
|
|
"totalItems" => length(followers),
|
2019-02-01 20:22:58 +00:00
|
|
|
"first" => collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_network)
|
2018-03-21 17:23:27 +00:00
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
2017-12-11 09:37:22 +00:00
|
|
|
end
|
2018-03-22 05:23:05 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2018-05-06 06:58:59 +00:00
|
|
|
params =
|
|
|
|
if max_qid != nil do
|
|
|
|
Map.put(params, "max_id", max_qid)
|
|
|
|
else
|
|
|
|
params
|
|
|
|
end
|
2018-03-22 05:23:05 +00:00
|
|
|
|
2018-05-26 05:10:12 +00:00
|
|
|
activities = ActivityPub.fetch_user_activities(user, nil, params)
|
2018-05-26 05:02:57 +00:00
|
|
|
min_id = Enum.at(Enum.reverse(activities), 0).id
|
2018-03-22 05:23:05 +00:00
|
|
|
max_id = Enum.at(activities, 0).id
|
|
|
|
|
2018-03-30 13:01:53 +00:00
|
|
|
collection =
|
|
|
|
Enum.map(activities, fn act ->
|
|
|
|
{:ok, data} = Transmogrifier.prepare_outgoing(act.data)
|
|
|
|
data
|
|
|
|
end)
|
2018-03-22 05:23:05 +00:00
|
|
|
|
|
|
|
iri = "#{user.ap_id}/outbox"
|
2018-03-30 13:01:53 +00:00
|
|
|
|
2018-03-22 05:23:05 +00:00
|
|
|
page = %{
|
|
|
|
"id" => "#{iri}?max_id=#{max_id}",
|
|
|
|
"type" => "OrderedCollectionPage",
|
|
|
|
"partOf" => iri,
|
|
|
|
"totalItems" => info.note_count,
|
|
|
|
"orderedItems" => collection,
|
2019-01-09 15:08:24 +00:00
|
|
|
"next" => "#{iri}?max_id=#{min_id}"
|
2018-03-22 05:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-05-04 22:31:46 +00:00
|
|
|
|
2018-12-29 17:15:28 +00: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,
|
|
|
|
"totalItems" => -1,
|
|
|
|
"orderedItems" => collection,
|
2019-01-09 15:08:24 +00:00
|
|
|
"next" => "#{iri}?max_id=#{min_id}"
|
2018-12-29 17:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if max_qid == nil do
|
|
|
|
%{
|
|
|
|
"id" => iri,
|
|
|
|
"type" => "OrderedCollection",
|
|
|
|
"totalItems" => -1,
|
|
|
|
"first" => page
|
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
else
|
|
|
|
page |> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-02 16:35:32 +00:00
|
|
|
def collection(collection, iri, page, show_items \\ true, total \\ nil) do
|
2018-05-04 22:31:46 +00:00
|
|
|
offset = (page - 1) * 10
|
|
|
|
items = Enum.slice(collection, offset, 10)
|
|
|
|
items = Enum.map(items, fn user -> user.ap_id end)
|
2018-05-07 16:11:37 +00:00
|
|
|
total = total || length(collection)
|
2018-05-04 22:31:46 +00:00
|
|
|
|
|
|
|
map = %{
|
|
|
|
"id" => "#{iri}?page=#{page}",
|
|
|
|
"type" => "OrderedCollectionPage",
|
|
|
|
"partOf" => iri,
|
2018-05-07 16:11:37 +00:00
|
|
|
"totalItems" => total,
|
2018-12-02 16:35:32 +00:00
|
|
|
"orderedItems" => if(show_items, do: items, else: [])
|
2018-05-04 22:31:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:11:37 +00:00
|
|
|
if offset < total do
|
2018-05-04 22:31:46 +00:00
|
|
|
Map.put(map, "next", "#{iri}?page=#{page + 1}")
|
|
|
|
end
|
|
|
|
end
|
2017-12-11 09:37:22 +00:00
|
|
|
end
|