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
12 alias Pleroma.Formatter
16 defp query_context_ids([]), do: []
17 defp query_context_ids(contexts) do
18 query = from o in Object,
19 where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts)
24 defp collect_context_ids(activities) do
26 |> Enum.reject(&(&1.data["context_id"]))
27 |> Enum.map(fn(%{data: data}) ->
31 |> query_context_ids()
32 |> Enum.reduce(%{}, fn(%{data: %{"id" => ap_id}, id: id}, acc) ->
33 Map.put(acc, ap_id, id)
37 defp get_context_id(%{data: %{"context_id" => context_id}}, _) when not is_nil(context_id), do: context_id
38 defp get_context_id(%{data: %{"context" => nil}}, _), do: nil
39 defp get_context_id(%{data: %{"context" => context}}, options) do
41 id = options[:context_ids][context] -> id
42 true -> TwitterAPI.context_to_conversation_id(context)
45 defp get_context_id(_, _), do: nil
47 def render("index.json", opts) do
48 context_ids = collect_context_ids(opts.activities)
50 |> Map.put(:context_ids, context_ids)
60 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
61 user = User.get_cached_by_ap_id(activity.data["actor"])
62 created_at = activity.data["published"] |> Utils.date_to_asctime()
66 "uri" => activity.data["object"],
67 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
69 "statusnet_html" => "deleted notice {{tag",
70 "text" => "deleted notice {{tag",
71 "is_local" => activity.local,
72 "is_post_verb" => false,
73 "created_at" => created_at,
74 "in_reply_to_status_id" => nil,
75 "external_url" => activity.data["id"],
76 "activity_type" => "delete"
80 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
81 user = User.get_cached_by_ap_id(activity.data["actor"])
82 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
83 created_at = created_at |> Utils.date_to_asctime()
85 followed = User.get_cached_by_ap_id(activity.data["object"])
86 text = "#{user.nickname} started following #{followed.nickname}"
90 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
92 "statusnet_html" => text,
94 "is_local" => activity.local,
95 "is_post_verb" => false,
96 "created_at" => created_at,
97 "in_reply_to_status_id" => nil,
98 "external_url" => activity.data["id"],
99 "activity_type" => "follow"
103 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
104 user = User.get_by_ap_id(activity.data["actor"])
105 created_at = activity.data["published"] |> Utils.date_to_asctime()
106 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
108 text = "#{user.nickname} retweeted a status."
110 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
114 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
115 "statusnet_html" => text,
117 "is_local" => activity.local,
118 "is_post_verb" => false,
119 "uri" => "tag:#{activity.data["id"]}:objectType=note",
120 "created_at" => created_at,
121 "retweeted_status" => retweeted_status,
122 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
123 "external_url" => activity.data["id"],
124 "activity_type" => "repeat"
128 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
129 user = User.get_cached_by_ap_id(activity.data["actor"])
130 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
133 activity.data["published"]
134 |> Utils.date_to_asctime()
136 text = "#{user.nickname} favorited a status."
140 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
141 "statusnet_html" => text,
143 "is_local" => activity.local,
144 "is_post_verb" => false,
145 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
146 "created_at" => created_at,
147 "in_reply_to_status_id" => liked_activity.id,
148 "external_url" => activity.data["id"],
149 "activity_type" => "like"
155 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
157 actor = get_in(activity.data, ["actor"])
158 user = User.get_cached_by_ap_id(actor)
160 created_at = object["published"] |> Utils.date_to_asctime()
161 like_count = object["like_count"] || 0
162 announcement_count = object["announcement_count"] || 0
163 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
164 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
168 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
170 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
172 conversation_id = get_context_id(activity, opts)
174 tags = activity.data["object"]["tag"] || []
175 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
177 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
179 summary = activity.data["object"]["summary"]
180 content = object["content"]
183 if !!summary and summary != "" do
184 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
190 HtmlSanitizeEx.basic_html(content)
191 |> Formatter.emojify(object["emoji"])
195 "uri" => activity.data["object"]["id"],
196 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
197 "statusnet_html" => html,
198 "text" => HtmlSanitizeEx.strip_tags(content),
199 "is_local" => activity.local,
200 "is_post_verb" => true,
201 "created_at" => created_at,
202 "in_reply_to_status_id" => object["inReplyToStatusId"],
203 "statusnet_conversation_id" => conversation_id,
204 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
205 "attentions" => attentions,
206 "fave_num" => like_count,
207 "repeat_num" => announcement_count,
208 "favorited" => !!favorited,
209 "repeated" => !!repeated,
210 "external_url" => object["external_url"] || object["id"],
212 "activity_type" => "post",
213 "possibly_sensitive" => possibly_sensitive