Fix most mastodon api bugs.

This commit is contained in:
lain 2018-11-20 20:12:39 +01:00
parent 6f0b9a5163
commit 0896cf4c0f
7 changed files with 57 additions and 53 deletions

View file

@ -17,6 +17,7 @@ defmodule Pleroma.User.Info do
field(:ap_enabled, :boolean, default: false)
field(:is_moderator, :boolean, default: false)
field(:keys, :string, default: nil)
field(:settings, :map, default: nil)
end
def set_activation_status(info, deactivated) do
@ -97,9 +98,11 @@ defmodule Pleroma.User.Info do
])
end
# Receives data from user_data_from_user_object
def user_upgrade(info, params) do
def set_source_data(info, source_data) do
params = %{source_data: source_data}
info
|> cast(params, [:ap_enabled, :source_data, :banner, :locked])
|> cast(params, [:source_data])
|> validate_required([:source_data])
end
end

View file

@ -509,8 +509,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end
defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
blocks = info["blocks"] || []
domain_blocks = info["domain_blocks"] || []
blocks = info.blocks || []
domain_blocks = info.domain_blocks || []
from(
activity in query,
@ -678,7 +678,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
remote_inboxes =
(Pleroma.Web.Salmon.remote_users(activity) ++ followers)
|> Enum.filter(fn user -> User.ap_enabled?(user) end)
|> Enum.map(fn %{info: %{"source_data" => data}} ->
|> Enum.map(fn %{info: %{source_data: data}} ->
(is_map(data["endpoints"]) && Map.get(data["endpoints"], "sharedInbox")) || data["inbox"]
end)
|> Enum.uniq()

View file

@ -12,7 +12,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
# 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)
{:ok, _, public_key} = Salmon.keys_from_pem(user.info["keys"])
{:ok, _, public_key} = Salmon.keys_from_pem(user.info.keys)
public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
public_key = :public_key.pem_encode([public_key])
@ -40,7 +40,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
def render("user.json", %{user: user}) do
{:ok, user} = WebFinger.ensure_keys_present(user)
{:ok, _, public_key} = Salmon.keys_from_pem(user.info["keys"])
{:ok, _, public_key} = Salmon.keys_from_pem(user.info.keys)
public_key = :public_key.pem_entry_encode(:SubjectPublicKeyInfo, public_key)
public_key = :public_key.pem_encode([public_key])
@ -55,7 +55,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
"name" => user.name,
"summary" => user.bio,
"url" => user.ap_id,
"manuallyApprovesFollowers" => user.info["locked"] || false,
"manuallyApprovesFollowers" => user.info.locked,
"publicKey" => %{
"id" => "#{user.ap_id}#main-key",
"owner" => user.ap_id,
@ -72,7 +72,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
"type" => "Image",
"url" => User.banner_url(user)
},
"tag" => user.info["source_data"]["tag"] || []
"tag" => user.info.source_data["tag"] || []
}
|> Map.merge(Utils.make_json_ld_header())
end

View file

@ -135,12 +135,13 @@ defmodule Pleroma.Web.CommonAPI do
end
end
# Updates the emojis for a user based on their profile
def update(user) do
user =
with emoji <- emoji_from_profile(user),
source_data <- (user.info["source_data"] || %{}) |> Map.put("tag", emoji),
new_info <- Map.put(user.info, "source_data", source_data),
change <- User.info_changeset(user, %{info: new_info}),
source_data <- (user.info.source_data || %{}) |> Map.put("tag", emoji),
info_cng <- Pleroma.User.Info.set_source_data(user.info, source_data),
change <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
{:ok, user} <- User.update_and_set_cache(change) do
user
else

View file

@ -71,34 +71,34 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
user
end
user =
if banner = params["header"] do
with %Plug.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
user
else
_e -> user
end
else
user
end
# user =
# if banner = params["header"] do
# with %Plug.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
# user
# else
# _e -> user
# end
# else
# user
# end
user =
if locked = params["locked"] do
with locked <- locked == "true",
new_info <- Map.put(user.info, "locked", locked),
change <- User.info_changeset(user, %{info: new_info}),
{:ok, user} <- User.update_and_set_cache(change) do
user
else
_e -> user
end
else
user
end
# user =
# if locked = params["locked"] do
# with locked <- locked == "true",
# new_info <- Map.put(user.info, "locked", locked),
# change <- User.info_changeset(user, %{info: new_info}),
# {:ok, user} <- User.update_and_set_cache(change) do
# user
# else
# _e -> user
# end
# else
# user
# end
with changeset <- User.update_changeset(user, params),
{:ok, user} <- User.update_and_set_cache(changeset) do
@ -659,7 +659,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
# TODO: Use proper query
def blocks(%{assigns: %{user: user}} = conn, _) do
with blocked_users <- user.info["blocks"] || [],
with blocked_users <- user.info.blocks || [],
accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do
res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
json(conn, res)
@ -667,7 +667,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end
def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
json(conn, info["domain_blocks"] || [])
json(conn, info.domain_blocks || [])
end
def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
@ -915,11 +915,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
max_toot_chars: limit
},
rights: %{
delete_others_notice: !!user.info["is_moderator"]
delete_others_notice: !!user.info.is_moderator
},
compose: %{
me: "#{user.id}",
default_privacy: user.info["default_scope"] || "public",
default_privacy: user.info.default_scope,
default_sensitive: false
},
media_attachments: %{
@ -939,7 +939,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
]
},
settings:
Map.get(user.info, "settings") ||
Map.get(user.info, :settings) ||
%{
onboarded: true,
home: %{

View file

@ -14,10 +14,10 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
image = User.avatar_url(user) |> MediaProxy.url()
header = User.banner_url(user) |> MediaProxy.url()
user_info = User.user_info(user)
bot = (user.info["source_data"]["type"] || "Person") in ["Application", "Service"]
bot = (user.info.source_data["type"] || "Person") in ["Application", "Service"]
emojis =
(user.info["source_data"]["tag"] || [])
(user.info.source_data["tag"] || [])
|> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
|> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
%{
@ -29,7 +29,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
end)
fields =
(user.info["source_data"]["attachment"] || [])
(user.info.source_data["attachment"] || [])
|> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
|> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)

View file

@ -252,7 +252,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
end
test "verify_credentials default scope unlisted", %{conn: conn} do
user = insert(:user, %{info: %{"default_scope" => "unlisted"}})
user = insert(:user, %{info: %Pleroma.User.Info{default_scope: "unlisted"}})
conn =
conn
@ -845,7 +845,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
describe "locked accounts" do
test "/api/v1/follow_requests works" do
user = insert(:user, %{info: %{"locked" => true}})
user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
other_user = insert(:user)
{:ok, activity} = ActivityPub.follow(other_user, user)
@ -865,7 +865,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
end
test "/api/v1/follow_requests/:id/authorize works" do
user = insert(:user, %{info: %{"locked" => true}})
user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
other_user = insert(:user)
{:ok, activity} = ActivityPub.follow(other_user, user)
@ -890,7 +890,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
end
test "verify_credentials", %{conn: conn} do
user = insert(:user, %{info: %{"default_scope" => "private"}})
user = insert(:user, %{info: %Pleroma.User.Info{default_scope: "private"}})
conn =
conn
@ -902,7 +902,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
end
test "/api/v1/follow_requests/:id/reject works" do
user = insert(:user, %{info: %{"locked" => true}})
user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
other_user = insert(:user)
{:ok, activity} = ActivityPub.follow(other_user, user)