1 defmodule Pleroma.Web.TwitterAPI.ActivityView do
3 alias Pleroma.Web.CommonAPI.Utils
5 alias Pleroma.Web.TwitterAPI.UserView
6 alias Pleroma.Web.TwitterAPI.ActivityView
7 alias Pleroma.Web.TwitterAPI.TwitterAPI
8 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
13 alias Pleroma.Formatter
17 defp query_context_ids([]), do: []
19 defp query_context_ids(contexts) do
20 query = from(o in Object, where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts))
25 defp query_users([]), do: []
27 defp query_users(user_ids) do
28 query = from(user in User, where: user.ap_id in ^user_ids)
33 defp collect_context_ids(activities) do
36 |> Enum.reject(& &1.data["context_id"])
37 |> Enum.map(fn %{data: data} ->
41 |> query_context_ids()
42 |> Enum.reduce(%{}, fn %{data: %{"id" => ap_id}, id: id}, acc ->
43 Map.put(acc, ap_id, id)
47 defp collect_users(activities) do
49 |> Enum.map(fn activity ->
51 data = %{"type" => "Follow"} ->
52 [data["actor"], data["object"]]
56 end ++ activity.recipients
61 |> Enum.reduce(%{}, fn user, acc ->
62 Map.put(acc, user.ap_id, user)
66 defp get_context_id(%{data: %{"context_id" => context_id}}, _) when not is_nil(context_id),
69 defp get_context_id(%{data: %{"context" => nil}}, _), do: nil
71 defp get_context_id(%{data: %{"context" => context}}, options) do
73 id = options[:context_ids][context] -> id
74 true -> TwitterAPI.context_to_conversation_id(context)
78 defp get_context_id(_, _), do: nil
80 defp get_user(ap_id, opts) do
82 user = opts[:users][ap_id] ->
85 String.ends_with?(ap_id, "/followers") ->
88 ap_id == "https://www.w3.org/ns/activitystreams#Public" ->
92 User.get_cached_by_ap_id(ap_id)
96 def render("index.json", opts) do
97 context_ids = collect_context_ids(opts.activities)
98 users = collect_users(opts.activities)
102 |> Map.put(:context_ids, context_ids)
103 |> Map.put(:users, users)
113 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
114 user = get_user(activity.data["actor"], opts)
115 created_at = activity.data["published"] |> Utils.date_to_asctime()
119 "uri" => activity.data["object"],
120 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
122 "statusnet_html" => "deleted notice {{tag",
123 "text" => "deleted notice {{tag",
124 "is_local" => activity.local,
125 "is_post_verb" => false,
126 "created_at" => created_at,
127 "in_reply_to_status_id" => nil,
128 "external_url" => activity.data["id"],
129 "activity_type" => "delete"
133 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
134 user = get_user(activity.data["actor"], opts)
135 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
136 created_at = created_at |> Utils.date_to_asctime()
138 followed = get_user(activity.data["object"], opts)
139 text = "#{user.nickname} started following #{followed.nickname}"
143 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
145 "statusnet_html" => text,
147 "is_local" => activity.local,
148 "is_post_verb" => false,
149 "created_at" => created_at,
150 "in_reply_to_status_id" => nil,
151 "external_url" => activity.data["id"],
152 "activity_type" => "follow"
156 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
157 user = get_user(activity.data["actor"], opts)
158 created_at = activity.data["published"] |> Utils.date_to_asctime()
159 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
161 text = "#{user.nickname} retweeted a status."
163 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
167 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
168 "statusnet_html" => text,
170 "is_local" => activity.local,
171 "is_post_verb" => false,
172 "uri" => "tag:#{activity.data["id"]}:objectType=note",
173 "created_at" => created_at,
174 "retweeted_status" => retweeted_status,
175 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
176 "external_url" => activity.data["id"],
177 "activity_type" => "repeat"
181 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
182 user = get_user(activity.data["actor"], opts)
183 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
186 activity.data["published"]
187 |> Utils.date_to_asctime()
189 text = "#{user.nickname} favorited a status."
193 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
194 "statusnet_html" => text,
196 "is_local" => activity.local,
197 "is_post_verb" => false,
198 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
199 "created_at" => created_at,
200 "in_reply_to_status_id" => liked_activity.id,
201 "external_url" => activity.data["id"],
202 "activity_type" => "like"
208 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
210 user = get_user(activity.data["actor"], opts)
212 created_at = object["published"] |> Utils.date_to_asctime()
213 like_count = object["like_count"] || 0
214 announcement_count = object["announcement_count"] || 0
215 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
216 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
220 |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
222 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
224 conversation_id = get_context_id(activity, opts)
226 tags = activity.data["object"]["tag"] || []
227 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
229 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
231 {summary, content} = render_content(object)
234 HtmlSanitizeEx.basic_html(content)
235 |> Formatter.emojify(object["emoji"])
239 "uri" => activity.data["object"]["id"],
240 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
241 "statusnet_html" => html,
242 "text" => HtmlSanitizeEx.strip_tags(content),
243 "is_local" => activity.local,
244 "is_post_verb" => true,
245 "created_at" => created_at,
246 "in_reply_to_status_id" => object["inReplyToStatusId"],
247 "statusnet_conversation_id" => conversation_id,
248 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
249 "attentions" => attentions,
250 "fave_num" => like_count,
251 "repeat_num" => announcement_count,
252 "favorited" => !!favorited,
253 "repeated" => !!repeated,
254 "external_url" => object["external_url"] || object["id"],
256 "activity_type" => "post",
257 "possibly_sensitive" => possibly_sensitive,
258 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
263 def render_content(%{"type" => "Note"} = object) do
264 summary = object["summary"]
266 if !!summary and summary != "" do
267 "<p>#{summary}</p>#{object["content"]}"
275 def render_content(%{"type" => "Article"} = object) do
276 summary = object["name"] || object["summary"]
278 if !!summary and summary != "" do
279 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
287 def render_content(object) do
288 summary = object["summary"] || "Unhandled activity type: #{object["type"]}"
289 content = "<p>#{summary}</p>#{object["content"]}"