1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ConversationTest do
8 alias Pleroma.Conversation
10 alias Pleroma.Web.CommonAPI
12 import Pleroma.Factory
14 clear_config_all([:instance, :federating]) do
15 Pleroma.Config.put([:instance, :federating], true)
18 test "it goes through old direct conversations" do
20 other_user = insert(:user)
23 CommonAPI.post(user, %{"visibility" => "direct", "status" => "hey @#{other_user.nickname}"})
25 Pleroma.Tests.ObanHelpers.perform_all()
27 Repo.delete_all(Conversation)
28 Repo.delete_all(Conversation.Participation)
30 refute Repo.one(Conversation)
32 Conversation.bump_for_all_activities()
34 assert Repo.one(Conversation)
35 [participation, _p2] = Repo.all(Conversation.Participation)
37 assert participation.read
40 test "it creates a conversation for given ap_id" do
41 assert {:ok, %Conversation{} = conversation} =
42 Conversation.create_for_ap_id("https://some_ap_id")
44 # Inserting again returns the same
45 assert {:ok, conversation_two} = Conversation.create_for_ap_id("https://some_ap_id")
46 assert conversation_two.id == conversation.id
49 test "public posts don't create conversations" do
51 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey"})
53 object = Pleroma.Object.normalize(activity)
54 context = object.data["context"]
56 conversation = Conversation.get_for_ap_id(context)
61 test "it creates or updates a conversation and participations for a given DM" do
63 jafnhar = insert(:user, local: false)
67 CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"})
69 object = Pleroma.Object.normalize(activity)
70 context = object.data["context"]
73 Conversation.get_for_ap_id(context)
74 |> Repo.preload(:participations)
78 assert Enum.find(conversation.participations, fn %{user_id: user_id} -> har.id == user_id end)
80 assert Enum.find(conversation.participations, fn %{user_id: user_id} ->
85 CommonAPI.post(jafnhar, %{
86 "status" => "Hey @#{har.nickname}",
87 "visibility" => "direct",
88 "in_reply_to_status_id" => activity.id
91 object = Pleroma.Object.normalize(activity)
92 context = object.data["context"]
95 Conversation.get_for_ap_id(context)
96 |> Repo.preload(:participations)
98 assert conversation_two.id == conversation.id
100 assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
104 assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
105 jafnhar.id == user_id
109 CommonAPI.post(tridi, %{
110 "status" => "Hey @#{har.nickname}",
111 "visibility" => "direct",
112 "in_reply_to_status_id" => activity.id
115 object = Pleroma.Object.normalize(activity)
116 context = object.data["context"]
119 Conversation.get_for_ap_id(context)
120 |> Repo.preload([:participations, :users])
122 assert conversation_three.id == conversation.id
124 assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
128 assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
129 jafnhar.id == user_id
132 assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
136 assert Enum.find(conversation_three.users, fn %{id: user_id} ->
140 assert Enum.find(conversation_three.users, fn %{id: user_id} ->
141 jafnhar.id == user_id
144 assert Enum.find(conversation_three.users, fn %{id: user_id} ->
149 test "create_or_bump_for returns the conversation with participations" do
151 jafnhar = insert(:user, local: false)
154 CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"})
156 {:ok, conversation} = Conversation.create_or_bump_for(activity)
158 assert length(conversation.participations) == 2
161 CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "public"})
163 assert {:error, _} = Conversation.create_or_bump_for(activity)
166 test "create_or_bump_for does not normalize objects before checking the activity type" do
168 note_id = note.data["id"]
170 refute Object.get_by_ap_id(note_id)
172 Tesla.Mock.mock(fn env ->
175 # TODO: add attributedTo and tag to the note factory
178 |> Map.put("attributedTo", note.data["actor"])
179 |> Map.put("tag", [])
182 %Tesla.Env{status: 200, body: body}
189 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
190 "actor" => note.data["actor"],
191 "to" => [note.data["actor"]],
197 Conversation.create_or_bump_for(undo)
199 refute Object.get_by_ap_id(note_id)