diff options
author | dtluna <dtluna@openmailbox.org> | 2017-04-16 17:08:17 +0300 |
---|---|---|
committer | dtluna <dtluna@openmailbox.org> | 2017-04-16 17:08:17 +0300 |
commit | 5229b0194477bea6e513d9e8286956a553133545 (patch) | |
tree | fb1655e5dd1f06e52da676c7c4026a5997cfd206 /lib | |
parent | 63f04b314d67decefab3a53b43d04b0347adde13 (diff) | |
parent | e158e32124a62f2c93a8910bf3edc4519c4a13e6 (diff) | |
download | pleroma-5229b0194477bea6e513d9e8286956a553133545.tar.gz |
Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into feature/user-timeline
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/upload.ex | 25 | ||||
-rw-r--r-- | lib/pleroma/user.ex | 1 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 1 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/representers/user_representer.ex | 6 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api_controller.ex | 13 |
6 files changed, 45 insertions, 3 deletions
diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index d22421d37..3aabf8157 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -18,6 +18,31 @@ defmodule Pleroma.Upload do } end + def store(%{"img" => "data:image/" <> image_data}) do + parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data) + data = Base.decode64!(parsed["data"]) + uuid = Ecto.UUID.generate + upload_folder = Path.join(upload_path(), uuid) + File.mkdir_p!(upload_folder) + filename = Base.encode16(:crypto.hash(:sha256, data)) <> ".#{parsed["filetype"]}" + result_file = Path.join(upload_folder, filename) + + File.write!(result_file, data) + + content_type = "image/#{parsed["filetype"]}" + + %{ + "type" => "Image", + "url" => [%{ + "type" => "Link", + "mediaType" => content_type, + "href" => url_for(Path.join(uuid, filename)) + }], + "name" => filename, + "uuid" => uuid + } + end + defp upload_path do Application.get_env(:pleroma, Pleroma.Upload) |> Keyword.fetch!(:uploads) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index ed85447fe..fdcc1b7d5 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -13,6 +13,7 @@ defmodule Pleroma.User do field :password_confirmation, :string, virtual: true field :following, { :array, :string }, default: [] field :ap_id, :string + field :avatar, :map timestamps() end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index eca9f3a05..e9f0dcd32 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -174,7 +174,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do Repo.all(query) end - def upload(%Plug.Upload{} = file) do + def upload(file) do data = Upload.store(file) Repo.insert(%Object{data: data}) end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 555eeec6e..d6cb970e9 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -46,5 +46,6 @@ defmodule Pleroma.Web.Router do post "/favorites/create", TwitterAPI.Controller, :favorite post "/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite post "/statuses/retweet/:id", TwitterAPI.Controller, :retweet + post "/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar end end diff --git a/lib/pleroma/web/twitter_api/representers/user_representer.ex b/lib/pleroma/web/twitter_api/representers/user_representer.ex index d8f98488e..2ee4ee254 100644 --- a/lib/pleroma/web/twitter_api/representers/user_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/user_representer.ex @@ -4,8 +4,10 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do alias Pleroma.User def to_map(user, opts) do - - image = "https://placehold.it/48x48" + image = case user.avatar do + %{"url" => [%{"href" => href} | _]} -> href + _ -> "https://placehold.it/48x48" + end following = if opts[:for] do User.following?(opts[:for], user) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 007b96bc7..36e2d235b 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -3,6 +3,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter} alias Pleroma.{Repo, Activity} + alias Pleroma.Web.ActivityPub.ActivityPub def verify_credentials(%{assigns: %{user: user}} = conn, _params) do response = user |> UserRepresenter.to_json(%{for: user}) @@ -154,6 +155,18 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end + def update_avatar(%{assigns: %{user: user}} = conn, params) do + {:ok, object} = ActivityPub.upload(params) + change = Ecto.Changeset.change(user, %{avatar: object.data}) + {:ok, user} = Repo.update(change) + + response = UserRepresenter.to_map(user, %{for: user}) + |> Poison.encode! + + conn + |> json_reply(200, response) + end + defp bad_request_reply(conn, error_message) do json = Poison.encode!(%{"error" => error_message}) json_reply(conn, 400, json) |