1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.Controller do
6 use Pleroma.Web, :controller
9 alias Pleroma.Web.OAuth.Token
10 alias Pleroma.Web.Plugs.OAuthScopesPlug
11 alias Pleroma.Web.TwitterAPI.TokenView
15 plug(:skip_auth when action == :confirm_email)
16 plug(:skip_plug, OAuthScopesPlug when action in [:oauth_tokens, :revoke_token])
18 action_fallback(:errors)
20 def confirm_email(conn, %{"user_id" => uid, "token" => token}) do
21 case User.get_cached_by_id(uid) do
22 %User{local: true, is_confirmed: false, confirmation_token: ^token} = user ->
23 case User.confirm(user) do
25 redirect(conn, to: "/")
28 json_reply(conn, 400, "Unable to confirm")
31 %User{is_confirmed: true} ->
32 json_reply(conn, 400, "Already verified email")
35 json_reply(conn, 400, "Couldn't verify email")
39 def oauth_tokens(%{assigns: %{user: user}} = conn, _params) do
40 with oauth_tokens <- Token.get_user_tokens(user) do
42 |> put_view(TokenView)
43 |> render("index.json", %{tokens: oauth_tokens})
47 def revoke_token(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
48 Token.delete_user_token(user, id)
50 json_reply(conn, 201, "")
53 defp errors(conn, {:param_cast, _}) do
56 |> json("Invalid parameters")
59 defp errors(conn, _) do
62 |> json("Something went wrong")
65 defp json_reply(conn, status, json) do
67 |> put_resp_content_type("application/json")
68 |> send_resp(status, json)