Revert "Remove newlines completely so mastodon doesn't bug out."
[akkoma] / test / web / twitter_api / representers / activity_representer_test.exs
1 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
2 use Pleroma.DataCase
3 alias Pleroma.{User, Activity, Object}
4 alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, ObjectRepresenter}
5 alias Pleroma.Web.ActivityPub.ActivityPub
6 alias Pleroma.Builders.UserBuilder
7 alias Pleroma.Web.TwitterAPI.UserView
8 import Pleroma.Factory
9
10 test "an announce activity" do
11 user = insert(:user)
12 note_activity = insert(:note_activity)
13 activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
14 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
15
16 {:ok, announce_activity, _object} = ActivityPub.announce(user, object)
17 note_activity = Activity.get_by_ap_id(note_activity.data["id"])
18
19 status = ActivityRepresenter.to_map(announce_activity, %{users: [user, activity_actor], announced_activity: note_activity, for: user})
20
21 assert status["id"] == announce_activity.id
22 assert status["user"] == UserView.render("show.json", %{user: user, for: user})
23
24 retweeted_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
25 assert retweeted_status["repeated"] == true
26 assert retweeted_status["id"] == note_activity.id
27 assert status["statusnet_conversation_id"] == retweeted_status["statusnet_conversation_id"]
28
29 assert status["retweeted_status"] == retweeted_status
30 end
31
32 test "a like activity" do
33 user = insert(:user)
34 note_activity = insert(:note_activity)
35 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
36
37 {:ok, like_activity, _object} = ActivityPub.like(user, object)
38 status = ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity})
39
40 assert status["id"] == like_activity.id
41 assert status["in_reply_to_status_id"] == note_activity.id
42
43 note_activity = Activity.get_by_ap_id(note_activity.data["id"])
44 activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
45 liked_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
46 assert liked_status["favorited"] == true
47 end
48
49 test "an activity" do
50 {:ok, user} = UserBuilder.insert
51 # {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
52 mentioned_user = insert(:user, %{nickname: "shp"})
53
54 # {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
55 follower = insert(:user, %{following: [User.ap_followers(user)]})
56
57 object = %Object{
58 data: %{
59 "type" => "Image",
60 "url" => [
61 %{
62 "type" => "Link",
63 "mediaType" => "image/jpg",
64 "href" => "http://example.org/image.jpg"
65 }
66 ],
67 "uuid" => 1
68 }
69 }
70
71 content_html = "<script>alert('YAY')</script>Some content mentioning <a href='#{mentioned_user.ap_id}'>@shp</shp>"
72 content = HtmlSanitizeEx.strip_tags(content_html)
73 date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601
74
75 {:ok, convo_object} = Object.context_mapping("2hu") |> Repo.insert
76
77 activity = %Activity{
78 id: 1,
79 data: %{
80 "type" => "Create",
81 "id" => "id",
82 "to" => [
83 User.ap_followers(user),
84 "https://www.w3.org/ns/activitystreams#Public",
85 mentioned_user.ap_id
86 ],
87 "actor" => User.ap_id(user),
88 "object" => %{
89 "published" => date,
90 "type" => "Note",
91 "content" => content_html,
92 "inReplyToStatusId" => 213123,
93 "attachment" => [
94 object
95 ],
96 "external_url" => "some url",
97 "like_count" => 5,
98 "announcement_count" => 3,
99 "context" => "2hu",
100 "tag" => ["content", "mentioning", "nsfw"]
101 },
102 "published" => date,
103 "context" => "2hu"
104 },
105 local: false
106 }
107
108
109 expected_status = %{
110 "id" => activity.id,
111 "user" => UserView.render("show.json", %{user: user, for: follower}),
112 "is_local" => false,
113 "statusnet_html" => HtmlSanitizeEx.basic_html(content_html),
114 "text" => content,
115 "is_post_verb" => true,
116 "created_at" => "Tue May 24 13:26:08 +0000 2016",
117 "in_reply_to_status_id" => 213123,
118 "statusnet_conversation_id" => convo_object.id,
119 "attachments" => [
120 ObjectRepresenter.to_map(object)
121 ],
122 "attentions" => [
123 UserView.render("show.json", %{user: mentioned_user, for: follower})
124 ],
125 "fave_num" => 5,
126 "repeat_num" => 3,
127 "favorited" => false,
128 "repeated" => false,
129 "external_url" => "some url",
130 "tags" => ["content", "mentioning", "nsfw"]
131 }
132
133 assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status
134 end
135
136 test "an undo for a follow" do
137 follower = insert(:user)
138 followed = insert(:user)
139
140 {:ok, follow} = ActivityPub.follow(follower, followed)
141 {:ok, unfollow} = ActivityPub.unfollow(follower, followed)
142
143 map = ActivityRepresenter.to_map(unfollow, %{user: follower})
144 assert map["is_post_verb"] == false
145 end
146 end