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 "Question objects" do
163 question = build(:question, user: user)
164 question_activity = build(:question_activity, question: question)
165 activity_data = Map.put(question_activity.data, "object", question.data["id"])
166 meta = [object_data: question.data, local: false]
168 {:ok, activity, meta} = ActivityPub.persist(activity_data, meta)
170 %{activity: activity, meta: meta}
173 test "enqueues the poll end", %{activity: activity, meta: meta} do
174 {:ok, activity, meta} = SideEffects.handle(activity, meta)
177 worker: Pleroma.Workers.PollWorker,
178 args: %{op: "poll_end", activity_id: activity.id},
179 scheduled_at: NaiveDateTime.from_iso8601!(meta[:object_data]["closed"])
184 describe "delete users with confirmation pending" do
186 user = insert(:user, is_confirmed: false)
187 {:ok, delete_user_data, _meta} = Builder.delete(user, user.ap_id)
188 {:ok, delete_user, _meta} = ActivityPub.persist(delete_user_data, local: true)
189 {:ok, delete: delete_user, user: user}
192 test "when activation is required", %{delete: delete, user: user} do
193 clear_config([:instance, :account_activation_required], true)
194 {:ok, _, _} = SideEffects.handle(delete)
195 ObanHelpers.perform_all()
197 refute User.get_cached_by_id(user.id)
201 describe "Undo objects" do
203 poster = insert(:user)
205 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
206 {:ok, like} = CommonAPI.favorite(user, post.id)
207 {:ok, reaction} = CommonAPI.react_with_emoji(post.id, user, "👍")
208 {:ok, announce} = CommonAPI.repeat(post.id, user)
209 {:ok, block} = CommonAPI.block(user, poster)
211 {:ok, undo_data, _meta} = Builder.undo(user, like)
212 {:ok, like_undo, _meta} = ActivityPub.persist(undo_data, local: true)
214 {:ok, undo_data, _meta} = Builder.undo(user, reaction)
215 {:ok, reaction_undo, _meta} = ActivityPub.persist(undo_data, local: true)
217 {:ok, undo_data, _meta} = Builder.undo(user, announce)
218 {:ok, announce_undo, _meta} = ActivityPub.persist(undo_data, local: true)
220 {:ok, undo_data, _meta} = Builder.undo(user, block)
221 {:ok, block_undo, _meta} = ActivityPub.persist(undo_data, local: true)
224 like_undo: like_undo,
227 reaction_undo: reaction_undo,
229 announce_undo: announce_undo,
231 block_undo: block_undo,
238 test "deletes the original block", %{
239 block_undo: block_undo,
242 {:ok, _block_undo, _meta} = SideEffects.handle(block_undo)
244 refute Activity.get_by_id(block.id)
247 test "unblocks the blocked user", %{block_undo: block_undo, block: block} do
248 blocker = User.get_by_ap_id(block.data["actor"])
249 blocked = User.get_by_ap_id(block.data["object"])
251 {:ok, _block_undo, _} = SideEffects.handle(block_undo)
252 refute User.blocks?(blocker, blocked)
255 test "an announce undo removes the announce from the object", %{
256 announce_undo: announce_undo,
259 {:ok, _announce_undo, _} = SideEffects.handle(announce_undo)
261 object = Object.get_by_ap_id(post.data["object"])
263 assert object.data["announcement_count"] == 0
264 assert object.data["announcements"] == []
267 test "deletes the original announce", %{announce_undo: announce_undo, announce: announce} do
268 {:ok, _announce_undo, _} = SideEffects.handle(announce_undo)
269 refute Activity.get_by_id(announce.id)
272 test "a reaction undo removes the reaction from the object", %{
273 reaction_undo: reaction_undo,
276 {:ok, _reaction_undo, _} = SideEffects.handle(reaction_undo)
278 object = Object.get_by_ap_id(post.data["object"])
280 assert object.data["reaction_count"] == 0
281 assert object.data["reactions"] == []
284 test "deletes the original reaction", %{reaction_undo: reaction_undo, reaction: reaction} do
285 {:ok, _reaction_undo, _} = SideEffects.handle(reaction_undo)
286 refute Activity.get_by_id(reaction.id)
289 test "a like undo removes the like from the object", %{like_undo: like_undo, post: post} do
290 {:ok, _like_undo, _} = SideEffects.handle(like_undo)
292 object = Object.get_by_ap_id(post.data["object"])
294 assert object.data["like_count"] == 0
295 assert object.data["likes"] == []
298 test "deletes the original like", %{like_undo: like_undo, like: like} do
299 {:ok, _like_undo, _} = SideEffects.handle(like_undo)
300 refute Activity.get_by_id(like.id)
304 describe "like objects" do
306 poster = insert(:user)
308 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
310 {:ok, like_data, _meta} = Builder.like(user, post.object)
311 {:ok, like, _meta} = ActivityPub.persist(like_data, local: true)
313 %{like: like, user: user, poster: poster}
316 test "add the like to the original object", %{like: like, user: user} do
317 {:ok, like, _} = SideEffects.handle(like)
318 object = Object.get_by_ap_id(like.data["object"])
319 assert object.data["like_count"] == 1
320 assert user.ap_id in object.data["likes"]
323 test "creates a notification", %{like: like, poster: poster} do
324 {:ok, like, _} = SideEffects.handle(like)
325 assert Repo.get_by(Notification, user_id: poster.id, activity_id: like.id)
329 describe "creation of ChatMessages" do
330 test "notifies the recipient" do
331 author = insert(:user, local: false)
332 recipient = insert(:user, local: true)
334 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
336 {:ok, create_activity_data, _meta} =
337 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
339 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
341 {:ok, _create_activity, _meta} =
342 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
344 assert Repo.get_by(Notification, user_id: recipient.id, activity_id: create_activity.id)
347 test "it streams the created ChatMessage" do
348 author = insert(:user, local: true)
349 recipient = insert(:user, local: true)
351 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
353 {:ok, create_activity_data, _meta} =
354 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
356 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
358 {:ok, _create_activity, meta} =
359 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
361 assert [_, _] = meta[:streamables]
364 test "it creates a Chat and MessageReferences for the local users and bumps the unread count, except for the author" do
365 author = insert(:user, local: true)
366 recipient = insert(:user, local: true)
368 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
370 {:ok, create_activity_data, _meta} =
371 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
373 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
377 Pleroma.Web.Streamer,
380 stream: fn _, _ -> nil end
387 send: fn _ -> nil end
391 {:ok, _create_activity, meta} =
392 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
394 # The notification gets created
395 assert [notification] = meta[:notifications]
396 assert notification.activity_id == create_activity.id
398 # But it is not sent out
399 refute called(Pleroma.Web.Streamer.stream(["user", "user:notification"], notification))
400 refute called(Pleroma.Web.Push.send(notification))
402 # Same for the user chat stream
403 assert [{topics, _}, _] = meta[:streamables]
404 assert topics == ["user", "user:pleroma_chat"]
405 refute called(Pleroma.Web.Streamer.stream(["user", "user:pleroma_chat"], :_))
407 chat = Chat.get(author.id, recipient.ap_id)
409 [cm_ref] = MessageReference.for_chat_query(chat) |> Repo.all()
411 assert cm_ref.object.data["content"] == "hey"
412 assert cm_ref.unread == false
414 chat = Chat.get(recipient.id, author.ap_id)
416 [cm_ref] = MessageReference.for_chat_query(chat) |> Repo.all()
418 assert cm_ref.object.data["content"] == "hey"
419 assert cm_ref.unread == true
423 test "it creates a Chat for the local users and bumps the unread count" do
424 author = insert(:user, local: false)
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 # An object is created
438 assert Object.get_by_ap_id(chat_message_data["id"])
440 # The remote user won't get a chat
441 chat = Chat.get(author.id, recipient.ap_id)
444 # The local user will get a chat
445 chat = Chat.get(recipient.id, author.ap_id)
448 author = insert(:user, local: true)
449 recipient = insert(:user, local: true)
451 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
453 {:ok, create_activity_data, _meta} =
454 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
456 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
458 {:ok, _create_activity, _meta} =
459 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
461 # Both users are local and get the chat
462 chat = Chat.get(author.id, recipient.ap_id)
465 chat = Chat.get(recipient.id, author.ap_id)
470 describe "announce objects" do
472 poster = insert(:user)
474 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
475 {:ok, private_post} = CommonAPI.post(poster, %{status: "hey", visibility: "private"})
477 {:ok, announce_data, _meta} = Builder.announce(user, post.object, public: true)
479 {:ok, private_announce_data, _meta} =
480 Builder.announce(user, private_post.object, public: false)
482 {:ok, relay_announce_data, _meta} =
483 Builder.announce(Pleroma.Web.ActivityPub.Relay.get_actor(), post.object, public: true)
485 {:ok, announce, _meta} = ActivityPub.persist(announce_data, local: true)
486 {:ok, private_announce, _meta} = ActivityPub.persist(private_announce_data, local: true)
487 {:ok, relay_announce, _meta} = ActivityPub.persist(relay_announce_data, local: true)
493 private_announce: private_announce,
494 relay_announce: relay_announce
498 test "adds the announce to the original object", %{announce: announce, user: user} do
499 {:ok, announce, _} = SideEffects.handle(announce)
500 object = Object.get_by_ap_id(announce.data["object"])
501 assert object.data["announcement_count"] == 1
502 assert user.ap_id in object.data["announcements"]
505 test "does not add the announce to the original object if the actor is a service actor", %{
506 relay_announce: announce
508 {:ok, announce, _} = SideEffects.handle(announce)
509 object = Object.get_by_ap_id(announce.data["object"])
510 assert object.data["announcement_count"] == nil
513 test "creates a notification", %{announce: announce, poster: poster} do
514 {:ok, announce, _} = SideEffects.handle(announce)
515 assert Repo.get_by(Notification, user_id: poster.id, activity_id: announce.id)
518 test "it streams out the announce", %{announce: announce} do
521 Pleroma.Web.Streamer,
524 stream: fn _, _ -> nil end
531 send: fn _ -> nil end
535 {:ok, announce, _} = SideEffects.handle(announce)
538 Pleroma.Web.Streamer.stream(["user", "list", "public", "public:local"], announce)
541 assert called(Pleroma.Web.Push.send(:_))