Add proper error handling for when the post exceeds character limits
authorrinpatch <rinpatch@sdf.org>
Tue, 18 Jun 2019 02:05:05 +0000 (05:05 +0300)
committerrinpatch <rinpatch@sdf.org>
Tue, 18 Jun 2019 02:05:05 +0000 (05:05 +0300)
lib/pleroma/web/common_api/common_api.ex
lib/pleroma/web/common_api/utils.ex

index f5193512ef98a0da0cf94d8a7c8a56aed035c706..42b78494dd18e42801577d49be5579bf4a9e2017 100644 (file)
@@ -212,7 +212,7 @@ defmodule Pleroma.Web.CommonAPI do
          cw <- data["spoiler_text"] || "",
          sensitive <- data["sensitive"] || Enum.member?(tags, {"#nsfw", "nsfw"}),
          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 <-
            make_note_data(
              user.ap_id,
@@ -247,6 +247,7 @@ defmodule Pleroma.Web.CommonAPI do
 
       res
     else
+      {:error, _} = e -> e
       e -> {:error, e}
     end
   end
index 6d82c0bd266cdf79abfce54f9c1c4b6a72384e33..8b9477927c84c3b5ae7f094a764810863358b88b 100644 (file)
@@ -504,4 +504,18 @@ defmodule Pleroma.Web.CommonAPI.Utils do
       "inReplyTo" => object.data["id"]
     }
   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