2021-02-24 19:06:48 +01:00
|
|
|
defmodule Mobilizon.Service.Workers.ActivityBuilder do
|
|
|
|
@moduledoc """
|
|
|
|
Worker to insert activity items in users feeds
|
|
|
|
"""
|
|
|
|
|
2021-03-23 15:18:03 +01:00
|
|
|
alias Mobilizon.{Activities, Actors, Users}
|
2021-02-24 19:06:48 +01:00
|
|
|
alias Mobilizon.Activities.Activity
|
2021-03-23 15:18:03 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Service.Notifier
|
|
|
|
alias Mobilizon.Users.User
|
2021-02-24 19:06:48 +01:00
|
|
|
|
|
|
|
use Mobilizon.Service.Workers.Helper, queue: "activity"
|
|
|
|
|
|
|
|
@impl Oban.Worker
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec perform(Job.t()) :: {:ok, Activity.t()} | {:error, Ecto.Changeset.t()}
|
2021-02-24 19:06:48 +01:00
|
|
|
def perform(%Job{args: args}) do
|
2021-09-24 16:46:42 +02:00
|
|
|
{"build_activity", args} = Map.pop(args, "op")
|
|
|
|
|
|
|
|
case build_activity(args) do
|
|
|
|
{:ok, %Activity{} = activity} ->
|
|
|
|
activity
|
|
|
|
|> Activities.preload_activity()
|
|
|
|
|> notify_activity()
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = err} ->
|
|
|
|
{:error, err}
|
2021-02-24 19:06:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec build_activity(map()) :: {:ok, Activity.t()} | {:error, Ecto.Changeset.t()}
|
2021-02-24 19:06:48 +01:00
|
|
|
def build_activity(args) do
|
|
|
|
Activities.create_activity(args)
|
|
|
|
end
|
2021-03-23 15:18:03 +01:00
|
|
|
|
|
|
|
@spec notify_activity(Activity.t()) :: :ok
|
|
|
|
def notify_activity(%Activity{} = activity) do
|
|
|
|
activity
|
|
|
|
|> users_to_notify()
|
2021-05-06 12:27:04 +02:00
|
|
|
|> Enum.each(&Notifier.notify(&1, activity, single_activity: true))
|
2021-03-23 15:18:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec users_to_notify(Activity.t()) :: list(User.t())
|
2021-06-30 10:13:57 +02:00
|
|
|
defp users_to_notify(%Activity{group: %Actor{type: :Group} = group, author_id: author_id}) do
|
2021-03-23 15:18:03 +01:00
|
|
|
group
|
|
|
|
|> Actors.list_internal_actors_members_for_group([
|
|
|
|
:creator,
|
|
|
|
:administrator,
|
|
|
|
:moderator,
|
|
|
|
:member
|
|
|
|
])
|
|
|
|
|> Enum.filter(&(&1.id != author_id))
|
|
|
|
|> Enum.map(& &1.user_id)
|
|
|
|
|> Enum.filter(& &1)
|
|
|
|
|> Enum.uniq()
|
2021-11-03 08:50:28 +01:00
|
|
|
|> Enum.map(&Users.get_user_with_activity_settings!/1)
|
2021-03-23 15:18:03 +01:00
|
|
|
end
|
2021-06-30 10:13:57 +02:00
|
|
|
|
|
|
|
defp users_to_notify(_), do: []
|
2021-02-24 19:06:48 +01:00
|
|
|
end
|