Address incoming messages to followers.
[akkoma] / test / web / ostatus / activity_representer_test.exs
1 defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Web.OStatus.ActivityRepresenter
5 alias Pleroma.{User, Activity, Object}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7
8 import Pleroma.Factory
9
10 test "a note activity" do
11 note_activity = insert(:note_activity)
12 updated_at = note_activity.updated_at
13 |> NaiveDateTime.to_iso8601
14 inserted_at = note_activity.inserted_at
15 |> NaiveDateTime.to_iso8601
16
17 user = User.get_cached_by_ap_id(note_activity.data["actor"])
18
19 expected = """
20 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
21 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
22 <id>#{note_activity.data["object"]["id"]}</id>
23 <title>New note by #{user.nickname}</title>
24 <content type="html">#{note_activity.data["object"]["content"]}</content>
25 <published>#{inserted_at}</published>
26 <updated>#{updated_at}</updated>
27 <ostatus:conversation>#{note_activity.data["context"]}</ostatus:conversation>
28 <link href="#{note_activity.data["context"]}" rel="ostatus:conversation" />
29 <link type="application/atom+xml" href="#{note_activity.data["object"]["id"]}" rel="self" />
30 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
31 """
32
33 tuple = ActivityRepresenter.to_simple_form(note_activity, user)
34
35 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
36
37 assert clean(res) == clean(expected)
38 end
39
40 test "a reply note" do
41 note = insert(:note_activity)
42 answer = insert(:note_activity)
43 object = answer.data["object"]
44 object = Map.put(object, "inReplyTo", note.data["object"]["id"])
45
46 data = %{answer.data | "object" => object}
47 answer = %{answer | data: data}
48
49 updated_at = answer.updated_at
50 |> NaiveDateTime.to_iso8601
51 inserted_at = answer.inserted_at
52 |> NaiveDateTime.to_iso8601
53
54 user = User.get_cached_by_ap_id(answer.data["actor"])
55
56 expected = """
57 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
58 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
59 <id>#{answer.data["object"]["id"]}</id>
60 <title>New note by #{user.nickname}</title>
61 <content type="html">#{answer.data["object"]["content"]}</content>
62 <published>#{inserted_at}</published>
63 <updated>#{updated_at}</updated>
64 <ostatus:conversation>#{answer.data["context"]}</ostatus:conversation>
65 <link href="#{answer.data["context"]}" rel="ostatus:conversation" />
66 <link type="application/atom+xml" href="#{answer.data["object"]["id"]}" rel="self" />
67 <thr:in-reply-to ref="#{note.data["object"]["id"]}" />
68 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
69 """
70
71 tuple = ActivityRepresenter.to_simple_form(answer, user)
72
73 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
74
75 assert clean(res) == clean(expected)
76 end
77
78 test "an announce activity" do
79 note = insert(:note_activity)
80 user = insert(:user)
81 object = Object.get_cached_by_ap_id(note.data["object"]["id"])
82
83 {:ok, announce, object} = ActivityPub.announce(user, object)
84
85 announce = Repo.get(Activity, announce.id)
86
87 note_user = User.get_cached_by_ap_id(note.data["actor"])
88 note = Repo.get(Activity, note.id)
89 note_xml = ActivityRepresenter.to_simple_form(note, note_user, true)
90 |> :xmerl.export_simple_content(:xmerl_xml)
91 |> to_string
92
93 updated_at = announce.updated_at
94 |> NaiveDateTime.to_iso8601
95 inserted_at = announce.inserted_at
96 |> NaiveDateTime.to_iso8601
97
98 expected = """
99 <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
100 <activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
101 <id>#{announce.data["id"]}</id>
102 <title>#{user.nickname} repeated a notice</title>
103 <content type="html">RT #{note.data["object"]["content"]}</content>
104 <published>#{inserted_at}</published>
105 <updated>#{updated_at}</updated>
106 <ostatus:conversation>#{announce.data["context"]}</ostatus:conversation>
107 <link href="#{announce.data["context"]}" rel="ostatus:conversation" />
108 <link rel="self" type="application/atom+xml" href="#{announce.data["id"]}"/>
109 <activity:object>
110 #{note_xml}
111 </activity:object>
112 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{note.data["actor"]}"/>
113 """
114
115 announce_xml = ActivityRepresenter.to_simple_form(announce, user)
116 |> :xmerl.export_simple_content(:xmerl_xml)
117 |> to_string
118
119 assert clean(expected) == clean(announce_xml)
120 end
121
122 test "a like activity" do
123 note = insert(:note)
124 user = insert(:user)
125 {:ok, like, _note} = ActivityPub.like(user, note)
126
127 updated_at = like.updated_at
128 |> NaiveDateTime.to_iso8601
129 inserted_at = like.inserted_at
130 |> NaiveDateTime.to_iso8601
131
132 tuple = ActivityRepresenter.to_simple_form(like, user)
133 refute is_nil(tuple)
134
135 res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
136
137 expected = """
138 <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
139 <id>#{like.data["id"]}</id>
140 <title>New favorite by #{user.nickname}</title>
141 <content type="html">#{user.nickname} favorited something</content>
142 <published>#{inserted_at}</published>
143 <updated>#{updated_at}</updated>
144 <activity:object>
145 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
146 <id>#{note.data["id"]}</id>
147 </activity:object>
148 <ostatus:conversation>#{like.data["context"]}</ostatus:conversation>
149 <link href="#{like.data["context"]}" rel="ostatus:conversation" />
150 <link rel="self" type="application/atom+xml" href="#{like.data["id"]}"/>
151 <thr:in-reply-to ref="#{note.data["id"]}" />
152 <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{note.data["actor"]}"/>
153 """
154
155 assert clean(res) == clean(expected)
156 end
157
158 test "an unknown activity" do
159 tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
160 assert is_nil(tuple)
161 end
162
163 defp clean(string) do
164 String.replace(string, ~r/\s/, "")
165 end
166 end