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
10 alias Pleroma.Notification
11 alias Pleroma.Pagination
12 alias Pleroma.ScheduledActivity
14 alias Pleroma.Web.CommonAPI
16 @spec follow(User.t(), User.t(), map) :: {:ok, User.t()} | {:error, String.t()}
17 def follow(follower, followed, params \\ %{}) do
19 if not User.following?(follower, followed) do
20 CommonAPI.follow(follower, followed)
22 {:ok, follower, followed, nil}
25 with {:ok, follower, _followed, _} <- result do
26 options = cast_params(params)
27 set_reblogs_visibility(options[:reblogs], result)
32 defp set_reblogs_visibility(false, {:ok, follower, followed, _}) do
33 CommonAPI.hide_reblogs(follower, followed)
36 defp set_reblogs_visibility(_, {:ok, follower, followed, _}) do
37 CommonAPI.show_reblogs(follower, followed)
40 @spec get_followers(User.t(), map()) :: list(User.t())
41 def get_followers(user, params \\ %{}) do
43 |> User.get_followers_query()
44 |> Pagination.fetch_paginated(params)
47 def get_friends(user, params \\ %{}) do
49 |> User.get_friends_query()
50 |> Pagination.fetch_paginated(params)
53 def get_notifications(user, params \\ %{}) do
54 options = cast_params(params)
57 |> Notification.for_user_query(options)
58 |> restrict(:include_types, options)
59 |> restrict(:exclude_types, options)
60 |> restrict(:account_ap_id, options)
61 |> Pagination.fetch_paginated(params)
64 def get_scheduled_activities(user, params \\ %{}) do
66 |> ScheduledActivity.for_user_query()
67 |> Pagination.fetch_paginated(params)
70 defp cast_params(params) do
72 exclude_types: {:array, :string},
73 include_types: {:array, :string},
74 exclude_visibilities: {:array, :string},
77 account_ap_id: :string
80 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
84 defp restrict(query, :include_types, %{include_types: mastodon_types = [_ | _]}) do
85 ap_types = convert_and_filter_mastodon_types(mastodon_types)
87 where(query, [q, a], fragment("? @> ARRAY[?->>'type']::varchar[]", ^ap_types, a.data))
90 defp restrict(query, :exclude_types, %{exclude_types: mastodon_types = [_ | _]}) do
91 ap_types = convert_and_filter_mastodon_types(mastodon_types)
93 where(query, [q, a], not fragment("? @> ARRAY[?->>'type']::varchar[]", ^ap_types, a.data))
96 defp restrict(query, :account_ap_id, %{account_ap_id: account_ap_id}) do
97 where(query, [n, a], a.actor == ^account_ap_id)
100 defp restrict(query, _, _), do: query
102 defp convert_and_filter_mastodon_types(types) do
104 |> Enum.map(&Activity.from_mastodon_notification_type/1)