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)
42 defp get_context_id(_, _), do: nil
44 def render("index.json", opts) do
45 context_ids = collect_context_ids(opts.activities)
47 |> Map.put(:context_ids, context_ids)
57 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
58 user = User.get_cached_by_ap_id(activity.data["actor"])
59 created_at = activity.data["published"] |> Utils.date_to_asctime()
63 "uri" => activity.data["object"],
64 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
66 "statusnet_html" => "deleted notice {{tag",
67 "text" => "deleted notice {{tag",
68 "is_local" => activity.local,
69 "is_post_verb" => false,
70 "created_at" => created_at,
71 "in_reply_to_status_id" => nil,
72 "external_url" => activity.data["id"],
73 "activity_type" => "delete"
77 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
78 user = User.get_cached_by_ap_id(activity.data["actor"])
79 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
80 created_at = created_at |> Utils.date_to_asctime()
82 followed = User.get_cached_by_ap_id(activity.data["object"])
83 text = "#{user.nickname} started following #{followed.nickname}"
87 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
89 "statusnet_html" => text,
91 "is_local" => activity.local,
92 "is_post_verb" => false,
93 "created_at" => created_at,
94 "in_reply_to_status_id" => nil,
95 "external_url" => activity.data["id"],
96 "activity_type" => "follow"
100 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
101 user = User.get_by_ap_id(activity.data["actor"])
102 created_at = activity.data["published"] |> Utils.date_to_asctime()
103 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
105 text = "#{user.nickname} retweeted a status."
107 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
111 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
112 "statusnet_html" => text,
114 "is_local" => activity.local,
115 "is_post_verb" => false,
116 "uri" => "tag:#{activity.data["id"]}:objectType=note",
117 "created_at" => created_at,
118 "retweeted_status" => retweeted_status,
119 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
120 "external_url" => activity.data["id"],
121 "activity_type" => "repeat"
125 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
126 user = User.get_cached_by_ap_id(activity.data["actor"])
127 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
130 activity.data["published"]
131 |> Utils.date_to_asctime()
133 text = "#{user.nickname} favorited a status."
137 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
138 "statusnet_html" => text,
140 "is_local" => activity.local,
141 "is_post_verb" => false,
142 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
143 "created_at" => created_at,
144 "in_reply_to_status_id" => liked_activity.id,
145 "external_url" => activity.data["id"],
146 "activity_type" => "like"
152 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
154 actor = get_in(activity.data, ["actor"])
155 user = User.get_cached_by_ap_id(actor)
157 created_at = object["published"] |> Utils.date_to_asctime()
158 like_count = object["like_count"] || 0
159 announcement_count = object["announcement_count"] || 0
160 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
161 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
165 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
167 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
169 conversation_id = get_context_id(activity, opts)
171 tags = activity.data["object"]["tag"] || []
172 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
174 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
176 summary = activity.data["object"]["summary"]
177 content = object["content"]
180 if !!summary and summary != "" do
181 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
187 HtmlSanitizeEx.basic_html(content)
188 |> Formatter.emojify(object["emoji"])
192 "uri" => activity.data["object"]["id"],
193 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
194 "statusnet_html" => html,
195 "text" => HtmlSanitizeEx.strip_tags(content),
196 "is_local" => activity.local,
197 "is_post_verb" => true,
198 "created_at" => created_at,
199 "in_reply_to_status_id" => object["inReplyToStatusId"],
200 "statusnet_conversation_id" => conversation_id,
201 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
202 "attentions" => attentions,
203 "fave_num" => like_count,
204 "repeat_num" => announcement_count,
205 "favorited" => !!favorited,
206 "repeated" => !!repeated,
207 "external_url" => object["external_url"] || object["id"],
209 "activity_type" => "post",
210 "possibly_sensitive" => possibly_sensitive
214 defp conversation_id(activity) do
215 with context when not is_nil(context) <- activity.data["context"] do
216 TwitterAPI.context_to_conversation_id(context)