2021-02-24 18:06:48 +00:00
|
|
|
defmodule Mobilizon.Service.Workers.ActivityBuilder do
|
|
|
|
@moduledoc """
|
|
|
|
Worker to insert activity items in users feeds
|
|
|
|
"""
|
|
|
|
|
2021-03-23 14:18:03 +00:00
|
|
|
alias Mobilizon.{Activities, Actors, Users}
|
2021-02-24 18:06:48 +00:00
|
|
|
alias Mobilizon.Activities.Activity
|
2021-03-23 14:18:03 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Service.Notifier
|
|
|
|
alias Mobilizon.Users.User
|
2021-02-24 18:06:48 +00:00
|
|
|
|
|
|
|
use Mobilizon.Service.Workers.Helper, queue: "activity"
|
|
|
|
|
|
|
|
@impl Oban.Worker
|
|
|
|
def perform(%Job{args: args}) do
|
2021-03-23 14:18:03 +00:00
|
|
|
with {"build_activity", args} <- Map.pop(args, "op"),
|
|
|
|
{:ok, %Activity{} = activity} <- build_activity(args),
|
|
|
|
preloaded_activity <- Activities.preload_activity(activity) do
|
|
|
|
notify_activity(preloaded_activity)
|
2021-02-24 18:06:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec build_activity(map()) :: {:ok, Activity.t()}
|
|
|
|
def build_activity(args) do
|
|
|
|
Activities.create_activity(args)
|
|
|
|
end
|
2021-03-23 14:18:03 +00:00
|
|
|
|
|
|
|
@spec notify_activity(Activity.t()) :: :ok
|
|
|
|
def notify_activity(%Activity{} = activity) do
|
|
|
|
activity
|
|
|
|
|> users_to_notify()
|
2021-05-06 10:27:04 +00:00
|
|
|
|> Enum.each(&Notifier.notify(&1, activity, single_activity: true))
|
2021-03-23 14:18:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec users_to_notify(Activity.t()) :: list(User.t())
|
|
|
|
defp users_to_notify(%Activity{group: %Actor{} = group, author_id: author_id}) do
|
|
|
|
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-05-06 10:27:04 +00:00
|
|
|
|> Enum.map(&Users.get_user_with_settings!/1)
|
2021-03-23 14:18:03 +00:00
|
|
|
end
|
2021-02-24 18:06:48 +00:00
|
|
|
end
|