aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorkaniini <nenolod@gmail.com>2019-04-10 02:10:31 +0000
committerkaniini <nenolod@gmail.com>2019-04-10 02:10:31 +0000
commit97395e013e5dac84399769438f8b90cffa38afd5 (patch)
tree1429546e8d412f5c52b5be77115c87f1bbb831a5 /lib
parent144648de92abea7330cf264b7608634a27bb6bdf (diff)
parentcd90695a349f33b84f287794bae6070e9eec446a (diff)
downloadpleroma-97395e013e5dac84399769438f8b90cffa38afd5.tar.gz
Merge branch 'notification-pleroma-settings' into 'develop'
Notification controls Closes #738 See merge request pleroma/pleroma!988
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/notification.ex63
-rw-r--r--lib/pleroma/user.ex8
-rw-r--r--lib/pleroma/user/info.ex17
-rw-r--r--lib/pleroma/web/mastodon_api/views/account_view.ex22
-rw-r--r--lib/pleroma/web/router.ex1
-rw-r--r--lib/pleroma/web/twitter_api/controllers/util_controller.ex6
6 files changed, 103 insertions, 14 deletions
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index cac10f24a..14de1a097 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -122,13 +122,7 @@ defmodule Pleroma.Notification do
# TODO move to sql, too.
def create_notification(%Activity{} = activity, %User{} = user) do
- unless User.blocks?(user, %{ap_id: activity.data["actor"]}) or
- CommonAPI.thread_muted?(user, activity) or user.ap_id == activity.data["actor"] or
- (activity.data["type"] == "Follow" and
- Enum.any?(Notification.for_user(user), fn notif ->
- notif.activity.data["type"] == "Follow" and
- notif.activity.data["actor"] == activity.data["actor"]
- end)) do
+ unless skip?(activity, user) do
notification = %Notification{user_id: user.id, activity: activity}
{:ok, notification} = Repo.insert(notification)
Pleroma.Web.Streamer.stream("user", notification)
@@ -154,4 +148,59 @@ defmodule Pleroma.Notification do
end
def get_notified_from_activity(_, _local_only), do: []
+
+ def skip?(activity, user) do
+ [:self, :blocked, :local, :muted, :followers, :follows, :recently_followed]
+ |> Enum.any?(&skip?(&1, activity, user))
+ end
+
+ def skip?(:self, activity, user) do
+ activity.data["actor"] == user.ap_id
+ end
+
+ def skip?(:blocked, activity, user) do
+ actor = activity.data["actor"]
+ User.blocks?(user, %{ap_id: actor})
+ end
+
+ def skip?(:local, %{local: true}, %{info: %{notification_settings: %{"local" => false}}}),
+ do: true
+
+ def skip?(:local, %{local: false}, %{info: %{notification_settings: %{"remote" => false}}}),
+ do: true
+
+ def skip?(:muted, activity, user) do
+ actor = activity.data["actor"]
+
+ User.mutes?(user, %{ap_id: actor}) or
+ CommonAPI.thread_muted?(user, activity)
+ 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?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
+ actor = activity.data["actor"]
+ followed = User.get_by_ap_id(actor)
+ User.following?(user, followed)
+ end
+
+ def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
+ actor = activity.data["actor"]
+
+ Notification.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/user.ex b/lib/pleroma/user.ex
index 4f579b597..ed23b8ef0 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -1092,6 +1092,14 @@ defmodule Pleroma.User do
update_and_set_cache(cng)
end
+ def update_notification_settings(%User{} = user, settings \\ %{}) do
+ info_changeset = User.Info.update_notification_settings(user.info, settings)
+
+ change(user)
+ |> put_embed(:info, info_changeset)
+ |> update_and_set_cache()
+ end
+
def delete(%User{} = user) do
{:ok, user} = User.deactivate(user)
diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex
index 740a46727..33fd77b02 100644
--- a/lib/pleroma/user/info.ex
+++ b/lib/pleroma/user/info.ex
@@ -40,6 +40,10 @@ defmodule Pleroma.User.Info do
field(:pinned_activities, {:array, :string}, default: [])
field(:flavour, :string, default: nil)
+ field(:notification_settings, :map,
+ default: %{"remote" => true, "local" => true, "followers" => true, "follows" => true}
+ )
+
# Found in the wild
# ap_id -> Where is this used?
# bio -> Where is this used?
@@ -57,6 +61,19 @@ defmodule Pleroma.User.Info do
|> validate_required([:deactivated])
end
+ def update_notification_settings(info, settings) do
+ notification_settings =
+ info.notification_settings
+ |> Map.merge(settings)
+ |> Map.take(["remote", "local", "followers", "follows"])
+
+ params = %{notification_settings: notification_settings}
+
+ info
+ |> cast(params, [:notification_settings])
+ |> validate_required([:notification_settings])
+ end
+
def add_to_note_count(info, number) do
set_note_count(info, info.note_count + number)
end
diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex
index b5f3bbb9d..25899e491 100644
--- a/lib/pleroma/web/mastodon_api/views/account_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/account_view.ex
@@ -117,13 +117,15 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
},
# Pleroma extension
- pleroma: %{
- confirmation_pending: user_info.confirmation_pending,
- tags: user.tags,
- is_moderator: user.info.is_moderator,
- is_admin: user.info.is_admin,
- relationship: relationship
- }
+ pleroma:
+ %{
+ confirmation_pending: user_info.confirmation_pending,
+ tags: user.tags,
+ is_moderator: user.info.is_moderator,
+ is_admin: user.info.is_admin,
+ relationship: relationship
+ }
+ |> with_notification_settings(user, opts[:for])
}
end
@@ -132,4 +134,10 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
end
defp username_from_nickname(_), do: nil
+
+ defp with_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
+ Map.put(data, :notification_settings, user.info.notification_settings)
+ end
+
+ defp with_notification_settings(data, _, _), do: data
end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index ef38fc34d..66db5e628 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -193,6 +193,7 @@ defmodule Pleroma.Web.Router do
post("/change_password", UtilController, :change_password)
post("/delete_account", UtilController, :delete_account)
+ put("/notification_settings", UtilController, :update_notificaton_settings)
end
scope [] do
diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex
index 26407aebd..bb71742ec 100644
--- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex
+++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex
@@ -292,6 +292,12 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
json(conn, emoji)
end
+ def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do
+ with {:ok, _} <- User.update_notification_settings(user, params) do
+ json(conn, %{status: "success"})
+ end
+ end
+
def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
follow_import(conn, %{"list" => File.read!(listfile.path)})
end