239dda04f0716adbf9512722058c886d1469e94e
[akkoma] / test / conversation_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ConversationTest do
6 use Pleroma.DataCase
7 alias Pleroma.Conversation
8 alias Pleroma.Web.CommonAPI
9
10 import Pleroma.Factory
11
12 test "it creates a conversation for given ap_id" do
13 assert {:ok, %Conversation{} = conversation} =
14 Conversation.create_for_ap_id("https://some_ap_id")
15
16 # Inserting again returns the same
17 assert {:ok, conversation_two} = Conversation.create_for_ap_id("https://some_ap_id")
18 assert conversation_two.id == conversation.id
19 end
20
21 test "public posts don't create conversations" do
22 user = insert(:user)
23 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey"})
24
25 context = activity.data["object"]["context"]
26
27 conversation = Conversation.get_for_ap_id(context)
28
29 refute conversation
30 end
31
32 test "it creates or updates a conversation and participations for a given DM" do
33 har = insert(:user)
34 jafnhar = insert(:user, local: false)
35 tridi = insert(:user)
36
37 {:ok, activity} =
38 CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"})
39
40 context = activity.data["object"]["context"]
41
42 conversation =
43 Conversation.get_for_ap_id(context)
44 |> Repo.preload(:participations)
45
46 assert conversation
47
48 assert Enum.find(conversation.participations, fn %{user_id: user_id} -> har.id == user_id end)
49
50 assert Enum.find(conversation.participations, fn %{user_id: user_id} ->
51 jafnhar.id == user_id
52 end)
53
54 {:ok, activity} =
55 CommonAPI.post(jafnhar, %{
56 "status" => "Hey @#{har.nickname}",
57 "visibility" => "direct",
58 "in_reply_to_status_id" => activity.id
59 })
60
61 context = activity.data["object"]["context"]
62
63 conversation_two =
64 Conversation.get_for_ap_id(context)
65 |> Repo.preload(:participations)
66
67 assert conversation_two.id == conversation.id
68
69 assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
70 har.id == user_id
71 end)
72
73 assert Enum.find(conversation_two.participations, fn %{user_id: user_id} ->
74 jafnhar.id == user_id
75 end)
76
77 {:ok, activity} =
78 CommonAPI.post(tridi, %{
79 "status" => "Hey @#{har.nickname}",
80 "visibility" => "direct",
81 "in_reply_to_status_id" => activity.id
82 })
83
84 context = activity.data["object"]["context"]
85
86 conversation_three =
87 Conversation.get_for_ap_id(context)
88 |> Repo.preload([:participations, :users])
89
90 assert conversation_three.id == conversation.id
91
92 assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
93 har.id == user_id
94 end)
95
96 assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
97 jafnhar.id == user_id
98 end)
99
100 assert Enum.find(conversation_three.participations, fn %{user_id: user_id} ->
101 tridi.id == user_id
102 end)
103
104 assert Enum.find(conversation_three.users, fn %{id: user_id} ->
105 har.id == user_id
106 end)
107
108 assert Enum.find(conversation_three.users, fn %{id: user_id} ->
109 jafnhar.id == user_id
110 end)
111
112 assert Enum.find(conversation_three.users, fn %{id: user_id} ->
113 tridi.id == user_id
114 end)
115 end
116 end