781ef8536ff76bf60e1eb1fdc01bd80617d78f77
[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 "like_count" => 5,
97 "announcement_count" => 3,
98 "context" => "2hu",
99 "tag" => ["content", "mentioning", "nsfw"]
100 },
101 "published" => date,
102 "context" => "2hu"
103 }
104 }
105
106
107 expected_status = %{
108 "id" => activity.id,
109 "user" => UserView.render("show.json", %{user: user, for: follower}),
110 "is_local" => true,
111 "statusnet_html" => HtmlSanitizeEx.basic_html(content_html),
112 "text" => content,
113 "is_post_verb" => true,
114 "created_at" => "Tue May 24 13:26:08 +0000 2016",
115 "in_reply_to_status_id" => 213123,
116 "statusnet_conversation_id" => convo_object.id,
117 "attachments" => [
118 ObjectRepresenter.to_map(object)
119 ],
120 "attentions" => [
121 UserView.render("show.json", %{user: mentioned_user, for: follower})
122 ],
123 "fave_num" => 5,
124 "repeat_num" => 3,
125 "favorited" => false,
126 "repeated" => false,
127 "external_url" => activity.data["id"],
128 "tags" => ["content", "mentioning", "nsfw"]
129 }
130
131 assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status
132 end
133 end