fix and improve web push; add configuration docs
This commit is contained in:
parent
30dc81667c
commit
658edb166f
|
@ -154,3 +154,11 @@ An example:
|
||||||
config :pleroma, :mrf_user_allowlist,
|
config :pleroma, :mrf_user_allowlist,
|
||||||
"example.org": ["https://example.org/users/admin"]
|
"example.org": ["https://example.org/users/admin"]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## :web_push_encryption
|
||||||
|
|
||||||
|
Web Push Notifications configuration. You could use a mix task `mix web_push.gen.keypair` to generate it.
|
||||||
|
|
||||||
|
* ``subject``: a mailto link for the administrative contact. It’s best if this email is not a personal email address, but rather a group email so that if a person leaves an organization, is unavailable for an extended period, or otherwise can’t respond, someone else on the list can.
|
||||||
|
* ``public_key``: VAPID public key
|
||||||
|
* ``private_key``: VAPID private key
|
||||||
|
|
|
@ -15,10 +15,10 @@ def init(options), do: options
|
||||||
def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
|
def call(%{assigns: %{user: %User{}}} = conn, _), do: conn
|
||||||
|
|
||||||
def call(conn, _) do
|
def call(conn, _) do
|
||||||
with {:ok, token} <- fetch_token(conn),
|
with {:ok, token_str} <- fetch_token_str(conn),
|
||||||
{:ok, user} <- fetch_user(token) do
|
{:ok, user, token_record} <- fetch_user_and_token(token_str) do
|
||||||
conn
|
conn
|
||||||
|> assign(:token, token)
|
|> assign(:token, token_record)
|
||||||
|> assign(:user, user)
|
|> assign(:user, user)
|
||||||
else
|
else
|
||||||
_ -> conn
|
_ -> conn
|
||||||
|
@ -27,12 +27,12 @@ def call(conn, _) do
|
||||||
|
|
||||||
# Gets user by token
|
# Gets user by token
|
||||||
#
|
#
|
||||||
@spec fetch_user(String.t()) :: {:ok, User.t()} | nil
|
@spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil
|
||||||
defp fetch_user(token) do
|
defp fetch_user_and_token(token) do
|
||||||
query = from(q in Token, where: q.token == ^token, preload: [:user])
|
query = from(q in Token, where: q.token == ^token, preload: [:user])
|
||||||
|
|
||||||
with %Token{user: %{info: %{deactivated: false} = _} = user} <- Repo.one(query) do
|
with %Token{user: %{info: %{deactivated: false} = _} = user} = token_record <- Repo.one(query) do
|
||||||
{:ok, user}
|
{:ok, user, token_record}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -48,23 +48,23 @@ defp fetch_token_from_session(conn) do
|
||||||
|
|
||||||
# Gets token from headers
|
# Gets token from headers
|
||||||
#
|
#
|
||||||
@spec fetch_token(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()}
|
@spec fetch_token_str(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()}
|
||||||
defp fetch_token(%Plug.Conn{} = conn) do
|
defp fetch_token_str(%Plug.Conn{} = conn) do
|
||||||
headers = get_req_header(conn, "authorization")
|
headers = get_req_header(conn, "authorization")
|
||||||
|
|
||||||
with :no_token_found <- fetch_token(headers),
|
with :no_token_found <- fetch_token_str(headers),
|
||||||
do: fetch_token_from_session(conn)
|
do: fetch_token_from_session(conn)
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec fetch_token(Keyword.t()) :: :no_token_found | {:ok, String.t()}
|
@spec fetch_token_str(Keyword.t()) :: :no_token_found | {:ok, String.t()}
|
||||||
defp fetch_token([]), do: :no_token_found
|
defp fetch_token_str([]), do: :no_token_found
|
||||||
|
|
||||||
defp fetch_token([token | tail]) do
|
defp fetch_token_str([token | tail]) do
|
||||||
trimmed_token = String.trim(token)
|
trimmed_token = String.trim(token)
|
||||||
|
|
||||||
case Regex.run(@realm_reg, trimmed_token) do
|
case Regex.run(@realm_reg, trimmed_token) do
|
||||||
[_, match] -> {:ok, String.trim(match)}
|
[_, match] -> {:ok, String.trim(match)}
|
||||||
_ -> fetch_token(tail)
|
_ -> fetch_token_str(tail)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,12 @@ def render("push_subscription.json", %{subscription: subscription}) do
|
||||||
%{
|
%{
|
||||||
id: to_string(subscription.id),
|
id: to_string(subscription.id),
|
||||||
endpoint: subscription.endpoint,
|
endpoint: subscription.endpoint,
|
||||||
alerts: Map.get(subscription.data, "alerts")
|
alerts: Map.get(subscription.data, "alerts"),
|
||||||
|
server_key: server_key()
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp server_key do
|
||||||
|
Keyword.get(Application.get_env(:web_push_encryption, :vapid_details), :public_key)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -100,16 +100,16 @@ def format(%{activity: %{data: %{"type" => "Follow"}}}, actor) do
|
||||||
|
|
||||||
def format(%{activity: %{data: %{"type" => "Announce"}}}, actor) do
|
def format(%{activity: %{data: %{"type" => "Announce"}}}, actor) do
|
||||||
%{
|
%{
|
||||||
title: "New Announce",
|
title: "New Repeat",
|
||||||
body: "@#{actor.nickname} has announced your post",
|
body: "@#{actor.nickname} has repeated your post",
|
||||||
icon: User.avatar_url(actor)
|
icon: User.avatar_url(actor)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def format(%{activity: %{data: %{"type" => "Like"}}}, actor) do
|
def format(%{activity: %{data: %{"type" => "Like"}}}, actor) do
|
||||||
%{
|
%{
|
||||||
title: "New Like",
|
title: "New Favorite",
|
||||||
body: "@#{actor.nickname} has liked your post",
|
body: "@#{actor.nickname} has favorited your post",
|
||||||
icon: User.avatar_url(actor)
|
icon: User.avatar_url(actor)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue