Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into feature/unfollow...
[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
5
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}} = activity, %{users: users, announced_activity: announced_activity} = opts) do
12 user = user_by_ap_id(users, actor)
13 created_at = get_in(activity.data, ["published"])
14 |> 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"}} = activity, %{user: user, liked_activity: liked_activity} = opts) do
34 created_at = get_in(activity.data, ["published"])
35 |> 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, # TODO: add summary
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"}} = activity, %{user: user} = opts) do
53 created_at = get_in(activity.data, ["published"])
54 |> date_to_asctime
55
56 %{
57 "id" => activity.id,
58 "user" => UserRepresenter.to_map(user, opts),
59 "attentions" => [],
60 "statusnet_html" => "", # TODO: add summary
61 "text" => "",
62 "is_local" => true,
63 "is_post_verb" => false,
64 "created_at" => created_at,
65 "in_reply_to_status_id" => nil,
66 }
67 end
68
69 def to_map(%Activity{} = activity, %{user: user} = opts) do
70 content = get_in(activity.data, ["object", "content"])
71 created_at = get_in(activity.data, ["object", "published"])
72 |> date_to_asctime
73 like_count = get_in(activity.data, ["object", "like_count"]) || 0
74 announcement_count = get_in(activity.data, ["object", "announcement_count"]) || 0
75 favorited = opts[:for] && opts[:for].ap_id in (activity.data["object"]["likes"] || [])
76 repeated = opts[:for] && opts[:for].ap_id in (activity.data["object"]["announcements"] || [])
77
78 mentions = opts[:mentioned] || []
79
80 attentions = activity.data["to"]
81 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
82 |> Enum.filter(&(&1))
83 |> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end)
84
85 %{
86 "id" => activity.id,
87 "user" => UserRepresenter.to_map(user, opts),
88 "attentions" => [],
89 "statusnet_html" => content,
90 "text" => HtmlSanitizeEx.strip_tags(content),
91 "is_local" => true,
92 "is_post_verb" => true,
93 "created_at" => created_at,
94 "in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"],
95 "statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"],
96 "attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
97 "attentions" => attentions,
98 "fave_num" => like_count,
99 "repeat_num" => announcement_count,
100 "favorited" => !!favorited,
101 "repeated" => !!repeated,
102 }
103 end
104
105 defp date_to_asctime(date) do
106 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
107 Calendar.Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
108 else _e ->
109 ""
110 end
111 end
112 end