X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Fconversation_test.exs;h=056a0e920eb89803d3253b62604d6a45b3eed8e8;hb=f7e623c11c4b6f4f323a4317e9489092be73f9cd;hp=1c9d485ff5c063e302266ce57d5dae05fe4ad41b;hpb=280172f6f6d74872349e3b4e6f1feaa9c95b3900;p=akkoma
diff --git a/test/conversation_test.exs b/test/conversation_test.exs
index 1c9d485ff..056a0e920 100644
--- a/test/conversation_test.exs
+++ b/test/conversation_test.exs
@@ -1,14 +1,40 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ConversationTest do
use Pleroma.DataCase
+ alias Pleroma.Activity
alias Pleroma.Conversation
+ alias Pleroma.Object
alias Pleroma.Web.CommonAPI
import Pleroma.Factory
+ setup_all do: clear_config([:instance, :federating], true)
+
+ test "it goes through old direct conversations" do
+ user = insert(:user)
+ other_user = insert(:user)
+
+ {:ok, _activity} =
+ CommonAPI.post(user, %{"visibility" => "direct", "status" => "hey @#{other_user.nickname}"})
+
+ Pleroma.Tests.ObanHelpers.perform_all()
+
+ Repo.delete_all(Conversation)
+ Repo.delete_all(Conversation.Participation)
+
+ refute Repo.one(Conversation)
+
+ Conversation.bump_for_all_activities()
+
+ assert Repo.one(Conversation)
+ [participation, _p2] = Repo.all(Conversation.Participation)
+
+ assert participation.read
+ end
+
test "it creates a conversation for given ap_id" do
assert {:ok, %Conversation{} = conversation} =
Conversation.create_for_ap_id("https://some_ap_id")
@@ -22,7 +48,8 @@ defmodule Pleroma.ConversationTest do
user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey"})
- context = activity.data["object"]["context"]
+ object = Pleroma.Object.normalize(activity)
+ context = object.data["context"]
conversation = Conversation.get_for_ap_id(context)
@@ -31,23 +58,26 @@ defmodule Pleroma.ConversationTest do
test "it creates or updates a conversation and participations for a given DM" do
har = insert(:user)
- jafnhar = insert(:user)
+ jafnhar = insert(:user, local: false)
tridi = insert(:user)
{:ok, activity} =
CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"})
- context = activity.data["object"]["context"]
+ object = Pleroma.Object.normalize(activity)
+ context = object.data["context"]
conversation =
Conversation.get_for_ap_id(context)
|> Repo.preload(:participations)
assert conversation
- [har_participation, jafnhar_participation] = conversation.participations
- assert har_participation.user_id == har.id
- assert jafnhar_participation.user_id == jafnhar.id
+ assert Enum.find(conversation.participations, fn %{user_id: user_id} -> har.id == user_id end)
+
+ assert Enum.find(conversation.participations, fn %{user_id: user_id} ->
+ jafnhar.id == user_id
+ end)
{:ok, activity} =
CommonAPI.post(jafnhar, %{
@@ -56,7 +86,8 @@ defmodule Pleroma.ConversationTest do
"in_reply_to_status_id" => activity.id
})
- context = activity.data["object"]["context"]
+ object = Pleroma.Object.normalize(activity)
+ context = object.data["context"]
conversation_two =
Conversation.get_for_ap_id(context)
@@ -64,10 +95,13 @@ defmodule Pleroma.ConversationTest do
assert conversation_two.id == conversation.id
- [har_participation_two, jafnhar_participation_two] = conversation_two.participations
+ assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
+ har.id == user_id
+ end)
- assert har_participation_two.user_id == har.id
- assert jafnhar_participation_two.user_id == jafnhar.id
+ assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
+ jafnhar.id == user_id
+ end)
{:ok, activity} =
CommonAPI.post(tridi, %{
@@ -76,19 +110,90 @@ defmodule Pleroma.ConversationTest do
"in_reply_to_status_id" => activity.id
})
- context = activity.data["object"]["context"]
+ object = Pleroma.Object.normalize(activity)
+ context = object.data["context"]
conversation_three =
Conversation.get_for_ap_id(context)
- |> Repo.preload(:participations)
+ |> Repo.preload([:participations, :users])
assert conversation_three.id == conversation.id
- [har_participation_three, jafnhar_participation_three, tridi_participation] =
- conversation_three.participations
+ assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
+ har.id == user_id
+ end)
+
+ assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
+ jafnhar.id == user_id
+ end)
+
+ assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
+ tridi.id == user_id
+ end)
+
+ assert Enum.find(conversation_three.users, fn %{id: user_id} ->
+ har.id == user_id
+ end)
+
+ assert Enum.find(conversation_three.users, fn %{id: user_id} ->
+ jafnhar.id == user_id
+ end)
+
+ assert Enum.find(conversation_three.users, fn %{id: user_id} ->
+ tridi.id == user_id
+ end)
+ end
+
+ test "create_or_bump_for returns the conversation with participations" do
+ har = insert(:user)
+ jafnhar = insert(:user, local: false)
+
+ {:ok, activity} =
+ CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"})
+
+ {:ok, conversation} = Conversation.create_or_bump_for(activity)
+
+ assert length(conversation.participations) == 2
+
+ {:ok, activity} =
+ CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "public"})
+
+ assert {:error, _} = Conversation.create_or_bump_for(activity)
+ end
- assert har_participation_three.user_id == har.id
- assert jafnhar_participation_three.user_id == jafnhar.id
- assert tridi_participation.user_id == tridi.id
+ test "create_or_bump_for does not normalize objects before checking the activity type" do
+ note = insert(:note)
+ note_id = note.data["id"]
+ Repo.delete(note)
+ refute Object.get_by_ap_id(note_id)
+
+ Tesla.Mock.mock(fn env ->
+ case env.url do
+ ^note_id ->
+ # TODO: add attributedTo and tag to the note factory
+ body =
+ note.data
+ |> Map.put("attributedTo", note.data["actor"])
+ |> Map.put("tag", [])
+ |> Jason.encode!()
+
+ %Tesla.Env{status: 200, body: body}
+ end
+ end)
+
+ undo = %Activity{
+ id: "fake",
+ data: %{
+ "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
+ "actor" => note.data["actor"],
+ "to" => [note.data["actor"]],
+ "object" => note_id,
+ "type" => "Undo"
+ }
+ }
+
+ Conversation.create_or_bump_for(undo)
+
+ refute Object.get_by_ap_id(note_id)
end
end