2021-02-24 19:06:48 +01:00
|
|
|
defmodule Mobilizon.Activities do
|
|
|
|
@moduledoc """
|
|
|
|
The Activities context.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Ecto.Query, warn: false
|
|
|
|
import EctoEnum
|
|
|
|
alias Mobilizon.Activities.Activity
|
|
|
|
alias Mobilizon.Actors.Member
|
|
|
|
alias Mobilizon.Storage.{Page, Repo}
|
|
|
|
|
|
|
|
defenum(Priority,
|
|
|
|
very_low: 10,
|
|
|
|
low: 20,
|
|
|
|
medium: 30,
|
|
|
|
high: 40,
|
|
|
|
very_high: 50
|
|
|
|
)
|
|
|
|
|
2023-10-17 16:41:31 +02:00
|
|
|
@activity_types [
|
|
|
|
"event",
|
|
|
|
"post",
|
|
|
|
"conversation",
|
|
|
|
"discussion",
|
|
|
|
"resource",
|
|
|
|
"group",
|
|
|
|
"member",
|
|
|
|
"comment"
|
|
|
|
]
|
2021-02-24 19:06:48 +01:00
|
|
|
@event_activity_subjects ["event_created", "event_updated", "event_deleted", "comment_posted"]
|
2023-08-24 11:42:03 +02:00
|
|
|
@participant_activity_subjects ["event_new_participation"]
|
2021-02-24 19:06:48 +01:00
|
|
|
@post_activity_subjects ["post_created", "post_updated", "post_deleted"]
|
2023-10-17 16:41:31 +02:00
|
|
|
@conversation_activity_subjects [
|
|
|
|
"conversation_created",
|
|
|
|
"conversation_replied",
|
|
|
|
"conversation_event_announcement"
|
|
|
|
]
|
2021-02-24 19:06:48 +01:00
|
|
|
@discussion_activity_subjects [
|
|
|
|
"discussion_created",
|
|
|
|
"discussion_replied",
|
|
|
|
"discussion_renamed",
|
|
|
|
"discussion_archived",
|
|
|
|
"discussion_deleted"
|
|
|
|
]
|
|
|
|
@resource_activity_subjects [
|
|
|
|
"resource_created",
|
|
|
|
"resource_renamed",
|
|
|
|
"resource_moved",
|
|
|
|
"resource_deleted"
|
|
|
|
]
|
|
|
|
@member_activity_subjects [
|
|
|
|
"member_request",
|
|
|
|
"member_invited",
|
|
|
|
"member_accepted_invitation",
|
|
|
|
"member_rejected_invitation",
|
|
|
|
"member_added",
|
|
|
|
"member_joined",
|
|
|
|
"member_approved",
|
|
|
|
"member_updated",
|
|
|
|
"member_removed",
|
|
|
|
"member_quit"
|
|
|
|
]
|
|
|
|
@settings_activity_subjects ["group_created", "group_updated"]
|
|
|
|
|
|
|
|
@subjects @event_activity_subjects ++
|
2023-10-17 16:41:31 +02:00
|
|
|
@conversation_activity_subjects ++
|
2023-08-24 11:42:03 +02:00
|
|
|
@participant_activity_subjects ++
|
2021-02-24 19:06:48 +01:00
|
|
|
@post_activity_subjects ++
|
|
|
|
@discussion_activity_subjects ++
|
|
|
|
@resource_activity_subjects ++
|
|
|
|
@member_activity_subjects ++ @settings_activity_subjects
|
|
|
|
|
2023-08-24 11:42:03 +02:00
|
|
|
@object_type [
|
|
|
|
"event",
|
|
|
|
"participant",
|
|
|
|
"actor",
|
|
|
|
"post",
|
|
|
|
"discussion",
|
2023-10-17 16:41:31 +02:00
|
|
|
"conversation",
|
2023-08-24 11:42:03 +02:00
|
|
|
"resource",
|
|
|
|
"member",
|
|
|
|
"group",
|
|
|
|
"comment"
|
|
|
|
]
|
2021-02-24 19:06:48 +01:00
|
|
|
|
|
|
|
defenum(Type, @activity_types)
|
|
|
|
defenum(Subject, @subjects)
|
|
|
|
defenum(ObjectType, @object_type)
|
|
|
|
|
2021-03-23 15:18:03 +01:00
|
|
|
@activity_preloads [:author, :group]
|
|
|
|
|
2021-02-24 19:06:48 +01:00
|
|
|
@doc """
|
|
|
|
Returns the list of activities.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> list_activities()
|
|
|
|
[%Activity{}, ...]
|
|
|
|
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec list_activities :: list(Activity.t())
|
2021-02-24 19:06:48 +01:00
|
|
|
def list_activities do
|
|
|
|
Repo.all(Activity)
|
|
|
|
end
|
|
|
|
|
2021-03-02 14:34:52 +01:00
|
|
|
@spec list_group_activities_for_member(
|
|
|
|
integer() | String.t(),
|
2021-02-24 19:06:48 +01:00
|
|
|
integer() | String.t(),
|
|
|
|
Keyword.t(),
|
|
|
|
integer() | nil,
|
|
|
|
integer() | nil
|
2022-04-07 18:37:44 +02:00
|
|
|
) :: Page.t(Activity.t())
|
2021-03-02 14:34:52 +01:00
|
|
|
def list_group_activities_for_member(
|
2021-02-24 19:06:48 +01:00
|
|
|
group_id,
|
|
|
|
actor_asking_id,
|
|
|
|
filters \\ [],
|
|
|
|
page \\ nil,
|
|
|
|
limit \\ nil
|
|
|
|
) do
|
|
|
|
Activity
|
|
|
|
|> where([a], a.group_id == ^group_id)
|
|
|
|
|> join(:inner, [a], m in Member,
|
|
|
|
on: m.parent_id == a.group_id and m.actor_id == ^actor_asking_id
|
|
|
|
)
|
|
|
|
|> where([a, m], a.inserted_at >= m.member_since)
|
|
|
|
|> filter_object_type(Keyword.get(filters, :type))
|
2021-03-09 16:20:58 +01:00
|
|
|
|> filter_author(Keyword.get(filters, :author), actor_asking_id)
|
2021-02-24 19:06:48 +01:00
|
|
|
|> order_by(desc: :inserted_at)
|
2021-03-02 14:34:52 +01:00
|
|
|
|> preload([:author, :group])
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec list_group_activities(
|
|
|
|
integer() | String.t(),
|
|
|
|
Keyword.t(),
|
|
|
|
integer() | nil,
|
|
|
|
integer() | nil
|
2022-04-07 18:37:44 +02:00
|
|
|
) :: Page.t(Activity.t())
|
2021-03-02 14:34:52 +01:00
|
|
|
def list_group_activities(
|
|
|
|
group_id,
|
|
|
|
filters \\ [],
|
|
|
|
page \\ nil,
|
|
|
|
limit \\ nil
|
|
|
|
) do
|
|
|
|
Activity
|
|
|
|
|> where([a], a.group_id == ^group_id)
|
|
|
|
|> filter_object_type(Keyword.get(filters, :type))
|
|
|
|
|> order_by(desc: :inserted_at)
|
2021-02-24 19:06:48 +01:00
|
|
|
|> preload([:author, :group])
|
|
|
|
|> Page.build_page(page, limit)
|
|
|
|
end
|
|
|
|
|
2021-06-26 15:23:22 +02:00
|
|
|
@spec list_group_activities_for_recap(
|
|
|
|
integer() | String.t(),
|
|
|
|
integer() | String.t(),
|
|
|
|
DateTime.t() | nil
|
|
|
|
) :: [Activity.t()]
|
|
|
|
def list_group_activities_for_recap(
|
|
|
|
group_id,
|
|
|
|
actor_asking_id,
|
|
|
|
last_sent_at \\ nil
|
|
|
|
) do
|
|
|
|
query =
|
|
|
|
Activity
|
|
|
|
|> where([a], a.group_id == ^group_id)
|
|
|
|
|> join(:inner, [a], m in Member,
|
|
|
|
on: m.parent_id == a.group_id and m.actor_id == ^actor_asking_id
|
|
|
|
)
|
|
|
|
|> where([a, m], a.inserted_at >= m.member_since)
|
|
|
|
|> order_by(desc: :inserted_at)
|
|
|
|
|> preload([:author, :group])
|
|
|
|
|
|
|
|
query =
|
|
|
|
if is_nil(last_sent_at), do: query, else: where(query, [a], a.inserted_at >= ^last_sent_at)
|
|
|
|
|
|
|
|
Repo.all(query)
|
|
|
|
end
|
|
|
|
|
2021-02-24 19:06:48 +01:00
|
|
|
@doc """
|
|
|
|
Gets a single activity.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Activity does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_activity!(123)
|
|
|
|
%Activity{}
|
|
|
|
|
|
|
|
iex> get_activity!(456)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec get_activity!(integer()) :: Activity.t()
|
2021-02-24 19:06:48 +01:00
|
|
|
def get_activity!(id), do: Repo.get!(Activity, id)
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a activity.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> create_activity(%{field: value})
|
|
|
|
{:ok, %Activity{}}
|
|
|
|
|
|
|
|
iex> create_activity(%{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec create_activity(map()) :: {:ok, Activity.t()} | {:error, Ecto.Changeset.t()}
|
2021-02-24 19:06:48 +01:00
|
|
|
def create_activity(attrs \\ %{}) do
|
|
|
|
%Activity{}
|
|
|
|
|> Activity.changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
2021-03-23 15:18:03 +01:00
|
|
|
@spec preload_activity(Activity.t()) :: Activity.t()
|
|
|
|
def preload_activity(%Activity{} = activity) do
|
|
|
|
Repo.preload(activity, @activity_preloads)
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec activity_types :: list(String.t())
|
2021-02-24 19:06:48 +01:00
|
|
|
def activity_types, do: @activity_types
|
|
|
|
|
2021-03-09 16:20:58 +01:00
|
|
|
@spec filter_object_type(Query.t(), atom() | nil) :: Query.t()
|
|
|
|
defp filter_object_type(query, nil), do: query
|
|
|
|
|
|
|
|
defp filter_object_type(query, type) do
|
|
|
|
where(query, [q], q.type == ^type)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec filter_author(Query.t(), atom() | nil, integer() | String.t()) :: Query.t()
|
|
|
|
defp filter_author(query, nil, _), do: query
|
|
|
|
|
|
|
|
defp filter_author(query, :self, actor_asking_id) do
|
|
|
|
where(query, [q], q.author_id == ^actor_asking_id)
|
2021-02-24 19:06:48 +01:00
|
|
|
end
|
|
|
|
|
2021-03-09 16:20:58 +01:00
|
|
|
defp filter_author(query, :by, actor_asking_id) do
|
|
|
|
where(query, [q], q.author_id != ^actor_asking_id)
|
2021-02-24 19:06:48 +01:00
|
|
|
end
|
|
|
|
end
|