def update_credentials(%{assigns: %{user: user}} = conn, params) do
original_user = user
+ avatar_upload_limit =
+ Application.get_env(:pleroma, :instance)
+ |> Keyword.fetch(:avatar_upload_limit)
+
+ banner_upload_limit =
+ Application.get_env(:pleroma, :instance)
+ |> Keyword.fetch(:banner_upload_limit)
+
params =
if bio = params["note"] do
Map.put(params, "bio", bio)
user =
if avatar = params["avatar"] do
with %Plug.Upload{} <- avatar,
- {:ok, object} <- ActivityPub.upload(avatar),
+ {:ok, object} <- ActivityPub.upload(avatar, avatar_upload_limit),
change = Ecto.Changeset.change(user, %{avatar: object.data}),
{:ok, user} = User.update_and_set_cache(change) do
user
user =
if banner = params["header"] do
with %Plug.Upload{} <- banner,
- {:ok, object} <- ActivityPub.upload(banner),
+ {:ok, object} <- ActivityPub.upload(banner, banner_upload_limit),
new_info <- Map.put(user.info, "banner", object.data),
change <- User.info_changeset(user, %{info: new_info}),
{:ok, user} <- User.update_and_set_cache(change) do
CommonAPI.update(user)
end
- json(conn, AccountView.render("account.json", %{user: user}))
+ json(conn, AccountView.render("account.json", %{user: user, for: user}))
else
_e ->
conn
end
def verify_credentials(%{assigns: %{user: user}} = conn, _) do
- account = AccountView.render("account.json", %{user: user})
+ account = AccountView.render("account.json", %{user: user, for: user})
json(conn, account)
end
- def user(conn, %{"id" => id}) do
+ def user(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
with %User{} = user <- Repo.get(User, id) do
- account = AccountView.render("account.json", %{user: user})
+ account = AccountView.render("account.json", %{user: user, for: for_user})
json(conn, account)
else
_e ->
end
end
- @instance Application.get_env(:pleroma, :instance)
- @mastodon_api_level "2.4.3"
+ @mastodon_api_level "2.5.0"
def masto_instance(conn, _params) do
+ instance = Pleroma.Config.get(:instance)
+
response = %{
uri: Web.base_url(),
- title: Keyword.get(@instance, :name),
- description: Keyword.get(@instance, :description),
- version: "#{@mastodon_api_level} (compatible; #{Keyword.get(@instance, :version)})",
- email: Keyword.get(@instance, :email),
+ title: Keyword.get(instance, :name),
+ description: Keyword.get(instance, :description),
+ version: "#{@mastodon_api_level} (compatible; #{Keyword.get(instance, :version)})",
+ email: Keyword.get(instance, :email),
urls: %{
streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws")
},
stats: Stats.get_stats(),
thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg",
- max_toot_chars: Keyword.get(@instance, :limit)
+ max_toot_chars: Keyword.get(instance, :limit)
}
json(conn, response)
end
defp mastodonized_emoji do
- Pleroma.Formatter.get_custom_emoji()
+ Pleroma.Emoji.get_all()
|> Enum.map(fn {shortcode, relative_url} ->
url = to_string(URI.merge(Web.base_url(), relative_url))
activities =
ActivityPub.fetch_activities([user.ap_id | user.following], params)
+ |> ActivityPub.contain_timeline(user)
|> Enum.reverse()
conn
def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with %Activity{} = activity <- Repo.get(Activity, id),
true <- ActivityPub.visible_for_user?(activity, user) do
- render(conn, StatusView, "status.json", %{activity: activity, for: user})
+ try_render(conn, StatusView, "status.json", %{activity: activity, for: user})
end
end
{:ok, activity} =
Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
- render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
+ try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
end
def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
- render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
+ try_render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
end
end
def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
- render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
+ try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
end
end
def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
- render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
+ try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
end
end
def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
- render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
+ try_render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
end
end
new_data = %{object.data | "name" => description}
change = Object.change(object, %{data: new_data})
- {:ok, media_obj} = Repo.update(change)
+ {:ok, _} = Repo.update(change)
data =
new_data
|> Map.put("type", "Create")
|> Map.put("local_only", local_only)
|> Map.put("blocking_user", user)
+ |> Map.put("tag", String.downcase(params["tag"]))
activities =
ActivityPub.fetch_public_activities(params)
def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
with %User{} = followed <- Repo.get(User, id),
{:ok, follower} <- User.maybe_direct_follow(follower, followed),
- {:ok, _activity} <- ActivityPub.follow(follower, followed) do
+ {:ok, _activity} <- ActivityPub.follow(follower, followed),
+ {:ok, follower, followed} <-
+ User.wait_and_refresh(
+ Pleroma.Config.get([:activitypub, :follow_handshake_timeout]),
+ follower,
+ followed
+ ) do
render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
else
{:error, message} ->
with %User{} = followed <- Repo.get_by(User, nickname: uri),
{:ok, follower} <- User.maybe_direct_follow(follower, followed),
{:ok, _activity} <- ActivityPub.follow(follower, followed) do
- render(conn, AccountView, "account.json", %{user: followed})
+ render(conn, AccountView, "account.json", %{user: followed, for: follower})
else
{:error, message} ->
conn
end
end
+ def account_lists(%{assigns: %{user: user}} = conn, %{"id" => account_id}) do
+ lists = Pleroma.List.get_lists_account_belongs(user, account_id)
+ res = ListView.render("lists.json", lists: lists)
+ json(conn, res)
+ end
+
def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
{:ok, _list} <- Pleroma.List.delete(list) do
if user && token do
mastodon_emoji = mastodonized_emoji()
- accounts = Map.put(%{}, user.id, AccountView.render("account.json", %{user: user}))
+
+ limit = Pleroma.Config.get([:instance, :limit])
+
+ accounts =
+ Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user}))
initial_state =
%{
auto_play_gif: false,
display_sensitive_media: false,
reduce_motion: false,
- max_toot_chars: Keyword.get(@instance, :limit)
+ max_toot_chars: limit
},
rights: %{
delete_others_notice: !!user.info["is_moderator"]
push_subscription: nil,
accounts: accounts,
custom_emojis: mastodon_emoji,
- char_limit: Keyword.get(@instance, :limit)
+ char_limit: limit
}
|> Jason.encode!()
id: id,
type: "mention",
created_at: created_at,
- account: AccountView.render("account.json", %{user: actor}),
+ account: AccountView.render("account.json", %{user: actor, for: user}),
status: StatusView.render("status.json", %{activity: activity, for: user})
}
id: id,
type: "favourite",
created_at: created_at,
- account: AccountView.render("account.json", %{user: actor}),
+ account: AccountView.render("account.json", %{user: actor, for: user}),
status: StatusView.render("status.json", %{activity: liked_activity, for: user})
}
id: id,
type: "reblog",
created_at: created_at,
- account: AccountView.render("account.json", %{user: actor}),
+ account: AccountView.render("account.json", %{user: actor, for: user}),
status: StatusView.render("status.json", %{activity: announced_activity, for: user})
}
id: id,
type: "follow",
created_at: created_at,
- account: AccountView.render("account.json", %{user: actor})
+ account: AccountView.render("account.json", %{user: actor, for: user})
}
_ ->
end
end
- def get_filters(%{assigns: %{user: user}} = conn, params) do
+ def get_filters(%{assigns: %{user: user}} = conn, _) do
filters = Pleroma.Filter.get_filters(user)
res = FilterView.render("filters.json", filters: filters)
json(conn, res)
json(conn, res)
end
- def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id} = params) do
+ def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
filter = Pleroma.Filter.get(filter_id, user)
res = FilterView.render("filter.json", filter: filter)
json(conn, res)
json(conn, res)
end
- def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id} = params) do
+ def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
query = %Pleroma.Filter{
user_id: user.id,
filter_id: filter_id
}
- {:ok, response} = Pleroma.Filter.delete(query)
+ {:ok, _} = Pleroma.Filter.delete(query)
json(conn, %{})
end
|> json("Something went wrong")
end
- @suggestions Application.get_env(:pleroma, :suggestions)
-
def suggestions(%{assigns: %{user: user}} = conn, _) do
- if Keyword.get(@suggestions, :enabled, false) do
- api = Keyword.get(@suggestions, :third_party_engine, "")
- timeout = Keyword.get(@suggestions, :timeout, 5000)
+ suggestions = Pleroma.Config.get(:suggestions)
+
+ if Keyword.get(suggestions, :enabled, false) do
+ api = Keyword.get(suggestions, :third_party_engine, "")
+ timeout = Keyword.get(suggestions, :timeout, 5000)
+ limit = Keyword.get(suggestions, :limit, 23)
- host =
- Application.get_env(:pleroma, Pleroma.Web.Endpoint)
- |> Keyword.get(:url)
- |> Keyword.get(:host)
+ host = Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
user = user.nickname
url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user)
@httpoison.get(url, [], timeout: timeout, recv_timeout: timeout),
{:ok, data} <- Jason.decode(body) do
data2 =
- Enum.slice(data, 0, 40)
+ Enum.slice(data, 0, limit)
|> Enum.map(fn x ->
Map.put(
x,
end
end
- def filters(conn, _) do
- json(conn, [])
+ def try_render(conn, renderer, target, params)
+ when is_binary(target) do
+ res = render(conn, renderer, target, params)
+
+ if res == nil do
+ conn
+ |> put_status(501)
+ |> json(%{error: "Can't display this activity"})
+ else
+ res
+ end
+ end
+
+ def try_render(conn, _, _, _) do
+ conn
+ |> put_status(501)
+ |> json(%{error: "Can't display this activity"})
end
end