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