forked from YokaiRick/akkoma
web push http_client fix
This commit is contained in:
parent
34029ffb90
commit
cbdaabad34
7 changed files with 21 additions and 54 deletions
|
@ -59,8 +59,6 @@
|
||||||
"BLH1qVhJItRGCfxgTtONfsOKDc9VRAraXw-3NsmjMngWSh7NxOizN6bkuRA7iLTMPS82PjwJAr3UoK9EC1IFrz4",
|
"BLH1qVhJItRGCfxgTtONfsOKDc9VRAraXw-3NsmjMngWSh7NxOizN6bkuRA7iLTMPS82PjwJAr3UoK9EC1IFrz4",
|
||||||
private_key: "_-XZ0iebPrRfZ_o0-IatTdszYa8VCH1yLN-JauK7HHA"
|
private_key: "_-XZ0iebPrRfZ_o0-IatTdszYa8VCH1yLN-JauK7HHA"
|
||||||
|
|
||||||
config :web_push_encryption, :http_client, Pleroma.Web.WebPushHttpClientMock
|
|
||||||
|
|
||||||
config :pleroma, Pleroma.ScheduledActivity,
|
config :pleroma, Pleroma.ScheduledActivity,
|
||||||
daily_user_limit: 2,
|
daily_user_limit: 2,
|
||||||
total_user_limit: 3,
|
total_user_limit: 3,
|
||||||
|
|
|
@ -809,7 +809,7 @@
|
||||||
|
|
||||||
config :ex_aws, http_client: Pleroma.HTTP.ExAws
|
config :ex_aws, http_client: Pleroma.HTTP.ExAws
|
||||||
|
|
||||||
config :web_push_encryption, http_client: Pleroma.HTTP
|
config :web_push_encryption, http_client: Pleroma.HTTP.WebPush
|
||||||
|
|
||||||
config :pleroma, :instances_favicons, enabled: false
|
config :pleroma, :instances_favicons, enabled: false
|
||||||
|
|
||||||
|
|
|
@ -83,8 +83,6 @@
|
||||||
"BLH1qVhJItRGCfxgTtONfsOKDc9VRAraXw-3NsmjMngWSh7NxOizN6bkuRA7iLTMPS82PjwJAr3UoK9EC1IFrz4",
|
"BLH1qVhJItRGCfxgTtONfsOKDc9VRAraXw-3NsmjMngWSh7NxOizN6bkuRA7iLTMPS82PjwJAr3UoK9EC1IFrz4",
|
||||||
private_key: "_-XZ0iebPrRfZ_o0-IatTdszYa8VCH1yLN-JauK7HHA"
|
private_key: "_-XZ0iebPrRfZ_o0-IatTdszYa8VCH1yLN-JauK7HHA"
|
||||||
|
|
||||||
config :web_push_encryption, :http_client, Pleroma.Web.WebPushHttpClientMock
|
|
||||||
|
|
||||||
config :pleroma, Oban,
|
config :pleroma, Oban,
|
||||||
queues: false,
|
queues: false,
|
||||||
crontab: false,
|
crontab: false,
|
||||||
|
|
12
lib/pleroma/http/web_push.ex
Normal file
12
lib/pleroma/http/web_push.ex
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.HTTP.WebPush do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
def post(url, payload, headers) do
|
||||||
|
list_headers = Map.to_list(headers)
|
||||||
|
Pleroma.HTTP.post(url, payload, list_headers)
|
||||||
|
end
|
||||||
|
end
|
|
@ -19,7 +19,7 @@ defmodule Pleroma.Web.Push.Impl do
|
||||||
@types ["Create", "Follow", "Announce", "Like", "Move"]
|
@types ["Create", "Follow", "Announce", "Like", "Move"]
|
||||||
|
|
||||||
@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 | {:error, :unknown_type}
|
||||||
def perform(
|
def perform(
|
||||||
%{
|
%{
|
||||||
activity: %{data: %{"type" => activity_type}} = activity,
|
activity: %{data: %{"type" => activity_type}} = activity,
|
||||||
|
@ -64,20 +64,20 @@ def perform(_) do
|
||||||
@doc "Push message to web"
|
@doc "Push message to web"
|
||||||
def push_message(body, sub, api_key, subscription) do
|
def push_message(body, sub, api_key, subscription) do
|
||||||
case WebPushEncryption.send_web_push(body, sub, api_key) do
|
case WebPushEncryption.send_web_push(body, sub, api_key) do
|
||||||
{:ok, %{status_code: code}} when 400 <= code and code < 500 ->
|
{:ok, %{status: code}} when code in 400..499 ->
|
||||||
Logger.debug("Removing subscription record")
|
Logger.debug("Removing subscription record")
|
||||||
Repo.delete!(subscription)
|
Repo.delete!(subscription)
|
||||||
:ok
|
:ok
|
||||||
|
|
||||||
{:ok, %{status_code: code}} when 200 <= code and code < 300 ->
|
{:ok, %{status: code}} when code in 200..299 ->
|
||||||
:ok
|
:ok
|
||||||
|
|
||||||
{:ok, %{status_code: code}} ->
|
{:ok, %{status: code}} ->
|
||||||
Logger.error("Web Push Notification failed with code: #{code}")
|
Logger.error("Web Push Notification failed with code: #{code}")
|
||||||
:error
|
:error
|
||||||
|
|
||||||
_ ->
|
error ->
|
||||||
Logger.error("Web Push Notification failed with unknown error")
|
Logger.error("Web Push Notification failed with #{inspect(error)}")
|
||||||
:error
|
:error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
# Pleroma: A lightweight social networking server
|
|
||||||
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
|
||||||
|
|
||||||
defmodule Pleroma.Web.WebPushHttpClientMock do
|
|
||||||
def get(url, headers \\ [], options \\ []) do
|
|
||||||
{
|
|
||||||
res,
|
|
||||||
%Tesla.Env{status: status}
|
|
||||||
} = Pleroma.HTTP.request(:get, url, "", headers, options)
|
|
||||||
|
|
||||||
{res, %{status_code: status}}
|
|
||||||
end
|
|
||||||
|
|
||||||
def post(url, body, headers \\ [], options \\ []) do
|
|
||||||
{
|
|
||||||
res,
|
|
||||||
%Tesla.Env{status: status}
|
|
||||||
} = Pleroma.HTTP.request(:post, url, body, headers, options)
|
|
||||||
|
|
||||||
{res, %{status_code: status}}
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -5,6 +5,8 @@
|
||||||
defmodule Pleroma.Web.Push.ImplTest do
|
defmodule Pleroma.Web.Push.ImplTest do
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
|
|
||||||
|
import Pleroma.Factory
|
||||||
|
|
||||||
alias Pleroma.Notification
|
alias Pleroma.Notification
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
|
@ -12,10 +14,6 @@ defmodule Pleroma.Web.Push.ImplTest do
|
||||||
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
|
||||||
alias Pleroma.Web.WebPushHttpClientMock
|
|
||||||
|
|
||||||
import Mock
|
|
||||||
import Pleroma.Factory
|
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
Tesla.Mock.mock(fn
|
Tesla.Mock.mock(fn
|
||||||
|
@ -80,22 +78,6 @@ test "successful message sending" do
|
||||||
assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
|
assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
|
||||||
end
|
end
|
||||||
|
|
||||||
test_with_mock "uses WebPushHttpClientMock as an HTTP client", WebPushHttpClientMock,
|
|
||||||
post: fn _, _, _ -> {:ok, %{status_code: 200}} end do
|
|
||||||
Impl.push_message(@message, @sub, @api_key, %Subscription{})
|
|
||||||
assert_called(WebPushHttpClientMock.post("https://example.com/example/1234", :_, :_))
|
|
||||||
end
|
|
||||||
|
|
||||||
test_with_mock "uses Pleroma.HTTP as an HTTP client", Pleroma.HTTP,
|
|
||||||
post: fn _, _, _ -> {:ok, %{status_code: 200}} end do
|
|
||||||
client = Application.get_env(:web_push_encryption, :http_client)
|
|
||||||
on_exit(fn -> Application.put_env(:web_push_encryption, :http_client, client) end)
|
|
||||||
Application.put_env(:web_push_encryption, :http_client, Pleroma.HTTP)
|
|
||||||
|
|
||||||
Impl.push_message(@message, @sub, @api_key, %Subscription{})
|
|
||||||
assert_called(Pleroma.HTTP.post("https://example.com/example/1234", :_, :_))
|
|
||||||
end
|
|
||||||
|
|
||||||
@tag capture_log: true
|
@tag capture_log: true
|
||||||
test "fail message sending" do
|
test "fail message sending" do
|
||||||
assert Impl.push_message(
|
assert Impl.push_message(
|
||||||
|
|
Loading…
Reference in a new issue