Merge branch 'develop' into dtluna/pleroma-feature/unfollow-activity
[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.{UserRepresenter, ActivityRepresenter, ObjectRepresenter}
5 alias Pleroma.Web.ActivityPub.ActivityPub
6 alias Pleroma.Builders.UserBuilder
7 import Pleroma.Factory
8
9 test "an announce activity" do
10 user = insert(:user)
11 note_activity = insert(:note_activity)
12 activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
13 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
14
15 {:ok, announce_activity, _object} = ActivityPub.announce(user, object)
16 note_activity = Activity.get_by_ap_id(note_activity.data["id"])
17
18 status = ActivityRepresenter.to_map(announce_activity, %{users: [user, activity_actor], announced_activity: note_activity, for: user})
19
20 assert status["id"] == announce_activity.id
21 assert status["user"] == UserRepresenter.to_map(user, %{for: user})
22
23 retweeted_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
24 assert retweeted_status["repeated"] == true
25 assert retweeted_status["id"] == note_activity.id
26 assert status["statusnet_conversation_id"] == retweeted_status["statusnet_conversation_id"]
27
28 assert status["retweeted_status"] == retweeted_status
29 end
30
31 test "a like activity" do
32 user = insert(:user)
33 note_activity = insert(:note_activity)
34 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
35
36 {:ok, like_activity, _object} = ActivityPub.like(user, object)
37 status = ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity})
38
39 assert status["id"] == like_activity.id
40 assert status["in_reply_to_status_id"] == note_activity.id
41
42 note_activity = Activity.get_by_ap_id(note_activity.data["id"])
43 activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
44 liked_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
45 assert liked_status["favorited"] == true
46 end
47
48 test "an activity" do
49 {:ok, user} = UserBuilder.insert
50 # {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
51 mentioned_user = insert(:user, %{nickname: "shp"})
52
53 # {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
54 follower = insert(:user, %{following: [User.ap_followers(user)]})
55
56 object = %Object{
57 data: %{
58 "type" => "Image",
59 "url" => [
60 %{
61 "type" => "Link",
62 "mediaType" => "image/jpg",
63 "href" => "http://example.org/image.jpg"
64 }
65 ],
66 "uuid" => 1
67 }
68 }
69
70 content_html = "Some content mentioning <a href='#{mentioned_user.ap_id}'>@shp</shp>"
71 content = HtmlSanitizeEx.strip_tags(content_html)
72 date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601
73
74 {:ok, convo_object} = Object.context_mapping("2hu") |> Repo.insert
75
76 activity = %Activity{
77 id: 1,
78 data: %{
79 "type" => "Create",
80 "to" => [
81 User.ap_followers(user),
82 "https://www.w3.org/ns/activitystreams#Public",
83 mentioned_user.ap_id
84 ],
85 "actor" => User.ap_id(user),
86 "object" => %{
87 "published" => date,
88 "type" => "Note",
89 "content" => content_html,
90 "inReplyToStatusId" => 213123,
91 "attachment" => [
92 object
93 ],
94 "like_count" => 5,
95 "announcement_count" => 3,
96 "context" => "2hu"
97 },
98 "published" => date,
99 "context" => "2hu"
100 }
101 }
102
103
104 expected_status = %{
105 "id" => activity.id,
106 "user" => UserRepresenter.to_map(user, %{for: follower}),
107 "is_local" => true,
108 "attentions" => [],
109 "statusnet_html" => content_html,
110 "text" => content,
111 "is_post_verb" => true,
112 "created_at" => "Tue May 24 13:26:08 +0000 2016",
113 "in_reply_to_status_id" => 213123,
114 "statusnet_conversation_id" => convo_object.id,
115 "attachments" => [
116 ObjectRepresenter.to_map(object)
117 ],
118 "attentions" => [
119 UserRepresenter.to_map(mentioned_user, %{for: follower})
120 ],
121 "fave_num" => 5,
122 "repeat_num" => 3,
123 "favorited" => false,
124 "repeated" => false
125 }
126
127 assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status
128 end
129 end