akkoma/lib/pleroma/web/static_fe/user_representer.ex

36 lines
982 B
Elixir
Raw Normal View History

2019-03-02 14:12:38 -07:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.StaticFE.UserRepresenter do
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.StaticFE.ActivityRepresenter
def prepare_user(%User{} = user) do
%{}
|> set_user(user)
|> set_timeline(user)
end
defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
defp set_timeline(data, %User{} = user) do
activities =
ActivityPub.fetch_user_activities(user, nil, %{})
|> Enum.map(fn activity -> ActivityRepresenter.prepare_activity(user, activity) end)
Map.put(data, :timeline, activities)
end
def represent(username_or_id) do
with %User{} = user <- User.get_cached_by_nickname_or_id(username_or_id),
data <- prepare_user(user) do
{:ok, data}
else
e ->
{:error, e}
end
end
end