3e69af3e382967b8fbad822a0a0c22d025b24640
[akkoma] / lib / pleroma / web / twitter_api / views / activity_view.ex
1 defmodule Pleroma.Web.TwitterAPI.ActivityView do
2 use Pleroma.Web, :view
3 alias Pleroma.Web.CommonAPI.Utils
4 alias Pleroma.User
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
9 alias Pleroma.Activity
10 alias Pleroma.Formatter
11
12 def render("index.json", opts) do
13 render_many(
14 opts.activities,
15 ActivityView,
16 "activity.json",
17 opts
18 )
19 end
20
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()
24
25 %{
26 "id" => activity.id,
27 "uri" => activity.data["object"],
28 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
29 "attentions" => [],
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"
38 }
39 end
40
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()
45
46 followed = User.get_cached_by_ap_id(activity.data["object"])
47 text = "#{user.nickname} started following #{followed.nickname}"
48
49 %{
50 "id" => activity.id,
51 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
52 "attentions" => [],
53 "statusnet_html" => text,
54 "text" => 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"
61 }
62 end
63
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"])
68
69 text = "#{user.nickname} retweeted a status."
70
71 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
72
73 %{
74 "id" => activity.id,
75 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
76 "statusnet_html" => text,
77 "text" => 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"
86 }
87 end
88
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"])
92
93 created_at =
94 activity.data["published"]
95 |> Utils.date_to_asctime()
96
97 text = "#{user.nickname} favorited a status."
98
99 %{
100 "id" => activity.id,
101 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
102 "statusnet_html" => text,
103 "text" => 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"
111 }
112 end
113
114 def render(
115 "activity.json",
116 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
117 ) do
118 actor = get_in(activity.data, ["actor"])
119 user = User.get_cached_by_ap_id(actor)
120
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"] || [])
126
127 attentions =
128 activity.recipients
129 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
130 |> Enum.filter(& &1)
131 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
132
133 conversation_id = conversation_id(activity)
134
135 tags = activity.data["object"]["tag"] || []
136 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
137
138 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
139
140 summary = activity.data["object"]["summary"]
141 content = object["content"]
142
143 content =
144 if !!summary and summary != "" do
145 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
146 else
147 content
148 end
149
150 html =
151 HtmlSanitizeEx.basic_html(content)
152 |> Formatter.emojify(object["emoji"])
153
154 %{
155 "id" => activity.id,
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"],
172 "tags" => tags,
173 "activity_type" => "post",
174 "possibly_sensitive" => possibly_sensitive
175 }
176 end
177
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)
181 else
182 _e -> nil
183 end
184 end
185 end