summaryrefslogtreecommitdiff
path: root/test/web/streamer/streamer_test.exs
blob: d56d7446448e10217d9e921d5db896bb05a92fa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.StreamerTest do
  use Pleroma.DataCase

  import Pleroma.Factory

  alias Pleroma.Chat
  alias Pleroma.Chat.MessageReference
  alias Pleroma.Conversation.Participation
  alias Pleroma.List
  alias Pleroma.Object
  alias Pleroma.User
  alias Pleroma.Web.CommonAPI
  alias Pleroma.Web.Streamer
  alias Pleroma.Web.StreamerView

  @moduletag needs_streamer: true, capture_log: true

  setup do: clear_config([:instance, :skip_thread_containment])

  describe "get_topic without an user" do
    test "allows public" do
      assert {:ok, "public"} = Streamer.get_topic("public", nil)
      assert {:ok, "public:local"} = Streamer.get_topic("public:local", nil)
      assert {:ok, "public:media"} = Streamer.get_topic("public:media", nil)
      assert {:ok, "public:local:media"} = Streamer.get_topic("public:local:media", nil)
    end

    test "allows hashtag streams" do
      assert {:ok, "hashtag:cofe"} = Streamer.get_topic("hashtag", nil, %{"tag" => "cofe"})
    end

    test "disallows user streams" do
      assert {:error, _} = Streamer.get_topic("user", nil)
      assert {:error, _} = Streamer.get_topic("user:notification", nil)
      assert {:error, _} = Streamer.get_topic("direct", nil)
    end

    test "disallows list streams" do
      assert {:error, _} = Streamer.get_topic("list", nil, %{"list" => 42})
    end
  end

  describe "get_topic with an user" do
    setup do
      user = insert(:user)
      {:ok, %{user: user}}
    end

    test "allows public streams", %{user: user} do
      assert {:ok, "public"} = Streamer.get_topic("public", user)
      assert {:ok, "public:local"} = Streamer.get_topic("public:local", user)
      assert {:ok, "public:media"} = Streamer.get_topic("public:media", user)
      assert {:ok, "public:local:media"} = Streamer.get_topic("public:local:media", user)
    end

    test "allows user streams", %{user: user} do
      expected_user_topic = "user:#{user.id}"
      expected_notif_topic = "user:notification:#{user.id}"
      expected_direct_topic = "direct:#{user.id}"
      assert {:ok, ^expected_user_topic} = Streamer.get_topic("user", user)
      assert {:ok, ^expected_notif_topic} = Streamer.get_topic("user:notification", user)
      assert {:ok, ^expected_direct_topic} = Streamer.get_topic("direct", user)
    end

    test "allows hashtag streams", %{user: user} do
      assert {:ok, "hashtag:cofe"} = Streamer.get_topic("hashtag", user, %{"tag" => "cofe"})
    end

    test "disallows registering to an user stream", %{user: user} do
      another_user = insert(:user)
      assert {:error, _} = Streamer.get_topic("user:#{another_user.id}", user)
      assert {:error, _} = Streamer.get_topic("user:notification:#{another_user.id}", user)
      assert {:error, _} = Streamer.get_topic("direct:#{another_user.id}", user)
    end

    test "allows list stream that are owned by the user", %{user: user} do
      {:ok, list} = List.create("Test", user)
      assert {:error, _} = Streamer.get_topic("list:#{list.id}", user)
      assert {:ok, _} = Streamer.get_topic("list", user, %{"list" => list.id})
    end

    test "disallows list stream that are not owned by the user", %{user: user} do
      another_user = insert(:user)
      {:ok, list} = List.create("Test", another_user)
      assert {:error, _} = Streamer.get_topic("list:#{list.id}", user)
      assert {:error, _} = Streamer.get_topic("list", user, %{"list" => list.id})
    end
  end

  describe "user streams" do
    setup do
      user = insert(:user)
      notify = insert(:notification, user: user, activity: build(:note_activity))
      {:ok, %{user: user, notify: notify}}
    end

    test "it streams the user's post in the 'user' stream", %{user: user} do
      Streamer.get_topic_and_add_socket("user", user)
      {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
      assert_receive {:render_with_user, _, _, ^activity}
      refute Streamer.filtered_by_user?(user, activity)
    end

    test "it streams boosts of the user in the 'user' stream", %{user: user} do
      Streamer.get_topic_and_add_socket("user", user)

      other_user = insert(:user)
      {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
      {:ok, announce} = CommonAPI.repeat(activity.id, user)

      assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce}
      refute Streamer.filtered_by_user?(user, announce)
    end

    test "it does not stream announces of the user's own posts in the 'user' stream", %{
      user: user
    } do
      Streamer.get_topic_and_add_socket("user", user)

      other_user = insert(:user)
      {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
      {:ok, announce} = CommonAPI.repeat(activity.id, other_user)

      assert Streamer.filtered_by_user?(user, announce)
    end

    test "it does stream notifications announces of the user's own posts in the 'user' stream", %{
      user: user
    } do
      Streamer.get_topic_and_add_socket("user", user)

      other_user = insert(:user)
      {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
      {:ok, announce} = CommonAPI.repeat(activity.id, other_user)

      notification =
        Pleroma.Notification
        |> Repo.get_by(%{user_id: user.id, activity_id: announce.id})
        |> Repo.preload(:activity)

      refute Streamer.filtered_by_user?(user, notification)
    end

    test "it streams boosts of mastodon user in the 'user' stream", %{user: user} do
      Streamer.get_topic_and_add_socket("user", user)

      other_user = insert(:user)
      {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})

      data =
        File.read!("test/fixtures/mastodon-announce.json")
        |> Poison.decode!()
        |> Map.put("object", activity.data["object"])
        |> Map.put("actor", user.ap_id)

      {:ok, %Pleroma.Activity{data: _data, local: false} = announce} =
        Pleroma.Web.ActivityPub.Transmogrifier.handle_incoming(data)

      assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce}
      refute Streamer.filtered_by_user?(user, announce)
    end

    test "it sends notify to in the 'user' stream", %{user: user, notify: notify} do
      Streamer.get_topic_and_add_socket("user", user)
      Streamer.stream("user", notify)
      assert_receive {:render_with_user, _, _, ^notify}
      refute Streamer.filtered_by_user?(user, notify)
    end

    test "it sends notify to in the 'user:notification' stream", %{user: user, notify: notify} do
      Streamer.get_topic_and_add_socket("user:notification", user)
      Streamer.stream("user:notification", notify)
      assert_receive {:render_with_user, _, _, ^notify}
      refute Streamer.filtered_by_user?(user, notify)
    end

    test "it sends chat messages to the 'user:pleroma_chat' stream", %{user: user} do
      other_user = insert(:user)

      {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey cirno")
      object = Object.normalize(create_activity, false)
      chat = Chat.get(user.id, other_user.ap_id)
      cm_ref = MessageReference.for_chat_and_object(chat, object)
      cm_ref = %{cm_ref | chat: chat, object: object}

      Streamer.get_topic_and_add_socket("user:pleroma_chat", user)
      Streamer.stream("user:pleroma_chat", {user, cm_ref})

      text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})

      assert text =~ "hey cirno"
      assert_receive {:text, ^text}
    end

    test "it sends chat messages to the 'user' stream", %{user: user} do
      other_user = insert(:user)

      {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey cirno")
      object = Object.normalize(create_activity, false)
      chat = Chat.get(user.id, other_user.ap_id)
      cm_ref = MessageReference.for_chat_and_object(chat, object)
      cm_ref = %{cm_ref | chat: chat, object: object}

      Streamer.get_topic_and_add_socket("user", user)
      Streamer.stream("user", {user, cm_ref})

      text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})

      assert text =~ "hey cirno"
      assert_receive {:text, ^text}
    end

    test "it sends chat message notifications to the 'user:notification' stream", %{user: user} do
      other_user = insert(:user)

      {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey")

      notify =
        Repo.get_by(Pleroma.Notification, user_id: user.id, activity_id: create_activity.id)
        |> Repo.preload(:activity)

      Streamer.get_topic_and_add_socket("user:notification", user)
      Streamer.stream("user:notification", notify)
      assert_receive {:render_with_user, _, _, ^notify}
      refute Streamer.filtered_by_user?(user, notify)
    end

    test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
      user: user
    } do
      blocked = insert(:user)
      {:ok, _user_relationship} = User.block(user, blocked)

      Streamer.get_topic_and_add_socket("user:notification", user)

      {:ok, activity} = CommonAPI.post(user, %{status: ":("})
      {:ok, _} = CommonAPI.favorite(blocked, activity.id)

      refute_receive _
    end

    test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
      user: user
    } do
      user2 = insert(:user)

      {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
      {:ok, _} = CommonAPI.add_mute(user, activity)

      Streamer.get_topic_and_add_socket("user:notification", user)

      {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)

      refute_receive _
      assert Streamer.filtered_by_user?(user, favorite_activity)
    end

    test "it sends favorite to 'user:notification' stream'", %{
      user: user
    } do
      user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})

      {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
      Streamer.get_topic_and_add_socket("user:notification", user)
      {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)

      assert_receive {:render_with_user, _, "notification.json", notif}
      assert notif.activity.id == favorite_activity.id
      refute Streamer.filtered_by_user?(user, notif)
    end

    test "it doesn't send the 'user:notification' stream' when a domain is blocked", %{
      user: user
    } do
      user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})

      {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
      {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
      Streamer.get_topic_and_add_socket("user:notification", user)
      {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)

      refute_receive _
      assert Streamer.filtered_by_user?(user, favorite_activity)
    end

    test "it sends follow activities to the 'user:notification' stream", %{
      user: user
    } do
      user_url = user.ap_id
      user2 = insert(:user)

      body =
        File.read!("test/fixtures/users_mock/localhost.json")
        |> String.replace("{{nickname}}", user.nickname)
        |> Jason.encode!()

      Tesla.Mock.mock_global(fn
        %{method: :get, url: ^user_url} ->
          %Tesla.Env{status: 200, body: body}
      end)

      Streamer.get_topic_and_add_socket("user:notification", user)
      {:ok, _follower, _followed, follow_activity} = CommonAPI.follow(user2, user)

      assert_receive {:render_with_user, _, "notification.json", notif}
      assert notif.activity.id == follow_activity.id
      refute Streamer.filtered_by_user?(user, notif)
    end
  end

  test "it sends to public authenticated" do
    user = insert(:user)
    other_user = insert(:user)

    Streamer.get_topic_and_add_socket("public", other_user)

    {:ok, activity} = CommonAPI.post(user, %{status: "Test"})
    assert_receive {:render_with_user, _, _, ^activity}
    refute Streamer.filtered_by_user?(user, activity)
  end

  test "works for deletions" do
    user = insert(:user)
    other_user = insert(:user)
    {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})

    Streamer.get_topic_and_add_socket("public", user)

    {:ok, _} = CommonAPI.delete(activity.id, other_user)
    activity_id = activity.id
    assert_receive {:text, event}
    assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
  end

  test "it sends to public unauthenticated" do
    user = insert(:user)

    Streamer.get_topic_and_add_socket("public", nil)

    {:ok, activity} = CommonAPI.post(user, %{status: "Test"})
    activity_id = activity.id
    assert_receive {:text, event}
    assert %{"event" => "update", "payload" => payload} = Jason.decode!(event)
    assert %{"id" => ^activity_id} = Jason.decode!(payload)

    {:ok, _} = CommonAPI.delete(activity.id, user)
    assert_receive {:text, event}
    assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
  end

  describe "thread_containment" do
    test "it filters to user if recipients invalid and thread containment is enabled" do
      Pleroma.Config.put([:instance, :skip_thread_containment], false)
      author = insert(:user)
      user = insert(:user)
      User.follow(user, author, :follow_accept)

      activity =
        insert(:note_activity,
          note:
            insert(:note,
              user: author,
              data: %{"to" => ["TEST-FFF"]}
            )
        )

      Streamer.get_topic_and_add_socket("public", user)
      Streamer.stream("public", activity)
      assert_receive {:render_with_user, _, _, ^activity}
      assert Streamer.filtered_by_user?(user, activity)
    end

    test "it sends message if recipients invalid and thread containment is disabled" do
      Pleroma.Config.put([:instance, :skip_thread_containment], true)
      author = insert(:user)
      user = insert(:user)
      User.follow(user, author, :follow_accept)

      activity =
        insert(:note_activity,
          note:
            insert(:note,
              user: author,
              data: %{"to" => ["TEST-FFF"]}
            )
        )

      Streamer.get_topic_and_add_socket("public", user)
      Streamer.stream("public", activity)

      assert_receive {:render_with_user, _, _, ^activity}
      refute Streamer.filtered_by_user?(user, activity)
    end

    test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
      Pleroma.Config.put([:instance, :skip_thread_containment], false)
      author = insert(:user)
      user = insert(:user, skip_thread_containment: true)
      User.follow(user, author, :follow_accept)

      activity =
        insert(:note_activity,
          note:
            insert(:note,
              user: author,
              data: %{"to" => ["TEST-FFF"]}
            )
        )

      Streamer.get_topic_and_add_socket("public", user)
      Streamer.stream("public", activity)

      assert_receive {:render_with_user, _, _, ^activity}
      refute Streamer.filtered_by_user?(user, activity)
    end
  end

  describe "blocks" do
    test "it filters messages involving blocked users" do
      user = insert(:user)
      blocked_user = insert(:user)
      {:ok, _user_relationship} = User.block(user, blocked_user)

      Streamer.get_topic_and_add_socket("public", user)
      {:ok, activity} = CommonAPI.post(blocked_user, %{status: "Test"})
      assert_receive {:render_with_user, _, _, ^activity}
      assert Streamer.filtered_by_user?(user, activity)
    end

    test "it filters messages transitively involving blocked users" do
      blocker = insert(:user)
      blockee = insert(:user)
      friend = insert(:user)

      Streamer.get_topic_and_add_socket("public", blocker)

      {:ok, _user_relationship} = User.block(blocker, blockee)

      {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})

      assert_receive {:render_with_user, _, _, ^activity_one}
      assert Streamer.filtered_by_user?(blocker, activity_one)

      {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})

      assert_receive {:render_with_user, _, _, ^activity_two}
      assert Streamer.filtered_by_user?(blocker, activity_two)

      {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})

      assert_receive {:render_with_user, _, _, ^activity_three}
      assert Streamer.filtered_by_user?(blocker, activity_three)
    end
  end

  describe "lists" do
    test "it doesn't send unwanted DMs to list" do
      user_a = insert(:user)
      user_b = insert(:user)
      user_c = insert(:user)

      {:ok, user_a} = User.follow(user_a, user_b)

      {:ok, list} = List.create("Test", user_a)
      {:ok, list} = List.follow(list, user_b)

      Streamer.get_topic_and_add_socket("list", user_a, %{"list" => list.id})

      {:ok, _activity} =
        CommonAPI.post(user_b, %{
          status: "@#{user_c.nickname} Test",
          visibility: "direct"
        })

      refute_receive _
    end

    test "it doesn't send unwanted private posts to list" do
      user_a = insert(:user)
      user_b = insert(:user)

      {:ok, list} = List.create("Test", user_a)
      {:ok, list} = List.follow(list, user_b)

      Streamer.get_topic_and_add_socket("list", user_a, %{"list" => list.id})

      {:ok, _activity} =
        CommonAPI.post(user_b, %{
          status: "Test",
          visibility: "private"
        })

      refute_receive _
    end

    test "it sends wanted private posts to list" do
      user_a = insert(:user)
      user_b = insert(:user)

      {:ok, user_a} = User.follow(user_a, user_b)

      {:ok, list} = List.create("Test", user_a)
      {:ok, list} = List.follow(list, user_b)

      Streamer.get_topic_and_add_socket("list", user_a, %{"list" => list.id})

      {:ok, activity} =
        CommonAPI.post(user_b, %{
          status: "Test",
          visibility: "private"
        })

      assert_receive {:render_with_user, _, _, ^activity}
      refute Streamer.filtered_by_user?(user_a, activity)
    end
  end

  describe "muted reblogs" do
    test "it filters muted reblogs" do
      user1 = insert(:user)
      user2 = insert(:user)
      user3 = insert(:user)
      CommonAPI.follow(user1, user2)
      CommonAPI.hide_reblogs(user1, user2)

      {:ok, create_activity} = CommonAPI.post(user3, %{status: "I'm kawen"})

      Streamer.get_topic_and_add_socket("user", user1)
      {:ok, announce_activity} = CommonAPI.repeat(create_activity.id, user2)
      assert_receive {:render_with_user, _, _, ^announce_activity}
      assert Streamer.filtered_by_user?(user1, announce_activity)
    end

    test "it filters reblog notification for reblog-muted actors" do
      user1 = insert(:user)
      user2 = insert(:user)
      CommonAPI.follow(user1, user2)
      CommonAPI.hide_reblogs(user1, user2)

      {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
      Streamer.get_topic_and_add_socket("user", user1)
      {:ok, _announce_activity} = CommonAPI.repeat(create_activity.id, user2)

      assert_receive {:render_with_user, _, "notification.json", notif}
      assert Streamer.filtered_by_user?(user1, notif)
    end

    test "it send non-reblog notification for reblog-muted actors" do
      user1 = insert(:user)
      user2 = insert(:user)
      CommonAPI.follow(user1, user2)
      CommonAPI.hide_reblogs(user1, user2)

      {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
      Streamer.get_topic_and_add_socket("user", user1)
      {:ok, _favorite_activity} = CommonAPI.favorite(user2, create_activity.id)

      assert_receive {:render_with_user, _, "notification.json", notif}
      refute Streamer.filtered_by_user?(user1, notif)
    end
  end

  test "it filters posts from muted threads" do
    user = insert(:user)
    user2 = insert(:user)
    Streamer.get_topic_and_add_socket("user", user2)
    {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
    {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
    {:ok, _} = CommonAPI.add_mute(user2, activity)
    assert_receive {:render_with_user, _, _, ^activity}
    assert Streamer.filtered_by_user?(user2, activity)
  end

  describe "direct streams" do
    setup do
      :ok
    end

    test "it sends conversation update to the 'direct' stream", %{} do
      user = insert(:user)
      another_user = insert(:user)

      Streamer.get_topic_and_add_socket("direct", user)

      {:ok, _create_activity} =
        CommonAPI.post(another_user, %{
          status: "hey @#{user.nickname}",
          visibility: "direct"
        })

      assert_receive {:text, received_event}

      assert %{"event" => "conversation", "payload" => received_payload} =
               Jason.decode!(received_event)

      assert %{"last_status" => last_status} = Jason.decode!(received_payload)
      [participation] = Participation.for_user(user)
      assert last_status["pleroma"]["direct_conversation_id"] == participation.id
    end

    test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted" do
      user = insert(:user)
      another_user = insert(:user)

      Streamer.get_topic_and_add_socket("direct", user)

      {:ok, create_activity} =
        CommonAPI.post(another_user, %{
          status: "hi @#{user.nickname}",
          visibility: "direct"
        })

      create_activity_id = create_activity.id
      assert_receive {:render_with_user, _, _, ^create_activity}
      assert_receive {:text, received_conversation1}
      assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)

      {:ok, _} = CommonAPI.delete(create_activity_id, another_user)

      assert_receive {:text, received_event}

      assert %{"event" => "delete", "payload" => ^create_activity_id} =
               Jason.decode!(received_event)

      refute_receive _
    end

    test "it sends conversation update to the 'direct' stream when a message is deleted" do
      user = insert(:user)
      another_user = insert(:user)
      Streamer.get_topic_and_add_socket("direct", user)

      {:ok, create_activity} =
        CommonAPI.post(another_user, %{
          status: "hi @#{user.nickname}",
          visibility: "direct"
        })

      {:ok, create_activity2} =
        CommonAPI.post(another_user, %{
          status: "hi @#{user.nickname} 2",
          in_reply_to_status_id: create_activity.id,
          visibility: "direct"
        })

      assert_receive {:render_with_user, _, _, ^create_activity}
      assert_receive {:render_with_user, _, _, ^create_activity2}
      assert_receive {:text, received_conversation1}
      assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
      assert_receive {:text, received_conversation1}
      assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)

      {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)

      assert_receive {:text, received_event}
      assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)

      assert_receive {:text, received_event}

      assert %{"event" => "conversation", "payload" => received_payload} =
               Jason.decode!(received_event)

      assert %{"last_status" => last_status} = Jason.decode!(received_payload)
      assert last_status["id"] == to_string(create_activity.id)
    end
  end
end