Fix undo activity.
[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 }
34 end
35
36 def to_map(%Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
37 %{user: user, liked_activity: liked_activity} = opts) do
38 created_at = created_at |> Utils.date_to_asctime
39
40 text = "#{user.nickname} favorited a status."
41
42 %{
43 "id" => activity.id,
44 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
45 "statusnet_html" => text,
46 "text" => text,
47 "is_local" => activity.local,
48 "is_post_verb" => false,
49 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
50 "created_at" => created_at,
51 "in_reply_to_status_id" => liked_activity.id,
52 "external_url" => activity.data["id"]
53 }
54 end
55
56 def to_map(%Activity{data: %{"type" => "Follow", "published" => created_at, "object" => followed_id}} = activity, %{user: user} = opts) do
57 created_at = created_at |> Utils.date_to_asctime
58
59 followed = User.get_cached_by_ap_id(followed_id)
60 text = "#{user.nickname} started following #{followed.nickname}"
61 %{
62 "id" => activity.id,
63 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
64 "attentions" => [],
65 "statusnet_html" => text,
66 "text" => text,
67 "is_local" => activity.local,
68 "is_post_verb" => false,
69 "created_at" => created_at,
70 "in_reply_to_status_id" => nil,
71 "external_url" => activity.data["id"]
72 }
73 end
74
75 # TODO:
76 # Make this more proper. Just a placeholder to not break the frontend.
77 def to_map(%Activity{data: %{"type" => "Undo", "published" => created_at, "object" => undid_activity }} = activity, %{user: user} = opts) do
78 created_at = created_at |> Utils.date_to_asctime
79
80 text = "#{user.nickname} undid the action at #{undid_activity}"
81 %{
82 "id" => activity.id,
83 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
84 "attentions" => [],
85 "statusnet_html" => text,
86 "text" => text,
87 "is_local" => activity.local,
88 "is_post_verb" => false,
89 "created_at" => created_at,
90 "in_reply_to_status_id" => nil,
91 "external_url" => activity.data["id"]
92 }
93 end
94
95 def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
96 created_at = object["published"] |> Utils.date_to_asctime
97 like_count = object["like_count"] || 0
98 announcement_count = object["announcement_count"] || 0
99 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
100 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
101
102 mentions = opts[:mentioned] || []
103
104 attentions = activity.data["to"]
105 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
106 |> Enum.filter(&(&1))
107 |> Enum.map(fn (user) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
108
109 conversation_id = conversation_id(activity)
110
111 %{
112 "id" => activity.id,
113 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
114 "statusnet_html" => HtmlSanitizeEx.basic_html(content) |> Formatter.finmojifiy,
115 "text" => HtmlSanitizeEx.strip_tags(content),
116 "is_local" => activity.local,
117 "is_post_verb" => true,
118 "created_at" => created_at,
119 "in_reply_to_status_id" => object["inReplyToStatusId"],
120 "statusnet_conversation_id" => conversation_id,
121 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
122 "attentions" => attentions,
123 "fave_num" => like_count,
124 "repeat_num" => announcement_count,
125 "favorited" => to_boolean(favorited),
126 "repeated" => to_boolean(repeated),
127 "external_url" => object["external_url"],
128 "tags" => activity.data["object"]["tag"] || []
129 }
130 end
131
132 def conversation_id(activity) do
133 with context when not is_nil(context) <- activity.data["context"] do
134 TwitterAPI.context_to_conversation_id(context)
135 else _e -> nil
136 end
137 end
138
139 defp to_boolean(false) do
140 false
141 end
142
143 defp to_boolean(nil) do
144 false
145 end
146
147 defp to_boolean(_) do
148 true
149 end
150 end