2cff05549b1abf532aa7948ce92478ed743d09c1
[akkoma] / lib / pleroma / user / welcome_message.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.User.WelcomeMessage do
6 alias Pleroma.Config
7 alias Pleroma.User
8 alias Pleroma.Web.CommonAPI
9
10 @spec enabled?() :: boolean()
11 def enabled?, do: Config.get([:welcome, :direct_message, :enabled], false)
12
13 @spec post_message(User.t()) :: {:ok, Pleroma.Activity.t() | nil}
14 def post_message(user) do
15 [:welcome, :direct_message, :sender_nickname]
16 |> Config.get(nil)
17 |> fetch_sender()
18 |> do_post(user, welcome_message())
19 end
20
21 defp do_post(%User{} = sender, %User{nickname: nickname}, message)
22 when is_binary(message) do
23 CommonAPI.post(
24 sender,
25 %{
26 visibility: "direct",
27 status: "@#{nickname}\n#{message}"
28 }
29 )
30 end
31
32 defp do_post(_sender, _recipient, _message), do: {:ok, nil}
33
34 defp fetch_sender(nickname) when is_binary(nickname) do
35 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
36 user
37 else
38 _ -> nil
39 end
40 end
41
42 defp fetch_sender(_), do: nil
43
44 defp welcome_message do
45 Config.get([:welcome, :direct_message, :message], nil)
46 end
47 end