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 Repo.delete_all(Conversation)
26 Repo.delete_all(Conversation.Participation)
28 refute Repo.one(Conversation)
30 Conversation.bump_for_all_activities()
32 assert Repo.one(Conversation)
33 [participation, _p2] = Repo.all(Conversation.Participation)
35 assert participation.read
38 test "it creates a conversation for given ap_id" do
39 assert {:ok, %Conversation{} = conversation} =
40 Conversation.create_for_ap_id("https://some_ap_id")
42 # Inserting again returns the same
43 assert {:ok, conversation_two} = Conversation.create_for_ap_id("https://some_ap_id")
44 assert conversation_two.id == conversation.id
47 test "public posts don't create conversations" do
49 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey"})
51 object = Pleroma.Object.normalize(activity)
52 context = object.data["context"]
54 conversation = Conversation.get_for_ap_id(context)
59 test "it creates or updates a conversation and participations for a given DM" do
61 jafnhar = insert(:user, local: false)
65 CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"})
67 object = Pleroma.Object.normalize(activity)
68 context = object.data["context"]
71 Conversation.get_for_ap_id(context)
72 |> Repo.preload(:participations)
76 assert Enum.find(conversation.participations, fn %{user_id: user_id} -> har.id == user_id end)
78 assert Enum.find(conversation.participations, fn %{user_id: user_id} ->
83 CommonAPI.post(jafnhar, %{
84 "status" => "Hey @#{har.nickname}",
85 "visibility" => "direct",
86 "in_reply_to_status_id" => activity.id
89 object = Pleroma.Object.normalize(activity)
90 context = object.data["context"]
93 Conversation.get_for_ap_id(context)
94 |> Repo.preload(:participations)
96 assert conversation_two.id == conversation.id
98 assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
102 assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
103 jafnhar.id == user_id
107 CommonAPI.post(tridi, %{
108 "status" => "Hey @#{har.nickname}",
109 "visibility" => "direct",
110 "in_reply_to_status_id" => activity.id
113 object = Pleroma.Object.normalize(activity)
114 context = object.data["context"]
117 Conversation.get_for_ap_id(context)
118 |> Repo.preload([:participations, :users])
120 assert conversation_three.id == conversation.id
122 assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
126 assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
127 jafnhar.id == user_id
130 assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
134 assert Enum.find(conversation_three.users, fn %{id: user_id} ->
138 assert Enum.find(conversation_three.users, fn %{id: user_id} ->
139 jafnhar.id == user_id
142 assert Enum.find(conversation_three.users, fn %{id: user_id} ->
147 test "create_or_bump_for returns the conversation with participations" do
149 jafnhar = insert(:user, local: false)
152 CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"})
154 {:ok, conversation} = Conversation.create_or_bump_for(activity)
156 assert length(conversation.participations) == 2
159 CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "public"})
161 assert {:error, _} = Conversation.create_or_bump_for(activity)
164 test "create_or_bump_for does not normalize objects before checking the activity type" do
166 note_id = note.data["id"]
168 refute Object.get_by_ap_id(note_id)
170 Tesla.Mock.mock(fn env ->
173 # TODO: add attributedTo and tag to the note factory
176 |> Map.put("attributedTo", note.data["actor"])
177 |> Map.put("tag", [])
180 %Tesla.Env{status: 200, body: body}
187 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
188 "actor" => note.data["actor"],
189 "to" => [note.data["actor"]],
195 Conversation.create_or_bump_for(undo)
197 refute Object.get_by_ap_id(note_id)