Compare commits

...

3 commits

Author SHA1 Message Date
Duponin 92f4a84f80 Merge branch 'fix-463' into 'develop'
Add mix task to send a notifaction, with a web push

Closes #463

See merge request pleroma/pleroma!3805
2024-04-14 16:48:51 +00:00
duponin 52fed681d4 Improve the message from the test notification
It makes it more clear it's a test from a Mix task
2022-12-17 18:35:40 +01:00
duponin a810cdee77 Add mix task to send a notifaction, with a web push
Sending only a Push notification can't be done, it needs a Notification.
Creating a notification needs an activity.
Creating an activity needs an object.
That's why it's running throughout the whole process.

It isn't really clean because it's forging a post, it might be
acceptable to perform it from an account like "admin" or similar.

That said, you can't use Pleroma's builtin users ('relay' and
'internal.fetch') because they are invisible and stripped out from
notification creation pipeline.
It would have been a better solution, to have real administrative users.
2022-12-13 21:27:28 +01:00
2 changed files with 111 additions and 0 deletions

View file

@ -0,0 +1,91 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Mix.Tasks.Pleroma.NotificationTest do
@moduledoc """
Send a test notification under the form of a direct message.
The "From" user must exist and be different from the "To" user.
For technical reasons, you can't just run this task like an usual one, you need to start the task with a node name and connecting to the running Pleroma instance.
Firstly, make sure Pleroma is running with a node name; by default it is not.
To add a node name, you must add the following between `mix` and `phx.server` in whatever you use to start Pleroma: `--sname pleroma-main`.
Example: `mix --sname pleroma-main phx.server`
> Warning: While running with a name can be extremely useful for debug purpose, you should disable it as soon you have done you work, unless you are fluent with Elixir, Erlang and BEAM (and make sure the port `4369/TCP` is firewalled!).
Secondly, to have the mix task to connect to the running Pleroma instance, you also have to give it a node name, it looks almost the same as running Pleroma with a node name: `iex --sname pleroma-mix -S mix pleroma.notification_test ...`
Thirdly, for the `--node` argument, you have to provide the node name you gave to Pleroma (in the example, that's "pleroma-main"), suffixed by `@<your machine hostname>`.
If you don't know what your machine hostname is, just run the command `hostname` in a shell and you will get it.
Example:
```
$ hostname
wired
$ iex --sname pleroma-mix -S mix pleroma.notification_test --node="pleroma-main@wired"
```
Wrapping everything together:
```
mix pleroma.notification_test --from="lain" --to="masami" --node="pleroma-main@wired"
```
"""
use Mix.Task
import Mix.Pleroma
def run(args) do
start_pleroma()
{options, _, _} =
OptionParser.parse(
args,
strict: [
from: :string,
to: :string,
node: :string
]
)
if Node.self() == :nonode@nohost do
shell_error("It seems like you didn't start this mix task with a node name.")
shell_error("Don't worry, run 'mix help pleroma.notification_test' to get help.")
System.halt()
end
if not Keyword.has_key?(options, :from) do
shell_error("I need '--from' to be set!")
System.halt()
end
if not Keyword.has_key?(options, :to) do
shell_error("I need '--to' to be set!")
System.halt()
end
if not Keyword.has_key?(options, :node) do
shell_error("I need '--node' to be set!")
System.halt()
end
node = options |> Keyword.get(:node) |> String.to_atom()
Node.connect(node)
# A spawn_link would be much better, but I don't want to run an IEx shell
# after running this task (hence the `System.halt/1`). Anyway, the spawned
# function should not run forever, it's a oneshot function, if it fails,
# it won't last forever, that's not like a GenServer or anything, right?
Node.spawn(node, Pleroma.Notification, :test_notification, [
Keyword.get(options, :from),
Keyword.get(options, :to)
])
shell_info("Done")
System.halt()
end
end

View file

@ -748,4 +748,24 @@ defmodule Pleroma.Notification do
)
|> Repo.update_all(set: [seen: true])
end
@doc """
This function should never be used for production, but only for debug purpose.
I would really prefer keeping it in the Mix.Task but these aren't available
during the runtime, but all my trials were unsuccessful.
"""
def test_notification(from, to) do
from = Pleroma.User.get_by_nickname(from)
to = Pleroma.User.get_by_nickname(to)
{:ok, activity} =
Pleroma.Web.CommonAPI.post(from, %{
:content_type => "text/plain",
:status => "This is a test notification made from a mix task.",
:visibility => "direct",
"source" => "The Wired"
})
Pleroma.Notification.create_notification(activity, to)
end
end