diff options
-rw-r--r-- | lib/pleroma/application.ex | 9 | ||||
-rw-r--r-- | lib/pleroma/user.ex | 40 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 7 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 3 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api.ex | 49 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api_controller.ex | 53 | ||||
-rw-r--r-- | mix.exs | 2 | ||||
-rw-r--r-- | mix.lock | 5 | ||||
-rw-r--r-- | test/web/twitter_api/twitter_api_controller_test.exs | 73 | ||||
-rw-r--r-- | test/web/twitter_api/twitter_api_test.exs | 60 |
10 files changed, 244 insertions, 57 deletions
diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index e5bd17ced..86b6c0c1e 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -14,10 +14,11 @@ defmodule Pleroma.Application do supervisor(Pleroma.Web.Endpoint, []), # Start your own worker by calling: Pleroma.Worker.start_link(arg1, arg2, arg3) # worker(Pleroma.Worker, [arg1, arg2, arg3]), - supervisor(ConCache, [[ - ttl_check: :timer.seconds(1), - ttl: :timer.seconds(5) - ], [name: :users]]) + worker(Cachex, [:user_cache, [ + default_ttl: 5000, + ttl_interval: 1000, + limit: 500 + ]]) ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index fdcc1b7d5..86b4b8b5e 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -55,22 +55,31 @@ defmodule Pleroma.User 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 @@ -78,15 +87,12 @@ defmodule Pleroma.User do end def get_cached_by_ap_id(ap_id) do - ConCache.get_or_store(:users, "ap_id:#{ap_id}", fn() -> - # Return false so the cache will store it. - Repo.get_by(User, ap_id: ap_id) || false - end) + key = "ap_id:#{ap_id}" + Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end) end def get_cached_by_nickname(nickname) do - ConCache.get_or_store(:users, "nickname:#{nickname}", fn() -> - Repo.get_by(User, nickname: nickname) || false - end) + key = "nickname:#{nickname}" + Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, nickname: nickname) end) end end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 125473b96..e9f0dcd32 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -133,6 +133,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub 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/router.ex b/lib/pleroma/web/router.ex index 2d7c25b50..b28813a45 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -21,8 +21,11 @@ defmodule Pleroma.Web.Router do scope "/api", Pleroma.Web do pipe_through :api + + get "/help/test", TwitterAPI.Controller, :help_test 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 diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index de76822fc..e02c0766f 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -80,6 +80,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do |> activities_to_statuses(%{for: user}) end + def fetch_user_statuses(user, opts \\ %{}) do + ActivityPub.fetch_activities([], opts) + |> activities_to_statuses(%{for: user}) + end + def fetch_conversation(user, id) do query = from activity in Activity, where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}), @@ -104,7 +109,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end def follow(%User{} = follower, params) do - with %User{} = followed <- get_user(params), + with { :ok, %User{} = followed } <- get_user(params), { :ok, follower } <- User.follow(follower, followed), { :ok, activity } <- ActivityPub.insert(%{ "type" => "Follow", @@ -114,6 +119,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do }) do { :ok, follower, followed, activity } + else + err -> err end end @@ -122,6 +129,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do { :ok, follower } <- User.unfollow(follower, followed) do { :ok, follower, followed } + else + err -> err end end @@ -244,12 +253,37 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI 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 \\ nil, 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 or user_id"} + end + end + end + defp activities_to_statuses(activities, opts) do Enum.map(activities, fn(activity) -> activity_to_status(activity, opts) @@ -290,13 +324,4 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do defp make_date do DateTime.utc_now() |> DateTime.to_iso8601 end - - defp get_user(params) do - case params do - %{ "user_id" => user_id } -> - Repo.get(User, user_id) - %{ "screen_name" => nickname } -> - Repo.get_by(User, nickname: nickname) - 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 429417884..92c20f97a 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -45,22 +45,37 @@ defmodule Pleroma.Web.TwitterAPI.Controller do |> json_reply(200, json) end - def follow(%{assigns: %{user: user}} = conn, params) do - { :ok, user, followed, _activity } = TwitterAPI.follow(user, params) - - response = followed |> UserRepresenter.to_json(%{for: user}) + def user_timeline(%{assigns: %{user: user}} = conn, params) do + 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 - conn - |> json_reply(200, response) + def follow(%{assigns: %{user: user}} = conn, params) do + case TwitterAPI.follow(user, params) 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 end def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do - { :ok, user, followed } = TwitterAPI.unfollow(user, followed_id) + case TwitterAPI.unfollow(user, followed_id) do + { :ok, user, followed } -> + response = followed |> UserRepresenter.to_json(%{for: user}) - response = followed |> 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 @@ -85,6 +100,10 @@ defmodule Pleroma.Web.TwitterAPI.Controller do |> send_resp(200, response) end + def help_test(conn, _params) do + conn |> json_reply(200, Poison.encode!("ok")) + end + def upload_json(conn, %{"media" => media}) do response = TwitterAPI.upload(media, "json") conn @@ -155,9 +174,21 @@ defmodule Pleroma.Web.TwitterAPI.Controller do |> json_reply(200, response) 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") |> 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 @@ -38,7 +38,7 @@ defmodule Pleroma.Mixfile do {:trailing_format_plug, "~> 0.0.5" }, {:html_sanitize_ex, "~> 1.0.0"}, {:calendar, "~> 0.16.1"}, - {:con_cache, "~> 0.12.0"}, + {:cachex, "~> 2.1"}, {:ex_machina, "~> 2.0", only: :test}, {:mix_test_watch, "~> 0.2", only: :dev}] end @@ -1,4 +1,5 @@ -%{"calendar": {:hex, :calendar, "0.16.1", "782327ad8bae7c797b887840dc4ddb933f05ce6e333e5b04964d7a5d5f79bde3", [:mix], [{:tzdata, "~> 0.5.8 or ~> 0.1.201603", [hex: :tzdata, optional: false]}]}, +%{"cachex": {:hex, :cachex, "2.1.0", "fad49b4e78d11c6c314e75bd8c9408f5b78cb065c047442798caed10803ee3be", [:mix], [{:eternal, "~> 1.1", [hex: :eternal, optional: false]}]}, + "calendar": {:hex, :calendar, "0.16.1", "782327ad8bae7c797b887840dc4ddb933f05ce6e333e5b04964d7a5d5f79bde3", [:mix], [{:tzdata, "~> 0.5.8 or ~> 0.1.201603", [hex: :tzdata, optional: false]}]}, "certifi": {:hex, :certifi, "1.0.0", "1c787a85b1855ba354f0b8920392c19aa1d06b0ee1362f9141279620a5be2039", [:rebar3], []}, "comeonin": {:hex, :comeonin, "3.0.2", "8b213268a6634bd2e31a8035a963e974681d13ccc1f73f2ae664b6ac4e993c96", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, optional: false]}]}, "con_cache": {:hex, :con_cache, "0.12.0", "2d961aec219aa5a914473873f348f5a6088292dc69d5192a9d25f8a1e13e9905", [:mix], [{:exactor, "~> 2.2.0", [hex: :exactor, optional: false]}]}, @@ -7,8 +8,10 @@ "cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []}, "db_connection": {:hex, :db_connection, "1.1.2", "2865c2a4bae0714e2213a0ce60a1b12d76a6efba0c51fbda59c9ab8d1accc7a8", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]}, "decimal": {:hex, :decimal, "1.3.1", "157b3cedb2bfcb5359372a7766dd7a41091ad34578296e951f58a946fcab49c6", [:mix], []}, + "deppie": {:hex, :deppie, "1.1.0", "cfb6fcee7dfb64eb78cb8505537971a0805131899326ad469ef10df04520f451", [:mix], []}, "ecto": {:hex, :ecto, "2.1.4", "d1ba932813ec0e0d9db481ef2c17777f1cefb11fc90fa7c142ff354972dfba7e", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]}, "elixir_make": {:hex, :elixir_make, "0.4.0", "992f38fabe705bb45821a728f20914c554b276838433349d4f2341f7a687cddf", [:mix], []}, + "eternal": {:hex, :eternal, "1.1.4", "3a40fd9b9708f79216a6ec8ae886f2b17685dc26b119b9c0403c2b0d3dc1ac69", [:mix], [{:deppie, "~> 1.1", [hex: :deppie, optional: false]}]}, "ex_machina": {:hex, :ex_machina, "2.0.0", "ec284c6f57233729cea9319e083f66e613e82549f78eccdb2059aeba5d0df9f3", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, optional: true]}]}, "exactor": {:hex, :exactor, "2.2.3", "a6972f43bb6160afeb73e1d8ab45ba604cd0ac8b5244c557093f6e92ce582786", [:mix], []}, "fs": {:hex, :fs, "2.12.0", "ad631efacc9a5683c8eaa1b274e24fa64a1b8eb30747e9595b93bec7e492e25e", [:rebar3], []}, diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index f9723dd9f..8bf3fe107 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 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest 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 @@ -157,6 +223,13 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end end + describe "GET /help/test.json" do + test "returns \"ok\"", %{conn: conn} do + conn = get conn, "/api/help/test.json" + assert json_response(conn, 200) == "ok" + end + end + describe "POST /api/qvitter/update_avatar.json" do setup [:valid_user] test "without valid credentials", %{conn: conn} do diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 4993da3ed..3a1745a1b 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -102,6 +102,38 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_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 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) + 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", 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, %{"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, %{"actor_id" => user2.ap_id}) + + 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() @@ -114,31 +146,37 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do test "Follow another user using user_id" do user = insert(:user) - following = insert(:user) + followed = insert(:user) - {:ok, user, following, activity } = TwitterAPI.follow(user, %{"user_id" => following.id}) + {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"user_id" => followed.id}) + assert user.following == [User.ap_followers(followed)] - assert user.following == [User.ap_followers(following)] + { :error, msg } = TwitterAPI.follow(user, %{"user_id" => followed.id}) + assert msg == "Could not follow user: #{followed.nickname} is already on your list." end test "Follow another user using screen_name" do user = insert(:user) - following = insert(:user) + followed = insert(:user) - {:ok, user, following, activity } = TwitterAPI.follow(user, %{"screen_name" => following.nickname}) + {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname}) + assert user.following == [User.ap_followers(followed)] - assert user.following == [User.ap_followers(following)] + { :error, msg } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname}) + assert msg == "Could not follow user: #{followed.nickname} is already on your list." end test "Unfollow another user" do - following = insert(:user) - user = insert(:user, %{following: [User.ap_followers(following)]}) + followed = insert(:user) + user = insert(:user, %{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 @@ -257,8 +295,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end setup do - Supervisor.terminate_child(Pleroma.Supervisor, ConCache) - Supervisor.restart_child(Pleroma.Supervisor, ConCache) + Supervisor.terminate_child(Pleroma.Supervisor, Cachex) + Supervisor.restart_child(Pleroma.Supervisor, Cachex) :ok end end |