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
9 alias Pleroma.Notification
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Builder
14 alias Pleroma.Web.ActivityPub.SideEffects
15 alias Pleroma.Web.CommonAPI
17 import Pleroma.Factory
19 describe "like objects" do
21 poster = insert(:user)
23 {:ok, post} = CommonAPI.post(poster, %{"status" => "hey"})
25 {:ok, like_data, _meta} = Builder.like(user, post.object)
26 {:ok, like, _meta} = ActivityPub.persist(like_data, local: true)
28 %{like: like, user: user, poster: poster}
31 test "add the like to the original object", %{like: like, user: user} do
32 {:ok, like, _} = SideEffects.handle(like)
33 object = Object.get_by_ap_id(like.data["object"])
34 assert object.data["like_count"] == 1
35 assert user.ap_id in object.data["likes"]
38 test "creates a notification", %{like: like, poster: poster} do
39 {:ok, like, _} = SideEffects.handle(like)
40 assert Repo.get_by(Notification, user_id: poster.id, activity_id: like.id)
44 describe "creation of ChatMessages" do
45 test "notifies the recipient" do
46 author = insert(:user, local: false)
47 recipient = insert(:user, local: true)
49 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
51 {:ok, create_activity_data, _meta} =
52 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
54 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
56 {:ok, _create_activity, _meta} =
57 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
59 assert Repo.get_by(Notification, user_id: recipient.id, activity_id: create_activity.id)
62 test "it creates a Chat for the local users and bumps the unread count" do
63 author = insert(:user, local: false)
64 recipient = insert(:user, local: true)
66 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
68 {:ok, create_activity_data, _meta} =
69 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
71 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
73 {:ok, _create_activity, _meta} =
74 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
76 # An object is created
77 assert Object.get_by_ap_id(chat_message_data["id"])
79 # The remote user won't get a chat
80 chat = Chat.get(author.id, recipient.ap_id)
83 # The local user will get a chat
84 chat = Chat.get(recipient.id, author.ap_id)
87 author = insert(:user, local: true)
88 recipient = insert(:user, local: true)
90 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
92 {:ok, create_activity_data, _meta} =
93 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
95 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
97 {:ok, _create_activity, _meta} =
98 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
100 # Both users are local and get the chat
101 chat = Chat.get(author.id, recipient.ap_id)
104 chat = Chat.get(recipient.id, author.ap_id)