1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 # THIS MODULE IS DEPRECATED! DON'T USE IT!
6 # USE THE Pleroma.Web.TwitterAPI.Views.ActivityView MODULE!
7 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
8 use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
9 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
10 alias Pleroma.{Activity, User}
11 alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView}
12 alias Pleroma.Web.CommonAPI.Utils
13 alias Pleroma.Formatter
16 defp user_by_ap_id(user_list, ap_id) do
17 Enum.find(user_list, fn %{ap_id: user_id} -> ap_id == user_id end)
21 %Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} =
23 %{users: users, announced_activity: announced_activity} = opts
25 user = user_by_ap_id(users, actor)
26 created_at = created_at |> Utils.date_to_asctime()
28 text = "#{user.nickname} retweeted a status."
30 announced_user = user_by_ap_id(users, announced_activity.data["actor"])
31 retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
35 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
36 "statusnet_html" => text,
38 "is_local" => activity.local,
39 "is_post_verb" => false,
40 "uri" => "tag:#{activity.data["id"]}:objectType=note",
41 "created_at" => created_at,
42 "retweeted_status" => retweeted_status,
43 "statusnet_conversation_id" => conversation_id(announced_activity),
44 "external_url" => activity.data["id"],
45 "activity_type" => "repeat"
50 %Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
51 %{user: user, liked_activity: liked_activity} = opts
53 created_at = created_at |> Utils.date_to_asctime()
55 text = "#{user.nickname} favorited a status."
59 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
60 "statusnet_html" => text,
62 "is_local" => activity.local,
63 "is_post_verb" => false,
64 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
65 "created_at" => created_at,
66 "in_reply_to_status_id" => liked_activity.id,
67 "external_url" => activity.data["id"],
68 "activity_type" => "like"
73 %Activity{data: %{"type" => "Follow", "object" => followed_id}} = activity,
76 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
77 created_at = created_at |> Utils.date_to_asctime()
79 followed = User.get_cached_by_ap_id(followed_id)
80 text = "#{user.nickname} started following #{followed.nickname}"
84 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
86 "statusnet_html" => text,
88 "is_local" => activity.local,
89 "is_post_verb" => false,
90 "created_at" => created_at,
91 "in_reply_to_status_id" => nil,
92 "external_url" => activity.data["id"],
93 "activity_type" => "follow"
98 # Make this more proper. Just a placeholder to not break the frontend.
101 data: %{"type" => "Undo", "published" => created_at, "object" => undid_activity}
105 created_at = created_at |> Utils.date_to_asctime()
107 text = "#{user.nickname} undid the action at #{undid_activity["id"]}"
111 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
113 "statusnet_html" => text,
115 "is_local" => activity.local,
116 "is_post_verb" => false,
117 "created_at" => created_at,
118 "in_reply_to_status_id" => nil,
119 "external_url" => activity.data["id"],
120 "activity_type" => "undo"
125 %Activity{data: %{"type" => "Delete", "published" => created_at, "object" => _}} =
129 created_at = created_at |> Utils.date_to_asctime()
133 "uri" => activity.data["object"],
134 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
136 "statusnet_html" => "deleted notice {{tag",
137 "text" => "deleted notice {{tag",
138 "is_local" => activity.local,
139 "is_post_verb" => false,
140 "created_at" => created_at,
141 "in_reply_to_status_id" => nil,
142 "external_url" => activity.data["id"],
143 "activity_type" => "delete"
148 %Activity{data: %{"object" => %{"content" => _content} = object}} = activity,
151 created_at = object["published"] |> Utils.date_to_asctime()
152 like_count = object["like_count"] || 0
153 announcement_count = object["announcement_count"] || 0
154 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
155 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
156 pinned = activity.id in user.info.pinned_activities
158 mentions = opts[:mentioned] || []
162 |> Enum.map(fn ap_id -> Enum.find(mentions, fn user -> ap_id == user.ap_id end) end)
164 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
166 conversation_id = conversation_id(activity)
168 tags = activity.data["object"]["tag"] || []
169 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
171 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
173 {_summary, content} = ActivityView.render_content(object)
176 HTML.filter_tags(content, User.html_filter_policy(opts[:for]))
177 |> Formatter.emojify(object["emoji"])
179 attachments = object["attachment"] || []
181 reply_parent = Activity.get_in_reply_to_activity(activity)
183 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
185 summary = HTML.strip_tags(object["summary"])
189 "uri" => activity.data["object"]["id"],
190 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
191 "statusnet_html" => html,
192 "text" => HTML.strip_tags(content),
193 "is_local" => activity.local,
194 "is_post_verb" => true,
195 "created_at" => created_at,
196 "in_reply_to_status_id" => object["inReplyToStatusId"],
197 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
198 "in_reply_to_profileurl" => User.profile_url(reply_user),
199 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
200 "in_reply_to_user_id" => reply_user && reply_user.id,
201 "statusnet_conversation_id" => conversation_id,
202 "attachments" => attachments |> ObjectRepresenter.enum_to_list(opts),
203 "attentions" => attentions,
204 "fave_num" => like_count,
205 "repeat_num" => announcement_count,
206 "favorited" => to_boolean(favorited),
207 "repeated" => to_boolean(repeated),
209 "external_url" => object["external_url"] || object["id"],
211 "activity_type" => "post",
212 "possibly_sensitive" => possibly_sensitive,
213 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
214 "summary" => summary,
215 "summary_html" => summary |> Formatter.emojify(object["emoji"])
219 def conversation_id(activity) do
220 with context when not is_nil(context) <- activity.data["context"] do
221 TwitterAPI.context_to_conversation_id(context)
227 defp to_boolean(false) do
231 defp to_boolean(nil) do
235 defp to_boolean(_) do