Fix tagpolicy to also work with Update
[akkoma] / lib / pleroma / web / twitter_api / controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.TwitterAPI.Controller do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.User
9 alias Pleroma.Web.OAuth.Token
10 alias Pleroma.Web.Plugs.OAuthScopesPlug
11 alias Pleroma.Web.TwitterAPI.TokenView
12
13 require Logger
14
15 plug(:skip_auth when action == :confirm_email)
16 plug(:skip_plug, OAuthScopesPlug when action in [:oauth_tokens, :revoke_token])
17
18 action_fallback(:errors)
19
20 def confirm_email(conn, %{"user_id" => uid, "token" => token}) do
21 with %User{} = user <- User.get_cached_by_id(uid),
22 true <- user.local and !user.is_confirmed and user.confirmation_token == token,
23 {:ok, _} <- User.confirm(user) do
24 redirect(conn, to: "/")
25 end
26 end
27
28 def oauth_tokens(%{assigns: %{user: user}} = conn, _params) do
29 with oauth_tokens <- Token.get_user_tokens(user) do
30 conn
31 |> put_view(TokenView)
32 |> render("index.json", %{tokens: oauth_tokens})
33 end
34 end
35
36 def revoke_token(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
37 Token.delete_user_token(user, id)
38
39 json_reply(conn, 201, "")
40 end
41
42 defp errors(conn, {:param_cast, _}) do
43 conn
44 |> put_status(400)
45 |> json("Invalid parameters")
46 end
47
48 defp errors(conn, _) do
49 conn
50 |> put_status(500)
51 |> json("Something went wrong")
52 end
53
54 defp json_reply(conn, status, json) do
55 conn
56 |> put_resp_content_type("application/json")
57 |> send_resp(status, json)
58 end
59 end