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" ->
98 User.get_cached_by_ap_id(ap_id)
102 def render("index.json", opts) do
103 context_ids = collect_context_ids(opts.activities)
104 users = collect_users(opts.activities)
108 |> Map.put(:context_ids, context_ids)
109 |> Map.put(:users, users)
119 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
120 user = get_user(activity.data["actor"], opts)
121 created_at = activity.data["published"] |> Utils.date_to_asctime()
125 "uri" => activity.data["object"],
126 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
128 "statusnet_html" => "deleted notice {{tag",
129 "text" => "deleted notice {{tag",
130 "is_local" => activity.local,
131 "is_post_verb" => false,
132 "created_at" => created_at,
133 "in_reply_to_status_id" => nil,
134 "external_url" => activity.data["id"],
135 "activity_type" => "delete"
139 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
140 user = get_user(activity.data["actor"], opts)
141 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
142 created_at = created_at |> Utils.date_to_asctime()
144 followed = get_user(activity.data["object"], opts)
145 text = "#{user.nickname} started following #{followed.nickname}"
149 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
151 "statusnet_html" => text,
153 "is_local" => activity.local,
154 "is_post_verb" => false,
155 "created_at" => created_at,
156 "in_reply_to_status_id" => nil,
157 "external_url" => activity.data["id"],
158 "activity_type" => "follow"
162 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
163 user = get_user(activity.data["actor"], opts)
164 created_at = activity.data["published"] |> Utils.date_to_asctime()
165 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
167 text = "#{user.nickname} retweeted a status."
169 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
173 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
174 "statusnet_html" => text,
176 "is_local" => activity.local,
177 "is_post_verb" => false,
178 "uri" => "tag:#{activity.data["id"]}:objectType=note",
179 "created_at" => created_at,
180 "retweeted_status" => retweeted_status,
181 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
182 "external_url" => activity.data["id"],
183 "activity_type" => "repeat"
187 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
188 user = get_user(activity.data["actor"], opts)
189 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
190 liked_activity_id = if liked_activity, do: liked_activity.id, else: nil
193 activity.data["published"]
194 |> Utils.date_to_asctime()
196 text = "#{user.nickname} favorited a status."
200 do: render("activity.json", Map.merge(opts, %{activity: liked_activity})),
205 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
206 "statusnet_html" => text,
208 "is_local" => activity.local,
209 "is_post_verb" => false,
210 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
211 "created_at" => created_at,
212 "favorited_status" => favorited_status,
213 "in_reply_to_status_id" => liked_activity_id,
214 "external_url" => activity.data["id"],
215 "activity_type" => "like"
221 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
223 user = get_user(activity.data["actor"], opts)
225 created_at = object["published"] |> Utils.date_to_asctime()
226 like_count = object["like_count"] || 0
227 announcement_count = object["announcement_count"] || 0
228 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
229 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
233 |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
235 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
237 conversation_id = get_context_id(activity, opts)
239 tags = activity.data["object"]["tag"] || []
240 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
242 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
244 {summary, content} = render_content(object)
248 |> HTML.get_cached_scrubbed_html_for_object(
249 User.html_filter_policy(opts[:for]),
253 |> Formatter.emojify(object["emoji"])
258 |> String.replace(~r/<br\s?\/?>/, "\n")
259 |> HTML.get_cached_stripped_html_for_object(activity, __MODULE__)
262 reply_parent = Activity.get_in_reply_to_activity(activity)
264 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
268 "uri" => activity.data["object"]["id"],
269 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
270 "statusnet_html" => html,
272 "is_local" => activity.local,
273 "is_post_verb" => true,
274 "created_at" => created_at,
275 "in_reply_to_status_id" => object["inReplyToStatusId"],
276 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
277 "in_reply_to_profileurl" => User.profile_url(reply_user),
278 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
279 "in_reply_to_user_id" => reply_user && reply_user.id,
280 "statusnet_conversation_id" => conversation_id,
281 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
282 "attentions" => attentions,
283 "fave_num" => like_count,
284 "repeat_num" => announcement_count,
285 "favorited" => !!favorited,
286 "repeated" => !!repeated,
287 "external_url" => object["external_url"] || object["id"],
289 "activity_type" => "post",
290 "possibly_sensitive" => possibly_sensitive,
291 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
296 def render("activity.json", %{activity: unhandled_activity}) do
297 Logger.warn("#{__MODULE__} unhandled activity: #{inspect(unhandled_activity)}")
301 def render_content(%{"type" => "Note"} = object) do
302 summary = object["summary"]
305 if !!summary and summary != "" do
306 "<p>#{summary}</p>#{object["content"]}"
314 def render_content(%{"type" => object_type} = object)
315 when object_type in ["Article", "Page", "Video"] do
316 summary = object["name"] || object["summary"]
319 if !!summary and summary != "" and is_bitstring(object["url"]) do
320 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
328 def render_content(object) do
329 summary = object["summary"] || "Unhandled activity type: #{object["type"]}"
330 content = "<p>#{summary}</p>#{object["content"]}"