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