diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mix/tasks/pleroma/database.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/activity.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/chat/message_reference.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/delivery.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/hashtag.ex | 5 | ||||
-rw-r--r-- | lib/pleroma/hashtag_object.ex | 17 | ||||
-rw-r--r-- | lib/pleroma/migrators/hashtags_table_migrator.ex | 3 | ||||
-rw-r--r-- | lib/pleroma/object.ex | 5 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 9 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/object_validators/common_fields.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/utils.ex | 31 | ||||
-rw-r--r-- | lib/pleroma/web/api_spec/schemas/status.ex | 4 | ||||
-rw-r--r-- | lib/pleroma/web/mastodon_api/views/status_view.ex | 14 |
13 files changed, 64 insertions, 34 deletions
diff --git a/lib/mix/tasks/pleroma/database.ex b/lib/mix/tasks/pleroma/database.ex index a973beaa9..41a36851e 100644 --- a/lib/mix/tasks/pleroma/database.ex +++ b/lib/mix/tasks/pleroma/database.ex @@ -33,7 +33,7 @@ defmodule Mix.Tasks.Pleroma.Database do Logger.info("Removing embedded objects") Repo.query!( - "update activities set data = safe_jsonb_set(data, '{object}'::text[], data->'object'->'id') where data->'object'->>'id' is not null;", + "update objects set data = safe_jsonb_set(data, '{object}'::text[], data->'object'->'id') where data->'object'->>'id' is not null;", [], timeout: :infinity ) diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index 4106feef6..30d8f7092 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -26,7 +26,7 @@ defmodule Pleroma.Activity do @cachex Pleroma.Config.get([:cachex, :provider], Cachex) - schema "activities" do + schema "objects" do field(:data, :map) field(:local, :boolean, default: true) field(:actor, :string) diff --git a/lib/pleroma/chat/message_reference.ex b/lib/pleroma/chat/message_reference.ex index 89537d155..06fdb3401 100644 --- a/lib/pleroma/chat/message_reference.ex +++ b/lib/pleroma/chat/message_reference.ex @@ -20,7 +20,7 @@ defmodule Pleroma.Chat.MessageReference do @primary_key {:id, FlakeId.Ecto.Type, autogenerate: true} schema "chat_message_references" do - belongs_to(:object, Object) + belongs_to(:object, Object, type: FlakeId.Ecto.CompatType) belongs_to(:chat, Chat, type: FlakeId.Ecto.CompatType) field(:unread, :boolean, default: true) diff --git a/lib/pleroma/delivery.ex b/lib/pleroma/delivery.ex index 511d5cf58..b1ed6a8fd 100644 --- a/lib/pleroma/delivery.ex +++ b/lib/pleroma/delivery.ex @@ -15,7 +15,7 @@ defmodule Pleroma.Delivery do schema "deliveries" do belongs_to(:user, User, type: FlakeId.Ecto.CompatType) - belongs_to(:object, Object) + belongs_to(:object, Object, type: FlakeId.Ecto.CompatType) end def changeset(delivery, params \\ %{}) do diff --git a/lib/pleroma/hashtag.ex b/lib/pleroma/hashtag.ex index 53e2e9c89..046c67943 100644 --- a/lib/pleroma/hashtag.ex +++ b/lib/pleroma/hashtag.ex @@ -10,13 +10,14 @@ defmodule Pleroma.Hashtag do alias Ecto.Multi alias Pleroma.Hashtag + alias Pleroma.HashtagObject alias Pleroma.Object alias Pleroma.Repo schema "hashtags" do field(:name, :string) - many_to_many(:objects, Object, join_through: "hashtags_objects", on_replace: :delete) + many_to_many(:objects, Object, join_through: HashtagObject, on_replace: :delete) timestamps() end @@ -80,7 +81,7 @@ defmodule Pleroma.Hashtag do def unlink(%Object{id: object_id}) do with {_, hashtag_ids} <- - from(hto in "hashtags_objects", + from(hto in HashtagObject, where: hto.object_id == ^object_id, select: hto.hashtag_id ) diff --git a/lib/pleroma/hashtag_object.ex b/lib/pleroma/hashtag_object.ex new file mode 100644 index 000000000..12b570715 --- /dev/null +++ b/lib/pleroma/hashtag_object.ex @@ -0,0 +1,17 @@ +defmodule Pleroma.HashtagObject do + @moduledoc """ + Through table relationship between hashtags and objects. + https://hexdocs.pm/ecto/polymorphic-associations-with-many-to-many.html + """ + use Ecto.Schema + + alias Pleroma.Hashtag + alias Pleroma.Object + + @primary_key false + + schema "hashtags_objects" do + belongs_to(:hashtag, Hashtag) + belongs_to(:object, Object, type: FlakeId.Ecto.CompatType) + end +end diff --git a/lib/pleroma/migrators/hashtags_table_migrator.ex b/lib/pleroma/migrators/hashtags_table_migrator.ex index b84058e11..3b8170c9f 100644 --- a/lib/pleroma/migrators/hashtags_table_migrator.ex +++ b/lib/pleroma/migrators/hashtags_table_migrator.ex @@ -13,6 +13,7 @@ defmodule Pleroma.Migrators.HashtagsTableMigrator do use Pleroma.Migrators.Support.BaseMigrator alias Pleroma.Hashtag + alias Pleroma.HashtagObject alias Pleroma.Migrators.Support.BaseMigrator alias Pleroma.Object @@ -120,7 +121,7 @@ defmodule Pleroma.Migrators.HashtagsTableMigrator do try do with {rows_count, _} when is_integer(rows_count) <- - Repo.insert_all("hashtags_objects", maps, on_conflict: :nothing) do + Repo.insert_all(HashtagObject, maps, on_conflict: :nothing) do object.id else e -> diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index c3ea1b98b..8569fed8c 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -11,6 +11,7 @@ defmodule Pleroma.Object do alias Pleroma.Activity alias Pleroma.Config alias Pleroma.Hashtag + alias Pleroma.HashtagObject alias Pleroma.Object alias Pleroma.Object.Fetcher alias Pleroma.ObjectTombstone @@ -22,6 +23,8 @@ defmodule Pleroma.Object do @type t() :: %__MODULE__{} + @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true} + @derive {Jason.Encoder, only: [:data]} @cachex Pleroma.Config.get([:cachex, :provider], Cachex) @@ -29,7 +32,7 @@ defmodule Pleroma.Object do schema "objects" do field(:data, :map) - many_to_many(:hashtags, Hashtag, join_through: "hashtags_objects", on_replace: :delete) + many_to_many(:hashtags, Hashtag, join_through: HashtagObject, on_replace: :delete) timestamps() end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 8324ca22c..3b7ff0554 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -11,6 +11,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do alias Pleroma.Conversation.Participation alias Pleroma.Filter alias Pleroma.Hashtag + alias Pleroma.HashtagObject alias Pleroma.Maps alias Pleroma.Notification alias Pleroma.Object @@ -775,8 +776,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do defp restrict_embedded_tag_reject_any(query, _), do: query defp object_ids_query_for_tags(tags) do - from(hto in "hashtags_objects") - |> join(:inner, [hto], ht in Pleroma.Hashtag, on: hto.hashtag_id == ht.id) + from(hto in HashtagObject) + |> join(:inner, [hto], ht in Hashtag, on: hto.hashtag_id == ht.id) |> where([hto, ht], ht.name in ^tags) |> select([hto], hto.object_id) |> distinct([hto], true) @@ -825,7 +826,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do # Note: NO extra ordering should be done on "activities.id desc nulls last" for optimal plan from( [_activity, object] in query, - join: hto in "hashtags_objects", + join: hto in HashtagObject, on: hto.object_id == object.id, where: hto.hashtag_id in ^hashtag_ids, distinct: [desc: object.id], @@ -1160,7 +1161,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do defp restrict_instance(query, %{instance: instance}) when is_binary(instance) do from( activity in query, - where: fragment("split_part(actor::text, '/'::text, 3) = ?", ^instance) + where: fragment("split_part(?::text, '/'::text, 3) = ?", activity.actor, ^instance) ) end diff --git a/lib/pleroma/web/activity_pub/object_validators/common_fields.ex b/lib/pleroma/web/activity_pub/object_validators/common_fields.ex index 872f80ec3..cb1f4e144 100644 --- a/lib/pleroma/web/activity_pub/object_validators/common_fields.ex +++ b/lib/pleroma/web/activity_pub/object_validators/common_fields.ex @@ -52,7 +52,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonFields do field(:context, :string) # short identifier for PleromaFE to group statuses by context - field(:context_id, :integer) + field(:context_id, :string) field(:sensitive, :boolean, default: false) field(:replies_count, :integer, default: 0) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 1df53f79a..9a45bb323 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -712,9 +712,10 @@ defmodule Pleroma.Web.ActivityPub.Utils do defp build_flag_object(%{statuses: statuses}) do Enum.map(statuses || [], &build_flag_object/1) + |> Enum.reject(&is_nil/1) end - defp build_flag_object(%Activity{data: %{"id" => id}, object: %{data: data}}) do + defp build_flag_object(%Activity{data: %{"id" => id, "type" => "Create"}, object: %{data: data}}) do activity_actor = User.get_by_ap_id(data["actor"]) %{ @@ -730,28 +731,26 @@ defmodule Pleroma.Web.ActivityPub.Utils do } end - defp build_flag_object(act) when is_map(act) or is_binary(act) do - id = - case act do - %Activity{} = act -> act.data["id"] - act when is_map(act) -> act["id"] - act when is_binary(act) -> act - end + defp build_flag_object(%{data: %{"id" => id}}), do: build_flag_object(id) + defp build_flag_object(%{"id" => id}), do: build_flag_object(id) - case Activity.get_by_ap_id_with_object(id) do - %Activity{} = activity -> + defp build_flag_object(ap_id) when is_binary(ap_id) do + case Activity.get_by_ap_id_with_object(ap_id) do + %Activity{data: %{"type" => "Create"}} = activity -> build_flag_object(activity) - nil -> - if activity = Activity.get_by_object_ap_id_with_object(id) do - build_flag_object(activity) - else - %{"id" => id, "deleted" => true} + _ -> + case Activity.get_by_object_ap_id_with_object(ap_id) do + %Activity{data: %{"type" => "Create"}} = activity -> + build_flag_object(activity) + + _ -> + %{"id" => ap_id, "deleted" => true} end end end - defp build_flag_object(_), do: [] + defp build_flag_object(_), do: nil #### Report-related helpers def get_reports(params, page, page_size) do diff --git a/lib/pleroma/web/api_spec/schemas/status.ex b/lib/pleroma/web/api_spec/schemas/status.ex index 3d042dc19..a12777068 100644 --- a/lib/pleroma/web/api_spec/schemas/status.ex +++ b/lib/pleroma/web/api_spec/schemas/status.ex @@ -143,7 +143,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Status do "A map consisting of alternate representations of the `content` property with the key being it's mimetype. Currently the only alternate representation supported is `text/plain`" }, conversation_id: %Schema{ - type: :integer, + type: :string, description: "The ID of the AP context the status is associated with (if any)" }, direct_conversation_id: %Schema{ @@ -317,7 +317,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Status do "pinned" => false, "pleroma" => %{ "content" => %{"text/plain" => "foobar"}, - "conversation_id" => 345_972, + "conversation_id" => "AEXFhY7X4zd8hZK8oK", "direct_conversation_id" => nil, "emoji_reactions" => [], "expires_at" => nil, diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 463f34198..ce350ad23 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -57,11 +57,19 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do end) end - defp get_context_id(%{data: %{"context_id" => context_id}}) when not is_nil(context_id), + defp get_context_id(%{data: %{"context_id" => context_id}}) when is_binary(context_id), do: context_id - defp get_context_id(%{data: %{"context" => context}}) when is_binary(context), - do: Utils.context_to_conversation_id(context) + defp get_context_id(%{data: %{"context_id" => context_id}}) when is_integer(context_id), + do: to_string(context_id) + + defp get_context_id(%{data: %{"context" => context}}) when is_binary(context) do + case Utils.context_to_conversation_id(context) do + id when is_binary(id) -> id + id when is_integer(id) -> to_string(id) + _ -> nil + end + end defp get_context_id(_), do: nil |