From a9c0f395cb4d854e552a3205cb5b38ff610f8e27 Mon Sep 17 00:00:00 2001 From: Thurloat Date: Fri, 31 Aug 2018 14:28:39 -0300 Subject: [PATCH 1/2] add nil clause for Formatter.get_emoji/1 to return an empty result --- lib/pleroma/formatter.ex | 2 ++ test/formatter_test.exs | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index cf2944c38..fc2c643da 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -157,6 +157,8 @@ defmodule Pleroma.Formatter do end) end + def get_emoji(nil), do: [] + def get_emoji(text) do Enum.filter(@emoji, fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end) end diff --git a/test/formatter_test.exs b/test/formatter_test.exs index 95558089b..4e27efe06 100644 --- a/test/formatter_test.exs +++ b/test/formatter_test.exs @@ -199,4 +199,14 @@ defmodule Pleroma.FormatterTest do assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}] end + + test "it returns a nice empty result when no emojis are present" do + text = "I love moominamma" + assert Formatter.get_emoji(text) == [] + end + + test "it doesn't die when text is absent" do + text = nil + assert Formatter.get_emoji(text) == [] + end end From 4257f784bc8e742888e978fccbab0f566c549376 Mon Sep 17 00:00:00 2001 From: Thurloat Date: Sun, 2 Sep 2018 20:44:37 -0300 Subject: [PATCH 2/2] sloop around get_emoji/1 to check is_binary and have a fallthrough default that returns empty --- lib/pleroma/formatter.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index fc2c643da..e5ccc7a49 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -157,12 +157,12 @@ defmodule Pleroma.Formatter do end) end - def get_emoji(nil), do: [] - - def get_emoji(text) do + def get_emoji(text) when is_binary(text) do Enum.filter(@emoji, fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end) end + def get_emoji(_), do: [] + def get_custom_emoji() do @emoji end