Add scheduled activities
[akkoma] / lib / pleroma / web / mastodon_api / mastodon_api.ex
1 defmodule Pleroma.Web.MastodonAPI.MastodonAPI do
2 import Ecto.Query
3 import Ecto.Changeset
4
5 alias Pleroma.Activity
6 alias Pleroma.Notification
7 alias Pleroma.Pagination
8 alias Pleroma.ScheduledActivity
9 alias Pleroma.User
10
11 def get_followers(user, params \\ %{}) do
12 user
13 |> User.get_followers_query()
14 |> Pagination.fetch_paginated(params)
15 end
16
17 def get_friends(user, params \\ %{}) do
18 user
19 |> User.get_friends_query()
20 |> Pagination.fetch_paginated(params)
21 end
22
23 def get_notifications(user, params \\ %{}) do
24 options = cast_params(params)
25
26 user
27 |> Notification.for_user_query()
28 |> restrict(:exclude_types, options)
29 |> Pagination.fetch_paginated(params)
30 end
31
32 def get_scheduled_activities(user, params \\ %{}) do
33 user
34 |> ScheduledActivity.for_user_query()
35 |> Pagination.fetch_paginated(params)
36 end
37
38 defp cast_params(params) do
39 param_types = %{
40 exclude_types: {:array, :string}
41 }
42
43 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
44 changeset.changes
45 end
46
47 defp restrict(query, :exclude_types, %{exclude_types: mastodon_types = [_ | _]}) do
48 ap_types =
49 mastodon_types
50 |> Enum.map(&Activity.from_mastodon_notification_type/1)
51 |> Enum.filter(& &1)
52
53 query
54 |> where([q, a], not fragment("? @> ARRAY[?->>'type']::varchar[]", ^ap_types, a.data))
55 end
56
57 defp restrict(query, _, _), do: query
58 end