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