Commit a5f4acb5 authored by Karen Konou's avatar Karen Konou

Handle case when updating a nil image

parent e0554eb3
......@@ -86,7 +86,7 @@ defmodule MoodleNet.Accounts do
# FIXME this should be a transaction
with {:ok, _icon} <- ActivityPub.update(icon, url: icon_url),
{:ok, _image} <- ActivityPub.update(image, url: image_url),
{:ok, _image} <- update_image(image, image_url, actor),
{:ok, _location} <- update_location(location, location_content, actor),
{:ok, _attachment} <- update_attachment(attachment, website, actor),
{:ok, actor} <- ActivityPub.update(actor, changes) do
......@@ -99,6 +99,18 @@ defmodule MoodleNet.Accounts do
end
end
defp update_image(image, url, actor) do
case image do
nil ->
with {:ok, image} <- ActivityPub.new(url: url),
{:ok, image} <- ActivityPub.insert(image),
{:ok, _} <- Alter.add(actor, :image, image),
do: {:ok, image}
image ->
ActivityPub.update(image, url: url)
end
end
defp update_location(nil, nil, _), do: {:ok, nil}
defp update_location(location, nil, _) do
......
......@@ -92,7 +92,7 @@ defmodule MoodleNet.AccountsTest do
describe "update_user/2" do
test "works" do
actor = Factory.actor(location: nil, attachment: nil)
actor = Factory.actor(location: nil, attachment: nil, image: nil)
attrs = %{
name: "name",
preferred_username: "username",
......@@ -112,6 +112,9 @@ defmodule MoodleNet.AccountsTest do
assert actor["primary_language"] == "cz"
assert actor.location == []
assert {:ok, actor} = MoodleNet.Accounts.update_user(actor, %{image: "https://images.unsplash.com/photo-1557943978-bea7e84f0e87"})
assert [%{url: ["https://images.unsplash.com/photo-1557943978-bea7e84f0e87"]}] = actor.image
assert {:ok, actor} = MoodleNet.Accounts.update_user(actor, %{location: "location"})
assert [%{content: %{"und" => "location"}, type: ["Object", "Place"]}] = actor.location
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment