2021-02-24 19:06:48 +01:00
|
|
|
defmodule Mobilizon.Service.Activity do
|
|
|
|
@moduledoc """
|
|
|
|
Behavior for Activity creators
|
|
|
|
"""
|
|
|
|
|
2021-03-23 15:18:03 +01:00
|
|
|
alias Mobilizon.Activities.Activity
|
|
|
|
alias Mobilizon.Service.Activity.{Comment, Discussion, Event, Group, Member, Post, Resource}
|
|
|
|
|
2021-06-02 18:37:17 +02:00
|
|
|
@callback insert_activity(entity :: struct(), options :: map()) ::
|
|
|
|
{:ok, Oban.Job.t()} | {:ok, any()}
|
2021-03-23 15:18:03 +01:00
|
|
|
|
|
|
|
@callback get_object(object_id :: String.t() | integer()) :: struct()
|
|
|
|
|
|
|
|
@spec object(Activity.t()) :: struct() | nil
|
|
|
|
def object(%Activity{object_type: object_type, object_id: object_id}) do
|
|
|
|
do_get_object(object_type, object_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec has_object?(Activity.t()) :: boolean()
|
|
|
|
def has_object?(%Activity{} = activity) do
|
|
|
|
!is_nil(object(activity))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_get_object(_, nil), do: nil
|
|
|
|
|
|
|
|
defp do_get_object(:event, event_id) do
|
|
|
|
Event.get_object(event_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_get_object(:post, post_id) do
|
|
|
|
Post.get_object(post_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_get_object(:member, member_id) do
|
|
|
|
Member.get_object(member_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_get_object(:resource, resource_id) do
|
|
|
|
Resource.get_object(resource_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_get_object(:discussion, discussion_id) do
|
|
|
|
Discussion.get_object(discussion_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_get_object(:group, group_id) do
|
|
|
|
Group.get_object(group_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_get_object(:comment, comment_id) do
|
|
|
|
Comment.get_object(comment_id)
|
|
|
|
end
|
2021-02-24 19:06:48 +01:00
|
|
|
end
|