1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
13 alias Pleroma.SubscriptionNotification
15 alias Pleroma.Web.CommonAPI
17 @spec follow(User.t(), User.t(), map) :: {:ok, User.t()} | {:error, String.t()}
18 def follow(follower, followed, params \\ %{}) do
20 if not User.following?(follower, followed) do
21 CommonAPI.follow(follower, followed)
23 {:ok, follower, followed, nil}
26 with {:ok, follower, _followed, _} <- result do
27 options = cast_params(params)
29 case reblogs_visibility(options[:reblogs], result) do
30 {:ok, follower} -> {:ok, follower}
36 defp reblogs_visibility(false, {:ok, follower, followed, _}) do
37 CommonAPI.hide_reblogs(follower, followed)
40 defp reblogs_visibility(_, {:ok, follower, followed, _}) do
41 CommonAPI.show_reblogs(follower, followed)
44 @spec get_followers(User.t(), map()) :: list(User.t())
45 def get_followers(user, params \\ %{}) do
47 |> User.get_followers_query()
48 |> Pagination.fetch_paginated(params)
51 def get_friends(user, params \\ %{}) do
53 |> User.get_friends_query()
54 |> Pagination.fetch_paginated(params)
57 def get_notifications(user, params \\ %{}) do
58 options = cast_params(params)
61 |> Notification.for_user_query(options)
62 |> restrict(:exclude_types, options)
63 |> Pagination.fetch_paginated(params)
66 def get_subscription_notifications(user, params \\ %{}) do
67 options = cast_params(params)
70 |> SubscriptionNotification.for_user_query(options)
71 |> restrict(:exclude_types, options)
72 |> Pagination.fetch_paginated(params)
75 def get_scheduled_activities(user, params \\ %{}) do
77 |> ScheduledActivity.for_user_query()
78 |> Pagination.fetch_paginated(params)
81 defp cast_params(params) do
83 exclude_types: {:array, :string},
88 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
92 defp restrict(query, :exclude_types, %{exclude_types: mastodon_types = [_ | _]}) do
95 |> Enum.map(&Activity.from_mastodon_notification_type/1)
99 |> where([q, a], not fragment("? @> ARRAY[?->>'type']::varchar[]", ^ap_types, a.data))
102 defp restrict(query, _, _), do: query