Add banner and background updating.

This commit is contained in:
Roger Braun 2017-08-29 17:18:33 +02:00
parent 5142a8efbb
commit 6c3a0ffae4
3 changed files with 36 additions and 3 deletions

View File

@ -70,6 +70,8 @@ defmodule Pleroma.Web.Router do
post "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials post "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials
post "/account/update_profile", TwitterAPI.Controller, :update_profile 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 post "/account/most_recent_notification", TwitterAPI.Controller, :update_most_recent_notification

View File

@ -190,6 +190,30 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
render(conn, UserView, "show.json", %{user: user, for: user}) render(conn, UserView, "show.json", %{user: user, for: user})
end 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 def external_profile(%{assigns: %{user: current_user}} = conn, %{"profileurl" => uri}) do
with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri), with {:ok, user_map} <- TwitterAPI.get_external_profile(current_user, uri),
response <- Poison.encode!(user_map) do response <- Poison.encode!(user_map) do
@ -229,8 +253,10 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
end end
def update_profile(%{assigns: %{user: user}} = conn, params) do def update_profile(%{assigns: %{user: user}} = conn, params) do
if bio = params["description"] do params = if bio = params["description"] do
params = Map.put(params, "bio", bio) Map.put(params, "bio", bio)
else
params
end end
with changeset <- User.update_changeset(user, params), with changeset <- User.update_changeset(user, params),

View File

@ -11,6 +11,9 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
render_many(users, Pleroma.Web.TwitterAPI.UserView, "user.json", for: user) render_many(users, Pleroma.Web.TwitterAPI.UserView, "user.json", for: user)
end end
defp image_url(%{"url" => [ %{ "href" => href } | t ]}), do: href
defp image_url(_), do: nil
def render("user.json", %{user: user = %User{}} = assigns) do def render("user.json", %{user: user = %User{}} = assigns) do
image = User.avatar_url(user) image = User.avatar_url(user)
following = if assigns[:for] do following = if assigns[:for] do
@ -37,7 +40,9 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
"rights" => %{}, "rights" => %{},
"screen_name" => user.nickname, "screen_name" => user.nickname,
"statuses_count" => user_info[:note_count], "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 end