Merge branch 'feature/notification-control-part-2' into 'develop'

notification controls, part 2

See merge request pleroma/pleroma!1204
This commit is contained in:
kaniini 2019-06-02 08:25:37 +00:00
commit 5402d04e3c
8 changed files with 84 additions and 42 deletions

View file

@ -166,7 +166,16 @@ defmodule Pleroma.Notification do
def get_notified_from_activity(_, _local_only), do: [] def get_notified_from_activity(_, _local_only), do: []
def skip?(activity, user) do def skip?(activity, user) do
[:self, :blocked, :local, :muted, :followers, :follows, :recently_followed] [
:self,
:blocked,
:muted,
:followers,
:follows,
:non_followers,
:non_follows,
:recently_followed
]
|> Enum.any?(&skip?(&1, activity, user)) |> Enum.any?(&skip?(&1, activity, user))
end end
@ -179,12 +188,6 @@ defmodule Pleroma.Notification do
User.blocks?(user, %{ap_id: actor}) User.blocks?(user, %{ap_id: actor})
end 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 def skip?(:muted, activity, user) do
actor = activity.data["actor"] actor = activity.data["actor"]
@ -201,12 +204,32 @@ defmodule Pleroma.Notification do
User.following?(follower, user) User.following?(follower, user)
end 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 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) 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,
%{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 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
actor = activity.data["actor"] actor = activity.data["actor"]

View file

@ -46,7 +46,12 @@ defmodule Pleroma.User.Info do
field(:emoji, {:array, :map}, default: []) field(:emoji, {:array, :map}, default: [])
field(:notification_settings, :map, field(:notification_settings, :map,
default: %{"remote" => true, "local" => true, "followers" => true, "follows" => true} default: %{
"followers" => true,
"follows" => true,
"non_follows" => true,
"non_followers" => true
}
) )
# Found in the wild # Found in the wild
@ -67,10 +72,15 @@ defmodule Pleroma.User.Info do
end end
def update_notification_settings(info, settings) do def update_notification_settings(info, settings) do
settings =
settings
|> Enum.map(fn {k, v} -> {k, v in [true, "true", "True", "1"]} end)
|> Map.new()
notification_settings = notification_settings =
info.notification_settings info.notification_settings
|> Map.merge(settings) |> Map.merge(settings)
|> Map.take(["remote", "local", "followers", "follows"]) |> Map.take(["followers", "follows", "non_follows", "non_followers"])
params = %{notification_settings: notification_settings} params = %{notification_settings: notification_settings}

View file

@ -121,6 +121,7 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
"tags" => user.tags "tags" => user.tags
} }
|> maybe_with_activation_status(user, for_user) |> maybe_with_activation_status(user, for_user)
|> with_notification_settings(user, for_user)
} }
|> maybe_with_user_settings(user, for_user) |> maybe_with_user_settings(user, for_user)
|> maybe_with_role(user, for_user) |> maybe_with_role(user, for_user)
@ -132,6 +133,12 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
end end
end end
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
defp maybe_with_activation_status(data, user, %User{info: %{is_admin: true}}) do defp maybe_with_activation_status(data, user, %User{info: %{is_admin: true}}) do
Map.put(data, "deactivated", user.info.deactivated) Map.put(data, "deactivated", user.info.deactivated)
end end

View file

@ -0,0 +1,10 @@
defmodule Pleroma.Repo.Migrations.AddNonFollowsAndNonFollowersFieldsToNotificationSettings do
use Ecto.Migration
def change do
execute("""
update users set info = jsonb_set(info, '{notification_settings}', '{"local": true, "remote": true, "follows": true, "followers": true, "non_follows": true, "non_followers": true}')
where local=true
""")
end
end

View file

@ -78,33 +78,6 @@ defmodule Pleroma.NotificationTest do
assert nil == Notification.create_notification(activity, muter) assert nil == Notification.create_notification(activity, muter)
end end
test "it disables notifications from people on remote instances" do
user = insert(:user, info: %{notification_settings: %{"remote" => false}})
other_user = insert(:user)
create_activity = %{
"@context" => "https://www.w3.org/ns/activitystreams",
"type" => "Create",
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
"actor" => other_user.ap_id,
"object" => %{
"type" => "Note",
"content" => "Hi @#{user.nickname}",
"attributedTo" => other_user.ap_id
}
}
{:ok, %{local: false} = activity} = Transmogrifier.handle_incoming(create_activity)
assert nil == Notification.create_notification(activity, user)
end
test "it disables notifications from people on the local instance" do
user = insert(:user, info: %{notification_settings: %{"local" => false}})
other_user = insert(:user)
{:ok, activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
assert nil == Notification.create_notification(activity, user)
end
test "it disables notifications from followers" do test "it disables notifications from followers" do
follower = insert(:user) follower = insert(:user)
followed = insert(:user, info: %{notification_settings: %{"followers" => false}}) followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
@ -113,6 +86,13 @@ defmodule Pleroma.NotificationTest do
assert nil == Notification.create_notification(activity, followed) assert nil == Notification.create_notification(activity, followed)
end end
test "it disables notifications from non-followers" do
follower = insert(:user)
followed = insert(:user, info: %{notification_settings: %{"non_followers" => false}})
{:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
assert nil == Notification.create_notification(activity, followed)
end
test "it disables notifications from people the user follows" do test "it disables notifications from people the user follows" do
follower = insert(:user, info: %{notification_settings: %{"follows" => false}}) follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
followed = insert(:user) followed = insert(:user)
@ -122,6 +102,13 @@ defmodule Pleroma.NotificationTest do
assert nil == Notification.create_notification(activity, follower) assert nil == Notification.create_notification(activity, follower)
end end
test "it disables notifications from people the user does not follow" do
follower = insert(:user, info: %{notification_settings: %{"non_follows" => false}})
followed = insert(:user)
{:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
assert nil == 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

@ -78,10 +78,10 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
user = insert(:user) user = insert(:user)
notification_settings = %{ notification_settings = %{
"remote" => true,
"local" => true,
"followers" => true, "followers" => true,
"follows" => true "follows" => true,
"non_follows" => true,
"non_followers" => true
} }
privacy = user.info.default_scope privacy = user.info.default_scope

View file

@ -102,7 +102,6 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
conn conn
|> assign(:user, user) |> assign(:user, user)
|> put("/api/pleroma/notification_settings", %{ |> put("/api/pleroma/notification_settings", %{
"remote" => false,
"followers" => false, "followers" => false,
"bar" => 1 "bar" => 1
}) })
@ -110,8 +109,12 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
user = Repo.get(User, user.id) user = Repo.get(User, user.id)
assert %{"remote" => false, "local" => true, "followers" => false, "follows" => true} == assert %{
user.info.notification_settings "followers" => false,
"follows" => true,
"non_follows" => true,
"non_followers" => true
} == user.info.notification_settings
end end
end end

View file

@ -112,9 +112,11 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
as_user = UserView.render("show.json", %{user: user, for: user}) as_user = UserView.render("show.json", %{user: user, for: user})
assert as_user["default_scope"] == user.info.default_scope assert as_user["default_scope"] == user.info.default_scope
assert as_user["no_rich_text"] == user.info.no_rich_text assert as_user["no_rich_text"] == user.info.no_rich_text
assert as_user["pleroma"]["notification_settings"] == user.info.notification_settings
as_stranger = UserView.render("show.json", %{user: user}) as_stranger = UserView.render("show.json", %{user: user})
refute as_stranger["default_scope"] refute as_stranger["default_scope"]
refute as_stranger["no_rich_text"] refute as_stranger["no_rich_text"]
refute as_stranger["pleroma"]["notification_settings"]
end end
test "A user for a given other follower", %{user: user} do test "A user for a given other follower", %{user: user} do