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
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)
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
18 text = "#{user.nickname} retweeted a status."
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))
24 "user" => UserRepresenter.to_map(user, opts),
25 "statusnet_html" => text,
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)
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
40 text = "#{user.nickname} favorited a status."
44 "user" => UserRepresenter.to_map(user, opts),
45 "statusnet_html" => text,
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,
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
58 followed = User.get_cached_by_ap_id(followed_id)
59 text = "#{user.nickname} started following #{followed.nickname}"
62 "user" => UserRepresenter.to_map(user, opts),
64 "statusnet_html" => text,
67 "is_post_verb" => false,
68 "created_at" => created_at,
69 "in_reply_to_status_id" => nil,
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"] || [])
80 mentions = opts[:mentioned] || []
82 attentions = activity.data["to"]
83 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
85 |> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end)
87 conversation_id = conversation_id(activity)
91 "user" => UserRepresenter.to_map(user, opts),
93 "statusnet_html" => content,
94 "text" => HtmlSanitizeEx.strip_tags(content),
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),
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)
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")
124 defp to_boolean(false) do
128 defp to_boolean(nil) do
132 defp to_boolean(_) do