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
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
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
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(
|> 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
scope [] do
pipe_through(:oauth_write)
get("/conversations/:id/statuses", PleromaAPIController, :conversation_statuses)
+ patch("/conversations/:id", PleromaAPIController, :update_conversation)
end
end
--- /dev/null
+# 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
alias Pleroma.Conversation.Participation
alias Pleroma.Web.CommonAPI
+ alias Pleroma.Repo
import Pleroma.Factory
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