From 380e9fba21123467b41629828f97d5f2c257a128 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Mon, 7 Jan 2019 20:45:33 +0700 Subject: [PATCH] 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 @@ def admin_api_update(info, params) 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 @@ def fetch_user_activities(user, reading_user, params \\ %{}) 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 @@ defp restrict_unlisted(query) 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 @@ def fetch_activities_query(recipients, opts \\ %{}) 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 @@ def update(user) 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 @@ def public_timeline(%{assigns: %{user: user}} = conn, params) 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 @@ def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) 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 @@ test "it can fetch peertube videos" 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 @@ test "favoriting a status twice returns an error" 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 @@ test "put settings", %{conn: conn} 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