Refactor code to comply with credo suggestions
[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
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 }
31 end
32
33 def to_map(%Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
34 %{user: user, liked_activity: liked_activity} = opts) do
35 created_at = created_at |> date_to_asctime
36
37 text = "#{user.nickname} favorited a status."
38
39 %{
40 "id" => activity.id,
41 "user" => UserRepresenter.to_map(user, opts),
42 "statusnet_html" => text,
43 "text" => text,
44 "is_local" => true,
45 "is_post_verb" => false,
46 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
47 "created_at" => created_at,
48 "in_reply_to_status_id" => liked_activity.id,
49 }
50 end
51
52 def to_map(%Activity{data: %{"type" => "Follow", "published" => created_at, "object" => followed_id}} = activity, %{user: user} = opts) do
53 created_at = created_at |> date_to_asctime
54
55 followed = User.get_cached_by_ap_id(followed_id)
56 text = "#{user.nickname} started following #{followed.nickname}"
57 %{
58 "id" => activity.id,
59 "user" => UserRepresenter.to_map(user, opts),
60 "attentions" => [],
61 "statusnet_html" => text,
62 "text" => text,
63 "is_local" => true,
64 "is_post_verb" => false,
65 "created_at" => created_at,
66 "in_reply_to_status_id" => nil,
67 }
68 end
69
70 def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
71 created_at = object["published"] |> date_to_asctime
72 like_count = object["like_count"] || 0
73 announcement_count = object["announcement_count"] || 0
74 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
75 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
76
77 mentions = opts[:mentioned] || []
78
79 attentions = activity.data["to"]
80 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
81 |> Enum.filter(&(&1))
82 |> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end)
83
84 %{
85 "id" => activity.id,
86 "user" => UserRepresenter.to_map(user, opts),
87 "attentions" => [],
88 "statusnet_html" => content,
89 "text" => HtmlSanitizeEx.strip_tags(content),
90 "is_local" => true,
91 "is_post_verb" => true,
92 "created_at" => created_at,
93 "in_reply_to_status_id" => object["inReplyToStatusId"],
94 "statusnet_conversation_id" => object["statusnetConversationId"],
95 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
96 "attentions" => attentions,
97 "fave_num" => like_count,
98 "repeat_num" => announcement_count,
99 "favorited" => to_boolean(favorited),
100 "repeated" => to_boolean(repeated),
101 }
102 end
103
104 defp date_to_asctime(date) do
105 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
106 Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
107 else _e ->
108 ""
109 end
110 end
111
112 defp to_boolean(false) do
113 false
114 end
115
116 defp to_boolean(nil) do
117 false
118 end
119
120 defp to_boolean(_) do
121 true
122 end
123 end