Introduce get_by_id in Activity, replace newlines with spaces
[akkoma] / lib / pleroma / formatter.ex
index d80ae6576cd071b3e9b49a35a0bdb43c25d2699c..49f7075e6f1640667bae117e9feb7325ec0d44f1 100644 (file)
@@ -183,4 +183,22 @@ defmodule Pleroma.Formatter do
       String.replace(result_text, uuid, replacement)
     end)
   end
+
+  def truncate(text, opts \\ []) do
+    max_length = opts[:max_length] || 200
+    omission = opts[:omission] || "..."
+
+    cond do
+      not String.valid?(text) ->
+        text
+
+      String.length(text) < max_length ->
+        text
+
+      true ->
+        length_with_omission = max_length - String.length(omission)
+
+        "#{String.slice(text, 0, length_with_omission)}#{omission}"
+    end
+  end
 end