affd435771e28d4f792d933b6041cd1bdbe58994
[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 alias Pleroma.Wi
8
9 defp user_by_ap_id(user_list, ap_id) do
10 Enum.find(user_list, fn (%{ap_id: user_id}) -> ap_id == user_id end)
11 end
12
13 def to_map(%Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} = activity,
14 %{users: users, announced_activity: announced_activity} = opts) do
15 user = user_by_ap_id(users, actor)
16 created_at = created_at |> date_to_asctime
17
18 text = "#{user.nickname} retweeted a status."
19
20 announced_user = user_by_ap_id(users, announced_activity.data["actor"])
21 retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
22 %{
23 "id" => activity.id,
24 "user" => UserRepresenter.to_map(user, opts),
25 "statusnet_html" => text,
26 "text" => text,
27 "is_local" => true,
28 "is_post_verb" => false,
29 "uri" => "tag:#{activity.data["id"]}:objectType=note",
30 "created_at" => created_at,
31 "retweeted_status" => retweeted_status,
32 "statusnet_conversation_id" => conversation_id(announced_activity)
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 }
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 }
71 end
72
73 def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
74 created_at = object["published"] |> date_to_asctime
75 like_count = object["like_count"] || 0
76 announcement_count = object["announcement_count"] || 0
77 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
78 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
79
80 mentions = opts[:mentioned] || []
81
82 attentions = activity.data["to"]
83 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
84 |> Enum.filter(&(&1))
85 |> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end)
86
87 conversation_id = conversation_id(activity)
88
89 %{
90 "id" => activity.id,
91 "user" => UserRepresenter.to_map(user, opts),
92 "attentions" => [],
93 "statusnet_html" => content,
94 "text" => HtmlSanitizeEx.strip_tags(content),
95 "is_local" => true,
96 "is_post_verb" => true,
97 "created_at" => created_at,
98 "in_reply_to_status_id" => object["inReplyToStatusId"],
99 "statusnet_conversation_id" => conversation_id,
100 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
101 "attentions" => attentions,
102 "fave_num" => like_count,
103 "repeat_num" => announcement_count,
104 "favorited" => to_boolean(favorited),
105 "repeated" => to_boolean(repeated),
106 }
107 end
108
109 def conversation_id(activity) do
110 with context when not is_nil(context) <- activity.data["context"] do
111 TwitterAPI.context_to_conversation_id(context)
112 else _e -> nil
113 end
114 end
115
116 defp date_to_asctime(date) do
117 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
118 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
119 else _e ->
120 ""
121 end
122 end
123
124 defp to_boolean(false) do
125 false
126 end
127
128 defp to_boolean(nil) do
129 false
130 end
131
132 defp to_boolean(_) do
133 true
134 end
135 end