From a7e74ee01232897a6172f6462cec282a451c319a Mon Sep 17 00:00:00 2001 From: dtluna Date: Mon, 10 Apr 2017 20:08:14 +0300 Subject: [PATCH 01/10] 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 65ef18a7157f8cfcc494ad7a72ce083e79e38d26 Mon Sep 17 00:00:00 2001 From: dtluna Date: Wed, 12 Apr 2017 17:34:36 +0300 Subject: [PATCH 02/10] Add error messages for repeated follows and unfollows --- lib/pleroma/user.ex | 29 +++++++++++------- lib/pleroma/web/twitter_api/twitter_api.ex | 6 +++- .../web/twitter_api/twitter_api_controller.ex | 30 ++++++++++++------- test/web/twitter_api/twitter_api_test.exs | 17 +++++++---- 4 files changed, 55 insertions(+), 27 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index c77704db0..5f5bc1c38 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -31,22 +31,31 @@ def follow_changeset(struct, params \\ %{}) do def follow(%User{} = follower, %User{} = followed) do ap_followers = User.ap_followers(followed) - following = [ap_followers | follower.following] - |> Enum.uniq + if following?(follower, followed) do + { :error, + "Could not follow user: #{followed.nickname} is already on your list." } + else + following = [ap_followers | follower.following] + |> Enum.uniq - follower - |> follow_changeset(%{following: following}) - |> Repo.update + follower + |> follow_changeset(%{following: following}) + |> Repo.update + end end def unfollow(%User{} = follower, %User{} = followed) do ap_followers = User.ap_followers(followed) - following = follower.following - |> List.delete(ap_followers) + if following?(follower, followed) do + following = follower.following + |> List.delete(ap_followers) - follower - |> follow_changeset(%{following: following}) - |> Repo.update + follower + |> follow_changeset(%{following: following}) + |> Repo.update + else + { :error, "Not subscribed!" } + end end def following?(%User{} = follower, %User{} = followed) do diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 0a942e880..66f78f340 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -103,7 +103,7 @@ def fetch_status(user, id) do def follow(%User{} = follower, followed_id) do with %User{} = followed <- Repo.get(User, followed_id), - { :ok, follower } <- User.follow(follower, followed), + { :ok, follower } <- User.follow(follower, followed), { :ok, activity } <- ActivityPub.insert(%{ "type" => "Follow", "actor" => follower.ap_id, @@ -112,6 +112,8 @@ def follow(%User{} = follower, followed_id) do }) do { :ok, follower, followed, activity } + else + err -> err end end @@ -120,6 +122,8 @@ def unfollow(%User{} = follower, followed_id) do { :ok, follower } <- User.unfollow(follower, followed) do { :ok, follower, followed } + else + err -> err 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 f2c893e96..f6574b8de 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -44,21 +44,24 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do end def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do - { :ok, _user, follower, _activity } = TwitterAPI.follow(user, followed_id) + case TwitterAPI.follow(user, followed_id) do + { :ok, _user, followed, _activity } -> + response = followed |> UserRepresenter.to_json(%{for: user}) + conn |> json_reply(200, response) + { :error, msg } -> forbidden_json_reply(conn, msg) + end - response = follower |> UserRepresenter.to_json(%{for: user}) - - conn - |> json_reply(200, response) end def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do - { :ok, user, follower } = TwitterAPI.unfollow(user, followed_id) + case TwitterAPI.unfollow(user, followed_id) do + { :ok, user, followed } -> + response = followed |> UserRepresenter.to_json(%{for: user}) - response = follower |> UserRepresenter.to_json(%{for: user}) - - conn - |> json_reply(200, response) + conn + |> json_reply(200, response) + { :error, msg } -> forbidden_json_reply(conn, msg) + end end def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do @@ -88,4 +91,11 @@ defp json_reply(conn, status, json) do |> put_resp_content_type("application/json") |> send_resp(status, json) end + + defp forbidden_json_reply(conn, error_message) do + json = %{"error" => error_message, "request" => conn.request_path} + |> Poison.encode! + + json_reply(conn, 403, json) + end end diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index ad932131a..c1f5881b8 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -105,26 +105,31 @@ test "fetch a single status" do test "Follow another user" do { :ok, user } = UserBuilder.insert - { :ok, following } = UserBuilder.insert(%{nickname: "guy"}) + { :ok, followed } = UserBuilder.insert(%{nickname: "guy"}) - {:ok, user, following, activity } = TwitterAPI.follow(user, following.id) + { :ok, user, followed, activity } = TwitterAPI.follow(user, followed.id) user = Repo.get(User, user.id) follow = Repo.get(Activity, activity.id) - assert user.following == [User.ap_followers(following)] + assert user.following == [User.ap_followers(followed)] assert follow == activity + + { :error, msg } = TwitterAPI.follow(user, followed.id) + assert msg == "Could not follow user: #{followed.nickname} is already on your list." end test "Unfollow another user" do - { :ok, following } = UserBuilder.insert(%{nickname: "guy"}) - { :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(following)]}) + { :ok, followed } = UserBuilder.insert(%{nickname: "guy"}) + { :ok, user } = UserBuilder.insert(%{following: [User.ap_followers(followed)]}) - {:ok, user, _following } = TwitterAPI.unfollow(user, following.id) + { :ok, user, _followed } = TwitterAPI.unfollow(user, followed.id) user = Repo.get(User, user.id) assert user.following == [] + { :error, msg } = TwitterAPI.unfollow(user, followed.id) + assert msg == "Not subscribed!" end test "fetch statuses in a context using the conversation id" do From 18cd04ade576969b25e96733dddb8255fbf13a6d Mon Sep 17 00:00:00 2001 From: dtluna Date: Fri, 14 Apr 2017 16:09:13 +0300 Subject: [PATCH 03/10] 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 04/10] 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 05/10] 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 06/10] 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 From 3a56d7318fed2b808161c186ba9d7ced73cdf020 Mon Sep 17 00:00:00 2001 From: dtluna Date: Thu, 20 Apr 2017 13:53:53 +0300 Subject: [PATCH 07/10] Add statuses/mentions endpoint --- lib/pleroma/web/router.ex | 16 +++++++++++--- lib/pleroma/web/twitter_api/twitter_api.ex | 5 +++++ .../web/twitter_api/twitter_api_controller.ex | 8 +++++++ .../twitter_api_controller_test.exs | 21 +++++++++++++++++++ test/web/twitter_api/twitter_api_test.exs | 11 ++++++++++ 5 files changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index b28813a45..28a54ec95 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -23,12 +23,15 @@ def user_fetcher(username) 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 @@ def user_fetcher(username) 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 diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 9b9ee0bd1..ce29d46a1 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -85,6 +85,11 @@ def fetch_user_statuses(user, opts \\ %{}) 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}), diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 8b5e6270b..8ea54852d 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -57,6 +57,14 @@ def user_timeline(%{assigns: %{user: user}} = conn, params) 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 } -> diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 8bf3fe107..e4ed6cd54 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -114,6 +114,27 @@ test "with credentials", %{conn: conn, user: current_user} 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 diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 196822f99..273093eba 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -102,6 +102,17 @@ test "fetch friends' statuses" 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"}) From 7a018a8d3bde27cdede0d7b942d23f1687c25f16 Mon Sep 17 00:00:00 2001 From: dtluna Date: Fri, 21 Apr 2017 00:29:08 +0300 Subject: [PATCH 08/10] Add /statuses/mentions_timeline route --- lib/pleroma/web/router.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 28a54ec95..6a2b37aec 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -44,6 +44,7 @@ def user_fetcher(username) do get "/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline get "/statuses/mentions", TwitterAPI.Controller, :mentions_timeline + get "/statuses/mentions_timeline", TwitterAPI.Controller, :mentions_timeline post "/statuses/update", TwitterAPI.Controller, :status_update post "/statuses/retweet/:id", TwitterAPI.Controller, :retweet From 1e88f102c49fb0a41f17f2c7f5161237e28b8baf Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 21 Apr 2017 00:51:09 +0200 Subject: [PATCH 09/10] Fix specs, add some user info. --- lib/pleroma/user.ex | 19 ++++++++++++++++- .../representers/user_representer.ex | 8 ++++--- .../representers/user_representer_test.exs | 21 ++++++++++++++----- .../twitter_api_controller_test.exs | 2 +- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 86b4b8b5e..3a4dd5d08 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1,7 +1,8 @@ defmodule Pleroma.User do use Ecto.Schema import Ecto.Changeset - alias Pleroma.{Repo, User} + import Ecto.Query + alias Pleroma.{Repo, User, Activity, Object} schema "users" do field :bio, :string @@ -32,6 +33,22 @@ def follow_changeset(struct, params \\ %{}) do |> validate_required([:following]) end + def user_info(%User{} = user) do + note_count_query = from a in Object, + where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}), + select: count(a.id) + + follower_count_query = from u in User, + where: fragment("? @> ?", u.following, ^User.ap_followers(user)), + select: count(u.id) + + %{ + following_count: length(user.following), + note_count: Repo.one(note_count_query), + follower_count: Repo.one(follower_count_query) + } + end + def register_changeset(struct, params \\ %{}) do changeset = struct |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation]) diff --git a/lib/pleroma/web/twitter_api/representers/user_representer.ex b/lib/pleroma/web/twitter_api/representers/user_representer.ex index 2ee4ee254..f358baf3c 100644 --- a/lib/pleroma/web/twitter_api/representers/user_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/user_representer.ex @@ -15,6 +15,8 @@ def to_map(user, opts) do false end + user_info = User.user_info(user) + map = %{ "id" => user.id, "name" => user.name, @@ -23,9 +25,9 @@ def to_map(user, opts) do "following" => following, # Fake fields "favourites_count" => 0, - "statuses_count" => 0, - "friends_count" => 0, - "followers_count" => 0, + "statuses_count" => user_info[:note_count], + "friends_count" => user_info[:following_count], + "followers_count" => user_info[:follower_count], "profile_image_url" => image, "profile_image_url_https" => image, "profile_image_url_profile_size" => image, diff --git a/test/web/twitter_api/representers/user_representer_test.exs b/test/web/twitter_api/representers/user_representer_test.exs index 913d1322c..1e92c5190 100644 --- a/test/web/twitter_api/representers/user_representer_test.exs +++ b/test/web/twitter_api/representers/user_representer_test.exs @@ -19,7 +19,18 @@ test "A user with an avatar object", %{user: user} do assert represented["profile_image_url"] == image end - test "A user", %{user: user} do + test "A user" do + note_activity = insert(:note_activity) + user = User.get_cached_by_ap_id(note_activity.data["actor"]) + follower = insert(:user) + second_follower = insert(:user) + + User.follow(follower, user) + User.follow(second_follower, user) + User.follow(user, follower) + + user = Repo.get!(User, user.id) + image = "https://placehold.it/48x48" represented = %{ @@ -29,9 +40,9 @@ test "A user", %{user: user} do "description" => user.bio, # Fake fields "favourites_count" => 0, - "statuses_count" => 0, - "friends_count" => 0, - "followers_count" => 0, + "statuses_count" => 1, + "friends_count" => 1, + "followers_count" => 2, "profile_image_url" => image, "profile_image_url_https" => image, "profile_image_url_profile_size" => image, @@ -55,7 +66,7 @@ test "A user for a given other follower", %{user: user} do "favourites_count" => 0, "statuses_count" => 0, "friends_count" => 0, - "followers_count" => 0, + "followers_count" => 1, "profile_image_url" => image, "profile_image_url_https" => image, "profile_image_url_profile_size" => image, diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index e4ed6cd54..0761d0566 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -367,7 +367,7 @@ test "it returns errors on a problem", %{conn: conn} do end defp valid_user(_context) do - { :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"}) + user = insert(:user) [user: user] end From 9afc672009ac49075530f44aefd73a17a0c50d43 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 21 Apr 2017 01:01:58 +0200 Subject: [PATCH 10/10] Quick fix for activities without 'to'. --- lib/pleroma/web/twitter_api/twitter_api.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index ce29d46a1..1053120c4 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -318,7 +318,7 @@ defp activity_to_status(activity, opts) do actor = get_in(activity.data, ["actor"]) user = User.get_cached_by_ap_id(actor) # mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"]) - mentioned_users = Enum.map(activity.data["to"], fn (ap_id) -> + mentioned_users = Enum.map(activity.data["to"] || [], fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end) |> Enum.filter(&(&1))