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 {:ok, chat_message_object} = Object.create(chat_message_data)
51
52 {:ok, create_activity_data, _meta} =
53 Builder.create(author, chat_message_object.data["id"], [recipient.ap_id])
54
55 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
56
57 {:ok, _create_activity, _meta} = SideEffects.handle(create_activity)
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 {:ok, chat_message_object} = Object.create(chat_message_data)
68
69 {:ok, create_activity_data, _meta} =
70 Builder.create(author, chat_message_object.data["id"], [recipient.ap_id])
71
72 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
73
74 {:ok, _create_activity, _meta} = SideEffects.handle(create_activity)
75
76 # The remote user won't get a chat
77 chat = Chat.get(author.id, recipient.ap_id)
78 refute chat
79
80 # The local user will get a chat
81 chat = Chat.get(recipient.id, author.ap_id)
82 assert chat
83
84 author = insert(:user, local: true)
85 recipient = insert(:user, local: true)
86
87 {:ok, chat_message_data, _meta} = Builder.chat_message(author, recipient.ap_id, "hey")
88 {:ok, chat_message_object} = Object.create(chat_message_data)
89
90 {:ok, create_activity_data, _meta} =
91 Builder.create(author, chat_message_object.data["id"], [recipient.ap_id])
92
93 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
94
95 {:ok, _create_activity, _meta} = SideEffects.handle(create_activity)
96
97 # Both users are local and get the chat
98 chat = Chat.get(author.id, recipient.ap_id)
99 assert chat
100
101 chat = Chat.get(recipient.id, author.ap_id)
102 assert chat
103 end
104 end
105 end