From ca860273a0ac32e55e51e093041c8158d7a34d11 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 21 Apr 2022 14:54:39 +0200 Subject: [PATCH 1/6] Exclude tags with more than 40 characters from being extracted. They are still in the HTML produced Closes #562 Signed-off-by: Thomas Citharel --- lib/federation/activity_pub/types/events.ex | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/federation/activity_pub/types/events.ex b/lib/federation/activity_pub/types/events.ex index 42d96d069..d6bc9113c 100644 --- a/lib/federation/activity_pub/types/events.ex +++ b/lib/federation/activity_pub/types/events.ex @@ -267,7 +267,15 @@ defmodule Mobilizon.Federation.ActivityPub.Types.Events do Map.merge(args, %{ description: description, mentions: mentions, - tags: tags + # Exclude tags with length > 40 + tags: + Enum.filter(tags, fn tag -> + case tag do + # For some reason transmogrifier gives us this + %{title: tag} -> String.length(tag) < 40 + tag -> String.length(tag) < 40 + end + end) }) else args From 7b3ce8d8124bf75f21b3ab4a192ae39d77fc7326 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 21 Apr 2022 14:55:30 +0200 Subject: [PATCH 2/6] Avoid duplicate tags with different casing Closes #562 Signed-off-by: Thomas Citharel --- lib/federation/activity_pub/types/events.ex | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/federation/activity_pub/types/events.ex b/lib/federation/activity_pub/types/events.ex index d6bc9113c..2016a417c 100644 --- a/lib/federation/activity_pub/types/events.ex +++ b/lib/federation/activity_pub/types/events.ex @@ -281,6 +281,21 @@ defmodule Mobilizon.Federation.ActivityPub.Types.Events do args end + # Make sure we don't have duplicate (with different casing) tags + args = + Map.update( + args, + :tags, + [], + &Enum.uniq_by(&1, fn tag -> + case tag do + # For some reason transmogrifier gives us this + %{title: tag} -> String.downcase(tag) + tag -> String.downcase(tag) + end + end) + ) + # Check that we can only allow anonymous participation if our instance allows it {_, options} = Map.get_and_update( From f23f43876363651cc9c54468f7522ad1adfcb737 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 21 Apr 2022 15:15:02 +0200 Subject: [PATCH 3/6] Add test for the hashtag special cases - Test that same hashtags with different casing are taken only once into account - Test that too long hashtags are not extracted from description Signed-off-by: Thomas Citharel --- test/graphql/resolvers/event_test.exs | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/test/graphql/resolvers/event_test.exs b/test/graphql/resolvers/event_test.exs index aeba6c2aa..7db786bca 100644 --- a/test/graphql/resolvers/event_test.exs +++ b/test/graphql/resolvers/event_test.exs @@ -808,6 +808,53 @@ defmodule Mobilizon.Web.Resolvers.EventTest do end end + describe "create_event/3 with special tags" do + test "same tags with different casing", %{conn: conn, actor: actor, user: user} do + begins_on = DateTime.utc_now() + + res = + conn + |> auth_conn(user) + |> AbsintheHelpers.graphql_query( + query: @create_event_mutation, + variables: %{ + title: "come to my event", + description: "it will be fine", + begins_on: "#{DateTime.add(begins_on, 3600 * 24)}", + organizer_actor_id: "#{actor.id}", + tags: ["Hello", "hello"] + } + ) + + assert res["error"] == nil + assert res["data"]["createEvent"]["tags"] == [%{"slug" => "hello", "title" => "Hello"}] + end + + test "too long tags", %{conn: conn, actor: actor, user: user} do + begins_on = DateTime.utc_now() + + res = + conn + |> auth_conn(user) + |> AbsintheHelpers.graphql_query( + query: @create_event_mutation, + variables: %{ + title: "come to my event", + description: + "

it will be fine, what do you think?
#Detected
#ThisIsAVeryLongHashTagThatWillNotBeDetectedByTheParser

", + begins_on: "#{DateTime.add(begins_on, 3600 * 24)}", + organizer_actor_id: "#{actor.id}" + } + ) + + assert res["error"] == nil + + assert res["data"]["createEvent"]["tags"] == [ + %{"slug" => "detected", "title" => "detected"} + ] + end + end + @update_event_mutation """ mutation updateEvent( $eventId: ID! From 274949146775ba8202a533ca35f3ac02ee63f432 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 21 Apr 2022 15:50:07 +0200 Subject: [PATCH 4/6] Fix invalid HTML (
inside