1 defmodule Pleroma.Web.TwitterAPI.ActivityView do
3 alias Pleroma.Web.CommonAPI.Utils
5 alias Pleroma.Web.TwitterAPI.UserView
6 alias Pleroma.Web.TwitterAPI.TwitterAPI
7 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
9 alias Pleroma.Formatter
11 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
12 user = User.get_by_ap_id(activity.data["actor"])
13 created_at = activity.data["published"] |> Utils.date_to_asctime
14 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
16 text = "#{user.nickname} retweeted a status."
18 # retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
19 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
23 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
24 "statusnet_html" => text,
26 "is_local" => activity.local,
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"],
33 "activity_type" => "repeat"
37 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
38 user = User.get_cached_by_ap_id(activity.data["actor"])
39 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
40 created_at = activity.data["published"]
41 |> Utils.date_to_asctime
43 text = "#{user.nickname} favorited a status."
47 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
48 "statusnet_html" => text,
50 "is_local" => activity.local,
51 "is_post_verb" => false,
52 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
53 "created_at" => created_at,
54 "in_reply_to_status_id" => liked_activity.id,
55 "external_url" => activity.data["id"],
56 "activity_type" => "like"
60 def render("activity.json", %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts) do
61 actor = get_in(activity.data, ["actor"])
62 user = User.get_cached_by_ap_id(actor)
64 created_at = object["published"] |> Utils.date_to_asctime
65 like_count = object["like_count"] || 0
66 announcement_count = object["announcement_count"] || 0
67 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
68 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
70 attentions = activity.recipients
71 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
73 |> Enum.map(fn (user) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
75 conversation_id = conversation_id(activity)
77 tags = activity.data["object"]["tag"] || []
78 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
80 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
82 summary = activity.data["object"]["summary"]
83 content = object["content"]
84 content = if !!summary and summary != "" do
85 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
90 html = HtmlSanitizeEx.basic_html(content)
91 |> Formatter.emojify(object["emoji"])
95 "uri" => activity.data["object"]["id"],
96 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
97 "statusnet_html" => html,
98 "text" => HtmlSanitizeEx.strip_tags(content),
99 "is_local" => activity.local,
100 "is_post_verb" => true,
101 "created_at" => created_at,
102 "in_reply_to_status_id" => object["inReplyToStatusId"],
103 "statusnet_conversation_id" => conversation_id,
104 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
105 "attentions" => attentions,
106 "fave_num" => like_count,
107 "repeat_num" => announcement_count,
108 "favorited" => !!favorited,
109 "repeated" => !!repeated,
110 "external_url" => object["external_url"] || object["id"],
112 "activity_type" => "post",
113 "possibly_sensitive" => possibly_sensitive
117 defp conversation_id(activity) do
118 with context when not is_nil(context) <- activity.data["context"] do
119 TwitterAPI.context_to_conversation_id(context)