From 0bd2b85edbf3b7062570778649cf2b77cc7a0bce Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Fri, 13 Sep 2019 18:25:27 +0300 Subject: [PATCH 01/13] Separate Subscription Notifications from regular Notifications --- lib/pleroma/notification.ex | 1 - lib/pleroma/subscription_notification.ex | 266 ++++++++++++++++++ lib/pleroma/web/activity_pub/activity_pub.ex | 2 + .../controllers/mastodon_api_controller.ex | 48 ++++ lib/pleroma/web/mastodon_api/mastodon_api.ex | 10 + .../views/subscription_notification_view.ex | 61 ++++ .../web/pleroma_api/pleroma_api_controller.ex | 26 ++ lib/pleroma/web/push/impl.ex | 3 +- lib/pleroma/web/router.ex | 28 ++ lib/pleroma/web/streamer.ex | 14 +- ...5028_create_subscription_notifications.exs | 15 + test/notification_test.exs | 12 +- .../mastodon_api_controller_test.exs | 192 +++++++++++++ test/web/mastodon_api/mastodon_api_test.exs | 4 +- 14 files changed, 670 insertions(+), 12 deletions(-) create mode 100644 lib/pleroma/subscription_notification.ex create mode 100644 lib/pleroma/web/mastodon_api/views/subscription_notification_view.ex create mode 100644 priv/repo/migrations/20190824195028_create_subscription_notifications.exs diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index b7c880c51..716d98733 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -228,7 +228,6 @@ def get_notified_from_activity( [] |> Utils.maybe_notify_to_recipients(activity) |> Utils.maybe_notify_mentioned_recipients(activity) - |> Utils.maybe_notify_subscribers(activity) |> Enum.uniq() User.get_users_from_set(recipients, local_only) diff --git a/lib/pleroma/subscription_notification.ex b/lib/pleroma/subscription_notification.ex new file mode 100644 index 000000000..7ae25a7b1 --- /dev/null +++ b/lib/pleroma/subscription_notification.ex @@ -0,0 +1,266 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.SubscriptionNotification do + use Ecto.Schema + + alias Pleroma.Activity + alias Pleroma.Object + alias Pleroma.Pagination + alias Pleroma.Repo + alias Pleroma.SubscriptionNotification + alias Pleroma.User + alias Pleroma.Web.CommonAPI.Utils + alias Pleroma.Web.Push + alias Pleroma.Web.Streamer + + import Ecto.Query + import Ecto.Changeset + + @type t :: %__MODULE__{} + + schema "subscription_notifications" do + belongs_to(:user, User, type: Pleroma.FlakeId) + belongs_to(:activity, Activity, type: Pleroma.FlakeId) + + timestamps() + end + + def changeset(%SubscriptionNotification{} = notification, attrs) do + cast(notification, attrs, []) + end + + def for_user_query(user, opts \\ []) do + query = + SubscriptionNotification + |> where(user_id: ^user.id) + |> where( + [n, a], + fragment( + "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')", + a.actor + ) + ) + |> join(:inner, [n], activity in assoc(n, :activity)) + |> join(:left, [n, a], object in Object, + on: + fragment( + "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)", + object.data, + a.data + ) + ) + |> preload([n, a, o], activity: {a, object: o}) + + if opts[:with_muted] do + query + else + where(query, [n, a], a.actor not in ^user.info.muted_notifications) + |> where([n, a], a.actor not in ^user.info.blocks) + |> where( + [n, a], + fragment("substring(? from '.*://([^/]*)')", a.actor) not in ^user.info.domain_blocks + ) + |> join(:left, [n, a], tm in Pleroma.ThreadMute, + on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data) + ) + |> where([n, a, o, tm], is_nil(tm.user_id)) + end + end + + def for_user(user, opts \\ %{}) do + user + |> for_user_query(opts) + |> Pagination.fetch_paginated(opts) + end + + @doc """ + Returns notifications for user received since given date. + + ## Examples + + iex> Pleroma.SubscriptionNotification.for_user_since(%Pleroma.User{}, ~N[2019-04-13 11:22:33]) + [%Pleroma.SubscriptionNotification{}, %Pleroma.SubscriptionNotification{}] + + iex> Pleroma.SubscriptionNotification.for_user_since(%Pleroma.User{}, ~N[2019-04-15 11:22:33]) + [] + """ + @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()] + def for_user_since(user, date) do + from(n in for_user_query(user), + where: n.updated_at > ^date + ) + |> Repo.all() + end + + def clear_up_to(%{id: user_id} = _user, id) do + from( + n in SubscriptionNotification, + where: n.user_id == ^user_id, + where: n.id <= ^id + ) + |> Repo.delete_all([]) + end + + def get(%{id: user_id} = _user, id) do + query = + from( + n in SubscriptionNotification, + where: n.id == ^id, + join: activity in assoc(n, :activity), + preload: [activity: activity] + ) + + notification = Repo.one(query) + + case notification do + %{user_id: ^user_id} -> + {:ok, notification} + + _ -> + {:error, "Cannot get notification"} + end + end + + def clear(user) do + from(n in SubscriptionNotification, where: n.user_id == ^user.id) + |> Repo.delete_all() + end + + def destroy_multiple(%{id: user_id} = _user, ids) do + from(n in SubscriptionNotification, + where: n.id in ^ids, + where: n.user_id == ^user_id + ) + |> Repo.delete_all() + end + + def dismiss(%{id: user_id} = _user, id) do + notification = Repo.get(SubscriptionNotification, id) + + case notification do + %{user_id: ^user_id} -> + Repo.delete(notification) + + _ -> + {:error, "Cannot dismiss notification"} + end + end + + def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do + object = Object.normalize(activity) + + unless object && object.data["type"] == "Answer" do + users = get_notified_from_activity(activity) + notifications = Enum.map(users, fn user -> create_notification(activity, user) end) + {:ok, notifications} + else + {:ok, []} + end + end + + def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity) + when type in ["Like", "Announce", "Follow"] do + users = get_notified_from_activity(activity) + notifications = Enum.map(users, fn user -> create_notification(activity, user) end) + {:ok, notifications} + end + + def create_notifications(_), do: {:ok, []} + + # TODO move to sql, too. + def create_notification(%Activity{} = activity, %User{} = user) do + unless skip?(activity, user) do + notification = %SubscriptionNotification{user_id: user.id, activity: activity} + {:ok, notification} = Repo.insert(notification) + Streamer.stream("user", notification) + Streamer.stream("user:subscription_notification", notification) + Push.send(notification) + notification + end + end + + def get_notified_from_activity(activity, local_only \\ true) + + def get_notified_from_activity( + %Activity{data: %{"to" => _, "type" => type} = _data} = activity, + local_only + ) + when type in ["Create", "Like", "Announce", "Follow"] do + recipients = + [] + |> Utils.maybe_notify_subscribers(activity) + |> Enum.uniq() + + User.get_users_from_set(recipients, local_only) + end + + def get_notified_from_activity(_, _local_only), do: [] + + @spec skip?(Activity.t(), User.t()) :: boolean() + def skip?(activity, user) do + [ + :self, + :followers, + :follows, + :non_followers, + :non_follows, + :recently_followed + ] + |> Enum.any?(&skip?(&1, activity, user)) + end + + @spec skip?(atom(), Activity.t(), User.t()) :: boolean() + def skip?(:self, activity, user) do + activity.data["actor"] == user.ap_id + end + + def skip?( + :followers, + activity, + %{info: %{notification_settings: %{"followers" => false}}} = user + ) do + actor = activity.data["actor"] + follower = User.get_cached_by_ap_id(actor) + User.following?(follower, user) + end + + def skip?( + :non_followers, + activity, + %{info: %{notification_settings: %{"non_followers" => false}}} = user + ) do + actor = activity.data["actor"] + follower = User.get_cached_by_ap_id(actor) + !User.following?(follower, user) + end + + def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do + actor = activity.data["actor"] + followed = User.get_cached_by_ap_id(actor) + User.following?(user, followed) + end + + def skip?( + :non_follows, + activity, + %{info: %{notification_settings: %{"non_follows" => false}}} = user + ) do + actor = activity.data["actor"] + followed = User.get_cached_by_ap_id(actor) + !User.following?(user, followed) + end + + def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do + actor = activity.data["actor"] + + SubscriptionNotification.for_user(user) + |> Enum.any?(fn + %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true + _ -> false + end) + end + + def skip?(_, _, _), do: false +end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index d23ec66ac..bc9a7a2d6 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -12,6 +12,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do alias Pleroma.Object.Fetcher alias Pleroma.Pagination alias Pleroma.Repo + alias Pleroma.SubscriptionNotification alias Pleroma.Upload alias Pleroma.User alias Pleroma.Web.ActivityPub.MRF @@ -148,6 +149,7 @@ def insert(map, local \\ true, fake \\ false, bypass_actor_check \\ false) when PleromaJobQueue.enqueue(:background, Pleroma.Web.RichMedia.Helpers, [:fetch, activity]) Notification.create_notifications(activity) + SubscriptionNotification.create_notifications(activity) participations = activity diff --git a/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex index c54462bb3..3730c962c 100644 --- a/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex @@ -23,6 +23,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do alias Pleroma.Repo alias Pleroma.ScheduledActivity alias Pleroma.Stats + alias Pleroma.SubscriptionNotification alias Pleroma.User alias Pleroma.Web alias Pleroma.Web.ActivityPub.ActivityPub @@ -39,6 +40,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do alias Pleroma.Web.MastodonAPI.ReportView alias Pleroma.Web.MastodonAPI.ScheduledActivityView alias Pleroma.Web.MastodonAPI.StatusView + alias Pleroma.Web.MastodonAPI.SubscriptionNotificationView alias Pleroma.Web.MediaProxy alias Pleroma.Web.OAuth.App alias Pleroma.Web.OAuth.Authorization @@ -725,6 +727,28 @@ def notifications(%{assigns: %{user: user}} = conn, params) do |> render("index.json", %{notifications: notifications, for: user}) end + def subscription_notifications(%{assigns: %{user: user}} = conn, params) do + notifications = MastodonAPI.get_subscription_notifications(user, params) + + conn + |> add_link_headers(:subscription_notifications, notifications) + |> put_view(SubscriptionNotificationView) + |> render("index.json", %{notifications: notifications, for: user}) + end + + def get_subscription_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do + with {:ok, notification} <- SubscriptionNotification.get(user, id) do + conn + |> put_view(SubscriptionNotificationView) + |> render("show.json", %{subscription_notification: notification, for: user}) + else + {:error, reason} -> + conn + |> put_status(:forbidden) + |> json(%{"error" => reason}) + end + end + def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do with {:ok, notification} <- Notification.get(user, id) do conn @@ -743,6 +767,11 @@ def clear_notifications(%{assigns: %{user: user}} = conn, _params) do json(conn, %{}) end + def clear_subscription_notifications(%{assigns: %{user: user}} = conn, _params) do + SubscriptionNotification.clear(user) + json(conn, %{}) + end + def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do with {:ok, _notif} <- Notification.dismiss(user, id) do json(conn, %{}) @@ -754,11 +783,30 @@ def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _para end end + def dismiss_subscription_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do + with {:ok, _notif} <- SubscriptionNotification.dismiss(user, id) do + json(conn, %{}) + else + {:error, reason} -> + conn + |> put_status(:forbidden) + |> json(%{"error" => reason}) + end + end + def destroy_multiple(%{assigns: %{user: user}} = conn, %{"ids" => ids} = _params) do Notification.destroy_multiple(user, ids) json(conn, %{}) end + def destroy_multiple_subscription_notifications( + %{assigns: %{user: user}} = conn, + %{"ids" => ids} = _params + ) do + SubscriptionNotification.destroy_multiple(user, ids) + json(conn, %{}) + end + def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do id = List.wrap(id) q = from(u in User, where: u.id in ^id) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api.ex b/lib/pleroma/web/mastodon_api/mastodon_api.ex index ac01d1ff3..6751e24d8 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api.ex @@ -10,6 +10,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI do alias Pleroma.Notification alias Pleroma.Pagination alias Pleroma.ScheduledActivity + alias Pleroma.SubscriptionNotification alias Pleroma.User alias Pleroma.Web.CommonAPI @@ -62,6 +63,15 @@ def get_notifications(user, params \\ %{}) do |> Pagination.fetch_paginated(params) end + def get_subscription_notifications(user, params \\ %{}) do + options = cast_params(params) + + user + |> SubscriptionNotification.for_user_query(options) + |> restrict(:exclude_types, options) + |> Pagination.fetch_paginated(params) + end + def get_scheduled_activities(user, params \\ %{}) do user |> ScheduledActivity.for_user_query() diff --git a/lib/pleroma/web/mastodon_api/views/subscription_notification_view.ex b/lib/pleroma/web/mastodon_api/views/subscription_notification_view.ex new file mode 100644 index 000000000..c6f0b5064 --- /dev/null +++ b/lib/pleroma/web/mastodon_api/views/subscription_notification_view.ex @@ -0,0 +1,61 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.MastodonAPI.SubscriptionNotificationView do + use Pleroma.Web, :view + + alias Pleroma.Activity + # alias Pleroma.SubscriptionNotification + alias Pleroma.User + alias Pleroma.Web.CommonAPI + alias Pleroma.Web.MastodonAPI.AccountView + alias Pleroma.Web.MastodonAPI.SubscriptionNotificationView + alias Pleroma.Web.MastodonAPI.StatusView + + def render("index.json", %{notifications: notifications, for: user}) do + safe_render_many(notifications, SubscriptionNotificationView, "show.json", %{for: user}) + end + + def render("show.json", %{ + subscription_notification: %{activity: activity} = notification, + for: user + }) do + actor = User.get_cached_by_ap_id(activity.data["actor"]) + parent_activity = Activity.get_create_by_object_ap_id(activity.data["object"]) + mastodon_type = Activity.mastodon_notification_type(activity) + + response = %{ + id: to_string(notification.id), + type: mastodon_type, + created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at), + account: AccountView.render("account.json", %{user: actor, for: user}) + } + + case mastodon_type do + "mention" -> + response + |> Map.merge(%{ + status: StatusView.render("status.json", %{activity: activity, for: user}) + }) + + "favourite" -> + response + |> Map.merge(%{ + status: StatusView.render("status.json", %{activity: parent_activity, for: user}) + }) + + "reblog" -> + response + |> Map.merge(%{ + status: StatusView.render("status.json", %{activity: parent_activity, for: user}) + }) + + "follow" -> + response + + _ -> + nil + end + end +end diff --git a/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex b/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex index f4df3b024..71792d913 100644 --- a/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex +++ b/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex @@ -9,6 +9,7 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do alias Pleroma.Conversation.Participation alias Pleroma.Notification + alias Pleroma.SubscriptionNotification alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.MastodonAPI.ConversationView alias Pleroma.Web.MastodonAPI.NotificationView @@ -95,4 +96,29 @@ def read_notification(%{assigns: %{user: user}} = conn, %{"max_id" => max_id}) d |> render("index.json", %{notifications: notifications, for: user}) end end + + def delete_subscription_notification(%{assigns: %{user: user}} = conn, %{ + "id" => notification_id + }) do + with {:ok, notification} <- SubscriptionNotification.dismiss(user, notification_id) do + conn + |> put_view(NotificationView) + |> render("show.json", %{notification: notification, for: user}) + else + {:error, message} -> + conn + |> put_status(:bad_request) + |> json(%{"error" => message}) + end + end + + def read_subscription_notification(%{assigns: %{user: user}} = conn, %{"max_id" => max_id}) do + with notifications <- SubscriptionNotification.clear_up_to(user, max_id) do + notifications = Enum.take(notifications, 80) + + conn + |> put_view(NotificationView) + |> render("index.json", %{notifications: notifications, for: user}) + end + end end diff --git a/lib/pleroma/web/push/impl.ex b/lib/pleroma/web/push/impl.ex index 35d3ff07c..7ea5607fa 100644 --- a/lib/pleroma/web/push/impl.ex +++ b/lib/pleroma/web/push/impl.ex @@ -9,6 +9,7 @@ defmodule Pleroma.Web.Push.Impl do alias Pleroma.Notification alias Pleroma.Object alias Pleroma.Repo + alias Pleroma.SubscriptionNotification alias Pleroma.User alias Pleroma.Web.Metadata.Utils alias Pleroma.Web.Push.Subscription @@ -19,7 +20,7 @@ defmodule Pleroma.Web.Push.Impl do @types ["Create", "Follow", "Announce", "Like"] @doc "Performs sending notifications for user subscriptions" - @spec perform(Notification.t()) :: list(any) | :error + @spec perform(Notification.t() | SubscriptionNotification.t()) :: list(any) | :error def perform( %{ activity: %{data: %{"type" => activity_type}, id: activity_id} = activity, diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index b0464037e..dbd0deecd 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -300,11 +300,39 @@ defmodule Pleroma.Web.Router do get("/bookmarks", MastodonAPIController, :bookmarks) post("/notifications/clear", MastodonAPIController, :clear_notifications) + + post( + "/notifications/subscription/clear", + MastodonAPIController, + :clear_subscription_notifications + ) + post("/notifications/dismiss", MastodonAPIController, :dismiss_notification) + + post( + "/notifications/subscription/dismiss", + MastodonAPIController, + :dismiss_subscription_notification + ) + get("/notifications", MastodonAPIController, :notifications) + get("/notifications/subscription", MastodonAPIController, :subscription_notifications) get("/notifications/:id", MastodonAPIController, :get_notification) + + get( + "/notifications/subscription/:id", + MastodonAPIController, + :get_subscription_notification + ) + delete("/notifications/destroy_multiple", MastodonAPIController, :destroy_multiple) + delete( + "/notifications/subscription/destroy_multiple", + MastodonAPIController, + :destroy_multiple_subscription_notifications + ) + get("/scheduled_statuses", MastodonAPIController, :scheduled_statuses) get("/scheduled_statuses/:id", MastodonAPIController, :show_scheduled_status) diff --git a/lib/pleroma/web/streamer.ex b/lib/pleroma/web/streamer.ex index 587c43f40..42d95e33a 100644 --- a/lib/pleroma/web/streamer.ex +++ b/lib/pleroma/web/streamer.ex @@ -10,6 +10,7 @@ defmodule Pleroma.Web.Streamer do alias Pleroma.Conversation.Participation alias Pleroma.Notification alias Pleroma.Object + alias Pleroma.SubscriptionNotification alias Pleroma.User alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.Visibility @@ -208,10 +209,17 @@ def represent_conversation(%Participation{} = participation) do |> Jason.encode!() end - @spec represent_notification(User.t(), Notification.t()) :: binary() - defp represent_notification(%User{} = user, %Notification{} = notify) do + @spec represent_notification(User.t(), Notification.t() | %SubscriptionNotification{}) :: + binary() + defp represent_notification(%User{} = user, notify) do + event = + case notify do + %Notification{} -> "notification" + %SubscriptionNotification{} -> "subscription_norification" + end + %{ - event: "notification", + event: event, payload: NotificationView.render( "show.json", diff --git a/priv/repo/migrations/20190824195028_create_subscription_notifications.exs b/priv/repo/migrations/20190824195028_create_subscription_notifications.exs new file mode 100644 index 000000000..fcceb4386 --- /dev/null +++ b/priv/repo/migrations/20190824195028_create_subscription_notifications.exs @@ -0,0 +1,15 @@ +defmodule Pleroma.Repo.Migrations.CreateSubscriptionNotifications do + use Ecto.Migration + + def change do + create_if_not_exists table(:subscription_notifications) do + add(:user_id, references(:users, type: :uuid, on_delete: :delete_all)) + add(:activity_id, references(:activities, type: :uuid, on_delete: :delete_all)) + + timestamps() + end + + create_if_not_exists(index(:subscription_notifications, [:user_id])) + create_if_not_exists(index(:subscription_notifications, ["id desc nulls last"])) + end +end diff --git a/test/notification_test.exs b/test/notification_test.exs index 2a52dad8d..0e2635aad 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -32,16 +32,16 @@ test "notifies someone when they are directly addressed" do assert other_notification.activity_id == activity.id end - test "it creates a notification for subscribed users" do + test "it does not create a notification for subscribed users" do user = insert(:user) subscriber = insert(:user) User.subscribe(subscriber, user) {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"}) - {:ok, [notification]} = Notification.create_notifications(status) + {:ok, notifications} = Notification.create_notifications(status) - assert notification.user_id == subscriber.id + assert notifications == [] end test "does not create a notification for subscribed users if status is a reply" do @@ -190,14 +190,16 @@ test "it doesn't create a notification for follow-unfollow-follow chains" do refute Notification.create_notification(activity_dupe, followed_user) end - test "it doesn't create duplicate notifications for follow+subscribed users" do + test "it doesn't create notifications for follow+subscribed users" do user = insert(:user) subscriber = insert(:user) {:ok, _, _, _} = CommonAPI.follow(subscriber, user) User.subscribe(subscriber, user) {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"}) - {:ok, [_notif]} = Notification.create_notifications(status) + {:ok, notifications} = Notification.create_notifications(status) + + assert notifications == [] end test "it doesn't create subscription notifications if the recipient cannot see the status" do diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index f4902d043..95fcecc52 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -13,6 +13,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do alias Pleroma.Object alias Pleroma.Repo alias Pleroma.ScheduledActivity + alias Pleroma.SubscriptionNotification alias Pleroma.User alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.CommonAPI @@ -1273,6 +1274,197 @@ test "see notifications after muting user with notifications and with_muted para end end + describe "subscription_notifications" do + setup do + user = insert(:user) + subscriber = insert(:user) + + User.subscribe(subscriber, user) + + {:ok, %{user: user, subscriber: subscriber}} + end + + test "list of notifications", %{conn: conn, user: user, subscriber: subscriber} do + status_text = "Hello" + {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text}) + + conn = + conn + |> assign(:user, subscriber) + |> get("/api/v1/notifications/subscription") + + assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200) + assert response == status_text + end + + test "getting a single notification", %{conn: conn, user: user, subscriber: subscriber} do + status_text = "Hello" + + {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text}) + [notification] = Repo.all(SubscriptionNotification) + + conn = + conn + |> assign(:user, subscriber) + |> get("/api/v1/notifications/subscription/#{notification.id}") + + assert %{"status" => %{"content" => response}} = json_response(conn, 200) + assert response == status_text + end + + test "dismissing a single notification also deletes it", %{ + conn: conn, + user: user, + subscriber: subscriber + } do + status_text = "Hello" + {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text}) + + [notification] = Repo.all(SubscriptionNotification) + + conn = + conn + |> assign(:user, subscriber) + |> post("/api/v1/notifications/subscription/dismiss", %{"id" => notification.id}) + + assert %{} = json_response(conn, 200) + + assert Repo.all(SubscriptionNotification) == [] + end + + test "clearing all notifications also deletes them", %{ + conn: conn, + user: user, + subscriber: subscriber + } do + status_text1 = "Hello" + status_text2 = "Hello again" + {:ok, _activity1} = CommonAPI.post(user, %{"status" => status_text1}) + {:ok, _activity2} = CommonAPI.post(user, %{"status" => status_text2}) + + conn = + conn + |> assign(:user, subscriber) + |> post("/api/v1/notifications/subscription/clear") + + assert %{} = json_response(conn, 200) + + conn = + build_conn() + |> assign(:user, subscriber) + |> get("/api/v1/notifications/subscription") + + assert json_response(conn, 200) == [] + + assert Repo.all(SubscriptionNotification) == [] + end + + test "paginates notifications using min_id, since_id, max_id, and limit", %{ + conn: conn, + user: user, + subscriber: subscriber + } do + {:ok, activity1} = CommonAPI.post(user, %{"status" => "Hello 1"}) + {:ok, activity2} = CommonAPI.post(user, %{"status" => "Hello 2"}) + {:ok, activity3} = CommonAPI.post(user, %{"status" => "Hello 3"}) + {:ok, activity4} = CommonAPI.post(user, %{"status" => "Hello 4"}) + + notification1_id = + Repo.get_by(SubscriptionNotification, activity_id: activity1.id).id |> to_string() + + notification2_id = + Repo.get_by(SubscriptionNotification, activity_id: activity2.id).id |> to_string() + + notification3_id = + Repo.get_by(SubscriptionNotification, activity_id: activity3.id).id |> to_string() + + notification4_id = + Repo.get_by(SubscriptionNotification, activity_id: activity4.id).id |> to_string() + + conn = assign(conn, :user, subscriber) + + # min_id + conn_res = + get(conn, "/api/v1/notifications/subscription?limit=2&min_id=#{notification1_id}") + + result = json_response(conn_res, 200) + assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result + + # since_id + conn_res = + get(conn, "/api/v1/notifications/subscription?limit=2&since_id=#{notification1_id}") + + result = json_response(conn_res, 200) + assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result + + # max_id + conn_res = + get(conn, "/api/v1/notifications/subscription?limit=2&max_id=#{notification4_id}") + + result = json_response(conn_res, 200) + assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result + end + + test "destroy multiple", %{conn: conn, user: user1, subscriber: user2} do + # mutual subscription + User.subscribe(user1, user2) + + {:ok, activity1} = CommonAPI.post(user1, %{"status" => "Hello 1"}) + {:ok, activity2} = CommonAPI.post(user1, %{"status" => "World 1"}) + {:ok, activity3} = CommonAPI.post(user2, %{"status" => "Hello 2"}) + {:ok, activity4} = CommonAPI.post(user2, %{"status" => "World 2"}) + + notification1_id = + Repo.get_by(SubscriptionNotification, activity_id: activity1.id).id |> to_string() + + notification2_id = + Repo.get_by(SubscriptionNotification, activity_id: activity2.id).id |> to_string() + + notification3_id = + Repo.get_by(SubscriptionNotification, activity_id: activity3.id).id |> to_string() + + notification4_id = + Repo.get_by(SubscriptionNotification, activity_id: activity4.id).id |> to_string() + + conn = assign(conn, :user, user1) + + conn_res = get(conn, "/api/v1/notifications/subscription") + + result = json_response(conn_res, 200) + + Enum.each(result, fn %{"id" => id} -> + assert id in [notification3_id, notification4_id] + end) + + conn2 = assign(conn, :user, user2) + + conn_res = get(conn2, "/api/v1/notifications/subscription") + + result = json_response(conn_res, 200) + + Enum.each(result, fn %{"id" => id} -> + assert id in [notification1_id, notification2_id] + end) + + conn_destroy = + delete(conn, "/api/v1/notifications/subscription/destroy_multiple", %{ + "ids" => [notification3_id, notification4_id] + }) + + assert json_response(conn_destroy, 200) == %{} + + conn_res = get(conn2, "/api/v1/notifications/subscription") + + result = json_response(conn_res, 200) + + Enum.each(result, fn %{"id" => id} -> + assert id in [notification1_id, notification2_id] + end) + + assert length(Repo.all(SubscriptionNotification)) == 2 + end + end + describe "reblogging" do test "reblogs and returns the reblogged status", %{conn: conn} do activity = insert(:note_activity) diff --git a/test/web/mastodon_api/mastodon_api_test.exs b/test/web/mastodon_api/mastodon_api_test.exs index 7fcb2bd55..848fce7ad 100644 --- a/test/web/mastodon_api/mastodon_api_test.exs +++ b/test/web/mastodon_api/mastodon_api_test.exs @@ -75,9 +75,9 @@ test "returns notifications for user" do User.subscribe(subscriber, user) - {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin"}) + {:ok, status} = CommonAPI.post(user, %{"status" => "Akariiiin @#{subscriber.nickname}"}) - {:ok, status1} = CommonAPI.post(user, %{"status" => "Magi"}) + {:ok, status1} = CommonAPI.post(user, %{"status" => "Magi @#{subscriber.nickname}"}) {:ok, [notification]} = Notification.create_notifications(status) {:ok, [notification1]} = Notification.create_notifications(status1) res = MastodonAPI.get_notifications(subscriber) From 56b60798c2282055089424f5dc6770a10876626b Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Mon, 16 Sep 2019 20:50:14 +0300 Subject: [PATCH 02/13] Code style fixes --- lib/pleroma/subscription_notification.ex | 72 +++++++++---------- .../views/subscription_notification_view.ex | 1 - 2 files changed, 33 insertions(+), 40 deletions(-) diff --git a/lib/pleroma/subscription_notification.ex b/lib/pleroma/subscription_notification.ex index 7ae25a7b1..9ce0c6598 100644 --- a/lib/pleroma/subscription_notification.ex +++ b/lib/pleroma/subscription_notification.ex @@ -56,7 +56,8 @@ def for_user_query(user, opts \\ []) do if opts[:with_muted] do query else - where(query, [n, a], a.actor not in ^user.info.muted_notifications) + query + |> where([n, a], a.actor not in ^user.info.muted_notifications) |> where([n, a], a.actor not in ^user.info.blocks) |> where( [n, a], @@ -88,9 +89,9 @@ def for_user(user, opts \\ %{}) do """ @spec for_user_since(Pleroma.User.t(), NaiveDateTime.t()) :: [t()] def for_user_since(user, date) do - from(n in for_user_query(user), - where: n.updated_at > ^date - ) + user + |> for_user_query() + |> where([n], n.updated_at > ^date) |> Repo.all() end @@ -112,10 +113,8 @@ def get(%{id: user_id} = _user, id) do preload: [activity: activity] ) - notification = Repo.one(query) - - case notification do - %{user_id: ^user_id} -> + case Repo.one(query) do + %{user_id: ^user_id} = notification -> {:ok, notification} _ -> @@ -137,10 +136,8 @@ def destroy_multiple(%{id: user_id} = _user, ids) do end def dismiss(%{id: user_id} = _user, id) do - notification = Repo.get(SubscriptionNotification, id) - - case notification do - %{user_id: ^user_id} -> + case Repo.get(SubscriptionNotification, id) do + %{user_id: ^user_id} = notification -> Repo.delete(notification) _ -> @@ -149,21 +146,24 @@ def dismiss(%{id: user_id} = _user, id) do end def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do - object = Object.normalize(activity) + case Object.normalize(activity) do + %{data: %{"type" => "Answer"}} -> + {:ok, []} - unless object && object.data["type"] == "Answer" do - users = get_notified_from_activity(activity) - notifications = Enum.map(users, fn user -> create_notification(activity, user) end) - {:ok, notifications} - else - {:ok, []} + _ -> + users = get_notified_from_activity(activity) + notifications = Enum.map(users, fn user -> create_notification(activity, user) end) + {:ok, notifications} end end def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity) when type in ["Like", "Announce", "Follow"] do - users = get_notified_from_activity(activity) - notifications = Enum.map(users, fn user -> create_notification(activity, user) end) + notifications = + activity + |> get_notified_from_activity() + |> Enum.map(&create_notification(activity, &1)) + {:ok, notifications} end @@ -188,12 +188,10 @@ def get_notified_from_activity( local_only ) when type in ["Create", "Like", "Announce", "Follow"] do - recipients = - [] - |> Utils.maybe_notify_subscribers(activity) - |> Enum.uniq() - - User.get_users_from_set(recipients, local_only) + [] + |> Utils.maybe_notify_subscribers(activity) + |> Enum.uniq() + |> User.get_users_from_set(local_only) end def get_notified_from_activity(_, _local_only), do: [] @@ -218,12 +216,12 @@ def skip?(:self, activity, user) do def skip?( :followers, - activity, + %{data: %{"actor" => actor}}, %{info: %{notification_settings: %{"followers" => false}}} = user ) do - actor = activity.data["actor"] - follower = User.get_cached_by_ap_id(actor) - User.following?(follower, user) + actor + |> User.get_cached_by_ap_id() + |> User.following?(user) end def skip?( @@ -252,14 +250,10 @@ def skip?( !User.following?(user, followed) end - def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do - actor = activity.data["actor"] - - SubscriptionNotification.for_user(user) - |> Enum.any?(fn - %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true - _ -> false - end) + def skip?(:recently_followed, %{data: %{"type" => "Follow", "actor" => actor}}, user) do + user + |> SubscriptionNotification.for_user() + |> Enum.any?(&match?(%{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}}, &1)) end def skip?(_, _, _), do: false diff --git a/lib/pleroma/web/mastodon_api/views/subscription_notification_view.ex b/lib/pleroma/web/mastodon_api/views/subscription_notification_view.ex index c6f0b5064..83d2b647f 100644 --- a/lib/pleroma/web/mastodon_api/views/subscription_notification_view.ex +++ b/lib/pleroma/web/mastodon_api/views/subscription_notification_view.ex @@ -6,7 +6,6 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionNotificationView do use Pleroma.Web, :view alias Pleroma.Activity - # alias Pleroma.SubscriptionNotification alias Pleroma.User alias Pleroma.Web.CommonAPI alias Pleroma.Web.MastodonAPI.AccountView From 6042e21b25885f9c3214d3296d9d2fdf35ad58ea Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Mon, 16 Sep 2019 21:59:49 +0300 Subject: [PATCH 03/13] Move subscription notifications to a separate controller --- .../controllers/mastodon_api_controller.ex | 48 ---- lib/pleroma/web/mastodon_api/mastodon_api.ex | 10 - lib/pleroma/web/pleroma_api/pleroma_api.ex | 40 +++ .../subscription_notification_controller.ex | 59 +++++ .../views/subscription_notification_view.ex | 4 +- lib/pleroma/web/router.ex | 33 +-- .../mastodon_api_controller_test.exs | 192 -------------- ...scription_notification_controller_test.exs | 234 ++++++++++++++++++ 8 files changed, 343 insertions(+), 277 deletions(-) create mode 100644 lib/pleroma/web/pleroma_api/pleroma_api.ex create mode 100644 lib/pleroma/web/pleroma_api/subscription_notification_controller.ex rename lib/pleroma/web/{mastodon_api => pleroma_api}/views/subscription_notification_view.ex (93%) create mode 100644 test/web/pleroma_api/subscription_notification_controller_test.exs diff --git a/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex index eefdb8c06..060137b80 100644 --- a/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex @@ -23,7 +23,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do alias Pleroma.Repo alias Pleroma.ScheduledActivity alias Pleroma.Stats - alias Pleroma.SubscriptionNotification alias Pleroma.User alias Pleroma.Web alias Pleroma.Web.ActivityPub.ActivityPub @@ -40,7 +39,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do alias Pleroma.Web.MastodonAPI.ReportView alias Pleroma.Web.MastodonAPI.ScheduledActivityView alias Pleroma.Web.MastodonAPI.StatusView - alias Pleroma.Web.MastodonAPI.SubscriptionNotificationView alias Pleroma.Web.MediaProxy alias Pleroma.Web.OAuth.App alias Pleroma.Web.OAuth.Authorization @@ -727,28 +725,6 @@ def notifications(%{assigns: %{user: user}} = conn, params) do |> render("index.json", %{notifications: notifications, for: user}) end - def subscription_notifications(%{assigns: %{user: user}} = conn, params) do - notifications = MastodonAPI.get_subscription_notifications(user, params) - - conn - |> add_link_headers(:subscription_notifications, notifications) - |> put_view(SubscriptionNotificationView) - |> render("index.json", %{notifications: notifications, for: user}) - end - - def get_subscription_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do - with {:ok, notification} <- SubscriptionNotification.get(user, id) do - conn - |> put_view(SubscriptionNotificationView) - |> render("show.json", %{subscription_notification: notification, for: user}) - else - {:error, reason} -> - conn - |> put_status(:forbidden) - |> json(%{"error" => reason}) - end - end - def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do with {:ok, notification} <- Notification.get(user, id) do conn @@ -767,11 +743,6 @@ def clear_notifications(%{assigns: %{user: user}} = conn, _params) do json(conn, %{}) end - def clear_subscription_notifications(%{assigns: %{user: user}} = conn, _params) do - SubscriptionNotification.clear(user) - json(conn, %{}) - end - def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do with {:ok, _notif} <- Notification.dismiss(user, id) do json(conn, %{}) @@ -783,30 +754,11 @@ def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _para end end - def dismiss_subscription_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do - with {:ok, _notif} <- SubscriptionNotification.dismiss(user, id) do - json(conn, %{}) - else - {:error, reason} -> - conn - |> put_status(:forbidden) - |> json(%{"error" => reason}) - end - end - def destroy_multiple(%{assigns: %{user: user}} = conn, %{"ids" => ids} = _params) do Notification.destroy_multiple(user, ids) json(conn, %{}) end - def destroy_multiple_subscription_notifications( - %{assigns: %{user: user}} = conn, - %{"ids" => ids} = _params - ) do - SubscriptionNotification.destroy_multiple(user, ids) - json(conn, %{}) - end - def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do id = List.wrap(id) q = from(u in User, where: u.id in ^id) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api.ex b/lib/pleroma/web/mastodon_api/mastodon_api.ex index 6751e24d8..ac01d1ff3 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api.ex @@ -10,7 +10,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI do alias Pleroma.Notification alias Pleroma.Pagination alias Pleroma.ScheduledActivity - alias Pleroma.SubscriptionNotification alias Pleroma.User alias Pleroma.Web.CommonAPI @@ -63,15 +62,6 @@ def get_notifications(user, params \\ %{}) do |> Pagination.fetch_paginated(params) end - def get_subscription_notifications(user, params \\ %{}) do - options = cast_params(params) - - user - |> SubscriptionNotification.for_user_query(options) - |> restrict(:exclude_types, options) - |> Pagination.fetch_paginated(params) - end - def get_scheduled_activities(user, params \\ %{}) do user |> ScheduledActivity.for_user_query() diff --git a/lib/pleroma/web/pleroma_api/pleroma_api.ex b/lib/pleroma/web/pleroma_api/pleroma_api.ex new file mode 100644 index 000000000..480964845 --- /dev/null +++ b/lib/pleroma/web/pleroma_api/pleroma_api.ex @@ -0,0 +1,40 @@ +defmodule Pleroma.Web.PleromaAPI.PleromaAPI do + import Ecto.Query + import Ecto.Changeset + + alias Pleroma.Activity + alias Pleroma.Pagination + alias Pleroma.SubscriptionNotification + + def get_subscription_notifications(user, params \\ %{}) do + options = cast_params(params) + + user + |> SubscriptionNotification.for_user_query(options) + |> restrict(:exclude_types, options) + |> Pagination.fetch_paginated(params) + end + + defp cast_params(params) do + param_types = %{ + exclude_types: {:array, :string}, + reblogs: :boolean, + with_muted: :boolean + } + + changeset = cast({%{}, param_types}, params, Map.keys(param_types)) + changeset.changes + end + + defp restrict(query, :exclude_types, %{exclude_types: mastodon_types = [_ | _]}) do + ap_types = + mastodon_types + |> Enum.map(&Activity.from_mastodon_notification_type/1) + |> Enum.filter(& &1) + + query + |> where([q, a], not fragment("? @> ARRAY[?->>'type']::varchar[]", ^ap_types, a.data)) + end + + defp restrict(query, _, _), do: query +end diff --git a/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex b/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex new file mode 100644 index 000000000..bfc2631dd --- /dev/null +++ b/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex @@ -0,0 +1,59 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationController do + use Pleroma.Web, :controller + + import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2] + + alias Pleroma.SubscriptionNotification + alias Pleroma.Web.PleromaAPI.PleromaAPI + alias Pleroma.Web.PleromaAPI.SubscriptionNotificationView + + def list(%{assigns: %{user: user}} = conn, params) do + notifications = PleromaAPI.get_subscription_notifications(user, params) + + conn + |> add_link_headers(notifications) + |> put_view(SubscriptionNotificationView) + |> render("index.json", %{notifications: notifications, for: user}) + end + + def get(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do + with {:ok, notification} <- SubscriptionNotification.get(user, id) do + conn + |> put_view(SubscriptionNotificationView) + |> render("show.json", %{subscription_notification: notification, for: user}) + else + {:error, reason} -> + conn + |> put_status(:forbidden) + |> json(%{"error" => reason}) + end + end + + def clear(%{assigns: %{user: user}} = conn, _params) do + SubscriptionNotification.clear(user) + json(conn, %{}) + end + + def dismiss(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do + with {:ok, _notif} <- SubscriptionNotification.dismiss(user, id) do + json(conn, %{}) + else + {:error, reason} -> + conn + |> put_status(:forbidden) + |> json(%{"error" => reason}) + end + end + + def destroy_multiple( + %{assigns: %{user: user}} = conn, + %{"ids" => ids} = _params + ) do + SubscriptionNotification.destroy_multiple(user, ids) + json(conn, %{}) + end +end diff --git a/lib/pleroma/web/mastodon_api/views/subscription_notification_view.ex b/lib/pleroma/web/pleroma_api/views/subscription_notification_view.ex similarity index 93% rename from lib/pleroma/web/mastodon_api/views/subscription_notification_view.ex rename to lib/pleroma/web/pleroma_api/views/subscription_notification_view.ex index 83d2b647f..d7f7f4c5a 100644 --- a/lib/pleroma/web/mastodon_api/views/subscription_notification_view.ex +++ b/lib/pleroma/web/pleroma_api/views/subscription_notification_view.ex @@ -2,15 +2,15 @@ # Copyright © 2017-2019 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only -defmodule Pleroma.Web.MastodonAPI.SubscriptionNotificationView do +defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationView do use Pleroma.Web, :view alias Pleroma.Activity alias Pleroma.User alias Pleroma.Web.CommonAPI alias Pleroma.Web.MastodonAPI.AccountView - alias Pleroma.Web.MastodonAPI.SubscriptionNotificationView alias Pleroma.Web.MastodonAPI.StatusView + alias Pleroma.Web.PleromaAPI.SubscriptionNotificationView def render("index.json", %{notifications: notifications, for: user}) do safe_render_many(notifications, SubscriptionNotificationView, "show.json", %{for: user}) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 409fc9eca..05891b6c0 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -268,6 +268,14 @@ defmodule Pleroma.Web.Router do pipe_through(:oauth_read) get("/conversations/:id/statuses", PleromaAPIController, :conversation_statuses) get("/conversations/:id", PleromaAPIController, :conversation) + + scope "/subscription_notifications" do + post("/clear", SubscriptionNotificationController, :clear) + post("/dismiss", SubscriptionNotificationController, :dismiss) + delete("/destroy_multiple", SubscriptionNotificationController, :destroy_multiple) + get("/", SubscriptionNotificationController, :list) + get("/id", SubscriptionNotificationController, :get) + end end scope [] do @@ -302,38 +310,13 @@ defmodule Pleroma.Web.Router do post("/notifications/clear", MastodonAPIController, :clear_notifications) - post( - "/notifications/subscription/clear", - MastodonAPIController, - :clear_subscription_notifications - ) - post("/notifications/dismiss", MastodonAPIController, :dismiss_notification) - post( - "/notifications/subscription/dismiss", - MastodonAPIController, - :dismiss_subscription_notification - ) - get("/notifications", MastodonAPIController, :notifications) - get("/notifications/subscription", MastodonAPIController, :subscription_notifications) get("/notifications/:id", MastodonAPIController, :get_notification) - get( - "/notifications/subscription/:id", - MastodonAPIController, - :get_subscription_notification - ) - delete("/notifications/destroy_multiple", MastodonAPIController, :destroy_multiple) - delete( - "/notifications/subscription/destroy_multiple", - MastodonAPIController, - :destroy_multiple_subscription_notifications - ) - get("/scheduled_statuses", MastodonAPIController, :scheduled_statuses) get("/scheduled_statuses/:id", MastodonAPIController, :show_scheduled_status) diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 1d2d9e134..fb04748bb 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -13,7 +13,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do alias Pleroma.Object alias Pleroma.Repo alias Pleroma.ScheduledActivity - alias Pleroma.SubscriptionNotification alias Pleroma.Tests.ObanHelpers alias Pleroma.User alias Pleroma.Web.ActivityPub.ActivityPub @@ -1275,197 +1274,6 @@ test "see notifications after muting user with notifications and with_muted para end end - describe "subscription_notifications" do - setup do - user = insert(:user) - subscriber = insert(:user) - - User.subscribe(subscriber, user) - - {:ok, %{user: user, subscriber: subscriber}} - end - - test "list of notifications", %{conn: conn, user: user, subscriber: subscriber} do - status_text = "Hello" - {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text}) - - conn = - conn - |> assign(:user, subscriber) - |> get("/api/v1/notifications/subscription") - - assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200) - assert response == status_text - end - - test "getting a single notification", %{conn: conn, user: user, subscriber: subscriber} do - status_text = "Hello" - - {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text}) - [notification] = Repo.all(SubscriptionNotification) - - conn = - conn - |> assign(:user, subscriber) - |> get("/api/v1/notifications/subscription/#{notification.id}") - - assert %{"status" => %{"content" => response}} = json_response(conn, 200) - assert response == status_text - end - - test "dismissing a single notification also deletes it", %{ - conn: conn, - user: user, - subscriber: subscriber - } do - status_text = "Hello" - {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text}) - - [notification] = Repo.all(SubscriptionNotification) - - conn = - conn - |> assign(:user, subscriber) - |> post("/api/v1/notifications/subscription/dismiss", %{"id" => notification.id}) - - assert %{} = json_response(conn, 200) - - assert Repo.all(SubscriptionNotification) == [] - end - - test "clearing all notifications also deletes them", %{ - conn: conn, - user: user, - subscriber: subscriber - } do - status_text1 = "Hello" - status_text2 = "Hello again" - {:ok, _activity1} = CommonAPI.post(user, %{"status" => status_text1}) - {:ok, _activity2} = CommonAPI.post(user, %{"status" => status_text2}) - - conn = - conn - |> assign(:user, subscriber) - |> post("/api/v1/notifications/subscription/clear") - - assert %{} = json_response(conn, 200) - - conn = - build_conn() - |> assign(:user, subscriber) - |> get("/api/v1/notifications/subscription") - - assert json_response(conn, 200) == [] - - assert Repo.all(SubscriptionNotification) == [] - end - - test "paginates notifications using min_id, since_id, max_id, and limit", %{ - conn: conn, - user: user, - subscriber: subscriber - } do - {:ok, activity1} = CommonAPI.post(user, %{"status" => "Hello 1"}) - {:ok, activity2} = CommonAPI.post(user, %{"status" => "Hello 2"}) - {:ok, activity3} = CommonAPI.post(user, %{"status" => "Hello 3"}) - {:ok, activity4} = CommonAPI.post(user, %{"status" => "Hello 4"}) - - notification1_id = - Repo.get_by(SubscriptionNotification, activity_id: activity1.id).id |> to_string() - - notification2_id = - Repo.get_by(SubscriptionNotification, activity_id: activity2.id).id |> to_string() - - notification3_id = - Repo.get_by(SubscriptionNotification, activity_id: activity3.id).id |> to_string() - - notification4_id = - Repo.get_by(SubscriptionNotification, activity_id: activity4.id).id |> to_string() - - conn = assign(conn, :user, subscriber) - - # min_id - conn_res = - get(conn, "/api/v1/notifications/subscription?limit=2&min_id=#{notification1_id}") - - result = json_response(conn_res, 200) - assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result - - # since_id - conn_res = - get(conn, "/api/v1/notifications/subscription?limit=2&since_id=#{notification1_id}") - - result = json_response(conn_res, 200) - assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result - - # max_id - conn_res = - get(conn, "/api/v1/notifications/subscription?limit=2&max_id=#{notification4_id}") - - result = json_response(conn_res, 200) - assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result - end - - test "destroy multiple", %{conn: conn, user: user1, subscriber: user2} do - # mutual subscription - User.subscribe(user1, user2) - - {:ok, activity1} = CommonAPI.post(user1, %{"status" => "Hello 1"}) - {:ok, activity2} = CommonAPI.post(user1, %{"status" => "World 1"}) - {:ok, activity3} = CommonAPI.post(user2, %{"status" => "Hello 2"}) - {:ok, activity4} = CommonAPI.post(user2, %{"status" => "World 2"}) - - notification1_id = - Repo.get_by(SubscriptionNotification, activity_id: activity1.id).id |> to_string() - - notification2_id = - Repo.get_by(SubscriptionNotification, activity_id: activity2.id).id |> to_string() - - notification3_id = - Repo.get_by(SubscriptionNotification, activity_id: activity3.id).id |> to_string() - - notification4_id = - Repo.get_by(SubscriptionNotification, activity_id: activity4.id).id |> to_string() - - conn = assign(conn, :user, user1) - - conn_res = get(conn, "/api/v1/notifications/subscription") - - result = json_response(conn_res, 200) - - Enum.each(result, fn %{"id" => id} -> - assert id in [notification3_id, notification4_id] - end) - - conn2 = assign(conn, :user, user2) - - conn_res = get(conn2, "/api/v1/notifications/subscription") - - result = json_response(conn_res, 200) - - Enum.each(result, fn %{"id" => id} -> - assert id in [notification1_id, notification2_id] - end) - - conn_destroy = - delete(conn, "/api/v1/notifications/subscription/destroy_multiple", %{ - "ids" => [notification3_id, notification4_id] - }) - - assert json_response(conn_destroy, 200) == %{} - - conn_res = get(conn2, "/api/v1/notifications/subscription") - - result = json_response(conn_res, 200) - - Enum.each(result, fn %{"id" => id} -> - assert id in [notification1_id, notification2_id] - end) - - assert length(Repo.all(SubscriptionNotification)) == 2 - end - end - describe "reblogging" do test "reblogs and returns the reblogged status", %{conn: conn} do activity = insert(:note_activity) diff --git a/test/web/pleroma_api/subscription_notification_controller_test.exs b/test/web/pleroma_api/subscription_notification_controller_test.exs new file mode 100644 index 000000000..ee495f112 --- /dev/null +++ b/test/web/pleroma_api/subscription_notification_controller_test.exs @@ -0,0 +1,234 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationControllerTest do + use Pleroma.Web.ConnCase + + alias Pleroma.Repo + alias Pleroma.SubscriptionNotification + alias Pleroma.User + alias Pleroma.Web.CommonAPI + import Pleroma.Factory + import Tesla.Mock + + setup do + mock(fn env -> apply(HttpRequestMock, :request, [env]) end) + :ok + end + + clear_config([:instance, :public]) + clear_config([:rich_media, :enabled]) + + describe "subscription_notifications" do + setup do + user = insert(:user) + subscriber = insert(:user) + + User.subscribe(subscriber, user) + + {:ok, %{user: user, subscriber: subscriber}} + end + + test "list of notifications", %{conn: conn, user: user, subscriber: subscriber} do + status_text = "Hello" + {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text}) + path = subscription_notification_path(conn, :list) + + conn = + conn + |> assign(:user, subscriber) + |> get(path) + + assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200) + assert response == status_text + end + + test "getting a single notification", %{conn: conn, user: user, subscriber: subscriber} do + status_text = "Hello" + + {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text}) + [notification] = Repo.all(SubscriptionNotification) + + path = subscription_notification_path(conn, :get, id: notification.id) + + conn = + conn + |> assign(:user, subscriber) + |> get(path) + + assert %{"status" => %{"content" => response}} = json_response(conn, 200) + assert response == status_text + end + + test "dismissing a single notification also deletes it", %{ + conn: conn, + user: user, + subscriber: subscriber + } do + status_text = "Hello" + {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text}) + + [notification] = Repo.all(SubscriptionNotification) + + conn = + conn + |> assign(:user, subscriber) + |> post(subscription_notification_path(conn, :dismiss), %{"id" => notification.id}) + + assert %{} = json_response(conn, 200) + + assert Repo.all(SubscriptionNotification) == [] + end + + test "clearing all notifications also deletes them", %{ + conn: conn, + user: user, + subscriber: subscriber + } do + status_text1 = "Hello" + status_text2 = "Hello again" + {:ok, _activity1} = CommonAPI.post(user, %{"status" => status_text1}) + {:ok, _activity2} = CommonAPI.post(user, %{"status" => status_text2}) + + conn = + conn + |> assign(:user, subscriber) + |> post(subscription_notification_path(conn, :clear)) + + assert %{} = json_response(conn, 200) + + conn = + build_conn() + |> assign(:user, subscriber) + |> get(subscription_notification_path(conn, :list)) + + assert json_response(conn, 200) == [] + + assert Repo.all(SubscriptionNotification) == [] + end + + test "paginates notifications using min_id, since_id, max_id, and limit", %{ + conn: conn, + user: user, + subscriber: subscriber + } do + {:ok, activity1} = CommonAPI.post(user, %{"status" => "Hello 1"}) + {:ok, activity2} = CommonAPI.post(user, %{"status" => "Hello 2"}) + {:ok, activity3} = CommonAPI.post(user, %{"status" => "Hello 3"}) + {:ok, activity4} = CommonAPI.post(user, %{"status" => "Hello 4"}) + + notification1_id = + Repo.get_by(SubscriptionNotification, activity_id: activity1.id).id |> to_string() + + notification2_id = + Repo.get_by(SubscriptionNotification, activity_id: activity2.id).id |> to_string() + + notification3_id = + Repo.get_by(SubscriptionNotification, activity_id: activity3.id).id |> to_string() + + notification4_id = + Repo.get_by(SubscriptionNotification, activity_id: activity4.id).id |> to_string() + + conn = assign(conn, :user, subscriber) + + # min_id + conn_res = + get( + conn, + subscription_notification_path(conn, :list, %{ + "limit" => 2, + "min_id" => notification1_id + }) + ) + + result = json_response(conn_res, 200) + assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result + + # since_id + conn_res = + get( + conn, + subscription_notification_path(conn, :list, %{ + "limit" => 2, + "since_id" => notification1_id + }) + ) + + result = json_response(conn_res, 200) + assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result + + # max_id + conn_res = + get( + conn, + subscription_notification_path(conn, :list, %{ + "limit" => 2, + "max_id" => notification4_id + }) + ) + + result = json_response(conn_res, 200) + assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result + end + + test "destroy multiple", %{conn: conn, user: user1, subscriber: user2} do + # mutual subscription + User.subscribe(user1, user2) + + {:ok, activity1} = CommonAPI.post(user1, %{"status" => "Hello 1"}) + {:ok, activity2} = CommonAPI.post(user1, %{"status" => "World 1"}) + {:ok, activity3} = CommonAPI.post(user2, %{"status" => "Hello 2"}) + {:ok, activity4} = CommonAPI.post(user2, %{"status" => "World 2"}) + + notification1_id = + Repo.get_by(SubscriptionNotification, activity_id: activity1.id).id |> to_string() + + notification2_id = + Repo.get_by(SubscriptionNotification, activity_id: activity2.id).id |> to_string() + + notification3_id = + Repo.get_by(SubscriptionNotification, activity_id: activity3.id).id |> to_string() + + notification4_id = + Repo.get_by(SubscriptionNotification, activity_id: activity4.id).id |> to_string() + + conn = assign(conn, :user, user1) + + conn_res = get(conn, subscription_notification_path(conn, :list)) + + result = json_response(conn_res, 200) + + Enum.each(result, fn %{"id" => id} -> + assert id in [notification3_id, notification4_id] + end) + + conn2 = assign(conn, :user, user2) + + conn_res = get(conn2, subscription_notification_path(conn, :list)) + + result = json_response(conn_res, 200) + + Enum.each(result, fn %{"id" => id} -> + assert id in [notification1_id, notification2_id] + end) + + conn_destroy = + delete(conn, subscription_notification_path(conn, :destroy_multiple), %{ + "ids" => [notification3_id, notification4_id] + }) + + assert json_response(conn_destroy, 200) == %{} + + conn_res = get(conn2, subscription_notification_path(conn, :list)) + + result = json_response(conn_res, 200) + + Enum.each(result, fn %{"id" => id} -> + assert id in [notification1_id, notification2_id] + end) + + assert length(Repo.all(SubscriptionNotification)) == 2 + end + end +end From 2688b876abf5ebd48d18e460eee7db992f984f5a Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 17 Sep 2019 13:42:28 +0000 Subject: [PATCH 04/13] Apply suggestion to lib/pleroma/web/pleroma_api/subscription_notification_controller.ex --- .../web/pleroma_api/subscription_notification_controller.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex b/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex index bfc2631dd..d5da92946 100644 --- a/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex +++ b/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex @@ -9,7 +9,6 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationController do alias Pleroma.SubscriptionNotification alias Pleroma.Web.PleromaAPI.PleromaAPI - alias Pleroma.Web.PleromaAPI.SubscriptionNotificationView def list(%{assigns: %{user: user}} = conn, params) do notifications = PleromaAPI.get_subscription_notifications(user, params) From c0f776faecfa91ed755760975da12b546ca89317 Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 17 Sep 2019 13:42:36 +0000 Subject: [PATCH 05/13] Apply suggestion to lib/pleroma/web/pleroma_api/subscription_notification_controller.ex --- .../web/pleroma_api/subscription_notification_controller.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex b/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex index d5da92946..fff307b4e 100644 --- a/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex +++ b/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex @@ -15,7 +15,6 @@ def list(%{assigns: %{user: user}} = conn, params) do conn |> add_link_headers(notifications) - |> put_view(SubscriptionNotificationView) |> render("index.json", %{notifications: notifications, for: user}) end From f9be517c7f3e63cfaaca871a4458cbf7c8a6a3f4 Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 17 Sep 2019 13:42:40 +0000 Subject: [PATCH 06/13] Apply suggestion to lib/pleroma/web/pleroma_api/subscription_notification_controller.ex --- .../web/pleroma_api/subscription_notification_controller.ex | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex b/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex index fff307b4e..969ce0179 100644 --- a/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex +++ b/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex @@ -20,9 +20,7 @@ def list(%{assigns: %{user: user}} = conn, params) do def get(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do with {:ok, notification} <- SubscriptionNotification.get(user, id) do - conn - |> put_view(SubscriptionNotificationView) - |> render("show.json", %{subscription_notification: notification, for: user}) + render(conn, "show.json", %{subscription_notification: notification, for: user}) else {:error, reason} -> conn From a81f80233d63d98a0de7b57def76275182d5477e Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 17 Sep 2019 13:43:10 +0000 Subject: [PATCH 07/13] Apply suggestion to lib/pleroma/web/router.ex --- lib/pleroma/web/router.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 05891b6c0..1fff94b38 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -274,7 +274,7 @@ defmodule Pleroma.Web.Router do post("/dismiss", SubscriptionNotificationController, :dismiss) delete("/destroy_multiple", SubscriptionNotificationController, :destroy_multiple) get("/", SubscriptionNotificationController, :list) - get("/id", SubscriptionNotificationController, :get) + get("/:id", SubscriptionNotificationController, :get) end end From 015597c2abbd9a78df76903bb2c3d229bf11e958 Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 17 Sep 2019 13:43:15 +0000 Subject: [PATCH 08/13] Apply suggestion to test/web/pleroma_api/subscription_notification_controller_test.exs --- .../pleroma_api/subscription_notification_controller_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/web/pleroma_api/subscription_notification_controller_test.exs b/test/web/pleroma_api/subscription_notification_controller_test.exs index ee495f112..781d27ead 100644 --- a/test/web/pleroma_api/subscription_notification_controller_test.exs +++ b/test/web/pleroma_api/subscription_notification_controller_test.exs @@ -50,7 +50,7 @@ test "getting a single notification", %{conn: conn, user: user, subscriber: subs {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text}) [notification] = Repo.all(SubscriptionNotification) - path = subscription_notification_path(conn, :get, id: notification.id) + path = subscription_notification_path(conn, :get, notification) conn = conn From a76168e743c3dd193db6ebca029f287da9edd290 Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 17 Sep 2019 16:44:41 +0300 Subject: [PATCH 09/13] Cleanup PleromaAPIController --- .../web/pleroma_api/pleroma_api_controller.ex | 26 ------------------- 1 file changed, 26 deletions(-) diff --git a/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex b/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex index 246b351dc..d17ccf84d 100644 --- a/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex +++ b/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex @@ -9,7 +9,6 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do alias Pleroma.Conversation.Participation alias Pleroma.Notification - alias Pleroma.SubscriptionNotification alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.MastodonAPI.ConversationView alias Pleroma.Web.MastodonAPI.NotificationView @@ -87,29 +86,4 @@ def read_notification(%{assigns: %{user: user}} = conn, %{"max_id" => max_id}) d |> render("index.json", %{notifications: notifications, for: user}) end end - - def delete_subscription_notification(%{assigns: %{user: user}} = conn, %{ - "id" => notification_id - }) do - with {:ok, notification} <- SubscriptionNotification.dismiss(user, notification_id) do - conn - |> put_view(NotificationView) - |> render("show.json", %{notification: notification, for: user}) - else - {:error, message} -> - conn - |> put_status(:bad_request) - |> json(%{"error" => message}) - end - end - - def read_subscription_notification(%{assigns: %{user: user}} = conn, %{"max_id" => max_id}) do - with notifications <- SubscriptionNotification.clear_up_to(user, max_id) do - notifications = Enum.take(notifications, 80) - - conn - |> put_view(NotificationView) - |> render("index.json", %{notifications: notifications, for: user}) - end - end end From 7d1773bc6b01caad8666ef07a9b2f2ac326fd0cd Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 17 Sep 2019 16:48:24 +0300 Subject: [PATCH 10/13] Rename SubscriptionNotificationController list and get actions to index and show --- .../subscription_notification_controller.ex | 4 ++-- lib/pleroma/web/router.ex | 4 ++-- ...bscription_notification_controller_test.exs | 18 +++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex b/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex index 969ce0179..fa8307668 100644 --- a/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex +++ b/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex @@ -10,7 +10,7 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationController do alias Pleroma.SubscriptionNotification alias Pleroma.Web.PleromaAPI.PleromaAPI - def list(%{assigns: %{user: user}} = conn, params) do + def index(%{assigns: %{user: user}} = conn, params) do notifications = PleromaAPI.get_subscription_notifications(user, params) conn @@ -18,7 +18,7 @@ def list(%{assigns: %{user: user}} = conn, params) do |> render("index.json", %{notifications: notifications, for: user}) end - def get(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do + def show(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do with {:ok, notification} <- SubscriptionNotification.get(user, id) do render(conn, "show.json", %{subscription_notification: notification, for: user}) else diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 1fff94b38..502c67e74 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -273,8 +273,8 @@ defmodule Pleroma.Web.Router do post("/clear", SubscriptionNotificationController, :clear) post("/dismiss", SubscriptionNotificationController, :dismiss) delete("/destroy_multiple", SubscriptionNotificationController, :destroy_multiple) - get("/", SubscriptionNotificationController, :list) - get("/:id", SubscriptionNotificationController, :get) + get("/", SubscriptionNotificationController, :index) + get("/:id", SubscriptionNotificationController, :show) end end diff --git a/test/web/pleroma_api/subscription_notification_controller_test.exs b/test/web/pleroma_api/subscription_notification_controller_test.exs index 781d27ead..c6a71732d 100644 --- a/test/web/pleroma_api/subscription_notification_controller_test.exs +++ b/test/web/pleroma_api/subscription_notification_controller_test.exs @@ -33,7 +33,7 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationControllerTest do test "list of notifications", %{conn: conn, user: user, subscriber: subscriber} do status_text = "Hello" {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text}) - path = subscription_notification_path(conn, :list) + path = subscription_notification_path(conn, :index) conn = conn @@ -50,7 +50,7 @@ test "getting a single notification", %{conn: conn, user: user, subscriber: subs {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text}) [notification] = Repo.all(SubscriptionNotification) - path = subscription_notification_path(conn, :get, notification) + path = subscription_notification_path(conn, :show, notification) conn = conn @@ -101,7 +101,7 @@ test "clearing all notifications also deletes them", %{ conn = build_conn() |> assign(:user, subscriber) - |> get(subscription_notification_path(conn, :list)) + |> get(subscription_notification_path(conn, :index)) assert json_response(conn, 200) == [] @@ -136,7 +136,7 @@ test "paginates notifications using min_id, since_id, max_id, and limit", %{ conn_res = get( conn, - subscription_notification_path(conn, :list, %{ + subscription_notification_path(conn, :index, %{ "limit" => 2, "min_id" => notification1_id }) @@ -149,7 +149,7 @@ test "paginates notifications using min_id, since_id, max_id, and limit", %{ conn_res = get( conn, - subscription_notification_path(conn, :list, %{ + subscription_notification_path(conn, :index, %{ "limit" => 2, "since_id" => notification1_id }) @@ -162,7 +162,7 @@ test "paginates notifications using min_id, since_id, max_id, and limit", %{ conn_res = get( conn, - subscription_notification_path(conn, :list, %{ + subscription_notification_path(conn, :index, %{ "limit" => 2, "max_id" => notification4_id }) @@ -195,7 +195,7 @@ test "destroy multiple", %{conn: conn, user: user1, subscriber: user2} do conn = assign(conn, :user, user1) - conn_res = get(conn, subscription_notification_path(conn, :list)) + conn_res = get(conn, subscription_notification_path(conn, :index)) result = json_response(conn_res, 200) @@ -205,7 +205,7 @@ test "destroy multiple", %{conn: conn, user: user1, subscriber: user2} do conn2 = assign(conn, :user, user2) - conn_res = get(conn2, subscription_notification_path(conn, :list)) + conn_res = get(conn2, subscription_notification_path(conn, :index)) result = json_response(conn_res, 200) @@ -220,7 +220,7 @@ test "destroy multiple", %{conn: conn, user: user1, subscriber: user2} do assert json_response(conn_destroy, 200) == %{} - conn_res = get(conn2, subscription_notification_path(conn, :list)) + conn_res = get(conn2, subscription_notification_path(conn, :index)) result = json_response(conn_res, 200) From e9f69a3eb7f17ae8c2890972851de1139983ce3d Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 17 Sep 2019 16:52:23 +0300 Subject: [PATCH 11/13] Move pleroma_api controllers into controllers sub-folders --- .../web/pleroma_api/{ => controllers}/pleroma_api_controller.ex | 0 .../{ => controllers}/subscription_notification_controller.ex | 0 .../pleroma_api/{ => controllers}/pleroma_api_controller_test.exs | 0 .../subscription_notification_controller_test.exs | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename lib/pleroma/web/pleroma_api/{ => controllers}/pleroma_api_controller.ex (100%) rename lib/pleroma/web/pleroma_api/{ => controllers}/subscription_notification_controller.ex (100%) rename test/web/pleroma_api/{ => controllers}/pleroma_api_controller_test.exs (100%) rename test/web/pleroma_api/{ => controllers}/subscription_notification_controller_test.exs (100%) diff --git a/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex b/lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex similarity index 100% rename from lib/pleroma/web/pleroma_api/pleroma_api_controller.ex rename to lib/pleroma/web/pleroma_api/controllers/pleroma_api_controller.ex diff --git a/lib/pleroma/web/pleroma_api/subscription_notification_controller.ex b/lib/pleroma/web/pleroma_api/controllers/subscription_notification_controller.ex similarity index 100% rename from lib/pleroma/web/pleroma_api/subscription_notification_controller.ex rename to lib/pleroma/web/pleroma_api/controllers/subscription_notification_controller.ex diff --git a/test/web/pleroma_api/pleroma_api_controller_test.exs b/test/web/pleroma_api/controllers/pleroma_api_controller_test.exs similarity index 100% rename from test/web/pleroma_api/pleroma_api_controller_test.exs rename to test/web/pleroma_api/controllers/pleroma_api_controller_test.exs diff --git a/test/web/pleroma_api/subscription_notification_controller_test.exs b/test/web/pleroma_api/controllers/subscription_notification_controller_test.exs similarity index 100% rename from test/web/pleroma_api/subscription_notification_controller_test.exs rename to test/web/pleroma_api/controllers/subscription_notification_controller_test.exs From 9fa2586abd915342095574f05358642412db0f04 Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 17 Sep 2019 17:44:10 +0300 Subject: [PATCH 12/13] Refactor SubscriptionNotificationView --- .../subscription_notification_controller.ex | 20 +++++++++++++++++-- .../views/subscription_notification_view.ex | 9 +++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/web/pleroma_api/controllers/subscription_notification_controller.ex b/lib/pleroma/web/pleroma_api/controllers/subscription_notification_controller.ex index fa8307668..37c2222de 100644 --- a/lib/pleroma/web/pleroma_api/controllers/subscription_notification_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/subscription_notification_controller.ex @@ -7,11 +7,16 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationController do import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2] + alias Pleroma.Activity alias Pleroma.SubscriptionNotification + alias Pleroma.User alias Pleroma.Web.PleromaAPI.PleromaAPI def index(%{assigns: %{user: user}} = conn, params) do - notifications = PleromaAPI.get_subscription_notifications(user, params) + notifications = + user + |> PleromaAPI.get_subscription_notifications(params) + |> Enum.map(&build_notification_data/1) conn |> add_link_headers(notifications) @@ -20,7 +25,10 @@ def index(%{assigns: %{user: user}} = conn, params) do def show(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do with {:ok, notification} <- SubscriptionNotification.get(user, id) do - render(conn, "show.json", %{subscription_notification: notification, for: user}) + render(conn, "show.json", %{ + subscription_notification: build_notification_data(notification), + for: user + }) else {:error, reason} -> conn @@ -52,4 +60,12 @@ def destroy_multiple( SubscriptionNotification.destroy_multiple(user, ids) json(conn, %{}) end + + defp build_notification_data(%{activity: %{data: data}} = notification) do + %{ + notification: notification, + actor: User.get_cached_by_ap_id(data["actor"]), + parent_activity: Activity.get_create_by_object_ap_id(data["object"]) + } + end end diff --git a/lib/pleroma/web/pleroma_api/views/subscription_notification_view.ex b/lib/pleroma/web/pleroma_api/views/subscription_notification_view.ex index d7f7f4c5a..0eccbcbb9 100644 --- a/lib/pleroma/web/pleroma_api/views/subscription_notification_view.ex +++ b/lib/pleroma/web/pleroma_api/views/subscription_notification_view.ex @@ -6,7 +6,6 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationView do use Pleroma.Web, :view alias Pleroma.Activity - alias Pleroma.User alias Pleroma.Web.CommonAPI alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.MastodonAPI.StatusView @@ -17,11 +16,13 @@ def render("index.json", %{notifications: notifications, for: user}) do end def render("show.json", %{ - subscription_notification: %{activity: activity} = notification, + subscription_notification: %{ + notification: %{activity: activity} = notification, + actor: actor, + parent_activity: parent_activity + }, for: user }) do - actor = User.get_cached_by_ap_id(activity.data["actor"]) - parent_activity = Activity.get_create_by_object_ap_id(activity.data["object"]) mastodon_type = Activity.mastodon_notification_type(activity) response = %{ From b736312123bd11e8ba87b8b39245c0f441ebd7fb Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Thu, 26 Sep 2019 15:17:36 +0300 Subject: [PATCH 13/13] Update CHANGELOG --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a76e6cf8..291c961ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Refreshing poll results for remote polls - Admin API: Add ability to require password reset +- Pleroma API: `GET /api/v1/pleroma/subscription_notifications/` to get list of subscription notifications +- Pleroma API: `GET /api/v1/pleroma/subscription_notifications/:id` to get a subscription notification +- Pleroma API: `POST /api/v1/pleroma/subscription_notifications/clear` to clear all subscription notifications +- Pleroma API: `POST /api/v1/pleroma/subscription_notifications/dismiss` to clear a subscription notification +- Pleroma API: `DELETE /api/v1/pleroma/subscription_notifications/destroy_multiple` to clear multiple subscription notifications ### Changed - **Breaking:** Elixir >=1.8 is now required (was >= 1.7) @@ -15,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Admin API: Return `total` when querying for reports - Mastodon API: Return `pleroma.direct_conversation_id` when creating a direct message (`POST /api/v1/statuses`) - Admin API: Return link alongside with token on password reset +- Mastodon API: notifications no longer include subscription notifications - they are now served from new endpoints in Pleroma API ### Fixed - Mastodon API: Fix private and direct statuses not being filtered out from the public timeline for an authenticated user (`GET /api/v1/timelines/public`)