logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

twitter_api_controller.ex (2826B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.TwitterAPI.Controller do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.Notification
  7. alias Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug
  8. alias Pleroma.Plugs.OAuthScopesPlug
  9. alias Pleroma.User
  10. alias Pleroma.Web.OAuth.Token
  11. alias Pleroma.Web.TwitterAPI.TokenView
  12. require Logger
  13. plug(
  14. OAuthScopesPlug,
  15. %{scopes: ["write:notifications"]} when action == :mark_notifications_as_read
  16. )
  17. plug(
  18. :skip_plug,
  19. [OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug] when action == :confirm_email
  20. )
  21. plug(:skip_plug, OAuthScopesPlug when action in [:oauth_tokens, :revoke_token])
  22. action_fallback(:errors)
  23. def confirm_email(conn, %{"user_id" => uid, "token" => token}) do
  24. with %User{} = user <- User.get_cached_by_id(uid),
  25. true <- user.local and user.confirmation_pending and user.confirmation_token == token,
  26. {:ok, _} <-
  27. user
  28. |> User.confirmation_changeset(need_confirmation: false)
  29. |> User.update_and_set_cache() do
  30. redirect(conn, to: "/")
  31. end
  32. end
  33. def oauth_tokens(%{assigns: %{user: user}} = conn, _params) do
  34. with oauth_tokens <- Token.get_user_tokens(user) do
  35. conn
  36. |> put_view(TokenView)
  37. |> render("index.json", %{tokens: oauth_tokens})
  38. end
  39. end
  40. def revoke_token(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
  41. Token.delete_user_token(user, id)
  42. json_reply(conn, 201, "")
  43. end
  44. defp errors(conn, {:param_cast, _}) do
  45. conn
  46. |> put_status(400)
  47. |> json("Invalid parameters")
  48. end
  49. defp errors(conn, _) do
  50. conn
  51. |> put_status(500)
  52. |> json("Something went wrong")
  53. end
  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. def mark_notifications_as_read(
  60. %{assigns: %{user: user}} = conn,
  61. %{"latest_id" => latest_id} = params
  62. ) do
  63. Notification.set_read_up_to(user, latest_id)
  64. notifications = Notification.for_user(user, params)
  65. conn
  66. # XXX: This is a hack because pleroma-fe still uses that API.
  67. |> put_view(Pleroma.Web.MastodonAPI.NotificationView)
  68. |> render("index.json", %{notifications: notifications, for: user})
  69. end
  70. def mark_notifications_as_read(%{assigns: %{user: _user}} = conn, _) do
  71. bad_request_reply(conn, "You need to specify latest_id")
  72. end
  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. defp error_json(conn, error_message) do
  78. %{"error" => error_message, "request" => conn.request_path} |> Jason.encode!()
  79. end
  80. end