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

View File

@ -509,8 +509,8 @@ defp restrict_recent(query, _) do
end end
defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
blocks = info["blocks"] || [] blocks = info.blocks || []
domain_blocks = info["domain_blocks"] || [] domain_blocks = info.domain_blocks || []
from( from(
activity in query, activity in query,
@ -678,7 +678,7 @@ def publish(actor, activity) do
remote_inboxes = remote_inboxes =
(Pleroma.Web.Salmon.remote_users(activity) ++ followers) (Pleroma.Web.Salmon.remote_users(activity) ++ followers)
|> Enum.filter(fn user -> User.ap_enabled?(user) end) |> 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"] (is_map(data["endpoints"]) && Map.get(data["endpoints"], "sharedInbox")) || data["inbox"]
end) end)
|> Enum.uniq() |> 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 # the instance itself is not a Person, but instead an Application
def render("user.json", %{user: %{nickname: nil} = user}) do def render("user.json", %{user: %{nickname: nil} = user}) do
{:ok, user} = WebFinger.ensure_keys_present(user) {: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_entry_encode(:SubjectPublicKeyInfo, public_key)
public_key = :public_key.pem_encode([public_key]) public_key = :public_key.pem_encode([public_key])
@ -40,7 +40,7 @@ def render("user.json", %{user: %{nickname: nil} = user}) do
def render("user.json", %{user: user}) do def render("user.json", %{user: user}) do
{:ok, user} = WebFinger.ensure_keys_present(user) {: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_entry_encode(:SubjectPublicKeyInfo, public_key)
public_key = :public_key.pem_encode([public_key]) public_key = :public_key.pem_encode([public_key])
@ -55,7 +55,7 @@ def render("user.json", %{user: user}) do
"name" => user.name, "name" => user.name,
"summary" => user.bio, "summary" => user.bio,
"url" => user.ap_id, "url" => user.ap_id,
"manuallyApprovesFollowers" => user.info["locked"] || false, "manuallyApprovesFollowers" => user.info.locked,
"publicKey" => %{ "publicKey" => %{
"id" => "#{user.ap_id}#main-key", "id" => "#{user.ap_id}#main-key",
"owner" => user.ap_id, "owner" => user.ap_id,
@ -72,7 +72,7 @@ def render("user.json", %{user: user}) do
"type" => "Image", "type" => "Image",
"url" => User.banner_url(user) "url" => User.banner_url(user)
}, },
"tag" => user.info["source_data"]["tag"] || [] "tag" => user.info.source_data["tag"] || []
} }
|> Map.merge(Utils.make_json_ld_header()) |> Map.merge(Utils.make_json_ld_header())
end end

View File

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

View File

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

View File

@ -14,10 +14,10 @@ def render("account.json", %{user: user} = opts) do
image = User.avatar_url(user) |> MediaProxy.url() image = User.avatar_url(user) |> MediaProxy.url()
header = User.banner_url(user) |> MediaProxy.url() header = User.banner_url(user) |> MediaProxy.url()
user_info = User.user_info(user) 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 = emojis =
(user.info["source_data"]["tag"] || []) (user.info.source_data["tag"] || [])
|> Enum.filter(fn %{"type" => t} -> t == "Emoji" end) |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
|> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} -> |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
%{ %{
@ -29,7 +29,7 @@ def render("account.json", %{user: user} = opts) do
end) end)
fields = fields =
(user.info["source_data"]["attachment"] || []) (user.info.source_data["attachment"] || [])
|> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end) |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
|> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end) |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)

View File

@ -252,7 +252,7 @@ test "verify_credentials", %{conn: conn} do
end end
test "verify_credentials default scope unlisted", %{conn: conn} do 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 =
conn conn
@ -845,7 +845,7 @@ test "returns the relationships for the current user", %{conn: conn} do
describe "locked accounts" do describe "locked accounts" do
test "/api/v1/follow_requests works" 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) other_user = insert(:user)
{:ok, activity} = ActivityPub.follow(other_user, user) {:ok, activity} = ActivityPub.follow(other_user, user)
@ -865,7 +865,7 @@ test "/api/v1/follow_requests works" do
end end
test "/api/v1/follow_requests/:id/authorize works" do 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) other_user = insert(:user)
{:ok, activity} = ActivityPub.follow(other_user, user) {:ok, activity} = ActivityPub.follow(other_user, user)
@ -890,7 +890,7 @@ test "/api/v1/follow_requests/:id/authorize works" do
end end
test "verify_credentials", %{conn: conn} do 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 =
conn conn
@ -902,7 +902,7 @@ test "verify_credentials", %{conn: conn} do
end end
test "/api/v1/follow_requests/:id/reject works" do 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) other_user = insert(:user)
{:ok, activity} = ActivityPub.follow(other_user, user) {:ok, activity} = ActivityPub.follow(other_user, user)