Various refactoring and typespec improvements
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -51,11 +51,11 @@ defmodule Mobilizon.Service.Activity.Comment do
|
||||
"object_type" => :comment
|
||||
}
|
||||
|
||||
@spec handle_notification(Keyword.t(), notification_type, Comment.t(), Event.t(), Keyword.t()) ::
|
||||
Keyword.t()
|
||||
defp handle_notification(global_res, function, comment, event, options) do
|
||||
case notify(function, comment, event, options) do
|
||||
{:ok, res} -> Keyword.put(global_res, function, res)
|
||||
_ -> Keyword.put(global_res, function, :error)
|
||||
end
|
||||
{:ok, res} = notify(function, comment, event, options)
|
||||
Keyword.put(global_res, function, res)
|
||||
end
|
||||
|
||||
@spec legacy_notifier_enqueue(map()) :: :ok
|
||||
@@ -66,11 +66,11 @@ defmodule Mobilizon.Service.Activity.Comment do
|
||||
)
|
||||
end
|
||||
|
||||
@type notification_type :: atom()
|
||||
@type notification_type :: :mentionned | :announcement | :organizer
|
||||
|
||||
# An actor is mentionned
|
||||
@spec notify(notification_type(), Comment.t(), Event.t(), Keyword.t()) ::
|
||||
{:ok, Oban.Job.t()} | {:ok, :skipped}
|
||||
{:ok, :enqueued} | {:ok, :skipped}
|
||||
defp notify(
|
||||
:mentionned,
|
||||
%Comment{actor_id: actor_id, id: comment_id, mentions: mentions},
|
||||
|
||||
@@ -10,31 +10,35 @@ defmodule Mobilizon.Service.Activity.Group do
|
||||
@behaviour Activity
|
||||
|
||||
@impl Activity
|
||||
@spec insert_activity(Actor.t(), Keyword.t()) ::
|
||||
{:ok, Job.t()} | {:ok, nil} | {:error, Ecto.Changeset.t()}
|
||||
def insert_activity(group, options \\ [])
|
||||
|
||||
def insert_activity(
|
||||
%Actor{type: :Group, id: group_id},
|
||||
options
|
||||
) do
|
||||
with %Actor{type: :Group} = group <- Actors.get_actor(group_id),
|
||||
subject when not is_nil(subject) <- Keyword.get(options, :subject),
|
||||
actor_id <- Keyword.get(options, :actor_id),
|
||||
default_updater_actor <- Actors.get_actor(actor_id),
|
||||
%Actor{id: actor_id} <-
|
||||
Keyword.get(options, :updater_actor, default_updater_actor),
|
||||
old_group <- Keyword.get(options, :old_group) do
|
||||
ActivityBuilder.enqueue(:build_activity, %{
|
||||
"type" => "group",
|
||||
"subject" => subject,
|
||||
"subject_params" => subject_params(group, subject, old_group),
|
||||
"group_id" => group.id,
|
||||
"author_id" => actor_id,
|
||||
"object_type" => "group",
|
||||
"object_id" => to_string(group.id),
|
||||
"inserted_at" => DateTime.utc_now()
|
||||
})
|
||||
else
|
||||
_ -> {:ok, nil}
|
||||
subject = Keyword.get(options, :subject)
|
||||
actor_id = Keyword.get(options, :actor_id)
|
||||
default_updater_actor = Actors.get_actor(actor_id)
|
||||
%Actor{id: actor_id} = Keyword.get(options, :updater_actor, default_updater_actor)
|
||||
old_group = Keyword.get(options, :old_group)
|
||||
|
||||
case Actors.get_actor(group_id) do
|
||||
%Actor{type: :Group} = group ->
|
||||
ActivityBuilder.enqueue(:build_activity, %{
|
||||
"type" => "group",
|
||||
"subject" => subject,
|
||||
"subject_params" => subject_params(group, subject, old_group),
|
||||
"group_id" => group.id,
|
||||
"author_id" => actor_id,
|
||||
"object_type" => "group",
|
||||
"object_id" => to_string(group.id),
|
||||
"inserted_at" => DateTime.utc_now()
|
||||
})
|
||||
|
||||
nil ->
|
||||
{:ok, nil}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ defmodule Mobilizon.Service.Activity.Utils do
|
||||
|> add_activity_object()
|
||||
end
|
||||
|
||||
@spec add_activity_object(Activity.t()) :: map()
|
||||
@spec add_activity_object(Activity.t()) :: Activity.t()
|
||||
def add_activity_object(%Activity{} = activity) do
|
||||
Map.put(activity, :object, ActivityService.object(activity))
|
||||
%Activity{activity | object: ActivityService.object(activity)}
|
||||
end
|
||||
|
||||
@spec transform_params(map()) :: list()
|
||||
|
||||
@@ -7,6 +7,7 @@ defmodule Mobilizon.Service.Address do
|
||||
|
||||
@type address :: %{name: String.t(), alternative_name: String.t()}
|
||||
|
||||
@spec render_address(AddressModel.t()) :: String.t() | no_return
|
||||
def render_address(%AddressModel{} = address) do
|
||||
%{name: name, alternative_name: alternative_name} = render_names(address)
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ defmodule Mobilizon.Service.CleanOldActivity do
|
||||
end
|
||||
end
|
||||
|
||||
@spec find_activities(Keyword.t()) :: {Ecto.Query.t(), list()}
|
||||
@spec find_activities(Keyword.t()) :: {Ecto.Query.t(), integer()}
|
||||
defp find_activities(opts) do
|
||||
grace_period =
|
||||
Keyword.get(opts, :grace_period, Config.get([:instance, :activity_expire_days], 365))
|
||||
|
||||
@@ -3,9 +3,10 @@ defmodule Mobilizon.Service.CleanUnconfirmedUsers do
|
||||
Service to clean unconfirmed users
|
||||
"""
|
||||
|
||||
alias Mobilizon.{Actors, Users}
|
||||
alias Mobilizon.Federation.ActivityPub.Relay
|
||||
alias Mobilizon.Service.ActorSuspension
|
||||
alias Mobilizon.Storage.Repo
|
||||
alias Mobilizon.Users
|
||||
alias Mobilizon.Users.User
|
||||
import Ecto.Query
|
||||
|
||||
@@ -27,21 +28,21 @@ defmodule Mobilizon.Service.CleanUnconfirmedUsers do
|
||||
end
|
||||
end
|
||||
|
||||
@spec delete_user(User.t()) :: {:ok, User.t()}
|
||||
@spec delete_user(User.t()) :: User.t() | {:error, Ecto.Changeset.t()} | no_return
|
||||
defp delete_user(%User{} = user) do
|
||||
with actors <- Users.get_actors_for_user(user),
|
||||
:ok <-
|
||||
Enum.each(actors, fn actor ->
|
||||
actor_performing = Relay.get_actor()
|
||||
actors = Users.get_actors_for_user(user)
|
||||
%{id: actor_performing_id} = Relay.get_actor()
|
||||
|
||||
Actors.perform(:delete_actor, actor,
|
||||
author_id: actor_performing.id,
|
||||
reserve_username: false
|
||||
)
|
||||
end),
|
||||
{:ok, %User{} = user} <- Users.delete_user(user, reserve_email: false),
|
||||
%User{} = user <- %User{user | actors: actors} do
|
||||
user
|
||||
Enum.each(actors, fn actor ->
|
||||
ActorSuspension.suspend_actor(actor,
|
||||
author_id: actor_performing_id,
|
||||
reserve_username: false
|
||||
)
|
||||
end)
|
||||
|
||||
case Users.delete_user(user, reserve_email: false) do
|
||||
{:ok, %User{} = user} ->
|
||||
%User{user | actors: actors}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -4,10 +4,14 @@ defmodule Mobilizon.Service.DateTime do
|
||||
"""
|
||||
alias Cldr.DateTime.Relative
|
||||
|
||||
@typep to_string_format :: :short | :medium | :long | :full
|
||||
|
||||
@spec datetime_to_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
|
||||
def datetime_to_string(%DateTime{} = datetime, locale \\ "en", format \\ :medium) do
|
||||
Mobilizon.Cldr.DateTime.to_string!(datetime, format: format, locale: locale_or_default(locale))
|
||||
end
|
||||
|
||||
@spec datetime_to_time_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
|
||||
def datetime_to_time_string(%DateTime{} = datetime, locale \\ "en", format \\ :short) do
|
||||
Mobilizon.Cldr.Time.to_string!(datetime, format: format, locale: locale_or_default(locale))
|
||||
end
|
||||
@@ -39,7 +43,8 @@ defmodule Mobilizon.Service.DateTime do
|
||||
end
|
||||
end
|
||||
|
||||
def is_first_day_of_week(%Date{} = date, locale \\ "en") do
|
||||
@spec is_first_day_of_week(Date.t(), String.t()) :: boolean()
|
||||
defp is_first_day_of_week(%Date{} = date, locale) do
|
||||
Date.day_of_week(date) == Cldr.Calendar.first_day_for_locale(locale)
|
||||
end
|
||||
|
||||
@@ -119,17 +124,18 @@ defmodule Mobilizon.Service.DateTime do
|
||||
compare_to
|
||||
|> DateTime.to_date()
|
||||
|> calculate_first_day_of_week(locale)
|
||||
|> Timex.add(Timex.Duration.from_weeks(1))
|
||||
|> Date.add(7)
|
||||
|> build_notification_datetime(options)
|
||||
|
||||
if Date.compare(datetime, next_first_day_of_week) == :gt do
|
||||
if next_first_day_of_week != nil && DateTime.compare(datetime, next_first_day_of_week) == :gt do
|
||||
next_first_day_of_week
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def appropriate_first_day_of_week(%DateTime{} = datetime, options) do
|
||||
@spec appropriate_first_day_of_week(DateTime.t(), keyword) :: DateTime.t() | nil
|
||||
defp appropriate_first_day_of_week(%DateTime{} = datetime, options) do
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
timezone = Keyword.get(options, :timezone, "Etc/UTC")
|
||||
|
||||
@@ -141,15 +147,20 @@ defmodule Mobilizon.Service.DateTime do
|
||||
if DateTime.compare(local_datetime, first_datetime) == :gt do
|
||||
first_datetime
|
||||
else
|
||||
next_first_day_of_week(local_datetime, options)
|
||||
local_datetime
|
||||
|> next_first_day_of_week(options)
|
||||
|> build_notification_datetime(options)
|
||||
end
|
||||
end
|
||||
|
||||
@spec build_notification_datetime(Date.t(), Keyword.t()) :: DateTime.t()
|
||||
def build_notification_datetime(
|
||||
%Date{} = date,
|
||||
options
|
||||
) do
|
||||
@spec build_notification_datetime(nil, Keyword.t()) :: nil
|
||||
defp build_notification_datetime(nil, _options), do: nil
|
||||
|
||||
defp build_notification_datetime(
|
||||
%Date{} = date,
|
||||
options
|
||||
) do
|
||||
notification_time = Keyword.get(options, :notification_time, ~T[08:00:00])
|
||||
timezone = Keyword.get(options, :timezone, "Etc/UTC")
|
||||
DateTime.new!(date, notification_time, timezone)
|
||||
@@ -158,8 +169,8 @@ defmodule Mobilizon.Service.DateTime do
|
||||
@start_time ~T[08:00:00]
|
||||
@end_time ~T[09:00:00]
|
||||
|
||||
@spec is_between_hours(Keyword.t()) :: boolean()
|
||||
def is_between_hours(options \\ []) when is_list(options) do
|
||||
@spec is_between_hours?(Keyword.t()) :: boolean()
|
||||
def is_between_hours?(options \\ []) when is_list(options) do
|
||||
compare_to_day = Keyword.get(options, :compare_to_day, Date.utc_today())
|
||||
compare_to = Keyword.get(options, :compare_to_datetime, DateTime.utc_now())
|
||||
start_time = Keyword.get(options, :start_time, @start_time)
|
||||
@@ -176,17 +187,16 @@ defmodule Mobilizon.Service.DateTime do
|
||||
) == :lt
|
||||
end
|
||||
|
||||
@spec is_between_hours_on_first_day(Keyword.t()) :: boolean()
|
||||
def is_between_hours_on_first_day(options) when is_list(options) do
|
||||
@spec is_between_hours_on_first_day?(Keyword.t()) :: boolean()
|
||||
def is_between_hours_on_first_day?(options) when is_list(options) do
|
||||
compare_to_day = Keyword.get(options, :compare_to_day, Date.utc_today())
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
|
||||
Mobilizon.Service.DateTime.is_first_day_of_week(compare_to_day, locale) &&
|
||||
is_between_hours(options)
|
||||
is_first_day_of_week(compare_to_day, locale) && is_between_hours?(options)
|
||||
end
|
||||
|
||||
@spec is_delay_ok_since_last_notification_sent(DateTime.t()) :: boolean()
|
||||
def is_delay_ok_since_last_notification_sent(%DateTime{} = last_notification_sent) do
|
||||
@spec is_delay_ok_since_last_notification_sent?(DateTime.t()) :: boolean()
|
||||
def is_delay_ok_since_last_notification_sent?(%DateTime{} = last_notification_sent) do
|
||||
DateTime.compare(DateTime.add(last_notification_sent, 3_600), DateTime.utc_now()) ==
|
||||
:lt
|
||||
end
|
||||
|
||||
@@ -29,6 +29,7 @@ defmodule Mobilizon.Service.ErrorReporting.Sentry do
|
||||
end
|
||||
|
||||
@impl ErrorReporting
|
||||
@spec attach :: :ok | {:error, :already_exists}
|
||||
def attach do
|
||||
:telemetry.attach(
|
||||
"oban-errors",
|
||||
|
||||
@@ -6,56 +6,99 @@ defmodule Mobilizon.Service.Export.Common do
|
||||
alias Mobilizon.{Actors, Events, Posts, Users}
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events.{Event, FeedToken}
|
||||
alias Mobilizon.Posts.Post
|
||||
alias Mobilizon.Storage.Page
|
||||
alias Mobilizon.Users.User
|
||||
|
||||
@spec fetch_actor_event_feed(String.t(), integer()) :: String.t()
|
||||
@spec fetch_actor_event_feed(String.t(), integer()) ::
|
||||
{:ok, Actor.t(), [Event.t()], [Post.t()]}
|
||||
| {:error, :actor_not_public | :actor_not_found}
|
||||
def fetch_actor_event_feed(name, limit) do
|
||||
with %Actor{} = actor <- Actors.get_actor_by_name(name),
|
||||
{:visibility, true} <- {:visibility, Actor.is_public_visibility?(actor)},
|
||||
%Page{elements: events} <- Events.list_public_events_for_actor(actor, 1, limit),
|
||||
%Page{elements: posts} <- Posts.get_public_posts_for_group(actor, 1, limit) do
|
||||
{:ok, actor, events, posts}
|
||||
else
|
||||
err ->
|
||||
{:error, err}
|
||||
case Actors.get_actor_by_name(name) do
|
||||
%Actor{} = actor ->
|
||||
if Actor.is_public_visibility?(actor) do
|
||||
%Page{elements: events} = Events.list_public_events_for_actor(actor, 1, limit)
|
||||
%Page{elements: posts} = Posts.get_public_posts_for_group(actor, 1, limit)
|
||||
{:ok, actor, events, posts}
|
||||
else
|
||||
{:error, :actor_not_public}
|
||||
end
|
||||
|
||||
nil ->
|
||||
{:error, :actor_not_found}
|
||||
end
|
||||
end
|
||||
|
||||
# Only events, not posts
|
||||
@spec fetch_events_from_token(String.t(), integer()) :: String.t()
|
||||
def fetch_events_from_token(token, limit) do
|
||||
with {:ok, uuid} <- ShortUUID.decode(token),
|
||||
{:ok, _uuid} <- Ecto.UUID.cast(uuid),
|
||||
%FeedToken{actor: actor, user: %User{} = user} <- Events.get_feed_token(uuid) do
|
||||
case actor do
|
||||
%Actor{} = actor ->
|
||||
%{
|
||||
type: :actor,
|
||||
actor: actor,
|
||||
events: fetch_actor_private_events(actor, limit),
|
||||
user: user,
|
||||
token: token
|
||||
}
|
||||
@typep token_feed_data :: %{
|
||||
type: :actor | :user,
|
||||
actor: Actor.t() | nil,
|
||||
user: User.t(),
|
||||
events: [Event.t()],
|
||||
token: String.t()
|
||||
}
|
||||
|
||||
nil ->
|
||||
with actors <- Users.get_actors_for_user(user),
|
||||
events <-
|
||||
actors
|
||||
|> Enum.map(&fetch_actor_private_events(&1, limit))
|
||||
|> Enum.concat() do
|
||||
%{type: :user, events: events, user: user, token: token, actor: nil}
|
||||
end
|
||||
end
|
||||
# Only events, not posts
|
||||
@spec fetch_events_from_token(String.t(), integer()) ::
|
||||
token_feed_data | {:error, :bad_token | :token_not_found}
|
||||
def fetch_events_from_token(token, limit) do
|
||||
case uuid_from_token(token) do
|
||||
{:ok, uuid} ->
|
||||
case Events.get_feed_token(uuid) do
|
||||
nil ->
|
||||
{:error, :token_not_found}
|
||||
|
||||
%FeedToken{actor: actor, user: %User{} = user} ->
|
||||
produce_actor_feed(actor, user, token, limit)
|
||||
end
|
||||
|
||||
{:error, :bad_token} ->
|
||||
{:error, :bad_token}
|
||||
end
|
||||
end
|
||||
|
||||
@spec uuid_from_token(String.t()) :: {:ok, String.t()} | {:error, :bad_token}
|
||||
defp uuid_from_token(token) do
|
||||
case ShortUUID.decode(token) do
|
||||
{:ok, uuid} ->
|
||||
case Ecto.UUID.cast(uuid) do
|
||||
{:ok, _uuid} ->
|
||||
{:ok, uuid}
|
||||
|
||||
:error ->
|
||||
{:error, :bad_token}
|
||||
end
|
||||
|
||||
{:error, _err} ->
|
||||
{:error, :bad_token}
|
||||
end
|
||||
end
|
||||
|
||||
@spec produce_actor_feed(Actor.t() | nil, User.t(), String.t(), integer()) :: token_feed_data
|
||||
defp produce_actor_feed(%Actor{} = actor, %User{} = user, token, limit) do
|
||||
%{
|
||||
type: :actor,
|
||||
actor: actor,
|
||||
events: fetch_actor_private_events(actor, limit),
|
||||
user: user,
|
||||
token: token
|
||||
}
|
||||
end
|
||||
|
||||
defp produce_actor_feed(nil, %User{} = user, token, limit) do
|
||||
with actors <- Users.get_actors_for_user(user),
|
||||
events <-
|
||||
actors
|
||||
|> Enum.map(&fetch_actor_private_events(&1, limit))
|
||||
|> Enum.concat() do
|
||||
%{type: :user, events: events, user: user, token: token, actor: nil}
|
||||
end
|
||||
end
|
||||
|
||||
@spec fetch_instance_public_content(integer()) :: {:ok, list(Event.t()), list(Post.t())}
|
||||
def fetch_instance_public_content(limit) do
|
||||
with %Page{elements: events} <- Events.list_public_local_events(1, limit),
|
||||
%Page{elements: posts} <- Posts.list_public_local_posts(1, limit) do
|
||||
{:ok, events, posts}
|
||||
end
|
||||
%Page{elements: events} = Events.list_public_local_events(1, limit)
|
||||
%Page{elements: posts} = Posts.list_public_local_posts(1, limit)
|
||||
{:ok, events, posts}
|
||||
end
|
||||
|
||||
@spec fetch_actor_private_events(Actor.t(), integer()) :: list(Event.t())
|
||||
|
||||
@@ -96,13 +96,13 @@ defmodule Mobilizon.Service.Export.Feed do
|
||||
{:ok, actor, events, posts} ->
|
||||
{:ok, build_actor_feed(actor, events, posts)}
|
||||
|
||||
err ->
|
||||
{:error, err} ->
|
||||
{:error, err}
|
||||
end
|
||||
end
|
||||
|
||||
# Build an atom feed from actor and its public events and posts
|
||||
@spec build_actor_feed(Actor.t(), list(), list(), boolean()) :: String.t()
|
||||
@spec build_actor_feed(Actor.t(), list(Event.t()), list(Post.t()), boolean()) :: String.t()
|
||||
defp build_actor_feed(%Actor{} = actor, events, posts, public \\ true) do
|
||||
display_name = Actor.display_name(actor)
|
||||
|
||||
@@ -199,19 +199,22 @@ defmodule Mobilizon.Service.Export.Feed do
|
||||
end
|
||||
|
||||
# Only events, not posts
|
||||
@spec fetch_events_from_token(String.t()) :: String.t()
|
||||
@spec fetch_events_from_token(String.t(), integer()) :: {:ok, String.t()} | {:error, atom()}
|
||||
defp fetch_events_from_token(token, limit \\ @item_limit) do
|
||||
with %{events: events, token: token, user: user, actor: actor, type: type} <-
|
||||
Common.fetch_events_from_token(token, limit) do
|
||||
case type do
|
||||
:user -> {:ok, build_user_feed(events, user, token)}
|
||||
:actor -> {:ok, build_actor_feed(actor, events, [], false)}
|
||||
end
|
||||
case Common.fetch_events_from_token(token, limit) do
|
||||
%{events: events, token: token, user: user, actor: actor, type: type} ->
|
||||
case type do
|
||||
:user -> {:ok, build_user_feed(events, user, token)}
|
||||
:actor -> {:ok, build_actor_feed(actor, events, [], false)}
|
||||
end
|
||||
|
||||
{:error, err} ->
|
||||
{:error, err}
|
||||
end
|
||||
end
|
||||
|
||||
# Build an atom feed from actor and its public events
|
||||
@spec build_user_feed(list(), User.t(), String.t()) :: String.t()
|
||||
@spec build_user_feed(list(Event.t()), User.t(), String.t()) :: String.t()
|
||||
defp build_user_feed(events, %User{email: email}, token) do
|
||||
self_url = Endpoint |> Routes.feed_url(:going, token, "atom") |> URI.decode()
|
||||
|
||||
|
||||
@@ -15,12 +15,13 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
@doc """
|
||||
Create cache for an actor, an event or an user token
|
||||
"""
|
||||
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, atom()}
|
||||
def create_cache("actor_" <> name) do
|
||||
case export_public_actor(name) do
|
||||
{:ok, res} ->
|
||||
{:commit, res}
|
||||
|
||||
err ->
|
||||
{:error, err} ->
|
||||
{:ignore, err}
|
||||
end
|
||||
end
|
||||
@@ -30,8 +31,11 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
{:ok, res} <- export_public_event(event) do
|
||||
{:commit, res}
|
||||
else
|
||||
err ->
|
||||
{:error, err} ->
|
||||
{:ignore, err}
|
||||
|
||||
nil ->
|
||||
{:ignore, :event_not_found}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -40,30 +44,20 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
{:ok, res} ->
|
||||
{:commit, res}
|
||||
|
||||
err ->
|
||||
{:error, err} ->
|
||||
{:ignore, err}
|
||||
end
|
||||
end
|
||||
|
||||
def create_cache("instance") do
|
||||
case fetch_instance_feed() do
|
||||
{:ok, res} ->
|
||||
{:commit, res}
|
||||
|
||||
err ->
|
||||
{:ignore, err}
|
||||
end
|
||||
{:ok, res} = fetch_instance_feed()
|
||||
{:commit, res}
|
||||
end
|
||||
|
||||
@spec fetch_instance_feed :: {:ok, String.t()}
|
||||
defp fetch_instance_feed do
|
||||
case Common.fetch_instance_public_content(@item_limit) do
|
||||
{:ok, events, _posts} ->
|
||||
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
|
||||
|
||||
err ->
|
||||
{:error, err}
|
||||
end
|
||||
{:ok, events, _posts} = Common.fetch_instance_public_content(@item_limit)
|
||||
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -77,13 +71,12 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
|
||||
The event must have a visibility of `:public` or `:unlisted`
|
||||
"""
|
||||
@spec export_public_event(Event.t()) :: {:ok, String.t()}
|
||||
@spec export_public_event(Event.t()) :: {:ok, String.t()} | {:error, :event_not_public}
|
||||
def export_public_event(%Event{visibility: visibility} = event)
|
||||
when visibility in [:public, :unlisted] do
|
||||
{:ok, events_to_ics([event])}
|
||||
end
|
||||
|
||||
@spec export_public_event(Event.t()) :: {:error, :event_not_public}
|
||||
def export_public_event(%Event{}), do: {:error, :event_not_public}
|
||||
|
||||
@doc """
|
||||
@@ -91,28 +84,33 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
|
||||
The actor must have a visibility of `:public` or `:unlisted`, as well as the events
|
||||
"""
|
||||
@spec export_public_actor(String.t()) :: String.t()
|
||||
@spec export_public_actor(String.t()) ::
|
||||
{:ok, String.t()} | {:error, :actor_not_public | :actor_not_found}
|
||||
def export_public_actor(name, limit \\ @item_limit) do
|
||||
case Common.fetch_actor_event_feed(name, limit) do
|
||||
{:ok, _actor, events, _posts} ->
|
||||
{:ok, events_to_ics(events)}
|
||||
|
||||
err ->
|
||||
{:error, err} ->
|
||||
{:error, err}
|
||||
end
|
||||
end
|
||||
|
||||
@spec export_private_actor(Actor.t()) :: String.t()
|
||||
@spec export_private_actor(Actor.t(), integer()) :: {:ok, String.t()}
|
||||
def export_private_actor(%Actor{} = actor, limit \\ @item_limit) do
|
||||
with events <- Common.fetch_actor_private_events(actor, limit) do
|
||||
{:ok, events_to_ics(events)}
|
||||
end
|
||||
events = Common.fetch_actor_private_events(actor, limit)
|
||||
{:ok, events_to_ics(events)}
|
||||
end
|
||||
|
||||
@spec fetch_events_from_token(String.t()) :: String.t()
|
||||
@spec fetch_events_from_token(String.t(), integer()) ::
|
||||
{:ok, String.t()} | {:error, :actor_not_public | :actor_not_found}
|
||||
defp fetch_events_from_token(token, limit \\ @item_limit) do
|
||||
with %{events: events} <- Common.fetch_events_from_token(token, limit) do
|
||||
{:ok, events_to_ics(events)}
|
||||
case Common.fetch_events_from_token(token, limit) do
|
||||
%{events: events} ->
|
||||
{:ok, events_to_ics(events)}
|
||||
|
||||
{:error, err} ->
|
||||
{:error, err}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -138,6 +136,7 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
}
|
||||
end
|
||||
|
||||
@spec vendor :: String.t()
|
||||
defp vendor do
|
||||
"Mobilizon #{Config.instance_version()}"
|
||||
end
|
||||
|
||||
@@ -18,18 +18,20 @@ defmodule Mobilizon.Service.Formatter do
|
||||
@link_regex ~r"((?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+)|[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+"ui
|
||||
@markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
|
||||
|
||||
def escape_mention_handler("@" <> nickname = mention, buffer, _, _) do
|
||||
@spec escape_mention_handler(String.t(), String.t(), any(), any()) :: String.t()
|
||||
defp escape_mention_handler("@" <> nickname = mention, buffer, _, _) do
|
||||
case Actors.get_actor_by_name(nickname) do
|
||||
%Actor{} ->
|
||||
# escape markdown characters with `\\`
|
||||
# (we don't want something like @user__name to be parsed by markdown)
|
||||
String.replace(mention, @markdown_characters_regex, "\\\\\\1")
|
||||
|
||||
_ ->
|
||||
nil ->
|
||||
buffer
|
||||
end
|
||||
end
|
||||
|
||||
@spec mention_handler(String.t(), String.t(), any(), map()) :: {String.t(), map()}
|
||||
def mention_handler("@" <> nickname, buffer, _opts, acc) do
|
||||
case Actors.get_actor_by_name(nickname) do
|
||||
# %Actor{preferred_username: preferred_username} = actor ->
|
||||
@@ -58,7 +60,7 @@ defmodule Mobilizon.Service.Formatter do
|
||||
|
||||
{link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, actor})}}
|
||||
|
||||
_ ->
|
||||
nil ->
|
||||
{buffer, acc}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,6 +14,7 @@ defmodule Mobilizon.Service.Formatter.HTML do
|
||||
|
||||
def filter_tags(html), do: Sanitizer.scrub(html, DefaultScrubbler)
|
||||
|
||||
@spec strip_tags(String.t()) :: String.t() | no_return()
|
||||
def strip_tags(html) do
|
||||
case FastSanitize.strip_tags(html) do
|
||||
{:ok, html} ->
|
||||
|
||||
@@ -93,7 +93,8 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
||||
if is_nil(value), do: url, else: do_add_parameter(url, key, value)
|
||||
end
|
||||
|
||||
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
|
||||
@spec do_add_parameter(String.t(), :coords | :type, %{lat: float, lon: float} | :administrative) ::
|
||||
String.t()
|
||||
defp do_add_parameter(url, :coords, coords),
|
||||
do: "#{url}&lat=#{coords.lat}&lon=#{coords.lon}"
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
@doc """
|
||||
Google Maps implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
|
||||
"""
|
||||
@spec geocode(String.t(), keyword()) :: list(Address.t())
|
||||
@spec geocode(String.t(), keyword()) :: list(Address.t()) | no_return
|
||||
def geocode(lon, lat, options \\ []) do
|
||||
url = build_url(:geocode, %{lon: lon, lat: lat}, options)
|
||||
|
||||
@@ -50,7 +50,7 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
@doc """
|
||||
Google Maps implementation for `c:Mobilizon.Service.Geospatial.Provider.search/2`.
|
||||
"""
|
||||
@spec search(String.t(), keyword()) :: list(Address.t())
|
||||
@spec search(String.t(), keyword()) :: list(Address.t()) | no_return
|
||||
def search(q, options \\ []) do
|
||||
url = build_url(:search, %{q: q}, options)
|
||||
|
||||
@@ -68,7 +68,7 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
end
|
||||
end
|
||||
|
||||
@spec build_url(atom(), map(), list()) :: String.t()
|
||||
@spec build_url(atom(), map(), list()) :: String.t() | no_return
|
||||
defp build_url(method, args, options) do
|
||||
limit = Keyword.get(options, :limit, 10)
|
||||
lang = Keyword.get(options, :lang, "en")
|
||||
@@ -148,6 +148,7 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
end
|
||||
end
|
||||
|
||||
@spec do_fetch_place_details(String.t() | nil, Keyword.t()) :: String.t() | no_return
|
||||
defp do_fetch_place_details(place_id, options) do
|
||||
url = build_url(:place_details, %{place_id: place_id}, options)
|
||||
|
||||
|
||||
@@ -42,7 +42,8 @@ defmodule Mobilizon.Service.Geospatial.Provider do
|
||||
iex> geocode(48.11, -1.77)
|
||||
%Address{}
|
||||
"""
|
||||
@callback geocode(longitude :: number, latitude :: number, options :: keyword) :: [Address.t()]
|
||||
@callback geocode(longitude :: number, latitude :: number, options :: keyword) ::
|
||||
[Address.t()] | {:error, atom()}
|
||||
|
||||
@doc """
|
||||
Search for an address
|
||||
@@ -62,12 +63,12 @@ defmodule Mobilizon.Service.Geospatial.Provider do
|
||||
iex> search("10 rue Jangot")
|
||||
%Address{}
|
||||
"""
|
||||
@callback search(address :: String.t(), options :: keyword) :: [Address.t()]
|
||||
@callback search(address :: String.t(), options :: keyword) :: [Address.t()] | {:error, atom()}
|
||||
|
||||
@doc """
|
||||
Returns a `Geo.Point` for given coordinates
|
||||
"""
|
||||
@spec coordinates([number], number) :: Geo.Point.t() | nil
|
||||
@spec coordinates([number | String.t()], number) :: Geo.Point.t() | nil
|
||||
def coordinates(coords, srid \\ 4326)
|
||||
|
||||
def coordinates([x, y], srid) when is_number(x) and is_number(y) do
|
||||
|
||||
@@ -12,7 +12,7 @@ defmodule Mobilizon.Service.Notifier.Email do
|
||||
|
||||
import Mobilizon.Service.DateTime,
|
||||
only: [
|
||||
is_delay_ok_since_last_notification_sent: 1
|
||||
is_delay_ok_since_last_notification_sent?: 1
|
||||
]
|
||||
|
||||
require Logger
|
||||
@@ -116,7 +116,7 @@ defmodule Mobilizon.Service.Notifier.Email do
|
||||
|
||||
# Delay ok since last notification
|
||||
defp match_group_notifications_setting(:one_hour, _, %DateTime{} = last_notification_sent, _) do
|
||||
is_delay_ok_since_last_notification_sent(last_notification_sent)
|
||||
is_delay_ok_since_last_notification_sent?(last_notification_sent)
|
||||
end
|
||||
|
||||
# This is a recap
|
||||
|
||||
@@ -66,12 +66,14 @@ defmodule Mobilizon.Service.Notifier.Push do
|
||||
Map.get(@default_behavior, activity_setting, false)
|
||||
end
|
||||
|
||||
@spec send_subscription(Activity.t(), any, Keyword.t()) :: no_return
|
||||
defp send_subscription(activity, subscription, options) do
|
||||
activity
|
||||
|> payload(options)
|
||||
|> WebPushEncryption.send_web_push(subscription)
|
||||
end
|
||||
|
||||
@spec payload(Activity.t(), Keyword.t()) :: String.t()
|
||||
defp payload(%Activity{} = activity, options) do
|
||||
activity
|
||||
|> Utils.add_activity_object()
|
||||
|
||||
@@ -192,7 +192,7 @@ defmodule Mobilizon.Service.RichMedia.Parser do
|
||||
end
|
||||
end
|
||||
|
||||
@spec maybe_parse(String.t()) :: {:halt, map()} | {:cont, map()}
|
||||
@spec maybe_parse(String.t()) :: map()
|
||||
defp maybe_parse(html) do
|
||||
Enum.reduce_while(parsers(), %{}, fn parser, acc ->
|
||||
case parser.parse(html, acc) do
|
||||
@@ -286,7 +286,7 @@ defmodule Mobilizon.Service.RichMedia.Parser do
|
||||
end
|
||||
end
|
||||
|
||||
@spec check_remote_picture_path(map()) :: map()
|
||||
@spec check_remote_picture_path(map()) :: {:ok, map()}
|
||||
defp check_remote_picture_path(%{image_remote_url: image_remote_url, url: url} = data)
|
||||
when is_binary(image_remote_url) and is_binary(url) do
|
||||
Logger.debug("Checking image_remote_url #{image_remote_url}")
|
||||
|
||||
@@ -73,9 +73,6 @@ defmodule Mobilizon.Service.RichMedia.Parsers.MetaTagsParser do
|
||||
"" ->
|
||||
meta
|
||||
|
||||
descriptions when is_list(descriptions) and length(descriptions) > 0 ->
|
||||
Map.put_new(meta, :description, hd(descriptions))
|
||||
|
||||
description ->
|
||||
Map.put_new(meta, :description, description)
|
||||
end
|
||||
@@ -99,8 +96,8 @@ defmodule Mobilizon.Service.RichMedia.Parsers.MetaTagsParser do
|
||||
with {:ok, document} <- Floki.parse_document(html),
|
||||
elem when not is_nil(elem) <-
|
||||
document |> Floki.find("html head meta[name='description']") |> List.first(),
|
||||
description when is_binary(description) <- Floki.attribute(elem, "content") do
|
||||
description
|
||||
[_ | _] = descriptions <- Floki.attribute(elem, "content") do
|
||||
hd(descriptions)
|
||||
else
|
||||
_ -> ""
|
||||
end
|
||||
|
||||
@@ -12,15 +12,22 @@ defmodule Mobilizon.Service.Workers.ActivityBuilder do
|
||||
use Mobilizon.Service.Workers.Helper, queue: "activity"
|
||||
|
||||
@impl Oban.Worker
|
||||
@spec perform(Job.t()) :: {:ok, Activity.t()} | {:error, Ecto.Changeset.t()}
|
||||
def perform(%Job{args: args}) do
|
||||
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)
|
||||
{"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}
|
||||
end
|
||||
end
|
||||
|
||||
@spec build_activity(map()) :: {:ok, Activity.t()}
|
||||
@spec build_activity(map()) :: {:ok, Activity.t()} | {:error, Ecto.Changeset.t()}
|
||||
def build_activity(args) do
|
||||
Activities.create_activity(args)
|
||||
end
|
||||
|
||||
@@ -11,11 +11,23 @@ defmodule Mobilizon.Service.Workers.Background do
|
||||
use Mobilizon.Service.Workers.Helper, queue: "background"
|
||||
|
||||
@impl Oban.Worker
|
||||
@spec perform(Job.t()) ::
|
||||
{:ok, Actor.t()}
|
||||
| {:error,
|
||||
:actor_not_found | :bad_option_value_for_reserve_username | Ecto.Changeset.t()}
|
||||
def perform(%Job{args: %{"op" => "delete_actor", "actor_id" => actor_id} = args}) do
|
||||
with reserve_username when is_boolean(reserve_username) <-
|
||||
Map.get(args, "reserve_username", true),
|
||||
%Actor{} = actor <- Actors.get_actor(actor_id) do
|
||||
ActorSuspension.suspend_actor(actor, reserve_username: reserve_username)
|
||||
case Map.get(args, "reserve_username", true) do
|
||||
reserve_username when is_boolean(reserve_username) ->
|
||||
case Actors.get_actor(actor_id) do
|
||||
%Actor{} = actor ->
|
||||
ActorSuspension.suspend_actor(actor, reserve_username: reserve_username)
|
||||
|
||||
nil ->
|
||||
{:error, :actor_not_found}
|
||||
end
|
||||
|
||||
_ ->
|
||||
{:error, :bad_option_value_for_reserve_username}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -4,11 +4,12 @@ defmodule Mobilizon.Service.Workers.CleanOrphanMediaWorker do
|
||||
"""
|
||||
|
||||
use Oban.Worker, queue: "background"
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.CleanOrphanMedia
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(%Job{}) do
|
||||
if Mobilizon.Config.get!([:instance, :remove_orphan_uploads]) and should_perform?() do
|
||||
if Keyword.get(Config.instance_config(), :remove_orphan_uploads, false) and should_perform?() do
|
||||
CleanOrphanMedia.clean()
|
||||
end
|
||||
end
|
||||
@@ -17,8 +18,7 @@ defmodule Mobilizon.Service.Workers.CleanOrphanMediaWorker do
|
||||
defp should_perform? do
|
||||
case Cachex.get(:key_value, "last_media_cleanup") do
|
||||
{:ok, %DateTime{} = last_media_cleanup} ->
|
||||
default_grace_period =
|
||||
Mobilizon.Config.get([:instance, :orphan_upload_grace_period_hours], 48)
|
||||
default_grace_period = Config.get([:instance, :orphan_upload_grace_period_hours], 48)
|
||||
|
||||
DateTime.compare(
|
||||
last_media_cleanup,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
defmodule Mobilizon.Service.Workers.CleanUnconfirmedUsersWorker do
|
||||
defmodule Mobilizon.Service.Workers.CleanSuspendedActors do
|
||||
@moduledoc """
|
||||
Worker to clean unattached media
|
||||
"""
|
||||
|
||||
@@ -11,6 +11,7 @@ defmodule Mobilizon.Service.Workers.Helper do
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.Workers.Helper
|
||||
|
||||
@spec worker_args(atom()) :: Keyword.t()
|
||||
def worker_args(queue) do
|
||||
case Config.get([:workers, :retries, queue]) do
|
||||
nil -> []
|
||||
@@ -18,6 +19,7 @@ defmodule Mobilizon.Service.Workers.Helper do
|
||||
end
|
||||
end
|
||||
|
||||
@spec sidekiq_backoff(integer, integer, integer) :: integer
|
||||
def sidekiq_backoff(attempt, pow \\ 4, base_backoff \\ 15) do
|
||||
backoff =
|
||||
:math.pow(attempt, pow) +
|
||||
@@ -39,6 +41,8 @@ defmodule Mobilizon.Service.Workers.Helper do
|
||||
|
||||
alias Oban.Job
|
||||
|
||||
@spec enqueue(String.t(), map(), Keyword.t()) ::
|
||||
{:ok, Job.t()} | {:error, Ecto.Changeset.t()}
|
||||
def enqueue(operation, params, worker_args \\ []) do
|
||||
params = Map.merge(%{"op" => operation}, params)
|
||||
queue_atom = String.to_existing_atom(unquote(queue))
|
||||
|
||||
@@ -13,17 +13,16 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(%Job{args: args}) do
|
||||
with {"legacy_notify", args} <- Map.pop(args, "op") do
|
||||
activity = build_activity(args)
|
||||
{"legacy_notify", args} = Map.pop(args, "op")
|
||||
activity = build_activity(args)
|
||||
|
||||
if args["subject"] == "participation_event_comment" do
|
||||
notify_anonymous_participants(get_in(args, ["subject_params", "event_id"]), activity)
|
||||
end
|
||||
|
||||
args
|
||||
|> users_to_notify(author_id: args["author_id"], group_id: Map.get(args, "group_id"))
|
||||
|> Enum.each(&Notifier.notify(&1, activity, single_activity: true))
|
||||
if args["subject"] == "participation_event_comment" do
|
||||
notify_anonymous_participants(get_in(args, ["subject_params", "event_id"]), activity)
|
||||
end
|
||||
|
||||
args
|
||||
|> users_to_notify(author_id: args["author_id"], group_id: Map.get(args, "group_id"))
|
||||
|> Enum.each(&Notifier.notify(&1, activity, single_activity: true))
|
||||
end
|
||||
|
||||
defp build_activity(args) do
|
||||
@@ -105,8 +104,8 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|
||||
is_map(metadata) && is_binary(metadata.email)
|
||||
end)
|
||||
|> Enum.map(fn %Participant{metadata: metadata} -> metadata end)
|
||||
|> Enum.map(fn metadata ->
|
||||
Notifier.Email.send_anonymous_activity(metadata.email, activity,
|
||||
|> Enum.map(fn %{email: email} = metadata ->
|
||||
Notifier.Email.send_anonymous_activity(email, activity,
|
||||
locale: Map.get(metadata, :locale, "en")
|
||||
)
|
||||
end)
|
||||
|
||||
@@ -13,9 +13,9 @@ defmodule Mobilizon.Service.Workers.SendActivityRecapWorker do
|
||||
|
||||
import Mobilizon.Service.DateTime,
|
||||
only: [
|
||||
is_between_hours: 1,
|
||||
is_between_hours_on_first_day: 1,
|
||||
is_delay_ok_since_last_notification_sent: 1
|
||||
is_between_hours?: 1,
|
||||
is_between_hours_on_first_day?: 1,
|
||||
is_delay_ok_since_last_notification_sent?: 1
|
||||
]
|
||||
|
||||
@impl Oban.Worker
|
||||
@@ -86,7 +86,7 @@ defmodule Mobilizon.Service.Workers.SendActivityRecapWorker do
|
||||
group_notifications: :one_hour
|
||||
}
|
||||
}) do
|
||||
is_delay_ok_since_last_notification_sent(last_notification_sent)
|
||||
is_delay_ok_since_last_notification_sent?(last_notification_sent)
|
||||
end
|
||||
|
||||
# If we're between notification hours
|
||||
@@ -96,7 +96,7 @@ defmodule Mobilizon.Service.Workers.SendActivityRecapWorker do
|
||||
timezone: timezone
|
||||
}
|
||||
}) do
|
||||
is_between_hours(timezone: timezone || "Etc/UTC")
|
||||
is_between_hours?(timezone: timezone || "Etc/UTC")
|
||||
end
|
||||
|
||||
# If we're on the first day of the week between notification hours
|
||||
@@ -107,6 +107,6 @@ defmodule Mobilizon.Service.Workers.SendActivityRecapWorker do
|
||||
timezone: timezone
|
||||
}
|
||||
}) do
|
||||
is_between_hours_on_first_day(timezone: timezone || "Etc/UTC", locale: locale)
|
||||
is_between_hours_on_first_day?(timezone: timezone || "Etc/UTC", locale: locale)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user