Replaced media actions
This commit is contained in:
parent
93e8382d75
commit
6a3efd27fa
|
@ -57,8 +57,8 @@ defmodule Pleroma.Web.Router do
|
|||
post "/friendships/create", TwitterAPI.UserController, :follow
|
||||
post "/friendships/destroy", TwitterAPI.UserController, :unfollow
|
||||
|
||||
post "/statusnet/media/upload", TwitterAPI.Controller, :upload
|
||||
post "/media/upload", TwitterAPI.Controller, :upload_json
|
||||
post "/statusnet/media/upload", TwitterAPI.MediaController, :upload
|
||||
post "/media/upload", TwitterAPI.MediaController, :upload_json
|
||||
|
||||
post "/favorites/create/:id", TwitterAPI.StatusController, :favorite
|
||||
post "/favorites/create", TwitterAPI.StatusController, :favorite
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.MediaController do
|
||||
use Pleroma.Web, :controller
|
||||
alias Pleroma.Object
|
||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
|
||||
def upload(conn, %{"media" => media = %Plug.Upload{}}) do
|
||||
{:ok, %Object{id: id, data: %{"url" => %{"href" => href, "type" => type}}}} =
|
||||
ActivityPub.upload(media)
|
||||
response = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rsp stat="ok" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||
<mediaid>#{id}</mediaid>
|
||||
<media_id>#{id}</media_id>
|
||||
<media_id_string>#{id}</media_id_string>
|
||||
<media_url>#{href}</media_url>
|
||||
<mediaurl>#{href}</mediaurl>
|
||||
<atom:link rel="enclosure" href="#{href}" type="#{type}"></atom:link>
|
||||
</rsp>
|
||||
"""
|
||||
|
||||
conn
|
||||
|> put_resp_content_type("application/atom+xml")
|
||||
|> send_resp(200, response)
|
||||
end
|
||||
|
||||
def upload_json(conn, %{"media" => media = %Plug.Upload{}}) do
|
||||
{:ok, %Object{id: id, data: %{"url" => %{"href" => href}}}} = ActivityPub.upload(media)
|
||||
|
||||
json conn, %{
|
||||
media_id: id,
|
||||
media_id_string: "#{id}}",
|
||||
media_url: href,
|
||||
size: 0
|
||||
}
|
||||
end
|
||||
end
|
|
@ -87,37 +87,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||
ActivityPub.create(to, user, context, object, additional, data)
|
||||
end
|
||||
|
||||
def upload(%Plug.Upload{} = file, format \\ "xml") do
|
||||
{:ok, object} = ActivityPub.upload(file)
|
||||
|
||||
url = List.first(object.data["url"])
|
||||
href = url["href"]
|
||||
type = url["mediaType"]
|
||||
|
||||
case format do
|
||||
"xml" ->
|
||||
# Fake this as good as possible...
|
||||
"""
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rsp stat="ok" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||
<mediaid>#{object.id}</mediaid>
|
||||
<media_id>#{object.id}</media_id>
|
||||
<media_id_string>#{object.id}</media_id_string>
|
||||
<media_url>#{href}</media_url>
|
||||
<mediaurl>#{href}</mediaurl>
|
||||
<atom:link rel="enclosure" href="#{href}" type="#{type}"></atom:link>
|
||||
</rsp>
|
||||
"""
|
||||
"json" ->
|
||||
%{
|
||||
media_id: object.id,
|
||||
media_id_string: "#{object.id}}",
|
||||
media_url: href,
|
||||
size: 0
|
||||
} |> Poison.encode!
|
||||
end
|
||||
end
|
||||
|
||||
def parse_mentions(text) do
|
||||
# Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
|
||||
regex = ~r/@[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@?[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
defmodule Pleroma.Web.TwitterAPI.Controller do
|
||||
use Pleroma.Web, :controller
|
||||
alias Pleroma.Web.TwitterAPI.TwitterAPI
|
||||
|
||||
def upload(conn, %{"media" => media}) do
|
||||
response = TwitterAPI.upload(media)
|
||||
conn
|
||||
|> put_resp_content_type("application/atom+xml")
|
||||
|> send_resp(200, response)
|
||||
end
|
||||
|
||||
def upload_json(conn, %{"media" => media}) do
|
||||
response = TwitterAPI.upload(media, "json")
|
||||
conn
|
||||
|> json_reply(200, response)
|
||||
end
|
||||
|
||||
defp json_reply(conn, status, json) do
|
||||
conn
|
||||
|> put_resp_content_type("application/json")
|
||||
|> send_resp(status, json)
|
||||
end
|
||||
end
|
|
@ -7,7 +7,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||
import Pleroma.Factory
|
||||
|
||||
test "create a status" do
|
||||
user = UserBuilder.build(%{ap_id: "142344"})
|
||||
%User{ap_id: user_ap_id} = user = UserBuilder.build(%{ap_id: "142344"})
|
||||
_mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
|
||||
|
||||
object_data = %{
|
||||
|
@ -22,31 +22,34 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||
"uuid" => 1
|
||||
}
|
||||
|
||||
object = Repo.insert!(%Object{data: object_data})
|
||||
%Object{id: object_id} = Repo.insert!(%Object{data: object_data})
|
||||
|
||||
input = %{
|
||||
"status" => "Hello again, @shp.<script></script>\nThis is on another line.",
|
||||
"media_ids" => [object.id]
|
||||
"media_ids" => [object_id]
|
||||
}
|
||||
{:ok, %Activity{local: true, data: %{
|
||||
"context" => context, "to" => to, "actor" => actor, "object" => object = %{
|
||||
"id" => ap_id, "content" => content, "type" => "Note", "actor" => obj_actor,
|
||||
"context" => obj_context, "attachment" => attachment
|
||||
}
|
||||
}}} = TwitterAPI.create_status(user, input)
|
||||
|
||||
{ :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
|
||||
assert content == "Hello again, <a href='shp'>@shp</a>.<br>This is on another line.<br><a href='http://example.org/image.jpg' class='attachment'>http://example.org/image.jpg</a>"
|
||||
|
||||
assert get_in(activity.data, ["object", "content"]) == "Hello again, <a href='shp'>@shp</a>.<br>This is on another line.<br><a href='http://example.org/image.jpg' class='attachment'>http://example.org/image.jpg</a>"
|
||||
assert get_in(activity.data, ["object", "type"]) == "Note"
|
||||
assert get_in(activity.data, ["object", "actor"]) == user.ap_id
|
||||
assert get_in(activity.data, ["actor"]) == user.ap_id
|
||||
assert Enum.member?(get_in(activity.data, ["to"]), User.ap_followers(user))
|
||||
assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public")
|
||||
assert Enum.member?(get_in(activity.data, ["to"]), "shp")
|
||||
assert activity.local == true
|
||||
assert user_ap_id == actor
|
||||
assert user_ap_id == obj_actor
|
||||
|
||||
# Add a context
|
||||
assert is_binary(get_in(activity.data, ["context"]))
|
||||
assert is_binary(get_in(activity.data, ["object", "context"]))
|
||||
assert is_binary(context)
|
||||
assert is_binary(obj_context)
|
||||
|
||||
assert is_list(activity.data["object"]["attachment"])
|
||||
assert Enum.member?(to, User.ap_followers(user))
|
||||
assert Enum.member?(to, "https://www.w3.org/ns/activitystreams#Public")
|
||||
assert Enum.member?(to, "shp")
|
||||
|
||||
assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
|
||||
assert is_list(attachment)
|
||||
|
||||
assert object == Object.get_by_ap_id(ap_id).data
|
||||
end
|
||||
|
||||
test "create a status that is a reply" do
|
||||
|
@ -55,14 +58,14 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||
"status" => "Hello again."
|
||||
}
|
||||
|
||||
{ :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
|
||||
{:ok, activity = %Activity{id: id}} = TwitterAPI.create_status(user, input)
|
||||
|
||||
input = %{
|
||||
"status" => "Here's your (you).",
|
||||
"in_reply_to_status_id" => activity.id
|
||||
"in_reply_to_status_id" => id
|
||||
}
|
||||
|
||||
{ :ok, reply = %Activity{} } = TwitterAPI.create_status(user, input)
|
||||
{:ok, reply = %Activity{}} = TwitterAPI.create_status(user, input)
|
||||
|
||||
assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
|
||||
assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"])
|
||||
|
@ -71,14 +74,6 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||
assert Enum.member?(get_in(reply.data, ["to"]), "some_cool_id")
|
||||
end
|
||||
|
||||
test "upload a file" do
|
||||
file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
|
||||
|
||||
response = TwitterAPI.upload(file)
|
||||
|
||||
assert is_binary(response)
|
||||
end
|
||||
|
||||
test "it can parse mentions and return the relevant users" do
|
||||
text = "@gsimg According to @archaeme , that is @daggsy."
|
||||
|
||||
|
|
Loading…
Reference in New Issue