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"])
42 activity.data["published"]
43 |> Utils.date_to_asctime()
45 text = "#{user.nickname} favorited a status."
49 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
50 "statusnet_html" => text,
52 "is_local" => activity.local,
53 "is_post_verb" => false,
54 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
55 "created_at" => created_at,
56 "in_reply_to_status_id" => liked_activity.id,
57 "external_url" => activity.data["id"],
58 "activity_type" => "like"
64 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
66 actor = get_in(activity.data, ["actor"])
67 user = User.get_cached_by_ap_id(actor)
69 created_at = object["published"] |> Utils.date_to_asctime()
70 like_count = object["like_count"] || 0
71 announcement_count = object["announcement_count"] || 0
72 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
73 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
77 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
79 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
81 conversation_id = conversation_id(activity)
83 tags = activity.data["object"]["tag"] || []
84 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
86 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
88 summary = activity.data["object"]["summary"]
89 content = object["content"]
92 if !!summary and summary != "" do
93 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
99 HtmlSanitizeEx.basic_html(content)
100 |> Formatter.emojify(object["emoji"])
104 "uri" => activity.data["object"]["id"],
105 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
106 "statusnet_html" => html,
107 "text" => HtmlSanitizeEx.strip_tags(content),
108 "is_local" => activity.local,
109 "is_post_verb" => true,
110 "created_at" => created_at,
111 "in_reply_to_status_id" => object["inReplyToStatusId"],
112 "statusnet_conversation_id" => conversation_id,
113 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
114 "attentions" => attentions,
115 "fave_num" => like_count,
116 "repeat_num" => announcement_count,
117 "favorited" => !!favorited,
118 "repeated" => !!repeated,
119 "external_url" => object["external_url"] || object["id"],
121 "activity_type" => "post",
122 "possibly_sensitive" => possibly_sensitive
126 defp conversation_id(activity) do
127 with context when not is_nil(context) <- activity.data["context"] do
128 TwitterAPI.context_to_conversation_id(context)