TwitterAPI: Display cws.
[akkoma] / lib / pleroma / web / twitter_api / representers / activity_representer.ex
1 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
2 use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
3 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
4 alias Pleroma.{Activity, User}
5 alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView}
6 alias Pleroma.Web.CommonAPI.Utils
7 alias Pleroma.Formatter
8
9 defp user_by_ap_id(user_list, ap_id) do
10 Enum.find(user_list, fn (%{ap_id: user_id}) -> ap_id == user_id end)
11 end
12
13 def to_map(%Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} = activity,
14 %{users: users, announced_activity: announced_activity} = opts) do
15 user = user_by_ap_id(users, actor)
16 created_at = created_at |> Utils.date_to_asctime
17
18 text = "#{user.nickname} retweeted a status."
19
20 announced_user = user_by_ap_id(users, announced_activity.data["actor"])
21 retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
22 %{
23 "id" => activity.id,
24 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
25 "statusnet_html" => text,
26 "text" => text,
27 "is_local" => activity.local,
28 "is_post_verb" => false,
29 "uri" => "tag:#{activity.data["id"]}:objectType=note",
30 "created_at" => created_at,
31 "retweeted_status" => retweeted_status,
32 "statusnet_conversation_id" => conversation_id(announced_activity),
33 "external_url" => activity.data["id"],
34 "activity_type" => "repeat"
35 }
36 end
37
38 def to_map(%Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
39 %{user: user, liked_activity: liked_activity} = opts) do
40 created_at = created_at |> Utils.date_to_asctime
41
42 text = "#{user.nickname} favorited a status."
43
44 %{
45 "id" => activity.id,
46 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
47 "statusnet_html" => text,
48 "text" => text,
49 "is_local" => activity.local,
50 "is_post_verb" => false,
51 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
52 "created_at" => created_at,
53 "in_reply_to_status_id" => liked_activity.id,
54 "external_url" => activity.data["id"],
55 "activity_type" => "like"
56 }
57 end
58
59 def to_map(%Activity{data: %{"type" => "Follow", "published" => created_at, "object" => followed_id}} = activity, %{user: user} = opts) do
60 created_at = created_at |> Utils.date_to_asctime
61
62 followed = User.get_cached_by_ap_id(followed_id)
63 text = "#{user.nickname} started following #{followed.nickname}"
64 %{
65 "id" => activity.id,
66 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
67 "attentions" => [],
68 "statusnet_html" => text,
69 "text" => text,
70 "is_local" => activity.local,
71 "is_post_verb" => false,
72 "created_at" => created_at,
73 "in_reply_to_status_id" => nil,
74 "external_url" => activity.data["id"],
75 "activity_type" => "follow"
76 }
77 end
78
79 # TODO:
80 # Make this more proper. Just a placeholder to not break the frontend.
81 def to_map(%Activity{data: %{"type" => "Undo", "published" => created_at, "object" => undid_activity }} = activity, %{user: user} = opts) do
82 created_at = created_at |> Utils.date_to_asctime
83
84 text = "#{user.nickname} undid the action at #{undid_activity}"
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" => "undo"
97 }
98 end
99
100 def to_map(%Activity{data: %{"type" => "Delete", "published" => created_at, "object" => deleted_object }} = activity, %{user: user} = opts) do
101 created_at = created_at |> Utils.date_to_asctime
102
103 %{
104 "id" => activity.id,
105 "uri" => activity.data["object"],
106 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
107 "attentions" => [],
108 "statusnet_html" => "deleted notice {{tag",
109 "text" => "deleted notice {{tag" ,
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" => "delete"
116 }
117 end
118
119 def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
120 created_at = object["published"] |> Utils.date_to_asctime
121 like_count = object["like_count"] || 0
122 announcement_count = object["announcement_count"] || 0
123 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
124 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
125
126 mentions = opts[:mentioned] || []
127
128 attentions = activity.data["to"]
129 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
130 |> Enum.filter(&(&1))
131 |> Enum.map(fn (user) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
132
133 conversation_id = conversation_id(activity)
134
135 tags = activity.data["object"]["tag"] || []
136 possibly_sensitive = Enum.member?(tags, "nsfw")
137
138 content = if activity.data["object"]["summary"] do
139 "<span>#{activity.data["object"]["summary"]}</span><br>#{content}</span>"
140 else
141 content
142 end
143
144 html = HtmlSanitizeEx.basic_html(content) |> Formatter.emojify(object["emoji"])
145
146 %{
147 "id" => activity.id,
148 "uri" => activity.data["object"]["id"],
149 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
150 "statusnet_html" => html,
151 "text" => HtmlSanitizeEx.strip_tags(content),
152 "is_local" => activity.local,
153 "is_post_verb" => true,
154 "created_at" => created_at,
155 "in_reply_to_status_id" => object["inReplyToStatusId"],
156 "statusnet_conversation_id" => conversation_id,
157 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
158 "attentions" => attentions,
159 "fave_num" => like_count,
160 "repeat_num" => announcement_count,
161 "favorited" => to_boolean(favorited),
162 "repeated" => to_boolean(repeated),
163 "external_url" => object["external_url"],
164 "tags" => tags,
165 "activity_type" => "post",
166 "possibly_sensitive" => possibly_sensitive
167 }
168 end
169
170 def conversation_id(activity) do
171 with context when not is_nil(context) <- activity.data["context"] do
172 TwitterAPI.context_to_conversation_id(context)
173 else _e -> nil
174 end
175 end
176
177 defp to_boolean(false) do
178 false
179 end
180
181 defp to_boolean(nil) do
182 false
183 end
184
185 defp to_boolean(_) do
186 true
187 end
188 end