- def post_chat_message(%User{} = user, %User{} = recipient, content, opts \\ []) do
- with maybe_attachment <- opts[:media_id] && Object.get_by_id(opts[:media_id]),
- :ok <- validate_chat_content_length(content, !!maybe_attachment),
- {_, {:ok, chat_message_data, _meta}} <-
- {:build_object,
- Builder.chat_message(
- user,
- recipient.ap_id,
- content |> format_chat_content,
- attachment: maybe_attachment
- )},
- {_, {:ok, create_activity_data, _meta}} <-
- {:build_create_activity, Builder.create(user, chat_message_data, [recipient.ap_id])},
- {_, {:ok, %Activity{} = activity, _meta}} <-
- {:common_pipeline,
- Pipeline.common_pipeline(create_activity_data,
- local: true
- )} do
- {:ok, activity}
- else
- {:common_pipeline, {:reject, _} = e} -> e
- e -> e
- end
- end
-
- defp format_chat_content(nil), do: nil
-
- defp format_chat_content(content) do
- {text, _, _} =
- content
- |> Formatter.html_escape("text/plain")
- |> Formatter.linkify()
- |> (fn {text, mentions, tags} ->
- {String.replace(text, ~r/\r?\n/, "<br>"), mentions, tags}
- end).()
-
- text
- end
-
- defp validate_chat_content_length(_, true), do: :ok
- defp validate_chat_content_length(nil, false), do: {:error, :no_content}
-
- defp validate_chat_content_length(content, _) do
- if String.length(content) <= Pleroma.Config.get([:instance, :chat_limit]) do
- :ok
- else
- {:error, :content_too_long}
- end
- end
-