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