added privacy option to push notifications
This commit is contained in:
parent
a52da55eb9
commit
04a8ffbe84
|
@ -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 @@ def changeset(schema, params) 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,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 @@ def perform(
|
|||
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 @@ def build_sub(subscription) 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,
|
||||
|
|
|
@ -13,7 +13,7 @@ def perform(%{"op" => "web_push", "notification_id" => notification_id}, _job) d
|
|||
notification =
|
||||
Notification
|
||||
|> Repo.get(notification_id)
|
||||
|> Repo.preload([:activity])
|
||||
|> Repo.preload([:activity, :user])
|
||||
|
||||
Pleroma.Web.Push.Impl.perform(notification)
|
||||
end
|
||||
|
|
|
@ -12,29 +12,10 @@ test "sets valid privacy option" 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
|
||||
|
|
|
@ -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 @@ test "renders title for create activity with direct visibility" 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
|
||||
|
|
|
@ -164,7 +164,7 @@ test "it updates notification settings", %{conn: conn} 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 @@ test "it update notificatin privacy option", %{conn: conn} 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 @@ test "it update notificatin privacy option", %{conn: conn} do
|
|||
follows: true,
|
||||
non_follows: true,
|
||||
non_followers: true,
|
||||
privacy_option: "name_only"
|
||||
privacy_option: true
|
||||
} == user.notification_settings
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue