Merge branch 'feature/activitypub' into 'develop'
[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", "object" => followed_id}} = activity, %{user: user} = opts) do
60 created_at = activity.data["published"] || (DateTime.to_iso8601(activity.inserted_at))
61 created_at = created_at |> Utils.date_to_asctime
62
63 followed = User.get_cached_by_ap_id(followed_id)
64 text = "#{user.nickname} started following #{followed.nickname}"
65 %{
66 "id" => activity.id,
67 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
68 "attentions" => [],
69 "statusnet_html" => text,
70 "text" => text,
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" => "follow"
77 }
78 end
79
80 # TODO:
81 # Make this more proper. Just a placeholder to not break the frontend.
82 def to_map(%Activity{data: %{"type" => "Undo", "published" => created_at, "object" => undid_activity }} = activity, %{user: user} = opts) do
83 created_at = created_at |> Utils.date_to_asctime
84
85 text = "#{user.nickname} undid the action at #{undid_activity}"
86 %{
87 "id" => activity.id,
88 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
89 "attentions" => [],
90 "statusnet_html" => text,
91 "text" => text,
92 "is_local" => activity.local,
93 "is_post_verb" => false,
94 "created_at" => created_at,
95 "in_reply_to_status_id" => nil,
96 "external_url" => activity.data["id"],
97 "activity_type" => "undo"
98 }
99 end
100
101 def to_map(%Activity{data: %{"type" => "Delete", "published" => created_at, "object" => _ }} = activity, %{user: user} = opts) do
102 created_at = created_at |> Utils.date_to_asctime
103
104 %{
105 "id" => activity.id,
106 "uri" => activity.data["object"],
107 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
108 "attentions" => [],
109 "statusnet_html" => "deleted notice {{tag",
110 "text" => "deleted notice {{tag" ,
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" => "delete"
117 }
118 end
119
120 def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
121 created_at = object["published"] |> Utils.date_to_asctime
122 like_count = object["like_count"] || 0
123 announcement_count = object["announcement_count"] || 0
124 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
125 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
126
127 mentions = opts[:mentioned] || []
128
129 attentions = activity.recipients
130 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
131 |> Enum.filter(&(&1))
132 |> Enum.map(fn (user) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
133
134 conversation_id = conversation_id(activity)
135
136 tags = activity.data["object"]["tag"] || []
137 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
138
139 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
140
141 summary = activity.data["object"]["summary"]
142 content = if !!summary and summary != "" do
143 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
144 else
145 content
146 end
147
148 html = HtmlSanitizeEx.basic_html(content) |> Formatter.emojify(object["emoji"])
149
150 %{
151 "id" => activity.id,
152 "uri" => activity.data["object"]["id"],
153 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
154 "statusnet_html" => html,
155 "text" => HtmlSanitizeEx.strip_tags(content),
156 "is_local" => activity.local,
157 "is_post_verb" => true,
158 "created_at" => created_at,
159 "in_reply_to_status_id" => object["inReplyToStatusId"],
160 "statusnet_conversation_id" => conversation_id,
161 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
162 "attentions" => attentions,
163 "fave_num" => like_count,
164 "repeat_num" => announcement_count,
165 "favorited" => to_boolean(favorited),
166 "repeated" => to_boolean(repeated),
167 "external_url" => object["external_url"] || object["id"],
168 "tags" => tags,
169 "activity_type" => "post",
170 "possibly_sensitive" => possibly_sensitive
171 }
172 end
173
174 def conversation_id(activity) do
175 with context when not is_nil(context) <- activity.data["context"] do
176 TwitterAPI.context_to_conversation_id(context)
177 else _e -> nil
178 end
179 end
180
181 defp to_boolean(false) do
182 false
183 end
184
185 defp to_boolean(nil) do
186 false
187 end
188
189 defp to_boolean(_) do
190 true
191 end
192 end