forked from AkkomaGang/akkoma
added privacy option to push notifications
This commit is contained in:
parent
a52da55eb9
commit
04a8ffbe84
6 changed files with 76 additions and 40 deletions
|
@ -9,18 +9,12 @@ defmodule Pleroma.User.NotificationSetting do
|
||||||
@derive Jason.Encoder
|
@derive Jason.Encoder
|
||||||
@primary_key false
|
@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
|
embedded_schema do
|
||||||
field(:followers, :boolean, default: true)
|
field(:followers, :boolean, default: true)
|
||||||
field(:follows, :boolean, default: true)
|
field(:follows, :boolean, default: true)
|
||||||
field(:non_follows, :boolean, default: true)
|
field(:non_follows, :boolean, default: true)
|
||||||
field(:non_followers, :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
|
end
|
||||||
|
|
||||||
def changeset(schema, params) do
|
def changeset(schema, params) do
|
||||||
|
@ -32,14 +26,11 @@ def changeset(schema, params) do
|
||||||
:non_followers,
|
:non_followers,
|
||||||
:privacy_option
|
:privacy_option
|
||||||
])
|
])
|
||||||
|> validate_inclusion(:privacy_option, Map.values(@privacy_options))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp prepare_attrs(params) do
|
defp prepare_attrs(params) do
|
||||||
Enum.reduce(params, %{}, fn
|
Enum.reduce(params, %{}, fn
|
||||||
{k, v}, acc
|
{k, v}, acc when is_binary(v) ->
|
||||||
when k in ["followers", "follows", "non_follows", "non_followers"] and
|
|
||||||
is_binary(v) ->
|
|
||||||
Map.put(acc, k, String.downcase(v))
|
Map.put(acc, k, String.downcase(v))
|
||||||
|
|
||||||
{k, v}, acc ->
|
{k, v}, acc ->
|
||||||
|
|
|
@ -22,8 +22,8 @@ defmodule Pleroma.Web.Push.Impl do
|
||||||
@spec perform(Notification.t()) :: list(any) | :error
|
@spec perform(Notification.t()) :: list(any) | :error
|
||||||
def perform(
|
def perform(
|
||||||
%{
|
%{
|
||||||
activity: %{data: %{"type" => activity_type}, id: activity_id} = activity,
|
activity: %{data: %{"type" => activity_type}} = activity,
|
||||||
user_id: user_id
|
user: %User{id: user_id}
|
||||||
} = notif
|
} = notif
|
||||||
)
|
)
|
||||||
when activity_type in @types do
|
when activity_type in @types do
|
||||||
|
@ -39,18 +39,17 @@ def perform(
|
||||||
for subscription <- fetch_subsriptions(user_id),
|
for subscription <- fetch_subsriptions(user_id),
|
||||||
get_in(subscription.data, ["alerts", type]) do
|
get_in(subscription.data, ["alerts", type]) do
|
||||||
%{
|
%{
|
||||||
title: format_title(notif),
|
|
||||||
access_token: subscription.token.token,
|
access_token: subscription.token.token,
|
||||||
body: format_body(notif, actor, object),
|
|
||||||
notification_id: notif.id,
|
notification_id: notif.id,
|
||||||
notification_type: type,
|
notification_type: type,
|
||||||
icon: avatar_url,
|
icon: avatar_url,
|
||||||
preferred_locale: "en",
|
preferred_locale: "en",
|
||||||
pleroma: %{
|
pleroma: %{
|
||||||
activity_id: activity_id,
|
activity_id: notif.activity.id,
|
||||||
direct_conversation_id: direct_conversation_id
|
direct_conversation_id: direct_conversation_id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|> Map.merge(build_content(notif, actor, object))
|
||||||
|> Jason.encode!()
|
|> Jason.encode!()
|
||||||
|> push_message(build_sub(subscription), gcm_api_key, subscription)
|
|> push_message(build_sub(subscription), gcm_api_key, subscription)
|
||||||
end
|
end
|
||||||
|
@ -100,6 +99,24 @@ def build_sub(subscription) do
|
||||||
}
|
}
|
||||||
end
|
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(
|
def format_body(
|
||||||
%{activity: %{data: %{"type" => "Create"}}},
|
%{activity: %{data: %{"type" => "Create"}}},
|
||||||
actor,
|
actor,
|
||||||
|
|
|
@ -13,7 +13,7 @@ def perform(%{"op" => "web_push", "notification_id" => notification_id}, _job) d
|
||||||
notification =
|
notification =
|
||||||
Notification
|
Notification
|
||||||
|> Repo.get(notification_id)
|
|> Repo.get(notification_id)
|
||||||
|> Repo.preload([:activity])
|
|> Repo.preload([:activity, :user])
|
||||||
|
|
||||||
Pleroma.Web.Push.Impl.perform(notification)
|
Pleroma.Web.Push.Impl.perform(notification)
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,29 +12,10 @@ test "sets valid privacy option" do
|
||||||
changeset =
|
changeset =
|
||||||
NotificationSetting.changeset(
|
NotificationSetting.changeset(
|
||||||
%NotificationSetting{},
|
%NotificationSetting{},
|
||||||
%{"privacy_option" => "name_only"}
|
%{"privacy_option" => true}
|
||||||
)
|
)
|
||||||
|
|
||||||
assert %Ecto.Changeset{valid?: true} = changeset
|
assert %Ecto.Changeset{valid?: true} = changeset
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,6 +6,7 @@ defmodule Pleroma.Web.Push.ImplTest do
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
|
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
|
alias Pleroma.User
|
||||||
alias Pleroma.Web.CommonAPI
|
alias Pleroma.Web.CommonAPI
|
||||||
alias Pleroma.Web.Push.Impl
|
alias Pleroma.Web.Push.Impl
|
||||||
alias Pleroma.Web.Push.Subscription
|
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}) ==
|
assert Impl.format_title(%{activity: activity}) ==
|
||||||
"New Direct Message"
|
"New Direct Message"
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -164,7 +164,7 @@ test "it updates notification settings", %{conn: conn} do
|
||||||
follows: true,
|
follows: true,
|
||||||
non_follows: true,
|
non_follows: true,
|
||||||
non_followers: true,
|
non_followers: true,
|
||||||
privacy_option: "name_and_message"
|
privacy_option: false
|
||||||
} == user.notification_settings
|
} == user.notification_settings
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -173,7 +173,7 @@ test "it update notificatin privacy option", %{conn: conn} do
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
|> put("/api/pleroma/notification_settings", %{"privacy_option" => "name_only"})
|
|> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"})
|
||||||
|> json_response(:ok)
|
|> json_response(:ok)
|
||||||
|
|
||||||
user = refresh_record(user)
|
user = refresh_record(user)
|
||||||
|
@ -183,7 +183,7 @@ test "it update notificatin privacy option", %{conn: conn} do
|
||||||
follows: true,
|
follows: true,
|
||||||
non_follows: true,
|
non_follows: true,
|
||||||
non_followers: true,
|
non_followers: true,
|
||||||
privacy_option: "name_only"
|
privacy_option: true
|
||||||
} == user.notification_settings
|
} == user.notification_settings
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue