7608402397eefc5e53a4d286223b3893ba86a80f
[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([]), do: []
17 defp query_context_ids(contexts) do
18 query = from o in Object,
19 where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts)
20
21 Repo.all(query)
22 end
23
24 defp collect_context_ids(activities) do
25 contexts = activities
26 |> Enum.reject(&(&1.data["context_id"]))
27 |> Enum.map(fn(%{data: data}) ->
28 data["context"]
29 end)
30 |> Enum.filter(&(&1))
31 |> query_context_ids()
32 |> Enum.reduce(%{}, fn(%{data: %{"id" => ap_id}, id: id}, acc) ->
33 Map.put(acc, ap_id, id)
34 end)
35 end
36
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
40 cond do
41 id = options[:context_ids][context] -> id
42 true -> TwitterAPI.context_to_conversation_id(context)
43 end
44 end
45 defp get_context_id(_, _), do: nil
46
47 def render("index.json", opts) do
48 context_ids = collect_context_ids(opts.activities)
49 opts = opts
50 |> Map.put(:context_ids, context_ids)
51
52 render_many(
53 opts.activities,
54 ActivityView,
55 "activity.json",
56 opts
57 )
58 end
59
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()
63
64 %{
65 "id" => activity.id,
66 "uri" => activity.data["object"],
67 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
68 "attentions" => [],
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"
77 }
78 end
79
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()
84
85 followed = User.get_cached_by_ap_id(activity.data["object"])
86 text = "#{user.nickname} started following #{followed.nickname}"
87
88 %{
89 "id" => activity.id,
90 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
91 "attentions" => [],
92 "statusnet_html" => text,
93 "text" => 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"
100 }
101 end
102
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"])
107
108 text = "#{user.nickname} retweeted a status."
109
110 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
111
112 %{
113 "id" => activity.id,
114 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
115 "statusnet_html" => text,
116 "text" => 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"
125 }
126 end
127
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"])
131
132 created_at =
133 activity.data["published"]
134 |> Utils.date_to_asctime()
135
136 text = "#{user.nickname} favorited a status."
137
138 %{
139 "id" => activity.id,
140 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
141 "statusnet_html" => text,
142 "text" => 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"
150 }
151 end
152
153 def render(
154 "activity.json",
155 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
156 ) do
157 actor = get_in(activity.data, ["actor"])
158 user = User.get_cached_by_ap_id(actor)
159
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"] || [])
165
166 attentions =
167 activity.recipients
168 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
169 |> Enum.filter(& &1)
170 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
171
172 conversation_id = get_context_id(activity, opts)
173
174 tags = activity.data["object"]["tag"] || []
175 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
176
177 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
178
179 summary = activity.data["object"]["summary"]
180 content = object["content"]
181
182 content =
183 if !!summary and summary != "" do
184 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
185 else
186 content
187 end
188
189 html =
190 HtmlSanitizeEx.basic_html(content)
191 |> Formatter.emojify(object["emoji"])
192
193 %{
194 "id" => activity.id,
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"],
211 "tags" => tags,
212 "activity_type" => "post",
213 "possibly_sensitive" => possibly_sensitive
214 }
215 end
216 end