b0769de8917b6823c35a6c6cdfe69e252f2392a4
[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.id,
104 "uri" => activity.data["object"],
105 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
106 "attentions" => [],
107 "statusnet_html" => "deleted notice {{tag",
108 "text" => "deleted notice {{tag" ,
109 "is_local" => activity.local,
110 "is_post_verb" => false,
111 "created_at" => created_at,
112 "in_reply_to_status_id" => nil,
113 "external_url" => activity.data["id"],
114 "activity_type" => "delete"
115 }
116 end
117
118 def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
119 created_at = object["published"] |> Utils.date_to_asctime
120 like_count = object["like_count"] || 0
121 announcement_count = object["announcement_count"] || 0
122 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
123 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
124
125 mentions = opts[:mentioned] || []
126
127 attentions = activity.data["to"]
128 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
129 |> Enum.filter(&(&1))
130 |> Enum.map(fn (user) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
131
132 conversation_id = conversation_id(activity)
133
134 tags = activity.data["object"]["tag"] || []
135 possibly_sensitive = Enum.member?(tags, "nsfw")
136
137 %{
138 "id" => activity.id,
139 "uri" => activity.data["object"]["id"],
140 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
141 "statusnet_html" => HtmlSanitizeEx.basic_html(content) |> Formatter.finmojifiy,
142 "text" => HtmlSanitizeEx.strip_tags(content),
143 "is_local" => activity.local,
144 "is_post_verb" => true,
145 "created_at" => created_at,
146 "in_reply_to_status_id" => object["inReplyToStatusId"],
147 "statusnet_conversation_id" => conversation_id,
148 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
149 "attentions" => attentions,
150 "fave_num" => like_count,
151 "repeat_num" => announcement_count,
152 "favorited" => to_boolean(favorited),
153 "repeated" => to_boolean(repeated),
154 "external_url" => object["external_url"],
155 "tags" => tags,
156 "activity_type" => "post",
157 "possibly_sensitive" => possibly_sensitive
158 }
159 end
160
161 def conversation_id(activity) do
162 with context when not is_nil(context) <- activity.data["context"] do
163 TwitterAPI.context_to_conversation_id(context)
164 else _e -> nil
165 end
166 end
167
168 defp to_boolean(false) do
169 false
170 end
171
172 defp to_boolean(nil) do
173 false
174 end
175
176 defp to_boolean(_) do
177 true
178 end
179 end