Safety measures.
[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}
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 }
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 }
52 end
53
54 def to_map(%Activity{data: %{"type" => "Follow", "published" => created_at, "object" => followed_id}} = activity, %{user: user} = opts) do
55 created_at = created_at |> date_to_asctime
56
57 followed = User.get_cached_by_ap_id(followed_id)
58 text = "#{user.nickname} started following #{followed.nickname}"
59 %{
60 "id" => activity.id,
61 "user" => UserRepresenter.to_map(user, opts),
62 "attentions" => [],
63 "statusnet_html" => text,
64 "text" => text,
65 "is_local" => true,
66 "is_post_verb" => false,
67 "created_at" => created_at,
68 "in_reply_to_status_id" => nil,
69 }
70 end
71
72 def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
73 created_at = object["published"] |> date_to_asctime
74 like_count = object["like_count"] || 0
75 announcement_count = object["announcement_count"] || 0
76 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
77 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
78
79 mentions = opts[:mentioned] || []
80
81 attentions = activity.data["to"]
82 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
83 |> Enum.filter(&(&1))
84 |> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end)
85
86 conversation_id = conversation_id(activity)
87
88 %{
89 "id" => activity.id,
90 "user" => UserRepresenter.to_map(user, opts),
91 "attentions" => [],
92 "statusnet_html" => content,
93 "text" => HtmlSanitizeEx.strip_tags(content),
94 "is_local" => true,
95 "is_post_verb" => true,
96 "created_at" => created_at,
97 "in_reply_to_status_id" => object["inReplyToStatusId"],
98 "statusnet_conversation_id" => conversation_id,
99 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
100 "attentions" => attentions,
101 "fave_num" => like_count,
102 "repeat_num" => announcement_count,
103 "favorited" => to_boolean(favorited),
104 "repeated" => to_boolean(repeated),
105 }
106 end
107
108 def conversation_id(activity) do
109 with context when not is_nil(context) <- activity.data["context"] do
110 TwitterAPI.context_to_conversation_id(context)
111 else _e -> nil
112 end
113 end
114
115 defp date_to_asctime(date) do
116 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
117 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
118 else _e ->
119 ""
120 end
121 end
122
123 defp to_boolean(false) do
124 false
125 end
126
127 defp to_boolean(nil) do
128 false
129 end
130
131 defp to_boolean(_) do
132 true
133 end
134 end