Browse Source

added privacy option to push notifications

remote-follow-auth-fix
Maksim Pechnikov 4 years ago
parent
commit
04a8ffbe84
6 changed files with 76 additions and 40 deletions
  1. +2
    -11
      lib/pleroma/user/notification_setting.ex
  2. +22
    -5
      lib/pleroma/web/push/impl.ex
  3. +1
    -1
      lib/pleroma/workers/web_pusher_worker.ex
  4. +1
    -20
      test/user/notification_setting_test.exs
  5. +47
    -0
      test/web/push/impl_test.exs
  6. +3
    -3
      test/web/twitter_api/util_controller_test.exs

+ 2
- 11
lib/pleroma/user/notification_setting.ex View File

@@ -9,18 +9,12 @@ defmodule Pleroma.User.NotificationSetting do
@derive Jason.Encoder
@primary_key false

@privacy_options %{
name_and_message: "name_and_message",
name_only: "name_only",
no_name_or_message: "no_name_or_message"
}

embedded_schema do
field(:followers, :boolean, default: true)
field(:follows, :boolean, default: true)
field(:non_follows, :boolean, default: true)
field(:non_followers, :boolean, default: true)
field(:privacy_option, :string, default: @privacy_options.name_and_message)
field(:privacy_option, :boolean, default: false)
end

def changeset(schema, params) do
@@ -32,14 +26,11 @@ defmodule Pleroma.User.NotificationSetting do
:non_followers,
:privacy_option
])
|> validate_inclusion(:privacy_option, Map.values(@privacy_options))
end

defp prepare_attrs(params) do
Enum.reduce(params, %{}, fn
{k, v}, acc
when k in ["followers", "follows", "non_follows", "non_followers"] and
is_binary(v) ->
{k, v}, acc when is_binary(v) ->
Map.put(acc, k, String.downcase(v))

{k, v}, acc ->


+ 22
- 5
lib/pleroma/web/push/impl.ex View File

@@ -22,8 +22,8 @@ defmodule Pleroma.Web.Push.Impl do
@spec perform(Notification.t()) :: list(any) | :error
def perform(
%{
activity: %{data: %{"type" => activity_type}, id: activity_id} = activity,
user_id: user_id
activity: %{data: %{"type" => activity_type}} = activity,
user: %User{id: user_id}
} = notif
)
when activity_type in @types do
@@ -39,18 +39,17 @@ defmodule Pleroma.Web.Push.Impl do
for subscription <- fetch_subsriptions(user_id),
get_in(subscription.data, ["alerts", type]) do
%{
title: format_title(notif),
access_token: subscription.token.token,
body: format_body(notif, actor, object),
notification_id: notif.id,
notification_type: type,
icon: avatar_url,
preferred_locale: "en",
pleroma: %{
activity_id: activity_id,
activity_id: notif.activity.id,
direct_conversation_id: direct_conversation_id
}
}
|> Map.merge(build_content(notif, actor, object))
|> Jason.encode!()
|> push_message(build_sub(subscription), gcm_api_key, subscription)
end
@@ -100,6 +99,24 @@ defmodule Pleroma.Web.Push.Impl do
}
end

def build_content(
%{
activity: %{data: %{"directMessage" => true}},
user: %{notification_settings: %{privacy_option: true}}
},
actor,
_
) do
%{title: "New Direct Message", body: "@#{actor.nickname}"}
end

def build_content(notif, actor, object) do
%{
title: format_title(notif),
body: format_body(notif, actor, object)
}
end

def format_body(
%{activity: %{data: %{"type" => "Create"}}},
actor,


+ 1
- 1
lib/pleroma/workers/web_pusher_worker.ex View File

@@ -13,7 +13,7 @@ defmodule Pleroma.Workers.WebPusherWorker do
notification =
Notification
|> Repo.get(notification_id)
|> Repo.preload([:activity])
|> Repo.preload([:activity, :user])

Pleroma.Web.Push.Impl.perform(notification)
end


+ 1
- 20
test/user/notification_setting_test.exs View File

@@ -12,29 +12,10 @@ defmodule Pleroma.User.NotificationSettingTest do
changeset =
NotificationSetting.changeset(
%NotificationSetting{},
%{"privacy_option" => "name_only"}
%{"privacy_option" => true}
)

assert %Ecto.Changeset{valid?: true} = changeset
end

test "returns invalid changeset when privacy option is incorrect" do
changeset =
NotificationSetting.changeset(
%NotificationSetting{},
%{"privacy_option" => "full_content"}
)

assert %Ecto.Changeset{valid?: false} = changeset

assert [
privacy_option:
{"is invalid",
[
validation: :inclusion,
enum: ["name_and_message", "name_only", "no_name_or_message"]
]}
] = changeset.errors
end
end
end

+ 47
- 0
test/web/push/impl_test.exs View File

@@ -6,6 +6,7 @@ defmodule Pleroma.Web.Push.ImplTest do
use Pleroma.DataCase

alias Pleroma.Object
alias Pleroma.User
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.Push.Impl
alias Pleroma.Web.Push.Subscription
@@ -182,4 +183,50 @@ defmodule Pleroma.Web.Push.ImplTest do
assert Impl.format_title(%{activity: activity}) ==
"New Direct Message"
end

describe "build_content/3" do
test "returns info content for direct message with enabled privacy option" do
user = insert(:user, nickname: "Bob")
user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: true})

{:ok, activity} =
CommonAPI.post(user, %{
"visibility" => "direct",
"status" => "<Lorem ipsum dolor sit amet."
})

notif = insert(:notification, user: user2, activity: activity)

actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
object = Object.normalize(activity)

assert Impl.build_content(notif, actor, object) == %{
body: "@Bob",
title: "New Direct Message"
}
end

test "returns regular content for direct message with disabled privacy option" do
user = insert(:user, nickname: "Bob")
user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: false})

{:ok, activity} =
CommonAPI.post(user, %{
"visibility" => "direct",
"status" =>
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
})

notif = insert(:notification, user: user2, activity: activity)

actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
object = Object.normalize(activity)

assert Impl.build_content(notif, actor, object) == %{
body:
"@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini...",
title: "New Direct Message"
}
end
end
end

+ 3
- 3
test/web/twitter_api/util_controller_test.exs View File

@@ -164,7 +164,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
follows: true,
non_follows: true,
non_followers: true,
privacy_option: "name_and_message"
privacy_option: false
} == user.notification_settings
end

@@ -173,7 +173,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do

conn
|> assign(:user, user)
|> put("/api/pleroma/notification_settings", %{"privacy_option" => "name_only"})
|> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"})
|> json_response(:ok)

user = refresh_record(user)
@@ -183,7 +183,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
follows: true,
non_follows: true,
non_followers: true,
privacy_option: "name_only"
privacy_option: true
} == user.notification_settings
end
end


Loading…
Cancel
Save