added privacy option to push notifications

This commit is contained in:
Maksim Pechnikov 2019-10-29 21:33:17 +03:00
commit 04a8ffbe84
6 changed files with 76 additions and 40 deletions

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 ->

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,

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