Parse incoming retweets.
[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.Web.TwitterAPI.TwitterAPI
5 alias Pleroma.Activity
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 conversation_id = with context when not is_nil(context) <- activity.data["context"] do
87 TwitterAPI.context_to_conversation_id(context)
88 else _e -> nil
89 end
90
91 %{
92 "id" => activity.id,
93 "user" => UserRepresenter.to_map(user, opts),
94 "attentions" => [],
95 "statusnet_html" => content,
96 "text" => HtmlSanitizeEx.strip_tags(content),
97 "is_local" => true,
98 "is_post_verb" => true,
99 "created_at" => created_at,
100 "in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"],
101 "statusnet_conversation_id" => conversation_id,
102 "attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
103 "attentions" => attentions,
104 "fave_num" => like_count,
105 "repeat_num" => announcement_count,
106 "favorited" => !!favorited,
107 "repeated" => !!repeated,
108 }
109 end
110
111 defp date_to_asctime(date) do
112 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
113 Calendar.Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
114 else _e ->
115 ""
116 end
117 end
118 end