Add statuses/mentions endpoint
authordtluna <dtluna@openmailbox.org>
Thu, 20 Apr 2017 10:53:53 +0000 (13:53 +0300)
committerdtluna <dtluna@openmailbox.org>
Thu, 20 Apr 2017 12:18:54 +0000 (15:18 +0300)
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/twitter_api/twitter_api_controller_test.exs
test/web/twitter_api/twitter_api_test.exs

index b28813a456997dfee793eed9200f84b8703e49af..28a54ec955b3a4d80e78c448a883a448c96e1c66 100644 (file)
@@ -23,12 +23,15 @@ defmodule Pleroma.Web.Router do
     pipe_through :api
 
     get "/help/test", TwitterAPI.Controller, :help_test
+    get "/statusnet/config", TwitterAPI.Controller, :config
+
     get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
     get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline
     get "/statuses/user_timeline", TwitterAPI.Controller, :user_timeline
+
     get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
     get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
-    get "/statusnet/config", TwitterAPI.Controller, :config
+
     post "/account/register", TwitterAPI.Controller, :register
   end
 
@@ -37,17 +40,24 @@ defmodule Pleroma.Web.Router do
 
     get "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials
     post "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials
-    post "/statuses/update", TwitterAPI.Controller, :status_update
+
     get "/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline
     get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline
+    get "/statuses/mentions", TwitterAPI.Controller, :mentions_timeline
+
+    post "/statuses/update", TwitterAPI.Controller, :status_update
+    post "/statuses/retweet/:id", TwitterAPI.Controller, :retweet
+
     post "/friendships/create", TwitterAPI.Controller, :follow
     post "/friendships/destroy", TwitterAPI.Controller, :unfollow
+
     post "/statusnet/media/upload", TwitterAPI.Controller, :upload
     post "/media/upload", TwitterAPI.Controller, :upload_json
+
     post "/favorites/create/:id", TwitterAPI.Controller, :favorite
     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
index 9b9ee0bd1f922b52b4457a24f6e803bc90a42bd6..ce29d46a1420add15ca6f0d965c99545c08b4634 100644 (file)
@@ -85,6 +85,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     |> activities_to_statuses(%{for: user})
   end
 
+  def fetch_mentions(user, opts \\ %{}) do
+    ActivityPub.fetch_activities([user.ap_id], opts)
+    |> activities_to_statuses(%{for: user})
+  end
+
   def fetch_conversation(user, id) do
     query = from activity in Activity,
       where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
index 8b5e6270bc95fc15d9b654011927d1a31fc64ca4..8ea54852dd6f90eb2bb380f9835c4bde1413870c 100644 (file)
@@ -57,6 +57,14 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     end
   end
 
+  def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
+    statuses = TwitterAPI.fetch_mentions(user, params)
+    {:ok, json} = Poison.encode(statuses)
+
+    conn
+    |> json_reply(200, json)
+  end
+
   def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
     case TwitterAPI.follow(user, followed_id) do
       { :ok, user, followed, _activity } ->
index 8bf3fe10772e4fb596805af4b2ba4000fa1e7b4b..e4ed6cd543c14577ec0be847e300ff207fea5ecb 100644 (file)
@@ -114,6 +114,27 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     end
   end
 
+  describe "GET /statuses/mentions.json" do
+    setup [:valid_user]
+    test "without valid credentials", %{conn: conn} do
+      conn = get conn, "/api/statuses/mentions.json"
+      assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
+    end
+
+    test "with credentials", %{conn: conn, user: current_user} do
+      {:ok, activity} = ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user})
+
+      conn = conn
+        |> with_credentials(current_user.nickname, "test")
+        |> get("/api/statuses/mentions.json")
+
+      response = json_response(conn, 200)
+
+      assert length(response) == 1
+      assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user, mentioned: [current_user]})
+    end
+  end
+
   describe "GET /statuses/user_timeline.json" do
     setup [:valid_user]
     test "without any params", %{conn: conn} do
index 196822f99284bd1fd0ea5bb8ae30297e06410527..273093ebad5814f309799bf2b495502cd6df9bfc 100644 (file)
@@ -102,6 +102,17 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
     assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]})
   end
 
+  test "fetch user's mentions" do
+    user = insert(:user)
+    {:ok, activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
+    activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
+
+    statuses = TwitterAPI.fetch_mentions(user)
+
+    assert length(statuses) == 1
+    assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user, mentioned: [user]})
+  end
+
   test "get a user by params" do
     user1_result = {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
     {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})