Add ActivityDraft.with_valid/2

This commit is contained in:
Egor Kislitsyn 2019-09-23 18:52:41 +07:00
parent eed774d058
commit de3e90e536

View File

@ -40,20 +40,20 @@ def create(user, params) do
|> put_params(params) |> put_params(params)
|> status() |> status()
|> summary() |> summary()
|> attachments()
|> full_payload() |> full_payload()
|> in_reply_to()
|> in_reply_to_conversation()
|> visibility()
|> expires_at() |> expires_at()
|> poll() |> poll()
|> with_valid(&in_reply_to/1)
|> with_valid(&attachments/1)
|> with_valid(&in_reply_to_conversation/1)
|> with_valid(&visibility/1)
|> content() |> content()
|> to_and_cc() |> with_valid(&to_and_cc/1)
|> context() |> with_valid(&context/1)
|> sensitive() |> sensitive()
|> object() |> with_valid(&object/1)
|> preview?() |> preview?()
|> changes() |> with_valid(&changes/1)
|> validate() |> validate()
end end
@ -136,8 +136,6 @@ defp content(draft) do
%__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags} %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
end end
defp to_and_cc(%{valid?: false} = draft), do: draft
defp to_and_cc(draft) do defp to_and_cc(draft) do
addressed_users = addressed_users =
draft.mentions draft.mentions
@ -166,8 +164,6 @@ defp sensitive(draft) do
%__MODULE__{draft | sensitive: sensitive} %__MODULE__{draft | sensitive: sensitive}
end end
defp object(%{valid?: false} = draft), do: draft
defp object(draft) do defp object(draft) do
emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji) emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
@ -195,8 +191,6 @@ defp preview?(draft) do
%__MODULE__{draft | preview?: preview?} %__MODULE__{draft | preview?: preview?}
end end
defp changes(%{valid?: false} = draft), do: draft
defp changes(draft) do defp changes(draft) do
direct? = draft.visibility == "direct" direct? = draft.visibility == "direct"
@ -213,6 +207,9 @@ defp changes(draft) do
%__MODULE__{draft | changes: changes} %__MODULE__{draft | changes: changes}
end end
defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
defp with_valid(draft, _func), do: draft
defp add_error(draft, message) do defp add_error(draft, message) do
%__MODULE__{draft | valid?: false, errors: [message | draft.errors]} %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
end end