Merge branch 'fix/add-default-scope-mastoapi' into 'develop'
authorrinpatch <rinpatch@sdf.org>
Mon, 29 Apr 2019 06:45:18 +0000 (06:45 +0000)
committerrinpatch <rinpatch@sdf.org>
Mon, 29 Apr 2019 06:45:18 +0000 (06:45 +0000)
Add default_scope to /api/v1/update_credentials

See merge request pleroma/pleroma!1100

.gitignore
lib/pleroma/bookmark.ex
lib/pleroma/web/common_api/common_api.ex
lib/pleroma/web/mastodon_api/views/status_view.ex
lib/pleroma/web/twitter_api/views/user_view.ex
test/bookmark_test.exs
test/web/mastodon_api/status_view_test.exs
test/web/twitter_api/views/user_view_test.exs
uploads/.gitignore [new file with mode: 0644]

index 774893b35f2a9a432738a33614fe17b4d001f6fb..a1e79e4be2a7a34ac0211ad65ae0df60cda1cb96 100644 (file)
@@ -3,7 +3,6 @@
 /db
 /deps
 /*.ez
-/uploads
 /test/uploads
 /.elixir_ls
 /test/fixtures/test_tmp.txt
index c5c3e078b67cfd055b25b8d0b5a5894eb382db11..7f8fd43b6f174f2367022727f6292956396f1d9c 100644 (file)
@@ -41,6 +41,13 @@ defmodule Pleroma.Bookmark do
     |> preload([b, a], activity: a)
   end
 
+  def get(user_id, activity_id) do
+    Bookmark
+    |> where(user_id: ^user_id)
+    |> where(activity_id: ^activity_id)
+    |> Repo.one()
+  end
+
   @spec destroy(FlakeId.t(), FlakeId.t()) :: {:ok, Bookmark.t()} | {:error, Changeset.t()}
   def destroy(user_id, activity_id) do
     from(b in Bookmark,
index cfbc5dc1074fd643f6f24508db98c6d447e532b4..ecd1831107d26efeba18d9eb483dbe18964b3160 100644 (file)
@@ -4,6 +4,7 @@
 
 defmodule Pleroma.Web.CommonAPI do
   alias Pleroma.Activity
+  alias Pleroma.Bookmark
   alias Pleroma.Formatter
   alias Pleroma.Object
   alias Pleroma.ThreadMute
@@ -282,6 +283,15 @@ defmodule Pleroma.Web.CommonAPI do
     end
   end
 
+  def bookmarked?(user, activity) do
+    with %Bookmark{} <- Bookmark.get(user.id, activity.id) do
+      true
+    else
+      _ ->
+        false
+    end
+  end
+
   def report(user, data) do
     with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
          {:account, %User{} = account} <- {:account, User.get_cached_by_id(account_id)},
index 57cb9fdcc465d404613f54d61f99c304e9d39f60..62d064d717f1d5a4d2c0f2fcbb50cfdae82a3e35 100644 (file)
@@ -86,11 +86,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     activity_object = Object.normalize(activity)
     favorited = opts[:for] && opts[:for].ap_id in (activity_object.data["likes"] || [])
 
-    bookmarked =
-      opts[:for] && Ecto.assoc_loaded?(opts[:for].bookmarks) &&
-        Enum.any?(opts[:for].bookmarks, fn b ->
-          b.activity_id == activity.id or b.activity.data["object"]["id"] == object
-        end)
+    bookmarked = opts[:for] && CommonAPI.bookmarked?(opts[:for], reblogged_activity)
 
     mentions =
       activity.recipients
@@ -153,11 +149,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
 
     favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || [])
 
-    bookmarked =
-      opts[:for] && Ecto.assoc_loaded?(opts[:for].bookmarks) &&
-        Enum.any?(opts[:for].bookmarks, fn b ->
-          b.activity_id == activity.id
-        end)
+    bookmarked = opts[:for] && CommonAPI.bookmarked?(opts[:for], activity)
 
     attachment_data = object.data["attachment"] || []
     attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
index 39b3f21c0f6ad65ec2e466d863f3d8804666ba72..ea015b8f05549b19a4a5721af4b1093349a654a0 100644 (file)
@@ -116,12 +116,7 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
           |> maybe_with_activation_status(user, for_user)
       }
       |> maybe_with_user_settings(user, for_user)
-
-    data =
-      if(user.info.is_admin || user.info.is_moderator,
-        do: maybe_with_role(data, user, for_user),
-        else: data
-      )
+      |> maybe_with_role(user, for_user)
 
     if assigns[:token] do
       Map.put(data, "token", token_string(assigns[:token]))
index 3be148023eb9079539b75f13b7a4ce1e50a4afc1..b81c102eff87084d9ede55b2c12ddf9944bdd340 100644 (file)
@@ -34,4 +34,19 @@ defmodule Pleroma.BookmarkTest do
       {:ok, _deleted_bookmark} = Bookmark.destroy(user.id, activity.id)
     end
   end
+
+  describe "get/2" do
+    test "gets a bookmark" do
+      user = insert(:user)
+
+      {:ok, activity} =
+        CommonAPI.post(user, %{
+          "status" =>
+            "Scientists Discover The Secret Behind Tenshi Eating A Corndog Being So Cute – Science Daily"
+        })
+
+      {:ok, bookmark} = Bookmark.create(user.id, activity.id)
+      assert bookmark == Bookmark.get(user.id, activity.id)
+    end
+  end
 end
index f74726212fa7eb8dafbe03a842b300b50d2d7c90..5fddc6c58bbcad6cc44e70addfbd701965f3d670 100644 (file)
@@ -6,6 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
   use Pleroma.DataCase
 
   alias Pleroma.Activity
+  alias Pleroma.Bookmark
   alias Pleroma.Object
   alias Pleroma.Repo
   alias Pleroma.User
@@ -153,6 +154,25 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
     assert status.muted == true
   end
 
+  test "tells if the status is bookmarked" do
+    user = insert(:user)
+
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
+    status = StatusView.render("status.json", %{activity: activity})
+
+    assert status.bookmarked == false
+
+    status = StatusView.render("status.json", %{activity: activity, for: user})
+
+    assert status.bookmarked == false
+
+    {:ok, _bookmark} = Bookmark.create(user.id, activity.id)
+
+    status = StatusView.render("status.json", %{activity: activity, for: user})
+
+    assert status.bookmarked == true
+  end
+
   test "a reply" do
     note = insert(:note_activity)
     user = insert(:user)
index 2f9b2af0127a918af8c4831378b4fc557214958d..c99dbddeb355630cb4819ec23b26f0130fa0452f 100644 (file)
@@ -100,7 +100,9 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
       "pleroma" => %{
         "confirmation_pending" => false,
         "tags" => []
-      }
+      },
+      "rights" => %{"admin" => false, "delete_others_notice" => false},
+      "role" => "member"
     }
 
     assert represented == UserView.render("show.json", %{user: user})
@@ -151,7 +153,9 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
       "pleroma" => %{
         "confirmation_pending" => false,
         "tags" => []
-      }
+      },
+      "rights" => %{"admin" => false, "delete_others_notice" => false},
+      "role" => "member"
     }
 
     assert represented == UserView.render("show.json", %{user: user, for: follower})
@@ -194,7 +198,9 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
       "pleroma" => %{
         "confirmation_pending" => false,
         "tags" => []
-      }
+      },
+      "rights" => %{"admin" => false, "delete_others_notice" => false},
+      "role" => "member"
     }
 
     assert represented == UserView.render("show.json", %{user: follower, for: user})
@@ -274,7 +280,9 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
       "pleroma" => %{
         "confirmation_pending" => false,
         "tags" => []
-      }
+      },
+      "rights" => %{"admin" => false, "delete_others_notice" => false},
+      "role" => "member"
     }
 
     blocker = User.get_cached_by_id(blocker.id)
diff --git a/uploads/.gitignore b/uploads/.gitignore
new file mode 100644 (file)
index 0000000..523e584
--- /dev/null
@@ -0,0 +1,3 @@
+# Git will ignore everything in this directory except this file.
+*
+!.gitignore