Strip unsafe html on output in TwAPI.
[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.{UserRepresenter, ObjectRepresenter}
4 alias Pleroma.{Activity, User, Formatter}
5 alias Pleroma.Web.TwitterAPI.TwitterAPI
6
7 defp user_by_ap_id(user_list, ap_id) do
8 Enum.find(user_list, fn (%{ap_id: user_id}) -> ap_id == user_id end)
9 end
10
11 def to_map(%Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} = activity,
12 %{users: users, announced_activity: announced_activity} = opts) do
13 user = user_by_ap_id(users, actor)
14 created_at = created_at |> date_to_asctime
15
16 text = "#{user.nickname} retweeted a status."
17
18 announced_user = user_by_ap_id(users, announced_activity.data["actor"])
19 retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
20 %{
21 "id" => activity.id,
22 "user" => UserRepresenter.to_map(user, opts),
23 "statusnet_html" => text,
24 "text" => text,
25 "is_local" => true,
26 "is_post_verb" => false,
27 "uri" => "tag:#{activity.data["id"]}:objectType=note",
28 "created_at" => created_at,
29 "retweeted_status" => retweeted_status,
30 "statusnet_conversation_id" => conversation_id(announced_activity),
31 "external_url" => activity.data["id"]
32 }
33 end
34
35 def to_map(%Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
36 %{user: user, liked_activity: liked_activity} = opts) do
37 created_at = created_at |> date_to_asctime
38
39 text = "#{user.nickname} favorited a status."
40
41 %{
42 "id" => activity.id,
43 "user" => UserRepresenter.to_map(user, opts),
44 "statusnet_html" => text,
45 "text" => text,
46 "is_local" => true,
47 "is_post_verb" => false,
48 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
49 "created_at" => created_at,
50 "in_reply_to_status_id" => liked_activity.id,
51 "external_url" => activity.data["id"]
52 }
53 end
54
55 def to_map(%Activity{data: %{"type" => "Follow", "published" => created_at, "object" => followed_id}} = activity, %{user: user} = opts) do
56 created_at = created_at |> date_to_asctime
57
58 followed = User.get_cached_by_ap_id(followed_id)
59 text = "#{user.nickname} started following #{followed.nickname}"
60 %{
61 "id" => activity.id,
62 "user" => UserRepresenter.to_map(user, opts),
63 "attentions" => [],
64 "statusnet_html" => text,
65 "text" => text,
66 "is_local" => true,
67 "is_post_verb" => false,
68 "created_at" => created_at,
69 "in_reply_to_status_id" => nil,
70 "external_url" => activity.data["id"]
71 }
72 end
73
74 def content_with_tags(content, tags) do
75 tags = tags || []
76 text_content = HtmlSanitizeEx.strip_tags(content)
77 found_tags = Formatter.parse_tags(text_content)
78 |> Enum.map(fn ({_, tag}) -> tag end)
79
80 missing_tags = tags -- found_tags
81 |> Enum.map(&"##{&1}")
82
83 Enum.join([content | missing_tags], "<br>\n")
84 end
85
86 def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
87 created_at = object["published"] |> date_to_asctime
88 like_count = object["like_count"] || 0
89 announcement_count = object["announcement_count"] || 0
90 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
91 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
92
93 mentions = opts[:mentioned] || []
94
95 attentions = activity.data["to"]
96 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
97 |> Enum.filter(&(&1))
98 |> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end)
99
100 conversation_id = conversation_id(activity)
101
102 content = content_with_tags(content, object["tag"])
103
104 %{
105 "id" => activity.id,
106 "user" => UserRepresenter.to_map(user, opts),
107 "attentions" => [],
108 "statusnet_html" => HtmlSanitizeEx.basic_html(content),
109 "text" => HtmlSanitizeEx.strip_tags(content),
110 "is_local" => true,
111 "is_post_verb" => true,
112 "created_at" => created_at,
113 "in_reply_to_status_id" => object["inReplyToStatusId"],
114 "statusnet_conversation_id" => conversation_id,
115 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
116 "attentions" => attentions,
117 "fave_num" => like_count,
118 "repeat_num" => announcement_count,
119 "favorited" => to_boolean(favorited),
120 "repeated" => to_boolean(repeated),
121 "external_url" => activity.data["id"]
122 }
123 end
124
125 def conversation_id(activity) do
126 with context when not is_nil(context) <- activity.data["context"] do
127 TwitterAPI.context_to_conversation_id(context)
128 else _e -> nil
129 end
130 end
131
132 defp to_boolean(false) do
133 false
134 end
135
136 defp to_boolean(nil) do
137 false
138 end
139
140 defp to_boolean(_) do
141 true
142 end
143 end