Pre-fetch conversation ids.
[akkoma] / lib / pleroma / web / twitter_api / views / activity_view.ex
1 defmodule Pleroma.Web.TwitterAPI.ActivityView do
2 use Pleroma.Web, :view
3 alias Pleroma.Web.CommonAPI.Utils
4 alias Pleroma.User
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
9 alias Pleroma.Activity
10 alias Pleroma.Object
11 alias Pleroma.Repo
12 alias Pleroma.Formatter
13
14 import Ecto.Query
15
16 defp query_context_ids(contexts) do
17 query = from o in Object,
18 where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts)
19
20 Repo.all(query)
21 end
22
23 defp collect_context_ids(activities) do
24 contexts = activities
25 |> Enum.map(fn(%{data: data}) ->
26 data["context"]
27 end)
28 |> Enum.filter(&(&1))
29 |> query_context_ids()
30 |> Enum.reduce(%{}, fn(%{data: %{"id" => ap_id}, id: id}, acc) ->
31 Map.put(acc, ap_id, id)
32 end)
33 end
34
35 defp get_context_id(%{data: %{"context" => nil}}), do: nil
36 defp get_context_id(%{data: %{"context" => context}}, options) do
37 cond do
38 id = options[:context_ids][context] -> id
39 true -> TwitterAPI.context_to_conversation_id(context)
40 end
41 end
42
43 def render("index.json", opts) do
44 context_ids = collect_context_ids(opts.activities)
45 opts = opts
46 |> Map.put(:context_ids, context_ids)
47
48 render_many(
49 opts.activities,
50 ActivityView,
51 "activity.json",
52 opts
53 )
54 end
55
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()
59
60 %{
61 "id" => activity.id,
62 "uri" => activity.data["object"],
63 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
64 "attentions" => [],
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"
73 }
74 end
75
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()
80
81 followed = User.get_cached_by_ap_id(activity.data["object"])
82 text = "#{user.nickname} started following #{followed.nickname}"
83
84 %{
85 "id" => activity.id,
86 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
87 "attentions" => [],
88 "statusnet_html" => text,
89 "text" => 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"
96 }
97 end
98
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"])
103
104 text = "#{user.nickname} retweeted a status."
105
106 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
107
108 %{
109 "id" => activity.id,
110 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
111 "statusnet_html" => text,
112 "text" => 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"
121 }
122 end
123
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"])
127
128 created_at =
129 activity.data["published"]
130 |> Utils.date_to_asctime()
131
132 text = "#{user.nickname} favorited a status."
133
134 %{
135 "id" => activity.id,
136 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
137 "statusnet_html" => text,
138 "text" => 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"
146 }
147 end
148
149 def render(
150 "activity.json",
151 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
152 ) do
153 actor = get_in(activity.data, ["actor"])
154 user = User.get_cached_by_ap_id(actor)
155
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"] || [])
161
162 attentions =
163 activity.recipients
164 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
165 |> Enum.filter(& &1)
166 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
167
168 conversation_id = get_context_id(activity, opts)
169
170 tags = activity.data["object"]["tag"] || []
171 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
172
173 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
174
175 summary = activity.data["object"]["summary"]
176 content = object["content"]
177
178 content =
179 if !!summary and summary != "" do
180 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
181 else
182 content
183 end
184
185 html =
186 HtmlSanitizeEx.basic_html(content)
187 |> Formatter.emojify(object["emoji"])
188
189 %{
190 "id" => activity.id,
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"],
207 "tags" => tags,
208 "activity_type" => "post",
209 "possibly_sensitive" => possibly_sensitive
210 }
211 end
212
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)
216 else
217 _e -> nil
218 end
219 end
220 end