From 93f2dc19d99375356d986651490cb9f496f18928 Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 1 Dec 2018 12:00:53 +0100 Subject: [PATCH] TwitterAPI: Fix banner and background update. --- lib/pleroma/user/info.ex | 16 ++++++++-- .../web/twitter_api/twitter_api_controller.ex | 14 +++++---- .../twitter_api_controller_test.exs | 30 +++++++++++++++++++ 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index 8a938e514..284645934 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -23,7 +23,18 @@ defmodule Pleroma.User.Info do field(:uri, :string, default: nil) field(:topic, :string, default: nil) field(:hub, :string, default: nil) - # topic, subject, salmon, subscribe_address, uri + + # Found in the wild + # ap_id -> Where is this used? + # bio -> Where is this used? + # avatar -> Where is this used? + # fqn -> Where is this used? + # host -> Where is this used? + # name -> Where is this used? + # nickname -> Where is this used? + # salmon -> Where is this used? + # subject _> Where is this used? + # subscribe_address -> Where is this used? end def set_activation_status(info, deactivated) do @@ -124,7 +135,8 @@ def profile_update(info, params) do |> cast(params, [ :locked, :no_rich_text, - :default_scope + :default_scope, + :banner ]) end diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 8f9d6c905..ff644dd79 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -300,9 +300,10 @@ def update_avatar(%{assigns: %{user: user}} = conn, params) do def update_banner(%{assigns: %{user: user}} = conn, params) do with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}, type: :banner), - 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 + new_info <- %{"banner" => object.data}, + info_cng <- User.Info.profile_update(user.info, new_info), + changeset <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng), + {:ok, user} <- User.update_and_set_cache(changeset) do CommonAPI.update(user) %{"url" => [%{"href" => href} | _]} = object.data response = %{url: href} |> Jason.encode!() @@ -314,9 +315,10 @@ def update_banner(%{assigns: %{user: user}} = conn, params) do def update_background(%{assigns: %{user: user}} = conn, params) do with {:ok, object} <- ActivityPub.upload(params, type: :background), - new_info <- Map.put(user.info, "background", object.data), - change <- User.info_changeset(user, %{info: new_info}), - {:ok, _user} <- User.update_and_set_cache(change) do + new_info <- %{"background" => object.data}, + info_cng <- User.Info.profile_update(user.info, new_info), + changeset <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng), + {:ok, _user} <- User.update_and_set_cache(changeset) do %{"url" => [%{"href" => href} | _]} = object.data response = %{url: href} |> Jason.encode!() diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index a76112e37..f7c6e6187 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -12,6 +12,36 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do import Pleroma.Factory + describe "POST /api/account/update_profile_banner" do + test "it updates the banner", %{conn: conn} do + user = insert(:user) + + new_banner = + "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7" + + response = + conn + |> assign(:user, user) + |> post(authenticated_twitter_api__path(conn, :update_banner), %{"banner" => new_banner}) + |> json_response(200) + end + end + + describe "POST /api/qvitter/update_background_image" do + test "it updates the background", %{conn: conn} do + user = insert(:user) + + new_bg = + "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7" + + response = + conn + |> assign(:user, user) + |> post(authenticated_twitter_api__path(conn, :update_background), %{"img" => new_bg}) + |> json_response(200) + end + end + describe "POST /api/account/verify_credentials" do setup [:valid_user]