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