Add invisible tags to TwAPI 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.{UserRepresenter, ObjectRepresenter}
4 alias Pleroma.{Activity, User, Formatter}
5 alias Calendar.Strftime
6 alias Pleroma.Web.TwitterAPI.TwitterAPI
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 |> 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" => UserRepresenter.to_map(user, opts),
24 "statusnet_html" => text,
25 "text" => text,
26 "is_local" => true,
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 |> date_to_asctime
39
40 text = "#{user.nickname} favorited a status."
41
42 %{
43 "id" => activity.id,
44 "user" => UserRepresenter.to_map(user, opts),
45 "statusnet_html" => text,
46 "text" => text,
47 "is_local" => true,
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 |> 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" => UserRepresenter.to_map(user, opts),
64 "attentions" => [],
65 "statusnet_html" => text,
66 "text" => text,
67 "is_local" => true,
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 def content_with_tags(content, tags) do
76 tags = tags || []
77 text_content = HtmlSanitizeEx.strip_tags(content)
78 found_tags = Formatter.parse_tags(text_content)
79 |> Enum.map(fn ({_, tag}) -> tag end)
80
81 missing_tags = tags -- found_tags
82 |> Enum.map(&"##{&1}")
83
84 Enum.join([content | missing_tags], "<br>\n")
85 end
86
87 def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
88 created_at = object["published"] |> date_to_asctime
89 like_count = object["like_count"] || 0
90 announcement_count = object["announcement_count"] || 0
91 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
92 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
93
94 mentions = opts[:mentioned] || []
95
96 attentions = activity.data["to"]
97 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
98 |> Enum.filter(&(&1))
99 |> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end)
100
101 conversation_id = conversation_id(activity)
102
103 content = content_with_tags(content, object["tag"])
104
105 %{
106 "id" => activity.id,
107 "user" => UserRepresenter.to_map(user, opts),
108 "attentions" => [],
109 "statusnet_html" => content,
110 "text" => HtmlSanitizeEx.strip_tags(content),
111 "is_local" => true,
112 "is_post_verb" => true,
113 "created_at" => created_at,
114 "in_reply_to_status_id" => object["inReplyToStatusId"],
115 "statusnet_conversation_id" => conversation_id,
116 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
117 "attentions" => attentions,
118 "fave_num" => like_count,
119 "repeat_num" => announcement_count,
120 "favorited" => to_boolean(favorited),
121 "repeated" => to_boolean(repeated),
122 "external_url" => activity.data["id"]
123 }
124 end
125
126 def conversation_id(activity) do
127 with context when not is_nil(context) <- activity.data["context"] do
128 TwitterAPI.context_to_conversation_id(context)
129 else _e -> nil
130 end
131 end
132
133 defp date_to_asctime(date) do
134 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
135 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
136 else _e ->
137 ""
138 end
139 end
140
141 defp to_boolean(false) do
142 false
143 end
144
145 defp to_boolean(nil) do
146 false
147 end
148
149 defp to_boolean(_) do
150 true
151 end
152 end