fb713d47c90fa65c067968d761fe9f5a8907f654
[akkoma] / lib / pleroma / web / mastodon_api / mastodon_api.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.Notification
10 alias Pleroma.Pagination
11 alias Pleroma.ScheduledActivity
12 alias Pleroma.User
13 alias Pleroma.Web.CommonAPI
14
15 @spec follow(User.t(), User.t(), map) :: {:ok, User.t()} | {:error, String.t()}
16 def follow(follower, followed, params \\ %{}) do
17 result =
18 if not User.following?(follower, followed) do
19 CommonAPI.follow(follower, followed)
20 else
21 {:ok, follower, followed, nil}
22 end
23
24 with {:ok, follower, _followed, _} <- result do
25 options = cast_params(params)
26 set_reblogs_visibility(options[:reblogs], result)
27 set_subscription(options[:notify], 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 defp set_subscription(true, {:ok, follower, followed, _}) do
41 User.subscribe(follower, followed)
42 end
43
44 defp set_subscription(_, {:ok, follower, followed, _}) do
45 User.unsubscribe(follower, followed)
46 end
47
48 @spec get_followers(User.t(), map()) :: list(User.t())
49 def get_followers(user, params \\ %{}) do
50 user
51 |> User.get_followers_query()
52 |> Pagination.fetch_paginated(params)
53 end
54
55 def get_friends(user, params \\ %{}) do
56 user
57 |> User.get_friends_query()
58 |> Pagination.fetch_paginated(params)
59 end
60
61 def get_notifications(user, params \\ %{}) do
62 options = cast_params(params)
63
64 user
65 |> Notification.for_user_query(options)
66 |> restrict(:include_types, options)
67 |> restrict(:exclude_types, options)
68 |> restrict(:account_ap_id, options)
69 |> Pagination.fetch_paginated(params)
70 end
71
72 def get_scheduled_activities(user, params \\ %{}) do
73 user
74 |> ScheduledActivity.for_user_query()
75 |> Pagination.fetch_paginated(params)
76 end
77
78 defp cast_params(params) do
79 param_types = %{
80 exclude_types: {:array, :string},
81 include_types: {:array, :string},
82 exclude_visibilities: {:array, :string},
83 reblogs: :boolean,
84 with_muted: :boolean,
85 account_ap_id: :string,
86 notify: :boolean
87 }
88
89 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
90 changeset.changes
91 end
92
93 defp restrict(query, :include_types, %{include_types: mastodon_types = [_ | _]}) do
94 where(query, [n], n.type in ^mastodon_types)
95 end
96
97 defp restrict(query, :exclude_types, %{exclude_types: mastodon_types = [_ | _]}) do
98 where(query, [n], n.type not in ^mastodon_types)
99 end
100
101 defp restrict(query, :account_ap_id, %{account_ap_id: account_ap_id}) do
102 where(query, [n, a], a.actor == ^account_ap_id)
103 end
104
105 defp restrict(query, _, _), do: query
106 end