Merge branch 'fix-webpush' into 'develop'
WebPush: Use Object.normalize, rewrite tests so they test reality. See merge request pleroma/pleroma!1105
This commit is contained in:
commit
41d78f9108
|
@ -21,8 +21,10 @@ defmodule Pleroma.Web.Push.Impl do
|
||||||
@doc "Performs sending notifications for user subscriptions"
|
@doc "Performs sending notifications for user subscriptions"
|
||||||
@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}, user_id: user_id} =
|
%{
|
||||||
notif
|
activity: %{data: %{"type" => activity_type}, id: activity_id} = activity,
|
||||||
|
user_id: user_id
|
||||||
|
} = notif
|
||||||
)
|
)
|
||||||
when activity_type in @types do
|
when activity_type in @types do
|
||||||
actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
|
actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
|
||||||
|
@ -30,13 +32,14 @@ def perform(
|
||||||
type = Activity.mastodon_notification_type(notif.activity)
|
type = Activity.mastodon_notification_type(notif.activity)
|
||||||
gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key)
|
gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key)
|
||||||
avatar_url = User.avatar_url(actor)
|
avatar_url = User.avatar_url(actor)
|
||||||
|
object = Object.normalize(activity)
|
||||||
|
|
||||||
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),
|
title: format_title(notif),
|
||||||
access_token: subscription.token.token,
|
access_token: subscription.token.token,
|
||||||
body: format_body(notif, actor),
|
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,
|
||||||
|
@ -95,25 +98,25 @@ def build_sub(subscription) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_body(
|
def format_body(
|
||||||
%{activity: %{data: %{"type" => "Create", "object" => %{"content" => content}}}},
|
%{activity: %{data: %{"type" => "Create"}}},
|
||||||
actor
|
actor,
|
||||||
|
%{data: %{"content" => content}}
|
||||||
) do
|
) do
|
||||||
"@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
|
"@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_body(
|
def format_body(
|
||||||
%{activity: %{data: %{"type" => "Announce", "object" => activity_id}}},
|
%{activity: %{data: %{"type" => "Announce"}}},
|
||||||
actor
|
actor,
|
||||||
|
%{data: %{"content" => content}}
|
||||||
) do
|
) do
|
||||||
%Activity{data: %{"object" => %{"id" => object_id}}} = Activity.get_by_ap_id(activity_id)
|
|
||||||
%Object{data: %{"content" => content}} = Object.get_by_ap_id(object_id)
|
|
||||||
|
|
||||||
"@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
|
"@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_body(
|
def format_body(
|
||||||
%{activity: %{data: %{"type" => type}}},
|
%{activity: %{data: %{"type" => type}}},
|
||||||
actor
|
actor,
|
||||||
|
_object
|
||||||
)
|
)
|
||||||
when type in ["Follow", "Like"] do
|
when type in ["Follow", "Like"] do
|
||||||
case type do
|
case type do
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
defmodule Pleroma.Web.Push.ImplTest do
|
defmodule Pleroma.Web.Push.ImplTest do
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
|
|
||||||
|
alias Pleroma.Object
|
||||||
|
alias Pleroma.Web.CommonAPI
|
||||||
alias Pleroma.Web.Push.Impl
|
alias Pleroma.Web.Push.Impl
|
||||||
alias Pleroma.Web.Push.Subscription
|
alias Pleroma.Web.Push.Subscription
|
||||||
|
|
||||||
|
@ -52,16 +54,12 @@ test "performs sending notifications" do
|
||||||
data: %{alerts: %{"follow" => true, "mention" => false}}
|
data: %{alerts: %{"follow" => true, "mention" => false}}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "<Lorem ipsum dolor sit amet."})
|
||||||
|
|
||||||
notif =
|
notif =
|
||||||
insert(:notification,
|
insert(:notification,
|
||||||
user: user,
|
user: user,
|
||||||
activity: %Pleroma.Activity{
|
activity: activity
|
||||||
data: %{
|
|
||||||
"type" => "Create",
|
|
||||||
"actor" => user.ap_id,
|
|
||||||
"object" => %{"content" => "<Lorem ipsum dolor sit amet."}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
assert Impl.perform(notif) == [:ok, :ok]
|
assert Impl.perform(notif) == [:ok, :ok]
|
||||||
|
@ -100,48 +98,65 @@ test "delete subsciption if restult send message between 400..500" do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "renders body for create activity" do
|
test "renders body for create activity" do
|
||||||
|
user = insert(:user, nickname: "Bob")
|
||||||
|
|
||||||
|
{:ok, activity} =
|
||||||
|
CommonAPI.post(user, %{
|
||||||
|
"status" =>
|
||||||
|
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
||||||
|
})
|
||||||
|
|
||||||
|
object = Object.normalize(activity)
|
||||||
|
|
||||||
assert Impl.format_body(
|
assert Impl.format_body(
|
||||||
%{
|
%{
|
||||||
activity: %{
|
activity: activity
|
||||||
data: %{
|
|
||||||
"type" => "Create",
|
|
||||||
"object" => %{
|
|
||||||
"content" =>
|
|
||||||
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
%{nickname: "Bob"}
|
user,
|
||||||
|
object
|
||||||
) ==
|
) ==
|
||||||
"@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
|
"@Bob: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
|
||||||
end
|
end
|
||||||
|
|
||||||
test "renders body for follow activity" do
|
test "renders body for follow activity" do
|
||||||
assert Impl.format_body(%{activity: %{data: %{"type" => "Follow"}}}, %{nickname: "Bob"}) ==
|
user = insert(:user, nickname: "Bob")
|
||||||
|
other_user = insert(:user)
|
||||||
|
{:ok, _, _, activity} = CommonAPI.follow(user, other_user)
|
||||||
|
object = Object.normalize(activity)
|
||||||
|
|
||||||
|
assert Impl.format_body(%{activity: activity}, user, object) ==
|
||||||
"@Bob has followed you"
|
"@Bob has followed you"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "renders body for announce activity" do
|
test "renders body for announce activity" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
note =
|
{:ok, activity} =
|
||||||
insert(:note, %{
|
CommonAPI.post(user, %{
|
||||||
data: %{
|
"status" =>
|
||||||
"content" =>
|
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
||||||
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
note_activity = insert(:note_activity, %{note: note})
|
{:ok, announce_activity, _} = CommonAPI.repeat(activity.id, user)
|
||||||
announce_activity = insert(:announce_activity, %{user: user, note_activity: note_activity})
|
object = Object.normalize(activity)
|
||||||
|
|
||||||
assert Impl.format_body(%{activity: announce_activity}, user) ==
|
assert Impl.format_body(%{activity: announce_activity}, user, object) ==
|
||||||
"@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
|
"@#{user.nickname} repeated: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce sagittis fini..."
|
||||||
end
|
end
|
||||||
|
|
||||||
test "renders body for like activity" do
|
test "renders body for like activity" do
|
||||||
assert Impl.format_body(%{activity: %{data: %{"type" => "Like"}}}, %{nickname: "Bob"}) ==
|
user = insert(:user, nickname: "Bob")
|
||||||
|
|
||||||
|
{:ok, activity} =
|
||||||
|
CommonAPI.post(user, %{
|
||||||
|
"status" =>
|
||||||
|
"<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
|
||||||
|
})
|
||||||
|
|
||||||
|
{:ok, activity, _} = CommonAPI.favorite(activity.id, user)
|
||||||
|
object = Object.normalize(activity)
|
||||||
|
|
||||||
|
assert Impl.format_body(%{activity: activity}, user, object) ==
|
||||||
"@Bob has favorited your post"
|
"@Bob has favorited your post"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue