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
19 defp query_context_ids([]), do: []
21 defp query_context_ids(contexts) do
22 query = from(o in Object, where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts))
27 defp query_users([]), do: []
29 defp query_users(user_ids) do
30 query = from(user in User, where: user.ap_id in ^user_ids)
35 defp collect_context_ids(activities) do
38 |> Enum.reject(& &1.data["context_id"])
39 |> Enum.map(fn %{data: data} ->
43 |> query_context_ids()
44 |> Enum.reduce(%{}, fn %{data: %{"id" => ap_id}, id: id}, acc ->
45 Map.put(acc, ap_id, id)
49 defp collect_users(activities) do
51 |> Enum.map(fn activity ->
53 data = %{"type" => "Follow"} ->
54 [data["actor"], data["object"]]
58 end ++ activity.recipients
63 |> Enum.reduce(%{}, fn user, acc ->
64 Map.put(acc, user.ap_id, user)
68 defp get_context_id(%{data: %{"context_id" => context_id}}, _) when not is_nil(context_id),
71 defp get_context_id(%{data: %{"context" => nil}}, _), do: nil
73 defp get_context_id(%{data: %{"context" => context}}, options) do
75 id = options[:context_ids][context] -> id
76 true -> TwitterAPI.context_to_conversation_id(context)
80 defp get_context_id(_, _), do: nil
82 defp get_user(ap_id, opts) do
84 user = opts[:users][ap_id] ->
87 String.ends_with?(ap_id, "/followers") ->
90 ap_id == "https://www.w3.org/ns/activitystreams#Public" ->
94 User.get_cached_by_ap_id(ap_id)
98 def render("index.json", opts) do
99 context_ids = collect_context_ids(opts.activities)
100 users = collect_users(opts.activities)
104 |> Map.put(:context_ids, context_ids)
105 |> Map.put(:users, users)
115 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
116 user = get_user(activity.data["actor"], opts)
117 created_at = activity.data["published"] |> Utils.date_to_asctime()
121 "uri" => activity.data["object"],
122 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
124 "statusnet_html" => "deleted notice {{tag",
125 "text" => "deleted notice {{tag",
126 "is_local" => activity.local,
127 "is_post_verb" => false,
128 "created_at" => created_at,
129 "in_reply_to_status_id" => nil,
130 "external_url" => activity.data["id"],
131 "activity_type" => "delete"
135 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
136 user = get_user(activity.data["actor"], opts)
137 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
138 created_at = created_at |> Utils.date_to_asctime()
140 followed = get_user(activity.data["object"], opts)
141 text = "#{user.nickname} started following #{followed.nickname}"
145 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
147 "statusnet_html" => text,
149 "is_local" => activity.local,
150 "is_post_verb" => false,
151 "created_at" => created_at,
152 "in_reply_to_status_id" => nil,
153 "external_url" => activity.data["id"],
154 "activity_type" => "follow"
158 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
159 user = get_user(activity.data["actor"], opts)
160 created_at = activity.data["published"] |> Utils.date_to_asctime()
161 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
163 text = "#{user.nickname} retweeted a status."
165 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
169 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
170 "statusnet_html" => text,
172 "is_local" => activity.local,
173 "is_post_verb" => false,
174 "uri" => "tag:#{activity.data["id"]}:objectType=note",
175 "created_at" => created_at,
176 "retweeted_status" => retweeted_status,
177 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
178 "external_url" => activity.data["id"],
179 "activity_type" => "repeat"
183 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
184 user = get_user(activity.data["actor"], opts)
185 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
186 liked_activity_id = if liked_activity, do: liked_activity.id, else: nil
189 activity.data["published"]
190 |> Utils.date_to_asctime()
192 text = "#{user.nickname} favorited a status."
196 do: render("activity.json", Map.merge(opts, %{activity: liked_activity})),
201 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
202 "statusnet_html" => text,
204 "is_local" => activity.local,
205 "is_post_verb" => false,
206 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
207 "created_at" => created_at,
208 "favorited_status" => favorited_status,
209 "in_reply_to_status_id" => liked_activity_id,
210 "external_url" => activity.data["id"],
211 "activity_type" => "like"
217 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
219 user = get_user(activity.data["actor"], opts)
221 created_at = object["published"] |> Utils.date_to_asctime()
222 like_count = object["like_count"] || 0
223 announcement_count = object["announcement_count"] || 0
224 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
225 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
229 |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
231 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
233 conversation_id = get_context_id(activity, opts)
235 tags = activity.data["object"]["tag"] || []
236 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
238 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
240 {summary, content} = render_content(object)
244 |> HTML.filter_tags(User.html_filter_policy(opts[:for]))
245 |> Formatter.emojify(object["emoji"])
249 |> String.replace(~r/<br\s?\/?>/, "\n")
252 reply_parent = Activity.get_in_reply_to_activity(activity)
254 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
258 "uri" => activity.data["object"]["id"],
259 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
260 "statusnet_html" => html,
262 "is_local" => activity.local,
263 "is_post_verb" => true,
264 "created_at" => created_at,
265 "in_reply_to_status_id" => object["inReplyToStatusId"],
266 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
267 "in_reply_to_profileurl" => User.profile_url(reply_user),
268 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
269 "in_reply_to_user_id" => reply_user && reply_user.id,
270 "statusnet_conversation_id" => conversation_id,
271 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
272 "attentions" => attentions,
273 "fave_num" => like_count,
274 "repeat_num" => announcement_count,
275 "favorited" => !!favorited,
276 "repeated" => !!repeated,
277 "external_url" => object["external_url"] || object["id"],
279 "activity_type" => "post",
280 "possibly_sensitive" => possibly_sensitive,
281 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
286 def render("activity.json", %{activity: unhandled_activity}) do
287 Logger.warn("#{__MODULE__} unhandled activity: #{inspect(unhandled_activity)}")
291 def render_content(%{"type" => "Note"} = object) do
292 summary = object["summary"]
295 if !!summary and summary != "" do
296 "<p>#{summary}</p>#{object["content"]}"
304 def render_content(%{"type" => object_type} = object) when object_type in ["Article", "Page"] do
305 summary = object["name"] || object["summary"]
308 if !!summary and summary != "" and is_bitstring(object["url"]) do
309 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
317 def render_content(object) do
318 summary = object["summary"] || "Unhandled activity type: #{object["type"]}"
319 content = "<p>#{summary}</p>#{object["content"]}"