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
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)
16 %Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} =
18 %{users: users, announced_activity: announced_activity} = opts
20 user = user_by_ap_id(users, actor)
21 created_at = created_at |> Utils.date_to_asctime()
23 text = "#{user.nickname} retweeted a status."
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))
30 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
31 "statusnet_html" => 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"
45 %Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
46 %{user: user, liked_activity: liked_activity} = opts
48 created_at = created_at |> Utils.date_to_asctime()
50 text = "#{user.nickname} favorited a status."
54 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
55 "statusnet_html" => 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"
68 %Activity{data: %{"type" => "Follow", "object" => followed_id}} = activity,
71 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
72 created_at = created_at |> Utils.date_to_asctime()
74 followed = User.get_cached_by_ap_id(followed_id)
75 text = "#{user.nickname} started following #{followed.nickname}"
79 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
81 "statusnet_html" => 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"
93 # Make this more proper. Just a placeholder to not break the frontend.
96 data: %{"type" => "Undo", "published" => created_at, "object" => undid_activity}
100 created_at = created_at |> Utils.date_to_asctime()
102 text = "#{user.nickname} undid the action at #{undid_activity}"
106 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
108 "statusnet_html" => 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"
120 %Activity{data: %{"type" => "Delete", "published" => created_at, "object" => _}} =
124 created_at = created_at |> Utils.date_to_asctime()
128 "uri" => activity.data["object"],
129 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
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"
143 %Activity{data: %{"object" => %{"content" => content} = object}} = activity,
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"] || [])
152 mentions = opts[:mentioned] || []
156 |> Enum.map(fn ap_id -> Enum.find(mentions, fn user -> ap_id == user.ap_id end) end)
158 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
160 conversation_id = conversation_id(activity)
162 tags = activity.data["object"]["tag"] || []
163 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
165 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
167 summary = activity.data["object"]["summary"]
170 if !!summary and summary != "" do
171 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
177 HtmlSanitizeEx.basic_html(content)
178 |> Formatter.emojify(object["emoji"])
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"],
199 "activity_type" => "post",
200 "possibly_sensitive" => possibly_sensitive
204 def conversation_id(activity) do
205 with context when not is_nil(context) <- activity.data["context"] do
206 TwitterAPI.context_to_conversation_id(context)
212 defp to_boolean(false) do
216 defp to_boolean(nil) do
220 defp to_boolean(_) do