Add activity_type to twitter api output.
[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: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
100 created_at = object["published"] |> Utils.date_to_asctime
101 like_count = object["like_count"] || 0
102 announcement_count = object["announcement_count"] || 0
103 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
104 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
105
106 mentions = opts[:mentioned] || []
107
108 attentions = activity.data["to"]
109 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
110 |> Enum.filter(&(&1))
111 |> Enum.map(fn (user) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
112
113 conversation_id = conversation_id(activity)
114
115 %{
116 "id" => activity.id,
117 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
118 "statusnet_html" => HtmlSanitizeEx.basic_html(content) |> Formatter.finmojifiy,
119 "text" => HtmlSanitizeEx.strip_tags(content),
120 "is_local" => activity.local,
121 "is_post_verb" => true,
122 "created_at" => created_at,
123 "in_reply_to_status_id" => object["inReplyToStatusId"],
124 "statusnet_conversation_id" => conversation_id,
125 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
126 "attentions" => attentions,
127 "fave_num" => like_count,
128 "repeat_num" => announcement_count,
129 "favorited" => to_boolean(favorited),
130 "repeated" => to_boolean(repeated),
131 "external_url" => object["external_url"],
132 "tags" => activity.data["object"]["tag"] || [],
133 "activity_type" => "post"
134 }
135 end
136
137 def conversation_id(activity) do
138 with context when not is_nil(context) <- activity.data["context"] do
139 TwitterAPI.context_to_conversation_id(context)
140 else _e -> nil
141 end
142 end
143
144 defp to_boolean(false) do
145 false
146 end
147
148 defp to_boolean(nil) do
149 false
150 end
151
152 defp to_boolean(_) do
153 true
154 end
155 end