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(contexts) do
17 query = from o in Object,
18 where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts)
23 defp collect_context_ids(activities) do
25 |> Enum.map(fn(%{data: data}) ->
29 |> query_context_ids()
30 |> Enum.reduce(%{}, fn(%{data: %{"id" => ap_id}, id: id}, acc) ->
31 Map.put(acc, ap_id, id)
35 defp get_context_id(%{data: %{"context" => nil}}), do: nil
36 defp get_context_id(%{data: %{"context" => context}}, options) do
38 id = options[:context_ids][context] -> id
39 true -> TwitterAPI.context_to_conversation_id(context)
43 def render("index.json", opts) do
44 context_ids = collect_context_ids(opts.activities)
46 |> Map.put(:context_ids, context_ids)
56 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
57 user = User.get_cached_by_ap_id(activity.data["actor"])
58 created_at = activity.data["published"] |> Utils.date_to_asctime()
62 "uri" => activity.data["object"],
63 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
65 "statusnet_html" => "deleted notice {{tag",
66 "text" => "deleted notice {{tag",
67 "is_local" => activity.local,
68 "is_post_verb" => false,
69 "created_at" => created_at,
70 "in_reply_to_status_id" => nil,
71 "external_url" => activity.data["id"],
72 "activity_type" => "delete"
76 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
77 user = User.get_cached_by_ap_id(activity.data["actor"])
78 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
79 created_at = created_at |> Utils.date_to_asctime()
81 followed = User.get_cached_by_ap_id(activity.data["object"])
82 text = "#{user.nickname} started following #{followed.nickname}"
86 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
88 "statusnet_html" => text,
90 "is_local" => activity.local,
91 "is_post_verb" => false,
92 "created_at" => created_at,
93 "in_reply_to_status_id" => nil,
94 "external_url" => activity.data["id"],
95 "activity_type" => "follow"
99 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
100 user = User.get_by_ap_id(activity.data["actor"])
101 created_at = activity.data["published"] |> Utils.date_to_asctime()
102 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
104 text = "#{user.nickname} retweeted a status."
106 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
110 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
111 "statusnet_html" => text,
113 "is_local" => activity.local,
114 "is_post_verb" => false,
115 "uri" => "tag:#{activity.data["id"]}:objectType=note",
116 "created_at" => created_at,
117 "retweeted_status" => retweeted_status,
118 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
119 "external_url" => activity.data["id"],
120 "activity_type" => "repeat"
124 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
125 user = User.get_cached_by_ap_id(activity.data["actor"])
126 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
129 activity.data["published"]
130 |> Utils.date_to_asctime()
132 text = "#{user.nickname} favorited a status."
136 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
137 "statusnet_html" => text,
139 "is_local" => activity.local,
140 "is_post_verb" => false,
141 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
142 "created_at" => created_at,
143 "in_reply_to_status_id" => liked_activity.id,
144 "external_url" => activity.data["id"],
145 "activity_type" => "like"
151 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
153 actor = get_in(activity.data, ["actor"])
154 user = User.get_cached_by_ap_id(actor)
156 created_at = object["published"] |> Utils.date_to_asctime()
157 like_count = object["like_count"] || 0
158 announcement_count = object["announcement_count"] || 0
159 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
160 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
164 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
166 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
168 conversation_id = get_context_id(activity, opts)
170 tags = activity.data["object"]["tag"] || []
171 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
173 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
175 summary = activity.data["object"]["summary"]
176 content = object["content"]
179 if !!summary and summary != "" do
180 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
186 HtmlSanitizeEx.basic_html(content)
187 |> Formatter.emojify(object["emoji"])
191 "uri" => activity.data["object"]["id"],
192 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
193 "statusnet_html" => html,
194 "text" => HtmlSanitizeEx.strip_tags(content),
195 "is_local" => activity.local,
196 "is_post_verb" => true,
197 "created_at" => created_at,
198 "in_reply_to_status_id" => object["inReplyToStatusId"],
199 "statusnet_conversation_id" => conversation_id,
200 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
201 "attentions" => attentions,
202 "fave_num" => like_count,
203 "repeat_num" => announcement_count,
204 "favorited" => !!favorited,
205 "repeated" => !!repeated,
206 "external_url" => object["external_url"] || object["id"],
208 "activity_type" => "post",
209 "possibly_sensitive" => possibly_sensitive
213 defp conversation_id(activity) do
214 with context when not is_nil(context) <- activity.data["context"] do
215 TwitterAPI.context_to_conversation_id(context)