2019-09-30 09:52:07 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 22:44:49 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-09-30 09:52:07 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.ConversationController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
|
|
|
|
|
|
|
|
alias Pleroma.Conversation.Participation
|
2019-10-02 17:42:40 +00:00
|
|
|
alias Pleroma.Plugs.OAuthScopesPlug
|
2019-09-30 09:52:07 +00:00
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
|
|
|
|
2020-05-05 13:44:46 +00:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
2019-10-02 17:42:40 +00:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action == :index)
|
2020-04-21 13:29:19 +00:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["write:conversations"]} when action != :index)
|
2019-10-02 17:42:40 +00:00
|
|
|
|
2020-05-05 13:44:46 +00:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ConversationOperation
|
|
|
|
|
2019-09-30 09:52:07 +00:00
|
|
|
@doc "GET /api/v1/conversations"
|
|
|
|
def index(%{assigns: %{user: user}} = conn, params) do
|
2020-05-30 10:02:37 +00:00
|
|
|
params = stringify_pagination_params(params)
|
2019-09-30 09:52:07 +00:00
|
|
|
participations = Participation.for_user_with_last_activity_id(user, params)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(participations)
|
|
|
|
|> render("participations.json", participations: participations, for: user)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/conversations/:id/read"
|
2020-05-05 13:44:46 +00:00
|
|
|
def mark_as_read(%{assigns: %{user: user}} = conn, %{id: participation_id}) do
|
2019-09-30 09:52:07 +00:00
|
|
|
with %Participation{} = participation <-
|
|
|
|
Repo.get_by(Participation, id: participation_id, user_id: user.id),
|
|
|
|
{:ok, participation} <- Participation.mark_as_read(participation) do
|
|
|
|
render(conn, "participation.json", participation: participation, for: user)
|
|
|
|
end
|
|
|
|
end
|
2020-05-30 10:02:37 +00:00
|
|
|
|
|
|
|
defp stringify_pagination_params(params) do
|
|
|
|
atom_keys =
|
|
|
|
Pleroma.Pagination.page_keys()
|
|
|
|
|> Enum.map(&String.to_atom(&1))
|
|
|
|
|
|
|
|
str_keys =
|
|
|
|
params
|
|
|
|
|> Map.take(atom_keys)
|
|
|
|
|> Enum.map(fn {key, value} -> {to_string(key), value} end)
|
|
|
|
|> Enum.into(%{})
|
|
|
|
|
|
|
|
params
|
|
|
|
|> Map.delete(atom_keys)
|
|
|
|
|> Map.merge(str_keys)
|
|
|
|
end
|
2019-09-30 09:52:07 +00:00
|
|
|
end
|