Merge branch 'preload-data' into 'develop'
[akkoma] / lib / pleroma / web / preload / timelines.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Preload.Providers.Timelines do
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.MastodonAPI.StatusView
8 alias Pleroma.Web.Preload.Providers.Provider
9
10 @behaviour Provider
11 @public_url :"/api/v1/timelines/public"
12
13 @impl Provider
14 def generate_terms(params) do
15 build_public_tag(%{}, params)
16 end
17
18 def build_public_tag(acc, params) do
19 if Pleroma.Config.get([:restrict_unauthenticated, :timelines, :federated], true) do
20 acc
21 else
22 Map.put(acc, @public_url, public_timeline(params))
23 end
24 end
25
26 defp public_timeline(%{"path" => ["main", "all"]}), do: get_public_timeline(false)
27
28 defp public_timeline(_params), do: get_public_timeline(true)
29
30 defp get_public_timeline(local_only) do
31 activities =
32 ActivityPub.fetch_public_activities(%{
33 type: ["Create"],
34 local_only: local_only
35 })
36
37 StatusView.render("index.json", activities: activities, for: nil, as: :activity)
38 end
39 end