2019-07-10 05:13:23 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-07-10 05:13:23 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-02-16 15:42:34 +00:00
|
|
|
defmodule Pleroma.User.WelcomeMessage do
|
2020-07-21 06:25:53 +00:00
|
|
|
alias Pleroma.Config
|
2019-02-16 15:42:34 +00:00
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
|
2020-07-21 06:25:53 +00:00
|
|
|
@spec enabled?() :: boolean()
|
|
|
|
def enabled?, do: Config.get([:welcome, :direct_message, :enabled], false)
|
|
|
|
|
|
|
|
@spec post_message(User.t()) :: {:ok, Pleroma.Activity.t() | nil}
|
|
|
|
def post_message(user) do
|
|
|
|
[:welcome, :direct_message, :sender_nickname]
|
|
|
|
|> Config.get(nil)
|
|
|
|
|> fetch_sender()
|
|
|
|
|> do_post(user, welcome_message())
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_post(%User{} = sender, %User{nickname: nickname}, message)
|
|
|
|
when is_binary(message) do
|
|
|
|
CommonAPI.post(
|
|
|
|
sender,
|
|
|
|
%{
|
2020-05-12 19:59:26 +00:00
|
|
|
visibility: "direct",
|
2020-07-21 06:25:53 +00:00
|
|
|
status: "@#{nickname}\n#{message}"
|
|
|
|
}
|
|
|
|
)
|
2019-02-16 15:42:34 +00:00
|
|
|
end
|
|
|
|
|
2020-07-21 06:25:53 +00:00
|
|
|
defp do_post(_sender, _recipient, _message), do: {:ok, nil}
|
|
|
|
|
|
|
|
defp fetch_sender(nickname) when is_binary(nickname) do
|
|
|
|
with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
|
2019-02-16 16:24:48 +00:00
|
|
|
user
|
2019-02-16 15:42:34 +00:00
|
|
|
else
|
2019-02-16 16:24:48 +00:00
|
|
|
_ -> nil
|
2019-02-16 15:42:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-21 06:25:53 +00:00
|
|
|
defp fetch_sender(_), do: nil
|
|
|
|
|
2019-03-05 03:18:43 +00:00
|
|
|
defp welcome_message do
|
2020-07-21 06:25:53 +00:00
|
|
|
Config.get([:welcome, :direct_message, :message], nil)
|
2019-02-16 15:42:34 +00:00
|
|
|
end
|
|
|
|
end
|