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