Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into feature/help-test
This commit is contained in:
commit
85bd480be3
22 changed files with 143 additions and 47 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -47,5 +47,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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
|
@ -146,6 +147,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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue