From a7e74ee01232897a6172f6462cec282a451c319a Mon Sep 17 00:00:00 2001 From: dtluna Date: Mon, 10 Apr 2017 20:08:14 +0300 Subject: [PATCH 1/5] Add user timeline --- lib/pleroma/web/router.ex | 1 + .../web/twitter_api/twitter_api_controller.ex | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 52030d684..8e6a2b012 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -33,6 +33,7 @@ def user_fetcher(username) do post "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials post "/statuses/update", TwitterAPI.Controller, :status_update get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline + get "/statuses/user_timeline", TwitterAPI.Controller, :user_timeline post "/friendships/create", TwitterAPI.Controller, :follow post "/friendships/destroy", TwitterAPI.Controller, :unfollow post "/statusnet/media/upload", TwitterAPI.Controller, :upload diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index f2c893e96..741689ebf 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -43,6 +43,26 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do |> json_reply(200, json) end + def user_timeline(conn, %{ "user_id" => user_id } = params) do + user = Repo.get(User, user_id) + conn = Map.merge(conn, %{assigns: %{user: user}}) + params = Map.delete(params, "user_id") + + friends_timeline(conn, params) + end + + def user_timeline(conn, %{ "screen_name" => nickname} = params) do + user = Repo.get_by!(User, nickname: nickname) + conn = Map.merge(conn, %{assigns: %{user: user}}) + params = Map.delete(params, "screen_name") + + friends_timeline(conn, params) + end + + def user_timeline(conn, params) do + friends_timeline(conn, params) + end + def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do { :ok, _user, follower, _activity } = TwitterAPI.follow(user, followed_id) From 18cd04ade576969b25e96733dddb8255fbf13a6d Mon Sep 17 00:00:00 2001 From: dtluna Date: Fri, 14 Apr 2017 16:09:13 +0300 Subject: [PATCH 2/5] Add user timeline --- lib/pleroma/web/activity_pub/activity_pub.ex | 7 ++++++ lib/pleroma/web/twitter_api/twitter_api.ex | 17 ++++++++++++++ .../web/twitter_api/twitter_api_controller.ex | 22 ++++--------------- test/web/twitter_api/twitter_api_test.exs | 18 +++++++++++++++ 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 75e4101f2..406ca5c42 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -60,6 +60,13 @@ def fetch_activities(recipients, opts \\ %{}) do query end + query = if opts["actor_id"] do + from activity in query, + where: fragment("? @> ?", activity.data, ^%{actor: opts["actor_id"]}) + else + query + end + Repo.all(query) |> Enum.reverse end diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 0217b28d6..064120031 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -78,6 +78,12 @@ def fetch_public_statuses(user, opts \\ %{}) do |> activities_to_statuses(%{for: user}) end + def fetch_user_statuses(user, opts \\ %{}) do + target = get_user(user, opts) + ActivityPub.fetch_activities([], Map.merge(opts, %{"actor_id" => target.ap_id})) + |> activities_to_statuses(%{for: user}) + end + def fetch_conversation(user, id) do query = from activity in Activity, where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}), @@ -188,4 +194,15 @@ defp activity_to_status(activity, opts) do defp make_date do DateTime.utc_now() |> DateTime.to_iso8601 end + + defp get_user(user, params) do + case params do + %{ "user_id" => user_id } -> + Repo.get(User, user_id) + %{ "screen_name" => nickname } -> + Repo.get_by!(User, nickname: nickname) + _ -> + user + end + end end diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 11e7b3bdf..1d9d7f540 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -43,24 +43,10 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do |> json_reply(200, json) end - def user_timeline(conn, %{ "user_id" => user_id } = params) do - user = Repo.get(User, user_id) - conn = Map.merge(conn, %{assigns: %{user: user}}) - params = Map.delete(params, "user_id") - - friends_timeline(conn, params) - end - - def user_timeline(conn, %{ "screen_name" => nickname} = params) do - user = Repo.get_by!(User, nickname: nickname) - conn = Map.merge(conn, %{assigns: %{user: user}}) - params = Map.delete(params, "screen_name") - - friends_timeline(conn, params) - end - - def user_timeline(conn, params) do - friends_timeline(conn, params) + def user_timeline(%{assigns: %{user: user}} = conn, params) do + statuses = TwitterAPI.fetch_user_statuses(user, params) + conn + |> json_reply(200, statuses |> Poison.encode!) end def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index e8853a910..558f26e74 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -96,6 +96,24 @@ test "fetch friends' statuses" do assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: activity_user, mentioned: [user]}) end + test "fetch user's statuses" do + {:ok, user1} = UserBuilder.insert(%{ap_id: "some id"}) + {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2"}) + + {:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1}) + {:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2}) + + user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{}) + + assert length(user1_statuses) == 1 + assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1}) + + user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"screen_name" => user2.nickname }) + + assert length(user2_statuses) == 1 + assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2}) + end + test "fetch a single status" do {:ok, activity} = ActivityBuilder.insert() {:ok, user} = UserBuilder.insert() From 4b4ece2c744dc23c5f4d2ef985fe858e23d880b1 Mon Sep 17 00:00:00 2001 From: dtluna Date: Sun, 16 Apr 2017 16:44:30 +0300 Subject: [PATCH 3/5] Add user timeline fetching without credentials --- lib/pleroma/web/router.ex | 2 +- lib/pleroma/web/twitter_api/twitter_api.ex | 43 ++++++++++++------- .../web/twitter_api/twitter_api_controller.ex | 17 ++++++-- test/web/twitter_api/twitter_api_test.exs | 22 ++++++++-- 4 files changed, 61 insertions(+), 23 deletions(-) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8aaff42f0..555eeec6e 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -23,6 +23,7 @@ def user_fetcher(username) do pipe_through :api 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 @@ -37,7 +38,6 @@ def user_fetcher(username) do 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/user_timeline", TwitterAPI.Controller, :user_timeline post "/friendships/create", TwitterAPI.Controller, :follow post "/friendships/destroy", TwitterAPI.Controller, :unfollow post "/statusnet/media/upload", TwitterAPI.Controller, :upload diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 495697a98..8a7ea87c8 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -81,8 +81,7 @@ def fetch_public_statuses(user, opts \\ %{}) do end def fetch_user_statuses(user, opts \\ %{}) do - target = get_user(user, opts) - ActivityPub.fetch_activities([], Map.merge(opts, %{"actor_id" => target.ap_id})) + ActivityPub.fetch_activities([], opts) |> activities_to_statuses(%{for: user}) end @@ -250,12 +249,37 @@ def register_user(params) do {:ok, UserRepresenter.to_map(user)} else {:error, changeset} -> - errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} -> msg end) - |> Poison.encode! + errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end) + |> Poison.encode! {:error, %{error: errors}} end end + def get_user(user, params) do + case params do + %{ "user_id" => user_id } -> + case target = Repo.get(User, user_id) do + nil -> + {:error, "No user with such user_id"} + _ -> + {:ok, target} + end + %{ "screen_name" => nickname } -> + case target = Repo.get_by(User, nickname: nickname) do + nil -> + {:error, "No user with such screen_name"} + _ -> + {:ok, target} + end + _ -> + if user do + {:ok, user} + else + {:error, "You need to specify screen_name of user_id"} + end + end + end + defp activities_to_statuses(activities, opts) do Enum.map(activities, fn(activity) -> activity_to_status(activity, opts) @@ -296,15 +320,4 @@ defp activity_to_status(activity, opts) do defp make_date do DateTime.utc_now() |> DateTime.to_iso8601 end - - defp get_user(user, params) do - case params do - %{ "user_id" => user_id } -> - Repo.get(User, user_id) - %{ "screen_name" => nickname } -> - Repo.get_by!(User, nickname: nickname) - _ -> - user - end - end end diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index ae5cf0ad0..007b96bc7 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -45,9 +45,15 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do end def user_timeline(%{assigns: %{user: user}} = conn, params) do - statuses = TwitterAPI.fetch_user_statuses(user, params) - conn - |> json_reply(200, statuses |> Poison.encode!) + case TwitterAPI.get_user(user, params) do + {:ok, target_user} -> + params = Map.merge(params, %{"actor_id" => target_user.ap_id}) + statuses = TwitterAPI.fetch_user_statuses(user, params) + conn + |> json_reply(200, statuses |> Poison.encode!) + {:error, msg} -> + bad_request_reply(conn, msg) + end end def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do @@ -148,6 +154,11 @@ def register(conn, params) do end end + defp bad_request_reply(conn, error_message) do + json = Poison.encode!(%{"error" => error_message}) + json_reply(conn, 400, json) + end + defp json_reply(conn, status, json) do conn |> put_resp_content_type("application/json") diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index cf2649817..bdad06d6e 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -102,19 +102,33 @@ test "fetch friends' statuses" do assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_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"}) + + assert {:error, "You need to specify screen_name of user_id"} == TwitterAPI.get_user(nil, nil) + assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.id}) + assert user1_result == TwitterAPI.get_user(nil, %{"screen_name" => user1.nickname}) + assert user1_result == TwitterAPI.get_user(user1, nil) + assert user1_result == TwitterAPI.get_user(user2, %{"user_id" => user1.id}) + assert user1_result == TwitterAPI.get_user(user2, %{"screen_name" => user1.nickname}) + assert {:error, "No user with such screen_name"} == TwitterAPI.get_user(nil, %{"screen_name" => "Satan"}) + assert {:error, "No user with such user_id"} == TwitterAPI.get_user(nil, %{"user_id" => 666}) + end + test "fetch user's statuses" do - {:ok, user1} = UserBuilder.insert(%{ap_id: "some id"}) - {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2"}) + {: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"}) {:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1}) {:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2}) - user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{}) + user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user1.ap_id}) assert length(user1_statuses) == 1 assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1}) - user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"screen_name" => user2.nickname }) + user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user2.ap_id}) assert length(user2_statuses) == 1 assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2}) From 63f04b314d67decefab3a53b43d04b0347adde13 Mon Sep 17 00:00:00 2001 From: dtluna Date: Sun, 16 Apr 2017 17:05:48 +0300 Subject: [PATCH 4/5] Fix typo --- lib/pleroma/web/twitter_api/twitter_api.ex | 2 +- test/web/twitter_api/twitter_api_test.exs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 8a7ea87c8..77195f66d 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -275,7 +275,7 @@ def get_user(user, params) do if user do {:ok, user} else - {:error, "You need to specify screen_name of user_id"} + {:error, "You need to specify screen_name or user_id"} end end end diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index bdad06d6e..2f7e77773 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -106,7 +106,7 @@ 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"}) - assert {:error, "You need to specify screen_name of user_id"} == TwitterAPI.get_user(nil, nil) + assert {:error, "You need to specify screen_name or user_id"} == TwitterAPI.get_user(nil, nil) assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.id}) assert user1_result == TwitterAPI.get_user(nil, %{"screen_name" => user1.nickname}) assert user1_result == TwitterAPI.get_user(user1, nil) From 470f5f7cd616852327706bae74bd1eddd653285f Mon Sep 17 00:00:00 2001 From: dtluna Date: Mon, 17 Apr 2017 16:31:19 +0300 Subject: [PATCH 5/5] Add user timeline spec test --- .../twitter_api_controller_test.exs | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index f9723dd9f..6f1788a58 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -114,6 +114,72 @@ test "with credentials", %{conn: conn, user: current_user} do end end + describe "GET /statuses/user_timeline.json" do + setup [:valid_user] + test "without any params", %{conn: conn} do + conn = get(conn, "/api/statuses/user_timeline.json") + assert json_response(conn, 400) == %{"error" => "You need to specify screen_name or user_id"} + end + + test "with user_id", %{conn: conn} do + user = insert(:user) + {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user}) + + conn = get(conn, "/api/statuses/user_timeline.json", %{"user_id" => user.id}) + response = json_response(conn, 200) + assert length(response) == 1 + assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user}) + end + + test "with screen_name", %{conn: conn} do + user = insert(:user) + {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user}) + + conn = get(conn, "/api/statuses/user_timeline.json", %{"screen_name" => user.nickname}) + response = json_response(conn, 200) + assert length(response) == 1 + assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user}) + end + + test "with credentials", %{conn: conn, user: current_user} do + {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: current_user}) + conn = conn + |> with_credentials(current_user.nickname, "test") + |> get("/api/statuses/user_timeline.json") + + response = json_response(conn, 200) + + assert length(response) == 1 + assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user}) + end + + test "with credentials with user_id", %{conn: conn, user: current_user} do + user = insert(:user) + {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user}) + conn = conn + |> with_credentials(current_user.nickname, "test") + |> get("/api/statuses/user_timeline.json", %{"user_id" => user.id}) + + response = json_response(conn, 200) + + assert length(response) == 1 + assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user}) + end + + test "with credentials screen_name", %{conn: conn, user: current_user} do + user = insert(:user) + {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user}) + conn = conn + |> with_credentials(current_user.nickname, "test") + |> get("/api/statuses/user_timeline.json", %{"screen_name" => user.nickname}) + + response = json_response(conn, 200) + + assert length(response) == 1 + assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user}) + end + end + describe "POST /friendships/create.json" do setup [:valid_user] test "without valid credentials", %{conn: conn} do