2021-02-24 19:06:48 +01:00
|
|
|
defmodule Mobilizon.Service.Activity.Resource do
|
|
|
|
@moduledoc """
|
|
|
|
Insert an resource activity
|
|
|
|
"""
|
2021-03-23 15:18:03 +01:00
|
|
|
alias Mobilizon.{Actors, Resources}
|
2021-02-24 19:06:48 +01:00
|
|
|
alias Mobilizon.Resources.Resource
|
|
|
|
alias Mobilizon.Service.Activity
|
|
|
|
alias Mobilizon.Service.Workers.ActivityBuilder
|
|
|
|
|
|
|
|
@behaviour Activity
|
|
|
|
|
|
|
|
@impl Activity
|
|
|
|
def insert_activity(resource, options \\ [])
|
|
|
|
|
|
|
|
def insert_activity(
|
|
|
|
%Resource{actor_id: actor_id, creator_id: creator_id} = resource,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
when not is_nil(actor_id) do
|
|
|
|
actor = Actors.get_actor(creator_id)
|
|
|
|
group = Actors.get_actor(actor_id)
|
|
|
|
subject = Keyword.fetch!(options, :subject)
|
|
|
|
old_resource = Keyword.get(options, :old_resource)
|
|
|
|
|
|
|
|
ActivityBuilder.enqueue(:build_activity, %{
|
|
|
|
"type" => "resource",
|
|
|
|
"subject" => subject,
|
|
|
|
"subject_params" => subject_params(resource, subject, old_resource),
|
|
|
|
"group_id" => group.id,
|
|
|
|
"author_id" => actor.id,
|
|
|
|
"object_type" => "resource",
|
|
|
|
"object_id" => if(subject != "resource_deleted", do: to_string(resource.id), else: nil),
|
|
|
|
"inserted_at" => DateTime.utc_now()
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Activity
|
|
|
|
def insert_activity(_, _), do: {:ok, nil}
|
|
|
|
|
2021-03-23 15:18:03 +01:00
|
|
|
@impl Activity
|
|
|
|
def get_object(resource_id) do
|
|
|
|
Resources.get_resource(resource_id)
|
|
|
|
end
|
|
|
|
|
2021-02-24 19:06:48 +01:00
|
|
|
@spec subject_params(Resource.t(), String.t() | nil, Resource.t() | nil) :: map()
|
|
|
|
defp subject_params(%Resource{} = resource, "resource_renamed", old_resource) do
|
|
|
|
resource
|
|
|
|
|> subject_params(nil, nil)
|
|
|
|
|> Map.put(:old_resource_title, old_resource.title)
|
|
|
|
end
|
|
|
|
|
2021-03-23 15:18:03 +01:00
|
|
|
defp subject_params(%Resource{path: path, title: title, type: type, id: id}, _, _) do
|
|
|
|
%{resource_path: path, resource_title: title, is_folder: type == :folder, resource_uuid: id}
|
2021-02-24 19:06:48 +01:00
|
|
|
end
|
|
|
|
end
|