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
14 alias Pleroma.Web.CommonAPI
16 def follow(follower, followed, params \\ %{}) do
17 options = cast_params(params)
18 reblogs = options[:reblogs]
21 if not User.following?(follower, followed) do
22 CommonAPI.follow(follower, followed)
24 {:ok, follower, followed, nil}
27 with {:ok, follower, followed, _} <- result do
30 false -> CommonAPI.hide_reblogs(follower, followed)
31 _ -> CommonAPI.show_reblogs(follower, followed)
34 {:ok, follower} -> {:ok, follower}
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(:exclude_types, options)
58 |> Pagination.fetch_paginated(params)
61 def get_scheduled_activities(user, params \\ %{}) do
63 |> ScheduledActivity.for_user_query()
64 |> Pagination.fetch_paginated(params)
67 defp cast_params(params) do
69 exclude_types: {:array, :string},
74 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
78 defp restrict(query, :exclude_types, %{exclude_types: mastodon_types = [_ | _]}) do
81 |> Enum.map(&Activity.from_mastodon_notification_type/1)
85 |> where([q, a], not fragment("? @> ARRAY[?->>'type']::varchar[]", ^ap_types, a.data))
88 defp restrict(query, _, _), do: query