Mastodon API, streaming: Add pleroma.direct_conversation_id to the conversation stream event payload.

This commit is contained in:
eugenijm 2019-10-31 03:44:27 +03:00
parent a304a2a845
commit ed29be24cb
6 changed files with 45 additions and 6 deletions

View file

@ -27,6 +27,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Mastodon API: Add `pleroma.direct_conversation_id` to the status endpoint (`GET /api/v1/statuses/:id`) - Mastodon API: Add `pleroma.direct_conversation_id` to the status endpoint (`GET /api/v1/statuses/:id`)
- Mastodon API: `pleroma.thread_muted` to the Status entity - Mastodon API: `pleroma.thread_muted` to the Status entity
- Mastodon API: Mark the direct conversation as read for the author when they send a new direct message - Mastodon API: Mark the direct conversation as read for the author when they send a new direct message
- Mastodon API, streaming: Add `pleroma.direct_conversation_id` to the `conversation` stream event payload.
</details> </details>
### Added ### Added

View file

@ -34,7 +34,11 @@ def render("participation.json", %{participation: participation, for: user}) do
id: participation.id |> to_string(), id: participation.id |> to_string(),
accounts: render(AccountView, "index.json", users: users, as: :user), accounts: render(AccountView, "index.json", users: users, as: :user),
unread: !participation.read, unread: !participation.read,
last_status: render(StatusView, "show.json", activity: activity, for: user) last_status:
render(StatusView, "show.json",
activity: activity,
direct_conversation_id: participation.id
)
} }
end end
end end

View file

@ -243,7 +243,8 @@ def render("show.json", %{activity: %{data: %{"object" => _object}} = activity}
end end
direct_conversation_id = direct_conversation_id =
with {_, true} <- {:include_id, opts[:with_direct_conversation_id]}, with {_, nil} <- {:direct_conversation_id, opts[:direct_conversation_id]},
{_, true} <- {:include_id, opts[:with_direct_conversation_id]},
{_, %User{} = for_user} <- {:for_user, opts[:for]}, {_, %User{} = for_user} <- {:for_user, opts[:for]},
%{data: %{"context" => context}} when is_binary(context) <- activity, %{data: %{"context" => context}} when is_binary(context) <- activity,
%Conversation{} = conversation <- Conversation.get_for_ap_id(context), %Conversation{} = conversation <- Conversation.get_for_ap_id(context),
@ -251,6 +252,9 @@ def render("show.json", %{activity: %{data: %{"object" => _object}} = activity}
Participation.for_user_and_conversation(for_user, conversation) do Participation.for_user_and_conversation(for_user, conversation) do
participation_id participation_id
else else
{:direct_conversation_id, participation_id} when is_integer(participation_id) ->
participation_id
_e -> _e ->
nil nil
end end

View file

@ -30,5 +30,6 @@ test "represents a Mastodon Conversation entity" do
assert [account] = conversation.accounts assert [account] = conversation.accounts
assert account.id == other_user.id assert account.id == other_user.id
assert conversation.last_status.pleroma.direct_conversation_id == participation.id
end end
end end

View file

@ -7,6 +7,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Bookmark alias Pleroma.Bookmark
alias Pleroma.Conversation.Participation
alias Pleroma.HTML alias Pleroma.HTML
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.Repo alias Pleroma.Repo
@ -23,10 +24,11 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
:ok :ok
end end
test "returns the direct conversation id when given the `with_conversation_id` option" do test "loads and returns the direct conversation id when given the `with_direct_conversation_id` option" do
user = insert(:user) user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"}) {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
[participation] = Participation.for_user(user)
status = status =
StatusView.render("show.json", StatusView.render("show.json",
@ -35,7 +37,26 @@ test "returns the direct conversation id when given the `with_conversation_id` o
for: user for: user
) )
assert status[:pleroma][:direct_conversation_id] assert status[:pleroma][:direct_conversation_id] == participation.id
status = StatusView.render("show.json", activity: activity, for: user)
assert status[:pleroma][:direct_conversation_id] == nil
end
test "returns the direct conversation id when given the `direct_conversation_id` option" do
user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
[participation] = Participation.for_user(user)
status =
StatusView.render("show.json",
activity: activity,
direct_conversation_id: participation.id,
for: user
)
assert status[:pleroma][:direct_conversation_id] == participation.id
end end
test "returns a temporary ap_id based user for activities missing db users" do test "returns a temporary ap_id based user for activities missing db users" do

View file

@ -7,6 +7,7 @@ defmodule Pleroma.Web.StreamerTest do
import Pleroma.Factory import Pleroma.Factory
alias Pleroma.Conversation.Participation
alias Pleroma.List alias Pleroma.List
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
@ -481,7 +482,14 @@ test "it sends conversation update to the 'direct' stream", %{} do
task = task =
Task.async(fn -> Task.async(fn ->
assert_receive {:text, _received_event}, 4_000 assert_receive {:text, received_event}, 4_000
assert %{"event" => "conversation", "payload" => received_payload} =
Jason.decode!(received_event)
assert %{"last_status" => last_status} = Jason.decode!(received_payload)
[participation] = Participation.for_user(user)
assert last_status["pleroma"]["direct_conversation_id"] == participation.id
end) end)
Streamer.add_socket( Streamer.add_socket(
@ -498,7 +506,7 @@ test "it sends conversation update to the 'direct' stream", %{} do
Task.await(task) Task.await(task)
end end
test "it doesn't send conversation update to the 'direct' streamj when the last message in the conversation is deleted" do test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted" do
user = insert(:user) user = insert(:user)
another_user = insert(:user) another_user = insert(:user)