Add proper error handling for when the post exceeds character limits

This commit is contained in:
rinpatch 2019-06-18 05:05:05 +03:00
parent dce27de733
commit c4e4f7d0e4
2 changed files with 16 additions and 1 deletions

View File

@ -212,7 +212,7 @@ def post(user, %{"status" => status} = data) do
cw <- data["spoiler_text"] || "", cw <- data["spoiler_text"] || "",
sensitive <- data["sensitive"] || Enum.member?(tags, {"#nsfw", "nsfw"}), sensitive <- data["sensitive"] || Enum.member?(tags, {"#nsfw", "nsfw"}),
full_payload <- String.trim(status <> cw), full_payload <- String.trim(status <> cw),
length when length in 1..limit <- String.length(full_payload), :ok <- validate_character_limit(full_payload, attachments, limit),
object <- object <-
make_note_data( make_note_data(
user.ap_id, user.ap_id,
@ -247,6 +247,7 @@ def post(user, %{"status" => status} = data) do
res res
else else
{:error, _} = e -> e
e -> {:error, e} e -> {:error, e}
end end
end end

View File

@ -504,4 +504,18 @@ def make_answer_data(%User{ap_id: ap_id}, object, name) do
"inReplyTo" => object.data["id"] "inReplyTo" => object.data["id"]
} }
end end
def validate_character_limit(full_payload, attachments, limit) do
length = String.length(full_payload)
if length < limit do
if length > 0 or Enum.count(attachments) > 0 do
:ok
else
{:error, "Cannot post an empty status without attachments"}
end
else
{:error, "The status is over the character limit"}
end
end
end end