Merge branch 'develop' into gun
[akkoma] / lib / pleroma / web / mastodon_api / mastodon_api.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.MastodonAPI.MastodonAPI do
6 import Ecto.Query
7 import Ecto.Changeset
8
9 alias Pleroma.Activity
10 alias Pleroma.Notification
11 alias Pleroma.Pagination
12 alias Pleroma.ScheduledActivity
13 alias Pleroma.User
14 alias Pleroma.Web.CommonAPI
15
16 @spec follow(User.t(), User.t(), map) :: {:ok, User.t()} | {:error, String.t()}
17 def follow(follower, followed, params \\ %{}) do
18 result =
19 if not User.following?(follower, followed) do
20 CommonAPI.follow(follower, followed)
21 else
22 {:ok, follower, followed, nil}
23 end
24
25 with {:ok, follower, _followed, _} <- result do
26 options = cast_params(params)
27 set_reblogs_visibility(options[:reblogs], result)
28 {:ok, follower}
29 end
30 end
31
32 defp set_reblogs_visibility(false, {:ok, follower, followed, _}) do
33 CommonAPI.hide_reblogs(follower, followed)
34 end
35
36 defp set_reblogs_visibility(_, {:ok, follower, followed, _}) do
37 CommonAPI.show_reblogs(follower, followed)
38 end
39
40 @spec get_followers(User.t(), map()) :: list(User.t())
41 def get_followers(user, params \\ %{}) do
42 user
43 |> User.get_followers_query()
44 |> Pagination.fetch_paginated(params)
45 end
46
47 def get_friends(user, params \\ %{}) do
48 user
49 |> User.get_friends_query()
50 |> Pagination.fetch_paginated(params)
51 end
52
53 def get_notifications(user, params \\ %{}) do
54 options = cast_params(params)
55
56 user
57 |> Notification.for_user_query(options)
58 |> restrict(:exclude_types, options)
59 |> restrict(:account_ap_id, options)
60 |> Pagination.fetch_paginated(params)
61 end
62
63 def get_scheduled_activities(user, params \\ %{}) do
64 user
65 |> ScheduledActivity.for_user_query()
66 |> Pagination.fetch_paginated(params)
67 end
68
69 defp cast_params(params) do
70 param_types = %{
71 exclude_types: {:array, :string},
72 exclude_visibilities: {:array, :string},
73 reblogs: :boolean,
74 with_muted: :boolean,
75 account_ap_id: :string
76 }
77
78 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
79 changeset.changes
80 end
81
82 defp restrict(query, :exclude_types, %{exclude_types: mastodon_types = [_ | _]}) do
83 ap_types =
84 mastodon_types
85 |> Enum.map(&Activity.from_mastodon_notification_type/1)
86 |> Enum.filter(& &1)
87
88 query
89 |> where([q, a], not fragment("? @> ARRAY[?->>'type']::varchar[]", ^ap_types, a.data))
90 end
91
92 defp restrict(query, :account_ap_id, %{account_ap_id: account_ap_id}) do
93 where(query, [n, a], a.actor == ^account_ap_id)
94 end
95
96 defp restrict(query, _, _), do: query
97 end