eca69cdfe655fec59624eab14df058f02241ccd9
[akkoma] / test / web / twitter_api / views / activity_view_test.exs
1 defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Web.CommonAPI
5 alias Pleroma.Web.CommonAPI.Utils
6 alias Pleroma.Web.TwitterAPI.ActivityView
7 alias Pleroma.Web.TwitterAPI.UserView
8 alias Pleroma.Web.TwitterAPI.TwitterAPI
9 alias Pleroma.Repo
10 alias Pleroma.Activity
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 import Pleroma.Factory
14
15 test "a create activity with a note" do
16 user = insert(:user)
17 other_user = insert(:user, %{nickname: "shp"})
18
19 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
20
21 result = ActivityView.render("activity.json", activity: activity)
22
23 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
24
25 expected = %{
26 "activity_type" => "post",
27 "attachments" => [],
28 "attentions" => [
29 UserView.render("show.json", %{user: other_user})
30 ],
31 "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
32 "external_url" => activity.data["object"]["id"],
33 "fave_num" => 0,
34 "favorited" => false,
35 "id" => activity.id,
36 "in_reply_to_status_id" => nil,
37 "is_local" => true,
38 "is_post_verb" => true,
39 "possibly_sensitive" => false,
40 "repeat_num" => 0,
41 "repeated" => false,
42 "statusnet_conversation_id" => convo_id,
43 "statusnet_html" =>
44 "Hey <span><a href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
45 "tags" => [],
46 "text" => "Hey @shp!",
47 "uri" => activity.data["object"]["id"],
48 "user" => UserView.render("show.json", %{user: user})
49 }
50
51 assert result == expected
52 end
53
54 test "an activity that is a reply" do
55 user = insert(:user)
56 other_user = insert(:user, %{nickname: "shp"})
57
58 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
59
60 {:ok, answer} =
61 CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
62
63 result = ActivityView.render("activity.json", %{activity: answer})
64
65 assert result["in_reply_to_status_id"] == activity.id
66 end
67
68 test "a like activity" do
69 user = insert(:user)
70 other_user = insert(:user, %{nickname: "shp"})
71
72 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
73 {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
74
75 result = ActivityView.render("activity.json", activity: like)
76
77 expected = %{
78 "activity_type" => "like",
79 "created_at" => like.data["published"] |> Utils.date_to_asctime(),
80 "external_url" => like.data["id"],
81 "id" => like.id,
82 "in_reply_to_status_id" => activity.id,
83 "is_local" => true,
84 "is_post_verb" => false,
85 "statusnet_html" => "shp favorited a status.",
86 "text" => "shp favorited a status.",
87 "uri" => "tag:#{like.data["id"]}:objectType=Favourite",
88 "user" => UserView.render("show.json", user: other_user)
89 }
90
91 assert result == expected
92 end
93
94 test "an announce activity" do
95 user = insert(:user)
96 other_user = insert(:user, %{nickname: "shp"})
97
98 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
99 {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user)
100
101 convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"])
102
103 activity = Repo.get(Activity, activity.id)
104
105 result = ActivityView.render("activity.json", activity: announce)
106
107 expected = %{
108 "activity_type" => "repeat",
109 "created_at" => announce.data["published"] |> Utils.date_to_asctime(),
110 "external_url" => announce.data["id"],
111 "id" => announce.id,
112 "is_local" => true,
113 "is_post_verb" => false,
114 "statusnet_html" => "shp retweeted a status.",
115 "text" => "shp retweeted a status.",
116 "uri" => "tag:#{announce.data["id"]}:objectType=note",
117 "user" => UserView.render("show.json", user: other_user),
118 "retweeted_status" => ActivityView.render("activity.json", activity: activity),
119 "statusnet_conversation_id" => convo_id
120 }
121
122 assert result == expected
123 end
124
125 test "A follow activity" do
126 user = insert(:user)
127 other_user = insert(:user, %{nickname: "shp"})
128
129 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
130 {:ok, follower} = User.follow(user, other_user)
131 {:ok, follow} = ActivityPub.follow(follower, other_user)
132
133 result = ActivityView.render("activity.json", activity: follow)
134
135 expected = %{
136 "activity_type" => "follow",
137 "attentions" => [],
138 "created_at" => follow.data["published"] |> Utils.date_to_asctime(),
139 "external_url" => follow.data["id"],
140 "id" => follow.id,
141 "in_reply_to_status_id" => nil,
142 "is_local" => true,
143 "is_post_verb" => false,
144 "statusnet_html" => "#{user.nickname} started following shp",
145 "text" => "#{user.nickname} started following shp",
146 "user" => UserView.render("show.json", user: user)
147 }
148
149 assert result == expected
150 end
151 end