From e25f13582ea84ee2b2523096362a15ed46089c3f Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 30 Apr 2021 08:03:30 +0200 Subject: [PATCH 1/4] fix vue warnings Signed-off-by: Thomas Citharel --- js/src/components/Comment/CommentTree.vue | 7 ++++--- js/src/views/Group/Group.vue | 10 +++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/js/src/components/Comment/CommentTree.vue b/js/src/components/Comment/CommentTree.vue index 2e6b1db6..3f3c64e5 100644 --- a/js/src/components/Comment/CommentTree.vue +++ b/js/src/components/Comment/CommentTree.vue @@ -48,8 +48,9 @@ > {{ $t("Loading comments…") }}

- + -
+
{{ $t("No comments yet") }}
- +
diff --git a/js/src/views/Group/Group.vue b/js/src/views/Group/Group.vue index 0d3d5280..03c970b8 100644 --- a/js/src/views/Group/Group.vue +++ b/js/src/views/Group/Group.vue @@ -31,7 +31,11 @@ {{ $t("You have been removed from this group's members.") }} {{ @@ -735,6 +739,10 @@ export default class Group extends mixins(GroupMixin) { ); } + get isCurrentActorOnADifferentDomainThanGroup(): boolean { + return this.group.domain !== null; + } + get members(): IMember[] { return this.group.members.elements.filter( (member) => From 741a084f4a7f8ad95ea9cd789949819941eba56a Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 3 May 2021 12:36:22 +0200 Subject: [PATCH 2/4] Fix favicon alignment on resource item Signed-off-by: Thomas Citharel --- js/src/components/Resource/ResourceItem.vue | 1 - 1 file changed, 1 deletion(-) diff --git a/js/src/components/Resource/ResourceItem.vue b/js/src/components/Resource/ResourceItem.vue index f0d7c1a5..b47be214 100644 --- a/js/src/components/Resource/ResourceItem.vue +++ b/js/src/components/Resource/ResourceItem.vue @@ -127,7 +127,6 @@ a { white-space: nowrap; display: inline-block; font-weight: 500; - margin-bottom: 5px; color: $primary; overflow: hidden; text-overflow: ellipsis; From 46120b16b62e1930920e815cbcba30761d232c27 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 3 May 2021 12:36:46 +0200 Subject: [PATCH 3/4] Fix merging URIs for media from url when doing a rich media preview Signed-off-by: Thomas Citharel --- lib/service/rich_media/favicon.ex | 16 ++++---------- lib/service/rich_media/parser.ex | 36 +++++++++++++++++-------------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/lib/service/rich_media/favicon.ex b/lib/service/rich_media/favicon.ex index ddbbb269..bdb146cf 100644 --- a/lib/service/rich_media/favicon.ex +++ b/lib/service/rich_media/favicon.ex @@ -56,20 +56,12 @@ defmodule Mobilizon.Service.RichMedia.Favicon do @spec format_url(String.t(), String.t()) :: String.t() defp format_url(url, path) do - image_uri = URI.parse(path) - uri = URI.parse(url) - - cond do - is_nil(image_uri.host) -> "#{uri.scheme}://#{uri.host}#{correct_path(path)}" - is_nil(image_uri.scheme) -> "#{uri.scheme}:#{path}" - true -> path - end + url + |> URI.parse() + |> URI.merge(path) + |> to_string() end - # Sometimes paths have "/" in front, sometimes not - defp correct_path("/" <> _ = path), do: path - defp correct_path(path), do: "/#{path}" - @spec find_favicon_link_tag(String.t()) :: {:ok, tuple()} | {:error, any()} defp find_favicon_link_tag(html) do with {:ok, html} <- Floki.parse_document(html), diff --git a/lib/service/rich_media/parser.ex b/lib/service/rich_media/parser.ex index 1d45b53d..87974148 100644 --- a/lib/service/rich_media/parser.ex +++ b/lib/service/rich_media/parser.ex @@ -19,6 +19,7 @@ defmodule Mobilizon.Service.RichMedia.Parser do alias Mobilizon.Config alias Mobilizon.Service.HTTP.RichMediaPreviewClient alias Mobilizon.Service.RichMedia.Favicon + alias Mobilizon.Service.RichMedia.Parsers.Fallback alias Plug.Conn.Utils require Logger @@ -211,9 +212,17 @@ defmodule Mobilizon.Service.RichMedia.Parser do data end - defp check_parsed_data(data) do - Logger.debug("Found metadata was invalid or incomplete: #{inspect(data)}") - {:error, :invalid_parsed_data} + defp check_parsed_data(data, html, first_run) do + # Maybe the first data found is incomplete, pass it through the Fallback parser once again + if first_run do + {:ok, data} = Fallback.parse(html, data) + Logger.debug("check parsed data") + Logger.debug(inspect(data)) + check_parsed_data(data, html, false) + else + Logger.debug("Found metadata was invalid or incomplete: #{inspect(data)}") + {:error, :invalid_parsed_data} + end end defp clean_parsed_data(data) do @@ -280,25 +289,20 @@ defmodule Mobilizon.Service.RichMedia.Parser do @spec check_remote_picture_path(map()) :: map() defp check_remote_picture_path(%{image_remote_url: image_remote_url, url: url} = data) do Logger.debug("Checking image_remote_url #{image_remote_url}") - image_uri = URI.parse(image_remote_url) - uri = URI.parse(url) - image_remote_url = - cond do - is_nil(image_uri.host) -> "#{uri.scheme}://#{uri.host}#{correct_path(image_remote_url)}" - is_nil(image_uri.scheme) -> "#{uri.scheme}:#{image_remote_url}" - true -> image_remote_url - end - - data = Map.put(data, :image_remote_url, image_remote_url) + data = Map.put(data, :image_remote_url, format_url(url, image_remote_url)) {:ok, data} end defp check_remote_picture_path(data), do: {:ok, data} - # Sometimes paths have "/" in front, sometimes not - defp correct_path("/" <> _ = path), do: path - defp correct_path(path), do: "/#{path}" + @spec format_url(String.t(), String.t()) :: String.t() + defp format_url(url, path) do + url + |> URI.parse() + |> URI.merge(path) + |> to_string() + end # Twitter requires a well-know crawler user-agent to show server-rendered data defp default_user_agent("https://twitter.com/" <> _) do From 5b36e71581bb5583a842e429f71061bbdf05b675 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 3 May 2021 14:52:37 +0200 Subject: [PATCH 4/4] Fix rich media parsers Signed-off-by: Thomas Citharel --- .../__snapshots__/CommentTree.spec.ts.snap | 8 +++---- lib/service/rich_media/parser.ex | 10 ++++----- lib/service/rich_media/parsers/fallback.ex | 12 ++++++++-- .../rich_media/parsers/meta_tags_parser.ex | 22 ++++++++++++++----- .../rich_media/parsers/oembed_parser.ex | 4 +++- 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/js/tests/unit/specs/components/Comment/__snapshots__/CommentTree.spec.ts.snap b/js/tests/unit/specs/components/Comment/__snapshots__/CommentTree.spec.ts.snap index 6754048a..f0000571 100644 --- a/js/tests/unit/specs/components/Comment/__snapshots__/CommentTree.spec.ts.snap +++ b/js/tests/unit/specs/components/Comment/__snapshots__/CommentTree.spec.ts.snap @@ -3,25 +3,25 @@ exports[`CommentTree renders a comment tree 1`] = `
- +
No comments yet
-
+
`; exports[`CommentTree renders a comment tree 2`] = `
- +
No comments yet
-
+
`; diff --git a/lib/service/rich_media/parser.ex b/lib/service/rich_media/parser.ex index 87974148..6ffcb2d5 100644 --- a/lib/service/rich_media/parser.ex +++ b/lib/service/rich_media/parser.ex @@ -74,12 +74,11 @@ defmodule Mobilizon.Service.RichMedia.Parser do {:is_html, _response_headers, true} <- {:is_html, response_headers, is_html(response_headers)} do body - |> parse_html() |> maybe_parse() |> Map.put(:url, url) |> maybe_add_favicon() |> clean_parsed_data() - |> check_parsed_data() + |> check_parsed_data(body) |> check_remote_picture_path() else {:is_html, response_headers, false} -> @@ -193,8 +192,7 @@ defmodule Mobilizon.Service.RichMedia.Parser do end end - defp parse_html(html), do: Floki.parse_document!(html) - + @spec maybe_parse(String.t()) :: {:halt, map()} | {:cont, map()} defp maybe_parse(html) do Enum.reduce_while(parsers(), %{}, fn parser, acc -> case parser.parse(html, acc) do @@ -207,7 +205,9 @@ defmodule Mobilizon.Service.RichMedia.Parser do end) end - defp check_parsed_data(%{title: title} = data) + defp check_parsed_data(data, html, first_run \\ true) + + defp check_parsed_data(%{title: title} = data, _html, _first_run) when is_binary(title) and byte_size(title) > 0 do data end diff --git a/lib/service/rich_media/parsers/fallback.ex b/lib/service/rich_media/parsers/fallback.ex index 5c2d8d57..2a92113f 100644 --- a/lib/service/rich_media/parsers/fallback.ex +++ b/lib/service/rich_media/parsers/fallback.ex @@ -29,11 +29,19 @@ defmodule Mobilizon.Service.RichMedia.Parsers.Fallback do end defp get_page(html, :title) do - html |> Floki.find("html head title") |> List.first() |> Floki.text() |> String.trim() + html + |> Floki.parse_document!() + |> Floki.find("html title") + |> List.first() + |> Floki.text() + |> String.trim() end defp get_page(html, :description) do - case html |> Floki.find("html head meta[name='description']") |> List.first() do + case html + |> Floki.parse_document!() + |> Floki.find("html meta[name='description']") + |> List.first() do nil -> "" elem -> elem |> Floki.attribute("content") |> List.first() |> String.trim() end diff --git a/lib/service/rich_media/parsers/meta_tags_parser.ex b/lib/service/rich_media/parsers/meta_tags_parser.ex index 316e444c..768baffa 100644 --- a/lib/service/rich_media/parsers/meta_tags_parser.ex +++ b/lib/service/rich_media/parsers/meta_tags_parser.ex @@ -36,7 +36,7 @@ defmodule Mobilizon.Service.RichMedia.Parsers.MetaTagsParser do end defp get_elements(html, key_name, prefix) do - html |> Floki.find("meta[#{to_string(key_name)}^='#{prefix}:']") + html |> Floki.parse_document!() |> Floki.find("meta[#{to_string(key_name)}^='#{prefix}:']") end defp normalize_attributes(html_node, prefix, key_name, value_name, allowed_attributes) do @@ -83,14 +83,26 @@ defmodule Mobilizon.Service.RichMedia.Parsers.MetaTagsParser do defp maybe_put_description(meta, _), do: meta + @spec get_page_title(String.t()) :: String.t() defp get_page_title(html) do - html |> Floki.find("html head title") |> List.first() |> Floki.text() + with {:ok, document} <- Floki.parse_document(html), + elem when not is_nil(elem) <- document |> Floki.find("html head title") |> List.first(), + title when is_binary(title) <- Floki.text(elem) do + title + else + _ -> "" + end end + @spec get_page_description(String.t()) :: String.t() defp get_page_description(html) do - case html |> Floki.find("html head meta[name='description']") |> List.first() do - nil -> "" - elem -> Floki.attribute(elem, "content") + with {:ok, document} <- Floki.parse_document(html), + elem when not is_nil(elem) <- + document |> Floki.find("html head meta[name='description']") |> List.first(), + description when is_binary(description) <- Floki.attribute(elem, "content") do + description + else + _ -> "" end end end diff --git a/lib/service/rich_media/parsers/oembed_parser.ex b/lib/service/rich_media/parsers/oembed_parser.ex index ce450d0d..56727747 100644 --- a/lib/service/rich_media/parsers/oembed_parser.ex +++ b/lib/service/rich_media/parsers/oembed_parser.ex @@ -32,7 +32,9 @@ defmodule Mobilizon.Service.RichMedia.Parsers.OEmbed do end defp get_discovery_data(html) do - html |> Floki.find("link[type='application/json+oembed']") + with {:ok, document} <- Floki.parse_document(html) do + Floki.find(document, "link[type='application/json+oembed']") + end end defp get_oembed_url(nodes) do