Merge branch 'fix/emoji-picker' into 'develop'
authoreal <eal@waifu.club>
Tue, 5 Jun 2018 21:18:25 +0000 (21:18 +0000)
committereal <eal@waifu.club>
Tue, 5 Jun 2018 21:18:25 +0000 (21:18 +0000)
set visible_in_picker to true in custom_emojis

Closes #177 and mastofe#16

See merge request pleroma/pleroma!188

12 files changed:
installation/caddyfile-pleroma.example [new file with mode: 0644]
lib/pleroma/activity.ex
lib/pleroma/web/activity_pub/activity_pub_controller.ex
lib/pleroma/web/common_api/utils.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/ostatus/ostatus_controller.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/activity_pub/activity_pub_controller_test.exs
test/web/mastodon_api/mastodon_api_controller_test.exs
test/web/ostatus/ostatus_controller_test.exs
test/web/twitter_api/twitter_api_controller_test.exs

diff --git a/installation/caddyfile-pleroma.example b/installation/caddyfile-pleroma.example
new file mode 100644 (file)
index 0000000..e0f9dc9
--- /dev/null
@@ -0,0 +1,18 @@
+social.domain.tld  {
+  tls user@domain.tld
+
+  log /var/log/caddy/pleroma.log
+
+  cors / {
+    origin https://halcyon.domain.tld
+    origin https://pinafore.domain.tld
+    methods POST,PUT,DELETE,GET,PATCH,OPTIONS
+    allowed_headers Authorization,Content-Type,Idempotency-Key
+    exposed_headers Link,X-RateLimit-Reset,X-RateLimit-Limit,X-RateLimit-Remaining,X-Request-Id
+  }
+
+  proxy / localhost:4000 {
+    websocket
+    transparent
+  }
+}
index c7502981e31be64eac7e478750537dc772280ea4..dd680512548f622bea2dd58f816df0c856405206 100644 (file)
@@ -72,8 +72,10 @@ defmodule Pleroma.Activity do
     )
   end
 
-  def get_create_activity_by_object_ap_id(ap_id) do
+  def get_create_activity_by_object_ap_id(ap_id) when is_binary(ap_id) do
     create_activity_by_object_id_query([ap_id])
     |> Repo.one()
   end
+
+  def get_create_activity_by_object_ap_id(_), do: nil
 end
index a6a9b99eff30604563d37981f5114318a2a1bd8c..ee5d319a716b76553fb55dcc8ee0512b3b08cd43 100644 (file)
@@ -27,9 +27,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
       |> json(ObjectView.render("object.json", %{object: object}))
     else
       {:public?, false} ->
-        conn
-        |> put_status(404)
-        |> json("Not found")
+        {:error, :not_found}
     end
   end
 
@@ -107,6 +105,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
     json(conn, "ok")
   end
 
+  def errors(conn, {:error, :not_found}) do
+    conn
+    |> put_status(404)
+    |> json("Not found")
+  end
+
   def errors(conn, _e) do
     conn
     |> put_status(500)
index 9c99513714c93e44e7b979c38b0101676213c8de..30089f553abbbf1646f6cd6e748bdd0828dfd7c3 100644 (file)
@@ -9,11 +9,12 @@ defmodule Pleroma.Web.CommonAPI.Utils do
   def get_by_id_or_ap_id(id) do
     activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
 
-    if activity.data["type"] == "Create" do
-      activity
-    else
-      Activity.get_create_activity_by_object_ap_id(activity.data["object"])
-    end
+    activity &&
+      if activity.data["type"] == "Create" do
+        activity
+      else
+        Activity.get_create_activity_by_object_ap_id(activity.data["object"])
+      end
   end
 
   def get_replied_to_activity(id) when not is_nil(id) do
index 3dd9aeb5797987814beaadc0ffc129ce707896b9..4252ac2fe2f0bce288bfb181901c2b1086277c6e 100644 (file)
@@ -10,6 +10,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
   import Ecto.Query
   require Logger
 
+  action_fallback(:errors)
+
   def create_app(conn, params) do
     with cs <- App.register_changeset(%App{}, params) |> IO.inspect(),
          {:ok, app} <- Repo.insert(cs) |> IO.inspect() do
@@ -328,27 +330,27 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
   end
 
   def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
-    with {:ok, announce, _activity} = CommonAPI.repeat(ap_id_or_id, user) do
+    with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user) do
       render(conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity})
     end
   end
 
   def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
-    with {:ok, _, _, %{data: %{"id" => id}}} = CommonAPI.unrepeat(ap_id_or_id, user),
+    with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
          %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
       render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
     end
   end
 
   def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
-    with {:ok, _fav, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user),
+    with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
          %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
       render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
     end
   end
 
   def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
-    with {:ok, _, _, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user),
+    with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
          %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
       render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
     end
@@ -920,4 +922,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
         nil
     end
   end
+
+  def errors(conn, _) do
+    conn
+    |> put_status(500)
+    |> json("Something went wrong")
+  end
 end
index 53278431e1b766b78c8cc09f825f8e43b4703d76..f346cc9afa94871b5a73f44734d8508f54b8de0f 100644 (file)
@@ -9,36 +9,42 @@ defmodule Pleroma.Web.OStatus.OStatusController do
   alias Pleroma.Web.ActivityPub.ActivityPubController
   alias Pleroma.Web.ActivityPub.ActivityPub
 
-  def feed_redirect(conn, %{"nickname" => nickname} = params) do
-    user = User.get_cached_by_nickname(nickname)
+  action_fallback(:errors)
 
-    case get_format(conn) do
-      "html" -> Fallback.RedirectController.redirector(conn, nil)
-      "activity+json" -> ActivityPubController.user(conn, params)
-      _ -> redirect(conn, external: OStatus.feed_path(user))
+  def feed_redirect(conn, %{"nickname" => nickname}) do
+    with {_, %User{} = user} <- {:user, User.get_cached_by_nickname(nickname)} do
+      case get_format(conn) do
+        "html" -> Fallback.RedirectController.redirector(conn, nil)
+        "activity+json" -> ActivityPubController.call(conn, :user)
+        _ -> redirect(conn, external: OStatus.feed_path(user))
+      end
+    else
+      {:user, nil} -> {:error, :not_found}
     end
   end
 
   def feed(conn, %{"nickname" => nickname} = params) do
-    user = User.get_cached_by_nickname(nickname)
-
-    query_params =
-      Map.take(params, ["max_id"])
-      |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
-
-    activities =
-      ActivityPub.fetch_public_activities(query_params)
-      |> Enum.reverse()
-
-    response =
-      user
-      |> FeedRepresenter.to_simple_form(activities, [user])
-      |> :xmerl.export_simple(:xmerl_xml)
-      |> to_string
-
-    conn
-    |> put_resp_content_type("application/atom+xml")
-    |> send_resp(200, response)
+    with {_, %User{} = user} <- {:user, User.get_cached_by_nickname(nickname)} do
+      query_params =
+        Map.take(params, ["max_id"])
+        |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
+
+      activities =
+        ActivityPub.fetch_public_activities(query_params)
+        |> Enum.reverse()
+
+      response =
+        user
+        |> FeedRepresenter.to_simple_form(activities, [user])
+        |> :xmerl.export_simple(:xmerl_xml)
+        |> to_string
+
+      conn
+      |> put_resp_content_type("application/atom+xml")
+      |> send_resp(200, response)
+    else
+      {:user, nil} -> {:error, :not_found}
+    end
   end
 
   defp decode_or_retry(body) do
@@ -68,12 +74,13 @@ defmodule Pleroma.Web.OStatus.OStatusController do
     |> send_resp(200, "")
   end
 
-  def object(conn, %{"uuid" => uuid} = params) do
+  def object(conn, %{"uuid" => uuid}) do
     if get_format(conn) == "activity+json" do
-      ActivityPubController.object(conn, params)
+      ActivityPubController.call(conn, :object)
     else
       with id <- o_status_url(conn, :object, uuid),
-           %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
+           {_, %Activity{} = activity} <-
+             {:activity, Activity.get_create_activity_by_object_ap_id(id)},
            {_, true} <- {:public?, ActivityPub.is_public?(activity)},
            %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
         case get_format(conn) do
@@ -82,16 +89,20 @@ defmodule Pleroma.Web.OStatus.OStatusController do
         end
       else
         {:public?, false} ->
-          conn
-          |> put_status(404)
-          |> json("Not found")
+          {:error, :not_found}
+
+        {:activity, nil} ->
+          {:error, :not_found}
+
+        e ->
+          e
       end
     end
   end
 
   def activity(conn, %{"uuid" => uuid}) do
     with id <- o_status_url(conn, :activity, uuid),
-         %Activity{} = activity <- Activity.get_by_ap_id(id),
+         {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(id)},
          {_, true} <- {:public?, ActivityPub.is_public?(activity)},
          %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
       case get_format(conn) do
@@ -100,14 +111,18 @@ defmodule Pleroma.Web.OStatus.OStatusController do
       end
     else
       {:public?, false} ->
-        conn
-        |> put_status(404)
-        |> json("Not found")
+        {:error, :not_found}
+
+      {:activity, nil} ->
+        {:error, :not_found}
+
+      e ->
+        e
     end
   end
 
   def notice(conn, %{"id" => id}) do
-    with %Activity{} = activity <- Repo.get(Activity, id),
+    with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
          {_, true} <- {:public?, ActivityPub.is_public?(activity)},
          %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
       case get_format(conn) do
@@ -121,9 +136,13 @@ defmodule Pleroma.Web.OStatus.OStatusController do
       end
     else
       {:public?, false} ->
-        conn
-        |> put_status(404)
-        |> json("Not found")
+        {:error, :not_found}
+
+      {:activity, nil} ->
+        {:error, :not_found}
+
+      e ->
+        e
     end
   end
 
@@ -139,4 +158,16 @@ defmodule Pleroma.Web.OStatus.OStatusController do
     |> put_resp_content_type("application/atom+xml")
     |> send_resp(200, response)
   end
+
+  def errors(conn, {:error, :not_found}) do
+    conn
+    |> put_status(404)
+    |> text("Not found")
+  end
+
+  def errors(conn, _) do
+    conn
+    |> put_status(500)
+    |> text("Something went wrong")
+  end
 end
index 331efa90b5512f91065c0ea7d68201465cc879e6..ccc6fe8e7be07fa7ccdb902cd20979e8c1ce6e10 100644 (file)
@@ -64,7 +64,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
   end
 
   def repeat(%User{} = user, ap_id_or_id) do
-    with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(ap_id_or_id, user),
+    with {:ok, _announce, %{data: %{"id" => id}}} <- CommonAPI.repeat(ap_id_or_id, user),
          %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
       {:ok, activity}
     end
@@ -77,14 +77,14 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
   end
 
   def fav(%User{} = user, ap_id_or_id) do
-    with {:ok, _fav, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user),
+    with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
          %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
       {:ok, activity}
     end
   end
 
   def unfav(%User{} = user, ap_id_or_id) do
-    with {:ok, _unfav, _fav, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user),
+    with {:ok, _unfav, _fav, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
          %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
       {:ok, activity}
     end
index 320f2fcf4e37ebe2564fde82fee8997f7ab09611..d53dd0c44897f6adb5c794dbe61be7a4e5386d19 100644 (file)
@@ -8,6 +8,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
 
   require Logger
 
+  action_fallback(:errors)
+
   def verify_credentials(%{assigns: %{user: user}} = conn, _params) do
     token = Phoenix.Token.sign(conn, "user socket", user.id)
     render(conn, UserView, "show.json", %{user: user, token: token})
@@ -218,19 +220,22 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
   end
 
   def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
-    with {:ok, activity} <- TwitterAPI.fav(user, id) do
+    with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)},
+         {:ok, activity} <- TwitterAPI.fav(user, id) do
       render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
     end
   end
 
   def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
-    with {:ok, activity} <- TwitterAPI.unfav(user, id) do
+    with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)},
+         {:ok, activity} <- TwitterAPI.unfav(user, id) do
       render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
     end
   end
 
   def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
-    with {:ok, activity} <- TwitterAPI.repeat(user, id) do
+    with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)},
+         {:ok, activity} <- TwitterAPI.repeat(user, id) do
       render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
     end
   end
@@ -389,4 +394,16 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
   defp error_json(conn, error_message) do
     %{"error" => error_message, "request" => conn.request_path} |> Jason.encode!()
   end
+
+  def errors(conn, {:param_cast, _}) do
+    conn
+    |> put_status(400)
+    |> json("Invalid parameters")
+  end
+
+  def errors(conn, _) do
+    conn
+    |> put_status(500)
+    |> json("Something went wrong")
+  end
 end
index 305f9d0e05a692af25a9e1c9bbf6ecda3b10b78e..bbf89136b1b8ee786d6e3e96fcb1395a6f9b1b59 100644 (file)
@@ -4,7 +4,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
   alias Pleroma.Web.ActivityPub.{UserView, ObjectView}
   alias Pleroma.{Repo, User}
   alias Pleroma.Activity
-  alias Pleroma.Web.CommonAPI
 
   describe "/users/:nickname" do
     test "it returns a json representation of the user", %{conn: conn} do
index 2abcf0dfe38c00e8e6072fdeb196b4948b8dea67..1291c3693dd04135c7363dbb0d3d9403aba0e7dc 100644 (file)
@@ -505,6 +505,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
 
       assert to_string(activity.id) == id
     end
+
+    test "returns 500 for a wrong id", %{conn: conn} do
+      user = insert(:user)
+
+      resp =
+        conn
+        |> assign(:user, user)
+        |> post("/api/v1/statuses/1/favourite")
+        |> json_response(500)
+
+      assert resp == "Something went wrong"
+    end
   end
 
   describe "unfavoriting" do
index faee4fc3e5a8da2a7c943e930e2c16d6bda0f4f4..d5adf3bf39f8de000834013110dff48d975cc86e 100644 (file)
@@ -53,11 +53,21 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do
 
     conn =
       conn
+      |> put_req_header("content-type", "application/atom+xml")
       |> get("/users/#{user.nickname}/feed.atom")
 
     assert response(conn, 200) =~ note_activity.data["object"]["content"]
   end
 
+  test "returns 404 for a missing feed", %{conn: conn} do
+    conn =
+      conn
+      |> put_req_header("content-type", "application/atom+xml")
+      |> get("/users/nonexisting/feed.atom")
+
+    assert response(conn, 404)
+  end
+
   test "gets an object", %{conn: conn} do
     note_activity = insert(:note_activity)
     user = User.get_by_ap_id(note_activity.data["actor"])
@@ -90,6 +100,16 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do
     assert response(conn, 404)
   end
 
+  test "404s on nonexisting objects", %{conn: conn} do
+    url = "/objects/123"
+
+    conn =
+      conn
+      |> get(url)
+
+    assert response(conn, 404)
+  end
+
   test "gets an activity", %{conn: conn} do
     note_activity = insert(:note_activity)
     [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
@@ -114,6 +134,16 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do
     assert response(conn, 404)
   end
 
+  test "404s on nonexistent activities", %{conn: conn} do
+    url = "/activities/123"
+
+    conn =
+      conn
+      |> get(url)
+
+    assert response(conn, 404)
+  end
+
   test "gets a notice", %{conn: conn} do
     note_activity = insert(:note_activity)
     url = "/notice/#{note_activity.id}"
@@ -135,4 +165,14 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do
 
     assert response(conn, 404)
   end
+
+  test "404s a nonexisting notice", %{conn: conn} do
+    url = "/notice/123"
+
+    conn =
+      conn
+      |> get(url)
+
+    assert response(conn, 404)
+  end
 end
index 03e5824a9872a89daaa87403befb79f8815b5226..68f4331dfbda7fc6ec64ca4584bf0e58638d03f9 100644 (file)
@@ -260,7 +260,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     test "with credentials", %{conn: conn, user: current_user} do
       other_user = insert(:user)
 
-      {:ok, activity} =
+      {:ok, _activity} =
         ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
 
       conn =
@@ -510,6 +510,24 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
 
       assert json_response(conn, 200)
     end
+
+    test "with credentials, invalid param", %{conn: conn, user: current_user} do
+      conn =
+        conn
+        |> with_credentials(current_user.nickname, "test")
+        |> post("/api/favorites/create/wrong.json")
+
+      assert json_response(conn, 400)
+    end
+
+    test "with credentials, invalid activity", %{conn: conn, user: current_user} do
+      conn =
+        conn
+        |> with_credentials(current_user.nickname, "test")
+        |> post("/api/favorites/create/1.json")
+
+      assert json_response(conn, 500)
+    end
   end
 
   describe "POST /api/favorites/destroy/:id" do
@@ -793,7 +811,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
   test "Convert newlines to <br> in bio", %{conn: conn} do
     user = insert(:user)
 
-    conn =
+    _conn =
       conn
       |> assign(:user, user)
       |> post("/api/account/update_profile.json", %{
@@ -904,6 +922,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
         |> post("/api/pleroma/delete_account", %{"password" => "test"})
 
       assert json_response(conn, 200) == %{"status" => "success"}
+      # Wait a second for the started task to end
+      :timer.sleep(1000)
     end
   end
 end