1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
6 use Oban.Testing, repo: Pleroma.Repo
11 alias Pleroma.Chat.MessageReference
12 alias Pleroma.Notification
15 alias Pleroma.Tests.ObanHelpers
17 alias Pleroma.Web.ActivityPub.ActivityPub
18 alias Pleroma.Web.ActivityPub.Builder
19 alias Pleroma.Web.ActivityPub.SideEffects
20 alias Pleroma.Web.CommonAPI
22 import Pleroma.Factory
25 describe "handle_after_transaction" do
26 test "it streams out notifications and streams" do
27 author = insert(:user, local: true)
28 recipient = insert(:user, local: true)
30 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
32 {:ok, create_activity_data, _meta} =
33 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
35 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
37 {:ok, _create_activity, meta} =
38 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
40 assert [notification] = meta[:notifications]
47 stream: fn _, _ -> nil end
58 SideEffects.handle_after_transaction(meta)
60 assert called(Pleroma.Web.Streamer.stream(["user", "user:notification"], notification))
61 assert called(Pleroma.Web.Streamer.stream(["user", "user:pleroma_chat"], :_))
62 assert called(Pleroma.Web.Push.send(notification))
67 describe "delete objects" do
70 other_user = insert(:user)
72 {:ok, op} = CommonAPI.post(other_user, %{status: "big oof"})
73 {:ok, post} = CommonAPI.post(user, %{status: "hey", in_reply_to_id: op})
74 {:ok, favorite} = CommonAPI.favorite(user, post.id)
75 object = Object.normalize(post)
76 {:ok, delete_data, _meta} = Builder.delete(user, object.data["id"])
77 {:ok, delete_user_data, _meta} = Builder.delete(user, user.ap_id)
78 {:ok, delete, _meta} = ActivityPub.persist(delete_data, local: true)
79 {:ok, delete_user, _meta} = ActivityPub.persist(delete_user_data, local: true)
86 delete_user: delete_user,
92 test "it handles object deletions", %{
100 with_mock Pleroma.Web.ActivityPub.ActivityPub, [:passthrough],
101 stream_out: fn _ -> nil end,
102 stream_out_participations: fn _, _ -> nil end do
103 {:ok, delete, _} = SideEffects.handle(delete)
104 user = User.get_cached_by_ap_id(object.data["actor"])
106 assert called(Pleroma.Web.ActivityPub.ActivityPub.stream_out(delete))
107 assert called(Pleroma.Web.ActivityPub.ActivityPub.stream_out_participations(object, user))
110 object = Object.get_by_id(object.id)
111 assert object.data["type"] == "Tombstone"
112 refute Activity.get_by_id(post.id)
113 refute Activity.get_by_id(favorite.id)
115 user = User.get_by_id(user.id)
116 assert user.note_count == 0
118 object = Object.normalize(op.data["object"], false)
120 assert object.data["repliesCount"] == 0
123 test "it handles object deletions when the object itself has been pruned", %{
130 with_mock Pleroma.Web.ActivityPub.ActivityPub, [:passthrough],
131 stream_out: fn _ -> nil end,
132 stream_out_participations: fn _, _ -> nil end do
133 {:ok, delete, _} = SideEffects.handle(delete)
134 user = User.get_cached_by_ap_id(object.data["actor"])
136 assert called(Pleroma.Web.ActivityPub.ActivityPub.stream_out(delete))
137 assert called(Pleroma.Web.ActivityPub.ActivityPub.stream_out_participations(object, user))
140 object = Object.get_by_id(object.id)
141 assert object.data["type"] == "Tombstone"
142 refute Activity.get_by_id(post.id)
144 user = User.get_by_id(user.id)
145 assert user.note_count == 0
147 object = Object.normalize(op.data["object"], false)
149 assert object.data["repliesCount"] == 0
152 test "it handles user deletions", %{delete_user: delete, user: user} do
153 {:ok, _delete, _} = SideEffects.handle(delete)
154 ObanHelpers.perform_all()
156 assert User.get_cached_by_ap_id(user.ap_id).deactivated
160 describe "EmojiReact objects" do
162 poster = insert(:user)
165 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
167 {:ok, emoji_react_data, []} = Builder.emoji_react(user, post.object, "👌")
168 {:ok, emoji_react, _meta} = ActivityPub.persist(emoji_react_data, local: true)
170 %{emoji_react: emoji_react, user: user, poster: poster}
173 test "adds the reaction to the object", %{emoji_react: emoji_react, user: user} do
174 {:ok, emoji_react, _} = SideEffects.handle(emoji_react)
175 object = Object.get_by_ap_id(emoji_react.data["object"])
177 assert object.data["reaction_count"] == 1
178 assert ["👌", [user.ap_id]] in object.data["reactions"]
181 test "creates a notification", %{emoji_react: emoji_react, poster: poster} do
182 {:ok, emoji_react, _} = SideEffects.handle(emoji_react)
183 assert Repo.get_by(Notification, user_id: poster.id, activity_id: emoji_react.id)
187 describe "delete users with confirmation pending" do
189 user = insert(:user, confirmation_pending: true)
190 {:ok, delete_user_data, _meta} = Builder.delete(user, user.ap_id)
191 {:ok, delete_user, _meta} = ActivityPub.persist(delete_user_data, local: true)
192 {:ok, delete: delete_user, user: user}
195 test "when activation is not required", %{delete: delete, user: user} do
196 clear_config([:instance, :account_activation_required], false)
197 {:ok, _, _} = SideEffects.handle(delete)
198 ObanHelpers.perform_all()
200 assert User.get_cached_by_id(user.id).deactivated
203 test "when activation is required", %{delete: delete, user: user} do
204 clear_config([:instance, :account_activation_required], true)
205 {:ok, _, _} = SideEffects.handle(delete)
206 ObanHelpers.perform_all()
208 refute User.get_cached_by_id(user.id)
212 describe "Undo objects" do
214 poster = insert(:user)
216 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
217 {:ok, like} = CommonAPI.favorite(user, post.id)
218 {:ok, reaction} = CommonAPI.react_with_emoji(post.id, user, "👍")
219 {:ok, announce} = CommonAPI.repeat(post.id, user)
220 {:ok, block} = ActivityPub.block(user, poster)
221 User.block(user, poster)
223 {:ok, undo_data, _meta} = Builder.undo(user, like)
224 {:ok, like_undo, _meta} = ActivityPub.persist(undo_data, local: true)
226 {:ok, undo_data, _meta} = Builder.undo(user, reaction)
227 {:ok, reaction_undo, _meta} = ActivityPub.persist(undo_data, local: true)
229 {:ok, undo_data, _meta} = Builder.undo(user, announce)
230 {:ok, announce_undo, _meta} = ActivityPub.persist(undo_data, local: true)
232 {:ok, undo_data, _meta} = Builder.undo(user, block)
233 {:ok, block_undo, _meta} = ActivityPub.persist(undo_data, local: true)
236 like_undo: like_undo,
239 reaction_undo: reaction_undo,
241 announce_undo: announce_undo,
243 block_undo: block_undo,
250 test "deletes the original block", %{block_undo: block_undo, block: block} do
251 {:ok, _block_undo, _} = SideEffects.handle(block_undo)
252 refute Activity.get_by_id(block.id)
255 test "unblocks the blocked user", %{block_undo: block_undo, block: block} do
256 blocker = User.get_by_ap_id(block.data["actor"])
257 blocked = User.get_by_ap_id(block.data["object"])
259 {:ok, _block_undo, _} = SideEffects.handle(block_undo)
260 refute User.blocks?(blocker, blocked)
263 test "an announce undo removes the announce from the object", %{
264 announce_undo: announce_undo,
267 {:ok, _announce_undo, _} = SideEffects.handle(announce_undo)
269 object = Object.get_by_ap_id(post.data["object"])
271 assert object.data["announcement_count"] == 0
272 assert object.data["announcements"] == []
275 test "deletes the original announce", %{announce_undo: announce_undo, announce: announce} do
276 {:ok, _announce_undo, _} = SideEffects.handle(announce_undo)
277 refute Activity.get_by_id(announce.id)
280 test "a reaction undo removes the reaction from the object", %{
281 reaction_undo: reaction_undo,
284 {:ok, _reaction_undo, _} = SideEffects.handle(reaction_undo)
286 object = Object.get_by_ap_id(post.data["object"])
288 assert object.data["reaction_count"] == 0
289 assert object.data["reactions"] == []
292 test "deletes the original reaction", %{reaction_undo: reaction_undo, reaction: reaction} do
293 {:ok, _reaction_undo, _} = SideEffects.handle(reaction_undo)
294 refute Activity.get_by_id(reaction.id)
297 test "a like undo removes the like from the object", %{like_undo: like_undo, post: post} do
298 {:ok, _like_undo, _} = SideEffects.handle(like_undo)
300 object = Object.get_by_ap_id(post.data["object"])
302 assert object.data["like_count"] == 0
303 assert object.data["likes"] == []
306 test "deletes the original like", %{like_undo: like_undo, like: like} do
307 {:ok, _like_undo, _} = SideEffects.handle(like_undo)
308 refute Activity.get_by_id(like.id)
312 describe "like objects" do
314 poster = insert(:user)
316 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
318 {:ok, like_data, _meta} = Builder.like(user, post.object)
319 {:ok, like, _meta} = ActivityPub.persist(like_data, local: true)
321 %{like: like, user: user, poster: poster}
324 test "add the like to the original object", %{like: like, user: user} do
325 {:ok, like, _} = SideEffects.handle(like)
326 object = Object.get_by_ap_id(like.data["object"])
327 assert object.data["like_count"] == 1
328 assert user.ap_id in object.data["likes"]
331 test "creates a notification", %{like: like, poster: poster} do
332 {:ok, like, _} = SideEffects.handle(like)
333 assert Repo.get_by(Notification, user_id: poster.id, activity_id: like.id)
337 describe "creation of ChatMessages" do
338 test "notifies the recipient" do
339 author = insert(:user, local: false)
340 recipient = insert(:user, local: true)
342 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
344 {:ok, create_activity_data, _meta} =
345 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
347 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
349 {:ok, _create_activity, _meta} =
350 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
352 assert Repo.get_by(Notification, user_id: recipient.id, activity_id: create_activity.id)
355 test "it streams the created ChatMessage" do
356 author = insert(:user, local: true)
357 recipient = insert(:user, local: true)
359 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
361 {:ok, create_activity_data, _meta} =
362 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
364 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
366 {:ok, _create_activity, meta} =
367 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
369 assert [_, _] = meta[:streamables]
372 test "it creates a Chat and MessageReferences for the local users and bumps the unread count, except for the author" do
373 author = insert(:user, local: true)
374 recipient = insert(:user, local: true)
376 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
378 {:ok, create_activity_data, _meta} =
379 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
381 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
385 Pleroma.Web.Streamer,
388 stream: fn _, _ -> nil end
395 send: fn _ -> nil end
399 {:ok, _create_activity, meta} =
400 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
402 # The notification gets created
403 assert [notification] = meta[:notifications]
404 assert notification.activity_id == create_activity.id
406 # But it is not sent out
407 refute called(Pleroma.Web.Streamer.stream(["user", "user:notification"], notification))
408 refute called(Pleroma.Web.Push.send(notification))
410 # Same for the user chat stream
411 assert [{topics, _}, _] = meta[:streamables]
412 assert topics == ["user", "user:pleroma_chat"]
413 refute called(Pleroma.Web.Streamer.stream(["user", "user:pleroma_chat"], :_))
415 chat = Chat.get(author.id, recipient.ap_id)
417 [cm_ref] = MessageReference.for_chat_query(chat) |> Repo.all()
419 assert cm_ref.object.data["content"] == "hey"
420 assert cm_ref.unread == false
422 chat = Chat.get(recipient.id, author.ap_id)
424 [cm_ref] = MessageReference.for_chat_query(chat) |> Repo.all()
426 assert cm_ref.object.data["content"] == "hey"
427 assert cm_ref.unread == true
431 test "it creates a Chat for the local users and bumps the unread count" do
432 author = insert(:user, local: false)
433 recipient = insert(:user, local: true)
435 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
437 {:ok, create_activity_data, _meta} =
438 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
440 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
442 {:ok, _create_activity, _meta} =
443 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
445 # An object is created
446 assert Object.get_by_ap_id(chat_message_data["id"])
448 # The remote user won't get a chat
449 chat = Chat.get(author.id, recipient.ap_id)
452 # The local user will get a chat
453 chat = Chat.get(recipient.id, author.ap_id)
456 author = insert(:user, local: true)
457 recipient = insert(:user, local: true)
459 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
461 {:ok, create_activity_data, _meta} =
462 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
464 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
466 {:ok, _create_activity, _meta} =
467 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
469 # Both users are local and get the chat
470 chat = Chat.get(author.id, recipient.ap_id)
473 chat = Chat.get(recipient.id, author.ap_id)
478 describe "announce objects" do
480 poster = insert(:user)
482 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
483 {:ok, private_post} = CommonAPI.post(poster, %{status: "hey", visibility: "private"})
485 {:ok, announce_data, _meta} = Builder.announce(user, post.object, public: true)
487 {:ok, private_announce_data, _meta} =
488 Builder.announce(user, private_post.object, public: false)
490 {:ok, relay_announce_data, _meta} =
491 Builder.announce(Pleroma.Web.ActivityPub.Relay.get_actor(), post.object, public: true)
493 {:ok, announce, _meta} = ActivityPub.persist(announce_data, local: true)
494 {:ok, private_announce, _meta} = ActivityPub.persist(private_announce_data, local: true)
495 {:ok, relay_announce, _meta} = ActivityPub.persist(relay_announce_data, local: true)
501 private_announce: private_announce,
502 relay_announce: relay_announce
506 test "adds the announce to the original object", %{announce: announce, user: user} do
507 {:ok, announce, _} = SideEffects.handle(announce)
508 object = Object.get_by_ap_id(announce.data["object"])
509 assert object.data["announcement_count"] == 1
510 assert user.ap_id in object.data["announcements"]
513 test "does not add the announce to the original object if the actor is a service actor", %{
514 relay_announce: announce
516 {:ok, announce, _} = SideEffects.handle(announce)
517 object = Object.get_by_ap_id(announce.data["object"])
518 assert object.data["announcement_count"] == nil
521 test "creates a notification", %{announce: announce, poster: poster} do
522 {:ok, announce, _} = SideEffects.handle(announce)
523 assert Repo.get_by(Notification, user_id: poster.id, activity_id: announce.id)
526 test "it streams out the announce", %{announce: announce} do
527 with_mock Pleroma.Web.ActivityPub.ActivityPub, [:passthrough], stream_out: fn _ -> nil end do
528 {:ok, announce, _} = SideEffects.handle(announce)
530 assert called(Pleroma.Web.ActivityPub.ActivityPub.stream_out(announce))