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
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)
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
18 text = "#{user.nickname} retweeted a status."
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))
24 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
25 "statusnet_html" => 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"
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
42 text = "#{user.nickname} favorited a status."
46 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
47 "statusnet_html" => 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"
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
63 followed = User.get_cached_by_ap_id(followed_id)
64 text = "#{user.nickname} started following #{followed.nickname}"
67 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
69 "statusnet_html" => 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"
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
85 text = "#{user.nickname} undid the action at #{undid_activity}"
88 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
90 "statusnet_html" => 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"
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
106 "uri" => activity.data["object"],
107 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
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"
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"] || [])
127 mentions = opts[:mentioned] || []
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)
134 conversation_id = conversation_id(activity)
136 tags = activity.data["object"]["tag"] || []
137 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
139 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
141 summary = activity.data["object"]["summary"]
142 content = if !!summary and summary != "" do
143 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
148 html = HtmlSanitizeEx.basic_html(content)
149 |> Formatter.emojify(object["emoji"])
153 "uri" => activity.data["object"]["id"],
154 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
155 "statusnet_html" => html,
156 "text" => HtmlSanitizeEx.strip_tags(content),
157 "is_local" => activity.local,
158 "is_post_verb" => true,
159 "created_at" => created_at,
160 "in_reply_to_status_id" => object["inReplyToStatusId"],
161 "statusnet_conversation_id" => conversation_id,
162 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
163 "attentions" => attentions,
164 "fave_num" => like_count,
165 "repeat_num" => announcement_count,
166 "favorited" => to_boolean(favorited),
167 "repeated" => to_boolean(repeated),
168 "external_url" => object["external_url"] || object["id"],
170 "activity_type" => "post",
171 "possibly_sensitive" => possibly_sensitive
175 def conversation_id(activity) do
176 with context when not is_nil(context) <- activity.data["context"] do
177 TwitterAPI.context_to_conversation_id(context)
182 defp to_boolean(false) do
186 defp to_boolean(nil) do
190 defp to_boolean(_) do