From 6c3a0ffae4d524619e969121dd719cafca2f1215 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 29 Aug 2017 17:18:33 +0200 Subject: [PATCH] Add banner and background updating. --- lib/pleroma/web/router.ex | 2 ++ .../web/twitter_api/twitter_api_controller.ex | 30 ++++++++++++++++++++-- lib/pleroma/web/twitter_api/views/user_view.ex | 7 ++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index ab849aef5..2b22140ee 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -70,6 +70,8 @@ defmodule Pleroma.Web.Router do post "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials post "/account/update_profile", TwitterAPI.Controller, :update_profile + post "/account/update_profile_banner", TwitterAPI.Controller, :update_banner + post "/qvitter/update_background_image", TwitterAPI.Controller, :update_background post "/account/most_recent_notification", TwitterAPI.Controller, :update_most_recent_notification diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 335cb3e7c..3580e48d2 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -190,6 +190,30 @@ defmodule Pleroma.Web.TwitterAPI.Controller do render(conn, UserView, "show.json", %{user: user, for: user}) end + def update_banner(%{assigns: %{user: user}} = conn, params) do + with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}), + new_info <- Map.put(user.info, "banner", object.data), + change <- User.info_changeset(user, %{info: new_info}), + {:ok, user} <- Repo.update(change) do + %{"url" => [ %{ "href" => href } | t ]} = object.data + response = %{ url: href } |> Poison.encode! + conn + |> json_reply(200, response) + end + end + + def update_background(%{assigns: %{user: user}} = conn, params) do + with {:ok, object} <- ActivityPub.upload(params), + new_info <- Map.put(user.info, "background", object.data), + change <- User.info_changeset(user, %{info: new_info}), + {:ok, user} <- Repo.update(change) do + %{"url" => [ %{ "href" => href } | t ]} = object.data + response = %{ url: href } |> Poison.encode! + conn + |> json_reply(200, response) + end + end + def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri), response <- Poison.encode!(user_map) do @@ -229,8 +253,10 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end def update_profile(%{assigns: %{user: user}} = conn, params) do - if bio = params["description"] do - params = Map.put(params, "bio", bio) + params = if bio = params["description"] do + Map.put(params, "bio", bio) + else + params end with changeset <- User.update_changeset(user, params), diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index 24d2981fa..932c018a6 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -11,6 +11,9 @@ defmodule Pleroma.Web.TwitterAPI.UserView do render_many(users, Pleroma.Web.TwitterAPI.UserView, "user.json", for: user) end + defp image_url(%{"url" => [ %{ "href" => href } | t ]}), do: href + defp image_url(_), do: nil + def render("user.json", %{user: user = %User{}} = assigns) do image = User.avatar_url(user) following = if assigns[:for] do @@ -37,7 +40,9 @@ defmodule Pleroma.Web.TwitterAPI.UserView do "rights" => %{}, "screen_name" => user.nickname, "statuses_count" => user_info[:note_count], - "statusnet_profile_url" => user.ap_id + "statusnet_profile_url" => user.ap_id, + "cover_photo" => image_url(user.info["banner"]), + "background_image" => image_url(user.info["background"]) } end