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
8 alias Pleroma.Formatter
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.CommonAPI.Utils
15 alias Pleroma.Web.MastodonAPI.StatusView
16 alias Pleroma.Web.TwitterAPI.ActivityView
17 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
18 alias Pleroma.Web.TwitterAPI.UserView
22 require Pleroma.Constants
24 defp query_context_ids([]), do: []
26 defp query_context_ids(contexts) do
27 query = from(o in Object, where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts))
32 defp query_users([]), do: []
34 defp query_users(user_ids) do
35 query = from(user in User, where: user.ap_id in ^user_ids)
40 defp collect_context_ids(activities) do
43 |> Enum.reject(& &1.data["context_id"])
44 |> Enum.map(fn %{data: data} ->
48 |> query_context_ids()
49 |> Enum.reduce(%{}, fn %{data: %{"id" => ap_id}, id: id}, acc ->
50 Map.put(acc, ap_id, id)
54 defp collect_users(activities) do
56 |> Enum.map(fn activity ->
58 data = %{"type" => "Follow"} ->
59 [data["actor"], data["object"]]
63 end ++ activity.recipients
68 |> Enum.reduce(%{}, fn user, acc ->
69 Map.put(acc, user.ap_id, user)
73 defp get_context_id(%{data: %{"context_id" => context_id}}, _) when not is_nil(context_id),
76 defp get_context_id(%{data: %{"context" => nil}}, _), do: nil
78 defp get_context_id(%{data: %{"context" => context}}, options) do
80 id = options[:context_ids][context] -> id
81 true -> Utils.context_to_conversation_id(context)
85 defp get_context_id(_, _), do: nil
87 defp get_user(ap_id, opts) do
89 user = opts[:users][ap_id] ->
92 String.ends_with?(ap_id, "/followers") ->
95 ap_id == Pleroma.Constants.as_public() ->
98 user = User.get_cached_by_ap_id(ap_id) ->
101 user = User.get_by_guessed_nickname(ap_id) ->
105 User.error_user(ap_id)
109 def render("index.json", opts) do
110 context_ids = collect_context_ids(opts.activities)
111 users = collect_users(opts.activities)
115 |> Map.put(:context_ids, context_ids)
116 |> Map.put(:users, users)
126 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
127 user = get_user(activity.data["actor"], opts)
128 created_at = activity.data["published"] |> Utils.date_to_asctime()
132 "uri" => activity.data["object"],
133 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
135 "statusnet_html" => "deleted notice {{tag",
136 "text" => "deleted notice {{tag",
137 "is_local" => activity.local,
138 "is_post_verb" => false,
139 "created_at" => created_at,
140 "in_reply_to_status_id" => nil,
141 "external_url" => activity.data["id"],
142 "activity_type" => "delete"
146 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
147 user = get_user(activity.data["actor"], opts)
148 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
149 created_at = created_at |> Utils.date_to_asctime()
151 followed = get_user(activity.data["object"], opts)
152 text = "#{user.nickname} started following #{followed.nickname}"
156 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
158 "statusnet_html" => text,
160 "is_local" => activity.local,
161 "is_post_verb" => false,
162 "created_at" => created_at,
163 "in_reply_to_status_id" => nil,
164 "external_url" => activity.data["id"],
165 "activity_type" => "follow"
169 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
170 user = get_user(activity.data["actor"], opts)
171 created_at = activity.data["published"] |> Utils.date_to_asctime()
172 announced_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
174 text = "#{user.nickname} repeated a status."
176 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
180 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
181 "statusnet_html" => text,
183 "is_local" => activity.local,
184 "is_post_verb" => false,
185 "uri" => "tag:#{activity.data["id"]}:objectType=note",
186 "created_at" => created_at,
187 "retweeted_status" => retweeted_status,
188 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
189 "external_url" => activity.data["id"],
190 "activity_type" => "repeat"
194 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
195 user = get_user(activity.data["actor"], opts)
196 liked_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
197 liked_activity_id = if liked_activity, do: liked_activity.id, else: nil
200 activity.data["published"]
201 |> Utils.date_to_asctime()
203 text = "#{user.nickname} favorited a status."
207 do: render("activity.json", Map.merge(opts, %{activity: liked_activity})),
212 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
213 "statusnet_html" => text,
215 "is_local" => activity.local,
216 "is_post_verb" => false,
217 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
218 "created_at" => created_at,
219 "favorited_status" => favorited_status,
220 "in_reply_to_status_id" => liked_activity_id,
221 "external_url" => activity.data["id"],
222 "activity_type" => "like"
228 %{activity: %{data: %{"type" => "Create", "object" => object_id}} = activity} = opts
230 user = get_user(activity.data["actor"], opts)
232 object = Object.normalize(object_id)
234 created_at = object.data["published"] |> Utils.date_to_asctime()
235 like_count = object.data["like_count"] || 0
236 announcement_count = object.data["announcement_count"] || 0
237 favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || [])
238 repeated = opts[:for] && opts[:for].ap_id in (object.data["announcements"] || [])
239 pinned = activity.id in user.info.pinned_activities
243 |> Utils.maybe_notify_to_recipients(activity)
244 |> Utils.maybe_notify_mentioned_recipients(activity)
245 |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
247 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
249 conversation_id = get_context_id(activity, opts)
251 tags = object.data["tag"] || []
252 possibly_sensitive = object.data["sensitive"] || Enum.member?(tags, "nsfw")
254 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
256 {summary, content} = render_content(object.data)
260 |> HTML.get_cached_scrubbed_html_for_activity(
261 User.html_filter_policy(opts[:for]),
265 |> Formatter.emojify(object.data["emoji"])
270 |> String.replace(~r/<br\s?\/?>/, "\n")
271 |> HTML.get_cached_stripped_html_for_activity(activity, "twitterapi:content")
276 reply_parent = Activity.get_in_reply_to_activity(activity)
278 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
280 summary = HTML.strip_tags(summary)
285 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
289 case activity.thread_muted? do
290 thread_muted? when is_boolean(thread_muted?) -> thread_muted?
291 nil -> CommonAPI.thread_muted?(user, activity)
296 "uri" => object.data["id"],
297 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
298 "statusnet_html" => html,
300 "is_local" => activity.local,
301 "is_post_verb" => true,
302 "created_at" => created_at,
303 "in_reply_to_status_id" => reply_parent && reply_parent.id,
304 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
305 "in_reply_to_profileurl" => User.profile_url(reply_user),
306 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
307 "in_reply_to_user_id" => reply_user && reply_user.id,
308 "statusnet_conversation_id" => conversation_id,
309 "attachments" => (object.data["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
310 "attentions" => attentions,
311 "fave_num" => like_count,
312 "repeat_num" => announcement_count,
313 "favorited" => !!favorited,
314 "repeated" => !!repeated,
316 "external_url" => object.data["external_url"] || object.data["id"],
318 "activity_type" => "post",
319 "possibly_sensitive" => possibly_sensitive,
320 "visibility" => Pleroma.Web.ActivityPub.Visibility.get_visibility(object),
321 "summary" => summary,
322 "summary_html" => summary |> Formatter.emojify(object.data["emoji"]),
324 "muted" => thread_muted? || User.mutes?(opts[:for], user)
328 def render("activity.json", %{activity: unhandled_activity}) do
329 Logger.warn("#{__MODULE__} unhandled activity: #{inspect(unhandled_activity)}")
333 def render_content(%{"type" => "Note"} = object) do
334 summary = object["summary"]
337 if !!summary and summary != "" do
338 "<p>#{summary}</p>#{object["content"]}"
346 def render_content(%{"type" => object_type} = object)
347 when object_type in ["Article", "Page", "Video"] do
348 summary = object["name"] || object["summary"]
351 if !!summary and summary != "" and is_bitstring(object["url"]) do
352 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
360 def render_content(object) do
361 summary = object["summary"] || "Unhandled activity type: #{object["type"]}"
362 content = "<p>#{summary}</p>#{object["content"]}"