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" => "Follow"}} = activity} = opts) do
12 user = User.get_cached_by_ap_id(activity.data["actor"])
13 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
14 created_at = created_at |> Utils.date_to_asctime()
16 followed = User.get_cached_by_ap_id(activity.data["object"])
17 text = "#{user.nickname} started following #{followed.nickname}"
21 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
23 "statusnet_html" => text,
25 "is_local" => activity.local,
26 "is_post_verb" => false,
27 "created_at" => created_at,
28 "in_reply_to_status_id" => nil,
29 "external_url" => activity.data["id"],
30 "activity_type" => "follow"
34 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
35 user = User.get_by_ap_id(activity.data["actor"])
36 created_at = activity.data["published"] |> Utils.date_to_asctime()
37 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
39 text = "#{user.nickname} retweeted a status."
41 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
45 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
46 "statusnet_html" => text,
48 "is_local" => activity.local,
49 "is_post_verb" => false,
50 "uri" => "tag:#{activity.data["id"]}:objectType=note",
51 "created_at" => created_at,
52 "retweeted_status" => retweeted_status,
53 "statusnet_conversation_id" => conversation_id(announced_activity),
54 "external_url" => activity.data["id"],
55 "activity_type" => "repeat"
59 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
60 user = User.get_cached_by_ap_id(activity.data["actor"])
61 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
64 activity.data["published"]
65 |> Utils.date_to_asctime()
67 text = "#{user.nickname} favorited a status."
71 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
72 "statusnet_html" => text,
74 "is_local" => activity.local,
75 "is_post_verb" => false,
76 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
77 "created_at" => created_at,
78 "in_reply_to_status_id" => liked_activity.id,
79 "external_url" => activity.data["id"],
80 "activity_type" => "like"
86 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
88 actor = get_in(activity.data, ["actor"])
89 user = User.get_cached_by_ap_id(actor)
91 created_at = object["published"] |> Utils.date_to_asctime()
92 like_count = object["like_count"] || 0
93 announcement_count = object["announcement_count"] || 0
94 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
95 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
99 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
101 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
103 conversation_id = conversation_id(activity)
105 tags = activity.data["object"]["tag"] || []
106 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
108 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
110 summary = activity.data["object"]["summary"]
111 content = object["content"]
114 if !!summary and summary != "" do
115 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
121 HtmlSanitizeEx.basic_html(content)
122 |> Formatter.emojify(object["emoji"])
126 "uri" => activity.data["object"]["id"],
127 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
128 "statusnet_html" => html,
129 "text" => HtmlSanitizeEx.strip_tags(content),
130 "is_local" => activity.local,
131 "is_post_verb" => true,
132 "created_at" => created_at,
133 "in_reply_to_status_id" => object["inReplyToStatusId"],
134 "statusnet_conversation_id" => conversation_id,
135 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
136 "attentions" => attentions,
137 "fave_num" => like_count,
138 "repeat_num" => announcement_count,
139 "favorited" => !!favorited,
140 "repeated" => !!repeated,
141 "external_url" => object["external_url"] || object["id"],
143 "activity_type" => "post",
144 "possibly_sensitive" => possibly_sensitive
148 defp conversation_id(activity) do
149 with context when not is_nil(context) <- activity.data["context"] do
150 TwitterAPI.context_to_conversation_id(context)