1 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
2 use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
3 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
4 alias Pleroma.{Activity, User}
5 alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, Utils}
6 alias Pleroma.Formatter
8 defp user_by_ap_id(user_list, ap_id) do
9 Enum.find(user_list, fn (%{ap_id: user_id}) -> ap_id == user_id end)
12 def to_map(%Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} = activity,
13 %{users: users, announced_activity: announced_activity} = opts) do
14 user = user_by_ap_id(users, actor)
15 created_at = created_at |> Utils.date_to_asctime
17 text = "#{user.nickname} retweeted a status."
19 announced_user = user_by_ap_id(users, announced_activity.data["actor"])
20 retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
23 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
24 "statusnet_html" => text,
27 "is_post_verb" => false,
28 "uri" => "tag:#{activity.data["id"]}:objectType=note",
29 "created_at" => created_at,
30 "retweeted_status" => retweeted_status,
31 "statusnet_conversation_id" => conversation_id(announced_activity),
32 "external_url" => activity.data["id"]
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 |> Utils.date_to_asctime
40 text = "#{user.nickname} favorited a status."
44 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
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,
52 "external_url" => activity.data["id"]
56 def to_map(%Activity{data: %{"type" => "Follow", "published" => created_at, "object" => followed_id}} = activity, %{user: user} = opts) do
57 created_at = created_at |> Utils.date_to_asctime
59 followed = User.get_cached_by_ap_id(followed_id)
60 text = "#{user.nickname} started following #{followed.nickname}"
63 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
65 "statusnet_html" => text,
68 "is_post_verb" => false,
69 "created_at" => created_at,
70 "in_reply_to_status_id" => nil,
71 "external_url" => activity.data["id"]
75 def to_map(%Activity{data: %{"object" => %{"content" => content} = object}} = activity, %{user: user} = opts) do
76 created_at = object["published"] |> Utils.date_to_asctime
77 like_count = object["like_count"] || 0
78 announcement_count = object["announcement_count"] || 0
79 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
80 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
82 mentions = opts[:mentioned] || []
84 attentions = activity.data["to"]
85 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
87 |> Enum.map(fn (user) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
89 conversation_id = conversation_id(activity)
93 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
94 "statusnet_html" => HtmlSanitizeEx.basic_html(content) |> Formatter.finmojifiy,
95 "text" => HtmlSanitizeEx.strip_tags(content),
97 "is_post_verb" => true,
98 "created_at" => created_at,
99 "in_reply_to_status_id" => object["inReplyToStatusId"],
100 "statusnet_conversation_id" => conversation_id,
101 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
102 "attentions" => attentions,
103 "fave_num" => like_count,
104 "repeat_num" => announcement_count,
105 "favorited" => to_boolean(favorited),
106 "repeated" => to_boolean(repeated),
107 "external_url" => activity.data["id"],
108 "tags" => activity.data["object"]["tag"] || []
112 def conversation_id(activity) do
113 with context when not is_nil(context) <- activity.data["context"] do
114 TwitterAPI.context_to_conversation_id(context)
119 defp to_boolean(false) do
123 defp to_boolean(nil) do
127 defp to_boolean(_) do