Merge branch 'add-direct-conversation-id-to-conversation-event' into 'develop'
[akkoma] / test / web / streamer / streamer_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.Web.StreamerTest do
6 use Pleroma.DataCase
7
8 import Pleroma.Factory
9
10 alias Pleroma.Conversation.Participation
11 alias Pleroma.List
12 alias Pleroma.User
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.Streamer
15 alias Pleroma.Web.Streamer.StreamerSocket
16 alias Pleroma.Web.Streamer.Worker
17
18 @moduletag needs_streamer: true
19 clear_config_all([:instance, :skip_thread_containment])
20
21 describe "user streams" do
22 setup do
23 user = insert(:user)
24 notify = insert(:notification, user: user, activity: build(:note_activity))
25 {:ok, %{user: user, notify: notify}}
26 end
27
28 test "it sends notify to in the 'user' stream", %{user: user, notify: notify} do
29 task =
30 Task.async(fn ->
31 assert_receive {:text, _}, 4_000
32 end)
33
34 Streamer.add_socket(
35 "user",
36 %{transport_pid: task.pid, assigns: %{user: user}}
37 )
38
39 Streamer.stream("user", notify)
40 Task.await(task)
41 end
42
43 test "it sends notify to in the 'user:notification' stream", %{user: user, notify: notify} do
44 task =
45 Task.async(fn ->
46 assert_receive {:text, _}, 4_000
47 end)
48
49 Streamer.add_socket(
50 "user:notification",
51 %{transport_pid: task.pid, assigns: %{user: user}}
52 )
53
54 Streamer.stream("user:notification", notify)
55 Task.await(task)
56 end
57
58 test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
59 user: user
60 } do
61 blocked = insert(:user)
62 {:ok, user} = User.block(user, blocked)
63
64 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
65
66 Streamer.add_socket(
67 "user:notification",
68 %{transport_pid: task.pid, assigns: %{user: user}}
69 )
70
71 {:ok, activity} = CommonAPI.post(user, %{"status" => ":("})
72 {:ok, notif, _} = CommonAPI.favorite(activity.id, blocked)
73
74 Streamer.stream("user:notification", notif)
75 Task.await(task)
76 end
77
78 test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
79 user: user
80 } do
81 user2 = insert(:user)
82 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
83
84 Streamer.add_socket(
85 "user:notification",
86 %{transport_pid: task.pid, assigns: %{user: user}}
87 )
88
89 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
90 {:ok, activity} = CommonAPI.add_mute(user, activity)
91 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
92 Streamer.stream("user:notification", notif)
93 Task.await(task)
94 end
95
96 test "it doesn't send notify to the 'user:notification' stream' when a domain is blocked", %{
97 user: user
98 } do
99 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
100 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
101
102 Streamer.add_socket(
103 "user:notification",
104 %{transport_pid: task.pid, assigns: %{user: user}}
105 )
106
107 {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
108 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
109 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
110
111 Streamer.stream("user:notification", notif)
112 Task.await(task)
113 end
114
115 test "it sends follow activities to the 'user:notification' stream", %{
116 user: user
117 } do
118 user2 = insert(:user)
119 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
120
121 Streamer.add_socket(
122 "user:notification",
123 %{transport_pid: task.pid, assigns: %{user: user}}
124 )
125
126 {:ok, _follower, _followed, _activity} = CommonAPI.follow(user2, user)
127
128 # We don't directly pipe the notification to the streamer as it's already
129 # generated as a side effect of CommonAPI.follow().
130 Task.await(task)
131 end
132 end
133
134 test "it sends to public" do
135 user = insert(:user)
136 other_user = insert(:user)
137
138 task =
139 Task.async(fn ->
140 assert_receive {:text, _}, 4_000
141 end)
142
143 fake_socket = %StreamerSocket{
144 transport_pid: task.pid,
145 user: user
146 }
147
148 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"})
149
150 topics = %{
151 "public" => [fake_socket]
152 }
153
154 Worker.push_to_socket(topics, "public", activity)
155
156 Task.await(task)
157
158 task =
159 Task.async(fn ->
160 expected_event =
161 %{
162 "event" => "delete",
163 "payload" => activity.id
164 }
165 |> Jason.encode!()
166
167 assert_receive {:text, received_event}, 4_000
168 assert received_event == expected_event
169 end)
170
171 fake_socket = %StreamerSocket{
172 transport_pid: task.pid,
173 user: user
174 }
175
176 {:ok, activity} = CommonAPI.delete(activity.id, other_user)
177
178 topics = %{
179 "public" => [fake_socket]
180 }
181
182 Worker.push_to_socket(topics, "public", activity)
183
184 Task.await(task)
185 end
186
187 describe "thread_containment" do
188 test "it doesn't send to user if recipients invalid and thread containment is enabled" do
189 Pleroma.Config.put([:instance, :skip_thread_containment], false)
190 author = insert(:user)
191 user = insert(:user)
192 User.follow(user, author, "accept")
193
194 activity =
195 insert(:note_activity,
196 note:
197 insert(:note,
198 user: author,
199 data: %{"to" => ["TEST-FFF"]}
200 )
201 )
202
203 task = Task.async(fn -> refute_receive {:text, _}, 1_000 end)
204 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
205 topics = %{"public" => [fake_socket]}
206 Worker.push_to_socket(topics, "public", activity)
207
208 Task.await(task)
209 end
210
211 test "it sends message if recipients invalid and thread containment is disabled" do
212 Pleroma.Config.put([:instance, :skip_thread_containment], true)
213 author = insert(:user)
214 user = insert(:user)
215 User.follow(user, author, "accept")
216
217 activity =
218 insert(:note_activity,
219 note:
220 insert(:note,
221 user: author,
222 data: %{"to" => ["TEST-FFF"]}
223 )
224 )
225
226 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
227 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
228 topics = %{"public" => [fake_socket]}
229 Worker.push_to_socket(topics, "public", activity)
230
231 Task.await(task)
232 end
233
234 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
235 Pleroma.Config.put([:instance, :skip_thread_containment], false)
236 author = insert(:user)
237 user = insert(:user, skip_thread_containment: true)
238 User.follow(user, author, "accept")
239
240 activity =
241 insert(:note_activity,
242 note:
243 insert(:note,
244 user: author,
245 data: %{"to" => ["TEST-FFF"]}
246 )
247 )
248
249 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
250 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
251 topics = %{"public" => [fake_socket]}
252 Worker.push_to_socket(topics, "public", activity)
253
254 Task.await(task)
255 end
256 end
257
258 describe "blocks" do
259 test "it doesn't send messages involving blocked users" do
260 user = insert(:user)
261 blocked_user = insert(:user)
262 {:ok, user} = User.block(user, blocked_user)
263
264 task =
265 Task.async(fn ->
266 refute_receive {:text, _}, 1_000
267 end)
268
269 fake_socket = %StreamerSocket{
270 transport_pid: task.pid,
271 user: user
272 }
273
274 {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
275
276 topics = %{
277 "public" => [fake_socket]
278 }
279
280 Worker.push_to_socket(topics, "public", activity)
281
282 Task.await(task)
283 end
284
285 test "it doesn't send messages transitively involving blocked users" do
286 blocker = insert(:user)
287 blockee = insert(:user)
288 friend = insert(:user)
289
290 task =
291 Task.async(fn ->
292 refute_receive {:text, _}, 1_000
293 end)
294
295 fake_socket = %StreamerSocket{
296 transport_pid: task.pid,
297 user: blocker
298 }
299
300 topics = %{
301 "public" => [fake_socket]
302 }
303
304 {:ok, blocker} = User.block(blocker, blockee)
305
306 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
307
308 Worker.push_to_socket(topics, "public", activity_one)
309
310 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
311
312 Worker.push_to_socket(topics, "public", activity_two)
313
314 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
315
316 Worker.push_to_socket(topics, "public", activity_three)
317
318 Task.await(task)
319 end
320 end
321
322 test "it doesn't send unwanted DMs to list" do
323 user_a = insert(:user)
324 user_b = insert(:user)
325 user_c = insert(:user)
326
327 {:ok, user_a} = User.follow(user_a, user_b)
328
329 {:ok, list} = List.create("Test", user_a)
330 {:ok, list} = List.follow(list, user_b)
331
332 task =
333 Task.async(fn ->
334 refute_receive {:text, _}, 1_000
335 end)
336
337 fake_socket = %StreamerSocket{
338 transport_pid: task.pid,
339 user: user_a
340 }
341
342 {:ok, activity} =
343 CommonAPI.post(user_b, %{
344 "status" => "@#{user_c.nickname} Test",
345 "visibility" => "direct"
346 })
347
348 topics = %{
349 "list:#{list.id}" => [fake_socket]
350 }
351
352 Worker.handle_call({:stream, "list", activity}, self(), topics)
353
354 Task.await(task)
355 end
356
357 test "it doesn't send unwanted private posts to list" do
358 user_a = insert(:user)
359 user_b = insert(:user)
360
361 {:ok, list} = List.create("Test", user_a)
362 {:ok, list} = List.follow(list, user_b)
363
364 task =
365 Task.async(fn ->
366 refute_receive {:text, _}, 1_000
367 end)
368
369 fake_socket = %StreamerSocket{
370 transport_pid: task.pid,
371 user: user_a
372 }
373
374 {:ok, activity} =
375 CommonAPI.post(user_b, %{
376 "status" => "Test",
377 "visibility" => "private"
378 })
379
380 topics = %{
381 "list:#{list.id}" => [fake_socket]
382 }
383
384 Worker.handle_call({:stream, "list", activity}, self(), topics)
385
386 Task.await(task)
387 end
388
389 test "it sends wanted private posts to list" do
390 user_a = insert(:user)
391 user_b = insert(:user)
392
393 {:ok, user_a} = User.follow(user_a, user_b)
394
395 {:ok, list} = List.create("Test", user_a)
396 {:ok, list} = List.follow(list, user_b)
397
398 task =
399 Task.async(fn ->
400 assert_receive {:text, _}, 1_000
401 end)
402
403 fake_socket = %StreamerSocket{
404 transport_pid: task.pid,
405 user: user_a
406 }
407
408 {:ok, activity} =
409 CommonAPI.post(user_b, %{
410 "status" => "Test",
411 "visibility" => "private"
412 })
413
414 Streamer.add_socket(
415 "list:#{list.id}",
416 fake_socket
417 )
418
419 Worker.handle_call({:stream, "list", activity}, self(), %{})
420
421 Task.await(task)
422 end
423
424 test "it doesn't send muted reblogs" do
425 user1 = insert(:user)
426 user2 = insert(:user)
427 user3 = insert(:user)
428 CommonAPI.hide_reblogs(user1, user2)
429
430 task =
431 Task.async(fn ->
432 refute_receive {:text, _}, 1_000
433 end)
434
435 fake_socket = %StreamerSocket{
436 transport_pid: task.pid,
437 user: user1
438 }
439
440 {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"})
441 {:ok, announce_activity, _} = CommonAPI.repeat(create_activity.id, user2)
442
443 topics = %{
444 "public" => [fake_socket]
445 }
446
447 Worker.push_to_socket(topics, "public", announce_activity)
448
449 Task.await(task)
450 end
451
452 test "it doesn't send posts from muted threads" do
453 user = insert(:user)
454 user2 = insert(:user)
455 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
456
457 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
458
459 {:ok, activity} = CommonAPI.add_mute(user2, activity)
460
461 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
462
463 Process.sleep(4000)
464
465 Streamer.add_socket(
466 "user",
467 %{transport_pid: task.pid, assigns: %{user: user2}}
468 )
469
470 Streamer.stream("user", activity)
471 Task.await(task)
472 end
473
474 describe "direct streams" do
475 setup do
476 :ok
477 end
478
479 test "it sends conversation update to the 'direct' stream", %{} do
480 user = insert(:user)
481 another_user = insert(:user)
482
483 task =
484 Task.async(fn ->
485 assert_receive {:text, received_event}, 4_000
486
487 assert %{"event" => "conversation", "payload" => received_payload} =
488 Jason.decode!(received_event)
489
490 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
491 [participation] = Participation.for_user(user)
492 assert last_status["pleroma"]["direct_conversation_id"] == participation.id
493 end)
494
495 Streamer.add_socket(
496 "direct",
497 %{transport_pid: task.pid, assigns: %{user: user}}
498 )
499
500 {:ok, _create_activity} =
501 CommonAPI.post(another_user, %{
502 "status" => "hey @#{user.nickname}",
503 "visibility" => "direct"
504 })
505
506 Task.await(task)
507 end
508
509 test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted" do
510 user = insert(:user)
511 another_user = insert(:user)
512
513 {:ok, create_activity} =
514 CommonAPI.post(another_user, %{
515 "status" => "hi @#{user.nickname}",
516 "visibility" => "direct"
517 })
518
519 task =
520 Task.async(fn ->
521 assert_receive {:text, received_event}, 4_000
522 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
523
524 refute_receive {:text, _}, 4_000
525 end)
526
527 Process.sleep(1000)
528
529 Streamer.add_socket(
530 "direct",
531 %{transport_pid: task.pid, assigns: %{user: user}}
532 )
533
534 {:ok, _} = CommonAPI.delete(create_activity.id, another_user)
535
536 Task.await(task)
537 end
538
539 test "it sends conversation update to the 'direct' stream when a message is deleted" do
540 user = insert(:user)
541 another_user = insert(:user)
542
543 {:ok, create_activity} =
544 CommonAPI.post(another_user, %{
545 "status" => "hi @#{user.nickname}",
546 "visibility" => "direct"
547 })
548
549 {:ok, create_activity2} =
550 CommonAPI.post(another_user, %{
551 "status" => "hi @#{user.nickname}",
552 "in_reply_to_status_id" => create_activity.id,
553 "visibility" => "direct"
554 })
555
556 task =
557 Task.async(fn ->
558 assert_receive {:text, received_event}, 4_000
559 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
560
561 assert_receive {:text, received_event}, 4_000
562
563 assert %{"event" => "conversation", "payload" => received_payload} =
564 Jason.decode!(received_event)
565
566 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
567 assert last_status["id"] == to_string(create_activity.id)
568 end)
569
570 Process.sleep(1000)
571
572 Streamer.add_socket(
573 "direct",
574 %{transport_pid: task.pid, assigns: %{user: user}}
575 )
576
577 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)
578
579 Task.await(task)
580 end
581 end
582 end