From 007762e767fcd734bacd9700f5268591854a446a Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 14:20:13 +0100 Subject: [PATCH] Add notification checks --- lib/pleroma/notification.ex | 1 + lib/pleroma/user.ex | 7 +++++-- lib/pleroma/user/info.ex | 10 +++++----- lib/pleroma/web/common_api/utils.ex | 16 ++++++++++++++++ lib/pleroma/web/twitter_api/twitter_api.ex | 1 + 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index cac10f24a..7c5856438 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -148,6 +148,7 @@ 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/user.ex b/lib/pleroma/user.ex index b7cab8642..70db520ca 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -935,10 +935,10 @@ def subscribe(subscriber, %{ap_id: ap_id}) do def unsubscribe(unsubscriber, %{ap_id: ap_id}) do info_cng = - subscriber.info + unsubscriber.info |> User.Info.remove_from_subscriptions(ap_id) - change(subscriber) + change(unsubscriber) |> put_embed(:info, info_cng) |> update_and_set_cache() end @@ -1005,6 +1005,9 @@ def muted_users(user), def blocked_users(user), do: Repo.all(from(u in User, where: u.ap_id in ^user.info.blocks)) + def subscribed_users(user), + do: Repo.all(from(u in User, where: u.ap_id in ^user.info.subscriptions)) + def block_domain(user, domain) do info_cng = user.info diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index 30c594f64..1746da576 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -22,7 +22,7 @@ defmodule Pleroma.User.Info do field(:domain_blocks, {:array, :string}, default: []) field(:mutes, {:array, :string}, default: []) field(:muted_reblogs, {:array, :string}, default: []) - field(:subscribed_to, {:array, :string}, default: []) + field(:subscriptions, {:array, :string}, default: []) field(:deactivated, :boolean, default: false) field(:no_rich_text, :boolean, default: false) field(:ap_enabled, :boolean, default: false) @@ -98,8 +98,8 @@ def set_subscriptions(info, subscriptions) do params = %{subscriptions: subscriptions} info - |> cast(params, [:subscribed_to]) - |> validate_required([:subscribed_to]) + |> cast(params, [:subscriptions]) + |> validate_required([:subscriptions]) end def add_to_mutes(info, muted) do @@ -119,11 +119,11 @@ def remove_from_block(info, blocked) do end def add_to_subscriptions(info, subscribed) do - set_subscriptions(info, Enum.uniq([subscribed | info.subscribed_to])) + set_subscriptions(info, Enum.uniq([subscribed | info.subscriptions])) end def remove_from_subscriptions(info, subscribed) do - set_subscriptions(info, List.delete(info.subscribed_to, subscribed)) + set_subscriptions(info, List.delete(info.subscriptions, subscribed)) end def set_domain_blocks(info, domain_blocks) do diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 9cd8b3758..effc49a01 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -335,6 +335,22 @@ def maybe_notify_mentioned_recipients( def maybe_notify_mentioned_recipients(recipients, _), do: recipients + def maybe_notify_subscribers( + recipients, + %Activity{data: %{"actor" => actor, "type" => type}} + ) when type == "Create" do + with %User{} = user <- User.get_by_ap_id(actor) do + subscriber_ids = + user + |> User.subscribed_users() + |> Enum.map(& &1.ap_id) + + recipients ++ subscriber_ids + end + end + + def maybe_notify_subscribers(recipients, _), do: recipients + def maybe_extract_mentions(%{"tag" => tag}) do tag |> Enum.filter(fn x -> is_map(x) end) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index c0a0a500f..7720367ad 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -62,6 +62,7 @@ def unblock(%User{} = blocker, params) do def subscribe(%User{} = subscriber, params) do with {:ok, %User{} = subscribed} <- get_user(params) do User.subscribe(subscriber, subscribed) + |> IO.inspect end end