Merge branch 'update-pleroma-frontend' into 'develop'
[akkoma] / lib / pleroma / web / twitter_api / representers / activity_representer.ex
1 # THIS MODULE IS DEPRECATED! DON'T USE IT!
2 # USE THE Pleroma.Web.TwitterAPI.Views.ActivityView MODULE!
3 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
4 use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
5 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
6 alias Pleroma.{Activity, User}
7 alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView}
8 alias Pleroma.Web.CommonAPI.Utils
9 alias Pleroma.Formatter
10 alias Pleroma.HTML
11
12 defp user_by_ap_id(user_list, ap_id) do
13 Enum.find(user_list, fn %{ap_id: user_id} -> ap_id == user_id end)
14 end
15
16 def to_map(
17 %Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} =
18 activity,
19 %{users: users, announced_activity: announced_activity} = opts
20 ) do
21 user = user_by_ap_id(users, actor)
22 created_at = created_at |> Utils.date_to_asctime()
23
24 text = "#{user.nickname} retweeted a status."
25
26 announced_user = user_by_ap_id(users, announced_activity.data["actor"])
27 retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
28
29 %{
30 "id" => activity.id,
31 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
32 "statusnet_html" => text,
33 "text" => text,
34 "is_local" => activity.local,
35 "is_post_verb" => false,
36 "uri" => "tag:#{activity.data["id"]}:objectType=note",
37 "created_at" => created_at,
38 "retweeted_status" => retweeted_status,
39 "statusnet_conversation_id" => conversation_id(announced_activity),
40 "external_url" => activity.data["id"],
41 "activity_type" => "repeat"
42 }
43 end
44
45 def to_map(
46 %Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
47 %{user: user, liked_activity: liked_activity} = opts
48 ) do
49 created_at = created_at |> Utils.date_to_asctime()
50
51 text = "#{user.nickname} favorited a status."
52
53 %{
54 "id" => activity.id,
55 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
56 "statusnet_html" => text,
57 "text" => text,
58 "is_local" => activity.local,
59 "is_post_verb" => false,
60 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
61 "created_at" => created_at,
62 "in_reply_to_status_id" => liked_activity.id,
63 "external_url" => activity.data["id"],
64 "activity_type" => "like"
65 }
66 end
67
68 def to_map(
69 %Activity{data: %{"type" => "Follow", "object" => followed_id}} = activity,
70 %{user: user} = opts
71 ) do
72 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
73 created_at = created_at |> Utils.date_to_asctime()
74
75 followed = User.get_cached_by_ap_id(followed_id)
76 text = "#{user.nickname} started following #{followed.nickname}"
77
78 %{
79 "id" => activity.id,
80 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
81 "attentions" => [],
82 "statusnet_html" => text,
83 "text" => text,
84 "is_local" => activity.local,
85 "is_post_verb" => false,
86 "created_at" => created_at,
87 "in_reply_to_status_id" => nil,
88 "external_url" => activity.data["id"],
89 "activity_type" => "follow"
90 }
91 end
92
93 # TODO:
94 # Make this more proper. Just a placeholder to not break the frontend.
95 def to_map(
96 %Activity{
97 data: %{"type" => "Undo", "published" => created_at, "object" => undid_activity}
98 } = activity,
99 %{user: user} = opts
100 ) do
101 created_at = created_at |> Utils.date_to_asctime()
102
103 text = "#{user.nickname} undid the action at #{undid_activity["id"]}"
104
105 %{
106 "id" => activity.id,
107 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
108 "attentions" => [],
109 "statusnet_html" => text,
110 "text" => text,
111 "is_local" => activity.local,
112 "is_post_verb" => false,
113 "created_at" => created_at,
114 "in_reply_to_status_id" => nil,
115 "external_url" => activity.data["id"],
116 "activity_type" => "undo"
117 }
118 end
119
120 def to_map(
121 %Activity{data: %{"type" => "Delete", "published" => created_at, "object" => _}} =
122 activity,
123 %{user: user} = opts
124 ) do
125 created_at = created_at |> Utils.date_to_asctime()
126
127 %{
128 "id" => activity.id,
129 "uri" => activity.data["object"],
130 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
131 "attentions" => [],
132 "statusnet_html" => "deleted notice {{tag",
133 "text" => "deleted notice {{tag",
134 "is_local" => activity.local,
135 "is_post_verb" => false,
136 "created_at" => created_at,
137 "in_reply_to_status_id" => nil,
138 "external_url" => activity.data["id"],
139 "activity_type" => "delete"
140 }
141 end
142
143 def to_map(
144 %Activity{data: %{"object" => %{"content" => content} = object}} = activity,
145 %{user: user} = opts
146 ) do
147 created_at = object["published"] |> Utils.date_to_asctime()
148 like_count = object["like_count"] || 0
149 announcement_count = object["announcement_count"] || 0
150 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
151 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
152
153 mentions = opts[:mentioned] || []
154
155 attentions =
156 activity.recipients
157 |> Enum.map(fn ap_id -> Enum.find(mentions, fn user -> ap_id == user.ap_id end) end)
158 |> Enum.filter(& &1)
159 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
160
161 conversation_id = conversation_id(activity)
162
163 tags = activity.data["object"]["tag"] || []
164 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
165
166 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
167
168 {summary, content} = ActivityView.render_content(object)
169
170 html =
171 HTML.filter_tags(content)
172 |> Formatter.emojify(object["emoji"])
173
174 video =
175 if object["type"] == "Video" do
176 vid = [object]
177 else
178 []
179 end
180
181 attachments = (object["attachment"] || []) ++ video
182
183 %{
184 "id" => activity.id,
185 "uri" => activity.data["object"]["id"],
186 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
187 "statusnet_html" => html,
188 "text" => HTML.strip_tags(content),
189 "is_local" => activity.local,
190 "is_post_verb" => true,
191 "created_at" => created_at,
192 "in_reply_to_status_id" => object["inReplyToStatusId"],
193 "statusnet_conversation_id" => conversation_id,
194 "attachments" => attachments |> ObjectRepresenter.enum_to_list(opts),
195 "attentions" => attentions,
196 "fave_num" => like_count,
197 "repeat_num" => announcement_count,
198 "favorited" => to_boolean(favorited),
199 "repeated" => to_boolean(repeated),
200 "external_url" => object["external_url"] || object["id"],
201 "tags" => tags,
202 "activity_type" => "post",
203 "possibly_sensitive" => possibly_sensitive,
204 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
205 "summary" => object["summary"]
206 }
207 end
208
209 def conversation_id(activity) do
210 with context when not is_nil(context) <- activity.data["context"] do
211 TwitterAPI.context_to_conversation_id(context)
212 else
213 _e -> nil
214 end
215 end
216
217 defp to_boolean(false) do
218 false
219 end
220
221 defp to_boolean(nil) do
222 false
223 end
224
225 defp to_boolean(_) do
226 true
227 end
228 end