Update Author to sync only a new non-local data.
This commit is contained in:
@@ -98,6 +98,7 @@ defmodule DecentralisedBookIndex.Metadata do
|
||||
define :search_author, action: :search, args: [:name]
|
||||
define :update_author, action: :update
|
||||
define :assign_author_avatar_image, args: [:avatar_url], action: :assign_avatar_image
|
||||
define :assign_author_dbi_server, args: [:dbi_server_id], action: :assign_dbi_server_id
|
||||
define :destroy_author, action: :destroy
|
||||
end
|
||||
|
||||
|
||||
@@ -195,6 +195,10 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
update :assign_avatar_image do
|
||||
accept [:avatar_url]
|
||||
end
|
||||
|
||||
update :assign_dbi_server_id do
|
||||
accept [:dbi_server_id]
|
||||
end
|
||||
end
|
||||
|
||||
policies do
|
||||
|
||||
@@ -8,15 +8,17 @@ defmodule DecentralisedBookIndex.Sync.AuthorSync do
|
||||
|
||||
case Metadata.get_author_by_id(attrs.id) do
|
||||
{:ok, author} ->
|
||||
attrs =
|
||||
attrs
|
||||
|> Map.delete(:id)
|
||||
|> update_registry(registry_info)
|
||||
|> Map.put(:dbi_server_id, server_id)
|
||||
if not is_local(author) and is_new(attrs, author) do
|
||||
attrs =
|
||||
attrs
|
||||
|> Map.delete(:id)
|
||||
|> update_registry(registry_info)
|
||||
|> Map.put_new(:dbi_server_id, server_id)
|
||||
|
||||
author
|
||||
|> Ash.Changeset.for_update(:sync, attrs)
|
||||
|> Ash.update!(authorize?: false)
|
||||
author
|
||||
|> Ash.Changeset.for_update(:sync, attrs)
|
||||
|> Ash.update!(authorize?: false)
|
||||
end
|
||||
|
||||
:ok
|
||||
|
||||
@@ -24,7 +26,7 @@ defmodule DecentralisedBookIndex.Sync.AuthorSync do
|
||||
attrs =
|
||||
attrs
|
||||
|> update_registry(registry_info)
|
||||
|> Map.put(:dbi_server_id, server_id)
|
||||
|> Map.put_new(:dbi_server_id, server_id)
|
||||
|
||||
Author
|
||||
|> Ash.Changeset.for_create(:sync_create, attrs)
|
||||
@@ -58,4 +60,17 @@ defmodule DecentralisedBookIndex.Sync.AuthorSync do
|
||||
|> Map.delete(:author_alias_registry_id)
|
||||
end
|
||||
end
|
||||
|
||||
defp is_local(author) do
|
||||
is_nil(author.dbi_server_id)
|
||||
end
|
||||
|
||||
defp is_new(new_author, author) do
|
||||
with {:ok, datetime, _offset} <- DateTime.from_iso8601(new_author[:updated_at]) do
|
||||
DateTime.after?(datetime, author.updated_at)
|
||||
else
|
||||
{:error, reason} ->
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user