Federate emoji out.
[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 %{
139 "id" => activity.id,
140 "uri" => activity.data["object"]["id"],
141 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
142 "statusnet_html" => HtmlSanitizeEx.basic_html(content) |> Formatter.emojify,
143 "text" => HtmlSanitizeEx.strip_tags(content),
144 "is_local" => activity.local,
145 "is_post_verb" => true,
146 "created_at" => created_at,
147 "in_reply_to_status_id" => object["inReplyToStatusId"],
148 "statusnet_conversation_id" => conversation_id,
149 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
150 "attentions" => attentions,
151 "fave_num" => like_count,
152 "repeat_num" => announcement_count,
153 "favorited" => to_boolean(favorited),
154 "repeated" => to_boolean(repeated),
155 "external_url" => object["external_url"],
156 "tags" => tags,
157 "activity_type" => "post",
158 "possibly_sensitive" => possibly_sensitive
159 }
160 end
161
162 def conversation_id(activity) do
163 with context when not is_nil(context) <- activity.data["context"] do
164 TwitterAPI.context_to_conversation_id(context)
165 else _e -> nil
166 end
167 end
168
169 defp to_boolean(false) do
170 false
171 end
172
173 defp to_boolean(nil) do
174 false
175 end
176
177 defp to_boolean(_) do
178 true
179 end
180 end