From 380e9fba21123467b41629828f97d5f2c257a128 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Mon, 7 Jan 2019 20:45:33 +0700 Subject: [PATCH 01/10] add pinned posts --- lib/pleroma/user/info.ex | 23 +++++ lib/pleroma/web/activity_pub/activity_pub.ex | 8 ++ lib/pleroma/web/common_api/common_api.ex | 34 +++++++ .../mastodon_api/mastodon_api_controller.ex | 29 ++++-- lib/pleroma/web/router.ex | 2 + test/web/activity_pub/activity_pub_test.exs | 22 +++++ test/web/common_api/common_api_test.exs | 36 +++++++ .../mastodon_api_controller_test.exs | 95 +++++++++++++++++++ 8 files changed, 242 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index 2f419a5a2..ffb800177 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -31,6 +31,7 @@ defmodule Pleroma.User.Info do field(:hub, :string, default: nil) field(:salmon, :string, default: nil) field(:hide_network, :boolean, default: false) + field(:pinned_activities, {:array, :integer}, default: []) # Found in the wild # ap_id -> Where is this used? @@ -198,4 +199,26 @@ defmodule Pleroma.User.Info do :is_admin ]) end + + def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do + if id not in info.pinned_activities do + max_pinned_posts = Pleroma.Config.get([:instance, :max_pinned_posts], 0) + params = %{pinned_activities: info.pinned_activities ++ [id]} + + info + |> cast(params, [:pinned_activities]) + |> validate_length(:pinned_activities, + max: max_pinned_posts, + message: "You have already pinned the maximum number of toots" + ) + else + change(info) + end + end + + def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do + params = %{pinned_activities: List.delete(info.pinned_activities, id)} + + cast(info, params, [:pinned_activities]) + end end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 4685f6d95..c5f62c4f8 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -394,6 +394,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do |> Map.put("type", ["Create", "Announce"]) |> Map.put("actor_id", user.ap_id) |> Map.put("whole_db", true) + |> Map.put("pinned_activity_ids", user.info.pinned_activities) recipients = if reading_user do @@ -552,6 +553,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do ) end + defp restrict_pinned(query, %{"pinned" => "true", "pinned_activity_ids" => ids}) do + from(activity in query, where: activity.id in ^ids) + end + + defp restrict_pinned(query, _), do: query + def fetch_activities_query(recipients, opts \\ %{}) do base_query = from( @@ -576,6 +583,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do |> restrict_visibility(opts) |> restrict_replies(opts) |> restrict_reblogs(opts) + |> restrict_pinned(opts) end def fetch_activities(recipients, opts \\ %{}) do diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index bb3c38f00..6d22813b2 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -164,4 +164,38 @@ defmodule Pleroma.Web.CommonAPI do object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user}) }) end + + def pin(id_or_ap_id, user) do + with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id), + %{valid?: true} = info_changeset <- + Pleroma.User.Info.add_pinnned_activity(user.info, activity), + changeset <- + Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset), + {:ok, _user} <- User.update_and_set_cache(changeset) do + {:ok, activity} + else + %{errors: [pinned_activities: {err, _}]} -> + {:error, err} + + _ -> + {:error, "Could not pin"} + end + end + + def unpin(id_or_ap_id, user) do + with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id), + %{valid?: true} = info_changeset <- + Pleroma.User.Info.remove_pinnned_activity(user.info, activity), + changeset <- + Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_changeset), + {:ok, _user} <- User.update_and_set_cache(changeset) do + {:ok, activity} + else + %{errors: [pinned_activities: {err, _}]} -> + {:error, err} + + _ -> + {:error, "Could not unpin"} + end + end end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 95d0f849c..2fb2943f1 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -256,13 +256,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do with %User{} = user <- Repo.get(User, params["id"]) do - # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here - activities = - if params["pinned"] == "true" do - [] - else - ActivityPub.fetch_user_activities(user, reading_user, params) - end + activities = ActivityPub.fetch_user_activities(user, reading_user, params) conn |> add_link_headers(:user_statuses, activities, params["id"]) @@ -409,6 +403,27 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end + def pin_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do + with {:ok, activity} <- CommonAPI.pin(ap_id_or_id, user) do + conn + |> put_view(StatusView) + |> try_render("status.json", %{activity: activity, for: user, as: :activity}) + else + {:error, reason} -> + conn + |> put_resp_content_type("application/json") + |> send_resp(:bad_request, Jason.encode!(%{"error" => reason})) + end + end + + def unpin_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do + with {:ok, activity} <- CommonAPI.unpin(ap_id_or_id, user) do + conn + |> put_view(StatusView) + |> try_render("status.json", %{activity: activity, for: user, as: :activity}) + end + end + def notifications(%{assigns: %{user: user}} = conn, params) do notifications = Notification.for_user(user, params) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8df45bf4d..ad73d8867 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -188,6 +188,8 @@ defmodule Pleroma.Web.Router do post("/statuses/:id/unreblog", MastodonAPIController, :unreblog_status) post("/statuses/:id/favourite", MastodonAPIController, :fav_status) post("/statuses/:id/unfavourite", MastodonAPIController, :unfav_status) + post("/statuses/:id/pin", MastodonAPIController, :pin_status) + post("/statuses/:id/unpin", MastodonAPIController, :unpin_status) post("/notifications/clear", MastodonAPIController, :clear_notifications) post("/notifications/dismiss", MastodonAPIController, :dismiss_notification) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 2453998ad..7d9febc47 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -601,6 +601,28 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert object end + test "returned pinned posts" do + Pleroma.Config.put([:instance, :max_pinned_posts], 3) + user = insert(:user) + + {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"}) + {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"}) + {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"}) + + CommonAPI.pin(activity_one.id, user) + + user = User.get_by_ap_id(user.ap_id) + CommonAPI.pin(activity_two.id, user) + + user = User.get_by_ap_id(user.ap_id) + CommonAPI.pin(activity_three.id, user) + + user = User.get_by_ap_id(user.ap_id) + activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"}) + + assert 3 = length(activities) + end + def data_uri do File.read!("test/fixtures/avatar_data_uri") end diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index c3674711a..59beb3120 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -96,4 +96,40 @@ defmodule Pleroma.Web.CommonAPI.Test do {:error, _} = CommonAPI.favorite(activity.id, user) end end + + describe "pinned posts" do + test "pin post" do + Pleroma.Config.put([:instance, :max_pinned_posts], 1) + user = insert(:user) + + {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + + assert {:ok, ^activity} = CommonAPI.pin(activity.id, user) + end + + test "max pinned posts" do + Pleroma.Config.put([:instance, :max_pinned_posts], 1) + user = insert(:user) + + {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"}) + {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"}) + + assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user) + + user = User.get_by_ap_id(user.ap_id) + + assert {:error, "You have already pinned the maximum number of toots"} = + CommonAPI.pin(activity_two.id, user) + end + + test "unpin post" do + Pleroma.Config.put([:instance, :max_pinned_posts], 1) + user = insert(:user) + + {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + {:ok, activity} = CommonAPI.pin(activity.id, user) + + assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user) + end + end end diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 0136acf8c..7f6c9fb88 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1453,4 +1453,99 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do user = User.get_cached_by_ap_id(user.ap_id) assert user.info.settings == %{"programming" => "socks"} end + + describe "pinned posts" do + test "returns pinned posts", %{conn: conn} do + Pleroma.Config.put([:instance, :max_pinned_posts], 1) + user = insert(:user) + + {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + {:ok, _} = CommonAPI.pin(activity.id, user) + + result = + conn + |> assign(:user, user) + |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") + |> Map.get(:resp_body) + |> Jason.decode!() + + id_str = Integer.to_string(activity.id) + + assert [%{"id" => ^id_str}] = result + end + + test "pin post", %{conn: conn} do + Pleroma.Config.put([:instance, :max_pinned_posts], 1) + user = insert(:user) + + {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + id_str = Integer.to_string(activity.id) + + assert %{"id" => ^id_str} = + conn + |> assign(:user, user) + |> post("/api/v1/statuses/#{activity.id}/pin") + |> Map.get(:resp_body) + |> Jason.decode!() + + assert [%{"id" => ^id_str}] = + conn + |> assign(:user, user) + |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") + |> Map.get(:resp_body) + |> Jason.decode!() + end + + test "unpin post", %{conn: conn} do + Pleroma.Config.put([:instance, :max_pinned_posts], 1) + user = insert(:user) + + {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + {:ok, _} = CommonAPI.pin(activity.id, user) + + id_str = Integer.to_string(activity.id) + user = User.get_by_ap_id(user.ap_id) + + assert %{"id" => ^id_str} = + conn + |> assign(:user, user) + |> post("/api/v1/statuses/#{activity.id}/unpin") + |> Map.get(:resp_body) + |> Jason.decode!() + + assert [] = + conn + |> assign(:user, user) + |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") + |> Map.get(:resp_body) + |> Jason.decode!() + end + + test "max pinned posts", %{conn: conn} do + Pleroma.Config.put([:instance, :max_pinned_posts], 1) + + user = insert(:user) + + {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"}) + {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"}) + + id_str_one = Integer.to_string(activity_one.id) + + assert %{"id" => ^id_str_one} = + conn + |> assign(:user, user) + |> post("/api/v1/statuses/#{id_str_one}/pin") + |> Map.get(:resp_body) + |> Jason.decode!() + + user = User.get_by_ap_id(user.ap_id) + + assert %{"error" => "You have already pinned the maximum number of toots"} = + conn + |> assign(:user, user) + |> post("/api/v1/statuses/#{activity_two.id}/pin") + |> Map.get(:resp_body) + |> Jason.decode!() + end + end end From 63dbd875684b192e57d356a194c5d07ad98bd810 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 8 Jan 2019 15:25:50 +0700 Subject: [PATCH 02/10] rename `post` to `status` --- lib/pleroma/user/info.ex | 6 +++--- test/web/activity_pub/activity_pub_test.exs | 4 ++-- test/web/common_api/common_api_test.exs | 14 ++++++------- .../mastodon_api_controller_test.exs | 20 +++++++++---------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index ffb800177..8ac50540d 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -202,14 +202,14 @@ defmodule Pleroma.User.Info do def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do if id not in info.pinned_activities do - max_pinned_posts = Pleroma.Config.get([:instance, :max_pinned_posts], 0) + max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0) params = %{pinned_activities: info.pinned_activities ++ [id]} info |> cast(params, [:pinned_activities]) |> validate_length(:pinned_activities, - max: max_pinned_posts, - message: "You have already pinned the maximum number of toots" + max: max_pinned_statuses, + message: "You have already pinned the maximum number of statuses" ) else change(info) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 7d9febc47..325ef6636 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -601,8 +601,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert object end - test "returned pinned posts" do - Pleroma.Config.put([:instance, :max_pinned_posts], 3) + test "returned pinned statuses" do + Pleroma.Config.put([:instance, :max_pinned_statuses], 3) user = insert(:user) {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"}) diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 59beb3120..652b53099 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -97,9 +97,9 @@ defmodule Pleroma.Web.CommonAPI.Test do end end - describe "pinned posts" do - test "pin post" do - Pleroma.Config.put([:instance, :max_pinned_posts], 1) + describe "pinned statuses" do + test "pin status" do + Pleroma.Config.put([:instance, :max_pinned_statuses], 1) user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) @@ -107,8 +107,8 @@ defmodule Pleroma.Web.CommonAPI.Test do assert {:ok, ^activity} = CommonAPI.pin(activity.id, user) end - test "max pinned posts" do - Pleroma.Config.put([:instance, :max_pinned_posts], 1) + test "max pinned statuses" do + Pleroma.Config.put([:instance, :max_pinned_statuses], 1) user = insert(:user) {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"}) @@ -122,8 +122,8 @@ defmodule Pleroma.Web.CommonAPI.Test do CommonAPI.pin(activity_two.id, user) end - test "unpin post" do - Pleroma.Config.put([:instance, :max_pinned_posts], 1) + test "unpin status" do + Pleroma.Config.put([:instance, :max_pinned_statuses], 1) user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 5ff7ef259..e7e42dd24 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1472,9 +1472,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert user.info.settings == %{"programming" => "socks"} end - describe "pinned posts" do - test "returns pinned posts", %{conn: conn} do - Pleroma.Config.put([:instance, :max_pinned_posts], 1) + describe "pinned statuses" do + test "returns pinned statuses", %{conn: conn} do + Pleroma.Config.put([:instance, :max_pinned_statuses], 1) user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) @@ -1492,8 +1492,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert [%{"id" => ^id_str}] = result end - test "pin post", %{conn: conn} do - Pleroma.Config.put([:instance, :max_pinned_posts], 1) + test "pin status", %{conn: conn} do + Pleroma.Config.put([:instance, :max_pinned_statuses], 1) user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) @@ -1514,8 +1514,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do |> Jason.decode!() end - test "unpin post", %{conn: conn} do - Pleroma.Config.put([:instance, :max_pinned_posts], 1) + test "unpin status", %{conn: conn} do + Pleroma.Config.put([:instance, :max_pinned_statuses], 1) user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) @@ -1539,8 +1539,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do |> Jason.decode!() end - test "max pinned posts", %{conn: conn} do - Pleroma.Config.put([:instance, :max_pinned_posts], 1) + test "max pinned statuses", %{conn: conn} do + Pleroma.Config.put([:instance, :max_pinned_statuses], 1) user = insert(:user) @@ -1558,7 +1558,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do user = User.get_by_ap_id(user.ap_id) - assert %{"error" => "You have already pinned the maximum number of toots"} = + assert %{"error" => "You have already pinned the maximum number of statuses"} = conn |> assign(:user, user) |> post("/api/v1/statuses/#{activity_two.id}/pin") From e679da4c34194b366624e31356d534ab73b11d2d Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 8 Jan 2019 15:27:02 +0700 Subject: [PATCH 03/10] add `pinned` property to `StatusView` --- lib/pleroma/web/mastodon_api/views/status_view.ex | 5 +++++ test/web/mastodon_api/mastodon_api_controller_test.exs | 10 +++++----- test/web/mastodon_api/status_view_test.exs | 1 + 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 8e8fa8121..db543ffe5 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -76,6 +76,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do reblogged: false, favourited: false, muted: false, + pinned: pinned?(activity, user), sensitive: false, spoiler_text: "", visibility: "public", @@ -142,6 +143,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do reblogged: present?(repeated), favourited: present?(favorited), muted: false, + pinned: pinned?(activity, user), sensitive: sensitive, spoiler_text: object["summary"] || "", visibility: get_visibility(object), @@ -295,4 +297,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do defp present?(nil), do: false defp present?(false), do: false defp present?(_), do: true + + defp pinned?(%Activity{id: id}, %User{info: %{pinned_activities: pinned_activities}}), + do: id in pinned_activities end diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index e7e42dd24..4a8c70b3b 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1489,7 +1489,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do id_str = Integer.to_string(activity.id) - assert [%{"id" => ^id_str}] = result + assert [%{"id" => ^id_str, "pinned" => true}] = result end test "pin status", %{conn: conn} do @@ -1499,14 +1499,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) id_str = Integer.to_string(activity.id) - assert %{"id" => ^id_str} = + assert %{"id" => ^id_str, "pinned" => true} = conn |> assign(:user, user) |> post("/api/v1/statuses/#{activity.id}/pin") |> Map.get(:resp_body) |> Jason.decode!() - assert [%{"id" => ^id_str}] = + assert [%{"id" => ^id_str, "pinned" => true}] = conn |> assign(:user, user) |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") @@ -1524,7 +1524,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do id_str = Integer.to_string(activity.id) user = User.get_by_ap_id(user.ap_id) - assert %{"id" => ^id_str} = + assert %{"id" => ^id_str, "pinned" => false} = conn |> assign(:user, user) |> post("/api/v1/statuses/#{activity.id}/unpin") @@ -1549,7 +1549,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do id_str_one = Integer.to_string(activity_one.id) - assert %{"id" => ^id_str_one} = + assert %{"id" => ^id_str_one, "pinned" => true} = conn |> assign(:user, user) |> post("/api/v1/statuses/#{id_str_one}/pin") diff --git a/test/web/mastodon_api/status_view_test.exs b/test/web/mastodon_api/status_view_test.exs index b953ccd76..1076b5002 100644 --- a/test/web/mastodon_api/status_view_test.exs +++ b/test/web/mastodon_api/status_view_test.exs @@ -63,6 +63,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do reblogged: false, favourited: false, muted: false, + pinned: false, sensitive: false, spoiler_text: note.data["object"]["summary"], visibility: "public", From db6f4496ebb5dbcb680104b2df80410b9dcb8407 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 8 Jan 2019 15:32:06 +0700 Subject: [PATCH 04/10] fix test --- test/web/common_api/common_api_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 652b53099..24e5e56f4 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -118,7 +118,7 @@ defmodule Pleroma.Web.CommonAPI.Test do user = User.get_by_ap_id(user.ap_id) - assert {:error, "You have already pinned the maximum number of toots"} = + assert {:error, "You have already pinned the maximum number of statuses"} = CommonAPI.pin(activity_two.id, user) end From 7b6c5f0a9d02785bee3e4c2585fea1f8983e61b0 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 8 Jan 2019 16:01:35 +0700 Subject: [PATCH 05/10] improve test readability --- test/web/activity_pub/activity_pub_test.exs | 6 +++--- test/web/common_api/common_api_test.exs | 2 +- test/web/mastodon_api/mastodon_api_controller_test.exs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 325ef6636..e6c998876 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -610,14 +610,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"}) CommonAPI.pin(activity_one.id, user) + user = refresh_record(user) - user = User.get_by_ap_id(user.ap_id) CommonAPI.pin(activity_two.id, user) + user = refresh_record(user) - user = User.get_by_ap_id(user.ap_id) CommonAPI.pin(activity_three.id, user) + user = refresh_record(user) - user = User.get_by_ap_id(user.ap_id) activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"}) assert 3 = length(activities) diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 24e5e56f4..7d5ceb398 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -116,7 +116,7 @@ defmodule Pleroma.Web.CommonAPI.Test do assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user) - user = User.get_by_ap_id(user.ap_id) + user = refresh_record(user) assert {:error, "You have already pinned the maximum number of statuses"} = CommonAPI.pin(activity_two.id, user) diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 4a8c70b3b..a3c58379e 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1522,7 +1522,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do {:ok, _} = CommonAPI.pin(activity.id, user) id_str = Integer.to_string(activity.id) - user = User.get_by_ap_id(user.ap_id) + user = refresh_record(user) assert %{"id" => ^id_str, "pinned" => false} = conn @@ -1556,7 +1556,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do |> Map.get(:resp_body) |> Jason.decode!() - user = User.get_by_ap_id(user.ap_id) + user = refresh_record(user) assert %{"error" => "You have already pinned the maximum number of statuses"} = conn From 6428ef77adef6aa3fca1fbcdcfffc5db5fc953d5 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 8 Jan 2019 16:11:03 +0700 Subject: [PATCH 06/10] add default configuration for the pinned statuses and some doc --- docs/config.md | 1 + lib/mix/tasks/pleroma/sample_config.eex | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/config.md b/docs/config.md index 1c3219efe..c903b53bf 100644 --- a/docs/config.md +++ b/docs/config.md @@ -93,6 +93,7 @@ config :pleroma, Pleroma.Mailer, * `always_show_subject_input`: When set to false, auto-hide the subject field when it's empty. * `extended_nickname_format`: Set to `true` to use extended local nicknames format (allows underscores/dashes). This will break federation with older software for theses nicknames. +* `max_pinned_statuses`: The maximum number of pinned statuses. `0` will disable the feature. ## :fe This section is used to configure Pleroma-FE, unless ``:managed_config`` in ``:instance`` is set to false. diff --git a/lib/mix/tasks/pleroma/sample_config.eex b/lib/mix/tasks/pleroma/sample_config.eex index 740b9f8d1..59c2c4fba 100644 --- a/lib/mix/tasks/pleroma/sample_config.eex +++ b/lib/mix/tasks/pleroma/sample_config.eex @@ -14,7 +14,8 @@ config :pleroma, :instance, email: "<%= email %>", limit: 5000, registrations_open: true, - dedupe_media: false + dedupe_media: false, + max_pinned_statuses: 1 config :pleroma, :media_proxy, enabled: false, From 28afcb7c31a357e05c6cb23645539389f37a15f4 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Wed, 9 Jan 2019 17:02:30 +0700 Subject: [PATCH 07/10] move defaults from sample_config.exs to config.exs --- config/config.exs | 3 ++- lib/mix/tasks/pleroma/sample_config.eex | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/config.exs b/config/config.exs index 4f4e2368a..9301758f0 100644 --- a/config/config.exs +++ b/config/config.exs @@ -138,7 +138,8 @@ config :pleroma, :instance, ], finmoji_enabled: true, mrf_transparency: true, - autofollowed_nicknames: [] + autofollowed_nicknames: [], + max_pinned_statuses: 1 config :pleroma, :markup, # XXX - unfortunately, inline images must be enabled by default right now, because diff --git a/lib/mix/tasks/pleroma/sample_config.eex b/lib/mix/tasks/pleroma/sample_config.eex index 59c2c4fba..740b9f8d1 100644 --- a/lib/mix/tasks/pleroma/sample_config.eex +++ b/lib/mix/tasks/pleroma/sample_config.eex @@ -14,8 +14,7 @@ config :pleroma, :instance, email: "<%= email %>", limit: 5000, registrations_open: true, - dedupe_media: false, - max_pinned_statuses: 1 + dedupe_media: false config :pleroma, :media_proxy, enabled: false, From 1b06e6fdf3d879422d6cb0fe57cfcef223b54196 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Wed, 9 Jan 2019 17:40:15 +0700 Subject: [PATCH 08/10] only non-reblogs, self-authored, public statuses can be pinned --- lib/pleroma/web/common_api/common_api.ex | 14 ++++++++++++-- test/web/common_api/common_api_test.exs | 10 ++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index 6d22813b2..7ec6aa0ea 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -165,8 +165,18 @@ defmodule Pleroma.Web.CommonAPI do }) end - def pin(id_or_ap_id, user) do - with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id), + def pin(id_or_ap_id, %{ap_id: user_ap_id} = user) do + with %Activity{ + actor: ^user_ap_id, + data: %{ + "type" => "Create", + "object" => %{ + "to" => object_to, + "type" => "Note" + } + } + } = activity <- get_by_id_or_ap_id(id_or_ap_id), + true <- Enum.member?(object_to, "https://www.w3.org/ns/activitystreams#Public"), %{valid?: true} = info_changeset <- Pleroma.User.Info.add_pinnned_activity(user.info, activity), changeset <- diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 7d5ceb398..84b264439 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -107,6 +107,16 @@ defmodule Pleroma.Web.CommonAPI.Test do assert {:ok, ^activity} = CommonAPI.pin(activity.id, user) end + test "only self-authored can be pinned" do + Pleroma.Config.put([:instance, :max_pinned_statuses], 1) + user_one = insert(:user) + user_two = insert(:user) + + {:ok, activity} = CommonAPI.post(user_one, %{"status" => "HI!!!"}) + + assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user_two) + end + test "max pinned statuses" do Pleroma.Config.put([:instance, :max_pinned_statuses], 1) user = insert(:user) From 44a1e6948488d9a96ed684f0a7855fe2fce02ed4 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Wed, 9 Jan 2019 19:54:19 +0700 Subject: [PATCH 09/10] Add Twitter API for the pinned statuses ``` # Only return statuses that have been pinned GET /api/statuses/user_timeline.json?pinned=true # Pin POST /api/statuses/pin/:id # Unpin POST /api/statuses/unpin/:id ``` --- lib/pleroma/web/router.ex | 3 + .../representers/activity_representer.ex | 2 + lib/pleroma/web/twitter_api/twitter_api.ex | 8 ++ .../web/twitter_api/twitter_api_controller.ex | 24 ++++++ .../web/twitter_api/views/activity_view.ex | 2 + .../activity_representer_test.exs | 3 +- .../twitter_api_controller_test.exs | 77 ++++++++++++++++++- .../twitter_api/views/activity_view_test.exs | 3 +- 8 files changed, 119 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index ad73d8867..a5f4d8126 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -355,6 +355,9 @@ defmodule Pleroma.Web.Router do post("/statuses/unretweet/:id", TwitterAPI.Controller, :unretweet) post("/statuses/destroy/:id", TwitterAPI.Controller, :delete_post) + post("/statuses/pin/:id", TwitterAPI.Controller, :pin) + post("/statuses/unpin/:id", TwitterAPI.Controller, :unpin) + get("/pleroma/friend_requests", TwitterAPI.Controller, :friend_requests) post("/pleroma/friendships/approve", TwitterAPI.Controller, :approve_friend_request) post("/pleroma/friendships/deny", TwitterAPI.Controller, :deny_friend_request) diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 245cd52fd..64f7c00c7 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -153,6 +153,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do announcement_count = object["announcement_count"] || 0 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || []) repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || []) + pinned = activity.id in user.info.pinned_activities mentions = opts[:mentioned] || [] @@ -202,6 +203,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do "repeat_num" => announcement_count, "favorited" => to_boolean(favorited), "repeated" => to_boolean(repeated), + "pinned" => pinned, "external_url" => object["external_url"] || object["id"], "tags" => tags, "activity_type" => "post", diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index ecf81d492..7a63724f1 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -82,6 +82,14 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end end + def pin(%User{} = user, ap_id_or_id) do + CommonAPI.pin(ap_id_or_id, user) + end + + def unpin(%User{} = user, ap_id_or_id) do + CommonAPI.unpin(ap_id_or_id, user) + end + def fav(%User{} = user, ap_id_or_id) do 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 diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 1e04b8c4b..d5b9d2fb8 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -375,6 +375,30 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end + def pin(%{assigns: %{user: user}} = conn, %{"id" => id}) do + with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)}, + {:ok, activity} <- TwitterAPI.pin(user, id) do + conn + |> put_view(ActivityView) + |> render("activity.json", %{activity: activity, for: user}) + else + {:error, message} -> bad_request_reply(conn, message) + err -> err + end + end + + def unpin(%{assigns: %{user: user}} = conn, %{"id" => id}) do + with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)}, + {:ok, activity} <- TwitterAPI.unpin(user, id) do + conn + |> put_view(ActivityView) + |> render("activity.json", %{activity: activity, for: user}) + else + {:error, message} -> bad_request_reply(conn, message) + err -> err + end + end + def register(conn, params) do with {:ok, user} <- TwitterAPI.register_user(params) do conn diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex index 25e1486c1..425df8672 100644 --- a/lib/pleroma/web/twitter_api/views/activity_view.ex +++ b/lib/pleroma/web/twitter_api/views/activity_view.ex @@ -243,6 +243,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do announcement_count = object["announcement_count"] || 0 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || []) repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || []) + pinned = activity.id in user.info.pinned_activities attentions = activity.recipients @@ -300,6 +301,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do "repeat_num" => announcement_count, "favorited" => !!favorited, "repeated" => !!repeated, + "pinned" => pinned, "external_url" => object["external_url"] || object["id"], "tags" => tags, "activity_type" => "post", diff --git a/test/web/twitter_api/representers/activity_representer_test.exs b/test/web/twitter_api/representers/activity_representer_test.exs index 2ac32aeb2..a4f97e0f2 100644 --- a/test/web/twitter_api/representers/activity_representer_test.exs +++ b/test/web/twitter_api/representers/activity_representer_test.exs @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors +# Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do @@ -157,6 +157,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do "repeat_num" => 3, "favorited" => false, "repeated" => false, + "pinned" => false, "external_url" => "some url", "tags" => ["nsfw", "content", "mentioning"], "activity_type" => "post", diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index c41f615ac..7d4c92c66 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors +# Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.TwitterAPI.ControllerTest do @@ -1694,4 +1694,79 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do assert object.data["name"] == description end end + + describe "POST /api/statuses/user_timeline.json?user_id=:user_id&pinned=true" do + test "it returns a list of pinned statuses", %{conn: conn} do + Pleroma.Config.put([:instance, :max_pinned_statuses], 1) + + user = insert(:user, %{name: "egor"}) + {:ok, %{id: activity_id}} = CommonAPI.post(user, %{"status" => "HI!!!"}) + {:ok, _} = CommonAPI.pin(activity_id, user) + + resp = + conn + |> get("/api/statuses/user_timeline.json", %{user_id: user.id, pinned: true}) + |> json_response(200) + + assert length(resp) == 1 + assert [%{"id" => ^activity_id, "pinned" => true}] = resp + end + end + + describe "POST /api/statuses/pin/:id" do + setup do + Pleroma.Config.put([:instance, :max_pinned_statuses], 1) + [user: insert(:user)] + end + + test "without valid credentials", %{conn: conn} do + note_activity = insert(:note_activity) + conn = post(conn, "/api/statuses/pin/#{note_activity.id}.json") + assert json_response(conn, 403) == %{"error" => "Invalid credentials."} + end + + test "with credentials", %{conn: conn, user: user} do + {:ok, activity} = CommonAPI.post(user, %{"status" => "test!"}) + + request_path = "/api/statuses/pin/#{activity.id}.json" + + response = + conn + |> with_credentials(user.nickname, "test") + |> post(request_path) + + user = refresh_record(user) + + assert json_response(response, 200) == ActivityRepresenter.to_map(activity, %{user: user}) + end + end + + describe "POST /api/statuses/unpin/:id" do + setup do + Pleroma.Config.put([:instance, :max_pinned_statuses], 1) + [user: insert(:user)] + end + + test "without valid credentials", %{conn: conn} do + note_activity = insert(:note_activity) + conn = post(conn, "/api/statuses/unpin/#{note_activity.id}.json") + assert json_response(conn, 403) == %{"error" => "Invalid credentials."} + end + + test "with credentials", %{conn: conn, user: user} do + {:ok, activity} = CommonAPI.post(user, %{"status" => "test!"}) + {:ok, activity} = CommonAPI.pin(activity.id, user) + + request_path = "/api/statuses/unpin/#{activity.id}.json" + + response = + conn + |> with_credentials(user.nickname, "test") + |> post(request_path) + + user = refresh_record(user) + + assert json_response(response, 200) == ActivityRepresenter.to_map(activity, %{user: user}) + end + end end diff --git a/test/web/twitter_api/views/activity_view_test.exs b/test/web/twitter_api/views/activity_view_test.exs index bd4878e98..374440300 100644 --- a/test/web/twitter_api/views/activity_view_test.exs +++ b/test/web/twitter_api/views/activity_view_test.exs @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors +# Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do @@ -132,6 +132,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do "possibly_sensitive" => false, "repeat_num" => 0, "repeated" => false, + "pinned" => false, "statusnet_conversation_id" => convo_id, "summary" => "", "statusnet_html" => From 6cbe63726d298ae85a75efa7591a54394469525e Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Wed, 9 Jan 2019 19:54:37 +0700 Subject: [PATCH 10/10] improve tests --- test/web/common_api/common_api_test.exs | 34 ++++------- .../mastodon_api_controller_test.exs | 59 +++++++------------ 2 files changed, 35 insertions(+), 58 deletions(-) diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 84b264439..eb69ea4b2 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors +# Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.CommonAPI.Test do @@ -98,30 +98,26 @@ defmodule Pleroma.Web.CommonAPI.Test do end describe "pinned statuses" do - test "pin status" do + setup do Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - user = insert(:user) + user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + [user: user, activity: activity] + end + + test "pin status", %{user: user, activity: activity} do assert {:ok, ^activity} = CommonAPI.pin(activity.id, user) end - test "only self-authored can be pinned" do - Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - user_one = insert(:user) - user_two = insert(:user) - - {:ok, activity} = CommonAPI.post(user_one, %{"status" => "HI!!!"}) - - assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user_two) - end - - test "max pinned statuses" do - Pleroma.Config.put([:instance, :max_pinned_statuses], 1) + test "only self-authored can be pinned", %{activity: activity} do user = insert(:user) - {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"}) + assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user) + end + + test "max pinned statuses", %{user: user, activity: activity_one} do {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"}) assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user) @@ -132,11 +128,7 @@ defmodule Pleroma.Web.CommonAPI.Test do CommonAPI.pin(activity_two.id, user) end - test "unpin status" do - Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - user = insert(:user) - - {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + test "unpin status", %{user: user, activity: activity} do {:ok, activity} = CommonAPI.pin(activity.id, user) assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user) diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index a3c58379e..b448d13f5 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2018 Pleroma Authors +# Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do @@ -1473,88 +1473,74 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do end describe "pinned statuses" do - test "returns pinned statuses", %{conn: conn} do + setup do Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - user = insert(:user) + user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + + [user: user, activity: activity] + end + + test "returns pinned statuses", %{conn: conn, user: user, activity: activity} do {:ok, _} = CommonAPI.pin(activity.id, user) result = conn |> assign(:user, user) |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(200) - id_str = Integer.to_string(activity.id) + id_str = to_string(activity.id) assert [%{"id" => ^id_str, "pinned" => true}] = result end - test "pin status", %{conn: conn} do - Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - user = insert(:user) - - {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) - id_str = Integer.to_string(activity.id) + test "pin status", %{conn: conn, user: user, activity: activity} do + id_str = to_string(activity.id) assert %{"id" => ^id_str, "pinned" => true} = conn |> assign(:user, user) |> post("/api/v1/statuses/#{activity.id}/pin") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(200) assert [%{"id" => ^id_str, "pinned" => true}] = conn |> assign(:user, user) |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(200) end - test "unpin status", %{conn: conn} do - Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - user = insert(:user) - - {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + test "unpin status", %{conn: conn, user: user, activity: activity} do {:ok, _} = CommonAPI.pin(activity.id, user) - id_str = Integer.to_string(activity.id) + id_str = to_string(activity.id) user = refresh_record(user) assert %{"id" => ^id_str, "pinned" => false} = conn |> assign(:user, user) |> post("/api/v1/statuses/#{activity.id}/unpin") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(200) assert [] = conn |> assign(:user, user) |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(200) end - test "max pinned statuses", %{conn: conn} do - Pleroma.Config.put([:instance, :max_pinned_statuses], 1) - - user = insert(:user) - - {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"}) + test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"}) - id_str_one = Integer.to_string(activity_one.id) + id_str_one = to_string(activity_one.id) assert %{"id" => ^id_str_one, "pinned" => true} = conn |> assign(:user, user) |> post("/api/v1/statuses/#{id_str_one}/pin") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(200) user = refresh_record(user) @@ -1562,8 +1548,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do conn |> assign(:user, user) |> post("/api/v1/statuses/#{activity_two.id}/pin") - |> Map.get(:resp_body) - |> Jason.decode!() + |> json_response(400) end end end