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