Move subscription notifications to a separate controller
[akkoma] / lib / pleroma / web / pleroma_api / pleroma_api.ex
1 defmodule Pleroma.Web.PleromaAPI.PleromaAPI do
2 import Ecto.Query
3 import Ecto.Changeset
4
5 alias Pleroma.Activity
6 alias Pleroma.Pagination
7 alias Pleroma.SubscriptionNotification
8
9 def get_subscription_notifications(user, params \\ %{}) do
10 options = cast_params(params)
11
12 user
13 |> SubscriptionNotification.for_user_query(options)
14 |> restrict(:exclude_types, options)
15 |> Pagination.fetch_paginated(params)
16 end
17
18 defp cast_params(params) do
19 param_types = %{
20 exclude_types: {:array, :string},
21 reblogs: :boolean,
22 with_muted: :boolean
23 }
24
25 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
26 changeset.changes
27 end
28
29 defp restrict(query, :exclude_types, %{exclude_types: mastodon_types = [_ | _]}) do
30 ap_types =
31 mastodon_types
32 |> Enum.map(&Activity.from_mastodon_notification_type/1)
33 |> Enum.filter(& &1)
34
35 query
36 |> where([q, a], not fragment("? @> ARRAY[?->>'type']::varchar[]", ^ap_types, a.data))
37 end
38
39 defp restrict(query, _, _), do: query
40 end