1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
23 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 "blocking users" do
70 blocked = insert(:user)
71 User.follow(blocked, user)
72 User.follow(user, blocked)
74 {:ok, block_data, []} = Builder.block(user, blocked)
75 {:ok, block, _meta} = ActivityPub.persist(block_data, local: true)
77 %{user: user, blocked: blocked, block: block}
80 test "it unfollows and blocks", %{user: user, blocked: blocked, block: block} do
81 assert User.following?(user, blocked)
82 assert User.following?(blocked, user)
84 {:ok, _, _} = SideEffects.handle(block)
86 refute User.following?(user, blocked)
87 refute User.following?(blocked, user)
88 assert User.blocks?(user, blocked)
91 test "it blocks but does not unfollow if the relevant setting is set", %{
96 clear_config([:activitypub, :unfollow_blocked], false)
97 assert User.following?(user, blocked)
98 assert User.following?(blocked, user)
100 {:ok, _, _} = SideEffects.handle(block)
102 refute User.following?(user, blocked)
103 assert User.following?(blocked, user)
104 assert User.blocks?(user, blocked)
108 describe "update users" do
110 user = insert(:user, local: false)
111 {:ok, update_data, []} = Builder.update(user, %{"id" => user.ap_id, "name" => "new name!"})
112 {:ok, update, _meta} = ActivityPub.persist(update_data, local: true)
114 %{user: user, update_data: update_data, update: update}
117 test "it updates the user", %{user: user, update: update} do
118 {:ok, _, _} = SideEffects.handle(update)
119 user = User.get_by_id(user.id)
120 assert user.name == "new name!"
123 test "it uses a given changeset to update", %{user: user, update: update} do
124 changeset = Ecto.Changeset.change(user, %{default_scope: "direct"})
126 assert user.default_scope == "public"
127 {:ok, _, _} = SideEffects.handle(update, user_update_changeset: changeset)
128 user = User.get_by_id(user.id)
129 assert user.default_scope == "direct"
133 describe "EmojiReact objects" do
135 poster = insert(:user)
138 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
140 {:ok, emoji_react_data, []} = Builder.emoji_react(user, post.object, "👌")
141 {:ok, emoji_react, _meta} = ActivityPub.persist(emoji_react_data, local: true)
143 %{emoji_react: emoji_react, user: user, poster: poster}
146 test "adds the reaction to the object", %{emoji_react: emoji_react, user: user} do
147 {:ok, emoji_react, _} = SideEffects.handle(emoji_react)
148 object = Object.get_by_ap_id(emoji_react.data["object"])
150 assert object.data["reaction_count"] == 1
151 assert ["👌", [user.ap_id]] in object.data["reactions"]
154 test "creates a notification", %{emoji_react: emoji_react, poster: poster} do
155 {:ok, emoji_react, _} = SideEffects.handle(emoji_react)
156 assert Repo.get_by(Notification, user_id: poster.id, activity_id: emoji_react.id)
160 describe "delete users with confirmation pending" do
162 user = insert(:user, is_confirmed: false)
163 {:ok, delete_user_data, _meta} = Builder.delete(user, user.ap_id)
164 {:ok, delete_user, _meta} = ActivityPub.persist(delete_user_data, local: true)
165 {:ok, delete: delete_user, user: user}
168 test "when activation is required", %{delete: delete, user: user} do
169 clear_config([:instance, :account_activation_required], true)
170 {:ok, _, _} = SideEffects.handle(delete)
171 ObanHelpers.perform_all()
173 refute User.get_cached_by_id(user.id)
177 describe "Undo objects" do
179 poster = insert(:user)
181 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
182 {:ok, like} = CommonAPI.favorite(user, post.id)
183 {:ok, reaction} = CommonAPI.react_with_emoji(post.id, user, "👍")
184 {:ok, announce} = CommonAPI.repeat(post.id, user)
185 {:ok, block} = CommonAPI.block(user, poster)
187 {:ok, undo_data, _meta} = Builder.undo(user, like)
188 {:ok, like_undo, _meta} = ActivityPub.persist(undo_data, local: true)
190 {:ok, undo_data, _meta} = Builder.undo(user, reaction)
191 {:ok, reaction_undo, _meta} = ActivityPub.persist(undo_data, local: true)
193 {:ok, undo_data, _meta} = Builder.undo(user, announce)
194 {:ok, announce_undo, _meta} = ActivityPub.persist(undo_data, local: true)
196 {:ok, undo_data, _meta} = Builder.undo(user, block)
197 {:ok, block_undo, _meta} = ActivityPub.persist(undo_data, local: true)
200 like_undo: like_undo,
203 reaction_undo: reaction_undo,
205 announce_undo: announce_undo,
207 block_undo: block_undo,
214 test "deletes the original block", %{
215 block_undo: block_undo,
218 {:ok, _block_undo, _meta} = SideEffects.handle(block_undo)
220 refute Activity.get_by_id(block.id)
223 test "unblocks the blocked user", %{block_undo: block_undo, block: block} do
224 blocker = User.get_by_ap_id(block.data["actor"])
225 blocked = User.get_by_ap_id(block.data["object"])
227 {:ok, _block_undo, _} = SideEffects.handle(block_undo)
228 refute User.blocks?(blocker, blocked)
231 test "an announce undo removes the announce from the object", %{
232 announce_undo: announce_undo,
235 {:ok, _announce_undo, _} = SideEffects.handle(announce_undo)
237 object = Object.get_by_ap_id(post.data["object"])
239 assert object.data["announcement_count"] == 0
240 assert object.data["announcements"] == []
243 test "deletes the original announce", %{announce_undo: announce_undo, announce: announce} do
244 {:ok, _announce_undo, _} = SideEffects.handle(announce_undo)
245 refute Activity.get_by_id(announce.id)
248 test "a reaction undo removes the reaction from the object", %{
249 reaction_undo: reaction_undo,
252 {:ok, _reaction_undo, _} = SideEffects.handle(reaction_undo)
254 object = Object.get_by_ap_id(post.data["object"])
256 assert object.data["reaction_count"] == 0
257 assert object.data["reactions"] == []
260 test "deletes the original reaction", %{reaction_undo: reaction_undo, reaction: reaction} do
261 {:ok, _reaction_undo, _} = SideEffects.handle(reaction_undo)
262 refute Activity.get_by_id(reaction.id)
265 test "a like undo removes the like from the object", %{like_undo: like_undo, post: post} do
266 {:ok, _like_undo, _} = SideEffects.handle(like_undo)
268 object = Object.get_by_ap_id(post.data["object"])
270 assert object.data["like_count"] == 0
271 assert object.data["likes"] == []
274 test "deletes the original like", %{like_undo: like_undo, like: like} do
275 {:ok, _like_undo, _} = SideEffects.handle(like_undo)
276 refute Activity.get_by_id(like.id)
280 describe "like objects" do
282 poster = insert(:user)
284 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
286 {:ok, like_data, _meta} = Builder.like(user, post.object)
287 {:ok, like, _meta} = ActivityPub.persist(like_data, local: true)
289 %{like: like, user: user, poster: poster}
292 test "add the like to the original object", %{like: like, user: user} do
293 {:ok, like, _} = SideEffects.handle(like)
294 object = Object.get_by_ap_id(like.data["object"])
295 assert object.data["like_count"] == 1
296 assert user.ap_id in object.data["likes"]
299 test "creates a notification", %{like: like, poster: poster} do
300 {:ok, like, _} = SideEffects.handle(like)
301 assert Repo.get_by(Notification, user_id: poster.id, activity_id: like.id)
305 describe "creation of ChatMessages" do
306 test "notifies the recipient" do
307 author = insert(:user, local: false)
308 recipient = insert(:user, local: true)
310 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
312 {:ok, create_activity_data, _meta} =
313 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
315 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
317 {:ok, _create_activity, _meta} =
318 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
320 assert Repo.get_by(Notification, user_id: recipient.id, activity_id: create_activity.id)
323 test "it streams the created ChatMessage" do
324 author = insert(:user, local: true)
325 recipient = insert(:user, local: true)
327 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
329 {:ok, create_activity_data, _meta} =
330 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
332 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
334 {:ok, _create_activity, meta} =
335 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
337 assert [_, _] = meta[:streamables]
340 test "it creates a Chat and MessageReferences for the local users and bumps the unread count, except for the author" do
341 author = insert(:user, local: true)
342 recipient = insert(:user, local: true)
344 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
346 {:ok, create_activity_data, _meta} =
347 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
349 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
353 Pleroma.Web.Streamer,
356 stream: fn _, _ -> nil end
363 send: fn _ -> nil end
367 {:ok, _create_activity, meta} =
368 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
370 # The notification gets created
371 assert [notification] = meta[:notifications]
372 assert notification.activity_id == create_activity.id
374 # But it is not sent out
375 refute called(Pleroma.Web.Streamer.stream(["user", "user:notification"], notification))
376 refute called(Pleroma.Web.Push.send(notification))
378 # Same for the user chat stream
379 assert [{topics, _}, _] = meta[:streamables]
380 assert topics == ["user", "user:pleroma_chat"]
381 refute called(Pleroma.Web.Streamer.stream(["user", "user:pleroma_chat"], :_))
383 chat = Chat.get(author.id, recipient.ap_id)
385 [cm_ref] = MessageReference.for_chat_query(chat) |> Repo.all()
387 assert cm_ref.object.data["content"] == "hey"
388 assert cm_ref.unread == false
390 chat = Chat.get(recipient.id, author.ap_id)
392 [cm_ref] = MessageReference.for_chat_query(chat) |> Repo.all()
394 assert cm_ref.object.data["content"] == "hey"
395 assert cm_ref.unread == true
399 test "it creates a Chat for the local users and bumps the unread count" do
400 author = insert(:user, local: false)
401 recipient = insert(:user, local: true)
403 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
405 {:ok, create_activity_data, _meta} =
406 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
408 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
410 {:ok, _create_activity, _meta} =
411 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
413 # An object is created
414 assert Object.get_by_ap_id(chat_message_data["id"])
416 # The remote user won't get a chat
417 chat = Chat.get(author.id, recipient.ap_id)
420 # The local user will get a chat
421 chat = Chat.get(recipient.id, author.ap_id)
424 author = insert(:user, local: true)
425 recipient = insert(:user, local: true)
427 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
429 {:ok, create_activity_data, _meta} =
430 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
432 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
434 {:ok, _create_activity, _meta} =
435 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
437 # Both users are local and get the chat
438 chat = Chat.get(author.id, recipient.ap_id)
441 chat = Chat.get(recipient.id, author.ap_id)
446 describe "announce objects" do
448 poster = insert(:user)
450 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
451 {:ok, private_post} = CommonAPI.post(poster, %{status: "hey", visibility: "private"})
453 {:ok, announce_data, _meta} = Builder.announce(user, post.object, public: true)
455 {:ok, private_announce_data, _meta} =
456 Builder.announce(user, private_post.object, public: false)
458 {:ok, relay_announce_data, _meta} =
459 Builder.announce(Pleroma.Web.ActivityPub.Relay.get_actor(), post.object, public: true)
461 {:ok, announce, _meta} = ActivityPub.persist(announce_data, local: true)
462 {:ok, private_announce, _meta} = ActivityPub.persist(private_announce_data, local: true)
463 {:ok, relay_announce, _meta} = ActivityPub.persist(relay_announce_data, local: true)
469 private_announce: private_announce,
470 relay_announce: relay_announce
474 test "adds the announce to the original object", %{announce: announce, user: user} do
475 {:ok, announce, _} = SideEffects.handle(announce)
476 object = Object.get_by_ap_id(announce.data["object"])
477 assert object.data["announcement_count"] == 1
478 assert user.ap_id in object.data["announcements"]
481 test "does not add the announce to the original object if the actor is a service actor", %{
482 relay_announce: announce
484 {:ok, announce, _} = SideEffects.handle(announce)
485 object = Object.get_by_ap_id(announce.data["object"])
486 assert object.data["announcement_count"] == nil
489 test "creates a notification", %{announce: announce, poster: poster} do
490 {:ok, announce, _} = SideEffects.handle(announce)
491 assert Repo.get_by(Notification, user_id: poster.id, activity_id: announce.id)
494 test "it streams out the announce", %{announce: announce} do
497 Pleroma.Web.Streamer,
500 stream: fn _, _ -> nil end
507 send: fn _ -> nil end
511 {:ok, announce, _} = SideEffects.handle(announce)
514 Pleroma.Web.Streamer.stream(["user", "list", "public", "public:local"], announce)
517 assert called(Pleroma.Web.Push.send(:_))