mastodon_api_controller.ex 5.58 KB
Newer Older
1 2
defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
  use Pleroma.Web, :controller
3
  alias Pleroma.{Repo, Activity, User, Notification}
lain's avatar
lain committed
4 5
  alias Pleroma.Web.OAuth.App
  alias Pleroma.Web
lain's avatar
lain committed
6 7 8
  alias Pleroma.Web.MastodonAPI.{StatusView, AccountView}
  alias Pleroma.Web.ActivityPub.ActivityPub
  alias Pleroma.Web.TwitterAPI.TwitterAPI
lain's avatar
lain committed
9
  alias Pleroma.Web.CommonAPI
10
  import Logger
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

  def create_app(conn, params) do
    with cs <- App.register_changeset(%App{}, params) |> IO.inspect,
         {:ok, app} <- Repo.insert(cs) |> IO.inspect do
      res = %{
        id: app.id,
        client_id: app.client_id,
        client_secret: app.client_secret
      }

      json(conn, res)
    end
  end

  def verify_credentials(%{assigns: %{user: user}} = conn, params) do
lain's avatar
lain committed
26 27 28 29 30 31 32 33 34 35
    account = AccountView.render("account.json", %{user: user})
    json(conn, account)
  end

  def masto_instance(conn, _params) do
    response = %{
      uri: Web.base_url,
      title: Web.base_url,
      description: "A Pleroma instance, an alternative fediverse server",
      version: "Pleroma Dev"
36 37
    }

lain's avatar
lain committed
38
    json(conn, response)
39
  end
lain's avatar
lain committed
40 41 42

  def home_timeline(%{assigns: %{user: user}} = conn, params) do
    activities = ActivityPub.fetch_activities([user.ap_id | user.following], Map.put(params, "type", "Create"))
lain's avatar
lain committed
43
    |> Enum.reverse
lain's avatar
lain committed
44 45 46 47 48 49 50 51 52
    render conn, StatusView, "index.json", %{activities: activities, for: user, as: :activity}
  end

  def public_timeline(%{assigns: %{user: user}} = conn, params) do
    params = params
    |> Map.put("type", "Create")
    |> Map.put("local_only", !!params["local"])

    activities = ActivityPub.fetch_public_activities(params)
lain's avatar
lain committed
53
    |> Enum.reverse
lain's avatar
lain committed
54 55 56 57

    render conn, StatusView, "index.json", %{activities: activities, for: user, as: :activity}
  end

lain's avatar
lain committed
58 59 60 61 62 63 64 65 66 67 68 69 70
  def user_statuses(%{assigns: %{user: user}} = conn, params) do
    with %User{ap_id: ap_id} <- Repo.get(User, params["id"]) do
      params = params
      |> Map.put("type", "Create")
      |> Map.put("actor_id", ap_id)

      activities = ActivityPub.fetch_activities([], params)
      |> Enum.reverse

      render conn, StatusView, "index.json", %{activities: activities, for: user, as: :activity}
    end
  end

lain's avatar
lain committed
71 72 73 74 75 76
  def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
    with %Activity{} = activity <- Repo.get(Activity, id) do
      render conn, StatusView, "status.json", %{activity: activity, for: user}
    end
  end

lain's avatar
lain committed
77 78 79
  def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
    with %Activity{} = activity <- Repo.get(Activity, id),
         activities <- ActivityPub.fetch_activities_for_context(activity.data["object"]["context"]),
lain's avatar
lain committed
80 81
         activities <- activities |> Enum.filter(fn (%{id: aid}) -> to_string(aid) != to_string(id) end),
         grouped_activities <- Enum.group_by(activities, fn (%{id: id}) -> id < activity.id end) do
lain's avatar
lain committed
82
      result = %{
lain's avatar
lain committed
83 84
        ancestors: StatusView.render("index.json", for: user, activities: grouped_activities[true] || [], as: :activity) |> Enum.reverse,
        descendants: StatusView.render("index.json", for: user, activities: grouped_activities[false] || [], as: :activity) |> Enum.reverse,
lain's avatar
lain committed
85 86 87 88 89 90
      }

      json(conn, result)
    end
  end

lain's avatar
lain committed
91 92 93 94 95 96 97 98 99 100 101
  def post_status(%{assigns: %{user: user}} = conn, %{"status" => status} = params) do
    l = status |> String.trim |> String.length

    params = params
    |> Map.put("in_reply_to_status_id", params["in_reply_to_id"])

    if l > 0 && l < 5000 do
      {:ok, activity} = TwitterAPI.create_status(user, params)
      render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}
    end
  end
lain's avatar
lain committed
102 103 104 105 106 107 108 109 110 111 112

  def delete_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
    with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
      json(conn, %{})
    else
      _e ->
        conn
        |> put_status(403)
        |> json(%{error: "Can't delete this post"})
    end
  end
lain's avatar
lain committed
113 114 115 116 117 118 119

  def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
    with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(ap_id_or_id, user),
         %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
      render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}
    end
  end
lain's avatar
lain committed
120 121

  def fav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
lain's avatar
lain committed
122 123 124 125 126 127 128 129
    with {:ok, _fav, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user),
         %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
      render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}
    end
  end

  def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
    with {:ok, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user),
lain's avatar
lain committed
130 131 132 133
         %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
      render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}
    end
  end
134

135 136 137 138 139 140 141 142 143 144 145 146 147 148
  def notifications(%{assigns: %{user: user}} = conn, params) do
    notifications = Notification.for_user(user, params)
    result = Enum.map(notifications, fn (%{id: id, activity: activity, inserted_at: created_at}) ->
      actor = User.get_cached_by_ap_id(activity.data["actor"])
      case activity.data["type"] do
        "Create" -> %{ id: id, type: "mention", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: activity})}
        _ -> nil
      end
    end)
    |> Enum.filter(&(&1))

    json(conn, result)
  end

149 150 151 152
  def empty_array(conn, _) do
    Logger.debug("Unimplemented, returning an empty array")
    json(conn, [])
  end
153
end