Add status posting via TwAPI.
authorRoger Braun <roger@rogerbraun.net>
Tue, 21 Mar 2017 17:17:35 +0000 (18:17 +0100)
committerRoger Braun <roger@rogerbraun.net>
Tue, 21 Mar 2017 17:17:35 +0000 (18:17 +0100)
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/activity_pub/activity_pub_test.exs
test/web/twitter_api/twitter_api_controller_test.exs
test/web/twitter_api/twitter_api_test.exs

index 83e4253ce9e532204ccc7e28e5e627a5f58c8a66..1e865b358b819af9a2c3725e206d51dcff7a660a 100644 (file)
@@ -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
index 16e05ded36707505e7fcdc63aaf3abbcfec2b423..9e3251df6db6ece674fdd7170b4666e84fe9bdaf 100644 (file)
@@ -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"]
       }
     }
 
index 836013cff9b4a4c3a4dccf149392e825a316d2c6..1f0a547d0f9e11a3dbd45a03e9895bf8a26e39ad 100644 (file)
@@ -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")
index 5ea2d40f11c527184869f74ae00e9bd5b03191c3..aa4abbec19b0a212a9f706a70a51e111cb6fd1e9 100644 (file)
@@ -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
index 81f1e893e08c7d91c45ad0cf4655a7d12beedeeb..c3241817d0fca3752dfc9e5399b1b318ee4df0df 100644 (file)
@@ -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]
index aecf0996957afe356f49d126d94592dc9ac1de16..8016f45379235dc2788052bda43f4716006cd471 100644 (file)
@@ -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