Resources fixes and improvements
- Fix getting page description - Fix fetching metadata from Twitter (thx @marienfressinaud) - Improve error handling Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
50c89e21da
commit
7b9910f251
@ -190,6 +190,9 @@
|
|||||||
<b-modal :active.sync="createLinkResourceModal" has-modal-card>
|
<b-modal :active.sync="createLinkResourceModal" has-modal-card>
|
||||||
<div class="modal-card">
|
<div class="modal-card">
|
||||||
<section class="modal-card-body">
|
<section class="modal-card-body">
|
||||||
|
<b-message type="is-danger" v-if="modalError">
|
||||||
|
{{ modalError }}
|
||||||
|
</b-message>
|
||||||
<form @submit.prevent="createResource">
|
<form @submit.prevent="createResource">
|
||||||
<b-field :label="$t('URL')">
|
<b-field :label="$t('URL')">
|
||||||
<b-input
|
<b-input
|
||||||
@ -317,6 +320,8 @@ export default class Resources extends Mixins(ResourceMixin) {
|
|||||||
|
|
||||||
renameModal = false;
|
renameModal = false;
|
||||||
|
|
||||||
|
modalError = "";
|
||||||
|
|
||||||
groupObject: Record<string, unknown> = {
|
groupObject: Record<string, unknown> = {
|
||||||
name: "resources",
|
name: "resources",
|
||||||
pull: "clone",
|
pull: "clone",
|
||||||
@ -341,6 +346,7 @@ export default class Resources extends Mixins(ResourceMixin) {
|
|||||||
|
|
||||||
async createResource(): Promise<void> {
|
async createResource(): Promise<void> {
|
||||||
if (!this.resource.actor) return;
|
if (!this.resource.actor) return;
|
||||||
|
this.modalError = "";
|
||||||
try {
|
try {
|
||||||
await this.$apollo.mutate({
|
await this.$apollo.mutate({
|
||||||
mutation: CREATE_RESOURCE,
|
mutation: CREATE_RESOURCE,
|
||||||
@ -364,10 +370,13 @@ export default class Resources extends Mixins(ResourceMixin) {
|
|||||||
this.newResource.resourceUrl = "";
|
this.newResource.resourceUrl = "";
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
this.modalError = err.graphQLErrors[0].message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async previewResource(): Promise<void> {
|
async previewResource(): Promise<void> {
|
||||||
|
this.modalError = "";
|
||||||
|
try {
|
||||||
if (this.newResource.resourceUrl === "") return;
|
if (this.newResource.resourceUrl === "") return;
|
||||||
const { data } = await this.$apollo.mutate({
|
const { data } = await this.$apollo.mutate({
|
||||||
mutation: PREVIEW_RESOURCE_LINK,
|
mutation: PREVIEW_RESOURCE_LINK,
|
||||||
@ -379,6 +388,10 @@ export default class Resources extends Mixins(ResourceMixin) {
|
|||||||
this.newResource.summary = data.previewResourceLink.description;
|
this.newResource.summary = data.previewResourceLink.description;
|
||||||
this.newResource.metadata = data.previewResourceLink;
|
this.newResource.metadata = data.previewResourceLink;
|
||||||
this.newResource.type = "link";
|
this.newResource.type = "link";
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
this.modalError = err.graphQLErrors[0].message;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
createSentenceForType(type: string): string {
|
createSentenceForType(type: string): string {
|
||||||
|
@ -118,6 +118,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do
|
|||||||
) do
|
) do
|
||||||
{:ok, resource}
|
{:ok, resource}
|
||||||
else
|
else
|
||||||
|
{:error, _} ->
|
||||||
|
{:error, dgettext("errors", "Error while creating resource")}
|
||||||
|
|
||||||
{:own_check, _} ->
|
{:own_check, _} ->
|
||||||
{:error, dgettext("errors", "Parent resource doesn't belong to this group")}
|
{:error, dgettext("errors", "Parent resource doesn't belong to this group")}
|
||||||
|
|
||||||
@ -201,8 +204,12 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do
|
|||||||
{:ok, data} when is_map(data) ->
|
{:ok, data} when is_map(data) ->
|
||||||
{:ok, struct(Metadata, data)}
|
{:ok, struct(Metadata, data)}
|
||||||
|
|
||||||
{:error, _err} ->
|
{:error, :invalid_parsed_data} ->
|
||||||
|
{:error, dgettext("errors", "Unable to fetch resource details from this URL.")}
|
||||||
|
|
||||||
|
{:error, err} ->
|
||||||
Logger.warn("Error while fetching preview from #{inspect(resource_url)}")
|
Logger.warn("Error while fetching preview from #{inspect(resource_url)}")
|
||||||
|
Logger.debug(inspect(err))
|
||||||
{:error, :unknown_resource}
|
{:error, :unknown_resource}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -57,7 +57,7 @@ defmodule Mobilizon.Service.RichMedia.Parser do
|
|||||||
|
|
||||||
@spec parse_url(String.t(), Enum.t()) :: {:ok, map()} | {:error, any()}
|
@spec parse_url(String.t(), Enum.t()) :: {:ok, map()} | {:error, any()}
|
||||||
defp parse_url(url, options \\ []) do
|
defp parse_url(url, options \\ []) do
|
||||||
user_agent = Keyword.get(options, :user_agent, Config.instance_user_agent())
|
user_agent = Keyword.get(options, :user_agent, default_user_agent(url))
|
||||||
headers = [{"User-Agent", user_agent}]
|
headers = [{"User-Agent", user_agent}]
|
||||||
Logger.debug("Fetching content at address #{inspect(url)}")
|
Logger.debug("Fetching content at address #{inspect(url)}")
|
||||||
|
|
||||||
@ -212,7 +212,8 @@ defmodule Mobilizon.Service.RichMedia.Parser do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp check_parsed_data(data) do
|
defp check_parsed_data(data) do
|
||||||
{:error, "Found metadata was invalid or incomplete: #{inspect(data)}"}
|
Logger.debug("Found metadata was invalid or incomplete: #{inspect(data)}")
|
||||||
|
{:error, :invalid_parsed_data}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp clean_parsed_data(data) do
|
defp clean_parsed_data(data) do
|
||||||
@ -293,4 +294,17 @@ defmodule Mobilizon.Service.RichMedia.Parser do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp check_remote_picture_path(data), do: data
|
defp check_remote_picture_path(data), do: data
|
||||||
|
|
||||||
|
# Twitter requires a well-know crawler user-agent to show server-rendered data
|
||||||
|
defp default_user_agent("https://twitter.com/" <> _) do
|
||||||
|
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
|
||||||
|
end
|
||||||
|
|
||||||
|
defp default_user_agent("https://mobile.twitter.com/" <> _) do
|
||||||
|
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
|
||||||
|
end
|
||||||
|
|
||||||
|
defp default_user_agent(_url) do
|
||||||
|
Config.instance_user_agent()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -70,8 +70,14 @@ defmodule Mobilizon.Service.RichMedia.Parsers.MetaTagsParser do
|
|||||||
|
|
||||||
defp maybe_put_description(meta, html) when meta != %{} do
|
defp maybe_put_description(meta, html) when meta != %{} do
|
||||||
case get_page_description(html) do
|
case get_page_description(html) do
|
||||||
"" -> meta
|
"" ->
|
||||||
description -> Map.put_new(meta, :description, description)
|
meta
|
||||||
|
|
||||||
|
descriptions when is_list(descriptions) and length(descriptions) > 0 ->
|
||||||
|
Map.put_new(meta, :description, hd(descriptions))
|
||||||
|
|
||||||
|
description ->
|
||||||
|
Map.put_new(meta, :description, description)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user