From ad303783afc0be046d0bcb4b70b3bce9aabac27f Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 21 Mar 2017 18:17:35 +0100 Subject: [PATCH] Add status posting via TwAPI. --- lib/pleroma/web/router.ex | 1 + lib/pleroma/web/twitter_api/twitter_api.ex | 12 ++++++------ .../web/twitter_api/twitter_api_controller.ex | 10 ++++++++-- test/web/activity_pub/activity_pub_test.exs | 4 ++-- .../twitter_api_controller_test.exs | 19 ++++++++++++++++++- test/web/twitter_api/twitter_api_test.exs | 15 +++++++-------- 6 files changed, 42 insertions(+), 19 deletions(-) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 83e4253ce..1e865b358 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -21,5 +21,6 @@ defmodule Pleroma.Web.Router do pipe_through :authenticated_api post "/account/verify_credentials.json", TwitterAPI.Controller, :verify_credentials + post "/statuses/update.json", TwitterAPI.Controller, :status_update end end diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 16e05ded3..9e3251df6 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -6,15 +6,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do def create_status(user = %User{}, data = %{}) do activity = %{ - type: "Create", - to: [ + "type" => "Create", + "to" => [ User.ap_followers(user), "https://www.w3.org/ns/activitystreams#Public" ], - actor: User.ap_id(user), - object: %{ - type: "Note", - content: data.status + "actor" => User.ap_id(user), + "object" => %{ + "type" => "Note", + "content" => data["status"] } } diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 836013cff..1f0a547d0 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -1,7 +1,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do use Pleroma.Web, :controller - - alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter + alias Pleroma.Web.TwitterAPI.TwitterAPI + alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter} def verify_credentials(%{assigns: %{user: user}} = conn, _params) do response = user |> UserRepresenter.to_json @@ -10,6 +10,12 @@ defmodule Pleroma.Web.TwitterAPI.Controller do |> json_reply(200, response) 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 conn |> put_resp_content_type("application/json") diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 5ea2d40f1..aa4abbec1 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -2,7 +2,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do use Pleroma.DataCase alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Activity - alias Pleroma.Builders.{UserBuilder, ActivityBuilder} + alias Pleroma.Builders.ActivityBuilder describe "insertion" 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 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 assert length(activities) == 1 diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 81f1e893e..c3241817d 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1,7 +1,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do use Pleroma.Web.ConnCase - alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter + alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter} alias Pleroma.Builders.UserBuilder + alias Pleroma.{Repo, Activity} describe "POST /api/account/verify_credentials" do setup [:valid_user] @@ -19,6 +20,22 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do 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 { :ok, user } = UserBuilder.insert [user: user] diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index aecf09969..8016f4537 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -3,22 +3,21 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do alias Pleroma.Builders.{UserBuilder, ActivityBuilder} alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.{Activity, User} - alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter} - alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter test "create a status" do user = UserBuilder.build input = %{ - status: "Hello again." + "status" => "Hello again." } { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input) - assert get_in(activity.data, [:object, :content]) == "Hello again." - assert get_in(activity.data, [:object, :type]) == "Note" - 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]), "https://www.w3.org/ns/activitystreams#Public") + assert get_in(activity.data, ["object", "content"]) == "Hello again." + assert get_in(activity.data, ["object", "type"]) == "Note" + 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"]), "https://www.w3.org/ns/activitystreams#Public") end test "fetch public activities" do