mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-12-25 04:53:06 +00:00
Pleroma Conversations API: Add a way to set recipients.
This commit is contained in:
parent
eee98aaa73
commit
3af6d14da7
|
@ -99,4 +99,24 @@ def get(nil), do: nil
|
|||
def get(id) do
|
||||
Repo.get(__MODULE__, id)
|
||||
end
|
||||
|
||||
def set_recipients(participation, user_ids) do
|
||||
Repo.transaction(fn ->
|
||||
query =
|
||||
from(r in RecipientShip,
|
||||
where: r.participation_id == ^participation.id
|
||||
)
|
||||
|
||||
Repo.delete_all(query)
|
||||
|
||||
users =
|
||||
from(u in User,
|
||||
where: u.id in ^user_ids
|
||||
)
|
||||
|> Repo.all()
|
||||
|
||||
RecipientShip.create(users, participation)
|
||||
:ok
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,7 +12,7 @@ defmodule Pleroma.Web.MastodonAPI.ConversationView do
|
|||
alias Pleroma.Web.MastodonAPI.StatusView
|
||||
|
||||
def render("participation.json", %{participation: participation, user: user}) do
|
||||
participation = Repo.preload(participation, conversation: :users)
|
||||
participation = Repo.preload(participation, conversation: :users, recipients: [])
|
||||
|
||||
last_activity_id =
|
||||
with nil <- participation.last_activity_id do
|
||||
|
@ -37,11 +37,20 @@ def render("participation.json", %{participation: participation, user: user}) do
|
|||
as: :user
|
||||
})
|
||||
|
||||
recipients =
|
||||
AccountView.render("accounts.json", %{
|
||||
users: participation.recipients,
|
||||
as: :user
|
||||
})
|
||||
|
||||
%{
|
||||
id: participation.id |> to_string(),
|
||||
accounts: accounts,
|
||||
unread: !participation.read,
|
||||
last_status: last_status
|
||||
last_status: last_status,
|
||||
pleroma: %{
|
||||
recipients: recipients
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,6 +10,7 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
|
|||
alias Pleroma.Conversation.Participation
|
||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
alias Pleroma.Web.MastodonAPI.StatusView
|
||||
alias Pleroma.Web.MastodonAPI.ConversationView
|
||||
alias Pleroma.Repo
|
||||
|
||||
def conversation_statuses(
|
||||
|
@ -46,4 +47,20 @@ def conversation_statuses(
|
|||
|> render("index.json", %{activities: activities, for: user, as: :activity})
|
||||
end
|
||||
end
|
||||
|
||||
def update_conversation(
|
||||
%{assigns: %{user: user}} = conn,
|
||||
%{"id" => participation_id, "recipients" => recipients}
|
||||
) do
|
||||
participation =
|
||||
participation_id
|
||||
|> Participation.get()
|
||||
|
||||
with true <- user.id == participation.user_id,
|
||||
{:ok, _} <- Participation.set_recipients(participation, recipients) do
|
||||
conn
|
||||
|> put_view(ConversationView)
|
||||
|> render("participation.json", %{participation: participation, user: user})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -263,6 +263,7 @@ defmodule Pleroma.Web.Router do
|
|||
scope [] do
|
||||
pipe_through(:oauth_write)
|
||||
get("/conversations/:id/statuses", PleromaAPIController, :conversation_statuses)
|
||||
patch("/conversations/:id", PleromaAPIController, :update_conversation)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
40
test/web/mastodon_api/conversation_view_test.exs
Normal file
40
test/web/mastodon_api/conversation_view_test.exs
Normal file
|
@ -0,0 +1,40 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.MastodonAPI.ConversationViewTest do
|
||||
use Pleroma.DataCase
|
||||
|
||||
alias Pleroma.Web.CommonAPI
|
||||
alias Pleroma.Conversation.Participation
|
||||
alias Pleroma.Web.MastodonAPI.ConversationView
|
||||
|
||||
import Pleroma.Factory
|
||||
|
||||
test "represents a Mastodon Conversation entity" do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
|
||||
{:ok, activity} =
|
||||
CommonAPI.post(user, %{"status" => "hey @#{other_user.nickname}", "visibility" => "direct"})
|
||||
|
||||
[participation] = Participation.for_user_with_last_activity_id(user)
|
||||
|
||||
assert participation
|
||||
|
||||
conversation =
|
||||
ConversationView.render("participation.json", %{participation: participation, user: user})
|
||||
|
||||
assert conversation.id == participation.id |> to_string()
|
||||
assert conversation.last_status.id == activity.id
|
||||
|
||||
assert [account] = conversation.accounts
|
||||
assert account.id == other_user.id
|
||||
|
||||
assert recipients = conversation.pleroma.recipients
|
||||
recipient_ids = recipients |> Enum.map(& &1.id)
|
||||
|
||||
assert user.id in recipient_ids
|
||||
assert other_user.id in recipient_ids
|
||||
end
|
||||
end
|
|
@ -7,6 +7,7 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
|
|||
|
||||
alias Pleroma.Conversation.Participation
|
||||
alias Pleroma.Web.CommonAPI
|
||||
alias Pleroma.Repo
|
||||
|
||||
import Pleroma.Factory
|
||||
|
||||
|
@ -42,4 +43,34 @@ test "/api/v1/pleroma/conversations/:id/statuses", %{conn: conn} do
|
|||
id_two = activity_two.id
|
||||
assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
|
||||
end
|
||||
|
||||
test "PATCH /api/v1/pleroma/conversations/:id", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
|
||||
{:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
|
||||
|
||||
[participation] = Participation.for_user(user)
|
||||
|
||||
participation = Repo.preload(participation, :recipients)
|
||||
|
||||
assert [user] == participation.recipients
|
||||
assert other_user not in participation.recipients
|
||||
|
||||
result =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
|
||||
"recipients" => [user.id, other_user.id]
|
||||
})
|
||||
|> json_response(200)
|
||||
|
||||
assert result["id"] == participation.id |> to_string
|
||||
|
||||
assert recipients = result["pleroma"]["recipients"]
|
||||
recipient_ids = Enum.map(recipients, & &1["id"])
|
||||
|
||||
assert user.id in recipient_ids
|
||||
assert other_user.id in recipient_ids
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue