Refactor notification settings

This commit is contained in:
Mark Felder 2020-05-28 10:16:09 -05:00
parent 089d72d2e6
commit 9eea800026
8 changed files with 41 additions and 66 deletions

View File

@ -9,6 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
<details> <details>
<summary>API Changes</summary> <summary>API Changes</summary>
- **Breaking:** Emoji API: changed methods and renamed routes. - **Breaking:** Emoji API: changed methods and renamed routes.
- **Breaking:** Notification Settings API for suppressing notification
now supports the following controls: `from_followers`, `from_following`,
and `from_strangers`.
</details> </details>
### Removed ### Removed

View File

@ -287,10 +287,9 @@ See [Admin-API](admin_api.md)
* Method `PUT` * Method `PUT`
* Authentication: required * Authentication: required
* Params: * Params:
* `followers`: BOOLEAN field, receives notifications from followers * `from_followers`: BOOLEAN field, receives notifications from followers
* `follows`: BOOLEAN field, receives notifications from people the user follows * `from_following`: BOOLEAN field, receives notifications from people the user follows
* `remote`: BOOLEAN field, receives notifications from people on remote instances * `from_strangers`: BOOLEAN field, receives notifications from people without an established relationship
* `local`: BOOLEAN field, receives notifications from people on the local instance
* `privacy_option`: BOOLEAN field. When set to true, it removes the contents of a message from the push notification. * `privacy_option`: BOOLEAN field. When set to true, it removes the contents of a message from the push notification.
* Response: JSON. Returns `{"status": "success"}` if the update was successful, otherwise returns `{"error": "error_msg"}` * Response: JSON. Returns `{"status": "success"}` if the update was successful, otherwise returns `{"error": "error_msg"}`

View File

@ -459,10 +459,9 @@ defmodule Pleroma.Notification do
def skip?(%Activity{} = activity, %User{} = user) do def skip?(%Activity{} = activity, %User{} = user) do
[ [
:self, :self,
:followers, :from_followers,
:follows, :from_following,
:non_followers, :from_strangers,
:non_follows,
:recently_followed :recently_followed
] ]
|> Enum.find(&skip?(&1, activity, user)) |> Enum.find(&skip?(&1, activity, user))
@ -476,9 +475,9 @@ defmodule Pleroma.Notification do
end end
def skip?( def skip?(
:followers, :from_followers,
%Activity{} = activity, %Activity{} = activity,
%User{notification_settings: %{followers: false}} = user %User{notification_settings: %{from_followers: false}} = user
) do ) do
actor = activity.data["actor"] actor = activity.data["actor"]
follower = User.get_cached_by_ap_id(actor) follower = User.get_cached_by_ap_id(actor)
@ -486,9 +485,9 @@ defmodule Pleroma.Notification do
end end
def skip?( def skip?(
:non_followers, :from_strangers,
%Activity{} = activity, %Activity{} = activity,
%User{notification_settings: %{non_followers: false}} = user %User{notification_settings: %{from_strangers: false}} = user
) do ) do
actor = activity.data["actor"] actor = activity.data["actor"]
follower = User.get_cached_by_ap_id(actor) follower = User.get_cached_by_ap_id(actor)
@ -496,25 +495,15 @@ defmodule Pleroma.Notification do
end end
def skip?( def skip?(
:follows, :from_following,
%Activity{} = activity, %Activity{} = activity,
%User{notification_settings: %{follows: false}} = user %User{notification_settings: %{from_following: false}} = user
) do ) do
actor = activity.data["actor"] actor = activity.data["actor"]
followed = User.get_cached_by_ap_id(actor) followed = User.get_cached_by_ap_id(actor)
User.following?(user, followed) User.following?(user, followed)
end end
def skip?(
:non_follows,
%Activity{} = activity,
%User{notification_settings: %{non_follows: false}} = user
) do
actor = activity.data["actor"]
followed = User.get_cached_by_ap_id(actor)
!User.following?(user, followed)
end
# To do: consider defining recency in hours and checking FollowingRelationship with a single SQL # To do: consider defining recency in hours and checking FollowingRelationship with a single SQL
def skip?(:recently_followed, %Activity{data: %{"type" => "Follow"}} = activity, %User{} = user) do def skip?(:recently_followed, %Activity{data: %{"type" => "Follow"}} = activity, %User{} = user) do
actor = activity.data["actor"] actor = activity.data["actor"]

View File

@ -10,20 +10,18 @@ defmodule Pleroma.User.NotificationSetting do
@primary_key false @primary_key false
embedded_schema do embedded_schema do
field(:followers, :boolean, default: true) field(:from_followers, :boolean, default: true)
field(:follows, :boolean, default: true) field(:from_following, :boolean, default: true)
field(:non_follows, :boolean, default: true) field(:from_strangers, :boolean, default: true)
field(:non_followers, :boolean, default: true)
field(:privacy_option, :boolean, default: false) field(:privacy_option, :boolean, default: false)
end end
def changeset(schema, params) do def changeset(schema, params) do
schema schema
|> cast(prepare_attrs(params), [ |> cast(prepare_attrs(params), [
:followers, :from_followers,
:follows, :from_following,
:non_follows, :from_strangers,
:non_followers,
:privacy_option :privacy_option
]) ])
end end

View File

@ -57,10 +57,9 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do
notification_settings: %Schema{ notification_settings: %Schema{
type: :object, type: :object,
properties: %{ properties: %{
followers: %Schema{type: :boolean}, from_followers: %Schema{type: :boolean},
follows: %Schema{type: :boolean}, from_following: %Schema{type: :boolean},
non_followers: %Schema{type: :boolean}, from_strangers: %Schema{type: :boolean},
non_follows: %Schema{type: :boolean},
privacy_option: %Schema{type: :boolean} privacy_option: %Schema{type: :boolean}
} }
}, },
@ -123,10 +122,9 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do
"unread_conversation_count" => 0, "unread_conversation_count" => 0,
"tags" => [], "tags" => [],
"notification_settings" => %{ "notification_settings" => %{
"followers" => true, "from_followers" => true,
"follows" => true, "from_following" => true,
"non_followers" => true, "from_strangers" => true,
"non_follows" => true,
"privacy_option" => false "privacy_option" => false
}, },
"relationship" => %{ "relationship" => %{

View File

@ -237,19 +237,19 @@ defmodule Pleroma.NotificationTest do
follower = insert(:user) follower = insert(:user)
followed = followed =
insert(:user, notification_settings: %Pleroma.User.NotificationSetting{followers: false}) insert(:user, notification_settings: %Pleroma.User.NotificationSetting{from_followers: false})
User.follow(follower, followed) User.follow(follower, followed)
{:ok, activity} = CommonAPI.post(follower, %{status: "hey @#{followed.nickname}"}) {:ok, activity} = CommonAPI.post(follower, %{status: "hey @#{followed.nickname}"})
refute Notification.create_notification(activity, followed) refute Notification.create_notification(activity, followed)
end end
test "it disables notifications from non-followers" do test "it disables notifications from strangers" do
follower = insert(:user) follower = insert(:user)
followed = followed =
insert(:user, insert(:user,
notification_settings: %Pleroma.User.NotificationSetting{non_followers: false} notification_settings: %Pleroma.User.NotificationSetting{from_strangers: false}
) )
{:ok, activity} = CommonAPI.post(follower, %{status: "hey @#{followed.nickname}"}) {:ok, activity} = CommonAPI.post(follower, %{status: "hey @#{followed.nickname}"})
@ -258,7 +258,7 @@ defmodule Pleroma.NotificationTest do
test "it disables notifications from people the user follows" do test "it disables notifications from people the user follows" do
follower = follower =
insert(:user, notification_settings: %Pleroma.User.NotificationSetting{follows: false}) insert(:user, notification_settings: %Pleroma.User.NotificationSetting{from_following: false})
followed = insert(:user) followed = insert(:user)
User.follow(follower, followed) User.follow(follower, followed)
@ -267,15 +267,6 @@ defmodule Pleroma.NotificationTest do
refute Notification.create_notification(activity, follower) refute Notification.create_notification(activity, follower)
end end
test "it disables notifications from people the user does not follow" do
follower =
insert(:user, notification_settings: %Pleroma.User.NotificationSetting{non_follows: false})
followed = insert(:user)
{:ok, activity} = CommonAPI.post(followed, %{status: "hey @#{follower.nickname}"})
refute Notification.create_notification(activity, follower)
end
test "it doesn't create a notification for user if he is the activity author" do test "it doesn't create a notification for user if he is the activity author" do
activity = insert(:note_activity) activity = insert(:note_activity)
author = User.get_cached_by_ap_id(activity.data["actor"]) author = User.get_cached_by_ap_id(activity.data["actor"])

View File

@ -94,10 +94,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
user = insert(:user) user = insert(:user)
notification_settings = %{ notification_settings = %{
followers: true, from_followers: true,
follows: true, from_following: true,
non_followers: true, from_strangers: true,
non_follows: true,
privacy_option: false privacy_option: false
} }

View File

@ -191,7 +191,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
test "it updates notification settings", %{user: user, conn: conn} do test "it updates notification settings", %{user: user, conn: conn} do
conn conn
|> put("/api/pleroma/notification_settings", %{ |> put("/api/pleroma/notification_settings", %{
"followers" => false, "from_followers" => false,
"bar" => 1 "bar" => 1
}) })
|> json_response(:ok) |> json_response(:ok)
@ -199,10 +199,9 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
user = refresh_record(user) user = refresh_record(user)
assert %Pleroma.User.NotificationSetting{ assert %Pleroma.User.NotificationSetting{
followers: false, from_followers: false,
follows: true, from_following: true,
non_follows: true, from_strangers: true,
non_followers: true,
privacy_option: false privacy_option: false
} == user.notification_settings } == user.notification_settings
end end
@ -215,10 +214,9 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
user = refresh_record(user) user = refresh_record(user)
assert %Pleroma.User.NotificationSetting{ assert %Pleroma.User.NotificationSetting{
followers: true, from_followers: true,
follows: true, from_following: true,
non_follows: true, from_strangers: true,
non_followers: true,
privacy_option: true privacy_option: true
} == user.notification_settings } == user.notification_settings
end end