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
18 defp query_context_ids([]), do: []
20 defp query_context_ids(contexts) do
21 query = from(o in Object, where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts))
26 defp query_users([]), do: []
28 defp query_users(user_ids) do
29 query = from(user in User, where: user.ap_id in ^user_ids)
34 defp collect_context_ids(activities) do
37 |> Enum.reject(& &1.data["context_id"])
38 |> Enum.map(fn %{data: data} ->
42 |> query_context_ids()
43 |> Enum.reduce(%{}, fn %{data: %{"id" => ap_id}, id: id}, acc ->
44 Map.put(acc, ap_id, id)
48 defp collect_users(activities) do
50 |> Enum.map(fn activity ->
52 data = %{"type" => "Follow"} ->
53 [data["actor"], data["object"]]
57 end ++ activity.recipients
62 |> Enum.reduce(%{}, fn user, acc ->
63 Map.put(acc, user.ap_id, user)
67 defp get_context_id(%{data: %{"context_id" => context_id}}, _) when not is_nil(context_id),
70 defp get_context_id(%{data: %{"context" => nil}}, _), do: nil
72 defp get_context_id(%{data: %{"context" => context}}, options) do
74 id = options[:context_ids][context] -> id
75 true -> TwitterAPI.context_to_conversation_id(context)
79 defp get_context_id(_, _), do: nil
81 defp get_user(ap_id, opts) do
83 user = opts[:users][ap_id] ->
86 String.ends_with?(ap_id, "/followers") ->
89 ap_id == "https://www.w3.org/ns/activitystreams#Public" ->
93 User.get_cached_by_ap_id(ap_id)
97 def render("index.json", opts) do
98 context_ids = collect_context_ids(opts.activities)
99 users = collect_users(opts.activities)
103 |> Map.put(:context_ids, context_ids)
104 |> Map.put(:users, users)
114 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
115 user = get_user(activity.data["actor"], opts)
116 created_at = activity.data["published"] |> Utils.date_to_asctime()
120 "uri" => activity.data["object"],
121 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
123 "statusnet_html" => "deleted notice {{tag",
124 "text" => "deleted notice {{tag",
125 "is_local" => activity.local,
126 "is_post_verb" => false,
127 "created_at" => created_at,
128 "in_reply_to_status_id" => nil,
129 "external_url" => activity.data["id"],
130 "activity_type" => "delete"
134 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
135 user = get_user(activity.data["actor"], opts)
136 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
137 created_at = created_at |> Utils.date_to_asctime()
139 followed = get_user(activity.data["object"], opts)
140 text = "#{user.nickname} started following #{followed.nickname}"
144 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
146 "statusnet_html" => text,
148 "is_local" => activity.local,
149 "is_post_verb" => false,
150 "created_at" => created_at,
151 "in_reply_to_status_id" => nil,
152 "external_url" => activity.data["id"],
153 "activity_type" => "follow"
157 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
158 user = get_user(activity.data["actor"], opts)
159 created_at = activity.data["published"] |> Utils.date_to_asctime()
160 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
162 text = "#{user.nickname} retweeted a status."
164 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
168 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
169 "statusnet_html" => text,
171 "is_local" => activity.local,
172 "is_post_verb" => false,
173 "uri" => "tag:#{activity.data["id"]}:objectType=note",
174 "created_at" => created_at,
175 "retweeted_status" => retweeted_status,
176 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
177 "external_url" => activity.data["id"],
178 "activity_type" => "repeat"
182 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
183 user = get_user(activity.data["actor"], opts)
184 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
185 liked_activity_id = if liked_activity, do: liked_activity.id, else: nil
188 activity.data["published"]
189 |> Utils.date_to_asctime()
191 text = "#{user.nickname} favorited a status."
195 do: render("activity.json", Map.merge(opts, %{activity: liked_activity})),
200 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
201 "statusnet_html" => text,
203 "is_local" => activity.local,
204 "is_post_verb" => false,
205 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
206 "created_at" => created_at,
207 "favorited_status" => favorited_status,
208 "in_reply_to_status_id" => liked_activity_id,
209 "external_url" => activity.data["id"],
210 "activity_type" => "like"
216 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
218 user = get_user(activity.data["actor"], opts)
220 created_at = object["published"] |> Utils.date_to_asctime()
221 like_count = object["like_count"] || 0
222 announcement_count = object["announcement_count"] || 0
223 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
224 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
228 |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
230 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
232 conversation_id = get_context_id(activity, opts)
234 tags = activity.data["object"]["tag"] || []
235 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
237 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
239 {summary, content} = render_content(object)
242 HTML.filter_tags(content, User.html_filter_policy(opts[:for]))
243 |> Formatter.emojify(object["emoji"])
245 reply_parent = Activity.get_in_reply_to_activity(activity)
247 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
251 "uri" => activity.data["object"]["id"],
252 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
253 "statusnet_html" => html,
254 "text" => HTML.strip_tags(content),
255 "is_local" => activity.local,
256 "is_post_verb" => true,
257 "created_at" => created_at,
258 "in_reply_to_status_id" => object["inReplyToStatusId"],
259 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
260 "in_reply_to_profileurl" => User.profile_url(reply_user),
261 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
262 "in_reply_to_user_id" => reply_user && reply_user.id,
263 "statusnet_conversation_id" => conversation_id,
264 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
265 "attentions" => attentions,
266 "fave_num" => like_count,
267 "repeat_num" => announcement_count,
268 "favorited" => !!favorited,
269 "repeated" => !!repeated,
270 "external_url" => object["external_url"] || object["id"],
272 "activity_type" => "post",
273 "possibly_sensitive" => possibly_sensitive,
274 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
279 def render_content(%{"type" => "Note"} = object) do
280 summary = object["summary"]
283 if !!summary and summary != "" do
284 "<p>#{summary}</p>#{object["content"]}"
292 def render_content(%{"type" => object_type} = object) when object_type in ["Article", "Page"] do
293 summary = object["name"] || object["summary"]
296 if !!summary and summary != "" and is_bitstring(object["url"]) do
297 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
305 def render_content(object) do
306 summary = object["summary"] || "Unhandled activity type: #{object["type"]}"
307 content = "<p>#{summary}</p>#{object["content"]}"