1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.MastodonAPI do
9 alias Pleroma.Notification
10 alias Pleroma.Pagination
11 alias Pleroma.ScheduledActivity
13 alias Pleroma.Web.CommonAPI
15 @spec follow(User.t(), User.t(), map) :: {:ok, User.t()} | {:error, String.t()}
16 def follow(follower, followed, params \\ %{}) do
18 if not User.following?(follower, followed) do
19 CommonAPI.follow(follower, followed)
21 {:ok, follower, followed, nil}
24 with {:ok, follower, _followed, _} <- result do
25 options = cast_params(params)
26 set_reblogs_visibility(options[:reblogs], result)
31 defp set_reblogs_visibility(false, {:ok, follower, followed, _}) do
32 CommonAPI.hide_reblogs(follower, followed)
35 defp set_reblogs_visibility(_, {:ok, follower, followed, _}) do
36 CommonAPI.show_reblogs(follower, followed)
39 @spec get_followers(User.t(), map()) :: list(User.t())
40 def get_followers(user, params \\ %{}) do
42 |> User.get_followers_query()
43 |> Pagination.fetch_paginated(params)
46 def get_friends(user, params \\ %{}) do
48 |> User.get_friends_query()
49 |> Pagination.fetch_paginated(params)
52 def get_notifications(user, params \\ %{}) do
53 options = cast_params(params)
56 |> Notification.for_user_query(options)
57 |> restrict(:include_types, options)
58 |> restrict(:exclude_types, options)
59 |> restrict(:account_ap_id, options)
60 |> Pagination.fetch_paginated(params)
63 def get_scheduled_activities(user, params \\ %{}) do
65 |> ScheduledActivity.for_user_query()
66 |> Pagination.fetch_paginated(params)
69 defp cast_params(params) do
71 exclude_types: {:array, :string},
72 include_types: {:array, :string},
73 exclude_visibilities: {:array, :string},
76 account_ap_id: :string
79 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
83 defp restrict(query, :include_types, %{include_types: mastodon_types = [_ | _]}) do
84 where(query, [n], n.type in ^mastodon_types)
87 defp restrict(query, :exclude_types, %{exclude_types: mastodon_types = [_ | _]}) do
88 where(query, [n], n.type not in ^mastodon_types)
91 defp restrict(query, :account_ap_id, %{account_ap_id: account_ap_id}) do
92 where(query, [n, a], a.actor == ^account_ap_id)
95 defp restrict(query, _, _), do: query