2019-01-03 11:34:31 +01:00
|
|
|
defmodule Mix.Tasks.Mobilizon.CreateBot do
|
2018-06-14 18:15:27 +02:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Creates a bot from a source.
|
2018-06-14 18:15:27 +02:00
|
|
|
"""
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
use Mix.Task
|
2019-09-22 16:26:23 +02:00
|
|
|
|
|
|
|
alias Mobilizon.{Actors, Users}
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Bot
|
2019-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.Users.User
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
require Logger
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2020-10-13 15:20:07 +02:00
|
|
|
@shortdoc "Create bot"
|
2018-05-30 14:27:21 +02:00
|
|
|
def run([email, name, summary, type, url]) do
|
|
|
|
Mix.Task.run("app.start")
|
|
|
|
|
2019-03-05 17:23:05 +01:00
|
|
|
with {:ok, %User{} = user} <- Users.get_user_by_email(email, true),
|
2019-09-11 03:16:37 +02:00
|
|
|
actor <- Actors.register_bot(%{name: name, summary: summary}),
|
2018-07-27 10:45:35 +02:00
|
|
|
{:ok, %Bot{} = bot} <-
|
|
|
|
Actors.create_bot(%{
|
|
|
|
"type" => type,
|
|
|
|
"source" => url,
|
|
|
|
"actor_id" => actor.id,
|
|
|
|
"user_id" => user.id
|
|
|
|
}) do
|
2018-05-30 14:27:21 +02:00
|
|
|
bot
|
|
|
|
else
|
2019-09-22 16:26:23 +02:00
|
|
|
error ->
|
|
|
|
Logger.error(inspect(error))
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|