Add notification checks
authorSadposter <hannah+pleroma@coffee-and-dreams.uk>
Fri, 5 Apr 2019 13:20:13 +0000 (14:20 +0100)
committerHannah Ward <Hannah.ward9001@gmail.com>
Fri, 5 Apr 2019 14:30:23 +0000 (15:30 +0100)
lib/pleroma/notification.ex
lib/pleroma/user.ex
lib/pleroma/user/info.ex
lib/pleroma/web/common_api/utils.ex
lib/pleroma/web/twitter_api/twitter_api.ex

index cac10f24af4ef984f46a8710227a32ff304284ef..7c58564384fe4cd21cc9057d6cba76534eadc1a6 100644 (file)
@@ -148,6 +148,7 @@ defmodule Pleroma.Notification do
       []
       |> 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)
index b7cab8642c53bc63cbbade9aebe93ab2b4c4d168..70db520ca9dafe583507e5e4c2d6c4b31e5f0452 100644 (file)
@@ -935,10 +935,10 @@ defmodule Pleroma.User 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 @@ defmodule Pleroma.User do
   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
index 30c594f64e68b5df7b45aeea9af24612f2874e0f..1746da576932081237fa638c0c4b6c8f477121ef 100644 (file)
@@ -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 @@ defmodule Pleroma.User.Info 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 @@ defmodule Pleroma.User.Info 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
index 9cd8b3758edd66c74abdfeee4d517e45caa6cdf6..effc49a01e5b3ded55f2be386f0e181e4ad972f1 100644 (file)
@@ -335,6 +335,22 @@ defmodule Pleroma.Web.CommonAPI.Utils do
 
   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)
index c0a0a500f8c4f79de04543e233ea23730e4f2bdf..7720367adb5097a2b7366d50cec01207ff331f53 100644 (file)
@@ -62,6 +62,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
   def subscribe(%User{} = subscriber, params) do
     with {:ok, %User{} = subscribed} <- get_user(params) do
       User.subscribe(subscriber, subscribed)
+      |> IO.inspect
     end
   end