1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.ActivityView do
7 alias Pleroma.Web.CommonAPI.Utils
9 alias Pleroma.Web.TwitterAPI.UserView
10 alias Pleroma.Web.TwitterAPI.ActivityView
11 alias Pleroma.Web.TwitterAPI.TwitterAPI
12 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
13 alias Pleroma.Activity
18 alias Pleroma.Formatter
23 defp query_context_ids([]), do: []
25 defp query_context_ids(contexts) do
26 query = from(o in Object, where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts))
31 defp query_users([]), do: []
33 defp query_users(user_ids) do
34 query = from(user in User, where: user.ap_id in ^user_ids)
39 defp collect_context_ids(activities) do
42 |> Enum.reject(& &1.data["context_id"])
43 |> Enum.map(fn %{data: data} ->
47 |> query_context_ids()
48 |> Enum.reduce(%{}, fn %{data: %{"id" => ap_id}, id: id}, acc ->
49 Map.put(acc, ap_id, id)
53 defp collect_users(activities) do
55 |> Enum.map(fn activity ->
57 data = %{"type" => "Follow"} ->
58 [data["actor"], data["object"]]
62 end ++ activity.recipients
67 |> Enum.reduce(%{}, fn user, acc ->
68 Map.put(acc, user.ap_id, user)
72 defp get_context_id(%{data: %{"context_id" => context_id}}, _) when not is_nil(context_id),
75 defp get_context_id(%{data: %{"context" => nil}}, _), do: nil
77 defp get_context_id(%{data: %{"context" => context}}, options) do
79 id = options[:context_ids][context] -> id
80 true -> TwitterAPI.context_to_conversation_id(context)
84 defp get_context_id(_, _), do: nil
86 defp get_user(ap_id, opts) do
88 user = opts[:users][ap_id] ->
91 String.ends_with?(ap_id, "/followers") ->
94 ap_id == "https://www.w3.org/ns/activitystreams#Public" ->
97 user = User.get_cached_by_ap_id(ap_id) ->
100 user = User.get_by_guessed_nickname(ap_id) ->
108 defp error_user(ap_id) do
113 nickname: "erroruser@example.com",
114 inserted_at: NaiveDateTime.utc_now()
118 def render("index.json", opts) do
119 context_ids = collect_context_ids(opts.activities)
120 users = collect_users(opts.activities)
124 |> Map.put(:context_ids, context_ids)
125 |> Map.put(:users, users)
135 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
136 user = get_user(activity.data["actor"], opts)
137 created_at = activity.data["published"] |> Utils.date_to_asctime()
141 "uri" => activity.data["object"],
142 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
144 "statusnet_html" => "deleted notice {{tag",
145 "text" => "deleted notice {{tag",
146 "is_local" => activity.local,
147 "is_post_verb" => false,
148 "created_at" => created_at,
149 "in_reply_to_status_id" => nil,
150 "external_url" => activity.data["id"],
151 "activity_type" => "delete"
155 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
156 user = get_user(activity.data["actor"], opts)
157 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
158 created_at = created_at |> Utils.date_to_asctime()
160 followed = get_user(activity.data["object"], opts)
161 text = "#{user.nickname} started following #{followed.nickname}"
165 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
167 "statusnet_html" => text,
169 "is_local" => activity.local,
170 "is_post_verb" => false,
171 "created_at" => created_at,
172 "in_reply_to_status_id" => nil,
173 "external_url" => activity.data["id"],
174 "activity_type" => "follow"
178 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
179 user = get_user(activity.data["actor"], opts)
180 created_at = activity.data["published"] |> Utils.date_to_asctime()
181 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
183 text = "#{user.nickname} retweeted a status."
185 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
189 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
190 "statusnet_html" => text,
192 "is_local" => activity.local,
193 "is_post_verb" => false,
194 "uri" => "tag:#{activity.data["id"]}:objectType=note",
195 "created_at" => created_at,
196 "retweeted_status" => retweeted_status,
197 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
198 "external_url" => activity.data["id"],
199 "activity_type" => "repeat"
203 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
204 user = get_user(activity.data["actor"], opts)
205 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
206 liked_activity_id = if liked_activity, do: liked_activity.id, else: nil
209 activity.data["published"]
210 |> Utils.date_to_asctime()
212 text = "#{user.nickname} favorited a status."
216 do: render("activity.json", Map.merge(opts, %{activity: liked_activity})),
221 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
222 "statusnet_html" => text,
224 "is_local" => activity.local,
225 "is_post_verb" => false,
226 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
227 "created_at" => created_at,
228 "favorited_status" => favorited_status,
229 "in_reply_to_status_id" => liked_activity_id,
230 "external_url" => activity.data["id"],
231 "activity_type" => "like"
237 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
239 user = get_user(activity.data["actor"], opts)
241 created_at = object["published"] |> Utils.date_to_asctime()
242 like_count = object["like_count"] || 0
243 announcement_count = object["announcement_count"] || 0
244 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
245 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
246 pinned = activity.id in user.info.pinned_activities
250 |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
252 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
254 conversation_id = get_context_id(activity, opts)
256 tags = activity.data["object"]["tag"] || []
257 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
259 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
261 {summary, content} = render_content(object)
265 |> HTML.get_cached_scrubbed_html_for_object(
266 User.html_filter_policy(opts[:for]),
270 |> Formatter.emojify(object["emoji"])
275 |> String.replace(~r/<br\s?\/?>/, "\n")
276 |> HTML.get_cached_stripped_html_for_object(activity, __MODULE__)
279 reply_parent = Activity.get_in_reply_to_activity(activity)
281 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
283 summary = HTML.strip_tags(summary)
287 "uri" => activity.data["object"]["id"],
288 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
289 "statusnet_html" => html,
291 "is_local" => activity.local,
292 "is_post_verb" => true,
293 "created_at" => created_at,
294 "in_reply_to_status_id" => object["inReplyToStatusId"],
295 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
296 "in_reply_to_profileurl" => User.profile_url(reply_user),
297 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
298 "in_reply_to_user_id" => reply_user && reply_user.id,
299 "statusnet_conversation_id" => conversation_id,
300 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
301 "attentions" => attentions,
302 "fave_num" => like_count,
303 "repeat_num" => announcement_count,
304 "favorited" => !!favorited,
305 "repeated" => !!repeated,
307 "external_url" => object["external_url"] || object["id"],
309 "activity_type" => "post",
310 "possibly_sensitive" => possibly_sensitive,
311 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
312 "summary" => summary,
313 "summary_html" => summary |> Formatter.emojify(object["emoji"])
317 def render("activity.json", %{activity: unhandled_activity}) do
318 Logger.warn("#{__MODULE__} unhandled activity: #{inspect(unhandled_activity)}")
322 def render_content(%{"type" => "Note"} = object) do
323 summary = object["summary"]
326 if !!summary and summary != "" do
327 "<p>#{summary}</p>#{object["content"]}"
335 def render_content(%{"type" => object_type} = object)
336 when object_type in ["Article", "Page", "Video"] do
337 summary = object["name"] || object["summary"]
340 if !!summary and summary != "" and is_bitstring(object["url"]) do
341 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
349 def render_content(object) do
350 summary = object["summary"] || "Unhandled activity type: #{object["type"]}"
351 content = "<p>#{summary}</p>#{object["content"]}"