2020-08-12 16:05:34 +02:00
|
|
|
defmodule Mobilizon.Service.SiteMap do
|
|
|
|
@moduledoc """
|
|
|
|
Generates a sitemap
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.{Actors, Events, Posts}
|
|
|
|
alias Mobilizon.Storage.Repo
|
|
|
|
alias Mobilizon.Web.Endpoint
|
2023-12-01 09:49:54 +01:00
|
|
|
use Mobilizon.Web, :verified_routes
|
2020-08-12 16:05:34 +02:00
|
|
|
|
|
|
|
@default_static_frequency :monthly
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec generate_sitemap :: {:ok, :ok}
|
2020-08-12 16:05:34 +02:00
|
|
|
def generate_sitemap do
|
|
|
|
static_routes = [
|
2023-12-01 09:49:54 +01:00
|
|
|
{url(~p"/*path"), :daily},
|
2021-11-03 09:25:08 +01:00
|
|
|
"#{Endpoint.url()}/search",
|
2020-08-12 16:05:34 +02:00
|
|
|
"#{Endpoint.url()}/about/instance",
|
|
|
|
"#{Endpoint.url()}/terms",
|
|
|
|
"#{Endpoint.url()}/privacy",
|
|
|
|
"#{Endpoint.url()}/rules",
|
|
|
|
"#{Endpoint.url()}/glossary"
|
|
|
|
]
|
|
|
|
|
|
|
|
config = [
|
|
|
|
store: Sitemapper.FileStore,
|
2023-09-05 20:04:26 +02:00
|
|
|
store_config: [path: Application.get_env(:mobilizon, __MODULE__) |> get_in([:path])],
|
2020-08-12 16:05:34 +02:00
|
|
|
sitemap_url: Endpoint.url(),
|
|
|
|
gzip: false
|
|
|
|
]
|
|
|
|
|
2022-05-04 08:21:15 +02:00
|
|
|
Repo.transaction(
|
|
|
|
fn ->
|
|
|
|
Events.stream_events_for_sitemap()
|
|
|
|
|> Stream.concat(Actors.list_groups_for_stream())
|
|
|
|
|> Stream.concat(Posts.list_posts_for_stream())
|
|
|
|
|> Stream.concat(
|
|
|
|
Enum.map(static_routes, fn route ->
|
|
|
|
{url, frequency} =
|
|
|
|
case route do
|
|
|
|
{url, frequency} -> {url, frequency}
|
|
|
|
url when is_binary(url) -> {url, @default_static_frequency}
|
|
|
|
end
|
2020-08-12 16:05:34 +02:00
|
|
|
|
2022-05-04 08:21:15 +02:00
|
|
|
%{url: url, updated_at: nil, frequence: frequency}
|
|
|
|
end)
|
|
|
|
)
|
|
|
|
|> Stream.map(fn %{url: url, updated_at: updated_at} = args ->
|
|
|
|
frequence = Map.get(args, :frequence, :weekly)
|
2020-08-12 16:05:34 +02:00
|
|
|
|
2022-05-04 08:21:15 +02:00
|
|
|
%Sitemapper.URL{
|
|
|
|
loc: url,
|
|
|
|
changefreq: frequence,
|
|
|
|
lastmod: check_date_time(updated_at)
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|> Sitemapper.generate(config)
|
|
|
|
|> Sitemapper.persist(config)
|
|
|
|
|> Sitemapper.ping(config)
|
|
|
|
|> Stream.run()
|
|
|
|
end,
|
|
|
|
timeout: :infinity
|
|
|
|
)
|
2020-08-12 16:05:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Sometimes we use naive datetimes
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec check_date_time(any()) :: DateTime.t() | nil
|
2020-08-12 16:05:34 +02:00
|
|
|
defp check_date_time(%NaiveDateTime{} = datetime), do: DateTime.from_naive!(datetime, "Etc/UTC")
|
|
|
|
defp check_date_time(%DateTime{} = datetime), do: datetime
|
|
|
|
defp check_date_time(_), do: nil
|
|
|
|
end
|