Commit 3cd254b4 authored by Alex Castaño's avatar Alex Castaño

DataMigration: Generate gravatars

parent 5e8a4a01
defmodule MoodleNet.DataMigration.CreateGravatarIcon do
alias ActivityPub.SQL.{Query, Alter}
alias MoodleNet.Repo
def call() do
actors =
Query.new()
|> Query.with_type("Person")
|> Query.all()
|> Query.preload_assoc(:icon)
|> Enum.filter(&(&1.icon == []))
Repo.transaction(fn ->
for actor <- actors do
gravatar = MoodleNet.Gravatar.url(actor["email"])
{:ok, icon} = ActivityPub.new(type: "Image", url: gravatar)
{:ok, icon} = ActivityPub.insert(icon)
Alter.add(actor, :icon, icon)
end
end)
end
end
defmodule MoodleNet.DataMigration.CreateGravatarIconTest do
use MoodleNet.DataCase, async: true
alias ActivityPub.SQL.{Query}
test "works" do
actor = Factory.actor(email: "alex@moodle.com")
for _ <- 1..100, do: Factory.actor()
Query.new()
|> Query.with_type("Image")
|> Query.delete_all()
actor = Query.reload(actor) |> Query.preload_assoc(:icon)
assert actor.icon == []
MoodleNet.DataMigration.CreateGravatarIcon.call()
actor = Query.reload(actor) |> Query.preload_assoc(:icon)
assert [icon] = actor.icon
assert ["https://s.gravatar.com/avatar/7779b850ea05dbeca7fc39a910a77f21?d=identicon&r=g&s=80"] == icon.url
end
end
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