aa06e263010e7d40d146b2182122c9dd15cc1685
[akkoma] / lib / pleroma / web / twitter_api / twitter_api_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Notification
9 alias Pleroma.User
10 alias Pleroma.Web.OAuth.Token
11 alias Pleroma.Web.TwitterAPI.TokenView
12
13 require Logger
14
15 action_fallback(:errors)
16
17 def confirm_email(conn, %{"user_id" => uid, "token" => token}) do
18 with %User{info: info} = user <- User.get_cached_by_id(uid),
19 true <- user.local and info.confirmation_pending and info.confirmation_token == token,
20 {:ok, _} <-
21 User.update_info(user, &User.Info.confirmation_changeset(&1, need_confirmation: false)) do
22 redirect(conn, to: "/")
23 end
24 end
25
26 def oauth_tokens(%{assigns: %{user: user}} = conn, _params) do
27 with oauth_tokens <- Token.get_user_tokens(user) do
28 conn
29 |> put_view(TokenView)
30 |> render("index.json", %{tokens: oauth_tokens})
31 end
32 end
33
34 def revoke_token(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
35 Token.delete_user_token(user, id)
36
37 json_reply(conn, 201, "")
38 end
39
40 def errors(conn, {:param_cast, _}) do
41 conn
42 |> put_status(400)
43 |> json("Invalid parameters")
44 end
45
46 def errors(conn, _) do
47 conn
48 |> put_status(500)
49 |> json("Something went wrong")
50 end
51
52 defp json_reply(conn, status, json) do
53 conn
54 |> put_resp_content_type("application/json")
55 |> send_resp(status, json)
56 end
57
58 def notifications_read(%{assigns: %{user: user}} = conn, %{"latest_id" => latest_id} = params) do
59 Notification.set_read_up_to(user, latest_id)
60
61 notifications = Notification.for_user(user, params)
62
63 conn
64 # XXX: This is a hack because pleroma-fe still uses that API.
65 |> put_view(Pleroma.Web.MastodonAPI.NotificationView)
66 |> render("index.json", %{notifications: notifications, for: user})
67 end
68
69 def notifications_read(%{assigns: %{user: _user}} = conn, _) do
70 bad_request_reply(conn, "You need to specify latest_id")
71 end
72
73 defp bad_request_reply(conn, error_message) do
74 json = error_json(conn, error_message)
75 json_reply(conn, 400, json)
76 end
77
78 defp error_json(conn, error_message) do
79 %{"error" => error_message, "request" => conn.request_path} |> Jason.encode!()
80 end
81 end