diff --git a/lib/philomena/notifications/notification.ex b/lib/philomena/notifications/notification.ex index 15632554..ad9c7d02 100644 --- a/lib/philomena/notifications/notification.ex +++ b/lib/philomena/notifications/notification.ex @@ -11,6 +11,9 @@ defmodule Philomena.Notifications.Notification do field :actor_child_id, :integer field :actor_child_type, :string + field :actor, :any, virtual: true + field :actor_child, :any, virtual: true + timestamps(inserted_at: :created_at) end diff --git a/lib/philomena/polymorphic.ex b/lib/philomena/polymorphic.ex new file mode 100644 index 00000000..eb2fcd43 --- /dev/null +++ b/lib/philomena/polymorphic.ex @@ -0,0 +1,53 @@ +defmodule Philomena.Polymorphic do + alias Philomena.Repo + import Ecto.Query + + @classes %{ + "Channel" => Philomena.Channels.Channel, + "Comment" => Philomena.Comments.Comment, + "Commission" => Philomena.Commissions.Commission, + "Conversation" => Philomena.Conversations.Conversation, + "Filter" => Philomena.Filters.Filter, + "Forum" => Philomena.Forums.Forum, + "Gallery" => Philomena.Galleries.Gallery, + "Image" => Philomena.Images.Image, + "LivestreamChannel" => Philomena.Channels.Channel, + "Post" => Philomena.Posts.Post, + "Topic" => Philomena.Topics.Topic, + "User" => Philomena.Users.User + } + + # Deal with Rails polymorphism BS + def load_polymorphic(structs, associations) when is_list(associations) do + Enum.reduce(associations, structs, fn asc, acc -> load_polymorphic(acc, asc) end) + end + + def load_polymorphic(structs, {name, {id, type}}) do + modules_and_ids = + structs + |> Enum.group_by(& &1[type], & &1[id]) + + loaded_rows = + modules_and_ids + |> Map.new(fn + {nil, _ids} -> + {nil, []} + + {type, ids} -> + rows = + @classes[type] + |> where([m], m.id in ^ids) + |> Repo.all() + |> Map.new(fn r -> {r.id, r} end) + + {type, rows} + end) + + structs + |> Enum.map(fn struct -> + row = loaded_rows[struct[type]][struct[id]] + + %{struct | name => row} + end) + end +end \ No newline at end of file diff --git a/lib/philomena/reports/report.ex b/lib/philomena/reports/report.ex index 4f04cddc..46372532 100644 --- a/lib/philomena/reports/report.ex +++ b/lib/philomena/reports/report.ex @@ -18,6 +18,8 @@ defmodule Philomena.Reports.Report do field :reportable_id, :integer field :reportable_type, :string + field :reportable, :any, virtual: true + timestamps(inserted_at: :created_at) end diff --git a/lib/philomena_web/templates/notification/_notification.html.slime b/lib/philomena_web/templates/notification/_notification.html.slime new file mode 100644 index 00000000..e69de29b