Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[akkoma] / test / web / activity_pub / side_effects_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Chat
9 alias Pleroma.Notification
10 alias Pleroma.Object
11 alias Pleroma.Repo
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Builder
14 alias Pleroma.Web.ActivityPub.SideEffects
15 alias Pleroma.Web.CommonAPI
16
17 import Pleroma.Factory
18
19 describe "like objects" do
20 setup do
21 poster = insert(:user)
22 user = insert(:user)
23 {:ok, post} = CommonAPI.post(poster, %{"status" => "hey"})
24
25 {:ok, like_data, _meta} = Builder.like(user, post.object)
26 {:ok, like, _meta} = ActivityPub.persist(like_data, local: true)
27
28 %{like: like, user: user, poster: poster}
29 end
30
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"]
36 end
37
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)
41 end
42 end
43
44 describe "creation of ChatMessages" do
45 test "notifies the recipient" do
46 author = insert(:user, local: false)
47 recipient = insert(:user, local: true)
48
49 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
50
51 {:ok, create_activity_data, _meta} =
52 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
53
54 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
55
56 {:ok, _create_activity, _meta} =
57 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
58
59 assert Repo.get_by(Notification, user_id: recipient.id, activity_id: create_activity.id)
60 end
61
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)
65
66 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
67
68 {:ok, create_activity_data, _meta} =
69 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
70
71 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
72
73 {:ok, _create_activity, _meta} =
74 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
75
76 # An object is created
77 assert Object.get_by_ap_id(chat_message_data["id"])
78
79 # The remote user won't get a chat
80 chat = Chat.get(author.id, recipient.ap_id)
81 refute chat
82
83 # The local user will get a chat
84 chat = Chat.get(recipient.id, author.ap_id)
85 assert chat
86
87 author = insert(:user, local: true)
88 recipient = insert(:user, local: true)
89
90 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
91
92 {:ok, create_activity_data, _meta} =
93 Builder.create(author, chat_message_data["id"], [recipient.ap_id])
94
95 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
96
97 {:ok, _create_activity, _meta} =
98 SideEffects.handle(create_activity, local: false, object_data: chat_message_data)
99
100 # Both users are local and get the chat
101 chat = Chat.get(author.id, recipient.ap_id)
102 assert chat
103
104 chat = Chat.get(recipient.id, author.ap_id)
105 assert chat
106 end
107 end
108 end