activity_pub_test.exs 39 KB
Newer Older
kaniini's avatar
kaniini committed
1
# Pleroma: A lightweight social networking server
minibikini's avatar
Reports  
minibikini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
kaniini's avatar
kaniini committed
3 4
# SPDX-License-Identifier: AGPL-3.0-only

lain's avatar
lain committed
5 6
defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
  use Pleroma.DataCase
Haelwenn's avatar
Haelwenn committed
7
  alias Pleroma.Activity
8 9
  alias Pleroma.Builders.ActivityBuilder
  alias Pleroma.Instances
Haelwenn's avatar
Haelwenn committed
10 11
  alias Pleroma.Object
  alias Pleroma.User
12
  alias Pleroma.Web.ActivityPub.ActivityPub
kaniini's avatar
kaniini committed
13
  alias Pleroma.Web.ActivityPub.Publisher
14 15
  alias Pleroma.Web.ActivityPub.Utils
  alias Pleroma.Web.CommonAPI
lain's avatar
lain committed
16

lain's avatar
lain committed
17
  import Pleroma.Factory
Maksim's avatar
Maksim committed
18
  import Tesla.Mock
19
  import Mock
Maksim's avatar
Maksim committed
20 21 22 23 24

  setup do
    mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
    :ok
  end
lain's avatar
lain committed
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  describe "streaming out participations" do
    test "it streams them out" do
      user = insert(:user)
      {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})

      {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)

      participations =
        conversation.participations
        |> Repo.preload(:user)

      with_mock Pleroma.Web.Streamer,
        stream: fn _, _ -> nil end do
        ActivityPub.stream_out_participations(conversation.participations)

        Enum.each(participations, fn participation ->
          assert called(Pleroma.Web.Streamer.stream("participation", participation))
        end)
      end
    end
  end

lain's avatar
lain committed
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
  describe "fetching restricted by visibility" do
    test "it restricts by the appropriate visibility" do
      user = insert(:user)

      {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})

      {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})

      {:ok, unlisted_activity} =
        CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})

      {:ok, private_activity} =
        CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})

      activities =
        ActivityPub.fetch_activities([], %{:visibility => "direct", "actor_id" => user.ap_id})

      assert activities == [direct_activity]

      activities =
        ActivityPub.fetch_activities([], %{:visibility => "unlisted", "actor_id" => user.ap_id})

      assert activities == [unlisted_activity]

      activities =
        ActivityPub.fetch_activities([], %{:visibility => "private", "actor_id" => user.ap_id})

      assert activities == [private_activity]

      activities =
        ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})

      assert activities == [public_activity]
81 82 83 84 85 86 87 88

      activities =
        ActivityPub.fetch_activities([], %{
          :visibility => ~w[private public],
          "actor_id" => user.ap_id
        })

      assert activities == [public_activity, private_activity]
lain's avatar
lain committed
89 90 91
    end
  end

lain's avatar
lain committed
92 93 94 95 96 97
  describe "building a user from his ap id" do
    test "it returns a user" do
      user_id = "http://mastodon.example.org/users/admin"
      {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
      assert user.ap_id == user_id
      assert user.nickname == "admin@mastodon.example.org"
lain's avatar
lain committed
98 99
      assert user.info.source_data
      assert user.info.ap_enabled
lain's avatar
lain committed
100 101
      assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
    end
102 103 104 105 106 107 108 109

    test "it fetches the appropriate tag-restricted posts" do
      user = insert(:user)

      {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
      {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
      {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})

110 111 112 113
      fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})

      fetch_two =
        ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
114 115 116

      fetch_three =
        ActivityPub.fetch_activities([], %{
117
          "type" => "Create",
118 119 120 121
          "tag" => ["test", "essais"],
          "tag_reject" => ["reject"]
        })

Haelwenn's avatar
Haelwenn committed
122 123
      fetch_four =
        ActivityPub.fetch_activities([], %{
124
          "type" => "Create",
Haelwenn's avatar
Haelwenn committed
125 126 127 128
          "tag" => ["test"],
          "tag_all" => ["test", "reject"]
        })

129 130 131
      assert fetch_one == [status_one, status_three]
      assert fetch_two == [status_one, status_two, status_three]
      assert fetch_three == [status_one, status_two]
Haelwenn's avatar
Haelwenn committed
132
      assert fetch_four == [status_three]
133
    end
lain's avatar
lain committed
134 135
  end

lain's avatar
lain committed
136
  describe "insertion" do
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    test "drops activities beyond a certain limit" do
      limit = Pleroma.Config.get([:instance, :remote_limit])

      random_text =
        :crypto.strong_rand_bytes(limit + 1)
        |> Base.encode64()
        |> binary_part(0, limit + 1)

      data = %{
        "ok" => true,
        "object" => %{
          "content" => random_text
        }
      }

      assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
    end

155
    test "doesn't drop activities with content being null" do
lain's avatar
lain committed
156 157
      user = insert(:user)

158
      data = %{
lain's avatar
lain committed
159 160
        "actor" => user.ap_id,
        "to" => [],
161
        "object" => %{
lain's avatar
lain committed
162 163 164
          "actor" => user.ap_id,
          "to" => [],
          "type" => "Note",
165 166 167 168 169 170 171
          "content" => nil
        }
      }

      assert {:ok, _} = ActivityPub.insert(data)
    end

lain's avatar
lain committed
172 173
    test "returns the activity if one with the same id is already in" do
      activity = insert(:note_activity)
lain's avatar
lain committed
174
      {:ok, new_activity} = ActivityPub.insert(activity.data)
lain's avatar
lain committed
175

176
      assert activity.id == new_activity.id
lain's avatar
lain committed
177 178
    end

lain's avatar
lain committed
179
    test "inserts a given map into the activity database, giving it an id if it has none." do
lain's avatar
lain committed
180 181
      user = insert(:user)

lain's avatar
lain committed
182
      data = %{
lain's avatar
lain committed
183 184 185 186 187 188 189 190
        "actor" => user.ap_id,
        "to" => [],
        "object" => %{
          "actor" => user.ap_id,
          "to" => [],
          "type" => "Note",
          "content" => "hey"
        }
lain's avatar
lain committed
191 192 193
      }

      {:ok, %Activity{} = activity} = ActivityPub.insert(data)
lain's avatar
lain committed
194 195 196 197
      assert activity.data["ok"] == data["ok"]
      assert is_binary(activity.data["id"])

      given_id = "bla"
lain's avatar
lain committed
198

lain's avatar
lain committed
199
      data = %{
200
        "id" => given_id,
lain's avatar
lain committed
201 202 203 204 205 206 207 208 209
        "actor" => user.ap_id,
        "to" => [],
        "context" => "blabla",
        "object" => %{
          "actor" => user.ap_id,
          "to" => [],
          "type" => "Note",
          "content" => "hey"
        }
lain's avatar
lain committed
210 211 212 213 214
      }

      {:ok, %Activity{} = activity} = ActivityPub.insert(data)
      assert activity.data["ok"] == data["ok"]
      assert activity.data["id"] == given_id
215 216 217 218 219
      assert activity.data["context"] == "blabla"
      assert activity.data["context_id"]
    end

    test "adds a context when none is there" do
lain's avatar
lain committed
220 221
      user = insert(:user)

222
      data = %{
lain's avatar
lain committed
223 224
        "actor" => user.ap_id,
        "to" => [],
225
        "object" => %{
lain's avatar
lain committed
226 227 228 229
          "actor" => user.ap_id,
          "to" => [],
          "type" => "Note",
          "content" => "hey"
lain's avatar
lain committed
230
        }
231 232 233
      }

      {:ok, %Activity{} = activity} = ActivityPub.insert(data)
Roman Chvanikov's avatar
Roman Chvanikov committed
234
      object = Pleroma.Object.normalize(activity)
235 236

      assert is_binary(activity.data["context"])
Roman Chvanikov's avatar
Roman Chvanikov committed
237
      assert is_binary(object.data["context"])
238
      assert activity.data["context_id"]
Roman Chvanikov's avatar
Roman Chvanikov committed
239
      assert object.data["context_id"]
lain's avatar
lain committed
240
    end
lain's avatar
lain committed
241

lain's avatar
lain committed
242
    test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
lain's avatar
lain committed
243 244
      user = insert(:user)

lain's avatar
lain committed
245
      data = %{
lain's avatar
lain committed
246 247
        "actor" => user.ap_id,
        "to" => [],
lain's avatar
lain committed
248
        "object" => %{
lain's avatar
lain committed
249 250
          "actor" => user.ap_id,
          "to" => [],
lain's avatar
lain committed
251
          "type" => "Note",
lain's avatar
lain committed
252
          "content" => "hey"
lain's avatar
lain committed
253 254 255 256
        }
      }

      {:ok, %Activity{} = activity} = ActivityPub.insert(data)
kaniini's avatar
kaniini committed
257 258 259 260
      object = Object.normalize(activity.data["object"])

      assert is_binary(object.data["id"])
      assert %Object{} = Object.get_by_ap_id(activity.data["object"])
lain's avatar
lain committed
261
    end
lain's avatar
lain committed
262 263
  end

lain's avatar
lain committed
264 265
  describe "create activities" do
    test "removes doubled 'to' recipients" do
eal's avatar
eal committed
266 267
      user = insert(:user)

lain's avatar
lain committed
268 269 270
      {:ok, activity} =
        ActivityPub.create(%{
          to: ["user1", "user1", "user2"],
eal's avatar
eal committed
271
          actor: user,
lain's avatar
lain committed
272
          context: "",
273 274 275 276 277
          object: %{
            "to" => ["user1", "user1", "user2"],
            "type" => "Note",
            "content" => "testing"
          }
lain's avatar
lain committed
278 279
        })

lain's avatar
lain committed
280
      assert activity.data["to"] == ["user1", "user2"]
eal's avatar
eal committed
281
      assert activity.actor == user.ap_id
Maxim Filippov's avatar
Maxim Filippov committed
282
      assert activity.recipients == ["user1", "user2", user.ap_id]
lain's avatar
lain committed
283
    end
284 285 286 287 288

    test "increases user note count only for public activities" do
      user = insert(:user)

      {:ok, _} =
minibikini's avatar
minibikini committed
289 290 291 292
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "1",
          "visibility" => "public"
        })
293 294

      {:ok, _} =
minibikini's avatar
minibikini committed
295 296 297 298
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "2",
          "visibility" => "unlisted"
        })
299 300

      {:ok, _} =
minibikini's avatar
minibikini committed
301 302 303 304
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "2",
          "visibility" => "private"
        })
305 306

      {:ok, _} =
minibikini's avatar
minibikini committed
307 308 309 310
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "3",
          "visibility" => "direct"
        })
311

minibikini's avatar
minibikini committed
312
      user = User.get_cached_by_id(user.id)
313 314
      assert user.info.note_count == 2
    end
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

    test "increases replies count" do
      user = insert(:user)
      user2 = insert(:user)

      {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
      ap_id = activity.data["id"]
      reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}

      # public
      {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 1

      # unlisted
      {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 2

      # private
      {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 2

      # direct
      {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 2
    end
lain's avatar
lain committed
344 345
  end

lain's avatar
lain committed
346 347 348 349
  describe "fetch activities for recipients" do
    test "retrieve the activities for certain recipients" do
      {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
      {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
lain's avatar
lain committed
350
      {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
lain's avatar
lain committed
351 352 353 354 355 356 357

      activities = ActivityPub.fetch_activities(["someone", "someone_else"])
      assert length(activities) == 2
      assert activities == [activity_one, activity_two]
    end
  end

lain's avatar
lain committed
358 359
  describe "fetch activities in context" do
    test "retrieves activities that have a given context" do
360 361 362 363
      {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
      {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
      {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
      {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
364 365
      activity_five = insert(:note_activity)
      user = insert(:user)
lain's avatar
lain committed
366

367
      {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
lain's avatar
lain committed
368

369
      activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
370
      assert activities == [activity_two, activity]
lain's avatar
lain committed
371 372 373
    end
  end

374 375 376
  test "doesn't return blocked activities" do
    activity_one = insert(:note_activity)
    activity_two = insert(:note_activity)
377
    activity_three = insert(:note_activity)
378
    user = insert(:user)
379
    booster = insert(:user)
380 381
    {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})

382 383
    activities =
      ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
384 385

    assert Enum.member?(activities, activity_two)
386
    assert Enum.member?(activities, activity_three)
387 388 389 390
    refute Enum.member?(activities, activity_one)

    {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})

391 392
    activities =
      ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
393 394

    assert Enum.member?(activities, activity_two)
395 396 397 398 399
    assert Enum.member?(activities, activity_three)
    assert Enum.member?(activities, activity_one)

    {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
    {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
400
    %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
401
    activity_three = Activity.get_by_id(activity_three.id)
402

403 404
    activities =
      ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
405 406 407 408

    assert Enum.member?(activities, activity_two)
    refute Enum.member?(activities, activity_three)
    refute Enum.member?(activities, boost_activity)
409
    assert Enum.member?(activities, activity_one)
lain's avatar
lain committed
410

411 412
    activities =
      ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
lain's avatar
lain committed
413 414

    assert Enum.member?(activities, activity_two)
415 416
    assert Enum.member?(activities, activity_three)
    assert Enum.member?(activities, boost_activity)
lain's avatar
lain committed
417
    assert Enum.member?(activities, activity_one)
418 419
  end

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
  test "doesn't return transitive interactions concerning blocked users" do
    blocker = insert(:user)
    blockee = insert(:user)
    friend = insert(:user)

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

    {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})

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

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

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

    activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})

    assert Enum.member?(activities, activity_one)
    refute Enum.member?(activities, activity_two)
    refute Enum.member?(activities, activity_three)
    refute Enum.member?(activities, activity_four)
  end

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  test "doesn't return announce activities concerning blocked users" do
    blocker = insert(:user)
    blockee = insert(:user)
    friend = insert(:user)

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

    {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})

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

    {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)

    activities =
      ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
      |> Enum.map(fn act -> act.id end)

    assert Enum.member?(activities, activity_one.id)
    refute Enum.member?(activities, activity_two.id)
    refute Enum.member?(activities, activity_three.id)
  end

Aaron Tinio's avatar
Aaron Tinio committed
465 466 467 468 469 470 471 472 473 474 475 476
  test "doesn't return activities from blocked domains" do
    domain = "dogwhistle.zone"
    domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
    note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
    activity = insert(:note_activity, %{note: note})
    user = insert(:user)
    {:ok, user} = User.block_domain(user, domain)

    activities =
      ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})

    refute activity in activities
477 478 479 480 481 482 483 484 485

    followed_user = insert(:user)
    ActivityPub.follow(user, followed_user)
    {:ok, repeat_activity, _} = CommonAPI.repeat(activity.id, followed_user)

    activities =
      ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})

    refute repeat_activity in activities
Aaron Tinio's avatar
Aaron Tinio committed
486 487
  end

lain's avatar
lain committed
488 489 490 491 492 493 494 495
  test "doesn't return muted activities" do
    activity_one = insert(:note_activity)
    activity_two = insert(:note_activity)
    activity_three = insert(:note_activity)
    user = insert(:user)
    booster = insert(:user)
    {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})

496 497
    activities =
      ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
lain's avatar
lain committed
498 499 500 501 502

    assert Enum.member?(activities, activity_two)
    assert Enum.member?(activities, activity_three)
    refute Enum.member?(activities, activity_one)

lain's avatar
lain committed
503
    # Calling with 'with_muted' will deliver muted activities, too.
504 505 506 507 508 509
    activities =
      ActivityPub.fetch_activities([], %{
        "muting_user" => user,
        "with_muted" => true,
        "skip_preload" => true
      })
lain's avatar
lain committed
510 511 512 513 514

    assert Enum.member?(activities, activity_two)
    assert Enum.member?(activities, activity_three)
    assert Enum.member?(activities, activity_one)

lain's avatar
lain committed
515 516
    {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})

517 518
    activities =
      ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
lain's avatar
lain committed
519 520 521 522 523 524 525 526

    assert Enum.member?(activities, activity_two)
    assert Enum.member?(activities, activity_three)
    assert Enum.member?(activities, activity_one)

    {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
    {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
    %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
527
    activity_three = Activity.get_by_id(activity_three.id)
lain's avatar
lain committed
528

529 530
    activities =
      ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
lain's avatar
lain committed
531 532 533 534 535 536

    assert Enum.member?(activities, activity_two)
    refute Enum.member?(activities, activity_three)
    refute Enum.member?(activities, boost_activity)
    assert Enum.member?(activities, activity_one)

537
    activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
lain's avatar
lain committed
538 539 540 541 542 543 544

    assert Enum.member?(activities, activity_two)
    assert Enum.member?(activities, activity_three)
    assert Enum.member?(activities, boost_activity)
    assert Enum.member?(activities, activity_one)
  end

545 546 547 548 549 550 551 552 553 554 555 556 557 558
  test "does include announces on request" do
    activity_three = insert(:note_activity)
    user = insert(:user)
    booster = insert(:user)

    {:ok, user} = User.follow(user, booster)

    {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)

    [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])

    assert announce_activity.id == announce.id
  end

559 560 561 562 563 564 565 566 567 568
  test "excludes reblogs on request" do
    user = insert(:user)
    {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
    {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})

    [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})

    assert activity == expected_activity
  end

lain's avatar
lain committed
569
  describe "public fetch activities" do
lain's avatar
lain committed
570 571
    test "doesn't retrieve unlisted activities" do
      user = insert(:user)
lain's avatar
lain committed
572

Maksim's avatar
Maksim committed
573
      {:ok, _unlisted_activity} =
lain's avatar
lain committed
574 575
        CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})

lain's avatar
lain committed
576 577 578 579 580 581 582
      {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})

      [activity] = ActivityPub.fetch_public_activities()

      assert activity == listed_activity
    end

lain's avatar
lain committed
583
    test "retrieves public activities" do
lain's avatar
lain committed
584
      _activities = ActivityPub.fetch_public_activities()
lain's avatar
lain committed
585

lain's avatar
lain committed
586
      %{public: public} = ActivityBuilder.public_and_non_public()
lain's avatar
lain committed
587

lain's avatar
lain committed
588
      activities = ActivityPub.fetch_public_activities()
lain's avatar
lain committed
589 590 591
      assert length(activities) == 1
      assert Enum.at(activities, 0) == public
    end
lain's avatar
lain committed
592 593 594 595 596

    test "retrieves a maximum of 20 activities" do
      activities = ActivityBuilder.insert_list(30)
      last_expected = List.last(activities)

lain's avatar
lain committed
597
      activities = ActivityPub.fetch_public_activities()
lain's avatar
lain committed
598 599 600 601 602
      last = List.last(activities)

      assert length(activities) == 20
      assert last == last_expected
    end
lain's avatar
lain committed
603 604 605 606 607 608 609

    test "retrieves ids starting from a since_id" do
      activities = ActivityBuilder.insert_list(30)
      later_activities = ActivityBuilder.insert_list(10)
      since_id = List.last(activities).id
      last_expected = List.last(later_activities)

lain's avatar
lain committed
610
      activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
lain's avatar
lain committed
611 612 613 614 615
      last = List.last(activities)

      assert length(activities) == 10
      assert last == last_expected
    end
616 617 618 619 620 621 622 623 624 625 626 627 628 629

    test "retrieves ids up to max_id" do
      _first_activities = ActivityBuilder.insert_list(10)
      activities = ActivityBuilder.insert_list(20)
      later_activities = ActivityBuilder.insert_list(10)
      max_id = List.first(later_activities).id
      last_expected = List.last(activities)

      activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
      last = List.last(activities)

      assert length(activities) == 20
      assert last == last_expected
    end
630 631 632 633 634 635 636 637 638 639 640

    test "doesn't return reblogs for users for whom reblogs have been muted" do
      activity = insert(:note_activity)
      user = insert(:user)
      booster = insert(:user)
      {:ok, user} = CommonAPI.hide_reblogs(user, booster)

      {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)

      activities = ActivityPub.fetch_activities([], %{"muting_user" => user})

rinpatch's avatar
rinpatch committed
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
      refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
    end

    test "returns reblogs for users for whom reblogs have not been muted" do
      activity = insert(:note_activity)
      user = insert(:user)
      booster = insert(:user)
      {:ok, user} = CommonAPI.hide_reblogs(user, booster)
      {:ok, user} = CommonAPI.show_reblogs(user, booster)

      {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)

      activities = ActivityPub.fetch_activities([], %{"muting_user" => user})

      assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
656
    end
lain's avatar
lain committed
657
  end
lain's avatar
lain committed
658

lain's avatar
lain committed
659 660 661 662 663 664 665 666 667 668 669 670
  describe "like an object" do
    test "adds a like activity to the db" do
      note_activity = insert(:note_activity)
      object = Object.get_by_ap_id(note_activity.data["object"]["id"])
      user = insert(:user)
      user_two = insert(:user)

      {:ok, like_activity, object} = ActivityPub.like(user, object)

      assert like_activity.data["actor"] == user.ap_id
      assert like_activity.data["type"] == "Like"
      assert like_activity.data["object"] == object.data["id"]
671
      assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
lain's avatar
lain committed
672
      assert like_activity.data["context"] == object.data["context"]
lain's avatar
lain committed
673
      assert object.data["like_count"] == 1
lain's avatar
lain committed
674 675 676 677 678 679 680
      assert object.data["likes"] == [user.ap_id]

      # Just return the original activity if the user already liked it.
      {:ok, same_like_activity, object} = ActivityPub.like(user, object)

      assert like_activity == same_like_activity
      assert object.data["likes"] == [user.ap_id]
lain's avatar
lain committed
681

682
      [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
lain's avatar
lain committed
683 684 685 686 687 688 689
      assert note_activity.data["object"]["like_count"] == 1

      {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
      assert object.data["like_count"] == 2
    end
  end

lain's avatar
lain committed
690 691 692 693 694 695 696 697 698 699 700 701 702
  describe "unliking" do
    test "unliking a previously liked object" do
      note_activity = insert(:note_activity)
      object = Object.get_by_ap_id(note_activity.data["object"]["id"])
      user = insert(:user)

      # Unliking something that hasn't been liked does nothing
      {:ok, object} = ActivityPub.unlike(user, object)
      assert object.data["like_count"] == 0

      {:ok, like_activity, object} = ActivityPub.like(user, object)
      assert object.data["like_count"] == 1

Thog's avatar
Thog committed
703
      {:ok, _, _, object} = ActivityPub.unlike(user, object)
lain's avatar
lain committed
704 705
      assert object.data["like_count"] == 0

706
      assert Activity.get_by_id(like_activity.id) == nil
lain's avatar
lain committed
707 708 709
    end
  end

lain's avatar
lain committed
710 711 712 713 714 715 716 717 718
  describe "announcing an object" do
    test "adds an announce activity to the db" do
      note_activity = insert(:note_activity)
      object = Object.get_by_ap_id(note_activity.data["object"]["id"])
      user = insert(:user)

      {:ok, announce_activity, object} = ActivityPub.announce(user, object)
      assert object.data["announcement_count"] == 1
      assert object.data["announcements"] == [user.ap_id]
lain's avatar
lain committed
719 720 721 722 723 724

      assert announce_activity.data["to"] == [
               User.ap_followers(user),
               note_activity.data["actor"]
             ]

lain's avatar
lain committed
725 726
      assert announce_activity.data["object"] == object.data["id"]
      assert announce_activity.data["actor"] == user.ap_id
lain's avatar
lain committed
727
      assert announce_activity.data["context"] == object.data["context"]
lain's avatar
lain committed
728 729 730
    end
  end

normandy's avatar
normandy committed
731 732 733 734 735 736 737
  describe "unannouncing an object" do
    test "unannouncing a previously announced object" do
      note_activity = insert(:note_activity)
      object = Object.get_by_ap_id(note_activity.data["object"]["id"])
      user = insert(:user)

      # Unannouncing an object that is not announced does nothing
normandy's avatar
normandy committed
738 739
      # {:ok, object} = ActivityPub.unannounce(user, object)
      # assert object.data["announcement_count"] == 0
normandy's avatar
normandy committed
740 741 742 743

      {:ok, announce_activity, object} = ActivityPub.announce(user, object)
      assert object.data["announcement_count"] == 1

744
      {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
normandy's avatar
normandy committed
745 746
      assert object.data["announcement_count"] == 0

747 748
      assert unannounce_activity.data["to"] == [
               User.ap_followers(user),
normandy's avatar
normandy committed
749
               announce_activity.data["actor"]
normandy's avatar
normandy committed
750
             ]
normandy's avatar
normandy committed
751

752
      assert unannounce_activity.data["type"] == "Undo"
753
      assert unannounce_activity.data["object"] == announce_activity.data
754
      assert unannounce_activity.data["actor"] == user.ap_id
normandy's avatar
normandy committed
755
      assert unannounce_activity.data["context"] == announce_activity.data["context"]
756

757
      assert Activity.get_by_id(announce_activity.id) == nil
normandy's avatar
normandy committed
758 759 760
    end
  end

lain's avatar
lain committed
761 762
  describe "uploading files" do
    test "copies the file to the configured folder" do
lain's avatar
lain committed
763 764 765 766 767
      file = %Plug.Upload{
        content_type: "image/jpg",
        path: Path.absname("test/fixtures/image.jpg"),
        filename: "an_image.jpg"
      }
lain's avatar
lain committed
768 769 770 771

      {:ok, %Object{} = object} = ActivityPub.upload(file)
      assert object.data["name"] == "an_image.jpg"
    end
lain's avatar
lain committed
772 773 774

    test "works with base64 encoded images" do
      file = %{
lain's avatar
lain committed
775
        "img" => data_uri()
lain's avatar
lain committed
776 777
      }

lain's avatar
lain committed
778
      {:ok, %Object{}} = ActivityPub.upload(file)
lain's avatar
lain committed
779
    end
lain's avatar
lain committed
780
  end
lain's avatar
lain committed
781

dtluna's avatar
dtluna committed
782 783 784 785 786
  describe "fetch the latest Follow" do
    test "fetches the latest Follow activity" do
      %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
      follower = Repo.get_by(User, ap_id: activity.data["actor"])
      followed = Repo.get_by(User, ap_id: activity.data["object"])
787

lain's avatar
lain committed
788
      assert activity == Utils.fetch_latest_follow(follower, followed)
789
    end
790 791
  end

792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
  describe "following / unfollowing" do
    test "creates a follow activity" do
      follower = insert(:user)
      followed = insert(:user)

      {:ok, activity} = ActivityPub.follow(follower, followed)
      assert activity.data["type"] == "Follow"
      assert activity.data["actor"] == follower.ap_id
      assert activity.data["object"] == followed.ap_id
    end

    test "creates an undo activity for the last follow" do
      follower = insert(:user)
      followed = insert(:user)

      {:ok, follow_activity} = ActivityPub.follow(follower, followed)
      {:ok, activity} = ActivityPub.unfollow(follower, followed)

      assert activity.data["type"] == "Undo"
      assert activity.data["actor"] == follower.ap_id
812 813 814 815

      assert is_map(activity.data["object"])
      assert activity.data["object"]["type"] == "Follow"
      assert activity.data["object"]["object"] == followed.ap_id
normandy's avatar
normandy committed
816
      assert activity.data["object"]["id"] == follow_activity.data["id"]
817 818 819
    end
  end

820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
  describe "blocking / unblocking" do
    test "creates a block activity" do
      blocker = insert(:user)
      blocked = insert(:user)

      {:ok, activity} = ActivityPub.block(blocker, blocked)

      assert activity.data["type"] == "Block"
      assert activity.data["actor"] == blocker.ap_id
      assert activity.data["object"] == blocked.ap_id
    end

    test "creates an undo activity for the last block" do
      blocker = insert(:user)
      blocked = insert(:user)

      {:ok, block_activity} = ActivityPub.block(blocker, blocked)
      {:ok, activity} = ActivityPub.unblock(blocker, blocked)

      assert activity.data["type"] == "Undo"
      assert activity.data["actor"] == blocker.ap_id

      assert is_map(activity.data["object"])
      assert activity.data["object"]["type"] == "Block"
      assert activity.data["object"]["object"] == blocked.ap_id
      assert activity.data["object"]["id"] == block_activity.data["id"]
    end
  end

lain's avatar
lain committed
849 850 851 852 853 854 855 856 857 858
  describe "deletion" do
    test "it creates a delete activity and deletes the original object" do
      note = insert(:note_activity)
      object = Object.get_by_ap_id(note.data["object"]["id"])
      {:ok, delete} = ActivityPub.delete(object)

      assert delete.data["type"] == "Delete"
      assert delete.data["actor"] == note.data["actor"]
      assert delete.data["object"] == note.data["object"]["id"]

859
      assert Activity.get_by_id(delete.id) != nil
lain's avatar
lain committed
860

Maxim Filippov's avatar
Maxim Filippov committed
861
      assert Repo.get(Object, object.id).data["type"] == "Tombstone"
lain's avatar
lain committed
862
    end
863 864 865 866 867

    test "decrements user note count only for public activities" do
      user = insert(:user, info: %{note_count: 10})

      {:ok, a1} =
minibikini's avatar
minibikini committed
868 869 870 871
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "yeah",
          "visibility" => "public"
        })
872 873

      {:ok, a2} =
minibikini's avatar
minibikini committed
874 875 876 877
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "yeah",
          "visibility" => "unlisted"
        })
878 879

      {:ok, a3} =
minibikini's avatar
minibikini committed
880 881 882 883
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "yeah",
          "visibility" => "private"
        })
884 885

      {:ok, a4} =
minibikini's avatar
minibikini committed
886 887 888 889
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "yeah",
          "visibility" => "direct"
        })
890

rinpatch's avatar
rinpatch committed
891 892 893 894
      {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
      {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
      {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
      {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
895

minibikini's avatar
minibikini committed
896
      user = User.get_cached_by_id(user.id)
897 898
      assert user.info.note_count == 10
    end
899

Karen Konou's avatar
Karen Konou committed
900 901 902
    test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
      user = insert(:user)
      note = insert(:note_activity)
903 904 905 906 907 908 909 910 911 912 913 914 915

      {:ok, object} =
        Object.get_by_ap_id(note.data["object"]["id"])
        |> Object.change(%{
          data: %{
            "actor" => note.data["object"]["actor"],
            "id" => note.data["object"]["id"],
            "to" => [user.ap_id],
            "type" => "Note"
          }
        })
        |> Object.update_and_set_cache()

Karen Konou's avatar
Karen Konou committed
916 917 918 919
      {:ok, delete} = ActivityPub.delete(object)

      assert user.ap_id in delete.data["to"]
    end
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949

    test "decreases reply count" do
      user = insert(:user)
      user2 = insert(:user)

      {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
      reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
      ap_id = activity.data["id"]

      {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
      {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
      {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
      {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))

      _ = CommonAPI.delete(direct_reply.id, user2)
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 2

      _ = CommonAPI.delete(private_reply.id, user2)
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 2

      _ = CommonAPI.delete(public_reply.id, user2)
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 1

      _ = CommonAPI.delete(unlisted_reply.id, user2)
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 0
    end
lain's avatar
lain committed
950 951
  end

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
  describe "timeline post-processing" do
    test "it filters broken threads" do
      user1 = insert(:user)
      user2 = insert(:user)
      user3 = insert(:user)

      {:ok, user1} = User.follow(user1, user3)
      assert User.following?(user1, user3)

      {:ok, user2} = User.follow(user2, user3)
      assert User.following?(user2, user3)

      {:ok, user3} = User.follow(user3, user2)
      assert User.following?(user3, user2)

      {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})

kaniini's avatar
kaniini committed
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
      {:ok, private_activity_1} =
        CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})

      {:ok, private_activity_2} =
        CommonAPI.post(user2, %{
          "status" => "hi 3",
          "visibility" => "private",
          "in_reply_to_status_id" => private_activity_1.id
        })

      {:ok, private_activity_3} =
        CommonAPI.post(user3, %{
          "status" => "hi 4",
          "visibility" => "private",
          "in_reply_to_status_id" => private_activity_2.id
        })
985

kaniini's avatar
kaniini committed
986 987 988
      activities =
        ActivityPub.fetch_activities([user1.ap_id | user1.following])
        |> Enum.map(fn a -> a.id end)
989

990
      private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
991

kaniini's avatar
kaniini committed
992
      assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
993

994 995
      assert length(activities) == 3

kaniini's avatar
kaniini committed
996 997
      activities =
        ActivityPub.fetch_activities([user1.ap_id | user1.following], %{"user" => user1})
kaniini's avatar
kaniini committed
998
        |> Enum.map(fn a -> a.id end)
999

kaniini's avatar
kaniini committed
1000
      assert [public_activity.id, private_activity_1.id] == activities
1001 1002 1003 1004
      assert length(activities) == 2
    end
  end

lain's avatar
lain committed
1005 1006 1007
  describe "update" do
    test "it creates an update activity with the new user data" do
      user = insert(:user)
1008
      {:ok, user} = User.ensure_keys_present(user)
lain's avatar
lain committed
1009
      user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
lain's avatar
lain committed
1010 1011 1012 1013 1014 1015 1016 1017

      {:ok, update} =
        ActivityPub.update(%{
          actor: user_data["id"],
          to: [user.follower_address],
          cc: [],
          object: user_data
        })
lain's avatar
lain committed
1018 1019 1020 1021 1022 1023 1024 1025

      assert update.data["actor"] == user.ap_id
      assert update.data["to"] == [user.follower_address]
      assert update.data["object"]["id"] == user_data["id"]
      assert update.data["object"]["type"] == user_data["type"]
    end
  end

minibikini's avatar
minibikini committed
1026 1027
  test "returned pinned statuses" do
    Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
minibikini's avatar
minibikini committed
1028 1029 1030 1031 1032 1033 1034
    user = insert(:user)

    {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
    {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
    {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})

    CommonAPI.pin(activity_one.id, user)
minibikini's avatar
minibikini committed
1035
    user = refresh_record(user)
minibikini's avatar
minibikini committed
1036 1037

    CommonAPI.pin(activity_two.id, user)
minibikini's avatar
minibikini committed
1038
    user = refresh_record(user)
minibikini's avatar
minibikini committed
1039 1040

    CommonAPI.pin(activity_three.id, user)
minibikini's avatar
minibikini committed
1041
    user = refresh_record(user)
minibikini's avatar
minibikini committed
1042 1043 1044 1045 1046 1047

    activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})

    assert 3 = length(activities)
  end

minibikini's avatar
Reports  
minibikini committed
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
  test "it can create a Flag activity" do
    reporter = insert(:user)
    target_account = insert(:user)
    {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
    context = Utils.generate_context_id()
    content = "foobar"

    reporter_ap_id = reporter.ap_id
    target_ap_id = target_account.ap_id
    activity_ap_id = activity.data["id"]

    assert {:ok, activity} =
             ActivityPub.flag(%{
               actor: reporter,
               context: context,
               account: target_account,
               statuses: [activity],
               content: content
             })

    assert %Activity{
             actor: ^reporter_ap_id,
             data: %{
               "type" => "Flag",
               "content" => ^content,
               "context" => ^context,
               "object" => [^target_ap_id, ^activity_ap_id]
             }
           } = activity
  end