2019-04-10 07:34:53 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-04-10 07:34:53 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.ConversationTest do
|
|
|
|
use Pleroma.DataCase
|
2019-05-13 07:32:04 +00:00
|
|
|
alias Pleroma.Activity
|
2019-04-10 07:34:53 +00:00
|
|
|
alias Pleroma.Conversation
|
2019-05-13 07:32:04 +00:00
|
|
|
alias Pleroma.Object
|
2019-04-10 14:33:45 +00:00
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
2019-04-10 07:34:53 +00:00
|
|
|
|
2020-03-20 15:33:00 +00:00
|
|
|
setup_all do: clear_config([:instance, :federating], true)
|
2019-07-09 06:30:51 +00:00
|
|
|
|
2019-05-08 15:37:00 +00:00
|
|
|
test "it goes through old direct conversations" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, _activity} =
|
2020-05-12 19:59:26 +00:00
|
|
|
CommonAPI.post(user, %{visibility: "direct", status: "hey @#{other_user.nickname}"})
|
2019-05-08 15:37:00 +00:00
|
|
|
|
2019-08-13 17:20:26 +00:00
|
|
|
Pleroma.Tests.ObanHelpers.perform_all()
|
2019-08-10 17:38:31 +00:00
|
|
|
|
2019-05-08 15:37:00 +00:00
|
|
|
Repo.delete_all(Conversation)
|
|
|
|
Repo.delete_all(Conversation.Participation)
|
|
|
|
|
|
|
|
refute Repo.one(Conversation)
|
|
|
|
|
|
|
|
Conversation.bump_for_all_activities()
|
|
|
|
|
|
|
|
assert Repo.one(Conversation)
|
2019-05-09 14:39:28 +00:00
|
|
|
[participation, _p2] = Repo.all(Conversation.Participation)
|
|
|
|
|
|
|
|
assert participation.read
|
2019-05-08 15:37:00 +00:00
|
|
|
end
|
|
|
|
|
2019-04-10 07:34:53 +00:00
|
|
|
test "it creates a conversation for given ap_id" do
|
2019-04-10 14:33:45 +00:00
|
|
|
assert {:ok, %Conversation{} = conversation} =
|
|
|
|
Conversation.create_for_ap_id("https://some_ap_id")
|
|
|
|
|
|
|
|
# Inserting again returns the same
|
|
|
|
assert {:ok, conversation_two} = Conversation.create_for_ap_id("https://some_ap_id")
|
|
|
|
assert conversation_two.id == conversation.id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "public posts don't create conversations" do
|
|
|
|
user = insert(:user)
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, activity} = CommonAPI.post(user, %{status: "Hey"})
|
2019-04-10 14:33:45 +00:00
|
|
|
|
2021-01-04 12:38:31 +00:00
|
|
|
object = Pleroma.Object.normalize(activity, fetch: false)
|
2019-04-28 20:44:04 +00:00
|
|
|
context = object.data["context"]
|
2019-04-10 14:33:45 +00:00
|
|
|
|
|
|
|
conversation = Conversation.get_for_ap_id(context)
|
|
|
|
|
|
|
|
refute conversation
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it creates or updates a conversation and participations for a given DM" do
|
|
|
|
har = insert(:user)
|
2019-04-15 19:45:25 +00:00
|
|
|
jafnhar = insert(:user, local: false)
|
2019-04-10 14:33:45 +00:00
|
|
|
tridi = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} =
|
2020-05-12 19:59:26 +00:00
|
|
|
CommonAPI.post(har, %{status: "Hey @#{jafnhar.nickname}", visibility: "direct"})
|
2019-04-10 14:33:45 +00:00
|
|
|
|
2021-01-04 12:38:31 +00:00
|
|
|
object = Pleroma.Object.normalize(activity, fetch: false)
|
2019-04-28 20:44:04 +00:00
|
|
|
context = object.data["context"]
|
2019-04-10 14:33:45 +00:00
|
|
|
|
|
|
|
conversation =
|
|
|
|
Conversation.get_for_ap_id(context)
|
|
|
|
|> Repo.preload(:participations)
|
|
|
|
|
|
|
|
assert conversation
|
|
|
|
|
2019-04-11 11:31:20 +00:00
|
|
|
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)
|
2019-04-10 14:33:45 +00:00
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(jafnhar, %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "Hey @#{har.nickname}",
|
|
|
|
visibility: "direct",
|
|
|
|
in_reply_to_status_id: activity.id
|
2019-04-10 14:33:45 +00:00
|
|
|
})
|
|
|
|
|
2021-01-04 12:38:31 +00:00
|
|
|
object = Pleroma.Object.normalize(activity, fetch: false)
|
2019-04-28 20:44:04 +00:00
|
|
|
context = object.data["context"]
|
2019-04-10 14:33:45 +00:00
|
|
|
|
|
|
|
conversation_two =
|
|
|
|
Conversation.get_for_ap_id(context)
|
|
|
|
|> Repo.preload(:participations)
|
|
|
|
|
|
|
|
assert conversation_two.id == conversation.id
|
|
|
|
|
2019-04-11 11:31:20 +00:00
|
|
|
assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
|
|
|
|
har.id == user_id
|
|
|
|
end)
|
2019-04-10 14:33:45 +00:00
|
|
|
|
2019-04-11 11:31:20 +00:00
|
|
|
assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
|
|
|
|
jafnhar.id == user_id
|
|
|
|
end)
|
2019-04-10 14:33:45 +00:00
|
|
|
|
|
|
|
{:ok, activity} =
|
|
|
|
CommonAPI.post(tridi, %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "Hey @#{har.nickname}",
|
|
|
|
visibility: "direct",
|
|
|
|
in_reply_to_status_id: activity.id
|
2019-04-10 14:33:45 +00:00
|
|
|
})
|
|
|
|
|
2021-01-04 12:38:31 +00:00
|
|
|
object = Pleroma.Object.normalize(activity, fetch: false)
|
2019-04-28 20:44:04 +00:00
|
|
|
context = object.data["context"]
|
2019-04-10 14:33:45 +00:00
|
|
|
|
|
|
|
conversation_three =
|
|
|
|
Conversation.get_for_ap_id(context)
|
2019-04-15 20:28:42 +00:00
|
|
|
|> Repo.preload([:participations, :users])
|
2019-04-10 14:33:45 +00:00
|
|
|
|
|
|
|
assert conversation_three.id == conversation.id
|
|
|
|
|
2019-04-11 11:31:20 +00:00
|
|
|
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)
|
2019-04-10 14:33:45 +00:00
|
|
|
|
2019-04-11 11:31:20 +00:00
|
|
|
assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
|
|
|
|
tridi.id == user_id
|
|
|
|
end)
|
2019-04-15 20:28:42 +00:00
|
|
|
|
|
|
|
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)
|
2019-04-10 07:34:53 +00:00
|
|
|
end
|
2019-05-03 11:39:14 +00:00
|
|
|
|
|
|
|
test "create_or_bump_for returns the conversation with participations" do
|
|
|
|
har = insert(:user)
|
|
|
|
jafnhar = insert(:user, local: false)
|
|
|
|
|
|
|
|
{:ok, activity} =
|
2020-05-12 19:59:26 +00:00
|
|
|
CommonAPI.post(har, %{status: "Hey @#{jafnhar.nickname}", visibility: "direct"})
|
2019-05-03 11:39:14 +00:00
|
|
|
|
|
|
|
{:ok, conversation} = Conversation.create_or_bump_for(activity)
|
|
|
|
|
|
|
|
assert length(conversation.participations) == 2
|
|
|
|
|
|
|
|
{:ok, activity} =
|
2020-05-12 19:59:26 +00:00
|
|
|
CommonAPI.post(har, %{status: "Hey @#{jafnhar.nickname}", visibility: "public"})
|
2019-05-03 11:39:14 +00:00
|
|
|
|
|
|
|
assert {:error, _} = Conversation.create_or_bump_for(activity)
|
|
|
|
end
|
2019-05-13 07:32:04 +00:00
|
|
|
|
|
|
|
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
|
2019-04-10 07:34:53 +00:00
|
|
|
end
|