aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authordtluna <dtluna@openmailbox.org>2017-04-16 17:13:42 +0300
committerdtluna <dtluna@openmailbox.org>2017-04-16 17:13:42 +0300
commitf6547f7b7fa72a9730773012394c522ac6b17398 (patch)
treebe5af86ddc8c353cdc90e1bfac67bbc1f66f9b1c /lib
parent9a8c348aed7772c9e2173163687e63943cf491fb (diff)
parente158e32124a62f2c93a8910bf3edc4519c4a13e6 (diff)
downloadpleroma-f6547f7b7fa72a9730773012394c522ac6b17398.tar.gz
Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into feature/unfollow-by-screen-name
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/upload.ex25
-rw-r--r--lib/pleroma/user.ex1
-rw-r--r--lib/pleroma/web/activity_pub/activity_pub.ex2
-rw-r--r--lib/pleroma/web/router.ex1
-rw-r--r--lib/pleroma/web/twitter_api/representers/user_representer.ex6
-rw-r--r--lib/pleroma/web/twitter_api/twitter_api_controller.ex13
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 0d3360ee1..125473b96 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -167,7 +167,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 2749be5e9..2d7c25b50 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -45,5 +45,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 56b8e5b3b..b70af3b09 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})
@@ -142,6 +143,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 json_reply(conn, status, json) do
conn
|> put_resp_content_type("application/json")