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