1 defmodule Pleroma.Web.TwitterAPI.ActivityView do
3 alias Pleroma.Web.CommonAPI.Utils
5 alias Pleroma.Web.TwitterAPI.UserView
6 alias Pleroma.Web.TwitterAPI.ActivityView
7 alias Pleroma.Web.TwitterAPI.TwitterAPI
8 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
10 alias Pleroma.Formatter
12 def render("index.json", opts) do
21 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
22 user = User.get_cached_by_ap_id(activity.data["actor"])
23 created_at = activity.data["published"] |> Utils.date_to_asctime()
27 "uri" => activity.data["object"],
28 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
30 "statusnet_html" => "deleted notice {{tag",
31 "text" => "deleted notice {{tag",
32 "is_local" => activity.local,
33 "is_post_verb" => false,
34 "created_at" => created_at,
35 "in_reply_to_status_id" => nil,
36 "external_url" => activity.data["id"],
37 "activity_type" => "delete"
41 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
42 user = User.get_cached_by_ap_id(activity.data["actor"])
43 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
44 created_at = created_at |> Utils.date_to_asctime()
46 followed = User.get_cached_by_ap_id(activity.data["object"])
47 text = "#{user.nickname} started following #{followed.nickname}"
51 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
53 "statusnet_html" => text,
55 "is_local" => activity.local,
56 "is_post_verb" => false,
57 "created_at" => created_at,
58 "in_reply_to_status_id" => nil,
59 "external_url" => activity.data["id"],
60 "activity_type" => "follow"
64 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
65 user = User.get_by_ap_id(activity.data["actor"])
66 created_at = activity.data["published"] |> Utils.date_to_asctime()
67 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
69 text = "#{user.nickname} retweeted a status."
71 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
75 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
76 "statusnet_html" => text,
78 "is_local" => activity.local,
79 "is_post_verb" => false,
80 "uri" => "tag:#{activity.data["id"]}:objectType=note",
81 "created_at" => created_at,
82 "retweeted_status" => retweeted_status,
83 "statusnet_conversation_id" => conversation_id(announced_activity),
84 "external_url" => activity.data["id"],
85 "activity_type" => "repeat"
89 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
90 user = User.get_cached_by_ap_id(activity.data["actor"])
91 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
94 activity.data["published"]
95 |> Utils.date_to_asctime()
97 text = "#{user.nickname} favorited a status."
101 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
102 "statusnet_html" => text,
104 "is_local" => activity.local,
105 "is_post_verb" => false,
106 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
107 "created_at" => created_at,
108 "in_reply_to_status_id" => liked_activity.id,
109 "external_url" => activity.data["id"],
110 "activity_type" => "like"
116 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
118 actor = get_in(activity.data, ["actor"])
119 user = User.get_cached_by_ap_id(actor)
121 created_at = object["published"] |> Utils.date_to_asctime()
122 like_count = object["like_count"] || 0
123 announcement_count = object["announcement_count"] || 0
124 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
125 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
129 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
131 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
133 conversation_id = conversation_id(activity)
135 tags = activity.data["object"]["tag"] || []
136 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
138 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
140 summary = activity.data["object"]["summary"]
141 content = object["content"]
144 if !!summary and summary != "" do
145 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
151 HtmlSanitizeEx.basic_html(content)
152 |> Formatter.emojify(object["emoji"])
156 "uri" => activity.data["object"]["id"],
157 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
158 "statusnet_html" => html,
159 "text" => HtmlSanitizeEx.strip_tags(content),
160 "is_local" => activity.local,
161 "is_post_verb" => true,
162 "created_at" => created_at,
163 "in_reply_to_status_id" => object["inReplyToStatusId"],
164 "statusnet_conversation_id" => conversation_id,
165 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
166 "attentions" => attentions,
167 "fave_num" => like_count,
168 "repeat_num" => announcement_count,
169 "favorited" => !!favorited,
170 "repeated" => !!repeated,
171 "external_url" => object["external_url"] || object["id"],
173 "activity_type" => "post",
174 "possibly_sensitive" => possibly_sensitive
178 defp conversation_id(activity) do
179 with context when not is_nil(context) <- activity.data["context"] do
180 TwitterAPI.context_to_conversation_id(context)