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