436f9bf92b307d2db9e3f6637939b4083a4bed7e
[akkoma] / lib / pleroma / web / twitter_api / representers / activity_representer.ex
1 # THIS MODULE IS DEPRECATED! DON'T USE IT!
2 # USE THE Pleroma.Web.TwitterAPI.Views.ActivityView MODULE!
3 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
4 use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
5 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
6 alias Pleroma.{Activity, User, Object}
7 alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView}
8 alias Pleroma.Web.CommonAPI.Utils
9 alias Pleroma.Formatter
10 alias Pleroma.HTML
11
12 defp user_by_ap_id(user_list, ap_id) do
13 Enum.find(user_list, fn %{ap_id: user_id} -> ap_id == user_id end)
14 end
15
16 def to_map(
17 %Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} =
18 activity,
19 %{users: users, announced_activity: announced_activity} = opts
20 ) do
21 user = user_by_ap_id(users, actor)
22 created_at = created_at |> Utils.date_to_asctime()
23
24 text = "#{user.nickname} retweeted a status."
25
26 announced_user = user_by_ap_id(users, announced_activity.data["actor"])
27 retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
28
29 %{
30 "id" => activity.id,
31 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
32 "statusnet_html" => text,
33 "text" => text,
34 "is_local" => activity.local,
35 "is_post_verb" => false,
36 "uri" => "tag:#{activity.data["id"]}:objectType=note",
37 "created_at" => created_at,
38 "retweeted_status" => retweeted_status,
39 "statusnet_conversation_id" => conversation_id(announced_activity),
40 "external_url" => activity.data["id"],
41 "activity_type" => "repeat"
42 }
43 end
44
45 def to_map(
46 %Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
47 %{user: user, liked_activity: liked_activity} = opts
48 ) do
49 created_at = created_at |> Utils.date_to_asctime()
50
51 text = "#{user.nickname} favorited a status."
52
53 %{
54 "id" => activity.id,
55 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
56 "statusnet_html" => text,
57 "text" => text,
58 "is_local" => activity.local,
59 "is_post_verb" => false,
60 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
61 "created_at" => created_at,
62 "in_reply_to_status_id" => liked_activity.id,
63 "external_url" => activity.data["id"],
64 "activity_type" => "like"
65 }
66 end
67
68 def to_map(
69 %Activity{data: %{"type" => "Follow", "object" => followed_id}} = activity,
70 %{user: user} = opts
71 ) do
72 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
73 created_at = created_at |> Utils.date_to_asctime()
74
75 followed = User.get_cached_by_ap_id(followed_id)
76 text = "#{user.nickname} started following #{followed.nickname}"
77
78 %{
79 "id" => activity.id,
80 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
81 "attentions" => [],
82 "statusnet_html" => text,
83 "text" => text,
84 "is_local" => activity.local,
85 "is_post_verb" => false,
86 "created_at" => created_at,
87 "in_reply_to_status_id" => nil,
88 "external_url" => activity.data["id"],
89 "activity_type" => "follow"
90 }
91 end
92
93 # TODO:
94 # Make this more proper. Just a placeholder to not break the frontend.
95 def to_map(
96 %Activity{
97 data: %{"type" => "Undo", "published" => created_at, "object" => undid_activity}
98 } = activity,
99 %{user: user} = opts
100 ) do
101 created_at = created_at |> Utils.date_to_asctime()
102
103 text = "#{user.nickname} undid the action at #{undid_activity["id"]}"
104
105 %{
106 "id" => activity.id,
107 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
108 "attentions" => [],
109 "statusnet_html" => text,
110 "text" => text,
111 "is_local" => activity.local,
112 "is_post_verb" => false,
113 "created_at" => created_at,
114 "in_reply_to_status_id" => nil,
115 "external_url" => activity.data["id"],
116 "activity_type" => "undo"
117 }
118 end
119
120 def to_map(
121 %Activity{data: %{"type" => "Delete", "published" => created_at, "object" => _}} =
122 activity,
123 %{user: user} = opts
124 ) do
125 created_at = created_at |> Utils.date_to_asctime()
126
127 %{
128 "id" => activity.id,
129 "uri" => activity.data["object"],
130 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
131 "attentions" => [],
132 "statusnet_html" => "deleted notice {{tag",
133 "text" => "deleted notice {{tag",
134 "is_local" => activity.local,
135 "is_post_verb" => false,
136 "created_at" => created_at,
137 "in_reply_to_status_id" => nil,
138 "external_url" => activity.data["id"],
139 "activity_type" => "delete"
140 }
141 end
142
143 def to_map(
144 %Activity{data: %{"object" => %{"content" => content} = object}} = activity,
145 %{user: user} = opts
146 ) do
147 object = Object.normalize(object)
148
149 created_at = object.data["published"] |> Utils.date_to_asctime()
150 like_count = object.data["like_count"] || 0
151 announcement_count = object.data["announcement_count"] || 0
152 favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || [])
153 repeated = opts[:for] && opts[:for].ap_id in (object.data["announcements"] || [])
154
155 mentions = opts[:mentioned] || []
156
157 attentions =
158 activity.recipients
159 |> Enum.map(fn ap_id -> Enum.find(mentions, fn user -> ap_id == user.ap_id end) end)
160 |> Enum.filter(& &1)
161 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
162
163 conversation_id = conversation_id(activity)
164
165 tags = object.data["tag"] || []
166 possibly_sensitive = object.data["sensitive"] || Enum.member?(tags, "nsfw")
167
168 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
169
170 {summary, content} = ActivityView.render_content(object.data)
171
172 html =
173 HTML.filter_tags(content, User.html_filter_policy(opts[:for]))
174 |> Formatter.emojify(object.data["emoji"])
175
176 video =
177 if object.data["type"] == "Video" do
178 vid = [object.data]
179 else
180 []
181 end
182
183 attachments = (object.data["attachment"] || []) ++ video
184
185 reply_parent = Activity.get_in_reply_to_activity(activity)
186
187 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
188
189 %{
190 "id" => activity.id,
191 "uri" => object.data["id"],
192 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
193 "statusnet_html" => html,
194 "text" => HTML.strip_tags(content),
195 "is_local" => activity.local,
196 "is_post_verb" => true,
197 "created_at" => created_at,
198 "in_reply_to_status_id" => object.data["inReplyToStatusId"],
199 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
200 "in_reply_to_profileurl" => User.profile_url(reply_user),
201 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
202 "in_reply_to_user_id" => reply_user && reply_user.id,
203 "statusnet_conversation_id" => conversation_id,
204 "attachments" => attachments |> ObjectRepresenter.enum_to_list(opts),
205 "attentions" => attentions,
206 "fave_num" => like_count,
207 "repeat_num" => announcement_count,
208 "favorited" => to_boolean(favorited),
209 "repeated" => to_boolean(repeated),
210 "external_url" => object.data["external_url"] || object.data["id"],
211 "tags" => tags,
212 "activity_type" => "post",
213 "possibly_sensitive" => possibly_sensitive,
214 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object.data),
215 "summary" => object.data["summary"]
216 }
217 end
218
219 def conversation_id(activity) do
220 with context when not is_nil(context) <- activity.data["context"] do
221 TwitterAPI.context_to_conversation_id(context)
222 else
223 _e -> nil
224 end
225 end
226
227 defp to_boolean(false) do
228 false
229 end
230
231 defp to_boolean(nil) do
232 false
233 end
234
235 defp to_boolean(_) do
236 true
237 end
238 end