Generates contexts and ids on insertion time.
[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 defp get_context_id(_, _), do: nil
43
44 def render("index.json", opts) do
45 context_ids = collect_context_ids(opts.activities)
46 opts = opts
47 |> Map.put(:context_ids, context_ids)
48
49 render_many(
50 opts.activities,
51 ActivityView,
52 "activity.json",
53 opts
54 )
55 end
56
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()
60
61 %{
62 "id" => activity.id,
63 "uri" => activity.data["object"],
64 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
65 "attentions" => [],
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"
74 }
75 end
76
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()
81
82 followed = User.get_cached_by_ap_id(activity.data["object"])
83 text = "#{user.nickname} started following #{followed.nickname}"
84
85 %{
86 "id" => activity.id,
87 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
88 "attentions" => [],
89 "statusnet_html" => text,
90 "text" => 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"
97 }
98 end
99
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"])
104
105 text = "#{user.nickname} retweeted a status."
106
107 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
108
109 %{
110 "id" => activity.id,
111 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
112 "statusnet_html" => text,
113 "text" => 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"
122 }
123 end
124
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"])
128
129 created_at =
130 activity.data["published"]
131 |> Utils.date_to_asctime()
132
133 text = "#{user.nickname} favorited a status."
134
135 %{
136 "id" => activity.id,
137 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
138 "statusnet_html" => text,
139 "text" => 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"
147 }
148 end
149
150 def render(
151 "activity.json",
152 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
153 ) do
154 actor = get_in(activity.data, ["actor"])
155 user = User.get_cached_by_ap_id(actor)
156
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"] || [])
162
163 attentions =
164 activity.recipients
165 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
166 |> Enum.filter(& &1)
167 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
168
169 conversation_id = get_context_id(activity, opts)
170
171 tags = activity.data["object"]["tag"] || []
172 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
173
174 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
175
176 summary = activity.data["object"]["summary"]
177 content = object["content"]
178
179 content =
180 if !!summary and summary != "" do
181 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
182 else
183 content
184 end
185
186 html =
187 HtmlSanitizeEx.basic_html(content)
188 |> Formatter.emojify(object["emoji"])
189
190 %{
191 "id" => activity.id,
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"],
208 "tags" => tags,
209 "activity_type" => "post",
210 "possibly_sensitive" => possibly_sensitive
211 }
212 end
213
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)
217 else
218 _e -> nil
219 end
220 end
221 end