Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
7.5KB

  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. defmodule Pleroma.Web.CommonAPI.ActivityDraft do
  5. alias Pleroma.Activity
  6. alias Pleroma.Conversation.Participation
  7. alias Pleroma.Object
  8. alias Pleroma.Web.CommonAPI
  9. alias Pleroma.Web.CommonAPI.Utils
  10. import Pleroma.Web.Gettext
  11. defstruct valid?: true,
  12. errors: [],
  13. user: nil,
  14. params: %{},
  15. status: nil,
  16. summary: nil,
  17. full_payload: nil,
  18. attachments: [],
  19. in_reply_to: nil,
  20. in_reply_to_conversation: nil,
  21. visibility: nil,
  22. expires_at: nil,
  23. extra: nil,
  24. emoji: %{},
  25. content_html: nil,
  26. mentions: [],
  27. tags: [],
  28. to: [],
  29. cc: [],
  30. context: nil,
  31. sensitive: false,
  32. object: nil,
  33. preview?: false,
  34. changes: %{}
  35. def new(user, params) do
  36. %__MODULE__{user: user}
  37. |> put_params(params)
  38. end
  39. def create(user, params) do
  40. user
  41. |> new(params)
  42. |> status()
  43. |> summary()
  44. |> with_valid(&attachments/1)
  45. |> full_payload()
  46. |> expires_at()
  47. |> poll()
  48. |> with_valid(&in_reply_to/1)
  49. |> with_valid(&in_reply_to_conversation/1)
  50. |> with_valid(&visibility/1)
  51. |> content()
  52. |> with_valid(&to_and_cc/1)
  53. |> with_valid(&context/1)
  54. |> sensitive()
  55. |> with_valid(&object/1)
  56. |> preview?()
  57. |> with_valid(&changes/1)
  58. |> validate()
  59. end
  60. def listen(user, params) do
  61. user
  62. |> new(params)
  63. |> visibility()
  64. |> to_and_cc()
  65. |> context()
  66. |> listen_object()
  67. |> with_valid(&changes/1)
  68. |> validate()
  69. end
  70. defp listen_object(draft) do
  71. object =
  72. draft.params
  73. |> Map.take([:album, :artist, :title, :length])
  74. |> Map.new(fn {key, value} -> {to_string(key), value} end)
  75. |> Map.put("type", "Audio")
  76. |> Map.put("to", draft.to)
  77. |> Map.put("cc", draft.cc)
  78. |> Map.put("actor", draft.user.ap_id)
  79. %__MODULE__{draft | object: object}
  80. end
  81. defp put_params(draft, params) do
  82. params = Map.put_new(params, :in_reply_to_status_id, params[:in_reply_to_id])
  83. %__MODULE__{draft | params: params}
  84. end
  85. defp status(%{params: %{status: status}} = draft) do
  86. %__MODULE__{draft | status: String.trim(status)}
  87. end
  88. defp summary(%{params: params} = draft) do
  89. %__MODULE__{draft | summary: Map.get(params, :spoiler_text, "")}
  90. end
  91. defp full_payload(%{status: status, summary: summary} = draft) do
  92. full_payload = String.trim(status <> summary)
  93. case Utils.validate_character_limit(full_payload, draft.attachments) do
  94. :ok -> %__MODULE__{draft | full_payload: full_payload}
  95. {:error, message} -> add_error(draft, message)
  96. end
  97. end
  98. defp attachments(%{params: params} = draft) do
  99. attachments = Utils.attachments_from_ids(params)
  100. %__MODULE__{draft | attachments: attachments}
  101. end
  102. defp in_reply_to(%{params: %{in_reply_to_status_id: ""}} = draft), do: draft
  103. defp in_reply_to(%{params: %{in_reply_to_status_id: id}} = draft) when is_binary(id) do
  104. %__MODULE__{draft | in_reply_to: Activity.get_by_id(id)}
  105. end
  106. defp in_reply_to(%{params: %{in_reply_to_status_id: %Activity{} = in_reply_to}} = draft) do
  107. %__MODULE__{draft | in_reply_to: in_reply_to}
  108. end
  109. defp in_reply_to(draft), do: draft
  110. defp in_reply_to_conversation(draft) do
  111. in_reply_to_conversation = Participation.get(draft.params[:in_reply_to_conversation_id])
  112. %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation}
  113. end
  114. defp visibility(%{params: params} = draft) do
  115. case CommonAPI.get_visibility(params, draft.in_reply_to, draft.in_reply_to_conversation) do
  116. {visibility, "direct"} when visibility != "direct" ->
  117. add_error(draft, dgettext("errors", "The message visibility must be direct"))
  118. {visibility, _} ->
  119. %__MODULE__{draft | visibility: visibility}
  120. end
  121. end
  122. defp expires_at(draft) do
  123. case CommonAPI.check_expiry_date(draft.params[:expires_in]) do
  124. {:ok, expires_at} -> %__MODULE__{draft | expires_at: expires_at}
  125. {:error, message} -> add_error(draft, message)
  126. end
  127. end
  128. defp poll(draft) do
  129. case Utils.make_poll_data(draft.params) do
  130. {:ok, {poll, poll_emoji}} ->
  131. %__MODULE__{draft | extra: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
  132. {:error, message} ->
  133. add_error(draft, message)
  134. end
  135. end
  136. defp content(draft) do
  137. {content_html, mentioned_users, tags} = Utils.make_content_html(draft)
  138. mentions =
  139. mentioned_users
  140. |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
  141. |> Utils.get_addressed_users(draft.params[:to])
  142. %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
  143. end
  144. defp to_and_cc(draft) do
  145. {to, cc} = Utils.get_to_and_cc(draft)
  146. %__MODULE__{draft | to: to, cc: cc}
  147. end
  148. defp context(draft) do
  149. context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
  150. %__MODULE__{draft | context: context}
  151. end
  152. defp sensitive(draft) do
  153. sensitive = draft.params[:sensitive]
  154. %__MODULE__{draft | sensitive: sensitive}
  155. end
  156. defp object(draft) do
  157. emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
  158. # Sometimes people create posts with subject containing emoji,
  159. # since subjects are usually copied this will result in a broken
  160. # subject when someone replies from an instance that does not have
  161. # the emoji or has it under different shortcode. This is an attempt
  162. # to mitigate this by copying emoji from inReplyTo if they are present
  163. # in the subject.
  164. summary_emoji =
  165. with %Activity{} <- draft.in_reply_to,
  166. %Object{data: %{"tag" => [_ | _] = tag}} <- Object.normalize(draft.in_reply_to) do
  167. Enum.reduce(tag, %{}, fn
  168. %{"type" => "Emoji", "name" => name, "icon" => %{"url" => url}}, acc ->
  169. if String.contains?(draft.summary, name) do
  170. Map.put(acc, name, url)
  171. else
  172. acc
  173. end
  174. _, acc ->
  175. acc
  176. end)
  177. else
  178. _ -> %{}
  179. end
  180. emoji = Map.merge(emoji, summary_emoji)
  181. object =
  182. Utils.make_note_data(draft)
  183. |> Map.put("emoji", emoji)
  184. |> Map.put("source", draft.status)
  185. |> Map.put("generator", draft.params[:generator])
  186. %__MODULE__{draft | object: object}
  187. end
  188. defp preview?(draft) do
  189. preview? = Pleroma.Web.ControllerHelper.truthy_param?(draft.params[:preview])
  190. %__MODULE__{draft | preview?: preview?}
  191. end
  192. defp changes(draft) do
  193. direct? = draft.visibility == "direct"
  194. additional = %{"cc" => draft.cc, "directMessage" => direct?}
  195. additional =
  196. case draft.expires_at do
  197. %DateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
  198. _ -> additional
  199. end
  200. changes =
  201. %{
  202. to: draft.to,
  203. actor: draft.user,
  204. context: draft.context,
  205. object: draft.object,
  206. additional: additional
  207. }
  208. |> Utils.maybe_add_list_data(draft.user, draft.visibility)
  209. %__MODULE__{draft | changes: changes}
  210. end
  211. defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
  212. defp with_valid(draft, _func), do: draft
  213. defp add_error(draft, message) do
  214. %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
  215. end
  216. defp validate(%{valid?: true} = draft), do: {:ok, draft}
  217. defp validate(%{errors: [message | _]}), do: {:error, message}
  218. end