2019-11-05 14:02:31 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-11-05 14:02:31 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-10-21 08:23:10 +00:00
|
|
|
defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator do
|
2019-11-05 14:02:31 +00:00
|
|
|
use Ecto.Schema
|
|
|
|
|
2020-06-16 14:50:33 +00:00
|
|
|
alias Pleroma.EctoType.ActivityPub.ObjectValidators
|
2022-01-07 17:31:13 +00:00
|
|
|
alias Pleroma.Object.Fetcher
|
2020-08-20 18:03:07 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
|
|
|
|
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
|
2020-08-24 23:16:12 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
2019-11-05 14:02:31 +00:00
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
|
2022-01-07 16:51:04 +00:00
|
|
|
require Logger
|
|
|
|
|
2019-11-05 14:02:31 +00:00
|
|
|
@primary_key false
|
2020-08-20 18:03:07 +00:00
|
|
|
@derive Jason.Encoder
|
2019-11-05 14:02:31 +00:00
|
|
|
|
|
|
|
embedded_schema do
|
2021-04-01 11:26:32 +00:00
|
|
|
quote do
|
|
|
|
unquote do
|
|
|
|
import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
|
|
|
|
message_fields()
|
|
|
|
object_fields()
|
|
|
|
status_object_fields()
|
|
|
|
end
|
|
|
|
end
|
2020-09-10 09:11:10 +00:00
|
|
|
|
|
|
|
field(:replies, {:array, ObjectValidators.ObjectID}, default: [])
|
2022-06-14 09:56:18 +00:00
|
|
|
field(:source, :map)
|
2019-11-05 14:02:31 +00:00
|
|
|
end
|
|
|
|
|
2020-08-20 18:03:07 +00:00
|
|
|
def cast_and_apply(data) do
|
|
|
|
data
|
|
|
|
|> cast_data
|
|
|
|
|> apply_action(:insert)
|
|
|
|
end
|
|
|
|
|
2019-11-05 14:02:31 +00:00
|
|
|
def cast_and_validate(data) do
|
|
|
|
data
|
|
|
|
|> cast_data()
|
|
|
|
|> validate_data()
|
|
|
|
end
|
|
|
|
|
2020-08-20 18:03:07 +00:00
|
|
|
def cast_data(data) do
|
|
|
|
%__MODULE__{}
|
|
|
|
|> changeset(data)
|
|
|
|
end
|
|
|
|
|
2020-09-10 09:11:10 +00:00
|
|
|
defp fix_url(%{"url" => url} = data) when is_bitstring(url), do: data
|
|
|
|
defp fix_url(%{"url" => url} = data) when is_map(url), do: Map.put(data, "url", url["href"])
|
2020-08-20 18:03:07 +00:00
|
|
|
defp fix_url(data), do: data
|
|
|
|
|
2020-09-10 09:11:10 +00:00
|
|
|
defp fix_tag(%{"tag" => tag} = data) when is_list(tag), do: data
|
|
|
|
defp fix_tag(%{"tag" => tag} = data) when is_map(tag), do: Map.put(data, "tag", [tag])
|
|
|
|
defp fix_tag(data), do: Map.drop(data, ["tag"])
|
|
|
|
|
|
|
|
defp fix_replies(%{"replies" => %{"first" => %{"items" => replies}}} = data)
|
|
|
|
when is_list(replies),
|
|
|
|
do: Map.put(data, "replies", replies)
|
|
|
|
|
|
|
|
defp fix_replies(%{"replies" => %{"items" => replies}} = data) when is_list(replies),
|
|
|
|
do: Map.put(data, "replies", replies)
|
|
|
|
|
|
|
|
defp fix_replies(%{"replies" => replies} = data) when is_bitstring(replies),
|
|
|
|
do: Map.drop(data, ["replies"])
|
|
|
|
|
2022-01-07 16:51:04 +00:00
|
|
|
defp fix_replies(%{"replies" => %{"first" => first}} = data) do
|
|
|
|
with {:ok, %{"orderedItems" => replies}} <-
|
|
|
|
Fetcher.fetch_and_contain_remote_object_from_id(first) do
|
|
|
|
Map.put(data, "replies", replies)
|
|
|
|
else
|
2022-06-11 13:08:54 +00:00
|
|
|
{:error, _} ->
|
2022-01-07 16:51:04 +00:00
|
|
|
Logger.error("Could not fetch replies for #{first}")
|
|
|
|
Map.put(data, "replies", [])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-10 09:11:10 +00:00
|
|
|
defp fix_replies(data), do: data
|
|
|
|
|
2022-06-14 09:56:18 +00:00
|
|
|
# https://github.com/misskey-dev/misskey/pull/8787
|
2022-06-14 15:24:03 +00:00
|
|
|
defp fix_misskey_content(%{"source" => %{"mediaType" => "text/x.misskeymarkdown"}} = object),
|
|
|
|
do: object
|
2022-06-14 09:56:18 +00:00
|
|
|
|
|
|
|
defp fix_misskey_content(%{"_misskey_content" => content} = object) do
|
|
|
|
object
|
2022-06-14 15:24:03 +00:00
|
|
|
|> Map.put("source", %{"content" => content, "mediaType" => "text/x.misskeymarkdown"})
|
2022-06-14 09:56:18 +00:00
|
|
|
|> Map.delete("_misskey_content")
|
|
|
|
end
|
|
|
|
|
|
|
|
defp fix_misskey_content(data), do: data
|
|
|
|
|
2022-06-14 15:24:03 +00:00
|
|
|
defp fix_source(%{"source" => source} = object) when is_binary(source) do
|
|
|
|
object
|
|
|
|
|> Map.put("source", %{"content" => source})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp fix_source(object), do: object
|
|
|
|
|
2020-08-24 23:16:12 +00:00
|
|
|
defp fix(data) do
|
|
|
|
data
|
2020-08-20 18:03:07 +00:00
|
|
|
|> CommonFixes.fix_actor()
|
2020-09-10 09:08:05 +00:00
|
|
|
|> CommonFixes.fix_object_defaults()
|
2020-08-20 18:03:07 +00:00
|
|
|
|> fix_url()
|
2020-09-10 09:11:10 +00:00
|
|
|
|> fix_tag()
|
|
|
|
|> fix_replies()
|
2022-06-14 15:24:03 +00:00
|
|
|
|> fix_source()
|
2022-06-14 09:56:18 +00:00
|
|
|
|> fix_misskey_content()
|
2020-08-24 23:16:12 +00:00
|
|
|
|> Transmogrifier.fix_emoji()
|
2020-09-10 09:11:10 +00:00
|
|
|
|> Transmogrifier.fix_content_map()
|
2020-08-24 23:16:12 +00:00
|
|
|
end
|
|
|
|
|
2020-08-20 18:03:07 +00:00
|
|
|
def changeset(struct, data) do
|
2020-08-24 23:16:12 +00:00
|
|
|
data = fix(data)
|
|
|
|
|
2020-08-20 18:03:07 +00:00
|
|
|
struct
|
2021-01-07 17:23:01 +00:00
|
|
|
|> cast(data, __schema__(:fields) -- [:attachment, :tag])
|
2020-08-20 18:03:07 +00:00
|
|
|
|> cast_embed(:attachment)
|
2021-01-07 17:23:01 +00:00
|
|
|
|> cast_embed(:tag)
|
2019-11-05 14:02:31 +00:00
|
|
|
end
|
|
|
|
|
2021-01-12 10:14:09 +00:00
|
|
|
defp validate_data(data_cng) do
|
2019-11-05 14:02:31 +00:00
|
|
|
data_cng
|
2020-10-21 08:23:10 +00:00
|
|
|
|> validate_inclusion(:type, ["Article", "Note", "Page"])
|
2020-08-20 18:03:07 +00:00
|
|
|
|> validate_required([:id, :actor, :attributedTo, :type, :context, :context_id])
|
|
|
|
|> CommonValidations.validate_any_presence([:cc, :to])
|
|
|
|
|> CommonValidations.validate_fields_match([:actor, :attributedTo])
|
|
|
|
|> CommonValidations.validate_actor_presence()
|
|
|
|
|> CommonValidations.validate_host_match()
|
2019-11-05 14:02:31 +00:00
|
|
|
end
|
|
|
|
end
|