Skip to content

Commit

Permalink
Fix reply to anything
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed May 6, 2022
1 parent 4aa14b9 commit c718963
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/social/acts/threaded.ex
Expand Up @@ -7,7 +7,7 @@ defmodule Bonfire.Social.Acts.Threaded do
alias Ecto.Changeset
alias Pointers.Changesets
import Epics
import Where, only: [warn: 2]
import Where
use Arrows

def run(epic, act) do
Expand Down Expand Up @@ -62,15 +62,11 @@ defmodule Bonfire.Social.Acts.Threaded do
{:ok, %{}=reply_to} ->
# we're permitted to reply to the parent, but it appears to have no threading information.
debug(epic, act, "parent missing threading, creating as root")
replied_attrs = %{id: reply_to.id, thread_id: reply_to.id}
# pretend the replied already exists, because it will in a moment
replied = Changesets.set_state(struct(Replied, replied_attrs), :loaded)
reply_to = Map.put(reply_to, :replied, replied)

reply_to = init_replied(reply_to)

changeset
# HACK: inserts the parent replied in a nasty way to get around there being no way to do an
# insert across an association with `on_conflict: :ignore`
|> Changeset.prepare_changes(&Threads.create_parent_replied(&1, replied, replied_attrs))
|> put_replied(reply_to.id, reply_to.id)
|> put_replied(reply_to.id, reply_to)
|> Epic.assign(epic, on, ...)
|> Epic.assign(:reply_to, reply_to)
_ ->
Expand All @@ -90,4 +86,21 @@ defmodule Bonfire.Social.Acts.Threaded do
|> Changeset.update_change(:replied, &Replied.make_child_of(&1, reply_to.replied))
end

defp init_replied(reply_to) do
%Replied{id: reply_to.id, thread_id: reply_to.id}
|> Threads.init_parent_replied()
~> Map.put(reply_to, :replied, ...)
end
# defp init_replied(changeset, reply_to) do
# replied_attrs = %{id: reply_to.id, thread_id: reply_to.id}
# # pretend the replied already exists, because it will in a moment
# replied = Changesets.set_state(struct(Replied, replied_attrs), :loaded)
# reply_to = Map.put(reply_to, :replied, replied)
# |> debug("reply_to")

# # HACK: inserts the parent replied in a nasty way to get around there being no way to do an insert across an association with `on_conflict: :ignore`
# # FIXME: causes a `no case clause matching: :raise` error
# Changeset.prepare_changes(changeset, &Threads.create_parent_replied(&1, replied, replied_attrs))
# end

end
5 changes: 5 additions & 0 deletions lib/social/threads.ex
Expand Up @@ -92,6 +92,11 @@ defmodule Bonfire.Social.Threads do
end
end

@doc false
def init_parent_replied(replied_attrs) do # TODO: can we do this in the transaction?
repo().insert(replied_attrs, on_conflict: :nothing)
end

@doc false
def create_parent_replied(changeset, replied, replied_attrs) do
changeset.repo.insert_all(Replied, [replied_attrs], on_conflict: :nothing)
Expand Down

0 comments on commit c718963

Please sign in to comment.