Skip to content

Commit

Permalink
https://github.com/bonfire-networks/bonfire-app/issues/1051
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Oct 22, 2024
1 parent 98e55b4 commit 6096d05
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 46 deletions.
2 changes: 1 addition & 1 deletion deps.hex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
paper_trail = "~> 1.1.2"
# paper_trail = "~> 1.1.2"
nimble_csv = "~> 1.2"
faker = "~> 0.14" # fake data generation
exto = "~> 0.3"
Expand Down
118 changes: 73 additions & 45 deletions lib/post_contents.ex
Original file line number Diff line number Diff line change
Expand Up @@ -401,36 +401,42 @@ defmodule Bonfire.Social.PostContents do
end
end

def get_versions(post_content) do
PaperTrail.get_versions(post_content)
|> repo().maybe_preload(user: [:profile, :character])
|> Enum.map_reduce(nil, fn
current, nil ->
current = %{
editor: current.user,
edited_at: current.inserted_at,
current_version: current.item_changes,
previous_version: %{}
}

{current, current}

current, %{current_version: previous_version} ->
current = %{
editor: current.user,
edited_at: current.inserted_at,
current_version: Map.merge(previous_version, current.item_changes),
previous_version: previous_version
}

{current, current}
end)
|> elem(0)
|> debug("vvv")
end

def get_versions_diffed(post_content) do
for %{} = version <- get_versions(post_content) do
def versioning_enabled?(opts), do: module_enabled?(PaperTrail, opts)

def get_versions(post_content, opts) do
if versioning_enabled?(opts) do
PaperTrail.get_versions(post_content)
|> repo().maybe_preload(user: [:profile, :character])
|> Enum.map_reduce(nil, fn
current, nil ->
current = %{
editor: current.user,
edited_at: current.inserted_at,
current_version: current.item_changes,
previous_version: %{}
}

{current, current}

current, %{current_version: previous_version} ->
current = %{
editor: current.user,
edited_at: current.inserted_at,
current_version: Map.merge(previous_version, current.item_changes),
previous_version: previous_version
}

{current, current}
end)
|> elem(0)
|> debug("vvv")
else
[]
end
end

def get_versions_diffed(post_content, opts) do
for %{} = version <- get_versions(post_content, opts) do
# TODO: make more flexble so the fields aren't hardcoded and put somewhere reusable by other objects/mixins

prev_length =
Expand Down Expand Up @@ -500,34 +506,56 @@ defmodule Bonfire.Social.PostContents do
with post_content <-
post_content
|> repo().maybe_preload(:created),
#  create the v1 entry if this is the first edir
:ok <-
PaperTrail.initialise(post_content,
user: %{id: e(post_content, :created, :creator_id, nil)}
),
{:ok, %{model: updated}} <-
changeset =
attrs
# TODO: apply the preparation/sanitation functions?
|> debug()
|> PostContent.changeset(post_content, ...)
# |> debug()
|> PaperTrail.update(user: current_user)
|> PostContent.changeset(post_content, ...),
#  create the v1 entry if this is the first edir
{:ok, updated_post_content} <-
save_edit(current_user, post_content, changeset)
|> debug() do
post =
Bonfire.Common.Needles.get(id(post_content),
current_user: current_user,
verbs: [:edit]
)
~> Map.put(:post_content, updated_post_content)

if Social.federate_outgoing?(current_user),
do:
Bonfire.Common.Needles.get(id(post_content),
current_user: current_user,
verbs: [:edit]
)
~> Map.put(:post_content, updated)
post
|> Social.maybe_federate(
current_user,
:edit,
...,
nil
)

{:ok, updated}
{:ok, post}
# {:ok, updated_post_content}
end
end

defp save_edit(current_user, %PostContent{} = post_content, changeset) do
if versioning_enabled?(current_user) do
#  create the v1 entry if this is the first edit
with :ok <-
PaperTrail.initialise(post_content,
user: %{id: e(post_content, :created, :creator_id, nil) || uid(current_user)}
),
# update the PostContent
{:ok, %{model: updated}} <-
PaperTrail.update(changeset, user: current_user)
|> debug() do
{:ok, updated}
end
else
with {:ok, updated} <-
repo().update(changeset, user: current_user)
|> debug() do
{:ok, updated}
end
end
end

Expand Down

0 comments on commit 6096d05

Please sign in to comment.