Add status posting via TwAPI.

This commit is contained in:
Roger Braun 2017-03-21 18:17:35 +01:00
parent 9a8850eb9e
commit ad303783af
6 changed files with 42 additions and 19 deletions

View File

@ -21,5 +21,6 @@ defmodule Pleroma.Web.Router do
pipe_through :authenticated_api pipe_through :authenticated_api
post "/account/verify_credentials.json", TwitterAPI.Controller, :verify_credentials post "/account/verify_credentials.json", TwitterAPI.Controller, :verify_credentials
post "/statuses/update.json", TwitterAPI.Controller, :status_update
end end
end end

View File

@ -6,15 +6,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
def create_status(user = %User{}, data = %{}) do def create_status(user = %User{}, data = %{}) do
activity = %{ activity = %{
type: "Create", "type" => "Create",
to: [ "to" => [
User.ap_followers(user), User.ap_followers(user),
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
], ],
actor: User.ap_id(user), "actor" => User.ap_id(user),
object: %{ "object" => %{
type: "Note", "type" => "Note",
content: data.status "content" => data["status"]
} }
} }

View File

@ -1,7 +1,7 @@
defmodule Pleroma.Web.TwitterAPI.Controller do defmodule Pleroma.Web.TwitterAPI.Controller do
use Pleroma.Web, :controller use Pleroma.Web, :controller
alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
def verify_credentials(%{assigns: %{user: user}} = conn, _params) do def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
response = user |> UserRepresenter.to_json response = user |> UserRepresenter.to_json
@ -10,6 +10,12 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|> json_reply(200, response) |> json_reply(200, response)
end end
def status_update(%{assigns: %{user: user}} = conn, status_data) do
{:ok, activity} = TwitterAPI.create_status(user, status_data)
conn
|> json_reply(200, ActivityRepresenter.to_json(activity, %{user: user}))
end
defp json_reply(conn, status, json) do defp json_reply(conn, status, json) do
conn conn
|> put_resp_content_type("application/json") |> put_resp_content_type("application/json")

View File

@ -2,7 +2,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Builders.{UserBuilder, ActivityBuilder} alias Pleroma.Builders.ActivityBuilder
describe "insertion" do describe "insertion" do
test "inserts a given map into the activity database" do test "inserts a given map into the activity database" do
@ -17,7 +17,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
describe "fetch activities" do describe "fetch activities" do
test "retrieves all public activities" do test "retrieves all public activities" do
%{user: user, public: public} = ActivityBuilder.public_and_non_public %{public: public} = ActivityBuilder.public_and_non_public
activities = ActivityPub.fetch_public_activities activities = ActivityPub.fetch_public_activities
assert length(activities) == 1 assert length(activities) == 1

View File

@ -1,7 +1,8 @@
defmodule Pleroma.Web.TwitterAPI.ControllerTest do defmodule Pleroma.Web.TwitterAPI.ControllerTest do
use Pleroma.Web.ConnCase use Pleroma.Web.ConnCase
alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter}
alias Pleroma.Builders.UserBuilder alias Pleroma.Builders.UserBuilder
alias Pleroma.{Repo, Activity}
describe "POST /api/account/verify_credentials" do describe "POST /api/account/verify_credentials" do
setup [:valid_user] setup [:valid_user]
@ -19,6 +20,22 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
end end
end end
describe "POST /statuses/update.json" do
setup [:valid_user]
test "without valid credentials", %{conn: conn} do
conn = post conn, "/api/statuses/update.json"
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: user} do
conn = conn
|> with_credentials(user.nickname, "test")
|> post("/api/statuses/update.json", %{ status: "Nice meme." })
assert json_response(conn, 200) == ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
end
end
defp valid_user(_context) do defp valid_user(_context) do
{ :ok, user } = UserBuilder.insert { :ok, user } = UserBuilder.insert
[user: user] [user: user]

View File

@ -3,22 +3,21 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
alias Pleroma.Builders.{UserBuilder, ActivityBuilder} alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.{Activity, User} alias Pleroma.{Activity, User}
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter} alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.Web.ActivityPub.ActivityPub
test "create a status" do test "create a status" do
user = UserBuilder.build user = UserBuilder.build
input = %{ input = %{
status: "Hello again." "status" => "Hello again."
} }
{ :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input) { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
assert get_in(activity.data, [:object, :content]) == "Hello again." assert get_in(activity.data, ["object", "content"]) == "Hello again."
assert get_in(activity.data, [:object, :type]) == "Note" assert get_in(activity.data, ["object", "type"]) == "Note"
assert get_in(activity.data, [:actor]) == User.ap_id(user) assert get_in(activity.data, ["actor"]) == User.ap_id(user)
assert Enum.member?(get_in(activity.data, [:to]), User.ap_followers(user)) 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"]), "https://www.w3.org/ns/activitystreams#Public")
end end
test "fetch public activities" do test "fetch public activities" do