From 2bc3505581e60b7c41225fbcd3798d64aa64f219 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 18 Dec 2021 15:27:05 -0500 Subject: Create HashtagObject schema Without it we can't specify that the object's ID is a FlakeId --- lib/pleroma/hashtag.ex | 5 +++-- lib/pleroma/hashtag_object.ex | 17 +++++++++++++++++ lib/pleroma/migrators/hashtags_table_migrator.ex | 3 ++- lib/pleroma/object.ex | 3 ++- lib/pleroma/web/activity_pub/activity_pub.ex | 7 ++++--- 5 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 lib/pleroma/hashtag_object.ex 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 cbb694408..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 @@ -31,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..9f51a3062 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], -- cgit v1.2.3